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 // FOR TORNEO TURN IT TO BIN5_TOURNAMENT_TOGETHER17
36 define('BIN5_TOURNAMENT_CURRENT', BIN5_TOURNAMENT_NO_DRAW);
38 $mlang_bin5_bin5 = array(
39 // br, hr, b, /b, win, fri
40 'info_last' => array( 'it' => '%3$sultima mano%4$s',
41 'en' => '%3$slast hand%4$s'),
42 'info_curr' => array( 'it' => '%3$smano corrente%4$s',
43 'en' => '%3$scurrent hand%4$s'),
44 'info_yshuf'=> array( 'it' => 'Fai <b>tu</b> il mazzo, ',
45 'en' => 'It\'s <b>your</b> shuffled the cards, '),
46 'info_shuf' => array( 'it' => 'Il mazzo a <b>%s</b>, ',
47 'en' => '<b>%s</b> shuffled the cards, '),
48 'info_yturn'=> array( 'it' => ' tocca a <b>te</b> giocare.',
49 'en' => ' it\'s <b>your</b> turn.'),
50 'info_turn' => array( 'it' => 'tocca a <b>%s</b> giocare.',
51 'en' => 'it\'s the <b>%s</b>\'s turn.'),
52 'info_mult' => array( 'it' => ' La partita vale <b>%s</b>.',
53 'en' => ' The game worth <b>%s</b>.' ),
54 'info_match'=> array( 'it' => 'Il codice della partita รจ <b>%d</b>.',
55 'en' => 'Match code is <b>%d</b>.'),
57 'btn_bkgame'=> array( 'it' => 'torna alla partita',
58 'en' => 'back to the game'),
59 'call_wptn' => array( 'it' => '<br>con %d punti',
60 'en' => '<br>with %d points'),
61 'call_ycall'=> array( 'it' => 'Chiami%s:',
63 'call_call' => array( 'it' => 'Chiama %s%s:',
64 'en' => 'The declarer is %s%s:')
68 $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.'),
69 '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.') );
72 // FIXME - DYNAMIC WITH CACHE AND RELOAD FROM DB
75 return array('xx', 'yy');
78 function deck_id2descr($id, $lang)
81 return 'carte originali';
83 return 'carte strette';
85 return 'carte sconosciute';
88 function multoval($mult)
93 return ($G_lang == 'en' ? 'double' : 'doppio');
95 return ($G_lang == 'en' ? 'triple' : 'triplo');
97 return ($G_lang == 'en' ? 'quadruple' : 'quadruplo');
99 return (sprintf(($G_lang == 'en' ? "%d-ple" : "%d-plo"), $mult));
102 function dom_select_rules()
106 $ret = "<select id='select_rules'>\n";
107 foreach (rules_keys() as $key) {
108 $value = rules_id2descr($key, $G_lang);
109 $ret .= sprintf("<option value='%d'%s>%s</option>\n", $key, ($key == BIN5_TOURNAMENT_CURRENT ? " selected='selected'" : ""), $value);
111 $ret .= "</select>\n";
116 function dom_select_deck($cur_sel)
120 $ret = "<select id='select_deck'>\n";
121 foreach (deck_keys() as $key) {
122 $value = deck_id2descr($key, $G_lang);
123 $ret .= sprintf("<option value='%s'%s>%s</option>\n", $key, ($key == $cur_sel ? " selected='selected'" : ""), $value);
125 $ret .= "</select>\n";
131 var $value; /* 0 - 39 card value */
132 var $stat; /* 'bunch', 'hand', 'table', 'take' */
133 var $owner; /* (table position 0-4) */
134 // var $pos; /* Pos in hand. */
135 var $x; /* When played the X position on the table of the owner. */
136 var $y; /* When played the Y position on the table of the owner. */
138 function Card($value, $stat, $owner)
140 $this->value = $value;
141 $this->stat = $stat; // Card stat
142 $this->owner = $owner;
145 function assign($stat,$owner)
147 $this->stat = $stat; // Card stat
148 $this->owner = $owner;
151 function setpos($pos)
158 $this->stat = 'table'; // Card stat
163 function take($newown)
165 $this->stat = 'take'; // Card stat
166 $this->owner = $newown;
170 class Bin5_table extends Table {
171 var $card; // il mazzo di carte
172 var $mazzo; // chi e' di mazzo
173 var $gstart; // first player of the current game
174 var $turn; // turn in the game (absolute, not modularized)
176 var $asta_pla; // array(); TRUE: in auction, FALSE: out of the auction
177 var $asta_pla_n; // number of players in auction
178 var $asta_card; // current card for auction
179 var $asta_pnt; // current point for auction
182 var $points; // points array
183 var $points_n; // number of row of points
186 var $asta_win; // the caller idx position at table
188 var $tourn_pts; // points in the caller hand
189 var $friend; // the callee idx position at table
191 var $match_id; // the id of the match on the database (-1 == just not saved)
193 var $old_act; // last action that trigs the end of the game
198 var $old_pnt; // points made by caller and callee
199 var $old_asta_win; // the old caller idx position at table
200 var $old_friend; // the old callee idx position at table
202 var $old_tourn_pts; // the old tournment computed points in the hand of caller
205 function Bin5_table()
211 function create($idx)
213 if (($thiz =& new Bin5_table()) == FALSE)
219 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
220 $thiz->asta_pla_n= -1;
221 $thiz->asta_card = -1;
222 $thiz->asta_pnt = -1;
225 $thiz->points = array( );
227 $thiz->total = array( 0, 0, 0, 0, 0);
228 $thiz->asta_win = -1;
229 $thiz->briscola = -1;
230 $thiz->tourn_pts = -1;
234 $thiz->match_id = -1;
237 $thiz->old_mazzo = -1;
238 $thiz->old_reason = "";
239 $thiz->old_asta_pnt = -1;
240 $thiz->old_mult = -1;
242 $thiz->old_asta_win = -1;
243 $thiz->old_friend = -1;
250 function myclone(&$from)
252 if (($thiz =& new Bin5_table()) == FALSE)
257 $thiz->card = $from->card;
258 $thiz->mazzo = $from->mazzo; // REVIEW
259 $thiz->gstart = $from->gstart;
260 $thiz->turn = $from->turn;
262 $thiz->asta_pla = $from->asta_pla;
263 $thiz->asta_pla_n = $from->asta_pla_n;
264 $thiz->asta_card = $from->asta_card;
265 $thiz->asta_pnt = $from->asta_pnt;
267 $thiz->mult = $from->mult;
268 $thiz->points = $from->points;
269 $thiz->points_n = $from->points_n;
270 $thiz->total = $from->total;
272 $thiz->asta_win = $from->asta_win;
273 $thiz->briscola = $from->briscola;
274 $thiz->tourn_pts = $from->tourn_pts;
275 $thiz->friend = $from->friend;
277 $thiz->match_id = $from->match_id;
279 $thiz->old_act = $from->old_act;
280 $thiz->old_mazzo = $from->old_mazzo;
281 $thiz->old_reason = $from->old_reason;
282 $thiz->old_asta_pnt = $from->old_asta_pnt;
283 $thiz->old_mult = $from->mult;
284 $thiz->old_pnt = $from->old_pnt;
285 $thiz->old_asta_win = $from->old_asta_win;
286 $thiz->old_friend = $from->old_friend;
292 function parentcopy(&$from)
297 function spawn(&$from)
300 if (($thiz =& new Bin5_table()) == FALSE)
303 $thiz->parentcopy($from);
305 log_main("PLAYER_N - spawn.".$thiz->player_n);
307 $thiz->card = array();
308 $thiz->bunch_create();
309 $thiz->mazzo = rand(0,PLAYERS_N-1);
310 $thiz->points = array();
311 $thiz->total = array();
315 $thiz->match_id = -1;
317 $thiz->old_asta_win = -1;
318 $thiz->old_reason = "";
320 $rules_name = rules_id2name(BIN5_TOURNAMENT_CURRENT);
321 $thiz->rules = new $rules_name($thiz);
322 // players are rearranged in an dedicated array
323 $thiz->player = array();
324 for ($i = 0 ; $i < $from->player_n ; $i++)
325 $thiz->player[$i] = $i;
327 log_main("TABLE_OLD_WIN - spawn:".$thiz->old_asta_win);
333 // function bunch_create_old() function AND
337 // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
338 // // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
339 // $ret[$i] =& new Card($i, 'bunch', 'no_owner');
346 function bunch_create()
350 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
351 $this->card[$i] = new Card($i, 'bunch', 'no_owner');
355 function bunch_make()
357 log_main("bunch_make start");
358 $ct = array(0,0,0,0,0);
360 mt_srand(make_seed());
362 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--)
365 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--) {
369 log_main("RND ZERO");
373 $owner = $i % BIN5_PLAYERS_N;
374 $this->card[$id]->assign('hand', $owner);
376 $rest[$rn] = $rest[$i];
377 // $pubbpos[$rn2] = $pubbpos[$i];
379 log_main("bunch_make end");
382 function init($userarr)
385 $this->mazzo = rand(0,PLAYERS_N-1);
388 $this->old_asta_win = -1;
389 $this->old_reason = "";
391 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
392 $this->total[$i] = 0;
393 $user_cur = $userarr[$this->player[$i]];
394 $user_cur->stat_set('table');
395 $user_cur->exitislock = TRUE;
398 log_main("table::init: ci siamo");
401 function game_init($userarr)
405 $this->gstart = ($this->mazzo+1) % BIN5_PLAYERS_N;
408 $this->asta_pla_n = BIN5_PLAYERS_N;
409 $this->asta_card = -1;
410 $this->asta_pnt = 60;
411 $this->asta_win = -1;
412 $this->briscola = -1;
413 $this->tourn_pts = -1;
417 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
418 $this->asta_pla[$i] = TRUE;
419 $user_cur = $userarr[$this->player[$i]];
420 $user_cur->subst = 'asta';
421 $user_cur->asta_card = -2;
422 $user_cur->asta_pnt = -1;
423 $user_cur->handpt = $this->hand_points($i);
424 $this->rules->tourn_points($user_cur, $i);
429 function game_next($delta)
431 $this->old_mazzo = $this->mazzo;
432 $this->mazzo = ($this->mazzo + $delta) % BIN5_PLAYERS_N;
435 function mult_inc($val)
437 $this->old_mult = $this->mult;
441 function mult_set($val)
443 $this->old_mult = $this->mult;
447 function hand_points($idx)
449 GLOBAL $G_all_points;
453 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
454 // for ($i = 0 ; $i < 40 ; $i++) {
455 if ($this->card[$i]->owner != $idx)
458 $ctt = $this->card[$i]->value % 10;
459 $tot += $G_all_points[$ctt];
466 function exitlock_show($userarr, $table_pos)
468 $ct = $this->exitlock_calc($userarr, $table_pos);
470 $ret = sprintf('exitlock_show(%d, %s);', $ct,
471 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
475 function exitlock_calc(&$userarr, $table_pos)
479 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
480 if ($userarr[$this->player[$i]]->exitislock == FALSE)
487 function match_continue(&$bri, $user, $match_id_s)
490 // Rules: update version following loaded tcode
496 if ($this->match_id != -1) {
497 $msg = sprintf("Stai giร giocando la partita con codice %d.", $this->match_id);
501 /* - verify if match_id and user are both valid to accept
502 the match_continue request - */
503 $match_id = (int)$match_id_s;
504 if ($match_id <= 0) {
505 $msg = "questa partita non esiste";
509 if ($user->continue_get() == $match_id) {
510 $msg = "Hai giร richiesto di continuare questa partita.";
513 // retrieves users list for this match
514 $match_data = array();
515 if (($bdb = BriskDB::create()) != FALSE) {
516 // match_order_get return FALSE for old matches
517 $ucodes = $bdb->match_order_get($match_data, $match_id, BIN5_PLAYERS_N);
520 if ($ucodes == FALSE) {
521 $msg = "questa partita non รจ stata memorizzata correttamente";
525 // if current user code must be in the users list
526 if (array_search($user->code, $ucodes) === FALSE) {
527 $msg = sprintf("Questo utente non compare nella partita che si vuole continuare [%d].", $user->code);
531 /* - the user is in the list, add the match_id to his profile
532 and check if he is the N'th to require continue - */
535 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
536 $user_cur = &$bri->user[$this->player[$i]];
538 if ($user == $user_cur) {
543 if ($user_cur->continue_get() == BIN5_USER_CONTINUE_INIT)
546 if ($user_cur->continue_get() != $match_id)
550 if ($i < BIN5_PLAYERS_N) {
551 $msg = sprintf("<b>L'utente <i>%s</i> voleva giร continuare la partita nยฐ %d.</b>",
552 xcape($user_cur->name), $user_cur->continue_get());
556 // set the match_id for the current user
557 $user->continue_set($match_id);
560 // not all players set the continue match than we exit
561 if ($continue_tot < BIN5_PLAYERS_N) {
562 $msg = sprintf("<b>L'utente <i>%s</i> vorrebbe continuare la partita nยฐ %d.</b>",
563 xcape($user->name), $match_id);
567 /* - all users decide to continue the same match, update all infos and rearrange users
568 to the right positions on the table - */
570 /* reset users table order */
571 for ($i = 0 ; $i < BIN5_PLAYERS_N - 1 ; $i++) {
572 if ($bri->user[$this->player[$i]]->code == $ucodes[$i]) {
575 for ($e = $i + 1 ; $e < BIN5_PLAYERS_N ; $e++) {
576 if ($bri->user[$this->player[$e]]->code == $ucodes[$i]) {
577 $swap = $this->player[$i];
578 $this->player[$i] = $this->player[$e];
579 $this->player[$e] = $swap;
580 $bri->user[$this->player[$i]]->table_pos = $i;
581 $bri->user[$this->player[$e]]->table_pos = $e;
585 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
586 fprintf(STDERR, "USERZ: [%s]\n", $bri->user[$this->player[$i]]->name);
589 // update database info to be aligned with current table (ttok and table_idx
590 if (($bdb = BriskDB::create()) != FALSE) {
591 if ($bdb->match_continue($match_id, $this, $user->table_orig) == FALSE) {
593 $msg = "aggiornamento dei dati della partita fallito";
599 /* update rules engine */
600 $rules_name = rules_id2name($match_data['tcode']);
601 $this->rules = new $rules_name($this);
603 /* bunch and multiplier status set */
604 $this->mazzo = $match_data['mazzo_next'];
605 $this->mult = $match_data['mult_next'];
606 $this->match_id = $match_id;
607 $this->game_init($bri->user);
609 /* reload of the page with the new layout */
610 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
611 $user_cur = &$bri->user[$this->player[$i]];
613 $user_cur->trans_step = $user_cur->step + 1;
614 $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);
615 $user_cur->step_inc();
617 // a void command force xynt-streamer to flush all data to client
618 $user_cur->trans_step = $user_cur->step + 1;
619 $user_cur->comm[$user_cur->step % COMM_N] = "";
620 $user_cur->step_inc();
622 $user_cur->comm[$user_cur->step % COMM_N] = show_table($bri, $user_cur, $user_cur->step+1, TRUE, FALSE);
623 $user_cur->step_inc();
628 $dt = date("H:i ", $curtime);
629 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
630 if ($ret == FALSE && $this->player[$i] != $user->idx)
632 $user_cur = &$bri->user[$this->player[$i]];
633 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
634 $user_cur->comm[$user_cur->step % COMM_N] .= nickserv_msg($dt, $msg);
635 $user_cur->step_inc();
639 function rules_change(&$bri, $user, $rules_id_s)
644 // Rules: update version following loaded tcode
650 if ($this->match_id != -1) {
652 $msg = sprintf("Stai giร giocando la partita con codice %d.", $this->match_id);
656 if ($this->asta_card != -1 || $this->asta_pla_n < BIN5_PLAYERS_N) {
658 $msg = sprintf("La partita รจ giร stata avviata.");
662 $rules_id = (int)$rules_id_s;
663 if (! in_array($rules_id, rules_keys())) {
665 $msg = "queste regole non esistono o non sono attive";
669 if ($user->rules_get() == $rules_id) {
671 $msg = "hai giร richiesto di usare queste regole";
675 /* - the user is in the list, add the match_id to his profile
676 and check if he is the N'th to require continue - */
679 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
680 $user_cur = &$bri->user[$this->player[$i]];
682 if ($user == $user_cur) {
687 if ($user_cur->rules_get() == BIN5_USER_CONTINUE_INIT)
690 if ($user_cur->rules_get() != $rules_id)
694 if ($i < BIN5_PLAYERS_N) {
696 $msg = sprintf("<b>L'utente <i>%s</i> voleva giร usare le %s.</b>",
697 xcape($user_cur->name), xcape(rules_id2descr($user_cur->rules_get(), $G_lang)));
701 // set the match_id for the current user
702 $user->rules_set($rules_id);
705 // not all players set the continue match than we exit
706 if ($rules_tot < BIN5_PLAYERS_N) {
708 $msg = sprintf("<b>L'utente <i>%s</i> vorrebbe usare le %s.</b>",
709 xcape($user->name), xcape(rules_id2descr($rules_id, $G_lang)));
713 /* - all users decide to continue the same match, update all infos and rearrange users
714 to the right positions on the table - */
716 /* update rules engine */
717 $rules_name = rules_id2name($rules_id);
718 $this->rules = new $rules_name($this);
720 // $this->game_init($bri->user);
722 /* reload of the page with the new layout */
723 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
724 $user_cur = &$bri->user[$this->player[$i]];
725 $this->rules->tourn_points($user_cur, $i);
727 $user_cur->rules_set(BIN5_USER_CONTINUE_INIT);
728 $user_cur->trans_step = $user_cur->step + 1;
729 $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);
730 $user_cur->step_inc();
732 // a void command force xynt-streamer to flush all data to client
733 $user_cur->trans_step = $user_cur->step + 1;
734 $user_cur->comm[$user_cur->step % COMM_N] = "";
735 $user_cur->step_inc();
737 $user_cur->comm[$user_cur->step % COMM_N] = show_table($bri, $user_cur, $user_cur->step+1, TRUE, FALSE);
738 $user_cur->step_inc();
743 $dt = date("H:i ", $curtime);
745 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
746 $user_cur = &$bri->user[$this->player[$i]];
747 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
748 $user_cur->comm[$user_cur->step % COMM_N] .= nickserv_msg($dt, $msg);
749 $user_cur->step_inc();
753 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
754 $user->comm[$user->step % COMM_N] = xcape(sprintf("rules_set(%d);",
756 $user->comm[$user->step % COMM_N] .= nickserv_msg($dt, $msg);
759 } // end function rules_change
760 } // end class Bin5_table
765 define('BIN5_USER_FLAG_RING_ENDAUCT', 0x01);
766 define('BIN5_USER_CONTINUE_INIT', -1);
768 define('BIN5_USER_RULES_INIT', -1);
769 class Bin5_user extends User {
772 var $handpt; // Total card points at the beginning of the current hand.
773 var $exitislock; // Player can exit from the table ?
774 var $privflags; // Flags for briskin5 only
776 var $continue; // Id of the match that the user would continue
777 var $rules; // Id of rules required by user
779 var $asta_tourn_pts; // array with tournment points for each suit
787 function create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
788 if (($thiz =& new User()) == FALSE)
791 $thiz->asta_card = -2;
792 $thiz->asta_pnt = -1;
794 $thiz->exitislock = TRUE;
795 $thiz->privflags = 0;
796 $thiz->continue = BIN5_USER_CONTINUE_INIT;
797 $thiz->rules = BIN5_USER_RULES_INIT;
803 function parentcopy(&$from)
808 function copy(&$from)
810 $this->parentcopy($from);
812 $this->asta_card = $from->asta_card;
813 $this->asta_pnt = $from->asta_pnt;
814 $this->handpt = $from->handpt;
815 $this->exitislock = $from->exitislock;
816 $this->privflags = $from->privflags;
817 $this->continue = $from->continue;
818 $this->rules = $from->rules;
822 function myclone(&$from)
824 if (($thiz =& new User()) == FALSE)
833 static function spawn($from, &$bri, $table, $table_pos, $get, $post, $cookie)
835 if (($thiz = new Bin5_user()) == FALSE)
838 if (($CO_bin5_pref_ring_endauct = gpcs_var("CO_bin5_pref_ring_endauct", $get, $post, $cookie)) === FALSE) {
839 $CO_bin5_pref_ring_endauct = "";
842 $thiz->parentcopy($from);
844 /* NOTE: at this moment idx and table_pos fields have the same value
845 but diffentent functions, we keep them separated for a while */
847 $thiz->idx = $table_pos; // it is the position in the mini-room,
848 // not related to table pos (see below)
849 $thiz->asta_card = -2;
850 $thiz->asta_pnt = -1;
852 $thiz->exitislock = TRUE;
853 $thiz->continue = BIN5_USER_CONTINUE_INIT;
854 $thiz->rules = BIN5_USER_RULES_INIT;
856 log_wr("Bin5 constructor");
858 $thiz->privflags = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
860 $thiz->table_orig = $table;
862 $thiz->table_pos = $table_pos;
869 function step_set($step)
871 $this->step = $step & 0x7fffffff;
876 function step_inc($delta = 1) {
877 $this->step += $delta;
878 /* modularization because unpack() not manage unsigned 32bit int correctly */
879 $this->step &= 0x7fffffff;
884 static function load_step($tab_id, $sess)
888 if (validate_sess($sess) == FALSE)
891 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
892 mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
893 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step", 'rb')) == FALSE)
895 if (($s = fread($fp, 8)) == FALSE)
897 if (mb_strlen($s, "ASCII") != 8)
899 $arr = unpack('Ls/Li', $s);
902 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
909 log_rd2("STEP_GET [".$sess."]: return false ");
917 if (validate_sess($this->sess) == FALSE)
919 if (file_exists(BIN5_PROXY_PATH."/table".$this->table_orig) == FALSE)
920 mkdir(BIN5_PROXY_PATH."/table".$this->table_orig, 0775, TRUE);
921 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$this->table_orig."/".$this->sess.".step", 'w')) == FALSE)
923 fwrite($fp, pack("LL",$this->step, $this->idx));
926 log_main("step_set [".$this->sess. "] [".$this->step."]");
934 static function unproxy_step($tab_id, $sess)
936 log_rd2("UNPROXY: ".BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
937 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
940 @unlink(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
943 function destroy_data($tab_id)
946 if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos, "B")) == -1) {
947 log_crit("BIN5 USER DATA REMOVE FAILED 1 [".FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos."]");
951 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
952 log_crit("BIN5 USER DATA REMOVE FAILED 2");
955 if (shmop_delete($shm) == 0) {
956 log_crit("BIN5 USER DATA REMOVE FAILED 3");
961 log_main("BIN5 USER DATA DESTROY SUCCESS");
963 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
973 static function blocking_error($is_unrecoverable)
975 log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
976 return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
979 protected function page_sync($sess, $page)
981 log_rd2("PAGE_SYNC");
982 // printf("xXx BIN5_USER::PAGE_SYNC\n");
983 return (sprintf('xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
986 protected function maincheck($get, $post, $cookie)
989 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
990 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
994 log_rd("maincheck begin");
999 /* Nothing changed, return. */
1000 if ($this->rd_step == $this->step)
1003 log_rd2("do other cur_stat[".$this->rd_stat."] user->stat[".$this->stat."] cur_step[".$this->rd_step."] user_step[".$this->step."]");
1005 if ($this->rd_step == -1) {
1007 * if $this->rd_step == -1 load the current state from the main struct
1010 $S_load_stat['wR_minusone']++;
1012 // if ($this->the_end == TRUE) {
1013 // log_rd2("main_check: the end".var_export(debug_backtrace()));
1016 if ($this->trans_step != -1) {
1017 log_rd2("TRANS USATO ".$this->trans_step);
1018 $this->rd_step = $this->trans_step;
1019 $this->trans_step = -1;
1022 log_rd2("TRANS NON ATTIVATO");
1027 /* this part I suppose is read only on $this->room structure */
1028 if ($this->rd_step == -1) {
1029 log_rd2("PRE-NEWSTAT");
1036 if ($this->stat == 'table') {
1038 /* NOTE: $this->room is associated with the current $bri object */
1039 // printf("xXx CLASS NAME [%s]\n", get_class($this->room));
1040 $ret = show_table($this->room, $this, $this->step, FALSE, FALSE);
1042 log_rd2("NEWSTAT: ".$this->stat);
1044 $this->rd_stat = $this->stat;
1045 $this->rd_subst = $this->subst;
1046 $this->rd_step = $this->step;
1047 } /* if ($this->rd_step == -1) { */
1049 $S_load_stat['rU_heavy']++;
1051 if ($this->rd_step < $this->step) {
1053 if ($this->rd_step + COMM_N < $this->step) {
1054 if ($this->rd_stat != $this->stat) {
1055 $to_stat = $this->stat;
1057 // printf("xXx BIN5_USER::MAINCHECK\n");
1058 return ($this->page_sync($this->sess, ($to_stat == "table" ? "index.php" : "../index.php"), $this->table, $this->table_token));
1060 log_rd2("lost history, refresh from scratch");
1061 // printf("xXx LOST HISTORY!\n");
1062 $this->rd_step = -1;
1065 for ($i = $this->rd_step ; $i < $this->step ; $i++) {
1067 if ($this->comm[$ii] == "") {
1068 if ($i == $this->rd_step)
1073 log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
1074 $ret .= $this->comm[$ii];
1076 $this->rd_stat = $this->stat;
1077 $this->rd_subst = $this->subst;
1078 $this->rd_step = $this->step;
1081 log_rd2($this->step, 'index_rd.php: after ret set');
1083 // if ($this->the_end == TRUE) { management is moved
1084 // in the spush scope
1085 } /* if ($this->rd_step < $this->step) { */
1086 } /* else of if ($this->rd_step == -1) { */
1090 } // function maincheck(...
1092 function continue_set($match_code)
1094 $this->continue = $match_code;
1097 function continue_get() {
1098 return ($this->continue);
1101 function rules_set($rules_id)
1103 $this->rules = $rules_id;
1106 function rules_get() {
1107 return ($this->rules);
1110 } // end class Bin5_user
1114 static $delta_t = array();
1115 var $brisk;// room object reference
1119 var $comm; // commands for many people
1120 var $step; // current step of the comm array
1121 var $garbage_timeout;
1132 function Bin5($brisk, $table_idx, $table_token, $get, $post, $cookie) {
1133 $this->user = array();
1134 $this->table = array();
1136 $this->the_end = FALSE;
1137 $this->shm_sz = BIN5_SHM_MIN;
1138 if (($this->tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
1143 $this->brisk = $brisk;
1144 $user = $brisk->user;
1145 $table = $brisk->table[$table_idx];
1146 log_wr("Bin5 constructor");
1148 for ($i = 0 ; $i < $table->player_n ; $i++) {
1149 $user[$table->player[$i]]->table_token = $table_token;
1150 $this->user[$i] = Bin5_user::spawn($user[$table->player[$i]], $this, $table_idx, $i, $get, $post, $cookie);
1152 $this->table[0] = Bin5_table::spawn($table);
1154 log_main("TABLE_OLD_WIN - Bin5:".$this->table[0]->old_asta_win);
1156 $this->table_idx = $table_idx;
1157 $this->table_token = $table_token;
1158 $this->garbage_timeout = 0;
1160 $this->delay_mgr = new Delay_Manager((GARBAGE_TIMEOUT *3.0) / 2.0);
1162 log_wr("Bin5 constructor end");
1166 function get_user($sess, &$idx)
1170 if (validate_sess($sess)) {
1171 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1172 if ($this->user[$i]->is_empty())
1174 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1177 $ret = &$this->user[$i];
1181 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1182 // for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++)
1183 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1186 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1191 function banned_kickoff()
1195 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1196 $user_cur = $this->user[$i];
1198 // check if the IP is blacklisted
1199 if ($this->brisk->black_check($user_cur->ip)) {
1200 $user_cur->lacc = 0;
1205 // if authorized not check if banlisted
1206 if ($user_cur->is_auth()) {
1210 if ($this->brisk->ban_check($user_cur->ip)) {
1211 $user_cur->lacc = 0;
1218 function garbage_manager($force)
1222 /* Garbage collector degli utenti in timeout */
1226 $delta = $this->delay_mgr->delta_get($curtime);
1228 if ($force || $this->garbage_timeout < $curtime) {
1229 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1230 $user_cur = $this->user[$i];
1231 if ($user_cur->is_active() == FALSE || // is not active user or
1232 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
1235 if ($user_cur->lacc + (($user_cur->ping_req ? 1.5 : 1.0) * EXPIRE_TIME_RD) < ($curtime - $delta)) { // Auto logout dell'utente
1236 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
1238 if ($user_cur->stat == 'table') {
1239 log_auth($user_cur->sess," bin5 Autologout session.");
1241 /* main garbage_manager is delegate as autologout management */
1242 $user_cur->the_end = TRUE;
1244 /* se gli altri utenti non erano d'accordo questo utente viene bannato */
1245 $remcalc = $this->table[0]->exitlock_calc($this->user, $user_cur->table_pos);
1247 require_once("${G_base}Obj/hardban.phh");
1248 Hardbans::add(($user_cur->is_auth() ? $user_cur->name : FALSE),
1249 $user_cur->ip, $user_cur->sess, $user_cur->laccwr + BAN_TIME);
1251 // $user->bantime = $user->laccwr + BAN_TIME;
1253 $this->table_wakeup($user_cur);
1257 log_rd2($user_cur->sess." GARBAGE UPDATED!");
1259 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
1264 $this->delay_mgr->lastcheck_set($curtime);
1269 function destroy_data()
1275 log_main("DESTROY BRISKIN5 DATA");
1278 log_main("DESTROY2 BRISKIN5 DATA [".$this->table_idx."]");
1279 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1280 $this->user[$i]->destroy_data($this->table_idx);
1281 Bin5_user::unproxy_step($this->table_idx, $this->user[$i]->sess);
1283 if (($tok = @ftok(FTOK_PATH."/bin5/table".$this->table_idx."/table", "B")) == -1)
1286 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
1289 if (shmop_delete($shm) == 0) {
1290 log_only("REMOVE FALLITA ");
1295 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
1297 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
1307 static function lock_data($is_exclusive, $table_idx)
1309 if (($res = file_lock(FTOK_PATH."/bin5/table".$table_idx."/table", $is_exclusive)) != FALSE) {
1310 self::$delta_t = microtime(TRUE);
1311 log_lock("LOCK table [".$table_idx."] [".self::$delta_t[$table_idx]."]");
1313 return (new Vect(array('res' => $res, 'tab' => $table_idx)));
1320 static function unlock_data($res_vect)
1324 $res = $res_vect->getbyid('res');
1325 $tab = $res_vect->getbyid('tab');
1327 log_lock("UNLOCK table [".$tab."] [".(microtime(TRUE) - (self::$delta_t[$tab]))."]");
1333 function chatt_send($user, $mesg, $mlang_indwr = NULL)
1335 GLOBAL $mlang_brisk, $G_lang;
1337 if ($user->stat != 'table') {
1342 $table = &$this->table[$user->table];
1343 $user_mesg = substr($mesg,6);
1348 $dt = date("H:i ", $curtime);
1349 if (strncmp($user_mesg, "/cont ", 6) == 0) {
1350 log_main($user->sess." chatt_send BEGIN");
1352 $match_id = substr($user_mesg, 6);
1353 $table->match_continue($this, $user, $match_id);
1355 else if (strncmp($user_mesg, "/rules ", 7) == 0) {
1356 log_main($user->sess." chatt_send BEGIN");
1358 $rules_id = substr($user_mesg, 7);
1359 $table->rules_change($this, $user, $rules_id);
1362 for ($i = 0 ; $i < ($user->stat == 'room' ? BIN5_MAX_PLAYERS : BIN5_PLAYERS_N) ; $i++) {
1363 if ($user->stat == 'room') {
1364 $user_cur = &$this->user[$i];
1365 if ($user_cur->is_active() == FALSE || $user_cur->stat != 'room') // is not active user or stat isn't 'room'
1369 $user_cur = &$this->user[$table->player[$i]];
1372 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1373 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
1374 $dt, $user->flags, xcape($user->name), xcape($user_mesg));
1375 $user_cur->step_inc();
1377 log_legal($curtime, $user->ip, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
1381 function table_wakeup($user)
1383 $table = &$this->table[0];
1385 log_main("BIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
1389 log_main("BIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1391 for ($i = 0 ; $i < $table->player_n ; $i++) {
1392 $user_cur = &$this->user[$i];
1393 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1395 if ($user_cur == $user)
1396 $user_cur->subst = "shutdowner";
1398 $user_cur->subst = "shutdowned";
1399 $user_cur->laccwr = $curtime;
1401 $ret = "gst.st = ".($user_cur->step+1)."; ";
1402 $ret .= 'gst.st_loc++; xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
1404 log_wr($user_cur->sess." BIN5_WAKEUP: ".$ret);
1405 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1406 $user_cur->step_inc();
1409 $this->the_end = TRUE;
1413 * If all players are freezed the room garbage_manager clean up table and users.
1415 function is_abandoned()
1420 $table = &$this->table[0];
1422 for ($i = 0 ; $i < $table->player_n ; $i++) {
1423 $user_cur = &$this->user[$i];
1425 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
1434 static function request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
1436 GLOBAL $G_ban_list, $G_black_list;
1438 // printf("NEW_SOCKET (root): %d\n", intval($new_socket));
1440 $enc = get_encoding($header);
1441 if (isset($header['User-Agent'])) {
1442 if (strstr($header['User-Agent'], "MSIE")) {
1443 $transp_type = "htmlfile";
1446 $transp_type = "xhr";
1450 $transp_type = "iframe";
1452 force_no_cache($header_out);
1454 if (($table_idx = gpcs_var('table_idx', $get, $post, $cookie)) === FALSE)
1457 if (($table_token = gpcs_var('table_token', $get, $post, $cookie)) === FALSE)
1458 unset($table_token);
1465 bin5_index_main($transp_type, $header, $header_out, $addr, $get, $post, $cookie);
1466 $content = ob_get_contents();
1469 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1473 case "index_wr.php":
1474 if (isset($table_idx) && isset($table_token)) {
1475 if (($bri = $s_a_p->app->match_get($table_idx, $table_token)) != FALSE) {
1477 bin5_index_wr_main($bri, $addr, $get, $post, $cookie);
1478 $content = ob_get_contents();
1482 $content = "Bin5 Load data error";
1486 $content = "Bin5 Load data error";
1488 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1492 case "index_rd.php":
1493 case "index_rd_wss.php":
1494 if (($transp = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
1496 if ($transp == 'websocket' || $transp == 'websocketsec')
1500 if (!isset($table_idx)
1501 || !isset($table_token)
1502 || !isset($cookie['sess'])
1503 || ($bri = $s_a_p->app->match_get($table_idx, $table_token)) == NULL
1504 || (($user = $bri->get_user($cookie['sess'], $idx)) == FALSE)) {
1506 $content = Bin5_user::stream_fini($transp_type, $s_a_p->rndstr, TRUE);
1507 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1512 $bri->brisk->sess_cur_set($user->sess);
1514 // close a previous opened index_read_ifra socket, if exists
1515 if (($prev = $user->rd_socket_get()) != NULL) {
1516 $s_a_p->socks_unset($user->rd_socket_get());
1517 fclose($user->rd_socket_get());
1518 // printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
1519 $user->rd_socket_set(NULL);
1523 $user->stream_init($s_a_p->rndstr, $enc, $header, $header_out, $content, $get, $post, $cookie);
1525 $response = headers_render($header_out, -1).$user->chunked_content($content);
1526 $response_l = mb_strlen($response, "ASCII");
1528 $wret = @fwrite($new_socket, $response, $response_l);
1529 if ($wret < $response_l) {
1530 printf("TROUBLES WITH FWRITE: %d\n", $wret);
1531 $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
1534 $user->rd_cache_set("");
1536 fflush($new_socket);
1539 $s_a_p->socks_set($new_socket, $user, NULL);
1540 $user->rd_socket_set($new_socket);
1541 // printf(" - qui ci siamo - ");
1560 function locshm_exists($tok)
1564 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
1565 log_main($tok." SHM NOT exists");
1571 log_main($tok." SHM exists");
1580 is_transition (is from room to table ?)
1581 is_again (is another game)
1583 Examples of $is_transition, $is_again:
1584 from reload of the page: FALSE, FALSE
1585 from sitdown in room: TRUE, FALSE
1586 from table: asta cmd e tutti passano: TRUE, TRUE
1587 from table: fine partita: TRUE, TRUE
1589 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1591 $table_idx = $user->table;
1592 $table = $bri->table[$table_idx];
1593 $table_pos = $user->table_pos;
1595 $ret = "table_init();";
1596 $ret .= $table->exitlock_show($bri->user, $table_pos);
1598 /* GENERAL STATUS */
1599 $user_rules = $user->rules_get();
1600 $ret .= sprintf('gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d; rules_set(%d);',
1601 $sendstep, $user->stat, $user->subst, $table_pos,
1602 ($user_rules == BIN5_USER_CONTINUE_INIT ? $table->rules->id_get() : $user_rules));
1604 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1607 $ret .= "background_set();";
1610 $ret .= $user->myname_innerHTML();
1611 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1612 $bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->flags,
1613 xcape($bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->name),
1615 $bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->flags,
1616 xcape($bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->name),
1618 $bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->flags,
1619 xcape($bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->name),
1621 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->flags),
1622 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->name)),
1624 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->flags),
1625 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->name)));
1627 /* NOTIFY FOR THE CARD MAKER */
1628 if ($is_transition) { // && $user->subst == "asta" superfluo
1629 $ret .= show_table_info($bri, $table, $table_pos);
1630 $ret .= "setTimeout(preload_images, 500, g_preload_img_arr, g_imgct);";
1633 $ret .= "\$('imgct').innerHTML = \$('imgct').innerHTML = mlang_commons['imgload_a'][g_lang]+\"100%.\";";
1636 $ret .= table_welcome($user);
1638 if ($is_transition && !$is_again) { // just sit, play cow
1639 $ret .= playsound("cow");
1644 if ($is_transition) { // && $user->subst == "asta" superfluo
1647 for ($i = 0 ; $i < BIN5_CARD_HAND ; $i++) {
1648 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++) {
1650 for ($o = 0 ; $o < (BIN5_CARD_HAND * BIN5_PLAYERS_N) && $ct < $i+1 ; $o++) {
1651 // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1652 if ($table->card[$o]->owner == (($e + $table->gstart) % BIN5_PLAYERS_N)) {
1658 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1660 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BIN5_PLAYERS_N,
1661 $i, ((($e + BIN5_PLAYERS_N - $table_pos + $table->gstart) % BIN5_PLAYERS_N) == 0 ?
1662 $table->card[$o]->value : -1),
1663 ($i == 7 && $e == (BIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1668 $taked = array(0,0,0,0,0);
1669 $inhand = array(0,0,0,0,0);
1670 $ontabl = array(-1,-1,-1,-1,-1);
1673 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1674 // for ($i = 0 ; $i < 40 ; $i++) {
1675 if ($table->card[$i]->stat == 'hand') {
1676 if ($table->card[$i]->owner == $table_pos) {
1677 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1679 $inhand[$table->card[$i]->owner]++;
1681 else if ($table->card[$i]->stat == 'take') {
1682 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1683 $taked[$table->card[$i]->owner]++;
1685 else if ($table->card[$i]->stat == 'table') {
1686 $ontabl[$table->card[$i]->owner] = $i;
1690 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1691 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1693 log_main("Stat table: ".$logg);
1695 /* Set ours cards. */
1697 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1698 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1699 for ($i = $inhand[$table_pos] ; $i < BIN5_CARD_HAND ; $i++)
1700 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1701 $ret .= sprintf('card_setours(%s);', $oursarg);
1703 /* Dispose all cards */
1704 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1705 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1706 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1707 ($inhand[$i] <= BIN5_CARD_HAND ? $inhand[$i] : BIN5_CARD_HAND), $taked[$i]);
1709 if ($ontabl[$i] != -1) {
1710 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1711 $table->card[$ontabl[$i]]->value,
1712 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1718 if ($user->subst == 'asta') {
1720 /* show users auction status */
1722 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1723 $user_cur = &$bri->user[$table->player[$i]];
1724 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1725 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1727 if (BIN5_PLAYERS_N == 3)
1728 $showst .= ",-2,-2";
1729 $ret .= sprintf('document.title = "Brisk - Tavolo %d (asta)";', $user->table_orig);
1730 $ret .= sprintf('show_astat(%s);', $showst);
1732 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1733 /* show card chooser */
1734 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1738 // FIXME - RULES to be able to abandon table
1740 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N) &&
1741 $table->asta_win == -1)
1742 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1743 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1745 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1746 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1750 if ($table->asta_win == -1) { // auction case
1751 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N))
1752 $ret .= "remark_on();";
1754 $ret .= "remark_off();";
1756 else { // chooseed case
1757 if ($table_pos == $table->asta_win)
1758 $ret .= "remark_on();";
1760 $ret .= "remark_off();";
1763 else if ($user->subst == 'game') {
1765 if (($table->gstart + $table->turn) % BIN5_PLAYERS_N == $table_pos)
1766 $ret .= "is_my_time = true; remark_on();";
1768 $ret .= "remark_off();";
1770 /* WHO CALL AND WHAT */
1771 $ret .= briscola_show($bri, $table, $user);
1775 } // end function show_table(...
1777 function calculate_winner(&$table)
1784 $cur_seed = $table->briscola - ($table->briscola % 10);
1786 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1787 // for ($i = 0 ; $i < 40 ; $i++) {
1788 if ($table->card[$i]->stat != "table")
1791 log_wr(sprintf("Card On table: [%d]", $i));
1793 $v = $table->card[$i]->value;
1794 $ontab[$table->card[$i]->owner] = $v;
1795 $ontid[$table->card[$i]->owner] = $i;
1796 /* se briscola setto il flag */
1797 if (($v - ($v % 10)) == $cur_seed)
1801 if ($briontab == FALSE) {
1802 $cur_win = $table->gstart;
1803 $cur_val = $ontab[$cur_win];
1804 $cur_seed = $cur_val - ($cur_val % 10);
1807 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1808 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1809 if ($ontab[$i] < $cur_val) {
1810 $cur_val = $ontab[$i];
1816 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1817 $table->card[$ontid[$i]]->owner = $cur_win;
1818 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1823 function show_table_info(&$bri, &$table, $table_pos)
1825 GLOBAL $G_lang, $mlang_bin5_bin5;
1833 $user = $bri->user[$table->player[$table_pos]];
1835 // TAG: POINTS_MANAGEMENT
1836 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1837 $noty = sprintf('<p>%s.</p>\n', xcape(ucfirst(rules_id2descr($table->rules->id_get(), $G_lang))));
1838 $noty .= sprintf('<table class=\"points\"><tr><th></th>');
1841 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++)
1842 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1843 $noty .= sprintf("</tr>");
1846 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1848 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1849 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1850 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1851 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1856 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1857 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1858 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1859 $noty .= "</tr></table>";
1861 if ($table->old_reason != "") {
1862 $noty .= sprintf($mlang_bin5_bin5['info_last'][$G_lang],
1863 $tg_br, $tg_hr, $tg_bo, $tg_bc);
1865 $noty .= $table->old_reason;
1868 $noty .= sprintf($mlang_bin5_bin5['info_curr'][$G_lang],
1869 $tg_br, $tg_hr, $tg_bo, $tg_bc);
1871 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1872 if ($table->mazzo == $table_pos)
1873 $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
1875 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1876 $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
1879 if ($user->subst == 'asta') {
1880 if ($table->asta_win == -1) // auction case
1881 $curplayer = $table->gstart % BIN5_PLAYERS_N;
1883 $curplayer = $table->asta_win;
1885 else if ($user->subst == 'game') {
1886 $curplayer = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
1889 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1890 if ($curplayer == $table_pos) {
1891 $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
1894 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1895 $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
1898 $multer = $table->rules->multer(TRUE);
1900 $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($multer) );
1903 if ($table->match_id != -1) {
1904 $noty .= sprintf($mlang_bin5_bin5['info_match'][$G_lang], $table->match_id);
1907 $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
1908 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
1909 * 130 + ($table->points_n > 0 ? 50 : 0) +
1910 * (120 * ($table->points_n / MAX_POINTS)));
1911 * will be used when we refact notify js function following
1912 * photoo class logic
1918 function table_welcome($user)
1920 GLOBAL $table_wellarr, $G_lang;
1923 for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
1924 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
1930 function briscola_show($bri, $table, $user)
1932 GLOBAL $G_lang, $mlang_bin5_bin5;
1936 if ($table->asta_card == 9)
1937 $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
1939 /* text of caller cell */
1940 if ($user->table_pos == $table->asta_win) {
1941 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
1942 $ret .= sprintf($prestr, $ptnadd);
1945 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
1946 $ret .= sprintf($prestr,
1947 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1949 $ret .= sprintf('set_iscalling(%d);', ($table->asta_win - $user->table_pos + BIN5_PLAYERS_N) % BIN5_PLAYERS_N);
1951 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1953 $ret .= sprintf('$("callerimg").setAttribute("data-card-id", "%02d"; ', $table->briscola);
1954 $ret .= sprintf('$("caller").style.visibility = "visible";');
1955 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1956 $ret .= sprintf('$("astalascio").style.visibility = "";');
1957 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1958 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1963 function log_points($remote_addr, $curtime, $user, $where, $mesg)
1965 if (($fp = @fopen(LEGAL_PATH."/points.log", 'a')) != FALSE) {
1966 /* Unix time | session | nickname | IP | where was | mesg */
1967 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
1968 ($user->is_auth() ? 'A' : 'N'),
1969 $user->name, $remote_addr, $where , $mesg));