-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.go
157 lines (138 loc) · 3.42 KB
/
world.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 (
"database/sql"
"fmt"
"log"
"net/http"
)
func myMapInfoHandler(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 := getMyMapInfo(userID, r)
if err != nil {
log.Println(err)
} else {
fmt.Fprint(w, tojson.toJSON())
}
}
func getMyMapInfo(userID int, _ *http.Request) (ToJSON, error) {
var (
isBeyond string
isLegacy string
isLocked string
isRepeatable string
rewards []Reward
)
rows, err := db.Query(sqlStmtMapInfo, userID)
if err != nil {
log.Println("Error occured while querying table WORLD_MAP.")
return nil, err
}
defer rows.Close()
infoes := []MapInfo{}
for rows.Next() {
info := new(MapInfo)
rows.Scan(
&info.AvailableFrom,
&info.AvailableTo,
&info.BeyondHealth,
&info.Chapter,
&info.Coordinate,
&info.CustomBG,
&isBeyond,
&isLegacy,
&isRepeatable,
&info.MapID,
&info.RequireID,
&info.RequireType,
&info.RequireValue,
&info.StamCost,
&info.StepCount,
&info.CurrCapture,
&info.CurrPosition,
&isLocked,
)
info.IsBeyond = isBeyond == "t"
info.IsLegacy = isLegacy == "t"
info.IsLocked = isLocked == "t"
info.IsRepeatable = isRepeatable == "t"
info.PartAffinity, info.AffMultiplier, err = getMapAffinity(info.MapID)
if err != nil {
return nil, err
}
rewards, err = getRewards(info.MapID)
if err != nil {
return nil, err
}
info.Rewards = rewards
infoes = append(infoes, *info)
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("error occured while reading map info: %w", err)
}
var currMap string
err = db.QueryRow(sqlStmtCurrentMap, userID).Scan(&currMap)
if err != nil {
return nil, fmt.Errorf("error occur while querying current map for user = %d: %w", userID, err)
}
return &MapInfoContainer{userID, currMap, infoes}, nil
}
func getMapAffinity(mapID string) ([]int8, []float64, error) {
partners, multipliers := []int8{}, []float64{}
rows, err := db.Query(sqlStmtMapAffinity, mapID)
if err != nil {
return nil, nil, fmt.Errorf("error occured while querying map affinity for map = %s: %w", mapID, err)
}
defer rows.Close()
var (
partID int8
mul float64
)
for rows.Next() {
rows.Scan(&partID, &mul)
partners = append(partners, partID)
multipliers = append(multipliers, mul)
}
if err = rows.Err(); err != nil {
return nil, nil, fmt.Errorf("Error occured while reading rows queried from MAP_AFFINITY: %w", err)
}
return partners, multipliers, nil
}
func getRewards(mapID string) ([]Reward, error) {
rows, err := db.Query(sqlStmtRewards, mapID)
if err != nil {
return nil, fmt.Errorf("Error occured while querying table MAP_REWARD with MAP_ID = %s: %w", mapID, err)
}
defer rows.Close()
rewards := []Reward{}
var (
position int
amount sql.NullInt32
)
for rows.Next() {
item := new(RewardItem)
rows.Scan(&item.ItemID, &item.ItemType, &amount, &position)
item.Amount = amount.Int32
rewards = append(rewards, Reward{
Items: []RewardItem{*item},
Position: position,
})
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf(
"Error occured while reading rows queried from tabletable MAP_REWARD with MAP_ID = %s: %w", mapID, err)
}
return rewards, nil
}