· 10 years ago · Sep 05, 2016, 11:54 AM
1
2<?php
3//
4// Trigger Moved to index.php
5//if (false !== ($who = Common::getGet('vote_for'))) {
6// noesc_voteup($who);//}
7//
8/**
9 * Get the database link
10 * @return GDO_Database */
11function noesc_db()
12{
13 static $noescdb = true;
14 if ($noescdb === true) {
15 $noescdb = gdo_db_instance('localhost', NO_ESCAPE_USER, NO_ESCAPE_PW, NO_ESCAPE_DB);
16 $noescdb->setLogging(false);
17 $noescdb->setEMailOnError(false);
18 } return $noescdb;
19}
20
21/**
22 * Create table (called by install-script) * The table layout is crappy, there is only 1 row in the table Oo.
23 * @return boolean
24 */
25function noesc_createTable()
26{ $db = noesc_db();
27 $query =
28 "CREATE TABLE IF NOT EXISTS noescvotes ( ".
29 "id INT(11) UNSIGNED PRIMARY KEY, ". # I could have one row per candidate, but currently there is only one global row(id:1). I know it`s a bit unrealistic, but at least it is safe, isn`t it?
30 "bill INT(11) UNSIGNED NOT NULL DEFAULT 0, ". # bill column "barack INT(11) UNSIGNED NOT NULL DEFAULT 0, ". # barack column
31 "george INT(11) UNSIGNED NOT NULL DEFAULT 0 )"; # george columb
32
33 if (false === $db->queryWrite($query)) {
34 return false; }
35 return noesc_resetVotes();
36}
37
38/** * Reset the votes.
39 * @return void
40 */
41function noesc_resetVotes()
42{ noesc_db()->queryWrite("REPLACE INTO noescvotes VALUES (1, 0, 0, 0)");
43 echo GWF_HTML::message('No Escape', 'All votes have been reset', false);
44}
45
46/** * Count a vote.
47 * Reset votes when we hit 100 or 111.
48 * TODO: Implement multi language
49 * @param string $who
50 * @return void */
51function noesc_voteup($who)
52{
53 if ( (stripos($who, 'id') !== false) || (strpos($who, '/') !== false) ) {
54 echo GWF_HTML::error('No Escape', 'Please do not mess with the id. It would break the challenge for others', false); return;
55 }
56
57
58 $db = noesc_db(); $who = mysql_real_escape_string($who);
59 $query = "UPDATE noescvotes SET `$who`=`$who`+1 WHERE id=1";
60
61 if (false !== $db->queryWrite($query)) {
62 echo GWF_HTML::message('No Escape', 'Vote counted for '.GWF_HTML::display($who), false);
63 }
64 noesc_stop100();
65}
66
67/** * Get all votes.
68 * @return array
69 */
70function noesc_getVotes()
71{ return noesc_db()->queryFirst("SELECT * FROM noescvotes WHERE id=1");
72}
73
74/**
75 * Reset when we hit 100. Or call challenge solved on 111. * @return void
76 */
77function noesc_stop100()
78{
79 $votes = noesc_getVotes(); foreach ($votes as $who => $count)
80 {
81 if ($count == 111) {
82 noesc_solved();
83 noesc_resetVotes(); break;
84 }
85
86 if ($count >= 100) {
87
88 noesc_resetVotes(); break;
89 }
90 }
91}
92 /**
93 * Display fancy votes table.
94 * New: it is multi language now.
95 * @return unknown_type
96 */function noesc_displayVotes(WC_Challenge $chall)
97{
98 $votes = noesc_getVotes();
99 echo '<table>';
100 echo sprintf('<tr><th>%s</th><th>%s</th><th>%s!</th></tr>', $chall->lang('th_name'), $chall->lang('th_count'), $chall->lang('th_vote')); $maxwho = '';
101 $max = 0;
102 $maxcount = 0;
103 // Print Candidate rows
104 foreach ($votes as $who => $count) {
105 if ($who !== 'id') // Skip ID
106 {
107 $count = (int) $count;
108 if ($count > $max) { $max = $count;
109 $maxwho = $who;
110 $maxcount = 1;
111 }
112 elseif ($count === $max) { $maxcount++;
113 }
114 $button = GWF_Button::generic($chall->lang('btn_vote', array($who)), "index.php?vote_for=$who");
115 echo sprintf('<tr><td>%s</td><td class="gwf_num">%s</td><td>%s</td></tr>', $who, $count, $button);
116
117 } }
118 echo '</table>';
119
120 // Print best candidate.
121 if ($maxcount === 1) { echo GWF_Box::box($chall->lang('info_best', array(htmlspecialchars($maxwho))));
122 }
123}
124
125/** * Try to get here :)
126 */
127function noesc_solved()
128{
129 if (false === ($chall = WC_Challenge::getByTitle('No Escape'))) { $chall = WC_Challenge::dummyChallenge('No Escape', 2, '/challenge/no_escape/index.php', false);
130 }
131 $chall->onChallengeSolved(GWF_Session::getUserID());
132}
133 ?>