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 // br, hr, b, /b, win, fri
44 'info_part' => array( 'it' => 'Ha chiamato %3$s%5$s%4$s (punti torneo %7$d), il socio era %3$s%6$s%4$s,%1$s',
45 'en' => 'The declarer was %3$s%5$s%4$s (tournment points %7$d), the partner was %3$s%6$s%4$s,%1$s'),
47 'info_capp' => array( 'it' => 'hanno fatto %3$scappotto%4$s EBBRAVI!.%1$s',
48 'en' => 'and they made %3Dscapot%4$s WELL DONE!.%1$s'),
50 // br, hr, b, /b, old_asta_pnt, old_pnt, winornot
51 'info_alea' => array( 'it' => 'dovevano fare %3$salmeno %5$d%4$s punti e ne hanno fatti %3$s%6$d%4$s: hanno %7$s.%1$s',
52 'en' => 'they had to do %3$sat least %5$d%4$s points and they had made %3$s%6$d%4$s: they have %7$s.%1$s'),
53 // br, hr, b, /b, old_pnt, winornot
54 'info_more' => array( 'it' => 'dovevano fare %3$spiù di 60%4$s punti e ne hanno fatti %3$s%5$d%4$s: hanno %6$s.%1$s',
55 'en' => 'they had to do %3$sover 60%4$s points and they had made %3$s%5$d%4$s: they have %3$s%6$s%4$s.%1$s'),
56 'info_win' => array( 'it' => 'vinto',
58 'info_peer' => array( 'it' => 'pareggiato',
60 'info_lost' => array( 'it' => 'perso',
63 // br, hr, b, /b, win_name
64 'info_alon' => array( 'it' => '%3$s%5$s%4$s si è chiamato in mano (punti torneo %6$d),%1$s',
65 'en' => '%3$s%5$s%4$s play alone against each other (tournment points %6$d),%1$s'),
66 // br, hr, b, /b, old_asta_pnt, old_pnt, winornot
67 'info_aleaa' => array( 'it' => 'doveva fare %3$salmeno %5$d%4$s punti e ne ha fatti %3$s%6$d%4$s: ha %3$s%7$s%4$s.%1$s',
68 'en' => 'he/she had to do %3$sat least %5$d%4$s points and they had made %3$s%6$d%4$s: he/she had %3$s%7$s%4$s.%1$s'),
69 // br, hr, b, /b, old_pnt, winornot
70 'info_morea' => array( 'it' => 'doveva fare %3$spiù di 60%4$s punti e ne ha fatti %3$s%5$d%4$s: ha %3$s%6$s%4$s.%1$s',
71 'en' => 'he/she had to do %3$smore than 60%4$s points and they had made %3$s%5$d%4$s: he/she had %3$s%6$s%4$s.%1$s'),
73 'info_acap' => array( 'it' => 'ha fatto %3$scappotto%4$s EBBRAVO!.%1$s',
74 'en' => 'and he/she made %3$scapot%4$s WELL DONE!.%1$s'),
77 'info_omul' => array( 'it' => '%1$sLa partita valeva %3$s%5$s%4$s.%1$s',
78 'en' => '%1$sEN: The game was worth %3$s%5$s%4$s.%1$s' ),
81 'info_alpa' => array( 'it' => '%1$sHanno passato %3$stutti%4$s.%1$s',
82 'en' => '%1$sEN: Hanno passato %3$stutti%4$s.%1$s' ),
84 // br, hr, b, /b, aband-name
85 'info_aban' => array( 'it' => ' Ha lasciato %3$s%5$s%4$s perché aveva al massimo %3$s2 punti%4$s.',
86 'en' => ' EN: Ha lasciato %3$s%5$s%4$s perché aveva al massimo %3$s2 punti%4$s.'),
88 'info_shuf' => array( 'it' => 'Il mazzo a <b>%s</b>, ',
89 'en' => '<b>%s</b> shuffled the cards, '),
90 'info_yturn'=> array( 'it' => ' tocca a <b>te</b> giocare.',
91 'en' => ' it\'s <b>your</b> turn.'),
92 'info_turn' => array( 'it' => 'tocca a <b>%s</b> giocare.',
93 'en' => 'it\'s the <b>%s</b>\'s turn.'),
94 'info_mult' => array( 'it' => ' La partita vale <b>%s</b>.',
95 'en' => ' The game worth <b>%s</b>.' ),
96 'info_yshuf'=> array( 'it' => 'Fai <b>tu</b> il mazzo, ',
97 'en' => 'It\'s <b>your</b> shuffled the cards, '),
98 'info_match'=> array( 'it' => 'Il codice della partita è <b>%d</b>.',
99 'en' => 'Match code is <b>%d</b>.'),
100 'btn_bkgame'=> array( 'it' => 'torna alla partita',
101 'en' => 'back to the game'),
102 'call_wptn' => array( 'it' => '<br>con %d punti',
103 'en' => '<br>with %d points'),
104 'call_ycall'=> array( 'it' => 'Chiami%s:',
106 'call_call' => array( 'it' => 'Chiama %s%s:',
107 'en' => 'The declarer is %s%s:')
111 $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.'),
112 '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.') );
114 function rules_id2name($id)
117 return 'Rules_old_rules';
119 return 'Rules_no_draw';
121 return 'Rules_unknown';
124 function multoval($mult)
129 return ($G_lang == 'en' ? 'double' : 'doppio');
131 return ($G_lang == 'en' ? 'triple' : 'triplo');
133 return ($G_lang == 'en' ? 'quadruple' : 'quadruplo');
135 return (sprintf(($G_lang == 'en' ? "%d-ple" : "%d-plo"), $mult));
140 var $value; /* 0 - 39 card value */
141 var $stat; /* 'bunch', 'hand', 'table', 'take' */
142 var $owner; /* (table position 0-4) */
143 // var $pos; /* Pos in hand. */
144 var $x; /* When played the X position on the table of the owner. */
145 var $y; /* When played the Y position on the table of the owner. */
147 function Card($value, $stat, $owner)
149 $this->value = $value;
150 $this->stat = $stat; // Card stat
151 $this->owner = $owner;
154 function assign($stat,$owner)
156 $this->stat = $stat; // Card stat
157 $this->owner = $owner;
160 function setpos($pos)
167 $this->stat = 'table'; // Card stat
172 function take($newown)
174 $this->stat = 'take'; // Card stat
175 $this->owner = $newown;
179 class Bin5_table extends Table {
180 var $card; // il mazzo di carte
181 var $mazzo; // chi e' di mazzo
191 var $points; // points array
192 var $points_n; // number of row of points
195 var $asta_win; // the caller idx position at table
197 var $tourn_pts; // points in the caller hand
198 var $friend; // the callee idx position at table
200 var $match_id; // the id of the match on the database (-1 == just not saved)
202 var $old_act; // last action that trigs the end of the game
207 var $old_pnt; // points made by caller and callee
208 var $old_asta_win; // the old caller idx position at table
209 var $old_friend; // the old callee idx position at table
211 var $old_tourn_pts; // the old tournment computed points in the hand of caller
214 function Bin5_table()
220 function create($idx)
222 if (($thiz =& new Bin5_table()) == FALSE)
228 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
229 $thiz->asta_pla_n= -1;
230 $thiz->asta_card = -1;
231 $thiz->asta_pnt = -1;
234 $thiz->points = array( );
236 $thiz->total = array( 0, 0, 0, 0, 0);
237 $thiz->asta_win = -1;
238 $thiz->briscola = -1;
239 $thiz->tourn_pts = -1;
243 $thiz->match_id = -1;
246 $thiz->old_mazzo = -1;
247 $thiz->old_reason = "";
248 $thiz->old_asta_pnt = -1;
249 $thiz->old_mult = -1;
251 $thiz->old_asta_win = -1;
252 $thiz->old_friend = -1;
259 function myclone(&$from)
261 if (($thiz =& new Bin5_table()) == FALSE)
266 $thiz->card = $from->card;
267 $thiz->mazzo = $from->mazzo; // REVIEW
268 $thiz->gstart = $from->gstart;
269 $thiz->turn = $from->turn;
271 $thiz->asta_pla = $from->asta_pla;
272 $thiz->asta_pla_n = $from->asta_pla_n;
273 $thiz->asta_card = $from->asta_card;
274 $thiz->asta_pnt = $from->asta_pnt;
276 $thiz->mult = $from->mult;
277 $thiz->points = $from->points;
278 $thiz->points_n = $from->points_n;
279 $thiz->total = $from->total;
281 $thiz->asta_win = $from->asta_win;
282 $thiz->briscola = $from->briscola;
283 $thiz->tourn_pts = $from->tourn_pts;
284 $thiz->friend = $from->friend;
286 $thiz->match_id = $from->match_id;
288 $thiz->old_act = $from->old_act;
289 $thiz->old_mazzo = $from->old_mazzo;
290 $thiz->old_reason = $from->old_reason;
291 $thiz->old_asta_pnt = $from->old_asta_pnt;
292 $thiz->old_mult = $from->mult;
293 $thiz->old_pnt = $from->old_pnt;
294 $thiz->old_asta_win = $from->old_asta_win;
295 $thiz->old_friend = $from->old_friend;
301 function parentcopy(&$from)
306 function spawn(&$from)
308 if (($thiz =& new Bin5_table()) == FALSE)
311 $thiz->parentcopy($from);
313 log_main("PLAYER_N - spawn.".$thiz->player_n);
315 $thiz->card = array();
316 $thiz->bunch_create();
317 $thiz->mazzo = rand(0,PLAYERS_N-1);
318 $thiz->points = array();
319 $thiz->total = array();
323 $thiz->match_id = -1;
325 $thiz->old_asta_win = -1;
326 $thiz->old_reason = "";
328 $rules_name = rules_id2name(BIN5_TOURNAMENT_CURRENT);
329 $thiz->rules = new $rules_name($thiz);
330 // players are rearranged in an dedicated array
331 $thiz->player = array();
332 for ($i = 0 ; $i < $from->player_n ; $i++)
333 $thiz->player[$i] = $i;
335 log_main("TABLE_OLD_WIN - spawn:".$thiz->old_asta_win);
341 // function bunch_create_old() function AND
345 // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
346 // // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
347 // $ret[$i] =& new Card($i, 'bunch', 'no_owner');
354 function bunch_create()
358 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
359 $this->card[$i] = new Card($i, 'bunch', 'no_owner');
363 function bunch_make()
365 log_main("bunch_make start");
366 $ct = array(0,0,0,0,0);
368 mt_srand(make_seed());
370 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--)
373 for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--) {
377 log_main("RND ZERO");
381 $owner = $i % BIN5_PLAYERS_N;
382 $this->card[$id]->assign('hand', $owner);
384 $rest[$rn] = $rest[$i];
385 // $pubbpos[$rn2] = $pubbpos[$i];
387 log_main("bunch_make end");
390 function init($userarr)
393 $this->mazzo = rand(0,PLAYERS_N-1);
396 $this->old_asta_win = -1;
397 $this->old_reason = "";
399 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
400 $this->total[$i] = 0;
401 $user_cur = $userarr[$this->player[$i]];
402 $user_cur->stat_set('table');
403 $user_cur->exitislock = TRUE;
406 log_main("table::init: ci siamo");
409 function game_init($userarr)
413 $this->gstart = ($this->mazzo+1) % BIN5_PLAYERS_N;
416 $this->asta_pla_n = BIN5_PLAYERS_N;
417 $this->asta_card = -1;
418 $this->asta_pnt = 60;
419 $this->asta_win = -1;
420 $this->briscola = -1;
421 $this->tourn_pts = -1;
425 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
426 $this->asta_pla[$i] = TRUE;
427 $user_cur = $userarr[$this->player[$i]];
428 $user_cur->subst = 'asta';
429 $user_cur->asta_card = -2;
430 $user_cur->asta_pnt = -1;
431 $user_cur->handpt = $this->hand_points($i);
436 function game_next($delta)
438 $this->old_mazzo = $this->mazzo;
439 $this->mazzo = ($this->mazzo + $delta) % BIN5_PLAYERS_N;
442 function mult_inc($val)
444 $this->old_mult = $this->mult;
448 function mult_set($val)
450 $this->old_mult = $this->mult;
454 function hand_points($idx)
456 GLOBAL $G_all_points;
460 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
461 // for ($i = 0 ; $i < 40 ; $i++) {
462 if ($this->card[$i]->owner != $idx)
465 $ctt = $this->card[$i]->value % 10;
466 $tot += $G_all_points[$ctt];
473 function exitlock_show($userarr, $table_pos)
475 $ct = $this->exitlock_calc($userarr, $table_pos);
477 $ret = sprintf('exitlock_show(%d, %s);', $ct,
478 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
482 function exitlock_calc(&$userarr, $table_pos)
486 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
487 if ($userarr[$this->player[$i]]->exitislock == FALSE)
494 function match_continue(&$bri, $user, $match_id_s)
497 // Rules: update version following loaded tcode
503 /* - verify if match_id and user are both valid to accept
504 the match_continue request - */
505 $match_id = (int)$match_id_s;
506 if ($match_id <= 0) {
507 $msg = "questa partita non esiste";
511 if ($user->continue_get() == BIN5_USER_CONTINUE_ALREADY) {
512 $msg = "Hai già richiesto di continuare una partita.";
516 if ($user->continue_get() == $match_id) {
517 $msg = "Hai già richiesto di continuare questa partita.";
520 // retrieves users list for this match
521 $match_data = array();
522 if (($bdb = BriskDB::create()) != FALSE) {
523 // match_order_get return FALSE for old matches
524 $ucodes = $bdb->match_order_get($match_data, $match_id, BIN5_PLAYERS_N);
527 if ($ucodes == FALSE) {
528 $msg = "questa partita non è stata memorizzata correttamente";
532 // if current user code must be in the users list
533 if (array_search($user->code, $ucodes) === FALSE) {
534 $msg = sprintf("Questo utente non compare nella partita che si vuole continuare [%d].", $user->code);
538 /* - the user is in the list, add the match_id to his profile
539 and check if he is the N'th to require continue - */
541 // set the match_id for the current user
542 $user->continue_set($match_id);
544 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
545 $user_cur = &$bri->user[$this->player[$i]];
546 if ($user_cur->continue_get() != $match_id) {
551 // not all players set the continue match than we exit
552 if ($i < BIN5_PLAYERS_N) {
553 $msg = sprintf("<b>L'utente <i>%s</i> vorrebbe continuare la partita n° %d.</b>",
554 xcape($user->name), $match_id);
558 /* - all users decide to continue the same match, update all infos and rearrange users
559 to the right positions on the table - */
561 /* reset users table order */
562 for ($i = 0 ; $i < BIN5_PLAYERS_N - 1 ; $i++) {
563 if ($bri->user[$this->player[$i]]->code == $ucodes[$i]) {
566 for ($e = $i + 1 ; $e < BIN5_PLAYERS_N ; $e++) {
567 if ($bri->user[$this->player[$e]]->code == $ucodes[$i]) {
568 $swap = $this->player[$i];
569 $this->player[$i] = $this->player[$e];
570 $this->player[$e] = $swap;
571 $bri->user[$this->player[$i]]->table_pos = $i;
572 $bri->user[$this->player[$e]]->table_pos = $e;
576 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
577 fprintf(STDERR, "USERZ: [%s]\n", $bri->user[$this->player[$i]]->name);
580 // update database info to be aligned with current table (ttok and table_idx
581 if (($bdb = BriskDB::create()) != FALSE) {
582 if ($bdb->match_continue($match_id, $this, $user->table_orig) == FALSE) {
584 $msg = "aggiornamento dei dati della partita fallito";
590 /* update rules engine */
591 $rules_name = rules_id2name($match_data['tcode']);
592 $this->rules = new $rules_name($this);
594 /* bunch and multiplier status set */
595 $this->mazzo = $match_data['mazzo_next'];
596 $this->mult = $match_data['mult_next'];
597 $this->match_id = $match_id;
598 $this->game_init(&$bri->user);
600 /* reload of the page with the new layout */
601 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
602 $user_cur = &$bri->user[$this->player[$i]];
603 $user_cur->continue_set(BIN5_USER_CONTINUE_ALREADY);
605 $user_cur->trans_step = $user_cur->step + 1;
606 $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);
607 $user_cur->step_inc();
609 // a void command force xynt-streamer to flush all data to client
610 $user_cur->trans_step = $user_cur->step + 1;
611 $user_cur->comm[$user_cur->step % COMM_N] = "";
612 $user_cur->step_inc();
614 $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$bri, &$user_cur, $user_cur->step+1, TRUE, FALSE);
615 $user_cur->step_inc();
620 $dt = date("H:i ", $curtime);
621 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
622 if ($ret == FALSE && $this->player[$i] != $user->idx)
624 $user_cur = &$bri->user[$this->player[$i]];
625 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
626 $user_cur->comm[$user_cur->step % COMM_N] .= nickserv_msg($dt, $msg);
627 $user_cur->step_inc();
631 } // end class Bin5_table
636 define('BIN5_USER_FLAG_RING_ENDAUCT', 0x01);
637 define('BIN5_USER_CONTINUE_INIT', -1);
638 define('BIN5_USER_CONTINUE_ALREADY', -2);
639 class Bin5_user extends User {
642 var $handpt; // Total card points at the beginning of the current hand.
643 var $exitislock; // Player can exit from the table ?
644 var $privflags; // Flags for briskin5 only
646 var $continue; // Id of the match that the user would continue
654 function create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
655 if (($thiz =& new User()) == FALSE)
658 $thiz->asta_card = -2;
659 $thiz->asta_pnt = -1;
661 $thiz->exitislock = TRUE;
662 $thiz->privflags = 0;
663 $thiz->continue = BIN5_USER_CONTINUE_INIT;
669 function parentcopy(&$from)
674 function copy(&$from)
676 $this->parentcopy($from);
678 $this->asta_card = $from->asta_card;
679 $this->asta_pnt = $from->asta_pnt;
680 $this->handpt = $from->handpt;
681 $this->exitislock = $from->exitislock;
682 $this->privflags = $from->privflags;
683 $this->continue = $from->continue;
687 function myclone(&$from)
689 if (($thiz =& new User()) == FALSE)
698 static function spawn($from, &$bri, $table, $table_pos, $get, $post, $cookie)
700 if (($thiz = new Bin5_user()) == FALSE)
703 if (($CO_bin5_pref_ring_endauct = gpcs_var("CO_bin5_pref_ring_endauct", $get, $post, $cookie)) === FALSE) {
704 $CO_bin5_pref_ring_endauct = "";
707 $thiz->parentcopy($from);
709 /* NOTE: at this moment idx and table_pos fields have the same value
710 but diffentent functions, we keep them separated for a while */
712 $thiz->idx = $table_pos; // it is the position in the mini-room,
713 // not related to table pos (see below)
714 $thiz->asta_card = -2;
715 $thiz->asta_pnt = -1;
717 $thiz->exitislock = TRUE;
718 $thiz->continue = BIN5_USER_CONTINUE_INIT;
720 log_wr("Bin5 constructor");
722 $thiz->privflags = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
724 $thiz->table_orig = $table;
726 $thiz->table_pos = $table_pos;
733 function step_set($step)
735 $this->step = $step & 0x7fffffff;
740 function step_inc($delta = 1) {
741 $this->step += $delta;
742 /* modularization because unpack() not manage unsigned 32bit int correctly */
743 $this->step &= 0x7fffffff;
748 static function load_step($tab_id, $sess)
752 if (validate_sess($sess) == FALSE)
755 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
756 mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
757 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step", 'rb')) == FALSE)
759 if (($s = fread($fp, 8)) == FALSE)
761 if (mb_strlen($s, "ASCII") != 8)
763 $arr = unpack('Ls/Li', $s);
766 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
773 log_rd2("STEP_GET [".$sess."]: return false ");
781 if (validate_sess($this->sess) == FALSE)
783 if (file_exists(BIN5_PROXY_PATH."/table".$this->table_orig) == FALSE)
784 mkdir(BIN5_PROXY_PATH."/table".$this->table_orig, 0775, TRUE);
785 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$this->table_orig."/".$this->sess.".step", 'w')) == FALSE)
787 fwrite($fp, pack("LL",$this->step, $this->idx));
790 log_main("step_set [".$this->sess. "] [".$this->step."]");
798 static function unproxy_step($tab_id, $sess)
800 log_rd2("UNPROXY: ".BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
801 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
804 @unlink(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
807 function destroy_data($tab_id)
810 if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos, "B")) == -1) {
811 log_crit("BIN5 USER DATA REMOVE FAILED 1 [".FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos."]");
815 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
816 log_crit("BIN5 USER DATA REMOVE FAILED 2");
819 if (shmop_delete($shm) == 0) {
820 log_crit("BIN5 USER DATA REMOVE FAILED 3");
825 log_main("BIN5 USER DATA DESTROY SUCCESS");
827 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
837 static function blocking_error($is_unrecoverable)
839 log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
840 return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
843 protected function page_sync($sess, $page)
845 log_rd2("PAGE_SYNC");
846 printf("xXx BIN5_USER::PAGE_SYNC\n");
847 return (sprintf('xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
850 protected function maincheck($get, $post, $cookie)
853 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
854 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
858 log_rd("maincheck begin");
863 /* Nothing changed, return. */
864 if ($this->rd_step == $this->step)
867 log_rd2("do other cur_stat[".$this->rd_stat."] user->stat[".$this->stat."] cur_step[".$this->rd_step."] user_step[".$this->step."]");
869 if ($this->rd_step == -1) {
871 * if $this->rd_step == -1 load the current state from the main struct
874 $S_load_stat['wR_minusone']++;
876 // if ($this->the_end == TRUE) {
877 // log_rd2("main_check: the end".var_export(debug_backtrace()));
880 if ($this->trans_step != -1) {
881 log_rd2("TRANS USATO ".$this->trans_step);
882 $this->rd_step = $this->trans_step;
883 $this->trans_step = -1;
886 log_rd2("TRANS NON ATTIVATO");
891 /* this part I suppose is read only on $this->room structure */
892 if ($this->rd_step == -1) {
893 log_rd2("PRE-NEWSTAT");
900 if ($this->stat == 'table') {
902 /* NOTE: $this->room is associated with the current $bri object */
903 printf("xXx CLASS NAME [%s]\n", get_class($this->room));
904 $ret = show_table(&$this->room, $this, $this->step, FALSE, FALSE);
906 log_rd2("NEWSTAT: ".$this->stat);
908 $this->rd_stat = $this->stat;
909 $this->rd_subst = $this->subst;
910 $this->rd_step = $this->step;
911 } /* if ($this->rd_step == -1) { */
913 $S_load_stat['rU_heavy']++;
915 if ($this->rd_step < $this->step) {
917 if ($this->rd_step + COMM_N < $this->step) {
918 if ($this->rd_stat != $this->stat) {
919 $to_stat = $this->stat;
921 printf("xXx BIN5_USER::MAINCHECK\n");
922 return ($this->page_sync($this->sess, ($to_stat == "table" ? "index.php" : "../index.php"), $this->table, $this->table_token));
924 log_rd2("lost history, refresh from scratch");
925 printf("xXx LOST HISTORY!\n");
929 for ($i = $this->rd_step ; $i < $this->step ; $i++) {
931 if ($this->comm[$ii] == "") {
932 if ($i == $this->rd_step)
937 log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
938 $ret .= $this->comm[$ii];
940 $this->rd_stat = $this->stat;
941 $this->rd_subst = $this->subst;
942 $this->rd_step = $this->step;
945 log_rd2($this->step, 'index_rd.php: after ret set');
947 // if ($this->the_end == TRUE) { management is moved
948 // in the spush scope
949 } /* if ($this->rd_step < $this->step) { */
950 } /* else of if ($this->rd_step == -1) { */
954 } // function maincheck(...
956 function continue_set($match_code)
958 $this->continue = $match_code;
961 function continue_get() {
962 return ($this->continue);
965 } // end class Bin5_user
969 static $delta_t = array();
970 var $brisk;// room object reference
974 var $comm; // commands for many people
975 var $step; // current step of the comm array
976 var $garbage_timeout;
987 function Bin5($brisk, $table_idx, $table_token, $get, $post, $cookie) {
988 $this->user = array();
989 $this->table = array();
991 $this->the_end = FALSE;
992 $this->shm_sz = BIN5_SHM_MIN;
993 if (($this->tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
998 $this->brisk = $brisk;
999 $user = $brisk->user;
1000 $table = $brisk->table[$table_idx];
1001 log_wr("Bin5 constructor");
1003 for ($i = 0 ; $i < $table->player_n ; $i++) {
1004 $user[$table->player[$i]]->table_token = $table_token;
1005 $this->user[$i] = Bin5_user::spawn($user[$table->player[$i]], $this, $table_idx, $i, $get, $post, $cookie);
1007 $this->table[0] = Bin5_table::spawn(&$table);
1009 log_main("TABLE_OLD_WIN - Bin5:".$this->table[0]->old_asta_win);
1011 $this->table_idx = $table_idx;
1012 $this->table_token = $table_token;
1013 $this->garbage_timeout = 0;
1015 $this->delay_mgr = new Delay_Manager((GARBAGE_TIMEOUT *3.0) / 2.0);
1017 log_wr("Bin5 constructor end");
1021 function get_user($sess, &$idx)
1025 if (validate_sess($sess)) {
1026 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1027 if ($this->user[$i]->is_empty())
1029 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1032 $ret = &$this->user[$i];
1036 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1037 // for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++)
1038 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1041 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1046 function banned_kickoff()
1050 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1051 $user_cur = $this->user[$i];
1053 // check if the IP is blacklisted
1054 if ($this->brisk->black_check($user_cur->ip)) {
1055 $user_cur->lacc = 0;
1060 // if authorized not check if banlisted
1061 if ($user_cur->is_auth()) {
1065 if ($this->brisk->ban_check($user_cur->ip)) {
1066 $user_cur->lacc = 0;
1073 function garbage_manager($force)
1077 /* Garbage collector degli utenti in timeout */
1081 $delta = $this->delay_mgr->delta_get($curtime);
1083 if ($force || $this->garbage_timeout < $curtime) {
1084 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1085 $user_cur = $this->user[$i];
1086 if ($user_cur->is_active() == FALSE || // is not active user or
1087 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
1090 if ($user_cur->lacc + EXPIRE_TIME_RD < ($curtime - $delta)) { // Auto logout dell'utente
1091 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
1093 if ($user_cur->stat == 'table') {
1094 log_auth($user_cur->sess," bin5 Autologout session.");
1096 /* main garbage_manager is delegate as autologout management */
1097 $user_cur->the_end = TRUE;
1099 /* se gli altri utenti non erano d'accordo questo utente viene bannato */
1100 $remcalc = $this->table[0]->exitlock_calc(&$this->user, $user_cur->table_pos);
1102 require_once("${G_base}Obj/hardban.phh");
1103 Hardbans::add(($user_cur->is_auth() ? $user_cur->name : FALSE),
1104 $user_cur->ip, $user_cur->sess, $user_cur->laccwr + BAN_TIME);
1106 // $user->bantime = $user->laccwr + BAN_TIME;
1108 $this->table_wakeup($user_cur);
1112 log_rd2($user_cur->sess." GARBAGE UPDATED!");
1114 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
1119 $this->delay_mgr->lastcheck_set($curtime);
1124 function destroy_data()
1130 log_main("DESTROY BRISKIN5 DATA");
1133 log_main("DESTROY2 BRISKIN5 DATA [".$this->table_idx."]");
1134 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1135 $this->user[$i]->destroy_data($this->table_idx);
1136 Bin5_user::unproxy_step($this->table_idx, $this->user[$i]->sess);
1138 if (($tok = @ftok(FTOK_PATH."/bin5/table".$this->table_idx."/table", "B")) == -1)
1141 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
1144 if (shmop_delete($shm) == 0) {
1145 log_only("REMOVE FALLITA ");
1150 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
1152 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
1162 static function lock_data($is_exclusive, $table_idx)
1164 if (($res = file_lock(FTOK_PATH."/bin5/table".$table_idx."/table", $is_exclusive)) != FALSE) {
1165 self::$delta_t = microtime(TRUE);
1166 log_lock("LOCK table [".$table_idx."] [".self::$delta_t[$table_idx]."]");
1168 return (new Vect(array('res' => $res, 'tab' => $table_idx)));
1175 static function unlock_data($res_vect)
1179 $res = $res_vect->getbyid('res');
1180 $tab = $res_vect->getbyid('tab');
1182 log_lock("UNLOCK table [".$tab."] [".(microtime(TRUE) - (self::$delta_t[$tab]))."]");
1188 function chatt_send($user, $mesg, $mlang_indwr = NULL)
1190 GLOBAL $mlang_brisk, $G_lang;
1192 if ($user->stat != 'table') {
1197 $table = &$this->table[$user->table];
1198 $user_mesg = substr($mesg,6);
1203 $dt = date("H:i ", $curtime);
1204 if (strncmp($user_mesg, "/cont ", 6) == 0) {
1205 log_main($user->sess." chatt_send BEGIN");
1207 $match_id = substr($user_mesg, 6);
1208 $table->match_continue($this, $user, $match_id);
1211 for ($i = 0 ; $i < ($user->stat == 'room' ? BIN5_MAX_PLAYERS : BIN5_PLAYERS_N) ; $i++) {
1212 if ($user->stat == 'room') {
1213 $user_cur = &$this->user[$i];
1214 if ($user_cur->is_active() == FALSE || $user_cur->stat != 'room') // is not active user or stat isn't 'room'
1218 $user_cur = &$this->user[$table->player[$i]];
1221 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1222 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
1223 $dt, $user->flags, xcape($user->name), xcape($user_mesg));
1224 $user_cur->step_inc();
1226 log_legal($curtime, $user->ip, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
1230 function table_wakeup($user)
1232 $table = &$this->table[0];
1234 log_main("BIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
1238 log_main("BIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1240 for ($i = 0 ; $i < $table->player_n ; $i++) {
1241 $user_cur = &$this->user[$i];
1242 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1244 if ($user_cur == $user)
1245 $user_cur->subst = "shutdowner";
1247 $user_cur->subst = "shutdowned";
1248 $user_cur->laccwr = $curtime;
1250 $ret = "gst.st = ".($user_cur->step+1)."; ";
1251 $ret .= 'gst.st_loc++; xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
1253 log_wr($user_cur->sess." BIN5_WAKEUP: ".$ret);
1254 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1255 $user_cur->step_inc();
1258 $this->the_end = TRUE;
1262 * If all players are freezed the room garbage_manager clean up table and users.
1264 function is_abandoned()
1269 $table = &$this->table[0];
1271 for ($i = 0 ; $i < $table->player_n ; $i++) {
1272 $user_cur = &$this->user[$i];
1274 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
1283 static function request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
1285 GLOBAL $G_ban_list, $G_black_list;
1287 printf("NEW_SOCKET (root): %d\n", intval($new_socket));
1289 $enc = get_encoding($header);
1290 if (isset($header['User-Agent'])) {
1291 if (strstr($header['User-Agent'], "MSIE")) {
1292 $transp_type = "htmlfile";
1295 $transp_type = "xhr";
1299 $transp_type = "iframe";
1301 force_no_cache($header_out);
1303 if (($table_idx = gpcs_var('table_idx', $get, $post, $cookie)) === FALSE)
1306 if (($table_token = gpcs_var('table_token', $get, $post, $cookie)) === FALSE)
1307 unset($table_token);
1314 bin5_index_main($transp_type, $header_out, $addr, $get, $post, $cookie);
1315 $content = ob_get_contents();
1318 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1322 case "index_wr.php":
1323 if (isset($table_idx) && isset($table_token)) {
1324 if (($bri = $s_a_p->app->match_get($table_idx, $table_token)) != FALSE) {
1326 bin5_index_wr_main($bri, $addr, $get, $post, $cookie);
1327 $content = ob_get_contents();
1331 $content = "Bin5 Load data error";
1335 $content = "Bin5 Load data error";
1337 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1341 case "index_rd.php":
1342 if (($transp = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
1344 if ($transp == 'websocket')
1348 if (!isset($table_idx)
1349 || !isset($table_token)
1350 || !isset($cookie['sess'])
1351 || ($bri = $s_a_p->app->match_get($table_idx, $table_token)) == NULL
1352 || (($user = $bri->get_user($cookie['sess'], $idx)) == FALSE)) {
1354 $content = Bin5_user::stream_fini($transp_type, $s_a_p->rndstr, TRUE);
1355 $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1360 $bri->brisk->sess_cur_set($user->sess);
1362 // close a previous opened index_read_ifra socket, if exists
1363 if (($prev = $user->rd_socket_get()) != NULL) {
1364 $s_a_p->socks_unset($user->rd_socket_get());
1365 fclose($user->rd_socket_get());
1366 printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
1367 $user->rd_socket_set(NULL);
1371 $user->stream_init($s_a_p->rndstr, $enc, $header, $header_out, $content, $get, $post, $cookie);
1373 $response = headers_render($header_out, -1).$user->chunked_content($content);
1374 $response_l = mb_strlen($response, "ASCII");
1376 $wret = @fwrite($new_socket, $response, $response_l);
1377 if ($wret < $response_l) {
1378 printf("TROUBLES WITH FWRITE: %d\n", $wret);
1379 $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
1382 $user->rd_cache_set("");
1384 fflush($new_socket);
1387 $s_a_p->socks_set($new_socket, $user, NULL);
1388 $user->rd_socket_set($new_socket);
1389 printf(" - qui ci siamo - ");
1408 function locshm_exists($tok)
1412 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
1413 log_main($tok." SHM NOT exists");
1419 log_main($tok." SHM exists");
1428 is_transition (is from room to table ?)
1429 is_again (is another game)
1431 Examples of $is_transition, $is_again:
1432 from reload of the page: FALSE, FALSE
1433 from sitdown in room: TRUE, FALSE
1434 from table: asta cmd e tutti passano: TRUE, TRUE
1435 from table: fine partita: TRUE, TRUE
1437 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1439 $table_idx = $user->table;
1440 $table = $bri->table[$table_idx];
1441 $table_pos = $user->table_pos;
1443 $ret = "table_init();";
1444 $ret .= $table->exitlock_show(&$bri->user, $table_pos);
1446 /* GENERAL STATUS */
1447 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1448 $sendstep, $user->stat, $user->subst, $table_pos);
1450 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1453 $ret .= "background_set();";
1456 $ret .= $user->myname_innerHTML();
1457 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1458 $bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->flags,
1459 xcape($bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->name),
1461 $bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->flags,
1462 xcape($bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->name),
1464 $bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->flags,
1465 xcape($bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->name),
1467 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->flags),
1468 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->name)),
1470 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->flags),
1471 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->name)));
1473 /* NOTIFY FOR THE CARD MAKER */
1474 if ($is_transition) { // && $user->subst == "asta" superfluo
1475 $ret .= show_table_info(&$bri, &$table, $table_pos);
1476 $ret .= "setTimeout(preload_images, 500, g_preload_img_arr, g_imgct);";
1479 $ret .= "\$('imgct').innerHTML = \$('imgct').innerHTML = mlang_commons['imgload_a'][g_lang]+\"100%.\";";
1482 $ret .= table_welcome($user);
1484 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1485 $ret .= playsound("cow.mp3");
1490 if ($is_transition) { // && $user->subst == "asta" superfluo
1493 for ($i = 0 ; $i < BIN5_CARD_HAND ; $i++) {
1494 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++) {
1496 for ($o = 0 ; $o < (BIN5_CARD_HAND * BIN5_PLAYERS_N) && $ct < $i+1 ; $o++) {
1497 // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1498 if ($table->card[$o]->owner == (($e + $table->gstart) % BIN5_PLAYERS_N)) {
1504 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1506 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BIN5_PLAYERS_N,
1507 $i, ((($e + BIN5_PLAYERS_N - $table_pos + $table->gstart) % BIN5_PLAYERS_N) == 0 ?
1508 $table->card[$o]->value : -1),
1509 ($i == 7 && $e == (BIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1514 $taked = array(0,0,0,0,0);
1515 $inhand = array(0,0,0,0,0);
1516 $ontabl = array(-1,-1,-1,-1,-1);
1519 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1520 // for ($i = 0 ; $i < 40 ; $i++) {
1521 if ($table->card[$i]->stat == 'hand') {
1522 if ($table->card[$i]->owner == $table_pos) {
1523 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1525 $inhand[$table->card[$i]->owner]++;
1527 else if ($table->card[$i]->stat == 'take') {
1528 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1529 $taked[$table->card[$i]->owner]++;
1531 else if ($table->card[$i]->stat == 'table') {
1532 $ontabl[$table->card[$i]->owner] = $i;
1536 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1537 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1539 log_main("Stat table: ".$logg);
1541 /* Set ours cards. */
1543 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1544 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1545 for ($i = $inhand[$table_pos] ; $i < BIN5_CARD_HAND ; $i++)
1546 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1547 $ret .= sprintf('card_setours(%s);', $oursarg);
1549 /* Dispose all cards */
1550 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1551 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1552 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1553 ($inhand[$i] <= BIN5_CARD_HAND ? $inhand[$i] : BIN5_CARD_HAND), $taked[$i]);
1555 if ($ontabl[$i] != -1) {
1556 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1557 $table->card[$ontabl[$i]]->value,
1558 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1564 if ($user->subst == 'asta') {
1566 /* show users auction status */
1568 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1569 $user_cur = &$bri->user[$table->player[$i]];
1570 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1571 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1573 if (BIN5_PLAYERS_N == 3)
1574 $showst .= ",-2,-2";
1575 $ret .= sprintf('document.title = "Brisk - Tavolo %d (asta)";', $user->table_orig);
1576 $ret .= sprintf('show_astat(%s);', $showst);
1578 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1579 /* show card chooser */
1580 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1585 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N) &&
1586 $table->asta_win == -1)
1587 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1588 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1590 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1591 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1595 if ($table->asta_win == -1) { // auction case
1596 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N))
1597 $ret .= "remark_on();";
1599 $ret .= "remark_off();";
1601 else { // chooseed case
1602 if ($table_pos == $table->asta_win)
1603 $ret .= "remark_on();";
1605 $ret .= "remark_off();";
1608 else if ($user->subst == 'game') {
1610 if (($table->gstart + $table->turn) % BIN5_PLAYERS_N == $table_pos)
1611 $ret .= "is_my_time = true; remark_on();";
1613 $ret .= "remark_off();";
1615 /* WHO CALL AND WHAT */
1616 $ret .= briscola_show($bri, $table, $user);
1620 } // end function show_table(...
1622 function calculate_winner(&$table)
1629 $cur_seed = $table->briscola - ($table->briscola % 10);
1631 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1632 // for ($i = 0 ; $i < 40 ; $i++) {
1633 if ($table->card[$i]->stat != "table")
1636 log_wr(sprintf("Card On table: [%d]", $i));
1638 $v = $table->card[$i]->value;
1639 $ontab[$table->card[$i]->owner] = $v;
1640 $ontid[$table->card[$i]->owner] = $i;
1641 /* se briscola setto il flag */
1642 if (($v - ($v % 10)) == $cur_seed)
1646 if ($briontab == FALSE) {
1647 $cur_win = $table->gstart;
1648 $cur_val = $ontab[$cur_win];
1649 $cur_seed = $cur_val - ($cur_val % 10);
1652 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1653 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1654 if ($ontab[$i] < $cur_val) {
1655 $cur_val = $ontab[$i];
1661 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1662 $table->card[$ontid[$i]]->owner = $cur_win;
1663 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1668 function show_table_info(&$bri, &$table, $table_pos)
1670 GLOBAL $G_lang, $mlang_bin5_bin5;
1678 $user = $bri->user[$table->player[$table_pos]];
1680 // TAG: POINTS_MANAGEMENT
1681 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1682 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1685 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++)
1686 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1687 $noty .= sprintf("</tr>");
1690 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1692 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1693 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1694 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1695 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1700 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1701 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1702 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1703 $noty .= "</tr></table>";
1705 if ($table->old_reason != "") {
1706 $noty .= sprintf($mlang_bin5_bin5['info_last'][$G_lang],
1707 $tg_br, $tg_hr, $tg_bo, $tg_bc);
1709 $noty .= $table->old_reason;
1712 $noty .= sprintf($mlang_bin5_bin5['info_curr'][$G_lang],
1713 $tg_br, $tg_hr, $tg_bo, $tg_bc);
1715 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1716 if ($table->mazzo == $table_pos)
1717 $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
1719 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1720 $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
1723 if ($user->subst == 'asta') {
1724 if ($table->asta_win == -1) // auction case
1725 $curplayer = $table->gstart % BIN5_PLAYERS_N;
1727 $curplayer = $table->asta_win;
1729 else if ($user->subst == 'game') {
1730 $curplayer = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
1733 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1734 if ($curplayer == $table_pos) {
1735 $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
1738 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1739 $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
1742 $multer = $table->rules->multer(TRUE);
1744 $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($multer) );
1747 if ($table->match_id != -1) {
1748 $noty .= sprintf($mlang_bin5_bin5['info_match'][$G_lang], $table->match_id);
1751 $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
1752 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
1753 * 130 + ($table->points_n > 0 ? 50 : 0) +
1754 * (120 * ($table->points_n / MAX_POINTS)));
1755 * will be used when we refact notify js function following
1756 * photoo class logic
1762 function table_welcome($user)
1764 GLOBAL $table_wellarr, $G_lang;
1767 for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
1768 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
1774 function briscola_show($bri, $table, $user)
1776 GLOBAL $G_lang, $mlang_bin5_bin5;
1780 if ($table->asta_card == 9)
1781 $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
1783 /* text of caller cell */
1784 if ($user->table_pos == $table->asta_win) {
1785 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
1786 $ret .= sprintf($prestr, $ptnadd);
1789 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
1790 $ret .= sprintf($prestr,
1791 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1793 $ret .= sprintf('set_iscalling(%d);', ($table->asta_win - $user->table_pos + BIN5_PLAYERS_N) % BIN5_PLAYERS_N);
1795 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1797 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1798 $ret .= sprintf('$("caller").style.visibility = "visible";');
1799 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1800 $ret .= sprintf('$("astalascio").style.visibility = "";');
1801 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1802 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1807 function log_points($remote_addr, $curtime, $user, $where, $mesg)
1809 if (($fp = @fopen(LEGAL_PATH."/points.log", 'a')) != FALSE) {
1810 /* Unix time | session | nickname | IP | where was | mesg */
1811 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
1812 ($user->is_auth() ? 'A' : 'N'),
1813 $user->name, $remote_addr, $where , $mesg));