Skip to content

Commit dcead32

Browse files
Merge branch 'eulerscheZahl-TinyToggleModule' into 'master'
[FIX][SDK] ToggleModule serialised data is now smaller See merge request codingame/game-engine!284
2 parents 3934d0c + e5f35da commit dcead32

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

engine/modules/toggle/src/main/java/com/codingame/gameengine/module/toggle/ToggleModule.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.HashMap;
44
import java.util.Map;
5+
import java.util.Map.Entry;
56

67
import com.codingame.gameengine.core.AbstractPlayer;
78
import com.codingame.gameengine.core.GameManager;
@@ -13,9 +14,9 @@
1314

1415
/**
1516
* @author Jean Porée
16-
*
17+
*
1718
* This module allows you to display or hide elements of the GraphicEntityModule using the viewer's options menu.
18-
*
19+
*
1920
*/
2021
@Singleton
2122
public class ToggleModule implements Module {
@@ -67,20 +68,29 @@ public void onAfterGameTurn() {
6768
}
6869

6970
@Override
70-
public void onAfterOnEnd() {}
71+
public void onAfterOnEnd() {
72+
}
7173

7274
private void sendFrameData() {
73-
Object[] data = { newRegistration };
74-
gameManager.setViewData("toggles", data);
75-
76-
newRegistration.clear();
75+
if (newRegistration.size() > 0) {
76+
Map<String, String> data = new HashMap<>();
77+
newRegistration.forEach((entityId, toggle) -> {
78+
data.put(toggle.name, data.getOrDefault(toggle.name, "") + entityId + (toggle.state ? "+" : "-"));
79+
});
80+
gameManager.setViewData("toggles", data);
81+
newRegistration.clear();
82+
}
7783
}
84+
7885
/**
7986
* Will display the entity only when the toggle state matches the state you set
80-
*
81-
* @param entity which will be displayed
82-
* @param toggle the name of the toggle you want to use
83-
* @param state the state of the toggle where the entity will be displayed at
87+
*
88+
* @param entity
89+
* which will be displayed
90+
* @param toggle
91+
* the name of the toggle you want to use
92+
* @param state
93+
* the state of the toggle where the entity will be displayed at
8494
*/
8595
public void displayOnToggleState(Entity<?> entity, String toggle, boolean state) {
8696
int id = entity.getId();
@@ -90,4 +100,4 @@ public void displayOnToggleState(Entity<?> entity, String toggle, boolean state)
90100
registered.put(id, associatedToggle);
91101
}
92102
}
93-
}
103+
}

engine/modules/toggle/src/main/resources/view/toggle-module/ToggleModule.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,19 @@ export class ToggleModule {
5555
}
5656

5757
handleFrameData (frameInfo, data) {
58-
if (!data) {
59-
return
58+
const newRegistration = {}
59+
if (data) {
60+
Object.entries(data).forEach(([key, value]) => {
61+
value.match(/\d+./g).forEach(m => {
62+
const entityId = m.slice(0, -1)
63+
const state = m.slice(-1) === '+'
64+
newRegistration[entityId] = {
65+
name: key,
66+
state: state
67+
}
68+
})
69+
})
6070
}
61-
const newRegistration = data[0]
6271
const registered = { ...this.previousFrame.registered, ...newRegistration }
6372
const frame = { registered, number: frameInfo.number }
6473
this.previousFrame = frame
@@ -81,7 +90,7 @@ function checkDuplicates (option) {
8190
let matchedPair = ToggleModule.optionValues[option.toggle].find(elem => elem.value === value)
8291

8392
if (!matchedPair) {
84-
matchedPair = { keys: [ ], value }
93+
matchedPair = { keys: [], value }
8594
ToggleModule.optionValues[option.toggle].push(matchedPair)
8695
}
8796

playground/misc/misc-3-release-notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The CodinGame SDK is regularly updated and improved. This document lets you know
77
### 🐞 Bug fix
88

99
- The statement preview page now presents HTML as it would show in the CodinGame IDE
10+
- Fixed `ToggleModule` taking up a lot of characters in the game result from the allowed quota.
1011

1112
## 3.15.2
1213

0 commit comments

Comments
 (0)