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;
409 function hand_points($idx)
411 GLOBAL $G_all_points;
415 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
416 // for ($i = 0 ; $i < 40 ; $i++) {
417 if ($this->card[$i]->owner != $idx)
420 $ctt = $this->card[$i]->value % 10;
421 $tot += $G_all_points[$ctt];
428 function exitlock_show($userarr, $table_pos)
430 $ct = $this->exitlock_calc($userarr, $table_pos);
432 $ret = sprintf('exitlock_show(%d, %s);', $ct,
433 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
437 function exitlock_calc(&$userarr, $table_pos)
441 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
442 if ($userarr[$this->player[$i]]->exitislock == FALSE)
449 function rules_engine($bri, $action, $user)
451 GLOBAL $G_all_points;
455 if ($action == BIN5_RULES_ALLPASS) { // return TRUE if all correct
456 $this->old_reason = "Hanno passato tutti.";
459 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
464 // $this->game_next(1);
465 $this->game_init(&$bri->user);
467 else if ($action == BIN5_RULES_ABANDON) { // return TRUE if all correct
468 log_wr(sprintf("GIOCO FINITO !!!"));
470 $this->old_reason = sprintf("Ha lasciato %s perché aveva al massimo 2 punti.", xcape($user->name));
474 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
478 // Non si cambia mazzo se si abbandona la partita
480 // $this->game_next(0);
481 $this->game_init(&$bri->user);
483 else if ($action == BIN5_RULES_FINISH) { // return TRUE if all correct
487 if ($this->asta_pnt == 60)
488 $this->asta_pnt = 61;
490 $this->old_reason = "";
492 // count points for the temporary 2 teams
493 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
494 $ctt = $this->card[$i]->value % 10;
495 $own = $this->card[$i]->owner;
496 if ($own == $this->asta_win || $own == $this->friend)
497 $pro += $G_all_points[$ctt];
500 log_wr(sprintf("PRO: [%d]", $pro));
503 if ($this->asta_pnt == 61 && $pro == 60) {
504 $this->points[$this->points_n % MAX_POINTS] = array();
505 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
506 $this->points[$this->points_n % MAX_POINTS][$i] = 0;
510 $this->old_pnt = $pro;
517 if ($pro >= $this->asta_pnt)
522 $this->points[$this->points_n % MAX_POINTS] = array();
523 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
524 if ($i == $this->asta_win)
525 $pt = ($i == $this->friend ? 4 : 2);
526 else if ($i == $this->friend)
531 log_wr(sprintf("PRO: pt[%d][%d] = %d", $this->points_n % MAX_POINTS, $i, $pt));
533 $pt = $pt * $sig * $this->multer(TRUE) * ($pro == 120 ? 2 : 1);
535 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $this->multer(TRUE), ($pro == 120 ? 2 : 1)));
537 $this->points[$this->points_n % MAX_POINTS][$i] = $pt;
538 $this->total[$i] += $pt;
542 $this->old_pnt = $pro;
552 $this->game_next($game_delta);
554 $plist = "$this->table_token|$user->table_orig|$this->player_n";
557 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
558 $user_cur = &$bri->user[$this->player[$i]];
561 $ucodes[$i] = $user_cur->code_get();
564 $plist .= '|'.xcapelt($user_cur->name).'|'.$pts[$i];
565 $codes .= '|'.xcapelt($user_cur->code_get());
568 log_legal($curtime, $user->ip, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
570 $this->old_mazzo = $this->mazzo;
571 $this->old_asta_pnt = $this->asta_pnt;
572 $this->old_mult = $this->mult;
573 // $this->old_pnt and $this->old_reason are specific
574 $this->old_asta_win = $this->asta_win;
575 $this->old_friend = $this->friend;
577 if ($user->table_orig < TABLES_AUTH_N) {
578 require_once("../Obj/dbase_".$G_dbasetype.".phh");
580 if (($bdb = BriskDB::create()) != FALSE) {
581 $bdb->bin5_points_save($curtime, $this, $user->table_orig, $ucodes, $pts);
585 log_points($remote_addr, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED");
587 log_points($curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
590 $this->game_init(&$bri->user);
595 } // end class Bin5_table
600 define('BIN5_USER_FLAG_RING_ENDAUCT', 0x01);
602 class Bin5_user extends User {
605 var $handpt; // Total card points at the beginning of the current hand.
606 var $exitislock; // Player can exit from the table ?
607 var $privflags; // Flags for briskin5 only
615 function create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
616 if (($thiz =& new User()) == FALSE)
619 $thiz->asta_card = -2;
620 $thiz->asta_pnt = -1;
622 $thiz->exitislock = TRUE;
623 $thiz->privflags = 0;
629 function parentcopy(&$from)
634 function copy(&$from)
636 $this->parentcopy($from);
638 $this->asta_card = $from->asta_card;
639 $this->asta_pnt = $from->asta_pnt;
640 $this->handpt = $from->handpt;
641 $this->exitislock = $from->exitislock;
642 $this->privflags = $from->privflags;
646 function myclone(&$from)
648 if (($thiz =& new User()) == FALSE)
657 static function spawn($from, &$bri, $table, $table_pos, $get, $post, $cookie)
659 if (($thiz = new Bin5_user()) == FALSE)
662 if (($CO_bin5_pref_ring_endauct = gpcs_var("CO_bin5_pref_ring_endauct", $get, $post, $cookie)) === FALSE) {
663 $CO_bin5_pref_ring_endauct = "";
666 $thiz->parentcopy($from);
668 /* NOTE: at this moment idx and table_pos fields have the same value
669 but diffentent functions, we keep them separated for a while */
671 $thiz->idx = $table_pos;
672 $thiz->asta_card = -2;
673 $thiz->asta_pnt = -1;
675 $thiz->exitislock = TRUE;
677 log_wr("Bin5 constructor");
679 $thiz->privflags = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
681 $thiz->table_orig = $table;
683 $thiz->table_pos = $table_pos;
690 function step_set($step)
692 $this->step = $step & 0x7fffffff;
697 function step_inc($delta = 1) {
698 $this->step += $delta;
699 /* modularization because unpack() not manage unsigned 32bit int correctly */
700 $this->step &= 0x7fffffff;
705 static function load_step($tab_id, $sess)
709 if (validate_sess($sess) == FALSE)
712 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
713 mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
714 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step", 'rb')) == FALSE)
716 if (($s = fread($fp, 8)) == FALSE)
718 if (mb_strlen($s, "ASCII") != 8)
720 $arr = unpack('Ls/Li', $s);
723 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
730 log_rd2("STEP_GET [".$sess."]: return false ");
738 if (validate_sess($this->sess) == FALSE)
740 if (file_exists(BIN5_PROXY_PATH."/table".$this->table_orig) == FALSE)
741 mkdir(BIN5_PROXY_PATH."/table".$this->table_orig, 0775, TRUE);
742 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$this->table_orig."/".$this->sess.".step", 'w')) == FALSE)
744 fwrite($fp, pack("LL",$this->step, $this->idx));
747 log_main("step_set [".$this->sess. "] [".$this->step."]");
755 static function unproxy_step($tab_id, $sess)
757 log_rd2("UNPROXY: ".BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
758 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
761 @unlink(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
764 function destroy_data($tab_id)
767 if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos, "B")) == -1) {
768 log_crit("BIN5 USER DATA REMOVE FAILED 1 [".FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos."]");
772 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
773 log_crit("BIN5 USER DATA REMOVE FAILED 2");
776 if (shmop_delete($shm) == 0) {
777 log_crit("BIN5 USER DATA REMOVE FAILED 3");
782 log_main("BIN5 USER DATA DESTROY SUCCESS");
784 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
794 static function blocking_error($is_unrecoverable)
796 log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
797 return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
800 protected function page_sync($sess, $page)
802 log_rd2("PAGE_SYNC");
803 printf("xXx BIN5_USER::PAGE_SYNC\n");
804 return (sprintf('xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
807 protected function maincheck($cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate, $table_idx, $table_token)
809 GLOBAL $G_lang, $mlang_indrd;
810 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
811 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
812 $CO_splashdate = "CO_splashdate".$G_splash_idx;
813 $$CO_splashdate = $splashdate;
817 log_rd("maincheck begin");
822 /* Nothing changed, return. */
823 if ($cur_step == $this->step)
826 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$this->stat."] cur_step[".$cur_step."] user_step[".$this->step."]");
828 if ($cur_step == -1) {
830 * if $cur_step == -1 load the current state from the main struct
833 $S_load_stat['wR_minusone']++;
835 // if ($this->the_end == TRUE) {
836 // log_rd2("main_check: the end".var_export(debug_backtrace()));
839 if ($this->trans_step != -1) {
840 log_rd2("TRANS USATO ".$this->trans_step);
841 $cur_step = $this->trans_step;
842 $this->trans_step = -1;
845 log_rd2("TRANS NON ATTIVATO");
850 /* this part I suppose is read only on $this->room structure */
851 if ($cur_step == -1) {
852 log_rd2("PRE-NEWSTAT");
859 if ($this->stat == 'table') {
861 /* NOTE: $this->room is associated with the current $bri object */
862 printf("xXx CLASS NAME [%s]\n", get_class($this->room));
863 $ret = show_table(&$this->room, $this, $this->step, FALSE, FALSE);
865 log_rd2("NEWSTAT: ".$this->stat);
867 $new_stat = $this->stat;
868 $new_subst = $this->subst;
869 $new_step = $this->step;
870 } /* if ($cur_step == -1) { */
872 /* $sem = Room::lock_data(FALSE); */
873 $S_load_stat['rU_heavy']++;
875 if ($cur_step < $this->step) {
877 if ($cur_step + COMM_N < $this->step) {
878 if (($cur_stat != $this->stat)) {
879 $to_stat = $this->stat;
880 /* Room::unlock_data($sem); */
882 printf("xXx BIN5_USER::MAINCHECK\n");
883 return ($this->page_sync($this->sess, ($to_stat == "table" ? "index.php" : "../index.php"), $this->table, $this->table_token));
885 log_rd2("lost history, refresh from scratch");
886 printf("xXx LOST HISTORY!\n");
890 for ($i = $cur_step ; $i < $this->step ; $i++) {
892 log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
893 $ret .= $this->comm[$ii];
895 $new_stat = $this->stat;
896 $new_subst = $this->subst;
897 $new_step = $this->step;
900 log_rd2($this->step, 'index_rd.php: after ret set');
902 if ($this->the_end == TRUE) {
903 log_rd2("LOGOUT BYE BYE!!");
904 log_auth($this->sess, "Explicit logout.");
906 if ($this->the_end == TRUE) {
910 /* if ($this->subst == 'sitdown') { */
911 /* log_load("ROOM WAKEUP"); */
912 /* $this->room->room_wakeup($this); */
914 /* else if ($this->subst == 'standup') */
915 /* $this->room->room_outstandup($this); */
917 /* log_rd2("LOGOUT FROM WHAT ???"); */
919 } /* if ($this->the_end == TRUE) { ... */
920 } /* if ($this->the_end == TRUE) { ... */
921 } /* if ($cur_step < $this->step) { */
923 /* Room::unlock_data($sem); */
924 } /* else of if ($cur_step == -1) { */
928 } // function maincheck (...
935 } // end class Bin5_user
940 static $delta_t = array();
944 var $comm; // commands for many people
945 var $step; // current step of the comm array
946 var $garbage_timeout;
957 function Bin5 ($room, $table_idx, $table_token, $get, $post, $cookie) {
958 $this->user = array();
959 $this->table = array();
961 $this->the_end = FALSE;
962 $this->shm_sz = BIN5_SHM_MIN;
963 if (($this->tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
969 $table = $room->table[$table_idx];
971 log_wr("Bin5 constructor");
973 for ($i = 0 ; $i < $table->player_n ; $i++) {
974 $user[$table->player[$i]]->table_token = $table_token;
975 $this->user[$i] = Bin5_user::spawn($user[$table->player[$i]], $this, $table_idx, $i, $get, $post, $cookie);
977 $this->table[0] = Bin5_table::spawn(&$table);
979 log_main("TABLE_OLD_WIN - Bin5:".$this->table[0]->old_asta_win);
981 $this->table_idx = $table_idx;
982 $this->table_token = $table_token;
983 $this->garbage_timeout = 0;
985 $this->delay_mgr = new Delay_Manager((GARBAGE_TIMEOUT *3.0) / 2.0);
987 log_wr("Bin5 constructor end");
991 function get_user($sess, &$idx)
995 if (validate_sess($sess)) {
996 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
997 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1000 $ret = &$this->user[$i];
1004 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1005 // for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++)
1006 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1009 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1016 function garbage_manager($force)
1020 /* Garbage collector degli utenti in timeout */
1024 $delta = $this->delay_mgr->delta_get($curtime);
1026 if ($force || $this->garbage_timeout < $curtime) {
1027 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1028 $user_cur = $this->user[$i];
1029 if ($user_cur->sess == "" ||
1030 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
1033 if ($user_cur->lacc + EXPIRE_TIME_RD < ($curtime - $delta)) { // Auto logout dell'utente
1034 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
1036 if ($user_cur->stat == 'table') {
1037 log_auth($user_cur->sess," bin5 Autologout session.");
1039 /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE
1041 $tmp_sess = $user_cur->sess;
1042 $user_cur->sess = "";
1043 Bin5_user::step_unproxy($tmp_sess);
1044 $user_cur->name = "";
1045 $user_cur->the_end = FALSE;
1049 /* se gli altri utenti non erano d'accordo questo utente viene bannato */
1050 $remcalc = $this->table[0]->exitlock_calc(&$this->user, $user_cur->table_pos);
1052 require_once("${G_base}Obj/hardban.phh");
1053 Hardbans::add(($user_cur->flags & USER_FLAG_AUTH ? $user_cur->name : FALSE),
1054 $user_cur->ip, $user_cur->sess, $user_cur->laccwr + BAN_TIME);
1056 // $user->bantime = $user->laccwr + BAN_TIME;
1058 $this->table_wakeup($user_cur);
1062 log_rd2($user_cur->sess." GARBAGE UPDATED!");
1064 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
1069 $this->delay_mgr->lastcheck_set($curtime);
1074 function destroy_data()
1080 log_main("DESTROY BRISKIN5 DATA");
1083 log_main("DESTROY2 BRISKIN5 DATA [".$this->table_idx."]");
1084 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1085 $this->user[$i]->destroy_data($this->table_idx);
1086 Bin5_user::unproxy_step($this->table_idx, $this->user[$i]->sess);
1088 if (($tok = @ftok(FTOK_PATH."/bin5/table".$this->table_idx."/table", "B")) == -1)
1091 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
1094 if (shmop_delete($shm) == 0) {
1095 log_only("REMOVE FALLITA ");
1100 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
1102 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
1112 static function lock_data($is_exclusive, $table_idx)
1114 if (($res = file_lock(FTOK_PATH."/bin5/table".$table_idx."/table", $is_exclusive)) != FALSE) {
1115 self::$delta_t = microtime(TRUE);
1116 log_lock("LOCK table [".$table_idx."] [".self::$delta_t[$table_idx]."]");
1118 return (new Vect(array('res' => $res, 'tab' => $table_idx)));
1125 static function unlock_data($res_vect)
1129 $res = $res_vect->getbyid('res');
1130 $tab = $res_vect->getbyid('tab');
1132 log_lock("UNLOCK table [".$tab."] [".(microtime(TRUE) - (self::$delta_t[$tab]))."]");
1138 function chatt_send($user, $mesg)
1140 GLOBAL $mlang_brisk, $G_lang;
1142 if ($user->stat == 'table') {
1143 $table = &$this->table[$user->table];
1146 $user_mesg = substr($mesg,6);
1150 $dt = date("H:i ", $curtime);
1151 if (strncmp($user_mesg, "/nick ", 6) == 0) {
1152 log_main($user->sess." chatt_send BEGIN");
1154 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
1155 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1156 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
1161 $user_mesg = "COMMAND ".$user_mesg;
1164 // update local graph
1165 // update remote graphs
1166 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1167 $user_cur = $this->user[$i];
1168 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1169 if ($user_cur->sess == '')
1171 if (strcasecmp($user_cur->name, $name_new) == 0) {
1172 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1174 $premsg = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
1175 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $premsg);
1180 if ($i == BIN5_MAX_PLAYERS) {
1181 if ($user->flags & USER_FLAG_AUTH && strcasecmp($user->name,$name_new) != 0) {
1182 if ($this->table[$user->table]->auth_only == TRUE) {
1183 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1184 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>%s</b>");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
1189 $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
1193 $user->name = $name_new;
1195 log_main($user->sess." chatt_send start set");
1198 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1199 log_main($user->sess." chatt_send set loop");
1201 $user_cur = &$this->user[$i];
1202 if ($user_cur->sess == '')
1205 if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
1206 $table = &$this->table[$user->table];
1208 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1209 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1210 $this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->flags,
1211 xcape($this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->name),
1213 $this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->flags,
1214 xcape($this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->name),
1216 $this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->flags,
1217 xcape($this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->name),
1219 (BIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->flags),
1220 (BIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->name)),
1222 (BIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->flags),
1223 (BIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->name)));
1224 if ($user_cur == $user) {
1225 $user_cur->comm[$user_cur->step % COMM_N] .= $user_cur->myname_innerHTML();
1227 $user_cur->step_inc();
1233 for ($i = 0 ; $i < ($user->stat == 'room' ? BIN5_MAX_PLAYERS : BIN5_PLAYERS_N) ; $i++) {
1234 if ($user->stat == 'room') {
1235 $user_cur = &$this->user[$i];
1236 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1240 $user_cur = &$this->user[$table->player[$i]];
1243 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1244 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
1245 $dt, $user->flags, xcape($user->name), xcape($user_mesg));
1246 $user_cur->step_inc();
1248 log_legal($curtime, $user->ip, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
1252 function table_wakeup($user)
1254 $table = &$this->table[0];
1256 log_main("BIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
1260 log_main("BIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1262 for ($i = 0 ; $i < $table->player_n ; $i++) {
1263 $user_cur = &$this->user[$i];
1264 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1266 if ($user_cur == $user)
1267 $user_cur->subst = "shutdowner";
1269 $user_cur->subst = "shutdowned";
1270 $user_cur->laccwr = $curtime;
1272 $ret = "gst.st = ".($user_cur->step+1)."; ";
1273 $ret .= 'gst.st_loc++; xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
1275 log_wr($user_cur->sess." BIN5_WAKEUP: ".$ret);
1276 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1277 $user_cur->step_inc();
1280 $this->the_end = TRUE;
1284 * If all players are freezed the room garbage_manager clean up table and users.
1286 function is_abandoned()
1291 $table = &$this->table[0];
1293 for ($i = 0 ; $i < $table->player_n ; $i++) {
1294 $user_cur = &$this->user[$i];
1296 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
1305 static function request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
1307 GLOBAL $G_black_list;
1309 printf("NEW_SOCKET (root): %d\n", intval($new_socket));
1311 $enc = get_encoding($header);
1312 if (isset($header['User-Agent'])) {
1313 if (strstr($header['User-Agent'], "MSIE")) {
1314 $transp_type = "htmlfile";
1317 $transp_type = "xhr";
1321 $transp_type = "iframe";
1323 force_no_cache($header_out);
1325 if (($table_idx = gpcs_var('table_idx', $get, $post, $cookie)) === FALSE)
1328 if (($table_token = gpcs_var('table_token', $get, $post, $cookie)) === FALSE)
1329 unset($table_token);
1336 bin5_index_main($transp_type, $header_out, $addr, $get, $post, $cookie);
1337 $content = ob_get_contents();
1340 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1344 case "index_wr.php":
1345 // FIXME $content can be unset
1346 if (isset($table_idx) && isset($table_token)) {
1347 if (($bri = $s_a_p->app->match_get($table_idx, $table_token)) != FALSE) {
1349 bin5_index_wr_main($bri, $addr, $get, $post, $cookie);
1350 $content = ob_get_contents();
1354 $content = "Bin5 Load data error";
1358 $content = "Bin5 Load data error";
1360 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1364 case "index_rd_ifra.php":
1365 if (($transp = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
1367 if ($transp == 'websocket')
1371 if (!isset($table_idx)
1372 || !isset($table_token)
1373 || !isset($cookie['sess'])
1374 || ($bri = $s_a_p->app->match_get($table_idx, $table_token)) == NULL
1375 || (($user = $bri->get_user($cookie['sess'], $idx)) == FALSE)) {
1377 $content = Bin5_user::stream_fini($transp_type, $s_a_p->rndstr, TRUE);
1378 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1383 // close a previous opened index_read_ifra socket, if exists
1384 if (($prev = $user->rd_socket_get()) != NULL) {
1385 $s_a_p->socks_unset($user->rd_socket_get());
1386 fclose($user->rd_socket_get());
1387 printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
1388 $user->rd_socket_set(NULL);
1392 $user->stream_init($s_a_p->rndstr, $enc, $header, $header_out, $content, $get, $post, $cookie);
1394 $response = headers_render($header_out, -1).$user->chunked_content($content);
1395 $response_l = mb_strlen($response, "ASCII");
1397 $wret = @fwrite($new_socket, $response, $response_l);
1398 if ($wret < $response_l) {
1399 printf("TROUBLES WITH FWRITE: %d\n", $wret);
1400 $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
1403 $user->rd_cache_set("");
1405 fflush($new_socket);
1408 $s_a_p->socks_set($new_socket, $user, NULL);
1409 $user->rd_socket_set($new_socket);
1410 printf(" - qui ci siamo - ");
1429 function locshm_exists($tok)
1433 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
1434 log_main($tok." SHM NOT exists");
1440 log_main($tok." SHM exists");
1448 // if success an array with points of each player is returned
1449 function calculate_points_old(&$table)
1451 GLOBAL $G_all_points;
1456 if ($table->asta_pnt == 60)
1457 $table->asta_pnt = 61;
1459 $table->old_reason = "";
1460 $table->old_asta_win = $table->asta_win;
1461 $table->old_friend = $table->friend;
1462 $table->old_asta_pnt = $table->asta_pnt;
1463 $table->old_mult = $table->mult;
1465 // count points for the temporary 2 teams
1466 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1467 // for ($i = 0 ; $i < 40 ; $i++) {
1468 $ctt = $table->card[$i]->value % 10;
1469 $own = $table->card[$i]->owner;
1470 if ($own == $table->asta_win || $own == $table->friend)
1471 $pro += $G_all_points[$ctt];
1474 log_wr(sprintf("PRO: [%d]", $pro));
1477 if ($table->asta_pnt == 61 && $pro == 60) {
1478 $table->points[$table->points_n % MAX_POINTS] = array();
1479 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1480 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1484 $table->old_pnt = $pro;
1490 if ($pro >= $table->asta_pnt)
1495 $table->points[$table->points_n % MAX_POINTS] = array();
1496 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1497 if ($i == $table->asta_win)
1498 $pt = ($i == $table->friend ? 4 : 2);
1499 else if ($i == $table->friend)
1506 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1508 $pt = $pt * $sig * $table->multer(TRUE) * ($pro == 120 ? 2 : 1);
1510 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->multer(TRUE), ($pro == 120 ? 2 : 1)));
1512 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1513 $table->total[$i] += $pt;
1517 $table->old_pnt = $pro;
1525 is_transition (is from room to table ?)
1526 is_again (is another game)
1528 Examples of $is_transition, $is_again:
1529 from reload of the page: FALSE, FALSE
1530 from sitdown in room: TRUE, FALSE
1531 from table: asta cmd e tutti passano: TRUE, TRUE
1532 from table: fine partita: TRUE, TRUE
1534 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1536 $table_idx = $user->table;
1537 $table = $bri->table[$table_idx];
1538 $table_pos = $user->table_pos;
1540 $ret = "table_init();";
1541 $ret .= $table->exitlock_show(&$bri->user, $table_pos);
1543 /* GENERAL STATUS */
1544 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1545 $sendstep, $user->stat, $user->subst, $table_pos);
1547 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1550 $ret .= "background_set();";
1553 $ret .= $user->myname_innerHTML();
1554 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1555 $bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->flags,
1556 xcape($bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->name),
1558 $bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->flags,
1559 xcape($bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->name),
1561 $bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->flags,
1562 xcape($bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->name),
1564 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->flags),
1565 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->name)),
1567 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->flags),
1568 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->name)));
1570 /* NOTIFY FOR THE CARD MAKER */
1571 if ($is_transition) { // && $user->subst == "asta" superfluo
1572 $ret .= show_table_info(&$bri, &$table, $table_pos);
1573 $ret .= "setTimeout(preload_images, 500, g_preload_img_arr, g_imgct);";
1576 $ret .= table_welcome($user);
1578 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1579 $ret .= playsound("cow.mp3");
1584 if ($is_transition) { // && $user->subst == "asta" superfluo
1587 for ($i = 0 ; $i < BIN5_CARD_HAND ; $i++) {
1588 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++) {
1590 for ($o = 0 ; $o < (BIN5_CARD_HAND * BIN5_PLAYERS_N) && $ct < $i+1 ; $o++) {
1591 // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1592 if ($table->card[$o]->owner == (($e + $table->gstart) % BIN5_PLAYERS_N)) {
1598 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1600 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BIN5_PLAYERS_N,
1601 $i, ((($e + BIN5_PLAYERS_N - $table_pos + $table->gstart) % BIN5_PLAYERS_N) == 0 ?
1602 $table->card[$o]->value : -1),
1603 ($i == 7 && $e == (BIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1608 $taked = array(0,0,0,0,0);
1609 $inhand = array(0,0,0,0,0);
1610 $ontabl = array(-1,-1,-1,-1,-1);
1613 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1614 // for ($i = 0 ; $i < 40 ; $i++) {
1615 if ($table->card[$i]->stat == 'hand') {
1616 if ($table->card[$i]->owner == $table_pos) {
1617 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1619 $inhand[$table->card[$i]->owner]++;
1621 else if ($table->card[$i]->stat == 'take') {
1622 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1623 $taked[$table->card[$i]->owner]++;
1625 else if ($table->card[$i]->stat == 'table') {
1626 $ontabl[$table->card[$i]->owner] = $i;
1630 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1631 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1633 log_main("Stat table: ".$logg);
1635 /* Set ours cards. */
1637 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1638 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1639 for ($i = $inhand[$table_pos] ; $i < BIN5_CARD_HAND ; $i++)
1640 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1641 $ret .= sprintf('card_setours(%s);', $oursarg);
1643 /* Dispose all cards */
1644 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1645 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1646 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1647 ($inhand[$i] <= BIN5_CARD_HAND ? $inhand[$i] : BIN5_CARD_HAND), $taked[$i]);
1649 if ($ontabl[$i] != -1) {
1650 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1651 $table->card[$ontabl[$i]]->value,
1652 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1658 if ($user->subst == 'asta') {
1660 /* show users auction status */
1662 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1663 $user_cur = &$bri->user[$table->player[$i]];
1664 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1665 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1667 if (BIN5_PLAYERS_N == 3)
1668 $showst .= ",-2,-2";
1669 $ret .= sprintf('document.title = "Brisk - Tavolo %d (asta)";', $user->table_orig);
1670 $ret .= sprintf('show_astat(%s);', $showst);
1672 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1673 /* show card chooser */
1674 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1679 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N) &&
1680 $table->asta_win == -1)
1681 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1682 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1684 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1685 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1689 if ($table->asta_win == -1) { // auction case
1690 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N))
1691 $ret .= "remark_on();";
1693 $ret .= "remark_off();";
1695 else { // chooseed case
1696 if ($table_pos == $table->asta_win)
1697 $ret .= "remark_on();";
1699 $ret .= "remark_off();";
1702 else if ($user->subst == 'game') {
1704 if (($table->gstart + $table->turn) % BIN5_PLAYERS_N == $table_pos)
1705 $ret .= "is_my_time = true; remark_on();";
1707 $ret .= "remark_off();";
1709 /* WHO CALL AND WHAT */
1710 $ret .= briscola_show($bri, $table, $user);
1714 } // end function show_table(...
1716 function calculate_winner(&$table)
1723 $cur_seed = $table->briscola - ($table->briscola % 10);
1725 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1726 // for ($i = 0 ; $i < 40 ; $i++) {
1727 if ($table->card[$i]->stat != "table")
1730 log_wr(sprintf("Card On table: [%d]", $i));
1732 $v = $table->card[$i]->value;
1733 $ontab[$table->card[$i]->owner] = $v;
1734 $ontid[$table->card[$i]->owner] = $i;
1735 /* se briscola setto il flag */
1736 if (($v - ($v % 10)) == $cur_seed)
1740 if ($briontab == FALSE) {
1741 $cur_win = $table->gstart;
1742 $cur_val = $ontab[$cur_win];
1743 $cur_seed = $cur_val - ($cur_val % 10);
1746 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1747 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1748 if ($ontab[$i] < $cur_val) {
1749 $cur_val = $ontab[$i];
1755 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1756 $table->card[$ontid[$i]]->owner = $cur_win;
1757 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1762 function show_table_info(&$bri, &$table, $table_pos)
1764 GLOBAL $G_lang, $mlang_bin5_bin5;
1767 $user = $bri->user[$table->player[$table_pos]];
1769 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1770 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1773 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++)
1774 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1775 $noty .= sprintf("</tr>");
1778 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1780 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1781 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1782 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1783 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1788 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1789 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1790 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1791 $noty .= "</tr></table>";
1793 if ($table->old_reason != "") {
1794 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1797 if ($table->old_asta_win != -1) {
1798 log_main("TABLE_OLD_WIN:".$table->old_asta_win);
1799 $win = $table->player[$table->old_asta_win];
1800 log_main("TABLE_OLD_FRIEND:".$table->old_friend);
1801 $fri = $table->player[$table->old_friend];
1803 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1806 /* 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" */
1808 $noty .= sprintf($mlang_bin5_bin5['info_part'][$G_lang],
1809 xcape($bri->user[$win]->name),
1810 xcape($bri->user[$fri]->name));
1811 if ($table->old_pnt == 120) {
1812 $noty .= sprintf($mlang_bin5_bin5['info_capp'][$G_lang]);
1815 $noty .= sprintf($mlang_bin5_bin5['info_pnt'][$G_lang],
1816 ($table->old_asta_pnt > 61 ? $mlang_bin5_bin5['info_alea'][$G_lang].$table->old_asta_pnt :
1817 $mlang_bin5_bin5['info_more'][$G_lang]), $table->old_pnt,
1818 ($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])));
1822 $noty .= sprintf($mlang_bin5_bin5['info_alon'][$G_lang],
1823 xcape($bri->user[$win]->name));
1824 if ($table->old_pnt == 120) {
1825 $noty .= sprintf($mlang_bin5_bin5['info_acap'][$G_lang]);
1828 $noty .= sprintf($mlang_bin5_bin5['info_apnt'][$G_lang],
1830 ($table->old_asta_pnt > 61 ? $mlang_bin5_bin5['info_alea'][$G_lang].$table->old_asta_pnt :
1831 $mlang_bin5_bin5['info_more'][$G_lang]), $table->old_pnt,
1833 ($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])));
1836 $old_multer = $table->multer(FALSE);
1837 if ($old_multer > 1) {
1838 $noty .= sprintf($mlang_bin5_bin5['info_omul'][$G_lang], multoval($old_multer));
1840 $noty .= "<hr><br>";
1842 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1843 if ($table->mazzo == $table_pos)
1844 $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
1846 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1847 $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
1850 if ($user->subst == 'asta') {
1851 if ($table->asta_win == -1) // auction case
1852 $curplayer = $table->gstart % BIN5_PLAYERS_N;
1854 $curplayer = $table->asta_win;
1856 else if ($user->subst == 'game') {
1857 $curplayer = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
1860 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1861 if ($curplayer == $table_pos) {
1862 $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
1865 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1866 $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
1869 $multer = $table->multer(TRUE);
1871 $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($multer) );
1873 $noty .= "<hr><br>";
1874 $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
1875 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
1876 * 130 + ($table->points_n > 0 ? 50 : 0) +
1877 * (120 * ($table->points_n / MAX_POINTS)));
1878 * will be used when we refact notify js function following
1879 * photoo class logic
1885 function table_welcome($user)
1887 GLOBAL $table_wellarr, $G_lang;
1890 for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
1891 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
1897 function briscola_show($bri, $table, $user)
1899 GLOBAL $G_lang, $mlang_bin5_bin5;
1903 if ($table->asta_card == 9)
1904 $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
1906 /* text of caller cell */
1907 if ($user->table_pos == $table->asta_win) {
1908 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
1909 $ret .= sprintf($prestr, $ptnadd);
1912 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
1913 $ret .= sprintf($prestr,
1914 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1916 $ret .= sprintf('set_iscalling(%d);', ($table->asta_win - $user->table_pos + BIN5_PLAYERS_N) % BIN5_PLAYERS_N);
1918 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1920 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1921 $ret .= sprintf('$("caller").style.visibility = "visible";');
1922 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1923 $ret .= sprintf('$("astalascio").style.visibility = "";');
1924 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1925 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1931 function game_result($asta_pnt, $pnt)
1933 if ($asta_pnt == 61) {
1936 else if ($pnt == 60)
1942 if ($pnt >= $asta_pnt)
1949 function log_points($remote_addr, $curtime, $user, $where, $mesg)
1951 if (($fp = @fopen(LEGAL_PATH."/points.log", 'a')) != FALSE) {
1952 /* Unix time | session | nickname | IP | where was | mesg */
1953 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
1954 ($user->flags & USER_FLAG_AUTH ? 'A' : 'N'),
1955 $user->name, $remote_addr, $where , $mesg));