forked from topfreegames/khan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.go
227 lines (205 loc) · 6.65 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
// khan
// https://github.com/topfreegames/khan
//
// Licensed under the MIT license:
// http://www.opensource.org/licenses/mit-license
// Copyright © 2016 Top Free Games <backend@tfgco.com>
package api
import (
"fmt"
"strings"
"time"
"github.com/labstack/echo"
"github.com/topfreegames/khan/log"
"github.com/topfreegames/khan/models"
"github.com/uber-go/zap"
)
//CreateGameHandler is the handler responsible for creating new games
func CreateGameHandler(app *App) func(c echo.Context) error {
return func(c echo.Context) error {
c.Set("route", "CreateGame")
start := time.Now()
l := app.Logger.With(
zap.String("source", "gameHandler"),
zap.String("operation", "createGame"),
)
log.D(l, "Retrieving parameters...")
var err error
var payload *CreateGamePayload
var optional *optionalParams
err = WithSegment("payload", c, func() error {
payload, optional, err = getCreateGamePayload(app, c, l)
return err
})
if err != nil {
log.E(l, "Failed to retrieve parameters.", func(cm log.CM) {
cm.Write(zap.Error(err))
})
return FailWith(400, err.Error(), c)
}
log.D(l, "Parameters retrieved successfully.", func(cm log.CM) {
cm.Write(
zap.Int("maxPendingInvites", optional.maxPendingInvites),
zap.Int("cooldownBeforeInvite", optional.cooldownBeforeInvite),
zap.Int("cooldownBeforeApply", optional.cooldownBeforeApply),
)
})
log.D(l, "Creating game...")
game, err := models.CreateGame(
app.Db,
payload.PublicID,
payload.Name,
payload.MembershipLevels,
payload.Metadata,
payload.MinLevelToAcceptApplication,
payload.MinLevelToCreateInvitation,
payload.MinLevelToRemoveMember,
payload.MinLevelOffsetToRemoveMember,
payload.MinLevelOffsetToPromoteMember,
payload.MinLevelOffsetToDemoteMember,
payload.MaxMembers,
payload.MaxClansPerPlayer,
payload.CooldownAfterDeny,
payload.CooldownAfterDelete,
optional.cooldownBeforeApply,
optional.cooldownBeforeInvite,
optional.maxPendingInvites,
false,
optional.clanUpdateMetadataFieldsHookTriggerWhitelist,
optional.playerUpdateMetadataFieldsHookTriggerWhitelist,
)
if err != nil {
log.E(l, "Create game failed.", func(cm log.CM) {
cm.Write(zap.Error(err))
})
return FailWith(500, err.Error(), c)
}
log.I(l, "Game created succesfully.", func(cm log.CM) {
cm.Write(zap.Duration("duration", time.Now().Sub(start)))
})
return SucceedWith(map[string]interface{}{
"publicID": game.PublicID,
}, c)
}
}
//UpdateGameHandler is the handler responsible for updating existing
func UpdateGameHandler(app *App) func(c echo.Context) error {
return func(c echo.Context) error {
c.Set("route", "UpdateGame")
start := time.Now()
gameID := c.Param("gameID")
l := app.Logger.With(
zap.String("source", "gameHandler"),
zap.String("operation", "updateGame"),
zap.String("gameID", gameID),
)
var payload UpdateGamePayload
var optional *optionalParams
var status int
err := WithSegment("payload", c, func() error {
var err error
log.D(l, "Retrieving parameters...")
if err = LoadJSONPayload(&payload, c, l); err != nil {
log.E(l, "Failed to retrieve parameters.", func(cm log.CM) {
cm.Write(zap.Error(err))
})
status = 400
return err
}
optional, err = getOptionalParameters(app, c)
if err != nil {
log.E(l, "Failed to retrieve optional parameters.", func(cm log.CM) {
cm.Write(zap.Error(err))
})
status = 400
return err
}
log.D(l, "Parameters retrieved successfully.", func(cm log.CM) {
cm.Write(
zap.Int("maxPendingInvites", optional.maxPendingInvites),
zap.Int("cooldownBeforeInvite", optional.cooldownBeforeInvite),
zap.Int("cooldownBeforeApply", optional.cooldownBeforeApply),
)
})
log.D(l, "Validating payload...")
if payloadErrors := ValidatePayload(&payload); len(payloadErrors) != 0 {
status = 422
logPayloadErrors(l, payloadErrors)
errorString := strings.Join(payloadErrors[:], ", ")
return fmt.Errorf(errorString)
}
return nil
})
if err != nil {
return FailWith(status, err.Error(), c)
}
err = WithSegment("game-update", c, func() error {
log.D(l, "Updating game...")
_, err = models.UpdateGame(
app.Db,
gameID,
payload.Name,
payload.MembershipLevels,
payload.Metadata,
payload.MinLevelToAcceptApplication,
payload.MinLevelToCreateInvitation,
payload.MinLevelToRemoveMember,
payload.MinLevelOffsetToRemoveMember,
payload.MinLevelOffsetToPromoteMember,
payload.MinLevelOffsetToDemoteMember,
payload.MaxMembers,
payload.MaxClansPerPlayer,
payload.CooldownAfterDeny,
payload.CooldownAfterDelete,
optional.cooldownBeforeApply,
optional.cooldownBeforeInvite,
optional.maxPendingInvites,
optional.clanUpdateMetadataFieldsHookTriggerWhitelist,
optional.playerUpdateMetadataFieldsHookTriggerWhitelist,
)
return err
})
if err != nil {
log.E(l, "Game update failed.", func(cm log.CM) {
cm.Write(zap.Error(err))
})
return FailWith(500, err.Error(), c)
}
successPayload := map[string]interface{}{
"publicID": gameID,
"name": payload.Name,
"membershipLevels": payload.MembershipLevels,
"metadata": payload.Metadata,
"minLevelToAcceptApplication": payload.MinLevelToAcceptApplication,
"minLevelToCreateInvitation": payload.MinLevelToCreateInvitation,
"minLevelToRemoveMember": payload.MinLevelToRemoveMember,
"minLevelOffsetToRemoveMember": payload.MinLevelOffsetToRemoveMember,
"minLevelOffsetToPromoteMember": payload.MinLevelOffsetToPromoteMember,
"minLevelOffsetToDemoteMember": payload.MinLevelOffsetToDemoteMember,
"maxMembers": payload.MaxMembers,
"maxClansPerPlayer": payload.MaxClansPerPlayer,
"cooldownAfterDeny": payload.CooldownAfterDeny,
"cooldownAfterDelete": payload.CooldownAfterDelete,
"cooldownBeforeApply": optional.cooldownBeforeApply,
"cooldownBeforeInvite": optional.cooldownBeforeInvite,
"maxPendingInvites": optional.maxPendingInvites,
}
err = WithSegment("hook-dispatch", c, func() error {
dErr := app.DispatchHooks(gameID, models.GameUpdatedHook, successPayload)
if dErr != nil {
log.E(l, "Game update hook dispatch failed.", func(cm log.CM) {
cm.Write(zap.Error(dErr))
})
return dErr
}
return nil
})
if err != nil {
return FailWith(500, err.Error(), c)
}
log.I(l, "Game updated succesfully.", func(cm log.CM) {
cm.Write(zap.Duration("duration", time.Now().Sub(start)))
})
return SucceedWith(map[string]interface{}{}, c)
}
}