-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter.go
157 lines (140 loc) · 3.48 KB
/
character.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
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"github.com/albrow/forms"
"github.com/gorilla/mux"
)
var voiceList = []int{0, 1, 2, 3, 100, 1000, 1001}
func getCharacterStats(userID int, partID int8) ([]CharacterStats, error) {
cond := ""
if partID >= 0 {
cond = fmt.Sprintf(sqlStmtSingleCharCond, partID)
}
statses := []CharacterStats{}
stmt, err := db.Prepare(sqlStmtCharStaticStats)
if err != nil {
return nil, err
}
defer stmt.Close()
rows, err := db.Query(sqlStmtOwnedChar+cond, userID)
if err != nil {
return statses, err
}
defer rows.Close()
var (
hasVoice int
isUncappedOverride string
isUncapped string
skillRequiresUncap string
)
for rows.Next() {
stats := new(CharacterStats)
rows.Scan(
&stats.PartID,
&isUncappedOverride,
&isUncapped,
&stats.Overdrive,
&stats.Prog,
&stats.Frag,
&stats.ProgTempest,
&stats.Level,
&stats.Exp,
&stats.LevelExp,
)
if err := stmt.QueryRow(stats.PartID).Scan(
&hasVoice,
&stats.SkillID,
&stats.SkillIDUncap,
&skillRequiresUncap,
&stats.SkillUnlockLevel,
&stats.PartName,
&stats.CharType,
); err != nil {
return nil, err
}
if hasVoice != -1 {
stats.Voice = voiceList
}
stats.IsUncappedOverride = isUncappedOverride == "t"
stats.IsUncapped = isUncapped == "t"
stats.SkillRequiresUncap = skillRequiresUncap == "t"
stats.UncapCores = []string{}
statses = append(statses, *stats)
}
if err := rows.Err(); err != nil {
return statses, fmt.Errorf("Error occured while querying table PART_STATS for single character, userID = %d, partID = %d: %w", userID, partID, err)
}
return statses, nil
}
func changeCharacter(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
}
data, err := forms.Parse(r)
if err != nil {
log.Println(err)
}
val := data.Validator()
val.Require("character")
val.Require("skill_sealed")
character := data.Get("character")
skillSealed := data.Get("skill_sealed")
if val, err := strconv.ParseBool(skillSealed); err == nil && val {
skillSealed = "t"
} else {
skillSealed = ""
}
if _, err := db.Exec(sqlStmtChangeChar, character, skillSealed, userID); err != nil {
log.Println(err)
}
fmt.Fprintf(
w,
`{"success": true,"value": {"user_id": %d, "character": %s}}`,
userID, character,
)
}
func toggleUncap(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
}
container := Container{true, nil, 0}
partID, err := strconv.Atoi(mux.Vars(r)["partID"])
if err != nil {
log.Printf("Error character ID for toggle uncap partID = %d: %s\n", partID, err)
container.Success = false
} else if _, err = db.Exec(sqlStmtToggleUncap, userID, partID); err != nil {
log.Printf("Error occured while modifying uncap toggle state in table: %s\n", err)
container.Success = false
} else if stats, err := getCharacterStats(userID, int8(partID)); err != nil {
log.Println(err)
container.Success = false
} else {
container.Value = &ToggleResult{userID, stats}
}
fmt.Fprint(w, container.toJSON())
}