-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathstorage.lua
More file actions
executable file
·2411 lines (1951 loc) · 65.4 KB
/
Copy pathstorage.lua
File metadata and controls
executable file
·2411 lines (1951 loc) · 65.4 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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--====================================================================--
-- Module: storage
--
-- Copyright (C) 2013-2015 Anedix Technologies, Inc. All Rights Reserved.
--
-- License:
--
--
-- Overview:
--
-- This module allows you to save game information and history.
--
--
-- Usage:
--
-- local storage = require("storage")
--
--
-- Notice:
--
--
--====================================================================--
--
local M = {}
M.filter = ""
M.orderby = ""
M.groupby = ""
M.searchResults = { tm=0, total=0, from=0, to=0, per_page = 5, page_count=0, page=0, method=nil, query="", filter=false, query="" }
M.pageinate_anim = false
M.callback = nil
M.callbackParms = {}
M.callbackTeamObj = nil
M.callbackDtype = nil
M.entryId = 0
M.entryPos = 0
M.mode = "insert"
M.game = {
id=0,
name="QUICKPLAY",
gametype=0,
mdate=0,
edate=0,
rowstate=1
}
M.team = {
id=0,
name="",
mdate=0,
rowstate=1
}
M.record = {
id=0,
name="",
team=0,
scoreint=0,
scoreflt=0.0,
scoretyp=0,
gameid=0,
color="grey",
mdate=0,
rowstate=1
}
M.log = {
id=0,
name="",
player=0,
team=0,
scoreint=0,
scoreflt=0.0,
scoretyp=0,
gameid=0,
gametype=0,
color="grey",
mdate=0,
rowstate=1
}
M.settings = {
id=0,
name="NONE",
value="EMPTY",
rowstate=1
}
-- method: init
-- expects: nothing
-- returns: nothing
function M.init()
if myDevice ~= nil then
M.searchResults.per_page = myDevice.basePerPage
end
end
function M.setPerPage(value)
if value ~= nil then
M.searchResults.per_page = tonumber(value)
myDevice.basePerPage = M.searchResults.per_page
end
end
-- method: firstSceneVisit
-- expects: nothing
-- returns: true/false if user has viewed the scene already
function M.firstSceneVisit(scene_name)
local total = 0
local return_val
local sql = ""
if scene_name == nil then return false end
myDatabase.open()
sql = "select count(*) from settings where name='"..scene_name.."' and rowstate < 999"
for x in myDatabase.myDB:urows(sql) do
total = x
end
if total == nil or total < 1 then
name = scene_name
value = 1
myDatabase.insert("settings",{name=name,value=1,rowstate=1})
return_val = true
else
return_val = false
end
myDatabase.close()
return return_val
end
-- method: resetFilter
-- expects: nothing
-- returns: nothing
function M.resetFilter()
M.filter = ""
M.orderby = ""
M.groupby = ""
end
function M.saveGame(name, game_type)
if name == nil then return end
myDatabase.open()
local total = 0
sql = "SELECT count(*) FROM game WHERE id=" .. GameSettings.gameid
for x in myDatabase.myDB:urows(sql) do
total = x
end
if total < 1 then
local res = myDatabase.insert("game",{name=name,gametype=game_type,mdate="now()",rowstate=1})
if res.newid > 0 then
M.game.id = res.newid
GameSettings.gameid = res.newid
GameSettings.name = name
GameSettings.gametype = game_type
GameSettings.count = 0
end
else
res = myDatabase.update("game",{mdate="now()",name=name,rowstate=1},"id=" .. GameSettings.gameid)
GameSettings.name = name
end
myDatabase.close()
return GameSettings.gameid
end
-- method: startGame
-- expects: nothing
-- returns: nothing
function M.startGame()
myDatabase.open()
if M.game.id == 0 then
M.game.type = GameSettings.gametype
M.game.name = GameSettings.name
--print("GAME",GameSettings.name,"TYPE",GameSettings.gametype,M.game.type)
-- ask database for new id
local res = myDatabase.insert("game",{name=M.game.name,gametype=M.game.type,mdate="now()",rowstate=1})
if res.newid > 0 then
M.game.id = res.newid
end
end
myDatabase.close()
return M.game.id
end
-- method: endGame
-- expects: nothing
-- returns: nothing
function M.endGame(attr)
myDatabase.open()
local endAllGames = false
if attr ~= nil then
if attr.endAll == true then endAllGames = true end
end
local res
if GameSettings.gameid > 0 then
-- end the game
if endAllGames == false then
res = myDatabase.update("game",{edate="now()",rowstate=2},"id=" .. GameSettings.gameid)
else
res = myDatabase.update("game",{edate="now()",rowstate=2},"rowstate=1")
end
--print("END THEGAME=", res)
end
myDatabase.close()
end
-- method: lastGame
-- expects: nothing
-- returns: nothing
function M.lastGame()
local res = 0
myDatabase.open()
local gamesql=""
if StartGameHistory == true then
gamesql = " AND id="..GameHistoryId.." "
StartGameHistory = false
end
local sql = "SELECT id,name,gametype FROM game WHERE id > 0 AND rowstate=1 "..gamesql.." ORDER BY id DESC LIMIT 1"
for id,name,gametype in myDatabase.myDB:urows(sql) do
GameSettings.gameid = id
GameSettings.name = name
GameSettings.gametype = gametype
--print("gametype="..gametype.." for name: "..name.." id="..id)
res = id
end
-- if game exist
if res > 0 then
-- remove prior friends
for i=0, #GameSettings.player do
table.remove(GameSettings.player)
end
-- remove prior team
for i=0, #GameSettings.team do
table.remove(GameSettings.team)
end
if GameSettings.gametype == 1 or GameSettings.gametype == 3 then -- individual
sql = "SELECT player FROM gamelog WHERE gameid=" .. GameSettings.gameid .. " and rowstate=1"
for player in myDatabase.myDB:urows(sql) do
table.insert(GameSettings.player, player)
end
elseif GameSettings.gametype == 2 then -- team
sql = "SELECT DISTINCT team FROM gamelog WHERE gameid=" .. GameSettings.gameid .. " and rowstate=1"
for team in myDatabase.myDB:urows(sql) do
table.insert(GameSettings.team, team)
end
end
end
-- WAIT CHECK AbOve iF statements for gametype == 3 too!
myDatabase.close()
return res
end
-- method: resumeGame
-- expects: nothing
-- returns: nothing
function M.resumeGame()
myDatabase.open()
if M.game.id > 0 then
-- resume the game
end
myDatabase.close()
end
-- method: playersExist
-- expects: nothing
-- returns: true/false
function M.playersExist()
local total = 0
local return_val
myDatabase.open()
sql = "SELECT count(*) FROM players WHERE rowstate=1"
for x in myDatabase.myDB:urows(sql) do
total = x
end
myDatabase.close()
if total == nil or total < 1 then
return_val = false
else
return_val = true
end
M.dialogResult("Need Players")
return return_val
end
-- method: teamsExist
-- expects: nothing
-- returns: true/false
function M.teamsExist()
local total = 0
local return_val
myDatabase.open()
sql = "SELECT count(*) FROM team WHERE rowstate=1"
for x in myDatabase.myDB:urows(sql) do
total = x
end
myDatabase.close()
if total == nil or total < 1 then
return_val = false
else
return_val = true
end
M.dialogResult("Need Teams")
return return_val
end
-- method: teamsAndPlayersExist
-- expects: nothing
-- returns: true/false
function M.teamsAndPlayersExist()
local total = 0
local return_val
local sql = ""
if M.teamsExist() == false then return false end
myDatabase.open()
sql = "select count(*) from teamplayers where rowstate < 999"
for x in myDatabase.myDB:urows(sql) do
total = x
end
myDatabase.close()
if total == nil or total < 1 then
return_val = false
else
return_val = true
end
M.dialogResult("Need Players")
return return_val
end
-- method: teamsAndPlayersExistForTeamId()
-- expects: nothing
-- returns: true/false
function M.teamsAndPlayersExistForTeamId(team_id)
local total = 0
local return_val
local sql = ""
if team_id == nil then return false end
if M.teamsExist() == false then return false end
myDatabase.open()
sql = "select count(*) from teamplayers where rowstate < 999 and team="..team_id
for x in myDatabase.myDB:urows(sql) do
total = x
end
myDatabase.close()
if total == nil or total < 1 then
return_val = false
else
return_val = true
end
M.dialogResult("Need Players")
return return_val
end
-- method: resetSearch
-- expects: nothing
-- returns: nothing
-- notes: it resets all search parameters back to default
function M.resetSearch()
--local scene = director:getCurrentScene()
M.searchResults.from=0
M.searchResults.to=0
M.searchResults.pages_count = 0
M.searchResults.page = 0
M.searchResults.method = nil
M.searchResults.query = ""
M.searchResults.filter = false
M.callbackDtype = nil
--scene:reload()
end
-- method: loadPlayers
-- expects: nothing
-- returns: nothing
-- notes: it populars the player board with the last names played for a game id
function M.loadPlayers(attr)
if attr.query ~= nil then
return M.loadPlayersFilter(attr)
end
myDatabase.open()
M.callback = assert(M.loadPlayers)
M.callbackParms = attr
local from = 0
if attr.cmd ~= nil then
if attr.cmd == 'next' then
if M.searchResults.page < M.searchResults.page_count-1 then
M.searchResults.from = (M.searchResults.page + 1) * M.searchResults.per_page
end
elseif attr.cmd == 'prev' then
if M.searchResults.page > 0 then
M.searchResults.from = (M.searchResults.page - 1) * M.searchResults.per_page
end
elseif attr.cmd == 'reload' then
M.searchResults.from = (M.searchResults.page) * M.searchResults.per_page
end
end
if M.searchResults.from > 0 then from = M.searchResults.from end
if searchPrevFrom.Players > 0 then
from = searchPrevFrom.Players
searchPrevFrom.Players = 0
end
local limit = from .. ", " .. M.searchResults.per_page
local total = 0
for x in myDatabase.myDB:urows("SELECT COUNT(*) FROM players WHERE rowstate < 999") do
total = x
end
M.searchResults.total = total
-- load the players with test data
if total < 1 and false then
for n=1,20 do
local res = myDatabase.insert("players",{name="Test Player "..(100+n).."",mdate="now()",rowstate=1})
end
total = 20
end
-- load the players
--
local sql = "SELECT"
sql = sql .. " id,name,mdate "
sql = sql .. " FROM players "
sql = sql .. " WHERE name != '' and rowstate < 999 ORDER BY name COLLATE NOCASE ASC LIMIT "..limit
local result = {}
local n = 1
--print("QUERY="..sql.." total="..total)
for id, name, mdate in myDatabase.myDB:urows(sql) do
result[n] = {}
result[n].name = name
result[n].mdate = mdate
result[n].id = id
n = n + 1
--print("LOADED PLAYER: "..name.." id="..id)
end
from = math.min(from, total)
M.searchResults.from = from
M.searchResults.page_count = math.floor((total + M.searchResults.per_page - 1) / M.searchResults.per_page)
M.searchResults.page = math.floor(from / M.searchResults.per_page)
myDatabase.close()
return result
end
-- method: savePlayerById
-- expects: data and id in an array
-- returns: nothing
function M.savePlayerById(attr)
local start=0
local id = 0
if attr == nil then
return id
elseif attr.data == nil or attr.id == nil then
return id
end
myDatabase.open()
if string.len(attr.data) > MaxNameLength then
attr.data = string.sub(attr.data,1,MaxNameLength)
end
local sql = "SELECT count(*) FROM players WHERE name='" .. attr.data .. "' and rowstate=1 ORDER BY name ASC"
local total = 0
for c in myDatabase.myDB:urows(sql) do
total = c
end
if total > 0 then return 0 end
if attr.id == 0 then
-- insert new
local res = myDatabase.insert("players",
{
name=attr.data,
mdate="now()",
rowstate=1
})
if res.newid > 0 then
id = res.newid
end
M.mode = "insert"
elseif attr.id > 0 then
-- update current
local res = myDatabase.update("players",
{
name=attr.data,
mdate="now()",
rowstate=1
},
"id=" .. attr.id)
id = tonumber(attr.id)
M.mode = "update"
end
myDatabase.close()
return id
end
-- method: saveTeamById
-- expects: data and id in an array
-- returns: nothing
function M.saveTeamById(attr)
local start=0
local id = 0
if attr == nil then
return id
elseif attr.data == nil or attr.id == nil then
return id
end
myDatabase.open()
if string.len(attr.data) > MaxNameLength then
attr.data = string.sub(attr.data,1,MaxNameLength)
end
local sql = "SELECT count(*) FROM team WHERE name='" .. attr.data .. "' and rowstate=1 ORDER BY name ASC"
local total = 0
for c in myDatabase.myDB:urows(sql) do
total = c
end
if total > 0 then return 0 end
if attr.id == 0 then
-- insert new
local res = myDatabase.insert("team",
{
name=attr.data,
mdate="now()",
rowstate=1
})
if res.newid > 0 then
id = res.newid
end
elseif attr.id > 0 then
-- update current
local res = myDatabase.update("team",
{
name=attr.data,
mdate="now()",
rowstate=1
},
"id=" .. attr.id)
id = tonumber(attr.id)
end
myDatabase.close()
return id
end
-- method: loadPlayersForGame
-- returns: array of players
-- notes: it populates the player list
function M.loadPlayersForGame(attr)
local result = {}
if GameSettings.gameid < 1 then return end
local scene = director:getCurrentScene()
myDatabase.open()
M.callback = assert(M.loadPlayersForGame)
M.callbackParms = attr
local from = 0
if attr.cmd ~= nil and attr.loadall == nil then
if attr.cmd == 'next' then
if M.searchResults.page < M.searchResults.page_count-1 then
M.searchResults.from = (M.searchResults.page + 1) * M.searchResults.per_page
end
elseif attr.cmd == 'prev' then
if M.searchResults.page > 0 then
M.searchResults.from = (M.searchResults.page - 1) * M.searchResults.per_page
end
elseif attr.cmd == 'reload' then
if M.searchResults.page > 0 then
M.searchResults.from = (M.searchResults.page) * M.searchResults.per_page
end
-- M.searchResults.from = (M.searchResults.page) * M.searchResults.per_page
end
end
--print("M.searchResults.from: " .. M.searchResults.from)
if M.searchResults.from > 0 then from = M.searchResults.from end
if searchPrevFrom.HistoryDetails > 0 and scene.name == "HistoryDetails" then
from = searchPrevFrom.HistoryDetails
searchPrevFrom.HistoryDetails = 0
end
if searchPrevFrom.GamePlayers > 0 and scene.name == "Game" then
from = searchPrevFrom.GamePlayers
searchPrevFrom.GamePlayers = 0
end
local limit = from .. ", " .. M.searchResults.per_page
-- check for load all
if attr.loadall ~= nil then limit = "" end
local total = 0
-- for History section, but maybe other areas later.
local game_id = GameSettings.gameid
if EventGameId > 0 then
game_id = EventGameId
EventGameId = 0
end
if GameSettings.gametype < 3 then
for x in myDatabase.myDB:urows("SELECT COUNT(*) FROM players WHERE id in (select player from gamelog where gameid="..game_id..") and rowstate < 999") do
total = x
end
else
for x in myDatabase.myDB:urows("SELECT COUNT(*) FROM team WHERE id in (select player from gamelog where gameid="..game_id..") and rowstate < 999") do
total = x
end
end
GameSettings.count = total
-- print("total is " .. total)
-- load the players
--
-- select players.id, players.name, players.mdate, gamelog.scoreint from players left join gamelog on players.id=gamelog.player where gamelog.gameid=50;
local sql = "SELECT"
print("GameSettings.gametype: "..GameSettings.gametype)
if GameSettings.gametype < 3 then
sql = sql .. " players.id, players.name, gamelog.scoreint, gamelog.team, players.mdate, (select name from team where id=gamelog.team) as teamname "
sql = sql .. " FROM players "
sql = sql .. " LEFT JOIN gamelog ON players.id=gamelog.player "
sql = sql .. " WHERE players.name != '' and gamelog.gameid="..game_id.." and players.rowstate < 999 ORDER BY teamname,gamelog.scoreint desc,players.name COLLATE NOCASE ASC "
else
sql = sql .. " team.id, team.name, gamelog.scoreint, team.id, 0, team.mdate, team.name"
sql = sql .. " FROM team "
sql = sql .. " LEFT JOIN gamelog ON team.id=gamelog.player "
sql = sql .. " WHERE team.name != '' and gamelog.gameid="..game_id.." and team.rowstate < 999 ORDER BY gamelog.scoreint desc,team.name COLLATE NOCASE ASC "
end
if string.len(limit) > 0 then sql = sql .." LIMIT "..limit end
local result = {}
local n = 1
local lastTeam = 0
local teamidx = 0
local tally = 0
--print("QUERY="..sql.." total="..total)
for id, name, scoreint, team, mdate, teamname in myDatabase.myDB:urows(sql) do
result[n] = {}
if M.checkForDuplicate(id, result) == false then
if GameSettings.gametype < 3 then
if team > 0 and lastTeam ~= team then
teamidx = n
result[n].id = -1
result[n].mdate = ""
result[n].name = teamname
result[n].team = team
result[n].teamname = teamname
result[n].scoreint = 0
lastTeam = team
-- since team takes a slot increment and create a new slot
total = total + 1
n = n + 1
result[n] = {}
end
end
result[n].name = name
result[n].mdate = mdate
result[n].scoreint = scoreint
result[n].id = id
result[n].team = team
result[n].teamname = teamname
n = n + 1
if teamidx > 0 then
result[teamidx].scoreint = result[teamidx].scoreint + scoreint
end
else
-- print("DuplicatePlayerFound")
end
-- print("LOADED PLAYER: "..name.." id="..id)
end
from = math.min(from, total)
M.searchResults.total = total
if cmd ~= "reload" then
M.searchResults.from = from
end
M.searchResults.page_count = math.floor((total + M.searchResults.per_page - 1) / M.searchResults.per_page)
M.searchResults.page = math.floor(from / M.searchResults.per_page)
myDatabase.close()
return result
end
-- method: addPlayersToGame
-- returns: nothing
-- notes: it populates the gamelog table (game creation only! no score updates)
function M.addPlayersToGame(player_id)
local player_name = "unknown"
if GameSettings.name == "" or player_id < 1 then return end
myDatabase.open()
if GameSettings.gameid < 1 then
-- create new game ()
GameSettings.gametype = 1
GameSettings.gameid = M.startGame()
end
if GameSettings.gameid > 0 then
if GameSettings.gametype < 3 then
-- look up the player and get the name
for x in myDatabase.myDB:urows("SELECT name FROM players WHERE id="..player_id.." and rowstate=1") do
player_name = x
end
else
-- look up the team and get the name
for x in myDatabase.myDB:urows("SELECT name FROM team WHERE id="..player_id.." and rowstate=1") do
player_name = x
end
end
if player_name == "" or player_name == "unknown" then return end
-- look up player in game on the team (team 0 in this case)
local total = 0
local sql = "gameid="..GameSettings.gameid
sql = sql .. " and team=0"
sql = sql .. " and player="..player_id
for x in myDatabase.myDB:urows("SELECT COUNT(*) FROM gamelog WHERE "..sql.." and rowstate=1") do
total = x
end
if total < 1 then
myDatabase.insert("gamelog",{
name=player_name,
player=player_id,
team=0,
scoreint=0,
gameid=GameSettings.gameid,
gametype=GameSettings.gametype,
mdate="now()",
rowstate=1
})
end
end
end
-- method: removePlayersFromGame
-- returns: nothing
-- notes: it removes players from the gamelog table
function M.removePlayersFromGame(player_id)
if GameSettings.gameid < 1 or player_id < 1 then return end
myDatabase.open()
myDatabase.delete("gamelog","player="..player_id.." and gameid="..GameSettings.gameid)
end
-- method: loadPlayersNotInGame
-- returns: nothing
-- notes: it loads the players based on the gamelog table
function M.loadPlayersNotInGame(attr)
local result = {}
if GameSettings.gameid < 1 then return end
myDatabase.open()
M.callback = assert(M.loadPlayersNotInGame)
M.callbackParms = attr
local from = 0
if attr.cmd ~= nil then
if attr.cmd == 'next' then
if M.searchResults.page < M.searchResults.page_count-1 then
M.searchResults.from = (M.searchResults.page + 1) * M.searchResults.per_page
end
elseif attr.cmd == 'prev' then
if M.searchResults.page > 0 then
M.searchResults.from = (M.searchResults.page - 1) * M.searchResults.per_page
end
elseif attr.cmd == 'reload' then
M.searchResults.from = (M.searchResults.page) * M.searchResults.per_page
end
end
query = ""
if attr.query ~= nil then
query = " and " .. attr.query
end
if M.searchResults.from > 0 then from = M.searchResults.from end
if searchPrevFrom.GetPlayers > 0 then
from = searchPrevFrom.Players
searchPrevFrom.GetPlayers = 0
end
local limit = from .. ", " .. M.searchResults.per_page
local total = 0
for x in myDatabase.myDB:urows("SELECT COUNT(*) FROM players WHERE id not in (select player from gamelog where gameid="..GameSettings.gameid..") and rowstate < 999 "..query) do
total = x
end
-- load the players
--
local sql = "SELECT"
sql = sql .. " id,name,mdate "
sql = sql .. " FROM players "
sql = sql .. " WHERE name != '' and id not in (select player from gamelog where gameid="..GameSettings.gameid..") and rowstate < 999 "..query.." ORDER BY name COLLATE NOCASE ASC LIMIT "..limit
local result = {}
local n = 1
--print("QUERY="..sql.." total="..total)
for id, name, mdate in myDatabase.myDB:urows(sql) do
result[n] = {}
result[n].name = name
result[n].mdate = mdate
result[n].id = id
n = n + 1
--print("LOADED PLAYER: "..name.." id="..id)
end
from = math.min(from, total)
M.searchResults.from = from
M.searchResults.page_count = math.floor((total + M.searchResults.per_page - 1) / M.searchResults.per_page)
M.searchResults.page = math.floor(from / M.searchResults.per_page)
M.searchResults.total = total
myDatabase.close()
return result
end
--[[ update scores ]]--
function M.updateScore(attr)
if GameSettings.gameid < 1 then return end
myDatabase.open()
-- put check if gamelog.gametype == 0 then normal if 1 then team name is player
if attr.player ~= nil and attr.scoreint ~= nil then
local team = 0
if attr.team == nil then
team = myGamePlayersDetailsInterface:getTeamId(attr.player)
else
team = tonumber(attr.team)
end
myDatabase.update("gamelog",{scoreint=tonumber(attr.scoreint)},"player="..attr.player.." AND gameid="..GameSettings.gameid.." AND team="..team)
-- update result set scores too...
for j,v in pairs(myGamePlayersDetailsInterface.result) do
if v ~= nil then
if tonumber(v.id) == tonumber(attr.player) then
v.scoreint = tonumber(attr.scoreint)
break
end
end
end
-- update team
if GameSettings.gametype < 3 then
if team > 0 then
local idx = 0
local tally = 0
for j,v in pairs(myGamePlayersDetailsInterface.result) do
if tonumber(v.team) == team then
if v.id == -1 then
idx = j
else
tally = tally + tonumber(v.scoreint)
end
end
end
if idx > 0 then myGamePlayersDetailsInterface.result[idx].scoreint = tally end
end
end
end
end
--[[ TEAM BASED ]]--
-- method: loadTeamsForGame
-- returns: array of players
-- notes: it populates the player list
function M.loadTeamsForGame(attr)
local result = {}
if GameSettings.gameid < 1 then return end
myDatabase.open()
M.callback = assert(M.loadTeamsForGame)