-
Notifications
You must be signed in to change notification settings - Fork 1
/
LlamaLotteryNpc.lua
212 lines (190 loc) · 5.21 KB
/
LlamaLotteryNpc.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
TARGET_WORLD_PID = TARGET_WORLD_PID or "9a_YP6M7iN7b6QUoSvpoV3oe3CqxosyuJnraCucy5ss"
LlamaCoinProcessId = LlamaCoinProcessId or "pazXumQI-HPH7iFGfTC-4_7biSnqz_U67oFAGry5zUY"
POOL = POOL or 'CWmP2C0iAZyFjfdkSZU2nl4kWEDfMIcIn5ESS_2ptw4'
HasReg = HasReg or false
LlamaCoinDenomination = 12
OneLlamaCoin = 10 ^ LlamaCoinDenomination
RecivceMsgTimes = RecivceMsgTimes or 0
LastPaticipantCount = LastPaticipantCount or 0
local json = require("json")
if HasReg == false then
Register()
HasReg = true
end
function Register()
ao.send({
Target = TARGET_WORLD_PID,
Tags = {
Action = "Reality.EntityCreate",
},
Data = json.encode({
Type = "Avatar",
Metadata = {
DisplayName = "Llama lottery",
SkinNumber = 1,
Interaction = {
Type = 'SchemaExternalForm',
Id = 'Lottery'
},
},
}),
})
end
function Move()
ao.send({
Target = TARGET_WORLD_PID,
Tags = {
Action = "Reality.EntityUpdatePosition",
},
Data = json.encode({
Position = {
math.random(-3, 3),
math.random(12, 15),
},
}),
})
end
function QueryRoundInfo()
ao.send({
Target = POOL,
Tags = {
Action = "RoundInfo",
}
})
end
function LlamaLotteryAttendSchemaTags()
return [[
{
"type": "object",
"required": [
"Action",
"Recipient",
"Quantity",
"X-Transfer-Purpose"
],
"properties": {
"Action": {
"type": "string",
"const": "Transfer"
},
"Recipient": {
"type": "string",
"const": "]] .. POOL .. [["
},
"Quantity": {
"type": "number",
"const": ]] .. 1 .. [[,
"$comment": "]] .. 1000000000000 .. [["
},
"X-Transfer-Purpose": {
"type": "string",
"const": "Lottery"
},
}
}
]]
end
function ChatToWorld(data)
ao.send({
Target = TARGET_WORLD_PID,
Tags = {
Action = 'ChatMessage',
['Author-Name'] = 'Llama Lottery',
},
Data = data
})
end
Handlers.add(
'SchemaExternal',
Handlers.utils.hasMatchingTag('Action', 'SchemaExternal'),
function(msg)
ao.send({
Target = msg.From,
Tags = { Type = 'SchemaExternal' },
Data = json.encode({
Lottery = {
Target = LlamaCoinProcessId,
Title = "Attend Llama Lottery?",
Description = [[
Pay 1 Llama Coin to enter the Llama Lottery. The winner will share 80% of the prize pool in Llama Coins.
The winner will be determined when there are 10 participants.
]],
Schema = {
Tags = json.decode(LlamaLotteryAttendSchemaTags()),
},
},
})
})
end
)
Handlers.add(
"CronTick",
Handlers.utils.hasMatchingTag("Action", "Cron"),
function()
QueryRoundInfo()
Move()
end
)
Handlers.add(
"ShowLottery",
function (msg)
if msg.Tags.Action == "RespRoundInfo" and msg.From == POOL then
return true
else
return false
end
end,
function (msg)
local lotteryInfo = json.decode(msg.Data)
print(lotteryInfo)
if lotteryInfo.count <= 0 then
return
end
local balance = lotteryInfo.balance / OneLlamaCoin
if lotteryInfo.count < 10 then
if RecivceMsgTimes > 5 or LastPaticipantCount ~= lotteryInfo.count then
local data = lotteryInfo.count .. " players participated in this round. If there are 10 participants, I will share "
.. balance .. " Llama Coins to the winners."
ChatToWorld(data)
RecivceMsgTimes = 0
LastPaticipantCount = lotteryInfo.count
else
RecivceMsgTimes = RecivceMsgTimes + 1
print("RecivceMsgTimes: " .. RecivceMsgTimes)
print("LastPaticipantCount:" .. LastPaticipantCount)
end
return
end
if lotteryInfo.count >= 10 then
ao.send({
Target = TARGET_WORLD_PID,
Tags = {
Action = 'ChatMessage',
['Author-Name'] = 'Llama Lottery',
},
Data = "There are 10 participants. I will share " .. balance .. " Llama Coins to the winners soon.",
})
end
end
)
Handlers.add(
"ShowLotteryResult",
function (msg)
if msg.Tags.Action == "DrawLotteryResult" and msg.From == POOL then
return true
else
return false
end
end,
function (msg)
print("Show lottery result")
ao.send({
Target = TARGET_WORLD_PID,
Tags = {
Action = 'ChatMessage',
['Author-Name'] = 'Llama Lottery',
},
Data = msg.Data,
})
end
)