forked from fluofoxxo/outrun
/
player.go
348 lines (342 loc) · 8.55 KB
/
player.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package netobj
import (
"fmt"
"time"
"github.com/fluofoxxo/outrun/config/eventconf"
"github.com/fluofoxxo/outrun/enums"
"github.com/fluofoxxo/outrun/obj"
"github.com/fluofoxxo/outrun/obj/constobjs"
)
type Player struct {
ID string `json:"userID"`
Username string `json:"username"`
Password string `json:"password"`
Key string `json:"key"`
LastLogin int64 // TODO: use `json:"lastLogin"`
PlayerState PlayerState `json:"playerState"`
CharacterState []Character `json:"characterState"`
ChaoState []Chao `json:"chaoState"`
MileageMapState MileageMapState `json:"mileageMapState"`
MileageFriends []MileageFriend `json:"mileageFriendList"`
PlayerVarious PlayerVarious `json:"playerVarious"`
LastWheelOptions WheelOptions `json:"ORN_wheelOptions"` // TODO: Make RouletteGroup to hold LastWheelOptions and RouletteInfo?
RouletteInfo RouletteInfo `json:"ORN_rouletteInfo"`
ChaoRouletteGroup ChaoRouletteGroup `json:"ORN_chaoRouletteGroup"`
PersonalEvents []eventconf.ConfiguredEvent `json:"ORN_personalEvents"`
}
func NewPlayer(id, username, password, key string, playerState PlayerState, characterState []Character, chaoState []Chao, mileageMapState MileageMapState, mf []MileageFriend, playerVarious PlayerVarious, wheelOptions WheelOptions, rouletteInfo RouletteInfo, chaoRouletteGroup ChaoRouletteGroup, personalEvents []eventconf.ConfiguredEvent) Player {
return Player{
id,
username,
password,
key,
time.Now().Unix(),
playerState,
characterState,
chaoState,
mileageMapState,
mf,
playerVarious,
wheelOptions,
rouletteInfo,
chaoRouletteGroup,
personalEvents,
}
}
/*
func (p *Player) Save() {
}
*/
// TODO: remove any functions that access p.PlayerState since we are not calling from a pointer anyways and it will not modify the object
func (p *Player) AddRings(amount int64) {
ps := p.PlayerState
ps.NumRings += amount
}
func (p *Player) SubRings(amount int64) {
ps := p.PlayerState
ps.NumRings -= amount
}
func (p *Player) AddRedRings(amount int64) {
ps := p.PlayerState
ps.NumRedRings += amount
}
func (p *Player) SubRedRings(amount int64) {
ps := p.PlayerState
ps.NumRedRings -= amount
}
func (p *Player) SetUsername(username string) {
p.Username = username
}
func (p *Player) SetPassword(password string) {
p.Password = password
}
func (p *Player) AddEnergy(amount int64) {
ps := p.PlayerState
ps.Energy += amount
}
func (p *Player) SubEnergy(amount int64) {
ps := p.PlayerState
ps.Energy -= amount
}
func (p *Player) SetMainCharacter(cid string) {
ps := p.PlayerState
ps.MainCharaID = cid
}
func (p *Player) SetSubCharacter(cid string) {
ps := p.PlayerState
ps.SubCharaID = cid
}
func (p *Player) SetMainChao(chid string) {
ps := p.PlayerState
ps.MainChaoID = chid
}
func (p *Player) SetSubChao(chid string) {
ps := p.PlayerState
ps.SubChaoID = chid
}
func (p *Player) AddItem(item obj.Item) {
ps := p.PlayerState
ps.Items = append(ps.Items, item)
}
func (p *Player) RemoveItemOf(iid string) bool {
newItems := []obj.Item{}
foundItem := false
ps := p.PlayerState
for _, item := range ps.Items {
if item.ID != iid || foundItem {
newItems = append(newItems, item)
} else if !foundItem {
foundItem = true
}
}
ps.Items = newItems
return foundItem
}
func (p *Player) IndexOfItem(iid string) int {
for i, item := range p.PlayerState.Items {
if item.ID == iid {
return i
}
}
return -1
}
func (p *Player) RemoveAllItemsOf(iid string) {
for p.RemoveItemOf(iid) {
}
}
func (p *Player) AddAnimals(amount int64) {
ps := p.PlayerState
ps.Animals += amount
}
func (p *Player) SubAnimals(amount int64) {
ps := p.PlayerState
ps.Animals -= amount
}
func (p *Player) ApplyHighScore(score int64) bool {
ps := p.PlayerState
if ps.HighScore < score {
ps.HighScore = score
return true
}
return false
}
func (p *Player) AddDistance(amount int64) {
ps := p.PlayerState
ps.TotalDistance += amount
p.ApplyHighDistance(amount)
}
func (p *Player) ApplyHighDistance(amount int64) {
ps := p.PlayerState
ps.HighDistance = amount
}
func (p *Player) AddNewChaoByID(chid string) bool {
chao := constobjs.Chao[chid]
netchao := NewNetChao(
chao,
enums.ChaoStatusOwned, // TODO: does the idea that a chao is owned mean that it's possible to send chao that are not owned?
1,
enums.ChaoDealingNone,
1, // implies that adding means acquired. This may not be the case if we can send non-owned chao.
)
return p.AddNetChao(netchao)
}
func (p *Player) AddNewChao(chao obj.Chao) bool {
netchao := NewNetChao(
chao,
enums.ChaoStatusOwned,
1,
enums.ChaoDealingNone,
1,
)
return p.AddNetChao(netchao)
}
func (p *Player) AddNetChao(netchao Chao) bool {
// Returns whether or not the Chao was already found.
// It will not add Chao already in the ChaoState.
if !p.HasChao(netchao.Chao.ID) {
p.ChaoState = append(p.ChaoState, netchao)
return false
}
return true
}
func (p *Player) HasChao(chid string) bool {
for _, netchao := range p.ChaoState {
if netchao.Chao.ID == chid {
return true
}
}
return false
}
func (p *Player) GetChara(cid string) (Character, error) {
var char Character
found := false
for _, c := range p.CharacterState {
if c.ID == cid {
char = c
found = true
}
}
if !found {
return char, fmt.Errorf("character not found")
}
return char, nil
}
func (p *Player) IndexOfChara(cid string) int {
for i, char := range p.CharacterState {
if char.ID == cid {
return i
}
}
return -1
}
func (p *Player) GetChao(chid string) (Chao, error) {
var chao Chao
found := false
for _, c := range p.ChaoState {
if c.ID == chid {
chao = c
found = true
}
}
if !found {
return chao, fmt.Errorf("chao not found")
}
return chao, nil
}
func (p *Player) IndexOfChao(chid string) int {
for i, chao := range p.ChaoState {
if chao.ID == chid {
return i
}
}
return -1
}
func (p *Player) GetMainChara() (Character, error) {
ps := p.PlayerState
cid := ps.MainCharaID
char, err := p.GetChara(cid)
return char, err
}
func (p *Player) GetSubChara() (Character, error) {
ps := p.PlayerState
cid := ps.SubCharaID
char, err := p.GetChara(cid)
return char, err
}
func (p *Player) GetMainChao() (Chao, error) {
ps := p.PlayerState
chid := ps.MainChaoID
chao, err := p.GetChao(chid)
return chao, err
}
func (p *Player) GetSubChao() (Chao, error) {
ps := p.PlayerState
chid := ps.SubChaoID
chao, err := p.GetChao(chid)
return chao, err
}
func (p *Player) GetMaxLevelChao() []Chao {
mxlvl := []Chao{}
for _, c := range p.ChaoState {
if c.Level >= 10 { // if max level (or above)
mxlvl = append(mxlvl, c)
}
}
return mxlvl
}
func (p *Player) GetMaxLevelChaoIDs() []string {
chao := p.GetMaxLevelChao()
ids := []string{}
for _, c := range chao {
if c.Level >= 10 { // if max level (or above)
ids = append(ids, c.ID)
}
}
return ids
}
func (p *Player) GetMaxLevelCharacters() []Character {
mxlvl := []Character{}
for _, ch := range p.CharacterState {
if ch.Star >= 10 { // if max stars (or above)
mxlvl = append(mxlvl, ch)
}
}
return mxlvl
}
func (p *Player) GetMaxLevelCharacterIDs() []string {
chars := p.GetMaxLevelCharacters()
ids := []string{}
for _, ch := range chars {
//if ch.Level >= 100 { // if max level (or above)
if ch.Star >= 10 {
ids = append(ids, ch.ID)
}
}
return ids
}
func (p *Player) GetAllMaxLevelIDs() []string {
chars := p.GetMaxLevelCharacterIDs()
chao := p.GetMaxLevelChaoIDs()
combined := []string{}
combined = append(combined, chars...)
combined = append(combined, chao...)
return combined
}
func (p *Player) AllChaoMaxLevel() bool {
for _, c := range p.ChaoState {
if c.Level < 10 {
return false
}
}
return true
}
func (p *Player) AllCharactersMaxLevel() bool {
for _, ch := range p.CharacterState {
if ch.Star < 10 {
return false
}
}
return true
}
func (p *Player) GetAllNonMaxedChaoAndCharacters() []string {
result := append(p.GetAllNonMaxedChao(), p.GetAllNonMaxedCharacters()...) // combine two
return result
}
func (p *Player) GetAllNonMaxedChao() []string {
result := []string{}
for _, chao := range p.ChaoState {
if chao.Level < 10 { // not max level
result = append(result, chao.ID)
}
}
return result
}
func (p *Player) GetAllNonMaxedCharacters() []string {
result := []string{}
for _, character := range p.CharacterState {
if character.Star < 10 { // not max star
result = append(result, character.ID)
}
}
return result
}