Skip to content

Commit aa5147c

Browse files
committed
fix(SDK): repair ToggleModule
1 parent 227ea06 commit aa5147c

File tree

4 files changed

+52
-27
lines changed

4 files changed

+52
-27
lines changed

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

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

66
export class ToggleModule {
7+
78
constructor (assets) {
89
this.previousFrame = {}
910
this.missingToggles = {}
11+
1012
ToggleModule.refreshContent = () => {
11-
if (!this.currentFrame) {
13+
if (!this.currentFrame || !ToggleModule.toggles) {
1214
return
1315
}
1416
for (const registeredEntity in this.currentFrame.registered) {
1517
const entity = entityModule.entities.get(parseInt(registeredEntity))
1618
const toggleInfo = this.currentFrame.registered[registeredEntity]
17-
const toggleState = toggles[toggleInfo.name]
18-
19+
const toggleState = ToggleModule.toggles[toggleInfo.name]
20+
1921
if (toggleState == null && !this.missingToggles[toggleInfo.name]) {
2022
ErrorLog.push(new MissingToggleError(toggleInfo.name))
2123
this.missingToggles[toggleInfo.name] = true
@@ -25,9 +27,29 @@ export class ToggleModule {
2527
)
2628
}
2729
}
30+
31+
for (const e of ToggleModule.errors) {
32+
ErrorLog.push(e)
33+
}
34+
2835
}
36+
2937
static refreshContent () {}
3038

39+
static defineToggle(option) {
40+
if (new Set(Object.values(option.values)).size != Object.values(option.values).length) {
41+
ToggleModule.errors.push(new DuplicateToggleValueError(option.toggle))
42+
}
43+
44+
ToggleModule.toggles[option.toggle] = option.default
45+
option.get = () => ToggleModule.toggles[option.toggle]
46+
option.set = (value) => {
47+
ToggleModule.toggles[option.toggle] = value
48+
ToggleModule.refreshContent()
49+
}
50+
return option
51+
}
52+
3153
static get name () {
3254
return 'toggles'
3355
}
@@ -53,3 +75,6 @@ export class ToggleModule {
5375
ToggleModule.refreshContent()
5476
}
5577
}
78+
79+
ToggleModule.toggles = {}
80+
ToggleModule.errors = []
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, cause) {
3+
super('Duplicate values in toggle "' + toggle + '". Make sure it is set up correctly 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: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,22 @@ 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+
toggle: 'myToggle',
35+
// The text displayed over the toggle
3436
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-
},
4337
// The labels for the on/off states of your toggle
4438
values: {
4539
'TOGGLED ON': true,
4640
'TOGGLED OFF': false
4741
}
48-
},
49-
{
50-
title: 'MY OTHER TOGGLE',
42+
}),
43+
ToggleModule.defineToggle({
44+
toggle: 'myOtherToggle',
5145
52-
}
46+
})
5347
]
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-
6248
```
6349

6450
## 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 added a better API.
10+
511
## 3.4.6
612

713
### 🐞 Bug fix

0 commit comments

Comments
 (0)