-
Notifications
You must be signed in to change notification settings - Fork 14
/
game.go
366 lines (330 loc) · 8.85 KB
/
game.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
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
363
364
365
366
package models
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/zond/stockholm-ai/hub/common"
"github.com/zond/stockholm-ai/state"
"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/delay"
"google.golang.org/appengine/log"
"google.golang.org/appengine/urlfetch"
ai "github.com/zond/stockholm-ai/ai"
aiCommon "github.com/zond/stockholm-ai/common"
)
const (
GameKind = "Game"
allGamesKey = "Games{All}"
maxGameDuration = 100
)
var nextTurnFunc *delay.Function
func init() {
nextTurnFunc = delay.Func("models/game.nextTurnFunc", nextTurn)
}
func gameKeyForId(k interface{}) string {
return fmt.Sprintf("Game{Id:%v}", k)
}
func gamePageKey(limit, offset int) string {
return fmt.Sprintf("GamePage{limit:%v,offset:%v}", limit, offset)
}
type GameState string
const (
StateCreated GameState = "Created"
StatePlaying GameState = "Playing"
StateFinished GameState = "Finished"
)
type Games []Game
func (self Games) Len() int {
return len(self)
}
func (self Games) Less(j, i int) bool {
return self[i].CreatedAt.Before(self[j].CreatedAt)
}
func (self Games) Swap(i, j int) {
self[j], self[i] = self[i], self[j]
}
func (self Games) process(c common.Context) Games {
for index, _ := range self {
(&self[index]).process(c)
}
return self
}
type Game struct {
Id *datastore.Key
Players []*datastore.Key
Winner *datastore.Key
State GameState
PlayerNames []string `datastore:"-"`
WinnerName string `datastore:"-"`
Length int
CreatedAt time.Time
}
type orderResponse struct {
DatastorePlayerId *datastore.Key
StatePlayerId state.PlayerId
Orders state.Orders
Error error
}
type orderError struct {
Request *http.Request
RequestBody string
Response *http.Response
ResponseBody string
}
func (self orderError) Error() string {
return fmt.Sprintf("Got %v from %v", self.Response.StatusCode, self.Request.URL)
}
func nextTurn(cont context.Context, id *datastore.Key, playerNames []string) {
con := common.Context{Context: cont}
self := getGameById(con, id)
self.PlayerNames = playerNames
if self.Length > maxGameDuration {
self.State = StateFinished
self.Save(con)
log.Infof(cont, "Ended %v due to timeout", self.Id)
return
}
errorSavers := []func(){}
if err := common.Transaction(con, func(c common.Context) (err error) {
lastTurn := GetLatestTurnByParent(c, self.Id)
responses := make(chan orderResponse, len(self.Players))
ais := map[state.PlayerId]string{}
for index, playerId := range self.Players {
ais[state.PlayerId(playerId.Encode())] = self.PlayerNames[index]
}
for _, playerId := range self.Players {
orderResp := orderResponse{
DatastorePlayerId: playerId,
StatePlayerId: state.PlayerId(playerId.Encode()),
}
if foundAi := GetAIById(c, playerId); foundAi != nil {
go func() {
// Always deliver the order response
defer func() {
responses <- orderResp
}()
// create a request
orderRequest := ai.OrderRequest{
Me: orderResp.StatePlayerId,
State: lastTurn.State,
GameId: state.GameId(self.Id.Encode()),
TurnOrdinal: lastTurn.Ordinal,
AIs: ais,
}
// encode it into a body, and remember its string representation
sendBody := &bytes.Buffer{}
aiCommon.MustEncodeJSON(sendBody, orderRequest)
sendBodyString := sendBody.String()
// get a client
client := urlfetch.Client(c)
// send the request to the ai
req, err := http.NewRequest("POST", foundAi.URL, sendBody)
var resp *http.Response
if err == nil {
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
resp, err = client.Do(req)
}
recvBody := &bytes.Buffer{}
recvBodyString := ""
if err == nil {
// check what we received
_, err = io.Copy(recvBody, resp.Body)
recvBodyString = recvBody.String()
}
// if we have no other errors, but we got a non-200
if err == nil && resp.StatusCode != 200 {
err = orderError{
Request: req,
RequestBody: sendBodyString,
Response: resp,
ResponseBody: recvBodyString,
}
}
// lets try to unserialize
if err == nil {
err = json.Unmarshal(recvBody.Bytes(), &orderResp.Orders)
}
// store the error, if any
if err != nil {
orderResp.Error = err
}
}()
} else {
responses <- orderResp
}
}
orderMap := map[state.PlayerId]state.Orders{}
for _, _ = range self.Players {
// wait for the responses
orderResp := <-responses
// store it
orderMap[orderResp.StatePlayerId] = orderResp.Orders
// if we got an error
if orderResp.Error != nil {
// make sure to save it later
errorSavers = append(errorSavers, func() {
if ai := GetAIById(con, orderResp.DatastorePlayerId); ai != nil {
ai.AddError(con, lastTurn.Id, orderResp.Error)
}
})
}
}
// execute the orders
newTurn, winner := lastTurn.Next(c, orderMap)
// save the new turn
newTurn.Save(c, self.Id)
// if we got a winner, end the game and store the winner
if winner == nil {
self.State = StatePlaying
} else {
self.Winner = common.MustDecodeKey(string(*winner))
self.State = StateFinished
}
// increase our length with the new turn
self.Length += 1
// save us
self.Save(c)
// if we didn't end, queue the next turn
if winner == nil {
nextTurnFunc.Call(c, self.Id, playerNames)
}
return nil
}); err != nil {
panic(err)
}
// run any error savers we got
for _, saver := range errorSavers {
saver()
}
// store the new stats in the players if we ended
if self.State == StateFinished {
for _, playerId := range self.Players {
common.Transaction(con, func(c common.Context) error {
if ai := GetAIById(c, playerId); ai != nil {
if playerId.Equal(self.Winner) {
ai.Wins += 1
} else {
ai.Losses += 1
}
ai.Save(c)
}
return nil
})
}
}
}
func (self *Game) setPlayerNames(c common.Context) {
self.PlayerNames = make([]string, len(self.Players))
for index, id := range self.Players {
if ai := GetAIById(c, id); ai != nil {
self.PlayerNames[index] = ai.Name
if ai.Id.Equal(self.Winner) {
self.WinnerName = ai.Name
}
} else {
self.PlayerNames[index] = "[redacted]"
}
}
}
func (self *Game) process(c common.Context) *Game {
self.setPlayerNames(c)
return self
}
type GamePage struct {
Content Games
Total int
}
func findGamePage(c common.Context, offset, limit int) (result GamePage) {
query := datastore.NewQuery(GameKind)
var err error
result.Total, err = query.Count(c)
common.AssertOkError(err)
var ids []*datastore.Key
ids, err = query.Limit(limit).Offset(offset).Order("-CreatedAt").GetAll(c, &result.Content)
common.AssertOkError(err)
for index, id := range ids {
result.Content[index].Id = id
}
if result.Content == nil {
result.Content = Games{}
}
return
}
func GetGamePage(c common.Context, offset, limit int) (result GamePage) {
common.Memoize2(c, allGamesKey, gamePageKey(limit, offset), &result, func() interface{} {
return findGamePage(c, offset, limit)
})
result.Content.process(c)
return result
}
func findGameById(c common.Context, id *datastore.Key) *Game {
var game Game
err := datastore.Get(c, id, &game)
if err == datastore.ErrNoSuchEntity {
return nil
}
common.AssertOkError(err)
game.Id = id
return &game
}
func getGameById(c common.Context, id *datastore.Key) *Game {
var game Game
if common.Memoize(c, gameKeyForId(id), &game, func() interface{} {
return findGameById(c, id)
}) {
return &game
}
return nil
}
func GetGameById(c common.Context, id *datastore.Key) (result *Game) {
result = getGameById(c, id)
if result != nil {
result.process(c)
}
return
}
func (self *Game) Save(c common.Context) *Game {
var err error
if self.Id == nil {
self.setPlayerNames(c)
err = common.Transaction(c, func(c common.Context) (err error) {
self.CreatedAt = time.Now()
self.State = StateCreated
self.Length = 1
self.Id, err = datastore.Put(c, datastore.NewKey(c, GameKind, "", 0, nil), self)
if err != nil {
return
}
playerIds := make([]state.PlayerId, 0, len(self.Players))
for _, id := range self.Players {
playerIds = append(playerIds, state.PlayerId(id.Encode()))
}
turn := &Turn{
State: state.RandomState(common.GAELogger{c}, playerIds),
}
turn.Save(c, self.Id)
nextTurnFunc.Call(c, self.Id, self.PlayerNames)
return nil
})
if err == nil {
for _, playerId := range self.Players {
common.Transaction(c, func(common.Context) error {
if ai := GetAIById(c, playerId); ai != nil {
ai.Games += 1
ai.Save(c)
}
return nil
})
}
}
} else {
_, err = datastore.Put(c, self.Id, self)
}
common.AssertOkError(err)
common.MemDel(c, allGamesKey, gameKeyForId(self.Id))
return self.process(c)
}