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(BRISKIN5_PLAYERS_N, 3);
25 define(BRISKIN5_MAX_PLAYERS, BRISKIN5_PLAYERS_N);
26 // define(BRISKIN5_SHM_MIN, (50000 * BRISKIN5_MAX_PLAYERS));
27 define(BRISKIN5_SHM_MIN, 32768);
28 define(BRISKIN5_SHM_MAX, (BRISKIN5_SHM_MIN + 1048576));
29 define(BRISKIN5_SHM_DLT, 32768);
31 $mlang_bin5_bin5 = array(
32 'info_part' => array( 'it' => '<hr>Nell\'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>',
33 'en' => '<hr>In the last hand the declarer was <b>%s</b>, the partner was <b>%s</b>,<br>'),
34 'info_capp' => array( 'it' => 'hanno fatto <b>cappotto</b> EBBRAVI!.<br>',
35 'en' => 'and they made <b>capot</b> WELL DONE!.<br>'),
36 'info_pnt' => array( 'it' => 'dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno %s.<br>',
37 'en' => 'they had to do <b>%s</b> points and they had made <b>%d</b>: they have %s.<br>'),
38 'info_alea' => array( 'it' => 'almeno ',
40 'info_more' => array( 'it' => 'più di 60',
42 'info_win' => array( 'it' => '<b>vinto</b>',
43 'en' => '<b>win</b>'),
44 'info_peer' => array( 'it' => '<b>pareggiato</b>',
45 'en' => '<b>drew</b>'),
46 'info_lost' => array( 'it' => '<b>perso</b>',
47 'en' => '<b>lost</b>'),
49 'info_alon' => array( 'it' => '<hr>Nell\'ultima partita <b>%s</b> si è chiamato in mano,<br>',
50 'en' => '<hr>In the last hand <b>%s</b> play alone against each other,<br>'),
51 'info_apnt' => array( 'it' => 'doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha %s.<br>',
52 'en' => 'he/she had to do <b>%s</b> points and they had made <b>%d</b>: he/she had %s.<br>'),
53 'info_acap' => array( 'it' => 'ha fatto <b>cappotto</b> EBBRAVO!.<hr>',
54 'en' => 'and he/she made <b>capot</b> WELL DONE!.<hr>'),
56 'info_omul' => array( 'it' => ' La partita valeva <b>%s</b>.',
57 'en' => ' EN: The game was worth <b>%s</b>.' ),
59 'info_yturn'=> array( 'it' => ' tocca a <b>te</b> giocare.',
60 'en' => ' it\'s <b>your</b> turn.'),
61 'info_turn' => array( 'it' => 'tocca a <b>%s</b> giocare.',
62 'en' => 'it\'s the <b>%s</b>\'s turn.'),
63 'info_mult' => array( 'it' => ' La partita vale <b>%s</b>.',
64 'en' => ' The game worth <b>%s</b>.' ),
65 'info_yshuf'=> array( 'it' => 'Fai <b>tu</b> il mazzo, ',
66 'en' => 'It\'s <b>your</b> shuffled the cards, '),
67 'info_shuf' => array( 'it' => 'Il mazzo a <b>%s</b>, ',
68 'en' => '<b>%s</b> shuffled the cards, '),
69 'btn_bkgame'=> array( 'it' => 'torna alla partita',
70 'en' => 'back to the game'),
71 'call_wptn' => array( 'it' => '<br>con %d punti',
72 'en' => '<br>with %d points'),
73 'call_ycall'=> array( 'it' => 'Chiami%s:',
75 'call_call' => array( 'it' => 'Chiama %s%s:',
76 'en' => 'The declarer is %s%s:')
82 $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.'),
83 '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.') );
85 function multoval($mult)
90 return ($G_lang == 'en' ? 'double' : 'doppio');
92 return ($G_lang == 'en' ? 'triple' : 'triplo');
94 return ($G_lang == 'en' ? 'quadruple' : 'quadruplo');
96 return (sprintf(($G_lang == 'en' ? "%d-ple" : "%d-plo"), $mult));
101 var $value; /* 0 - 39 card value */
102 var $stat; /* 'bunch', 'hand', 'table', 'take' */
103 var $owner; /* (table position 0-4) */
104 // var $pos; /* Pos in hand. */
105 var $x; /* When played the X position on the table of the owner. */
106 var $y; /* When played the Y position on the table of the owner. */
108 function Card($value, $stat, $owner)
110 $this->value = $value;
111 $this->stat = $stat; // Card stat
112 $this->owner = $owner;
115 function assign($stat,$owner)
117 $this->stat = $stat; // Card stat
118 $this->owner = $owner;
121 function setpos($pos)
128 $this->stat = 'table'; // Card stat
133 function take($newown)
135 $this->stat = 'take'; // Card stat
136 $this->owner = $newown;
140 class Table_briskin5 extends Table {
141 var $card; // il mazzo di carte
142 var $mazzo; // chi e' di mazzo
152 var $points; // points array
153 var $points_n; // number of row of points
167 function Table_briskin5()
173 function &create($idx)
177 if (($thiz =& new Table_briskin5()) == FALSE)
183 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
184 $thiz->asta_pla_n= -1;
185 $thiz->asta_card = -1;
186 $thiz->asta_pnt = -1;
189 $thiz->points = array( );
191 $thiz->total = array( 0, 0, 0, 0, 0);
192 $thiz->asta_win = -1;
193 $thiz->briscola = -1;
197 $thiz->old_reason = "";
198 $thiz->old_asta_pnt = -1;
199 $thiz->old_mult = -1;
202 $thiz->old_friend = -1;
209 function &myclone(&$from)
213 if (($thiz =& new Table_briskin5()) == FALSE)
218 $thiz->card = $from->card;
219 $thiz->mazzo = $from->mazzo; // REVIEW
220 $thiz->gstart = $from->gstart;
221 $thiz->turn = $from->turn;
223 $thiz->asta_pla = $from->asta_pla;
224 $thiz->asta_pla_n = $from->asta_pla_n;
225 $thiz->asta_card = $from->asta_card;
226 $thiz->asta_pnt = $from->asta_pnt;
228 $thiz->mult = $from->mult;
229 $thiz->points = $from->points;
230 $thiz->points_n = $from->points_n;
231 $thiz->total = $from->total;
233 $thiz->asta_win = $from->asta_win;
234 $thiz->briscola = $from->briscola;
235 $thiz->friend = $from->friend;
237 $thiz->old_reason = $from->old_reason;
238 $thiz->old_asta_pnt = $from->old_asta_pnt;
239 $thiz->old_mult = $from->mult;
240 $thiz->old_pnt = $from->old_pnt;
241 $thiz->old_win = $from->old_win;
242 $thiz->old_friend = $from->old_friend;
248 function parentcopy(&$from)
253 function &spawn(&$from)
257 if (($thiz =& new Table_briskin5()) == FALSE)
260 $thiz->parentcopy($from);
262 log_main("PLAYER_N - spawn.".$thiz->player_n);
264 $thiz->card = &$thiz->bunch_create();
265 $thiz->mazzo = rand(0,PLAYERS_N-1);
269 $thiz->old_reason = "";
271 // players are rearranged in an dedicated array
272 $thiz->player = array();
273 for ($i = 0 ; $i < $from->player_n ; $i++)
274 $thiz->player[$i] = $i;
276 log_main("TABLE_OLD_WIN - spawn:".$thiz->old_win);
282 function &bunch_create()
286 for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
287 // for ($i = 0 ; $i < 40 ; $i++) {
288 $ret[$i] =& new Card($i, 'bunch', 'no_owner');
295 function bunch_make()
297 log_main("bunch_make start");
298 $ct = array(0,0,0,0,0);
300 mt_srand(make_seed());
302 for ($i = (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) - 1 ; $i >= 0 ; $i--)
305 for ($i = (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) - 1 ; $i >= 0 ; $i--) {
309 log_main("RND ZERO");
313 $owner = $i % BRISKIN5_PLAYERS_N;
314 $this->card[$id]->assign('hand', $owner);
316 $rest[$rn] = $rest[$i];
317 // $pubbpos[$rn2] = $pubbpos[$i];
319 log_main("bunch_make end");
322 function init(&$userarr)
325 $this->mazzo = rand(0,PLAYERS_N-1);
329 $this->old_reason = "";
331 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
332 $this->total[$i] = 0;
333 $user_cur = &$userarr[$this->player[$i]];
334 $user_cur->exitislock = TRUE;
337 log_main("table::init: ci siamo");
340 function game_init(&$userarr)
344 $this->gstart = ($this->mazzo+1) % BRISKIN5_PLAYERS_N;
348 $this->asta_pla_n = BRISKIN5_PLAYERS_N;
349 $this->asta_card = -1;
350 $this->asta_pnt = 60;
351 $this->asta_win = -1;
352 $this->briscola = -1;
356 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
357 $this->asta_pla[$i] = TRUE;
358 $user_cur = &$userarr[$this->player[$i]];
359 $user_cur->subst = 'asta';
360 $user_cur->asta_card = -2;
361 $user_cur->asta_pnt = -1;
362 $user_cur->handpt = $this->hand_points($i);
363 // SEE function calculate_points(&$table)
370 $this->mazzo = ($this->mazzo + 1) % BRISKIN5_PLAYERS_N;
374 function hand_points($idx)
376 GLOBAL $G_all_points;
380 for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
381 // for ($i = 0 ; $i < 40 ; $i++) {
382 if ($this->card[$i]->owner != $idx)
385 $ctt = $this->card[$i]->value % 10;
386 $tot += $G_all_points[$ctt];
393 function exitlock_show(&$userarr, $table_pos)
395 $ct = $this->exitlock_calc(&$userarr, $table_pos);
397 $ret = sprintf('exitlock_show(%d, %s);', $ct,
398 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
402 function exitlock_calc(&$userarr, $table_pos)
406 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
407 if ($userarr[$this->player[$i]]->exitislock == FALSE)
413 } // end class Table_briskin5
418 define(BIN5_USER_FLAG_RING_ENDAUCT, 0x01);
420 class User_briskin5 extends User {
423 var $handpt; // Total card points at the beginning of the current hand.
424 var $exitislock; // Player can exit from the table ?
425 var $privflags; // Flags for briskin5 only
431 function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
434 if (($thiz =& new User()) == FALSE)
437 $thiz->asta_card = -2;
438 $thiz->asta_pnt = -1;
440 $thiz->exitislock = TRUE;
441 $thiz->privflags = 0;
447 function parentcopy(&$from)
452 function copy(&$from)
454 $this->parentcopy($from);
456 $this->asta_card = $from->asta_card;
457 $this->asta_pnt = $from->asta_pnt;
458 $this->handpt = $from->handpt;
459 $this->exitislock = $from->exitislock;
460 $this->privflags = $from->privflags;
464 function &myclone(&$from)
468 if (($thiz =& new User()) == FALSE)
477 function &spawn(&$from, $table, $table_pos)
479 GLOBAL $G_false, $CO_bin5_pref_ring_endauct;
481 if (($thiz =& new User_briskin5()) == FALSE)
484 $thiz->parentcopy($from);
486 $thiz->asta_card = -2;
487 $thiz->asta_pnt = -1;
489 $thiz->exitislock = TRUE;
491 log_wr("Briskin5 constructor");
493 $this->privflags = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
495 $thiz->table_orig = $table;
497 $thiz->table_pos = $table_pos;
501 } // end class User_briskin5
508 var $comm; // commands for many people
509 var $step; // current step of the comm array
510 var $garbage_timeout;
519 function Briskin5 (&$room, $table_idx, $table_token) {
520 $this->user = array();
521 $this->table = array();
523 $this->the_end = FALSE;
524 $this->shm_sz = BRISKIN5_SHM_MIN;
525 if (($this->tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
530 $user =& $room->user;
531 $table =& $room->table[$table_idx];
533 log_wr("Briskin5 constructor");
535 for ($i = 0 ; $i < $table->player_n ; $i++) {
536 $user[$table->player[$i]]->table_token = $table_token;
537 $this->user[$i] =& User_briskin5::spawn(&$user[$table->player[$i]], $table_idx, $i);
539 $this->table[0] =& Table_briskin5::spawn(&$table);
541 log_main("TABLE_OLD_WIN - Briskin5:".$this->table[0]->old_win);
543 $this->table_idx = $table_idx;
544 $this->table_token = $table_token;
545 $this->garbage_timeout = 0;
547 log_wr("Briskin5 constructor end");
551 function &get_user($sess, &$idx)
553 GLOBAL $PHP_SELF, $G_false;
555 if (validate_sess($sess)) {
556 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
557 if (strcmp($sess, $this->user[$i]->sess) == 0) {
560 $ret = &$this->user[$i];
564 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
565 // for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++)
566 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
569 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
576 function garbage_manager($force)
579 /* Garbage collector degli utenti in timeout */
582 if ($force || $this->garbage_timeout < $curtime) {
584 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
585 $user_cur = &$this->user[$i];
586 if ($user_cur->sess == "" ||
587 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
590 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
591 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
593 if ($user_cur->stat == 'table') {
594 log_auth($user_cur->sess," bin5 Autologout session.");
596 /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE
598 $tmp_sess = $user_cur->sess;
599 $user_cur->sess = "";
600 step_unproxy($tmp_sess);
601 $user_cur->name = "";
602 $user_cur->the_end = FALSE;
606 /* se gli altri utenti non erano d'accordo questo utente viene bannato */
607 $remcalc = $this->table[0]->exitlock_calc(&$this->user, $user_cur->table_pos);
609 require_once("/var/www/webspace/trusty/Obj/hardban.phh");
610 Hardbans::add(($user_cur->flags & USER_FLAG_AUTH ? $user_cur->name : FALSE),
611 $user_cur->ip, $user_cur->sess, $user_cur->laccwr + BAN_TIME);
613 // $user->bantime = $user->laccwr + BAN_TIME;
615 $this->table_wakeup(&$user_cur);
619 log_rd2($user_cur->sess." GARBAGE UPDATED!");
621 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
634 function &load_data($table_idx, $table_token = "")
636 GLOBAL $G_false, $sess;
640 log_wr("TABLE_IDX ".FTOK_PATH."/table".$table_idx);
643 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
644 log_main("ftok failed");
649 if (($shm_sz = sharedmem_sz($tok)) == -1) {
650 log_main("shmop_open failed");
654 if (($shm = shm_attach($tok, $shm_sz)) == FALSE)
657 if (($bri = @shm_get_var($shm, $tok)) == FALSE)
660 if ($table_token != "" && $bri->table_token != $table_token) {
661 log_wr("bri->table_token: ".$bri->table_token."table_token: ".$table_token);
676 log_wr("briskin5 load_data failed");
685 function save_data(&$bri)
692 log_main("SAVE BRISKIN5 DATA");
694 if (!isset($bri->tok))
697 while ($bri->shm_sz < BRISKIN5_SHM_MAX) {
698 if (($shm = shm_attach($bri->tok, $bri->shm_sz)) == FALSE)
701 if (@shm_put_var($shm, $bri->tok, $bri) != FALSE) {
705 if (shm_remove($shm) === FALSE) {
706 log_only("REMOVE FALLITA");
710 $bri->shm_sz += BRISKIN5_SHM_DLT;
713 log_crit("save data failed!");
723 function destroy_data($table_idx)
729 log_main("DESTROY BRISKIN5 DATA");
732 log_main("DESTROY2 BRISKIN5 DATA");
734 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1)
737 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
740 if (shmop_delete($shm) == 0) {
741 log_only("REMOVE FALLITA ");
746 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
748 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
758 function lock_data($table_idx)
762 log_lock("LOCK_DATA ".FTOK_PATH."/table".$table_idx);
763 // echo "LOCK: ".FTOK_PATH."/main";
765 // WARNING monitor this step
766 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
769 // WARNING monitor this step
770 if (($res = @sem_get($tok)) == FALSE) {
773 if (sem_acquire($res)) {
774 log_lock("LOCK table ".$table_idx."[res: ".$res."]");
778 log_lock("LOCK table ".$table_idx.":FAILED");
783 function unlock_data($res)
787 log_lock("UNLOCK table [res: ".$res."]");
789 return (sem_release($res));
793 function chatt_send(&$user, $mesg)
795 GLOBAL $mlang_brisk, $G_lang;
797 if ($user->stat == 'table') {
798 $table = &$this->table[$user->table];
801 $user_mesg = substr($mesg,6);
805 $dt = date("H:i ", $curtime);
806 if (strncmp($user_mesg, "/nick ", 6) == 0) {
807 log_main($user->sess." chatt_send BEGIN");
809 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
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"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
816 $user_mesg = "COMMAND ".$user_mesg;
819 // update local graph
820 // update remote graphs
821 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
822 $user_cur = &$this->user[$i];
823 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
824 if ($user_cur->sess == '')
826 if ($user_cur->name == $name_new) {
827 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
829 $premsg = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
830 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $premsg);
835 if ($i == BRISKIN5_MAX_PLAYERS) {
836 if ($user->flags & USER_FLAG_AUTH && strcasecmp($user->name,$name_new) != 0) {
837 if ($this->table[$user->table]->auth_only == TRUE) {
838 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
839 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>%s</b>");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
844 $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
848 $user->name = $name_new;
850 log_main($user->sess." chatt_send start set");
853 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
854 log_main($user->sess." chatt_send set loop");
856 $user_cur = &$this->user[$i];
857 if ($user_cur->sess == '')
860 if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
861 $table = &$this->table[$user->table];
863 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
864 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
865 $this->user[$table->player[($user_cur->table_pos) % BRISKIN5_PLAYERS_N]]->flags,
866 xcape($this->user[$table->player[($user_cur->table_pos) % BRISKIN5_PLAYERS_N]]->name),
868 $this->user[$table->player[($user_cur->table_pos+1) % BRISKIN5_PLAYERS_N]]->flags,
869 xcape($this->user[$table->player[($user_cur->table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
871 $this->user[$table->player[($user_cur->table_pos+2) % BRISKIN5_PLAYERS_N]]->flags,
872 xcape($this->user[$table->player[($user_cur->table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
874 (BRISKIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+3) % BRISKIN5_PLAYERS_N]]->flags),
875 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
877 (BRISKIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+4) % BRISKIN5_PLAYERS_N]]->flags),
878 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
879 if ($user_cur == $user) {
880 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
881 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
882 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";',
883 $itin, xcape($user->name,ENT_COMPAT,"UTF-8"), $itou);
885 $user_cur->step_inc();
891 for ($i = 0 ; $i < ($user->stat == 'room' ? BRISKIN5_MAX_PLAYERS : BRISKIN5_PLAYERS_N) ; $i++) {
892 if ($user->stat == 'room') {
893 $user_cur = &$this->user[$i];
894 if ($user_cur->sess == '' || $user_cur->stat != 'room')
898 $user_cur = &$this->user[$table->player[$i]];
901 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
902 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
903 $dt, $user->flags, xcape($user->name), xcape($user_mesg));
904 $user_cur->step_inc();
906 log_legal($curtime, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
910 function table_wakeup(&$user)
912 $table = &$this->table[0];
914 log_main("BRISKIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
918 log_main("BRISKIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
920 for ($i = 0 ; $i < $table->player_n ; $i++) {
921 $user_cur = &$this->user[$i];
922 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
924 if ($user_cur == $user)
925 $user_cur->subst = "shutdowner";
927 $user_cur->subst = "shutdowned";
928 $user_cur->laccwr = $curtime;
930 $ret = "gst.st = ".($user_cur->step+1)."; ";
931 $ret .= 'gst.st_loc++; the_end=true; window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
933 log_wr($user_cur->sess." BRISKIN5_WAKEUP: ".$ret);
934 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
935 $user_cur->step_inc();
938 $this->the_end = TRUE;
942 * If all players are freezed the room garbage_manager clean up table and users.
944 function is_abandoned()
949 $table = &$this->table[0];
951 for ($i = 0 ; $i < $table->player_n ; $i++) {
952 $user_cur = &$this->user[$i];
954 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
962 } // end class Briskin5
964 function locshm_exists($tok)
968 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
969 log_main($tok." SHM NOT exists");
975 log_main($tok." SHM exists");
982 function asta2mult($asta_pnt)
986 else if ($asta_pnt > 100)
988 else if ($asta_pnt > 90)
990 else if ($asta_pnt > 80)
992 else if ($asta_pnt > 70)
998 // rendiamo qui l'elenco dei punti come return della func
999 function calculate_points(&$table)
1001 GLOBAL $G_all_points;
1006 if ($table->asta_pnt == 60)
1007 $table->asta_pnt = 61;
1009 $table->old_reason = "";
1010 $table->old_win = $table->asta_win;
1011 $table->old_friend = $table->friend;
1012 $table->old_asta_pnt = $table->asta_pnt;
1013 $table->old_mult = $table->mult;
1015 for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1016 // for ($i = 0 ; $i < 40 ; $i++) {
1017 $ctt = $table->card[$i]->value % 10;
1018 $own = $table->card[$i]->owner;
1019 if ($own == $table->asta_win || $own == $table->friend)
1020 $pro += $G_all_points[$ctt];
1023 log_wr(sprintf("PRO: [%d]", $pro));
1026 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1027 $table->points[$table->points_n % MAX_POINTS] = array();
1028 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1029 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1033 $table->old_pnt = $pro;
1039 if ($pro >= $table->asta_pnt)
1044 $gamult = asta2mult($table->asta_pnt);
1046 $table->points[$table->points_n % MAX_POINTS] = array();
1047 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1048 if ($i == $table->asta_win)
1049 $pt = ($i == $table->friend ? 4 : 2);
1050 else if ($i == $table->friend)
1057 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1059 $pt = $pt * $sig * ($gamult + $table->mult) * ($pro == 120 ? 2 : 1);
1061 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1063 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1064 $table->total[$i] += $pt;
1068 $table->old_pnt = $pro;
1075 is_transition (is from room to table ?)
1076 is_again (is another game)
1078 Examples of $is_transition, $is_again:
1079 from reload of the page: FALSE, FALSE
1080 from sitdown in room: TRUE, FALSE
1081 from table: asta cmd e tutti passano: TRUE, TRUE
1082 from table: fine partita: TRUE, TRUE
1084 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
1086 $table_idx = $user->table;
1087 $table = &$room->table[$table_idx];
1088 $table_pos = $user->table_pos;
1090 $ret = "table_init();";
1091 $ret .= $table->exitlock_show(&$room->user, $table_pos);
1093 /* GENERAL STATUS */
1094 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1095 $sendstep, $user->stat, $user->subst, $table_pos);
1097 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1100 $ret .= "background_set();";
1103 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
1104 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
1106 $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', $itin, xcape($user->name), $itou);
1107 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1108 $room->user[$table->player[($table_pos) % BRISKIN5_PLAYERS_N]]->flags,
1109 xcape($room->user[$table->player[($table_pos) % BRISKIN5_PLAYERS_N]]->name),
1111 $room->user[$table->player[($table_pos+1) % BRISKIN5_PLAYERS_N]]->flags,
1112 xcape($room->user[$table->player[($table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
1114 $room->user[$table->player[($table_pos+2) % BRISKIN5_PLAYERS_N]]->flags,
1115 xcape($room->user[$table->player[($table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
1117 (BRISKIN5_PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+3) % BRISKIN5_PLAYERS_N]]->flags),
1118 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
1120 (BRISKIN5_PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+4) % BRISKIN5_PLAYERS_N]]->flags),
1121 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
1123 /* NOTIFY FOR THE CARD MAKER */
1124 if ($is_transition) { // && $user->subst == "asta" superfluo
1125 $ret .= show_table_info(&$room, &$table, $table_pos);
1128 $ret .= table_wellcome($user);
1130 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1131 $ret .= playsound("cow.mp3");
1136 if ($is_transition) { // && $user->subst == "asta" superfluo
1139 for ($i = 0 ; $i < 8 ; $i++) {
1140 for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++) {
1142 for ($o = 0 ; $o < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) && $ct < $i+1 ; $o++) {
1143 // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1144 if ($table->card[$o]->owner == (($e + $table->gstart) % BRISKIN5_PLAYERS_N)) {
1150 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1152 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BRISKIN5_PLAYERS_N,
1153 $i, ((($e + BRISKIN5_PLAYERS_N - $table_pos + $table->gstart) % BRISKIN5_PLAYERS_N) == 0 ?
1154 $table->card[$o]->value : -1),
1155 ($i == 7 && $e == (BRISKIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1160 $taked = array(0,0,0,0,0);
1161 $inhand = array(0,0,0,0,0);
1162 $ontabl = array(-1,-1,-1,-1,-1);
1165 for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1166 // for ($i = 0 ; $i < 40 ; $i++) {
1167 if ($table->card[$i]->stat == 'hand') {
1168 if ($table->card[$i]->owner == $table_pos) {
1169 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1171 $inhand[$table->card[$i]->owner]++;
1173 else if ($table->card[$i]->stat == 'take') {
1174 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1175 $taked[$table->card[$i]->owner]++;
1177 else if ($table->card[$i]->stat == 'table') {
1178 $ontabl[$table->card[$i]->owner] = $i;
1182 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1183 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1185 log_main("Stat table: ".$logg);
1187 /* Set ours cards. */
1189 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1190 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1191 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
1192 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1193 $ret .= sprintf('card_setours(%s);', $oursarg);
1195 /* Dispose all cards */
1196 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1197 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1198 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1199 ($inhand[$i] <= 8 ? $inhand[$i] : 8) , $taked[$i]);
1201 if ($ontabl[$i] != -1) {
1202 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1203 $table->card[$ontabl[$i]]->value,
1204 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1210 if ($user->subst == 'asta') {
1212 /* show users auction status */
1214 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1215 $user_cur = &$room->user[$table->player[$i]];
1216 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1217 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1219 if (BRISKIN5_PLAYERS_N == 3)
1220 $showst .= ",-2,-2";
1221 $ret .= sprintf('document.title = "Brisk - Tavolo %d (asta)";', $user->table_orig);
1222 $ret .= sprintf('show_astat(%s);', $showst);
1224 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1225 /* show card chooser */
1226 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1231 if ($table_pos == ($table->gstart % BRISKIN5_PLAYERS_N) &&
1232 $table->asta_win == -1)
1233 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1234 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1236 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1237 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1241 if ($table->asta_win == -1) { // auction case
1242 if ($table_pos == ($table->gstart % BRISKIN5_PLAYERS_N))
1243 $ret .= "remark_on();";
1245 $ret .= "remark_off();";
1247 else { // chooseed case
1248 if ($table_pos == $table->asta_win)
1249 $ret .= "remark_on();";
1251 $ret .= "remark_off();";
1254 else if ($user->subst == 'game') {
1256 if (($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N == $table_pos)
1257 $ret .= "is_my_time = true; remark_on();";
1259 $ret .= "remark_off();";
1261 /* WHO CALL AND WHAT */
1262 $ret .= briscola_show($room, $table, $user);
1266 } // end function show_table(...
1268 function calculate_winner(&$table)
1275 $cur_seed = $table->briscola - ($table->briscola % 10);
1277 for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1278 // for ($i = 0 ; $i < 40 ; $i++) {
1279 if ($table->card[$i]->stat != "table")
1282 log_wr(sprintf("Card On table: [%d]", $i));
1284 $v = $table->card[$i]->value;
1285 $ontab[$table->card[$i]->owner] = $v;
1286 $ontid[$table->card[$i]->owner] = $i;
1287 /* se briscola setto il flag */
1288 if (($v - ($v % 10)) == $cur_seed)
1292 if ($briontab == FALSE) {
1293 $cur_win = $table->gstart;
1294 $cur_val = $ontab[$cur_win];
1295 $cur_seed = $cur_val - ($cur_val % 10);
1298 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1299 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1300 if ($ontab[$i] < $cur_val) {
1301 $cur_val = $ontab[$i];
1307 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1308 $table->card[$ontid[$i]]->owner = $cur_win;
1309 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1314 function show_table_info(&$room, &$table, $table_pos)
1316 GLOBAL $G_lang, $mlang_bin5_bin5;
1319 $user = &$room->user[$table->player[$table_pos]];
1321 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1322 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1325 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++)
1326 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
1327 $noty .= sprintf("</tr>");
1330 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1332 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1333 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1334 for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++)
1335 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1340 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1341 for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++)
1342 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1343 $noty .= "</tr></table>";
1345 if ($table->old_reason != "") {
1346 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1349 if ($table->old_win != -1) {
1350 log_main("TABLE_OLD_WIN:".$table->old_win);
1351 $win = $table->player[$table->old_win];
1352 log_main("TABLE_OLD_FRIEND:".$table->old_friend);
1353 $fri = $table->player[$table->old_friend];
1355 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1358 /* 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" */
1360 $noty .= sprintf($mlang_bin5_bin5['info_part'][$G_lang],
1361 xcape($room->user[$win]->name),
1362 xcape($room->user[$fri]->name));
1363 if ($table->old_pnt == 120) {
1364 $noty .= sprintf($mlang_bin5_bin5['info_capp'][$G_lang]);
1367 $noty .= sprintf($mlang_bin5_bin5['info_pnt'][$G_lang],
1368 ($table->old_asta_pnt > 61 ? $mlang_bin5_bin5['info_alea'][$G_lang].$table->old_asta_pnt :
1369 $mlang_bin5_bin5['info_more'][$G_lang]), $table->old_pnt,
1370 ($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])));
1374 $noty .= sprintf($mlang_bin5_bin5['info_alon'][$G_lang],
1375 xcape($room->user[$win]->name));
1376 if ($table->old_pnt == 120) {
1377 $noty .= sprintf($mlang_bin5_bin5['info_acap'][$G_lang]);
1380 $noty .= sprintf($mlang_bin5_bin5['info_apnt'][$G_lang],
1382 ($table->old_asta_pnt > 61 ? $mlang_bin5_bin5['info_alea'][$G_lang].$table->old_asta_pnt :
1383 $mlang_bin5_bin5['info_more'][$G_lang]), $table->old_pnt,
1385 ($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])));
1388 if (($table->old_mult + asta2mult($table->old_asta_pnt)) > 1) {
1389 $noty .= sprintf($mlang_bin5_bin5['info_omul'][$G_lang], multoval($table->old_mult + asta2mult($table->old_asta_pnt)));
1391 $noty .= "<hr><br>";
1393 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1394 if ($table->mazzo == $table_pos)
1395 $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
1397 $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
1398 $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
1401 if ($user->subst == 'asta') {
1402 if ($table->asta_win == -1) // auction case
1403 $curplayer = $table->gstart % BRISKIN5_PLAYERS_N;
1405 $curplayer = $table->asta_win;
1407 else if ($user->subst == 'game') {
1408 $curplayer = ($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N;
1411 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1412 if ($curplayer == $table_pos) {
1413 $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
1416 $unam = xcape($room->user[$table->player[$curplayer]]->name);
1417 $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
1420 if (($table->mult + asta2mult($table->asta_pnt)) > 1) {
1421 $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($table->mult + asta2mult($table->asta_pnt)));
1423 $noty .= "<hr><br>";
1424 $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
1425 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
1426 * 130 + ($table->points_n > 0 ? 50 : 0) +
1427 * (120 * ($table->points_n / MAX_POINTS)));
1428 * will be used when we refact notify js function following
1429 * photoo class logic
1435 function table_wellcome($user)
1437 GLOBAL $table_wellarr, $G_lang;
1440 for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
1441 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
1447 function briscola_show($room, $table, $user)
1449 GLOBAL $G_lang, $mlang_bin5_bin5;
1453 if ($table->asta_card == 9)
1454 $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
1456 /* text of caller cell */
1457 if ($user->table_pos == $table->asta_win) {
1458 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
1459 $ret .= sprintf($prestr, $ptnadd);
1462 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
1463 $ret .= sprintf($prestr,
1464 xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
1466 $ret .= sprintf('set_iscalling(%d);', ($table->asta_win - $user->table_pos + BRISKIN5_PLAYERS_N) % BRISKIN5_PLAYERS_N);
1468 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1470 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1471 $ret .= sprintf('$("caller").style.visibility = "visible";');
1472 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1473 $ret .= sprintf('$("astalascio").style.visibility = "";');
1474 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1475 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1481 function game_result($asta_pnt, $pnt)
1483 if ($asta_pnt == 61) {
1486 else if ($pnt == 60)
1492 if ($pnt >= $asta_pnt)