-
Notifications
You must be signed in to change notification settings - Fork 14
/
ai.go
199 lines (171 loc) · 4.41 KB
/
ai.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
package models
import (
"fmt"
"sort"
"time"
"github.com/zond/stockholm-ai/hub/common"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
)
const (
AIKind = "AI"
AIErrorKind = "AIError"
AllAIsKey = "AIs{All}"
)
func aIByIdKey(k interface{}) string {
return fmt.Sprintf("AI{Id:%v}", k)
}
func aiErrorsKeyByParent(k interface{}) string {
return fmt.Sprintf("AIErrors{Parent:%v}", k)
}
type AIError struct {
Turn *datastore.Key
Game *datastore.Key `datastore:"-"`
Error string `datastore:"-"`
ErrorDetail1 string `datastore:"-"`
ErrorDetail2 string `datastore:"-"`
ErrorBytes []byte `json:"-"`
ErrorDetail1Bytes []byte `json:"-"`
ErrorDetail2Bytes []byte `json:"-"`
CreatedAt time.Time
}
func (self *AIError) process(c common.Context) *AIError {
self.Error = string(self.ErrorBytes)
self.ErrorDetail1 = string(self.ErrorDetail1Bytes)
self.ErrorDetail2 = string(self.ErrorDetail2Bytes)
self.Game = self.Turn.Parent()
return self
}
type AIErrors []AIError
func (self AIErrors) process(c common.Context) AIErrors {
for index, _ := range self {
(&self[index]).process(c)
}
return self
}
func (self AIErrors) Len() int {
return len(self)
}
func (self AIErrors) Less(j, i int) bool {
return self[i].CreatedAt.Before(self[j].CreatedAt)
}
func (self AIErrors) Swap(i, j int) {
self[i], self[j] = self[j], self[i]
}
type AIs []AI
func (self AIs) Len() int {
return len(self)
}
func (self AIs) Less(i, j int) bool {
return self[j].CreatedAt.Before(self[i].CreatedAt)
}
func (self AIs) Swap(i, j int) {
self[i], self[j] = self[j], self[i]
}
func (self AIs) process(c common.Context) AIs {
for index, _ := range self {
(&self[index]).process(c)
}
return self
}
type AI struct {
Id *datastore.Key
URL string
Name string
Games int
Wins int
Losses int
Owner string `json:"-"`
IsOwner bool `datastore:"-"`
CreatedAt time.Time
}
func (self *AI) AddError(c common.Context, turnId *datastore.Key, err error) {
_, e := datastore.Put(c, datastore.NewKey(c, AIErrorKind, "", 0, self.Id), &AIError{
CreatedAt: time.Now(),
Turn: turnId,
ErrorBytes: []byte(err.Error()),
ErrorDetail1Bytes: []byte(fmt.Sprintf("%+v", err)),
ErrorDetail2Bytes: []byte(fmt.Sprintf("%#v", err)),
})
if e != nil {
log.Errorf(c, "Got %+v when trying to save a new error!", e)
}
common.MemDel(c, aiErrorsKeyByParent(self.Id))
}
func (self *AI) findErrors(c common.Context) (result AIErrors) {
_, err := datastore.NewQuery(AIErrorKind).Ancestor(self.Id).Order("-CreatedAt").Limit(50).GetAll(c, &result)
common.AssertOkError(err)
if result == nil {
result = AIErrors{}
}
return
}
func (self *AI) GetErrors(c common.Context) (result AIErrors) {
common.Memoize(c, aiErrorsKeyByParent(self.Id), &result, func() interface{} {
return self.findErrors(c)
})
sort.Sort(result)
return result.process(c)
}
func (self *AI) process(c common.Context) *AI {
if c.User != nil {
self.IsOwner = self.Owner == c.User.Email
}
if !self.IsOwner {
self.URL = ""
}
return self
}
func findAIById(c common.Context, id *datastore.Key) *AI {
var ai AI
err := datastore.Get(c, id, &ai)
if err == datastore.ErrNoSuchEntity {
return nil
}
common.AssertOkError(err)
ai.Id = id
return &ai
}
func GetAIById(c common.Context, id *datastore.Key) *AI {
var ai AI
if common.Memoize(c, aIByIdKey(id), &ai, func() interface{} {
return findAIById(c, id)
}) {
return &ai
}
return nil
}
func findAllAIs(c common.Context) (result AIs) {
ids, err := datastore.NewQuery(AIKind).GetAll(c, &result)
common.AssertOkError(err)
for index, id := range ids {
result[index].Id = id
}
if result == nil {
result = AIs{}
}
return
}
func GetAllAIs(c common.Context) (result AIs) {
common.Memoize(c, AllAIsKey, &result, func() interface{} {
return findAllAIs(c)
})
sort.Sort(result)
return result.process(c)
}
func (self *AI) Delete(c common.Context) {
datastore.Delete(c, self.Id)
common.MemDel(c, AllAIsKey, aIByIdKey(self.Id))
}
func (self *AI) Save(c common.Context) *AI {
var err error
if self.Id == nil {
self.CreatedAt = time.Now()
self.Id, err = datastore.Put(c, datastore.NewKey(c, AIKind, "", 0, nil), self)
} else {
_, err = datastore.Put(c, self.Id, self)
}
common.AssertOkError(err)
common.MemDel(c, AllAIsKey, aIByIdKey(self.Id))
return self
}