Skip to content

Commit 99f51e9

Browse files
Update DevtoolZoneUtils for restructure & BaseZonePreferences
1 parent 74d249f commit 99f51e9

File tree

2 files changed

+84
-98
lines changed

2 files changed

+84
-98
lines changed

fission/src/systems/preferences/PreferenceTypes.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,26 +147,26 @@ export type Alliance = "red" | "blue"
147147

148148
export type Station = 1 | 2 | 3
149149

150-
export type ScoringZonePreferences = {
150+
/**
151+
* Base properties shared by all zone types
152+
*/
153+
export type BaseZonePreferences = {
151154
name: string
152155
alliance: Alliance
153156
parentNode: string | undefined
157+
deltaTransformation: number[]
158+
}
159+
160+
export type ScoringZonePreferences = BaseZonePreferences & {
154161
points: number
155162
destroyGamepiece: boolean
156163
persistentPoints: boolean
157-
158-
deltaTransformation: number[]
159164
}
160165

161-
export type ProtectedZonePreferences = {
162-
name: string
163-
alliance: Alliance
166+
export type ProtectedZonePreferences = BaseZonePreferences & {
164167
penaltyPoints: number
165-
parentNode: string | undefined
166168
contactType: ContactType
167169
activeDuring: MatchModeType[]
168-
169-
deltaTransformation: number[]
170170
}
171171

172172
export type SpawnLocation = Readonly<{
Lines changed: 75 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
import FieldMiraEditor from "@/mirabuf/FieldMiraEditor"
22
import MirabufCachingService, { MiraType } from "@/mirabuf/MirabufLoader"
33
import PreferencesSystem from "@/systems/preferences/PreferencesSystem"
4-
import type { ProtectedZonePreferences, ScoringZonePreferences } from "@/systems/preferences/PreferenceTypes"
4+
import type { BaseZonePreferences, ProtectedZonePreferences, ScoringZonePreferences } from "@/systems/preferences/PreferenceTypes"
55
import World from "@/systems/World"
66

77
export type ZoneType = "scoring" | "protected"
88

9+
/**
10+
* Checks if two zones are equal by comparing their common base properties
11+
*/
12+
function zonesEqual(zone1: BaseZonePreferences, zone2: BaseZonePreferences): boolean {
13+
return (
14+
zone1.name === zone2.name &&
15+
zone1.alliance === zone2.alliance &&
16+
zone1.parentNode === zone2.parentNode &&
17+
JSON.stringify(zone1.deltaTransformation) === JSON.stringify(zone2.deltaTransformation)
18+
)
19+
}
20+
921
/**
1022
* Checks if a zone was originally defined in the field file by comparing it with the cached field data.
1123
*/
1224
export function isZoneFromDevtools(
13-
zone: ScoringZonePreferences | ProtectedZonePreferences,
25+
zone: BaseZonePreferences,
1426
zoneType: ZoneType
1527
): boolean {
1628
const field = World.sceneRenderer.mirabufSceneObjects.getField()
@@ -21,22 +33,14 @@ export function isZoneFromDevtools(
2133

2234
const editor = new FieldMiraEditor(parts)
2335

24-
if (zoneType === "scoring") {
25-
const devtoolZones = editor.getUserData("devtool:scoring_zones") as ScoringZonePreferences[] | undefined
26-
if (!devtoolZones) return false
27-
28-
return devtoolZones.some(
29-
devZone =>
30-
devZone.name === zone.name &&
31-
devZone.alliance === zone.alliance &&
32-
devZone.parentNode === zone.parentNode &&
33-
JSON.stringify(devZone.deltaTransformation) === JSON.stringify(zone.deltaTransformation)
34-
)
35-
} else {
36-
// For protected zones, we'd need to add field file support first
37-
// For now, return false as protected zones don't have field file support yet
36+
if (zoneType === "protected") {
3837
return false
3938
}
39+
40+
const devtoolZones = editor.getUserData("devtool:scoring_zones") as ScoringZonePreferences[] | undefined
41+
if (!devtoolZones) return false
42+
43+
return devtoolZones.some(devZone => zonesEqual(devZone, zone))
4044
}
4145

4246
/**
@@ -53,47 +57,34 @@ export async function removeZoneFromDevtools(
5357
if (!parts) throw new Error("No field parts found")
5458

5559
const editor = new FieldMiraEditor(parts)
60+
61+
if (zoneType === "protected") {
62+
throw new Error("Protected zone field file removal not yet implemented")
63+
}
64+
65+
const devtoolZones = editor.getUserData("devtool:scoring_zones") as ScoringZonePreferences[] | undefined
66+
if (!devtoolZones) return
5667

57-
if (zoneType === "scoring") {
58-
const devtoolZones = editor.getUserData("devtool:scoring_zones") as ScoringZonePreferences[] | undefined
59-
if (!devtoolZones) return
60-
61-
// Remove the zone from field file data
62-
const filteredZones = devtoolZones.filter(
63-
devZone =>
64-
!(
65-
devZone.name === zone.name &&
66-
devZone.alliance === zone.alliance &&
67-
devZone.parentNode === zone.parentNode &&
68-
JSON.stringify(devZone.deltaTransformation) === JSON.stringify(zone.deltaTransformation)
69-
)
70-
)
71-
72-
// Update the field file data
73-
if (filteredZones.length === 0) {
74-
editor.removeUserData("devtool:scoring_zones")
75-
} else {
76-
editor.setUserData("devtool:scoring_zones", filteredZones)
77-
}
68+
const filteredZones = devtoolZones.filter(devZone => !zonesEqual(devZone, zone))
69+
if (filteredZones.length === 0) {
70+
editor.removeUserData("devtool:scoring_zones")
71+
} else {
72+
editor.setUserData("devtool:scoring_zones", filteredZones)
73+
}
7874

79-
// Update field preferences to match the filtered field file data
80-
if (field.fieldPreferences) {
81-
field.fieldPreferences.scoringZones = filteredZones
82-
PreferencesSystem.savePreferences?.()
83-
field.updateScoringZones()
84-
}
75+
if (field.fieldPreferences) {
76+
field.fieldPreferences.scoringZones = filteredZones
77+
PreferencesSystem.savePreferences?.()
78+
field.updateScoringZones()
79+
}
8580

86-
// Persist changes to cache
87-
const assembly = field.mirabufInstance.parser.assembly
88-
const cacheId = field.cacheId
89-
if (cacheId) {
90-
const success = await MirabufCachingService.persistDevtoolChanges(cacheId, MiraType.FIELD, assembly)
91-
if (!success) {
92-
throw new Error("Failed to persist changes to cache")
93-
}
81+
const assembly = field.mirabufInstance.parser.assembly
82+
const cacheId = field.cacheId
83+
if (cacheId) {
84+
const success = await MirabufCachingService.persistDevtoolChanges(cacheId, MiraType.FIELD, assembly)
85+
if (!success) {
86+
throw new Error("Failed to persist changes to cache")
9487
}
95-
} else {
96-
throw new Error("Protected zone field file removal not yet implemented")
9788
}
9889
}
9990

@@ -112,42 +103,37 @@ export async function modifyZoneInDevtools(
112103
if (!parts) throw new Error("No field parts found")
113104

114105
const editor = new FieldMiraEditor(parts)
115-
116-
if (zoneType === "scoring") {
117-
const devtoolZones = editor.getUserData("devtool:scoring_zones") as ScoringZonePreferences[] | undefined
118-
if (!devtoolZones) return
119-
120-
// Find and replace the zone in field file data
121-
const updatedZones = devtoolZones.map(devZone => {
122-
if (
123-
devZone.name === originalZone.name &&
124-
devZone.alliance === originalZone.alliance &&
125-
devZone.parentNode === originalZone.parentNode &&
126-
JSON.stringify(devZone.deltaTransformation) === JSON.stringify(originalZone.deltaTransformation)
127-
) {
128-
return modifiedZone as ScoringZonePreferences
129-
}
130-
return devZone
131-
})
132-
133-
editor.setUserData("devtool:scoring_zones", updatedZones)
134-
135-
if (field.fieldPreferences) {
136-
field.fieldPreferences.scoringZones = updatedZones
137-
PreferencesSystem.savePreferences?.()
138-
field.updateScoringZones()
106+
107+
if (zoneType === "protected") {
108+
throw new Error("Protected zone field file modification not yet implemented")
109+
}
110+
111+
const devtoolZones = editor.getUserData("devtool:scoring_zones") as ScoringZonePreferences[] | undefined
112+
if (!devtoolZones) return
113+
114+
// Find and replace the zone in field file data
115+
const updatedZones = devtoolZones.map(devZone => {
116+
if (zonesEqual(devZone, originalZone)) {
117+
return modifiedZone
139118
}
119+
return devZone
120+
})
121+
122+
editor.setUserData("devtool:scoring_zones", updatedZones as ScoringZonePreferences[])
140123

141-
const assembly = field.mirabufInstance.parser.assembly
142-
const cacheId = field.cacheId
143-
if (cacheId) {
144-
const success = await MirabufCachingService.persistDevtoolChanges(cacheId, MiraType.FIELD, assembly)
145-
if (!success) {
146-
throw new Error("Failed to persist changes to cache")
147-
}
124+
if (field.fieldPreferences) {
125+
field.fieldPreferences.scoringZones = updatedZones as ScoringZonePreferences[]
126+
PreferencesSystem.savePreferences?.()
127+
field.updateScoringZones()
128+
}
129+
130+
const assembly = field.mirabufInstance.parser.assembly
131+
const cacheId = field.cacheId
132+
if (cacheId) {
133+
const success = await MirabufCachingService.persistDevtoolChanges(cacheId, MiraType.FIELD, assembly)
134+
if (!success) {
135+
throw new Error("Failed to persist changes to cache")
148136
}
149-
} else {
150-
throw new Error("Protected zone field file modification not yet implemented")
151137
}
152138
}
153139

@@ -163,9 +149,9 @@ export function getDevtoolZones(zoneType: ZoneType): ScoringZonePreferences[] |
163149

164150
const editor = new FieldMiraEditor(parts)
165151

166-
if (zoneType === "scoring") {
167-
return editor.getUserData("devtool:scoring_zones") as ScoringZonePreferences[] | undefined
168-
} else {
152+
if (zoneType === "protected") {
169153
return undefined
170154
}
155+
156+
return editor.getUserData("devtool:scoring_zones") as ScoringZonePreferences[] | undefined
171157
}

0 commit comments

Comments
 (0)