forked from fusion32/tibia-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.go
More file actions
743 lines (664 loc) · 20.7 KB
/
query.go
File metadata and controls
743 lines (664 loc) · 20.7 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
package main
import (
"encoding/binary"
"net"
"strings"
"sync"
"time"
)
// TQueryManagerConnection
// ==============================================================================
const (
APPLICATION_TYPE_GAME = 1
APPLICATION_TYPE_LOGIN = 2
APPLICATION_TYPE_WEB = 3
)
const (
QUERY_STATUS_OK = 0
QUERY_STATUS_ERROR = 1
QUERY_STATUS_FAILED = 3
)
const (
// TODO(fusion): There are newly created queries to support basic account
// management. A production ready website would need even more queries to
// allow account activation, recovery, deletion, password change, character
// deletion, etc...
QUERY_LOGIN = 0
QUERY_CHECK_ACCOUNT_PASSWORD = 10
QUERY_CREATE_ACCOUNT = 100
QUERY_CREATE_CHARACTER = 101
QUERY_GET_ACCOUNT_SUMMARY = 102
QUERY_GET_CHARACTER_PROFILE = 103
QUERY_GET_WORLDS = 150
QUERY_GET_ONLINE_CHARACTERS = 151
QUERY_GET_KILL_STATISTICS = 152
)
type (
TWorld struct {
Name string
Type string
NumPlayers int
MaxPlayers int
OnlinePeak int
OnlinePeakTimestamp int
LastStartup int
LastShutdown int
}
TAccountSummary struct {
AccountID int
Email string
PremiumDays int
PendingPremiumDays int
Deleted bool
Characters []TCharacterSummary
}
TCharacterSummary struct {
Name string
World string
Level int
Profession string
Online bool
Deleted bool
}
TCharacterProfile struct {
Name string
World string
Sex int
Guild string
Rank string
Title string
Level int
Profession string
Residence string
LastLogin int
PremiumDays int
Online bool
Deleted bool
}
TKillStatistics struct {
RaceName string
TimesKilled int
PlayersKilled int
}
TOnlineCharacter struct {
Name string
Level int
Profession string
}
TAccountCacheEntry struct {
AccountID int
Result int
Data TAccountSummary
LastAccess time.Time
}
TCharacterCacheEntry struct {
CharacterName string
Result int
Data TCharacterProfile
LastAccess time.Time
}
TKillStatisticsCacheEntry struct {
World string
Data []TKillStatistics
RefreshTime time.Time
}
TOnlineCharactersCacheEntry struct {
World string
Data []TOnlineCharacter
RefreshTime time.Time
}
TQueryManagerConnection struct {
Handle net.Conn
}
)
func (Connection *TQueryManagerConnection) Connect() bool {
if Connection.Handle != nil {
g_LogErr.Print("Already connected")
return false
}
var Err error
QueryManagerAddress := JoinHostPort(g_QueryManagerHost, g_QueryManagerPort)
Connection.Handle, Err = net.Dial("tcp4", QueryManagerAddress)
if Err != nil {
g_LogErr.Print(Err)
return false
}
var LoginBuffer [1024]byte
WriteBuffer := Connection.PrepareQuery(QUERY_LOGIN, LoginBuffer[:])
WriteBuffer.Write8(APPLICATION_TYPE_WEB)
WriteBuffer.WriteString(g_QueryManagerPassword)
Status, _ := Connection.ExecuteQuery(false, &WriteBuffer)
if Status != QUERY_STATUS_OK {
Connection.Disconnect()
g_LogErr.Printf("Failed to login to query manager (%v)", Status)
return false
}
return true
}
func (Connection *TQueryManagerConnection) Disconnect() {
if Connection.Handle != nil {
if Err := Connection.Handle.Close(); Err != nil {
g_LogErr.Print(Err)
}
Connection.Handle = nil
}
}
func (Connection *TQueryManagerConnection) PrepareQuery(QueryType int, Buffer []byte) TWriteBuffer {
WriteBuffer := TWriteBuffer{Buffer: Buffer, Position: 0}
WriteBuffer.Write16(0) // Request Size
WriteBuffer.Write8(uint8(QueryType))
return WriteBuffer
}
func (Connection *TQueryManagerConnection) ExecuteQuery(AutoReconnect bool, WriteBuffer *TWriteBuffer) (Status int, ReadBuffer TReadBuffer) {
// IMPORTANT(fusion): Different from the C++ version, there is no connection
// buffer, and the response is read into the same buffer used by `WriteBuffer`,
// to avoid moving data around when reconnecting in the middle of a query.
// TODO(fusion): Maybe join `TWriteBuffer` and `TReadBuffer` into `TQueryBuffer`
// to avoid confusion on how this function operates?
if WriteBuffer == nil || WriteBuffer.Position <= 2 {
panic("write buffer is empty")
}
RequestSize := WriteBuffer.Position - 2
if RequestSize < 0xFFFF {
WriteBuffer.Rewrite16(0, uint16(RequestSize))
} else {
WriteBuffer.Rewrite16(0, 0xFFFF)
WriteBuffer.Insert32(2, uint32(RequestSize))
}
Status = QUERY_STATUS_FAILED
if WriteBuffer.Overflowed() {
g_LogErr.Print("Write buffer overflowed")
return
}
const MaxAttempts = 2
Buffer := WriteBuffer.Buffer
WriteSize := WriteBuffer.Position
for Attempt := 1; true; Attempt += 1 {
if Connection.Handle == nil && (!AutoReconnect || !Connection.Connect()) {
return
}
if _, Err := Connection.Handle.Write(Buffer[:WriteSize]); Err != nil {
Connection.Disconnect()
if Attempt >= MaxAttempts {
g_LogErr.Printf("Failed to write request: %v", Err)
return
}
continue
}
var Help [4]byte
if _, Err := Connection.Handle.Read(Help[:2]); Err != nil {
Connection.Disconnect()
if Attempt >= MaxAttempts {
g_LogErr.Printf("Failed to read response size: %v", Err)
return
}
continue
}
ResponseSize := int(binary.LittleEndian.Uint16(Help[:2]))
if ResponseSize == 0xFFFF {
if _, Err := Connection.Handle.Read(Help[:]); Err != nil {
Connection.Disconnect()
g_LogErr.Printf("Failed to read response extended size: %v", Err)
return
}
ResponseSize = int(binary.LittleEndian.Uint32(Help[:]))
}
if ResponseSize <= 0 || ResponseSize > len(Buffer) {
Connection.Disconnect()
g_LogErr.Printf("Invalid response size %v (BufferSize: %v)",
ResponseSize, len(Buffer))
return
}
if _, Err := Connection.Handle.Read(Buffer[:ResponseSize]); Err != nil {
Connection.Disconnect()
g_LogErr.Printf("Failed to read response: %v", Err)
return
}
ReadBuffer = TReadBuffer{
Buffer: Buffer,
Position: 0,
}
Status = int(ReadBuffer.Read8())
return
}
// NOTE(fusion): The compiler complains there is no return statement here
// but the loop above can only exit by returning from the function which
// make anything after it UNREACHABLE.
return
}
func (Connection *TQueryManagerConnection) CheckAccountPassword(AccountID int, Password, IPAddress string) (Result int) {
var Buffer [1024]byte
WriteBuffer := Connection.PrepareQuery(QUERY_CHECK_ACCOUNT_PASSWORD, Buffer[:])
WriteBuffer.Write32(uint32(AccountID))
WriteBuffer.WriteString(Password)
WriteBuffer.WriteString(IPAddress)
Status, ReadBuffer := Connection.ExecuteQuery(true, &WriteBuffer)
Result = -1
switch Status {
case QUERY_STATUS_OK:
Result = 0
case QUERY_STATUS_ERROR:
ErrorCode := int(ReadBuffer.Read8())
if ErrorCode >= 1 && ErrorCode <= 4 {
Result = ErrorCode
} else {
g_LogErr.Printf("Invalid error code %v", ErrorCode)
}
default:
g_LogErr.Printf("Request failed (%v)", Status)
}
return
}
func (Connection *TQueryManagerConnection) CreateAccount(AccountID int, Email string, Password string) (Result int) {
var Buffer [1024]byte
WriteBuffer := Connection.PrepareQuery(QUERY_CREATE_ACCOUNT, Buffer[:])
WriteBuffer.Write32(uint32(AccountID))
WriteBuffer.WriteString(Email)
WriteBuffer.WriteString(Password)
Status, ReadBuffer := Connection.ExecuteQuery(true, &WriteBuffer)
Result = -1
switch Status {
case QUERY_STATUS_OK:
Result = 0
case QUERY_STATUS_ERROR:
ErrorCode := int(ReadBuffer.Read8())
if ErrorCode >= 1 && ErrorCode <= 2 {
Result = ErrorCode
} else {
g_LogErr.Printf("Invalid error code %v", ErrorCode)
}
default:
g_LogErr.Printf("Request failed (%v)", Status)
}
return
}
func (Connection *TQueryManagerConnection) CreateCharacter(World string, AccountID int, Name string, Sex int) (Result int) {
var Buffer [1024]byte
WriteBuffer := Connection.PrepareQuery(QUERY_CREATE_CHARACTER, Buffer[:])
WriteBuffer.WriteString(World)
WriteBuffer.Write32(uint32(AccountID))
WriteBuffer.WriteString(Name)
WriteBuffer.Write8(uint8(Sex))
Status, ReadBuffer := Connection.ExecuteQuery(true, &WriteBuffer)
Result = -1
switch Status {
case QUERY_STATUS_OK:
Result = 0
case QUERY_STATUS_ERROR:
ErrorCode := int(ReadBuffer.Read8())
if ErrorCode >= 1 && ErrorCode <= 3 {
Result = ErrorCode
} else {
g_LogErr.Printf("Invalid error code %v", ErrorCode)
}
default:
g_LogErr.Printf("Request failed (%v)", Status)
}
return
}
func (Connection *TQueryManagerConnection) GetAccountSummary(AccountID int) (Result int, Account TAccountSummary) {
var Buffer [16384]byte
WriteBuffer := Connection.PrepareQuery(QUERY_GET_ACCOUNT_SUMMARY, Buffer[:])
WriteBuffer.Write32(uint32(AccountID))
Status, ReadBuffer := Connection.ExecuteQuery(true, &WriteBuffer)
Result = -1
switch Status {
case QUERY_STATUS_OK:
Result = 0
Account.AccountID = AccountID
Account.Email = ReadBuffer.ReadString()
Account.PremiumDays = int(ReadBuffer.Read16())
Account.PendingPremiumDays = int(ReadBuffer.Read16())
Account.Deleted = ReadBuffer.ReadFlag()
NumCharacters := int(ReadBuffer.Read8())
if NumCharacters > 0 {
Account.Characters = make([]TCharacterSummary, NumCharacters)
for Index := range Account.Characters {
Account.Characters[Index].Name = ReadBuffer.ReadString()
Account.Characters[Index].World = ReadBuffer.ReadString()
Account.Characters[Index].Level = int(ReadBuffer.Read16())
Account.Characters[Index].Profession = ReadBuffer.ReadString()
Account.Characters[Index].Online = ReadBuffer.ReadFlag()
Account.Characters[Index].Deleted = ReadBuffer.ReadFlag()
}
}
case QUERY_STATUS_ERROR:
ErrorCode := int(ReadBuffer.Read8())
if ErrorCode >= 1 && ErrorCode <= 4 {
Result = ErrorCode
} else {
g_LogErr.Printf("Invalid error code %v", ErrorCode)
}
default:
g_LogErr.Printf("Request failed (%v)", Status)
}
return
}
func (Connection *TQueryManagerConnection) GetCharacterProfile(CharacterName string) (Result int, Character TCharacterProfile) {
var Buffer [16384]byte
WriteBuffer := Connection.PrepareQuery(QUERY_GET_CHARACTER_PROFILE, Buffer[:])
WriteBuffer.WriteString(CharacterName)
Status, ReadBuffer := Connection.ExecuteQuery(true, &WriteBuffer)
Result = -1
switch Status {
case QUERY_STATUS_OK:
Result = 0
Character.Name = ReadBuffer.ReadString()
Character.World = ReadBuffer.ReadString()
Character.Sex = int(ReadBuffer.Read8())
Character.Guild = ReadBuffer.ReadString()
Character.Rank = ReadBuffer.ReadString()
Character.Title = ReadBuffer.ReadString()
Character.Level = int(ReadBuffer.Read16())
Character.Profession = ReadBuffer.ReadString()
Character.Residence = ReadBuffer.ReadString()
Character.LastLogin = int(ReadBuffer.Read32())
Character.PremiumDays = int(ReadBuffer.Read16())
Character.Online = ReadBuffer.ReadFlag()
Character.Deleted = ReadBuffer.ReadFlag()
case QUERY_STATUS_ERROR:
ErrorCode := int(ReadBuffer.Read8())
if ErrorCode == 1 {
Result = ErrorCode
} else {
g_LogErr.Printf("Invalid error code %v", ErrorCode)
}
default:
g_LogErr.Printf("Request failed (%v)", Status)
}
return
}
func (Connection *TQueryManagerConnection) GetWorlds() (Result int, Worlds []TWorld) {
var Buffer [16384]byte
WriteBuffer := Connection.PrepareQuery(QUERY_GET_WORLDS, Buffer[:])
Status, ReadBuffer := Connection.ExecuteQuery(true, &WriteBuffer)
Result = -1
switch Status {
case QUERY_STATUS_OK:
Result = 0
NumWorlds := int(ReadBuffer.Read8())
if NumWorlds > 0 {
Worlds = make([]TWorld, NumWorlds)
for Index := range Worlds {
Worlds[Index].Name = ReadBuffer.ReadString()
Worlds[Index].Type = WorldTypeString(int(ReadBuffer.Read8()))
Worlds[Index].NumPlayers = int(ReadBuffer.Read16())
Worlds[Index].MaxPlayers = int(ReadBuffer.Read16())
Worlds[Index].OnlinePeak = int(ReadBuffer.Read16())
Worlds[Index].OnlinePeakTimestamp = int(ReadBuffer.Read32())
Worlds[Index].LastStartup = int(ReadBuffer.Read32())
Worlds[Index].LastShutdown = int(ReadBuffer.Read32())
}
}
default:
g_LogErr.Printf("Request failed (%v)", Status)
}
return
}
func (Connection *TQueryManagerConnection) GetOnlineCharacters(World string) (Result int, Characters []TOnlineCharacter) {
var Buffer [65536]byte
WriteBuffer := Connection.PrepareQuery(QUERY_GET_ONLINE_CHARACTERS, Buffer[:])
WriteBuffer.WriteString(World)
Status, ReadBuffer := Connection.ExecuteQuery(true, &WriteBuffer)
Result = -1
switch Status {
case QUERY_STATUS_OK:
Result = 0
NumCharacters := int(ReadBuffer.Read16())
if NumCharacters > 0 {
Characters = make([]TOnlineCharacter, NumCharacters)
for Index := 0; Index < NumCharacters; Index += 1 {
Characters[Index].Name = ReadBuffer.ReadString()
Characters[Index].Level = int(ReadBuffer.Read16())
Characters[Index].Profession = ReadBuffer.ReadString()
}
}
default:
g_LogErr.Printf("Request failed (%v)", Status)
}
return
}
func (Connection *TQueryManagerConnection) GetKillStatistics(World string) (Result int, Stats []TKillStatistics) {
var Buffer [65536]byte
WriteBuffer := Connection.PrepareQuery(QUERY_GET_KILL_STATISTICS, Buffer[:])
WriteBuffer.WriteString(World)
Status, ReadBuffer := Connection.ExecuteQuery(true, &WriteBuffer)
Result = -1
switch Status {
case QUERY_STATUS_OK:
Result = 0
NumStats := int(ReadBuffer.Read16())
if NumStats > 0 {
Stats = make([]TKillStatistics, NumStats)
for Index := 0; Index < NumStats; Index += 1 {
Stats[Index].RaceName = ReadBuffer.ReadString()
Stats[Index].PlayersKilled = int(ReadBuffer.Read32())
Stats[Index].TimesKilled = int(ReadBuffer.Read32())
}
}
default:
g_LogErr.Printf("Request failed (%v)", Status)
}
return
}
// Query Subsystem
// ==============================================================================
var (
g_QueryManagerMutex sync.Mutex
g_QueryManagerConnection TQueryManagerConnection
g_AccountCache []TAccountCacheEntry
g_CharacterCache []TCharacterCacheEntry
g_WorldCache []TWorld
g_WorldCacheRefreshTime time.Time
g_OnlineCharactersCache []TOnlineCharactersCacheEntry
g_KillStatisticsCache []TKillStatisticsCacheEntry
)
func InitQuery() bool {
g_Log.Printf("QueryManagerHost: %v", g_QueryManagerHost)
g_Log.Printf("QueryManagerPort: %v", g_QueryManagerPort)
g_Log.Printf("MaxCachedAccounts: %v", g_MaxCachedAccounts)
g_Log.Printf("MaxCachedCharacters: %v", g_MaxCachedCharacters)
g_Log.Printf("CharacterRefreshInterval: %v", g_CharacterRefreshInterval)
g_Log.Printf("WorldRefreshInterval: %v", g_WorldRefreshInterval)
Result := g_QueryManagerConnection.Connect()
if !Result {
g_LogErr.Print("Failed to connect to query manager")
}
return Result
}
func ExitQuery() {
g_QueryManagerConnection.Disconnect()
}
func CheckAccountPassword(AccountID int, Password, IPAddress string) int {
g_QueryManagerMutex.Lock()
defer g_QueryManagerMutex.Unlock()
return g_QueryManagerConnection.CheckAccountPassword(AccountID, Password, IPAddress)
}
func CreateAccount(AccountID int, Email string, Password string) int {
g_QueryManagerMutex.Lock()
defer g_QueryManagerMutex.Unlock()
return g_QueryManagerConnection.CreateAccount(AccountID, Email, Password)
}
func CreateCharacter(World string, AccountID int, Name string, Sex int) int {
g_QueryManagerMutex.Lock()
defer g_QueryManagerMutex.Unlock()
return g_QueryManagerConnection.CreateCharacter(World, AccountID, Name, Sex)
}
func GetAccountSummary(AccountID int) (Result int, Account TAccountSummary) {
g_QueryManagerMutex.Lock()
defer g_QueryManagerMutex.Unlock()
if g_AccountCache == nil {
g_AccountCache = make([]TAccountCacheEntry, g_MaxCachedAccounts)
}
var Entry *TAccountCacheEntry
LeastRecentlyUsedIndex := 0
LeastRecentlyUsedTime := g_AccountCache[0].LastAccess
for Index := 0; Index < len(g_AccountCache); Index += 1 {
Current := &g_AccountCache[Index]
// NOTE(fusion): Account data itself shouldn't change over time unless
// we do it ourselves, in which case `InvalidateAccountCachedData` is
// used to invalidate the cache entry. The problem is that the account
// summary also includes character data which will change, depending on
// activities on the game server.
if time.Since(Current.LastAccess) >= g_CharacterRefreshInterval {
*Current = TAccountCacheEntry{}
}
if Current.LastAccess.Before(LeastRecentlyUsedTime) {
LeastRecentlyUsedIndex = Index
LeastRecentlyUsedTime = Current.LastAccess
}
if Current.AccountID == AccountID {
Entry = Current
break
}
}
if Entry == nil {
Result, Account = g_QueryManagerConnection.GetAccountSummary(AccountID)
if Result == 0 {
Entry = &g_AccountCache[LeastRecentlyUsedIndex]
Entry.AccountID = AccountID
Entry.Data = Account
Entry.LastAccess = time.Now()
}
} else {
Result = 0
Account = Entry.Data
Entry.LastAccess = time.Now()
}
return
}
func InvalidateAccountCachedData(AccountID int) {
g_QueryManagerMutex.Lock()
defer g_QueryManagerMutex.Unlock()
for Index := 0; Index < len(g_AccountCache); Index += 1 {
if g_AccountCache[Index].AccountID == AccountID {
g_AccountCache[Index] = TAccountCacheEntry{}
break
}
}
}
func GetCharacterProfile(CharacterName string) (Result int, Character TCharacterProfile) {
g_QueryManagerMutex.Lock()
defer g_QueryManagerMutex.Unlock()
if g_CharacterCache == nil {
g_CharacterCache = make([]TCharacterCacheEntry, g_MaxCachedCharacters)
}
var Entry *TCharacterCacheEntry
LeastRecentlyUsedIndex := 0
LeastRecentlyUsedTime := g_CharacterCache[0].LastAccess
for Index := 0; Index < len(g_CharacterCache); Index += 1 {
Current := &g_CharacterCache[Index]
if time.Since(Current.LastAccess) >= g_CharacterRefreshInterval {
*Current = TCharacterCacheEntry{}
}
if Current.LastAccess.Before(LeastRecentlyUsedTime) {
LeastRecentlyUsedIndex = Index
LeastRecentlyUsedTime = Current.LastAccess
}
if strings.EqualFold(Current.CharacterName, CharacterName) {
Entry = Current
break
}
}
if Entry == nil {
Result, Character = g_QueryManagerConnection.GetCharacterProfile(CharacterName)
Entry = &g_CharacterCache[LeastRecentlyUsedIndex]
Entry.CharacterName = CharacterName
Entry.Result = Result
Entry.Data = Character
Entry.LastAccess = time.Now()
} else {
Result = Entry.Result
Character = Entry.Data
Entry.LastAccess = time.Now()
}
return
}
func GetWorlds() []TWorld {
g_QueryManagerMutex.Lock()
defer g_QueryManagerMutex.Unlock()
if time.Until(g_WorldCacheRefreshTime) <= 0 {
// IMPORTANT(fusion): `GetWorlds` will return a FRESH slice. This will
// prevent race conditions regarding any previous world slice, assuming
// we're only reading from them.
Result, Worlds := g_QueryManagerConnection.GetWorlds()
if Result == 0 {
g_WorldCache = Worlds
g_WorldCacheRefreshTime = time.Now().Add(g_WorldRefreshInterval)
}
}
return g_WorldCache
}
func GetWorld(World string) *TWorld {
Worlds := GetWorlds()
for Index := range Worlds {
if strings.EqualFold(Worlds[Index].Name, World) {
return &Worlds[Index]
}
}
return nil
}
func GetOnlineCharacters(World string) []TOnlineCharacter {
g_QueryManagerMutex.Lock()
defer g_QueryManagerMutex.Unlock()
var Entry *TOnlineCharactersCacheEntry
for Index := 0; Index < len(g_OnlineCharactersCache); Index += 1 {
Current := &g_OnlineCharactersCache[Index]
if time.Until(Current.RefreshTime) <= 0 {
g_OnlineCharactersCache = SwapAndPop(g_OnlineCharactersCache, Index)
Index -= 1
continue
}
if strings.EqualFold(Current.World, World) {
Entry = Current
break
}
}
if Entry == nil {
Result, Characters := g_QueryManagerConnection.GetOnlineCharacters(World)
if Result == 0 {
g_OnlineCharactersCache = append(g_OnlineCharactersCache, TOnlineCharactersCacheEntry{})
Entry = &g_OnlineCharactersCache[len(g_OnlineCharactersCache)-1]
Entry.World = World
Entry.Data = Characters
Entry.RefreshTime = time.Now().Add(g_WorldRefreshInterval)
}
}
if Entry != nil {
return Entry.Data
} else {
return nil
}
}
func GetKillStatistics(World string) []TKillStatistics {
g_QueryManagerMutex.Lock()
defer g_QueryManagerMutex.Unlock()
var Entry *TKillStatisticsCacheEntry
for Index := 0; Index < len(g_KillStatisticsCache); Index += 1 {
Current := &g_KillStatisticsCache[Index]
if time.Until(Current.RefreshTime) <= 0 {
g_KillStatisticsCache = SwapAndPop(g_KillStatisticsCache, Index)
Index -= 1
continue
}
if strings.EqualFold(Current.World, World) {
Entry = Current
break
}
}
if Entry == nil {
Result, Stats := g_QueryManagerConnection.GetKillStatistics(World)
if Result == 0 {
g_KillStatisticsCache = append(g_KillStatisticsCache, TKillStatisticsCacheEntry{})
Entry = &g_KillStatisticsCache[len(g_KillStatisticsCache)-1]
Entry.World = World
Entry.Data = Stats
Entry.RefreshTime = time.Now().Add(g_WorldRefreshInterval)
}
}
if Entry != nil {
return Entry.Data
} else {
return nil
}
}