5 * Copyright (C) 2006-2011 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_MAX_PLAYERS', BIN5_PLAYERS_N);
26 // define(BIN5_SHM_MIN', (50000 * BIN5_MAX_PLAYERS));
27 define('BIN5_SHM_MIN', 32768);
28 define('BIN5_SHM_MAX', (BIN5_SHM_MIN + 1048576));
29 define('BIN5_SHM_DLT', 32768);
30 define('BIN5_PROXY_PATH', PROXY_PATH."/bin5");
32 $mlang_bin5_bin5 = array(
33 'info_part' => array( 'it' => '<hr>Nell\'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>',
34 'en' => '<hr>In the last hand the declarer was <b>%s</b>, the partner was <b>%s</b>,<br>'),
35 'info_capp' => array( 'it' => 'hanno fatto <b>cappotto</b> EBBRAVI!.<br>',
36 'en' => 'and they made <b>capot</b> WELL DONE!.<br>'),
37 'info_pnt' => array( 'it' => 'dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno %s.<br>',
38 'en' => 'they had to do <b>%s</b> points and they had made <b>%d</b>: they have %s.<br>'),
39 'info_alea' => array( 'it' => 'almeno ',
41 'info_more' => array( 'it' => 'più di 60',
43 'info_win' => array( 'it' => '<b>vinto</b>',
44 'en' => '<b>win</b>'),
45 'info_peer' => array( 'it' => '<b>pareggiato</b>',
46 'en' => '<b>drew</b>'),
47 'info_lost' => array( 'it' => '<b>perso</b>',
48 'en' => '<b>lost</b>'),
50 'info_alon' => array( 'it' => '<hr>Nell\'ultima partita <b>%s</b> si è chiamato in mano,<br>',
51 'en' => '<hr>In the last hand <b>%s</b> play alone against each other,<br>'),
52 'info_apnt' => array( 'it' => 'doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha %s.<br>',
53 'en' => 'he/she had to do <b>%s</b> points and they had made <b>%d</b>: he/she had %s.<br>'),
54 'info_acap' => array( 'it' => 'ha fatto <b>cappotto</b> EBBRAVO!.<hr>',
55 'en' => 'and he/she made <b>capot</b> WELL DONE!.<hr>'),
57 'info_omul' => array( 'it' => ' La partita valeva <b>%s</b>.',
58 'en' => ' EN: The game was worth <b>%s</b>.' ),
60 'info_yturn'=> array( 'it' => ' tocca a <b>te</b> giocare.',
61 'en' => ' it\'s <b>your</b> turn.'),
62 'info_turn' => array( 'it' => 'tocca a <b>%s</b> giocare.',
63 'en' => 'it\'s the <b>%s</b>\'s turn.'),
64 'info_mult' => array( 'it' => ' La partita vale <b>%s</b>.',
65 'en' => ' The game worth <b>%s</b>.' ),
66 'info_yshuf'=> array( 'it' => 'Fai <b>tu</b> il mazzo, ',
67 'en' => 'It\'s <b>your</b> shuffled the cards, '),
68 'info_shuf' => array( 'it' => 'Il mazzo a <b>%s</b>, ',
69 'en' => '<b>%s</b> shuffled the cards, '),
70 'btn_bkgame'=> array( 'it' => 'torna alla partita',
71 'en' => 'back to the game'),
72 'call_wptn' => array( 'it' => '<br>con %d punti',
73 'en' => '<br>with %d points'),
74 'call_ycall'=> array( 'it' => 'Chiami%s:',
76 'call_call' => array( 'it' => 'Chiama %s%s:',
77 'en' => 'The declarer is %s%s:')
83 $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.'),
84 '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.') );
86 function multoval($mult)
91 return ($G_lang == 'en' ? 'double' : 'doppio');
93 return ($G_lang == 'en' ? 'triple' : 'triplo');
95 return ($G_lang == 'en' ? 'quadruple' : 'quadruplo');
97 return (sprintf(($G_lang == 'en' ? "%d-ple" : "%d-plo"), $mult));
102 var $value; /* 0 - 39 card value */
103 var $stat; /* 'bunch', 'hand', 'table', 'take' */
104 var $owner; /* (table position 0-4) */
105 // var $pos; /* Pos in hand. */
106 var $x; /* When played the X position on the table of the owner. */
107 var $y; /* When played the Y position on the table of the owner. */
109 function Card($value, $stat, $owner)
111 $this->value = $value;
112 $this->stat = $stat; // Card stat
113 $this->owner = $owner;
116 function assign($stat,$owner)
118 $this->stat = $stat; // Card stat
119 $this->owner = $owner;
122 function setpos($pos)
129 $this->stat = 'table'; // Card stat
134 function take($newown)
136 $this->stat = 'take'; // Card stat
137 $this->owner = $newown;
141 class Bin5_table extends Table {
142 var $card; // il mazzo di carte
143 var $mazzo; // chi e' di mazzo
153 var $points; // points array
154 var $points_n; // number of row of points
168 function Bin5_table()
174 function create($idx)
176 if (($thiz =& new Bin5_table()) == FALSE)
182 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
183 $thiz->asta_pla_n= -1;
184 $thiz->asta_card = -1;
185 $thiz->asta_pnt = -1;
188 $thiz->points = array( );
190 $thiz->total = array( 0, 0, 0, 0, 0);
191 $thiz->asta_win = -1;
192 $thiz->briscola = -1;
196 $thiz->old_reason = "";
197 $thiz->old_asta_pnt = -1;
198 $thiz->old_mult = -1;
201 $thiz->old_friend = -1;
208 function myclone(&$from)
210 if (($thiz =& new Bin5_table()) == FALSE)
215 $thiz->card = $from->card;
216 $thiz->mazzo = $from->mazzo; // REVIEW
217 $thiz->gstart = $from->gstart;
218 $thiz->turn = $from->turn;
220 $thiz->asta_pla = $from->asta_pla;
221 $thiz->asta_pla_n = $from->asta_pla_n;
222 $thiz->asta_card = $from->asta_card;
223 $thiz->asta_pnt = $from->asta_pnt;
225 $thiz->mult = $from->mult;
226 $thiz->points = $from->points;
227 $thiz->points_n = $from->points_n;
228 $thiz->total = $from->total;
230 $thiz->asta_win = $from->asta_win;
231 $thiz->briscola = $from->briscola;
232 $thiz->friend = $from->friend;
234 $thiz->old_reason = $from->old_reason;
235 $thiz->old_asta_pnt = $from->old_asta_pnt;
236 $thiz->old_mult = $from->mult;
237 $thiz->old_pnt = $from->old_pnt;
238 $thiz->old_win = $from->old_win;
239 $thiz->old_friend = $from->old_friend;
245 function parentcopy(&$from)
250 function spawn(&$from)
252 if (($thiz =& new Bin5_table()) == FALSE)
255 $thiz->parentcopy($from);
257 log_main("PLAYER_N - spawn.".$thiz->player_n);
259 $thiz->card = array();
260 $thiz->bunch_create();
261 $thiz->mazzo = rand(0,PLAYERS_N-1);
265 $thiz->old_reason = "";
267 // players are rearranged in an dedicated array
268 $thiz->player = array();
269 for ($i = 0 ; $i < $from->player_n ; $i++)
270 $thiz->player[$i] = $i;
272 log_main("TABLE_OLD_WIN - spawn:".$thiz->old_win);
278 // function bunch_create_old() function AND
282 // for ($i = 0 ; $i < (BIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
283 // // for ($i = 0 ; $i < 40 ; $i++) {
284 // $ret[$i] =& new Card($i, 'bunch', 'no_owner');
291 function bunch_create()
295 for ($i = 0 ; $i < (BIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
296 $this->card[$i] = new Card($i, 'bunch', 'no_owner');
300 function bunch_make()
302 log_main("bunch_make start");
303 $ct = array(0,0,0,0,0);
305 mt_srand(make_seed());
307 for ($i = (BIN5_PLAYERS_N == 5 ? 40 : 24) - 1 ; $i >= 0 ; $i--)
310 for ($i = (BIN5_PLAYERS_N == 5 ? 40 : 24) - 1 ; $i >= 0 ; $i--) {
314 log_main("RND ZERO");
318 $owner = $i % BIN5_PLAYERS_N;
319 $this->card[$id]->assign('hand', $owner);
321 $rest[$rn] = $rest[$i];
322 // $pubbpos[$rn2] = $pubbpos[$i];
324 log_main("bunch_make end");
327 function init($userarr)
330 $this->mazzo = rand(0,PLAYERS_N-1);
334 $this->old_reason = "";
336 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
337 $this->total[$i] = 0;
338 $user_cur = $userarr[$this->player[$i]];
339 $user_cur->exitislock = TRUE;
342 log_main("table::init: ci siamo");
345 function game_init($userarr)
349 $this->gstart = ($this->mazzo+1) % BIN5_PLAYERS_N;
353 $this->asta_pla_n = BIN5_PLAYERS_N;
354 $this->asta_card = -1;
355 $this->asta_pnt = 60;
356 $this->asta_win = -1;
357 $this->briscola = -1;
361 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
362 $this->asta_pla[$i] = TRUE;
363 $user_cur = $userarr[$this->player[$i]];
364 $user_cur->subst = 'asta';
365 $user_cur->asta_card = -2;
366 $user_cur->asta_pnt = -1;
367 $user_cur->handpt = $this->hand_points($i);
368 // SEE function calculate_points(&$table)
375 $this->mazzo = ($this->mazzo + 1) % BIN5_PLAYERS_N;
379 function hand_points($idx)
381 GLOBAL $G_all_points;
385 for ($i = 0 ; $i < (BIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
386 // for ($i = 0 ; $i < 40 ; $i++) {
387 if ($this->card[$i]->owner != $idx)
390 $ctt = $this->card[$i]->value % 10;
391 $tot += $G_all_points[$ctt];
398 function exitlock_show($userarr, $table_pos)
400 $ct = $this->exitlock_calc($userarr, $table_pos);
402 $ret = sprintf('exitlock_show(%d, %s);', $ct,
403 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
407 function exitlock_calc(&$userarr, $table_pos)
411 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
412 if ($userarr[$this->player[$i]]->exitislock == FALSE)
418 } // end class Bin5_table
423 define('BIN5_USER_FLAG_RING_ENDAUCT', 0x01);
425 class Bin5_user extends User {
428 var $handpt; // Total card points at the beginning of the current hand.
429 var $exitislock; // Player can exit from the table ?
430 var $privflags; // Flags for briskin5 only
438 function create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
439 if (($thiz =& new User()) == FALSE)
442 $thiz->asta_card = -2;
443 $thiz->asta_pnt = -1;
445 $thiz->exitislock = TRUE;
446 $thiz->privflags = 0;
452 function parentcopy(&$from)
457 function copy(&$from)
459 $this->parentcopy($from);
461 $this->asta_card = $from->asta_card;
462 $this->asta_pnt = $from->asta_pnt;
463 $this->handpt = $from->handpt;
464 $this->exitislock = $from->exitislock;
465 $this->privflags = $from->privflags;
469 function myclone(&$from)
471 if (($thiz =& new User()) == FALSE)
480 function spawn($from, &$bri, $table, $table_pos)
482 GLOBAL $CO_bin5_pref_ring_endauct;
484 if (($thiz = new Bin5_user()) == FALSE)
487 $thiz->parentcopy($from);
489 /* NOTE: at this moment idx and table_pos fields have the same value
490 but diffentent functions, we keep them separated for a while */
492 $thiz->idx = $table_pos;
493 $thiz->asta_card = -2;
494 $thiz->asta_pnt = -1;
496 $thiz->exitislock = TRUE;
498 log_wr("Bin5 constructor");
500 $this->privflags = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
502 $thiz->table_orig = $table;
504 $thiz->table_pos = $table_pos;
511 function step_set($step)
513 $this->step = $step & 0x7fffffff;
518 function step_inc($delta = 1) {
519 $this->step += $delta;
520 /* modularization because unpack() not manage unsigned 32bit int correctly */
521 $this->step &= 0x7fffffff;
526 static function load_step($tab_id, $sess)
530 if (validate_sess($sess) == FALSE)
533 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
534 mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
535 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step", 'rb')) == FALSE)
537 if (($s = fread($fp, 8)) == FALSE)
539 if (mb_strlen($s, "ASCII") != 8)
541 $arr = unpack('Ls/Li', $s);
544 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
551 log_rd2("STEP_GET [".$sess."]: return false ");
559 if (validate_sess($this->sess) == FALSE)
561 if (file_exists(BIN5_PROXY_PATH."/table".$this->table_orig) == FALSE)
562 mkdir(BIN5_PROXY_PATH."/table".$this->table_orig, 0775, TRUE);
563 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$this->table_orig."/".$this->sess.".step", 'w')) == FALSE)
565 fwrite($fp, pack("LL",$this->step, $this->idx));
568 log_main("step_set [".$this->sess. "] [".$this->step."]");
576 static function unproxy_step($tab_id, $sess)
578 log_rd2("UNPROXY: ".BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
579 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
582 @unlink(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
585 static function load_data($tab_id, $id, $sess)
587 log_load("Bin5_user::load_data: tab_id [".$tab_id."] id [".$id."] sess [".($sess == FALSE ? "FALSE" : $sess)."] ");
590 if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$id, "B")) == -1) {
591 log_main("ftok failed");
595 if (($shm_sz = sharedmem_sz($tok)) == -1) {
596 log_main("shmop_open failed");
600 $shm_sz = SHM_DIMS_U_MIN;
602 if ($shm = shm_attach($tok, $shm_sz)) {
603 if (($user = @shm_get_var($shm, $tok)) == FALSE) {
607 if ($sess != FALSE && $user->sess != $sess) {
610 log_only("user == ".($user == FALSE ? "FALSE" : "TRUE")." user === ".($user === FALSE ? "FALSE" : "TRUE")." user isset ".(isset($user) ? "TRUE" : "FALSE"));
612 if ($user == FALSE) {
613 log_only("INIT MAIN DATA");
615 // SHSPLIT FIXME: init_data for User class ??
616 $user = User::create($id, "", "");
617 if (@shm_put_var($shm, $tok, $user) == FALSE) {
618 log_shme("Bin5_user::save_data2");
620 log_only("PUT_VAR FALLITA ".strlen(serialize($user)));
621 log_only(serialize($user));
625 if ($sess != FALSE) {
626 // This part isn't strictly required but is good to verify
627 // the coerence of cached and User class saved value of step field.
628 $old_step = $user->step;
629 $arr = Bin5_user::load_step($tab_id, $sess);
630 $user->step = $arr['s'];
631 if ($old_step != $user->step) {
632 log_crit("Bin5:: steps are diffetents User->step ".$user->step." Old_step: ".$old_step);
638 $user->shm_sz = $shm_sz;
644 // SHSPLIT: load users from the shared memory
649 log_crit("Bin5_user::load_data:ret FALSE");
654 static function save_data($user, $tab_id, $id)
660 if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$id, "B")) == -1) {
663 while ($user->shm_sz < SHM_DIMS_U_MAX) {
664 if (($shm = shm_attach($tok, $user->shm_sz)) == FALSE)
667 // log_only("PUT_VAR DI ".strlen(serialize($user)));
668 if (@shm_put_var($shm, $tok, $user) != FALSE) {
671 log_main("User[".$id."] saved.");
673 log_shme("Bin5_user::save_data");
677 if (shm_remove($shm) === FALSE) {
678 log_only("REMOVE FALLITA");
682 $user->shm_sz += SHM_DIMS_U_DLT;
691 function destroy_data($tab_id)
694 if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos, "B")) == -1) {
695 log_crit("BIN5 USER DATA REMOVE FAILED 1 [".FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos."]");
699 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
700 log_crit("BIN5 USER DATA REMOVE FAILED 2");
703 if (shmop_delete($shm) == 0) {
704 log_crit("BIN5 USER DATA REMOVE FAILED 3");
709 log_main("BIN5 USER DATA DESTROY SUCCESS");
711 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
721 static function blocking_error($is_unrecoverable)
723 GLOBAL $is_page_streaming;
725 $is_page_streaming = TRUE;
726 log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
727 return (sprintf(($is_unrecoverable ? 'hstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
730 protected function page_sync($sess, $page)
732 GLOBAL $is_page_streaming;
734 $is_page_streaming = TRUE;
735 log_rd2("PAGE_SYNC");
736 printf("xXx BIN5_USER::PAGE_SYNC\n");
737 return (sprintf('hstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
740 protected function maincheck($cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate, $table_idx, $table_token)
742 GLOBAL $G_lang, $mlang_indrd, $is_page_streaming;
743 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
744 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
745 $CO_splashdate = "CO_splashdate".$G_splash_idx;
746 $$CO_splashdate = $splashdate;
750 log_rd("maincheck begin");
755 /* if ((($curtime - $this->lacc) > STREAM_TIMEOUT) || Room::garbage_time_is_expired($curtime)) { */
756 /* $S_load_stat['lL_laccgarb']++; */
758 /* if (($curtime - $this->lacc) >= STREAM_TIMEOUT) { */
759 /* $S_load_stat['wU_lacc_upd']++; */
760 /* $this->lacc = $curtime; */
763 /* if (Room::garbage_time_is_expired($curtime)) { */
766 /* $S_load_stat['wR_garbage']++; */
767 /* log_main("pre garbage_manager TRE"); */
768 /* $this->room->garbage_manager(FALSE); */
772 /* Nothing changed, return. */
773 if ($cur_step == $this->step)
776 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$this->stat."] cur_step[".$cur_step."] user_step[".$this->step."]");
778 if ($cur_step == -1) {
780 * if $cur_step == -1 load the current state from the main struct
783 $S_load_stat['wR_minusone']++;
785 if ($this->the_end == TRUE) {
786 log_rd2("main_check: the end".var_export(debug_backtrace()));
787 $is_page_streaming = TRUE;
790 if ($this->trans_step != -1) {
791 log_rd2("TRANS USATO ".$this->trans_step);
792 $cur_step = $this->trans_step;
793 $this->trans_step = -1;
796 log_rd2("TRANS NON ATTIVATO");
801 /* this part I suppose is read only on $this->room structure */
802 if ($cur_step == -1) {
803 log_rd2("PRE-NEWSTAT");
810 if ($this->stat == 'table') {
812 /* NOTE: $this->room is associated with the current $bri object */
813 printf("xXx CLASS NAME [%s]\n", get_class($this->room));
814 $ret = show_table(&$this->room, $this, $this->step, FALSE, FALSE);
816 log_rd2("NEWSTAT: ".$this->stat);
818 $new_stat = $this->stat;
819 $new_subst = $this->subst;
820 $new_step = $this->step;
821 } /* if ($cur_step == -1) { */
823 /* $sem = Room::lock_data(FALSE); */
824 $S_load_stat['rU_heavy']++;
826 if ($cur_step < $this->step) {
828 if ($cur_step + COMM_N < $this->step) {
829 if (($cur_stat != $this->stat)) {
830 $to_stat = $this->stat;
831 /* Room::unlock_data($sem); */
833 printf("xXx BIN5_USER::MAINCHECK\n");
834 return ($this->page_sync($this->sess, ($to_stat == "table" ? "index.php" : "../index.php"), $this->table, $this->table_token));
836 log_rd2("lost history, refresh from scratch");
837 printf("xXx LOST HISTORY!\n");
841 for ($i = $cur_step ; $i < $this->step ; $i++) {
843 log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
844 $ret .= $this->comm[$ii];
846 $new_stat = $this->stat;
847 $new_subst = $this->subst;
848 $new_step = $this->step;
851 log_rd2($this->step, 'index_rd.php: after ret set');
853 if ($this->the_end == TRUE) {
854 log_rd2("LOGOUT BYE BYE!!");
855 log_auth($this->sess, "Explicit logout.");
857 if ($this->the_end == TRUE) {
861 /* if ($this->subst == 'sitdown') { */
862 /* log_load("ROOM WAKEUP"); */
863 /* $this->room->room_wakeup($this); */
865 /* else if ($this->subst == 'standup') */
866 /* $this->room->room_outstandup($this); */
868 /* log_rd2("LOGOUT FROM WHAT ???"); */
870 } /* if ($this->the_end == TRUE) { ... */
871 } /* if ($this->the_end == TRUE) { ... */
872 } /* if ($cur_step < $this->step) { */
874 /* Room::unlock_data($sem); */
875 } /* else of if ($cur_step == -1) { */
879 } // function maincheck (...
886 } // end class Bin5_user
891 static $delta_t = array();
895 var $comm; // commands for many people
896 var $step; // current step of the comm array
897 // externalized var $garbage_timeout;
906 static function page_manager($room, $header_out, $path, $method, $addr, $get, $post, $cookie)
912 bin5_index_main($room, $header_out, $addr, $get, $post, $cookie);
913 $content = ob_get_contents();
916 force_no_cache($header_out);
918 $pgflush = new PageFlush($new_socket, $curtime, 20, $header_out, $content);
920 if ($pgflush->try_flush($curtime) == FALSE) {
921 // Add $pgflush to the pgflush array
922 array_push($pages_flush, $pgflush);
929 function Bin5 ($room, $table_idx, $table_token) {
930 $this->user = array();
931 $this->table = array();
933 $this->the_end = FALSE;
934 $this->shm_sz = BIN5_SHM_MIN;
935 if (($this->tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
941 $table = $room->table[$table_idx];
943 log_wr("Bin5 constructor");
945 for ($i = 0 ; $i < $table->player_n ; $i++) {
946 $user[$table->player[$i]]->table_token = $table_token;
947 $this->user[$i] = Bin5_user::spawn($user[$table->player[$i]], $this, $table_idx, $i);
949 $this->table[0] = Bin5_table::spawn(&$table);
951 log_main("TABLE_OLD_WIN - Bin5:".$this->table[0]->old_win);
953 $this->table_idx = $table_idx;
954 $this->table_token = $table_token;
955 Bin5::garbage_time_expire_set($table_idx, 0);
957 log_wr("Bin5 constructor end");
961 function get_user($sess, &$idx)
965 if (validate_sess($sess)) {
966 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
967 if (strcmp($sess, $this->user[$i]->sess) == 0) {
970 $ret = &$this->user[$i];
974 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
975 // for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++)
976 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
979 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
986 function garbage_manager($force)
990 /* Garbage collector degli utenti in timeout */
993 // externalized if ($force || $this->garbage_timeout < $curtime) {
994 if ($force || Bin5::garbage_time_is_expired($this->table_idx, $curtime)) {
995 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
996 $user_cur = $this->user[$i];
997 if ($user_cur->sess == "" ||
998 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
1001 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
1002 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
1004 if ($user_cur->stat == 'table') {
1005 log_auth($user_cur->sess," bin5 Autologout session.");
1007 /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE
1009 $tmp_sess = $user_cur->sess;
1010 $user_cur->sess = "";
1011 Bin5_user::step_unproxy($tmp_sess);
1012 $user_cur->name = "";
1013 $user_cur->the_end = FALSE;
1017 /* se gli altri utenti non erano d'accordo questo utente viene bannato */
1018 $remcalc = $this->table[0]->exitlock_calc(&$this->user, $user_cur->table_pos);
1020 require_once("${G_base}Obj/hardban.phh");
1021 Hardbans::add(($user_cur->flags & USER_FLAG_AUTH ? $user_cur->name : FALSE),
1022 $user_cur->ip, $user_cur->sess, $user_cur->laccwr + BAN_TIME);
1024 // $user->bantime = $user->laccwr + BAN_TIME;
1026 $this->table_wakeup($user_cur);
1030 log_rd2($user_cur->sess." GARBAGE UPDATED!");
1032 // externalized $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
1033 Bin5::garbage_time_expire_set($this->table_idx, $curtime + GARBAGE_TIMEOUT);
1042 static function load_data($table_idx, $table_token = "")
1046 log_wr("TABLE_FTOK ".FTOK_PATH."/bin5/table".$table_idx."/table");
1049 if (($tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
1050 log_main("ftok failed");
1054 if (($shm_sz = sharedmem_sz($tok)) == -1) {
1055 log_main("shmop_open failed");
1059 if (($shm = shm_attach($tok, $shm_sz)) == FALSE)
1062 if (($bri = @shm_get_var($shm, $tok)) == FALSE)
1065 if ($table_token != "" && $bri->table_token != $table_token) {
1066 log_wr("bri->table_token: ".$bri->table_token."table_token: ".$table_token);
1073 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1074 if (($bri->user[$i] = Bin5_user::load_data($table_idx, $i, FALSE)) == FALSE) {
1075 log_crit("Bin5_user::load_data failed");
1079 if ($i < BIN5_MAX_PLAYERS) {
1089 log_wr("briskin5 load_data failed");
1096 function save_data($bri)
1103 log_main("SAVE BRISKIN5 DATA");
1105 if (!isset($bri->tok))
1108 $user_park = array();
1109 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1110 $user_park[$i] = $bri->user[$i];
1111 $bri->user[$i] = FALSE;
1114 while ($bri->shm_sz < BIN5_SHM_MAX) {
1115 if (($shm = shm_attach($bri->tok, $bri->shm_sz)) == FALSE)
1118 if (@shm_put_var($shm, $bri->tok, $bri) != FALSE) {
1120 log_shme("Bin5::save_data");
1125 if (shm_remove($shm) === FALSE) {
1126 log_only("REMOVE FALLITA");
1130 $bri->shm_sz += BIN5_SHM_DLT;
1136 // SHSPLIT: reattach users to the room class
1137 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1138 Bin5_user::save_data($user_park[$i], $bri->table_idx, $i);
1139 $bri->user[$i] = $user_park[$i];
1141 log_load("FINISH: ".($ret == TRUE ? "TRUE" : "FALSE"));
1148 function destroy_data()
1154 log_main("DESTROY BRISKIN5 DATA");
1157 log_main("DESTROY2 BRISKIN5 DATA [".$this->table_idx."]");
1158 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1159 $this->user[$i]->destroy_data($this->table_idx);
1160 Bin5_user::unproxy_step($this->table_idx, $this->user[$i]->sess);
1162 if (($tok = @ftok(FTOK_PATH."/bin5/table".$this->table_idx."/table", "B")) == -1)
1165 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
1168 if (shmop_delete($shm) == 0) {
1169 log_only("REMOVE FALLITA ");
1174 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
1176 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
1186 static function lock_data($is_exclusive, $table_idx)
1188 if (($res = file_lock(FTOK_PATH."/bin5/table".$table_idx."/table", $is_exclusive)) != FALSE) {
1189 self::$delta_t = microtime(TRUE);
1190 log_lock("LOCK table [".$table_idx."] [".self::$delta_t[$table_idx]."]");
1192 return (new Vect(array('res' => $res, 'tab' => $table_idx)));
1199 static function unlock_data($res_vect)
1203 $res = $res_vect->getbyid('res');
1204 $tab = $res_vect->getbyid('tab');
1206 log_lock("UNLOCK table [".$tab."] [".(microtime(TRUE) - (self::$delta_t[$tab]))."]");
1212 function chatt_send($user, $mesg)
1214 GLOBAL $mlang_brisk, $G_lang;
1216 if ($user->stat == 'table') {
1217 $table = &$this->table[$user->table];
1220 $user_mesg = substr($mesg,6);
1224 $dt = date("H:i ", $curtime);
1225 if (strncmp($user_mesg, "/nick ", 6) == 0) {
1226 log_main($user->sess." chatt_send BEGIN");
1228 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
1229 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1230 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
1235 $user_mesg = "COMMAND ".$user_mesg;
1238 // update local graph
1239 // update remote graphs
1240 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1241 $user_cur = $this->user[$i];
1242 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1243 if ($user_cur->sess == '')
1245 if (strcasecmp($user_cur->name, $name_new) == 0) {
1246 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1248 $premsg = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
1249 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $premsg);
1254 if ($i == BIN5_MAX_PLAYERS) {
1255 if ($user->flags & USER_FLAG_AUTH && strcasecmp($user->name,$name_new) != 0) {
1256 if ($this->table[$user->table]->auth_only == TRUE) {
1257 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1258 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>%s</b>");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
1263 $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
1267 $user->name = $name_new;
1269 log_main($user->sess." chatt_send start set");
1272 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1273 log_main($user->sess." chatt_send set loop");
1275 $user_cur = &$this->user[$i];
1276 if ($user_cur->sess == '')
1279 if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
1280 $table = &$this->table[$user->table];
1282 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1283 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1284 $this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->flags,
1285 xcape($this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->name),
1287 $this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->flags,
1288 xcape($this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->name),
1290 $this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->flags,
1291 xcape($this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->name),
1293 (BIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->flags),
1294 (BIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->name)),
1296 (BIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->flags),
1297 (BIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->name)));
1298 if ($user_cur == $user) {
1299 $user_cur->comm[$user_cur->step % COMM_N] .= $user_cur->myname_innerHTML();
1301 $user_cur->step_inc();
1307 for ($i = 0 ; $i < ($user->stat == 'room' ? BIN5_MAX_PLAYERS : BIN5_PLAYERS_N) ; $i++) {
1308 if ($user->stat == 'room') {
1309 $user_cur = &$this->user[$i];
1310 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1314 $user_cur = &$this->user[$table->player[$i]];
1317 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1318 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
1319 $dt, $user->flags, xcape($user->name), xcape($user_mesg));
1320 $user_cur->step_inc();
1322 log_legal($curtime, 'xxx', $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
1326 function table_wakeup($user)
1328 $table = &$this->table[0];
1330 log_main("BIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
1334 log_main("BIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1336 for ($i = 0 ; $i < $table->player_n ; $i++) {
1337 $user_cur = &$this->user[$i];
1338 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1340 if ($user_cur == $user)
1341 $user_cur->subst = "shutdowner";
1343 $user_cur->subst = "shutdowned";
1344 $user_cur->laccwr = $curtime;
1346 $ret = "gst.st = ".($user_cur->step+1)."; ";
1347 $ret .= 'gst.st_loc++; hstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
1349 log_wr($user_cur->sess." BIN5_WAKEUP: ".$ret);
1350 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1351 $user_cur->step_inc();
1354 $this->the_end = TRUE;
1358 * If all players are freezed the room garbage_manager clean up table and users.
1360 function is_abandoned()
1365 $table = &$this->table[0];
1367 for ($i = 0 ; $i < $table->player_n ; $i++) {
1368 $user_cur = &$this->user[$i];
1370 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
1380 static function garbage_time_is_expired($tab_id, $tm)
1385 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
1386 mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
1387 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/garbage_time.expired", 'rb')) == FALSE)
1389 if (($s = fread($fp, 4)) == FALSE)
1391 if (mb_strlen($s, "ASCII") != 4)
1393 $arr = unpack('Le', $s);
1394 if ($arr['e'] > $tm)
1401 log_rd2("END: return ".($ret ? "TRUE" : "FALSE"));
1406 static function garbage_time_expire_set($tab_id, $tm)
1409 if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
1410 mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
1411 if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/garbage_time.expired", 'wb')) == FALSE)
1413 fwrite($fp, pack("L",$tm));
1422 static function request_mgr(&$s_a_p, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
1424 printf("NEW_SOCKET (root): %d\n", intval($new_socket));
1426 if (($table_idx = gpcs_var('table_idx', $get, $post, $cookie)) === FALSE)
1429 if (($table_token = gpcs_var('table_token', $get, $post, $cookie)) === FALSE)
1430 unset($table_token);
1436 bin5_index_main($header_out, $addr, $get, $post, $cookie);
1437 $content = ob_get_contents();
1440 force_no_cache($header_out);
1442 $s_a_p->pgflush_try_add($new_socket, 20, $header_out, $content);
1446 case "index_wr.php":
1447 $bri = $s_a_p->app->match_get($table_idx, $table_token);
1449 bin5_index_wr_main($bri, $addr, $get, $post, $cookie);
1450 $content = ob_get_contents();
1453 force_no_cache($header_out);
1455 $s_a_p->pgflush_try_add($new_socket, 20, $header_out, $content);
1459 case "index_rd_ifra.php":
1461 if (($bri = $s_a_p->app->match_get($table_idx, $table_token)) == NULL
1462 || !isset($cookie['sess'])
1463 || (($user = $bri->get_user($cookie['sess'], $idx)) == FALSE)) {
1465 $content = Bin5_user::stream_fini(TRUE);
1466 $s_a_p->pgflush_try_add($new_socket, 20, $header_out, $content);
1471 // close a previous opened index_read_ifra socket, if exists
1472 if (($prev = $user->rd_socket_get()) != NULL) {
1473 $s_a_p->socks_unset($user->rd_socket_get());
1474 fclose($user->rd_socket_get());
1475 printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
1476 $user->rd_socket_set(NULL);
1480 $user->stream_init($header_out, $content, $get, $post, $cookie);
1481 $response = headers_render($header_out, -1).chunked_content($content);
1482 $response_l = mb_strlen($response, "ASCII");
1484 $wret = @fwrite($new_socket, $response, $response_l);
1485 if ($wret < $response_l) {
1486 printf("TROUBLES WITH FWRITE: %d\n", $wret);
1487 $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
1490 $user->rd_cache_set("");
1492 fflush($new_socket);
1495 $s_a_p->socks_set($new_socket, $user);
1496 $user->rd_socket_set($new_socket);
1497 printf(" - qui ci siamo - ");
1516 function locshm_exists($tok)
1520 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
1521 log_main($tok." SHM NOT exists");
1527 log_main($tok." SHM exists");
1534 function asta2mult($asta_pnt)
1536 if ($asta_pnt > 110)
1538 else if ($asta_pnt > 100)
1540 else if ($asta_pnt > 90)
1542 else if ($asta_pnt > 80)
1544 else if ($asta_pnt > 70)
1550 // rendiamo qui l'elenco dei punti come return della func
1551 function calculate_points(&$table)
1553 GLOBAL $G_all_points;
1558 if ($table->asta_pnt == 60)
1559 $table->asta_pnt = 61;
1561 $table->old_reason = "";
1562 $table->old_win = $table->asta_win;
1563 $table->old_friend = $table->friend;
1564 $table->old_asta_pnt = $table->asta_pnt;
1565 $table->old_mult = $table->mult;
1567 for ($i = 0 ; $i < (BIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1568 // for ($i = 0 ; $i < 40 ; $i++) {
1569 $ctt = $table->card[$i]->value % 10;
1570 $own = $table->card[$i]->owner;
1571 if ($own == $table->asta_win || $own == $table->friend)
1572 $pro += $G_all_points[$ctt];
1575 log_wr(sprintf("PRO: [%d]", $pro));
1578 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1579 $table->points[$table->points_n % MAX_POINTS] = array();
1580 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1581 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1585 $table->old_pnt = $pro;
1591 if ($pro >= $table->asta_pnt)
1596 $gamult = asta2mult($table->asta_pnt);
1598 $table->points[$table->points_n % MAX_POINTS] = array();
1599 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1600 if ($i == $table->asta_win)
1601 $pt = ($i == $table->friend ? 4 : 2);
1602 else if ($i == $table->friend)
1609 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1611 $pt = $pt * $sig * ($gamult + $table->mult) * ($pro == 120 ? 2 : 1);
1613 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1615 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1616 $table->total[$i] += $pt;
1620 $table->old_pnt = $pro;
1627 is_transition (is from room to table ?)
1628 is_again (is another game)
1630 Examples of $is_transition, $is_again:
1631 from reload of the page: FALSE, FALSE
1632 from sitdown in room: TRUE, FALSE
1633 from table: asta cmd e tutti passano: TRUE, TRUE
1634 from table: fine partita: TRUE, TRUE
1636 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1638 $table_idx = $user->table;
1639 $table = $bri->table[$table_idx];
1640 $table_pos = $user->table_pos;
1642 $ret = "table_init();";
1643 $ret .= $table->exitlock_show(&$bri->user, $table_pos);
1645 /* GENERAL STATUS */
1646 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1647 $sendstep, $user->stat, $user->subst, $table_pos);
1649 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1652 $ret .= "background_set();";
1655 $ret .= $user->myname_innerHTML();
1656 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1657 $bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->flags,
1658 xcape($bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->name),
1660 $bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->flags,
1661 xcape($bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->name),
1663 $bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->flags,
1664 xcape($bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->name),
1666 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->flags),
1667 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->name)),
1669 (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->flags),
1670 (BIN5_PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->name)));
1672 /* NOTIFY FOR THE CARD MAKER */
1673 if ($is_transition) { // && $user->subst == "asta" superfluo
1674 $ret .= show_table_info(&$bri, &$table, $table_pos);
1677 $ret .= table_wellcome($user);
1679 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1680 $ret .= playsound("cow.mp3");
1685 if ($is_transition) { // && $user->subst == "asta" superfluo
1688 for ($i = 0 ; $i < 8 ; $i++) {
1689 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++) {
1691 for ($o = 0 ; $o < (BIN5_PLAYERS_N == 5 ? 40 : 24) && $ct < $i+1 ; $o++) {
1692 // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1693 if ($table->card[$o]->owner == (($e + $table->gstart) % BIN5_PLAYERS_N)) {
1699 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1701 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BIN5_PLAYERS_N,
1702 $i, ((($e + BIN5_PLAYERS_N - $table_pos + $table->gstart) % BIN5_PLAYERS_N) == 0 ?
1703 $table->card[$o]->value : -1),
1704 ($i == 7 && $e == (BIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1709 $taked = array(0,0,0,0,0);
1710 $inhand = array(0,0,0,0,0);
1711 $ontabl = array(-1,-1,-1,-1,-1);
1714 for ($i = 0 ; $i < (BIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1715 // for ($i = 0 ; $i < 40 ; $i++) {
1716 if ($table->card[$i]->stat == 'hand') {
1717 if ($table->card[$i]->owner == $table_pos) {
1718 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1720 $inhand[$table->card[$i]->owner]++;
1722 else if ($table->card[$i]->stat == 'take') {
1723 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1724 $taked[$table->card[$i]->owner]++;
1726 else if ($table->card[$i]->stat == 'table') {
1727 $ontabl[$table->card[$i]->owner] = $i;
1731 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1732 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1734 log_main("Stat table: ".$logg);
1736 /* Set ours cards. */
1738 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1739 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1740 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
1741 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1742 $ret .= sprintf('card_setours(%s);', $oursarg);
1744 /* Dispose all cards */
1745 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1746 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1747 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1748 ($inhand[$i] <= 8 ? $inhand[$i] : 8) , $taked[$i]);
1750 if ($ontabl[$i] != -1) {
1751 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1752 $table->card[$ontabl[$i]]->value,
1753 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1759 if ($user->subst == 'asta') {
1761 /* show users auction status */
1763 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1764 $user_cur = &$bri->user[$table->player[$i]];
1765 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1766 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1768 if (BIN5_PLAYERS_N == 3)
1769 $showst .= ",-2,-2";
1770 $ret .= sprintf('document.title = "Brisk - Tavolo %d (asta)";', $user->table_orig);
1771 $ret .= sprintf('show_astat(%s);', $showst);
1773 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1774 /* show card chooser */
1775 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1780 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N) &&
1781 $table->asta_win == -1)
1782 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1783 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1785 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1786 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1790 if ($table->asta_win == -1) { // auction case
1791 if ($table_pos == ($table->gstart % BIN5_PLAYERS_N))
1792 $ret .= "remark_on();";
1794 $ret .= "remark_off();";
1796 else { // chooseed case
1797 if ($table_pos == $table->asta_win)
1798 $ret .= "remark_on();";
1800 $ret .= "remark_off();";
1803 else if ($user->subst == 'game') {
1805 if (($table->gstart + $table->turn) % BIN5_PLAYERS_N == $table_pos)
1806 $ret .= "is_my_time = true; remark_on();";
1808 $ret .= "remark_off();";
1810 /* WHO CALL AND WHAT */
1811 $ret .= briscola_show($bri, $table, $user);
1815 } // end function show_table(...
1817 function calculate_winner(&$table)
1824 $cur_seed = $table->briscola - ($table->briscola % 10);
1826 for ($i = 0 ; $i < (BIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1827 // for ($i = 0 ; $i < 40 ; $i++) {
1828 if ($table->card[$i]->stat != "table")
1831 log_wr(sprintf("Card On table: [%d]", $i));
1833 $v = $table->card[$i]->value;
1834 $ontab[$table->card[$i]->owner] = $v;
1835 $ontid[$table->card[$i]->owner] = $i;
1836 /* se briscola setto il flag */
1837 if (($v - ($v % 10)) == $cur_seed)
1841 if ($briontab == FALSE) {
1842 $cur_win = $table->gstart;
1843 $cur_val = $ontab[$cur_win];
1844 $cur_seed = $cur_val - ($cur_val % 10);
1847 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1848 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1849 if ($ontab[$i] < $cur_val) {
1850 $cur_val = $ontab[$i];
1856 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1857 $table->card[$ontid[$i]]->owner = $cur_win;
1858 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1863 function show_table_info(&$bri, &$table, $table_pos)
1865 GLOBAL $G_lang, $mlang_bin5_bin5;
1868 $user = $bri->user[$table->player[$table_pos]];
1870 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1871 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1874 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++)
1875 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1876 $noty .= sprintf("</tr>");
1879 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1881 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1882 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1883 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1884 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1889 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1890 for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
1891 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1892 $noty .= "</tr></table>";
1894 if ($table->old_reason != "") {
1895 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1898 if ($table->old_win != -1) {
1899 log_main("TABLE_OLD_WIN:".$table->old_win);
1900 $win = $table->player[$table->old_win];
1901 log_main("TABLE_OLD_FRIEND:".$table->old_friend);
1902 $fri = $table->player[$table->old_friend];
1904 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1907 /* MLANG: "<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>", "hanno fatto <b>cappotto</b> EBBRAVI!.<hr>", "dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>", "<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>", "ha fatto <b>cappotto</b> EBBRAVO!.<hr>", "doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>", ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt : 'più di 60'), $table->old_pnt, ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso" */
1909 $noty .= sprintf($mlang_bin5_bin5['info_part'][$G_lang],
1910 xcape($bri->user[$win]->name),
1911 xcape($bri->user[$fri]->name));
1912 if ($table->old_pnt == 120) {
1913 $noty .= sprintf($mlang_bin5_bin5['info_capp'][$G_lang]);
1916 $noty .= sprintf($mlang_bin5_bin5['info_pnt'][$G_lang],
1917 ($table->old_asta_pnt > 61 ? $mlang_bin5_bin5['info_alea'][$G_lang].$table->old_asta_pnt :
1918 $mlang_bin5_bin5['info_more'][$G_lang]), $table->old_pnt,
1919 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] : ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] : $mlang_bin5_bin5['info_lost'][$G_lang])));
1923 $noty .= sprintf($mlang_bin5_bin5['info_alon'][$G_lang],
1924 xcape($bri->user[$win]->name));
1925 if ($table->old_pnt == 120) {
1926 $noty .= sprintf($mlang_bin5_bin5['info_acap'][$G_lang]);
1929 $noty .= sprintf($mlang_bin5_bin5['info_apnt'][$G_lang],
1931 ($table->old_asta_pnt > 61 ? $mlang_bin5_bin5['info_alea'][$G_lang].$table->old_asta_pnt :
1932 $mlang_bin5_bin5['info_more'][$G_lang]), $table->old_pnt,
1934 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] : ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] : $mlang_bin5_bin5['info_lost'][$G_lang])));
1937 if (($table->old_mult + asta2mult($table->old_asta_pnt)) > 1) {
1938 $noty .= sprintf($mlang_bin5_bin5['info_omul'][$G_lang], multoval($table->old_mult + asta2mult($table->old_asta_pnt)));
1940 $noty .= "<hr><br>";
1942 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1943 if ($table->mazzo == $table_pos)
1944 $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
1946 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1947 $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
1950 if ($user->subst == 'asta') {
1951 if ($table->asta_win == -1) // auction case
1952 $curplayer = $table->gstart % BIN5_PLAYERS_N;
1954 $curplayer = $table->asta_win;
1956 else if ($user->subst == 'game') {
1957 $curplayer = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
1960 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1961 if ($curplayer == $table_pos) {
1962 $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
1965 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1966 $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
1969 if (($table->mult + asta2mult($table->asta_pnt)) > 1) {
1970 $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($table->mult + asta2mult($table->asta_pnt)));
1972 $noty .= "<hr><br>";
1973 $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
1974 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
1975 * 130 + ($table->points_n > 0 ? 50 : 0) +
1976 * (120 * ($table->points_n / MAX_POINTS)));
1977 * will be used when we refact notify js function following
1978 * photoo class logic
1984 function table_wellcome($user)
1986 GLOBAL $table_wellarr, $G_lang;
1989 for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
1990 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
1996 function briscola_show($bri, $table, $user)
1998 GLOBAL $G_lang, $mlang_bin5_bin5;
2002 if ($table->asta_card == 9)
2003 $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
2005 /* text of caller cell */
2006 if ($user->table_pos == $table->asta_win) {
2007 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
2008 $ret .= sprintf($prestr, $ptnadd);
2011 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
2012 $ret .= sprintf($prestr,
2013 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
2015 $ret .= sprintf('set_iscalling(%d);', ($table->asta_win - $user->table_pos + BIN5_PLAYERS_N) % BIN5_PLAYERS_N);
2017 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
2019 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
2020 $ret .= sprintf('$("caller").style.visibility = "visible";');
2021 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
2022 $ret .= sprintf('$("astalascio").style.visibility = "";');
2023 $ret .= sprintf('$("asta").style.visibility = "hidden";');
2024 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
2030 function game_result($asta_pnt, $pnt)
2032 if ($asta_pnt == 61) {
2035 else if ($pnt == 60)
2041 if ($pnt >= $asta_pnt)
2048 function log_points($remote_addr, $curtime, $user, $where, $mesg)
2050 if (($fp = @fopen(LEGAL_PATH."/points.log", 'a')) != FALSE) {
2051 /* Unix time | session | nickname | IP | where was | mesg */
2052 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
2053 ($user->flags & USER_FLAG_AUTH ? 'A' : 'N'),
2054 $user->name, $remote_addr, $where , $mesg));