· 7 years ago · Jan 24, 2019, 11:00 PM
1<?php
2
3/* ----------------------------------------------------------------------------------
4 *
5 * Xaseco plugin to serve betting function. all bet participants are fighting for the
6 * stake made by all of them. so it's all about beeing in front of others again^^.
7 * there are several manialink buttons to serve the chatcommands.
8 * this plugin is tested only in TA mode, but should also work in Rounds & Stunt.
9 *
10 * -----------------------------------------------------------------------------------
11 *
12 * Chatcommands:
13 * /bet "n" - to bet any amount of coppers above the "minbet" limit (n = coppers)
14 * /accept - to accept the started bet with given stake
15 * /betstake - to view a list of players and theire bet stakes
16 * /betwin - to view a list of players and theire bet wins (coppers and count)
17 * /bettotalstake - to view list of all participants
18 *
19 * MasterAdmin and Admin only:
20 * /betstate ON/OFF - to enable/disable betting from next new challenge
21 *
22 * -----------------------------------------------------------------------------------
23 *
24 * Function:
25 * At new challenge server coppers are checked, if below "minservercoppers" betting
26 * will be disabled. It will also be disabled by admin command. With "betenabled" you
27 * decide if betting is enabled/disabled with xaseco startup.
28 *
29 * If nobody started bet during "timelimitbet" seconds, betting will be not allowed for
30 * running round.
31 * If bet started but nobody accepted in "timelimitbet" seconds, bet starter get back
32 * his/her stake. Nadeo tax will be deducted.
33 * If bet started and accepted by players but nobody won, stake can be refund or not,
34 * depending on "paybacknowin" option. Nadeo tax will be deducted before.
35 * Bet participants must have finished the track to be able to win.
36 * There are two options ("winneronly") for winning conditions. one is Winner only
37 * can win the bet, second is, bet winner need to be in front of other bet participants.
38 * If there is a bet winner he/she will get the stake, in this case nadeo tax is splitted.
39 * half is payed by server, half is payed by winner.
40 * This system will avoid the server losing too much coppers.
41 * The ingame message with coppers and winmessage could take a while, the time is not
42 * influenced by the plugin.
43 *
44 * There are 4 different manialink windows:
45 * - bet panel, serving the start panel with 5 buttons to bet different amounts.
46 * amount can be configured vie betting_config.xml.
47 * - accept panel, serving the accept button and display the stake.
48 * - win panel, giving only a message of who win and how much, cause chat message only
49 * is not enough at end of race.
50 * - state panel, displays the total stake, click it to see a list of all bet participants.
51 *
52 * all main positions can be adjusted via the config file.
53 * if you want to edit the whole apperance, check out the manialink section somewhere below.
54 * Same for colors and text of any chatmessage, check out the chat command section at bottom.
55 *
56 * The plugin will create a new table "betting" in your database, this is necessary to
57 * save the betting data like player, nickname, stake, win, wincount.
58 *
59 * No need to modify any other existing file.
60 *
61 * ----------------------------------------------------------------------------------
62 *
63 * Installation:
64 * Copy plugin.nouse.betting.php into plugins folder.
65 * Copy nouse_betting_config.xml into xaseco root
66 * Add "<plugin>plugin.nouse.betting.php</plugin> into plugins.xml
67 * Configure to ur needs.
68 * Enjoy
69 *
70 * ----------------------------------------------------------------------------------
71 *
72 * Author: ML aka RookieNouse aka nouseforname @ http://www.tm-forum.com
73 * Home: http://nouseforname
74 * Date: 11.05.2012
75 * Version: 1.8.1
76 * Dependencies: none
77 *
78 * ----------------------------------------------------------------------------------
79 *
80 * LICENSE: This program is free software; you can redistribute it and/or
81 * modify it under the terms of the GNU General Public License as published
82 * by the Free Software Foundation; either version 2 of the License, or
83 * (at your option) any later version.
84 *
85 * This program is distributed in the hope that it will be useful,
86 * but WITHOUT ANY WARRANTY; without even the implied warranty of
87 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
88 * GNU General Public License for more details.
89 *
90 * You should have received a copy of the GNU General Public License
91 * along with this program; if not, write to the Free Software
92 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
93 *
94 * ----------------------------------------------------------------------------------
95 */
96
97class betting_coppers {
98 var $valid_players = array();
99 var $acceptors = array();
100 var $ranks = array();
101 var $showaccept = array();
102 var $bets = array();
103 private $Aseco, $index, $servername;
104 private $minbet, $maxbet, $enabled, $minservercoppers, $state;
105 private $timelimit_bet, $timelimit_accept;
106 private $bet_active, $bet_starter, $bet_starter_nick, $bet_start, $bet_amount;
107 private $bet1, $bet2, $bet3, $bet4, $bet5;
108 private $timebet, $countsec, $player, $clicktime, $clicked;
109 private $nickname, $coppers, $paybackstarter, $paybackbetstarter;
110 private $checkbet, $checkaccept, $betbill;
111 private $paybacknowin, $winneronly;
112 private $bet_starter_factor, $bet_nowin_factor, $bet_winpayment_factor;
113 private $betpanelmainpos, $acceptpanelmainpos, $bwinpanelmainpos;
114
115 // debug
116 function betting_coppers ($debug){
117 $this->debug = $debug;
118 }
119
120 function upToDate($aseco) {
121 $aseco->plugin_versions[] = array(
122 'plugin' => 'plugin.nouse.betting.php',
123 'author' => 'Nouseforname',
124 'version' => '1.8.1'
125 );
126 }
127
128 // startup
129 function startUp($aseco){
130 $this->Aseco = $aseco;
131 $this->loadSettings();
132 $this->bet_mysql_create();
133 }
134
135 // load xml configs
136 function loadSettings(){
137 $file = file_get_contents('nouse_betting_config.xml');
138 $xml = simplexml_load_string($file);
139
140 $this->enabled = intval($xml->betenabled);
141
142 $this->minservercoppers = intval($xml->minservercoppers);
143
144 $this->minbet = intval($xml->minbet);
145 $this->maxbet = intval($xml->maxbet);
146
147 $this->timelimit_bet = intval($xml->timelimit_bet);
148 $this->timelimit_accept = intval($xml->timelimit_accept);
149
150 $this->bet1 = intval($xml->bet1);
151 $this->bet2 = intval($xml->bet2);
152 $this->bet3 = intval($xml->bet3);
153 $this->bet4 = intval($xml->bet4);
154 $this->bet5 = intval($xml->bet5);
155
156 $this->winneronly = intval($xml->winneronly);
157 $this->paybacknowin = intval($xml->paybacknowin);
158 $this->paybackbetstarter = intval($xml->paybackstarter);
159
160 $this->bet_starter_factor = intval($xml->betstarterpayback);
161 $this->bet_nowin_factor = intval($xml->betnowinpayback);
162 $this->bet_winpayment_factor = intval($xml->betwinpayment);
163
164 $this->betpanelmainpos = strval($xml->bet_panel->mainpos);
165 $this->acceptpanelmainpos = strval($xml->accept_panel->mainpos);
166 $this->winpanelmainpos = strval($xml->win_panel->mainpos);
167 $this->statepanelmainpos = strval($xml->state_panel->mainpos);
168
169 $this->debug = intval($xml->debug);
170 }
171
172 // create database at startup
173 function bet_mysql_create() {
174 $query = "CREATE TABLE IF NOT EXISTS betting (
175 ID mediumint(9) NOT NULL auto_increment,
176 login varchar(100),
177 nickname varchar(100),
178 stake mediumint(9),
179 wins mediumint(9),
180 countwins mediumint(9),
181 PRIMARY KEY (ID),
182 UNIQUE (login)
183 ) TYPE=MyISAM";
184 mysql_query($query);
185 }
186
187 // insert data into database
188 function bet_mysql_insert($login, $nickname, $stake, $win, $countwins) {
189 $query = 'INSERT INTO betting (`login`, `nickname`, `stake`, `wins`, `countwins`)
190 VALUES (\''.$login.'\', \''.$nickname.'\', \''.$stake.'\', \''.$win.'\', \''.$countwins.'\')
191 ON DUPLICATE KEY UPDATE
192 stake=stake + '.$stake.',
193 wins=wins + '.$win.',
194 countwins=countwins + '.$countwins.'
195 ';
196 mysql_query($query);
197 }
198
199 function reset_bet_counter() {
200 $this->countsec = -3;
201 if (!$this->index) {
202 $this->bet_ml_on();
203 $this->index = 1;
204 }
205 }
206
207 function reset_bet_counter2() {
208 $this->countsec = -90;
209 }
210 // reset all states at begin round
211 function reset_bet() {
212 $this->bet_active = 0;
213 $this->bet_amount = 0;
214 $this->bet_start = 0;
215 $this->paybackstarter = 0;
216 $this->checkaccept = 0;
217 $this->bet_starter = NULL;
218 $this->bet_starter_nick = NULL;
219 $this->bets = array();
220 $this->acceptors = array();
221 $this->checkbet = 1;
222 $this->index = 0;
223 $this->ranks = array();
224 $this->showaccept = array();
225 $this->bet_winner_ml_off();
226 $this->bet_nowinner_ml_off();
227 $this->accept_ml_off_all();
228 $this->bet_state_ml_off();
229 if (!$this->enabled) {
230 $message = '$z$s$fffBetting is $f00disabled $ffffor this round!';
231 $this->Aseco->client->addCall('ChatSendServerMessage', array($this->Aseco->formatColors($message)));
232 } elseif (!$this->state) {
233 $message = '$z$s$fffDue to low coppers betting is $f00disabled$fff!';
234 $this->Aseco->client->addCall('ChatSendServerMessage', array($this->Aseco->formatColors($message)));
235 }
236 }
237
238 // count seconds and check if bet/accept still available
239 function on_everysecond() {
240 $this->countsec++;
241
242 if ($this->countsec == $this->timelimit_bet && $this->checkbet && !$this->bet_active && $this->enabled && $this->state) {
243 $this->checkbet = 0;
244 $this->bet_ml_off();
245 // changed by L3cKy
246 /*
247 $message = '$ff0$cf0$iTimelimit exceeded. Nobody started bet!'; // chat message if nobody started bet
248 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), implode(',', $this->valid_players)));
249 */
250 }
251 elseif ($this->countsec == $this->timelimit_accept && !$this->acceptors && $this->bet_active && !$this->checkaccept) {
252 $this->checkaccept = 1;
253 if ($this->paybackbetstarter) {
254 $this->payback_bet_starter();
255 }
256 foreach ($this->valid_players as $login) {
257 $this->accept_ml_off($login);
258 }
259 $message = '$z$s$fffNobody accepted bet from '. $this->bet_starter_nick.' $z$s$fffwith $f00'. $this->bet_amount.' $fffCoppers!'; // chat message if nobody accepted
260 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), implode(',', $this->valid_players)));
261 }
262 elseif ($this->countsec == $this->timelimit_accept && $this->acceptors && $this->bet_active) {
263 foreach ($this->valid_players as $login) {
264 $this->accept_ml_off($login);
265 }
266 }
267
268 // reset login of double click block
269 if (time() >= $this->clicktime + 5) $this->clicked = NULL;
270
271 }
272
273 // check if player login is TMU account and put into array
274 function validate_players() {
275 $this->valid_players = array();
276 foreach ($this->Aseco->server->players->player_list as $player) {
277 if ($player->rights) {
278 $this->valid_players[] = $player->login;
279 }
280 }
281 }
282
283 function getServerCoppers() {
284 $this->Aseco->client->query('GetServerCoppers');
285 $coppers = $this->Aseco->client->getResponse();
286 $this->Aseco->client->query('GetServerName');
287 $this->servername = $this->Aseco->client->getResponse();
288 if ($coppers <= $this->minservercoppers) {
289 $this->state = 0;
290 } else {
291 $this->state = 1;
292 }
293 }
294
295 // refund coppers to bet starter if nobody accepted (nadeo tax deducted to avoid server lose coppers
296 function payback_bet_starter() {
297 $coppers = $this->bet_amount;
298 if ($this->bet_starter_factor) {
299 for ($i = 1; $i <= $this->bet_starter_factor; $i++) {
300 $coppers = $coppers - 2 - $coppers * 0.05;
301 }
302 $coppers = floor($coppers);
303 }
304 $message = 'Refund '.$this->bet_amount.' coppers, due to nobody accepted your bet at '.$this->servername.'$g$z! Nadeo tax was deducted.'; // text in mail for refund coppers to bet starter
305 $this->Aseco->client->addCall('Pay', array($this->bet_starter, (int)$coppers, $this->Aseco->formatColors($message)));
306 $this->paybackstarter = 1;
307 }
308
309 // refund coppers if nobody won
310 function bet_paybacknowin() {
311 $coppers = $this->bet_start;
312 if ($this->bet_nowin_factor) {
313 for ($i = 1; $i <= $this->bet_nowin_factor; $i++) {
314 $coppers = $coppers - 2 - $coppers * 0.05;
315 }
316 $coppers = floor($coppers);
317 }
318 $message = 'Refund '.$this->bet_start.' coppers, due to nobody won the stake at '.$this->servername.'$g$z! Nadeo tax was deducted.'; // text in mail for refund coppers to all participants
319 foreach ($this->acceptors as $nick => $login) {
320 $this->Aseco->client->addCall('Pay', array($login, (int)$coppers, $this->Aseco->formatColors($message)));
321 }
322 }
323
324 // pay winner, decuct one time tax
325 function bet_paywin($winner) {
326 $coppers = $this->bet_amount;
327 if ($this->bet_winpayment_factor) {
328 for ($i = 1; $i <= $this->bet_winpayment_factor; $i++) {
329 $coppers = $coppers - 2 - $coppers * 0.05;
330 }
331 $coppers = floor($coppers);
332 }
333 $message = 'You won the stake of '.$this->bet_amount.' coppers at '.$this->servername.'$g$z! Nadeo tax was deducted.'; // text in mail to winner
334 $this->Aseco->client->addCall('Pay', array($winner[0][0], (int)$coppers, $this->Aseco->formatColors($message)));
335 }
336
337 // get winner of bet and pay win or refund all coppers back if no win
338 function get_winner() {
339 $this->bet_ml_off();
340 $this->accept_ml_off_all();
341 $this->bet_state_ml_off();
342 // reset counter for case of restart and skip
343 $this->countsec = -30;
344 // do action only if somebody accepted bet
345 if ($this->acceptors) {
346 // get ranking at rounds end
347 $this->Aseco->client->query('GetCurrentRanking', 100, 0);
348 $ranking = $this->Aseco->client->getResponse();
349 // put all players in array
350 foreach ($ranking as $key => $var) {
351 $this->ranks[$key] = array($var[Login], $var[BestTime], $var[NickName], $var[Rank], $var[Score]);
352 }
353 // put bet starter into same array as acceptors
354 $this->acceptors[betstarter] = $this->bet_starter;
355 $this->bet_starter = NULL;
356 $winner = array();
357 // check for option winner only and if a participant is rank 1
358 if ($this->winneronly) {
359 if ((in_array($this->ranks[0][0], $this->acceptors)) && ($this->ranks[0][1] > 0 || $this->ranks[0][4] > 0)) {
360 $winner[] = array($this->ranks[0][0], $this->ranks[0][2]);
361 }
362 } else {
363 // check if bet participants finished challenge and put them in array
364 foreach ($this->ranks as $result) {
365 if (($result[1] > 0 || $result[4] > 0) && (in_array($result[0], $this->acceptors))) {
366 $winner[] = array($result[0], $result[2]);
367 }
368 }
369 }
370
371 // check who is winner, if no winner pay back coppers or not
372 if ($winner) {
373 $this->bet_winner_ml_on($winner);
374 $this->bet_paywin($winner);
375 $message = '$z$s'. $winner[0][1] .' $z$s$fffwon the stake with total amount of $f00'.$this->bet_amount.' $fffcoppers!'; // chat message winner
376 $this->Aseco->client->addCall('ChatSendServerMessage', array($this->Aseco->formatColors($message)));
377 $this->bet_mysql_insert($winner[0][0], $winner[0][1], $coppers=0, $this->bet_amount, $countwins=1);
378 } else {
379 $this->bet_nowinner_ml_on();
380 $message = '$z$s$fffNobody won the last stake!'; //chat message no winner
381 $this->Aseco->client->addCall('ChatSendServerMessage', array($this->Aseco->formatColors($message)));
382 if ($this->paybacknowin) {
383 $this->bet_paybacknowin();
384 }
385 }
386 // payback if no acceptors and next/skip was executed
387 } elseif ($this->bet_starter && !$this->paybackstarter) {
388 $message = '$z$s$fffNobody accepted bet from '. $this->bet_starter_nick.' $z$s$fffwith $f00'. $this->bet_amount.' $fffcoppers!'; // chat message in case of skip
389 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), implode(',', $this->valid_players)));
390 if ($this->paybackbetstarter) {
391 $this->payback_bet_starter();
392 }
393 }
394 }
395
396 // get bill and set bet state
397 function bet_bill_upd($bill) {
398 $billid = $bill[0];
399
400 if (array_key_exists($billid, $this->bets)) {
401
402 // get bill info
403 $login = $this->bets[$billid][0];
404 $nickname = $this->bets[$billid][1];
405 $coppers = $this->bets[$billid][2];
406 $checkbet = $this->bets[$billid][3];
407 // check bill state
408 switch($bill[1]) {
409 case 4: // Payed (Paid)
410 if ($coppers > 0) {
411 if (!$this->bet_active and $login != $this->bet_starter and ($checkbet === true)) {
412 $this->bet_active = 1;
413 $this->bet_starter = $login;
414 $this->bet_starter_nick = $nickname;
415 $this->bet_start = $coppers;
416 $this->bet_amount = $coppers;
417 $this->bet_ml_off();
418 $this->accept_ml_on();
419 $message = '$z$s'. $nickname.'$z$s$fff set $f00'.$coppers.' $fffcoppers for next bet!'; // chat message if somebody started bet
420 $this->Aseco->client->addCall('ChatSendServerMessage', array($this->Aseco->formatColors($message)));
421 $this->bet_mysql_insert($login, $nickname, $coppers, $win=0, $countwins=0);
422 //$this->bet_state_ml_on();
423 }
424 elseif (!in_array($login, $this->acceptors) and ($login != $this->bet_starter) and ($checkbet === false)) {
425 $this->acceptors[$nickname] = $login;
426 $this->accept_ml_off($login);
427 $count = count($this->acceptors) + 1;
428 $this->bet_amount = $count * $this->bet_start;
429 $message = '$z$s'.$nickname.'$z$s$fff accepted bet! Total win is $f00'.$this->bet_amount.' $fffcoppers now!'; // chat message if somebody accepted bet
430 $this->Aseco->client->addCall('ChatSendServerMessage', array($this->Aseco->formatColors($message)));
431 $this->accept_ml_off($login);
432 $this->bet_mysql_insert($login, $nickname, $coppers, $win=0, $countwins=0);
433 $this->bet_state_ml_on();
434 }
435 elseif ($this->bet_active and $checkbet === true) {
436 $message = '$z$s'.$nickname.' $z$s$fffwith login:'. $login .' tried to cheat or is just to slow to bet!'; // chat message if somebody try to cheat
437 $this->Aseco->client->addCall('ChatSendServerMessage', array($this->Aseco->formatColors($message)));
438 $this->Aseco->console('$ff0'. $nickname .'$g$z$f00$s$i with login:'. $login .' tried to cheat or is just to slow to bet!');
439 }
440 }
441 unset($this->bets[$billid]);
442 break;
443 case 5: // Refused
444 $message = '{#server}{#error}Transaction refused, no bet placed!';
445 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
446 unset($bets[$billid]);
447 break;
448 case 6: // Error
449 $message = '{#server}{#error}Transaction failed: {#highlite}$i ' . $bill[2];
450 if ($login != '')
451 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
452 else
453 $this->Aseco->client->addCall('ChatSendServerMessage', array($this->Aseco->formatColors($message)));
454 unset($this->bets[$billid]);
455 break;
456 default: // CreatingTransaction/Issued/ValidatingPay(e)ment
457 break;
458 }
459 }
460 } // bet_updated end
461
462 // excute button click as chat command
463 function bet_handle_buttons($command){
464 $playerid = $command[0];
465 $login = $command[1];
466 $action = $command[2];
467
468 // only go ahead if not same login or 5 seconds later than first click
469 if ($this->clicked != $login) {
470 // try to avoid any action for doubleclick
471 $this->clicked = $login;
472 $this->clicktime = time();
473
474 switch ($action) {
475 case 27378501:
476 $chat = array();
477 $chat[0] = $playerid;
478 $chat[1] = $login;
479 $chat[2] = '/bet '. $this->bet1 .'';
480 $chat[3] = true;
481 $this->Aseco->playerChat($chat);
482 //$this->clicked = $login;
483 break;
484 case 27378502:
485 $chat = array();
486 $chat[0] = $playerid;
487 $chat[1] = $login;
488 $chat[2] = '/bet '. $this->bet2 .'';
489 $chat[3] = true;
490 $this->Aseco->playerChat($chat);
491 //$this->clicked = $login;
492 break;
493 case 27378503:
494 $chat = array();
495 $chat[0] = $playerid;
496 $chat[1] = $login;
497 $chat[2] = '/bet '. $this->bet3 .'';
498 $chat[3] = true;
499 $this->Aseco->playerChat($chat);
500 //$this->clicked = $login;
501 break;
502 case 27378504:
503 $chat = array();
504 $chat[0] = $playerid;
505 $chat[1] = $login;
506 $chat[2] = '/bet '. $this->bet4 .'';
507 $chat[3] = true;
508 $this->Aseco->playerChat($chat);
509 //$this->clicked = $login;
510 break;
511 case 27378505:
512 $chat = array();
513 $chat[0] = $playerid;
514 $chat[1] = $login;
515 $chat[2] = '/bet '. $this->bet5 .'';
516 $chat[3] = true;
517 $this->Aseco->playerChat($chat);
518 //$this->clicked = $login;
519 break;
520 case 27378506:
521 $chat = array();
522 $chat[0] = $playerid;
523 $chat[1] = $login;
524 $chat[2] = '/accept';
525 $chat[3] = true;
526 $this->Aseco->playerChat($chat);
527 //$this->clicked = $login;
528 break;
529 case 27378507:
530 $chat = array();
531 $chat[0] = $playerid;
532 $chat[1] = $login;
533 $chat[2] = '/bettotalstake';
534 $chat[3] = true;
535 $this->Aseco->playerChat($chat);
536 //$this->clicked = $login;
537 break;
538 default:
539 break;
540 }
541 }
542 } // placebet end
543
544 /************************** MANIALINKS START *********************************/
545
546 // display manialink for betting buttons
547 function bet_ml_on() {
548 if ($this->enabled && $this->state) {
549 $xml = '<manialink id="471108157051">
550 <frame posn="'.$this->betpanelmainpos.'">
551 <format style="TextCardInfoSmall" textsize="1" />
552 <quad sizen="4.5 28 0" style="Bgs1InRace" substyle="NavButton" halign="center" valign="center"/>
553 <label scale="0.8" posn="0 12 0" sizen="4 2" halign="center" style="TextTitle2Blink" valign="center" text="$z$s$f00Bet" />
554 <label scale="0.8" posn="0 8 0" sizen="4 2" halign="center" valign="center" text="$z$s$FFF'.$this->bet1.'C" action="27378501"/>
555 <label scale="0.8" posn="0 3 0" sizen="4 2" halign="center" valign="center" text="$z$s$FFF'.$this->bet2.'C" action="27378502"/>
556 <label scale="0.8" posn="0 -2 0" sizen="4 2" halign="center" valign="center" text="$z$s$FFF'.$this->bet3.'C" action="27378503"/>
557 <label scale="0.8" posn="0 -7 0" sizen="4 2" halign="center" valign="center" text="$z$s$FFF'.$this->bet4.'C" action="27378504"/>
558 <label scale="0.8" posn="0 -12 0" sizen="4 2" halign="center" valign="center" text="$z$s$FFF'.$this->bet5.'C" action="27378505"/>
559 </frame>
560 </manialink>';
561 $this->Aseco->client->addCall('SendDisplayManialinkPageToLogin', array(implode(',', $this->valid_players), $xml, 0, false));
562 }
563 } // display_manialink
564
565 // display manialink for betting buttons off
566 function bet_ml_off() {
567 $xml = '<manialink id="471108157051">
568 </manialink>';
569 $this->Aseco->client->addCall('SendDisplayManialinkPage', array($xml, 0, false));
570 } // display_manialink
571
572 // display manialink for accept button
573 function accept_ml_on() {
574 foreach ($this->valid_players as $login) {
575 if ($login != $this->bet_starter) {
576 $this->showaccept[] = $login;
577 }
578 }
579 $xml = '<manialink id="471108157052">
580 <frame posn="'.$this->acceptpanelmainpos.'">
581 <format style="TextCardInfoSmall" textsize="1" />
582 <quad sizen="12 8.5 0" style="Bgs1InRace" substyle="NavButton" halign="center" valign="center" action="27378506"/>
583 <label posn="0 2 1" sizen="10 4" scale="0.9" halign="center" valign="center" style="TextTitle2Blink" text="$z$s$F00Accept Bet" />
584 <label posn="0 -1 1" sizen="10 4" scale="1" halign="center" valign="center" text="$z$s$fffStake $f00'.$this->bet_start.'$fffC" />
585 </frame>
586 </manialink>';
587 $this->Aseco->client->addCall('SendDisplayManialinkPageToLogin', array(implode(',', $this->showaccept), $xml, 0, false));
588 } // display_manialink
589
590 // display manialink for accept button off
591 function accept_ml_off($login) {
592 $xml = '<manialink id="471108157052">
593 </manialink>';
594 $this->Aseco->client->addCall('SendDisplayManialinkPageToLogin', array($login, $xml, 0, false));
595 } // display_manialink
596
597 // display manialink for accept button off to all
598 function accept_ml_off_all() {
599 $xml = '<manialink id="471108157052">
600 </manialink>';
601 $this->Aseco->client->addCall('SendDisplayManialinkPage', array($xml, 0, false));
602 } // display_manialink
603
604 // display manialink for betting winner
605 function bet_winner_ml_on($winner) {
606 $xml = '<manialink id="471108157053">
607 <frame posn="'.$this->winpanelmainpos.'">
608 <format style="TextCardMedium" textsize="3" />
609 <label posn="0 0 0" sizen="40 3" halign="center" valign="center" text="$s$o'.$winner[0][1].' $g$z$i$s$o$fffwon '.$this->bet_amount.' coppers!" />
610 </frame>
611 </manialink>';
612 $this->Aseco->client->addCall('SendDisplayManialinkPage', array($xml, 0, false));
613 } // display_manialink
614
615 // display manialink for betting winner off
616 function bet_winner_ml_off() {
617 $xml = '<manialink id="471108157053">
618 </manialink>';
619 $this->Aseco->client->addCall('SendDisplayManialinkPage', array($xml, 0, false));
620 } // display_manialink
621
622 // display manialink for betting if no winner
623 function bet_nowinner_ml_on() {
624 $xml = '<manialink id="471108157054">
625 <frame posn="'.$this->winpanelmainpos.'">
626 <format style="TextCardMedium" textsize="3" />
627 <label posn="0 0 0" sizen="30 5" halign="center" valign="center" text="$s$o$fffNobody won the last stake!" />
628 </frame>
629 </manialink>';
630 $this->Aseco->client->addCall('SendDisplayManialinkPage', array($xml, 0, false));
631 } // display_manialink
632
633 // display manialink for betting if no winner winner off
634 function bet_nowinner_ml_off() {
635 $xml = '<manialink id="471108157054">
636 </manialink>';
637 $this->Aseco->client->addCall('SendDisplayManialinkPage', array($xml, 0, false));
638 } // display_manialink
639
640 // display manialink state of bet
641 function bet_state_ml_on() {
642 $xml = '<manialink id="471108157055">
643 <frame posn="'.$this->statepanelmainpos.'">
644 <format style="TextCardInfoSmall" textsize="1" />
645 <label posn="-2 0 0" sizen="13 3" halign="center" valign="center" text="$s$o$fffTotal win '.$this->bet_amount.' C" action="27378507" />
646 </frame>
647 </manialink>';
648 $this->Aseco->client->addCall('SendDisplayManialinkPage', array($xml, 0, false));
649 } // display_manialink
650
651 // display manialink state of bet off
652 function bet_state_ml_off() {
653 $xml = '<manialink id="471108157055">
654 </manialink>';
655 $this->Aseco->client->addCall('SendDisplayManialinkPage', array($xml, 0, false));
656 } // display_manialink
657
658/************************** MANIALINKS END *********************************/
659
660/*********************** CHAT COMMANDS START *******************************/
661
662// proceed chat command bet and get bill id
663 function bet_command($command) {
664 $player = $command['author']; // get author
665 $login = $player->login; // get login of author
666 $nickname = $player->nickname; // get nickname
667 $coppers = $command['params']; // get parameter
668
669 $checker = 0;
670 foreach ($this->Aseco->server->players->player_list as $playercheck) {
671 if ($playercheck->rights) {
672 $checker++;
673 }
674 }
675
676 if ($this->Aseco->server->getGame() == 'TMF') {
677 // check for TMUF server
678 if ($this->Aseco->server->rights) {
679 // check for TMUF player
680 if ($player->rights) {
681 // check if betting is enabled
682 if ($this->enabled && $this->state) {
683 // check for valid amount
684 if ($coppers != '' && is_numeric($coppers)) {
685 // check for betting time limit
686 if (($this->countsec <= $this->timelimit_bet-2) && ($this->bet_active == 0)) {
687 // check for minimum donation
688 if ($coppers >= $this->minbet and $coppers <= $this->maxbet) {
689 // check for double command
690 if ($login != $this->bet_starter and !in_array($login, $this->acceptors)) {
691 // check for any other connected tmu players
692 if ($checker >= 1) {
693 // start the transaction
694 $message = ' $f80You have to pay $f00'.$coppers.' $f80coppers to set the next bet!$g$z'; // text in bill popup "start bet"
695 $this->Aseco->client->query('SendBill', $login, (int)$coppers, $this->Aseco->formatColors($message), '');
696 $billid = $this->Aseco->client->getResponse();
697 $this->bets[$billid] = array($login, $nickname, $coppers, true);
698 } else {
699 $message = formatText('{#server}{#error}No players connected who can accept your bet!!!!');
700 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
701 }
702 }
703 else {
704 $message = formatText('{#server}{#error}You don\'t need to double click or double execute the chat command!!!!');
705 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
706 }
707 } else {
708 $message = formatText('{#server}{#error}You\'ll have to set {#highlite}minimum '.$this->minbet.' {#error}coppers and not more than {#highlite}'. $this->maxbet .' {#error}to proceed the bet!');
709 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
710 }
711 } else {
712 $message = formatText('{#server}{#error}Time limit for bet expired or bet already placed, wait till next round!');
713 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
714 }
715 } else {
716 $message = formatText('{#server}{#error}No amount of coppers defined. Please use {#interact}"/bet [coppers]"');
717 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
718 }
719 } else {
720 $message = formatText('{#server}{#error}Betting is disabled!');
721 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
722 }
723 } else {
724 $message = formatText($this->Aseco->getChatMessage('UNITED_ONLY'), 'account');
725 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
726 }
727 } else {
728 $message = formatText($this->Aseco->getChatMessage('UNITED_ONLY'), 'server');
729 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
730 }
731 } else {
732 $message = $this->Aseco->getChatMessage('FOREVER_ONLY');
733 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
734 }
735 } //chat commmand bet coppers end
736
737 // proceed chat command accept bet and get bill id
738 function accept_command($command) {
739 $player = $command['author']; // get author
740 $login = $player->login; // get login of author
741 $nickname = $player->nickname; // get nickname
742 $coppers = $command['params']; // get parameter
743
744 if ($this->Aseco->server->getGame() == 'TMF') {
745 // check for TMUF server
746 if ($this->Aseco->server->rights) {
747 // check for TMUF player
748 if ($player->rights) {
749 // check if acceptor is not bet starter
750 if ($login != $this->bet_starter) {
751 // check if bet is set
752 if ($this->bet_active) {
753 // check if player already accepted
754 if (!$this->acceptors || !in_array($login, $this->acceptors)) {
755 // check for betting time limit
756 if (($this->countsec <= $this->timelimit_accept)) {
757 // start the transaction
758 $message = ' $f80You have to pay $f00'.$this->bet_start.' $f80coppers to accept the bet!$g$z'; // text in bill popup "accept bet"
759 $this->Aseco->client->query('SendBill', $login, (int)$this->bet_start, $this->Aseco->formatColors($message), '');
760 $billid = $this->Aseco->client->getResponse();
761 $this->bets[$billid] = array($login, $nickname, $this->bet_start, false);
762 } else {
763 $message = formatText('{#server}{#error}Time limit to accept expired, wait till next round!');
764 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
765 }
766 } else {
767 $message = formatText('{#server}{#error}You accepted already!!!');
768 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
769 }
770 } else {
771 $message = formatText('{#server}{#error}No bet started yet!!!');
772 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
773 }
774 } else {
775 $message = formatText('{#server}{#error}You\'ve just started the bet by yourself!');
776 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
777 }
778 } else {
779 $message = formatText($this->Aseco->getChatMessage('UNITED_ONLY'), 'account');
780 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
781 }
782 } else {
783 $message = formatText($this->Aseco->getChatMessage('UNITED_ONLY'), 'server');
784 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
785 }
786 } else {
787 $message = $this->Aseco->getChatMessage('FOREVER_ONLY');
788 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
789 }
790 } //chat commmand accept bet end
791
792 // proceed chat command betcount and display list
793 function betstake_command($command) {
794
795 $player = $command['author'];
796 $login = $player->login;
797
798 if ($this->Aseco->server->getGame() == 'TMF') {
799 // check for TMUF server
800 if ($this->Aseco->server->rights) {
801 $bgn = '{#black}'; // nickname begin
802 $query = 'SELECT nickname, stake FROM betting WHERE stake != 0 ORDER BY stake DESC';
803 $res = mysql_query($query);
804
805 if (mysql_num_rows($res) > 0) {
806 $stakes = array();
807 $player->msgs = array();
808 $total = 0;
809 $i = 1;
810 while($row = mysql_fetch_row($res)) {
811 $nick = $row[0];
812 if (!$this->Aseco->settings['lists_colornicks']) $nick = stripColors($nick);
813 $stakes[] = array(str_pad($i, 2, '0', STR_PAD_LEFT) . '.', $bgn . $nick, $row[1].' C');
814 $i++;
815 $n++;
816 $total = $total + $row[1];
817 }
818 $head = '{#welcome}List Total Player Stakes. Over all stake: $i'.$total.' coppers';
819 $player->msgs[0] = array(1, $head, array(1.0, 0.2, 0.6, 0.2), array('Icons128x128_1', 'Coppers'));
820 $n = 0;
821 $send = array();
822 foreach ($stakes as $stake) {
823 if ($n == 14) {
824 $player->msgs[] = $send;
825 $n = 0;
826 $send = array();
827 }
828 $send[] = $stake;
829 $n++;
830 }
831 $player->msgs[] = $send;
832 display_manialink_multi($player);
833
834 } else {
835 $message = '{#server}{#error}No player(s) found!';
836 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
837 }
838 mysql_free_result($res);
839 } else {
840 $message = formatText($this->Aseco->getChatMessage('UNITED_ONLY'), 'server');
841 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
842 }
843 } else {
844 $message = $this->Aseco->getChatMessage('FOREVER_ONLY');
845 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
846 }
847 }
848
849 // proceed chat command betcount and display list
850 function betwin_command($command) {
851 $player = $command['author'];
852 $login = $player->login;
853
854 if ($this->Aseco->server->getGame() == 'TMF') {
855 // check for TMUF server
856 if ($this->Aseco->server->rights) {
857 $bgn = '{#black}'; // nickname begin
858 $query = 'SELECT nickname, wins, countwins FROM betting WHERE wins != 0 ORDER BY wins DESC';
859 $res = mysql_query($query);
860
861 if (mysql_num_rows($res) > 0) {
862 $stakes = array();
863 $player->msgs = array();
864 $total = 0;
865 $i = 1;
866 while($row = mysql_fetch_row($res)) {
867 $nick = $row[0];
868 if (!$this->Aseco->settings['lists_colornicks']) $nick = stripColors($nick);
869 $stakes[] = array(str_pad($i, 2, '0', STR_PAD_LEFT) . '.', $bgn . $nick, $row[1].' C', $row[2].' Wins');
870 $i++;
871 $n++;
872 $total = $total + $row[1];
873 }
874 $head = '{#welcome}List Total Player Wins. Over all wins: $i'.$total.' coppers';
875 $player->msgs[0] = array(1, $head, array(1.1, 0.15, 0.55, 0.2, 0.2), array('Icons128x128_1', 'Coppers'));
876 $n = 0;
877 $send = array();
878 foreach ($stakes as $stake) {
879 if ($n == 14) {
880 $player->msgs[] = $send;
881 $n = 0;
882 $send = array();
883 }
884 $send[] = $stake;
885 $n++;
886 }
887 $player->msgs[] = $send;
888 display_manialink_multi($player);
889
890 } else {
891 $message = '{#server}{#error}No player(s) found!';
892 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
893 }
894 mysql_free_result($res);
895 } else {
896 $message = formatText($this->Aseco->getChatMessage('UNITED_ONLY'), 'server');
897 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
898 }
899 } else {
900 $message = $this->Aseco->getChatMessage('FOREVER_ONLY');
901 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
902 }
903 }
904
905 // list all participants of actual bet
906 function totalstake_command($command) {
907 $player = $command['author'];
908 $login = $player->login;
909
910 if ($this->Aseco->server->getGame() == 'TMF') {
911 // check for TMUF server
912 if ($this->Aseco->server->rights) {
913 // check for acitve bet
914 if ($this->acceptors) {
915 $bgn = '{#black}'; // nickname begin
916 $player->msgs = array();
917 $head = '{#welcome}List Bet Participants. Total Stake: $i'.$this->bet_amount.' coppers';
918 $player->msgs[0] = array(1, $head, array(1.0, 0.2, 0.8), array('Icons128x128_1', 'Coppers'));
919 $party = array();
920 $i = 2;
921 $nick = $this->bet_starter_nick;
922 if (!$this->Aseco->settings['lists_colornicks']) $nick = stripColors($nick);
923 $party[] = array(str_pad(1, 2, '0', STR_PAD_LEFT) . '.', $bgn . $nick );
924
925 foreach ($this->acceptors as $nick => $login) {
926 if (!$this->Aseco->settings['lists_colornicks']) $nick = stripColors($nick);
927 $party[] = array(str_pad($i, 2, '0', STR_PAD_LEFT) . '.', $bgn . $nick );
928 if ($i >= 13) {
929 $player->msgs[] = $party;
930 $party = array();
931 }
932 $i++;
933 }
934 $player->msgs[] = $party;
935 display_manialink_multi($player);
936
937 } else {
938 $message = '{#server}{#error}No active bet found!';
939 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
940 }
941 } else {
942 $message = formatText($this->Aseco->getChatMessage('UNITED_ONLY'), 'server');
943 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array($this->Aseco->formatColors($message), $login));
944 }
945 } else {
946 $message = $this->Aseco->getChatMessage('FOREVER_ONLY');
947 $this->Aseco->client->addCall('ChatSendServerMessageToLogin', array( $this->Aseco->formatColors($message), $login));
948 }
949 }
950
951 // admin chat command enable bettting
952 function enable_command($command) {
953 $admin = $command['author'];
954 $login = $admin->login;
955 $nick = $admin->nickname;
956 $com = $command['params'];
957 $com = strtolower($com);
958 // check if chat command was allowed for a masteradmin/admin/operator
959 if ($this->Aseco->isMasterAdmin($admin) || $this->Aseco->isAdmin($admin)) {
960 // check for unlocked password (or unlock command)
961 if ($this->Aseco->settings['lock_password'] == '' || $admin->unlocked) {
962 if ($com) {
963 switch ($com) {
964 case 'on':
965 $this->enabled = 1;
966 $message = formatText('{#server}'.$nick.' $g$fffenabled betting. Start next round!');
967 $this->Aseco->client->addCall('ChatSendServerMessage', array($this->Aseco->formatColors($message)));
968 break;
969 case 'off':
970 $this->enabled = 0;
971 $message = formatText('{#server}'.$nick.' $g$fffdisabled betting. This is the last round!');
972 $this->Aseco->client->addCall('ChatSendServerMessage', array($this->Aseco->formatColors($message)));
973 break;
974 default:
975 break;
976 }
977 } else {
978 $message = '{#server}{#error}Missing parameter. Usage like $fff"/betstate on/off"{#error}!';
979 $this->Aseco->client->query('ChatSendToLogin', $this->Aseco->formatColors($message), $login);
980 }
981 } else {
982 // write warning in console
983 $this->Aseco->console($login . ' tried to use admin chat command (not unlocked!): "/beton"');
984 // show chat message
985 $this->Aseco->client->query('ChatSendToLogin', $this->Aseco->formatColors('{#error}You don\'t have the required admin rights to do that, unlock first!'), $login);
986 }
987 } else {
988 // write warning in console
989 $this->Aseco->console($login . ' tried to use admin chat command (no permission!): "/beton" ');
990 // show chat message
991 $this->Aseco->client->query('ChatSendToLogin', $this->Aseco->formatColors('{#error}You don\'t have the required admin rights to do that!'), $login);
992 }
993 }
994
995/*********************** CHAT COMMANDS END *******************************/
996
997} // end class
998
999// chat commands
1000Aseco::addChatCommand('bet', 'Bet amount of coppers "/bet coppers"');
1001Aseco::addChatCommand('accept', 'Accept the bet with given amount of coppers.');
1002Aseco::addChatCommand('betstake', 'Display players stake.');
1003Aseco::addChatCommand('betwin', 'Display player wins.');
1004Aseco::addChatCommand('bettotalstake', 'List actual bet participants');
1005Aseco::addChatCommand('betstate', 'Enable/Disable betting "/betstate On/Off".');
1006
1007global $bcp;
1008$bcp = new betting_coppers(false);
1009
1010// events
1011Aseco::registerEvent('onSync', array($bcp, 'upToDate'));
1012Aseco::registerEvent('onStartup', 'betting_coppers_startup');
1013Aseco::registerEvent('onEverySecond', 'bet_counter');
1014Aseco::registerEvent('onNewChallenge', 'start_bet_round');
1015Aseco::registerEvent('onRestartChallenge', 'start_bet_round');
1016Aseco::registerEvent('onBillUpdated', 'bet_bill_update');
1017Aseco::registerEvent('onPlayerManialinkPageAnswer', 'bet_handle_buttonclick');
1018Aseco::registerEvent('onEndRace', 'pay_bet_winner');
1019
1020Aseco::registerEvent('onBeginRound', array($bcp, 'reset_bet_counter'));
1021Aseco::registerEvent('onRestartChallenge', array($bcp, 'reset_bet_counter2'));
1022
1023function betting_coppers_startup($aseco){
1024 global $bcp;
1025 $bcp->startUp($aseco);
1026}
1027
1028function chat_bet($aseco,$command) {
1029 global $bcp;
1030 $bcp->bet_command($command);
1031}
1032
1033function chat_accept($aseco,$command) {
1034 global $bcp;
1035 $bcp->accept_command($command);
1036}
1037
1038function chat_betstake($aseco,$command) {
1039 global $bcp;
1040 $bcp->betstake_command($command);
1041}
1042
1043function chat_betwin($aseco,$command) {
1044 global $bcp;
1045 $bcp->betwin_command($command);
1046}
1047
1048function chat_bettotalstake($aseco,$command) {
1049 global $bcp;
1050 $bcp->totalstake_command($command);
1051}
1052
1053function chat_betstate($aseco,$command) {
1054 global $bcp;
1055 $bcp->enable_command($command);
1056}
1057
1058function bet_counter() {
1059 global $bcp;
1060 $bcp->on_everysecond();
1061}
1062
1063function start_bet_round($aseco, $player) {
1064 global $bcp;
1065 $bcp->validate_players();
1066 $bcp->getServerCoppers();
1067 $bcp->reset_bet();
1068}
1069
1070function bet_bill_update($aseco, $bill) {
1071 global $bcp;
1072 $bcp->bet_bill_upd($bill);
1073}
1074
1075function bet_handle_buttonclick($aseco, $command) {
1076 global $bcp;
1077 $bcp->bet_handle_buttons($command);
1078}
1079
1080function pay_bet_winner($aseco) {
1081 global $bcp;
1082 $bcp->get_winner();
1083}
1084
1085?>