-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.go
72 lines (66 loc) · 1.48 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
package main
import (
"fmt"
"log"
"net/http"
)
func gameInfoHandler(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 := getGameInfo(userID, r)
if err != nil {
log.Println(err)
} else {
fmt.Fprint(w, tojson.toJSON())
}
}
func getGameInfo(_ int, _ *http.Request) (ToJSON, error) {
var (
worldRankingEnabled string
isBydChapterUnlocked string
)
info := new(GameInfo)
err := db.QueryRow(slqStmtGameInfo).Scan(
&info.Now,
&info.MaxStam,
&info.StaminaRecoverTick,
&info.CoreExp,
&worldRankingEnabled,
&isBydChapterUnlocked,
)
if err != nil {
log.Println("Error occured while querying GAME_INFO")
return nil, err
}
levelsteps := []levelStep{}
rows, err := db.Query(sqlStmtLevelStep)
if err != nil {
return nil, fmt.Errorf("Error occured while querying table LEVEL_EXP: %w", err)
}
defer rows.Close()
step := new(levelStep)
for rows.Next() {
rows.Scan(&step.Lv, &step.Exp)
levelsteps = append(levelsteps, *step)
}
if err := rows.Err(); err != nil {
log.Println("Error occured while reading rows from LEVEL_EXP")
return nil, err
}
info.WorldRankingEnabled = worldRankingEnabled == "t"
info.BydUnlocked = isBydChapterUnlocked == "t"
info.LevelSteps = levelsteps
return info, nil
}