-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHandlers.lua
More file actions
181 lines (162 loc) · 5.99 KB
/
Handlers.lua
File metadata and controls
181 lines (162 loc) · 5.99 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
-- Handlers.lua
require("State")
require("Utils")
require("Constants")
local json = require("json")
Handlers = Handlers or {}
-- Updated Balance handler to check against all tokens in Constants
Handlers.add(
"Local-Balances",
function(msg)
for _, token in ipairs(Constants.Tokens) do
if msg.From == token.process and msg.Tags.Balance then
return true
end
end
return false
end,
function(msg)
Utils.updateRandomness(msg["Block-Height"])
print("Attempting to update house balance for " .. msg.From)
HouseBalance[msg.From] = tonumber(msg.Tags.Balance)
print("balance updated")
end
)
Handlers.add(
"Credit-Notice",
Handlers.utils.hasMatchingTag("Action", "Credit-Notice"),
function(msg)
Utils.updateRandomness(msg["Block-Height"])
local playerName = msg.Tags.Sender
local quantity = tonumber(msg.Tags.Quantity)
local xNote = msg.Tags["X-Note"]
-- print("credit notice received")
local success, tokenValid, tokenDetails = pcall(Utils.checkTokenValidity, msg.From)
if not success then
print("Error in checkTokenValidity: " .. tostring(tokenValid)) -- tokenValid contains the error message in this case
return
end
if not tokenValid then
print("Invalid TOken")
Utils.returnBet(msg.From, playerName, quantity, "Invalid token received.")
return
end
local llama
if xNote == "LlamaMessage" then
llama = Constants.ExclusiveLlama
end
if xNote == "Bankroll" then
Send({ Target = msg.From, Action = "Balance" })
return
end
if tokenDetails and not State.GameStates[playerName] then
-- print(tokenDetails)
local newGameSuccess, err = pcall(Utils.handleNewGame, playerName, quantity, tokenDetails, llama)
if not newGameSuccess then
print("Error in handleNewGame: " .. err)
end
else
local gameState = State.GameStates[playerName]
if gameState and gameState.token ~= msg.From then
Utils.returnBet(msg.From, playerName, quantity, "Token mismatch in active game.")
return
end
if xNote == "Double down bet" then
Utils.handleDoubleDown(playerName, quantity, gameState)
elseif xNote == "Split bet" then
Utils.handleSplitBet(playerName, quantity, gameState)
elseif xNote == "Insurance bet" then
Utils.handleInsuranceBet(playerName, quantity, gameState)
else
Utils.returnBet(msg.From, playerName, quantity,
"You already have an active game. Finish it before starting a new one.")
end
end
end
)
Handlers.add(
"Hit",
function(msg) return msg.Tags.Action == "Hit" end,
function(msg)
Utils.updateRandomness(msg["Block-Height"])
local playerName = msg.From
local llama
if msg['X-Note'] == "LlamaMessage" then
llama = Constants.ExclusiveLlama
end
print(llama)
if not State.GameStates[playerName] then
Send({
Target = playerName,
Action = "BlackJackMessage",
Data = "You have no active game. Start one by placing a bet."
})
else
local gameState = State.GameStates[playerName]
local newCard = table.remove(gameState.deck)
table.insert(gameState.hands[gameState.activeHandIndex].cards, newCard)
local playerHandValue = Utils.calculateHandValue(gameState.hands[gameState.activeHandIndex].cards)
if playerHandValue == 21 then
Send({ Target = playerName, Action = "BlackJackMessage", Data = "21! Dealer's draw" })
State.moveToNextHandOrDealer(playerName, llama)
elseif playerHandValue > 21 then
Send({
Target = playerName,
Action = "BlackJackMessage",
Data = "Busted! Your hand value exceeded 21. Game over for this hand."
})
gameState.hands[gameState.activeHandIndex].dealerCardShown = true
State.moveToNextHandOrDealer(playerName, llama)
else
State.sendGameStateMessage(playerName, llama)
end
end
end
)
Handlers.add(
"Stay",
function(msg) return msg.Tags.Action == "Stay" end,
function(msg)
Utils.updateRandomness(msg["Block-Height"])
local playerName = msg.From
if msg['X-Note'] == "LlamaMessage" then
llama = Constants.ExclusiveLlama
end
if not State.GameStates[playerName] then
Send({
Target = llama or playerName,
Action = "BlackJackMessage",
Data = "You have no active game. Start one by placing a bet."
})
else
State.moveToNextHandOrDealer(playerName, llama)
end
end
)
Handlers.add(
"showState",
function(msg) return msg.Tags.Action == "showState" end,
function(msg)
Utils.updateRandomness(msg["Block-Height"])
-- print("Getting Game State To Send")
local caller = msg.Caller or msg.From
local success, err = pcall(State.sendGameStateMessage, caller, msg.From)
if not success then print("Error in showState handler: " .. err) end
end
)
Handlers.add(
"TokenInfo",
function(msg) return msg.Tags.Action == "TokenInfo" end,
function(msg)
Utils.updateRandomness(msg["Block-Height"])
Send({ Target = msg.From, Action = "TokenInfo", Data = json.encode(Constants.Tokens) })
end
)
function Handlers.updateHouseBalance(token, balance)
HouseBalance[token] = balance
end
function Handlers.getHouseBalance(token)
Send({ Target = token, Action = "Balance" })
end
HandlersList = Handlers.list
return HandlersList