-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
checkpoint.go
107 lines (90 loc) · 2.42 KB
/
checkpoint.go
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
package game
import (
"encoding/gob"
"github.com/samber/lo"
)
func init() {
gob.Register(StateEventDeathData{})
gob.Register(StateEventDamageData{})
gob.Register(StateEventHealData{})
gob.Register(StateEventMoneyData{})
gob.Register(StateCheckpoint{})
gob.Register(StateCheckpointMarker{})
}
type StateEvent string
const (
StateEventDeath = StateEvent("Death")
StateEventDamage = StateEvent("Damage")
StateEventHeal = StateEvent("Heal")
StateEventMoney = StateEvent("Money")
StateEventArtifactAdded = StateEvent("ArtifactAdded")
StateEventArtifactRemoved = StateEvent("ArtifactRemoved")
StateEventCardAdded = StateEvent("CardAdded")
StateEventCardRemoved = StateEvent("CardRemoved")
)
type StateEventDeathData struct {
Source string
Target string
Damage int
}
type StateEventDamageData struct {
Source string
Target string
Damage int
}
type StateEventHealData struct {
Target string
Damage int
}
type StateEventMoneyData struct {
Target string
Money int
}
type StateEventArtifactAddedData struct {
Owner string
GUID string
TypeID string
}
type StateEventArtifactRemovedData struct {
Owner string
GUID string
TypeID string
}
type StateEventCardAddedData struct {
Owner string
GUID string
TypeID string
}
type StateEventCardRemovedData struct {
Owner string
GUID string
TypeID string
}
// StateCheckpoint saves the state of a session at a certain point. This can be used
// to retroactively check what happened between certain actions.
type StateCheckpoint struct {
Session *Session
// Events describe the events that
Events map[StateEvent]any
}
// StateCheckpointMarker is a saved state of a checkpoint log.
type StateCheckpointMarker struct {
checkpoints []StateCheckpoint
}
// Diff returns the new states that happened between the marker and a new session.
func (sm StateCheckpointMarker) Diff(session *Session) []StateCheckpoint {
if len(sm.checkpoints) >= len(session.stateCheckpoints) {
return nil
}
return session.stateCheckpoints[len(sm.checkpoints):]
}
// DiffEvent returns the new states that happened between the marker and a new session that contain a certain event.
func (sm StateCheckpointMarker) DiffEvent(session *Session, event StateEvent) []StateCheckpoint {
return lo.Filter(sm.Diff(session), func(item StateCheckpoint, index int) bool {
if item.Events == nil {
return false
}
_, ok := item.Events[event]
return ok
})
}