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 'info_match'=> array( 'it' => 'Il codice della partita è <b>%d</b>.',
95 'en' => 'Match code is <b>%d</b>.'),
96 'btn_bkgame'=> array( 'it' => 'torna alla partita',
97 'en' => 'back to the game'),
98 'call_wptn' => array( 'it' => '<br>con %d punti',
99 'en' => '<br>with %d points'),
100 'call_ycall'=> array( 'it' => 'Chiami%s:',
102 'call_call' => array( 'it' => 'Chiama %s%s:',
103 'en' => 'The declarer is %s%s:')
107 $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.'),
108 '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.') );
110 function multoval($mult)
115 return ($G_lang == 'en' ? 'double' : 'doppio');
117 return ($G_lang == 'en' ? 'triple' : 'triplo');
119 return ($G_lang == 'en' ? 'quadruple' : 'quadruplo');
121 return (sprintf(($G_lang == 'en' ? "%d-ple" : "%d-plo"), $mult));
126 var $value; /* 0 - 39 card value */
127 var $stat; /* 'bunch', 'hand', 'table', 'take' */
128 var $owner; /* (table position 0-4) */
129 // var $pos; /* Pos in hand. */
130 var $x; /* When played the X position on the table of the owner. */
131 var $y; /* When played the Y position on the table of the owner. */
133 function Card($value, $stat, $owner)
135 $this->value = $value;
136 $this->stat = $stat; // Card stat
137 $this->owner = $owner;
140 function assign($stat,$owner)
142 $this->stat = $stat; // Card stat
143 $this->owner = $owner;
146 function setpos($pos)
153 $this->stat = 'table'; // Card stat
158 function take($newown)
160 $this->stat = 'take'; // Card stat
161 $this->owner = $newown;
165 class Bin5_table extends Table {
166 var $card; // il mazzo di carte
167 var $mazzo; // chi e' di mazzo
177 var $points; // points array
178 var $points_n; // number of row of points
181 var $asta_win; // the caller idx position at table
183 var $friend; // the callee idx position at table
185 var $match_id; // the id of the match on the database (-1 == just not saved)
187 var $old_act; // last action that trigs the end of the game
192 var $old_pnt; // points made by caller and callee
193 var $old_asta_win; // the old caller idx position at table
194 var $old_friend; // the old callee idx position at table
196 function Bin5_table()
202 function create($idx)
204 if (($thiz =& new Bin5_table()) == FALSE)
210 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
211 $thiz->asta_pla_n= -1;
212 $thiz->asta_card = -1;
213 $thiz->asta_pnt = -1;
216 $thiz->points = array( );
218 $thiz->total = array( 0, 0, 0, 0, 0);
219 $thiz->asta_win = -1;
220 $thiz->briscola = -1;
224 $thiz->match_id = -1;
227 $thiz->old_mazzo = -1;
228 $thiz->old_reason = "";
229 $thiz->old_asta_pnt = -1;
230 $thiz->old_mult = -1;
232 $thiz->old_asta_win = -1;
233 $thiz->old_friend = -1;
240 function myclone(&$from)
242 if (($thiz =& new Bin5_table()) == FALSE)
247 $thiz->card = $from->card;
248 $thiz->mazzo = $from->mazzo; // REVIEW
249 $thiz->gstart = $from->gstart;
250 $thiz->turn = $from->turn;
252 $thiz->asta_pla = $from->asta_pla;
253 $thiz->asta_pla_n = $from->asta_pla_n;
254 $thiz->asta_card = $from->asta_card;
255 $thiz->asta_pnt = $from->asta_pnt;
257 $thiz->mult = $from->mult;
258 $thiz->points = $from->points;
259 $thiz->points_n = $from->points_n;
260 $thiz->total = $from->total;
262 $thiz->asta_win = $from->asta_win;
263 $thiz->briscola = $from->briscola;
264 $thiz->friend = $from->friend;
266 $thiz->match_id = $from->match_id;
268 $thiz->old_act = $from->old_act;
269 $thiz->old_mazzo = $from->old_mazzo;
270 $thiz->old_reason = $from->old_reason;
271 $thiz->old_asta_pnt = $from->old_asta_pnt;
272 $thiz->old_mult = $from->mult;
273 $thiz->old_pnt = $from->old_pnt;
274 $thiz->old_asta_win = $from->old_asta_win;
275 $thiz->old_friend = $from->old_friend;
281 function parentcopy(&$from)
286 function spawn(&$from)
288 if (($thiz =& new Bin5_table()) == FALSE)
291 $thiz->parentcopy($from);
293 log_main("PLAYER_N - spawn.".$thiz->player_n);
295 $thiz->card = array();
296 $thiz->bunch_create();
297 $thiz->mazzo = rand(0,PLAYERS_N-1);
301 $thiz->match_id = -1;
303 $thiz->old_asta_win = -1;
304 $thiz->old_reason = "";
306 // players are rearranged in an dedicated array
307 $thiz->player = array();
308 for ($i = 0 ; $i < $from->player_n ; $i++)
309 $thiz->player[$i] = $i;
311 log_main("TABLE_OLD_WIN - spawn:".$thiz->old_asta_win);
316 static function asta2mult($asta_pnt)
320 else if ($asta_pnt > 100)
322 else if ($asta_pnt > 90)
324 else if ($asta_pnt > 80)
326 else if ($asta_pnt > 70)
332 function multer($is_new)
335 // return (pow(2, $this->mult) * $this->asta2mult($this->asta_pnt));
336 return (static::s_multer($this->mult, $this->asta_pnt));
339 // return (pow(2, $this->old_mult) * $this->asta2mult($this->old_asta_pnt));
340 return (static::s_multer($this->old_mult, $this->old_asta_pnt));
344 static function s_multer($mult, $pnt)
346 return (pow(2, $mult) * static::asta2mult($pnt));
349 // function bunch_create_old() function AND
353 // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
354 // // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
355 // $ret[$i] =& new Card($i, 'bunch', 'no_owner');
362 function bunch_create()
366 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
367 $this->card[$i] = new Card($i, 'bunch', 'no_owner');
371 function bunch_make()
373 log_main("bunch_make start");
374 $ct = array(0,0,0,0,0);
376 mt_srand(make_seed());
378 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--)
381 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--) {
385 log_main("RND ZERO");
389 $owner = $i % BIN5_PLAYERS_N;
390 $this->card[$id]->assign('hand', $owner);
392 $rest[$rn] = $rest[$i];
393 // $pubbpos[$rn2] = $pubbpos[$i];
395 log_main("bunch_make end");
398 function init($userarr)
401 $this->mazzo = rand(0,PLAYERS_N-1);
404 $this->old_asta_win = -1;
405 $this->old_reason = "";
407 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
408 $this->total[$i] = 0;
409 $user_cur = $userarr[$this->player[$i]];
410 $user_cur->stat_set('table');
411 $user_cur->exitislock = TRUE;
414 log_main("table::init: ci siamo");
417 function game_init($userarr)
421 $this->gstart = ($this->mazzo+1) % BIN5_PLAYERS_N;
424 $this->asta_pla_n = BIN5_PLAYERS_N;
425 $this->asta_card = -1;
426 $this->asta_pnt = 60;
427 $this->asta_win = -1;
428 $this->briscola = -1;
432 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
433 $this->asta_pla[$i] = TRUE;
434 $user_cur = $userarr[$this->player[$i]];
435 $user_cur->subst = 'asta';
436 $user_cur->asta_card = -2;
437 $user_cur->asta_pnt = -1;
438 $user_cur->handpt = $this->hand_points($i);
443 function game_next($delta)
445 $this->old_mazzo = $this->mazzo;
446 $this->mazzo = ($this->mazzo + $delta) % BIN5_PLAYERS_N;
449 function mult_inc($val)
451 $this->old_mult = $this->mult;
455 function mult_set($val)
457 $this->old_mult = $this->mult;
461 function hand_points($idx)
463 GLOBAL $G_all_points;
467 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
468 // for ($i = 0 ; $i < 40 ; $i++) {
469 if ($this->card[$i]->owner != $idx)
472 $ctt = $this->card[$i]->value % 10;
473 $tot += $G_all_points[$ctt];
480 function exitlock_show($userarr, $table_pos)
482 $ct = $this->exitlock_calc($userarr, $table_pos);
484 $ret = sprintf('exitlock_show(%d, %s);', $ct,
485 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
489 function exitlock_calc(&$userarr, $table_pos)
493 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
494 if ($userarr[$this->player[$i]]->exitislock == FALSE)
501 function rules_engine(&$bri, $curtime, $action, $user)
503 GLOBAL $G_all_points, $G_dbasetype;
507 $this->old_act = $action;
508 if ($action == BIN5_RULES_ALLPASS) { // return TRUE if all correct
509 $this->old_asta_win = -1;
512 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
517 // $this->game_next(1);
518 $this->game_init(&$bri->user);
520 else if ($action == BIN5_RULES_ABANDON) { // return TRUE if all correct
521 log_wr(sprintf("GIOCO FINITO !!!"));
522 $this->old_asta_win = $user->table_pos;
526 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
530 // Non si cambia mazzo se si abbandona la partita
532 // $this->game_next(0);
533 $this->game_init(&$bri->user);
535 else if ($action == BIN5_RULES_FINISH) { // return TRUE if all correct
539 if ($this->asta_pnt == 60)
540 $this->asta_pnt = 61;
542 $this->old_reason = "";
544 // count points for the temporary 2 teams
545 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
546 $ctt = $this->card[$i]->value % 10;
547 $own = $this->card[$i]->owner;
548 if ($own == $this->asta_win || $own == $this->friend)
549 $pro += $G_all_points[$ctt];
552 log_wr(sprintf("PRO: [%d]", $pro));
555 if ($this->asta_pnt == 61 && $pro == 60) {
556 $this->points[$this->points_n % MAX_POINTS] = array();
557 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
558 $this->points[$this->points_n % MAX_POINTS][$i] = 0;
562 $this->old_pnt = $pro;
569 if ($pro >= $this->asta_pnt)
574 $this->points[$this->points_n % MAX_POINTS] = array();
575 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
576 if ($i == $this->asta_win)
577 $pt = ($i == $this->friend ? 4 : 2);
578 else if ($i == $this->friend)
583 log_wr(sprintf("PRO: pt[%d][%d] = %d", $this->points_n % MAX_POINTS, $i, $pt));
585 $pt = $pt * $sig * $this->multer(TRUE) * ($pro == 120 ? 2 : 1);
587 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $this->multer(TRUE), ($pro == 120 ? 2 : 1)));
589 $this->points[$this->points_n % MAX_POINTS][$i] = $pt;
590 $this->total[$i] += $pt;
594 $this->old_pnt = $pro;
595 $this->old_asta_win = $this->asta_win;
605 $this->game_next($game_delta);
607 $plist = "$this->table_token|$user->table_orig|$this->player_n";
610 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
611 $user_cur = &$bri->user[$this->player[$i]];
614 $ucodes[$i] = $user_cur->code_get();
617 $plist .= '|'.xcapelt($user_cur->name).'|'.$pts[$i];
618 $codes .= '|'.xcapelt($user_cur->code_get());
621 log_legal($curtime, $user->ip, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
623 $this->old_asta_pnt = $this->asta_pnt;
624 // $this->old_mazzo is managed by ->game_next();
625 // $this->old_mult, $this->old_pnt, $this->old_reason and $this->old_asta_win are specific
627 $this->old_friend = $this->friend;
628 $this->old_reason = game_description($action, 'html', $this->old_mult,
629 $this->old_asta_win, $bri->user[$this->player[$this->old_asta_win]]->name,
630 $this->old_friend, $bri->user[$this->player[$this->old_friend]]->name,
631 $this->old_pnt, $this->old_asta_pnt);
634 if ($user->table_orig < TABLES_AUTH_N) {
635 require_once("../Obj/dbase_".$G_dbasetype.".phh");
637 if (($bdb = BriskDB::create()) != FALSE) {
638 $bdb->bin5_points_save($curtime, $this, $user->table_orig, $action, $ucodes, $pts);
642 log_points($remote_addr, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED");
644 log_points($remote_addr, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
647 $this->game_init(&$bri->user);
652 } // end class Bin5_table
657 define('BIN5_USER_FLAG_RING_ENDAUCT', 0x01);
659 class Bin5_user extends User {
662 var $handpt; // Total card points at the beginning of the current hand.
663 var $exitislock; // Player can exit from the table ?
664 var $privflags; // Flags for briskin5 only
672 function create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
673 if (($thiz =& new User()) == FALSE)
676 $thiz->asta_card = -2;
677 $thiz->asta_pnt = -1;
679 $thiz->exitislock = TRUE;
680 $thiz->privflags = 0;
686 function parentcopy(&$from)
691 function copy(&$from)
693 $this->parentcopy($from);
695 $this->asta_card = $from->asta_card;
696 $this->asta_pnt = $from->asta_pnt;
697 $this->handpt = $from->handpt;
698 $this->exitislock = $from->exitislock;
699 $this->privflags = $from->privflags;
703 function myclone(&$from)
705 if (($thiz =& new User()) == FALSE)
714 static function spawn($from, &$bri, $table, $table_pos, $get, $post, $cookie)
716 if (($thiz = new Bin5_user()) == FALSE)
719 if (($CO_bin5_pref_ring_endauct = gpcs_var("CO_bin5_pref_ring_endauct", $get, $post, $cookie)) === FALSE) {
720 $CO_bin5_pref_ring_endauct = "";
723 $thiz->parentcopy($from);
725 /* NOTE: at this moment idx and table_pos fields have the same value
726 but diffentent functions, we keep them separated for a while */
728 $thiz->idx = $table_pos;
729 $thiz->asta_card = -2;
730 $thiz->asta_pnt = -1;
732 $thiz->exitislock = TRUE;
734 log_wr("Bin5 constructor");
736 $thiz->privflags = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
738 $thiz->table_orig = $table;
740 $thiz->table_pos = $table_pos;
747 function step_set($step)
749 $this->step = $step & 0x7fffffff;
754 function step_inc($delta = 1) {
755 $this->step += $delta;
756 /* modularization because unpack() not manage unsigned 32bit int correctly */
757 $this->step &= 0x7fffffff;
762 static function load_step($tab_id, $sess)
766 if (validate_sess($sess) == FALSE)
769 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
770 mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
771 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step", 'rb')) == FALSE)
773 if (($s = fread($fp, 8)) == FALSE)
775 if (mb_strlen($s, "ASCII") != 8)
777 $arr = unpack('Ls/Li', $s);
780 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
787 log_rd2("STEP_GET [".$sess."]: return false ");
795 if (validate_sess($this->sess) == FALSE)
797 if (file_exists(BIN5_PROXY_PATH."/table".$this->table_orig) == FALSE)
798 mkdir(BIN5_PROXY_PATH."/table".$this->table_orig, 0775, TRUE);
799 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$this->table_orig."/".$this->sess.".step", 'w')) == FALSE)
801 fwrite($fp, pack("LL",$this->step, $this->idx));
804 log_main("step_set [".$this->sess. "] [".$this->step."]");
812 static function unproxy_step($tab_id, $sess)
814 log_rd2("UNPROXY: ".BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
815 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
818 @unlink(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
821 function destroy_data($tab_id)
824 if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos, "B")) == -1) {
825 log_crit("BIN5 USER DATA REMOVE FAILED 1 [".FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos."]");
829 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
830 log_crit("BIN5 USER DATA REMOVE FAILED 2");
833 if (shmop_delete($shm) == 0) {
834 log_crit("BIN5 USER DATA REMOVE FAILED 3");
839 log_main("BIN5 USER DATA DESTROY SUCCESS");
841 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
851 static function blocking_error($is_unrecoverable)
853 log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
854 return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
857 protected function page_sync($sess, $page)
859 log_rd2("PAGE_SYNC");
860 printf("xXx BIN5_USER::PAGE_SYNC\n");
861 return (sprintf('xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
864 protected function maincheck($cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate, $table_idx, $table_token)
866 GLOBAL $G_lang, $mlang_indrd;
867 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
868 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
869 $CO_splashdate = "CO_splashdate".$G_splash_idx;
870 $$CO_splashdate = $splashdate;
874 log_rd("maincheck begin");
879 /* Nothing changed, return. */
880 if ($cur_step == $this->step)
883 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$this->stat."] cur_step[".$cur_step."] user_step[".$this->step."]");
885 if ($cur_step == -1) {
887 * if $cur_step == -1 load the current state from the main struct
890 $S_load_stat['wR_minusone']++;
892 // if ($this->the_end == TRUE) {
893 // log_rd2("main_check: the end".var_export(debug_backtrace()));
896 if ($this->trans_step != -1) {
897 log_rd2("TRANS USATO ".$this->trans_step);
898 $cur_step = $this->trans_step;
899 $this->trans_step = -1;
902 log_rd2("TRANS NON ATTIVATO");
907 /* this part I suppose is read only on $this->room structure */
908 if ($cur_step == -1) {
909 log_rd2("PRE-NEWSTAT");
916 if ($this->stat == 'table') {
918 /* NOTE: $this->room is associated with the current $bri object */
919 printf("xXx CLASS NAME [%s]\n", get_class($this->room));
920 $ret = show_table(&$this->room, $this, $this->step, FALSE, FALSE);
922 log_rd2("NEWSTAT: ".$this->stat);
924 $new_stat = $this->stat;
925 $new_subst = $this->subst;
926 $new_step = $this->step;
927 } /* if ($cur_step == -1) { */
929 /* $sem = Room::lock_data(FALSE); */
930 $S_load_stat['rU_heavy']++;
932 if ($cur_step < $this->step) {
934 if ($cur_step + COMM_N < $this->step) {
935 if (($cur_stat != $this->stat)) {
936 $to_stat = $this->stat;
937 /* Room::unlock_data($sem); */
939 printf("xXx BIN5_USER::MAINCHECK\n");
940 return ($this->page_sync($this->sess, ($to_stat == "table" ? "index.php" : "../index.php"), $this->table, $this->table_token));
942 log_rd2("lost history, refresh from scratch");
943 printf("xXx LOST HISTORY!\n");
947 for ($i = $cur_step ; $i < $this->step ; $i++) {
949 if ($this->comm[$ii] == "") {
955 log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
956 $ret .= $this->comm[$ii];
958 $new_stat = $this->stat;
959 $new_subst = $this->subst;
960 $new_step = $this->step;
963 log_rd2($this->step, 'index_rd.php: after ret set');
965 if ($this->the_end == TRUE) {
966 log_rd2("LOGOUT BYE BYE!!");
967 log_auth($this->sess, "Explicit logout.");
969 if ($this->the_end == TRUE) {
973 /* if ($this->subst == 'sitdown') { */
974 /* log_load("ROOM WAKEUP"); */
975 /* $this->room->room_wakeup($this); */
977 /* else if ($this->subst == 'standup') */
978 /* $this->room->room_outstandup($this); */
980 /* log_rd2("LOGOUT FROM WHAT ???"); */
982 } /* if ($this->the_end == TRUE) { ... */
983 } /* if ($this->the_end == TRUE) { ... */
984 } /* if ($cur_step < $this->step) { */
986 /* Room::unlock_data($sem); */
987 } /* else of if ($cur_step == -1) { */
991 } // function maincheck (...
998 } // end class Bin5_user
1003 static $delta_t = array();
1007 var $comm; // commands for many people
1008 var $step; // current step of the comm array
1009 var $garbage_timeout;
1020 function Bin5 ($room, $table_idx, $table_token, $get, $post, $cookie) {
1021 $this->user = array();
1022 $this->table = array();
1024 $this->the_end = FALSE;
1025 $this->shm_sz = BIN5_SHM_MIN;
1026 if (($this->tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
1031 $user = $room->user;
1032 $table = $room->table[$table_idx];
1034 log_wr("Bin5 constructor");
1036 for ($i = 0 ; $i < $table->player_n ; $i++) {
1037 $user[$table->player[$i]]->table_token = $table_token;
1038 $this->user[$i] = Bin5_user::spawn($user[$table->player[$i]], $this, $table_idx, $i, $get, $post, $cookie);
1040 $this->table[0] = Bin5_table::spawn(&$table);
1042 log_main("TABLE_OLD_WIN - Bin5:".$this->table[0]->old_asta_win);
1044 $this->table_idx = $table_idx;
1045 $this->table_token = $table_token;
1046 $this->garbage_timeout = 0;
1048 $this->delay_mgr = new Delay_Manager((GARBAGE_TIMEOUT *3.0) / 2.0);
1050 log_wr("Bin5 constructor end");
1054 function get_user($sess, &$idx)
1058 if (validate_sess($sess)) {
1059 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1060 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1063 $ret = &$this->user[$i];
1067 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1068 // for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++)
1069 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1072 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1079 function garbage_manager($force)
1083 /* Garbage collector degli utenti in timeout */
1087 $delta = $this->delay_mgr->delta_get($curtime);
1089 if ($force || $this->garbage_timeout < $curtime) {
1090 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1091 $user_cur = $this->user[$i];
1092 if ($user_cur->sess == "" ||
1093 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
1096 if ($user_cur->lacc + EXPIRE_TIME_RD < ($curtime - $delta)) { // Auto logout dell'utente
1097 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
1099 if ($user_cur->stat == 'table') {
1100 log_auth($user_cur->sess," bin5 Autologout session.");
1102 /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE
1104 $tmp_sess = $user_cur->sess;
1105 $user_cur->sess = "";
1106 Bin5_user::step_unproxy($tmp_sess);
1107 $user_cur->name = "";
1108 $user_cur->the_end = FALSE;
1112 /* se gli altri utenti non erano d'accordo questo utente viene bannato */
1113 $remcalc = $this->table[0]->exitlock_calc(&$this->user, $user_cur->table_pos);
1115 require_once("${G_base}Obj/hardban.phh");
1116 Hardbans::add(($user_cur->flags & USER_FLAG_AUTH ? $user_cur->name : FALSE),
1117 $user_cur->ip, $user_cur->sess, $user_cur->laccwr + BAN_TIME);
1119 // $user->bantime = $user->laccwr + BAN_TIME;
1121 $this->table_wakeup($user_cur);
1125 log_rd2($user_cur->sess." GARBAGE UPDATED!");
1127 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
1132 $this->delay_mgr->lastcheck_set($curtime);
1137 function destroy_data()
1143 log_main("DESTROY BRISKIN5 DATA");
1146 log_main("DESTROY2 BRISKIN5 DATA [".$this->table_idx."]");
1147 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1148 $this->user[$i]->destroy_data($this->table_idx);
1149 Bin5_user::unproxy_step($this->table_idx, $this->user[$i]->sess);
1151 if (($tok = @ftok(FTOK_PATH."/bin5/table".$this->table_idx."/table", "B")) == -1)
1154 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
1157 if (shmop_delete($shm) == 0) {
1158 log_only("REMOVE FALLITA ");
1163 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
1165 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
1175 static function lock_data($is_exclusive, $table_idx)
1177 if (($res = file_lock(FTOK_PATH."/bin5/table".$table_idx."/table", $is_exclusive)) != FALSE) {
1178 self::$delta_t = microtime(TRUE);
1179 log_lock("LOCK table [".$table_idx."] [".self::$delta_t[$table_idx]."]");
1181 return (new Vect(array('res' => $res, 'tab' => $table_idx)));
1188 static function unlock_data($res_vect)
1192 $res = $res_vect->getbyid('res');
1193 $tab = $res_vect->getbyid('tab');
1195 log_lock("UNLOCK table [".$tab."] [".(microtime(TRUE) - (self::$delta_t[$tab]))."]");
1201 function chatt_send($user, $mesg)
1203 GLOBAL $mlang_brisk, $G_lang;
1205 if ($user->stat == 'table') {
1206 $table = &$this->table[$user->table];
1209 $user_mesg = substr($mesg,6);
1213 $dt = date("H:i ", $curtime);
1214 if (strncmp($user_mesg, "/nick ", 6) == 0) {
1215 log_main($user->sess." chatt_send BEGIN");
1217 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
1218 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1219 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
1224 $user_mesg = "COMMAND ".$user_mesg;
1227 // update local graph
1228 // update remote graphs
1229 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1230 $user_cur = $this->user[$i];
1231 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1232 if ($user_cur->sess == '')
1234 if (strcasecmp($user_cur->name, $name_new) == 0) {
1235 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1237 $premsg = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
1238 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $premsg);
1243 if ($i == BIN5_MAX_PLAYERS) {
1244 if ($user->flags & USER_FLAG_AUTH && strcasecmp($user->name,$name_new) != 0) {
1245 if ($this->table[$user->table]->auth_only == TRUE) {
1246 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1247 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>%s</b>");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
1252 $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
1256 $user->name = $name_new;
1258 log_main($user->sess." chatt_send start set");
1261 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1262 log_main($user->sess." chatt_send set loop");
1264 $user_cur = &$this->user[$i];
1265 if ($user_cur->sess == '')
1268 if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
1269 $table = &$this->table[$user->table];
1271 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1272 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1273 $this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->flags,
1274 xcape($this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->name),
1276 $this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->flags,
1277 xcape($this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->name),
1279 $this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->flags,
1280 xcape($this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->name),
1282 (BIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->flags),
1283 (BIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->name)),
1285 (BIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->flags),
1286 (BIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->name)));
1287 if ($user_cur == $user) {
1288 $user_cur->comm[$user_cur->step % COMM_N] .= $user_cur->myname_innerHTML();
1290 $user_cur->step_inc();
1296 for ($i = 0 ; $i < ($user->stat == 'room' ? BIN5_MAX_PLAYERS : BIN5_PLAYERS_N) ; $i++) {
1297 if ($user->stat == 'room') {
1298 $user_cur = &$this->user[$i];
1299 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1303 $user_cur = &$this->user[$table->player[$i]];
1306 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1307 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
1308 $dt, $user->flags, xcape($user->name), xcape($user_mesg));
1309 $user_cur->step_inc();
1311 log_legal($curtime, $user->ip, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
1315 function table_wakeup($user)
1317 $table = &$this->table[0];
1319 log_main("BIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
1323 log_main("BIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1325 for ($i = 0 ; $i < $table->player_n ; $i++) {
1326 $user_cur = &$this->user[$i];
1327 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1329 if ($user_cur == $user)
1330 $user_cur->subst = "shutdowner";
1332 $user_cur->subst = "shutdowned";
1333 $user_cur->laccwr = $curtime;
1335 $ret = "gst.st = ".($user_cur->step+1)."; ";
1336 $ret .= 'gst.st_loc++; xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
1338 log_wr($user_cur->sess." BIN5_WAKEUP: ".$ret);
1339 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1340 $user_cur->step_inc();
1343 $this->the_end = TRUE;
1347 * If all players are freezed the room garbage_manager clean up table and users.
1349 function is_abandoned()
1354 $table = &$this->table[0];
1356 for ($i = 0 ; $i < $table->player_n ; $i++) {
1357 $user_cur = &$this->user[$i];
1359 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
1368 static function request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
1370 GLOBAL $G_black_list;
1372 printf("NEW_SOCKET (root): %d\n", intval($new_socket));
1374 $enc = get_encoding($header);
1375 if (isset($header['User-Agent'])) {
1376 if (strstr($header['User-Agent'], "MSIE")) {
1377 $transp_type = "htmlfile";
1380 $transp_type = "xhr";
1384 $transp_type = "iframe";
1386 force_no_cache($header_out);
1388 if (($table_idx = gpcs_var('table_idx', $get, $post, $cookie)) === FALSE)
1391 if (($table_token = gpcs_var('table_token', $get, $post, $cookie)) === FALSE)
1392 unset($table_token);
1399 bin5_index_main($transp_type, $header_out, $addr, $get, $post, $cookie);
1400 $content = ob_get_contents();
1403 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1407 case "index_wr.php":
1408 // FIXME $content can be unset
1409 if (isset($table_idx) && isset($table_token)) {
1410 if (($bri = $s_a_p->app->match_get($table_idx, $table_token)) != FALSE) {
1412 bin5_index_wr_main($bri, $addr, $get, $post, $cookie);
1413 $content = ob_get_contents();
1417 $content = "Bin5 Load data error";
1421 $content = "Bin5 Load data error";
1423 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1427 case "index_rd_ifra.php":
1428 if (($transp = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
1430 if ($transp == 'websocket')
1434 if (!isset($table_idx)
1435 || !isset($table_token)
1436 || !isset($cookie['sess'])
1437 || ($bri = $s_a_p->app->match_get($table_idx, $table_token)) == NULL
1438 || (($user = $bri->get_user($cookie['sess'], $idx)) == FALSE)) {
1440 $content = Bin5_user::stream_fini($transp_type, $s_a_p->rndstr, TRUE);
1441 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1446 // close a previous opened index_read_ifra socket, if exists
1447 if (($prev = $user->rd_socket_get()) != NULL) {
1448 $s_a_p->socks_unset($user->rd_socket_get());
1449 fclose($user->rd_socket_get());
1450 printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
1451 $user->rd_socket_set(NULL);
1455 $user->stream_init($s_a_p->rndstr, $enc, $header, $header_out, $content, $get, $post, $cookie);
1457 $response = headers_render($header_out, -1).$user->chunked_content($content);
1458 $response_l = mb_strlen($response, "ASCII");
1460 $wret = @fwrite($new_socket, $response, $response_l);
1461 if ($wret < $response_l) {
1462 printf("TROUBLES WITH FWRITE: %d\n", $wret);
1463 $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
1466 $user->rd_cache_set("");
1468 fflush($new_socket);
1471 $s_a_p->socks_set($new_socket, $user, NULL);
1472 $user->rd_socket_set($new_socket);
1473 printf(" - qui ci siamo - ");
1492 function locshm_exists($tok)
1496 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
1497 log_main($tok." SHM NOT exists");
1503 log_main($tok." SHM exists");
1512 is_transition (is from room to table ?)
1513 is_again (is another game)
1515 Examples of $is_transition, $is_again:
1516 from reload of the page: FALSE, FALSE
1517 from sitdown in room: TRUE, FALSE
1518 from table: asta cmd e tutti passano: TRUE, TRUE
1519 from table: fine partita: TRUE, TRUE
1521 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1523 $table_idx = $user->table;
1524 $table = $bri->table[$table_idx];
1525 $table_pos = $user->table_pos;
1527 $ret = "table_init();";
1528 $ret .= $table->exitlock_show(&$bri->user, $table_pos);
1530 /* GENERAL STATUS */
1531 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1532 $sendstep, $user->stat, $user->subst, $table_pos);
1534 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1537 $ret .= "background_set();";
1540 $ret .= $user->myname_innerHTML();
1541 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1542 $bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->flags,
1543 xcape($bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->name),
1545 $bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->flags,
1546 xcape($bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->name),
1548 $bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->flags,
1549 xcape($bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->name),
1551 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->flags),
1552 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->name)),
1554 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->flags),
1555 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->name)));
1557 /* NOTIFY FOR THE CARD MAKER */
1558 if ($is_transition) { // && $user->subst == "asta" superfluo
1559 $ret .= show_table_info(&$bri, &$table, $table_pos);
1560 $ret .= "setTimeout(preload_images, 500, g_preload_img_arr, g_imgct);";
1563 $ret .= table_welcome($user);
1565 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1566 $ret .= playsound("cow.mp3");
1571 if ($is_transition) { // && $user->subst == "asta" superfluo
1574 for ($i = 0 ; $i < BIN5_CARD_HAND ; $i++) {
1575 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++) {
1577 for ($o = 0 ; $o < (BIN5_CARD_HAND * BIN5_PLAYERS_N) && $ct < $i+1 ; $o++) {
1578 // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1579 if ($table->card[$o]->owner == (($e + $table->gstart) % BIN5_PLAYERS_N)) {
1585 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1587 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BIN5_PLAYERS_N,
1588 $i, ((($e + BIN5_PLAYERS_N - $table_pos + $table->gstart) % BIN5_PLAYERS_N) == 0 ?
1589 $table->card[$o]->value : -1),
1590 ($i == 7 && $e == (BIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1595 $taked = array(0,0,0,0,0);
1596 $inhand = array(0,0,0,0,0);
1597 $ontabl = array(-1,-1,-1,-1,-1);
1600 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1601 // for ($i = 0 ; $i < 40 ; $i++) {
1602 if ($table->card[$i]->stat == 'hand') {
1603 if ($table->card[$i]->owner == $table_pos) {
1604 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1606 $inhand[$table->card[$i]->owner]++;
1608 else if ($table->card[$i]->stat == 'take') {
1609 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1610 $taked[$table->card[$i]->owner]++;
1612 else if ($table->card[$i]->stat == 'table') {
1613 $ontabl[$table->card[$i]->owner] = $i;
1617 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1618 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1620 log_main("Stat table: ".$logg);
1622 /* Set ours cards. */
1624 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1625 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1626 for ($i = $inhand[$table_pos] ; $i < BIN5_CARD_HAND ; $i++)
1627 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1628 $ret .= sprintf('card_setours(%s);', $oursarg);
1630 /* Dispose all cards */
1631 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1632 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1633 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1634 ($inhand[$i] <= BIN5_CARD_HAND ? $inhand[$i] : BIN5_CARD_HAND), $taked[$i]);
1636 if ($ontabl[$i] != -1) {
1637 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1638 $table->card[$ontabl[$i]]->value,
1639 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1645 if ($user->subst == 'asta') {
1647 /* show users auction status */
1649 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1650 $user_cur = &$bri->user[$table->player[$i]];
1651 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1652 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1654 if (BIN5_PLAYERS_N == 3)
1655 $showst .= ",-2,-2";
1656 $ret .= sprintf('document.title = "Brisk - Tavolo %d (asta)";', $user->table_orig);
1657 $ret .= sprintf('show_astat(%s);', $showst);
1659 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1660 /* show card chooser */
1661 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1666 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N) &&
1667 $table->asta_win == -1)
1668 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1669 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1671 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1672 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1676 if ($table->asta_win == -1) { // auction case
1677 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N))
1678 $ret .= "remark_on();";
1680 $ret .= "remark_off();";
1682 else { // chooseed case
1683 if ($table_pos == $table->asta_win)
1684 $ret .= "remark_on();";
1686 $ret .= "remark_off();";
1689 else if ($user->subst == 'game') {
1691 if (($table->gstart + $table->turn) % BIN5_PLAYERS_N == $table_pos)
1692 $ret .= "is_my_time = true; remark_on();";
1694 $ret .= "remark_off();";
1696 /* WHO CALL AND WHAT */
1697 $ret .= briscola_show($bri, $table, $user);
1701 } // end function show_table(...
1703 function calculate_winner(&$table)
1710 $cur_seed = $table->briscola - ($table->briscola % 10);
1712 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1713 // for ($i = 0 ; $i < 40 ; $i++) {
1714 if ($table->card[$i]->stat != "table")
1717 log_wr(sprintf("Card On table: [%d]", $i));
1719 $v = $table->card[$i]->value;
1720 $ontab[$table->card[$i]->owner] = $v;
1721 $ontid[$table->card[$i]->owner] = $i;
1722 /* se briscola setto il flag */
1723 if (($v - ($v % 10)) == $cur_seed)
1727 if ($briontab == FALSE) {
1728 $cur_win = $table->gstart;
1729 $cur_val = $ontab[$cur_win];
1730 $cur_seed = $cur_val - ($cur_val % 10);
1733 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1734 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1735 if ($ontab[$i] < $cur_val) {
1736 $cur_val = $ontab[$i];
1742 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1743 $table->card[$ontid[$i]]->owner = $cur_win;
1744 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1749 function show_table_info(&$bri, &$table, $table_pos)
1751 GLOBAL $G_lang, $mlang_bin5_bin5;
1754 $user = $bri->user[$table->player[$table_pos]];
1756 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1757 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1760 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++)
1761 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1762 $noty .= sprintf("</tr>");
1765 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1767 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1768 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1769 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1770 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1775 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1776 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1777 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1778 $noty .= "</tr></table>";
1780 if ($table->old_reason != "") {
1781 $noty .= $table->old_reason;
1784 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1785 if ($table->mazzo == $table_pos)
1786 $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
1788 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1789 $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
1792 if ($user->subst == 'asta') {
1793 if ($table->asta_win == -1) // auction case
1794 $curplayer = $table->gstart % BIN5_PLAYERS_N;
1796 $curplayer = $table->asta_win;
1798 else if ($user->subst == 'game') {
1799 $curplayer = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
1802 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1803 if ($curplayer == $table_pos) {
1804 $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
1807 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1808 $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
1811 $multer = $table->multer(TRUE);
1813 $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($multer) );
1815 $noty .= "<hr><br>";
1816 if ($table->match_id != -1) {
1817 $noty .= sprintf($mlang_bin5_bin5['info_match'][$G_lang], $table->match_id);
1818 $noty .= "<hr><br>";
1820 $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
1821 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
1822 * 130 + ($table->points_n > 0 ? 50 : 0) +
1823 * (120 * ($table->points_n / MAX_POINTS)));
1824 * will be used when we refact notify js function following
1825 * photoo class logic
1831 function table_welcome($user)
1833 GLOBAL $table_wellarr, $G_lang;
1836 for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
1837 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
1843 function briscola_show($bri, $table, $user)
1845 GLOBAL $G_lang, $mlang_bin5_bin5;
1849 if ($table->asta_card == 9)
1850 $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
1852 /* text of caller cell */
1853 if ($user->table_pos == $table->asta_win) {
1854 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
1855 $ret .= sprintf($prestr, $ptnadd);
1858 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
1859 $ret .= sprintf($prestr,
1860 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1862 $ret .= sprintf('set_iscalling(%d);', ($table->asta_win - $user->table_pos + BIN5_PLAYERS_N) % BIN5_PLAYERS_N);
1864 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1866 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1867 $ret .= sprintf('$("caller").style.visibility = "visible";');
1868 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1869 $ret .= sprintf('$("astalascio").style.visibility = "";');
1870 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1871 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1877 function game_result($asta_pnt, $pnt)
1879 if ($asta_pnt == 61) {
1882 else if ($pnt == 60)
1888 if ($pnt >= $asta_pnt)
1895 function log_points($remote_addr, $curtime, $user, $where, $mesg)
1897 if (($fp = @fopen(LEGAL_PATH."/points.log", 'a')) != FALSE) {
1898 /* Unix time | session | nickname | IP | where was | mesg */
1899 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
1900 ($user->flags & USER_FLAG_AUTH ? 'A' : 'N'),
1901 $user->name, $remote_addr, $where , $mesg));
1906 function game_description($act, $form, $old_mult, $win = -1, $win_name = "?1?", $fri = -1, $fri_name = "?2?",
1907 $old_pnt = -1, $old_asta_pnt = -1)
1909 GLOBAL $G_lang, $mlang_bin5_bin5;
1911 if ($form == 'html') {
1916 $win_name = xcape($win_name);
1917 $fri_name = xcape($fri_name);
1926 if ($act == BIN5_RULES_OLDSCHEMA) {
1929 else if ($act == BIN5_RULES_ALLPASS) {
1930 return (sprintf($mlang_bin5_bin5['info_alpa'][$G_lang],
1931 $tg_br, $tg_hr, $tg_bo, $tg_bc));
1933 else if ($act == BIN5_RULES_ABANDON) {
1934 return (sprintf($mlang_bin5_bin5['info_aban'][$G_lang],
1935 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1939 $wol = game_result($old_asta_pnt, $old_pnt);
1943 if ($win != $fri) { // not alone case
1944 /* 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" */
1946 $noty .= sprintf($mlang_bin5_bin5['info_part'][$G_lang],
1947 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1950 if ($old_pnt == 120) {
1951 $noty .= sprintf($mlang_bin5_bin5['info_capp'][$G_lang],
1952 $tg_br, $tg_hr, $tg_bo, $tg_bc );
1955 if ($old_asta_pnt > 61) {
1956 $noty .= sprintf($mlang_bin5_bin5['info_alea'][$G_lang],
1957 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1958 $old_asta_pnt, $old_pnt,
1959 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
1960 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
1961 $mlang_bin5_bin5['info_lost'][$G_lang])));
1964 $noty .= sprintf($mlang_bin5_bin5['info_more'][$G_lang],
1965 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1967 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
1968 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
1969 $mlang_bin5_bin5['info_lost'][$G_lang])));
1970 } // else of if ($old_asta_pnt > 61) {
1971 } // else of if ($old_pnt == 120) {
1972 } // if ($win != $fri) { // not alone case
1974 $noty .= sprintf($mlang_bin5_bin5['info_alon'][$G_lang],
1975 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1977 if ($old_pnt == 120) {
1978 $noty .= sprintf($mlang_bin5_bin5['info_acap'][$G_lang],
1979 $tg_br, $tg_hr, $tg_bo, $tg_bc);
1982 if ($old_asta_pnt > 61) {
1983 $noty .= sprintf($mlang_bin5_bin5['info_aleaa'][$G_lang],
1984 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1985 $old_asta_pnt, $old_pnt,
1986 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
1987 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
1988 $mlang_bin5_bin5['info_lost'][$G_lang])));
1991 $noty .= sprintf($mlang_bin5_bin5['info_morea'][$G_lang],
1992 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1994 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
1995 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
1996 $mlang_bin5_bin5['info_lost'][$G_lang])));
2002 $old_multer = Bin5_table::s_multer($old_mult, $old_asta_pnt);
2003 if ($old_multer > 1) {
2004 $noty .= sprintf($mlang_bin5_bin5['info_omul'][$G_lang],
2005 $tg_br, $tg_hr, $tg_bo, $tg_bc,
2006 multoval($old_multer));
2008 $noty .= sprintf('%2$s%1$s', $tg_br, $tg_hr);