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_part' => array( 'it' => 'Nell\'ultima mano ha chiamato %3$s%5$s%4$s, il socio era %3$s%6$s%4$s,%1$s',
41 'en' => 'In the last hand the declarer was %3$s%5$s%4$s, the partner was %3$s%6$s%4$s,%1$s'),
43 'info_capp' => array( 'it' => 'hanno fatto %3$scappotto%4$s EBBRAVI!.%1$s',
44 'en' => 'and they made %3Dscapot%4$s WELL DONE!.%1$s'),
46 // br, hr, b, /b, old_asta_pnt, old_pnt, winornot
47 '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',
48 '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'),
49 // br, hr, b, /b, old_pnt, winornot
50 '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',
51 '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'),
52 'info_win' => array( 'it' => 'vinto',
54 'info_peer' => array( 'it' => 'pareggiato',
56 'info_lost' => array( 'it' => 'perso',
59 // br, hr, b, /b, win_name
60 'info_alon' => array( 'it' => 'Nell\'ultima partita %3$s%5$s%4$s si è chiamato in mano,%1$s',
61 'en' => 'In the last hand %3$s%5$s%4$s play alone against each other,%1$s'),
62 // br, hr, b, /b, old_asta_pnt, old_pnt, winornot
63 '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',
64 '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'),
65 // br, hr, b, /b, old_pnt, winornot
66 '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',
67 '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'),
69 'info_acap' => array( 'it' => 'ha fatto %3$scappotto%4$s EBBRAVO!.%1$s',
70 'en' => 'and he/she made %3$scapot%4$s WELL DONE!.%1$s'),
73 'info_omul' => array( 'it' => '%1$sLa partita valeva %3$s%5$s%4$s.%1$s',
74 'en' => '%1$sEN: The game was worth %3$s%5$s%4$s.%1$s' ),
77 'info_alpa' => array( 'it' => '%1$sHanno passato %3$stutti%4$s.%1$s',
78 'en' => '%1$sEN: Hanno passato %3$stutti%4$s.%1$s' ),
80 // br, hr, b, /b, aband-name
81 'info_aban' => array( 'it' => ' Ha lasciato %3$s%5$s%4$s perché aveva al massimo %3$s2 punti%4$s.',
82 'en' => ' EN: Ha lasciato %3$s%5$s%4$s perché aveva al massimo %3$s2 punti%4$s.'),
84 'info_shuf' => array( 'it' => 'Il mazzo a <b>%s</b>, ',
85 'en' => '<b>%s</b> shuffled the cards, '),
86 'info_yturn'=> array( 'it' => ' tocca a <b>te</b> giocare.',
87 'en' => ' it\'s <b>your</b> turn.'),
88 'info_turn' => array( 'it' => 'tocca a <b>%s</b> giocare.',
89 'en' => 'it\'s the <b>%s</b>\'s turn.'),
90 'info_mult' => array( 'it' => ' La partita vale <b>%s</b>.',
91 'en' => ' The game worth <b>%s</b>.' ),
92 'info_yshuf'=> array( 'it' => 'Fai <b>tu</b> il mazzo, ',
93 'en' => 'It\'s <b>your</b> shuffled the cards, '),
94 'btn_bkgame'=> array( 'it' => 'torna alla partita',
95 'en' => 'back to the game'),
96 'call_wptn' => array( 'it' => '<br>con %d punti',
97 'en' => '<br>with %d points'),
98 'call_ycall'=> array( 'it' => 'Chiami%s:',
100 'call_call' => array( 'it' => 'Chiama %s%s:',
101 'en' => 'The declarer is %s%s:')
105 $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.'),
106 '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.') );
108 function multoval($mult)
113 return ($G_lang == 'en' ? 'double' : 'doppio');
115 return ($G_lang == 'en' ? 'triple' : 'triplo');
117 return ($G_lang == 'en' ? 'quadruple' : 'quadruplo');
119 return (sprintf(($G_lang == 'en' ? "%d-ple" : "%d-plo"), $mult));
124 var $value; /* 0 - 39 card value */
125 var $stat; /* 'bunch', 'hand', 'table', 'take' */
126 var $owner; /* (table position 0-4) */
127 // var $pos; /* Pos in hand. */
128 var $x; /* When played the X position on the table of the owner. */
129 var $y; /* When played the Y position on the table of the owner. */
131 function Card($value, $stat, $owner)
133 $this->value = $value;
134 $this->stat = $stat; // Card stat
135 $this->owner = $owner;
138 function assign($stat,$owner)
140 $this->stat = $stat; // Card stat
141 $this->owner = $owner;
144 function setpos($pos)
151 $this->stat = 'table'; // Card stat
156 function take($newown)
158 $this->stat = 'take'; // Card stat
159 $this->owner = $newown;
163 class Bin5_table extends Table {
164 var $card; // il mazzo di carte
165 var $mazzo; // chi e' di mazzo
175 var $points; // points array
176 var $points_n; // number of row of points
179 var $asta_win; // the caller idx position at table
181 var $friend; // the callee idx position at table
183 var $old_act; // last action that trigs the end of the game
188 var $old_pnt; // points made by caller and callee
189 var $old_asta_win; // the old caller idx position at table
190 var $old_friend; // the old callee idx position at table
192 function Bin5_table()
198 function create($idx)
200 if (($thiz =& new Bin5_table()) == FALSE)
206 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
207 $thiz->asta_pla_n= -1;
208 $thiz->asta_card = -1;
209 $thiz->asta_pnt = -1;
212 $thiz->points = array( );
214 $thiz->total = array( 0, 0, 0, 0, 0);
215 $thiz->asta_win = -1;
216 $thiz->briscola = -1;
221 $thiz->old_mazzo = -1;
222 $thiz->old_reason = "";
223 $thiz->old_asta_pnt = -1;
224 $thiz->old_mult = -1;
226 $thiz->old_asta_win = -1;
227 $thiz->old_friend = -1;
234 function myclone(&$from)
236 if (($thiz =& new Bin5_table()) == FALSE)
241 $thiz->card = $from->card;
242 $thiz->mazzo = $from->mazzo; // REVIEW
243 $thiz->gstart = $from->gstart;
244 $thiz->turn = $from->turn;
246 $thiz->asta_pla = $from->asta_pla;
247 $thiz->asta_pla_n = $from->asta_pla_n;
248 $thiz->asta_card = $from->asta_card;
249 $thiz->asta_pnt = $from->asta_pnt;
251 $thiz->mult = $from->mult;
252 $thiz->points = $from->points;
253 $thiz->points_n = $from->points_n;
254 $thiz->total = $from->total;
256 $thiz->asta_win = $from->asta_win;
257 $thiz->briscola = $from->briscola;
258 $thiz->friend = $from->friend;
260 $thiz->old_act = $from->old_act;
261 $thiz->old_mazzo = $from->old_mazzo;
262 $thiz->old_reason = $from->old_reason;
263 $thiz->old_asta_pnt = $from->old_asta_pnt;
264 $thiz->old_mult = $from->mult;
265 $thiz->old_pnt = $from->old_pnt;
266 $thiz->old_asta_win = $from->old_asta_win;
267 $thiz->old_friend = $from->old_friend;
273 function parentcopy(&$from)
278 function spawn(&$from)
280 if (($thiz =& new Bin5_table()) == FALSE)
283 $thiz->parentcopy($from);
285 log_main("PLAYER_N - spawn.".$thiz->player_n);
287 $thiz->card = array();
288 $thiz->bunch_create();
289 $thiz->mazzo = rand(0,PLAYERS_N-1);
292 $thiz->old_asta_win = -1;
293 $thiz->old_reason = "";
295 // players are rearranged in an dedicated array
296 $thiz->player = array();
297 for ($i = 0 ; $i < $from->player_n ; $i++)
298 $thiz->player[$i] = $i;
300 log_main("TABLE_OLD_WIN - spawn:".$thiz->old_asta_win);
305 static function asta2mult($asta_pnt)
309 else if ($asta_pnt > 100)
311 else if ($asta_pnt > 90)
313 else if ($asta_pnt > 80)
315 else if ($asta_pnt > 70)
321 function multer($is_new)
324 // return (pow(2, $this->mult) * $this->asta2mult($this->asta_pnt));
325 return (static::s_multer($this->mult, $this->asta_pnt));
328 // return (pow(2, $this->old_mult) * $this->asta2mult($this->old_asta_pnt));
329 return (static::s_multer($this->old_mult, $this->old_asta_pnt));
333 static function s_multer($mult, $pnt)
335 return (pow(2, $mult) * static::asta2mult($pnt));
338 // function bunch_create_old() function AND
342 // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
343 // // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
344 // $ret[$i] =& new Card($i, 'bunch', 'no_owner');
351 function bunch_create()
355 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
356 $this->card[$i] = new Card($i, 'bunch', 'no_owner');
360 function bunch_make()
362 log_main("bunch_make start");
363 $ct = array(0,0,0,0,0);
365 mt_srand(make_seed());
367 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--)
370 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--) {
374 log_main("RND ZERO");
378 $owner = $i % BIN5_PLAYERS_N;
379 $this->card[$id]->assign('hand', $owner);
381 $rest[$rn] = $rest[$i];
382 // $pubbpos[$rn2] = $pubbpos[$i];
384 log_main("bunch_make end");
387 function init($userarr)
390 $this->mazzo = rand(0,PLAYERS_N-1);
393 $this->old_asta_win = -1;
394 $this->old_reason = "";
396 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
397 $this->total[$i] = 0;
398 $user_cur = $userarr[$this->player[$i]];
399 $user_cur->exitislock = TRUE;
402 log_main("table::init: ci siamo");
405 function game_init($userarr)
409 $this->gstart = ($this->mazzo+1) % BIN5_PLAYERS_N;
412 $this->asta_pla_n = BIN5_PLAYERS_N;
413 $this->asta_card = -1;
414 $this->asta_pnt = 60;
415 $this->asta_win = -1;
416 $this->briscola = -1;
420 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
421 $this->asta_pla[$i] = TRUE;
422 $user_cur = $userarr[$this->player[$i]];
423 $user_cur->subst = 'asta';
424 $user_cur->asta_card = -2;
425 $user_cur->asta_pnt = -1;
426 $user_cur->handpt = $this->hand_points($i);
431 function game_next($delta)
433 $this->old_mazzo = $this->mazzo;
434 $this->mazzo = ($this->mazzo + $delta) % BIN5_PLAYERS_N;
437 function mult_inc($val)
439 $this->old_mult = $this->mult;
443 function mult_set($val)
445 $this->old_mult = $this->mult;
449 function hand_points($idx)
451 GLOBAL $G_all_points;
455 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
456 // for ($i = 0 ; $i < 40 ; $i++) {
457 if ($this->card[$i]->owner != $idx)
460 $ctt = $this->card[$i]->value % 10;
461 $tot += $G_all_points[$ctt];
468 function exitlock_show($userarr, $table_pos)
470 $ct = $this->exitlock_calc($userarr, $table_pos);
472 $ret = sprintf('exitlock_show(%d, %s);', $ct,
473 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
477 function exitlock_calc(&$userarr, $table_pos)
481 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
482 if ($userarr[$this->player[$i]]->exitislock == FALSE)
489 function rules_engine($bri, $curtime, $action, $user)
491 GLOBAL $G_all_points, $G_dbasetype;
495 $this->old_act = $action;
496 if ($action == BIN5_RULES_ALLPASS) { // return TRUE if all correct
497 $this->old_asta_win = -1;
500 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
505 // $this->game_next(1);
506 $this->game_init(&$bri->user);
508 else if ($action == BIN5_RULES_ABANDON) { // return TRUE if all correct
509 log_wr(sprintf("GIOCO FINITO !!!"));
510 $this->old_asta_win = $user->table_pos;
514 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
518 // Non si cambia mazzo se si abbandona la partita
520 // $this->game_next(0);
521 $this->game_init(&$bri->user);
523 else if ($action == BIN5_RULES_FINISH) { // return TRUE if all correct
527 if ($this->asta_pnt == 60)
528 $this->asta_pnt = 61;
530 $this->old_reason = "";
532 // count points for the temporary 2 teams
533 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
534 $ctt = $this->card[$i]->value % 10;
535 $own = $this->card[$i]->owner;
536 if ($own == $this->asta_win || $own == $this->friend)
537 $pro += $G_all_points[$ctt];
540 log_wr(sprintf("PRO: [%d]", $pro));
543 if ($this->asta_pnt == 61 && $pro == 60) {
544 $this->points[$this->points_n % MAX_POINTS] = array();
545 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
546 $this->points[$this->points_n % MAX_POINTS][$i] = 0;
550 $this->old_pnt = $pro;
557 if ($pro >= $this->asta_pnt)
562 $this->points[$this->points_n % MAX_POINTS] = array();
563 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
564 if ($i == $this->asta_win)
565 $pt = ($i == $this->friend ? 4 : 2);
566 else if ($i == $this->friend)
571 log_wr(sprintf("PRO: pt[%d][%d] = %d", $this->points_n % MAX_POINTS, $i, $pt));
573 $pt = $pt * $sig * $this->multer(TRUE) * ($pro == 120 ? 2 : 1);
575 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $this->multer(TRUE), ($pro == 120 ? 2 : 1)));
577 $this->points[$this->points_n % MAX_POINTS][$i] = $pt;
578 $this->total[$i] += $pt;
582 $this->old_pnt = $pro;
583 $this->old_asta_win = $this->asta_win;
593 $this->game_next($game_delta);
595 $plist = "$this->table_token|$user->table_orig|$this->player_n";
598 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
599 $user_cur = &$bri->user[$this->player[$i]];
602 $ucodes[$i] = $user_cur->code_get();
605 $plist .= '|'.xcapelt($user_cur->name).'|'.$pts[$i];
606 $codes .= '|'.xcapelt($user_cur->code_get());
609 log_legal($curtime, $user->ip, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
611 $this->old_asta_pnt = $this->asta_pnt;
612 // $this->old_mazzo is managed by ->game_next();
613 // $this->old_mult, $this->old_pnt, $this->old_reason and $this->old_asta_win are specific
615 $this->old_friend = $this->friend;
616 $this->old_reason = game_description($action, 'html', $this->old_mult,
617 $this->old_asta_win, $bri->user[$this->player[$this->old_asta_win]]->name,
618 $this->old_friend, $bri->user[$this->player[$this->old_friend]]->name,
619 $this->old_pnt, $this->old_asta_pnt);
622 if ($user->table_orig < TABLES_AUTH_N) {
623 require_once("../Obj/dbase_".$G_dbasetype.".phh");
625 if (($bdb = BriskDB::create()) != FALSE) {
626 $bdb->bin5_points_save($curtime, $this, $user->table_orig, $action, $ucodes, $pts);
630 log_points($remote_addr, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED");
632 log_points($remote_addr, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
635 $this->game_init(&$bri->user);
640 } // end class Bin5_table
645 define('BIN5_USER_FLAG_RING_ENDAUCT', 0x01);
647 class Bin5_user extends User {
650 var $handpt; // Total card points at the beginning of the current hand.
651 var $exitislock; // Player can exit from the table ?
652 var $privflags; // Flags for briskin5 only
660 function create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
661 if (($thiz =& new User()) == FALSE)
664 $thiz->asta_card = -2;
665 $thiz->asta_pnt = -1;
667 $thiz->exitislock = TRUE;
668 $thiz->privflags = 0;
674 function parentcopy(&$from)
679 function copy(&$from)
681 $this->parentcopy($from);
683 $this->asta_card = $from->asta_card;
684 $this->asta_pnt = $from->asta_pnt;
685 $this->handpt = $from->handpt;
686 $this->exitislock = $from->exitislock;
687 $this->privflags = $from->privflags;
691 function myclone(&$from)
693 if (($thiz =& new User()) == FALSE)
702 static function spawn($from, &$bri, $table, $table_pos, $get, $post, $cookie)
704 if (($thiz = new Bin5_user()) == FALSE)
707 if (($CO_bin5_pref_ring_endauct = gpcs_var("CO_bin5_pref_ring_endauct", $get, $post, $cookie)) === FALSE) {
708 $CO_bin5_pref_ring_endauct = "";
711 $thiz->parentcopy($from);
713 /* NOTE: at this moment idx and table_pos fields have the same value
714 but diffentent functions, we keep them separated for a while */
716 $thiz->idx = $table_pos;
717 $thiz->asta_card = -2;
718 $thiz->asta_pnt = -1;
720 $thiz->exitislock = TRUE;
722 log_wr("Bin5 constructor");
724 $thiz->privflags = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
726 $thiz->table_orig = $table;
728 $thiz->table_pos = $table_pos;
735 function step_set($step)
737 $this->step = $step & 0x7fffffff;
742 function step_inc($delta = 1) {
743 $this->step += $delta;
744 /* modularization because unpack() not manage unsigned 32bit int correctly */
745 $this->step &= 0x7fffffff;
750 static function load_step($tab_id, $sess)
754 if (validate_sess($sess) == FALSE)
757 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
758 mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
759 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step", 'rb')) == FALSE)
761 if (($s = fread($fp, 8)) == FALSE)
763 if (mb_strlen($s, "ASCII") != 8)
765 $arr = unpack('Ls/Li', $s);
768 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
775 log_rd2("STEP_GET [".$sess."]: return false ");
783 if (validate_sess($this->sess) == FALSE)
785 if (file_exists(BIN5_PROXY_PATH."/table".$this->table_orig) == FALSE)
786 mkdir(BIN5_PROXY_PATH."/table".$this->table_orig, 0775, TRUE);
787 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$this->table_orig."/".$this->sess.".step", 'w')) == FALSE)
789 fwrite($fp, pack("LL",$this->step, $this->idx));
792 log_main("step_set [".$this->sess. "] [".$this->step."]");
800 static function unproxy_step($tab_id, $sess)
802 log_rd2("UNPROXY: ".BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
803 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
806 @unlink(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
809 function destroy_data($tab_id)
812 if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos, "B")) == -1) {
813 log_crit("BIN5 USER DATA REMOVE FAILED 1 [".FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos."]");
817 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
818 log_crit("BIN5 USER DATA REMOVE FAILED 2");
821 if (shmop_delete($shm) == 0) {
822 log_crit("BIN5 USER DATA REMOVE FAILED 3");
827 log_main("BIN5 USER DATA DESTROY SUCCESS");
829 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
839 static function blocking_error($is_unrecoverable)
841 log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
842 return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
845 protected function page_sync($sess, $page)
847 log_rd2("PAGE_SYNC");
848 printf("xXx BIN5_USER::PAGE_SYNC\n");
849 return (sprintf('xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
852 protected function maincheck($cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate, $table_idx, $table_token)
854 GLOBAL $G_lang, $mlang_indrd;
855 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
856 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
857 $CO_splashdate = "CO_splashdate".$G_splash_idx;
858 $$CO_splashdate = $splashdate;
862 log_rd("maincheck begin");
867 /* Nothing changed, return. */
868 if ($cur_step == $this->step)
871 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$this->stat."] cur_step[".$cur_step."] user_step[".$this->step."]");
873 if ($cur_step == -1) {
875 * if $cur_step == -1 load the current state from the main struct
878 $S_load_stat['wR_minusone']++;
880 // if ($this->the_end == TRUE) {
881 // log_rd2("main_check: the end".var_export(debug_backtrace()));
884 if ($this->trans_step != -1) {
885 log_rd2("TRANS USATO ".$this->trans_step);
886 $cur_step = $this->trans_step;
887 $this->trans_step = -1;
890 log_rd2("TRANS NON ATTIVATO");
895 /* this part I suppose is read only on $this->room structure */
896 if ($cur_step == -1) {
897 log_rd2("PRE-NEWSTAT");
904 if ($this->stat == 'table') {
906 /* NOTE: $this->room is associated with the current $bri object */
907 printf("xXx CLASS NAME [%s]\n", get_class($this->room));
908 $ret = show_table(&$this->room, $this, $this->step, FALSE, FALSE);
910 log_rd2("NEWSTAT: ".$this->stat);
912 $new_stat = $this->stat;
913 $new_subst = $this->subst;
914 $new_step = $this->step;
915 } /* if ($cur_step == -1) { */
917 /* $sem = Room::lock_data(FALSE); */
918 $S_load_stat['rU_heavy']++;
920 if ($cur_step < $this->step) {
922 if ($cur_step + COMM_N < $this->step) {
923 if (($cur_stat != $this->stat)) {
924 $to_stat = $this->stat;
925 /* Room::unlock_data($sem); */
927 printf("xXx BIN5_USER::MAINCHECK\n");
928 return ($this->page_sync($this->sess, ($to_stat == "table" ? "index.php" : "../index.php"), $this->table, $this->table_token));
930 log_rd2("lost history, refresh from scratch");
931 printf("xXx LOST HISTORY!\n");
935 for ($i = $cur_step ; $i < $this->step ; $i++) {
937 log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
938 $ret .= $this->comm[$ii];
940 $new_stat = $this->stat;
941 $new_subst = $this->subst;
942 $new_step = $this->step;
945 log_rd2($this->step, 'index_rd.php: after ret set');
947 if ($this->the_end == TRUE) {
948 log_rd2("LOGOUT BYE BYE!!");
949 log_auth($this->sess, "Explicit logout.");
951 if ($this->the_end == TRUE) {
955 /* if ($this->subst == 'sitdown') { */
956 /* log_load("ROOM WAKEUP"); */
957 /* $this->room->room_wakeup($this); */
959 /* else if ($this->subst == 'standup') */
960 /* $this->room->room_outstandup($this); */
962 /* log_rd2("LOGOUT FROM WHAT ???"); */
964 } /* if ($this->the_end == TRUE) { ... */
965 } /* if ($this->the_end == TRUE) { ... */
966 } /* if ($cur_step < $this->step) { */
968 /* Room::unlock_data($sem); */
969 } /* else of if ($cur_step == -1) { */
973 } // function maincheck (...
980 } // end class Bin5_user
985 static $delta_t = array();
989 var $comm; // commands for many people
990 var $step; // current step of the comm array
991 var $garbage_timeout;
1002 function Bin5 ($room, $table_idx, $table_token, $get, $post, $cookie) {
1003 $this->user = array();
1004 $this->table = array();
1006 $this->the_end = FALSE;
1007 $this->shm_sz = BIN5_SHM_MIN;
1008 if (($this->tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
1013 $user = $room->user;
1014 $table = $room->table[$table_idx];
1016 log_wr("Bin5 constructor");
1018 for ($i = 0 ; $i < $table->player_n ; $i++) {
1019 $user[$table->player[$i]]->table_token = $table_token;
1020 $this->user[$i] = Bin5_user::spawn($user[$table->player[$i]], $this, $table_idx, $i, $get, $post, $cookie);
1022 $this->table[0] = Bin5_table::spawn(&$table);
1024 log_main("TABLE_OLD_WIN - Bin5:".$this->table[0]->old_asta_win);
1026 $this->table_idx = $table_idx;
1027 $this->table_token = $table_token;
1028 $this->garbage_timeout = 0;
1030 $this->delay_mgr = new Delay_Manager((GARBAGE_TIMEOUT *3.0) / 2.0);
1032 log_wr("Bin5 constructor end");
1036 function get_user($sess, &$idx)
1040 if (validate_sess($sess)) {
1041 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1042 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1045 $ret = &$this->user[$i];
1049 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1050 // for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++)
1051 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1054 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1061 function garbage_manager($force)
1065 /* Garbage collector degli utenti in timeout */
1069 $delta = $this->delay_mgr->delta_get($curtime);
1071 if ($force || $this->garbage_timeout < $curtime) {
1072 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1073 $user_cur = $this->user[$i];
1074 if ($user_cur->sess == "" ||
1075 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
1078 if ($user_cur->lacc + EXPIRE_TIME_RD < ($curtime - $delta)) { // Auto logout dell'utente
1079 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
1081 if ($user_cur->stat == 'table') {
1082 log_auth($user_cur->sess," bin5 Autologout session.");
1084 /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE
1086 $tmp_sess = $user_cur->sess;
1087 $user_cur->sess = "";
1088 Bin5_user::step_unproxy($tmp_sess);
1089 $user_cur->name = "";
1090 $user_cur->the_end = FALSE;
1094 /* se gli altri utenti non erano d'accordo questo utente viene bannato */
1095 $remcalc = $this->table[0]->exitlock_calc(&$this->user, $user_cur->table_pos);
1097 require_once("${G_base}Obj/hardban.phh");
1098 Hardbans::add(($user_cur->flags & USER_FLAG_AUTH ? $user_cur->name : FALSE),
1099 $user_cur->ip, $user_cur->sess, $user_cur->laccwr + BAN_TIME);
1101 // $user->bantime = $user->laccwr + BAN_TIME;
1103 $this->table_wakeup($user_cur);
1107 log_rd2($user_cur->sess." GARBAGE UPDATED!");
1109 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
1114 $this->delay_mgr->lastcheck_set($curtime);
1119 function destroy_data()
1125 log_main("DESTROY BRISKIN5 DATA");
1128 log_main("DESTROY2 BRISKIN5 DATA [".$this->table_idx."]");
1129 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1130 $this->user[$i]->destroy_data($this->table_idx);
1131 Bin5_user::unproxy_step($this->table_idx, $this->user[$i]->sess);
1133 if (($tok = @ftok(FTOK_PATH."/bin5/table".$this->table_idx."/table", "B")) == -1)
1136 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
1139 if (shmop_delete($shm) == 0) {
1140 log_only("REMOVE FALLITA ");
1145 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
1147 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
1157 static function lock_data($is_exclusive, $table_idx)
1159 if (($res = file_lock(FTOK_PATH."/bin5/table".$table_idx."/table", $is_exclusive)) != FALSE) {
1160 self::$delta_t = microtime(TRUE);
1161 log_lock("LOCK table [".$table_idx."] [".self::$delta_t[$table_idx]."]");
1163 return (new Vect(array('res' => $res, 'tab' => $table_idx)));
1170 static function unlock_data($res_vect)
1174 $res = $res_vect->getbyid('res');
1175 $tab = $res_vect->getbyid('tab');
1177 log_lock("UNLOCK table [".$tab."] [".(microtime(TRUE) - (self::$delta_t[$tab]))."]");
1183 function chatt_send($user, $mesg)
1185 GLOBAL $mlang_brisk, $G_lang;
1187 if ($user->stat == 'table') {
1188 $table = &$this->table[$user->table];
1191 $user_mesg = substr($mesg,6);
1195 $dt = date("H:i ", $curtime);
1196 if (strncmp($user_mesg, "/nick ", 6) == 0) {
1197 log_main($user->sess." chatt_send BEGIN");
1199 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
1200 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1201 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
1206 $user_mesg = "COMMAND ".$user_mesg;
1209 // update local graph
1210 // update remote graphs
1211 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1212 $user_cur = $this->user[$i];
1213 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1214 if ($user_cur->sess == '')
1216 if (strcasecmp($user_cur->name, $name_new) == 0) {
1217 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1219 $premsg = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
1220 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $premsg);
1225 if ($i == BIN5_MAX_PLAYERS) {
1226 if ($user->flags & USER_FLAG_AUTH && strcasecmp($user->name,$name_new) != 0) {
1227 if ($this->table[$user->table]->auth_only == TRUE) {
1228 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1229 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>%s</b>");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
1234 $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
1238 $user->name = $name_new;
1240 log_main($user->sess." chatt_send start set");
1243 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1244 log_main($user->sess." chatt_send set loop");
1246 $user_cur = &$this->user[$i];
1247 if ($user_cur->sess == '')
1250 if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
1251 $table = &$this->table[$user->table];
1253 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1254 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1255 $this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->flags,
1256 xcape($this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->name),
1258 $this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->flags,
1259 xcape($this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->name),
1261 $this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->flags,
1262 xcape($this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->name),
1264 (BIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->flags),
1265 (BIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->name)),
1267 (BIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->flags),
1268 (BIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->name)));
1269 if ($user_cur == $user) {
1270 $user_cur->comm[$user_cur->step % COMM_N] .= $user_cur->myname_innerHTML();
1272 $user_cur->step_inc();
1278 for ($i = 0 ; $i < ($user->stat == 'room' ? BIN5_MAX_PLAYERS : BIN5_PLAYERS_N) ; $i++) {
1279 if ($user->stat == 'room') {
1280 $user_cur = &$this->user[$i];
1281 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1285 $user_cur = &$this->user[$table->player[$i]];
1288 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1289 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
1290 $dt, $user->flags, xcape($user->name), xcape($user_mesg));
1291 $user_cur->step_inc();
1293 log_legal($curtime, $user->ip, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
1297 function table_wakeup($user)
1299 $table = &$this->table[0];
1301 log_main("BIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
1305 log_main("BIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1307 for ($i = 0 ; $i < $table->player_n ; $i++) {
1308 $user_cur = &$this->user[$i];
1309 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1311 if ($user_cur == $user)
1312 $user_cur->subst = "shutdowner";
1314 $user_cur->subst = "shutdowned";
1315 $user_cur->laccwr = $curtime;
1317 $ret = "gst.st = ".($user_cur->step+1)."; ";
1318 $ret .= 'gst.st_loc++; xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
1320 log_wr($user_cur->sess." BIN5_WAKEUP: ".$ret);
1321 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1322 $user_cur->step_inc();
1325 $this->the_end = TRUE;
1329 * If all players are freezed the room garbage_manager clean up table and users.
1331 function is_abandoned()
1336 $table = &$this->table[0];
1338 for ($i = 0 ; $i < $table->player_n ; $i++) {
1339 $user_cur = &$this->user[$i];
1341 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
1350 static function request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
1352 GLOBAL $G_black_list;
1354 printf("NEW_SOCKET (root): %d\n", intval($new_socket));
1356 $enc = get_encoding($header);
1357 if (isset($header['User-Agent'])) {
1358 if (strstr($header['User-Agent'], "MSIE")) {
1359 $transp_type = "htmlfile";
1362 $transp_type = "xhr";
1366 $transp_type = "iframe";
1368 force_no_cache($header_out);
1370 if (($table_idx = gpcs_var('table_idx', $get, $post, $cookie)) === FALSE)
1373 if (($table_token = gpcs_var('table_token', $get, $post, $cookie)) === FALSE)
1374 unset($table_token);
1381 bin5_index_main($transp_type, $header_out, $addr, $get, $post, $cookie);
1382 $content = ob_get_contents();
1385 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1389 case "index_wr.php":
1390 // FIXME $content can be unset
1391 if (isset($table_idx) && isset($table_token)) {
1392 if (($bri = $s_a_p->app->match_get($table_idx, $table_token)) != FALSE) {
1394 bin5_index_wr_main($bri, $addr, $get, $post, $cookie);
1395 $content = ob_get_contents();
1399 $content = "Bin5 Load data error";
1403 $content = "Bin5 Load data error";
1405 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1409 case "index_rd_ifra.php":
1410 if (($transp = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
1412 if ($transp == 'websocket')
1416 if (!isset($table_idx)
1417 || !isset($table_token)
1418 || !isset($cookie['sess'])
1419 || ($bri = $s_a_p->app->match_get($table_idx, $table_token)) == NULL
1420 || (($user = $bri->get_user($cookie['sess'], $idx)) == FALSE)) {
1422 $content = Bin5_user::stream_fini($transp_type, $s_a_p->rndstr, TRUE);
1423 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1428 // close a previous opened index_read_ifra socket, if exists
1429 if (($prev = $user->rd_socket_get()) != NULL) {
1430 $s_a_p->socks_unset($user->rd_socket_get());
1431 fclose($user->rd_socket_get());
1432 printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
1433 $user->rd_socket_set(NULL);
1437 $user->stream_init($s_a_p->rndstr, $enc, $header, $header_out, $content, $get, $post, $cookie);
1439 $response = headers_render($header_out, -1).$user->chunked_content($content);
1440 $response_l = mb_strlen($response, "ASCII");
1442 $wret = @fwrite($new_socket, $response, $response_l);
1443 if ($wret < $response_l) {
1444 printf("TROUBLES WITH FWRITE: %d\n", $wret);
1445 $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
1448 $user->rd_cache_set("");
1450 fflush($new_socket);
1453 $s_a_p->socks_set($new_socket, $user, NULL);
1454 $user->rd_socket_set($new_socket);
1455 printf(" - qui ci siamo - ");
1474 function locshm_exists($tok)
1478 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
1479 log_main($tok." SHM NOT exists");
1485 log_main($tok." SHM exists");
1494 is_transition (is from room to table ?)
1495 is_again (is another game)
1497 Examples of $is_transition, $is_again:
1498 from reload of the page: FALSE, FALSE
1499 from sitdown in room: TRUE, FALSE
1500 from table: asta cmd e tutti passano: TRUE, TRUE
1501 from table: fine partita: TRUE, TRUE
1503 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1505 $table_idx = $user->table;
1506 $table = $bri->table[$table_idx];
1507 $table_pos = $user->table_pos;
1509 $ret = "table_init();";
1510 $ret .= $table->exitlock_show(&$bri->user, $table_pos);
1512 /* GENERAL STATUS */
1513 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1514 $sendstep, $user->stat, $user->subst, $table_pos);
1516 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1519 $ret .= "background_set();";
1522 $ret .= $user->myname_innerHTML();
1523 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1524 $bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->flags,
1525 xcape($bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->name),
1527 $bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->flags,
1528 xcape($bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->name),
1530 $bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->flags,
1531 xcape($bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->name),
1533 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->flags),
1534 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->name)),
1536 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->flags),
1537 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->name)));
1539 /* NOTIFY FOR THE CARD MAKER */
1540 if ($is_transition) { // && $user->subst == "asta" superfluo
1541 $ret .= show_table_info(&$bri, &$table, $table_pos);
1542 $ret .= "setTimeout(preload_images, 500, g_preload_img_arr, g_imgct);";
1545 $ret .= table_welcome($user);
1547 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1548 $ret .= playsound("cow.mp3");
1553 if ($is_transition) { // && $user->subst == "asta" superfluo
1556 for ($i = 0 ; $i < BIN5_CARD_HAND ; $i++) {
1557 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++) {
1559 for ($o = 0 ; $o < (BIN5_CARD_HAND * BIN5_PLAYERS_N) && $ct < $i+1 ; $o++) {
1560 // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1561 if ($table->card[$o]->owner == (($e + $table->gstart) % BIN5_PLAYERS_N)) {
1567 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1569 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BIN5_PLAYERS_N,
1570 $i, ((($e + BIN5_PLAYERS_N - $table_pos + $table->gstart) % BIN5_PLAYERS_N) == 0 ?
1571 $table->card[$o]->value : -1),
1572 ($i == 7 && $e == (BIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1577 $taked = array(0,0,0,0,0);
1578 $inhand = array(0,0,0,0,0);
1579 $ontabl = array(-1,-1,-1,-1,-1);
1582 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1583 // for ($i = 0 ; $i < 40 ; $i++) {
1584 if ($table->card[$i]->stat == 'hand') {
1585 if ($table->card[$i]->owner == $table_pos) {
1586 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1588 $inhand[$table->card[$i]->owner]++;
1590 else if ($table->card[$i]->stat == 'take') {
1591 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1592 $taked[$table->card[$i]->owner]++;
1594 else if ($table->card[$i]->stat == 'table') {
1595 $ontabl[$table->card[$i]->owner] = $i;
1599 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1600 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1602 log_main("Stat table: ".$logg);
1604 /* Set ours cards. */
1606 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1607 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1608 for ($i = $inhand[$table_pos] ; $i < BIN5_CARD_HAND ; $i++)
1609 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1610 $ret .= sprintf('card_setours(%s);', $oursarg);
1612 /* Dispose all cards */
1613 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1614 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1615 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1616 ($inhand[$i] <= BIN5_CARD_HAND ? $inhand[$i] : BIN5_CARD_HAND), $taked[$i]);
1618 if ($ontabl[$i] != -1) {
1619 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1620 $table->card[$ontabl[$i]]->value,
1621 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1627 if ($user->subst == 'asta') {
1629 /* show users auction status */
1631 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1632 $user_cur = &$bri->user[$table->player[$i]];
1633 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1634 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1636 if (BIN5_PLAYERS_N == 3)
1637 $showst .= ",-2,-2";
1638 $ret .= sprintf('document.title = "Brisk - Tavolo %d (asta)";', $user->table_orig);
1639 $ret .= sprintf('show_astat(%s);', $showst);
1641 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1642 /* show card chooser */
1643 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1648 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N) &&
1649 $table->asta_win == -1)
1650 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1651 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1653 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1654 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1658 if ($table->asta_win == -1) { // auction case
1659 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N))
1660 $ret .= "remark_on();";
1662 $ret .= "remark_off();";
1664 else { // chooseed case
1665 if ($table_pos == $table->asta_win)
1666 $ret .= "remark_on();";
1668 $ret .= "remark_off();";
1671 else if ($user->subst == 'game') {
1673 if (($table->gstart + $table->turn) % BIN5_PLAYERS_N == $table_pos)
1674 $ret .= "is_my_time = true; remark_on();";
1676 $ret .= "remark_off();";
1678 /* WHO CALL AND WHAT */
1679 $ret .= briscola_show($bri, $table, $user);
1683 } // end function show_table(...
1685 function calculate_winner(&$table)
1692 $cur_seed = $table->briscola - ($table->briscola % 10);
1694 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1695 // for ($i = 0 ; $i < 40 ; $i++) {
1696 if ($table->card[$i]->stat != "table")
1699 log_wr(sprintf("Card On table: [%d]", $i));
1701 $v = $table->card[$i]->value;
1702 $ontab[$table->card[$i]->owner] = $v;
1703 $ontid[$table->card[$i]->owner] = $i;
1704 /* se briscola setto il flag */
1705 if (($v - ($v % 10)) == $cur_seed)
1709 if ($briontab == FALSE) {
1710 $cur_win = $table->gstart;
1711 $cur_val = $ontab[$cur_win];
1712 $cur_seed = $cur_val - ($cur_val % 10);
1715 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1716 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1717 if ($ontab[$i] < $cur_val) {
1718 $cur_val = $ontab[$i];
1724 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1725 $table->card[$ontid[$i]]->owner = $cur_win;
1726 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1731 function show_table_info(&$bri, &$table, $table_pos)
1733 GLOBAL $G_lang, $mlang_bin5_bin5;
1736 $user = $bri->user[$table->player[$table_pos]];
1738 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1739 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1742 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++)
1743 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1744 $noty .= sprintf("</tr>");
1747 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1749 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1750 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1751 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1752 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1757 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1758 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1759 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1760 $noty .= "</tr></table>";
1762 if ($table->old_reason != "") {
1763 $noty .= $table->old_reason;
1766 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1767 if ($table->mazzo == $table_pos)
1768 $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
1770 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1771 $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
1774 if ($user->subst == 'asta') {
1775 if ($table->asta_win == -1) // auction case
1776 $curplayer = $table->gstart % BIN5_PLAYERS_N;
1778 $curplayer = $table->asta_win;
1780 else if ($user->subst == 'game') {
1781 $curplayer = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
1784 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1785 if ($curplayer == $table_pos) {
1786 $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
1789 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1790 $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
1793 $multer = $table->multer(TRUE);
1795 $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($multer) );
1797 $noty .= "<hr><br>";
1798 $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
1799 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
1800 * 130 + ($table->points_n > 0 ? 50 : 0) +
1801 * (120 * ($table->points_n / MAX_POINTS)));
1802 * will be used when we refact notify js function following
1803 * photoo class logic
1809 function table_welcome($user)
1811 GLOBAL $table_wellarr, $G_lang;
1814 for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
1815 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
1821 function briscola_show($bri, $table, $user)
1823 GLOBAL $G_lang, $mlang_bin5_bin5;
1827 if ($table->asta_card == 9)
1828 $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
1830 /* text of caller cell */
1831 if ($user->table_pos == $table->asta_win) {
1832 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
1833 $ret .= sprintf($prestr, $ptnadd);
1836 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
1837 $ret .= sprintf($prestr,
1838 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1840 $ret .= sprintf('set_iscalling(%d);', ($table->asta_win - $user->table_pos + BIN5_PLAYERS_N) % BIN5_PLAYERS_N);
1842 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1844 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1845 $ret .= sprintf('$("caller").style.visibility = "visible";');
1846 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1847 $ret .= sprintf('$("astalascio").style.visibility = "";');
1848 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1849 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1855 function game_result($asta_pnt, $pnt)
1857 if ($asta_pnt == 61) {
1860 else if ($pnt == 60)
1866 if ($pnt >= $asta_pnt)
1873 function log_points($remote_addr, $curtime, $user, $where, $mesg)
1875 if (($fp = @fopen(LEGAL_PATH."/points.log", 'a')) != FALSE) {
1876 /* Unix time | session | nickname | IP | where was | mesg */
1877 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
1878 ($user->flags & USER_FLAG_AUTH ? 'A' : 'N'),
1879 $user->name, $remote_addr, $where , $mesg));
1884 function game_description($act, $form, $old_mult, $win = -1, $win_name = "?1?", $fri = -1, $fri_name = "?2?",
1885 $old_pnt = -1, $old_asta_pnt = -1)
1887 GLOBAL $G_lang, $mlang_bin5_bin5;
1889 if ($form == 'html') {
1894 $win_name = xcape($win_name);
1895 $fri_name = xcape($fri_name);
1904 if ($act == BIN5_RULES_OLDSCHEMA) {
1907 else if ($act == BIN5_RULES_ALLPASS) {
1908 return (sprintf($mlang_bin5_bin5['info_alpa'][$G_lang],
1909 $tg_br, $tg_hr, $tg_bo, $tg_bc));
1911 else if ($act == BIN5_RULES_ABANDON) {
1912 return (sprintf($mlang_bin5_bin5['info_aban'][$G_lang],
1913 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1917 $wol = game_result($old_asta_pnt, $old_pnt);
1921 if ($win != $fri) { // not alone case
1923 /* 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" */
1925 $noty .= sprintf($mlang_bin5_bin5['info_part'][$G_lang],
1926 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1929 if ($old_pnt == 120) {
1930 $noty .= sprintf($mlang_bin5_bin5['info_capp'][$G_lang],
1931 $tg_br, $tg_hr, $tg_bo, $tg_bc );
1934 if ($old_asta_pnt > 61) {
1935 $noty .= sprintf($mlang_bin5_bin5['info_alea'][$G_lang],
1936 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1937 $old_asta_pnt, $old_pnt,
1938 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
1939 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
1940 $mlang_bin5_bin5['info_lost'][$G_lang])));
1943 $noty .= sprintf($mlang_bin5_bin5['info_more'][$G_lang],
1944 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1946 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
1947 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
1948 $mlang_bin5_bin5['info_lost'][$G_lang])));
1949 } // else of if ($old_asta_pnt > 61) {
1950 } // else of if ($old_pnt == 120) {
1951 } // if ($win != $fri) { // not alone case
1953 $noty .= sprintf($mlang_bin5_bin5['info_alon'][$G_lang],
1954 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1956 if ($old_pnt == 120) {
1957 $noty .= sprintf($mlang_bin5_bin5['info_acap'][$G_lang],
1958 $tg_br, $tg_hr, $tg_bo, $tg_bc);
1961 if ($old_asta_pnt > 61) {
1962 $noty .= sprintf($mlang_bin5_bin5['info_aleaa'][$G_lang],
1963 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1964 $old_asta_pnt, $old_pnt,
1965 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
1966 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
1967 $mlang_bin5_bin5['info_lost'][$G_lang])));
1970 $noty .= sprintf($mlang_bin5_bin5['info_morea'][$G_lang],
1971 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1973 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
1974 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
1975 $mlang_bin5_bin5['info_lost'][$G_lang])));
1981 $old_multer = Bin5_table::s_multer($old_mult, $old_asta_pnt);
1982 if ($old_multer > 1) {
1983 $noty .= sprintf($mlang_bin5_bin5['info_omul'][$G_lang],
1984 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1985 multoval($old_multer));
1987 $noty .= sprintf('%2$s%1$s', $tg_br, $tg_hr);