5 * Copyright (C) 2006-2013 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details. You should have received a
19 * copy of the GNU General Public License along with this program; if
20 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21 * Suite 330, Boston, MA 02111-1307, USA.
24 define('BIN5_PLAYERS_N', 3);
25 define('BIN5_CARD_HAND', 3); // normal value 8
26 define('BIN5_MAX_PLAYERS', BIN5_PLAYERS_N);
27 // define(BIN5_SHM_MIN', (50000 * BIN5_MAX_PLAYERS));
28 define('BIN5_SHM_MIN', 32768);
29 define('BIN5_SHM_MAX', (BIN5_SHM_MIN + 1048576));
30 define('BIN5_SHM_DLT', 32768);
31 define('BIN5_PROXY_PATH', PROXY_PATH."/bin5");
33 define('BIN5_RULES_OLDSCHEMA', -1);
34 define('BIN5_RULES_FINISH', 0);
35 define('BIN5_RULES_ABANDON', 1);
36 define('BIN5_RULES_ALLPASS', 2);
38 define('BIN5_TOURNAMENT_OLDRULES', 1);
39 define('BIN5_TOURNAMENT_NO_DRAW', 2);
41 $mlang_bin5_bin5 = array(
42 // br, hr, b, /b, win, fri
43 'info_last' => array( 'it' => '%3$sultima mano%4$s',
44 'en' => '%3$slast hand%4$s'),
45 'info_curr' => array( 'it' => '%3$smano corrente%4$s',
46 'en' => '%3$scurrent hand%4$s'),
47 // br, hr, b, /b, win, fri
48 'info_part' => array( 'it' => 'Ha chiamato %3$s%5$s%4$s (punti torneo %7$d), il socio era %3$s%6$s%4$s,%1$s',
49 'en' => 'The declarer was %3$s%5$s%4$s (tournment points %7$d), the partner was %3$s%6$s%4$s,%1$s'),
51 'info_capp' => array( 'it' => 'hanno fatto %3$scappotto%4$s EBBRAVI!.%1$s',
52 'en' => 'and they made %3Dscapot%4$s WELL DONE!.%1$s'),
54 // br, hr, b, /b, old_asta_pnt, old_pnt, winornot
55 'info_alea' => array( 'it' => 'dovevano fare %3$salmeno %5$d%4$s punti e ne hanno fatti %3$s%6$d%4$s: hanno %7$s.%1$s',
56 'en' => 'they had to do %3$sat least %5$d%4$s points and they had made %3$s%6$d%4$s: they have %7$s.%1$s'),
57 // br, hr, b, /b, old_pnt, winornot
58 'info_more' => array( 'it' => 'dovevano fare %3$spiù di 60%4$s punti e ne hanno fatti %3$s%5$d%4$s: hanno %6$s.%1$s',
59 'en' => 'they had to do %3$sover 60%4$s points and they had made %3$s%5$d%4$s: they have %3$s%6$s%4$s.%1$s'),
60 'info_win' => array( 'it' => 'vinto',
62 'info_peer' => array( 'it' => 'pareggiato',
64 'info_lost' => array( 'it' => 'perso',
67 // br, hr, b, /b, win_name
68 'info_alon' => array( 'it' => '%3$s%5$s%4$s si è chiamato in mano (punti torneo %6$d),%1$s',
69 'en' => '%3$s%5$s%4$s play alone against each other (tournment points %6$d),%1$s'),
70 // br, hr, b, /b, old_asta_pnt, old_pnt, winornot
71 'info_aleaa' => array( 'it' => 'doveva fare %3$salmeno %5$d%4$s punti e ne ha fatti %3$s%6$d%4$s: ha %3$s%7$s%4$s.%1$s',
72 'en' => 'he/she had to do %3$sat least %5$d%4$s points and they had made %3$s%6$d%4$s: he/she had %3$s%7$s%4$s.%1$s'),
73 // br, hr, b, /b, old_pnt, winornot
74 'info_morea' => array( 'it' => 'doveva fare %3$spiù di 60%4$s punti e ne ha fatti %3$s%5$d%4$s: ha %3$s%6$s%4$s.%1$s',
75 'en' => 'he/she had to do %3$smore than 60%4$s points and they had made %3$s%5$d%4$s: he/she had %3$s%6$s%4$s.%1$s'),
77 'info_acap' => array( 'it' => 'ha fatto %3$scappotto%4$s EBBRAVO!.%1$s',
78 'en' => 'and he/she made %3$scapot%4$s WELL DONE!.%1$s'),
81 'info_omul' => array( 'it' => '%1$sLa partita valeva %3$s%5$s%4$s.%1$s',
82 'en' => '%1$sEN: The game was worth %3$s%5$s%4$s.%1$s' ),
85 'info_alpa' => array( 'it' => '%1$sHanno passato %3$stutti%4$s.%1$s',
86 'en' => '%1$sEN: Hanno passato %3$stutti%4$s.%1$s' ),
88 // br, hr, b, /b, aband-name
89 'info_aban' => array( 'it' => ' Ha lasciato %3$s%5$s%4$s perché aveva al massimo %3$s2 punti%4$s.',
90 'en' => ' EN: Ha lasciato %3$s%5$s%4$s perché aveva al massimo %3$s2 punti%4$s.'),
92 'info_shuf' => array( 'it' => 'Il mazzo a <b>%s</b>, ',
93 'en' => '<b>%s</b> shuffled the cards, '),
94 'info_yturn'=> array( 'it' => ' tocca a <b>te</b> giocare.',
95 'en' => ' it\'s <b>your</b> turn.'),
96 'info_turn' => array( 'it' => 'tocca a <b>%s</b> giocare.',
97 'en' => 'it\'s the <b>%s</b>\'s turn.'),
98 'info_mult' => array( 'it' => ' La partita vale <b>%s</b>.',
99 'en' => ' The game worth <b>%s</b>.' ),
100 'info_yshuf'=> array( 'it' => 'Fai <b>tu</b> il mazzo, ',
101 'en' => 'It\'s <b>your</b> shuffled the cards, '),
102 'info_match'=> array( 'it' => 'Il codice della partita è <b>%d</b>.',
103 'en' => 'Match code is <b>%d</b>.'),
104 'btn_bkgame'=> array( 'it' => 'torna alla partita',
105 'en' => 'back to the game'),
106 'call_wptn' => array( 'it' => '<br>con %d punti',
107 'en' => '<br>with %d points'),
108 'call_ycall'=> array( 'it' => 'Chiami%s:',
110 'call_call' => array( 'it' => 'Chiama %s%s:',
111 'en' => 'The declarer is %s%s:')
115 $table_wellarr = Array( 'it' => Array ( 'Benvenuto al tavolo. Se almeno tre giocatori non sbloccano l\'uscita cliccando il lucchetto, chi esce non può risedersi a un qualunque tavolo per '.floor(BAN_TIME/60).' minuti.'),
116 'en' => Array ( 'EN Benvenuto al tavolo. Se almeno tre giocatori non sbloccano l\'uscita cliccando il lucchetto, chi esce non può risedersi a un qualunque tavolo per '.floor(BAN_TIME/60).' minuti.') );
118 function multoval($mult)
123 return ($G_lang == 'en' ? 'double' : 'doppio');
125 return ($G_lang == 'en' ? 'triple' : 'triplo');
127 return ($G_lang == 'en' ? 'quadruple' : 'quadruplo');
129 return (sprintf(($G_lang == 'en' ? "%d-ple" : "%d-plo"), $mult));
132 abstract class Rules {
135 abstract function engine(&$bri, $curtime, $action, $user);
137 function __construct($table)
139 $this->table = $table;
143 class Rules_old_rules extends Rules {
144 function __construct($table)
146 parent::__construct($table);
149 static function asta2mult($asta_pnt)
153 else if ($asta_pnt > 100)
155 else if ($asta_pnt > 90)
157 else if ($asta_pnt > 80)
159 else if ($asta_pnt > 70)
164 static function s_multer($mult, $pnt)
166 return (pow(2, $mult) * static::asta2mult($pnt));
169 static function s_point_calc($pnt_done, $mult, $pnt_req, $is_allpoints)
171 return ($pnt_done * static::s_multer($mult, $pnt_req) * ($is_allpoints ? 2 : 1));
174 function multer($is_new)
177 return (static::s_multer($this->table->mult, $this->table->asta_pnt));
180 return (static::s_multer($this->table->old_mult, $this->table->old_asta_pnt));
184 static function game_result($asta_pnt, $pnt)
186 $sixty = (BIN5_PLAYERS_N == 3 ? 30 : 60);
187 if ($asta_pnt == 61) {
190 else if ($pnt == $sixty)
196 if ($pnt >= $asta_pnt)
203 function engine(&$bri, $curtime, $action, $user)
205 GLOBAL $G_all_points, $G_dbasetype;
207 $table = $this->table;
210 if ($action == BIN5_RULES_ALLPASS) { // return TRUE if all correct
211 $table->old_act = $action;
212 $table->old_asta_win = -1;
215 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
220 // $table->game_next(1);
221 $table->game_init(&$bri->user);
223 else if ($action == BIN5_RULES_ABANDON) { // return TRUE if all correct
224 if (!($user->handpt <= 2)) {
227 $table->old_act = $action;
228 log_wr(sprintf("GIOCO FINITO !!!"));
229 $table->old_asta_win = $user->table_pos;
233 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
237 // Non si cambia mazzo se si abbandona la partita
239 // $table->game_next(0);
240 $table->game_init(&$bri->user);
242 else if ($action == BIN5_RULES_FINISH) { // return TRUE if all correct
243 $table->old_act = $action;
247 if ($table->asta_pnt == 60)
248 $table->asta_pnt = 61;
250 $table->old_reason = "";
252 // count points for the temporary 2 teams
253 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
254 $ctt = $table->card[$i]->value % 10;
255 $own = $table->card[$i]->owner;
256 if ($own == $table->asta_win || $own == $table->friend)
257 $pro += $G_all_points[$ctt];
260 log_wr(sprintf("PRO: [%d]", $pro));
263 if (static::game_result($table->asta_pnt, $pro) == 0) {
264 $table->points[$table->points_n % MAX_POINTS] = array();
265 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
266 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
270 $table->old_pnt = $pro;
271 $table->old_asta_win = $table->asta_win;
277 if (static::game_result($table->asta_pnt, $pro) == 1)
282 // TAG: POINTS_MANAGEMENT
283 $table->points[$table->points_n % MAX_POINTS] = array();
284 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
285 if ($i == $table->asta_win)
286 $pnt = ($i == $table->friend ? 4 : 2);
287 else if ($i == $table->friend)
292 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pnt));
294 $pnt_sav = static::s_point_calc($pnt * $sig, 0, $table->asta_pnt, ($pro == 120));
295 $pnt_tab = static::s_point_calc($pnt * $sig, $table->mult, $table->asta_pnt, ($pro == 120));
297 $table->points[$table->points_n % MAX_POINTS][$i] = $pnt_tab;
298 $table->total[$i] += $pnt_tab;
299 $pnts_sav[$i] = $pnt_sav;
302 $table->old_pnt = $pro;
303 $table->old_asta_win = $table->asta_win;
311 $table->game_next($game_delta);
313 $plist = "$table->table_token|$user->table_orig|$table->player_n";
316 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
317 $user_cur = &$bri->user[$table->player[$i]];
320 $ucodes[$i] = $user_cur->code_get();
323 $plist .= '|'.xcapelt($user_cur->name).'|'.$pnts_sav[$i];
324 $codes .= '|'.xcapelt($user_cur->code_get());
327 log_legal($curtime, $user->ip, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
329 $table->old_asta_pnt = $table->asta_pnt;
330 // $table->old_mazzo is managed by ->game_next();
331 // $table->old_mult, $table->old_pnt, $table->old_reason and $table->old_asta_win are specific
333 $table->old_friend = $table->friend;
334 $table->old_tourn_pts = $table->tourn_pts;
336 $table->old_reason = static::game_description($action, 'html', $table->old_mult,
337 $table->old_asta_win,
338 ($table->old_asta_win != -1 ?
339 $bri->user[$table->player[$table->old_asta_win]]->name : ""),
341 ($table->old_friend != -1 ?
342 $bri->user[$table->player[$table->old_friend]]->name : ""),
343 $table->old_pnt, $table->old_asta_pnt, $table->old_tourn_pts);
346 if ($user->table_orig < TABLES_AUTH_N) {
347 require_once("../Obj/dbase_".$G_dbasetype.".phh");
349 if (($bdb = BriskDB::create()) != FALSE) {
350 $bdb->bin5_points_save($curtime, $table, $user->table_orig, $action, $ucodes, $pnts_sav);
354 log_points($user->ip, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED");
356 log_points($user->ip, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
359 $table->game_init(&$bri->user);
364 static function game_description($act, $form, $old_mult, $win = -1, $win_name = "?1?", $fri = -1, $fri_name = "?2?",
365 $old_pnt = -1, $old_asta_pnt = -1, $old_tourn_pts = -1)
367 GLOBAL $G_lang, $mlang_bin5_bin5;
369 if ($form == 'html') {
374 $win_name = xcape($win_name);
375 $fri_name = xcape($fri_name);
384 if ($act == BIN5_RULES_OLDSCHEMA) {
387 else if ($act == BIN5_RULES_ALLPASS) {
388 return (sprintf($mlang_bin5_bin5['info_alpa'][$G_lang],
389 $tg_br, $tg_hr, $tg_bo, $tg_bc));
391 else if ($act == BIN5_RULES_ABANDON) {
392 return (sprintf($mlang_bin5_bin5['info_aban'][$G_lang],
393 $tg_br, $tg_hr, $tg_bo, $tg_bc,
397 $wol = static::game_result($old_asta_pnt, $old_pnt);
401 if ($win != $fri) { // not alone case
402 /* MLANG: "<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>", "hanno fatto <b>cappotto</b> EBBRAVI!.<hr>", "dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>", "<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>", "ha fatto <b>cappotto</b> EBBRAVO!.<hr>", "doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>", ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt : 'più di 60'), $table->old_pnt, ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso" */
404 $noty .= sprintf($mlang_bin5_bin5['info_part'][$G_lang],
405 $tg_br, $tg_hr, $tg_bo, $tg_bc,
409 if ($old_pnt == 120) {
410 $noty .= sprintf($mlang_bin5_bin5['info_capp'][$G_lang],
411 $tg_br, $tg_hr, $tg_bo, $tg_bc );
414 if ($old_asta_pnt > 61) {
415 $noty .= sprintf($mlang_bin5_bin5['info_alea'][$G_lang],
416 $tg_br, $tg_hr, $tg_bo, $tg_bc,
417 $old_asta_pnt, $old_pnt,
418 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
419 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
420 $mlang_bin5_bin5['info_lost'][$G_lang])));
423 $noty .= sprintf($mlang_bin5_bin5['info_more'][$G_lang],
424 $tg_br, $tg_hr, $tg_bo, $tg_bc,
426 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
427 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
428 $mlang_bin5_bin5['info_lost'][$G_lang])));
429 } // else of if ($old_asta_pnt > 61) {
430 } // else of if ($old_pnt == 120) {
431 } // if ($win != $fri) { // not alone case
433 $noty .= sprintf($mlang_bin5_bin5['info_alon'][$G_lang],
434 $tg_br, $tg_hr, $tg_bo, $tg_bc,
437 if ($old_pnt == 120) {
438 $noty .= sprintf($mlang_bin5_bin5['info_acap'][$G_lang],
439 $tg_br, $tg_hr, $tg_bo, $tg_bc);
442 if ($old_asta_pnt > 61) {
443 $noty .= sprintf($mlang_bin5_bin5['info_aleaa'][$G_lang],
444 $tg_br, $tg_hr, $tg_bo, $tg_bc,
445 $old_asta_pnt, $old_pnt,
446 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
447 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
448 $mlang_bin5_bin5['info_lost'][$G_lang])));
451 $noty .= sprintf($mlang_bin5_bin5['info_morea'][$G_lang],
452 $tg_br, $tg_hr, $tg_bo, $tg_bc,
454 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
455 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
456 $mlang_bin5_bin5['info_lost'][$G_lang])));
462 $old_multer = static::s_multer($old_mult, $old_asta_pnt);
463 if ($old_multer > 1) {
464 $noty .= sprintf($mlang_bin5_bin5['info_omul'][$G_lang],
465 $tg_br, $tg_hr, $tg_bo, $tg_bc,
466 multoval($old_multer));
468 $noty .= sprintf('%2$s', $tg_br, $tg_hr);
472 } // class Rules_old_rules
475 var $value; /* 0 - 39 card value */
476 var $stat; /* 'bunch', 'hand', 'table', 'take' */
477 var $owner; /* (table position 0-4) */
478 // var $pos; /* Pos in hand. */
479 var $x; /* When played the X position on the table of the owner. */
480 var $y; /* When played the Y position on the table of the owner. */
482 function Card($value, $stat, $owner)
484 $this->value = $value;
485 $this->stat = $stat; // Card stat
486 $this->owner = $owner;
489 function assign($stat,$owner)
491 $this->stat = $stat; // Card stat
492 $this->owner = $owner;
495 function setpos($pos)
502 $this->stat = 'table'; // Card stat
507 function take($newown)
509 $this->stat = 'take'; // Card stat
510 $this->owner = $newown;
514 class Bin5_table extends Table {
515 var $card; // il mazzo di carte
516 var $mazzo; // chi e' di mazzo
526 var $points; // points array
527 var $points_n; // number of row of points
530 var $asta_win; // the caller idx position at table
532 var $tourn_pts; // points in the caller hand
533 var $friend; // the callee idx position at table
535 var $match_id; // the id of the match on the database (-1 == just not saved)
537 var $old_act; // last action that trigs the end of the game
542 var $old_pnt; // points made by caller and callee
543 var $old_asta_win; // the old caller idx position at table
544 var $old_friend; // the old callee idx position at table
546 var $old_tourn_pts; // the old tournment computed points in the hand of caller
549 function Bin5_table()
555 function create($idx)
557 if (($thiz =& new Bin5_table()) == FALSE)
563 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
564 $thiz->asta_pla_n= -1;
565 $thiz->asta_card = -1;
566 $thiz->asta_pnt = -1;
569 $thiz->points = array( );
571 $thiz->total = array( 0, 0, 0, 0, 0);
572 $thiz->asta_win = -1;
573 $thiz->briscola = -1;
574 $thiz->tourn_pts = -1;
578 $thiz->match_id = -1;
581 $thiz->old_mazzo = -1;
582 $thiz->old_reason = "";
583 $thiz->old_asta_pnt = -1;
584 $thiz->old_mult = -1;
586 $thiz->old_asta_win = -1;
587 $thiz->old_friend = -1;
594 function myclone(&$from)
596 if (($thiz =& new Bin5_table()) == FALSE)
601 $thiz->card = $from->card;
602 $thiz->mazzo = $from->mazzo; // REVIEW
603 $thiz->gstart = $from->gstart;
604 $thiz->turn = $from->turn;
606 $thiz->asta_pla = $from->asta_pla;
607 $thiz->asta_pla_n = $from->asta_pla_n;
608 $thiz->asta_card = $from->asta_card;
609 $thiz->asta_pnt = $from->asta_pnt;
611 $thiz->mult = $from->mult;
612 $thiz->points = $from->points;
613 $thiz->points_n = $from->points_n;
614 $thiz->total = $from->total;
616 $thiz->asta_win = $from->asta_win;
617 $thiz->briscola = $from->briscola;
618 $thiz->tourn_pts = $from->tourn_pts;
619 $thiz->friend = $from->friend;
621 $thiz->match_id = $from->match_id;
623 $thiz->old_act = $from->old_act;
624 $thiz->old_mazzo = $from->old_mazzo;
625 $thiz->old_reason = $from->old_reason;
626 $thiz->old_asta_pnt = $from->old_asta_pnt;
627 $thiz->old_mult = $from->mult;
628 $thiz->old_pnt = $from->old_pnt;
629 $thiz->old_asta_win = $from->old_asta_win;
630 $thiz->old_friend = $from->old_friend;
636 function parentcopy(&$from)
641 function spawn(&$from)
643 if (($thiz =& new Bin5_table()) == FALSE)
646 $thiz->parentcopy($from);
648 log_main("PLAYER_N - spawn.".$thiz->player_n);
650 $thiz->card = array();
651 $thiz->bunch_create();
652 $thiz->mazzo = rand(0,PLAYERS_N-1);
653 $thiz->points = array();
654 $thiz->total = array();
658 $thiz->match_id = -1;
660 $thiz->old_asta_win = -1;
661 $thiz->old_reason = "";
664 $rules_name = "Rules_old_rules";
665 $thiz->rules = new $rules_name($thiz);
666 // players are rearranged in an dedicated array
667 $thiz->player = array();
668 for ($i = 0 ; $i < $from->player_n ; $i++)
669 $thiz->player[$i] = $i;
671 log_main("TABLE_OLD_WIN - spawn:".$thiz->old_asta_win);
677 // function bunch_create_old() function AND
681 // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
682 // // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
683 // $ret[$i] =& new Card($i, 'bunch', 'no_owner');
690 function bunch_create()
694 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
695 $this->card[$i] = new Card($i, 'bunch', 'no_owner');
699 function bunch_make()
701 log_main("bunch_make start");
702 $ct = array(0,0,0,0,0);
704 mt_srand(make_seed());
706 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--)
709 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--) {
713 log_main("RND ZERO");
717 $owner = $i % BIN5_PLAYERS_N;
718 $this->card[$id]->assign('hand', $owner);
720 $rest[$rn] = $rest[$i];
721 // $pubbpos[$rn2] = $pubbpos[$i];
723 log_main("bunch_make end");
726 function init($userarr)
729 $this->mazzo = rand(0,PLAYERS_N-1);
732 $this->old_asta_win = -1;
733 $this->old_reason = "";
735 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
736 $this->total[$i] = 0;
737 $user_cur = $userarr[$this->player[$i]];
738 $user_cur->stat_set('table');
739 $user_cur->exitislock = TRUE;
742 log_main("table::init: ci siamo");
745 function game_init($userarr)
749 $this->gstart = ($this->mazzo+1) % BIN5_PLAYERS_N;
752 $this->asta_pla_n = BIN5_PLAYERS_N;
753 $this->asta_card = -1;
754 $this->asta_pnt = 60;
755 $this->asta_win = -1;
756 $this->briscola = -1;
757 $this->tourn_pts = -1;
761 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
762 $this->asta_pla[$i] = TRUE;
763 $user_cur = $userarr[$this->player[$i]];
764 $user_cur->subst = 'asta';
765 $user_cur->asta_card = -2;
766 $user_cur->asta_pnt = -1;
767 $user_cur->handpt = $this->hand_points($i);
772 function game_next($delta)
774 $this->old_mazzo = $this->mazzo;
775 $this->mazzo = ($this->mazzo + $delta) % BIN5_PLAYERS_N;
778 function mult_inc($val)
780 $this->old_mult = $this->mult;
784 function mult_set($val)
786 $this->old_mult = $this->mult;
790 function hand_points($idx)
792 GLOBAL $G_all_points;
796 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
797 // for ($i = 0 ; $i < 40 ; $i++) {
798 if ($this->card[$i]->owner != $idx)
801 $ctt = $this->card[$i]->value % 10;
802 $tot += $G_all_points[$ctt];
809 function exitlock_show($userarr, $table_pos)
811 $ct = $this->exitlock_calc($userarr, $table_pos);
813 $ret = sprintf('exitlock_show(%d, %s);', $ct,
814 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
818 function exitlock_calc(&$userarr, $table_pos)
822 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
823 if ($userarr[$this->player[$i]]->exitislock == FALSE)
830 function match_continue(&$bri, $user, $match_id_s)
836 /* - verify if match_id and user are both valid to accept
837 the match_continue request - */
838 $match_id = (int)$match_id_s;
839 if ($match_id <= 0) {
840 $msg = "questa partita non esiste";
844 if ($user->continue_get() == BIN5_USER_CONTINUE_ALREADY) {
845 $msg = "Hai già richiesto di continuare una partita.";
849 if ($user->continue_get() == $match_id) {
850 $msg = "Hai già richiesto di continuare questa partita.";
853 // retrieves users list for this match
854 $match_data = array();
855 if (($bdb = BriskDB::create()) != FALSE) {
856 // match_order_get return FALSE for old matches
857 $ucodes = $bdb->match_order_get($match_data, $match_id, BIN5_PLAYERS_N);
860 if ($ucodes == FALSE) {
861 $msg = "questa partita non è stata memorizzata correttamente";
865 // if current user code must be in the users list
866 if (array_search($user->code, $ucodes) === FALSE) {
867 $msg = sprintf("Questo utente non compare nella partita che si vuole continuare [%d].", $user->code);
871 /* - the user is in the list, add the match_id to his profile
872 and check if he is the N'th to require continue - */
874 // set the match_id for the current user
875 $user->continue_set($match_id);
877 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
878 $user_cur = &$bri->user[$this->player[$i]];
879 if ($user_cur->continue_get() != $match_id) {
884 // not all players set the continue match than we exit
885 if ($i < BIN5_PLAYERS_N) {
886 $msg = sprintf("<b>L'utente <i>%s</i> vorrebbe continuare la partita n° %d.</b>",
887 xcape($user->name), $match_id);
891 /* - all users decide to continue the same match, update all infos and rearrange users
892 to the right positions on the table - */
894 /* reset users table order */
895 for ($i = 0 ; $i < BIN5_PLAYERS_N - 1 ; $i++) {
896 if ($bri->user[$this->player[$i]]->code == $ucodes[$i]) {
899 for ($e = $i + 1 ; $e < BIN5_PLAYERS_N ; $e++) {
900 if ($bri->user[$this->player[$e]]->code == $ucodes[$i]) {
901 $swap = $this->player[$i];
902 $this->player[$i] = $this->player[$e];
903 $this->player[$e] = $swap;
904 $bri->user[$this->player[$i]]->table_pos = $i;
905 $bri->user[$this->player[$e]]->table_pos = $e;
909 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
910 fprintf(STDERR, "USERZ: [%s]\n", $bri->user[$this->player[$i]]->name);
913 // update database info to be aligned with current table (ttok and table_idx
914 if (($bdb = BriskDB::create()) != FALSE) {
915 if ($bdb->match_continue($match_id, $this, $user->table_orig) == FALSE) {
917 $msg = "aggiornamento dei dati della partita fallito";
923 /* bunch and multiplier status set */
924 $this->mazzo = $match_data['mazzo_next'];
925 $this->mult = $match_data['mult_next'];
926 $this->match_id = $match_id;
927 $this->game_init(&$bri->user);
929 /* reload of the page with the new layout */
930 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
931 $user_cur = &$bri->user[$this->player[$i]];
932 $user_cur->continue_set(BIN5_USER_CONTINUE_ALREADY);
934 $user_cur->trans_step = $user_cur->step + 1;
935 $user_cur->comm[$user_cur->step % COMM_N] = sprintf('gst.st_loc++; gst.st=%d; xstm.stop(); window.onunload = null ; window.onbeforeunload = null ; document.location.assign("index.php");|', $user_cur->step+1);
936 $user_cur->step_inc();
938 // a void command force xynt-streamer to flush all data to client
939 $user_cur->trans_step = $user_cur->step + 1;
940 $user_cur->comm[$user_cur->step % COMM_N] = "";
941 $user_cur->step_inc();
943 $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$bri, &$user_cur, $user_cur->step+1, TRUE, FALSE);
944 $user_cur->step_inc();
949 $dt = date("H:i ", $curtime);
950 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
951 if ($ret == FALSE && $this->player[$i] != $user->idx)
953 $user_cur = &$bri->user[$this->player[$i]];
954 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
955 $user_cur->comm[$user_cur->step % COMM_N] .= nickserv_msg($dt, $msg);
956 $user_cur->step_inc();
960 } // end class Bin5_table
965 define('BIN5_USER_FLAG_RING_ENDAUCT', 0x01);
966 define('BIN5_USER_CONTINUE_INIT', -1);
967 define('BIN5_USER_CONTINUE_ALREADY', -2);
968 class Bin5_user extends User {
971 var $handpt; // Total card points at the beginning of the current hand.
972 var $exitislock; // Player can exit from the table ?
973 var $privflags; // Flags for briskin5 only
975 var $continue; // Id of the match that the user would continue
983 function create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
984 if (($thiz =& new User()) == FALSE)
987 $thiz->asta_card = -2;
988 $thiz->asta_pnt = -1;
990 $thiz->exitislock = TRUE;
991 $thiz->privflags = 0;
992 $thiz->continue = BIN5_USER_CONTINUE_INIT;
998 function parentcopy(&$from)
1000 parent::copy($from);
1003 function copy(&$from)
1005 $this->parentcopy($from);
1007 $this->asta_card = $from->asta_card;
1008 $this->asta_pnt = $from->asta_pnt;
1009 $this->handpt = $from->handpt;
1010 $this->exitislock = $from->exitislock;
1011 $this->privflags = $from->privflags;
1012 $this->continue = $from->continue;
1016 function myclone(&$from)
1018 if (($thiz =& new User()) == FALSE)
1027 static function spawn($from, &$bri, $table, $table_pos, $get, $post, $cookie)
1029 if (($thiz = new Bin5_user()) == FALSE)
1032 if (($CO_bin5_pref_ring_endauct = gpcs_var("CO_bin5_pref_ring_endauct", $get, $post, $cookie)) === FALSE) {
1033 $CO_bin5_pref_ring_endauct = "";
1036 $thiz->parentcopy($from);
1038 /* NOTE: at this moment idx and table_pos fields have the same value
1039 but diffentent functions, we keep them separated for a while */
1041 $thiz->idx = $table_pos; // it is the position in the mini-room,
1042 // not related to table pos (see below)
1043 $thiz->asta_card = -2;
1044 $thiz->asta_pnt = -1;
1046 $thiz->exitislock = TRUE;
1047 $thiz->continue = BIN5_USER_CONTINUE_INIT;
1049 log_wr("Bin5 constructor");
1051 $thiz->privflags = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
1053 $thiz->table_orig = $table;
1055 $thiz->table_pos = $table_pos;
1062 function step_set($step)
1064 $this->step = $step & 0x7fffffff;
1069 function step_inc($delta = 1) {
1070 $this->step += $delta;
1071 /* modularization because unpack() not manage unsigned 32bit int correctly */
1072 $this->step &= 0x7fffffff;
1077 static function load_step($tab_id, $sess)
1081 if (validate_sess($sess) == FALSE)
1084 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
1085 mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
1086 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step", 'rb')) == FALSE)
1088 if (($s = fread($fp, 8)) == FALSE)
1090 if (mb_strlen($s, "ASCII") != 8)
1092 $arr = unpack('Ls/Li', $s);
1095 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
1102 log_rd2("STEP_GET [".$sess."]: return false ");
1107 function save_step()
1110 if (validate_sess($this->sess) == FALSE)
1112 if (file_exists(BIN5_PROXY_PATH."/table".$this->table_orig) == FALSE)
1113 mkdir(BIN5_PROXY_PATH."/table".$this->table_orig, 0775, TRUE);
1114 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$this->table_orig."/".$this->sess.".step", 'w')) == FALSE)
1116 fwrite($fp, pack("LL",$this->step, $this->idx));
1119 log_main("step_set [".$this->sess. "] [".$this->step."]");
1127 static function unproxy_step($tab_id, $sess)
1129 log_rd2("UNPROXY: ".BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
1130 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
1133 @unlink(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
1136 function destroy_data($tab_id)
1139 if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos, "B")) == -1) {
1140 log_crit("BIN5 USER DATA REMOVE FAILED 1 [".FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos."]");
1144 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
1145 log_crit("BIN5 USER DATA REMOVE FAILED 2");
1148 if (shmop_delete($shm) == 0) {
1149 log_crit("BIN5 USER DATA REMOVE FAILED 3");
1154 log_main("BIN5 USER DATA DESTROY SUCCESS");
1156 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
1166 static function blocking_error($is_unrecoverable)
1168 log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
1169 return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
1172 protected function page_sync($sess, $page)
1174 log_rd2("PAGE_SYNC");
1175 printf("xXx BIN5_USER::PAGE_SYNC\n");
1176 return (sprintf('xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
1179 protected function maincheck($get, $post, $cookie)
1182 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
1183 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
1185 GLOBAL $S_load_stat;
1187 log_rd("maincheck begin");
1192 /* Nothing changed, return. */
1193 if ($this->rd_step == $this->step)
1196 log_rd2("do other cur_stat[".$this->rd_stat."] user->stat[".$this->stat."] cur_step[".$this->rd_step."] user_step[".$this->step."]");
1198 if ($this->rd_step == -1) {
1200 * if $this->rd_step == -1 load the current state from the main struct
1203 $S_load_stat['wR_minusone']++;
1205 // if ($this->the_end == TRUE) {
1206 // log_rd2("main_check: the end".var_export(debug_backtrace()));
1209 if ($this->trans_step != -1) {
1210 log_rd2("TRANS USATO ".$this->trans_step);
1211 $this->rd_step = $this->trans_step;
1212 $this->trans_step = -1;
1215 log_rd2("TRANS NON ATTIVATO");
1220 /* this part I suppose is read only on $this->room structure */
1221 if ($this->rd_step == -1) {
1222 log_rd2("PRE-NEWSTAT");
1229 if ($this->stat == 'table') {
1231 /* NOTE: $this->room is associated with the current $bri object */
1232 printf("xXx CLASS NAME [%s]\n", get_class($this->room));
1233 $ret = show_table(&$this->room, $this, $this->step, FALSE, FALSE);
1235 log_rd2("NEWSTAT: ".$this->stat);
1237 $this->rd_stat = $this->stat;
1238 $this->rd_subst = $this->subst;
1239 $this->rd_step = $this->step;
1240 } /* if ($this->rd_step == -1) { */
1242 $S_load_stat['rU_heavy']++;
1244 if ($this->rd_step < $this->step) {
1246 if ($this->rd_step + COMM_N < $this->step) {
1247 if ($this->rd_stat != $this->stat) {
1248 $to_stat = $this->stat;
1250 printf("xXx BIN5_USER::MAINCHECK\n");
1251 return ($this->page_sync($this->sess, ($to_stat == "table" ? "index.php" : "../index.php"), $this->table, $this->table_token));
1253 log_rd2("lost history, refresh from scratch");
1254 printf("xXx LOST HISTORY!\n");
1255 $this->rd_step = -1;
1258 for ($i = $this->rd_step ; $i < $this->step ; $i++) {
1260 if ($this->comm[$ii] == "") {
1261 if ($i == $this->rd_step)
1266 log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
1267 $ret .= $this->comm[$ii];
1269 $this->rd_stat = $this->stat;
1270 $this->rd_subst = $this->subst;
1271 $this->rd_step = $this->step;
1274 log_rd2($this->step, 'index_rd.php: after ret set');
1276 // if ($this->the_end == TRUE) { management is moved
1277 // in the spush scope
1278 } /* if ($this->rd_step < $this->step) { */
1279 } /* else of if ($this->rd_step == -1) { */
1283 } // function maincheck(...
1285 function continue_set($match_code)
1287 $this->continue = $match_code;
1290 function continue_get() {
1291 return ($this->continue);
1294 } // end class Bin5_user
1298 static $delta_t = array();
1299 var $brisk;// room object reference
1303 var $comm; // commands for many people
1304 var $step; // current step of the comm array
1305 var $garbage_timeout;
1316 function Bin5($brisk, $table_idx, $table_token, $get, $post, $cookie) {
1317 $this->user = array();
1318 $this->table = array();
1320 $this->the_end = FALSE;
1321 $this->shm_sz = BIN5_SHM_MIN;
1322 if (($this->tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
1327 $this->brisk = $brisk;
1328 $user = $brisk->user;
1329 $table = $brisk->table[$table_idx];
1330 log_wr("Bin5 constructor");
1332 for ($i = 0 ; $i < $table->player_n ; $i++) {
1333 $user[$table->player[$i]]->table_token = $table_token;
1334 $this->user[$i] = Bin5_user::spawn($user[$table->player[$i]], $this, $table_idx, $i, $get, $post, $cookie);
1336 $this->table[0] = Bin5_table::spawn(&$table);
1338 log_main("TABLE_OLD_WIN - Bin5:".$this->table[0]->old_asta_win);
1340 $this->table_idx = $table_idx;
1341 $this->table_token = $table_token;
1342 $this->garbage_timeout = 0;
1344 $this->delay_mgr = new Delay_Manager((GARBAGE_TIMEOUT *3.0) / 2.0);
1346 log_wr("Bin5 constructor end");
1350 function get_user($sess, &$idx)
1354 if (validate_sess($sess)) {
1355 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1356 if ($this->user[$i]->is_empty())
1358 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1361 $ret = &$this->user[$i];
1365 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1366 // for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++)
1367 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1370 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1375 function banned_kickoff()
1379 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1380 $user_cur = $this->user[$i];
1382 // check if the IP is blacklisted
1383 if ($this->brisk->black_check($user_cur->ip)) {
1384 $user_cur->lacc = 0;
1389 // if authorized not check if banlisted
1390 if ($user_cur->is_auth()) {
1394 if ($this->brisk->ban_check($user_cur->ip)) {
1395 $user_cur->lacc = 0;
1402 function garbage_manager($force)
1406 /* Garbage collector degli utenti in timeout */
1410 $delta = $this->delay_mgr->delta_get($curtime);
1412 if ($force || $this->garbage_timeout < $curtime) {
1413 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1414 $user_cur = $this->user[$i];
1415 if ($user_cur->is_active() == FALSE || // is not active user or
1416 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
1419 if ($user_cur->lacc + EXPIRE_TIME_RD < ($curtime - $delta)) { // Auto logout dell'utente
1420 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
1422 if ($user_cur->stat == 'table') {
1423 log_auth($user_cur->sess," bin5 Autologout session.");
1425 /* main garbage_manager is delegate as autologout management */
1426 $user_cur->the_end = TRUE;
1428 /* se gli altri utenti non erano d'accordo questo utente viene bannato */
1429 $remcalc = $this->table[0]->exitlock_calc(&$this->user, $user_cur->table_pos);
1431 require_once("${G_base}Obj/hardban.phh");
1432 Hardbans::add(($user_cur->is_auth() ? $user_cur->name : FALSE),
1433 $user_cur->ip, $user_cur->sess, $user_cur->laccwr + BAN_TIME);
1435 // $user->bantime = $user->laccwr + BAN_TIME;
1437 $this->table_wakeup($user_cur);
1441 log_rd2($user_cur->sess." GARBAGE UPDATED!");
1443 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
1448 $this->delay_mgr->lastcheck_set($curtime);
1453 function destroy_data()
1459 log_main("DESTROY BRISKIN5 DATA");
1462 log_main("DESTROY2 BRISKIN5 DATA [".$this->table_idx."]");
1463 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1464 $this->user[$i]->destroy_data($this->table_idx);
1465 Bin5_user::unproxy_step($this->table_idx, $this->user[$i]->sess);
1467 if (($tok = @ftok(FTOK_PATH."/bin5/table".$this->table_idx."/table", "B")) == -1)
1470 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
1473 if (shmop_delete($shm) == 0) {
1474 log_only("REMOVE FALLITA ");
1479 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
1481 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
1491 static function lock_data($is_exclusive, $table_idx)
1493 if (($res = file_lock(FTOK_PATH."/bin5/table".$table_idx."/table", $is_exclusive)) != FALSE) {
1494 self::$delta_t = microtime(TRUE);
1495 log_lock("LOCK table [".$table_idx."] [".self::$delta_t[$table_idx]."]");
1497 return (new Vect(array('res' => $res, 'tab' => $table_idx)));
1504 static function unlock_data($res_vect)
1508 $res = $res_vect->getbyid('res');
1509 $tab = $res_vect->getbyid('tab');
1511 log_lock("UNLOCK table [".$tab."] [".(microtime(TRUE) - (self::$delta_t[$tab]))."]");
1517 function chatt_send($user, $mesg, $mlang_indwr = NULL)
1519 GLOBAL $mlang_brisk, $G_lang;
1521 if ($user->stat != 'table') {
1526 $table = &$this->table[$user->table];
1527 $user_mesg = substr($mesg,6);
1532 $dt = date("H:i ", $curtime);
1533 if (strncmp($user_mesg, "/cont ", 6) == 0) {
1534 log_main($user->sess." chatt_send BEGIN");
1536 $match_id = substr($user_mesg, 6);
1537 $table->match_continue($this, $user, $match_id);
1540 for ($i = 0 ; $i < ($user->stat == 'room' ? BIN5_MAX_PLAYERS : BIN5_PLAYERS_N) ; $i++) {
1541 if ($user->stat == 'room') {
1542 $user_cur = &$this->user[$i];
1543 if ($user_cur->is_active() == FALSE || $user_cur->stat != 'room') // is not active user or stat isn't 'room'
1547 $user_cur = &$this->user[$table->player[$i]];
1550 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1551 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
1552 $dt, $user->flags, xcape($user->name), xcape($user_mesg));
1553 $user_cur->step_inc();
1555 log_legal($curtime, $user->ip, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
1559 function table_wakeup($user)
1561 $table = &$this->table[0];
1563 log_main("BIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
1567 log_main("BIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1569 for ($i = 0 ; $i < $table->player_n ; $i++) {
1570 $user_cur = &$this->user[$i];
1571 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1573 if ($user_cur == $user)
1574 $user_cur->subst = "shutdowner";
1576 $user_cur->subst = "shutdowned";
1577 $user_cur->laccwr = $curtime;
1579 $ret = "gst.st = ".($user_cur->step+1)."; ";
1580 $ret .= 'gst.st_loc++; xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
1582 log_wr($user_cur->sess." BIN5_WAKEUP: ".$ret);
1583 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1584 $user_cur->step_inc();
1587 $this->the_end = TRUE;
1591 * If all players are freezed the room garbage_manager clean up table and users.
1593 function is_abandoned()
1598 $table = &$this->table[0];
1600 for ($i = 0 ; $i < $table->player_n ; $i++) {
1601 $user_cur = &$this->user[$i];
1603 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
1612 static function request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
1614 GLOBAL $G_ban_list, $G_black_list;
1616 printf("NEW_SOCKET (root): %d\n", intval($new_socket));
1618 $enc = get_encoding($header);
1619 if (isset($header['User-Agent'])) {
1620 if (strstr($header['User-Agent'], "MSIE")) {
1621 $transp_type = "htmlfile";
1624 $transp_type = "xhr";
1628 $transp_type = "iframe";
1630 force_no_cache($header_out);
1632 if (($table_idx = gpcs_var('table_idx', $get, $post, $cookie)) === FALSE)
1635 if (($table_token = gpcs_var('table_token', $get, $post, $cookie)) === FALSE)
1636 unset($table_token);
1643 bin5_index_main($transp_type, $header_out, $addr, $get, $post, $cookie);
1644 $content = ob_get_contents();
1647 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1651 case "index_wr.php":
1652 if (isset($table_idx) && isset($table_token)) {
1653 if (($bri = $s_a_p->app->match_get($table_idx, $table_token)) != FALSE) {
1655 bin5_index_wr_main($bri, $addr, $get, $post, $cookie);
1656 $content = ob_get_contents();
1660 $content = "Bin5 Load data error";
1664 $content = "Bin5 Load data error";
1666 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1670 case "index_rd.php":
1671 if (($transp = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
1673 if ($transp == 'websocket')
1677 if (!isset($table_idx)
1678 || !isset($table_token)
1679 || !isset($cookie['sess'])
1680 || ($bri = $s_a_p->app->match_get($table_idx, $table_token)) == NULL
1681 || (($user = $bri->get_user($cookie['sess'], $idx)) == FALSE)) {
1683 $content = Bin5_user::stream_fini($transp_type, $s_a_p->rndstr, TRUE);
1684 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1689 $bri->brisk->sess_cur_set($user->sess);
1691 // close a previous opened index_read_ifra socket, if exists
1692 if (($prev = $user->rd_socket_get()) != NULL) {
1693 $s_a_p->socks_unset($user->rd_socket_get());
1694 fclose($user->rd_socket_get());
1695 printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
1696 $user->rd_socket_set(NULL);
1700 $user->stream_init($s_a_p->rndstr, $enc, $header, $header_out, $content, $get, $post, $cookie);
1702 $response = headers_render($header_out, -1).$user->chunked_content($content);
1703 $response_l = mb_strlen($response, "ASCII");
1705 $wret = @fwrite($new_socket, $response, $response_l);
1706 if ($wret < $response_l) {
1707 printf("TROUBLES WITH FWRITE: %d\n", $wret);
1708 $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
1711 $user->rd_cache_set("");
1713 fflush($new_socket);
1716 $s_a_p->socks_set($new_socket, $user, NULL);
1717 $user->rd_socket_set($new_socket);
1718 printf(" - qui ci siamo - ");
1737 function locshm_exists($tok)
1741 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
1742 log_main($tok." SHM NOT exists");
1748 log_main($tok." SHM exists");
1757 is_transition (is from room to table ?)
1758 is_again (is another game)
1760 Examples of $is_transition, $is_again:
1761 from reload of the page: FALSE, FALSE
1762 from sitdown in room: TRUE, FALSE
1763 from table: asta cmd e tutti passano: TRUE, TRUE
1764 from table: fine partita: TRUE, TRUE
1766 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1768 $table_idx = $user->table;
1769 $table = $bri->table[$table_idx];
1770 $table_pos = $user->table_pos;
1772 $ret = "table_init();";
1773 $ret .= $table->exitlock_show(&$bri->user, $table_pos);
1775 /* GENERAL STATUS */
1776 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1777 $sendstep, $user->stat, $user->subst, $table_pos);
1779 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1782 $ret .= "background_set();";
1785 $ret .= $user->myname_innerHTML();
1786 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1787 $bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->flags,
1788 xcape($bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->name),
1790 $bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->flags,
1791 xcape($bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->name),
1793 $bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->flags,
1794 xcape($bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->name),
1796 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->flags),
1797 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->name)),
1799 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->flags),
1800 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->name)));
1802 /* NOTIFY FOR THE CARD MAKER */
1803 if ($is_transition) { // && $user->subst == "asta" superfluo
1804 $ret .= show_table_info(&$bri, &$table, $table_pos);
1805 $ret .= "setTimeout(preload_images, 500, g_preload_img_arr, g_imgct);";
1808 $ret .= "\$('imgct').innerHTML = \$('imgct').innerHTML = mlang_commons['imgload_a'][g_lang]+\"100%.\";";
1811 $ret .= table_welcome($user);
1813 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1814 $ret .= playsound("cow.mp3");
1819 if ($is_transition) { // && $user->subst == "asta" superfluo
1822 for ($i = 0 ; $i < BIN5_CARD_HAND ; $i++) {
1823 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++) {
1825 for ($o = 0 ; $o < (BIN5_CARD_HAND * BIN5_PLAYERS_N) && $ct < $i+1 ; $o++) {
1826 // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1827 if ($table->card[$o]->owner == (($e + $table->gstart) % BIN5_PLAYERS_N)) {
1833 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1835 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BIN5_PLAYERS_N,
1836 $i, ((($e + BIN5_PLAYERS_N - $table_pos + $table->gstart) % BIN5_PLAYERS_N) == 0 ?
1837 $table->card[$o]->value : -1),
1838 ($i == 7 && $e == (BIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1843 $taked = array(0,0,0,0,0);
1844 $inhand = array(0,0,0,0,0);
1845 $ontabl = array(-1,-1,-1,-1,-1);
1848 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1849 // for ($i = 0 ; $i < 40 ; $i++) {
1850 if ($table->card[$i]->stat == 'hand') {
1851 if ($table->card[$i]->owner == $table_pos) {
1852 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1854 $inhand[$table->card[$i]->owner]++;
1856 else if ($table->card[$i]->stat == 'take') {
1857 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1858 $taked[$table->card[$i]->owner]++;
1860 else if ($table->card[$i]->stat == 'table') {
1861 $ontabl[$table->card[$i]->owner] = $i;
1865 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1866 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1868 log_main("Stat table: ".$logg);
1870 /* Set ours cards. */
1872 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1873 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1874 for ($i = $inhand[$table_pos] ; $i < BIN5_CARD_HAND ; $i++)
1875 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1876 $ret .= sprintf('card_setours(%s);', $oursarg);
1878 /* Dispose all cards */
1879 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1880 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1881 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1882 ($inhand[$i] <= BIN5_CARD_HAND ? $inhand[$i] : BIN5_CARD_HAND), $taked[$i]);
1884 if ($ontabl[$i] != -1) {
1885 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1886 $table->card[$ontabl[$i]]->value,
1887 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1893 if ($user->subst == 'asta') {
1895 /* show users auction status */
1897 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1898 $user_cur = &$bri->user[$table->player[$i]];
1899 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1900 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1902 if (BIN5_PLAYERS_N == 3)
1903 $showst .= ",-2,-2";
1904 $ret .= sprintf('document.title = "Brisk - Tavolo %d (asta)";', $user->table_orig);
1905 $ret .= sprintf('show_astat(%s);', $showst);
1907 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1908 /* show card chooser */
1909 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1914 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N) &&
1915 $table->asta_win == -1)
1916 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1917 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1919 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1920 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1924 if ($table->asta_win == -1) { // auction case
1925 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N))
1926 $ret .= "remark_on();";
1928 $ret .= "remark_off();";
1930 else { // chooseed case
1931 if ($table_pos == $table->asta_win)
1932 $ret .= "remark_on();";
1934 $ret .= "remark_off();";
1937 else if ($user->subst == 'game') {
1939 if (($table->gstart + $table->turn) % BIN5_PLAYERS_N == $table_pos)
1940 $ret .= "is_my_time = true; remark_on();";
1942 $ret .= "remark_off();";
1944 /* WHO CALL AND WHAT */
1945 $ret .= briscola_show($bri, $table, $user);
1949 } // end function show_table(...
1951 function calculate_winner(&$table)
1958 $cur_seed = $table->briscola - ($table->briscola % 10);
1960 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1961 // for ($i = 0 ; $i < 40 ; $i++) {
1962 if ($table->card[$i]->stat != "table")
1965 log_wr(sprintf("Card On table: [%d]", $i));
1967 $v = $table->card[$i]->value;
1968 $ontab[$table->card[$i]->owner] = $v;
1969 $ontid[$table->card[$i]->owner] = $i;
1970 /* se briscola setto il flag */
1971 if (($v - ($v % 10)) == $cur_seed)
1975 if ($briontab == FALSE) {
1976 $cur_win = $table->gstart;
1977 $cur_val = $ontab[$cur_win];
1978 $cur_seed = $cur_val - ($cur_val % 10);
1981 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1982 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1983 if ($ontab[$i] < $cur_val) {
1984 $cur_val = $ontab[$i];
1990 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1991 $table->card[$ontid[$i]]->owner = $cur_win;
1992 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1997 function show_table_info(&$bri, &$table, $table_pos)
1999 GLOBAL $G_lang, $mlang_bin5_bin5;
2007 $user = $bri->user[$table->player[$table_pos]];
2009 // TAG: POINTS_MANAGEMENT
2010 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
2011 $noty = sprintf('<table class=\"points\"><tr><th></th>');
2014 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++)
2015 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
2016 $noty .= sprintf("</tr>");
2019 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
2021 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
2022 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
2023 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
2024 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
2029 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
2030 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
2031 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
2032 $noty .= "</tr></table>";
2034 if ($table->old_reason != "") {
2035 $noty .= sprintf($mlang_bin5_bin5['info_last'][$G_lang],
2036 $tg_br, $tg_hr, $tg_bo, $tg_bc);
2038 $noty .= $table->old_reason;
2041 $noty .= sprintf($mlang_bin5_bin5['info_curr'][$G_lang],
2042 $tg_br, $tg_hr, $tg_bo, $tg_bc);
2044 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
2045 if ($table->mazzo == $table_pos)
2046 $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
2048 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
2049 $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
2052 if ($user->subst == 'asta') {
2053 if ($table->asta_win == -1) // auction case
2054 $curplayer = $table->gstart % BIN5_PLAYERS_N;
2056 $curplayer = $table->asta_win;
2058 else if ($user->subst == 'game') {
2059 $curplayer = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
2062 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
2063 if ($curplayer == $table_pos) {
2064 $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
2067 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
2068 $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
2071 $multer = $table->rules->multer(TRUE);
2073 $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($multer) );
2076 if ($table->match_id != -1) {
2077 $noty .= sprintf($mlang_bin5_bin5['info_match'][$G_lang], $table->match_id);
2080 $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
2081 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
2082 * 130 + ($table->points_n > 0 ? 50 : 0) +
2083 * (120 * ($table->points_n / MAX_POINTS)));
2084 * will be used when we refact notify js function following
2085 * photoo class logic
2091 function table_welcome($user)
2093 GLOBAL $table_wellarr, $G_lang;
2096 for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
2097 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
2103 function briscola_show($bri, $table, $user)
2105 GLOBAL $G_lang, $mlang_bin5_bin5;
2109 if ($table->asta_card == 9)
2110 $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
2112 /* text of caller cell */
2113 if ($user->table_pos == $table->asta_win) {
2114 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
2115 $ret .= sprintf($prestr, $ptnadd);
2118 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
2119 $ret .= sprintf($prestr,
2120 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
2122 $ret .= sprintf('set_iscalling(%d);', ($table->asta_win - $user->table_pos + BIN5_PLAYERS_N) % BIN5_PLAYERS_N);
2124 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
2126 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
2127 $ret .= sprintf('$("caller").style.visibility = "visible";');
2128 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
2129 $ret .= sprintf('$("astalascio").style.visibility = "";');
2130 $ret .= sprintf('$("asta").style.visibility = "hidden";');
2131 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
2137 function game_result_new($asta_pnt, $pnt)
2139 if ($pnt >= $asta_pnt)
2146 function log_points($remote_addr, $curtime, $user, $where, $mesg)
2148 if (($fp = @fopen(LEGAL_PATH."/points.log", 'a')) != FALSE) {
2149 /* Unix time | session | nickname | IP | where was | mesg */
2150 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
2151 ($user->is_auth() ? 'A' : 'N'),
2152 $user->name, $remote_addr, $where , $mesg));