1
1
import FieldMiraEditor from "@/mirabuf/FieldMiraEditor"
2
2
import MirabufCachingService , { MiraType } from "@/mirabuf/MirabufLoader"
3
3
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"
5
5
import World from "@/systems/World"
6
6
7
7
export type ZoneType = "scoring" | "protected"
8
8
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
+
9
21
/**
10
22
* Checks if a zone was originally defined in the field file by comparing it with the cached field data.
11
23
*/
12
24
export function isZoneFromDevtools (
13
- zone : ScoringZonePreferences | ProtectedZonePreferences ,
25
+ zone : BaseZonePreferences ,
14
26
zoneType : ZoneType
15
27
) : boolean {
16
28
const field = World . sceneRenderer . mirabufSceneObjects . getField ( )
@@ -21,22 +33,14 @@ export function isZoneFromDevtools(
21
33
22
34
const editor = new FieldMiraEditor ( parts )
23
35
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" ) {
38
37
return false
39
38
}
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 ) )
40
44
}
41
45
42
46
/**
@@ -53,47 +57,34 @@ export async function removeZoneFromDevtools(
53
57
if ( ! parts ) throw new Error ( "No field parts found" )
54
58
55
59
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
56
67
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
+ }
78
74
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
+ }
85
80
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" )
94
87
}
95
- } else {
96
- throw new Error ( "Protected zone field file removal not yet implemented" )
97
88
}
98
89
}
99
90
@@ -112,42 +103,37 @@ export async function modifyZoneInDevtools(
112
103
if ( ! parts ) throw new Error ( "No field parts found" )
113
104
114
105
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
139
118
}
119
+ return devZone
120
+ } )
121
+
122
+ editor . setUserData ( "devtool:scoring_zones" , updatedZones as ScoringZonePreferences [ ] )
140
123
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" )
148
136
}
149
- } else {
150
- throw new Error ( "Protected zone field file modification not yet implemented" )
151
137
}
152
138
}
153
139
@@ -163,9 +149,9 @@ export function getDevtoolZones(zoneType: ZoneType): ScoringZonePreferences[] |
163
149
164
150
const editor = new FieldMiraEditor ( parts )
165
151
166
- if ( zoneType === "scoring" ) {
167
- return editor . getUserData ( "devtool:scoring_zones" ) as ScoringZonePreferences [ ] | undefined
168
- } else {
152
+ if ( zoneType === "protected" ) {
169
153
return undefined
170
154
}
155
+
156
+ return editor . getUserData ( "devtool:scoring_zones" ) as ScoringZonePreferences [ ] | undefined
171
157
}
0 commit comments