-
Notifications
You must be signed in to change notification settings - Fork 0
/
modladder.php
265 lines (230 loc) · 11.7 KB
/
modladder.php
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
<?php
require_once("common.php");
if (!isset($_GET["ladder"])) error404();
$ladderID = $_GET["ladder"];
checkLadderMod($ladderID);
$ladderName = db()->stdGet("ladders", array("ladderID"=>$ladderID), "name");
$ladderNameHtml = htmlentities($ladderName);
$removeModeratorError = "";
$change_settings_messages = array();
$change_settings_error = "";
$removeTemplateError = "";
$addTemplateError = "";
$startError = "";
$html = "";
if (($removeTemplate = post("remove-template")) !== null) {
$templateIDSql = db()->addSlashes($removeTemplate);
$ladderIDSql = db()->addSlashes($ladderID);
$sql = "SELECT userID, name, COUNT(templateCount.templateID) AS playableTemplates
FROM ladderPlayers
INNER JOIN users USING(userID)
LEFT JOIN (
SELECT userID, templateID
FROM playerLadderTemplates
WHERE templateID <> $templateIDSql
AND ladderID = $ladderIDSql
AND canPlay = 1
) AS templateCount USING(userID)
WHERE ladderID = $ladderIDSql
AND active = 1
AND joinStatus = 'JOINED'
GROUP BY userID
";
$templateCount = db()->query($sql)->fetchList();
$zeroTemplatePlayers = array();
foreach($templateCount as $user) {
if($user["playableTemplates"] == 0) {
$zeroTemplatePlayers[] = $user["name"];
}
}
$templateNameHtml = htmlentities(db()->stdGet("ladderTemplates", array("ladderID"=>$ladderID, "templateID"=>$removeTemplate), "name"));
if(count($zeroTemplatePlayers) > 0) {
$error = "Template <i>$templateNameHtml</i> cannot be removed, as this would leave ";
if(count($zeroTemplatePlayers) > 10) {
$error .= count($zeroTemplatePlayers) . " players without playable templates. ";
} else {
$error .= "the following players without playable templates: ";
$error .= implode(",", $zeroTemplatePlayers) . ". ";
}
$error .= "<br>Please add another template first, or remove these players.";
$removeTemplateError = formError($error);
} else if(post("confirm") == 1) {
db()->stdDel("playerLadderTemplates", array("ladderID"=>$ladderID, "templateID"=>$removeTemplate));
db()->stdDel("ladderTemplates", array("ladderID"=>$ladderID, "templateID"=>$removeTemplate));
} else {
$html .= operationForm("modladder.php?ladder=$ladderID", null, "Remove template", "Remove template", array(
array("type"=>"hidden", "name"=>"remove-template", "value"=>post("remove-template")),
), null, array("custom"=>"<p>This remove the template <i>$templateNameHtml</i> from this ladder.</p>"));
page($html, "modladder", "Ladder configuration - $ladderNameHtml", "Remove template");
die();
}
} else if (($removeAdmin = post("remove-admin")) !== null) {
if ($removeAdmin == currentUserID()) {
$removeModeratorError = formError("You cannot remove yourself as a moderator.");
} else if (isMod($ladderID, $removeAdmin)) {
db()->stdDel("ladderAdmins", array("userID"=>$removeAdmin, "ladderID"=>$ladderID));
}
} else if(($action = post("action")) !== null) {
if($action == "change-settings") {
$values["name"] = post("name");
if($values["name"] === null || $values["name"] == "") {
$change_settings_error .= formError("Please specify a name.");
}
$values["summary"] = post("summary");
if($values["summary"] === null || $values["summary"] == "") {
$change_settings_error .= formError("Please specify a summary.");
}
if(strlen($values["summary"]) > 255) {
$change_settings_error .= formError("Please make your summary shorter.");
}
$values["message"] = post("message");
$values["accessibility"] = post("accessibility");
if($values["accessibility"] != "PUBLIC" && $values["accessibility"] != "MODERATED") {
$change_settings_error .= formError("Invalid joining policy.");
}
$values["visibility"] = post("visibility");
if($values["visibility"] != "PUBLIC" && $values["visibility"] != "PRIVATE") {
$change_settings_error .= formError("Show on front page answer invalid.");
}
$values["minSimultaneousGames"] = post("minSimultaneousGames");
if(!ctype_digit($values["minSimultaneousGames"])) {
$change_settings_error .= formError("Please enter a number for the minimal simultaneous games.");
}
$values["maxSimultaneousGames"] = post("maxSimultaneousGames");
if(!ctype_digit($values["maxSimultaneousGames"])) {
$change_settings_error .= formError("Please enter a number for the maximal simultaneous games.");
}
if($values["minSimultaneousGames"] > $values["maxSimultaneousGames"]) {
$change_settings_error .= formError("The minimal number of simultaneous games must be smaller than the maximal number of simultaneous games.");
}
if($change_settings_error == "") {
db()->stdSet("ladders", array("ladderID"=>$ladderID), $values);
$change_settings_messages["custom"] = "<div class=\"alert alert-success\" role=\"alert\">Settings saved.</div>";
}
}
if($action == "start-ladder") {
if(!db()->stdExists("ladderTemplates", array("ladderID"=>$ladderID))) {
$startError = formError("Please add templates to this ladder before starting.");
} else if(post("confirm") == 1) {
db()->stdSet("ladders", array("ladderID"=>$ladderID), array("active"=>1));
} else {
$html .= operationForm("modladder.php?ladder=$ladderID", null, "Start Ladder", "Start", array(
array("type"=>"hidden", "name"=>"action", "value"=>"start-ladder"),
), null, array("custom"=>"<p>This will activate the ladder, allowing players to join and play games.</p>"));
page($html, "modladder", "Ladder configuration - $ladderNameHtml", "Start ladder");
die();
}
}
if($action == "stop-ladder") {
if(post("confirm") == 1) {
db()->stdSet("ladders", array("ladderID"=>$ladderID), array("active"=>0));
} else {
$html .= operationForm("modladder.php?ladder=$ladderID", null, "Deactivate Ladder", "Deactivate", array(
array("type"=>"hidden", "name"=>"action", "value"=>"stop-ladder"),
), null, array("custom"=>"<p>This will deactivate the ladder. New games will no longer be created, and players cannot join the ladder anymore.</p>"));
page($html, "modladder", "Ladder configuration - $ladderNameHtml", "Deactivate Ladder");
die();
}
}
if($action == "add-moderator") {
$newModID = post("userID");
if(!isMod($ladderID, $newModID)) {
db()->stdNew("ladderAdmins", array("userID"=>$newModID, "ladderID"=>$ladderID));
}
}
if($action == "ban-player") {
$userID = post("userID");
checkLadderPlayer($ladderID, $userID);
if(post("confirm") == 1) {
removePlayerRank($ladderID, $userID);
db()->stdSet("ladderPlayers", array("ladderID"=>$ladderID, "userID"=>$userID), array("joinStatus"=>"BOOTED"));
redirect("ladder.php?ladder=" . $ladderID);
} else {
$name = db()->stdGet("users", array("userID"=>$userID), "name");
$html .= operationForm("modladder.php?ladder=$ladderID", null, "Ban player from ladder", "Yes, ban this player!", array(
array("type"=>"hidden", "name"=>"action", "value"=>"ban-player"),
array("type"=>"hidden", "name"=>"userID", "value"=>$userID),
), null, array("custom"=>"<p class=\"alert alert-warning\"><b>Warning:</b> Are you sure you want to ban <b>$name</b> from this ladder?</p>"));
page($html, "modladder", "Ladder configuration - $ladderNameHtml", "Ban player");
die();
}
}
if($action == "add-template") {
$template = post("template");
$name = post("templateName");
if(strpos($template, "TemplateID=") === false) {
$warlightTemplateID = (int)$template;
} else {
$warlightTemplateID = (int)substr($template, strrpos($template, "=") + 1);
}
if ($warlightTemplateID == 0) {
$addTemplateError = formError("Invalid template ID.");
} else if ($name == "") {
// do nothing
} else if (db()->stdExists("ladderTemplates", array("ladderID"=>$ladderID, "warlightTemplateID"=>$warlightTemplateID))) {
$addTemplateError = formError("This template is already in use on this ladder.");
} else {
$templateID = db()->stdNew("ladderTemplates", array("ladderID"=>$ladderID, "warlightTemplateID"=>$warlightTemplateID, "name"=>$name));
$ladderIDSql = db()->addSlashes($ladderID);
$players = db()->query("
SELECT userID, warlightUserID, newTemplateScore
FROM ladderPlayers
LEFT JOIN users USING(userID)
WHERE ladderID = $ladderIDSql
")->fetchList();
foreach($players as $player) {
$result = apiGetUserTemplates($player["warlightUserID"], $warlightTemplateID);
$canPlay = in_array($warlightTemplateID, $result);
db()->stdNew("playerLadderTemplates", array("userID"=>$player["userID"], "ladderID"=>$ladderID, "templateID"=>$templateID, "score"=>$player["newTemplateScore"], "canPlay"=>$canPlay));
}
}
}
}
if(db()->stdGet("ladders", array("ladderID"=>$ladderID), "active") == 0) {
$html .= operationForm("modladder.php?ladder=$ladderID", $startError, "Start Ladder", "Start", array(
array("type"=>"hidden", "name"=>"action", "value"=>"start-ladder"),
), null, array("custom"=>"<p>This will activate the ladder, allowing players to join and play games.</p>"));
}
if(db()->stdGet("ladders", array("ladderID"=>$ladderID), "accessibility") == "MODERATED") {
$html .= "<div class=\"accept-players\">\n";
$html .= "<h2>Joining Players</h2>\n";
$html .= "<p>These players want to join this ladder. As a moderator, you can either accept or reject them.</p>";
$html .= renderAcceptList($ladderID, "New players", 0, 10);
$html .= "</div>\n";
}
if($change_settings_error == "") {
$values = db()->stdGet("ladders", array("ladderID"=>$ladderID), array("name", "summary", "message", "accessibility", "visibility", "minSimultaneousGames", "maxSimultaneousGames"));
}
$html .= operationForm("modladder.php?ladder=$ladderID", $change_settings_error, "Ladder Settings", "Save", array(
array("type"=>"hidden", "name"=>"action", "value"=>"change-settings"),
array("title"=>"Name", "type"=>"text", "name"=>"name"),
array("title"=>"Summary", "type"=>"text", "name"=>"summary"),
array("title"=>"Message", "type"=>"textarea", "name"=>"message"),
array("title"=>"Joining policy", "type"=>"dropdown", "name"=>"accessibility", "options"=>array(array("value"=>"PUBLIC", "label"=>"Everyone can join"), array("value"=>"MODERATED", "label"=>"Moderator must approve"))),
array("title"=>"Show on front page", "type"=>"dropdown", "name"=>"visibility", "options"=>array(array("value"=>"PUBLIC", "label"=>"Yes"), array("value"=>"PRIVATE", "label"=>"No"))),
array("title"=>"Number of simultaneous games", "type"=>"colspan", "columns"=>array(
array("type"=>"html", "html"=>"Minimal"),
array("type"=>"text", "name"=>"minSimultaneousGames", "cellclass"=>"stretch-50"),
array("type"=>"html", "html"=>"Maximal"),
array("type"=>"text", "name"=>"maxSimultaneousGames", "cellclass"=>"stretch-50")
)),
), $values, $change_settings_messages);
$html .= "<div class=\"ladder-templates\">\n";
$html .= "<h2>Templates</h2>\n";
$html .= $addTemplateError;
$html .= $removeTemplateError;
$html .= "<p>Those are the games that can be played on this ladder. Players can choose which of these templates they like to play.</p>";
$html .= renderLadderTemplates($ladderID, "modladder.php?ladder=$ladderID", "templateName", "template", "add-template");
$html .= "</div>\n";
$html .= "<div class=\"ladder-moderators\">\n";
$html .= "<h2>Moderators</h2>\n";
$html .= $removeModeratorError;
$html .= "<p>Moderators can change ladder settings, ban players, and accept new recruits.</p>\n";
$html .= renderLadderMods($ladderID, "modladder.php?ladder=$ladderID", "userID", "add-moderator");
$html .= "</div>\n";
if(!db()->stdGet("ladders", array("ladderID"=>$ladderID), "active") == 0) {
$html .= operationForm("modladder.php?ladder=$ladderID", "", "Deactivate Ladder", "Deactivate", array(
array("type"=>"hidden", "name"=>"action", "value"=>"stop-ladder"),
), null, array("custom"=>"<p>This will deactivate the ladder. New games will no longer be created, and players cannot join the ladder anymore.</p>"));
}
page($html, "modladder", "Ladder configuration", "<a href=\"ladder.php?ladder=$ladderID\">$ladderNameHtml</a>", null, "Ladder configuration - $ladderNameHtml");