5 * Copyright (C) 2006-2009 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.
26 define(BRISKIN5_PLAYERS_N, 3);
27 define(BRISKIN5_MAX_PLAYERS, BRISKIN5_PLAYERS_N);
28 // define(BRISKIN5_SHM_MIN, (50000 * BRISKIN5_MAX_PLAYERS));
29 define(BRISKIN5_SHM_MIN, 32768);
30 define(BRISKIN5_SHM_MAX, (BRISKIN5_SHM_MIN + 1048576));
31 define(BRISKIN5_SHM_DLT, 32768);
33 $mlang_bin5_bin5 = array(
34 'info_part' => array( 'it' => '<hr>Nell\'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>',
35 'en' => '<hr>In the last hand the declarer was <b>%s</b>, the partner was <b>%s</b>,<br>'),
36 'info_capp' => array( 'it' => 'hanno fatto <b>cappotto</b> EBBRAVI!.<hr>',
37 'en' => 'and he made <b>capot</b> WELL DONE!.<hr>'),
38 'info_pnt' => array( 'it' => 'dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: %s.<hr>',
39 'en' => 'they had to do <b>%s</b> points and they had made <b>%d</b>: %s.<hr>'),
40 'info_alea' => array( 'it' => 'almeno ',
42 'info_more' => array( 'it' => 'più di 60',
44 'info_win' => array( 'it' => 'hanno <b>vinto</b>',
45 'en' => 'they have <b>win</b>'),
46 'info_peer' => array( 'it' => 'hanno <b>pareggiato</b>',
47 'en' => 'they have <b>drew</b>'),
48 'info_lost' => array( 'it' => 'hanno <b>perso</b>',
49 'en' => 'they have <b>lost</b>'),
50 'info_yturn'=> array( 'it' => ' tocca a <b>te</b> giocare.',
51 'en' => ' it\'s <b>your</b> turn.'),
52 'info_turn' => array( 'it' => 'tocca a <b>$unam</b> giocare.',
53 'en' => 'it\'s the <b>%s</b>\'s turn.'),
54 'info_mult' => array( 'it' => ' La partita vale <b>%s</b>.',
55 'en' => ' The game worth <b>%s</b>.' ),
56 'info_yshuf'=> array( 'it' => 'Fai <b>tu</b> il mazzo, ',
57 'en' => 'It\'s <b>your</b> shuffled the cards, '),
58 'info_shuf' => array( 'it' => 'Il mazzo a <b>%s</b>, ',
59 'en' => '<b>%s</b> shuffled the cards, '),
60 'btn_bkgame'=> array( 'it' => 'torna alla partita',
61 'en' => 'back to the game'),
62 'call_wptn' => array( 'it' => '<br>con %d punti',
63 'en' => '<br>with %d points'),
64 'call_ycall'=> array( 'it' => 'Chiami%s:',
66 'call_call' => array( 'it' => 'Chiama %s%s:',
67 'en' => 'The declarer is %s%s:')
73 $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.'),
74 '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.') );
76 function multoval($mult)
81 return ($G_lang == 'en' ? 'double' : 'doppio');
83 return ($G_lang == 'en' ? 'triple' : 'triplo');
85 return ($G_lang == 'en' ? 'quadruple' : 'quadruplo');
87 return (sprintf(($G_lang == 'en' ? "%d-ple" : "%d-plo"), $mult));
92 var $value; /* 0 - 39 card value */
93 var $stat; /* 'bunch', 'hand', 'table', 'take' */
94 var $owner; /* (table position 0-4) */
95 // var $pos; /* Pos in hand. */
96 var $x; /* When played the X position on the table of the owner. */
97 var $y; /* When played the Y position on the table of the owner. */
99 function Card($value, $stat, $owner)
101 $this->value = $value;
102 $this->stat = $stat; // Card stat
103 $this->owner = $owner;
106 function assign($stat,$owner)
108 $this->stat = $stat; // Card stat
109 $this->owner = $owner;
112 function setpos($pos)
119 $this->stat = 'table'; // Card stat
124 function take($newown)
126 $this->stat = 'take'; // Card stat
127 $this->owner = $newown;
131 class Table_briskin5 extends Table {
132 var $card; // il mazzo di carte
133 var $mazzo; // chi e' di mazzo
143 var $points; // points array
144 var $points_n; // number of row of points
157 function Table_briskin5()
163 function &create($idx)
167 if (($thiz =& new Table_briskin5()) == FALSE)
173 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
174 $thiz->asta_pla_n= -1;
175 $thiz->asta_card = -1;
176 $thiz->asta_pnt = -1;
179 $thiz->points = array( );
181 $thiz->total = array( 0, 0, 0, 0, 0);
182 $thiz->asta_win = -1;
183 $thiz->briscola = -1;
187 $thiz->old_reason = "";
188 $thiz->old_asta_pnt = -1;
191 $thiz->old_friend = -1;
198 function &clone(&$from)
202 if (($thiz =& new Table_briskin5()) == FALSE)
207 $thiz->card = $from->card;
208 $thiz->mazzo = $from->mazzo; // REVIEW
209 $thiz->gstart = $from->gstart;
210 $thiz->turn = $from->turn;
212 $thiz->asta_pla = $from->asta_pla;
213 $thiz->asta_pla_n = $from->asta_pla_n;
214 $thiz->asta_card = $from->asta_card;
215 $thiz->asta_pnt = $from->asta_pnt;
217 $thiz->mult = $from->mult;
218 $thiz->points = $from->points;
219 $thiz->points_n = $from->points_n;
220 $thiz->total = $from->total;
222 $thiz->asta_win = $from->asta_win;
223 $thiz->briscola = $from->briscola;
224 $thiz->friend = $from->friend;
226 $thiz->old_reason = $from->old_reason;
227 $thiz->old_asta_pnt = $from->old_asta_pnt;
228 $thiz->old_pnt = $from->old_pnt;
229 $thiz->old_win = $from->old_win;
230 $thiz->old_friend = $from->old_friend;
236 function parentcopy(&$from)
241 function &spawn(&$from)
245 if (($thiz =& new Table_briskin5()) == FALSE)
248 $thiz->parentcopy($from);
250 log_main("PLAYER_N - spawn.".$thiz->player_n);
252 $thiz->card = &$thiz->bunch_create();
253 $thiz->mazzo = rand(0,PLAYERS_N-1);
257 $thiz->old_reason = "";
259 // players are rearranged in an dedicated array
260 $thiz->player = array();
261 for ($i = 0 ; $i < $from->player_n ; $i++)
262 $thiz->player[$i] = $i;
264 log_main("TABLE_OLD_WIN - spawn:".$thiz->old_win);
270 function &bunch_create()
274 for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
275 // for ($i = 0 ; $i < 40 ; $i++) {
276 $ret[$i] =& new Card($i, 'bunch', 'no_owner');
283 function bunch_make()
285 log_main("bunch_make start");
286 $ct = array(0,0,0,0,0);
288 mt_srand(make_seed());
290 for ($i = (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) - 1 ; $i >= 0 ; $i--)
293 for ($i = (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) - 1 ; $i >= 0 ; $i--) {
297 log_main("RND ZERO");
301 $owner = $i % BRISKIN5_PLAYERS_N;
302 $this->card[$id]->assign('hand', $owner);
304 $rest[$rn] = $rest[$i];
305 // $pubbpos[$rn2] = $pubbpos[$i];
307 log_main("bunch_make end");
310 function init(&$userarr)
313 $this->mazzo = rand(0,PLAYERS_N-1);
317 $this->old_reason = "";
319 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
320 $this->total[$i] = 0;
321 $user_cur = &$userarr[$this->player[$i]];
322 $user_cur->exitislock = TRUE;
325 log_main("table::init: ci siamo");
328 function game_init(&$userarr)
332 $this->gstart = ($this->mazzo+1) % BRISKIN5_PLAYERS_N;
336 $this->asta_pla_n = BRISKIN5_PLAYERS_N;
337 $this->asta_card = -1;
338 $this->asta_pnt = 60;
339 $this->asta_win = -1;
340 $this->briscola = -1;
344 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
345 $this->asta_pla[$i] = TRUE;
346 $user_cur = &$userarr[$this->player[$i]];
347 $user_cur->subst = 'asta';
348 $user_cur->asta_card = -2;
349 $user_cur->asta_pnt = -1;
350 $user_cur->handpt = $this->hand_points($i);
351 // SEE function calculate_points(&$table)
358 $this->mazzo = ($this->mazzo + 1) % BRISKIN5_PLAYERS_N;
362 function hand_points($idx)
364 GLOBAL $G_all_points;
368 for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
369 // for ($i = 0 ; $i < 40 ; $i++) {
370 if ($this->card[$i]->owner != $idx)
373 $ctt = $this->card[$i]->value % 10;
374 $tot += $G_all_points[$ctt];
381 function exitlock_show(&$userarr, $table_pos)
383 $ct = $this->exitlock_calc(&$userarr, $table_pos);
385 $ret = sprintf('exitlock_show(%d, %s);', $ct,
386 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
390 function exitlock_calc(&$userarr, $table_pos)
394 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
395 if ($userarr[$this->player[$i]]->exitislock == FALSE)
401 } // end class Table_briskin5
408 class User_briskin5 extends User {
411 var $handpt; // Total card points at the beginning of the current hand.
412 var $exitislock; // Player can exit from the table ?
418 function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
421 if (($thiz =& new User()) == FALSE)
424 $thiz->asta_card = -2;
425 $thiz->asta_pnt = -1;
427 $thiz->exitislock = TRUE;
433 function parentcopy(&$from)
438 function copy(&$from)
440 $this->parentcopy($from);
442 $this->asta_card = $from->asta_card;
443 $this->asta_pnt = $from->asta_pnt;
444 $this->handpt = $from->handpt;
445 $this->exitislock = $from->exitislock;
449 function &clone(&$from)
453 if (($thiz =& new User()) == FALSE)
462 function &spawn(&$from, $table, $table_pos)
466 if (($thiz =& new User_briskin5()) == FALSE)
469 $thiz->parentcopy($from);
471 $thiz->asta_card = -2;
472 $thiz->asta_pnt = -1;
474 $thiz->exitislock = TRUE;
476 $thiz->table_orig = $table;
478 $thiz->table_pos = $table_pos;
482 } // end class User_briskin5
489 var $comm; // commands for many people
490 var $step; // current step of the comm array
491 var $garbage_timeout;
500 function Briskin5 (&$room, $table_idx, $table_token) {
501 $this->user = array();
502 $this->table = array();
504 $this->the_end = FALSE;
505 $this->shm_sz = BRISKIN5_SHM_MIN;
506 if (($this->tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
511 $user =& $room->user;
512 $table =& $room->table[$table_idx];
514 log_wr("Briskin5 constructor");
516 for ($i = 0 ; $i < $table->player_n ; $i++) {
517 $user[$table->player[$i]]->table_token = $table_token;
518 $this->user[$i] =& User_briskin5::spawn(&$user[$table->player[$i]], $table_idx, $i);
520 $this->table[0] =& Table_briskin5::spawn(&$table);
522 log_main("TABLE_OLD_WIN - Briskin5:".$this->table[0]->old_win);
524 $this->table_idx = $table_idx;
525 $this->table_token = $table_token;
526 $this->garbage_timeout = 0;
528 log_wr("Briskin5 constructor end");
532 function &get_user($sess, &$idx)
534 GLOBAL $PHP_SELF, $G_false;
536 if (validate_sess($sess)) {
537 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
538 if (strcmp($sess, $this->user[$i]->sess) == 0) {
541 $ret = &$this->user[$i];
545 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
546 // for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++)
547 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
550 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
557 function garbage_manager($force)
560 /* Garbage collector degli utenti in timeout */
563 if ($force || $this->garbage_timeout < $curtime) {
565 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
566 $user_cur = &$this->user[$i];
567 if ($user_cur->sess == "" ||
568 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
571 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
572 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
574 if ($user_cur->stat == 'table') {
575 log_auth($user_cur->sess," bin5 Autologout session.");
577 /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE
579 $tmp_sess = $user_cur->sess;
580 $user_cur->sess = "";
581 step_unproxy($tmp_sess);
582 $user_cur->name = "";
583 $user_cur->the_end = FALSE;
587 $this->table_wakeup(&$user_cur);
591 log_rd2($user_cur->sess." GARBAGE UPDATED!");
593 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
606 function &load_data($table_idx, $table_token = "")
608 GLOBAL $G_false, $sess;
612 log_wr("TABLE_IDX ".FTOK_PATH."/table".$table_idx);
615 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
616 log_main("ftok failed");
621 if (($shm_sz = sharedmem_sz($tok)) == -1) {
622 log_main("shmop_open failed");
626 if (($shm = shm_attach($tok, $shm_sz)) == FALSE)
629 if (($bri = @shm_get_var($shm, $tok)) == FALSE)
632 if ($table_token != "" && $bri->table_token != $table_token) {
633 log_wr("bri->table_token: ".$bri->table_token."table_token: ".$table_token);
648 log_wr("briskin5 load_data failed");
657 function save_data(&$bri)
664 log_main("SAVE BRISKIN5 DATA");
666 if (!isset($bri->tok))
669 while ($bri->shm_sz < BRISKIN5_SHM_MAX) {
670 if (($shm = shm_attach($bri->tok, $bri->shm_sz)) == FALSE)
673 if (@shm_put_var($shm, $bri->tok, $bri) != FALSE) {
677 if (shm_remove($shm) === FALSE) {
678 log_only("REMOVE FALLITA");
682 $bri->shm_sz += BRISKIN5_SHM_DLT;
685 log_crit("save data failed!");
695 function destroy_data($table_idx)
701 log_main("DESTROY BRISKIN5 DATA");
704 log_main("DESTROY2 BRISKIN5 DATA");
706 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1)
709 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
712 if (shmop_delete($shm) == 0) {
713 log_only("REMOVE FALLITA ");
718 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
720 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
730 function lock_data($table_idx)
734 log_lock("LOCK_DATA ".FTOK_PATH."/table".$table_idx);
735 // echo "LOCK: ".FTOK_PATH."/main";
737 // WARNING monitor this step
738 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
741 // WARNING monitor this step
742 if (($res = @sem_get($tok)) == FALSE) {
745 if (sem_acquire($res)) {
746 log_lock("LOCK table ".$table_idx."[res: ".$res."]");
750 log_lock("LOCK table ".$table_idx.":FAILED");
755 function unlock_data($res)
759 log_lock("UNLOCK table [res: ".$res."]");
761 return (sem_release($res));
765 function chatt_send(&$user, $mesg)
767 GLOBAL $mlang_brisk, $G_lang;
769 if ($user->stat == 'table') {
770 $table = &$this->table[$user->table];
773 $user_mesg = substr($mesg,6);
777 $dt = date("H:i ", $curtime);
778 if (strncmp($user_mesg, "/nick ", 6) == 0) {
779 log_main($user->sess." chatt_send BEGIN");
781 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
782 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
783 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
788 $user_mesg = "COMMAND ".$user_mesg;
791 // update local graph
792 // update remote graphs
793 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
794 $user_cur = &$this->user[$i];
795 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
796 if ($user_cur->sess == '')
798 if ($user_cur->name == $name_new) {
799 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
801 $premsg = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
802 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $premsg);
807 if ($i == BRISKIN5_MAX_PLAYERS) {
808 if ($user->flags & USER_FLAG_AUTH && strcasecmp($user->name,$name_new) != 0) {
809 if ($this->table[$user->table]->auth_only == TRUE) {
810 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
811 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>%s</b>");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
816 $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
820 $user->name = $name_new;
822 log_main($user->sess." chatt_send start set");
825 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
826 log_main($user->sess." chatt_send set loop");
828 $user_cur = &$this->user[$i];
829 if ($user_cur->sess == '')
832 if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
833 $table = &$this->table[$user->table];
835 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
836 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
837 $this->user[$table->player[($user_cur->table_pos) % BRISKIN5_PLAYERS_N]]->flags,
838 xcape($this->user[$table->player[($user_cur->table_pos) % BRISKIN5_PLAYERS_N]]->name),
840 $this->user[$table->player[($user_cur->table_pos+1) % BRISKIN5_PLAYERS_N]]->flags,
841 xcape($this->user[$table->player[($user_cur->table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
843 $this->user[$table->player[($user_cur->table_pos+2) % BRISKIN5_PLAYERS_N]]->flags,
844 xcape($this->user[$table->player[($user_cur->table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
846 (BRISKIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+3) % BRISKIN5_PLAYERS_N]]->flags),
847 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
849 (BRISKIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+4) % BRISKIN5_PLAYERS_N]]->flags),
850 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
851 if ($user_cur == $user) {
852 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
853 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
854 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";',
855 $itin, xcape($user->name,ENT_COMPAT,"UTF-8"), $itou);
857 $user_cur->step_inc();
863 for ($i = 0 ; $i < ($user->stat == 'room' ? BRISKIN5_MAX_PLAYERS : BRISKIN5_PLAYERS_N) ; $i++) {
864 if ($user->stat == 'room') {
865 $user_cur = &$this->user[$i];
866 if ($user_cur->sess == '' || $user_cur->stat != 'room')
870 $user_cur = &$this->user[$table->player[$i]];
873 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
874 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
875 $dt, $user->flags, xcape($user->name), xcape($user_mesg));
876 $user_cur->step_inc();
878 log_legal($curtime, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
882 function table_wakeup(&$user)
884 $table = &$this->table[0];
886 log_main("BRISKIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
890 log_main("BRISKIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
892 for ($i = 0 ; $i < $table->player_n ; $i++) {
893 $user_cur = &$this->user[$i];
894 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
896 if ($user_cur == $user)
897 $user_cur->subst = "shutdowner";
899 $user_cur->subst = "shutdowned";
900 $user_cur->laccwr = $curtime;
902 $ret = "gst.st = ".($user_cur->step+1)."; ";
903 $ret .= 'gst.st_loc++; the_end=true; window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
905 log_wr($user_cur->sess." BRISKIN5_WAKEUP: ".$ret);
906 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
907 $user_cur->step_inc();
910 $this->the_end = TRUE;
914 * If all players are freezed the room garbage_manager clean up table and users.
916 function is_abandoned()
921 $table = &$this->table[0];
923 for ($i = 0 ; $i < $table->player_n ; $i++) {
924 $user_cur = &$this->user[$i];
926 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
934 } // end class Briskin5
936 function locshm_exists($tok)
940 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
941 log_main($tok." SHM NOT exists");
947 log_main($tok." SHM exists");
955 function calculate_points(&$table)
957 GLOBAL $G_all_points;
961 if ($table->asta_pnt == 60)
962 $table->asta_pnt = 61;
964 $table->old_reason = "";
965 $table->old_win = $table->asta_win;
966 $table->old_friend = $table->friend;
967 $table->old_asta_pnt = $table->asta_pnt;
969 for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
970 // for ($i = 0 ; $i < 40 ; $i++) {
971 $ctt = $table->card[$i]->value % 10;
972 $own = $table->card[$i]->owner;
973 if ($own == $table->asta_win || $own == $table->friend)
974 $pro += $G_all_points[$ctt];
977 log_wr(sprintf("PRO: [%d]", $pro));
980 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
981 $table->points[$table->points_n % MAX_POINTS] = array();
982 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++)
983 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
985 $table->old_pnt = $pro;
991 if ($pro >= $table->asta_pnt)
996 $table->points[$table->points_n % MAX_POINTS] = array();
997 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
998 if ($i == $table->asta_win)
999 $pt = ($i == $table->friend ? 4 : 2);
1000 else if ($i == $table->friend)
1005 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1007 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1009 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1011 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1012 $table->total[$i] += $pt;
1015 $table->old_pnt = $pro;
1020 is_transition (is from room to table ?)
1021 is_again (is another game)
1023 Examples of $is_transition, $is_again:
1024 from reload of the page: FALSE, FALSE
1025 from sitdown in room: TRUE, FALSE
1026 from table: asta cmd e tutti passano: TRUE, TRUE
1027 from table: fine partita: TRUE, TRUE
1029 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
1031 $table_idx = $user->table;
1032 $table = &$room->table[$table_idx];
1033 $table_pos = $user->table_pos;
1035 $ret = "table_init();";
1036 $ret .= $table->exitlock_show(&$room->user, $table_pos);
1038 /* GENERAL STATUS */
1039 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1040 $sendstep, $user->stat, $user->subst, $table_pos);
1042 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1045 $ret .= "background_set();";
1048 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
1049 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
1051 $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', $itin, xcape($user->name), $itou);
1052 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1053 $room->user[$table->player[($table_pos) % BRISKIN5_PLAYERS_N]]->flags,
1054 xcape($room->user[$table->player[($table_pos) % BRISKIN5_PLAYERS_N]]->name),
1056 $room->user[$table->player[($table_pos+1) % BRISKIN5_PLAYERS_N]]->flags,
1057 xcape($room->user[$table->player[($table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
1059 $room->user[$table->player[($table_pos+2) % BRISKIN5_PLAYERS_N]]->flags,
1060 xcape($room->user[$table->player[($table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
1062 (BRISKIN5_PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+3) % BRISKIN5_PLAYERS_N]]->flags),
1063 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
1065 (BRISKIN5_PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+4) % BRISKIN5_PLAYERS_N]]->flags),
1066 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
1068 /* NOTIFY FOR THE CARD MAKER */
1069 if ($is_transition) { // && $user->subst == "asta" superfluo
1070 $ret .= show_table_info(&$room, &$table, $table_pos);
1073 $ret .= table_wellcome($user);
1075 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1076 $ret .= playsound("cow.mp3");
1081 if ($is_transition) { // && $user->subst == "asta" superfluo
1084 for ($i = 0 ; $i < 8 ; $i++) {
1085 for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++) {
1087 for ($o = 0 ; $o < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) && $ct < $i+1 ; $o++) {
1088 // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1089 if ($table->card[$o]->owner == (($e + $table->gstart) % BRISKIN5_PLAYERS_N)) {
1095 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1097 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BRISKIN5_PLAYERS_N,
1098 $i, ((($e + BRISKIN5_PLAYERS_N - $table_pos + $table->gstart) % BRISKIN5_PLAYERS_N) == 0 ?
1099 $table->card[$o]->value : -1),
1100 ($i == 7 && $e == (BRISKIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1105 $taked = array(0,0,0,0,0);
1106 $inhand = array(0,0,0,0,0);
1107 $ontabl = array(-1,-1,-1,-1,-1);
1110 for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1111 // for ($i = 0 ; $i < 40 ; $i++) {
1112 if ($table->card[$i]->stat == 'hand') {
1113 if ($table->card[$i]->owner == $table_pos) {
1114 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1116 $inhand[$table->card[$i]->owner]++;
1118 else if ($table->card[$i]->stat == 'take') {
1119 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1120 $taked[$table->card[$i]->owner]++;
1122 else if ($table->card[$i]->stat == 'table') {
1123 $ontabl[$table->card[$i]->owner] = $i;
1127 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1128 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1130 log_main("Stat table: ".$logg);
1132 /* Set ours cards. */
1134 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1135 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1136 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
1137 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1138 $ret .= sprintf('card_setours(%s);', $oursarg);
1140 /* Dispose all cards */
1141 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1142 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1143 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1144 ($inhand[$i] <= 8 ? $inhand[$i] : 8) , $taked[$i]);
1146 if ($ontabl[$i] != -1) {
1147 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1148 $table->card[$ontabl[$i]]->value,
1149 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1155 if ($user->subst == 'asta') {
1157 /* show users auction status */
1159 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1160 $user_cur = &$room->user[$table->player[$i]];
1161 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1162 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1164 if (BRISKIN5_PLAYERS_N == 3)
1165 $showst .= ",-2,-2";
1166 $ret .= sprintf('show_astat(%s);', $showst);
1168 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1169 /* show card chooser */
1170 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1175 if ($table_pos == ($table->gstart % BRISKIN5_PLAYERS_N) &&
1176 $table->asta_win == -1)
1177 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1178 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1180 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1181 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1185 if ($table->asta_win == -1) { // auction case
1186 if ($table_pos == ($table->gstart % BRISKIN5_PLAYERS_N))
1187 $ret .= "remark_on();";
1189 $ret .= "remark_off();";
1191 else { // chooseed case
1192 if ($table_pos == $table->asta_win)
1193 $ret .= "remark_on();";
1195 $ret .= "remark_off();";
1198 else if ($user->subst == 'game') {
1200 if (($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N == $table_pos)
1201 $ret .= "is_my_time = true; remark_on();";
1203 $ret .= "remark_off();";
1205 /* WHO CALL AND WATH */
1206 $ret .= briscola_show($room, $table, $user);
1210 } // end function show_table(...
1212 function calculate_winner(&$table)
1219 $cur_seed = $table->briscola - ($table->briscola % 10);
1221 for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1222 // for ($i = 0 ; $i < 40 ; $i++) {
1223 if ($table->card[$i]->stat != "table")
1226 log_wr(sprintf("Card On table: [%d]", $i));
1228 $v = $table->card[$i]->value;
1229 $ontab[$table->card[$i]->owner] = $v;
1230 $ontid[$table->card[$i]->owner] = $i;
1231 /* se briscola setto il flag */
1232 if (($v - ($v % 10)) == $cur_seed)
1236 if ($briontab == FALSE) {
1237 $cur_win = $table->gstart;
1238 $cur_val = $ontab[$cur_win];
1239 $cur_seed = $cur_val - ($cur_val % 10);
1242 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1243 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1244 if ($ontab[$i] < $cur_val) {
1245 $cur_val = $ontab[$i];
1251 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1252 $table->card[$ontid[$i]]->owner = $cur_win;
1253 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1258 function show_table_info(&$room, &$table, $table_pos)
1260 GLOBAL $G_lang, $mlang_bin5_bin5;
1263 $user = &$room->user[$table->player[$table_pos]];
1265 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1266 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1269 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++)
1270 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
1271 $noty .= sprintf("</tr>");
1274 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1276 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1277 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1278 for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++)
1279 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1284 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1285 for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++)
1286 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1287 $noty .= "</tr></table>";
1289 if ($table->old_reason != "") {
1290 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1293 if ($table->old_win != -1) {
1294 log_main("TABLE_OLD_WIN:".$table->old_win);
1295 $win = $table->player[$table->old_win];
1296 log_main("TABLE_OLD_FRIEND:".$table->old_friend);
1297 $fri = $table->player[$table->old_friend];
1299 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1302 /* 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" */
1304 $noty .= sprintf($mlang_bin5_bin5['info_part'][$G_lang],
1305 xcape($room->user[$win]->name),
1306 xcape($room->user[$fri]->name));
1307 if ($table->old_pnt == 120) {
1308 $noty .= sprintf($mlang_bin5_bin5['info_capp'][$G_lang]);
1311 $noty .= sprintf($mlang_bin5_bin5['info_pnt'][$G_lang],
1312 ($table->old_asta_pnt > 61 ? $mlang_bin5_bin5['info_alea'][$G_lang].$table->old_asta_pnt :
1313 $mlang_bin5_bin5['info_more'][$G_lang]), $table->old_pnt,
1314 ($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])));
1318 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1319 xcape($room->user[$win]->name));
1320 if ($table->old_pnt == 120) {
1321 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1324 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1325 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1326 'più di 60'), $table->old_pnt,
1327 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1331 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1332 if ($table->mazzo == $table_pos)
1333 $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
1335 $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
1336 $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
1339 if ($user->subst == 'asta') {
1340 if ($table->asta_win == -1) // auction case
1341 $curplayer = $table->gstart % BRISKIN5_PLAYERS_N;
1343 $curplayer = $table->asta_win;
1345 else if ($user->subst == 'game') {
1346 $curplayer = ($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N;
1349 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1350 if ($curplayer == $table_pos) {
1351 $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
1354 $unam = xcape($room->user[$table->player[$curplayer]]->name);
1355 $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
1358 if ($table->mult > 1) {
1359 $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($table->mult));
1361 $noty .= "<hr><br>";
1362 $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
1363 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
1364 * 130 + ($table->points_n > 0 ? 50 : 0) +
1365 * (120 * ($table->points_n / MAX_POINTS)));
1366 * will be used when we refact notify js function following
1367 * photoo class logic
1373 function table_wellcome($user)
1375 GLOBAL $table_wellarr, $G_lang;
1378 for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
1379 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
1385 function briscola_show($room, $table, $user)
1390 if ($table->asta_card == 9)
1391 $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
1393 /* text of caller cell */
1394 if ($user->table_pos == $table->asta_win) {
1395 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
1396 $ret .= sprintf($prestr, $ptnadd);
1399 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
1400 $ret .= sprintf($prestr,
1401 xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
1404 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1406 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1407 $ret .= sprintf('$("caller").style.visibility = "visible";');
1408 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1409 $ret .= sprintf('$("astalascio").style.visibility = "";');
1410 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1411 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1417 function game_result($asta_pnt, $pnt)
1419 if ($asta_pnt == 61) {
1422 else if ($pnt == 60)
1428 if ($pnt >= $asta_pnt)