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 $mlang_bin5_bin5 = array(
39 // br, hr, b, /b, win, fri
40 'info_last' => array( 'it' => '%3$sUltima mano%4$s',
41 'en' => '%3$sLast hand%4$s'),
42 'info_curr' => array( 'it' => '%3$sMano corrente%4$s',
43 'en' => '%3$sCurrent hand%4$s'),
44 // br, hr, b, /b, win, fri
45 'info_part' => array( 'it' => 'Ha chiamato %3$s%5$s%4$s, il socio era %3$s%6$s%4$s,%1$s',
46 'en' => 'The declarer was %3$s%5$s%4$s, the partner was %3$s%6$s%4$s,%1$s'),
48 'info_capp' => array( 'it' => 'hanno fatto %3$scappotto%4$s EBBRAVI!.%1$s',
49 'en' => 'and they made %3Dscapot%4$s WELL DONE!.%1$s'),
51 // br, hr, b, /b, old_asta_pnt, old_pnt, winornot
52 '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',
53 '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'),
54 // br, hr, b, /b, old_pnt, winornot
55 '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',
56 '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'),
57 'info_win' => array( 'it' => 'vinto',
59 'info_peer' => array( 'it' => 'pareggiato',
61 'info_lost' => array( 'it' => 'perso',
64 // br, hr, b, /b, win_name
65 'info_alon' => array( 'it' => '%3$s%5$s%4$s si è chiamato in mano,%1$s',
66 'en' => '%3$s%5$s%4$s play alone against each other,%1$s'),
67 // br, hr, b, /b, old_asta_pnt, old_pnt, winornot
68 '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',
69 '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'),
70 // br, hr, b, /b, old_pnt, winornot
71 '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',
72 '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'),
74 'info_acap' => array( 'it' => 'ha fatto %3$scappotto%4$s EBBRAVO!.%1$s',
75 'en' => 'and he/she made %3$scapot%4$s WELL DONE!.%1$s'),
78 'info_omul' => array( 'it' => '%1$sLa partita valeva %3$s%5$s%4$s.%1$s',
79 'en' => '%1$sEN: The game was worth %3$s%5$s%4$s.%1$s' ),
82 'info_alpa' => array( 'it' => '%1$sHanno passato %3$stutti%4$s.%1$s',
83 'en' => '%1$sEN: Hanno passato %3$stutti%4$s.%1$s' ),
85 // br, hr, b, /b, aband-name
86 'info_aban' => array( 'it' => ' Ha lasciato %3$s%5$s%4$s perché aveva al massimo %3$s2 punti%4$s.',
87 'en' => ' EN: Ha lasciato %3$s%5$s%4$s perché aveva al massimo %3$s2 punti%4$s.'),
89 'info_shuf' => array( 'it' => 'Il mazzo a <b>%s</b>, ',
90 'en' => '<b>%s</b> shuffled the cards, '),
91 'info_yturn'=> array( 'it' => ' tocca a <b>te</b> giocare.',
92 'en' => ' it\'s <b>your</b> turn.'),
93 'info_turn' => array( 'it' => 'tocca a <b>%s</b> giocare.',
94 'en' => 'it\'s the <b>%s</b>\'s turn.'),
95 'info_mult' => array( 'it' => ' La partita vale <b>%s</b>.',
96 'en' => ' The game worth <b>%s</b>.' ),
97 'info_yshuf'=> array( 'it' => 'Fai <b>tu</b> il mazzo, ',
98 'en' => 'It\'s <b>your</b> shuffled the cards, '),
99 'info_match'=> array( 'it' => 'Il codice della partita è <b>%d</b>.',
100 'en' => 'Match code is <b>%d</b>.'),
101 'btn_bkgame'=> array( 'it' => 'torna alla partita',
102 'en' => 'back to the game'),
103 'call_wptn' => array( 'it' => '<br>con %d punti',
104 'en' => '<br>with %d points'),
105 'call_ycall'=> array( 'it' => 'Chiami%s:',
107 'call_call' => array( 'it' => 'Chiama %s%s:',
108 'en' => 'The declarer is %s%s:')
112 $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.'),
113 '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.') );
115 function multoval($mult)
120 return ($G_lang == 'en' ? 'double' : 'doppio');
122 return ($G_lang == 'en' ? 'triple' : 'triplo');
124 return ($G_lang == 'en' ? 'quadruple' : 'quadruplo');
126 return (sprintf(($G_lang == 'en' ? "%d-ple" : "%d-plo"), $mult));
131 var $value; /* 0 - 39 card value */
132 var $stat; /* 'bunch', 'hand', 'table', 'take' */
133 var $owner; /* (table position 0-4) */
134 // var $pos; /* Pos in hand. */
135 var $x; /* When played the X position on the table of the owner. */
136 var $y; /* When played the Y position on the table of the owner. */
138 function Card($value, $stat, $owner)
140 $this->value = $value;
141 $this->stat = $stat; // Card stat
142 $this->owner = $owner;
145 function assign($stat,$owner)
147 $this->stat = $stat; // Card stat
148 $this->owner = $owner;
151 function setpos($pos)
158 $this->stat = 'table'; // Card stat
163 function take($newown)
165 $this->stat = 'take'; // Card stat
166 $this->owner = $newown;
170 class Bin5_table extends Table {
171 var $card; // il mazzo di carte
172 var $mazzo; // chi e' di mazzo
182 var $points; // points array
183 var $points_n; // number of row of points
186 var $asta_win; // the caller idx position at table
188 var $friend; // the callee idx position at table
190 var $match_id; // the id of the match on the database (-1 == just not saved)
192 var $old_act; // last action that trigs the end of the game
197 var $old_pnt; // points made by caller and callee
198 var $old_asta_win; // the old caller idx position at table
199 var $old_friend; // the old callee idx position at table
201 function Bin5_table()
207 function create($idx)
209 if (($thiz =& new Bin5_table()) == FALSE)
215 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
216 $thiz->asta_pla_n= -1;
217 $thiz->asta_card = -1;
218 $thiz->asta_pnt = -1;
221 $thiz->points = array( );
223 $thiz->total = array( 0, 0, 0, 0, 0);
224 $thiz->asta_win = -1;
225 $thiz->briscola = -1;
229 $thiz->match_id = -1;
232 $thiz->old_mazzo = -1;
233 $thiz->old_reason = "";
234 $thiz->old_asta_pnt = -1;
235 $thiz->old_mult = -1;
237 $thiz->old_asta_win = -1;
238 $thiz->old_friend = -1;
245 function myclone(&$from)
247 if (($thiz =& new Bin5_table()) == FALSE)
252 $thiz->card = $from->card;
253 $thiz->mazzo = $from->mazzo; // REVIEW
254 $thiz->gstart = $from->gstart;
255 $thiz->turn = $from->turn;
257 $thiz->asta_pla = $from->asta_pla;
258 $thiz->asta_pla_n = $from->asta_pla_n;
259 $thiz->asta_card = $from->asta_card;
260 $thiz->asta_pnt = $from->asta_pnt;
262 $thiz->mult = $from->mult;
263 $thiz->points = $from->points;
264 $thiz->points_n = $from->points_n;
265 $thiz->total = $from->total;
267 $thiz->asta_win = $from->asta_win;
268 $thiz->briscola = $from->briscola;
269 $thiz->friend = $from->friend;
271 $thiz->match_id = $from->match_id;
273 $thiz->old_act = $from->old_act;
274 $thiz->old_mazzo = $from->old_mazzo;
275 $thiz->old_reason = $from->old_reason;
276 $thiz->old_asta_pnt = $from->old_asta_pnt;
277 $thiz->old_mult = $from->mult;
278 $thiz->old_pnt = $from->old_pnt;
279 $thiz->old_asta_win = $from->old_asta_win;
280 $thiz->old_friend = $from->old_friend;
286 function parentcopy(&$from)
291 function spawn(&$from)
293 if (($thiz =& new Bin5_table()) == FALSE)
296 $thiz->parentcopy($from);
298 log_main("PLAYER_N - spawn.".$thiz->player_n);
300 $thiz->card = array();
301 $thiz->bunch_create();
302 $thiz->mazzo = rand(0,PLAYERS_N-1);
303 $thiz->points = array();
304 $thiz->total = array();
308 $thiz->match_id = -1;
310 $thiz->old_asta_win = -1;
311 $thiz->old_reason = "";
313 // players are rearranged in an dedicated array
314 $thiz->player = array();
315 for ($i = 0 ; $i < $from->player_n ; $i++)
316 $thiz->player[$i] = $i;
318 log_main("TABLE_OLD_WIN - spawn:".$thiz->old_asta_win);
323 static function asta2mult($asta_pnt)
327 else if ($asta_pnt > 100)
329 else if ($asta_pnt > 90)
331 else if ($asta_pnt > 80)
333 else if ($asta_pnt > 70)
339 function multer($is_new)
342 // return (pow(2, $this->mult) * $this->asta2mult($this->asta_pnt));
343 return (static::s_multer($this->mult, $this->asta_pnt));
346 // return (pow(2, $this->old_mult) * $this->asta2mult($this->old_asta_pnt));
347 return (static::s_multer($this->old_mult, $this->old_asta_pnt));
351 static function s_multer($mult, $pnt)
353 return (pow(2, $mult) * static::asta2mult($pnt));
356 // function bunch_create_old() function AND
360 // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
361 // // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
362 // $ret[$i] =& new Card($i, 'bunch', 'no_owner');
369 function bunch_create()
373 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
374 $this->card[$i] = new Card($i, 'bunch', 'no_owner');
378 function bunch_make()
380 log_main("bunch_make start");
381 $ct = array(0,0,0,0,0);
383 mt_srand(make_seed());
385 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--)
388 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--) {
392 log_main("RND ZERO");
396 $owner = $i % BIN5_PLAYERS_N;
397 $this->card[$id]->assign('hand', $owner);
399 $rest[$rn] = $rest[$i];
400 // $pubbpos[$rn2] = $pubbpos[$i];
402 log_main("bunch_make end");
405 function init($userarr)
408 $this->mazzo = rand(0,PLAYERS_N-1);
411 $this->old_asta_win = -1;
412 $this->old_reason = "";
414 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
415 $this->total[$i] = 0;
416 $user_cur = $userarr[$this->player[$i]];
417 $user_cur->stat_set('table');
418 $user_cur->exitislock = TRUE;
421 log_main("table::init: ci siamo");
424 function game_init($userarr)
428 $this->gstart = ($this->mazzo+1) % BIN5_PLAYERS_N;
431 $this->asta_pla_n = BIN5_PLAYERS_N;
432 $this->asta_card = -1;
433 $this->asta_pnt = 60;
434 $this->asta_win = -1;
435 $this->briscola = -1;
439 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
440 $this->asta_pla[$i] = TRUE;
441 $user_cur = $userarr[$this->player[$i]];
442 $user_cur->subst = 'asta';
443 $user_cur->asta_card = -2;
444 $user_cur->asta_pnt = -1;
445 $user_cur->handpt = $this->hand_points($i);
450 function game_next($delta)
452 $this->old_mazzo = $this->mazzo;
453 $this->mazzo = ($this->mazzo + $delta) % BIN5_PLAYERS_N;
456 function mult_inc($val)
458 $this->old_mult = $this->mult;
462 function mult_set($val)
464 $this->old_mult = $this->mult;
468 function hand_points($idx)
470 GLOBAL $G_all_points;
474 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
475 // for ($i = 0 ; $i < 40 ; $i++) {
476 if ($this->card[$i]->owner != $idx)
479 $ctt = $this->card[$i]->value % 10;
480 $tot += $G_all_points[$ctt];
487 function exitlock_show($userarr, $table_pos)
489 $ct = $this->exitlock_calc($userarr, $table_pos);
491 $ret = sprintf('exitlock_show(%d, %s);', $ct,
492 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
496 function exitlock_calc(&$userarr, $table_pos)
500 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
501 if ($userarr[$this->player[$i]]->exitislock == FALSE)
508 function rules_engine(&$bri, $curtime, $action, $user)
510 GLOBAL $G_all_points, $G_dbasetype;
514 $this->old_act = $action;
515 if ($action == BIN5_RULES_ALLPASS) { // return TRUE if all correct
516 $this->old_asta_win = -1;
519 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
524 // $this->game_next(1);
525 $this->game_init(&$bri->user);
527 else if ($action == BIN5_RULES_ABANDON) { // return TRUE if all correct
528 log_wr(sprintf("GIOCO FINITO !!!"));
529 $this->old_asta_win = $user->table_pos;
533 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
537 // Non si cambia mazzo se si abbandona la partita
539 // $this->game_next(0);
540 $this->game_init(&$bri->user);
542 else if ($action == BIN5_RULES_FINISH) { // return TRUE if all correct
546 if ($this->asta_pnt == 60)
547 $this->asta_pnt = 61;
549 $this->old_reason = "";
551 // count points for the temporary 2 teams
552 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
553 $ctt = $this->card[$i]->value % 10;
554 $own = $this->card[$i]->owner;
555 if ($own == $this->asta_win || $own == $this->friend)
556 $pro += $G_all_points[$ctt];
559 log_wr(sprintf("PRO: [%d]", $pro));
562 if ($this->asta_pnt == 61 && $pro == 60) {
563 $this->points[$this->points_n % MAX_POINTS] = array();
564 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
565 $this->points[$this->points_n % MAX_POINTS][$i] = 0;
569 $this->old_pnt = $pro;
576 if ($pro >= $this->asta_pnt)
581 $this->points[$this->points_n % MAX_POINTS] = array();
582 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
583 if ($i == $this->asta_win)
584 $pt = ($i == $this->friend ? 4 : 2);
585 else if ($i == $this->friend)
590 log_wr(sprintf("PRO: pt[%d][%d] = %d", $this->points_n % MAX_POINTS, $i, $pt));
592 $pt = $pt * $sig * $this->multer(TRUE) * ($pro == 120 ? 2 : 1);
594 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $this->multer(TRUE), ($pro == 120 ? 2 : 1)));
596 $this->points[$this->points_n % MAX_POINTS][$i] = $pt;
597 $this->total[$i] += $pt;
601 $this->old_pnt = $pro;
602 $this->old_asta_win = $this->asta_win;
612 $this->game_next($game_delta);
614 $plist = "$this->table_token|$user->table_orig|$this->player_n";
617 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
618 $user_cur = &$bri->user[$this->player[$i]];
621 $ucodes[$i] = $user_cur->code_get();
624 $plist .= '|'.xcapelt($user_cur->name).'|'.$pts[$i];
625 $codes .= '|'.xcapelt($user_cur->code_get());
628 log_legal($curtime, $user->ip, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
630 $this->old_asta_pnt = $this->asta_pnt;
631 // $this->old_mazzo is managed by ->game_next();
632 // $this->old_mult, $this->old_pnt, $this->old_reason and $this->old_asta_win are specific
634 $this->old_friend = $this->friend;
635 $this->old_reason = game_description($action, 'html', $this->old_mult,
636 $this->old_asta_win, $bri->user[$this->player[$this->old_asta_win]]->name,
637 $this->old_friend, $bri->user[$this->player[$this->old_friend]]->name,
638 $this->old_pnt, $this->old_asta_pnt);
641 if ($user->table_orig < TABLES_AUTH_N) {
642 require_once("../Obj/dbase_".$G_dbasetype.".phh");
644 if (($bdb = BriskDB::create()) != FALSE) {
645 $bdb->bin5_points_save($curtime, $this, $user->table_orig, $action, $ucodes, $pts);
649 log_points($remote_addr, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED");
651 log_points($remote_addr, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
654 $this->game_init(&$bri->user);
659 function match_continue(&$bri, $user, $match_id_s)
665 /* - verify if match_id and user are both valid to accept
666 the match_continue request - */
667 $match_id = (int)$match_id_s;
668 if ($match_id <= 0) {
669 $msg = "questa partita non esiste";
673 if ($user->continue_get() == BIN5_USER_CONTINUE_ALREADY) {
674 $msg = "Hai già richiesto di continuare una partita.";
678 if ($user->continue_get() == $match_id) {
679 $msg = "Hai già richiesto di continuare questa partita.";
682 // retrieves users list for this match
683 $match_data = array();
684 if (($bdb = BriskDB::create()) != FALSE) {
685 // match_order_get return FALSE for old matches
686 $ucodes = $bdb->match_order_get($match_data, $match_id, BIN5_PLAYERS_N);
689 if ($ucodes == FALSE) {
690 $msg = "questa partita non è stata memorizzata correttamente";
694 // if current user code must be in the users list
695 if (array_search($user->code, $ucodes) === FALSE) {
696 $msg = sprintf("Questo utente non compare nella partita che si vuole continuare [%d].", $user->code);
700 /* - the user is in the list, add the match_id to his profile
701 and check if he is the N'th to require continue - */
703 // set the match_id for the current user
704 $user->continue_set($match_id);
706 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
707 $user_cur = &$bri->user[$this->player[$i]];
708 if ($user_cur->continue_get() != $match_id) {
713 // not all players set the continue match than we exit
714 if ($i < BIN5_PLAYERS_N) {
715 $msg = sprintf("<b>L'utente <i>%s</i> vorrebbe continuare la partita n° %d.</b>",
716 xcape($user->name), $match_id);
720 /* - all users decide to continue the same match, update all infos and rearrange users
721 to the right positions on the table - */
723 /* reset users table order */
724 for ($i = 0 ; $i < BIN5_PLAYERS_N - 1 ; $i++) {
725 if ($bri->user[$this->player[$i]]->code == $ucodes[$i]) {
728 for ($e = $i + 1 ; $e < BIN5_PLAYERS_N ; $e++) {
729 if ($bri->user[$this->player[$e]]->code == $ucodes[$i]) {
730 $swap = $this->player[$i];
731 $this->player[$i] = $this->player[$e];
732 $this->player[$e] = $swap;
733 $bri->user[$this->player[$i]]->table_pos = $i;
734 $bri->user[$this->player[$e]]->table_pos = $e;
738 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
739 fprintf(STDERR, "USERZ: [%s]\n", $bri->user[$this->player[$i]]->name);
742 // update database info to be aligned with current table (ttok and table_idx
743 if (($bdb = BriskDB::create()) != FALSE) {
744 if ($bdb->match_continue($match_id, $this, $user->table_orig) == FALSE) {
746 $msg = "aggiornamento dei dati della partita fallito";
752 /* bunch and multiplier status set */
753 $this->mazzo = $match_data['mazzo_next'];
754 $this->mult = $match_data['mult_next'];
755 $this->match_id = $match_id;
756 $this->game_init(&$bri->user);
758 /* reload of the page with the new layout */
759 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
760 $user_cur = &$bri->user[$this->player[$i]];
761 $user_cur->continue_set(BIN5_USER_CONTINUE_ALREADY);
763 $user_cur->trans_step = $user_cur->step + 1;
764 $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);
765 $user_cur->step_inc();
767 // a void command force xynt-streamer to flush all data to client
768 $user_cur->trans_step = $user_cur->step + 1;
769 $user_cur->comm[$user_cur->step % COMM_N] = "";
770 $user_cur->step_inc();
772 $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$bri, &$user_cur, $user_cur->step+1, TRUE, FALSE);
773 $user_cur->step_inc();
778 $dt = date("H:i ", $curtime);
779 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
780 if ($ret == FALSE && $this->player[$i] != $user->idx)
782 $user_cur = &$bri->user[$this->player[$i]];
783 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
784 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");',
785 $dt, NICKSERV, $msg);
786 $user_cur->step_inc();
790 } // end class Bin5_table
795 define('BIN5_USER_FLAG_RING_ENDAUCT', 0x01);
796 define('BIN5_USER_CONTINUE_INIT', -1);
797 define('BIN5_USER_CONTINUE_ALREADY', -2);
798 class Bin5_user extends User {
801 var $handpt; // Total card points at the beginning of the current hand.
802 var $exitislock; // Player can exit from the table ?
803 var $privflags; // Flags for briskin5 only
805 var $continue; // Id of the match that the user would continue
813 function create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
814 if (($thiz =& new User()) == FALSE)
817 $thiz->asta_card = -2;
818 $thiz->asta_pnt = -1;
820 $thiz->exitislock = TRUE;
821 $thiz->privflags = 0;
822 $thiz->continue = BIN5_USER_CONTINUE_INIT;
828 function parentcopy(&$from)
833 function copy(&$from)
835 $this->parentcopy($from);
837 $this->asta_card = $from->asta_card;
838 $this->asta_pnt = $from->asta_pnt;
839 $this->handpt = $from->handpt;
840 $this->exitislock = $from->exitislock;
841 $this->privflags = $from->privflags;
842 $this->continue = $from->continue;
846 function myclone(&$from)
848 if (($thiz =& new User()) == FALSE)
857 static function spawn($from, &$bri, $table, $table_pos, $get, $post, $cookie)
859 if (($thiz = new Bin5_user()) == FALSE)
862 if (($CO_bin5_pref_ring_endauct = gpcs_var("CO_bin5_pref_ring_endauct", $get, $post, $cookie)) === FALSE) {
863 $CO_bin5_pref_ring_endauct = "";
866 $thiz->parentcopy($from);
868 /* NOTE: at this moment idx and table_pos fields have the same value
869 but diffentent functions, we keep them separated for a while */
871 $thiz->idx = $table_pos; // it is the position in the mini-room,
872 // not related to table pos (see below)
873 $thiz->asta_card = -2;
874 $thiz->asta_pnt = -1;
876 $thiz->exitislock = TRUE;
877 $thiz->continue = BIN5_USER_CONTINUE_INIT;
879 log_wr("Bin5 constructor");
881 $thiz->privflags = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
883 $thiz->table_orig = $table;
885 $thiz->table_pos = $table_pos;
892 function step_set($step)
894 $this->step = $step & 0x7fffffff;
899 function step_inc($delta = 1) {
900 $this->step += $delta;
901 /* modularization because unpack() not manage unsigned 32bit int correctly */
902 $this->step &= 0x7fffffff;
907 static function load_step($tab_id, $sess)
911 if (validate_sess($sess) == FALSE)
914 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
915 mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
916 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step", 'rb')) == FALSE)
918 if (($s = fread($fp, 8)) == FALSE)
920 if (mb_strlen($s, "ASCII") != 8)
922 $arr = unpack('Ls/Li', $s);
925 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
932 log_rd2("STEP_GET [".$sess."]: return false ");
940 if (validate_sess($this->sess) == FALSE)
942 if (file_exists(BIN5_PROXY_PATH."/table".$this->table_orig) == FALSE)
943 mkdir(BIN5_PROXY_PATH."/table".$this->table_orig, 0775, TRUE);
944 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$this->table_orig."/".$this->sess.".step", 'w')) == FALSE)
946 fwrite($fp, pack("LL",$this->step, $this->idx));
949 log_main("step_set [".$this->sess. "] [".$this->step."]");
957 static function unproxy_step($tab_id, $sess)
959 log_rd2("UNPROXY: ".BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
960 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
963 @unlink(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
966 function destroy_data($tab_id)
969 if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos, "B")) == -1) {
970 log_crit("BIN5 USER DATA REMOVE FAILED 1 [".FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos."]");
974 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
975 log_crit("BIN5 USER DATA REMOVE FAILED 2");
978 if (shmop_delete($shm) == 0) {
979 log_crit("BIN5 USER DATA REMOVE FAILED 3");
984 log_main("BIN5 USER DATA DESTROY SUCCESS");
986 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
996 static function blocking_error($is_unrecoverable)
998 log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
999 return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
1002 protected function page_sync($sess, $page)
1004 log_rd2("PAGE_SYNC");
1005 printf("xXx BIN5_USER::PAGE_SYNC\n");
1006 return (sprintf('xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
1009 protected function maincheck($cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate, $table_idx, $table_token)
1011 GLOBAL $G_lang, $mlang_indrd;
1012 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
1013 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
1014 $CO_splashdate = "CO_splashdate".$G_splash_idx;
1015 $$CO_splashdate = $splashdate;
1017 GLOBAL $S_load_stat;
1019 log_rd("maincheck begin");
1024 /* Nothing changed, return. */
1025 if ($cur_step == $this->step)
1028 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$this->stat."] cur_step[".$cur_step."] user_step[".$this->step."]");
1030 if ($cur_step == -1) {
1032 * if $cur_step == -1 load the current state from the main struct
1035 $S_load_stat['wR_minusone']++;
1037 // if ($this->the_end == TRUE) {
1038 // log_rd2("main_check: the end".var_export(debug_backtrace()));
1041 if ($this->trans_step != -1) {
1042 log_rd2("TRANS USATO ".$this->trans_step);
1043 $cur_step = $this->trans_step;
1044 $this->trans_step = -1;
1047 log_rd2("TRANS NON ATTIVATO");
1052 /* this part I suppose is read only on $this->room structure */
1053 if ($cur_step == -1) {
1054 log_rd2("PRE-NEWSTAT");
1061 if ($this->stat == 'table') {
1063 /* NOTE: $this->room is associated with the current $bri object */
1064 printf("xXx CLASS NAME [%s]\n", get_class($this->room));
1065 $ret = show_table(&$this->room, $this, $this->step, FALSE, FALSE);
1067 log_rd2("NEWSTAT: ".$this->stat);
1069 $new_stat = $this->stat;
1070 $new_subst = $this->subst;
1071 $new_step = $this->step;
1072 } /* if ($cur_step == -1) { */
1074 /* $sem = Room::lock_data(FALSE); */
1075 $S_load_stat['rU_heavy']++;
1077 if ($cur_step < $this->step) {
1079 if ($cur_step + COMM_N < $this->step) {
1080 if (($cur_stat != $this->stat)) {
1081 $to_stat = $this->stat;
1082 /* Room::unlock_data($sem); */
1084 printf("xXx BIN5_USER::MAINCHECK\n");
1085 return ($this->page_sync($this->sess, ($to_stat == "table" ? "index.php" : "../index.php"), $this->table, $this->table_token));
1087 log_rd2("lost history, refresh from scratch");
1088 printf("xXx LOST HISTORY!\n");
1092 for ($i = $cur_step ; $i < $this->step ; $i++) {
1094 if ($this->comm[$ii] == "") {
1095 if ($i == $cur_step)
1100 log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
1101 $ret .= $this->comm[$ii];
1103 $new_stat = $this->stat;
1104 $new_subst = $this->subst;
1105 $new_step = $this->step;
1108 log_rd2($this->step, 'index_rd.php: after ret set');
1110 if ($this->the_end == TRUE) {
1111 log_rd2("LOGOUT BYE BYE!!");
1112 log_auth($this->sess, "Explicit logout.");
1114 if ($this->the_end == TRUE) {
1118 /* if ($this->subst == 'sitdown') { */
1119 /* log_load("ROOM WAKEUP"); */
1120 /* $this->room->room_wakeup($this); */
1122 /* else if ($this->subst == 'standup') */
1123 /* $this->room->room_outstandup($this); */
1125 /* log_rd2("LOGOUT FROM WHAT ???"); */
1127 } /* if ($this->the_end == TRUE) { ... */
1128 } /* if ($this->the_end == TRUE) { ... */
1129 } /* if ($cur_step < $this->step) { */
1131 /* Room::unlock_data($sem); */
1132 } /* else of if ($cur_step == -1) { */
1136 } // function maincheck (...
1138 function continue_set($match_code)
1140 $this->continue = $match_code;
1143 function continue_get() {
1144 return ($this->continue);
1147 } // end class Bin5_user
1151 static $delta_t = array();
1155 var $comm; // commands for many people
1156 var $step; // current step of the comm array
1157 var $garbage_timeout;
1168 function Bin5 ($room, $table_idx, $table_token, $get, $post, $cookie) {
1169 $this->user = array();
1170 $this->table = array();
1172 $this->the_end = FALSE;
1173 $this->shm_sz = BIN5_SHM_MIN;
1174 if (($this->tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
1179 $user = $room->user;
1180 $table = $room->table[$table_idx];
1182 log_wr("Bin5 constructor");
1184 for ($i = 0 ; $i < $table->player_n ; $i++) {
1185 $user[$table->player[$i]]->table_token = $table_token;
1186 $this->user[$i] = Bin5_user::spawn($user[$table->player[$i]], $this, $table_idx, $i, $get, $post, $cookie);
1188 $this->table[0] = Bin5_table::spawn(&$table);
1190 log_main("TABLE_OLD_WIN - Bin5:".$this->table[0]->old_asta_win);
1192 $this->table_idx = $table_idx;
1193 $this->table_token = $table_token;
1194 $this->garbage_timeout = 0;
1196 $this->delay_mgr = new Delay_Manager((GARBAGE_TIMEOUT *3.0) / 2.0);
1198 log_wr("Bin5 constructor end");
1202 function get_user($sess, &$idx)
1206 if (validate_sess($sess)) {
1207 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1208 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1211 $ret = &$this->user[$i];
1215 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1216 // for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++)
1217 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1220 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1227 function garbage_manager($force)
1231 /* Garbage collector degli utenti in timeout */
1235 $delta = $this->delay_mgr->delta_get($curtime);
1237 if ($force || $this->garbage_timeout < $curtime) {
1238 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1239 $user_cur = $this->user[$i];
1240 if ($user_cur->sess == "" ||
1241 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
1244 if ($user_cur->lacc + EXPIRE_TIME_RD < ($curtime - $delta)) { // Auto logout dell'utente
1245 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
1247 if ($user_cur->stat == 'table') {
1248 log_auth($user_cur->sess," bin5 Autologout session.");
1250 /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE
1252 $tmp_sess = $user_cur->sess;
1253 $user_cur->sess = "";
1254 Bin5_user::step_unproxy($tmp_sess);
1255 $user_cur->name = "";
1256 $user_cur->the_end = FALSE;
1260 /* se gli altri utenti non erano d'accordo questo utente viene bannato */
1261 $remcalc = $this->table[0]->exitlock_calc(&$this->user, $user_cur->table_pos);
1263 require_once("${G_base}Obj/hardban.phh");
1264 Hardbans::add(($user_cur->flags & USER_FLAG_AUTH ? $user_cur->name : FALSE),
1265 $user_cur->ip, $user_cur->sess, $user_cur->laccwr + BAN_TIME);
1267 // $user->bantime = $user->laccwr + BAN_TIME;
1269 $this->table_wakeup($user_cur);
1273 log_rd2($user_cur->sess." GARBAGE UPDATED!");
1275 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
1280 $this->delay_mgr->lastcheck_set($curtime);
1285 function destroy_data()
1291 log_main("DESTROY BRISKIN5 DATA");
1294 log_main("DESTROY2 BRISKIN5 DATA [".$this->table_idx."]");
1295 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1296 $this->user[$i]->destroy_data($this->table_idx);
1297 Bin5_user::unproxy_step($this->table_idx, $this->user[$i]->sess);
1299 if (($tok = @ftok(FTOK_PATH."/bin5/table".$this->table_idx."/table", "B")) == -1)
1302 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
1305 if (shmop_delete($shm) == 0) {
1306 log_only("REMOVE FALLITA ");
1311 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
1313 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
1323 static function lock_data($is_exclusive, $table_idx)
1325 if (($res = file_lock(FTOK_PATH."/bin5/table".$table_idx."/table", $is_exclusive)) != FALSE) {
1326 self::$delta_t = microtime(TRUE);
1327 log_lock("LOCK table [".$table_idx."] [".self::$delta_t[$table_idx]."]");
1329 return (new Vect(array('res' => $res, 'tab' => $table_idx)));
1336 static function unlock_data($res_vect)
1340 $res = $res_vect->getbyid('res');
1341 $tab = $res_vect->getbyid('tab');
1343 log_lock("UNLOCK table [".$tab."] [".(microtime(TRUE) - (self::$delta_t[$tab]))."]");
1349 function chatt_send($user, $mesg)
1351 GLOBAL $mlang_brisk, $G_lang;
1353 if ($user->stat != 'table') {
1358 $table = &$this->table[$user->table];
1359 $user_mesg = substr($mesg,6);
1364 $dt = date("H:i ", $curtime);
1365 if (strncmp($user_mesg, "/cont ", 6) == 0) {
1366 log_main($user->sess." chatt_send BEGIN");
1368 $match_id = substr($user_mesg, 6);
1369 $table->match_continue($this, $user, $match_id);
1372 for ($i = 0 ; $i < ($user->stat == 'room' ? BIN5_MAX_PLAYERS : BIN5_PLAYERS_N) ; $i++) {
1373 if ($user->stat == 'room') {
1374 $user_cur = &$this->user[$i];
1375 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1379 $user_cur = &$this->user[$table->player[$i]];
1382 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1383 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
1384 $dt, $user->flags, xcape($user->name), xcape($user_mesg));
1385 $user_cur->step_inc();
1387 log_legal($curtime, $user->ip, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
1391 function table_wakeup($user)
1393 $table = &$this->table[0];
1395 log_main("BIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
1399 log_main("BIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1401 for ($i = 0 ; $i < $table->player_n ; $i++) {
1402 $user_cur = &$this->user[$i];
1403 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1405 if ($user_cur == $user)
1406 $user_cur->subst = "shutdowner";
1408 $user_cur->subst = "shutdowned";
1409 $user_cur->laccwr = $curtime;
1411 $ret = "gst.st = ".($user_cur->step+1)."; ";
1412 $ret .= 'gst.st_loc++; xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
1414 log_wr($user_cur->sess." BIN5_WAKEUP: ".$ret);
1415 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1416 $user_cur->step_inc();
1419 $this->the_end = TRUE;
1423 * If all players are freezed the room garbage_manager clean up table and users.
1425 function is_abandoned()
1430 $table = &$this->table[0];
1432 for ($i = 0 ; $i < $table->player_n ; $i++) {
1433 $user_cur = &$this->user[$i];
1435 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
1444 static function request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
1446 GLOBAL $G_black_list;
1448 printf("NEW_SOCKET (root): %d\n", intval($new_socket));
1450 $enc = get_encoding($header);
1451 if (isset($header['User-Agent'])) {
1452 if (strstr($header['User-Agent'], "MSIE")) {
1453 $transp_type = "htmlfile";
1456 $transp_type = "xhr";
1460 $transp_type = "iframe";
1462 force_no_cache($header_out);
1464 if (($table_idx = gpcs_var('table_idx', $get, $post, $cookie)) === FALSE)
1467 if (($table_token = gpcs_var('table_token', $get, $post, $cookie)) === FALSE)
1468 unset($table_token);
1475 bin5_index_main($transp_type, $header_out, $addr, $get, $post, $cookie);
1476 $content = ob_get_contents();
1479 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1483 case "index_wr.php":
1484 // FIXME $content can be unset
1485 if (isset($table_idx) && isset($table_token)) {
1486 if (($bri = $s_a_p->app->match_get($table_idx, $table_token)) != FALSE) {
1488 bin5_index_wr_main($bri, $addr, $get, $post, $cookie);
1489 $content = ob_get_contents();
1493 $content = "Bin5 Load data error";
1497 $content = "Bin5 Load data error";
1499 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1503 case "index_rd_ifra.php":
1504 if (($transp = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
1506 if ($transp == 'websocket')
1510 if (!isset($table_idx)
1511 || !isset($table_token)
1512 || !isset($cookie['sess'])
1513 || ($bri = $s_a_p->app->match_get($table_idx, $table_token)) == NULL
1514 || (($user = $bri->get_user($cookie['sess'], $idx)) == FALSE)) {
1516 $content = Bin5_user::stream_fini($transp_type, $s_a_p->rndstr, TRUE);
1517 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1522 // close a previous opened index_read_ifra socket, if exists
1523 if (($prev = $user->rd_socket_get()) != NULL) {
1524 $s_a_p->socks_unset($user->rd_socket_get());
1525 fclose($user->rd_socket_get());
1526 printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
1527 $user->rd_socket_set(NULL);
1531 $user->stream_init($s_a_p->rndstr, $enc, $header, $header_out, $content, $get, $post, $cookie);
1533 $response = headers_render($header_out, -1).$user->chunked_content($content);
1534 $response_l = mb_strlen($response, "ASCII");
1536 $wret = @fwrite($new_socket, $response, $response_l);
1537 if ($wret < $response_l) {
1538 printf("TROUBLES WITH FWRITE: %d\n", $wret);
1539 $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
1542 $user->rd_cache_set("");
1544 fflush($new_socket);
1547 $s_a_p->socks_set($new_socket, $user, NULL);
1548 $user->rd_socket_set($new_socket);
1549 printf(" - qui ci siamo - ");
1568 function locshm_exists($tok)
1572 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
1573 log_main($tok." SHM NOT exists");
1579 log_main($tok." SHM exists");
1588 is_transition (is from room to table ?)
1589 is_again (is another game)
1591 Examples of $is_transition, $is_again:
1592 from reload of the page: FALSE, FALSE
1593 from sitdown in room: TRUE, FALSE
1594 from table: asta cmd e tutti passano: TRUE, TRUE
1595 from table: fine partita: TRUE, TRUE
1597 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1599 $table_idx = $user->table;
1600 $table = $bri->table[$table_idx];
1601 $table_pos = $user->table_pos;
1603 $ret = "table_init();";
1604 $ret .= $table->exitlock_show(&$bri->user, $table_pos);
1606 /* GENERAL STATUS */
1607 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1608 $sendstep, $user->stat, $user->subst, $table_pos);
1610 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1613 $ret .= "background_set();";
1616 $ret .= $user->myname_innerHTML();
1617 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1618 $bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->flags,
1619 xcape($bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->name),
1621 $bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->flags,
1622 xcape($bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->name),
1624 $bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->flags,
1625 xcape($bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->name),
1627 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->flags),
1628 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->name)),
1630 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->flags),
1631 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->name)));
1633 /* NOTIFY FOR THE CARD MAKER */
1634 if ($is_transition) { // && $user->subst == "asta" superfluo
1635 $ret .= show_table_info(&$bri, &$table, $table_pos);
1636 $ret .= "setTimeout(preload_images, 500, g_preload_img_arr, g_imgct);";
1639 $ret .= "\$('imgct').innerHTML = \$('imgct').innerHTML = mlang_commons['imgload_a'][g_lang]+\"100%.\";";
1642 $ret .= table_welcome($user);
1644 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1645 $ret .= playsound("cow.mp3");
1650 if ($is_transition) { // && $user->subst == "asta" superfluo
1653 for ($i = 0 ; $i < BIN5_CARD_HAND ; $i++) {
1654 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++) {
1656 for ($o = 0 ; $o < (BIN5_CARD_HAND * BIN5_PLAYERS_N) && $ct < $i+1 ; $o++) {
1657 // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1658 if ($table->card[$o]->owner == (($e + $table->gstart) % BIN5_PLAYERS_N)) {
1664 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1666 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BIN5_PLAYERS_N,
1667 $i, ((($e + BIN5_PLAYERS_N - $table_pos + $table->gstart) % BIN5_PLAYERS_N) == 0 ?
1668 $table->card[$o]->value : -1),
1669 ($i == 7 && $e == (BIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1674 $taked = array(0,0,0,0,0);
1675 $inhand = array(0,0,0,0,0);
1676 $ontabl = array(-1,-1,-1,-1,-1);
1679 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1680 // for ($i = 0 ; $i < 40 ; $i++) {
1681 if ($table->card[$i]->stat == 'hand') {
1682 if ($table->card[$i]->owner == $table_pos) {
1683 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1685 $inhand[$table->card[$i]->owner]++;
1687 else if ($table->card[$i]->stat == 'take') {
1688 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1689 $taked[$table->card[$i]->owner]++;
1691 else if ($table->card[$i]->stat == 'table') {
1692 $ontabl[$table->card[$i]->owner] = $i;
1696 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1697 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1699 log_main("Stat table: ".$logg);
1701 /* Set ours cards. */
1703 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1704 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1705 for ($i = $inhand[$table_pos] ; $i < BIN5_CARD_HAND ; $i++)
1706 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1707 $ret .= sprintf('card_setours(%s);', $oursarg);
1709 /* Dispose all cards */
1710 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1711 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1712 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1713 ($inhand[$i] <= BIN5_CARD_HAND ? $inhand[$i] : BIN5_CARD_HAND), $taked[$i]);
1715 if ($ontabl[$i] != -1) {
1716 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1717 $table->card[$ontabl[$i]]->value,
1718 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1724 if ($user->subst == 'asta') {
1726 /* show users auction status */
1728 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1729 $user_cur = &$bri->user[$table->player[$i]];
1730 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1731 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1733 if (BIN5_PLAYERS_N == 3)
1734 $showst .= ",-2,-2";
1735 $ret .= sprintf('document.title = "Brisk - Tavolo %d (asta)";', $user->table_orig);
1736 $ret .= sprintf('show_astat(%s);', $showst);
1738 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1739 /* show card chooser */
1740 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1745 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N) &&
1746 $table->asta_win == -1)
1747 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1748 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1750 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1751 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1755 if ($table->asta_win == -1) { // auction case
1756 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N))
1757 $ret .= "remark_on();";
1759 $ret .= "remark_off();";
1761 else { // chooseed case
1762 if ($table_pos == $table->asta_win)
1763 $ret .= "remark_on();";
1765 $ret .= "remark_off();";
1768 else if ($user->subst == 'game') {
1770 if (($table->gstart + $table->turn) % BIN5_PLAYERS_N == $table_pos)
1771 $ret .= "is_my_time = true; remark_on();";
1773 $ret .= "remark_off();";
1775 /* WHO CALL AND WHAT */
1776 $ret .= briscola_show($bri, $table, $user);
1780 } // end function show_table(...
1782 function calculate_winner(&$table)
1789 $cur_seed = $table->briscola - ($table->briscola % 10);
1791 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1792 // for ($i = 0 ; $i < 40 ; $i++) {
1793 if ($table->card[$i]->stat != "table")
1796 log_wr(sprintf("Card On table: [%d]", $i));
1798 $v = $table->card[$i]->value;
1799 $ontab[$table->card[$i]->owner] = $v;
1800 $ontid[$table->card[$i]->owner] = $i;
1801 /* se briscola setto il flag */
1802 if (($v - ($v % 10)) == $cur_seed)
1806 if ($briontab == FALSE) {
1807 $cur_win = $table->gstart;
1808 $cur_val = $ontab[$cur_win];
1809 $cur_seed = $cur_val - ($cur_val % 10);
1812 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1813 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1814 if ($ontab[$i] < $cur_val) {
1815 $cur_val = $ontab[$i];
1821 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1822 $table->card[$ontid[$i]]->owner = $cur_win;
1823 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1828 function show_table_info(&$bri, &$table, $table_pos)
1830 GLOBAL $G_lang, $mlang_bin5_bin5;
1838 $user = $bri->user[$table->player[$table_pos]];
1840 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1841 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1844 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++)
1845 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1846 $noty .= sprintf("</tr>");
1849 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1851 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1852 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1853 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1854 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1859 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1860 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1861 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1862 $noty .= "</tr></table>";
1864 if ($table->old_reason != "") {
1865 $noty .= sprintf($mlang_bin5_bin5['info_last'][$G_lang],
1866 $tg_br, $tg_hr, $tg_bo, $tg_bc);
1868 $noty .= $table->old_reason;
1871 $noty .= sprintf($mlang_bin5_bin5['info_curr'][$G_lang],
1872 $tg_br, $tg_hr, $tg_bo, $tg_bc);
1874 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1875 if ($table->mazzo == $table_pos)
1876 $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
1878 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1879 $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
1882 if ($user->subst == 'asta') {
1883 if ($table->asta_win == -1) // auction case
1884 $curplayer = $table->gstart % BIN5_PLAYERS_N;
1886 $curplayer = $table->asta_win;
1888 else if ($user->subst == 'game') {
1889 $curplayer = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
1892 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1893 if ($curplayer == $table_pos) {
1894 $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
1897 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1898 $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
1901 $multer = $table->multer(TRUE);
1903 $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($multer) );
1905 $noty .= "<hr><br>";
1906 if ($table->match_id != -1) {
1907 $noty .= sprintf($mlang_bin5_bin5['info_match'][$G_lang], $table->match_id);
1908 $noty .= "<hr><br>";
1910 $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
1911 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
1912 * 130 + ($table->points_n > 0 ? 50 : 0) +
1913 * (120 * ($table->points_n / MAX_POINTS)));
1914 * will be used when we refact notify js function following
1915 * photoo class logic
1921 function table_welcome($user)
1923 GLOBAL $table_wellarr, $G_lang;
1926 for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
1927 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
1933 function briscola_show($bri, $table, $user)
1935 GLOBAL $G_lang, $mlang_bin5_bin5;
1939 if ($table->asta_card == 9)
1940 $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
1942 /* text of caller cell */
1943 if ($user->table_pos == $table->asta_win) {
1944 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
1945 $ret .= sprintf($prestr, $ptnadd);
1948 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
1949 $ret .= sprintf($prestr,
1950 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1952 $ret .= sprintf('set_iscalling(%d);', ($table->asta_win - $user->table_pos + BIN5_PLAYERS_N) % BIN5_PLAYERS_N);
1954 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1956 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1957 $ret .= sprintf('$("caller").style.visibility = "visible";');
1958 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1959 $ret .= sprintf('$("astalascio").style.visibility = "";');
1960 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1961 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1967 function game_result($asta_pnt, $pnt)
1969 if ($asta_pnt == 61) {
1972 else if ($pnt == 60)
1978 if ($pnt >= $asta_pnt)
1985 function log_points($remote_addr, $curtime, $user, $where, $mesg)
1987 if (($fp = @fopen(LEGAL_PATH."/points.log", 'a')) != FALSE) {
1988 /* Unix time | session | nickname | IP | where was | mesg */
1989 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
1990 ($user->flags & USER_FLAG_AUTH ? 'A' : 'N'),
1991 $user->name, $remote_addr, $where , $mesg));
1996 function game_description($act, $form, $old_mult, $win = -1, $win_name = "?1?", $fri = -1, $fri_name = "?2?",
1997 $old_pnt = -1, $old_asta_pnt = -1)
1999 GLOBAL $G_lang, $mlang_bin5_bin5;
2001 if ($form == 'html') {
2006 $win_name = xcape($win_name);
2007 $fri_name = xcape($fri_name);
2016 if ($act == BIN5_RULES_OLDSCHEMA) {
2019 else if ($act == BIN5_RULES_ALLPASS) {
2020 return (sprintf($mlang_bin5_bin5['info_alpa'][$G_lang],
2021 $tg_br, $tg_hr, $tg_bo, $tg_bc));
2023 else if ($act == BIN5_RULES_ABANDON) {
2024 return (sprintf($mlang_bin5_bin5['info_aban'][$G_lang],
2025 $tg_br, $tg_hr, $tg_bo, $tg_bc,
2029 $wol = game_result($old_asta_pnt, $old_pnt);
2033 if ($win != $fri) { // not alone case
2034 /* 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" */
2036 $noty .= sprintf($mlang_bin5_bin5['info_part'][$G_lang],
2037 $tg_br, $tg_hr, $tg_bo, $tg_bc,
2040 if ($old_pnt == 120) {
2041 $noty .= sprintf($mlang_bin5_bin5['info_capp'][$G_lang],
2042 $tg_br, $tg_hr, $tg_bo, $tg_bc );
2045 if ($old_asta_pnt > 61) {
2046 $noty .= sprintf($mlang_bin5_bin5['info_alea'][$G_lang],
2047 $tg_br, $tg_hr, $tg_bo, $tg_bc,
2048 $old_asta_pnt, $old_pnt,
2049 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
2050 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
2051 $mlang_bin5_bin5['info_lost'][$G_lang])));
2054 $noty .= sprintf($mlang_bin5_bin5['info_more'][$G_lang],
2055 $tg_br, $tg_hr, $tg_bo, $tg_bc,
2057 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
2058 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
2059 $mlang_bin5_bin5['info_lost'][$G_lang])));
2060 } // else of if ($old_asta_pnt > 61) {
2061 } // else of if ($old_pnt == 120) {
2062 } // if ($win != $fri) { // not alone case
2064 $noty .= sprintf($mlang_bin5_bin5['info_alon'][$G_lang],
2065 $tg_br, $tg_hr, $tg_bo, $tg_bc,
2067 if ($old_pnt == 120) {
2068 $noty .= sprintf($mlang_bin5_bin5['info_acap'][$G_lang],
2069 $tg_br, $tg_hr, $tg_bo, $tg_bc);
2072 if ($old_asta_pnt > 61) {
2073 $noty .= sprintf($mlang_bin5_bin5['info_aleaa'][$G_lang],
2074 $tg_br, $tg_hr, $tg_bo, $tg_bc,
2075 $old_asta_pnt, $old_pnt,
2076 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
2077 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
2078 $mlang_bin5_bin5['info_lost'][$G_lang])));
2081 $noty .= sprintf($mlang_bin5_bin5['info_morea'][$G_lang],
2082 $tg_br, $tg_hr, $tg_bo, $tg_bc,
2084 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
2085 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
2086 $mlang_bin5_bin5['info_lost'][$G_lang])));
2092 $old_multer = Bin5_table::s_multer($old_mult, $old_asta_pnt);
2093 if ($old_multer > 1) {
2094 $noty .= sprintf($mlang_bin5_bin5['info_omul'][$G_lang],
2095 $tg_br, $tg_hr, $tg_bo, $tg_bc,
2096 multoval($old_multer));
2098 $noty .= sprintf('%2$s%1$s', $tg_br, $tg_hr);