-
Notifications
You must be signed in to change notification settings - Fork 42
/
web_playerranks.lua
422 lines (318 loc) · 11.4 KB
/
web_playerranks.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
-- web_playerranks.lua
-- Implements the Player Ranks tab in the webadmin
--- Maximum number of players displayed on a single page.
local PLAYERS_PER_PAGE = 20
local ins = table.insert
local con = table.concat
--- Updates the rank from the ingame player with this uuid
local function UpdateIngamePlayer(a_PlayerUUID, a_Message)
cRoot:Get():ForEachPlayer(
function(a_Player)
if (a_Player:GetUUID() == a_PlayerUUID) then
if (a_Message ~= "") then
a_Player:SendMessage(a_Message)
end
a_Player:LoadRank()
end
end
)
end
--- Returns the HTML contents of the rank list
local function GetRankList(a_SelectedRank)
local RankList = {}
ins(RankList, "<select name='RankName'>")
local AllRanks = cRankManager:GetAllRanks()
table.sort(AllRanks)
for _, Rank in ipairs(AllRanks) do
local HTMLRankName = cWebAdmin:GetHTMLEscapedString(Rank)
ins(RankList, "<option value='")
ins(RankList, HTMLRankName)
if (HTMLRankName == a_SelectedRank) then
ins(RankList, "' selected>")
else
ins(RankList, "'>")
end
ins(RankList, HTMLRankName)
ins(RankList, "</option>")
end
ins(RankList, "</select>")
return con(RankList)
end
--- Returns the HTML contents of a single row in the Players table
local function GetPlayerRow(a_PlayerUUID)
-- Get the player name/rank:
local PlayerName = cRankManager:GetPlayerName(a_PlayerUUID)
local PlayerRank = cRankManager:GetPlayerRankName(a_PlayerUUID)
-- First row: player name:
local Row = {"<tr><td>"}
ins(Row, cWebAdmin:GetHTMLEscapedString(PlayerName))
ins(Row, "</td><td>")
-- Rank:
ins(Row, cWebAdmin:GetHTMLEscapedString(PlayerRank))
-- Display actions for this player:
ins(Row, "</td><td><form style='float: left'>")
ins(Row, GetFormButton("editplayer", "Edit", {PlayerUUID = a_PlayerUUID}))
ins(Row, "</form><form style='float: left'>")
ins(Row, GetFormButton("confirmdel", "Remove rank", {PlayerUUID = a_PlayerUUID, PlayerName = PlayerName}))
-- Terminate the row and return the entire concatenated string:
ins(Row, "</form></td></tr>")
return con(Row)
end
--- Returns the HTML contents of the main Rankplayers page
local function ShowMainPlayersPage(a_Request)
-- Read the page number:
local PageNumber = tonumber(a_Request.Params["PageNumber"])
if (PageNumber == nil) then
PageNumber = 1
end
local StartRow = (PageNumber - 1) * PLAYERS_PER_PAGE
local EndRow = PageNumber * PLAYERS_PER_PAGE - 1
-- Accumulator for the page data
local PageText = {}
ins(PageText, "<p><a href='?subpage=addplayer'>Add a new player</a>, <a href='?subpage=confirmclear'>Clear players</a></p>")
-- Add a table describing each player:
ins(PageText, "<table><tr><th>Playername</th><th>Rank</th><th>Action</th></tr>\n")
local AllPlayers = cRankManager:GetAllPlayerUUIDs()
for i = StartRow, EndRow, 1 do
local PlayerUUID = AllPlayers[i + 1]
if (PlayerUUID ~= nil) then
ins(PageText, GetPlayerRow(PlayerUUID))
end
end
ins(PageText, "</table>")
-- Calculate the page num:
local MaxPages = math.floor((#AllPlayers + PLAYERS_PER_PAGE - 1) / PLAYERS_PER_PAGE)
-- Display the pages list:
ins(PageText, "<table style='width: 100%;'><tr>")
if (PageNumber > 1) then
ins(PageText, "<td><a href='?PageNumber=" .. (PageNumber - 1) .. "'><b><<<</b></a></td>")
else
ins(PageText, "<td><b><</b></td>")
end
ins(PageText, "<th style='width: 100%; text-align: center;'>Page " .. PageNumber .. " of " .. MaxPages .. "</th>")
if (PageNumber < MaxPages) then
ins(PageText, "<td><a href='?PageNumber=" .. (PageNumber + 1) .. "'><b>>>></b></a></td>")
else
ins(PageText, "<td><b>></b></td>")
end
ins(PageText, "</tr></table>")
-- Return the entire concatenated string:
return con(PageText)
end
--- Returns the HTML contents of the player add page
local function ShowAddPlayerPage(a_Request)
return [[
<h4>Add Player</h4>
<form method="POST">
<input type="hidden" name="subpage" value="addplayerproc" />
<table>
<tr>
<th>Player Name:</th>
<td><input type="text" name="PlayerName" maxlength="16" /></td>
</tr>
<tr>
<th>Player UUID (short):</th>
<td>
<input type="text" name="PlayerUUID" maxlength="32" />
If you leave this empty, the server will generate the UUID automatically.
</td>
</tr>
<tr>
<th>Rank</th>
<td>]] .. GetRankList(cRankManager:GetDefaultRank()) .. [[</td>
</tr>
<tr>
<td />
<td><input type="submit" name="AddPlayer" value="Add Player" /></td>
</tr>
</table>
</form>]]
end
--- Processes the AddPlayer page's input, creating a new player and redirecting to the player rank list
local function ShowAddPlayerProcessPage(a_Request)
-- Check the received values:
local PlayerName = a_Request.PostParams["PlayerName"]
local PlayerUUID = a_Request.PostParams["PlayerUUID"]
local RankName = a_Request.PostParams["RankName"]
if ((PlayerName == nil) or (PlayerUUID == nil) or (RankName == nil) or (RankName == "")) then
return HTMLError("Invalid request received, missing values.")
end
-- Check if playername is given
if (PlayerName == "") then
return [[
<h4>Add Player</h4>
<p>Missing Playername or name is longer than 16 chars!</p>
<p><a href="?subpage=addplayer">Go back</a></p>
]]
end
-- Search the uuid (if uuid line is empty)
if (PlayerUUID == "") then
if (cRoot:Get():GetServer():ShouldAuthenticate()) then
PlayerUUID = cMojangAPI:GetUUIDFromPlayerName(PlayerName, false)
else
PlayerUUID = cClientHandle:GenerateOfflineUUID(PlayerName)
end
end
-- Check if the uuid is correct
if ((PlayerUUID == "") or (string.len(PlayerUUID) ~= 32)) then
if (a_Request.PostParams["PlayerUUID"] == "") then
return [[
<h4>Add Player</h4>
<p>Bad uuid. <a href="?subpage=addplayer">Go back</a></p>
]]
else
return [[
<h4>Add Player</h4>
<p>UUID for player ]] .. PlayerName .. [[ not found!<br />
Maybe the player doesn't exists?</p>
<p><a href="?subpage=addplayer">Go back</a></p>
]]
end
end
-- Exists the player already?
if (cRankManager:GetPlayerRankName(PlayerUUID) ~= "") then
return [[
<h4>Add Player</h4>
<p>Can't create the player because a player with the same uuid already exists!</p>
<p><a href="?subpage=addplayer">Go back</a></p>
]]
end
-- Add the new player:
cRankManager:SetPlayerRank(PlayerUUID, PlayerName, RankName)
UpdateIngamePlayer(PlayerUUID, "You were assigned the rank " .. RankName .. " by webadmin.")
return "<p>Player created. <a href='/" .. a_Request.Path .. "'>Return</a>.</p>"
end
--- Deletes the specified player and redirects back to list
local function ShowDelPlayerPage(a_Request)
-- Check the input:
local PlayerUUID = a_Request.PostParams["PlayerUUID"]
if (PlayerUUID == nil) then
return HTMLError("Bad request")
end
-- Delete the player:
cRankManager:RemovePlayerRank(PlayerUUID)
UpdateIngamePlayer(PlayerUUID, "You were assigned the rank " .. cRankManager:GetDefaultRank() .. " by webadmin.")
-- Redirect back to list:
return "<p>Rank deleted. <a href='/" .. a_Request.Path .. "'>Return to list</a>."
end
--- Shows a confirmation page for deleting the specified player
local function ShowConfirmDelPage(a_Request)
-- Check the input:
local PlayerUUID = a_Request.PostParams["PlayerUUID"]
local PlayerName = a_Request.PostParams["PlayerName"]
if ((PlayerUUID == nil) or (PlayerName == nil)) then
return HTMLError("Bad request")
end
-- Show confirmation:
return [[
<h4>Delete player</h4>
<p>Are you sure you want to delete player ]] .. PlayerName .. [[?<br />
<short>UUID: ]] .. PlayerUUID .. [[</short></p>
<p><a href='?subpage=delplayer&PlayerUUID=]] .. PlayerUUID .. [['>Delete</a></p>
<p><a href='/]] .. a_Request.Path .. [['>Cancel</a></p>
]]
end
--- Returns the HTML contents of the playerrank edit page.
local function ShowEditPlayerRankPage(a_Request)
-- Check the input:
local PlayerUUID = a_Request.PostParams["PlayerUUID"]
if ((PlayerUUID == nil) or (string.len(PlayerUUID) ~= 32)) then
return HTMLError("Bad request")
end
-- Get player name:
local PlayerName = cRankManager:GetPlayerName(PlayerUUID)
local PlayerRank = cRankManager:GetPlayerRankName(PlayerUUID)
return [[
<h4>Change rank from ]] .. PlayerName .. [[</h4>
<form method="POST">
<input type="hidden" name="subpage" value="editplayerproc" />
<input type="hidden" name="PlayerUUID" value="]] .. PlayerUUID .. [[" />
<table>
<tr>
<th>UUID</th>
<td>]] .. PlayerUUID .. [[</td>
</tr>
<tr>
<th>Current Rank</th>
<td>]] .. PlayerRank .. [[</td>
</tr>
<tr>
<th>New Rank</th>
<td>]] .. GetRankList(PlayerRank) .. [[</td>
</tr>
<tr>
<td />
<td><input type="submit" name="EditPlayerRank" value="Edit Rank" /></td>
</tr>
</table>
</form>
]]
end
--- Processes the edit rank page's input, change the rank and redirecting to the player rank list
local function ShowEditPlayerRankProcessPage(a_Request)
-- Check the input:
local PlayerUUID = a_Request.PostParams["PlayerUUID"]
local NewRank = a_Request.PostParams["RankName"]
if ((PlayerUUID == nil) or (NewRank == nil) or (string.len(PlayerUUID) ~= 32) or (NewRank == "")) then
return HTMLError("Bad request")
end
-- Get the player name:
local PlayerName = cRankManager:GetPlayerName(PlayerUUID)
if (PlayerName == "") then
return [[
<p>Can't change the rank because this user doesn't exists!</p>
<p><a href="/]] .. a_Request.Path .. [[">Go back</a></p>
]]
end
-- Edit the rank:
cRankManager:SetPlayerRank(PlayerUUID, PlayerName, NewRank)
UpdateIngamePlayer(PlayerUUID, "You were assigned the rank " .. NewRank .. " by webadmin.")
return "<p>The rank from player " .. PlayerName .. " was changed to " .. NewRank .. ". <a href='/" .. a_Request.Path .. "'>Return</a>.</p>"
end
--- Processes the clear of all player ranks
local function ShowClearPlayersPage(a_Request)
cRankManager:ClearPlayerRanks()
LOGINFO("WebAdmin: A user cleared all player ranks")
-- Update ingame players:
cRoot:Get():ForEachPlayer(
function(a_Player)
a_Player:LoadRank()
end
)
return "<p>Cleared all player ranks! <a href='/" .. a_Request.Path .. "'>Return</a>.</p>"
end
--- Shows a confirmation page for deleting all players
local function ShowConfirmClearPage(a_Request)
-- Show confirmation:
return [[
<h4>Clear all player ranks</h4>
<p>Are you sure you want to delete all players from the database?</p>
<p><a href='?subpage=clear'>Clear</a></p>
<p><a href='/]] .. a_Request.Path .. [['>Cancel</a></p>
]]
end
--- Handlers for the individual subpages in this tab
-- Each item maps a subpage name to a handler function that receives a HTTPRequest object and returns the HTML to return
local g_SubpageHandlers =
{
[""] = ShowMainPlayersPage,
["addplayer"] = ShowAddPlayerPage,
["addplayerproc"] = ShowAddPlayerProcessPage,
["delplayer"] = ShowDelPlayerPage,
["confirmdel"] = ShowConfirmDelPage,
["editplayer"] = ShowEditPlayerRankPage,
["editplayerproc"] = ShowEditPlayerRankProcessPage,
["clear"] = ShowClearPlayersPage,
["confirmclear"] = ShowConfirmClearPage,
}
--- Handles the web request coming from MCS
-- Returns the entire tab's HTML contents, based on the player's request
function HandleRequest_PlayerRanks(a_Request)
local Subpage = (a_Request.PostParams["subpage"] or "")
local Handler = g_SubpageHandlers[Subpage]
if (Handler == nil) then
return HTMLError("An internal error has occurred, no handler for subpage " .. Subpage .. ".")
end
local PageContent = Handler(a_Request)
return PageContent
end