-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrps-game.lua
More file actions
338 lines (293 loc) · 8.6 KB
/
rps-game.lua
File metadata and controls
338 lines (293 loc) · 8.6 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
-- Game Variables
successText = 'Win'
failedText = "Lose"
drawText = "Draw"
rankList = rankList or {}
pointsList = pointsList or {}
members = members or {}
gameTimeTag = gameTimeTag or ""
turnOrder = turnOrder or {} -- Players' turn
currentTurnIndex = currentTurnIndex or 1 -- The current player's turn
timeout = 60 -- 60 second time-out
timer = nil -- To keep the timer
options = { "Rock", "Paper", "Scissor" }
local function guid()
local seed = { 'e', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }
local tb = {}
for i = 1, 32 do
table.insert(tb, seed[math.random(1, 16)])
end
local sid = table.concat(tb)
return string.format('%s-%s-%s-%s-%s',
string.sub(sid, 1, 8),
string.sub(sid, 9, 12),
string.sub(sid, 13, 16),
string.sub(sid, 17, 20),
string.sub(sid, 21, 32)
)
end
local function getComputerChoice()
return options[math.random(1, #options)]
end
local function getMembers()
local memberList = {}
for id, _ in pairs(members) do
table.insert(memberList, id)
end
return memberList
end
local function addMember(id)
if not members[id] then
members[id] = true
table.insert(turnOrder, id)
end
end
local function getNextTurn()
currentTurnIndex = currentTurnIndex + 1
if currentTurnIndex > #turnOrder then
currentTurnIndex = 1
end
return turnOrder[currentTurnIndex]
end
local function getCurrentTurn()
return turnOrder[currentTurnIndex]
end
local function joinStatistic(id)
addMember(id)
end
local function getPersonPoints(id)
return pointsList[id] or 0
end
local function sortRankList()
table.sort(rankList, function(a, b)
return a.points > b.points
end)
end
local function getGameTimeTag()
local currentTime = os.date("*t", os.time())
return currentTime.year .. '-' .. currentTime.month
end
local function checkRankExpire()
local currentTag = getGameTimeTag()
if gameTimeTag ~= currentTag then
for i = 1, 10 do
if rankList[i] and rankList[i].pid then
ao.send({ Target = ao.id, Action = "Transfer", Recipient = rankList[i].pid, Quantity = tostring(100 - (i - 1) * 10) })
end
end
rankList = {}
gameTimeTag = currentTag
end
end
local function log(message)
print("[LOG] " .. message)
end
local function determineWinner(UserChoice, computerChoice)
if UserChoice == computerChoice then
return drawText
elseif (UserChoice == "Rock" and computerChoice == "Scissor") or
(UserChoice == "Paper" and computerChoice == "Rock") or
(UserChoice == "Scissor" and computerChoice == "Paper") then
return successText
else
return failedText
end
end
local function calculatePoints(result, points)
if result == successText then
points = points + 1
elseif result == failedText then
points = points - 1
end
return points
end
local function startTimer()
if timer then
timer:stop()
end
timer = uv.new_timer()
timer:start(timeout * 1000, 0, function()
log("Turn skipped due to timeout")
skipTurn()
end)
end
local function skipTurn()
local nextTurn = getNextTurn()
ao.send({
Target = nextTurn,
Action = "YourTurn",
Data = "It's your turn now!"
})
startTimer()
end
-- Handler Functions
-- Get All Rank List
Handlers.add(
"HandlerGetRank",
Handlers.utils.hasMatchingTag("Action", "GetRank"),
function(Msg)
log("HandlerGetRank called")
if #rankList == 0 then
ao.send({
Target = Msg.From,
Action = "RankList",
Data = "rankList : No Any Person"
})
return
end
checkRankExpire()
local page = tonumber(Msg.Data)
if (page == nil) then
page = 1
end
local startPos = (page - 1) * 10 + 1
if (startPos > #rankList) then
startPos = 1
end
local endPos = startPos + 10
local maxPos = math.min(endPos, #rankList)
local retText = ''
for i = startPos, maxPos do
retText = retText .. 'Rank ' .. i .. " : " .. rankList[i].name .. " " .. rankList[i].points
if startPos ~= maxPos then
retText = retText .. '\n'
end
end
ao.send({
Target = Msg.From,
Action = "RankList",
Data = retText
})
end
)
-- Get Current Points
Handlers.add(
"HandlerGetPoints",
Handlers.utils.hasMatchingTag("Action", "GetPoints"),
function(Msg)
log("HandlerGetPoints called")
local text = getPersonPoints(Msg.From)
log(text .. " Points")
ao.send({
Target = Msg.From,
Action = "CurrentPoints",
Data = text .. ""
})
end
)
-- Finish Current Points
Handlers.add(
"HandlerFinishPoints",
Handlers.utils.hasMatchingTag("Action", "FinishPoints"),
function(Msg)
log("HandlerFinishPoints called")
checkRankExpire()
local uuid = guid()
log("Generated UUID: " .. uuid)
table.insert(rankList, {
points = pointsList[Msg.From],
name = Msg.Data,
uuid = uuid,
pid = Msg.From
})
log("Updated rankList with new entry")
sortRankList()
log("Sorted rankList")
pointsList[Msg.From] = 0
log("Reset points for: " .. Msg.From)
local current = "Unknown"
for index, obj in pairs(rankList) do
if obj.uuid == uuid then
current = index
break
end
end
log("Current rank: " .. current)
ao.send({
Target = Msg.From,
Action = "FinishPointsResult",
Data = "Hey! You ranked " .. current
})
log("FinishPointsResult sent to " .. Msg.From)
end
)
-- User Makes a Choice
Handlers.add(
"HandlerUserChoice",
Handlers.utils.hasMatchingTag("Action", "UserChoice"),
function(Msg)
log("HandlerUserChoice called")
local currentTurn = getCurrentTurn()
log("Current Turn: " .. currentTurn .. ", Message From: " .. Msg.From)
if Msg.From ~= currentTurn then
ao.send({
Target = Msg.From,
Action = "Error",
Data = "It's not your turn!"
})
log("Error: It's not your turn!")
return
end
if timer then
timer:stop()
end
local UserChoice = Msg.Data
log("UserChoice: " .. UserChoice)
local computerChoice = getComputerChoice()
log("ComputerChoice: " .. computerChoice)
local result = determineWinner(UserChoice, computerChoice)
log("Result: " .. result)
local points = getPersonPoints(Msg.From)
log("Current Points: " .. points)
points = calculatePoints(result, points)
log("Updated Points: " .. points)
pointsList[Msg.From] = points
joinStatistic(Msg.From)
ao.send({
Target = Msg.From,
Action = "UserChoiceResult",
Data = "Your Choice: " .. UserChoice .. ", Computer's Choice: " .. computerChoice .. ", Result: " .. result .. ", Total Points: " .. points
})
log("UserChoiceResult sent")
local nextTurn = getNextTurn()
log("Next Turn: " .. nextTurn)
ao.send({
Target = nextTurn,
Action = "YourTurn",
Data = "It's your turn now!"
})
log("YourTurn sent to " .. nextTurn)
startTimer()
end
)
-- Join Game
Handlers.add(
"HandlerJoinGame",
Handlers.utils.hasMatchingTag("Action", "JoinGame"),
function(Msg)
log("HandlerJoinGame called")
addMember(Msg.From)
ao.send({
Target = Msg.From,
Action = "JoinGameResult",
Data = "You have joined the game!"
})
end
)
-- Get All Members Handler
Handlers.add(
"HandlerGetMembers",
Handlers.utils.hasMatchingTag("Action", "GetMembers"),
function(Msg)
local membersList = getMembers()
local retText = 'Members:\n'
for i, member in ipairs(membersList) do
retText = retText .. i .. ": " .. member .. "\n"
end
ao.send({
Target = Msg.From,
Action = "MembersList",
Data = retText
})
end
)