forked from Spring-Chobby/Chobby
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathapi_battle_proposal_handler.lua
More file actions
314 lines (269 loc) · 9.68 KB
/
Copy pathapi_battle_proposal_handler.lua
File metadata and controls
314 lines (269 loc) · 9.68 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
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function widget:GetInfo()
return {
name = "Battle Proposal Handler",
desc = "Handles the battle proposal system.",
author = "GoogleFrog",
date = "28 March 2020",
license = "GNU LGPL, v2.1 or later",
layer = 0,
enabled = true -- loaded by default?
}
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local parameterStr = {
"minelo=",
"maxelo=",
"minsize=",
"maxsize=",
}
for i = 1, #parameterStr do
parameterStr[i] = {parameterStr[i], string.len(parameterStr[i])}
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Variables
local currentProposal
local acceptedProposal
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function CheckCancelProposal(message)
if string.sub(message, 1, 1) ~= "!" or string.sub(message, 1, 12) ~= "!endproposal"then
return false
end
if currentProposal then
Chotify:Post({
title = "Battle Proposal",
body = "Ongoing proposal ended.",
})
end
currentProposal = nil
return true
end
local function GetProposalFromString(message)
if string.sub(message, 1, 1) ~= "!" or string.sub(message, 1, 14) ~= "!proposebattle"then
return false
end
local data = message:split(" ")
local paramValues = {}
for i = 1, #data do
for j = 1, #parameterStr do
if string.sub(data[i], 1, parameterStr[j][2]) == parameterStr[j][1] then
local value = tonumber(string.sub(data[i], parameterStr[j][2] + 1))
if value then
paramValues[j] = value
end
end
end
end
local proposalValues = {
minelo = paramValues[1] or false,
maxelo = paramValues[2] or false,
minsize = math.max(1, math.floor(paramValues[3] or 4)),
}
proposalValues.maxsize = math.max(proposalValues.minsize, paramValues[4] or 8)
return true, proposalValues
end
local function CheckProposalSent(prop)
if currentProposal and prop.minelo == currentProposal.minelo
and prop.maxelo == currentProposal.maxelo
and prop.minsize == currentProposal.minsize
and prop.maxsize == currentProposal.maxsize then
Chotify:Post({
title = "Battle Proposal",
body = "Invite for ongoing proposal sent",
})
return false
end
currentProposal = prop
currentProposal.currentPlayers = 1
currentProposal.acceptedPlayers = {}
Chotify:Post({
title = "Battle Proposal",
body = "New proposal sent.\nUse !endproposal to cancel.",
})
return true
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- External functions
local BattleProposalHandler = {}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function BattleProposalHandler.AddClickableInvites(userName, preMessage, message, onTextClick, textTooltip)
if not (WG.LibLobby and WG.LibLobby.lobby and userName) then
return
end
local myProposal = (userName == WG.LibLobby.lobby:GetMyUserName())
if myProposal and CheckCancelProposal(message) then
return onTextClick, textTooltip
end
local hasProposal, prop = GetProposalFromString(message)
if not hasProposal then
return onTextClick, textTooltip
end
local myInfo = WG.LibLobby.lobby:GetMyInfo()
local effectiveSkill = math.max(myInfo.skill or 1500, myInfo.casualSkill or 1500)
local skillTooLow = (prop.minelo and effectiveSkill < prop.minelo)
local skillTooHigh = (prop.maxelo and effectiveSkill > prop.maxelo)
if myProposal then
if skillTooLow then
Chotify:Post({
title = "Battle Proposal",
body = "Your skill rating is too low for your proposal",
})
return onTextClick, textTooltip
elseif skillTooHigh then
Chotify:Post({
title = "Battle Proposal",
body = "Your skill rating is too high for your proposal",
})
return onTextClick, textTooltip
end
CheckProposalSent(prop)
end
local startIndex = string.len(preMessage)
local endIndex = startIndex + string.len(message) + 1
if not (skillTooLow or skillTooLow) then
onTextClick[#onTextClick + 1] = {
startIndex = startIndex,
endIndex = endIndex,
OnTextClick = {
function()
if WG.LibLobby and WG.LibLobby.lobby then
acceptedProposal = userName
WG.LibLobby.lobby:BattleProposalRespond(userName, true)
Chotify:Post({
title = "Battle Proposal",
body = "Signed up for " .. userName .. "'s battle",
})
end
end
}
}
end
local proposalString
if skillTooLow then
proposalString = "Your skill rating is too low for this proposal."
elseif skillTooHigh then
proposalString = "Your skill rating is too high for this proposal."
else
if myProposal then
proposalString = "This is your battle proposal.\nOther players may click it to accept."
else
proposalString = "Click to accept " .. userName .. "'s battle"
end
if prop.minelo then
proposalString = proposalString .. "\nMin rating: " .. prop.minelo
end
if prop.maxelo then
proposalString = proposalString .. "\nMax rating: " .. prop.maxelo
end
proposalString = proposalString .. "\nA battle will open when " .. prop.minsize .. " players accept."
end
textTooltip[#textTooltip + 1] = {
startIndex = startIndex,
endIndex = endIndex,
tooltip = proposalString,
}
return onTextClick, textTooltip
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Widget Interface
function DelayedInitialize()
local lobby = WG.LibLobby.lobby
local function OnBattleProposalResponse(listener, userName, accepted)
if (not accepted) then
return
end
if (not currentProposal) or (currentProposal.acceptedPlayers[userName] and not currentProposal.battleHostComplete) then
return
end
local user = lobby:GetUser(userName)
if (not user) or (userName == lobby:GetMyUserName())then
return
end
local effectiveSkill = math.max(user.skill or 1500, user.casualSkill or 1500)
if (currentProposal.minelo and effectiveSkill < currentProposal.minelo) or (currentProposal.maxelo and effectiveSkill > currentProposal.maxelo) then
return
end
if currentProposal.battleHostComplete then
lobby:BattleProposalBattleInvite(userName, currentProposal.battleID, currentProposal.password)
Chotify:Post({
title = "Battle Proposal",
body = userName .. " accepted and joined.",
})
return
end
currentProposal.currentPlayers = currentProposal.currentPlayers + 1
currentProposal.acceptedPlayers[userName] = true
Chotify:Post({
title = "Battle Proposal",
body = userName .. " accepted.\nPlayers: " .. currentProposal.currentPlayers .. "/" .. currentProposal.minsize,
})
if currentProposal.currentPlayers >= currentProposal.minsize and not currentProposal.openingBattleName then
-- Check for users leaving
for acceptedUserName, _ in pairs(currentProposal.acceptedPlayers) do
local currentUser = lobby:GetUser(acceptedUserName)
if (not currentUser) or currentUser.isOffline then
currentProposal.acceptedPlayers[acceptedUserName] = nil
currentProposal.currentPlayers = currentProposal.currentPlayers - 1
end
end
-- Host the battle
if currentProposal.currentPlayers >= currentProposal.minsize then
WG.BattleRoomWindow.LeaveBattle()
currentProposal.password = math.floor(math.random()*100000)
currentProposal.openingBattleName = (lobby:GetMyUserName() or "Player") .. "'s Proposed Battle"
lobby:HostBattle(currentProposal.openingBattleName, currentProposal.password, "Team")
Chotify:Post({
title = "Battle Proposal",
body = "Hosting game",
})
end
end
end
local function OnJoinedBattle(listener, battleID, hashCode)
if not (currentProposal and currentProposal.openingBattleName) then
return
end
local battleInfo = lobby:GetBattle(battleID)
if battleInfo.title ~= currentProposal.openingBattleName then
return
end
for acceptedUserName, _ in pairs(currentProposal.acceptedPlayers) do
lobby:BattleProposalBattleInvite(acceptedUserName, battleID, currentProposal.password)
end
if currentProposal.maxsize then
lobby:SayBattle("!maxplayers " .. currentProposal.maxsize)
end
currentProposal.battleID = battleID
currentProposal.battleHostComplete = true
end
local function OnBattleProposalBattleInvite(listener, userName, battleID, password)
if acceptedProposal ~= userName then
return
end
lobby:JoinBattle(battleID, password)
acceptedProposal = false
Chotify:Post({
title = "Battle Proposal",
body = "Joining " .. userName .. "'s battle",
})
end
lobby:AddListener("OnBattleProposalResponse", OnBattleProposalResponse)
lobby:AddListener("OnJoinedBattle", OnJoinedBattle)
lobby:AddListener("OnBattleProposalBattleInvite", OnBattleProposalBattleInvite)
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function widget:Initialize()
VFS.Include(LUA_DIRNAME .. "widgets/chobby/headers/exports.lua", nil, VFS.RAW_FIRST)
WG.BattleProposalHandler = BattleProposalHandler
WG.Delay(DelayedInitialize, 1)
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------