-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjudged_conflict.php
377 lines (322 loc) · 11.6 KB
/
judged_conflict.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
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
<?php
//===============
// EggsChange
// judged_conflict.php
//===============
session_start();
require_once(__DIR__ . "/global.php");
HtmlHeader("conflicts");
if (empty($_SESSION['IsLogged']) || $_SESSION['IsLogged'] != "online")
{
echo "<a>you have to be logged in.</a></br>";
echo "<form><input type=\"button\" value=\"Login\" onclick=\"window.location.href='login.php'\"/></form>";
fok();
}
//Check valid account state
CheckAccountState($_SESSION['Username']);
//Check for judging permission
$rep_lvl = GetReputationLevel($_SESSION['Username']);
if ($rep_lvl < NEEDED_REP_LVL_FOR_JUDGING)
{
echo "<a>You need atleast reputation level " . NEEDED_REP_LVL_FOR_JUDGING . " to do this.</br> Your are level $rep_lvl</a></br>";
echo "<form><input type=\"button\" value=\"Back\" onclick=\"window.location.href='index.php'\"/></form>";
fok();
}
$wish_id = 0; //sql ids start with 1 so it doesnt show shit here
if (!empty($_GET['id']) && $_GET['id'] > 0)
{
$wish_id = $_GET['id'];
}
else
{
echo "<a>Invalid wish id.</a></br>";
echo "<form><input type=\"button\" value=\"Back\" onclick=\"window.location.href='index.php'\"/></form>";
fok();
}
$right = "fullfiller";
if (!empty($_GET['right']))
{
$right = $_GET['right'];
if ($right != "fullfiller" && $right != "wisher")
{
echo "<a>Error: invalid right value</a></br>";
echo "<form><input type=\"button\" value=\"Back\" onclick=\"window.location.href='index.php'\"/></form>";
fok();
}
}
else
{
echo "<a>Error: you have to choose who is right.</a></br>";
echo "<form><input type=\"button\" value=\"Back\" onclick=\"window.location.href='index.php'\"/></form>";
fok();
}
function PointsRewardVoters($looser_username, $winner_amount, $id, $wish_name)
{
$looser_points = CountPoints($looser_username);
//echo "Loosers points ($looser_points) have to be shared to ($winner_amount) vote winners</br>";
if ($looser_points > $winner_amount)
{
//echo "User has enough points to give 1 to every voter</br>";
$db = new PDO(DATABASE_PATH);
$stmt = $db->prepare('SELECT * FROM Conflicts WHERE wish_ID = ?');
$stmt->execute(array($id));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($rows)
{
foreach ($rows as $row)
{
$voter_username = $row['judger'];
//echo "Giving 1 point to '$voter_username' reason '$reason'</br>";
$error_send = SendPoints($voter_username, $looser_username, 1, "Conflict judge '" . $wish_name . "'");
if ($error_send != 0)
echo "error: $error_send</br>";
}
}
}
}
function VoterReputation($id, $winner, $looser_username, $wish_name)
{
$db = new PDO(DATABASE_PATH);
$stmt = $db->prepare('SELECT * FROM Conflicts WHERE wish_ID = ?');
$stmt->execute(array($id));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$winner_amount = 0;
if ($rows)
{
foreach ($rows as $row)
{
$voter_name = $row['judger'];
if ($row['IsRight'] == $winner)
{
//increment winner voter reputation
$db = new PDO(DATABASE_PATH);
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
$stmt = $db->prepare('UPDATE Accounts SET REPp = REPp + 1 WHERE Username = ?');
$stmt->execute(array($voter_name));
$db = NULL;
$winner_amount++;
}
else
{
//decrement looser voter reputation
$db = new PDO(DATABASE_PATH);
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
$stmt = $db->prepare('UPDATE Accounts SET REPm = REPm + 1 WHERE Username = ?');
$stmt->execute(array($voter_name));
$db = NULL;
}
}
PointsRewardVoters($looser_username, $winner_amount, $id, $wish_name);
$del_reason = "[" . $looser_username . "] lost conflict";
DeleteWish($id, $del_reason);
}
else
{
die("Error updating voter reputation.</br>");
}
}
function RewardWinnerPunishLooser($id, $winner)
{
$db = new PDO(DATABASE_PATH);
$stmt = $db->prepare('SELECT * FROM Wishes WHERE ID = ?');
$stmt->execute(array($id));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$winner_name = "none";
$looser_name = "none";
if ($rows)
{
if ($winner == "wisher")
{
$winner_name = $rows[0]['wisher'];
$looser_name = $rows[0]['wish_fullfiller'];
}
else if ($winner == "fullfiller")
{
$winner_name = $rows[0]['wish_fullfiller'];
$looser_name = $rows[0]['wisher'];
}
else
die("Either wisher or fullfiller has to win.");
$reward = $rows[0]['wish_reward'];
//Give Winner the reward
/*
$db = new PDO(DATABASE_PATH);
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
$stmt = $db->prepare('UPDATE Accounts SET Points = Points + ? WHERE Username = ?');
$stmt->execute(array($reward, $winner_name));
$db = NULL;
*/
SendPoints($winner_name, "SERVER", $reward, "Won conflict on wish '" . $rows[0]['wish_name'] . "'");
//decrement loosers reputation
$db = new PDO(DATABASE_PATH);
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
$stmt = $db->prepare('UPDATE Accounts SET REPm = REPm + 1 WHERE Username = ?');
$stmt->execute(array($looser_name));
$db = NULL;
//a win for the fullfiller counts as normal accept
if ($winner == "fullfiller")
{
$db = new PDO(DATABASE_PATH);
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
$stmt = $db->prepare('UPDATE Accounts SET OtherWishesAccepted = OtherWishesAccepted + 1 WHERE Username = ?');
$stmt->execute(array($winner_name));
$db = NULL;
}
VoterReputation($id, $winner, $looser_name, $rows[0]['wish_name']);
}
else
{
die("error rewarding winner</br>");
}
}
function EvaluateWinner($id)
{
$db = new PDO(DATABASE_PATH);
$stmt = $db->prepare('SELECT * FROM Conflicts WHERE wish_ID = ?');
$stmt->execute(array($id));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$wisher_value = 0;
$fullfiller_value = 0;
if ($rows)
{
foreach ($rows as $row)
{
if ($row['IsRight'] == "wisher")
$wisher_value += $row['VoteValue'];
else if ($row['IsRight'] == "fullfiller")
$fullfiller_value += $row['VoteValue'];
else
die("<a>Error something went wrong</a></br>");
}
echo "<h3>Results</h3><a>Wisher: $wisher_value</br>Fullfiller: $fullfiller_value</br><a>";
if (is_numeric($fullfiller_value) && is_numeric($wisher_value))
{
$vote_diff = abs( $fullfiller_value - $wisher_value );
if ($vote_diff < NEEDED_VOTE_DIFF)
{
//echo "<a>Vote difference ($vote_diff) too low... collecting more votes.</a></br>";
fok();
}
if ($fullfiller_value > $wisher_value)
{
RewardWinnerPunishLooser($id, "fullfiller");
}
else
{
RewardWinnerPunishLooser($id, "wisher");
}
}
else
{
die("<a>Error calculating winner (not numeric).<a></br>");
}
}
else
{
echo "<a>Error calculating winner.<a></br>";
}
}
function CheckEnoughJudges($id)
{
$db = new PDO(DATABASE_PATH);
$stmt = $db->prepare('SELECT COUNT(*) AS TotalJudges FROM Conflicts WHERE wish_ID = ?');
$stmt->execute(array($id));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($rows)
{
$total_judges = $rows[0]['TotalJudges'];
$total_judges = (int)$total_judges;
echo "<a>Total judges on this wish: $total_judges</a></br>";
if ($total_judges > NEEDED_JUDGES)
{
echo "<a>$total_judges/" . NEEDED_JUDGES . " judges reached.</a></br>";
EvaluateWinner($id);
}
}
}
function DecisionDecided($right, $id)
{
$db = new PDO(DATABASE_PATH);
$stmt = $db->prepare('INSERT INTO Conflicts (wish_ID, judger, IsRight, VoteValue) VALUES (?, ?, ?, ?)');
$stmt->execute(array($id, $_SESSION['Username'], $right, CalculateVoteValue($_SESSION['Username'])));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($rows)
{
echo "<font color=\"red\">Something went wrong.</font>";
echo "<form><input type=\"button\" value=\"Back\" onclick=\"window.location.href='index.php'\"/></form>";
}
else
{
echo "<h1>Conflict successfully judged</h1>";
echo "<a>Thank you for improving EggsChange.</a></br>";
echo "<a>You supported the <strong>$right</strong>.</a></br>";
echo "<form><input type=\"button\" value=\"Back\" onclick=\"window.location.href='index.php'\"/></form>";
}
CheckEnoughJudges($id);
}
$db = new PDO(DATABASE_PATH);
$stmt = $db->prepare('SELECT * FROM Wishes WHERE ID = ?');
$stmt->execute(array($wish_id));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($rows)
{
$wisher = $rows[0]['wisher'];
$fullfiller = $rows[0]['wish_fullfiller'];
$title = $rows[0]['wish_name'];
$state = $rows[0]['wish_STATE'];
if ($state != 4)
{
echo "<a>This wish isn't involved in any conflict.</br></a>";
echo "<form><input type=\"button\" value=\"Back\" onclick=\"window.location.href='index.php'\"/></form>";
fok();
}
if ($_SESSION['Username'] == $wisher || $_SESSION['Username'] == $fullfiller)
{
echo "<a>You are involved in this wish.</br></a>";
echo "<form><input type=\"button\" value=\"Back\" onclick=\"window.location.href='index.php'\"/></form>";
fok();
}
//Check if judged already
$db = new PDO(DATABASE_PATH);
$stmt = $db->prepare('SELECT * FROM Conflicts WHERE wish_ID = ? AND judger = ?');
$stmt->execute(array($wish_id, $_SESSION['Username']));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($rows)
{
echo "<a>You already judged this wish</br></a>";
echo "<form><input type=\"button\" value=\"Back\" onclick=\"window.location.href='index.php'\"/></form>";
fok();
}
if (!empty($_GET['sure']) && $_GET['sure'] == "yes")
{
DecisionDecided($right, $wish_id);
fok();
}
echo "<h1>Conflict '$title'</h1>";
echo "<a>Are you sure you did everything correct?</a></br>";
if ($right == "wisher")
{
echo "<a>Your said the <strong>wisher</strong> is right.</a></br>";
}
else if ($right == "fullfiller")
{
echo "<a>Your said the <strong>fullfiller</strong> is right.</a></br>";
}
else
{
echo "<a>Erronus values.</a></br>";
echo "<form><input type=\"button\" value=\"Back\" onclick=\"window.location.href='index.php'\"/></form>";
fok();
}
echo "<a></br>Make sure to doublecheck and only judge after good research.</br>If you choose wrong this will harm your reputation.</br>If you are right this will increase your reputation and reward you with points.</a></br>";
echo "<form><input type=\"button\" value=\"Yes i am sure\" onclick=\"window.location.href='judged_conflict.php?id=$wish_id&right=$right&sure=yes'\"/></form>";
echo "<form><input type=\"button\" value=\"Back\" onclick=\"window.location.href='index.php'\"/></form>";
}
else
{
echo "<a>This wish doesn't exist.</a>";
echo "<form><input type=\"button\" value=\"Back\" onclick=\"window.location.href='index.php'\"/></form>";
fok();
}
fok();
?>