-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstorage_test.go
178 lines (133 loc) · 4.09 KB
/
storage_test.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
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
package boardgame
import (
"errors"
"strconv"
"strings"
)
//This file is actually used to just implement a shim StorageManager so our
//own package tests can run. The shim is basically storage/memory/StorageManager.
type testStorageManager struct {
states map[string]map[int]StateStorageRecord
moves map[string]map[int]*MoveStorageRecord
games map[string]*GameStorageRecord
}
func newTestStorageManager() *testStorageManager {
//InMemoryStorageManager is an extremely simple StorageManager that just keeps
//track of the objects in memory.
return &testStorageManager{
states: make(map[string]map[int]StateStorageRecord),
moves: make(map[string]map[int]*MoveStorageRecord),
games: make(map[string]*GameStorageRecord),
}
}
func (t *testStorageManager) String() string {
var results []string
results = append(results, "States")
for key, states := range t.states {
results = append(results, key)
for version, state := range states {
results = append(results, strconv.Itoa(version)+": "+string(state))
}
}
results = append(results, "Games")
for key, game := range t.games {
results = append(results, key, game.Name, game.ID, strconv.Itoa(game.Version))
}
return strings.Join(results, "\n")
}
func (t *testStorageManager) State(gameID string, version int) (StateStorageRecord, error) {
if gameID == "" {
return nil, errors.New("No game provided")
}
if version < 0 {
return nil, errors.New("Illegal version")
}
versionMap, ok := t.states[gameID]
if !ok {
return nil, errors.New("That game does not exist")
}
record, ok := versionMap[version]
if !ok {
return nil, errors.New("That version of that game doesn't exist")
}
return record, nil
}
func (t *testStorageManager) Moves(gameID string, fromVersion, toVersion int) ([]*MoveStorageRecord, error) {
result := make([]*MoveStorageRecord, toVersion-fromVersion+1)
index := 0
for i := fromVersion; i <= toVersion; i++ {
move, err := t.Move(gameID, i)
if err != nil {
return nil, err
}
result[index] = move
index++
}
return result, nil
}
func (t *testStorageManager) Move(gameID string, version int) (*MoveStorageRecord, error) {
if gameID == "" {
return nil, errors.New("No game provided")
}
if version < 0 {
return nil, errors.New("Illegal version")
}
versionMap, ok := t.moves[gameID]
if !ok {
return nil, errors.New("That game does not exist")
}
record, ok := versionMap[version]
if !ok {
return nil, errors.New("That version of that game doesn't exist")
}
return record, nil
}
func (t *testStorageManager) Game(id string) (*GameStorageRecord, error) {
record := t.games[id]
if record == nil {
return nil, errors.New("That game does not exist")
}
return record, nil
}
func (t *testStorageManager) SaveGameAndCurrentState(game *GameStorageRecord, state StateStorageRecord, move *MoveStorageRecord) error {
if game == nil {
return errors.New("No game provided")
}
//TODO: validate that state.Version is reasonable.
if _, ok := t.states[game.ID]; !ok {
t.states[game.ID] = make(map[int]StateStorageRecord)
}
if _, ok := t.moves[game.ID]; !ok {
t.moves[game.ID] = make(map[int]*MoveStorageRecord)
}
version := game.Version
versionMap := t.states[game.ID]
moveMap := t.moves[game.ID]
if _, ok := versionMap[version]; ok {
//Wait, there was already a version stored there?
return errors.New("There was already a version for that game stored")
}
if _, ok := moveMap[version]; ok {
//Wait, there was already a version stored there?
return errors.New("There was already a version for that game stored")
}
versionMap[version] = state
moveMap[version] = move
t.games[game.ID] = game
return nil
}
func (t *testStorageManager) PlayerMoveApplied(game *GameStorageRecord) error {
//Pass
return nil
}
func (t *testStorageManager) FetchInjectedDataForGame(gameID string, dataType string) interface{} {
return nil
}
func (t *testStorageManager) AgentState(gameID string, player PlayerIndex) ([]byte, error) {
//TODO: implement
return nil, nil
}
func (t *testStorageManager) SaveAgentState(gameID string, player PlayerIndex, state []byte) error {
//TODO: implement
return nil
}