5 * Copyright (C) 2006-2013 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details. You should have received a
19 * copy of the GNU General Public License along with this program; if
20 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21 * Suite 330, Boston, MA 02111-1307, USA.
24 define('BIN5_PLAYERS_N', 3);
25 define('BIN5_CARD_HAND', 3); // normal value 8
26 define('BIN5_MAX_PLAYERS', BIN5_PLAYERS_N);
27 // define(BIN5_SHM_MIN', (50000 * BIN5_MAX_PLAYERS));
28 define('BIN5_SHM_MIN', 32768);
29 define('BIN5_SHM_MAX', (BIN5_SHM_MIN + 1048576));
30 define('BIN5_SHM_DLT', 32768);
31 define('BIN5_PROXY_PATH', PROXY_PATH."/bin5");
33 require_once('rules.phh');
35 define('BIN5_TOURNAMENT_CURRENT', BIN5_TOURNAMENT_NO_DRAW);
37 $mlang_bin5_bin5 = array(
38 // br, hr, b, /b, win, fri
39 'info_last' => array( 'it' => '%3$sultima mano%4$s',
40 'en' => '%3$slast hand%4$s'),
41 'info_curr' => array( 'it' => '%3$smano corrente%4$s',
42 'en' => '%3$scurrent hand%4$s'),
43 'info_yshuf'=> array( 'it' => 'Fai <b>tu</b> il mazzo, ',
44 'en' => 'It\'s <b>your</b> shuffled the cards, '),
45 'info_shuf' => array( 'it' => 'Il mazzo a <b>%s</b>, ',
46 'en' => '<b>%s</b> shuffled the cards, '),
47 'info_yturn'=> array( 'it' => ' tocca a <b>te</b> giocare.',
48 'en' => ' it\'s <b>your</b> turn.'),
49 'info_turn' => array( 'it' => 'tocca a <b>%s</b> giocare.',
50 'en' => 'it\'s the <b>%s</b>\'s turn.'),
51 'info_mult' => array( 'it' => ' La partita vale <b>%s</b>.',
52 'en' => ' The game worth <b>%s</b>.' ),
53 'info_match'=> array( 'it' => 'Il codice della partita รจ <b>%d</b>.',
54 'en' => 'Match code is <b>%d</b>.'),
56 'btn_bkgame'=> array( 'it' => 'torna alla partita',
57 'en' => 'back to the game'),
58 'call_wptn' => array( 'it' => '<br>con %d punti',
59 'en' => '<br>with %d points'),
60 'call_ycall'=> array( 'it' => 'Chiami%s:',
62 'call_call' => array( 'it' => 'Chiama %s%s:',
63 'en' => 'The declarer is %s%s:')
67 $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.'),
68 '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.') );
70 function multoval($mult)
75 return ($G_lang == 'en' ? 'double' : 'doppio');
77 return ($G_lang == 'en' ? 'triple' : 'triplo');
79 return ($G_lang == 'en' ? 'quadruple' : 'quadruplo');
81 return (sprintf(($G_lang == 'en' ? "%d-ple" : "%d-plo"), $mult));
84 function dom_select_rules()
88 $ret = "<select id='select_rules'>\n";
89 foreach (rules_keys() as $key) {
90 $value = rules_id2descr($key, $G_lang);
91 $ret .= sprintf("<option value='%d'>%s</option>\n", $key, $value);
93 $ret .= "</select>\n";
99 var $value; /* 0 - 39 card value */
100 var $stat; /* 'bunch', 'hand', 'table', 'take' */
101 var $owner; /* (table position 0-4) */
102 // var $pos; /* Pos in hand. */
103 var $x; /* When played the X position on the table of the owner. */
104 var $y; /* When played the Y position on the table of the owner. */
106 function Card($value, $stat, $owner)
108 $this->value = $value;
109 $this->stat = $stat; // Card stat
110 $this->owner = $owner;
113 function assign($stat,$owner)
115 $this->stat = $stat; // Card stat
116 $this->owner = $owner;
119 function setpos($pos)
126 $this->stat = 'table'; // Card stat
131 function take($newown)
133 $this->stat = 'take'; // Card stat
134 $this->owner = $newown;
138 class Bin5_table extends Table {
139 var $card; // il mazzo di carte
140 var $mazzo; // chi e' di mazzo
141 var $gstart; // first player of the current game
142 var $turn; // turn in the game (absolute, not modularized)
144 var $asta_pla; // array(); TRUE: in auction, FALSE: out of the auction
145 var $asta_pla_n; // number of players in auction
146 var $asta_card; // current card for auction
147 var $asta_pnt; // current point for auction
150 var $points; // points array
151 var $points_n; // number of row of points
154 var $asta_win; // the caller idx position at table
156 var $tourn_pts; // points in the caller hand
157 var $friend; // the callee idx position at table
159 var $match_id; // the id of the match on the database (-1 == just not saved)
161 var $old_act; // last action that trigs the end of the game
166 var $old_pnt; // points made by caller and callee
167 var $old_asta_win; // the old caller idx position at table
168 var $old_friend; // the old callee idx position at table
170 var $old_tourn_pts; // the old tournment computed points in the hand of caller
173 function Bin5_table()
179 function create($idx)
181 if (($thiz =& new Bin5_table()) == FALSE)
187 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
188 $thiz->asta_pla_n= -1;
189 $thiz->asta_card = -1;
190 $thiz->asta_pnt = -1;
193 $thiz->points = array( );
195 $thiz->total = array( 0, 0, 0, 0, 0);
196 $thiz->asta_win = -1;
197 $thiz->briscola = -1;
198 $thiz->tourn_pts = -1;
202 $thiz->match_id = -1;
205 $thiz->old_mazzo = -1;
206 $thiz->old_reason = "";
207 $thiz->old_asta_pnt = -1;
208 $thiz->old_mult = -1;
210 $thiz->old_asta_win = -1;
211 $thiz->old_friend = -1;
218 function myclone(&$from)
220 if (($thiz =& new Bin5_table()) == FALSE)
225 $thiz->card = $from->card;
226 $thiz->mazzo = $from->mazzo; // REVIEW
227 $thiz->gstart = $from->gstart;
228 $thiz->turn = $from->turn;
230 $thiz->asta_pla = $from->asta_pla;
231 $thiz->asta_pla_n = $from->asta_pla_n;
232 $thiz->asta_card = $from->asta_card;
233 $thiz->asta_pnt = $from->asta_pnt;
235 $thiz->mult = $from->mult;
236 $thiz->points = $from->points;
237 $thiz->points_n = $from->points_n;
238 $thiz->total = $from->total;
240 $thiz->asta_win = $from->asta_win;
241 $thiz->briscola = $from->briscola;
242 $thiz->tourn_pts = $from->tourn_pts;
243 $thiz->friend = $from->friend;
245 $thiz->match_id = $from->match_id;
247 $thiz->old_act = $from->old_act;
248 $thiz->old_mazzo = $from->old_mazzo;
249 $thiz->old_reason = $from->old_reason;
250 $thiz->old_asta_pnt = $from->old_asta_pnt;
251 $thiz->old_mult = $from->mult;
252 $thiz->old_pnt = $from->old_pnt;
253 $thiz->old_asta_win = $from->old_asta_win;
254 $thiz->old_friend = $from->old_friend;
260 function parentcopy(&$from)
265 function spawn(&$from)
268 if (($thiz =& new Bin5_table()) == FALSE)
271 $thiz->parentcopy($from);
273 log_main("PLAYER_N - spawn.".$thiz->player_n);
275 $thiz->card = array();
276 $thiz->bunch_create();
277 $thiz->mazzo = rand(0,PLAYERS_N-1);
278 $thiz->points = array();
279 $thiz->total = array();
283 $thiz->match_id = -1;
285 $thiz->old_asta_win = -1;
286 $thiz->old_reason = "";
288 $rules_name = rules_id2name(BIN5_TOURNAMENT_CURRENT);
289 $thiz->rules = new $rules_name($thiz);
290 // players are rearranged in an dedicated array
291 $thiz->player = array();
292 for ($i = 0 ; $i < $from->player_n ; $i++)
293 $thiz->player[$i] = $i;
295 log_main("TABLE_OLD_WIN - spawn:".$thiz->old_asta_win);
301 // function bunch_create_old() function AND
305 // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
306 // // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
307 // $ret[$i] =& new Card($i, 'bunch', 'no_owner');
314 function bunch_create()
318 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
319 $this->card[$i] = new Card($i, 'bunch', 'no_owner');
323 function bunch_make()
325 log_main("bunch_make start");
326 $ct = array(0,0,0,0,0);
328 mt_srand(make_seed());
330 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--)
333 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--) {
337 log_main("RND ZERO");
341 $owner = $i % BIN5_PLAYERS_N;
342 $this->card[$id]->assign('hand', $owner);
344 $rest[$rn] = $rest[$i];
345 // $pubbpos[$rn2] = $pubbpos[$i];
347 log_main("bunch_make end");
350 function init($userarr)
353 $this->mazzo = rand(0,PLAYERS_N-1);
356 $this->old_asta_win = -1;
357 $this->old_reason = "";
359 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
360 $this->total[$i] = 0;
361 $user_cur = $userarr[$this->player[$i]];
362 $user_cur->stat_set('table');
363 $user_cur->exitislock = TRUE;
366 log_main("table::init: ci siamo");
369 function game_init($userarr)
373 $this->gstart = ($this->mazzo+1) % BIN5_PLAYERS_N;
376 $this->asta_pla_n = BIN5_PLAYERS_N;
377 $this->asta_card = -1;
378 $this->asta_pnt = 60;
379 $this->asta_win = -1;
380 $this->briscola = -1;
381 $this->tourn_pts = -1;
385 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
386 $this->asta_pla[$i] = TRUE;
387 $user_cur = $userarr[$this->player[$i]];
388 $user_cur->subst = 'asta';
389 $user_cur->asta_card = -2;
390 $user_cur->asta_pnt = -1;
391 $user_cur->handpt = $this->hand_points($i);
392 $this->rules->tourn_points($user_cur, $i);
397 function game_next($delta)
399 $this->old_mazzo = $this->mazzo;
400 $this->mazzo = ($this->mazzo + $delta) % BIN5_PLAYERS_N;
403 function mult_inc($val)
405 $this->old_mult = $this->mult;
409 function mult_set($val)
411 $this->old_mult = $this->mult;
415 function hand_points($idx)
417 GLOBAL $G_all_points;
421 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
422 // for ($i = 0 ; $i < 40 ; $i++) {
423 if ($this->card[$i]->owner != $idx)
426 $ctt = $this->card[$i]->value % 10;
427 $tot += $G_all_points[$ctt];
434 function exitlock_show($userarr, $table_pos)
436 $ct = $this->exitlock_calc($userarr, $table_pos);
438 $ret = sprintf('exitlock_show(%d, %s);', $ct,
439 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
443 function exitlock_calc(&$userarr, $table_pos)
447 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
448 if ($userarr[$this->player[$i]]->exitislock == FALSE)
455 function match_continue(&$bri, $user, $match_id_s)
458 // Rules: update version following loaded tcode
464 if ($this->match_id != -1) {
465 $msg = sprintf("Stai giร giocando la partita con codice %d.", $this->match_id);
469 /* - verify if match_id and user are both valid to accept
470 the match_continue request - */
471 $match_id = (int)$match_id_s;
472 if ($match_id <= 0) {
473 $msg = "questa partita non esiste";
477 if ($user->continue_get() == $match_id) {
478 $msg = "Hai giร richiesto di continuare questa partita.";
481 // retrieves users list for this match
482 $match_data = array();
483 if (($bdb = BriskDB::create()) != FALSE) {
484 // match_order_get return FALSE for old matches
485 $ucodes = $bdb->match_order_get($match_data, $match_id, BIN5_PLAYERS_N);
488 if ($ucodes == FALSE) {
489 $msg = "questa partita non รจ stata memorizzata correttamente";
493 // if current user code must be in the users list
494 if (array_search($user->code, $ucodes) === FALSE) {
495 $msg = sprintf("Questo utente non compare nella partita che si vuole continuare [%d].", $user->code);
499 /* - the user is in the list, add the match_id to his profile
500 and check if he is the N'th to require continue - */
503 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
504 $user_cur = &$bri->user[$this->player[$i]];
506 if ($user == $user_cur) {
511 if ($user_cur->continue_get() == BIN5_USER_CONTINUE_INIT)
514 if ($user_cur->continue_get() != $match_id)
518 if ($i < BIN5_PLAYERS_N) {
519 $msg = sprintf("<b>L'utente <i>%s</i> voleva giร continuare la partita nยฐ %d.</b>",
520 xcape($user_cur->name), $user_cur->continue_get());
524 // set the match_id for the current user
525 $user->continue_set($match_id);
528 // not all players set the continue match than we exit
529 if ($continue_tot < BIN5_PLAYERS_N) {
530 $msg = sprintf("<b>L'utente <i>%s</i> vorrebbe continuare la partita nยฐ %d.</b>",
531 xcape($user->name), $match_id);
535 /* - all users decide to continue the same match, update all infos and rearrange users
536 to the right positions on the table - */
538 /* reset users table order */
539 for ($i = 0 ; $i < BIN5_PLAYERS_N - 1 ; $i++) {
540 if ($bri->user[$this->player[$i]]->code == $ucodes[$i]) {
543 for ($e = $i + 1 ; $e < BIN5_PLAYERS_N ; $e++) {
544 if ($bri->user[$this->player[$e]]->code == $ucodes[$i]) {
545 $swap = $this->player[$i];
546 $this->player[$i] = $this->player[$e];
547 $this->player[$e] = $swap;
548 $bri->user[$this->player[$i]]->table_pos = $i;
549 $bri->user[$this->player[$e]]->table_pos = $e;
553 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
554 fprintf(STDERR, "USERZ: [%s]\n", $bri->user[$this->player[$i]]->name);
557 // update database info to be aligned with current table (ttok and table_idx
558 if (($bdb = BriskDB::create()) != FALSE) {
559 if ($bdb->match_continue($match_id, $this, $user->table_orig) == FALSE) {
561 $msg = "aggiornamento dei dati della partita fallito";
567 /* update rules engine */
568 $rules_name = rules_id2name($match_data['tcode']);
569 $this->rules = new $rules_name($this);
571 /* bunch and multiplier status set */
572 $this->mazzo = $match_data['mazzo_next'];
573 $this->mult = $match_data['mult_next'];
574 $this->match_id = $match_id;
575 $this->game_init($bri->user);
577 /* reload of the page with the new layout */
578 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
579 $user_cur = &$bri->user[$this->player[$i]];
581 $user_cur->trans_step = $user_cur->step + 1;
582 $user_cur->comm[$user_cur->step % COMM_N] = sprintf('gst.st_loc++; gst.st=%d; xstm.stop(); window.onunload = null ; window.onbeforeunload = null ; document.location.assign("index.php");|', $user_cur->step+1);
583 $user_cur->step_inc();
585 // a void command force xynt-streamer to flush all data to client
586 $user_cur->trans_step = $user_cur->step + 1;
587 $user_cur->comm[$user_cur->step % COMM_N] = "";
588 $user_cur->step_inc();
590 $user_cur->comm[$user_cur->step % COMM_N] = show_table($bri, $user_cur, $user_cur->step+1, TRUE, FALSE);
591 $user_cur->step_inc();
596 $dt = date("H:i ", $curtime);
597 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
598 if ($ret == FALSE && $this->player[$i] != $user->idx)
600 $user_cur = &$bri->user[$this->player[$i]];
601 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
602 $user_cur->comm[$user_cur->step % COMM_N] .= nickserv_msg($dt, $msg);
603 $user_cur->step_inc();
607 function rules_change(&$bri, $user, $rules_id_s)
612 // Rules: update version following loaded tcode
618 if ($this->match_id != -1) {
620 $msg = sprintf("Stai giร giocando la partita con codice %d.", $this->match_id);
624 if ($this->asta_card != -1 || $this->asta_pla_n < BIN5_PLAYERS_N) {
626 $msg = sprintf("La partita รจ giร stata avviata.");
630 $rules_id = (int)$rules_id_s;
631 if (! in_array($rules_id, rules_keys())) {
633 $msg = "queste regole non esistono o non sono attive";
637 if ($user->rules_get() == $rules_id) {
639 $msg = "hai giร richiesto di usare queste regole";
643 /* - the user is in the list, add the match_id to his profile
644 and check if he is the N'th to require continue - */
647 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
648 $user_cur = &$bri->user[$this->player[$i]];
650 if ($user == $user_cur) {
655 if ($user_cur->rules_get() == BIN5_USER_CONTINUE_INIT)
658 if ($user_cur->rules_get() != $rules_id)
662 if ($i < BIN5_PLAYERS_N) {
664 $msg = sprintf("<b>L'utente <i>%s</i> voleva giร usare le %s.</b>",
665 xcape($user_cur->name), xcape(rules_id2descr($user_cur->rules_get(), $G_lang)));
669 // set the match_id for the current user
670 $user->rules_set($rules_id);
673 // not all players set the continue match than we exit
674 if ($rules_tot < BIN5_PLAYERS_N) {
676 $msg = sprintf("<b>L'utente <i>%s</i> vorrebbe usare le %s.</b>",
677 xcape($user->name), xcape(rules_id2descr($rules_id, $G_lang)));
681 /* - all users decide to continue the same match, update all infos and rearrange users
682 to the right positions on the table - */
684 /* update rules engine */
685 $rules_name = rules_id2name($rules_id);
686 $this->rules = new $rules_name($this);
688 // $this->game_init(&$bri->user);
690 /* reload of the page with the new layout */
691 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
692 $user_cur = &$bri->user[$this->player[$i]];
693 $this->rules->tourn_points($user_cur, $i);
695 $user_cur->rules_set(BIN5_USER_CONTINUE_INIT);
696 $user_cur->trans_step = $user_cur->step + 1;
697 $user_cur->comm[$user_cur->step % COMM_N] = sprintf('gst.st_loc++; gst.st=%d; xstm.stop(); window.onunload = null ; window.onbeforeunload = null ; document.location.assign("index.php");|', $user_cur->step+1);
698 $user_cur->step_inc();
700 // a void command force xynt-streamer to flush all data to client
701 $user_cur->trans_step = $user_cur->step + 1;
702 $user_cur->comm[$user_cur->step % COMM_N] = "";
703 $user_cur->step_inc();
705 $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$bri, &$user_cur, $user_cur->step+1, TRUE, FALSE);
706 $user_cur->step_inc();
711 $dt = date("H:i ", $curtime);
713 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
714 $user_cur = &$bri->user[$this->player[$i]];
715 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
716 $user_cur->comm[$user_cur->step % COMM_N] .= nickserv_msg($dt, $msg);
717 $user_cur->step_inc();
721 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
722 $user->comm[$user->step % COMM_N] = xcape(sprintf("rules_set(%d);",
724 $user->comm[$user->step % COMM_N] .= nickserv_msg($dt, $msg);
727 } // end function rules_change
728 } // end class Bin5_table
733 define('BIN5_USER_FLAG_RING_ENDAUCT', 0x01);
734 define('BIN5_USER_CONTINUE_INIT', -1);
736 define('BIN5_USER_RULES_INIT', -1);
737 class Bin5_user extends User {
740 var $handpt; // Total card points at the beginning of the current hand.
741 var $exitislock; // Player can exit from the table ?
742 var $privflags; // Flags for briskin5 only
744 var $continue; // Id of the match that the user would continue
745 var $rules; // Id of rules required by user
747 var $asta_tourn_pts; // array with tournment points for each suit
755 function create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
756 if (($thiz =& new User()) == FALSE)
759 $thiz->asta_card = -2;
760 $thiz->asta_pnt = -1;
762 $thiz->exitislock = TRUE;
763 $thiz->privflags = 0;
764 $thiz->continue = BIN5_USER_CONTINUE_INIT;
765 $thiz->rules = BIN5_USER_RULES_INIT;
771 function parentcopy(&$from)
776 function copy(&$from)
778 $this->parentcopy($from);
780 $this->asta_card = $from->asta_card;
781 $this->asta_pnt = $from->asta_pnt;
782 $this->handpt = $from->handpt;
783 $this->exitislock = $from->exitislock;
784 $this->privflags = $from->privflags;
785 $this->continue = $from->continue;
786 $this->rules = $from->rules;
790 function myclone(&$from)
792 if (($thiz =& new User()) == FALSE)
801 static function spawn($from, &$bri, $table, $table_pos, $get, $post, $cookie)
803 if (($thiz = new Bin5_user()) == FALSE)
806 if (($CO_bin5_pref_ring_endauct = gpcs_var("CO_bin5_pref_ring_endauct", $get, $post, $cookie)) === FALSE) {
807 $CO_bin5_pref_ring_endauct = "";
810 $thiz->parentcopy($from);
812 /* NOTE: at this moment idx and table_pos fields have the same value
813 but diffentent functions, we keep them separated for a while */
815 $thiz->idx = $table_pos; // it is the position in the mini-room,
816 // not related to table pos (see below)
817 $thiz->asta_card = -2;
818 $thiz->asta_pnt = -1;
820 $thiz->exitislock = TRUE;
821 $thiz->continue = BIN5_USER_CONTINUE_INIT;
822 $thiz->rules = BIN5_USER_RULES_INIT;
824 log_wr("Bin5 constructor");
826 $thiz->privflags = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
828 $thiz->table_orig = $table;
830 $thiz->table_pos = $table_pos;
837 function step_set($step)
839 $this->step = $step & 0x7fffffff;
844 function step_inc($delta = 1) {
845 $this->step += $delta;
846 /* modularization because unpack() not manage unsigned 32bit int correctly */
847 $this->step &= 0x7fffffff;
852 static function load_step($tab_id, $sess)
856 if (validate_sess($sess) == FALSE)
859 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
860 mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
861 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step", 'rb')) == FALSE)
863 if (($s = fread($fp, 8)) == FALSE)
865 if (mb_strlen($s, "ASCII") != 8)
867 $arr = unpack('Ls/Li', $s);
870 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
877 log_rd2("STEP_GET [".$sess."]: return false ");
885 if (validate_sess($this->sess) == FALSE)
887 if (file_exists(BIN5_PROXY_PATH."/table".$this->table_orig) == FALSE)
888 mkdir(BIN5_PROXY_PATH."/table".$this->table_orig, 0775, TRUE);
889 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$this->table_orig."/".$this->sess.".step", 'w')) == FALSE)
891 fwrite($fp, pack("LL",$this->step, $this->idx));
894 log_main("step_set [".$this->sess. "] [".$this->step."]");
902 static function unproxy_step($tab_id, $sess)
904 log_rd2("UNPROXY: ".BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
905 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
908 @unlink(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
911 function destroy_data($tab_id)
914 if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos, "B")) == -1) {
915 log_crit("BIN5 USER DATA REMOVE FAILED 1 [".FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos."]");
919 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
920 log_crit("BIN5 USER DATA REMOVE FAILED 2");
923 if (shmop_delete($shm) == 0) {
924 log_crit("BIN5 USER DATA REMOVE FAILED 3");
929 log_main("BIN5 USER DATA DESTROY SUCCESS");
931 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
941 static function blocking_error($is_unrecoverable)
943 log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
944 return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
947 protected function page_sync($sess, $page)
949 log_rd2("PAGE_SYNC");
950 printf("xXx BIN5_USER::PAGE_SYNC\n");
951 return (sprintf('xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
954 protected function maincheck($get, $post, $cookie)
957 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
958 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
962 log_rd("maincheck begin");
967 /* Nothing changed, return. */
968 if ($this->rd_step == $this->step)
971 log_rd2("do other cur_stat[".$this->rd_stat."] user->stat[".$this->stat."] cur_step[".$this->rd_step."] user_step[".$this->step."]");
973 if ($this->rd_step == -1) {
975 * if $this->rd_step == -1 load the current state from the main struct
978 $S_load_stat['wR_minusone']++;
980 // if ($this->the_end == TRUE) {
981 // log_rd2("main_check: the end".var_export(debug_backtrace()));
984 if ($this->trans_step != -1) {
985 log_rd2("TRANS USATO ".$this->trans_step);
986 $this->rd_step = $this->trans_step;
987 $this->trans_step = -1;
990 log_rd2("TRANS NON ATTIVATO");
995 /* this part I suppose is read only on $this->room structure */
996 if ($this->rd_step == -1) {
997 log_rd2("PRE-NEWSTAT");
1004 if ($this->stat == 'table') {
1006 /* NOTE: $this->room is associated with the current $bri object */
1007 printf("xXx CLASS NAME [%s]\n", get_class($this->room));
1008 $ret = show_table($this->room, $this, $this->step, FALSE, FALSE);
1010 log_rd2("NEWSTAT: ".$this->stat);
1012 $this->rd_stat = $this->stat;
1013 $this->rd_subst = $this->subst;
1014 $this->rd_step = $this->step;
1015 } /* if ($this->rd_step == -1) { */
1017 $S_load_stat['rU_heavy']++;
1019 if ($this->rd_step < $this->step) {
1021 if ($this->rd_step + COMM_N < $this->step) {
1022 if ($this->rd_stat != $this->stat) {
1023 $to_stat = $this->stat;
1025 printf("xXx BIN5_USER::MAINCHECK\n");
1026 return ($this->page_sync($this->sess, ($to_stat == "table" ? "index.php" : "../index.php"), $this->table, $this->table_token));
1028 log_rd2("lost history, refresh from scratch");
1029 printf("xXx LOST HISTORY!\n");
1030 $this->rd_step = -1;
1033 for ($i = $this->rd_step ; $i < $this->step ; $i++) {
1035 if ($this->comm[$ii] == "") {
1036 if ($i == $this->rd_step)
1041 log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
1042 $ret .= $this->comm[$ii];
1044 $this->rd_stat = $this->stat;
1045 $this->rd_subst = $this->subst;
1046 $this->rd_step = $this->step;
1049 log_rd2($this->step, 'index_rd.php: after ret set');
1051 // if ($this->the_end == TRUE) { management is moved
1052 // in the spush scope
1053 } /* if ($this->rd_step < $this->step) { */
1054 } /* else of if ($this->rd_step == -1) { */
1058 } // function maincheck(...
1060 function continue_set($match_code)
1062 $this->continue = $match_code;
1065 function continue_get() {
1066 return ($this->continue);
1069 function rules_set($rules_id)
1071 $this->rules = $rules_id;
1074 function rules_get() {
1075 return ($this->rules);
1078 } // end class Bin5_user
1082 static $delta_t = array();
1083 var $brisk;// room object reference
1087 var $comm; // commands for many people
1088 var $step; // current step of the comm array
1089 var $garbage_timeout;
1100 function Bin5($brisk, $table_idx, $table_token, $get, $post, $cookie) {
1101 $this->user = array();
1102 $this->table = array();
1104 $this->the_end = FALSE;
1105 $this->shm_sz = BIN5_SHM_MIN;
1106 if (($this->tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
1111 $this->brisk = $brisk;
1112 $user = $brisk->user;
1113 $table = $brisk->table[$table_idx];
1114 log_wr("Bin5 constructor");
1116 for ($i = 0 ; $i < $table->player_n ; $i++) {
1117 $user[$table->player[$i]]->table_token = $table_token;
1118 $this->user[$i] = Bin5_user::spawn($user[$table->player[$i]], $this, $table_idx, $i, $get, $post, $cookie);
1120 $this->table[0] = Bin5_table::spawn($table);
1122 log_main("TABLE_OLD_WIN - Bin5:".$this->table[0]->old_asta_win);
1124 $this->table_idx = $table_idx;
1125 $this->table_token = $table_token;
1126 $this->garbage_timeout = 0;
1128 $this->delay_mgr = new Delay_Manager((GARBAGE_TIMEOUT *3.0) / 2.0);
1130 log_wr("Bin5 constructor end");
1134 function get_user($sess, &$idx)
1138 if (validate_sess($sess)) {
1139 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1140 if ($this->user[$i]->is_empty())
1142 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1145 $ret = &$this->user[$i];
1149 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1150 // for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++)
1151 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1154 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1159 function banned_kickoff()
1163 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1164 $user_cur = $this->user[$i];
1166 // check if the IP is blacklisted
1167 if ($this->brisk->black_check($user_cur->ip)) {
1168 $user_cur->lacc = 0;
1173 // if authorized not check if banlisted
1174 if ($user_cur->is_auth()) {
1178 if ($this->brisk->ban_check($user_cur->ip)) {
1179 $user_cur->lacc = 0;
1186 function garbage_manager($force)
1190 /* Garbage collector degli utenti in timeout */
1194 $delta = $this->delay_mgr->delta_get($curtime);
1196 if ($force || $this->garbage_timeout < $curtime) {
1197 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1198 $user_cur = $this->user[$i];
1199 if ($user_cur->is_active() == FALSE || // is not active user or
1200 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
1203 if ($user_cur->lacc + EXPIRE_TIME_RD < ($curtime - $delta)) { // Auto logout dell'utente
1204 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
1206 if ($user_cur->stat == 'table') {
1207 log_auth($user_cur->sess," bin5 Autologout session.");
1209 /* main garbage_manager is delegate as autologout management */
1210 $user_cur->the_end = TRUE;
1212 /* se gli altri utenti non erano d'accordo questo utente viene bannato */
1213 $remcalc = $this->table[0]->exitlock_calc($this->user, $user_cur->table_pos);
1215 require_once("${G_base}Obj/hardban.phh");
1216 Hardbans::add(($user_cur->is_auth() ? $user_cur->name : FALSE),
1217 $user_cur->ip, $user_cur->sess, $user_cur->laccwr + BAN_TIME);
1219 // $user->bantime = $user->laccwr + BAN_TIME;
1221 $this->table_wakeup($user_cur);
1225 log_rd2($user_cur->sess." GARBAGE UPDATED!");
1227 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
1232 $this->delay_mgr->lastcheck_set($curtime);
1237 function destroy_data()
1243 log_main("DESTROY BRISKIN5 DATA");
1246 log_main("DESTROY2 BRISKIN5 DATA [".$this->table_idx."]");
1247 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1248 $this->user[$i]->destroy_data($this->table_idx);
1249 Bin5_user::unproxy_step($this->table_idx, $this->user[$i]->sess);
1251 if (($tok = @ftok(FTOK_PATH."/bin5/table".$this->table_idx."/table", "B")) == -1)
1254 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
1257 if (shmop_delete($shm) == 0) {
1258 log_only("REMOVE FALLITA ");
1263 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
1265 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
1275 static function lock_data($is_exclusive, $table_idx)
1277 if (($res = file_lock(FTOK_PATH."/bin5/table".$table_idx."/table", $is_exclusive)) != FALSE) {
1278 self::$delta_t = microtime(TRUE);
1279 log_lock("LOCK table [".$table_idx."] [".self::$delta_t[$table_idx]."]");
1281 return (new Vect(array('res' => $res, 'tab' => $table_idx)));
1288 static function unlock_data($res_vect)
1292 $res = $res_vect->getbyid('res');
1293 $tab = $res_vect->getbyid('tab');
1295 log_lock("UNLOCK table [".$tab."] [".(microtime(TRUE) - (self::$delta_t[$tab]))."]");
1301 function chatt_send($user, $mesg, $mlang_indwr = NULL)
1303 GLOBAL $mlang_brisk, $G_lang;
1305 if ($user->stat != 'table') {
1310 $table = &$this->table[$user->table];
1311 $user_mesg = substr($mesg,6);
1316 $dt = date("H:i ", $curtime);
1317 if (strncmp($user_mesg, "/cont ", 6) == 0) {
1318 log_main($user->sess." chatt_send BEGIN");
1320 $match_id = substr($user_mesg, 6);
1321 $table->match_continue($this, $user, $match_id);
1323 else if (strncmp($user_mesg, "/rules ", 7) == 0) {
1324 log_main($user->sess." chatt_send BEGIN");
1326 $rules_id = substr($user_mesg, 7);
1327 $table->rules_change($this, $user, $rules_id);
1330 for ($i = 0 ; $i < ($user->stat == 'room' ? BIN5_MAX_PLAYERS : BIN5_PLAYERS_N) ; $i++) {
1331 if ($user->stat == 'room') {
1332 $user_cur = &$this->user[$i];
1333 if ($user_cur->is_active() == FALSE || $user_cur->stat != 'room') // is not active user or stat isn't 'room'
1337 $user_cur = &$this->user[$table->player[$i]];
1340 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1341 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
1342 $dt, $user->flags, xcape($user->name), xcape($user_mesg));
1343 $user_cur->step_inc();
1345 log_legal($curtime, $user->ip, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
1349 function table_wakeup($user)
1351 $table = &$this->table[0];
1353 log_main("BIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
1357 log_main("BIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1359 for ($i = 0 ; $i < $table->player_n ; $i++) {
1360 $user_cur = &$this->user[$i];
1361 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1363 if ($user_cur == $user)
1364 $user_cur->subst = "shutdowner";
1366 $user_cur->subst = "shutdowned";
1367 $user_cur->laccwr = $curtime;
1369 $ret = "gst.st = ".($user_cur->step+1)."; ";
1370 $ret .= 'gst.st_loc++; xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
1372 log_wr($user_cur->sess." BIN5_WAKEUP: ".$ret);
1373 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1374 $user_cur->step_inc();
1377 $this->the_end = TRUE;
1381 * If all players are freezed the room garbage_manager clean up table and users.
1383 function is_abandoned()
1388 $table = &$this->table[0];
1390 for ($i = 0 ; $i < $table->player_n ; $i++) {
1391 $user_cur = &$this->user[$i];
1393 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
1402 static function request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
1404 GLOBAL $G_ban_list, $G_black_list;
1406 printf("NEW_SOCKET (root): %d\n", intval($new_socket));
1408 $enc = get_encoding($header);
1409 if (isset($header['User-Agent'])) {
1410 if (strstr($header['User-Agent'], "MSIE")) {
1411 $transp_type = "htmlfile";
1414 $transp_type = "xhr";
1418 $transp_type = "iframe";
1420 force_no_cache($header_out);
1422 if (($table_idx = gpcs_var('table_idx', $get, $post, $cookie)) === FALSE)
1425 if (($table_token = gpcs_var('table_token', $get, $post, $cookie)) === FALSE)
1426 unset($table_token);
1433 bin5_index_main($transp_type, $header, $header_out, $addr, $get, $post, $cookie);
1434 $content = ob_get_contents();
1437 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1441 case "index_wr.php":
1442 if (isset($table_idx) && isset($table_token)) {
1443 if (($bri = $s_a_p->app->match_get($table_idx, $table_token)) != FALSE) {
1445 bin5_index_wr_main($bri, $addr, $get, $post, $cookie);
1446 $content = ob_get_contents();
1450 $content = "Bin5 Load data error";
1454 $content = "Bin5 Load data error";
1456 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1460 case "index_rd.php":
1461 case "index_rd_wss.php":
1462 if (($transp = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
1464 if ($transp == 'websocket' || $transp == 'websocketsec')
1468 if (!isset($table_idx)
1469 || !isset($table_token)
1470 || !isset($cookie['sess'])
1471 || ($bri = $s_a_p->app->match_get($table_idx, $table_token)) == NULL
1472 || (($user = $bri->get_user($cookie['sess'], $idx)) == FALSE)) {
1474 $content = Bin5_user::stream_fini($transp_type, $s_a_p->rndstr, TRUE);
1475 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1480 $bri->brisk->sess_cur_set($user->sess);
1482 // close a previous opened index_read_ifra socket, if exists
1483 if (($prev = $user->rd_socket_get()) != NULL) {
1484 $s_a_p->socks_unset($user->rd_socket_get());
1485 fclose($user->rd_socket_get());
1486 printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
1487 $user->rd_socket_set(NULL);
1491 $user->stream_init($s_a_p->rndstr, $enc, $header, $header_out, $content, $get, $post, $cookie);
1493 $response = headers_render($header_out, -1).$user->chunked_content($content);
1494 $response_l = mb_strlen($response, "ASCII");
1496 $wret = @fwrite($new_socket, $response, $response_l);
1497 if ($wret < $response_l) {
1498 printf("TROUBLES WITH FWRITE: %d\n", $wret);
1499 $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
1502 $user->rd_cache_set("");
1504 fflush($new_socket);
1507 $s_a_p->socks_set($new_socket, $user, NULL);
1508 $user->rd_socket_set($new_socket);
1509 printf(" - qui ci siamo - ");
1528 function locshm_exists($tok)
1532 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
1533 log_main($tok." SHM NOT exists");
1539 log_main($tok." SHM exists");
1548 is_transition (is from room to table ?)
1549 is_again (is another game)
1551 Examples of $is_transition, $is_again:
1552 from reload of the page: FALSE, FALSE
1553 from sitdown in room: TRUE, FALSE
1554 from table: asta cmd e tutti passano: TRUE, TRUE
1555 from table: fine partita: TRUE, TRUE
1557 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1559 $table_idx = $user->table;
1560 $table = $bri->table[$table_idx];
1561 $table_pos = $user->table_pos;
1563 $ret = "table_init();";
1564 $ret .= $table->exitlock_show($bri->user, $table_pos);
1566 /* GENERAL STATUS */
1567 $user_rules = $user->rules_get();
1568 $ret .= sprintf('gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d; rules_set(%d);',
1569 $sendstep, $user->stat, $user->subst, $table_pos,
1570 ($user_rules == BIN5_USER_CONTINUE_INIT ? $table->rules->id_get() : $user_rules));
1572 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1575 $ret .= "background_set();";
1578 $ret .= $user->myname_innerHTML();
1579 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1580 $bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->flags,
1581 xcape($bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->name),
1583 $bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->flags,
1584 xcape($bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->name),
1586 $bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->flags,
1587 xcape($bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->name),
1589 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->flags),
1590 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->name)),
1592 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->flags),
1593 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->name)));
1595 /* NOTIFY FOR THE CARD MAKER */
1596 if ($is_transition) { // && $user->subst == "asta" superfluo
1597 $ret .= show_table_info($bri, $table, $table_pos);
1598 $ret .= "setTimeout(preload_images, 500, g_preload_img_arr, g_imgct);";
1601 $ret .= "\$('imgct').innerHTML = \$('imgct').innerHTML = mlang_commons['imgload_a'][g_lang]+\"100%.\";";
1604 $ret .= table_welcome($user);
1606 if ($is_transition && !$is_again) { // just sit, play cow
1607 $ret .= playsound("cow.mp3");
1612 if ($is_transition) { // && $user->subst == "asta" superfluo
1615 for ($i = 0 ; $i < BIN5_CARD_HAND ; $i++) {
1616 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++) {
1618 for ($o = 0 ; $o < (BIN5_CARD_HAND * BIN5_PLAYERS_N) && $ct < $i+1 ; $o++) {
1619 // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1620 if ($table->card[$o]->owner == (($e + $table->gstart) % BIN5_PLAYERS_N)) {
1626 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1628 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BIN5_PLAYERS_N,
1629 $i, ((($e + BIN5_PLAYERS_N - $table_pos + $table->gstart) % BIN5_PLAYERS_N) == 0 ?
1630 $table->card[$o]->value : -1),
1631 ($i == 7 && $e == (BIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1636 $taked = array(0,0,0,0,0);
1637 $inhand = array(0,0,0,0,0);
1638 $ontabl = array(-1,-1,-1,-1,-1);
1641 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1642 // for ($i = 0 ; $i < 40 ; $i++) {
1643 if ($table->card[$i]->stat == 'hand') {
1644 if ($table->card[$i]->owner == $table_pos) {
1645 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1647 $inhand[$table->card[$i]->owner]++;
1649 else if ($table->card[$i]->stat == 'take') {
1650 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1651 $taked[$table->card[$i]->owner]++;
1653 else if ($table->card[$i]->stat == 'table') {
1654 $ontabl[$table->card[$i]->owner] = $i;
1658 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1659 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1661 log_main("Stat table: ".$logg);
1663 /* Set ours cards. */
1665 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1666 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1667 for ($i = $inhand[$table_pos] ; $i < BIN5_CARD_HAND ; $i++)
1668 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1669 $ret .= sprintf('card_setours(%s);', $oursarg);
1671 /* Dispose all cards */
1672 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1673 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1674 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1675 ($inhand[$i] <= BIN5_CARD_HAND ? $inhand[$i] : BIN5_CARD_HAND), $taked[$i]);
1677 if ($ontabl[$i] != -1) {
1678 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1679 $table->card[$ontabl[$i]]->value,
1680 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1686 if ($user->subst == 'asta') {
1688 /* show users auction status */
1690 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1691 $user_cur = &$bri->user[$table->player[$i]];
1692 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1693 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1695 if (BIN5_PLAYERS_N == 3)
1696 $showst .= ",-2,-2";
1697 $ret .= sprintf('document.title = "Brisk - Tavolo %d (asta)";', $user->table_orig);
1698 $ret .= sprintf('show_astat(%s);', $showst);
1700 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1701 /* show card chooser */
1702 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1706 // FIXME - RULES to be able to abandon table
1708 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N) &&
1709 $table->asta_win == -1)
1710 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1711 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1713 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1714 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1718 if ($table->asta_win == -1) { // auction case
1719 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N))
1720 $ret .= "remark_on();";
1722 $ret .= "remark_off();";
1724 else { // chooseed case
1725 if ($table_pos == $table->asta_win)
1726 $ret .= "remark_on();";
1728 $ret .= "remark_off();";
1731 else if ($user->subst == 'game') {
1733 if (($table->gstart + $table->turn) % BIN5_PLAYERS_N == $table_pos)
1734 $ret .= "is_my_time = true; remark_on();";
1736 $ret .= "remark_off();";
1738 /* WHO CALL AND WHAT */
1739 $ret .= briscola_show($bri, $table, $user);
1743 } // end function show_table(...
1745 function calculate_winner(&$table)
1752 $cur_seed = $table->briscola - ($table->briscola % 10);
1754 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1755 // for ($i = 0 ; $i < 40 ; $i++) {
1756 if ($table->card[$i]->stat != "table")
1759 log_wr(sprintf("Card On table: [%d]", $i));
1761 $v = $table->card[$i]->value;
1762 $ontab[$table->card[$i]->owner] = $v;
1763 $ontid[$table->card[$i]->owner] = $i;
1764 /* se briscola setto il flag */
1765 if (($v - ($v % 10)) == $cur_seed)
1769 if ($briontab == FALSE) {
1770 $cur_win = $table->gstart;
1771 $cur_val = $ontab[$cur_win];
1772 $cur_seed = $cur_val - ($cur_val % 10);
1775 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1776 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1777 if ($ontab[$i] < $cur_val) {
1778 $cur_val = $ontab[$i];
1784 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1785 $table->card[$ontid[$i]]->owner = $cur_win;
1786 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1791 function show_table_info(&$bri, &$table, $table_pos)
1793 GLOBAL $G_lang, $mlang_bin5_bin5;
1801 $user = $bri->user[$table->player[$table_pos]];
1803 // TAG: POINTS_MANAGEMENT
1804 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1805 $noty = sprintf('<p>%s.</p>\n', xcape(ucfirst(rules_id2descr($table->rules->id_get(), $G_lang))));
1806 $noty .= sprintf('<table class=\"points\"><tr><th></th>');
1809 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++)
1810 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1811 $noty .= sprintf("</tr>");
1814 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1816 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1817 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1818 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1819 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1824 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1825 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1826 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1827 $noty .= "</tr></table>";
1829 if ($table->old_reason != "") {
1830 $noty .= sprintf($mlang_bin5_bin5['info_last'][$G_lang],
1831 $tg_br, $tg_hr, $tg_bo, $tg_bc);
1833 $noty .= $table->old_reason;
1836 $noty .= sprintf($mlang_bin5_bin5['info_curr'][$G_lang],
1837 $tg_br, $tg_hr, $tg_bo, $tg_bc);
1839 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1840 if ($table->mazzo == $table_pos)
1841 $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
1843 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1844 $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
1847 if ($user->subst == 'asta') {
1848 if ($table->asta_win == -1) // auction case
1849 $curplayer = $table->gstart % BIN5_PLAYERS_N;
1851 $curplayer = $table->asta_win;
1853 else if ($user->subst == 'game') {
1854 $curplayer = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
1857 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1858 if ($curplayer == $table_pos) {
1859 $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
1862 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1863 $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
1866 $multer = $table->rules->multer(TRUE);
1868 $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($multer) );
1871 if ($table->match_id != -1) {
1872 $noty .= sprintf($mlang_bin5_bin5['info_match'][$G_lang], $table->match_id);
1875 $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
1876 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
1877 * 130 + ($table->points_n > 0 ? 50 : 0) +
1878 * (120 * ($table->points_n / MAX_POINTS)));
1879 * will be used when we refact notify js function following
1880 * photoo class logic
1886 function table_welcome($user)
1888 GLOBAL $table_wellarr, $G_lang;
1891 for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
1892 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
1898 function briscola_show($bri, $table, $user)
1900 GLOBAL $G_lang, $mlang_bin5_bin5;
1904 if ($table->asta_card == 9)
1905 $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
1907 /* text of caller cell */
1908 if ($user->table_pos == $table->asta_win) {
1909 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
1910 $ret .= sprintf($prestr, $ptnadd);
1913 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
1914 $ret .= sprintf($prestr,
1915 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1917 $ret .= sprintf('set_iscalling(%d);', ($table->asta_win - $user->table_pos + BIN5_PLAYERS_N) % BIN5_PLAYERS_N);
1919 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1921 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1922 $ret .= sprintf('$("caller").style.visibility = "visible";');
1923 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1924 $ret .= sprintf('$("astalascio").style.visibility = "";');
1925 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1926 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1931 function log_points($remote_addr, $curtime, $user, $where, $mesg)
1933 if (($fp = @fopen(LEGAL_PATH."/points.log", 'a')) != FALSE) {
1934 /* Unix time | session | nickname | IP | where was | mesg */
1935 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
1936 ($user->is_auth() ? 'A' : 'N'),
1937 $user->name, $remote_addr, $where , $mesg));