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");
954 // rendiamo qui l'elenco dei punti come return della func
955 function calculate_points(&$table)
957 GLOBAL $G_all_points;
962 if ($table->asta_pnt == 60)
963 $table->asta_pnt = 61;
965 $table->old_reason = "";
966 $table->old_win = $table->asta_win;
967 $table->old_friend = $table->friend;
968 $table->old_asta_pnt = $table->asta_pnt;
970 for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
971 // for ($i = 0 ; $i < 40 ; $i++) {
972 $ctt = $table->card[$i]->value % 10;
973 $own = $table->card[$i]->owner;
974 if ($own == $table->asta_win || $own == $table->friend)
975 $pro += $G_all_points[$ctt];
978 log_wr(sprintf("PRO: [%d]", $pro));
981 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
982 $table->points[$table->points_n % MAX_POINTS] = array();
983 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++)
984 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
986 $table->old_pnt = $pro;
992 if ($pro >= $table->asta_pnt)
997 $table->points[$table->points_n % MAX_POINTS] = array();
998 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
999 if ($i == $table->asta_win)
1000 $pt = ($i == $table->friend ? 4 : 2);
1001 else if ($i == $table->friend)
1006 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1008 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1010 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1012 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1013 $table->total[$i] += $pt;
1017 $table->old_pnt = $pro;
1024 is_transition (is from room to table ?)
1025 is_again (is another game)
1027 Examples of $is_transition, $is_again:
1028 from reload of the page: FALSE, FALSE
1029 from sitdown in room: TRUE, FALSE
1030 from table: asta cmd e tutti passano: TRUE, TRUE
1031 from table: fine partita: TRUE, TRUE
1033 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
1035 $table_idx = $user->table;
1036 $table = &$room->table[$table_idx];
1037 $table_pos = $user->table_pos;
1039 $ret = "table_init();";
1040 $ret .= $table->exitlock_show(&$room->user, $table_pos);
1042 /* GENERAL STATUS */
1043 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1044 $sendstep, $user->stat, $user->subst, $table_pos);
1046 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1049 $ret .= "background_set();";
1052 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
1053 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
1055 $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', $itin, xcape($user->name), $itou);
1056 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1057 $room->user[$table->player[($table_pos) % BRISKIN5_PLAYERS_N]]->flags,
1058 xcape($room->user[$table->player[($table_pos) % BRISKIN5_PLAYERS_N]]->name),
1060 $room->user[$table->player[($table_pos+1) % BRISKIN5_PLAYERS_N]]->flags,
1061 xcape($room->user[$table->player[($table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
1063 $room->user[$table->player[($table_pos+2) % BRISKIN5_PLAYERS_N]]->flags,
1064 xcape($room->user[$table->player[($table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
1066 (BRISKIN5_PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+3) % BRISKIN5_PLAYERS_N]]->flags),
1067 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
1069 (BRISKIN5_PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+4) % BRISKIN5_PLAYERS_N]]->flags),
1070 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
1072 /* NOTIFY FOR THE CARD MAKER */
1073 if ($is_transition) { // && $user->subst == "asta" superfluo
1074 $ret .= show_table_info(&$room, &$table, $table_pos);
1077 $ret .= table_wellcome($user);
1079 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1080 $ret .= playsound("cow.mp3");
1085 if ($is_transition) { // && $user->subst == "asta" superfluo
1088 for ($i = 0 ; $i < 8 ; $i++) {
1089 for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++) {
1091 for ($o = 0 ; $o < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) && $ct < $i+1 ; $o++) {
1092 // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1093 if ($table->card[$o]->owner == (($e + $table->gstart) % BRISKIN5_PLAYERS_N)) {
1099 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1101 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BRISKIN5_PLAYERS_N,
1102 $i, ((($e + BRISKIN5_PLAYERS_N - $table_pos + $table->gstart) % BRISKIN5_PLAYERS_N) == 0 ?
1103 $table->card[$o]->value : -1),
1104 ($i == 7 && $e == (BRISKIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1109 $taked = array(0,0,0,0,0);
1110 $inhand = array(0,0,0,0,0);
1111 $ontabl = array(-1,-1,-1,-1,-1);
1114 for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1115 // for ($i = 0 ; $i < 40 ; $i++) {
1116 if ($table->card[$i]->stat == 'hand') {
1117 if ($table->card[$i]->owner == $table_pos) {
1118 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1120 $inhand[$table->card[$i]->owner]++;
1122 else if ($table->card[$i]->stat == 'take') {
1123 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1124 $taked[$table->card[$i]->owner]++;
1126 else if ($table->card[$i]->stat == 'table') {
1127 $ontabl[$table->card[$i]->owner] = $i;
1131 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1132 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1134 log_main("Stat table: ".$logg);
1136 /* Set ours cards. */
1138 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1139 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1140 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
1141 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1142 $ret .= sprintf('card_setours(%s);', $oursarg);
1144 /* Dispose all cards */
1145 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1146 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1147 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1148 ($inhand[$i] <= 8 ? $inhand[$i] : 8) , $taked[$i]);
1150 if ($ontabl[$i] != -1) {
1151 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1152 $table->card[$ontabl[$i]]->value,
1153 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1159 if ($user->subst == 'asta') {
1161 /* show users auction status */
1163 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1164 $user_cur = &$room->user[$table->player[$i]];
1165 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1166 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1168 if (BRISKIN5_PLAYERS_N == 3)
1169 $showst .= ",-2,-2";
1170 $ret .= sprintf('show_astat(%s);', $showst);
1172 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1173 /* show card chooser */
1174 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1179 if ($table_pos == ($table->gstart % BRISKIN5_PLAYERS_N) &&
1180 $table->asta_win == -1)
1181 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1182 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1184 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1185 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1189 if ($table->asta_win == -1) { // auction case
1190 if ($table_pos == ($table->gstart % BRISKIN5_PLAYERS_N))
1191 $ret .= "remark_on();";
1193 $ret .= "remark_off();";
1195 else { // chooseed case
1196 if ($table_pos == $table->asta_win)
1197 $ret .= "remark_on();";
1199 $ret .= "remark_off();";
1202 else if ($user->subst == 'game') {
1204 if (($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N == $table_pos)
1205 $ret .= "is_my_time = true; remark_on();";
1207 $ret .= "remark_off();";
1209 /* WHO CALL AND WATH */
1210 $ret .= briscola_show($room, $table, $user);
1214 } // end function show_table(...
1216 function calculate_winner(&$table)
1223 $cur_seed = $table->briscola - ($table->briscola % 10);
1225 for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1226 // for ($i = 0 ; $i < 40 ; $i++) {
1227 if ($table->card[$i]->stat != "table")
1230 log_wr(sprintf("Card On table: [%d]", $i));
1232 $v = $table->card[$i]->value;
1233 $ontab[$table->card[$i]->owner] = $v;
1234 $ontid[$table->card[$i]->owner] = $i;
1235 /* se briscola setto il flag */
1236 if (($v - ($v % 10)) == $cur_seed)
1240 if ($briontab == FALSE) {
1241 $cur_win = $table->gstart;
1242 $cur_val = $ontab[$cur_win];
1243 $cur_seed = $cur_val - ($cur_val % 10);
1246 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1247 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1248 if ($ontab[$i] < $cur_val) {
1249 $cur_val = $ontab[$i];
1255 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1256 $table->card[$ontid[$i]]->owner = $cur_win;
1257 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1262 function show_table_info(&$room, &$table, $table_pos)
1264 GLOBAL $G_lang, $mlang_bin5_bin5;
1267 $user = &$room->user[$table->player[$table_pos]];
1269 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1270 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1273 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++)
1274 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
1275 $noty .= sprintf("</tr>");
1278 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1280 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1281 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1282 for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++)
1283 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1288 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1289 for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++)
1290 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1291 $noty .= "</tr></table>";
1293 if ($table->old_reason != "") {
1294 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1297 if ($table->old_win != -1) {
1298 log_main("TABLE_OLD_WIN:".$table->old_win);
1299 $win = $table->player[$table->old_win];
1300 log_main("TABLE_OLD_FRIEND:".$table->old_friend);
1301 $fri = $table->player[$table->old_friend];
1303 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1306 /* 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" */
1308 $noty .= sprintf($mlang_bin5_bin5['info_part'][$G_lang],
1309 xcape($room->user[$win]->name),
1310 xcape($room->user[$fri]->name));
1311 if ($table->old_pnt == 120) {
1312 $noty .= sprintf($mlang_bin5_bin5['info_capp'][$G_lang]);
1315 $noty .= sprintf($mlang_bin5_bin5['info_pnt'][$G_lang],
1316 ($table->old_asta_pnt > 61 ? $mlang_bin5_bin5['info_alea'][$G_lang].$table->old_asta_pnt :
1317 $mlang_bin5_bin5['info_more'][$G_lang]), $table->old_pnt,
1318 ($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])));
1322 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1323 xcape($room->user[$win]->name));
1324 if ($table->old_pnt == 120) {
1325 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1328 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1329 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1330 'più di 60'), $table->old_pnt,
1331 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1335 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1336 if ($table->mazzo == $table_pos)
1337 $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
1339 $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
1340 $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
1343 if ($user->subst == 'asta') {
1344 if ($table->asta_win == -1) // auction case
1345 $curplayer = $table->gstart % BRISKIN5_PLAYERS_N;
1347 $curplayer = $table->asta_win;
1349 else if ($user->subst == 'game') {
1350 $curplayer = ($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N;
1353 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1354 if ($curplayer == $table_pos) {
1355 $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
1358 $unam = xcape($room->user[$table->player[$curplayer]]->name);
1359 $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
1362 if ($table->mult > 1) {
1363 $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($table->mult));
1365 $noty .= "<hr><br>";
1366 $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
1367 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
1368 * 130 + ($table->points_n > 0 ? 50 : 0) +
1369 * (120 * ($table->points_n / MAX_POINTS)));
1370 * will be used when we refact notify js function following
1371 * photoo class logic
1377 function table_wellcome($user)
1379 GLOBAL $table_wellarr, $G_lang;
1382 for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
1383 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
1389 function briscola_show($room, $table, $user)
1391 GLOBAL $G_lang, $mlang_bin5_bin5;
1395 if ($table->asta_card == 9)
1396 $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
1398 /* text of caller cell */
1399 if ($user->table_pos == $table->asta_win) {
1400 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
1401 $ret .= sprintf($prestr, $ptnadd);
1404 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
1405 $ret .= sprintf($prestr,
1406 xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
1409 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1411 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1412 $ret .= sprintf('$("caller").style.visibility = "visible";');
1413 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1414 $ret .= sprintf('$("astalascio").style.visibility = "";');
1415 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1416 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1422 function game_result($asta_pnt, $pnt)
1424 if ($asta_pnt == 61) {
1427 else if ($pnt == 60)
1433 if ($pnt >= $asta_pnt)