5 * Copyright (C) 2006-2008 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(BRISK_CONF, "brisk.conf.pho");
27 define(FTOK_PATH, "/var/lib/brisk");
28 define(LEGAL_PATH, "/tmp/legal_brisk");
29 define(PROXY_PATH, "/var/lib/brisk_proxy");
32 define(MAX_POINTS, 5);
33 define(MAX_PLAYERS, (20 + (PLAYERS_N * TABLES_N)));
34 define(SHM_DIMS_MIN, (50000 + 10000 * TABLES_N + 15000 * MAX_PLAYERS));
35 define(SHM_DIMS_MAX, SHM_DIMS_MIN + 1048576);
36 define(SHM_DIMS_DLT, 65536);
39 define(COMM_GEN_N, 50);
42 define(CHAT_ILL_TIME, 6);
45 define(STREAM_TIMEOUT, 20);
46 define(EXPIRE_TIME_RD, 180);
47 define(EXPIRE_TIME_SMAMMA, 360);
48 define(EXPIRE_TIME_WAG, 10);
49 define(WAKEUP_TIME, 12);
50 // BAN_TIME da allineare anche in commons.js
51 define(BAN_TIME, 3600);
52 define(GARBAGE_TIMEOUT, 10);
53 define(NICKSERV, "<i>BriskServ</i>");
55 define(DBG_ONL2, 0x0001);
56 define(DBG_ONLY, 0x0002);
57 define(DBG_MAIN, 0x0004);
58 define(DBG_READ, 0x0008);
59 define(DBG_REA2, 0x0010);
60 define(DBG_SEND, 0x0020);
61 define(DBG_LOCK, 0x0040);
62 define(DBG_WRIT, 0x0080);
63 define(DBG_LOAD, 0x0100);
64 define(DBG_AUTH, 0x0200);
65 define(DBG_CRIT, 0x0400);
67 define(BRISK_DEBUG, 0);
69 define(BRISK_SINGLE_DEBUG,0);
70 define(BRISK_SINGLE_SESS, "");
71 // define(DEBUGGING, "local");
73 require_once("$DOCUMENT_ROOT/Etc/".BRISK_CONF);
77 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
78 $G_brisk_version = "2.1.3 - trusty";
80 $root_wellarr = Array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: Garanzia dal sito e stato degli utenti (guarda l\'help).',
81 'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
82 $table_wellarr = 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.');
86 <div style=\\"text-align: left; padding: 8px;\\">
87 <b>Descrizione</b><br>
88 Questa รจ un\'implementazione della briscola in cinque, così come è spiegata su
89 <a target=\\"_blank\\" href=\\"http://it.wikipedia.org/wiki/Briscola#Gioco_a_5\\">Wikipedia</a>; in breve è la variante con l\'asta prima sulla carta e poi sui punti.<br><br>
90 <b>Configurazione del browser.</b><br>
91 Occorre abilitare i cookies.<br>
93 <b>Uso del sito</b><br>
94 Potete sedervi a un tavolo o rimanere in piedi.<br>
95 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
98 All\'inizio vengono distribuite le carte e parte l\'asta; per partecipare all\'asta, quando sarà il vostro turno, potrete scegliere se andare avanti o passare cliccando sulle icone corrispondenti. Se si arriva ai punti, scrivete nella textbox il vostro rilancio e cliccate PUNTI.<br><br>
99 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
100 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
101 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
102 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
103 Dopo che è iniziata una partita per uscirne dovete chiedere agli altri giocatori di sbloccarla cliccando sul lucchetto. Se non si segue questa prassi, una volta usciti, non vi potrete sedere a nessun tavolo per '.floor(BAN_TIME/60).' minuti.
105 <dt><b>Comandi della chat</b>
106 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
107 <dd><b>/tav <i><frase di invito></i></b> - invito per gli altri giocatori al tavolo dove si è seduti
108 <dd><b>/st <i><stato></i></b> - cambia l\'icona associata al tuo user; <i>stato</i> può valere: \\"normale\\", \\"fuori\\", \\"pausa\\", \\"cibo\\", \\"cane\\", \\"lavoro\\" oppure \\"sigaretta\\"
109 <dd><b>/garante</b> - se si è autenticati permette di garantire per un utente fidato
117 <div id=\\"header\\" class=\\"header\\">
118 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
119 briscola chiamata in salsa ajax
121 <br><b>version '.$G_brisk_version.'</b><br><br>
122 Copyright 2006-2008 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
127 $from = array ( '\\', '@', '|' );
128 $to = array ( '\\\\', '@', '¦' );
130 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
135 $from = array ( '\\', '|' );
136 $to = array ( '\\\\', '\\|' );
138 return (str_replace($from, $to, $s));
142 var $value; /* 0 - 39 card value */
143 var $stat; /* 'bunch', 'hand', 'table', 'take' */
144 var $owner; /* (table position 0-4) */
145 // var $pos; /* Pos in hand. */
146 var $x; /* When played the X position on the table of the owner. */
147 var $y; /* When played the Y position on the table of the owner. */
149 function Card($value, $stat, $owner)
151 $this->value = $value;
152 $this->stat = $stat; // Card stat
153 $this->owner = $owner;
156 function assign($stat,$owner)
158 $this->stat = $stat; // Card stat
159 $this->owner = $owner;
162 function setpos($pos)
169 $this->stat = 'table'; // Card stat
174 function take($newown)
176 $this->stat = 'take'; // Card stat
177 $this->owner = $newown;
201 var $points; // points array
202 var $points_n; // number of row of points
224 function &create($idx)
228 if (($thiz =& new Table()) == FALSE)
232 $thiz->player = array();
235 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
236 $thiz->asta_pla_n= -1;
237 $thiz->asta_card = -1;
238 $thiz->asta_pnt = -1;
241 $thiz->auth_only = FALSE;
243 $thiz->points = array( );
245 $thiz->total = array( 0, 0, 0, 0, 0);
246 $thiz->asta_win = -1;
247 $thiz->briscola = -1;
251 $thiz->wag_own = NULL;
255 $thiz->old_reason = "";
256 $thiz->old_asta_pnt = -1;
259 $thiz->old_friend = -1;
261 $thiz->table_token = "";
262 $thiz->table_start = 0;
264 $thiz->wakeup_time = 0;
269 function &clone(&$from)
273 if (($thiz =& new Table()) == FALSE)
276 $thiz->idx = $from->idx;
277 $thiz->player = array();
278 for ($i = 0 ; $i < $from->player_n ; $i++)
279 $thiz->player[$i] = $from->player[$i];
280 $thiz->player_n = $from->player_n;
281 $thiz->card = $from->card;
282 $thiz->mazzo = $from->mazzo; // REVIEW
283 $thiz->gstart = $from->gstart;
284 $thiz->turn = $from->turn;
286 $thiz->auth_only = $from->auth_only;
288 $thiz->wag_own = $from->wag_own;
289 $thiz->wag_com = $from->wag_com;
290 $thiz->wag_tout = $from->wag_taut;
292 $thiz->asta_pla = $from->asta_pla;
293 $thiz->asta_pla_n = $from->asta_pla_n;
294 $thiz->asta_card = $from->asta_card;
295 $thiz->asta_pnt = $from->asta_pnt;
297 $thiz->mult = $from->mult;
298 $thiz->points = $from->points;
299 $thiz->points_n = $from->points_n;
300 $thiz->total = $from->total;
302 $thiz->asta_win = $from->asta_win;
303 $thiz->briscola = $from->briscola;
304 $thiz->friend = $from->friend;
306 $thiz->old_reason = $from->old_reason;
307 $thiz->old_asta_pnt = $from->old_asta_pnt;
308 $thiz->old_pnt = $from->old_pnt;
309 $thiz->old_win = $from->old_win;
310 $thiz->old_friend = $from->old_friend;
312 $thiz->table_token = $from->table_token;
313 $thiz->table_start = $from->table_start;
315 $thiz->wakeup_time = $from->wakeup_time;
320 function &spawn(&$from)
324 if (($thiz =& new Table()) == FALSE)
327 $thiz->idx = $from->idx;
328 $thiz->player_n = $from->player_n;
329 $thiz->card = &$thiz->bunch_create();
330 $thiz->mazzo = $from->mazzo;
331 $thiz->gstart = $from->gstart;
332 $thiz->turn = $from->turn;
334 $thiz->auth_only = $from->auth_only;
336 $thiz->wag_own = $from->wag_own;
337 $thiz->wag_com = $from->wag_com;
338 $thiz->wag_tout = $from->wag_taut;
340 $thiz->asta_pla = $from->asta_pla;
341 $thiz->asta_pla_n = $from->asta_pla_n;
342 $thiz->asta_card = $from->asta_card;
343 $thiz->asta_pnt = $from->asta_pnt;
345 $thiz->mult = $from->mult;
346 $thiz->points = $from->points;
347 $thiz->points_n = $from->points_n;
348 $thiz->total = $from->total;
350 $thiz->asta_win = $from->asta_win;
351 $thiz->briscola = $from->briscola;
352 $thiz->friend = $from->friend;
354 $thiz->old_reason = $from->old_reason;
355 $thiz->old_asta_pnt = $from->old_asta_pnt;
356 $thiz->old_pnt = $from->old_pnt;
357 $thiz->old_win = $from->old_win;
358 $thiz->old_friend = $from->old_friend;
360 // players are rearranged in an dedicated array
361 $thiz->player = array();
362 for ($i = 0 ; $i < $from->player_n ; $i++)
363 $thiz->player[$i] = $i;
365 $thiz->table_token = $from->table_token;
366 $thiz->table_start = $from->table_start;
368 $thiz->wakeup_time = $from->wakeup_time;
373 function &bunch_create()
377 for ($i = 0 ; $i < 40 ; $i++) {
378 $ret[$i] =& new Card($i, 'bunch', 'no_owner');
385 function wag_set(&$user, $mesg)
389 $this->wag_own = &$user;
390 $this->wag_com = $mesg;
394 function wag_reset($timeout)
396 log_main("WAG_RESET");
398 unset($this->wag_own);
399 $this->wag_own = NULL;
401 $this->wag_tout = $timeout;
404 function bunch_make()
406 $ct = array(0,0,0,0,0);
408 mt_srand(make_seed());
410 for ($i = 39 ; $i >= 0 ; $i--)
413 for ($i = 39 ; $i >= 0 ; $i--) {
417 log_main("RND ZERO");
422 $this->card[$id]->assign('hand', $owner);
424 $rest[$rn] = $rest[$i];
425 // $pubbpos[$rn2] = $pubbpos[$i];
429 function init(&$userarr)
431 $this->mazzo = rand(0,PLAYERS_N-1);
435 $this->old_reason = "";
436 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
437 $this->total[$i] = 0;
438 $user_cur = &$userarr[$this->player[$i]];
439 $user_cur->exitislock = TRUE;
442 log_main("table::init: ci siamo");
445 function game_init(&$userarr)
449 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
453 $this->asta_pla_n = PLAYERS_N;
454 $this->asta_card = -1;
455 $this->asta_pnt = 60;
456 $this->asta_win = -1;
457 $this->briscola = -1;
461 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
462 $this->asta_pla[$i] = TRUE;
463 $user_cur = &$userarr[$this->player[$i]];
464 $user_cur->subst = 'asta';
465 $user_cur->asta_card = -2;
466 $user_cur->asta_pnt = -1;
467 $user_cur->handpt = $this->hand_points($i);
468 // SEE function calculate_points(&$table)
474 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
477 function getPlayer($idx)
479 return ($this->player[$idx]);
482 function setPlayer($idx, $player)
484 $this->player[$idx] = $player;
487 function user_add($idx)
489 $this->player[$this->player_n] = $idx;
492 return ($this->player_n - 1);
495 function user_rem(&$room, &$user)
497 $tabpos = $user->table_pos;
499 /* verifico la consistenza dei dati */
500 if ($room->user[$this->player[$tabpos]] == $user) {
502 /* aggiorna l'array dei giocatori al tavolo. */
503 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
504 $this->player[$i] = $this->player[$i+1];
505 $user_cur = &$room->user[$this->player[$i]];
506 $user_cur->table_pos = $i;
511 log_main("INCONSISTENCY ON TABLE.");
515 function hand_points($idx)
517 GLOBAL $G_all_points;
521 for ($i = 0 ; $i < 40 ; $i++) {
522 if ($this->card[$i]->owner != $idx)
525 $ctt = $this->card[$i]->value % 10;
526 $tot += $G_all_points[$ctt];
532 function exitlock_show(&$userarr, $table_pos)
534 $ct = $this->exitlock_calc(&$userarr, $table_pos);
536 $ret = sprintf('exitlock_show(%d, %s);', $ct,
537 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
541 function exitlock_calc(&$userarr, $table_pos)
545 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
546 if ($userarr[$this->player[$i]]->exitislock == FALSE)
556 // $ret .= table_act_content(($user->subst == 'standup'), $this->table[$i]->player_n, $i, $user->table,
557 // ($this->table[$i]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
559 // function act_content($isstanding, $sitted, $table, $cur_table, $allowed)
560 function act_content(&$user)
563 $isstanding = ($user->subst == 'standup');
564 $sitted = $this->player_n;
566 $cur_table = $user->table;
570 if ($sitted < PLAYERS_N) {
571 if ($this->auth_only) {
572 if ($user->flags & USER_FLAG_AUTH)
586 if ($table == $cur_table)
593 $ret = sprintf('j_tab_act_cont(%d, \'%s\');', $table, $act);
603 define(USER_FLAG_AUTH, 0x02);
606 define(USER_FLAG_S_NORM, 0x000); // done
607 define(USER_FLAG_S_PAU, 0x100); // done
608 define(USER_FLAG_S_OUT, 0x200); // done
609 define(USER_FLAG_S_DOG, 0x300); // done
610 define(USER_FLAG_S_EAT, 0x400); // done
611 define(USER_FLAG_S_WRK, 0x500); // done
612 define(USER_FLAG_S_SMK, 0x600); // done
614 define(USER_FLAG_S_ALL, 0xf00); // done
617 var $name; // name of the user
618 var $sess; // session of the user
619 var $ip; // ip of the user
620 var $lacc; // last access (for the cleanup)
621 var $laccwr; // last access (for the cleanup)
622 var $bantime; // timeout to temporary ban
623 var $stat; // status (outdoor, room, table, game, ...)
624 var $subst; // substatus for each status
625 var $step; // step of the current status
626 var $trans_step; // step to enable transition between pages (disable == -1)
627 var $comm; // commands array
630 var $handpt; // Total card points at the beginning of the current hand.
631 var $exitislock; // Player can exit from the table ?
632 var $table; // id of the current table (if in table state)
633 var $table_pos; // idx on the table
634 var $table_token;// token that identify a game on a table
635 var $flags; // Bitfield with: AUTHENTICATE: 0x02
636 var $the_end; // Flag to change the end of the session
638 var $chat_lst; // Last chat line
639 var $chattime; // Array of chat times
640 var $chat_cur; // Current chat line number
641 var $chat_ban; // Time for ban chat
642 var $chat_dlt; // Delta t for ban
646 function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
649 if (($thiz =& new User()) == FALSE)
655 $thiz->lacc = time();
656 $thiz->laccwr = time();
659 $thiz->subst = $subst;
661 $thiz->trans_step = -1;
662 $thiz->comm = array();
663 $thiz->asta_card = -2;
664 $thiz->asta_pnt = -1;
666 $thiz->exitislock = TRUE;
670 $thiz->chattime = array_fill(0, CHAT_N, 0);
672 $thiz->chat_lst = "";
676 $thiz->table = $table;
677 $thiz->table_pos = -1;
678 $thiz->table_token = "";
683 function &clone(&$from)
687 if (($thiz =& new User()) == FALSE)
690 $thiz->name = $from->name;
691 $thiz->sess = $from->sess;
692 $thiz->ip = $from->ip;
693 $thiz->lacc = $from->lacc;
694 $thiz->laccwr = $from->laccwr;
695 $thiz->bantime = $from->bantime;
696 $thiz->stat = $from->stat;
697 $thiz->subst = $from->subst;
698 $thiz->step = $from->step;
699 $thiz->trans_step = $from->trans_step;
700 $thiz->comm = array();
702 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
703 for ($i = $i_start ; $i < $from->step ; $i++) {
705 $thiz->comm[$ii] = $from->comm[$ii];
707 $thiz->asta_card = $from->asta_card;
708 $thiz->asta_pnt = $from->asta_pnt;
709 $thiz->handpt = $from->handpt;
710 $thiz->exitislock = $from->exitislock;
712 $thiz->flags = $from->flags;
714 $thiz->chattime = array();
715 for ($i = 0 ; $i < CHAT_N ; $i++)
716 $thiz->chattime[$i] = $from->chattime[$i];
717 $thiz->chat_cur = $from->chat_cur;
718 $thiz->chat_lst = $from->chat_lst;
719 $thiz->chat_ban = $from->chat_ban;
720 $thiz->chat_dlt = $from->chat_dlt;
722 $thiz->table = $from->table;
723 $thiz->table_pos = $from->table_pos;
724 $thiz->table_token = $from->table_token;
725 $thiz->the_end = $from->the_end;
730 function &spawn(&$from, $table, $table_pos)
734 if (($thiz =& new User()) == FALSE)
737 $thiz->name = $from->name;
738 $thiz->sess = $from->sess;
739 $thiz->ip = $from->ip;
740 $thiz->lacc = $from->lacc;
741 $thiz->laccwr = $from->laccwr;
742 $thiz->bantime = $from->bantime;
743 $thiz->stat = $from->stat;
744 $thiz->subst = $from->subst;
745 $thiz->step = $from->step;
746 $thiz->trans_step = $from->trans_step;
747 $thiz->comm = array();
750 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
751 for ($i = $i_start ; $i < $from->step ; $i++) {
752 log_wr("TRY PUSH:".$i);
754 $thiz->comm[$ii] = $from->comm[$ii];
757 $thiz->asta_card = $from->asta_card;
758 $thiz->asta_pnt = $from->asta_pnt;
759 $thiz->handpt = $from->handpt;
760 $thiz->exitislock = $from->exitislock;
761 $thiz->the_end = $from->the_end;
763 $thiz->flags = $from->flags;
765 $thiz->chattime = array_fill(0, CHAT_N, 0);
767 $thiz->chat_lst = "";
772 $thiz->table = $table;
773 $thiz->table_pos = $table_pos;
774 $thiz->table_token = $from->table_token;
779 function stat_set($stat) {
780 log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]");
781 $this->stat = "$stat";
784 if (validate_sess($this->sess)) {
785 if (file_exists(PROXY_PATH) == FALSE)
787 $fp = fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
788 fwrite($fp, sprintf("%s\n",$this->stat));
794 function step_set($step) {
798 if (validate_sess($this->sess) == FALSE)
800 if (file_exists(PROXY_PATH) == FALSE)
802 if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
804 fwrite($fp, pack("l",$this->step), 4);
813 function step_inc($delta = 1) {
814 $this->step += $delta;
816 if (validate_sess($this->sess)) {
817 if (file_exists(PROXY_PATH) == FALSE)
819 $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
820 fwrite($fp, pack("l",$this->step), 4);
831 log_legal($curtime, $this->sess, $this->name, "STAT:LOGOUT", '');
833 $tmp_sess = $this->sess;
835 step_unproxy($tmp_sess);
836 $this->name = ""; // OK here
837 while (array_pop($this->comm) != NULL);
839 $this->chattime = array_fill(0, CHAT_N, 0);
841 $this->chat_lst = "";
844 $this->the_end = FALSE;
849 function step_get($sess) {
852 if (validate_sess($sess) == FALSE)
855 if (file_exists(PROXY_PATH) == FALSE)
857 if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
859 if (($s = fread($fp, 4)) == FALSE)
863 $arr = unpack('l', $s);
866 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
873 log_rd2("STEP_GET: return false ");
878 function step_unproxy($sess) {
879 log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
880 if (file_exists(PROXY_PATH) == FALSE)
882 @unlink(PROXY_PATH."/".$sess.".step");
889 var $comm; // commands for many people
890 var $step; // current step of the comm array
891 var $garbage_timeout;
895 $this->user = array();
896 $this->table = array();
898 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
899 $this->user[$i] =& User::create("", "");
902 for ($i = 0 ; $i < TABLES_N ; $i++) {
903 $this->table[$i] =& Table::create($i);
905 $row = ( (((int)($i / 4)) % 2) == 0 );
906 $col = ($i % 2 == 0);
907 $this->table[$i]->auth_only = (($row && $col) || (!$row && !$col));
910 $this->table[$i]->auth_only = FALSE;
913 $this->garbage_timeout = 0;
916 function garbage_manager($force)
920 log_rd2("garbage_manager START");
922 /* Garbage collector degli utenti in timeout */
924 if ($force || $this->garbage_timeout < $curtime) {
926 // Before all align times with table timeout
927 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
928 $table_cur =& $this->table[$table_idx];
929 // if the table is complete and exists its shared mem we get the info about users lacc
931 if ($table_cur->player_n == PLAYERS_N) {
932 log_main("PLAYERS == N TABLE ".$table_idx);
934 if (($sem = Briskin5::lock_data($table_idx)) != FALSE) {
935 log_main("bin5 lock data success");
937 $no_recovery = FALSE;
938 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
939 if ($table_cur->table_token != $bri->table_token) {
940 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
941 log_main("ERROR: not matching table_start. Room: ".$table_cur->table_start." Table: ".$bri->table_start);
951 log_main("garbage_manager: bri loaded successfully.");
952 $bri->garbage_manager(TRUE);
954 $bri_table = &$bri->table[0];
956 // is the end of the table
957 if ($bri->the_end == TRUE) {
959 * DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN
961 log_main("garbage_manager: INSIDE THE END.");
963 $plist = "$table_cur->table_token|$user_cur->table|$table_cur->player_n";
964 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
965 $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
968 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
969 // stat must be "table" by definition
970 $user_cur =& $this->user[$table_cur->player[$i]];
971 $bri_user =& $bri->user[$i];
973 $user_cur->subst = $bri_user->subst;
974 $user_cur->step = $bri_user->step;
975 $user_cur->lacc = $bri_user->lacc;
976 $user_cur->laccwr = $bri_user->lacc;
977 $user_cur->bantime = $bri_user->bantime;
980 log_legal($curtime, $user_cur->sess, $user_cur->name, "STAT:DESTROY_GAME", $plist);
982 $this->room_join_wakeup(&$user_cur, FALSE, 0);
983 $table_cur->table_token = "";
984 $table_cur->wakeup_time = $curtime + WAKEUP_TIME;
985 Briskin5::destroy_data($table_idx);
988 log_main("gm:: save_data");
990 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
991 $this->user[$table_cur->player[$i]]->lacc = $bri->user[$i]->lacc;
994 Briskin5::save_data(&$bri);
996 } // else if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
997 else if ($no_recovery == FALSE) {
998 log_crit("ERROR: table ".$table_idx." unrecoverable join");
1000 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
1001 $user_cur = &$this->user[$table_cur->player[$i]];
1002 $user_cur->subst = "shutdowner";
1003 $user_cur->step_inc();
1005 $ret = sprintf('stat = "%s"; subst = "%s";', $user_cur->stat, $user_cur->subst);
1006 $ret .= "gst.st = ".($user_cur->step+1)."; ";
1007 $ret .= show_notify("<br>I dati del tavolo n° ".$user_cur->table." sono inconsistenti, verranno resettati.<br><br>Torni in piedi.<br><br>", 2000, "Chiudi.", 400, 110);
1008 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1009 $user_cur->step_inc();
1012 $plist = "$table_cur->table_token|$user_cur->table|$table_cur->player_n";
1013 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
1014 $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
1016 log_legal($curtime, $user_cur->sess, $user_cur->name, "STAT:DESTROY_GAME(RECOVERY)", $plist);
1018 $this->room_join_wakeup(&$user_cur, TRUE, -2);
1019 $table_cur->table_token = "";
1022 Briskin5::unlock_data($sem);
1024 } // if ($table_cur->player_n == PLAYERS_N) {
1025 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1027 log_rd2("out new loop.");
1029 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1030 $user_cur = &$this->user[$i];
1032 log_rd2("User: ".$user_cur->name." stat: ".$user_cur->stat." subst: ".$user_cur->subst);
1034 if ($user_cur->sess == "")
1037 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
1038 // Auto logout dell'utente
1039 log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
1041 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
1042 log_auth($user_cur->sess, "Autologout session.");
1046 log_rd2("AUTO LOGOUT.");
1047 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
1048 $this->room_wakeup(&$user_cur);
1049 else if ($user_cur->subst == 'standup')
1050 $this->room_outstandup(&$user_cur);
1052 log_rd2("LOGOUT FROM WHAT ???");
1056 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
1057 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
1058 $this->room_wakeup(&$user_cur);
1059 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1060 $user_cur->comm[$user_cur->step % COMM_N] .= show_notify("<br>Sei stato inattivo per ".(EXPIRE_TIME_SMAMMA/60.0)." minuti. <br><br>Quindi ritorni tra i <b>Giocatori in piedi</b>.", 0, "torna ai tavoli", 400, 100);
1061 $user_cur->step_inc();
1065 log_rd2("GARBAGE UPDATED!");
1067 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
1074 function show_room($user_step, &$user)
1076 log_main("show_room: username: ".$user->name);
1079 $ret = sprintf('gst.st = %d; ', $user_step);
1080 if ($user->subst == 'standup')
1081 $ret .= "tra.show(); ";
1083 $ret .= "tra.hide(); ";
1085 $ret .= sprintf('stat = "%s";', $user->stat);
1087 $ret .= root_wellcome($user);
1088 $ret .= sprintf('subst = "%s";', $user->subst);
1089 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
1090 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
1091 $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', $itin, xcape($user->name), $itou);
1092 for ($i = 0 ; $i < TABLES_N ; $i++) {
1093 $ret .= $this->table_content($user, $i);
1094 // $ret .= table_act_content(($user->subst == 'standup'), $this->table[$i]->player_n, $i, $user->table,
1095 // ($this->table[$i]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
1096 $ret .= $this->table[$i]->act_content($user);
1097 if ($this->table[$i]->wag_own != NULL)
1098 $ret .= sprintf('tra.add(%d, "%s: %s"); ', $i, $this->table[$i]->wag_own->name, $this->table[$i]->wag_com);
1100 $ret .= sprintf('tra.rem(%d); ', $i);
1102 $ret .= $this->standup_content($user);
1108 function room_wakeup(&$user)
1110 $table_idx = $user->table;
1111 $table = &$this->table[$table_idx];
1113 log_main("WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
1117 $from_table = ($user->stat == "table");
1119 log_main("WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1121 for ($i = 0 ; $i < $table->player_n ; $i++) {
1122 $user_cur = &$this->user[$table->player[$i]];
1123 log_main("PREIMPOST: INLOOP name: ".$user_cur->name);
1125 if ($user_cur != $user) {
1126 $user_cur->stat_set("room");
1127 $user_cur->subst = "sitdown";
1128 $user_cur->laccwr = $curtime;
1130 else if ($user->sess != "") {
1131 $user_cur->stat_set("room");
1132 $user_cur->subst = "standup";
1133 $user_cur->laccwr = $curtime;
1134 $user_cur->table = -1;
1139 $user->stat_set("room");
1140 $user->subst = "standup";
1141 $user->laccwr = $curtime;
1144 $remove_wagon = false;
1145 if($table->wag_own == $user) {
1146 $remove_wagon = true;
1147 $table->wag_reset($curtime);
1152 /* aggiorna l'array dei giocatori al tavolo. */
1153 $table->user_rem(&$this, &$user);
1155 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1156 $user_cur = &$this->user[$i];
1157 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1160 log_main("VALORI: name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
1162 $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
1163 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
1164 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; window.onbeforeunload = null; document.location.assign("index.php");|';
1165 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
1166 log_main("DOCUMENT.index.php: from table");
1168 else if ($user_cur->stat == "room") {
1169 log_main("DOCUMENT.index.php: from table");
1171 $ret .= $this->table_content($user_cur, $table_idx);
1172 $ret .= $this->standup_content($user_cur);
1174 // $ret .= table_act_content(FALSE, 0, $table_idx, $user->table, FALSE);
1175 $ret .= $table->act_content($user);
1177 if ($user_cur == $user) {
1178 // set the new status
1179 $ret .= 'subst = "standup"; tra.show(); ';
1180 // clean the action buttons in other tables
1181 for ($e = 0 ; $e < TABLES_N ; $e++) {
1182 if ($this->table[$e]->player_n < PLAYERS_N) {
1183 // $ret .= table_act_content(TRUE, 0, $e, $user->table,
1184 // ($this->table[$e]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
1185 $ret .= $this->table[$e]->act_content($user);
1190 // $ret .= table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table,
1191 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1192 $ret .= $table->act_content($user_cur);
1195 log_wr("ROOM_WAKEUP: ".$ret);
1196 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1197 $user_cur->step_inc();
1201 function room_join_wakeup(&$user, $update_lacc = FALSE, $trans_delta)
1203 $table_idx = $user->table;
1204 $table = &$this->table[$table_idx];
1206 log_main("JOIN_WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
1209 $user_wup = array();
1211 $user_tab = array();
1213 log_main("JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1215 for ($i = 0 ; $i < $table->player_n ; $i++) {
1216 $user_cur = &$this->user[$table->player[$i]];
1217 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1218 if ($user_cur->sess != "") {
1219 if ($update_lacc == TRUE) {
1220 $user_cur->laccwr = $curtime;
1222 log_main("cur: ".$user_cur->name." subst: ".$user_cur->subst);
1223 if ($user_cur->subst == "shutdowned") {
1224 $user_cur->stat_set("room");
1225 $user_cur->subst = "sitdown";
1227 else if ($user_cur->subst == "shutdowner") {
1228 $user_cur->stat_set("room");
1229 $user_cur->subst = "standup";
1230 $user_cur->table = -1;
1231 $user_wup[$user_wup_n++] = &$user_cur;
1233 $remove_wagon = false;
1234 if($table->wag_own == $user_cur) {
1235 $remove_wagon = true;
1236 $table->wag_reset($curtime);
1239 $user_tab[$user_tab_n++] = &$user_cur;
1243 for ($wup_idx = 0 ; $wup_idx < $user_wup_n ; $wup_idx++)
1244 $table->user_rem(&$this, &$user_wup[$wup_idx]);
1246 /* aggiorna l'array dei giocatori al tavolo. */
1248 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1249 log_main("START LOOP");
1250 $user_cur = &$this->user[$i];
1251 if ($user_cur->sess == '' || $user_cur->stat != 'room') {
1252 log_main("name: ".$user_cur->name."skip subst: ".$user_cur->subst);
1257 log_main("VALORI name: ".$user_cur->name." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
1259 $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
1260 if ($user_cur->stat == "room") {
1261 log_main("DOCUMENT.index.php from table");
1263 $ret .= $this->table_content($user_cur, $table_idx);
1264 $ret .= $this->standup_content($user_cur);
1266 // $ret .= table_act_content(FALSE, 0, $table_idx, $user_cur->table,
1267 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1268 $ret .= $table->act_content($user_cur);
1271 for ($tab_idx = 0 ; $tab_idx < $user_tab_n ; $tab_idx++)
1272 if ($user_cur == $user_tab[$tab_idx])
1275 // for users that wakeup the room will be reconstructed by index_rd.php
1276 if ($tab_idx < $user_tab_n) {
1277 log_main("PRE show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1279 // ARRAY_POP DISABLED
1280 // if ($trans_delta == 0)
1281 // while (array_pop($user_cur->comm) != NULL);
1283 $user_cur->trans_step = $user_cur->step + 1 + $trans_delta;
1284 $user_cur->comm[$user_cur->step % COMM_N] = "";
1285 $user_cur->step_inc();
1286 $user_cur->comm[$user_cur->step % COMM_N] = $this->show_room(($user_cur->step + 1), &$user_cur);
1287 $user_cur->step_inc();
1288 log_main("POST show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1292 log_main("JOIN_WAKEUP wup_idx ".$wup_idx." wup_n ".$user_wup_n);
1294 log_main("JOIN_WAKEUP more");
1295 // $ret .= table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table,
1296 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1297 $ret .= $table->act_content($user_cur);
1299 log_main("JOIN_WAKEUP end more");
1301 log_wr("ROOM_JOIN_WAKEUP: ".$ret);
1302 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1303 $user_cur->step_inc();
1307 function room_outstandup(&$user)
1309 $this->room_sitdown(&$user, -1);
1312 function table_update(&$user)
1314 log_main("table_update: pre - USER: ".$user->name);
1316 $table_idx = $user->table;
1318 if ($table_idx > -1)
1319 $table = &$this->table[$table_idx];
1321 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1323 $user_cur = &$this->user[$i];
1324 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1327 $ret = "gst.st = ".($user_cur->step+1)."; ";
1328 if ($table_idx > -1)
1329 $ret .= $this->table_content($user_cur, $table_idx);
1331 if ($user_cur == $user) {
1332 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
1333 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
1334 $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>: ";', $itin, xcape($user->name), $itou);
1336 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1337 $user_cur->step_inc();
1340 log_main("table_update: post");
1343 function room_sitdown(&$user, $table_idx)
1345 log_main("room_sitdown ".($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
1349 if ($table_idx > -1 && $table_idx < TABLES_N) {
1350 $table = &$this->table[$table_idx];
1353 if ($table->wag_own != NULL && $table->player_n == PLAYERS_N) {
1354 for ($i = 0 ; $i < TABLES_N ; $i++) {
1355 $user_cur =& $this->user[$table->player[$i]];
1356 if ($user_cur == $table->wag_own) {
1357 $train_app = sprintf("tra.rem(%d); ", $table_idx);
1358 $table->wag_reset(time());
1365 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1367 $user_cur = &$this->user[$i];
1368 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1371 $ret = "gst.st = ".($user_cur->step+1)."; ".$train_app;
1372 if ($table_idx > -1)
1373 $ret .= $this->table_content($user_cur, $table_idx);
1374 $ret .= $this->standup_content($user_cur);
1376 if ($user_cur == $user) {
1377 $ret .= 'subst = "sitdown"; tra.hide(); ';
1378 // clean the action buttons in other tables
1379 for ($e = 0 ; $e < TABLES_N ; $e++) {
1380 // $ret .= table_act_content(FALSE, 0, $e, $user_cur->table, FALSE);
1381 $ret .= $this->table[$e]->act_content($user_cur);
1384 else if ($table_idx > -1) {
1385 if ($table->player_n == PLAYERS_N) {
1386 // $ret .= table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table,
1387 /// ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1388 $ret .= $table->act_content($user_cur);
1391 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1392 $user_cur->step_inc();
1396 function chatt_send(&$user, $mesg)
1398 GLOBAL $G_alarm_passwd;
1402 $msg = substr($mesg, 6, 128);
1404 $dt = date("H:i ", $curtime);
1414 $update_room = FALSE;
1416 if (strcmp($msg, "/tav") == 0 ||
1417 strncmp($msg, "/tav ", 5) == 0) {
1419 if ($user->stat != 'room' || $user->subst != 'sitdown') {
1420 $msg = "<br>Per attivare il messaggio di segnalazione del tavolo occorre essere seduti.<br><br>";
1421 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1426 $table = &$this->table[$user->table];
1428 if ($table->wag_own != NULL) {
1429 $msg = "<br>Il messaggio di segnalazione del tavolo è già attivato.<br><br>";
1430 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1435 $dtime = $curtime - $table->wag_tout;
1436 if ($dtime < EXPIRE_TIME_WAG) {
1437 $msg = sprintf("<br>Il messaggio di segnalazione del tavolo<br>è disattivato ancora per %d second%s.<br><br>",
1438 EXPIRE_TIME_WAG - $dtime, (EXPIRE_TIME_WAG - $dtime == 1 ? "o" : "i"));
1439 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1444 $msg = substr($msg, 5);
1446 $table->wag_set($user, $msg);
1447 $to_user = sprintf('tra.add(%d, "%s");', $user->table, xcape(sprintf("%s: %s", $user->name, $msg)));
1448 $to_room = $to_user;
1451 } // /tav chat command
1453 else if (strncmp($msg, "/alarm ", 7) == 0) {
1454 if (strncmp($msg, "/alarm to ", 10) == 0) {
1455 $sp_pos = strpos($msg, " ", 10);
1456 $target = substr($msg, 10, $sp_pos - 10);
1457 $alarm_check = "/alarm to ".$target." ".$G_alarm_passwd." ";
1461 $alarm_check = "/alarm ".$G_alarm_passwd." ";
1464 if (strncmp($msg, $alarm_check, strlen($alarm_check)) != 0) {
1465 $msg = "<br>La password digitata non è corretta.<br><br>";
1466 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1471 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"Alarm <b>%s</b> inviato a <b>%s</b>.");',
1472 $dt, NICKSERV, xcape(substr($msg, strlen($alarm_check))),
1473 ($target == "" ? "tutti" : xcape($target)) );
1475 $msg = sprintf("<br><b>%s<br><br>%s</b><br><br>",
1476 $dt.NICKSERV, xcape(substr($msg, strlen($alarm_check))));
1477 $to_all = show_notify($msg, 0, "chiudi", 400, 120);
1479 } // /alarm chat command
1480 else if (strncmp($msg, "/garante", 8) == 0) {
1481 if ($user->flags & USER_FLAG_AUTH) {
1482 $to_user = sprintf('authbox(300,200);');
1485 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>");', $dt, NICKSERV);
1488 else if (strncmp($msg, "/nick ", 6) == 0) {
1489 log_main("chatt_send BEGIN");
1492 if (($name_new = validate_name(substr($msg, 6))) == FALSE) {
1493 $to_user = sprintf('chatt_sub("%s", [2,"%s"],"Il nickname deve contenere almeno una lettera dell\'alfabeto o una cifra.");', $dt, NICKSERV);
1497 $msg = "COMMAND ".$msg;
1498 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1499 $user_cur = &$this->user[$i];
1501 if ($user_cur->sess == '')
1503 if ($user_cur->name == $name_new)
1506 if ($i < MAX_PLAYERS) {
1507 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"Nickname <b>%s</b> già in uso.");', $dt, NICKSERV, xcape($name_new));
1511 if ($user->flags & USER_FLAG_AUTH) {
1512 if (strcasecmp($user->name,$name_new) != 0) {
1513 if ($user->subst == 'standup' || ($user->subst != 'standup' && $this->table[$user->table]->auth_only == FALSE)) {
1514 $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
1515 for ($i = 0 ; $i < TABLES_N ; $i++) {
1516 $to_user .= $this->table[$i]->act_content($user);
1520 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"<b>Non puoi cambiare nick a un tavolo per soli autenticati.</b>");', $dt, NICKSERV);
1525 $user->name = $name_new; // OK - nick changed
1527 /* se nome gia' in uso, segnala cosa potrebbe capitare */
1528 if (($user->flags & USER_FLAG_AUTH) == 0) {
1529 $userdb = new LoginDB();
1530 if ($userdb->login_exists($name_new)) {
1531 $to_user .= sprintf('chatt_sub("%s", [2, "%s"],"Il nickname <b>\'%s\'</b> è già registrato, <b>se il suo proprietario si autentificherà verrai rinominato d\'ufficio come ghost<i>N</i>.</b>");', $dt, NICKSERV, xcape($name_new));
1535 log_main("chatt_send start set");
1537 $update_room = TRUE;
1539 } // nick chat command
1541 else if (strncmp($msg, "/st ", 4) == 0) {
1542 log_main("chatt_send BEGIN");
1545 $st_str = substr($msg, 4);
1547 if (strcasecmp($st_str, "normale") == 0) {
1548 $st = USER_FLAG_S_NORM;
1550 else if (strcasecmp($st_str, "pausa") == 0) {
1551 $st = USER_FLAG_S_PAU;
1553 else if (strcasecmp($st_str, "fuori") == 0) {
1554 $st = USER_FLAG_S_OUT;
1556 else if (strcasecmp($st_str, "cane") == 0) {
1557 $st = USER_FLAG_S_DOG;
1559 else if (strcasecmp($st_str, "cibo") == 0) {
1560 $st = USER_FLAG_S_EAT;
1562 else if (strcasecmp($st_str, "lavoro") == 0) {
1563 $st = USER_FLAG_S_WRK;
1565 else if (strcasecmp($st_str, "sigaretta") == 0) {
1566 $st = USER_FLAG_S_SMK;
1569 $to_user = sprintf('chatt_sub("%s", [2,"%s"],"Questo stato non esiste.");', $dt, NICKSERV);
1573 log_main("chatt_send start set");
1574 if (($user->flags & USER_FLAG_S_ALL) != $st) {
1575 $update_room = TRUE;
1576 $user->flags = ($user->flags & ~USER_FLAG_S_ALL) | $st;
1579 } // nick chat command
1581 else { // normal chat line
1582 if ($curtime < ($user->chat_ban + $user->chat_dlt)) {
1584 $user->chat_dlt = $user->chat_dlt * 2;
1585 if ($user->chat_dlt > 120)
1586 $user->chat_dlt = 120;
1588 else if ($user->chat_lst == $msg)
1590 else if ($curtime - $user->chattime[($user->chat_cur + 1) % CHAT_N] < CHAT_ILL_TIME) {
1591 $user->chat_ban = $curtime;
1592 $user->chat_dlt = 5;
1596 $user->chat_ban = 0;
1597 $user->chat_dlt = 0;
1601 $to_user = sprintf('chatt_sub("%s", [%d, "%s"],"%s");', $dt, $user->flags, xcape($user->name), xcape("== chat ban =="));
1604 $to_user = sprintf('chatt_sub("%s", [%d, "%s"],"%s");', $dt, $user->flags, xcape($user->name), xcape($msg));
1605 // temporary silentiation for troll (will became array check)
1606 if (strcasecmp($user->name,'JackRokka') != 0 && $user->sess != '47ea653f602e8')
1607 $to_room = $to_user;
1610 log_legal($curtime, $user->sess, $user->name,
1611 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$msg);
1613 $user->chat_lst = "$msg";
1614 $user->chattime[$user->chat_cur % CHAT_N] = $curtime;
1624 // Output to clients
1627 if ($to_user != FALSE) {
1628 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1629 $user->comm[$user->step % COMM_N] .= $to_user;
1633 if ($to_room != FALSE) {
1634 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1635 $user_cur = &$this->user[$i];
1636 if ($target != "" && $user_cur->name != $target)
1638 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1639 if ($user_cur->sess == '' || $user_cur->stat == 'table' || $user_cur == $user)
1642 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1643 $user_cur->comm[$user_cur->step % COMM_N] .= $to_room;
1644 $user_cur->step_inc();
1649 // Before all align times with table timeout
1650 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1651 $table_cur =& $this->table[$table_idx];
1652 // if the table is complete and exists its shared mem we get the info about users lacc
1654 if ($table_cur->player_n == PLAYERS_N) {
1655 log_main("PLAYERS == N TABLE ".$table_idx);
1657 if (($sem = Briskin5::lock_data($table_idx)) != FALSE) {
1658 log_main("bin5 lock data success");
1660 $no_recovery = FALSE;
1661 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
1662 if ($table_cur->table_token != $bri->table_token) {
1663 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
1668 if ($bri != FALSE) {
1669 $bri_table = &$bri->table[0];
1670 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
1671 // stat must be "table" by definition
1672 $bri_user =& $bri->user[$i];
1674 if ($target != "" && $bri_user->name != $target)
1676 log_main("writa: ".$user_mesg);
1677 $bri_user->comm[$bri_user->step % COMM_N] = "gst.st = ".($bri_user->step+1)."; ";
1678 $bri_user->comm[$bri_user->step % COMM_N] .= $to_tabl;
1679 $bri_user->step_inc();
1681 Briskin5::save_data(&$bri);
1683 Briskin5::unlock_data($sem);
1685 } // if ($table_cur->player_n == PLAYERS_N) {
1686 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1687 } // if ($to_tabl == true ...
1690 if ($user->stat == 'room' && $user->subst == 'standup') {
1691 $this->standup_update(&$user);
1693 else if ($user->stat == 'room' && $user->subst == 'sitdown') {
1694 log_main("chatt_send pre table update");
1695 $this->table_update(&$user);
1696 log_main("chatt_send post table update");
1698 } // if ($update_room ...
1701 } // function chatt_send( ...
1703 function &get_user($sess, &$idx)
1705 GLOBAL $PHP_SELF, $G_false;
1707 if (validate_sess($sess)) {
1708 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1709 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1712 $ret = &$this->user[$i];
1716 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1717 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
1718 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1721 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1730 * function &add_user(&$room, &$sess, &$idx, $name, $pass, $ip)
1733 * if ($idx > -1 && ret == FALSE) => duplicated nick
1734 * if ($idx == -2 && ret == FALSE) => invalid name
1735 * if ($idx == -3 && ret == FALSE) => wrong password
1736 * if ($idx == -1 && ret == FALSE) => no space left
1737 * if ($idx == 0 && ret == user) => SUCCESS
1738 * if ($idx == -$idx && ret == user) => SUCCESS (but the login exists in the auth db
1743 function &add_user(&$sess, &$idx, $name, $pass, $ip)
1749 $authenticate = FALSE;
1750 $login_exists = FALSE;
1752 $ghost_auth = FALSE;
1756 if (($name_new = validate_name($name)) == FALSE) {
1761 log_auth("XXX", sprintf("ARRIVA: [%s] pass:[%s]", $sess, ($pass == FALSE ? "FALSE" : $pass)));
1762 if (validate_sess($sess) == FALSE)
1765 /* if pass != FALSE verify the login with pass */
1766 log_auth("XXX", "auth1");
1767 $userdb = new LoginDB();
1768 if ($pass != FALSE) {
1769 log_auth("XXX", "auth2");
1770 $authenticate = $userdb->login_verify($name_new, $pass);
1771 log_auth("XXX", "authenticate: ".($authenticate == TRUE ? "TRUE" : "FALSE"));
1773 if ($authenticate == FALSE) {
1779 $login_exists = $userdb->login_exists($name_new);
1781 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1783 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1787 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
1789 continue; // NOTE: CHECK IT !!
1791 if (strcasecmp($this->user[$i]->name, $name_new) == 0) {
1792 if ($authenticate == FALSE) {
1798 $ghost_auth = ($this->user[$i]->flags & USER_FLAG_AUTH);
1805 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1807 if ($ghost > -1 && $authenticate) {
1810 $ghost_user =& $this->user[$ghost];
1812 $ghost_user->step_inc();
1815 $ghost_user->sess = $sess;
1818 $ghost_user->sess = $sess;
1821 // If user at the table we need to update the table data too
1822 $table_idx = $ghost_user->table;
1823 if ($ghost_user->stat == "table" && $this->table[$table_idx]->player_n == PLAYERS_N) {
1824 if (($brisem = Briskin5::lock_data($table_idx)) != FALSE) {
1825 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
1826 if ($bri->the_end != TRUE) {
1827 $bri->user[$ghost_user->table_pos]->step_inc();
1828 $bri->user[$ghost_user->table_pos]->sess = $sess;
1829 Briskin5::save_data(&$bri);
1832 Briskin5::unlock_data($brisem);
1837 return ($this->user[$ghost]);
1839 else if ($idx != -1 && $i == MAX_PLAYERS) {
1844 $this->user[$idx]->sess = $sess;
1847 $this->user[$idx]->sess = $sess;
1849 $this->user[$idx]->name = $name_new; // OK - add new user
1850 $this->user[$idx]->stat_set("room");
1851 $this->user[$idx]->step_set(0);
1852 while (array_pop($this->user[$idx]->comm) != NULL);
1853 $this->user[$idx]->subst = "standup";
1854 $this->user[$idx]->lacc = $curtime;
1855 $this->user[$idx]->laccwr = $curtime;
1856 $this->user[$idx]->bantime = 0;
1857 $this->user[$idx]->ip = $ip;
1859 $this->user[$idx]->flags = ($authenticate ? USER_FLAG_AUTH : 0x00);
1862 log_main("ghost: rename!");
1863 $ghost_user =& $this->user[$ghost];
1865 if ($ghost_auth == FALSE) {
1866 for ($sfx = 1 ; $sfx <= MAX_PLAYERS ; $sfx++) {
1867 $ghostname = 'ghost'.$sfx;
1868 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1869 if (strcmp("", $this->user[$i]->sess) == 0)
1872 if (strcmp($this->user[$i]->name, $ghostname) == 0) {
1877 if ($ghostname != '')
1881 $ghost_user->name = $ghostname;
1883 if ($ghost_user->stat == 'room' && $ghost_user->subst == 'standup') {
1884 $this->standup_update(&$ghost_user);
1887 log_main("chatt_send pre table update");
1888 $this->table_update(&$ghost_user);
1889 log_main("chatt_send post table update");
1891 } // if ($ghost_auth == FALSE
1893 // FIXME: cacciare il vecchio utente room && table (if needed)
1894 $ghost_user->the_end = TRUE;
1895 $ghost_user->lacc = 0;
1896 $this->garbage_manager(TRUE);
1898 } // if ($ghost > -1) {
1900 log_main(sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1904 return ($this->user[$real_idx]);
1910 function standup_update(&$user)
1912 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1913 $user_cur = &$this->user[$i];
1914 if ($user_cur->sess == '')
1917 log_main("STANDUP START: ".$user_cur->stat);
1919 if ($user_cur->stat == 'room') {
1920 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".$this->standup_content($user_cur);
1921 if ($user_cur == $user) {
1922 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
1923 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
1925 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>: ";',
1926 $itin, xcape($user->name), $itou);
1928 log_main("FROM STANDUP: NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
1930 $user_cur->step_inc();
1936 function &init_data()
1938 $room =& new Room();
1944 function &load_data()
1946 GLOBAL $G_false, $sess;
1949 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1950 log_main("ftok failed");
1955 if (($shm_sz = sharedmem_sz($tok)) == -1) {
1956 log_main("shmop_open failed");
1960 $shm_sz = SHM_DIMS_MIN;
1962 if ($shm = shm_attach($tok, $shm_sz)) {
1963 $room = @shm_get_var($shm, $tok);
1965 log_only("bri == ".($room == FALSE ? "FALSE" : "TRUE")." bri === ".($room === FALSE ? "FALSE" : "TRUE")." bri isset ".(isset($room) ? "TRUE" : "FALSE"));
1967 log_only("bri count ".count($room));
1969 if ($room == FALSE) {
1970 log_only("INIT MAIN DATA");
1972 $room =& Room::init_data();
1973 if (@shm_put_var($shm, $tok, $room) == FALSE) {
1974 log_only("PUT_VAR FALLITA ".strlen(serialize($room)));
1975 log_only(serialize($room));
1978 $room->shm_sz = $shm_sz;
1993 function save_data(&$room)
2001 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1)
2004 while ($room->shm_sz < SHM_DIMS_MAX) {
2005 if (($shm = shm_attach($tok, $room->shm_sz)) == FALSE)
2008 // log_only("PUT_VAR DI ".strlen(serialize($room)));
2009 if (shm_put_var($shm, $tok, $room) != FALSE) {
2013 if (shm_remove($shm) === FALSE) {
2014 log_only("REMOVE FALLITA");
2018 $room->shm_sz += SHM_DIMS_DLT;
2027 function lock_data()
2031 // echo "LOCK: ".FTOK_PATH."/main";
2033 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
2037 // echo "FTOK ".$tok."<br>";
2038 if (($res = sem_get($tok)) == FALSE) {
2039 echo "SEM_GET FAILED";
2042 if (sem_acquire($res)) {
2043 log_lock("LOCK room");
2050 function unlock_data($res)
2054 log_lock("UNLOCK room");
2056 return (sem_release($res));
2060 function standup_content($user)
2065 if ($user->stat != 'room')
2068 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
2069 if ($this->user[$e]->sess == "" || $this->user[$e]->stat != "room" || $this->user[$e]->name == "")
2074 // $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
2076 $content = ' j_stand_cont( [ ';
2078 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
2079 if ($this->user[$e]->sess == "" || $this->user[$e]->stat != "room" || $this->user[$e]->name == "")
2082 $flags = $this->user[$e]->flags;
2084 if ($this->user[$e]->subst == "standup") {
2085 if ($this->user[$e] == $user)
2090 $content .= sprintf('%s[ %d, "%s" ]',($ct > 0 ? ', ' : ''), $flags, xcape($this->user[$e]->name));
2099 function table_content_old($user, $table_idx)
2105 // Si possono usare i dati nella classe table
2108 $sess = $user->sess;
2109 $table = &$this->table[$table_idx];
2111 if ($user->stat != 'room')
2114 for ($i = 0 ; $i < $table->player_n ; $i++) {
2115 $user_cur = &$this->user[$table->player[$i]];
2117 if ($user_cur == $user)
2118 { $hilion = "<b>"; $hilioff = "</b>"; }
2120 { $hilion = ""; $hilioff = ""; }
2122 log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
2124 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
2127 for ( ; $i < PLAYERS_N ; $i++)
2131 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
2137 function table_content($user, $table_idx)
2143 // Si possono usare i dati nella classe table
2146 $sess = $user->sess;
2147 $table = &$this->table[$table_idx];
2149 if ($user->stat != 'room')
2153 for ($i = 0 ; $i < $table->player_n ; $i++) {
2154 $user_cur = &$this->user[$table->player[$i]];
2156 $flags = $user_cur->flags;
2158 if ($user_cur == $user)
2161 log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
2163 $content .= sprintf('%s[ %d, "%s" ]',($i == 0 ? '' : ', '), $flags, xcape($user_cur->name));
2168 $ret .= sprintf('j_tab_cont(%d, %s);', $table_idx, $content);
2175 function make_seed()
2177 list($usec, $sec) = explode(' ', microtime());
2178 return (float) $sec + ((float) $usec * 100000);
2182 function log_only2($log) {
2185 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONL2) == 0)
2188 if (isset($sess) == FALSE)
2193 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONL2) == 0)
2196 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2197 fwrite($fp, sprintf("ONL2: [%s] [%s]\n", $ssess, $log));
2202 function log_crit($log) {
2205 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_CRIT) == 0)
2208 if (isset($sess) == FALSE)
2213 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_CRIT) == 0)
2216 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2217 fwrite($fp, sprintf("CRIT: [%s] [%s]\n", $ssess, $log));
2222 function log_only($log) {
2225 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONLY) == 0)
2228 if (isset($sess) == FALSE)
2233 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONLY) == 0)
2236 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2237 fwrite($fp, sprintf("ONLY: [%s] [%s]\n", $ssess, $log));
2242 function log_main($log) {
2245 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_MAIN) == 0)
2248 if (isset($sess) == FALSE)
2253 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_MAIN) == 0)
2256 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2257 fwrite($fp, sprintf("MAIN: [%s] [%s]\n", $ssess, $log));
2262 function log_rd($log) {
2265 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_READ) == 0)
2268 if (isset($sess) == FALSE)
2273 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_READ) == 0)
2276 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2277 fwrite($fp, sprintf("READ: [%s] [%s]\n", $ssess, $log));
2282 function log_rd2($log) {
2285 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_REA2) == 0)
2288 if (isset($sess) == FALSE)
2293 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_REA2) == 0)
2296 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2297 fwrite($fp, sprintf("REA2: [%s] [%s]\n", $ssess, $log));
2302 function log_send($log) {
2305 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SEND) == 0)
2308 if (isset($sess) == FALSE)
2313 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SEND) == 0)
2316 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2317 fwrite($fp, sprintf("SEND: [%s] [%s]\n", $ssess, $log));
2322 function log_lock($log) {
2325 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOCK) == 0)
2328 if (isset($sess) == FALSE)
2333 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOCK) == 0)
2336 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2337 fwrite($fp, sprintf("LOCK: [%s] [%s]\n", $ssess, $log));
2342 function log_wr($log) {
2345 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_WRIT) == 0)
2348 if (isset($sess) == FALSE)
2353 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_WRIT) == 0)
2356 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2357 fwrite($fp, sprintf("WRIT: [%s] [%s]\n", $ssess, $log));
2362 function log_load($log) {
2365 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOAD) == 0)
2368 if (isset($sess) == FALSE)
2373 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOAD) == 0)
2376 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2377 fwrite($fp, sprintf("LOAD: [%s] [%s]\n", $ssess, $log));
2382 function log_auth($sess, $log) {
2383 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_AUTH) == 0)
2386 if (( (BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_AUTH) == 0)
2389 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2390 fwrite($fp, sprintf("AUTH: [%s] [%d] [%s]\n", $sess, time(), $log));
2396 function log_legal($curtime, $sess, $name, $where, $mesg)
2400 if (($fp = @fopen(LEGAL_PATH."/legal.log", 'a')) != FALSE) {
2401 /* Unix time | session | nickname | IP | where was | mesg */
2402 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|\n", $curtime, $sess, $name, $_SERVER['REMOTE_ADDR'], $where , $mesg));
2410 function lock_banlist()
2412 if (($tok = @ftok(FTOK_PATH."/main", "L")) == -1) {
2416 if (($res = sem_get($tok)) == FALSE) {
2417 echo "SEM_GET FAILED";
2420 if (sem_acquire($res))
2426 function unlock_banlist($res)
2428 return (sem_release($res));
2431 function table_act_content_old($isstanding, $sitted, $table, $cur_table)
2436 if ($sitted < PLAYERS_N) {
2437 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
2441 if ($table == $cur_table)
2442 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
2449 function table_act_content($isstanding, $sitted, $table, $cur_table, $allowed)
2454 if ($sitted < PLAYERS_N) {
2462 if ($table == $cur_table)
2469 $ret = sprintf('j_tab_act_cont(%d, \'%s\');', $table, $act);
2474 function show_notify($text, $tout, $butt, $w, $h)
2476 log_main("SHOW_NOTIFY: ".$text);
2477 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
2480 function briscola_show($room, $table, $user)
2485 if ($table->asta_card == 9)
2486 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
2488 /* text of caller cell */
2489 if ($user->table_pos == $table->asta_win)
2490 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
2492 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
2493 xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
2495 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
2497 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
2498 $ret .= sprintf('$("caller").style.visibility = "visible";');
2499 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
2500 $ret .= sprintf('$("astalascio").style.visibility = "";');
2501 $ret .= sprintf('$("asta").style.visibility = "hidden";');
2502 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
2508 function game_result($asta_pnt, $pnt)
2510 if ($asta_pnt == 61) {
2513 else if ($pnt == 60)
2519 if ($pnt >= $asta_pnt)
2526 function multoval($mult)
2530 else if ($mult == 4)
2531 return ("quadruplo");
2533 return (sprintf("%d-plo", $mult));
2536 function show_table_info(&$room, &$table, $table_pos)
2539 $user = &$room->user[$table->player[$table_pos]];
2541 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
2542 $noty = sprintf('<table class=\"points\"><tr><th></th>');
2545 for ($i = 0 ; $i < PLAYERS_N ; $i++)
2546 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
2547 $noty .= sprintf("</tr>");
2550 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
2552 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
2553 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
2554 for ($e = 0 ; $e < PLAYERS_N ; $e++)
2555 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
2560 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
2561 for ($e = 0 ; $e < PLAYERS_N ; $e++)
2562 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
2563 $noty .= "</tr></table>";
2565 if ($table->old_reason != "") {
2566 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
2569 if ($table->old_win != -1) {
2570 $win = $table->player[$table->old_win];
2571 $fri = $table->player[$table->old_friend];
2573 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
2576 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
2577 xcape($room->user[$win]->name),
2578 xcape($room->user[$fri]->name));
2579 if ($table->old_pnt == 120) {
2580 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
2583 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
2584 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
2585 'più di 60'), $table->old_pnt,
2586 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
2590 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
2591 xcape($room->user[$win]->name));
2592 if ($table->old_pnt == 120) {
2593 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
2596 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
2597 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
2598 'più di 60'), $table->old_pnt,
2599 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
2603 if ($table->mazzo == $table_pos)
2604 $noty .= "Fai <b>tu</b> il mazzo,";
2606 $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
2607 $noty .= "Il mazzo a <b>$unam</b>,";
2610 if ($user->subst == 'asta') {
2611 if ($table->asta_win == -1) // auction case
2612 $curplayer = $table->gstart % PLAYERS_N;
2614 $curplayer = $table->asta_win;
2616 else if ($user->subst == 'game') {
2617 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
2621 if ($curplayer == $table_pos) {
2622 $noty .= " tocca a <b>te</b> giocare.";
2625 $unam = xcape($room->user[$table->player[$curplayer]]->name);
2626 $noty .= " tocca a <b>$unam</b> giocare.";
2629 if ($table->mult > 1) {
2630 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
2632 $noty .= "<hr><br>";
2633 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
2634 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
2635 * 130 + ($table->points_n > 0 ? 50 : 0) +
2636 * (120 * ($table->points_n / MAX_POINTS)));
2637 * will be used when we refact notify js function following
2638 * photoo class logic
2644 function root_wellcome($user)
2646 GLOBAL $root_wellarr;
2650 $dt = date("H:i ", $curtime);
2652 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
2653 $ret .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, str_replace('"', '\"', $root_wellarr[$i]));
2658 function table_wellcome($user)
2660 GLOBAL $table_wellarr;
2663 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
2664 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$i]));
2672 is_transition (is from room to table ?)
2673 is_again (is another game)
2675 Examples of $is_transition, $is_again:
2676 from reload of the page: FALSE, FALSE
2677 from sitdown in room: TRUE, FALSE
2678 from table: asta cmd e tutti passano: TRUE, TRUE
2679 from table: fine partita: TRUE, TRUE
2681 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
2683 $table_idx = $user->table;
2684 $table = &$room->table[$table_idx];
2685 $table_pos = $user->table_pos;
2687 $ret = "table_init();";
2688 $ret .= $table->exitlock_show(&$room->user, $table_pos);
2690 /* GENERAL STATUS */
2691 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
2692 $sendstep, $user->stat, $user->subst, $table_pos);
2694 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
2695 $sendstep, $user->stat, $user->subst, $table_pos));
2698 $ret .= "background_set();";
2701 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
2702 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
2704 $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', $itin, xcape($user->name), $itou);
2705 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
2706 $room->user[$table->player[($table_pos)%PLAYERS_N]]->flags,
2707 xcape($room->user[$table->player[($table_pos)%PLAYERS_N]]->name),
2709 $room->user[$table->player[($table_pos+1)%PLAYERS_N]]->flags,
2710 xcape($room->user[$table->player[($table_pos+1)%PLAYERS_N]]->name),
2712 $room->user[$table->player[($table_pos+2)%PLAYERS_N]]->flags,
2713 xcape($room->user[$table->player[($table_pos+2)%PLAYERS_N]]->name),
2715 (PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+3)%PLAYERS_N]]->flags),
2716 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+3)%PLAYERS_N]]->name)),
2718 (PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+4)%PLAYERS_N]]->flags),
2719 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+4)%PLAYERS_N]]->name)));
2721 /* NOTIFY FOR THE CARD MAKER */
2722 if ($is_transition) { // && $user->subst == "asta" superfluo
2723 $ret .= show_table_info(&$room, &$table, $table_pos);
2726 $ret .= table_wellcome($user);
2728 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
2729 $ret .= playsound("cow.mp3");
2734 if ($is_transition) { // && $user->subst == "asta" superfluo
2737 for ($i = 0 ; $i < 8 ; $i++) {
2738 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
2740 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
2741 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
2747 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
2749 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
2750 $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
2751 $table->card[$o]->value : -1),
2752 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
2757 $taked = array(0,0,0,0,0);
2758 $inhand = array(0,0,0,0,0);
2759 $ontabl = array(-1,-1,-1,-1,-1);
2762 for ($i = 0 ; $i < 40 ; $i++) {
2763 if ($table->card[$i]->stat == 'hand') {
2764 if ($table->card[$i]->owner == $table_pos) {
2765 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
2767 $inhand[$table->card[$i]->owner]++;
2769 else if ($table->card[$i]->stat == 'take') {
2770 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
2771 $taked[$table->card[$i]->owner]++;
2773 else if ($table->card[$i]->stat == 'table') {
2774 $ontabl[$table->card[$i]->owner] = $i;
2778 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2779 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
2781 log_main("Stat table: ".$logg);
2783 /* Set ours cards. */
2785 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
2786 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
2787 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
2788 $oursarg .= ($i == 0 ? "" : ", ")."-1";
2789 $ret .= sprintf('card_setours(%s);', $oursarg);
2791 /* Dispose all cards */
2792 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2793 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
2794 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
2795 $inhand[$i], $taked[$i]);
2797 if ($ontabl[$i] != -1) {
2798 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
2799 $table->card[$ontabl[$i]]->value,
2800 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
2806 if ($user->subst == 'asta') {
2808 /* show users auction status */
2810 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2811 $user_cur = &$room->user[$table->player[$i]];
2812 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
2813 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
2816 $showst .= ",-2,-2";
2817 $ret .= sprintf('show_astat(%s);', $showst);
2819 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
2820 /* show card chooser */
2821 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
2826 if ($table_pos == ($table->gstart % PLAYERS_N) &&
2827 $table->asta_win == -1)
2828 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2829 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
2831 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2832 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
2836 if ($table->asta_win == -1) { // auction case
2837 if ($table_pos == ($table->gstart % PLAYERS_N))
2838 $ret .= "remark_on();";
2840 $ret .= "remark_off();";
2842 else { // chooseed case
2843 if ($table_pos == $table->asta_win)
2844 $ret .= "remark_on();";
2846 $ret .= "remark_off();";
2849 else if ($user->subst == 'game') {
2851 if (($table->gstart + $table->turn) % PLAYERS_N == $table_pos)
2852 $ret .= "is_my_time = true; remark_on();";
2854 $ret .= "remark_off();";
2856 /* WHO CALL AND WATH */
2857 $ret .= briscola_show($room, $table, $user);
2861 } // end function show_table(...
2863 function calculate_winner(&$table)
2870 $cur_seed = $table->briscola - ($table->briscola % 10);
2872 for ($i = 0 ; $i < 40 ; $i++) {
2873 if ($table->card[$i]->stat != "table")
2876 log_wr(sprintf("Card On table: [%d]", $i));
2878 $v = $table->card[$i]->value;
2879 $ontab[$table->card[$i]->owner] = $v;
2880 $ontid[$table->card[$i]->owner] = $i;
2881 /* se briscola setto il flag */
2882 if (($v - ($v % 10)) == $cur_seed)
2886 if ($briontab == FALSE) {
2887 $cur_win = $table->gstart;
2888 $cur_val = $ontab[$cur_win];
2889 $cur_seed = $cur_val - ($cur_val % 10);
2892 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2893 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
2894 if ($ontab[$i] < $cur_val) {
2895 $cur_val = $ontab[$i];
2901 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2902 $table->card[$ontid[$i]]->owner = $cur_win;
2903 $table->card[$ontid[$i]]->stat = "take"; // Card stat
2908 function calculate_points(&$table)
2910 GLOBAL $G_all_points;
2914 if ($table->asta_pnt == 60)
2915 $table->asta_pnt = 61;
2917 $table->old_reason = "";
2918 $table->old_win = $table->asta_win;
2919 $table->old_friend = $table->friend;
2920 $table->old_asta_pnt = $table->asta_pnt;
2922 for ($i = 0 ; $i < 40 ; $i++) {
2923 $ctt = $table->card[$i]->value % 10;
2924 $own = $table->card[$i]->owner;
2925 if ($own == $table->asta_win || $own == $table->friend)
2926 $pro += $G_all_points[$ctt];
2929 log_wr(sprintf("PRO: [%d]", $pro));
2932 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
2933 $table->points[$table->points_n % MAX_POINTS] = array();
2934 for ($i = 0 ; $i < PLAYERS_N ; $i++)
2935 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
2937 $table->old_pnt = $pro;
2943 if ($pro >= $table->asta_pnt)
2948 $table->points[$table->points_n % MAX_POINTS] = array();
2949 for ($i = 0 ; $i < 5 ; $i++) {
2950 if ($i == $table->asta_win)
2951 $pt = ($i == $table->friend ? 4 : 2);
2952 else if ($i == $table->friend)
2957 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
2959 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
2961 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
2963 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
2964 $table->total[$i] += $pt;
2967 $table->old_pnt = $pro;
2971 function validate_sess($sess)
2973 if (strlen($sess) == SESS_LEN)
2979 function validate_name($name)
2981 $name_new = str_replace(' ', '_', substr(trim($name),0,12));
2983 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
2985 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
2992 function playsound($filename)
2994 return (sprintf('playsound("flasou", "%s");', $filename));
2997 function secstoword($secs)
2999 $mins = floor($secs / 60);
3002 $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
3005 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));
3010 function sharedmem_sz($tok)
3012 if (($shm_id = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
3013 log_main("shmop_open failed");
3016 $shm_sz = shmop_size($shm_id);
3017 shmop_close($shm_id);
3019 log_main("shm_sz: ".$shm_sz." SHM_DIMS: ".SHM_DIMS);
3024 function lock_data()
3028 if (($tok = @ftok(FTOK_PATH."/warrant", "B")) == -1) {
3032 // echo "FTOK ".$tok."<br>";
3033 if (($res = sem_get($tok)) == FALSE) {
3034 echo "SEM_GET FAILED";
3037 if (sem_acquire($res)) {
3038 log_lock("LOCK room");
3045 function unlock_data($res)
3049 log_lock("UNLOCK room");
3051 return (sem_release($res));