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 'info_part' => array( 'it' => '<hr>Nell\'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>',
39 'en' => '<hr>In the last hand the declarer was <b>%s</b>, the partner was <b>%s</b>,<br>'),
40 'info_capp' => array( 'it' => 'hanno fatto <b>cappotto</b> EBBRAVI!.<br>',
41 'en' => 'and they made <b>capot</b> WELL DONE!.<br>'),
42 'info_pnt' => array( 'it' => 'dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno %s.<br>',
43 'en' => 'they had to do <b>%s</b> points and they had made <b>%d</b>: they have %s.<br>'),
44 'info_alea' => array( 'it' => 'almeno ',
46 'info_more' => array( 'it' => 'più di 60',
48 'info_win' => array( 'it' => '<b>vinto</b>',
49 'en' => '<b>win</b>'),
50 'info_peer' => array( 'it' => '<b>pareggiato</b>',
51 'en' => '<b>drew</b>'),
52 'info_lost' => array( 'it' => '<b>perso</b>',
53 'en' => '<b>lost</b>'),
55 'info_alon' => array( 'it' => '<hr>Nell\'ultima partita <b>%s</b> si è chiamato in mano,<br>',
56 'en' => '<hr>In the last hand <b>%s</b> play alone against each other,<br>'),
57 'info_apnt' => array( 'it' => 'doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha %s.<br>',
58 'en' => 'he/she had to do <b>%s</b> points and they had made <b>%d</b>: he/she had %s.<br>'),
59 'info_acap' => array( 'it' => 'ha fatto <b>cappotto</b> EBBRAVO!.<hr>',
60 'en' => 'and he/she made <b>capot</b> WELL DONE!.<hr>'),
62 'info_omul' => array( 'it' => ' La partita valeva <b>%s</b>.',
63 'en' => ' EN: The game was worth <b>%s</b>.' ),
65 'info_yturn'=> array( 'it' => ' tocca a <b>te</b> giocare.',
66 'en' => ' it\'s <b>your</b> turn.'),
67 'info_turn' => array( 'it' => 'tocca a <b>%s</b> giocare.',
68 'en' => 'it\'s the <b>%s</b>\'s turn.'),
69 'info_mult' => array( 'it' => ' La partita vale <b>%s</b>.',
70 'en' => ' The game worth <b>%s</b>.' ),
71 'info_yshuf'=> array( 'it' => 'Fai <b>tu</b> il mazzo, ',
72 'en' => 'It\'s <b>your</b> shuffled the cards, '),
73 'info_shuf' => array( 'it' => 'Il mazzo a <b>%s</b>, ',
74 'en' => '<b>%s</b> shuffled the cards, '),
75 'btn_bkgame'=> array( 'it' => 'torna alla partita',
76 'en' => 'back to the game'),
77 'call_wptn' => array( 'it' => '<br>con %d punti',
78 'en' => '<br>with %d points'),
79 'call_ycall'=> array( 'it' => 'Chiami%s:',
81 'call_call' => array( 'it' => 'Chiama %s%s:',
82 'en' => 'The declarer is %s%s:')
86 $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.'),
87 '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.') );
89 function multoval($mult)
94 return ($G_lang == 'en' ? 'double' : 'doppio');
96 return ($G_lang == 'en' ? 'triple' : 'triplo');
98 return ($G_lang == 'en' ? 'quadruple' : 'quadruplo');
100 return (sprintf(($G_lang == 'en' ? "%d-ple" : "%d-plo"), $mult));
105 var $value; /* 0 - 39 card value */
106 var $stat; /* 'bunch', 'hand', 'table', 'take' */
107 var $owner; /* (table position 0-4) */
108 // var $pos; /* Pos in hand. */
109 var $x; /* When played the X position on the table of the owner. */
110 var $y; /* When played the Y position on the table of the owner. */
112 function Card($value, $stat, $owner)
114 $this->value = $value;
115 $this->stat = $stat; // Card stat
116 $this->owner = $owner;
119 function assign($stat,$owner)
121 $this->stat = $stat; // Card stat
122 $this->owner = $owner;
125 function setpos($pos)
132 $this->stat = 'table'; // Card stat
137 function take($newown)
139 $this->stat = 'take'; // Card stat
140 $this->owner = $newown;
144 class Bin5_table extends Table {
145 var $card; // il mazzo di carte
146 var $mazzo; // chi e' di mazzo
156 var $points; // points array
157 var $points_n; // number of row of points
160 var $asta_win; // the caller idx position at table
162 var $friend; // the callee idx position at table
168 var $old_pnt; // points made by caller and callee
169 var $old_asta_win; // the old caller idx position at table
170 var $old_friend; // the old callee idx position at table
172 function Bin5_table()
178 function create($idx)
180 if (($thiz =& new Bin5_table()) == FALSE)
186 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
187 $thiz->asta_pla_n= -1;
188 $thiz->asta_card = -1;
189 $thiz->asta_pnt = -1;
192 $thiz->points = array( );
194 $thiz->total = array( 0, 0, 0, 0, 0);
195 $thiz->asta_win = -1;
196 $thiz->briscola = -1;
200 $thiz->old_reason = "";
201 $thiz->old_asta_pnt = -1;
202 $thiz->old_mult = -1;
204 $thiz->old_asta_win = -1;
205 $thiz->old_friend = -1;
212 function myclone(&$from)
214 if (($thiz =& new Bin5_table()) == FALSE)
219 $thiz->card = $from->card;
220 $thiz->mazzo = $from->mazzo; // REVIEW
221 $thiz->gstart = $from->gstart;
222 $thiz->turn = $from->turn;
224 $thiz->asta_pla = $from->asta_pla;
225 $thiz->asta_pla_n = $from->asta_pla_n;
226 $thiz->asta_card = $from->asta_card;
227 $thiz->asta_pnt = $from->asta_pnt;
229 $thiz->mult = $from->mult;
230 $thiz->points = $from->points;
231 $thiz->points_n = $from->points_n;
232 $thiz->total = $from->total;
234 $thiz->asta_win = $from->asta_win;
235 $thiz->briscola = $from->briscola;
236 $thiz->friend = $from->friend;
238 $thiz->old_reason = $from->old_reason;
239 $thiz->old_asta_pnt = $from->old_asta_pnt;
240 $thiz->old_mult = $from->mult;
241 $thiz->old_pnt = $from->old_pnt;
242 $thiz->old_asta_win = $from->old_asta_win;
243 $thiz->old_friend = $from->old_friend;
249 function parentcopy(&$from)
254 function spawn(&$from)
256 if (($thiz =& new Bin5_table()) == FALSE)
259 $thiz->parentcopy($from);
261 log_main("PLAYER_N - spawn.".$thiz->player_n);
263 $thiz->card = array();
264 $thiz->bunch_create();
265 $thiz->mazzo = rand(0,PLAYERS_N-1);
268 $thiz->old_asta_win = -1;
269 $thiz->old_reason = "";
271 // players are rearranged in an dedicated array
272 $thiz->player = array();
273 for ($i = 0 ; $i < $from->player_n ; $i++)
274 $thiz->player[$i] = $i;
276 log_main("TABLE_OLD_WIN - spawn:".$thiz->old_asta_win);
281 function asta2mult($asta_pnt)
285 else if ($asta_pnt > 100)
287 else if ($asta_pnt > 90)
289 else if ($asta_pnt > 80)
291 else if ($asta_pnt > 70)
297 function multer($is_new)
300 return (pow(2, $this->mult) * $this->asta2mult($this->asta_pnt));
303 return (pow(2, $this->old_mult) * $this->asta2mult($this->old_asta_pnt));
308 // function bunch_create_old() function AND
312 // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
313 // // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
314 // $ret[$i] =& new Card($i, 'bunch', 'no_owner');
321 function bunch_create()
325 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
326 $this->card[$i] = new Card($i, 'bunch', 'no_owner');
330 function bunch_make()
332 log_main("bunch_make start");
333 $ct = array(0,0,0,0,0);
335 mt_srand(make_seed());
337 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--)
340 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--) {
344 log_main("RND ZERO");
348 $owner = $i % BIN5_PLAYERS_N;
349 $this->card[$id]->assign('hand', $owner);
351 $rest[$rn] = $rest[$i];
352 // $pubbpos[$rn2] = $pubbpos[$i];
354 log_main("bunch_make end");
357 function init($userarr)
360 $this->mazzo = rand(0,PLAYERS_N-1);
363 $this->old_asta_win = -1;
364 $this->old_reason = "";
366 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
367 $this->total[$i] = 0;
368 $user_cur = $userarr[$this->player[$i]];
369 $user_cur->exitislock = TRUE;
372 log_main("table::init: ci siamo");
375 function game_init($userarr)
379 $this->gstart = ($this->mazzo+1) % BIN5_PLAYERS_N;
382 $this->asta_pla_n = BIN5_PLAYERS_N;
383 $this->asta_card = -1;
384 $this->asta_pnt = 60;
385 $this->asta_win = -1;
386 $this->briscola = -1;
390 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
391 $this->asta_pla[$i] = TRUE;
392 $user_cur = $userarr[$this->player[$i]];
393 $user_cur->subst = 'asta';
394 $user_cur->asta_card = -2;
395 $user_cur->asta_pnt = -1;
396 $user_cur->handpt = $this->hand_points($i);
397 // SEE function calculate_points(&$table)
402 function game_next($delta)
404 $this->old_mazzo = $this->mazzo;
405 $this->mazzo = ($this->mazzo + $delta) % BIN5_PLAYERS_N;
408 function mult_inc($val)
410 $this->old_mult = $this->mult;
414 function mult_set($val)
416 $this->old_mult = $this->mult;
420 function hand_points($idx)
422 GLOBAL $G_all_points;
426 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
427 // for ($i = 0 ; $i < 40 ; $i++) {
428 if ($this->card[$i]->owner != $idx)
431 $ctt = $this->card[$i]->value % 10;
432 $tot += $G_all_points[$ctt];
439 function exitlock_show($userarr, $table_pos)
441 $ct = $this->exitlock_calc($userarr, $table_pos);
443 $ret = sprintf('exitlock_show(%d, %s);', $ct,
444 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
448 function exitlock_calc(&$userarr, $table_pos)
452 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
453 if ($userarr[$this->player[$i]]->exitislock == FALSE)
460 function rules_engine($bri, $curtime, $action, $user)
462 GLOBAL $G_all_points, $G_dbasetype;
466 if ($action == BIN5_RULES_ALLPASS) { // return TRUE if all correct
467 $this->old_reason = "Hanno passato tutti.";
468 $this->old_asta_win = -1;
471 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
476 // $this->game_next(1);
477 $this->game_init(&$bri->user);
479 else if ($action == BIN5_RULES_ABANDON) { // return TRUE if all correct
480 log_wr(sprintf("GIOCO FINITO !!!"));
482 $this->old_reason = sprintf("Ha lasciato %s perché aveva al massimo 2 punti.", xcape($user->name));
483 $this->old_asta_win = $user->table_pos;
487 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
491 // Non si cambia mazzo se si abbandona la partita
493 // $this->game_next(0);
494 $this->game_init(&$bri->user);
496 else if ($action == BIN5_RULES_FINISH) { // return TRUE if all correct
500 if ($this->asta_pnt == 60)
501 $this->asta_pnt = 61;
503 $this->old_reason = "";
505 // count points for the temporary 2 teams
506 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
507 $ctt = $this->card[$i]->value % 10;
508 $own = $this->card[$i]->owner;
509 if ($own == $this->asta_win || $own == $this->friend)
510 $pro += $G_all_points[$ctt];
513 log_wr(sprintf("PRO: [%d]", $pro));
516 if ($this->asta_pnt == 61 && $pro == 60) {
517 $this->points[$this->points_n % MAX_POINTS] = array();
518 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
519 $this->points[$this->points_n % MAX_POINTS][$i] = 0;
523 $this->old_pnt = $pro;
530 if ($pro >= $this->asta_pnt)
535 $this->points[$this->points_n % MAX_POINTS] = array();
536 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
537 if ($i == $this->asta_win)
538 $pt = ($i == $this->friend ? 4 : 2);
539 else if ($i == $this->friend)
544 log_wr(sprintf("PRO: pt[%d][%d] = %d", $this->points_n % MAX_POINTS, $i, $pt));
546 $pt = $pt * $sig * $this->multer(TRUE) * ($pro == 120 ? 2 : 1);
548 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $this->multer(TRUE), ($pro == 120 ? 2 : 1)));
550 $this->points[$this->points_n % MAX_POINTS][$i] = $pt;
551 $this->total[$i] += $pt;
555 $this->old_pnt = $pro;
556 $this->old_asta_win = $this->asta_win;
566 $this->game_next($game_delta);
568 $plist = "$this->table_token|$user->table_orig|$this->player_n";
571 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
572 $user_cur = &$bri->user[$this->player[$i]];
575 $ucodes[$i] = $user_cur->code_get();
578 $plist .= '|'.xcapelt($user_cur->name).'|'.$pts[$i];
579 $codes .= '|'.xcapelt($user_cur->code_get());
582 log_legal($curtime, $user->ip, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
584 $this->old_asta_pnt = $this->asta_pnt;
585 // $this->old_mazzo is managed by ->game_next();
586 // $this->old_mult, $this->old_pnt, $this->old_reason and $this->old_asta_win are specific
588 $this->old_friend = $this->friend;
590 if ($user->table_orig < TABLES_AUTH_N) {
591 require_once("../Obj/dbase_".$G_dbasetype.".phh");
593 if (($bdb = BriskDB::create()) != FALSE) {
594 $bdb->bin5_points_save($curtime, $this, $user->table_orig, $ucodes, $pts);
598 log_points($remote_addr, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED");
600 log_points($remote_addr, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
603 $this->game_init(&$bri->user);
608 } // end class Bin5_table
613 define('BIN5_USER_FLAG_RING_ENDAUCT', 0x01);
615 class Bin5_user extends User {
618 var $handpt; // Total card points at the beginning of the current hand.
619 var $exitislock; // Player can exit from the table ?
620 var $privflags; // Flags for briskin5 only
628 function create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
629 if (($thiz =& new User()) == FALSE)
632 $thiz->asta_card = -2;
633 $thiz->asta_pnt = -1;
635 $thiz->exitislock = TRUE;
636 $thiz->privflags = 0;
642 function parentcopy(&$from)
647 function copy(&$from)
649 $this->parentcopy($from);
651 $this->asta_card = $from->asta_card;
652 $this->asta_pnt = $from->asta_pnt;
653 $this->handpt = $from->handpt;
654 $this->exitislock = $from->exitislock;
655 $this->privflags = $from->privflags;
659 function myclone(&$from)
661 if (($thiz =& new User()) == FALSE)
670 static function spawn($from, &$bri, $table, $table_pos, $get, $post, $cookie)
672 if (($thiz = new Bin5_user()) == FALSE)
675 if (($CO_bin5_pref_ring_endauct = gpcs_var("CO_bin5_pref_ring_endauct", $get, $post, $cookie)) === FALSE) {
676 $CO_bin5_pref_ring_endauct = "";
679 $thiz->parentcopy($from);
681 /* NOTE: at this moment idx and table_pos fields have the same value
682 but diffentent functions, we keep them separated for a while */
684 $thiz->idx = $table_pos;
685 $thiz->asta_card = -2;
686 $thiz->asta_pnt = -1;
688 $thiz->exitislock = TRUE;
690 log_wr("Bin5 constructor");
692 $thiz->privflags = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
694 $thiz->table_orig = $table;
696 $thiz->table_pos = $table_pos;
703 function step_set($step)
705 $this->step = $step & 0x7fffffff;
710 function step_inc($delta = 1) {
711 $this->step += $delta;
712 /* modularization because unpack() not manage unsigned 32bit int correctly */
713 $this->step &= 0x7fffffff;
718 static function load_step($tab_id, $sess)
722 if (validate_sess($sess) == FALSE)
725 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
726 mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
727 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step", 'rb')) == FALSE)
729 if (($s = fread($fp, 8)) == FALSE)
731 if (mb_strlen($s, "ASCII") != 8)
733 $arr = unpack('Ls/Li', $s);
736 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
743 log_rd2("STEP_GET [".$sess."]: return false ");
751 if (validate_sess($this->sess) == FALSE)
753 if (file_exists(BIN5_PROXY_PATH."/table".$this->table_orig) == FALSE)
754 mkdir(BIN5_PROXY_PATH."/table".$this->table_orig, 0775, TRUE);
755 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$this->table_orig."/".$this->sess.".step", 'w')) == FALSE)
757 fwrite($fp, pack("LL",$this->step, $this->idx));
760 log_main("step_set [".$this->sess. "] [".$this->step."]");
768 static function unproxy_step($tab_id, $sess)
770 log_rd2("UNPROXY: ".BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
771 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
774 @unlink(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
777 function destroy_data($tab_id)
780 if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos, "B")) == -1) {
781 log_crit("BIN5 USER DATA REMOVE FAILED 1 [".FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos."]");
785 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
786 log_crit("BIN5 USER DATA REMOVE FAILED 2");
789 if (shmop_delete($shm) == 0) {
790 log_crit("BIN5 USER DATA REMOVE FAILED 3");
795 log_main("BIN5 USER DATA DESTROY SUCCESS");
797 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
807 static function blocking_error($is_unrecoverable)
809 log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
810 return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
813 protected function page_sync($sess, $page)
815 log_rd2("PAGE_SYNC");
816 printf("xXx BIN5_USER::PAGE_SYNC\n");
817 return (sprintf('xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
820 protected function maincheck($cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate, $table_idx, $table_token)
822 GLOBAL $G_lang, $mlang_indrd;
823 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
824 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
825 $CO_splashdate = "CO_splashdate".$G_splash_idx;
826 $$CO_splashdate = $splashdate;
830 log_rd("maincheck begin");
835 /* Nothing changed, return. */
836 if ($cur_step == $this->step)
839 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$this->stat."] cur_step[".$cur_step."] user_step[".$this->step."]");
841 if ($cur_step == -1) {
843 * if $cur_step == -1 load the current state from the main struct
846 $S_load_stat['wR_minusone']++;
848 // if ($this->the_end == TRUE) {
849 // log_rd2("main_check: the end".var_export(debug_backtrace()));
852 if ($this->trans_step != -1) {
853 log_rd2("TRANS USATO ".$this->trans_step);
854 $cur_step = $this->trans_step;
855 $this->trans_step = -1;
858 log_rd2("TRANS NON ATTIVATO");
863 /* this part I suppose is read only on $this->room structure */
864 if ($cur_step == -1) {
865 log_rd2("PRE-NEWSTAT");
872 if ($this->stat == 'table') {
874 /* NOTE: $this->room is associated with the current $bri object */
875 printf("xXx CLASS NAME [%s]\n", get_class($this->room));
876 $ret = show_table(&$this->room, $this, $this->step, FALSE, FALSE);
878 log_rd2("NEWSTAT: ".$this->stat);
880 $new_stat = $this->stat;
881 $new_subst = $this->subst;
882 $new_step = $this->step;
883 } /* if ($cur_step == -1) { */
885 /* $sem = Room::lock_data(FALSE); */
886 $S_load_stat['rU_heavy']++;
888 if ($cur_step < $this->step) {
890 if ($cur_step + COMM_N < $this->step) {
891 if (($cur_stat != $this->stat)) {
892 $to_stat = $this->stat;
893 /* Room::unlock_data($sem); */
895 printf("xXx BIN5_USER::MAINCHECK\n");
896 return ($this->page_sync($this->sess, ($to_stat == "table" ? "index.php" : "../index.php"), $this->table, $this->table_token));
898 log_rd2("lost history, refresh from scratch");
899 printf("xXx LOST HISTORY!\n");
903 for ($i = $cur_step ; $i < $this->step ; $i++) {
905 log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
906 $ret .= $this->comm[$ii];
908 $new_stat = $this->stat;
909 $new_subst = $this->subst;
910 $new_step = $this->step;
913 log_rd2($this->step, 'index_rd.php: after ret set');
915 if ($this->the_end == TRUE) {
916 log_rd2("LOGOUT BYE BYE!!");
917 log_auth($this->sess, "Explicit logout.");
919 if ($this->the_end == TRUE) {
923 /* if ($this->subst == 'sitdown') { */
924 /* log_load("ROOM WAKEUP"); */
925 /* $this->room->room_wakeup($this); */
927 /* else if ($this->subst == 'standup') */
928 /* $this->room->room_outstandup($this); */
930 /* log_rd2("LOGOUT FROM WHAT ???"); */
932 } /* if ($this->the_end == TRUE) { ... */
933 } /* if ($this->the_end == TRUE) { ... */
934 } /* if ($cur_step < $this->step) { */
936 /* Room::unlock_data($sem); */
937 } /* else of if ($cur_step == -1) { */
941 } // function maincheck (...
948 } // end class Bin5_user
953 static $delta_t = array();
957 var $comm; // commands for many people
958 var $step; // current step of the comm array
959 var $garbage_timeout;
970 function Bin5 ($room, $table_idx, $table_token, $get, $post, $cookie) {
971 $this->user = array();
972 $this->table = array();
974 $this->the_end = FALSE;
975 $this->shm_sz = BIN5_SHM_MIN;
976 if (($this->tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
982 $table = $room->table[$table_idx];
984 log_wr("Bin5 constructor");
986 for ($i = 0 ; $i < $table->player_n ; $i++) {
987 $user[$table->player[$i]]->table_token = $table_token;
988 $this->user[$i] = Bin5_user::spawn($user[$table->player[$i]], $this, $table_idx, $i, $get, $post, $cookie);
990 $this->table[0] = Bin5_table::spawn(&$table);
992 log_main("TABLE_OLD_WIN - Bin5:".$this->table[0]->old_asta_win);
994 $this->table_idx = $table_idx;
995 $this->table_token = $table_token;
996 $this->garbage_timeout = 0;
998 $this->delay_mgr = new Delay_Manager((GARBAGE_TIMEOUT *3.0) / 2.0);
1000 log_wr("Bin5 constructor end");
1004 function get_user($sess, &$idx)
1008 if (validate_sess($sess)) {
1009 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1010 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1013 $ret = &$this->user[$i];
1017 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1018 // for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++)
1019 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1022 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1029 function garbage_manager($force)
1033 /* Garbage collector degli utenti in timeout */
1037 $delta = $this->delay_mgr->delta_get($curtime);
1039 if ($force || $this->garbage_timeout < $curtime) {
1040 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1041 $user_cur = $this->user[$i];
1042 if ($user_cur->sess == "" ||
1043 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
1046 if ($user_cur->lacc + EXPIRE_TIME_RD < ($curtime - $delta)) { // Auto logout dell'utente
1047 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
1049 if ($user_cur->stat == 'table') {
1050 log_auth($user_cur->sess," bin5 Autologout session.");
1052 /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE
1054 $tmp_sess = $user_cur->sess;
1055 $user_cur->sess = "";
1056 Bin5_user::step_unproxy($tmp_sess);
1057 $user_cur->name = "";
1058 $user_cur->the_end = FALSE;
1062 /* se gli altri utenti non erano d'accordo questo utente viene bannato */
1063 $remcalc = $this->table[0]->exitlock_calc(&$this->user, $user_cur->table_pos);
1065 require_once("${G_base}Obj/hardban.phh");
1066 Hardbans::add(($user_cur->flags & USER_FLAG_AUTH ? $user_cur->name : FALSE),
1067 $user_cur->ip, $user_cur->sess, $user_cur->laccwr + BAN_TIME);
1069 // $user->bantime = $user->laccwr + BAN_TIME;
1071 $this->table_wakeup($user_cur);
1075 log_rd2($user_cur->sess." GARBAGE UPDATED!");
1077 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
1082 $this->delay_mgr->lastcheck_set($curtime);
1087 function destroy_data()
1093 log_main("DESTROY BRISKIN5 DATA");
1096 log_main("DESTROY2 BRISKIN5 DATA [".$this->table_idx."]");
1097 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1098 $this->user[$i]->destroy_data($this->table_idx);
1099 Bin5_user::unproxy_step($this->table_idx, $this->user[$i]->sess);
1101 if (($tok = @ftok(FTOK_PATH."/bin5/table".$this->table_idx."/table", "B")) == -1)
1104 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
1107 if (shmop_delete($shm) == 0) {
1108 log_only("REMOVE FALLITA ");
1113 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
1115 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
1125 static function lock_data($is_exclusive, $table_idx)
1127 if (($res = file_lock(FTOK_PATH."/bin5/table".$table_idx."/table", $is_exclusive)) != FALSE) {
1128 self::$delta_t = microtime(TRUE);
1129 log_lock("LOCK table [".$table_idx."] [".self::$delta_t[$table_idx]."]");
1131 return (new Vect(array('res' => $res, 'tab' => $table_idx)));
1138 static function unlock_data($res_vect)
1142 $res = $res_vect->getbyid('res');
1143 $tab = $res_vect->getbyid('tab');
1145 log_lock("UNLOCK table [".$tab."] [".(microtime(TRUE) - (self::$delta_t[$tab]))."]");
1151 function chatt_send($user, $mesg)
1153 GLOBAL $mlang_brisk, $G_lang;
1155 if ($user->stat == 'table') {
1156 $table = &$this->table[$user->table];
1159 $user_mesg = substr($mesg,6);
1163 $dt = date("H:i ", $curtime);
1164 if (strncmp($user_mesg, "/nick ", 6) == 0) {
1165 log_main($user->sess." chatt_send BEGIN");
1167 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
1168 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1169 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
1174 $user_mesg = "COMMAND ".$user_mesg;
1177 // update local graph
1178 // update remote graphs
1179 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1180 $user_cur = $this->user[$i];
1181 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1182 if ($user_cur->sess == '')
1184 if (strcasecmp($user_cur->name, $name_new) == 0) {
1185 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1187 $premsg = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
1188 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $premsg);
1193 if ($i == BIN5_MAX_PLAYERS) {
1194 if ($user->flags & USER_FLAG_AUTH && strcasecmp($user->name,$name_new) != 0) {
1195 if ($this->table[$user->table]->auth_only == TRUE) {
1196 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1197 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>%s</b>");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
1202 $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
1206 $user->name = $name_new;
1208 log_main($user->sess." chatt_send start set");
1211 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1212 log_main($user->sess." chatt_send set loop");
1214 $user_cur = &$this->user[$i];
1215 if ($user_cur->sess == '')
1218 if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
1219 $table = &$this->table[$user->table];
1221 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1222 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1223 $this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->flags,
1224 xcape($this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->name),
1226 $this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->flags,
1227 xcape($this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->name),
1229 $this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->flags,
1230 xcape($this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->name),
1232 (BIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->flags),
1233 (BIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->name)),
1235 (BIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->flags),
1236 (BIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->name)));
1237 if ($user_cur == $user) {
1238 $user_cur->comm[$user_cur->step % COMM_N] .= $user_cur->myname_innerHTML();
1240 $user_cur->step_inc();
1246 for ($i = 0 ; $i < ($user->stat == 'room' ? BIN5_MAX_PLAYERS : BIN5_PLAYERS_N) ; $i++) {
1247 if ($user->stat == 'room') {
1248 $user_cur = &$this->user[$i];
1249 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1253 $user_cur = &$this->user[$table->player[$i]];
1256 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1257 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
1258 $dt, $user->flags, xcape($user->name), xcape($user_mesg));
1259 $user_cur->step_inc();
1261 log_legal($curtime, $user->ip, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
1265 function table_wakeup($user)
1267 $table = &$this->table[0];
1269 log_main("BIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
1273 log_main("BIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1275 for ($i = 0 ; $i < $table->player_n ; $i++) {
1276 $user_cur = &$this->user[$i];
1277 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1279 if ($user_cur == $user)
1280 $user_cur->subst = "shutdowner";
1282 $user_cur->subst = "shutdowned";
1283 $user_cur->laccwr = $curtime;
1285 $ret = "gst.st = ".($user_cur->step+1)."; ";
1286 $ret .= 'gst.st_loc++; xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
1288 log_wr($user_cur->sess." BIN5_WAKEUP: ".$ret);
1289 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1290 $user_cur->step_inc();
1293 $this->the_end = TRUE;
1297 * If all players are freezed the room garbage_manager clean up table and users.
1299 function is_abandoned()
1304 $table = &$this->table[0];
1306 for ($i = 0 ; $i < $table->player_n ; $i++) {
1307 $user_cur = &$this->user[$i];
1309 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
1318 static function request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
1320 GLOBAL $G_black_list;
1322 printf("NEW_SOCKET (root): %d\n", intval($new_socket));
1324 $enc = get_encoding($header);
1325 if (isset($header['User-Agent'])) {
1326 if (strstr($header['User-Agent'], "MSIE")) {
1327 $transp_type = "htmlfile";
1330 $transp_type = "xhr";
1334 $transp_type = "iframe";
1336 force_no_cache($header_out);
1338 if (($table_idx = gpcs_var('table_idx', $get, $post, $cookie)) === FALSE)
1341 if (($table_token = gpcs_var('table_token', $get, $post, $cookie)) === FALSE)
1342 unset($table_token);
1349 bin5_index_main($transp_type, $header_out, $addr, $get, $post, $cookie);
1350 $content = ob_get_contents();
1353 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1357 case "index_wr.php":
1358 // FIXME $content can be unset
1359 if (isset($table_idx) && isset($table_token)) {
1360 if (($bri = $s_a_p->app->match_get($table_idx, $table_token)) != FALSE) {
1362 bin5_index_wr_main($bri, $addr, $get, $post, $cookie);
1363 $content = ob_get_contents();
1367 $content = "Bin5 Load data error";
1371 $content = "Bin5 Load data error";
1373 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1377 case "index_rd_ifra.php":
1378 if (($transp = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
1380 if ($transp == 'websocket')
1384 if (!isset($table_idx)
1385 || !isset($table_token)
1386 || !isset($cookie['sess'])
1387 || ($bri = $s_a_p->app->match_get($table_idx, $table_token)) == NULL
1388 || (($user = $bri->get_user($cookie['sess'], $idx)) == FALSE)) {
1390 $content = Bin5_user::stream_fini($transp_type, $s_a_p->rndstr, TRUE);
1391 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1396 // close a previous opened index_read_ifra socket, if exists
1397 if (($prev = $user->rd_socket_get()) != NULL) {
1398 $s_a_p->socks_unset($user->rd_socket_get());
1399 fclose($user->rd_socket_get());
1400 printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
1401 $user->rd_socket_set(NULL);
1405 $user->stream_init($s_a_p->rndstr, $enc, $header, $header_out, $content, $get, $post, $cookie);
1407 $response = headers_render($header_out, -1).$user->chunked_content($content);
1408 $response_l = mb_strlen($response, "ASCII");
1410 $wret = @fwrite($new_socket, $response, $response_l);
1411 if ($wret < $response_l) {
1412 printf("TROUBLES WITH FWRITE: %d\n", $wret);
1413 $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
1416 $user->rd_cache_set("");
1418 fflush($new_socket);
1421 $s_a_p->socks_set($new_socket, $user, NULL);
1422 $user->rd_socket_set($new_socket);
1423 printf(" - qui ci siamo - ");
1442 function locshm_exists($tok)
1446 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
1447 log_main($tok." SHM NOT exists");
1453 log_main($tok." SHM exists");
1461 // if success an array with points of each player is returned
1462 function calculate_points_old(&$table)
1464 GLOBAL $G_all_points;
1469 if ($table->asta_pnt == 60)
1470 $table->asta_pnt = 61;
1472 $table->old_reason = "";
1473 $table->old_asta_win = $table->asta_win;
1474 $table->old_friend = $table->friend;
1475 $table->old_asta_pnt = $table->asta_pnt;
1476 $table->old_mult = $table->mult;
1478 // count points for the temporary 2 teams
1479 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1480 // for ($i = 0 ; $i < 40 ; $i++) {
1481 $ctt = $table->card[$i]->value % 10;
1482 $own = $table->card[$i]->owner;
1483 if ($own == $table->asta_win || $own == $table->friend)
1484 $pro += $G_all_points[$ctt];
1487 log_wr(sprintf("PRO: [%d]", $pro));
1490 if ($table->asta_pnt == 61 && $pro == 60) {
1491 $table->points[$table->points_n % MAX_POINTS] = array();
1492 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1493 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1497 $table->old_pnt = $pro;
1503 if ($pro >= $table->asta_pnt)
1508 $table->points[$table->points_n % MAX_POINTS] = array();
1509 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1510 if ($i == $table->asta_win)
1511 $pt = ($i == $table->friend ? 4 : 2);
1512 else if ($i == $table->friend)
1519 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1521 $pt = $pt * $sig * $table->multer(TRUE) * ($pro == 120 ? 2 : 1);
1523 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->multer(TRUE), ($pro == 120 ? 2 : 1)));
1525 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1526 $table->total[$i] += $pt;
1530 $table->old_pnt = $pro;
1538 is_transition (is from room to table ?)
1539 is_again (is another game)
1541 Examples of $is_transition, $is_again:
1542 from reload of the page: FALSE, FALSE
1543 from sitdown in room: TRUE, FALSE
1544 from table: asta cmd e tutti passano: TRUE, TRUE
1545 from table: fine partita: TRUE, TRUE
1547 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1549 $table_idx = $user->table;
1550 $table = $bri->table[$table_idx];
1551 $table_pos = $user->table_pos;
1553 $ret = "table_init();";
1554 $ret .= $table->exitlock_show(&$bri->user, $table_pos);
1556 /* GENERAL STATUS */
1557 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1558 $sendstep, $user->stat, $user->subst, $table_pos);
1560 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1563 $ret .= "background_set();";
1566 $ret .= $user->myname_innerHTML();
1567 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1568 $bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->flags,
1569 xcape($bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->name),
1571 $bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->flags,
1572 xcape($bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->name),
1574 $bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->flags,
1575 xcape($bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->name),
1577 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->flags),
1578 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->name)),
1580 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->flags),
1581 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->name)));
1583 /* NOTIFY FOR THE CARD MAKER */
1584 if ($is_transition) { // && $user->subst == "asta" superfluo
1585 $ret .= show_table_info(&$bri, &$table, $table_pos);
1586 $ret .= "setTimeout(preload_images, 500, g_preload_img_arr, g_imgct);";
1589 $ret .= table_welcome($user);
1591 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1592 $ret .= playsound("cow.mp3");
1597 if ($is_transition) { // && $user->subst == "asta" superfluo
1600 for ($i = 0 ; $i < BIN5_CARD_HAND ; $i++) {
1601 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++) {
1603 for ($o = 0 ; $o < (BIN5_CARD_HAND * BIN5_PLAYERS_N) && $ct < $i+1 ; $o++) {
1604 // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1605 if ($table->card[$o]->owner == (($e + $table->gstart) % BIN5_PLAYERS_N)) {
1611 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1613 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BIN5_PLAYERS_N,
1614 $i, ((($e + BIN5_PLAYERS_N - $table_pos + $table->gstart) % BIN5_PLAYERS_N) == 0 ?
1615 $table->card[$o]->value : -1),
1616 ($i == 7 && $e == (BIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1621 $taked = array(0,0,0,0,0);
1622 $inhand = array(0,0,0,0,0);
1623 $ontabl = array(-1,-1,-1,-1,-1);
1626 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1627 // for ($i = 0 ; $i < 40 ; $i++) {
1628 if ($table->card[$i]->stat == 'hand') {
1629 if ($table->card[$i]->owner == $table_pos) {
1630 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1632 $inhand[$table->card[$i]->owner]++;
1634 else if ($table->card[$i]->stat == 'take') {
1635 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1636 $taked[$table->card[$i]->owner]++;
1638 else if ($table->card[$i]->stat == 'table') {
1639 $ontabl[$table->card[$i]->owner] = $i;
1643 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1644 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1646 log_main("Stat table: ".$logg);
1648 /* Set ours cards. */
1650 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1651 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1652 for ($i = $inhand[$table_pos] ; $i < BIN5_CARD_HAND ; $i++)
1653 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1654 $ret .= sprintf('card_setours(%s);', $oursarg);
1656 /* Dispose all cards */
1657 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1658 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1659 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1660 ($inhand[$i] <= BIN5_CARD_HAND ? $inhand[$i] : BIN5_CARD_HAND), $taked[$i]);
1662 if ($ontabl[$i] != -1) {
1663 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1664 $table->card[$ontabl[$i]]->value,
1665 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1671 if ($user->subst == 'asta') {
1673 /* show users auction status */
1675 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1676 $user_cur = &$bri->user[$table->player[$i]];
1677 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1678 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1680 if (BIN5_PLAYERS_N == 3)
1681 $showst .= ",-2,-2";
1682 $ret .= sprintf('document.title = "Brisk - Tavolo %d (asta)";', $user->table_orig);
1683 $ret .= sprintf('show_astat(%s);', $showst);
1685 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1686 /* show card chooser */
1687 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1692 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N) &&
1693 $table->asta_win == -1)
1694 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1695 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1697 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1698 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1702 if ($table->asta_win == -1) { // auction case
1703 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N))
1704 $ret .= "remark_on();";
1706 $ret .= "remark_off();";
1708 else { // chooseed case
1709 if ($table_pos == $table->asta_win)
1710 $ret .= "remark_on();";
1712 $ret .= "remark_off();";
1715 else if ($user->subst == 'game') {
1717 if (($table->gstart + $table->turn) % BIN5_PLAYERS_N == $table_pos)
1718 $ret .= "is_my_time = true; remark_on();";
1720 $ret .= "remark_off();";
1722 /* WHO CALL AND WHAT */
1723 $ret .= briscola_show($bri, $table, $user);
1727 } // end function show_table(...
1729 function calculate_winner(&$table)
1736 $cur_seed = $table->briscola - ($table->briscola % 10);
1738 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1739 // for ($i = 0 ; $i < 40 ; $i++) {
1740 if ($table->card[$i]->stat != "table")
1743 log_wr(sprintf("Card On table: [%d]", $i));
1745 $v = $table->card[$i]->value;
1746 $ontab[$table->card[$i]->owner] = $v;
1747 $ontid[$table->card[$i]->owner] = $i;
1748 /* se briscola setto il flag */
1749 if (($v - ($v % 10)) == $cur_seed)
1753 if ($briontab == FALSE) {
1754 $cur_win = $table->gstart;
1755 $cur_val = $ontab[$cur_win];
1756 $cur_seed = $cur_val - ($cur_val % 10);
1759 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1760 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1761 if ($ontab[$i] < $cur_val) {
1762 $cur_val = $ontab[$i];
1768 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1769 $table->card[$ontid[$i]]->owner = $cur_win;
1770 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1775 function show_table_info(&$bri, &$table, $table_pos)
1777 GLOBAL $G_lang, $mlang_bin5_bin5;
1780 $user = $bri->user[$table->player[$table_pos]];
1782 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1783 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1786 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++)
1787 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1788 $noty .= sprintf("</tr>");
1791 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1793 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1794 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1795 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1796 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1801 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1802 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1803 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1804 $noty .= "</tr></table>";
1806 if ($table->old_reason != "") {
1807 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1810 if ($table->old_asta_win != -1) {
1811 log_main("TABLE_OLD_WIN:".$table->old_asta_win);
1812 $win = $table->player[$table->old_asta_win];
1813 log_main("TABLE_OLD_FRIEND:".$table->old_friend);
1814 $fri = $table->player[$table->old_friend];
1816 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1819 /* 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" */
1821 $noty .= sprintf($mlang_bin5_bin5['info_part'][$G_lang],
1822 xcape($bri->user[$win]->name),
1823 xcape($bri->user[$fri]->name));
1824 if ($table->old_pnt == 120) {
1825 $noty .= sprintf($mlang_bin5_bin5['info_capp'][$G_lang]);
1828 $noty .= sprintf($mlang_bin5_bin5['info_pnt'][$G_lang],
1829 ($table->old_asta_pnt > 61 ? $mlang_bin5_bin5['info_alea'][$G_lang].$table->old_asta_pnt :
1830 $mlang_bin5_bin5['info_more'][$G_lang]), $table->old_pnt,
1831 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] : ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] : $mlang_bin5_bin5['info_lost'][$G_lang])));
1835 $noty .= sprintf($mlang_bin5_bin5['info_alon'][$G_lang],
1836 xcape($bri->user[$win]->name));
1837 if ($table->old_pnt == 120) {
1838 $noty .= sprintf($mlang_bin5_bin5['info_acap'][$G_lang]);
1841 $noty .= sprintf($mlang_bin5_bin5['info_apnt'][$G_lang],
1843 ($table->old_asta_pnt > 61 ? $mlang_bin5_bin5['info_alea'][$G_lang].$table->old_asta_pnt :
1844 $mlang_bin5_bin5['info_more'][$G_lang]), $table->old_pnt,
1846 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] : ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] : $mlang_bin5_bin5['info_lost'][$G_lang])));
1849 $old_multer = $table->multer(FALSE);
1850 if ($old_multer > 1) {
1851 $noty .= sprintf($mlang_bin5_bin5['info_omul'][$G_lang], multoval($old_multer));
1853 $noty .= "<hr><br>";
1855 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1856 if ($table->mazzo == $table_pos)
1857 $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
1859 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1860 $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
1863 if ($user->subst == 'asta') {
1864 if ($table->asta_win == -1) // auction case
1865 $curplayer = $table->gstart % BIN5_PLAYERS_N;
1867 $curplayer = $table->asta_win;
1869 else if ($user->subst == 'game') {
1870 $curplayer = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
1873 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1874 if ($curplayer == $table_pos) {
1875 $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
1878 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1879 $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
1882 $multer = $table->multer(TRUE);
1884 $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($multer) );
1886 $noty .= "<hr><br>";
1887 $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
1888 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
1889 * 130 + ($table->points_n > 0 ? 50 : 0) +
1890 * (120 * ($table->points_n / MAX_POINTS)));
1891 * will be used when we refact notify js function following
1892 * photoo class logic
1898 function table_welcome($user)
1900 GLOBAL $table_wellarr, $G_lang;
1903 for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
1904 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
1910 function briscola_show($bri, $table, $user)
1912 GLOBAL $G_lang, $mlang_bin5_bin5;
1916 if ($table->asta_card == 9)
1917 $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
1919 /* text of caller cell */
1920 if ($user->table_pos == $table->asta_win) {
1921 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
1922 $ret .= sprintf($prestr, $ptnadd);
1925 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
1926 $ret .= sprintf($prestr,
1927 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1929 $ret .= sprintf('set_iscalling(%d);', ($table->asta_win - $user->table_pos + BIN5_PLAYERS_N) % BIN5_PLAYERS_N);
1931 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1933 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1934 $ret .= sprintf('$("caller").style.visibility = "visible";');
1935 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1936 $ret .= sprintf('$("astalascio").style.visibility = "";');
1937 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1938 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1944 function game_result($asta_pnt, $pnt)
1946 if ($asta_pnt == 61) {
1949 else if ($pnt == 60)
1955 if ($pnt >= $asta_pnt)
1962 function log_points($remote_addr, $curtime, $user, $where, $mesg)
1964 if (($fp = @fopen(LEGAL_PATH."/points.log", 'a')) != FALSE) {
1965 /* Unix time | session | nickname | IP | where was | mesg */
1966 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
1967 ($user->flags & USER_FLAG_AUTH ? 'A' : 'N'),
1968 $user->name, $remote_addr, $where , $mesg));