-
Notifications
You must be signed in to change notification settings - Fork 156
/
priority.js
362 lines (356 loc) · 11.5 KB
/
priority.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*Copyright 2024 Kirk McDonald
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
import { spec } from "./factory.js"
import { Rational } from "./rational.js"
class Resource {
constructor(recipe, weight) {
this.level = null
this.recipe = recipe
this.weight = weight
let self = this
this.div = d3.create("div")
this.div.classed("resource", true)
//.attr("draggable", "true")
.on("dragstart", function(event, d) {
self.level.list.div.classed("dragging", true)
self.level.list.dragItem = self
})
.on("dragend", function(event, d) {
self.level.list.div.classed("dragging", false)
})
this.div.append(() => self.recipe.icon.make(48))
this.div.append("input")
.attr("type", "text")
.attr("size", 4)
.attr("value", this.weight.toString())
.on("change", function(event, d) {
self.weight = Rational.from_string(this.value)
self.level.insertSorted(self)
spec.updateSolution()
})
}
// Removes this Resource from its current PriorityLevel. If the level is
// left empty as a rusult, it is removed. The Resource is then free to be
// inserted into a different level.
remove() {
if (this.level === null) {
return
}
for (let i = 0; i < this.level.resources.length; i++) {
let r = this.level.resources[i]
if (r === this) {
this.level.resources.splice(i, 1)
break
}
}
this.div.remove()
if (this.level.isEmpty()) {
this.level.remove()
}
this.level = null
}
}
class PriorityLevel {
constructor(list) {
this.resources = []
this.middle = null
this.list = list
this.div = d3.create("div")
.datum(this)
.classed("resource-tier", true)
let self = this
list._dropTargetBoilerplate(this.div, function(event, d) {
if (list.dragItem.level !== self) {
self.insertSorted(list.dragItem)
}
})
}
[Symbol.iterator]() {
return this.resources[Symbol.iterator]()
}
equalMap(m) {
if (m.size !== this.resources.length) {
return false
}
for (let {recipe, weight} of this) {
if (!m.has(recipe) || !m.get(recipe).equal(weight)) {
return false
}
}
return true
}
has(resource) {
return resource.level === this
}
// Removes this level (and its 'middle' divider) from the PriorityList.
// It is an error to call this if the level is not empty.
remove() {
if (this.resources.length !== 0) {
throw new Error("cannot remove non-empty PriorityLevel")
}
if (this.middle) {
this.middle.remove()
this.middle = null
}
this.div.remove()
this.list.removeEmptyLevels()
}
isEmpty() {
return this.resources.length === 0
}
// Moves the given resource to this level. Removes it from its old level.
// If the old level is left empty as a result, it will be removed.
//
// If the resource is already in this level, it will be re-inserted in
// sorted order.
insertSorted(resource) {
if (resource.level === this && this.resources.length === 1) {
// If it's the only resource on this level, then no re-sorting is
// required.
return
} else if (resource.level !== null) {
resource.remove()
}
resource.level = this
for (let i = 0; i < this.resources.length; i++) {
let r = this.resources[i]
if (resource.weight.less(r.weight)) {
this.resources.splice(i, 0, resource)
this.div.node().insertBefore(resource.div.node(), r.div.node())
return
}
}
this.resources.push(resource)
this.div.node().appendChild(resource.div.node())
}
}
export class PriorityList {
constructor() {
this.priorities = []
this.dragItem = null
this.div = d3.select("#resource_settings")
this.renderEmpty()
}
[Symbol.iterator]() {
return this.priorities[Symbol.iterator]()
}
static getDefaultArray(recipe) {
let a = []
for (let [recipeKey, recipe] of recipes) {
if (recipe.isResource()) {
let pri = recipe.defaultPriority
while (a.length < pri + 1) {
a.push(new Map())
}
a.set(recipe, recipe.defaultWeight)
}
}
return a
}
static fromArray(a) {
let p = new PriorityList()
for (let m of a) {
let level = p.addPriorityBefore(null)
for (let [recipe, weight] of m) {
p.addRecipe(recipe, weight, level)
}
}
return p
}
applyArray(a) {
for (let i = 0; i < a.length; i++) {
let m = a[i]
while (this.priorities.length < i + 1) {
this.addPriorityBefore(null)
}
let level = this.priorities[i]
for (let [recipe, weight] of m) {
let resource = this.getResource(recipe)
if (resource === null) {
this.addRecipe(recipe, weight, level)
} else {
level.insertSorted(resource)
}
}
}
}
/*makeArray() {
let result = []
for (let level of this) {
let levelMap = new Map()
for (let {recipe, weight} of level) {
levelMap.set(recipe, weight)
}
result.push(levelMap)
}
return result
}*/
equalArray(a) {
if (a.length !== this.priorities.length) {
return false
}
for (let i = 0; i < a.length; i++) {
if (!this.priorities[i].equalMap(a[i])) {
return false
}
}
return true
}
// Creates a new priority level immediately preceding the given one.
// If the given priority is null, adds the new priority to the end of
// the priority list.
//
// Returns the new PriorityLevel.
addPriorityBefore(level) {
let newLevel = new PriorityLevel(this)
let successorNode = null
let isFirst = null
if (level === null) {
this.priorities.push(newLevel)
successorNode = this.div.node().lastChild
isFirst = this.priorities.length === 1
} else {
for (let i = 0; i < this.priorities.length; i++) {
if (this.priorities[i] === level) {
this.priorities.splice(i, 0, newLevel)
isFirst = i === 0
if (isFirst) {
successorNode = level.div.node()
} else {
successorNode = level.middle.node()
}
break
}
}
}
if (!isFirst) {
let middle = this._makeMiddle(newLevel)
newLevel.middle = middle
this.div.node().insertBefore(middle.node(), successorNode)
}
this.div.node().insertBefore(newLevel.div.node(), successorNode)
if (isFirst && level !== null) {
let middle = this._makeMiddle(level)
level.middle = middle
this.div.node().insertBefore(middle.node(), successorNode)
}
return newLevel
}
getFirstLevel() {
if (this.priorities.length === 0) {
return null
}
return this.priorities[0]
}
getLastLevel() {
if (this.priorities.length === 0) {
return null
}
return this.priorities[this.priorities.length - 1]
}
// Moves resource from its current level to the given level.
// If the resource's previous level is left empty as a result, it will be
// removed.
setPriority(resource, level) {
level.insertSorted(resource)
}
addRecipe(recipe, weight, level) {
let resource = new Resource(recipe, weight)
level.insertSorted(resource)
}
getResource(recipe) {
for (let level of this.priorities) {
for (let resource of level.resources) {
if (resource.recipe === recipe) {
return resource
}
}
}
return null
}
getWeight(recipe) {
return this.getResource(recipe).weight
}
removeRecipe(recipe) {
let resource = this.getResource(recipe)
resource.remove()
}
renderEmpty() {
let self = this
this.div.selectAll("*").remove()
let less = this.div.append("div")
.classed("resource-tier bookend", true)
this._dropTargetBoilerplate(less, function(event, d) {
let first = self.priorities[0]
let p = self.addPriorityBefore(first)
self.setPriority(self.dragItem, p)
})
less.append("span")
.text("less valuable")
let more = this.div.append("div")
.classed("resource-tier bookend", true)
this._dropTargetBoilerplate(more, function(event, d) {
let p = self.addPriorityBefore(null)
self.setPriority(self.dragItem, p)
})
more.append("span")
.text("more valuable")
}
removeEmptyLevels() {
let newLevels = []
for (let level of this) {
if (!level.isEmpty()) {
newLevels.push(level)
}
}
if (newLevels.length > 0 && newLevels[0].middle !== null) {
newLevels[0].middle.remove()
newLevels[0].middle = null
}
this.priorities = newLevels
}
_dropTargetBoilerplate(s, drop) {
let self = this
s.on("dragover", function(event, d) {
event.preventDefault()
})
s.on("dragenter", function(event, d) {
this.classList.add("highlight")
})
s.on("dragleave", function(event, d) {
if (event.target === this) {
this.classList.remove("highlight")
}
})
s.on("drop", function(event, d) {
if (self.dragItem === null) {
return
}
event.preventDefault()
this.classList.remove("highlight")
drop.call(this, event, d)
self.dragItem = null
spec.updateSolution()
})
}
// Creates a divider to insert before the given priority level.
_makeMiddle(level) {
let self = this
let middle = d3.create("div")
.datum(level)
.classed("middle", true)
this._dropTargetBoilerplate(middle, function(event, d) {
let p = self.addPriorityBefore(d)
self.setPriority(self.dragItem, p)
})
return middle
}
}