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_FINISH', 0);
34 define('BIN5_RULES_ABANDON', 1);
35 define('BIN5_RULES_ALLPASS', 2);
37 $mlang_bin5_bin5 = array(
38 // br, hr, b, /b, win, fri
39 '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',
40 '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'),
42 'info_capp' => array( 'it' => 'hanno fatto %3$scappotto%4$s EBBRAVI!.%1$s',
43 'en' => 'and they made %3Dscapot%4$s WELL DONE!.%1$s'),
45 // br, hr, b, /b, old_asta_pnt, old_pnt, winornot
46 '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',
47 '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'),
48 // br, hr, b, /b, old_pnt, winornot
49 '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',
50 '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'),
51 'info_win' => array( 'it' => 'vinto',
53 'info_peer' => array( 'it' => 'pareggiato',
55 'info_lost' => array( 'it' => 'perso',
58 // br, hr, b, /b, win_name
59 'info_alon' => array( 'it' => 'Nell\'ultima partita %3$s%5$s%4$s si è chiamato in mano,%1$s',
60 'en' => 'In the last hand %3$s%5$s%4$s play alone against each other,%1$s'),
61 // br, hr, b, /b, old_asta_pnt, old_pnt, winornot
62 'info_aleaa' => array( 'it' => 'doveva fare %3$salmeno %5$d%4$s punti e ne ha fatti %3$s%6$d%4$s: ha %6$s.%1$s',
63 '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'),
64 // br, hr, b, /b, old_pnt, winornot
65 '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',
66 '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'),
68 'info_acap' => array( 'it' => 'ha fatto %3$scappotto%4$s EBBRAVO!.%1$s',
69 'en' => 'and he/she made %3$scapot%4$s WELL DONE!.%1$s'),
72 'info_omul' => array( 'it' => '%1$sLa partita valeva %3$s%5$s%4$s.%1$s',
73 'en' => '%1$sEN: The game was worth %3$s%5$s%4$s.%1$s' ),
76 'info_alpa' => array( 'it' => '%1$sHanno passato %3$stutti%4$s.%1$s',
77 'en' => '%1$sEN: Hanno passato %3$stutti%4$s.%1$s' ),
79 // br, hr, b, /b, aband-name
80 'info_aban' => array( 'it' => ' Ha lasciato %3$s%5$s%4$s perché aveva al massimo %3$s2 punti%4$s.',
81 'en' => ' EN: Ha lasciato %3$s%5$s%4$s perché aveva al massimo %3$s2 punti%4$s.'),
83 'info_shuf' => array( 'it' => 'Il mazzo a <b>%s</b>, ',
84 'en' => '<b>%s</b> shuffled the cards, '),
85 'info_yturn'=> array( 'it' => ' tocca a <b>te</b> giocare.',
86 'en' => ' it\'s <b>your</b> turn.'),
87 'info_turn' => array( 'it' => 'tocca a <b>%s</b> giocare.',
88 'en' => 'it\'s the <b>%s</b>\'s turn.'),
89 'info_mult' => array( 'it' => ' La partita vale <b>%s</b>.',
90 'en' => ' The game worth <b>%s</b>.' ),
91 'info_yshuf'=> array( 'it' => 'Fai <b>tu</b> il mazzo, ',
92 'en' => 'It\'s <b>your</b> shuffled the cards, '),
93 'btn_bkgame'=> array( 'it' => 'torna alla partita',
94 'en' => 'back to the game'),
95 'call_wptn' => array( 'it' => '<br>con %d punti',
96 'en' => '<br>with %d points'),
97 'call_ycall'=> array( 'it' => 'Chiami%s:',
99 'call_call' => array( 'it' => 'Chiama %s%s:',
100 'en' => 'The declarer is %s%s:')
104 $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.'),
105 '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.') );
107 function multoval($mult)
112 return ($G_lang == 'en' ? 'double' : 'doppio');
114 return ($G_lang == 'en' ? 'triple' : 'triplo');
116 return ($G_lang == 'en' ? 'quadruple' : 'quadruplo');
118 return (sprintf(($G_lang == 'en' ? "%d-ple" : "%d-plo"), $mult));
123 var $value; /* 0 - 39 card value */
124 var $stat; /* 'bunch', 'hand', 'table', 'take' */
125 var $owner; /* (table position 0-4) */
126 // var $pos; /* Pos in hand. */
127 var $x; /* When played the X position on the table of the owner. */
128 var $y; /* When played the Y position on the table of the owner. */
130 function Card($value, $stat, $owner)
132 $this->value = $value;
133 $this->stat = $stat; // Card stat
134 $this->owner = $owner;
137 function assign($stat,$owner)
139 $this->stat = $stat; // Card stat
140 $this->owner = $owner;
143 function setpos($pos)
150 $this->stat = 'table'; // Card stat
155 function take($newown)
157 $this->stat = 'take'; // Card stat
158 $this->owner = $newown;
162 class Bin5_table extends Table {
163 var $card; // il mazzo di carte
164 var $mazzo; // chi e' di mazzo
174 var $points; // points array
175 var $points_n; // number of row of points
178 var $asta_win; // the caller idx position at table
180 var $friend; // the callee idx position at table
182 var $old_act; // last action that trigs the end of the game
187 var $old_pnt; // points made by caller and callee
188 var $old_asta_win; // the old caller idx position at table
189 var $old_friend; // the old callee idx position at table
191 function Bin5_table()
197 function create($idx)
199 if (($thiz =& new Bin5_table()) == FALSE)
205 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
206 $thiz->asta_pla_n= -1;
207 $thiz->asta_card = -1;
208 $thiz->asta_pnt = -1;
211 $thiz->points = array( );
213 $thiz->total = array( 0, 0, 0, 0, 0);
214 $thiz->asta_win = -1;
215 $thiz->briscola = -1;
220 $thiz->old_mazzo = -1;
221 $thiz->old_reason = "";
222 $thiz->old_asta_pnt = -1;
223 $thiz->old_mult = -1;
225 $thiz->old_asta_win = -1;
226 $thiz->old_friend = -1;
233 function myclone(&$from)
235 if (($thiz =& new Bin5_table()) == FALSE)
240 $thiz->card = $from->card;
241 $thiz->mazzo = $from->mazzo; // REVIEW
242 $thiz->gstart = $from->gstart;
243 $thiz->turn = $from->turn;
245 $thiz->asta_pla = $from->asta_pla;
246 $thiz->asta_pla_n = $from->asta_pla_n;
247 $thiz->asta_card = $from->asta_card;
248 $thiz->asta_pnt = $from->asta_pnt;
250 $thiz->mult = $from->mult;
251 $thiz->points = $from->points;
252 $thiz->points_n = $from->points_n;
253 $thiz->total = $from->total;
255 $thiz->asta_win = $from->asta_win;
256 $thiz->briscola = $from->briscola;
257 $thiz->friend = $from->friend;
259 $thiz->old_act = $from->old_act;
260 $thiz->old_mazzo = $from->old_mazzo;
261 $thiz->old_reason = $from->old_reason;
262 $thiz->old_asta_pnt = $from->old_asta_pnt;
263 $thiz->old_mult = $from->mult;
264 $thiz->old_pnt = $from->old_pnt;
265 $thiz->old_asta_win = $from->old_asta_win;
266 $thiz->old_friend = $from->old_friend;
272 function parentcopy(&$from)
277 function spawn(&$from)
279 if (($thiz =& new Bin5_table()) == FALSE)
282 $thiz->parentcopy($from);
284 log_main("PLAYER_N - spawn.".$thiz->player_n);
286 $thiz->card = array();
287 $thiz->bunch_create();
288 $thiz->mazzo = rand(0,PLAYERS_N-1);
291 $thiz->old_asta_win = -1;
292 $thiz->old_reason = "";
294 // players are rearranged in an dedicated array
295 $thiz->player = array();
296 for ($i = 0 ; $i < $from->player_n ; $i++)
297 $thiz->player[$i] = $i;
299 log_main("TABLE_OLD_WIN - spawn:".$thiz->old_asta_win);
304 static function asta2mult($asta_pnt)
308 else if ($asta_pnt > 100)
310 else if ($asta_pnt > 90)
312 else if ($asta_pnt > 80)
314 else if ($asta_pnt > 70)
320 function multer($is_new)
323 // return (pow(2, $this->mult) * $this->asta2mult($this->asta_pnt));
324 return (static::s_multer($this->mult, $this->asta_pnt));
327 // return (pow(2, $this->old_mult) * $this->asta2mult($this->old_asta_pnt));
328 return (static::s_multer($this->old_mult, $this->old_asta_pnt));
332 static function s_multer($mult, $pnt)
334 return (pow(2, $mult) * static::asta2mult($pnt));
337 // function bunch_create_old() function AND
341 // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
342 // // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
343 // $ret[$i] =& new Card($i, 'bunch', 'no_owner');
350 function bunch_create()
354 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
355 $this->card[$i] = new Card($i, 'bunch', 'no_owner');
359 function bunch_make()
361 log_main("bunch_make start");
362 $ct = array(0,0,0,0,0);
364 mt_srand(make_seed());
366 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--)
369 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--) {
373 log_main("RND ZERO");
377 $owner = $i % BIN5_PLAYERS_N;
378 $this->card[$id]->assign('hand', $owner);
380 $rest[$rn] = $rest[$i];
381 // $pubbpos[$rn2] = $pubbpos[$i];
383 log_main("bunch_make end");
386 function init($userarr)
389 $this->mazzo = rand(0,PLAYERS_N-1);
392 $this->old_asta_win = -1;
393 $this->old_reason = "";
395 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
396 $this->total[$i] = 0;
397 $user_cur = $userarr[$this->player[$i]];
398 $user_cur->exitislock = TRUE;
401 log_main("table::init: ci siamo");
404 function game_init($userarr)
408 $this->gstart = ($this->mazzo+1) % BIN5_PLAYERS_N;
411 $this->asta_pla_n = BIN5_PLAYERS_N;
412 $this->asta_card = -1;
413 $this->asta_pnt = 60;
414 $this->asta_win = -1;
415 $this->briscola = -1;
419 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
420 $this->asta_pla[$i] = TRUE;
421 $user_cur = $userarr[$this->player[$i]];
422 $user_cur->subst = 'asta';
423 $user_cur->asta_card = -2;
424 $user_cur->asta_pnt = -1;
425 $user_cur->handpt = $this->hand_points($i);
430 function game_next($delta)
432 $this->old_mazzo = $this->mazzo;
433 $this->mazzo = ($this->mazzo + $delta) % BIN5_PLAYERS_N;
436 function mult_inc($val)
438 $this->old_mult = $this->mult;
442 function mult_set($val)
444 $this->old_mult = $this->mult;
448 function hand_points($idx)
450 GLOBAL $G_all_points;
454 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
455 // for ($i = 0 ; $i < 40 ; $i++) {
456 if ($this->card[$i]->owner != $idx)
459 $ctt = $this->card[$i]->value % 10;
460 $tot += $G_all_points[$ctt];
467 function exitlock_show($userarr, $table_pos)
469 $ct = $this->exitlock_calc($userarr, $table_pos);
471 $ret = sprintf('exitlock_show(%d, %s);', $ct,
472 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
476 function exitlock_calc(&$userarr, $table_pos)
480 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
481 if ($userarr[$this->player[$i]]->exitislock == FALSE)
488 function rules_engine($bri, $curtime, $action, $user)
490 GLOBAL $G_all_points, $G_dbasetype;
494 $this->old_act = $action;
495 if ($action == BIN5_RULES_ALLPASS) { // return TRUE if all correct
496 $this->old_asta_win = -1;
499 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
504 // $this->game_next(1);
505 $this->game_init(&$bri->user);
507 else if ($action == BIN5_RULES_ABANDON) { // return TRUE if all correct
508 log_wr(sprintf("GIOCO FINITO !!!"));
509 $this->old_asta_win = $user->table_pos;
513 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
517 // Non si cambia mazzo se si abbandona la partita
519 // $this->game_next(0);
520 $this->game_init(&$bri->user);
522 else if ($action == BIN5_RULES_FINISH) { // return TRUE if all correct
526 if ($this->asta_pnt == 60)
527 $this->asta_pnt = 61;
529 $this->old_reason = "";
531 // count points for the temporary 2 teams
532 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
533 $ctt = $this->card[$i]->value % 10;
534 $own = $this->card[$i]->owner;
535 if ($own == $this->asta_win || $own == $this->friend)
536 $pro += $G_all_points[$ctt];
539 log_wr(sprintf("PRO: [%d]", $pro));
542 if ($this->asta_pnt == 61 && $pro == 60) {
543 $this->points[$this->points_n % MAX_POINTS] = array();
544 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
545 $this->points[$this->points_n % MAX_POINTS][$i] = 0;
549 $this->old_pnt = $pro;
556 if ($pro >= $this->asta_pnt)
561 $this->points[$this->points_n % MAX_POINTS] = array();
562 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
563 if ($i == $this->asta_win)
564 $pt = ($i == $this->friend ? 4 : 2);
565 else if ($i == $this->friend)
570 log_wr(sprintf("PRO: pt[%d][%d] = %d", $this->points_n % MAX_POINTS, $i, $pt));
572 $pt = $pt * $sig * $this->multer(TRUE) * ($pro == 120 ? 2 : 1);
574 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $this->multer(TRUE), ($pro == 120 ? 2 : 1)));
576 $this->points[$this->points_n % MAX_POINTS][$i] = $pt;
577 $this->total[$i] += $pt;
581 $this->old_pnt = $pro;
582 $this->old_asta_win = $this->asta_win;
592 $this->game_next($game_delta);
594 $plist = "$this->table_token|$user->table_orig|$this->player_n";
597 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
598 $user_cur = &$bri->user[$this->player[$i]];
601 $ucodes[$i] = $user_cur->code_get();
604 $plist .= '|'.xcapelt($user_cur->name).'|'.$pts[$i];
605 $codes .= '|'.xcapelt($user_cur->code_get());
608 log_legal($curtime, $user->ip, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
610 $this->old_asta_pnt = $this->asta_pnt;
611 // $this->old_mazzo is managed by ->game_next();
612 // $this->old_mult, $this->old_pnt, $this->old_reason and $this->old_asta_win are specific
614 $this->old_friend = $this->friend;
615 $this->old_reason = game_description($action, 'html', $this->old_mult,
616 $this->old_asta_win, $bri->user[$this->player[$this->old_asta_win]]->name,
617 $this->old_friend, $bri->user[$this->player[$this->old_friend]]->name,
618 $this->old_pnt, $this->old_asta_pnt);
621 if ($user->table_orig < TABLES_AUTH_N) {
622 require_once("../Obj/dbase_".$G_dbasetype.".phh");
624 if (($bdb = BriskDB::create()) != FALSE) {
625 $bdb->bin5_points_save($curtime, $this, $user->table_orig, $action, $ucodes, $pts);
629 log_points($remote_addr, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED");
631 log_points($remote_addr, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
634 $this->game_init(&$bri->user);
639 } // end class Bin5_table
644 define('BIN5_USER_FLAG_RING_ENDAUCT', 0x01);
646 class Bin5_user extends User {
649 var $handpt; // Total card points at the beginning of the current hand.
650 var $exitislock; // Player can exit from the table ?
651 var $privflags; // Flags for briskin5 only
659 function create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
660 if (($thiz =& new User()) == FALSE)
663 $thiz->asta_card = -2;
664 $thiz->asta_pnt = -1;
666 $thiz->exitislock = TRUE;
667 $thiz->privflags = 0;
673 function parentcopy(&$from)
678 function copy(&$from)
680 $this->parentcopy($from);
682 $this->asta_card = $from->asta_card;
683 $this->asta_pnt = $from->asta_pnt;
684 $this->handpt = $from->handpt;
685 $this->exitislock = $from->exitislock;
686 $this->privflags = $from->privflags;
690 function myclone(&$from)
692 if (($thiz =& new User()) == FALSE)
701 static function spawn($from, &$bri, $table, $table_pos, $get, $post, $cookie)
703 if (($thiz = new Bin5_user()) == FALSE)
706 if (($CO_bin5_pref_ring_endauct = gpcs_var("CO_bin5_pref_ring_endauct", $get, $post, $cookie)) === FALSE) {
707 $CO_bin5_pref_ring_endauct = "";
710 $thiz->parentcopy($from);
712 /* NOTE: at this moment idx and table_pos fields have the same value
713 but diffentent functions, we keep them separated for a while */
715 $thiz->idx = $table_pos;
716 $thiz->asta_card = -2;
717 $thiz->asta_pnt = -1;
719 $thiz->exitislock = TRUE;
721 log_wr("Bin5 constructor");
723 $thiz->privflags = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
725 $thiz->table_orig = $table;
727 $thiz->table_pos = $table_pos;
734 function step_set($step)
736 $this->step = $step & 0x7fffffff;
741 function step_inc($delta = 1) {
742 $this->step += $delta;
743 /* modularization because unpack() not manage unsigned 32bit int correctly */
744 $this->step &= 0x7fffffff;
749 static function load_step($tab_id, $sess)
753 if (validate_sess($sess) == FALSE)
756 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
757 mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
758 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step", 'rb')) == FALSE)
760 if (($s = fread($fp, 8)) == FALSE)
762 if (mb_strlen($s, "ASCII") != 8)
764 $arr = unpack('Ls/Li', $s);
767 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
774 log_rd2("STEP_GET [".$sess."]: return false ");
782 if (validate_sess($this->sess) == FALSE)
784 if (file_exists(BIN5_PROXY_PATH."/table".$this->table_orig) == FALSE)
785 mkdir(BIN5_PROXY_PATH."/table".$this->table_orig, 0775, TRUE);
786 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$this->table_orig."/".$this->sess.".step", 'w')) == FALSE)
788 fwrite($fp, pack("LL",$this->step, $this->idx));
791 log_main("step_set [".$this->sess. "] [".$this->step."]");
799 static function unproxy_step($tab_id, $sess)
801 log_rd2("UNPROXY: ".BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
802 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
805 @unlink(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
808 function destroy_data($tab_id)
811 if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos, "B")) == -1) {
812 log_crit("BIN5 USER DATA REMOVE FAILED 1 [".FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos."]");
816 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
817 log_crit("BIN5 USER DATA REMOVE FAILED 2");
820 if (shmop_delete($shm) == 0) {
821 log_crit("BIN5 USER DATA REMOVE FAILED 3");
826 log_main("BIN5 USER DATA DESTROY SUCCESS");
828 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
838 static function blocking_error($is_unrecoverable)
840 log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
841 return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
844 protected function page_sync($sess, $page)
846 log_rd2("PAGE_SYNC");
847 printf("xXx BIN5_USER::PAGE_SYNC\n");
848 return (sprintf('xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
851 protected function maincheck($cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate, $table_idx, $table_token)
853 GLOBAL $G_lang, $mlang_indrd;
854 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
855 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
856 $CO_splashdate = "CO_splashdate".$G_splash_idx;
857 $$CO_splashdate = $splashdate;
861 log_rd("maincheck begin");
866 /* Nothing changed, return. */
867 if ($cur_step == $this->step)
870 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$this->stat."] cur_step[".$cur_step."] user_step[".$this->step."]");
872 if ($cur_step == -1) {
874 * if $cur_step == -1 load the current state from the main struct
877 $S_load_stat['wR_minusone']++;
879 // if ($this->the_end == TRUE) {
880 // log_rd2("main_check: the end".var_export(debug_backtrace()));
883 if ($this->trans_step != -1) {
884 log_rd2("TRANS USATO ".$this->trans_step);
885 $cur_step = $this->trans_step;
886 $this->trans_step = -1;
889 log_rd2("TRANS NON ATTIVATO");
894 /* this part I suppose is read only on $this->room structure */
895 if ($cur_step == -1) {
896 log_rd2("PRE-NEWSTAT");
903 if ($this->stat == 'table') {
905 /* NOTE: $this->room is associated with the current $bri object */
906 printf("xXx CLASS NAME [%s]\n", get_class($this->room));
907 $ret = show_table(&$this->room, $this, $this->step, FALSE, FALSE);
909 log_rd2("NEWSTAT: ".$this->stat);
911 $new_stat = $this->stat;
912 $new_subst = $this->subst;
913 $new_step = $this->step;
914 } /* if ($cur_step == -1) { */
916 /* $sem = Room::lock_data(FALSE); */
917 $S_load_stat['rU_heavy']++;
919 if ($cur_step < $this->step) {
921 if ($cur_step + COMM_N < $this->step) {
922 if (($cur_stat != $this->stat)) {
923 $to_stat = $this->stat;
924 /* Room::unlock_data($sem); */
926 printf("xXx BIN5_USER::MAINCHECK\n");
927 return ($this->page_sync($this->sess, ($to_stat == "table" ? "index.php" : "../index.php"), $this->table, $this->table_token));
929 log_rd2("lost history, refresh from scratch");
930 printf("xXx LOST HISTORY!\n");
934 for ($i = $cur_step ; $i < $this->step ; $i++) {
936 log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
937 $ret .= $this->comm[$ii];
939 $new_stat = $this->stat;
940 $new_subst = $this->subst;
941 $new_step = $this->step;
944 log_rd2($this->step, 'index_rd.php: after ret set');
946 if ($this->the_end == TRUE) {
947 log_rd2("LOGOUT BYE BYE!!");
948 log_auth($this->sess, "Explicit logout.");
950 if ($this->the_end == TRUE) {
954 /* if ($this->subst == 'sitdown') { */
955 /* log_load("ROOM WAKEUP"); */
956 /* $this->room->room_wakeup($this); */
958 /* else if ($this->subst == 'standup') */
959 /* $this->room->room_outstandup($this); */
961 /* log_rd2("LOGOUT FROM WHAT ???"); */
963 } /* if ($this->the_end == TRUE) { ... */
964 } /* if ($this->the_end == TRUE) { ... */
965 } /* if ($cur_step < $this->step) { */
967 /* Room::unlock_data($sem); */
968 } /* else of if ($cur_step == -1) { */
972 } // function maincheck (...
979 } // end class Bin5_user
984 static $delta_t = array();
988 var $comm; // commands for many people
989 var $step; // current step of the comm array
990 var $garbage_timeout;
1001 function Bin5 ($room, $table_idx, $table_token, $get, $post, $cookie) {
1002 $this->user = array();
1003 $this->table = array();
1005 $this->the_end = FALSE;
1006 $this->shm_sz = BIN5_SHM_MIN;
1007 if (($this->tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
1012 $user = $room->user;
1013 $table = $room->table[$table_idx];
1015 log_wr("Bin5 constructor");
1017 for ($i = 0 ; $i < $table->player_n ; $i++) {
1018 $user[$table->player[$i]]->table_token = $table_token;
1019 $this->user[$i] = Bin5_user::spawn($user[$table->player[$i]], $this, $table_idx, $i, $get, $post, $cookie);
1021 $this->table[0] = Bin5_table::spawn(&$table);
1023 log_main("TABLE_OLD_WIN - Bin5:".$this->table[0]->old_asta_win);
1025 $this->table_idx = $table_idx;
1026 $this->table_token = $table_token;
1027 $this->garbage_timeout = 0;
1029 $this->delay_mgr = new Delay_Manager((GARBAGE_TIMEOUT *3.0) / 2.0);
1031 log_wr("Bin5 constructor end");
1035 function get_user($sess, &$idx)
1039 if (validate_sess($sess)) {
1040 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1041 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1044 $ret = &$this->user[$i];
1048 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1049 // for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++)
1050 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1053 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1060 function garbage_manager($force)
1064 /* Garbage collector degli utenti in timeout */
1068 $delta = $this->delay_mgr->delta_get($curtime);
1070 if ($force || $this->garbage_timeout < $curtime) {
1071 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1072 $user_cur = $this->user[$i];
1073 if ($user_cur->sess == "" ||
1074 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
1077 if ($user_cur->lacc + EXPIRE_TIME_RD < ($curtime - $delta)) { // Auto logout dell'utente
1078 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
1080 if ($user_cur->stat == 'table') {
1081 log_auth($user_cur->sess," bin5 Autologout session.");
1083 /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE
1085 $tmp_sess = $user_cur->sess;
1086 $user_cur->sess = "";
1087 Bin5_user::step_unproxy($tmp_sess);
1088 $user_cur->name = "";
1089 $user_cur->the_end = FALSE;
1093 /* se gli altri utenti non erano d'accordo questo utente viene bannato */
1094 $remcalc = $this->table[0]->exitlock_calc(&$this->user, $user_cur->table_pos);
1096 require_once("${G_base}Obj/hardban.phh");
1097 Hardbans::add(($user_cur->flags & USER_FLAG_AUTH ? $user_cur->name : FALSE),
1098 $user_cur->ip, $user_cur->sess, $user_cur->laccwr + BAN_TIME);
1100 // $user->bantime = $user->laccwr + BAN_TIME;
1102 $this->table_wakeup($user_cur);
1106 log_rd2($user_cur->sess." GARBAGE UPDATED!");
1108 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
1113 $this->delay_mgr->lastcheck_set($curtime);
1118 function destroy_data()
1124 log_main("DESTROY BRISKIN5 DATA");
1127 log_main("DESTROY2 BRISKIN5 DATA [".$this->table_idx."]");
1128 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1129 $this->user[$i]->destroy_data($this->table_idx);
1130 Bin5_user::unproxy_step($this->table_idx, $this->user[$i]->sess);
1132 if (($tok = @ftok(FTOK_PATH."/bin5/table".$this->table_idx."/table", "B")) == -1)
1135 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
1138 if (shmop_delete($shm) == 0) {
1139 log_only("REMOVE FALLITA ");
1144 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
1146 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
1156 static function lock_data($is_exclusive, $table_idx)
1158 if (($res = file_lock(FTOK_PATH."/bin5/table".$table_idx."/table", $is_exclusive)) != FALSE) {
1159 self::$delta_t = microtime(TRUE);
1160 log_lock("LOCK table [".$table_idx."] [".self::$delta_t[$table_idx]."]");
1162 return (new Vect(array('res' => $res, 'tab' => $table_idx)));
1169 static function unlock_data($res_vect)
1173 $res = $res_vect->getbyid('res');
1174 $tab = $res_vect->getbyid('tab');
1176 log_lock("UNLOCK table [".$tab."] [".(microtime(TRUE) - (self::$delta_t[$tab]))."]");
1182 function chatt_send($user, $mesg)
1184 GLOBAL $mlang_brisk, $G_lang;
1186 if ($user->stat == 'table') {
1187 $table = &$this->table[$user->table];
1190 $user_mesg = substr($mesg,6);
1194 $dt = date("H:i ", $curtime);
1195 if (strncmp($user_mesg, "/nick ", 6) == 0) {
1196 log_main($user->sess." chatt_send BEGIN");
1198 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
1199 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1200 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
1205 $user_mesg = "COMMAND ".$user_mesg;
1208 // update local graph
1209 // update remote graphs
1210 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1211 $user_cur = $this->user[$i];
1212 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1213 if ($user_cur->sess == '')
1215 if (strcasecmp($user_cur->name, $name_new) == 0) {
1216 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1218 $premsg = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
1219 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $premsg);
1224 if ($i == BIN5_MAX_PLAYERS) {
1225 if ($user->flags & USER_FLAG_AUTH && strcasecmp($user->name,$name_new) != 0) {
1226 if ($this->table[$user->table]->auth_only == TRUE) {
1227 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1228 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>%s</b>");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
1233 $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
1237 $user->name = $name_new;
1239 log_main($user->sess." chatt_send start set");
1242 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1243 log_main($user->sess." chatt_send set loop");
1245 $user_cur = &$this->user[$i];
1246 if ($user_cur->sess == '')
1249 if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
1250 $table = &$this->table[$user->table];
1252 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1253 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1254 $this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->flags,
1255 xcape($this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->name),
1257 $this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->flags,
1258 xcape($this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->name),
1260 $this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->flags,
1261 xcape($this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->name),
1263 (BIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->flags),
1264 (BIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->name)),
1266 (BIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->flags),
1267 (BIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->name)));
1268 if ($user_cur == $user) {
1269 $user_cur->comm[$user_cur->step % COMM_N] .= $user_cur->myname_innerHTML();
1271 $user_cur->step_inc();
1277 for ($i = 0 ; $i < ($user->stat == 'room' ? BIN5_MAX_PLAYERS : BIN5_PLAYERS_N) ; $i++) {
1278 if ($user->stat == 'room') {
1279 $user_cur = &$this->user[$i];
1280 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1284 $user_cur = &$this->user[$table->player[$i]];
1287 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1288 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
1289 $dt, $user->flags, xcape($user->name), xcape($user_mesg));
1290 $user_cur->step_inc();
1292 log_legal($curtime, $user->ip, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
1296 function table_wakeup($user)
1298 $table = &$this->table[0];
1300 log_main("BIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
1304 log_main("BIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1306 for ($i = 0 ; $i < $table->player_n ; $i++) {
1307 $user_cur = &$this->user[$i];
1308 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1310 if ($user_cur == $user)
1311 $user_cur->subst = "shutdowner";
1313 $user_cur->subst = "shutdowned";
1314 $user_cur->laccwr = $curtime;
1316 $ret = "gst.st = ".($user_cur->step+1)."; ";
1317 $ret .= 'gst.st_loc++; xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
1319 log_wr($user_cur->sess." BIN5_WAKEUP: ".$ret);
1320 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1321 $user_cur->step_inc();
1324 $this->the_end = TRUE;
1328 * If all players are freezed the room garbage_manager clean up table and users.
1330 function is_abandoned()
1335 $table = &$this->table[0];
1337 for ($i = 0 ; $i < $table->player_n ; $i++) {
1338 $user_cur = &$this->user[$i];
1340 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
1349 static function request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
1351 GLOBAL $G_black_list;
1353 printf("NEW_SOCKET (root): %d\n", intval($new_socket));
1355 $enc = get_encoding($header);
1356 if (isset($header['User-Agent'])) {
1357 if (strstr($header['User-Agent'], "MSIE")) {
1358 $transp_type = "htmlfile";
1361 $transp_type = "xhr";
1365 $transp_type = "iframe";
1367 force_no_cache($header_out);
1369 if (($table_idx = gpcs_var('table_idx', $get, $post, $cookie)) === FALSE)
1372 if (($table_token = gpcs_var('table_token', $get, $post, $cookie)) === FALSE)
1373 unset($table_token);
1380 bin5_index_main($transp_type, $header_out, $addr, $get, $post, $cookie);
1381 $content = ob_get_contents();
1384 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1388 case "index_wr.php":
1389 // FIXME $content can be unset
1390 if (isset($table_idx) && isset($table_token)) {
1391 if (($bri = $s_a_p->app->match_get($table_idx, $table_token)) != FALSE) {
1393 bin5_index_wr_main($bri, $addr, $get, $post, $cookie);
1394 $content = ob_get_contents();
1398 $content = "Bin5 Load data error";
1402 $content = "Bin5 Load data error";
1404 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1408 case "index_rd_ifra.php":
1409 if (($transp = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
1411 if ($transp == 'websocket')
1415 if (!isset($table_idx)
1416 || !isset($table_token)
1417 || !isset($cookie['sess'])
1418 || ($bri = $s_a_p->app->match_get($table_idx, $table_token)) == NULL
1419 || (($user = $bri->get_user($cookie['sess'], $idx)) == FALSE)) {
1421 $content = Bin5_user::stream_fini($transp_type, $s_a_p->rndstr, TRUE);
1422 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1427 // close a previous opened index_read_ifra socket, if exists
1428 if (($prev = $user->rd_socket_get()) != NULL) {
1429 $s_a_p->socks_unset($user->rd_socket_get());
1430 fclose($user->rd_socket_get());
1431 printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
1432 $user->rd_socket_set(NULL);
1436 $user->stream_init($s_a_p->rndstr, $enc, $header, $header_out, $content, $get, $post, $cookie);
1438 $response = headers_render($header_out, -1).$user->chunked_content($content);
1439 $response_l = mb_strlen($response, "ASCII");
1441 $wret = @fwrite($new_socket, $response, $response_l);
1442 if ($wret < $response_l) {
1443 printf("TROUBLES WITH FWRITE: %d\n", $wret);
1444 $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
1447 $user->rd_cache_set("");
1449 fflush($new_socket);
1452 $s_a_p->socks_set($new_socket, $user, NULL);
1453 $user->rd_socket_set($new_socket);
1454 printf(" - qui ci siamo - ");
1473 function locshm_exists($tok)
1477 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
1478 log_main($tok." SHM NOT exists");
1484 log_main($tok." SHM exists");
1493 is_transition (is from room to table ?)
1494 is_again (is another game)
1496 Examples of $is_transition, $is_again:
1497 from reload of the page: FALSE, FALSE
1498 from sitdown in room: TRUE, FALSE
1499 from table: asta cmd e tutti passano: TRUE, TRUE
1500 from table: fine partita: TRUE, TRUE
1502 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1504 $table_idx = $user->table;
1505 $table = $bri->table[$table_idx];
1506 $table_pos = $user->table_pos;
1508 $ret = "table_init();";
1509 $ret .= $table->exitlock_show(&$bri->user, $table_pos);
1511 /* GENERAL STATUS */
1512 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1513 $sendstep, $user->stat, $user->subst, $table_pos);
1515 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1518 $ret .= "background_set();";
1521 $ret .= $user->myname_innerHTML();
1522 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1523 $bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->flags,
1524 xcape($bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->name),
1526 $bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->flags,
1527 xcape($bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->name),
1529 $bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->flags,
1530 xcape($bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->name),
1532 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->flags),
1533 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->name)),
1535 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->flags),
1536 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->name)));
1538 /* NOTIFY FOR THE CARD MAKER */
1539 if ($is_transition) { // && $user->subst == "asta" superfluo
1540 $ret .= show_table_info(&$bri, &$table, $table_pos);
1541 $ret .= "setTimeout(preload_images, 500, g_preload_img_arr, g_imgct);";
1544 $ret .= table_welcome($user);
1546 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1547 $ret .= playsound("cow.mp3");
1552 if ($is_transition) { // && $user->subst == "asta" superfluo
1555 for ($i = 0 ; $i < BIN5_CARD_HAND ; $i++) {
1556 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++) {
1558 for ($o = 0 ; $o < (BIN5_CARD_HAND * BIN5_PLAYERS_N) && $ct < $i+1 ; $o++) {
1559 // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1560 if ($table->card[$o]->owner == (($e + $table->gstart) % BIN5_PLAYERS_N)) {
1566 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1568 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BIN5_PLAYERS_N,
1569 $i, ((($e + BIN5_PLAYERS_N - $table_pos + $table->gstart) % BIN5_PLAYERS_N) == 0 ?
1570 $table->card[$o]->value : -1),
1571 ($i == 7 && $e == (BIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1576 $taked = array(0,0,0,0,0);
1577 $inhand = array(0,0,0,0,0);
1578 $ontabl = array(-1,-1,-1,-1,-1);
1581 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1582 // for ($i = 0 ; $i < 40 ; $i++) {
1583 if ($table->card[$i]->stat == 'hand') {
1584 if ($table->card[$i]->owner == $table_pos) {
1585 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1587 $inhand[$table->card[$i]->owner]++;
1589 else if ($table->card[$i]->stat == 'take') {
1590 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1591 $taked[$table->card[$i]->owner]++;
1593 else if ($table->card[$i]->stat == 'table') {
1594 $ontabl[$table->card[$i]->owner] = $i;
1598 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1599 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1601 log_main("Stat table: ".$logg);
1603 /* Set ours cards. */
1605 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1606 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1607 for ($i = $inhand[$table_pos] ; $i < BIN5_CARD_HAND ; $i++)
1608 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1609 $ret .= sprintf('card_setours(%s);', $oursarg);
1611 /* Dispose all cards */
1612 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1613 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1614 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1615 ($inhand[$i] <= BIN5_CARD_HAND ? $inhand[$i] : BIN5_CARD_HAND), $taked[$i]);
1617 if ($ontabl[$i] != -1) {
1618 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1619 $table->card[$ontabl[$i]]->value,
1620 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1626 if ($user->subst == 'asta') {
1628 /* show users auction status */
1630 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1631 $user_cur = &$bri->user[$table->player[$i]];
1632 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1633 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1635 if (BIN5_PLAYERS_N == 3)
1636 $showst .= ",-2,-2";
1637 $ret .= sprintf('document.title = "Brisk - Tavolo %d (asta)";', $user->table_orig);
1638 $ret .= sprintf('show_astat(%s);', $showst);
1640 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1641 /* show card chooser */
1642 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1647 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N) &&
1648 $table->asta_win == -1)
1649 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1650 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1652 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1653 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1657 if ($table->asta_win == -1) { // auction case
1658 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N))
1659 $ret .= "remark_on();";
1661 $ret .= "remark_off();";
1663 else { // chooseed case
1664 if ($table_pos == $table->asta_win)
1665 $ret .= "remark_on();";
1667 $ret .= "remark_off();";
1670 else if ($user->subst == 'game') {
1672 if (($table->gstart + $table->turn) % BIN5_PLAYERS_N == $table_pos)
1673 $ret .= "is_my_time = true; remark_on();";
1675 $ret .= "remark_off();";
1677 /* WHO CALL AND WHAT */
1678 $ret .= briscola_show($bri, $table, $user);
1682 } // end function show_table(...
1684 function calculate_winner(&$table)
1691 $cur_seed = $table->briscola - ($table->briscola % 10);
1693 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1694 // for ($i = 0 ; $i < 40 ; $i++) {
1695 if ($table->card[$i]->stat != "table")
1698 log_wr(sprintf("Card On table: [%d]", $i));
1700 $v = $table->card[$i]->value;
1701 $ontab[$table->card[$i]->owner] = $v;
1702 $ontid[$table->card[$i]->owner] = $i;
1703 /* se briscola setto il flag */
1704 if (($v - ($v % 10)) == $cur_seed)
1708 if ($briontab == FALSE) {
1709 $cur_win = $table->gstart;
1710 $cur_val = $ontab[$cur_win];
1711 $cur_seed = $cur_val - ($cur_val % 10);
1714 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1715 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1716 if ($ontab[$i] < $cur_val) {
1717 $cur_val = $ontab[$i];
1723 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1724 $table->card[$ontid[$i]]->owner = $cur_win;
1725 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1730 function show_table_info(&$bri, &$table, $table_pos)
1732 GLOBAL $G_lang, $mlang_bin5_bin5;
1735 $user = $bri->user[$table->player[$table_pos]];
1737 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1738 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1741 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++)
1742 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1743 $noty .= sprintf("</tr>");
1746 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1748 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1749 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1750 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1751 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1756 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1757 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1758 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1759 $noty .= "</tr></table>";
1761 if ($table->old_reason != "") {
1762 $noty .= $table->old_reason;
1765 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1766 if ($table->mazzo == $table_pos)
1767 $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
1769 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1770 $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
1773 if ($user->subst == 'asta') {
1774 if ($table->asta_win == -1) // auction case
1775 $curplayer = $table->gstart % BIN5_PLAYERS_N;
1777 $curplayer = $table->asta_win;
1779 else if ($user->subst == 'game') {
1780 $curplayer = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
1783 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1784 if ($curplayer == $table_pos) {
1785 $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
1788 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1789 $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
1792 $multer = $table->multer(TRUE);
1794 $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($multer) );
1796 $noty .= "<hr><br>";
1797 $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
1798 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
1799 * 130 + ($table->points_n > 0 ? 50 : 0) +
1800 * (120 * ($table->points_n / MAX_POINTS)));
1801 * will be used when we refact notify js function following
1802 * photoo class logic
1808 function table_welcome($user)
1810 GLOBAL $table_wellarr, $G_lang;
1813 for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
1814 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
1820 function briscola_show($bri, $table, $user)
1822 GLOBAL $G_lang, $mlang_bin5_bin5;
1826 if ($table->asta_card == 9)
1827 $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
1829 /* text of caller cell */
1830 if ($user->table_pos == $table->asta_win) {
1831 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
1832 $ret .= sprintf($prestr, $ptnadd);
1835 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
1836 $ret .= sprintf($prestr,
1837 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1839 $ret .= sprintf('set_iscalling(%d);', ($table->asta_win - $user->table_pos + BIN5_PLAYERS_N) % BIN5_PLAYERS_N);
1841 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1843 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1844 $ret .= sprintf('$("caller").style.visibility = "visible";');
1845 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1846 $ret .= sprintf('$("astalascio").style.visibility = "";');
1847 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1848 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1854 function game_result($asta_pnt, $pnt)
1856 if ($asta_pnt == 61) {
1859 else if ($pnt == 60)
1865 if ($pnt >= $asta_pnt)
1872 function log_points($remote_addr, $curtime, $user, $where, $mesg)
1874 if (($fp = @fopen(LEGAL_PATH."/points.log", 'a')) != FALSE) {
1875 /* Unix time | session | nickname | IP | where was | mesg */
1876 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
1877 ($user->flags & USER_FLAG_AUTH ? 'A' : 'N'),
1878 $user->name, $remote_addr, $where , $mesg));
1883 function game_description($act, $form, $old_mult, $win = -1, $win_name = "?1?", $fri = -1, $fri_name = "?2?",
1884 $old_pnt = -1, $old_asta_pnt = -1)
1886 GLOBAL $G_lang, $mlang_bin5_bin5;
1888 if ($form == 'html') {
1893 $win_name = xcape($win_name);
1894 $fri_name = xcape($fri_name);
1903 if ($act == BIN5_RULES_ALLPASS) {
1904 return (sprintf($mlang_bin5_bin5['info_alpa'][$G_lang],
1905 $tg_br, $tg_hr, $tg_bo, $tg_bc));
1907 else if ($act == BIN5_RULES_ABANDON) {
1908 return (sprintf($mlang_bin5_bin5['info_aban'][$G_lang],
1909 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1913 $wol = game_result($old_asta_pnt, $old_pnt);
1917 if ($win != $fri) { // not alone case
1919 /* 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" */
1921 $noty .= sprintf($mlang_bin5_bin5['info_part'][$G_lang],
1922 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1925 if ($old_pnt == 120) {
1926 $noty .= sprintf($mlang_bin5_bin5['info_capp'][$G_lang],
1927 $tg_br, $tg_hr, $tg_bo, $tg_bc );
1930 if ($old_asta_pnt > 61) {
1931 $noty .= sprintf($mlang_bin5_bin5['info_alea'][$G_lang],
1932 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1933 $old_asta_pnt, $old_pnt,
1934 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
1935 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
1936 $mlang_bin5_bin5['info_lost'][$G_lang])));
1939 $noty .= sprintf($mlang_bin5_bin5['info_more'][$G_lang],
1940 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1942 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
1943 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
1944 $mlang_bin5_bin5['info_lost'][$G_lang])));
1945 } // else of if ($old_asta_pnt > 61) {
1946 } // else of if ($old_pnt == 120) {
1947 } // if ($win != $fri) { // not alone case
1949 $noty .= sprintf($mlang_bin5_bin5['info_alon'][$G_lang],
1950 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1952 if ($old_pnt == 120) {
1953 $noty .= sprintf($mlang_bin5_bin5['info_acap'][$G_lang],
1954 $tg_br, $tg_hr, $tg_bo, $tg_bc);
1957 if ($old_asta_pnt > 61) {
1958 $noty .= sprintf($mlang_bin5_bin5['info_aleaa'][$G_lang],
1959 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1960 $old_asta_pnt, $old_pnt,
1961 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
1962 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
1963 $mlang_bin5_bin5['info_lost'][$G_lang])));
1966 $noty .= sprintf($mlang_bin5_bin5['info_morea'][$G_lang],
1967 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1969 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
1970 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
1971 $mlang_bin5_bin5['info_lost'][$G_lang])));
1977 $old_multer = Bin5_table::s_multer($old_mult, $old_asta_pnt);
1978 if ($old_multer > 1) {
1979 $noty .= sprintf($mlang_bin5_bin5['info_omul'][$G_lang],
1980 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1981 multoval($old_multer));
1983 $noty .= sprintf('%2$s%1$s', $tg_br, $tg_hr);