Skip to content

Commit 8770c8a

Browse files
committed
Merge branch '5929-sdk-togglemodule-only-works-locally' into 'master'
fix(SDK): repair ToggleModule See merge request codingame/game-engine!205
2 parents 227ea06 + 90cb2d7 commit 8770c8a

File tree

4 files changed

+76
-28
lines changed

4 files changed

+76
-28
lines changed

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

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import { api as entityModule } from '../../entity-module/GraphicEntityModule.js'
2-
import { toggles } from '../../config.js'
3-
import {ErrorLog} from '../core/ErrorLog.js'
4-
import {MissingToggleError} from './errors/MissingToggleError.js'
1+
import { api as entityModule } from '../entity-module/GraphicEntityModule.js'
2+
import { ErrorLog } from '../core/ErrorLog.js'
3+
import { MissingToggleError } from './errors/MissingToggleError.js'
4+
import { DuplicateToggleValueError } from './errors/DuplicateToggleValueError.js'
55

66
export class ToggleModule {
77
constructor (assets) {
88
this.previousFrame = {}
99
this.missingToggles = {}
10+
1011
ToggleModule.refreshContent = () => {
1112
if (!this.currentFrame) {
1213
return
1314
}
1415
for (const registeredEntity in this.currentFrame.registered) {
1516
const entity = entityModule.entities.get(parseInt(registeredEntity))
1617
const toggleInfo = this.currentFrame.registered[registeredEntity]
17-
const toggleState = toggles[toggleInfo.name]
18+
const toggleState = ToggleModule.toggles[toggleInfo.name]
1819

1920
if (toggleState == null && !this.missingToggles[toggleInfo.name]) {
2021
ErrorLog.push(new MissingToggleError(toggleInfo.name))
@@ -25,9 +26,24 @@ export class ToggleModule {
2526
)
2627
}
2728
}
29+
30+
pushDuplicateErrors()
2831
}
32+
2933
static refreshContent () {}
3034

35+
static defineToggle (option) {
36+
checkDuplicates(option)
37+
38+
ToggleModule.toggles[option.toggle] = option.default
39+
option.get = () => ToggleModule.toggles[option.toggle]
40+
option.set = (value) => {
41+
ToggleModule.toggles[option.toggle] = value
42+
ToggleModule.refreshContent()
43+
}
44+
return option
45+
}
46+
3147
static get name () {
3248
return 'toggles'
3349
}
@@ -53,3 +69,32 @@ export class ToggleModule {
5369
ToggleModule.refreshContent()
5470
}
5571
}
72+
73+
ToggleModule.toggles = {}
74+
ToggleModule.optionValues = {}
75+
76+
function checkDuplicates (option) {
77+
ToggleModule.optionValues[option.toggle] = []
78+
79+
for (const key in option.values) {
80+
const value = option.values[key]
81+
let matchedPair = ToggleModule.optionValues[option.toggle].find(elem => elem.value === value)
82+
83+
if (!matchedPair) {
84+
matchedPair = { keys: [ ], value }
85+
ToggleModule.optionValues[option.toggle].push(matchedPair)
86+
}
87+
88+
matchedPair.keys.push(key)
89+
}
90+
}
91+
92+
function pushDuplicateErrors () {
93+
for (const toggle in ToggleModule.optionValues) {
94+
for (const optionValues of ToggleModule.optionValues[toggle]) {
95+
if (optionValues.keys.length > 1) {
96+
ErrorLog.push(new DuplicateToggleValueError(toggle, optionValues))
97+
}
98+
}
99+
}
100+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export class DuplicateToggleValueError extends Error {
2+
constructor (toggle, dup, cause) {
3+
super('(' + dup.keys.toString() + ') have the same value \'' + dup.value + '\' in toggle "' + toggle + '". Avoid those duplicates in your config.js.')
4+
this.toggle = toggle
5+
this.cause = cause
6+
this.name = 'DuplicateToggleValueError'
7+
}
8+
}

playground/extensions/extensions-toggle.md

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,25 @@ export const modules = [
2929

3030
// The list of toggles displayed in the options of the viewer
3131
export const options = [
32-
{
33-
// The name displayed over the toggle
32+
ToggleModule.defineToggle({
33+
// The name of the toggle
34+
// replace "myToggle" by the name of the toggle you want to use
35+
toggle: 'myToggle',
36+
// The text displayed over the toggle
3437
title: 'MY TOGGLE',
35-
// Getters and setters for the on/off state of your toggle
36-
get: function () {
37-
return toggles.myToggle // replace "myToggle" by the name of the toggle you want to use
38-
},
39-
set: function (value) {
40-
toggles.myToggle = value // replace "myToggle" by the name of the toggle you want to use
41-
ToggleModule.refreshContent()
42-
},
4338
// The labels for the on/off states of your toggle
4439
values: {
4540
'TOGGLED ON': true,
4641
'TOGGLED OFF': false
47-
}
48-
},
49-
{
50-
title: 'MY OTHER TOGGLE',
42+
},
43+
// Default value of your toggle
44+
default: true
45+
}),
46+
ToggleModule.defineToggle({
47+
toggle: 'myOtherToggle',
5148
52-
}
49+
})
5350
]
54-
55-
// The list of the toggles used by the ToggleModule
56-
// replace "myToggle" by the name of the toggle you want to use
57-
export const toggles = {
58-
myToggle: true, // default value of your toggle
59-
myOtherToggle: false
60-
}
61-
6251
```
6352

6453
## Usage

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

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

33
The CodinGame SDK is regularly updated and improved. This document lets you know what changed in the latest releases.
44

5+
## Next Version
6+
7+
### 🐞 Bug fix
8+
9+
- Fixed ToggleModule and improved the API.
10+
511
## 3.4.6
612

713
### 🐞 Bug fix

0 commit comments

Comments
 (0)