-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLlamaJack.lua
330 lines (284 loc) · 7.89 KB
/
LlamaJack.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
-- Configure this to the process ID of the world you want to send chat messages to, currently the blackjack process is hard coded for the below world id
WORLD_ID = 'RXeJvxYtBVdZZ8xR9JJEUtNBXqwQmOrTZthiQL-bZD4'
BlackJackProcess = '1NroE5BWvZXvoSuQLuXRvRPaHTLZS940zgAxwG_1mcA'
-- Enter in world process
-- RealityEntitiesStatic = {
-- ['zsU5NxCo8WbpyLCqXrckjmXfP0PWpAGzo1E-W_iegbI'] = {
-- Position = { 11, 8 },
-- Type = 'Avatar',
-- Metadata = {
-- DisplayName = "LLama-Jack",
-- SkinNumber = 9,
-- Interaction = {
-- Type = "SchemaExternalForm",
-- Id = "BlackJack",
-- }
-- }
-- }
-- }
local json = require('json')
Cards = {
-- Spades
["A of Spades"] = "🂡",
["2 of Spades"] = "🂢",
["3 of Spades"] = "🂣",
["4 of Spades"] = "🂤",
["5 of Spades"] = "🂥",
["6 of Spades"] = "🂦",
["7 of Spades"] = "🂧",
["8 of Spades"] = "🂨",
["9 of Spades"] = "🂩",
["10 of Spades"] = "🂪",
["J of Spades"] = "🂫",
["Q of Spades"] = "🂭",
["K of Spades"] = "🂮",
-- Hearts
["A of Hearts"] = "🂱",
["2 of Hearts"] = "🂲",
["3 of Hearts"] = "🂳",
["4 of Hearts"] = "🂴",
["5 of Hearts"] = "🂵",
["6 of Hearts"] = "🂶",
["7 of Hearts"] = "🂷",
["8 of Hearts"] = "🂸",
["9 of Hearts"] = "🂹",
["10 of Hearts"] = "🂺",
["J of Hearts"] = "🂻",
["Q of Hearts"] = "🂽",
["K of Hearts"] = "🂾",
-- Diamonds
["A of Diamonds"] = "🃁",
["2 of Diamonds"] = "🃂",
["3 of Diamonds"] = "🃃",
["4 of Diamonds"] = "🃄",
["5 of Diamonds"] = "🃅",
["6 of Diamonds"] = "🃆",
["7 of Diamonds"] = "🃇",
["8 of Diamonds"] = "🃈",
["9 of Diamonds"] = "🃉",
["10 of Diamonds"] = "🃊",
["J of Diamonds"] = "🃋",
["Q of Diamonds"] = "🃍",
["K of Diamonds"] = "🃎",
-- Clubs
["A of Clubs"] = "🃑",
["2 of Clubs"] = "🃒",
["3 of Clubs"] = "🃓",
["4 of Clubs"] = "🃔",
["5 of Clubs"] = "🃕",
["6 of Clubs"] = "🃖",
["7 of Clubs"] = "🃗",
["8 of Clubs"] = "🃘",
["9 of Clubs"] = "🃙",
["10 of Clubs"] = "🃚",
["J of Clubs"] = "🃛",
["Q of Clubs"] = "🃝",
["K of Clubs"] = "🃞"
}
Llama = Llama or nil
LLAMA_TOKEN_PROCESS = 'pazXumQI-HPH7iFGfTC-4_7biSnqz_U67oFAGry5zUY'
LLAMA_TOKEN_DENOMINATION = 12
LLAMA_TOKEN_MULTIPLIER = 10 ^ LLAMA_TOKEN_DENOMINATION
LLAMA_JOKE_PRICE_WHOLE_MIN = 1
LLAMA_JOKE_PRICE_WHOLE_MIN_QUANTITY = LLAMA_JOKE_PRICE_WHOLE_MIN * LLAMA_TOKEN_MULTIPLIER
function StartGameSchema()
return [[
{
"type": "object",
"required": [
"Action",
"Recipient",
"Quantity",
"X-Note"
],
"properties": {
"Action": {
"type": "string",
"const": "Transfer"
},
"Recipient": {
"type": "string",
"const": "]] .. BlackJackProcess .. [["
},
"Quantity": {
"type": "number",
"const": ]] .. LLAMA_JOKE_PRICE_WHOLE_MIN_QUANTITY .. [[
},
"X-Note": {
"type": "string",
"const": "LlamaMessage"
}
}
}
]]
end
function PlayHandSchema()
return [[
{
"type": "object",
"required": [
"Action",
"X-Note"
],
"properties": {
"Action": {
"title": "Action",
"type": "string",
"enum": ["Hit", "Stay"]
},
"X-Note": {
"type": "string",
"const": "LlamaMessage"
}
}
}
]]
end
Handlers.add(
'SchemaExternal',
Handlers.utils.hasMatchingTag('Action', 'SchemaExternal'),
function(msg)
print('SchemaExternal')
ao.send({
Target = BlackJackProcess,
Tags = {
Action = 'showState',
Caller = msg.From,
},
})
print("The message should have sent")
end
)
Handlers.add(
"StateMessage",
Handlers.utils.hasMatchingTag('Action', 'BlackJackMessage'),
function(msg)
print("BlackJack state received.")
local account = msg.Player
local truncatedAccount = account:sub(1, 3) .. "..." .. account:sub(-3)
local messageData = msg.Data
-- Function to calculate hand value
local function calculateHandValue(hand)
local value = 0
local aces = 0
for _, card in ipairs(hand) do
if card.value == "A" then
value = value + 11
aces = aces + 1
elseif card.value == "K" or card.value == "Q" or card.value == "J" or card.value == "10" then
value = value + 10
else
value = value + tonumber(card.value)
end
end
while value > 21 and aces > 0 do
value = value - 10
aces = aces - 1
end
return value
end
local function formatHand(hand)
local handStr = ""
for i, card in ipairs(hand) do
local cardKey = card.value .. " of " .. string.sub(card.suit, 1, 1):upper() .. string.sub(card.suit, 2):lower()
local unicodeCard = Cards[cardKey] or cardKey
handStr = handStr .. unicodeCard
if i < #hand then
handStr = handStr .. ", "
end
end
return handStr
end
local function getActiveHand(state)
local activeHandIndex = state.activeHandIndex or 1
return state.hands[activeHandIndex]
end
if msg.Data == "This game has ended" or msg.Data == "You have no active game, start one by sending a bet" then
local stateDescription
if msg.State then
local state = json.decode(msg.State)
-- Dealer's hand
local dealerHand = state.dealerCards
local dealerValue = calculateHandValue(dealerHand)
-- Player's active hand
local activeHand = getActiveHand(state)
local playerHand = activeHand.cards
local playerValue = calculateHandValue(playerHand)
stateDescription = string.format(
"Dealer's Hand: %s (Value: %d)\nYour Hand: %s (Value: %d)\n\n%s",
formatHand(dealerHand),
tonumber(dealerValue),
formatHand(playerHand),
tonumber(playerValue),
"This game has ended. Start a new game by transferring 1 $Llama."
)
else
stateDescription = msg.Data .. " Start a new game by transferring 1 $Llama"
end
Send({
Target = account,
Tags = { Type = 'SchemaExternal' },
Data = json.encode({
BlackJack = {
Target = LLAMA_TOKEN_PROCESS,
Title = "Deal me in",
Description = stateDescription,
Schema = { Tags = json.decode(StartGameSchema()) },
},
})
})
Send({
Target = WORLD_ID,
Tags = {
Action = 'ChatMessage',
['Author-Name'] = "Llama-Jack"
},
Data = truncatedAccount .. " " .. msg.Data
})
else
local state = json.decode(msg.State)
-- Dealer's hand
local dealerHand = state.dealerCards
local dealerValue = calculateHandValue(dealerHand)
-- Player's active hand
local activeHand = getActiveHand(state)
local playerHand = activeHand.cards
local playerValue = calculateHandValue(playerHand)
local description = string.format(
"Dealer's Hand: %s (Value: %d)\nYour Hand: %s (Value: %d)",
formatHand(dealerHand),
tonumber(dealerValue),
formatHand(playerHand),
tonumber(playerValue)
)
print("active game")
Send({
Target = account,
Tags = { Type = "SchemaExternal" },
Data = json.encode({
BlackJack = {
Target = BlackJackProcess,
Title = "Play Hand",
Description = description,
Schema = { Tags = json.decode(PlayHandSchema()) }
}
})
})
local chatMessage
if not state.isHistoric then
chatMessage = truncatedAccount .. " current game status: " .. description
else
chatMessage = truncatedAccount .. " final state of last game: " .. description
end
Send({
Target = WORLD_ID,
Tags = {
Action = 'ChatMessage',
['Author-Name'] = "Llama-Jack"
},
Data = chatMessage
})
end
print("Schema sent.")
end
)