-
Notifications
You must be signed in to change notification settings - Fork 32
/
server.lua
executable file
·972 lines (802 loc) · 36.4 KB
/
server.lua
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
ranks = {'02', '03', '04', '05', '06', '07', '08', '09', '10', --[['11',]] 'JACK', 'QUEEN', 'KING', 'ACE'}
suits = {'SPD', 'HRT', 'DIA', 'CLUB'}
function shuffle(tbl)
for i = #tbl, 2, -1 do
local j = math.random(i)
tbl[i], tbl[j] = tbl[j], tbl[i]
end
--[[
for i = 1, #tbl, 1 do
DebugPrint(tbl[i])
end
]]--
return tbl
end
function getDeck()
local tDeck = {}
for _,rank in pairs(ranks) do
for _,suit in pairs(suits) do
table.insert(tDeck, suit .. "_" .. rank)
end
end
return shuffle(tDeck)
end
function takeCard(tDeck)
--local card_pick = math.random(1,#tDeck)
--DebugPrint(card_pick)
return table.remove(tDeck, 1)
end
function cardValue(card)
local rank = 10
for i=2,11 do
if string.find(card, tostring(i)) then
rank = i
end
end
if string.find(card, 'ACE') then
rank = 11
end
return rank
end
function handValue(hand)
local tmpValue = 0
local numAces = 0
for i,v in pairs(hand) do
tmpValue = tmpValue + cardValue(v)
end
for i,v in pairs(hand) do
if string.find(v, 'ACE') then numAces = numAces + 1 end
end
repeat
if tmpValue > 21 and numAces > 0 then
tmpValue = tmpValue - 10
numAces = numAces - 1
else
break
end
until numAces == 0
return tmpValue
end
players = {
-- [1] = { -- table
-- [1] = { -- player
-- player = source
-- seat = 1
-- hand = {},
-- splitHand = {}
-- player_in = true,
-- bet = 1500,
-- }
-- },
-- [2] = {},
-- [3] = {},
-- [4] = {},
}
timeTracker = {}
tableTracker = {
-- ["2"] = 1,
}
--[===[
exports["kgv-blackjack"]:SetGetChipsCallback(function(source)
return 0 -- [[ return money ]]
end)
exports["kgv-blackjack"]:SetTakeChipsCallback(function(source, amount)
--[[ money = money - amount? ]]
end)
exports["kgv-blackjack"]:SetGiveChipsCallback(function(source, amount)
--[[ money = money + amount? ]]
end)
--]===]
getChipsCallback = nil
takeChipsCallback = nil
giveChipsCallback = nil
function FindPlayerIdx(tbl, src)
for i = 1, #tbl do
if tbl[i].player == src then
return i
end
end
return nil
end
function SetGetChipsCallback(cb)
getChipsCallback = cb
end
function SetTakeChipsCallback(cb)
takeChipsCallback = cb
end
function SetGiveChipsCallback(cb)
giveChipsCallback = cb
end
function GiveMoney(player, money)
if giveChipsCallback ~= nil then
giveChipsCallback(player, math.tointeger(money))
end
-- DebugPrint("MONEY: GIVE "..GetPlayerName(player):upper().." "..money)
end
function TakeMoney(player, money)
if takeChipsCallback ~= nil then
takeChipsCallback(player, math.tointeger(money))
end
-- DebugPrint("MONEY: TAKE "..GetPlayerName(player):upper().." "..money)
end
function HaveAllPlayersBetted(table)
for i,v in pairs(table) do
if v.bet < 1 then
return false
end
end
return true
end
function ArePlayersStillIn(table)
for i,v in pairs(table) do
if v.player_in == true then
return true
end
end
return false
end
function PlayDealerAnim(dealer, animDict, anim)
TriggerClientEvent("BLACKJACK:PlayDealerAnim", -1, dealer, animDict, anim)
end
function PlayDealerSpeech(dealer, speech)
TriggerClientEvent("BLACKJACK:PlayDealerSpeech", -1, dealer, speech)
end
function SetPlayerBet(i, seat, bet, betId, double, split)
split = split or false
double = double or false
local num = FindPlayerIdx(players[i], source)
if num ~= nil then
if double == false and split == false then
TakeMoney(source, bet)
players[i][num].bet = tonumber(bet)
end
TriggerClientEvent("BLACKJACK:PlaceBetChip", -1, i, 5-seat, bet, double, split)
else
DebugPrint("TABLE "..i..": PLAYER "..source.." ATTEMPTED BET BUT NO LONGER TRACKED?")
end
end
RegisterServerEvent("BLACKJACK:SetPlayerBet")
AddEventHandler('BLACKJACK:SetPlayerBet', SetPlayerBet)
function CheckPlayerBet(i, bet)
DebugPrint("TABLE "..i..": CHECKING "..GetPlayerName(source):upper().."'s CHIPS")
local playerChips = 0 -- Get money
if getChipsCallback ~= nil then
playerChips = getChipsCallback(source)
end
local canBet = false
if playerChips ~= nil then
if playerChips >= bet then
canBet = true
end
end
TriggerClientEvent("BLACKJACK:BetReceived", source, canBet)
end
RegisterServerEvent("BLACKJACK:CheckPlayerBet")
AddEventHandler("BLACKJACK:CheckPlayerBet", CheckPlayerBet)
function SortPlayers(pTable)
local temp
for i=1,#pTable-1 do
for j=i+1,#pTable do
if pTable[i].seat < pTable[j].seat then
temp = pTable[i]
pTable[i] = pTable[j]
pTable[j] = temp
end
end
end
return pTable
end
RegisterServerEvent("BLACKJACK:ReceivedMove")
function StartTableThread(i)
Citizen.CreateThread(function()
local index = i
-- DebugPrint(index)
while true do Wait(0)
if players[index] and #players[index] ~= 0 then
DebugPrint("WAITING FOR ALL PLAYERS AT TABLE "..index.." TO PLACE THEIR BETS.")
-- TODO: DONT FORGET TO REMOVE THIS JESUS CHRIST
-- local bet = 15000
-- TakeMoney(players[index][1].player, bet)
-- players[index][1].bet = bet
-- for num,_ in pairs(players[index]) do
-- TriggerClientEvent("BLACKJACK:RequestBets", players[index][num].player)
-- end
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_place_bet_request")
PlayDealerSpeech(index, "MINIGAME_DEALER_PLACE_CHIPS")
repeat
for i,v in pairs(players[index]) do
TriggerClientEvent("BLACKJACK:SyncTimer", v.player, bettingTime - timeTracker[index])
end -- Remove players from round who didn't bet in time
Wait(1000)
timeTracker[index] = timeTracker[index] + 1
until HaveAllPlayersBetted(players[index]) or #players[index] == 0 or timeTracker[index] >= bettingTime
if #players[index] == 0 then
DebugPrint("BETTING ENDED AT TABLE "..index..", NO MORE PLAYERS")
-- break
else
for i,v in pairs(players[index]) do
if v.bet < 1 then
v.player_in = false
end
end -- Remove players from round who didn't bet in time
if ArePlayersStillIn(players[index]) then -- did everyone just not bet?
DebugPrint("BETS PLACED AT TABLE "..index..", STARTING GAME")
PlayDealerSpeech(index, "MINIGAME_DEALER_CLOSED_BETS")
local currentPlayers = {table.unpack(players[i])}
local deck = getDeck()
local dealerHand = {}
local dealerVisibleHand = {}
TriggerClientEvent("BLACKJACK:UpdateDealerHand", -1, index, handValue(dealerVisibleHand))
currentPlayers = SortPlayers(currentPlayers)
local gameRunning = true
Wait(1500)
for x=1,2 do
local card = takeCard(deck)
table.insert(dealerHand, card)
TriggerClientEvent("BLACKJACK:GiveCard", -1, index, 0, #dealerHand, card, #dealerHand == 1)
if #dealerHand == 1 then
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_deal_card_self")
DebugPrint("TABLE "..index..": DEALT DEALER [HIDDEN] ") -- ..card) -- Add this to see the Dealer's hidden card
else
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_deal_card_self_second_card")
DebugPrint("TABLE "..index..": DEALT DEALER "..card)
table.insert(dealerVisibleHand, card)
end
Wait(2000)
TriggerClientEvent("BLACKJACK:UpdateDealerHand", -1, index, handValue(dealerVisibleHand))
if #dealerHand > 1 then
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_"..cardValue(dealerHand[2]))
end
for i,v in pairs(currentPlayers) do
if v.player_in then
local card = takeCard(deck)
TriggerClientEvent("BLACKJACK:GiveCard", -1, index, v.seat, #v.hand+1, card)
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_deal_card_player_0" .. 5-v.seat)
table.insert(v.hand, card)
Wait(2000)
DebugPrint("TABLE "..index..": DEALT "..GetPlayerName(v.player):upper().." "..card)
if handValue(v.hand) == 21 then
TriggerClientEvent("BLACKJACK:GameEndReaction", v.player, "good")
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." HAS BLACKJACK")
GiveMoney(v.player, v.bet*2.5)
v.player_in = false
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_BLACKJACK")
else
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_"..handValue(v.hand))
end
end
end
end
-- female_dealer_focus_player_01_idle
if handValue(dealerHand) == 21 then
DebugPrint("TABLE "..index..": DEALER HAS BLACKJACK")
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_check_and_turn_card")
dealerVisibleHand = dealerHand
Wait(2000)
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_BLACKJACK")
TriggerClientEvent("BLACKJACK:DealerTurnOverCard", -1, index)
TriggerClientEvent("BLACKJACK:UpdateDealerHand", -1, index, handValue(dealerVisibleHand))
for i,v in pairs(currentPlayers) do
TriggerClientEvent("BLACKJACK:GameEndReaction", v.player, "bad")
end
gameRunning = false
elseif cardValue(dealerHand[2]) == 10 or cardValue(dealerHand[2]) == 11 then
DebugPrint("TABLE "..index..": DEALER HAS A 10, CHECKING..")
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_check_card")
Wait(2000)
end
if gameRunning == true then
for i,v in pairs(currentPlayers) do
if v.player_in then
if tableTracker[tostring(v.player)] == nil then
DebugPrint("TABLE "..index..": "..v.player.." WAS PUT OUT DUE TO LEAVING")
v.player_in = false
TriggerClientEvent("BLACKJACK:RetrieveCards", -1, index, v.seat)
else
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_intro")
Wait(1500)
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_ANOTHER_CARD")
while v.player_in == true and #v.hand < 5 do
timeTracker[index] = 0
Wait(0)
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle")
DebugPrint("TABLE "..index..": AWAITING MOVE FROM "..GetPlayerName(v.player):upper())
TriggerClientEvent("BLACKJACK:RequestMove", v.player, moveTime - timeTracker[index])
local receivedMove = false
local move = "stand"
local eventHandler = AddEventHandler("BLACKJACK:ReceivedMove", function(m)
if source ~= v.player then return end
move = m
receivedMove = true
end)
while receivedMove == false and tableTracker[tostring(v.player)] ~= nil and timeTracker[index] < moveTime do
for i,v in pairs(currentPlayers) do
TriggerClientEvent("BLACKJACK:SyncTimer", v.player, moveTime - timeTracker[index])
end
Wait(1000)
timeTracker[index] = timeTracker[index] + 1
end
--repeat Wait(0) until receivedMove == true
RemoveEventHandler(eventHandler)
if tableTracker[tostring(v.player)] == nil then
DebugPrint("TABLE "..index..": "..v.player.." WAS PUT OUT DUE TO LEAVING")
v.player_in = false
TriggerClientEvent("BLACKJACK:RetrieveCards", -1, index, v.seat)
else
if move == "hit" then
local card = takeCard(deck)
TriggerClientEvent("BLACKJACK:GiveCard", -1, index, v.seat, #v.hand+1, card)
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_hit_card_player_0" .. 5-v.seat)
table.insert(v.hand, card)
Wait(1500)
DebugPrint("TABLE "..index..": DEALT "..GetPlayerName(v.player):upper().." "..card)
if handValue(v.hand) == 21 then
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." HAS 21")
-- v.player_in = false
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_"..handValue(v.hand))
break
elseif handValue(v.hand) > 21 then
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
TriggerClientEvent("BLACKJACK:GameEndReaction", v.player, "bad")
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." WENT BUST")
v.player_in = false
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_PLAYER_BUST")
else
-- Wait(1000)
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_"..handValue(v.hand))
end
elseif move == "double" then
TakeMoney(v.player, v.bet)
v.bet = v.bet*2
-- TriggerClientEvent("BLACKJACK:PlaceBetChip", -1, i, 5-v.seat, betId)
local card = takeCard(deck)
TriggerClientEvent("BLACKJACK:GiveCard", -1, index, v.seat, #v.hand+1, card)
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_hit_card_player_0" .. 5-v.seat)
table.insert(v.hand, card)
Wait(1500)
DebugPrint("TABLE "..index..": DEALT "..GetPlayerName(v.player):upper().." "..card)
if handValue(v.hand) == 21 then
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." HAS 21")
-- v.player_in = false
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_"..handValue(v.hand))
break
elseif handValue(v.hand) > 21 then
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
TriggerClientEvent("BLACKJACK:GameEndReaction", v.player, "bad")
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." WENT BUST")
v.player_in = false
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_PLAYER_BUST")
else
-- Wait(2000)
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_"..handValue(v.hand))
end
break
elseif move == "split" then
TakeMoney(v.player, v.bet)
v.bet = v.bet*2
-- TriggerClientEvent("BLACKJACK:PlaceBetChip", -1, i, 5-v.seat, betId)
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_split_card_player_0" .. 5-v.seat)
v.splitHand = {}
local splitCard = table.remove(v.hand, 2)
table.insert(v.splitHand, splitCard)
Wait(500)
TriggerClientEvent("BLACKJACK:SplitHand", -1, index, v.seat, #v.splitHand, v.hand, v.splitHand)
Wait(1000)
local card = takeCard(deck)
TriggerClientEvent("BLACKJACK:GiveCard", -1, index, v.seat, #v.hand+1, card, false, false)
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_hit_card_player_0" .. 5-v.seat)
-- female_dealer_focus_player_01_idle_split
table.insert(v.hand, card)
Wait(1500)
DebugPrint("TABLE "..index..": DEALT "..GetPlayerName(v.player):upper().." "..card)
if handValue(v.hand) == 21 then
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." HAS 21")
-- v.player_in = false
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_BLACKJACK")
break
elseif handValue(v.hand) > 21 then
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
TriggerClientEvent("BLACKJACK:GameEndReaction", v.player, "bad")
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." WENT BUST")
-- v.player_in = false
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_PLAYER_BUST")
else
-- Wait(2000)
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_"..handValue(v.hand))
end
local card = takeCard(deck)
TriggerClientEvent("BLACKJACK:GiveCard", -1, index, v.seat, #v.splitHand+1, card, false, true)
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_hit_second_card_player_0" .. 5-v.seat)
table.insert(v.splitHand, card)
Wait(1500)
DebugPrint("TABLE "..index..": DEALT "..GetPlayerName(v.player):upper().." "..card)
if handValue(v.splitHand) == 21 then
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." HAS 21")
-- v.player_in = false
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_"..handValue(v.splitHand))
break
elseif handValue(v.splitHand) > 21 then
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
TriggerClientEvent("BLACKJACK:GameEndReaction", v.player, "bad")
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." WENT BUST")
-- v.player_in = false
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_PLAYER_BUST")
else
-- Wait(2000)
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_"..handValue(v.splitHand))
end
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_intro")
-- Wait(1500)
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_ANOTHER_CARD")
repeat Wait(0)
timeTracker[index] = 0
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle")
DebugPrint("TABLE "..index..": AWAITING MOVE FROM "..GetPlayerName(v.player):upper())
TriggerClientEvent("BLACKJACK:RequestMove", v.player, moveTime - timeTracker[index])
local receivedMove = false
local move = "stand"
local eventHandler = AddEventHandler("BLACKJACK:ReceivedMove", function(m)
if source ~= v.player then return end
move = m
receivedMove = true
end)
while receivedMove == false and tableTracker[tostring(v.player)] ~= nil and timeTracker[index] < moveTime do
for i,v in pairs(currentPlayers) do
TriggerClientEvent("BLACKJACK:SyncTimer", v.player, moveTime - timeTracker[index])
end
Wait(1000)
timeTracker[index] = timeTracker[index] + 1
end
--repeat Wait(0) until receivedMove == true
RemoveEventHandler(eventHandler)
if tableTracker[tostring(v.player)] == nil then
DebugPrint("TABLE "..index..": "..v.player.." WAS PUT OUT DUE TO LEAVING")
v.player_in = false
TriggerClientEvent("BLACKJACK:RetrieveCards", -1, index, v.seat)
print("breaking on 1st hand")
break
else
if move == "hit" then
local card = takeCard(deck)
TriggerClientEvent("BLACKJACK:GiveCard", -1, index, v.seat, #v.hand+1, card, false, false)
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_hit_card_player_0" .. 5-v.seat)
table.insert(v.hand, card)
Wait(1500)
DebugPrint("TABLE "..index..": DEALT "..GetPlayerName(v.player):upper().." "..card)
if handValue(v.hand) == 21 then
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." HAS 21")
-- v.player_in = false
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_"..handValue(v.hand))
break
elseif handValue(v.hand) > 21 then
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
TriggerClientEvent("BLACKJACK:GameEndReaction", v.player, "bad")
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." WENT BUST")
-- v.player_in = false
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_PLAYER_BUST")
else
-- Wait(1000)
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_"..handValue(v.hand))
end
elseif move == "stand" then
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro_split")
-- Wait(1500)
break
end
end
until handValue(v.hand) >= 21 or #v.hand == 5
if v.player_in == true then
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
Wait(1500)
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_intro_split")
Wait(1500)
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_ANOTHER_CARD")
repeat Wait(0)
timeTracker[index] = 0
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_split")
print(""..v.player)
DebugPrint("TABLE "..index..": AWAITING MOVE FROM "..GetPlayerName(v.player):upper())
TriggerClientEvent("BLACKJACK:RequestMove", v.player, moveTime - timeTracker[index])
local receivedMove = false
local move = "stand"
local eventHandler = AddEventHandler("BLACKJACK:ReceivedMove", function(m)
if source ~= v.player then return end
move = m
receivedMove = true
end)
while receivedMove == false and tableTracker[tostring(v.player)] ~= nil and timeTracker[index] < moveTime do
for i,v in pairs(currentPlayers) do
TriggerClientEvent("BLACKJACK:SyncTimer", v.player, moveTime - timeTracker[index])
end
Wait(1000)
timeTracker[index] = timeTracker[index] + 1
end
--repeat Wait(0) until receivedMove == true
RemoveEventHandler(eventHandler)
if tableTracker[tostring(v.player)] == nil then
DebugPrint("TABLE "..index..": "..v.player.." WAS PUT OUT DUE TO LEAVING")
v.player_in = false
TriggerClientEvent("BLACKJACK:RetrieveCards", -1, index, v.seat)
break
else
if move == "hit" then
local card = takeCard(deck)
TriggerClientEvent("BLACKJACK:GiveCard", -1, index, v.seat, #v.splitHand+1, card, false, true)
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_hit_second_card_player_0" .. 5-v.seat)
table.insert(v.splitHand, card)
Wait(1500)
DebugPrint("TABLE "..index..": DEALT "..GetPlayerName(v.player):upper().." "..card)
if handValue(v.splitHand) == 21 then
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." HAS 21")
-- v.player_in = false
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_"..handValue(v.splitHand))
break
elseif handValue(v.splitHand) > 21 then
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
TriggerClientEvent("BLACKJACK:GameEndReaction", v.player, "bad")
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." WENT BUST")
-- v.player_in = false
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_PLAYER_BUST")
else
-- Wait(1000)
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_"..handValue(v.splitHand))
end
elseif move == "stand" then
break
end
end
until handValue(v.splitHand) >= 21 or #v.splitHand == 5
if handValue(v.hand) > 21 and handValue(v.splitHand) > 21 then
v.player_in = false
end
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro_split")
Wait(1500)
end
break
-- end
elseif move == "stand" then
-- PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
-- Wait(1500)
break
end
end
end
if not v.splitHand then
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_dealer_focus_player_0".. 5-v.seat .."_idle_outro")
Wait(1500)
end
end
end
end
-- Remove offline players from table
local j = 1
while j <= #currentPlayers do
local player = currentPlayers[j]
if tableTracker[tostring(player.player)] == nil then
DebugPrint("TABLE "..index..": "..player.player.." WAS REMOVED FROM PLAYERS LIST FOR LEAVING")
table.remove(currentPlayers, j)
else
j = j + 1
end
end
if ArePlayersStillIn(currentPlayers) then
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_turn_card")
Wait(1000)
TriggerClientEvent("BLACKJACK:DealerTurnOverCard", -1, index)
dealerVisibleHand = dealerHand
Wait(1000)
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_"..handValue(dealerHand))
TriggerClientEvent("BLACKJACK:UpdateDealerHand", -1, index, handValue(dealerVisibleHand))
end
if handValue(dealerHand) < 17 and ArePlayersStillIn(currentPlayers) then
repeat
local card = takeCard(deck)
table.insert(dealerHand, card)
TriggerClientEvent("BLACKJACK:GiveCard", -1, index, 0, #dealerHand, card, #dealerHand == 1)
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_deal_card_self_second_card")
DebugPrint("TABLE "..index..": DEALT DEALER "..card)
Wait(2000)
PlayDealerSpeech(index, "MINIGAME_BJACK_DEALER_"..handValue(dealerHand))
TriggerClientEvent("BLACKJACK:UpdateDealerHand", -1, index, handValue(dealerVisibleHand))
until handValue(dealerHand) >= 17
end
end
if handValue(dealerHand) > 21 then
PlayDealerSpeech(index, "MINIGAME_DEALER_BUSTS")
-- elseif handValue(dealerHand) < 21 and ArePlayersStillIn(currentPlayers) then
-- PlayDealerSpeech(index, "MINIGAME_DEALER_WINS")
end
DebugPrint("TABLE "..index..": DEALER HAS "..handValue(dealerHand))
for i,v in pairs(currentPlayers) do
-- if v.player_in then
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." HAS "..handValue(v.hand))
if v.player_in == true and (handValue(v.hand) > handValue(dealerHand) or handValue(dealerHand) > 21) then -- WIN
if v.splitHand then
if handValue(v.splitHand) > handValue(dealerHand) or handValue(dealerHand) > 21 then -- WIN
TriggerClientEvent("BLACKJACK:GameEndReaction", v.player, "good")
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." WON")
v.player_in = false
end
end
TriggerClientEvent("BLACKJACK:GameEndReaction", v.player, "good")
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." WON")
GiveMoney(v.player, v.bet*2)
v.player_in = false
end
if v.player_in == true and handValue(v.hand) == handValue(dealerHand) then -- PUSH
if v.splitHand then
if handValue(v.splitHand) == handValue(dealerHand) then -- PUSH
TriggerClientEvent("BLACKJACK:GameEndReaction", v.player, "impartial")
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." IS PUSH")
v.player_in = false
end
end
TriggerClientEvent("BLACKJACK:GameEndReaction", v.player, "impartial")
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." IS PUSH")
GiveMoney(v.player, v.bet)
v.player_in = false
end
if v.player_in == true and handValue(v.hand) < handValue(dealerHand) and handValue(dealerHand) <= 21 then -- LOSE
if v.splitHand then
if handValue(v.splitHand) < handValue(dealerHand) and handValue(dealerHand) <= 21 then -- LOSE
TriggerClientEvent("BLACKJACK:GameEndReaction", v.player, "bad")
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." LOST")
v.player_in = false
end
end
TriggerClientEvent("BLACKJACK:GameEndReaction", v.player, "bad")
DebugPrint("TABLE "..index..": "..GetPlayerName(v.player):upper().." LOST")
v.player_in = false
end
-- end
end
if handValue(dealerHand) >= 17 then
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@shared@dealer@", "female_dealer_reaction_impartial_var0"..math.random(1,3))
elseif handValue(dealerHand) > 21 then
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@shared@dealer@", "female_dealer_reaction_good_var0"..math.random(1,3))
else
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@shared@dealer@", "female_dealer_reaction_bad_var0"..math.random(1,3))
end
Wait(2500)
for i,v in pairs(currentPlayers) do
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_retrieve_cards_player_0".. 5-v.seat)
Wait(500)
TriggerClientEvent("BLACKJACK:RetrieveCards", -1, index, v.seat)
Wait(1500)
v.bet = 0
v.player_in = true
v.hand = {}
v.splitHand = nil
end
PlayDealerAnim(index, "anim_casino_b@amb@casino@games@blackjack@dealer", "female_retrieve_own_cards_and_remove")
Wait(500)
TriggerClientEvent("BLACKJACK:RetrieveCards", -1, index, 0)
Wait(1500)
timeTracker[index] = 0
for i,v in pairs(currentPlayers) do
TriggerClientEvent("BLACKJACK:RequestBets", v.player, index, timeTracker[index])
end
-- while true do Wait(0) end
else
for i,v in pairs(players[index]) do
v.bet = 0
v.player_in = true
v.hand = {}
v.splitHand = nil
end
timeTracker[index] = 0
end
end
end
end
end)
end
Citizen.CreateThread(function() -- INIT
for i,_ in pairs(tables) do
StartTableThread(i)
players[i] = {}
timeTracker[i] = 0
end
end)
function PlayerSatDown(i, seat)
DebugPrint(GetPlayerName(source):upper() .. " SAT DOWN AT TABLE " .. i)
-- player = source
-- index = i
-- chair = seat
table.insert(players[i], {player = source, seat = seat, hand = {}, player_in = true, bet = 0})
tableTracker[tostring(source)] = i
-- PlayDealerSpeech(i, "MINIGAME_DEALER_GREET")
TriggerClientEvent("BLACKJACK:RequestBets", source, i)
-- DebugPrint(#players[i])
-- Citizen.CreateThread(function()
-- local deck = getDeck()
-- local card1 = takeCard(deck)
-- TriggerClientEvent("BLACKJACK:GiveCard", player, index, card1)
-- TriggerClientEvent("BLACKJACK:ANIM:DealCard", -1, index, chair)
-- Wait(3000)
-- local card2 = takeCard(deck)
-- TriggerClientEvent("BLACKJACK:GiveCard", player, index, card2)
-- TriggerClientEvent("BLACKJACK:ANIM:DealCard", -1, index, chair)
-- end)
-- local card1 = takeCard(deck)
-- local card2 = takeCard(deck)
-- TriggerEvent('_chat:messageEntered', GetPlayerName(source), {0, 0, 0}, "has " .. handValue({card1, card2}) .. " ("..cardValue(card1)..", "..cardValue(card2)..")")
end
RegisterServerEvent("BLACKJACK:PlayerSatDown")
AddEventHandler('BLACKJACK:PlayerSatDown', PlayerSatDown)
function PlayerSatUp(i)
DebugPrint(GetPlayerName(source):upper() .. " LEFT TABLE "..i)
local num = FindPlayerIdx(players[i], source)
if num ~= nil then
DebugPrint(GetPlayerName(source):upper() .. " SUCCESSFULLY REMOVED FROM TABLE "..i)
table.remove(players[i], num)
tableTracker[tostring(source)] = nil
PlayDealerSpeech(i, "MINIGAME_DEALER_LEAVE_NEUTRAL_GAME")
end
end
RegisterServerEvent("BLACKJACK:PlayerSatUp")
AddEventHandler('BLACKJACK:PlayerSatUp', PlayerSatUp)
function PlayerLeft()
local playerTbl = tableTracker[tostring(source)]
if playerTbl ~= nil then
DebugPrint(GetPlayerName(source):upper() .. " LEFT SERVER")
local num = FindPlayerIdx(players[playerTbl], source)
if num ~= nil then
DebugPrint(GetPlayerName(source):upper() .. " REMOVED FROM TABLE FOR LEAVING")
table.remove(players[playerTbl], num)
end
tableTracker[tostring(source)] = nil
end
end
AddEventHandler("playerDropped", PlayerLeft)
function PlayerRemove(i)
DebugPrint(GetPlayerName(source):upper() .. " LEFT TABLE "..i)
local num = FindPlayerIdx(players[i], source)
if num ~= nil then
DebugPrint(GetPlayerName(source):upper() .. " SUCCESSFULLY REMOVED FROM TABLE "..i)
local playerInfo = players[i][num]
if playerInfo.player_in then
if playerInfo.bet > 0 then
GiveMoney(source, playerInfo.bet) -- give money back as player was removed before losing or winning?
end
end
table.remove(players[i], num)
tableTracker[tostring(source)] = nil
PlayDealerSpeech(i, "MINIGAME_DEALER_LEAVE_NEUTRAL_GAME")
end
end
RegisterServerEvent("BLACKJACK:PlayerRemove")
AddEventHandler('BLACKJACK:PlayerRemove', PlayerRemove)
exports("SetGetChipsCallback", SetGetChipsCallback)
exports("SetTakeChipsCallback", SetTakeChipsCallback)
exports("SetGiveChipsCallback", SetGiveChipsCallback)