-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.js
More file actions
executable file
·67 lines (57 loc) · 1.86 KB
/
Copy pathupdate.js
File metadata and controls
executable file
·67 lines (57 loc) · 1.86 KB
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
var onTurnD = require("./onTurn")
var onResolveD = require("./onResolve")
var utils = require("./utils")
var makeUpdate = (prep, apply, now, resolved, onResolve = onResolveD, onTurn = onTurnD) => {
return function update (state) {
// Prune pending actions
let pending = state.pending.filter(x => !resolved.some(y => x.id === y.id))
if (state.pending.length !== pending.length) {
state = Object.assign({}, state, {pending})
}
pending = state.pending
let {turn, updated} = state.times
// Resolve the next batch or actions preceding the next turn
let nextTurnTime = utils.getNextTurnTime(state.times)
let batch = getBatch(pending, nextTurnTime)
if (batch.length > 0) {
let batchTime = batch[0].time
batch.forEach(action => {
state = apply(state, action, batchTime)
})
return update(state)
}
// Are their actions arising from the state?
state = onResolve(state, apply, prep, updated)
if (state.pending !== pending) {
return update(state)
}
// Resolve next turn
if (nextTurnTime <= now) {
turn = turn + 1
let turnActions = pending.filter(x => x.turn === turn)
state = onTurn(state, apply, prep, turnActions)
let times = Object.assign({}, state.times, {
turn,
updated: nextTurnTime
})
state = Object.assign({}, state, {times})
return update(state)
}
return state
}
}
var getBatch = (pending, time) => {
let batch = pending
.filter(action => action.time <= time)
.sort((a, b) => {
if (a.time > b.time) return 1
if (a.time < b.time) return -1
return 0
})
if (batch.length > 1) {
let batchTime = batch[0].time
batch = batch.filter(action => action.time === batchTime)
}
return batch
}
module.exports = makeUpdate