5 * Copyright (C) 2006-2012 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' => '%2$sNell\'ultima mano ha chiamato %3$s%5$s%4$s, il socio era %3$s%6$s%4$s,%1$s',
40 'en' => '%2$sIn 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' => '%2$sNell\'ultima partita %3$s%5$s%4$s si è chiamato in mano,%1$s',
60 'en' => '%2$sIn 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!.%2$s',
69 'en' => 'and he/she made %3$scapot%4$s WELL DONE!.%2$s'),
72 'info_omul' => array( 'it' => ' La partita valeva %3$s%5$s%4$s.',
73 'en' => ' EN: The game was worth %3$s%5$s%4$s.' ),
76 'info_alpa' => array( 'it' => ' Hanno passato %3$stutti%4$s.',
77 'en' => ' EN: Hanno passato %3$stutti%4$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_yturn'=> array( 'it' => ' tocca a <b>te</b> giocare.',
84 'en' => ' it\'s <b>your</b> turn.'),
85 'info_turn' => array( 'it' => 'tocca a <b>%s</b> giocare.',
86 'en' => 'it\'s the <b>%s</b>\'s turn.'),
87 'info_mult' => array( 'it' => ' La partita vale <b>%s</b>.',
88 'en' => ' The game worth <b>%s</b>.' ),
89 'info_yshuf'=> array( 'it' => 'Fai <b>tu</b> il mazzo, ',
90 'en' => 'It\'s <b>your</b> shuffled the cards, '),
91 'info_shuf' => array( 'it' => 'Il mazzo a <b>%s</b>, ',
92 'en' => '<b>%s</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
186 var $old_pnt; // points made by caller and callee
187 var $old_asta_win; // the old caller idx position at table
188 var $old_friend; // the old callee idx position at table
190 function Bin5_table()
196 function create($idx)
198 if (($thiz =& new Bin5_table()) == FALSE)
204 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
205 $thiz->asta_pla_n= -1;
206 $thiz->asta_card = -1;
207 $thiz->asta_pnt = -1;
210 $thiz->points = array( );
212 $thiz->total = array( 0, 0, 0, 0, 0);
213 $thiz->asta_win = -1;
214 $thiz->briscola = -1;
218 $thiz->old_reason = "";
219 $thiz->old_asta_pnt = -1;
220 $thiz->old_mult = -1;
222 $thiz->old_asta_win = -1;
223 $thiz->old_friend = -1;
230 function myclone(&$from)
232 if (($thiz =& new Bin5_table()) == FALSE)
237 $thiz->card = $from->card;
238 $thiz->mazzo = $from->mazzo; // REVIEW
239 $thiz->gstart = $from->gstart;
240 $thiz->turn = $from->turn;
242 $thiz->asta_pla = $from->asta_pla;
243 $thiz->asta_pla_n = $from->asta_pla_n;
244 $thiz->asta_card = $from->asta_card;
245 $thiz->asta_pnt = $from->asta_pnt;
247 $thiz->mult = $from->mult;
248 $thiz->points = $from->points;
249 $thiz->points_n = $from->points_n;
250 $thiz->total = $from->total;
252 $thiz->asta_win = $from->asta_win;
253 $thiz->briscola = $from->briscola;
254 $thiz->friend = $from->friend;
256 $thiz->old_reason = $from->old_reason;
257 $thiz->old_asta_pnt = $from->old_asta_pnt;
258 $thiz->old_mult = $from->mult;
259 $thiz->old_pnt = $from->old_pnt;
260 $thiz->old_asta_win = $from->old_asta_win;
261 $thiz->old_friend = $from->old_friend;
267 function parentcopy(&$from)
272 function spawn(&$from)
274 if (($thiz =& new Bin5_table()) == FALSE)
277 $thiz->parentcopy($from);
279 log_main("PLAYER_N - spawn.".$thiz->player_n);
281 $thiz->card = array();
282 $thiz->bunch_create();
283 $thiz->mazzo = rand(0,PLAYERS_N-1);
286 $thiz->old_asta_win = -1;
287 $thiz->old_reason = "";
289 // players are rearranged in an dedicated array
290 $thiz->player = array();
291 for ($i = 0 ; $i < $from->player_n ; $i++)
292 $thiz->player[$i] = $i;
294 log_main("TABLE_OLD_WIN - spawn:".$thiz->old_asta_win);
299 function asta2mult($asta_pnt)
303 else if ($asta_pnt > 100)
305 else if ($asta_pnt > 90)
307 else if ($asta_pnt > 80)
309 else if ($asta_pnt > 70)
315 function multer($is_new)
318 return (pow(2, $this->mult) * $this->asta2mult($this->asta_pnt));
321 return (pow(2, $this->old_mult) * $this->asta2mult($this->old_asta_pnt));
326 // function bunch_create_old() function AND
330 // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
331 // // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
332 // $ret[$i] =& new Card($i, 'bunch', 'no_owner');
339 function bunch_create()
343 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
344 $this->card[$i] = new Card($i, 'bunch', 'no_owner');
348 function bunch_make()
350 log_main("bunch_make start");
351 $ct = array(0,0,0,0,0);
353 mt_srand(make_seed());
355 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--)
358 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--) {
362 log_main("RND ZERO");
366 $owner = $i % BIN5_PLAYERS_N;
367 $this->card[$id]->assign('hand', $owner);
369 $rest[$rn] = $rest[$i];
370 // $pubbpos[$rn2] = $pubbpos[$i];
372 log_main("bunch_make end");
375 function init($userarr)
378 $this->mazzo = rand(0,PLAYERS_N-1);
381 $this->old_asta_win = -1;
382 $this->old_reason = "";
384 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
385 $this->total[$i] = 0;
386 $user_cur = $userarr[$this->player[$i]];
387 $user_cur->exitislock = TRUE;
390 log_main("table::init: ci siamo");
393 function game_init($userarr)
397 $this->gstart = ($this->mazzo+1) % BIN5_PLAYERS_N;
400 $this->asta_pla_n = BIN5_PLAYERS_N;
401 $this->asta_card = -1;
402 $this->asta_pnt = 60;
403 $this->asta_win = -1;
404 $this->briscola = -1;
408 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
409 $this->asta_pla[$i] = TRUE;
410 $user_cur = $userarr[$this->player[$i]];
411 $user_cur->subst = 'asta';
412 $user_cur->asta_card = -2;
413 $user_cur->asta_pnt = -1;
414 $user_cur->handpt = $this->hand_points($i);
415 // SEE function calculate_points(&$table)
420 function game_next($delta)
422 $this->old_mazzo = $this->mazzo;
423 $this->mazzo = ($this->mazzo + $delta) % BIN5_PLAYERS_N;
426 function mult_inc($val)
428 $this->old_mult = $this->mult;
432 function mult_set($val)
434 $this->old_mult = $this->mult;
438 function hand_points($idx)
440 GLOBAL $G_all_points;
444 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
445 // for ($i = 0 ; $i < 40 ; $i++) {
446 if ($this->card[$i]->owner != $idx)
449 $ctt = $this->card[$i]->value % 10;
450 $tot += $G_all_points[$ctt];
457 function exitlock_show($userarr, $table_pos)
459 $ct = $this->exitlock_calc($userarr, $table_pos);
461 $ret = sprintf('exitlock_show(%d, %s);', $ct,
462 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
466 function exitlock_calc(&$userarr, $table_pos)
470 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
471 if ($userarr[$this->player[$i]]->exitislock == FALSE)
478 function rules_engine($bri, $curtime, $action, $user)
480 GLOBAL $G_all_points, $G_dbasetype;
484 if ($action == BIN5_RULES_ALLPASS) { // return TRUE if all correct
485 $this->old_reason = game_description(BIN5_RULES_ALLPASS, 'html');
486 $this->old_asta_win = -1;
489 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
494 // $this->game_next(1);
495 $this->game_init(&$bri->user);
497 else if ($action == BIN5_RULES_ABANDON) { // return TRUE if all correct
498 log_wr(sprintf("GIOCO FINITO !!!"));
500 $this->old_reason = game_description(BIN5_RULES_ABANDON, 'html', $user->table_pos, $user->name);
501 $this->old_asta_win = $user->table_pos;
505 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
509 // Non si cambia mazzo se si abbandona la partita
511 // $this->game_next(0);
512 $this->game_init(&$bri->user);
514 else if ($action == BIN5_RULES_FINISH) { // return TRUE if all correct
518 if ($this->asta_pnt == 60)
519 $this->asta_pnt = 61;
521 $this->old_reason = "";
523 // count points for the temporary 2 teams
524 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
525 $ctt = $this->card[$i]->value % 10;
526 $own = $this->card[$i]->owner;
527 if ($own == $this->asta_win || $own == $this->friend)
528 $pro += $G_all_points[$ctt];
531 log_wr(sprintf("PRO: [%d]", $pro));
534 if ($this->asta_pnt == 61 && $pro == 60) {
535 $this->points[$this->points_n % MAX_POINTS] = array();
536 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
537 $this->points[$this->points_n % MAX_POINTS][$i] = 0;
541 $this->old_pnt = $pro;
548 if ($pro >= $this->asta_pnt)
553 $this->points[$this->points_n % MAX_POINTS] = array();
554 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
555 if ($i == $this->asta_win)
556 $pt = ($i == $this->friend ? 4 : 2);
557 else if ($i == $this->friend)
562 log_wr(sprintf("PRO: pt[%d][%d] = %d", $this->points_n % MAX_POINTS, $i, $pt));
564 $pt = $pt * $sig * $this->multer(TRUE) * ($pro == 120 ? 2 : 1);
566 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $this->multer(TRUE), ($pro == 120 ? 2 : 1)));
568 $this->points[$this->points_n % MAX_POINTS][$i] = $pt;
569 $this->total[$i] += $pt;
573 $this->old_pnt = $pro;
574 $this->old_asta_win = $this->asta_win;
584 $this->game_next($game_delta);
586 $plist = "$this->table_token|$user->table_orig|$this->player_n";
589 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
590 $user_cur = &$bri->user[$this->player[$i]];
593 $ucodes[$i] = $user_cur->code_get();
596 $plist .= '|'.xcapelt($user_cur->name).'|'.$pts[$i];
597 $codes .= '|'.xcapelt($user_cur->code_get());
600 log_legal($curtime, $user->ip, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
602 $this->old_asta_pnt = $this->asta_pnt;
603 // $this->old_mazzo is managed by ->game_next();
604 // $this->old_mult, $this->old_pnt, $this->old_reason and $this->old_asta_win are specific
606 $this->old_friend = $this->friend;
608 if ($user->table_orig < TABLES_AUTH_N) {
609 require_once("../Obj/dbase_".$G_dbasetype.".phh");
611 if (($bdb = BriskDB::create()) != FALSE) {
612 $bdb->bin5_points_save($curtime, $this, $user->table_orig, $ucodes, $pts);
616 log_points($remote_addr, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED");
618 log_points($remote_addr, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
621 $this->game_init(&$bri->user);
626 } // end class Bin5_table
631 define('BIN5_USER_FLAG_RING_ENDAUCT', 0x01);
633 class Bin5_user extends User {
636 var $handpt; // Total card points at the beginning of the current hand.
637 var $exitislock; // Player can exit from the table ?
638 var $privflags; // Flags for briskin5 only
646 function create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
647 if (($thiz =& new User()) == FALSE)
650 $thiz->asta_card = -2;
651 $thiz->asta_pnt = -1;
653 $thiz->exitislock = TRUE;
654 $thiz->privflags = 0;
660 function parentcopy(&$from)
665 function copy(&$from)
667 $this->parentcopy($from);
669 $this->asta_card = $from->asta_card;
670 $this->asta_pnt = $from->asta_pnt;
671 $this->handpt = $from->handpt;
672 $this->exitislock = $from->exitislock;
673 $this->privflags = $from->privflags;
677 function myclone(&$from)
679 if (($thiz =& new User()) == FALSE)
688 static function spawn($from, &$bri, $table, $table_pos, $get, $post, $cookie)
690 if (($thiz = new Bin5_user()) == FALSE)
693 if (($CO_bin5_pref_ring_endauct = gpcs_var("CO_bin5_pref_ring_endauct", $get, $post, $cookie)) === FALSE) {
694 $CO_bin5_pref_ring_endauct = "";
697 $thiz->parentcopy($from);
699 /* NOTE: at this moment idx and table_pos fields have the same value
700 but diffentent functions, we keep them separated for a while */
702 $thiz->idx = $table_pos;
703 $thiz->asta_card = -2;
704 $thiz->asta_pnt = -1;
706 $thiz->exitislock = TRUE;
708 log_wr("Bin5 constructor");
710 $thiz->privflags = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
712 $thiz->table_orig = $table;
714 $thiz->table_pos = $table_pos;
721 function step_set($step)
723 $this->step = $step & 0x7fffffff;
728 function step_inc($delta = 1) {
729 $this->step += $delta;
730 /* modularization because unpack() not manage unsigned 32bit int correctly */
731 $this->step &= 0x7fffffff;
736 static function load_step($tab_id, $sess)
740 if (validate_sess($sess) == FALSE)
743 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
744 mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
745 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step", 'rb')) == FALSE)
747 if (($s = fread($fp, 8)) == FALSE)
749 if (mb_strlen($s, "ASCII") != 8)
751 $arr = unpack('Ls/Li', $s);
754 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
761 log_rd2("STEP_GET [".$sess."]: return false ");
769 if (validate_sess($this->sess) == FALSE)
771 if (file_exists(BIN5_PROXY_PATH."/table".$this->table_orig) == FALSE)
772 mkdir(BIN5_PROXY_PATH."/table".$this->table_orig, 0775, TRUE);
773 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$this->table_orig."/".$this->sess.".step", 'w')) == FALSE)
775 fwrite($fp, pack("LL",$this->step, $this->idx));
778 log_main("step_set [".$this->sess. "] [".$this->step."]");
786 static function unproxy_step($tab_id, $sess)
788 log_rd2("UNPROXY: ".BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
789 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
792 @unlink(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
795 function destroy_data($tab_id)
798 if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos, "B")) == -1) {
799 log_crit("BIN5 USER DATA REMOVE FAILED 1 [".FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos."]");
803 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
804 log_crit("BIN5 USER DATA REMOVE FAILED 2");
807 if (shmop_delete($shm) == 0) {
808 log_crit("BIN5 USER DATA REMOVE FAILED 3");
813 log_main("BIN5 USER DATA DESTROY SUCCESS");
815 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
825 static function blocking_error($is_unrecoverable)
827 log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
828 return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
831 protected function page_sync($sess, $page)
833 log_rd2("PAGE_SYNC");
834 printf("xXx BIN5_USER::PAGE_SYNC\n");
835 return (sprintf('xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
838 protected function maincheck($cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate, $table_idx, $table_token)
840 GLOBAL $G_lang, $mlang_indrd;
841 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
842 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
843 $CO_splashdate = "CO_splashdate".$G_splash_idx;
844 $$CO_splashdate = $splashdate;
848 log_rd("maincheck begin");
853 /* Nothing changed, return. */
854 if ($cur_step == $this->step)
857 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$this->stat."] cur_step[".$cur_step."] user_step[".$this->step."]");
859 if ($cur_step == -1) {
861 * if $cur_step == -1 load the current state from the main struct
864 $S_load_stat['wR_minusone']++;
866 // if ($this->the_end == TRUE) {
867 // log_rd2("main_check: the end".var_export(debug_backtrace()));
870 if ($this->trans_step != -1) {
871 log_rd2("TRANS USATO ".$this->trans_step);
872 $cur_step = $this->trans_step;
873 $this->trans_step = -1;
876 log_rd2("TRANS NON ATTIVATO");
881 /* this part I suppose is read only on $this->room structure */
882 if ($cur_step == -1) {
883 log_rd2("PRE-NEWSTAT");
890 if ($this->stat == 'table') {
892 /* NOTE: $this->room is associated with the current $bri object */
893 printf("xXx CLASS NAME [%s]\n", get_class($this->room));
894 $ret = show_table(&$this->room, $this, $this->step, FALSE, FALSE);
896 log_rd2("NEWSTAT: ".$this->stat);
898 $new_stat = $this->stat;
899 $new_subst = $this->subst;
900 $new_step = $this->step;
901 } /* if ($cur_step == -1) { */
903 /* $sem = Room::lock_data(FALSE); */
904 $S_load_stat['rU_heavy']++;
906 if ($cur_step < $this->step) {
908 if ($cur_step + COMM_N < $this->step) {
909 if (($cur_stat != $this->stat)) {
910 $to_stat = $this->stat;
911 /* Room::unlock_data($sem); */
913 printf("xXx BIN5_USER::MAINCHECK\n");
914 return ($this->page_sync($this->sess, ($to_stat == "table" ? "index.php" : "../index.php"), $this->table, $this->table_token));
916 log_rd2("lost history, refresh from scratch");
917 printf("xXx LOST HISTORY!\n");
921 for ($i = $cur_step ; $i < $this->step ; $i++) {
923 log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
924 $ret .= $this->comm[$ii];
926 $new_stat = $this->stat;
927 $new_subst = $this->subst;
928 $new_step = $this->step;
931 log_rd2($this->step, 'index_rd.php: after ret set');
933 if ($this->the_end == TRUE) {
934 log_rd2("LOGOUT BYE BYE!!");
935 log_auth($this->sess, "Explicit logout.");
937 if ($this->the_end == TRUE) {
941 /* if ($this->subst == 'sitdown') { */
942 /* log_load("ROOM WAKEUP"); */
943 /* $this->room->room_wakeup($this); */
945 /* else if ($this->subst == 'standup') */
946 /* $this->room->room_outstandup($this); */
948 /* log_rd2("LOGOUT FROM WHAT ???"); */
950 } /* if ($this->the_end == TRUE) { ... */
951 } /* if ($this->the_end == TRUE) { ... */
952 } /* if ($cur_step < $this->step) { */
954 /* Room::unlock_data($sem); */
955 } /* else of if ($cur_step == -1) { */
959 } // function maincheck (...
966 } // end class Bin5_user
971 static $delta_t = array();
975 var $comm; // commands for many people
976 var $step; // current step of the comm array
977 var $garbage_timeout;
988 function Bin5 ($room, $table_idx, $table_token, $get, $post, $cookie) {
989 $this->user = array();
990 $this->table = array();
992 $this->the_end = FALSE;
993 $this->shm_sz = BIN5_SHM_MIN;
994 if (($this->tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
1000 $table = $room->table[$table_idx];
1002 log_wr("Bin5 constructor");
1004 for ($i = 0 ; $i < $table->player_n ; $i++) {
1005 $user[$table->player[$i]]->table_token = $table_token;
1006 $this->user[$i] = Bin5_user::spawn($user[$table->player[$i]], $this, $table_idx, $i, $get, $post, $cookie);
1008 $this->table[0] = Bin5_table::spawn(&$table);
1010 log_main("TABLE_OLD_WIN - Bin5:".$this->table[0]->old_asta_win);
1012 $this->table_idx = $table_idx;
1013 $this->table_token = $table_token;
1014 $this->garbage_timeout = 0;
1016 $this->delay_mgr = new Delay_Manager((GARBAGE_TIMEOUT *3.0) / 2.0);
1018 log_wr("Bin5 constructor end");
1022 function get_user($sess, &$idx)
1026 if (validate_sess($sess)) {
1027 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1028 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1031 $ret = &$this->user[$i];
1035 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1036 // for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++)
1037 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1040 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1047 function garbage_manager($force)
1051 /* Garbage collector degli utenti in timeout */
1055 $delta = $this->delay_mgr->delta_get($curtime);
1057 if ($force || $this->garbage_timeout < $curtime) {
1058 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1059 $user_cur = $this->user[$i];
1060 if ($user_cur->sess == "" ||
1061 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
1064 if ($user_cur->lacc + EXPIRE_TIME_RD < ($curtime - $delta)) { // Auto logout dell'utente
1065 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
1067 if ($user_cur->stat == 'table') {
1068 log_auth($user_cur->sess," bin5 Autologout session.");
1070 /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE
1072 $tmp_sess = $user_cur->sess;
1073 $user_cur->sess = "";
1074 Bin5_user::step_unproxy($tmp_sess);
1075 $user_cur->name = "";
1076 $user_cur->the_end = FALSE;
1080 /* se gli altri utenti non erano d'accordo questo utente viene bannato */
1081 $remcalc = $this->table[0]->exitlock_calc(&$this->user, $user_cur->table_pos);
1083 require_once("${G_base}Obj/hardban.phh");
1084 Hardbans::add(($user_cur->flags & USER_FLAG_AUTH ? $user_cur->name : FALSE),
1085 $user_cur->ip, $user_cur->sess, $user_cur->laccwr + BAN_TIME);
1087 // $user->bantime = $user->laccwr + BAN_TIME;
1089 $this->table_wakeup($user_cur);
1093 log_rd2($user_cur->sess." GARBAGE UPDATED!");
1095 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
1100 $this->delay_mgr->lastcheck_set($curtime);
1105 function destroy_data()
1111 log_main("DESTROY BRISKIN5 DATA");
1114 log_main("DESTROY2 BRISKIN5 DATA [".$this->table_idx."]");
1115 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1116 $this->user[$i]->destroy_data($this->table_idx);
1117 Bin5_user::unproxy_step($this->table_idx, $this->user[$i]->sess);
1119 if (($tok = @ftok(FTOK_PATH."/bin5/table".$this->table_idx."/table", "B")) == -1)
1122 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
1125 if (shmop_delete($shm) == 0) {
1126 log_only("REMOVE FALLITA ");
1131 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
1133 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
1143 static function lock_data($is_exclusive, $table_idx)
1145 if (($res = file_lock(FTOK_PATH."/bin5/table".$table_idx."/table", $is_exclusive)) != FALSE) {
1146 self::$delta_t = microtime(TRUE);
1147 log_lock("LOCK table [".$table_idx."] [".self::$delta_t[$table_idx]."]");
1149 return (new Vect(array('res' => $res, 'tab' => $table_idx)));
1156 static function unlock_data($res_vect)
1160 $res = $res_vect->getbyid('res');
1161 $tab = $res_vect->getbyid('tab');
1163 log_lock("UNLOCK table [".$tab."] [".(microtime(TRUE) - (self::$delta_t[$tab]))."]");
1169 function chatt_send($user, $mesg)
1171 GLOBAL $mlang_brisk, $G_lang;
1173 if ($user->stat == 'table') {
1174 $table = &$this->table[$user->table];
1177 $user_mesg = substr($mesg,6);
1181 $dt = date("H:i ", $curtime);
1182 if (strncmp($user_mesg, "/nick ", 6) == 0) {
1183 log_main($user->sess." chatt_send BEGIN");
1185 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
1186 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1187 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
1192 $user_mesg = "COMMAND ".$user_mesg;
1195 // update local graph
1196 // update remote graphs
1197 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1198 $user_cur = $this->user[$i];
1199 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1200 if ($user_cur->sess == '')
1202 if (strcasecmp($user_cur->name, $name_new) == 0) {
1203 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1205 $premsg = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
1206 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $premsg);
1211 if ($i == BIN5_MAX_PLAYERS) {
1212 if ($user->flags & USER_FLAG_AUTH && strcasecmp($user->name,$name_new) != 0) {
1213 if ($this->table[$user->table]->auth_only == TRUE) {
1214 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1215 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>%s</b>");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
1220 $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
1224 $user->name = $name_new;
1226 log_main($user->sess." chatt_send start set");
1229 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1230 log_main($user->sess." chatt_send set loop");
1232 $user_cur = &$this->user[$i];
1233 if ($user_cur->sess == '')
1236 if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
1237 $table = &$this->table[$user->table];
1239 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1240 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1241 $this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->flags,
1242 xcape($this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->name),
1244 $this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->flags,
1245 xcape($this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->name),
1247 $this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->flags,
1248 xcape($this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->name),
1250 (BIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->flags),
1251 (BIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->name)),
1253 (BIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->flags),
1254 (BIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->name)));
1255 if ($user_cur == $user) {
1256 $user_cur->comm[$user_cur->step % COMM_N] .= $user_cur->myname_innerHTML();
1258 $user_cur->step_inc();
1264 for ($i = 0 ; $i < ($user->stat == 'room' ? BIN5_MAX_PLAYERS : BIN5_PLAYERS_N) ; $i++) {
1265 if ($user->stat == 'room') {
1266 $user_cur = &$this->user[$i];
1267 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1271 $user_cur = &$this->user[$table->player[$i]];
1274 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1275 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
1276 $dt, $user->flags, xcape($user->name), xcape($user_mesg));
1277 $user_cur->step_inc();
1279 log_legal($curtime, $user->ip, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
1283 function table_wakeup($user)
1285 $table = &$this->table[0];
1287 log_main("BIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
1291 log_main("BIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1293 for ($i = 0 ; $i < $table->player_n ; $i++) {
1294 $user_cur = &$this->user[$i];
1295 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1297 if ($user_cur == $user)
1298 $user_cur->subst = "shutdowner";
1300 $user_cur->subst = "shutdowned";
1301 $user_cur->laccwr = $curtime;
1303 $ret = "gst.st = ".($user_cur->step+1)."; ";
1304 $ret .= 'gst.st_loc++; xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
1306 log_wr($user_cur->sess." BIN5_WAKEUP: ".$ret);
1307 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1308 $user_cur->step_inc();
1311 $this->the_end = TRUE;
1315 * If all players are freezed the room garbage_manager clean up table and users.
1317 function is_abandoned()
1322 $table = &$this->table[0];
1324 for ($i = 0 ; $i < $table->player_n ; $i++) {
1325 $user_cur = &$this->user[$i];
1327 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
1336 static function request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
1338 GLOBAL $G_black_list;
1340 printf("NEW_SOCKET (root): %d\n", intval($new_socket));
1342 $enc = get_encoding($header);
1343 if (isset($header['User-Agent'])) {
1344 if (strstr($header['User-Agent'], "MSIE")) {
1345 $transp_type = "htmlfile";
1348 $transp_type = "xhr";
1352 $transp_type = "iframe";
1354 force_no_cache($header_out);
1356 if (($table_idx = gpcs_var('table_idx', $get, $post, $cookie)) === FALSE)
1359 if (($table_token = gpcs_var('table_token', $get, $post, $cookie)) === FALSE)
1360 unset($table_token);
1367 bin5_index_main($transp_type, $header_out, $addr, $get, $post, $cookie);
1368 $content = ob_get_contents();
1371 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1375 case "index_wr.php":
1376 // FIXME $content can be unset
1377 if (isset($table_idx) && isset($table_token)) {
1378 if (($bri = $s_a_p->app->match_get($table_idx, $table_token)) != FALSE) {
1380 bin5_index_wr_main($bri, $addr, $get, $post, $cookie);
1381 $content = ob_get_contents();
1385 $content = "Bin5 Load data error";
1389 $content = "Bin5 Load data error";
1391 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1395 case "index_rd_ifra.php":
1396 if (($transp = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
1398 if ($transp == 'websocket')
1402 if (!isset($table_idx)
1403 || !isset($table_token)
1404 || !isset($cookie['sess'])
1405 || ($bri = $s_a_p->app->match_get($table_idx, $table_token)) == NULL
1406 || (($user = $bri->get_user($cookie['sess'], $idx)) == FALSE)) {
1408 $content = Bin5_user::stream_fini($transp_type, $s_a_p->rndstr, TRUE);
1409 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1414 // close a previous opened index_read_ifra socket, if exists
1415 if (($prev = $user->rd_socket_get()) != NULL) {
1416 $s_a_p->socks_unset($user->rd_socket_get());
1417 fclose($user->rd_socket_get());
1418 printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
1419 $user->rd_socket_set(NULL);
1423 $user->stream_init($s_a_p->rndstr, $enc, $header, $header_out, $content, $get, $post, $cookie);
1425 $response = headers_render($header_out, -1).$user->chunked_content($content);
1426 $response_l = mb_strlen($response, "ASCII");
1428 $wret = @fwrite($new_socket, $response, $response_l);
1429 if ($wret < $response_l) {
1430 printf("TROUBLES WITH FWRITE: %d\n", $wret);
1431 $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
1434 $user->rd_cache_set("");
1436 fflush($new_socket);
1439 $s_a_p->socks_set($new_socket, $user, NULL);
1440 $user->rd_socket_set($new_socket);
1441 printf(" - qui ci siamo - ");
1460 function locshm_exists($tok)
1464 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
1465 log_main($tok." SHM NOT exists");
1471 log_main($tok." SHM exists");
1479 // if success an array with points of each player is returned
1480 function calculate_points_old(&$table)
1482 GLOBAL $G_all_points;
1487 if ($table->asta_pnt == 60)
1488 $table->asta_pnt = 61;
1490 $table->old_reason = "";
1491 $table->old_asta_win = $table->asta_win;
1492 $table->old_friend = $table->friend;
1493 $table->old_asta_pnt = $table->asta_pnt;
1494 $table->old_mult = $table->mult;
1496 // count points for the temporary 2 teams
1497 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1498 // for ($i = 0 ; $i < 40 ; $i++) {
1499 $ctt = $table->card[$i]->value % 10;
1500 $own = $table->card[$i]->owner;
1501 if ($own == $table->asta_win || $own == $table->friend)
1502 $pro += $G_all_points[$ctt];
1505 log_wr(sprintf("PRO: [%d]", $pro));
1508 if ($table->asta_pnt == 61 && $pro == 60) {
1509 $table->points[$table->points_n % MAX_POINTS] = array();
1510 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1511 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1515 $table->old_pnt = $pro;
1521 if ($pro >= $table->asta_pnt)
1526 $table->points[$table->points_n % MAX_POINTS] = array();
1527 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1528 if ($i == $table->asta_win)
1529 $pt = ($i == $table->friend ? 4 : 2);
1530 else if ($i == $table->friend)
1537 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1539 $pt = $pt * $sig * $table->multer(TRUE) * ($pro == 120 ? 2 : 1);
1541 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->multer(TRUE), ($pro == 120 ? 2 : 1)));
1543 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1544 $table->total[$i] += $pt;
1548 $table->old_pnt = $pro;
1556 is_transition (is from room to table ?)
1557 is_again (is another game)
1559 Examples of $is_transition, $is_again:
1560 from reload of the page: FALSE, FALSE
1561 from sitdown in room: TRUE, FALSE
1562 from table: asta cmd e tutti passano: TRUE, TRUE
1563 from table: fine partita: TRUE, TRUE
1565 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1567 $table_idx = $user->table;
1568 $table = $bri->table[$table_idx];
1569 $table_pos = $user->table_pos;
1571 $ret = "table_init();";
1572 $ret .= $table->exitlock_show(&$bri->user, $table_pos);
1574 /* GENERAL STATUS */
1575 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1576 $sendstep, $user->stat, $user->subst, $table_pos);
1578 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1581 $ret .= "background_set();";
1584 $ret .= $user->myname_innerHTML();
1585 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1586 $bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->flags,
1587 xcape($bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->name),
1589 $bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->flags,
1590 xcape($bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->name),
1592 $bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->flags,
1593 xcape($bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->name),
1595 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->flags),
1596 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->name)),
1598 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->flags),
1599 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->name)));
1601 /* NOTIFY FOR THE CARD MAKER */
1602 if ($is_transition) { // && $user->subst == "asta" superfluo
1603 $ret .= show_table_info(&$bri, &$table, $table_pos);
1604 $ret .= "setTimeout(preload_images, 500, g_preload_img_arr, g_imgct);";
1607 $ret .= table_welcome($user);
1609 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1610 $ret .= playsound("cow.mp3");
1615 if ($is_transition) { // && $user->subst == "asta" superfluo
1618 for ($i = 0 ; $i < BIN5_CARD_HAND ; $i++) {
1619 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++) {
1621 for ($o = 0 ; $o < (BIN5_CARD_HAND * BIN5_PLAYERS_N) && $ct < $i+1 ; $o++) {
1622 // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1623 if ($table->card[$o]->owner == (($e + $table->gstart) % BIN5_PLAYERS_N)) {
1629 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1631 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BIN5_PLAYERS_N,
1632 $i, ((($e + BIN5_PLAYERS_N - $table_pos + $table->gstart) % BIN5_PLAYERS_N) == 0 ?
1633 $table->card[$o]->value : -1),
1634 ($i == 7 && $e == (BIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1639 $taked = array(0,0,0,0,0);
1640 $inhand = array(0,0,0,0,0);
1641 $ontabl = array(-1,-1,-1,-1,-1);
1644 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1645 // for ($i = 0 ; $i < 40 ; $i++) {
1646 if ($table->card[$i]->stat == 'hand') {
1647 if ($table->card[$i]->owner == $table_pos) {
1648 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1650 $inhand[$table->card[$i]->owner]++;
1652 else if ($table->card[$i]->stat == 'take') {
1653 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1654 $taked[$table->card[$i]->owner]++;
1656 else if ($table->card[$i]->stat == 'table') {
1657 $ontabl[$table->card[$i]->owner] = $i;
1661 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1662 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1664 log_main("Stat table: ".$logg);
1666 /* Set ours cards. */
1668 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1669 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1670 for ($i = $inhand[$table_pos] ; $i < BIN5_CARD_HAND ; $i++)
1671 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1672 $ret .= sprintf('card_setours(%s);', $oursarg);
1674 /* Dispose all cards */
1675 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1676 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1677 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1678 ($inhand[$i] <= BIN5_CARD_HAND ? $inhand[$i] : BIN5_CARD_HAND), $taked[$i]);
1680 if ($ontabl[$i] != -1) {
1681 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1682 $table->card[$ontabl[$i]]->value,
1683 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1689 if ($user->subst == 'asta') {
1691 /* show users auction status */
1693 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1694 $user_cur = &$bri->user[$table->player[$i]];
1695 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1696 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1698 if (BIN5_PLAYERS_N == 3)
1699 $showst .= ",-2,-2";
1700 $ret .= sprintf('document.title = "Brisk - Tavolo %d (asta)";', $user->table_orig);
1701 $ret .= sprintf('show_astat(%s);', $showst);
1703 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1704 /* show card chooser */
1705 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1710 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N) &&
1711 $table->asta_win == -1)
1712 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1713 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1715 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1716 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1720 if ($table->asta_win == -1) { // auction case
1721 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N))
1722 $ret .= "remark_on();";
1724 $ret .= "remark_off();";
1726 else { // chooseed case
1727 if ($table_pos == $table->asta_win)
1728 $ret .= "remark_on();";
1730 $ret .= "remark_off();";
1733 else if ($user->subst == 'game') {
1735 if (($table->gstart + $table->turn) % BIN5_PLAYERS_N == $table_pos)
1736 $ret .= "is_my_time = true; remark_on();";
1738 $ret .= "remark_off();";
1740 /* WHO CALL AND WHAT */
1741 $ret .= briscola_show($bri, $table, $user);
1745 } // end function show_table(...
1747 function calculate_winner(&$table)
1754 $cur_seed = $table->briscola - ($table->briscola % 10);
1756 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1757 // for ($i = 0 ; $i < 40 ; $i++) {
1758 if ($table->card[$i]->stat != "table")
1761 log_wr(sprintf("Card On table: [%d]", $i));
1763 $v = $table->card[$i]->value;
1764 $ontab[$table->card[$i]->owner] = $v;
1765 $ontid[$table->card[$i]->owner] = $i;
1766 /* se briscola setto il flag */
1767 if (($v - ($v % 10)) == $cur_seed)
1771 if ($briontab == FALSE) {
1772 $cur_win = $table->gstart;
1773 $cur_val = $ontab[$cur_win];
1774 $cur_seed = $cur_val - ($cur_val % 10);
1777 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1778 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1779 if ($ontab[$i] < $cur_val) {
1780 $cur_val = $ontab[$i];
1786 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1787 $table->card[$ontid[$i]]->owner = $cur_win;
1788 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1793 function show_table_info(&$bri, &$table, $table_pos)
1795 GLOBAL $G_lang, $mlang_bin5_bin5;
1798 $user = $bri->user[$table->player[$table_pos]];
1800 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1801 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1804 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++)
1805 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1806 $noty .= sprintf("</tr>");
1809 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1811 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1812 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1813 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1814 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1819 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1820 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1821 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1822 $noty .= "</tr></table>";
1824 if ($table->old_reason != "") {
1825 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1828 if ($table->old_asta_win != -1) {
1829 log_main("TABLE_OLD_WIN:".$table->old_asta_win);
1830 $win = $table->player[$table->old_asta_win];
1831 log_main("TABLE_OLD_FRIEND:".$table->old_friend);
1832 $fri = $table->player[$table->old_friend];
1834 $noty = game_description(BIN5_RULES_FINISH, 'html', $win, $bri->user[$win]->name, $fri, $bri->user[$fri]->name,
1835 $table->old_pnt, $table->old_asta_pnt);
1837 $old_multer = $table->multer(FALSE);
1838 if ($old_multer > 1) {
1839 $noty .= sprintf($mlang_bin5_bin5['info_omul'][$G_lang], multoval($old_multer));
1841 $noty .= "<hr><br>";
1843 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1844 if ($table->mazzo == $table_pos)
1845 $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
1847 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1848 $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
1851 if ($user->subst == 'asta') {
1852 if ($table->asta_win == -1) // auction case
1853 $curplayer = $table->gstart % BIN5_PLAYERS_N;
1855 $curplayer = $table->asta_win;
1857 else if ($user->subst == 'game') {
1858 $curplayer = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
1861 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1862 if ($curplayer == $table_pos) {
1863 $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
1866 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1867 $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
1870 $multer = $table->multer(TRUE);
1872 $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($multer) );
1874 $noty .= "<hr><br>";
1875 $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
1876 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
1877 * 130 + ($table->points_n > 0 ? 50 : 0) +
1878 * (120 * ($table->points_n / MAX_POINTS)));
1879 * will be used when we refact notify js function following
1880 * photoo class logic
1886 function table_welcome($user)
1888 GLOBAL $table_wellarr, $G_lang;
1891 for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
1892 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
1898 function briscola_show($bri, $table, $user)
1900 GLOBAL $G_lang, $mlang_bin5_bin5;
1904 if ($table->asta_card == 9)
1905 $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
1907 /* text of caller cell */
1908 if ($user->table_pos == $table->asta_win) {
1909 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
1910 $ret .= sprintf($prestr, $ptnadd);
1913 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
1914 $ret .= sprintf($prestr,
1915 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1917 $ret .= sprintf('set_iscalling(%d);', ($table->asta_win - $user->table_pos + BIN5_PLAYERS_N) % BIN5_PLAYERS_N);
1919 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1921 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1922 $ret .= sprintf('$("caller").style.visibility = "visible";');
1923 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1924 $ret .= sprintf('$("astalascio").style.visibility = "";');
1925 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1926 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1932 function game_result($asta_pnt, $pnt)
1934 if ($asta_pnt == 61) {
1937 else if ($pnt == 60)
1943 if ($pnt >= $asta_pnt)
1950 function log_points($remote_addr, $curtime, $user, $where, $mesg)
1952 if (($fp = @fopen(LEGAL_PATH."/points.log", 'a')) != FALSE) {
1953 /* Unix time | session | nickname | IP | where was | mesg */
1954 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
1955 ($user->flags & USER_FLAG_AUTH ? 'A' : 'N'),
1956 $user->name, $remote_addr, $where , $mesg));
1961 function game_description($act, $form, $win = -1, $win_name = "?1?", $fri = -1, $fri_name "?2?",
1962 $old_pnt = -1, $old_asta_pnt = -1)
1964 GLOBAL $G_lang, $mlang_bin5_bin5;
1966 if ($form == 'html') {
1971 $win_name = xcape($win_name);
1972 $fri_name = xcape($fri_name);
1981 if ($act == BIN5_RULES_ALLPASS) {
1982 return (sprintf($mlang_bin5_bin5['info_alpa'][$G_lang],
1983 $tg_br, $tg_hr, $tg_bo, $tg_bc));
1985 else if ($act == BIN5_RULES_ABANDON) {
1986 return (sprintf($mlang_bin5_bin5['info_alpa'][$G_lang],
1987 $tg_br, $tg_hr, $tg_bo, $tg_bc,
1990 // implicit else bin5_rules_finish
1991 $wol = game_result($old_asta_pnt, $old_pnt);
1995 if ($win != $fri) { // not alone case
1997 /* 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" */
1999 $noty .= sprintf($mlang_bin5_bin5['info_part'][$G_lang],
2000 $tg_br, $tg_hr, $tg_bo, $tg_bc,
2003 if ($old_pnt == 120) {
2004 $noty .= sprintf($mlang_bin5_bin5['info_capp'][$G_lang],
2005 $tg_br, $tg_hr, $tg_bo, $tg_bc );
2008 if ($old_asta_pnt > 61) {
2009 $noty .= sprintf($mlang_bin5_bin5['info_alea'][$G_lang],
2010 $tg_br, $tg_hr, $tg_bo, $tg_bc,
2011 $old_asta_pnt, $old_pnt,
2012 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
2013 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
2014 $mlang_bin5_bin5['info_lost'][$G_lang])));
2017 $noty .= sprintf($mlang_bin5_bin5['info_more'][$G_lang],
2018 $tg_br, $tg_hr, $tg_bo, $tg_bc,
2020 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
2021 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
2022 $mlang_bin5_bin5['info_lost'][$G_lang])));
2023 } // else of if ($old_asta_pnt > 61) {
2024 } // else of if ($old_pnt == 120) {
2025 } // if ($win != $fri) { // not alone case
2027 $noty .= sprintf($mlang_bin5_bin5['info_alon'][$G_lang],
2028 $tg_br, $tg_hr, $tg_bo, $tg_bc,
2030 if ($old_pnt == 120) {
2031 $noty .= sprintf($mlang_bin5_bin5['info_acap'][$G_lang],
2032 $tg_br, $tg_hr, $tg_bo, $tg_bc);
2035 if ($old_asta_pnt > 61) {
2036 $noty .= sprintf($mlang_bin5_bin5['info_aleaa'][$G_lang],
2037 $tg_br, $tg_hr, $tg_bo, $tg_bc,
2038 $old_asta_pnt, $old_pnt,
2039 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
2040 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
2041 $mlang_bin5_bin5['info_lost'][$G_lang])));
2044 $noty .= sprintf($mlang_bin5_bin5['info_morea'][$G_lang],
2045 $tg_br, $tg_hr, $tg_bo, $tg_bc,
2047 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
2048 ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
2049 $mlang_bin5_bin5['info_lost'][$G_lang])));