-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
332 lines (298 loc) · 7.61 KB
/
user.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
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"path"
"github.com/albrow/forms"
)
// SettingMap mapping request URL into column in PLAYER table
var SettingMap = map[string]string{}
func init() {
SettingMap["is_hide_rating"] = "is_hide_rating"
SettingMap["max_stamina_notification_enabled"] = "max_stamina_notification"
SettingMap["favorite_character"] = "favorite_partner"
}
func presentMeHandler(w http.ResponseWriter, r *http.Request) {
var (
userID int
err error
)
if NeedAuth {
userID, err = verifyBearerAuth(r.Header.Get("Authorization"))
if err != nil {
c := Container{false, nil, 203}
http.Error(w, c.toJSON(), http.StatusUnauthorized)
return
}
} else {
userID = staticUserID
}
tojson, err := presentMe(userID, r)
if err != nil {
log.Println(err)
} else {
fmt.Fprint(w, tojson.toJSON())
}
}
func presentMe(_ int, _ *http.Request) (ToJSON, error) {
return &EmptyList{}, nil
}
func userInfoHandler(w http.ResponseWriter, r *http.Request) {
var (
userID int
err error
)
if NeedAuth {
userID, err = verifyBearerAuth(r.Header.Get("Authorization"))
if err != nil {
c := Container{false, nil, 203}
http.Error(w, c.toJSON(), http.StatusUnauthorized)
return
}
} else {
userID = staticUserID
}
tojson, err := getUserInfo(userID, r)
if err != nil {
log.Println(err)
} else {
fmt.Fprint(w, tojson.toJSON())
}
}
func getUserInfo(userID int, _ *http.Request) (ToJSON, error) {
var (
userCode int64
isLockedNameDuplicate string
isSkillSealed string
staminaNotification string
hideRating string
recentScoreDate sql.NullInt64
)
info := new(UserInfo)
err := db.QueryRow(sqlStmtUserInfo, userID).Scan(
&info.Name,
&userCode,
&info.DisplaName,
&info.Ticket,
&info.PartID,
&isLockedNameDuplicate,
&isSkillSealed,
&info.CurrentMap,
&info.ProgBoost,
&info.Stamina,
&info.NextFragstamTs,
&info.MaxStaminaTs,
&staminaNotification,
&hideRating,
&info.Settings.FavoriteCharacter,
&recentScoreDate,
&info.MaxFriend,
&info.Rating,
&info.JoinDate,
)
if err != nil {
log.Println("Error occured while querying table PLAYER with USER_ID:", userID)
return nil, err
}
info.CurrAvailableMaps = []string{}
info.Friends = []string{}
info.Settings.StaminaNotification = staminaNotification == "t"
info.Settings.HideRating = hideRating == "t"
info.UserCode = fmt.Sprintf("%09d", userCode)
info.IsLockedNameDuplicate = isLockedNameDuplicate == "t"
info.IsSkillSealed = isSkillSealed == "t"
var charStatses []CharacterStats
if charStatses, err = getCharacterStats(userID, -1); err != nil {
return nil, err
}
info.CharacterStats = charStatses
characters := []int8{}
for _, status := range charStatses {
characters = append(characters, status.PartID)
}
info.Characters = characters
var worldUnlocks []string
if worldUnlocks, err = getItemList(userID, "world_unlock", "item_name"); err != nil {
return nil, err
}
info.WorldUnlocks = worldUnlocks
var worldSongUnlocks []string
if worldSongUnlocks, err = getItemList(userID, "world_song_unlock", "item_name"); err != nil {
return nil, err
}
info.WorldSongs = worldSongUnlocks
var packs []string
if packs, err = getItemList(userID, "pack_purchase_info", "pack_name"); err != nil {
return nil, err
}
info.Packs = packs
var singles []string
if singles, err = getItemList(userID, "single_purchase_info", "song_id"); err != nil {
return nil, err
}
info.Singles = singles
var coreInfoes []CoreInfo
if coreInfoes, err = getCoreInfo(userID); err != nil {
return nil, err
}
info.Cores = coreInfoes
var recentScore ScoreRecord
if recentScore, err = getMostRecentScore(userID); err != nil {
return nil, err
}
info.RecentScore = []ScoreRecord{recentScore}
var isAprilFools string
if err := db.QueryRow(sqlStmtAprilfools).Scan(&isAprilFools); err != nil {
log.Println("Error occured while reading April Fools info.")
return nil, err
}
info.IsAprilFools = isAprilFools == "t"
return info, nil
}
func getCoreInfo(userID int) ([]CoreInfo, error) {
rows, err := db.Query(sqlStmtCoreInfo, userID)
if err != nil {
log.Println("Error occured while querying table CORE_POSSESS_INFO")
return nil, err
}
defer rows.Close()
coreInfoes := []CoreInfo{}
var (
coreName string
internalID string
amount int8
)
for rows.Next() {
rows.Scan(&coreName, &internalID, &amount)
coreInfoes = append(coreInfoes, CoreInfo{coreName, amount, internalID})
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("error occured while querying core info: %w", err)
}
return coreInfoes, nil
}
func getMostRecentScore(userID int) (ScoreRecord, error) {
record := ScoreRecord{}
var modifier sql.NullInt32
err := db.QueryRow(sqlStmtMostRecentScore, userID).Scan(
&record.SongID, &record.Difficulty, &record.Score,
&record.Shiny, &record.Pure, &record.Far, &record.Lost,
&record.Health, &modifier,
&record.ClearType, &record.BestClearType,
)
if err != nil {
return record, fmt.Errorf("error occured while querying most recent score: %w", err)
}
if modifier.Valid {
record.Modifier = int(modifier.Int32)
} else {
record.Modifier = 0
}
return record, nil
}
func getItemList(userID int, tableName string, targetName string) ([]string, error) {
rows, err := db.Query(
fmt.Sprintf(
"select %s from %s where user_id = ?", targetName, tableName),
userID,
)
if err != nil {
log.Println("Error occured while querying table ", tableName)
return nil, err
}
defer rows.Close()
results := []string{}
var itemName string
for rows.Next() {
rows.Scan(&itemName)
results = append(results, itemName)
}
if err = rows.Err(); err != nil {
log.Println("Error occured while querying table ", tableName)
return nil, err
}
return results, nil
}
func userSettingHandler(w http.ResponseWriter, r *http.Request) {
targetPath := path.Base(r.URL.Path)
data, err := forms.Parse(r)
if err != nil {
log.Println(err)
}
var userID int
if NeedAuth {
userID, err = verifyBearerAuth(r.Header.Get("Authorization"))
if err != nil {
c := Container{false, nil, 203}
http.Error(w, c.toJSON(), http.StatusUnauthorized)
return
}
} else {
userID = staticUserID
}
val := data.Validator()
val.Require("value")
if val.HasErrors() {
log.Println("Immproper setting request with no field in form")
for k, v := range val.ErrorMap() {
log.Println(k, v)
}
return
}
target, ok := SettingMap[targetPath]
if !ok {
log.Printf(
"Unknow setting option: `%s`\n been passed to /user/me/setting",
r.URL.Path,
)
return
}
if target == "favorite_partner" {
partID := data.GetInt("value")
err = changeFavouritePartner(userID, partID)
} else {
value := data.GetBool("value")
err = changeSetting(userID, target, value)
}
if err != nil {
log.Println(err)
}
tojson, err := getUserInfo(userID, r)
if err != nil {
log.Println(err)
} else {
container := Container{true, tojson, 0}
fmt.Fprintln(w, container.toJSON())
}
}
func changeSetting(userID int, target string, isOn bool) error {
var value string
if isOn {
value = "t"
} else {
value = ""
}
if _, err := db.Exec(
fmt.Sprintf(sqlStmtUserSetting, target, value, userID),
); err != nil {
return fmt.Errorf(
"Error occured while modifying PLAYER for setting `%s` to `%v` with userID = %d: %w",
target, value, userID, err,
)
}
return nil
}
func changeFavouritePartner(userID int, partID int) error {
_, err := db.Exec(
fmt.Sprintf(sqlStmtFavouritePartner, partID, userID),
)
if err != nil {
return fmt.Errorf(
"Error occured while modifying PLAYER for setting `favorite_partner` to `%v` with userID = %d: %w",
partID, userID, err,
)
}
return nil
}