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 // NOTE: BRISK DEBUG must be a numerical constant, not the result of operations on symbols
68 define(BRISK_DEBUG, 0xffffffff);
70 define(BRISK_SINGLE_DEBUG,0);
71 define(BRISK_SINGLE_SESS, "");
72 // define(DEBUGGING, "local");
74 require_once("$DOCUMENT_ROOT/Etc/".BRISK_CONF);
78 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
79 $G_brisk_version = "2.1.4 - trusty";
81 /* MLANG: ALL THE INFO STRINGS IN brisk.phh */
82 $root_wellarr = Array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: Aggiunto un watchdog per la rete e il tasto "reload" in room.',
83 'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
84 $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.');
88 <div style=\\"text-align: left; padding: 8px;\\">
89 <b>Descrizione</b><br>
90 Questa รจ un\'implementazione della briscola in cinque, così come è spiegata su
91 <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>
92 <b>Configurazione del browser.</b><br>
93 Occorre abilitare i cookies.<br>
95 <b>Uso del sito</b><br>
96 Potete sedervi a un tavolo o rimanere in piedi.<br>
97 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
100 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>
101 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
102 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
103 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
104 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
105 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.
107 <dt><b>Comandi della chat</b>
108 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
109 <dd><b>/tav <i><frase di invito></i></b> - invito per gli altri giocatori al tavolo dove si è seduti
110 <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\\", \\"presente\\" oppure \\"sigaretta\\"
111 <dd><b>/garante</b> - se si è autenticati permette di garantire per un utente fidato
119 <div id=\\"header\\" class=\\"header\\">
120 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
121 briscola chiamata in salsa ajax
123 <br><b>version '.$G_brisk_version.'</b><br><br>
124 Copyright 2006-2008 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
129 $from = array ( '\\', '@', '|' );
130 $to = array ( '\\\\', '@', '¦' );
132 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
137 $from = array ( '\\', '|' );
138 $to = array ( '\\\\', '\\|' );
140 return (str_replace($from, $to, $s));
144 var $value; /* 0 - 39 card value */
145 var $stat; /* 'bunch', 'hand', 'table', 'take' */
146 var $owner; /* (table position 0-4) */
147 // var $pos; /* Pos in hand. */
148 var $x; /* When played the X position on the table of the owner. */
149 var $y; /* When played the Y position on the table of the owner. */
151 function Card($value, $stat, $owner)
153 $this->value = $value;
154 $this->stat = $stat; // Card stat
155 $this->owner = $owner;
158 function assign($stat,$owner)
160 $this->stat = $stat; // Card stat
161 $this->owner = $owner;
164 function setpos($pos)
171 $this->stat = 'table'; // Card stat
176 function take($newown)
178 $this->stat = 'take'; // Card stat
179 $this->owner = $newown;
203 var $points; // points array
204 var $points_n; // number of row of points
226 function &create($idx)
230 if (($thiz =& new Table()) == FALSE)
234 $thiz->player = array();
237 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
238 $thiz->asta_pla_n= -1;
239 $thiz->asta_card = -1;
240 $thiz->asta_pnt = -1;
243 $thiz->auth_only = FALSE;
245 $thiz->points = array( );
247 $thiz->total = array( 0, 0, 0, 0, 0);
248 $thiz->asta_win = -1;
249 $thiz->briscola = -1;
253 $thiz->wag_own = NULL;
257 $thiz->old_reason = "";
258 $thiz->old_asta_pnt = -1;
261 $thiz->old_friend = -1;
263 $thiz->table_token = "";
264 $thiz->table_start = 0;
266 $thiz->wakeup_time = 0;
271 function &clone(&$from)
275 if (($thiz =& new Table()) == FALSE)
278 $thiz->idx = $from->idx;
279 $thiz->player = array();
280 for ($i = 0 ; $i < $from->player_n ; $i++)
281 $thiz->player[$i] = $from->player[$i];
282 $thiz->player_n = $from->player_n;
283 $thiz->card = $from->card;
284 $thiz->mazzo = $from->mazzo; // REVIEW
285 $thiz->gstart = $from->gstart;
286 $thiz->turn = $from->turn;
288 $thiz->auth_only = $from->auth_only;
290 $thiz->wag_own = $from->wag_own;
291 $thiz->wag_com = $from->wag_com;
292 $thiz->wag_tout = $from->wag_taut;
294 $thiz->asta_pla = $from->asta_pla;
295 $thiz->asta_pla_n = $from->asta_pla_n;
296 $thiz->asta_card = $from->asta_card;
297 $thiz->asta_pnt = $from->asta_pnt;
299 $thiz->mult = $from->mult;
300 $thiz->points = $from->points;
301 $thiz->points_n = $from->points_n;
302 $thiz->total = $from->total;
304 $thiz->asta_win = $from->asta_win;
305 $thiz->briscola = $from->briscola;
306 $thiz->friend = $from->friend;
308 $thiz->old_reason = $from->old_reason;
309 $thiz->old_asta_pnt = $from->old_asta_pnt;
310 $thiz->old_pnt = $from->old_pnt;
311 $thiz->old_win = $from->old_win;
312 $thiz->old_friend = $from->old_friend;
314 $thiz->table_token = $from->table_token;
315 $thiz->table_start = $from->table_start;
317 $thiz->wakeup_time = $from->wakeup_time;
322 function &spawn(&$from)
326 if (($thiz =& new Table()) == FALSE)
329 $thiz->idx = $from->idx;
330 $thiz->player_n = $from->player_n;
331 $thiz->card = &$thiz->bunch_create();
332 $thiz->mazzo = $from->mazzo;
333 $thiz->gstart = $from->gstart;
334 $thiz->turn = $from->turn;
336 $thiz->auth_only = $from->auth_only;
338 $thiz->wag_own = $from->wag_own;
339 $thiz->wag_com = $from->wag_com;
340 $thiz->wag_tout = $from->wag_taut;
342 $thiz->asta_pla = $from->asta_pla;
343 $thiz->asta_pla_n = $from->asta_pla_n;
344 $thiz->asta_card = $from->asta_card;
345 $thiz->asta_pnt = $from->asta_pnt;
347 $thiz->mult = $from->mult;
348 $thiz->points = $from->points;
349 $thiz->points_n = $from->points_n;
350 $thiz->total = $from->total;
352 $thiz->asta_win = $from->asta_win;
353 $thiz->briscola = $from->briscola;
354 $thiz->friend = $from->friend;
356 $thiz->old_reason = $from->old_reason;
357 $thiz->old_asta_pnt = $from->old_asta_pnt;
358 $thiz->old_pnt = $from->old_pnt;
359 $thiz->old_win = $from->old_win;
360 $thiz->old_friend = $from->old_friend;
362 // players are rearranged in an dedicated array
363 $thiz->player = array();
364 for ($i = 0 ; $i < $from->player_n ; $i++)
365 $thiz->player[$i] = $i;
367 $thiz->table_token = $from->table_token;
368 $thiz->table_start = $from->table_start;
370 $thiz->wakeup_time = $from->wakeup_time;
375 function &bunch_create()
379 for ($i = 0 ; $i < 40 ; $i++) {
380 $ret[$i] =& new Card($i, 'bunch', 'no_owner');
387 function wag_set(&$user, $mesg)
391 $this->wag_own = &$user;
392 $this->wag_com = $mesg;
396 function wag_reset($timeout)
398 log_main("WAG_RESET");
400 unset($this->wag_own);
401 $this->wag_own = NULL;
403 $this->wag_tout = $timeout;
406 function bunch_make()
408 $ct = array(0,0,0,0,0);
410 mt_srand(make_seed());
412 for ($i = 39 ; $i >= 0 ; $i--)
415 for ($i = 39 ; $i >= 0 ; $i--) {
419 log_main("RND ZERO");
424 $this->card[$id]->assign('hand', $owner);
426 $rest[$rn] = $rest[$i];
427 // $pubbpos[$rn2] = $pubbpos[$i];
431 function init(&$userarr)
433 $this->mazzo = rand(0,PLAYERS_N-1);
437 $this->old_reason = "";
438 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
439 $this->total[$i] = 0;
440 $user_cur = &$userarr[$this->player[$i]];
441 $user_cur->exitislock = TRUE;
444 log_main("table::init: ci siamo");
447 function game_init(&$userarr)
451 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
455 $this->asta_pla_n = PLAYERS_N;
456 $this->asta_card = -1;
457 $this->asta_pnt = 60;
458 $this->asta_win = -1;
459 $this->briscola = -1;
463 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
464 $this->asta_pla[$i] = TRUE;
465 $user_cur = &$userarr[$this->player[$i]];
466 $user_cur->subst = 'asta';
467 $user_cur->asta_card = -2;
468 $user_cur->asta_pnt = -1;
469 $user_cur->handpt = $this->hand_points($i);
470 // SEE function calculate_points(&$table)
476 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
479 function getPlayer($idx)
481 return ($this->player[$idx]);
484 function setPlayer($idx, $player)
486 $this->player[$idx] = $player;
489 function user_add($idx)
491 $this->player[$this->player_n] = $idx;
494 return ($this->player_n - 1);
497 function user_rem(&$room, &$user)
499 $tabpos = $user->table_pos;
501 /* verifico la consistenza dei dati */
502 if ($room->user[$this->player[$tabpos]] == $user) {
504 /* aggiorna l'array dei giocatori al tavolo. */
505 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
506 $this->player[$i] = $this->player[$i+1];
507 $user_cur = &$room->user[$this->player[$i]];
508 $user_cur->table_pos = $i;
513 log_main("INCONSISTENCY ON TABLE.");
517 function hand_points($idx)
519 GLOBAL $G_all_points;
523 for ($i = 0 ; $i < 40 ; $i++) {
524 if ($this->card[$i]->owner != $idx)
527 $ctt = $this->card[$i]->value % 10;
528 $tot += $G_all_points[$ctt];
534 function exitlock_show(&$userarr, $table_pos)
536 $ct = $this->exitlock_calc(&$userarr, $table_pos);
538 $ret = sprintf('exitlock_show(%d, %s);', $ct,
539 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
543 function exitlock_calc(&$userarr, $table_pos)
547 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
548 if ($userarr[$this->player[$i]]->exitislock == FALSE)
558 // $ret .= table_act_content(($user->subst == 'standup'), $this->table[$i]->player_n, $i, $user->table,
559 // ($this->table[$i]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
561 // function act_content($isstanding, $sitted, $table, $cur_table, $allowed)
562 function act_content(&$user)
565 $isstanding = ($user->subst == 'standup');
566 $sitted = $this->player_n;
568 $cur_table = $user->table;
572 if ($sitted < PLAYERS_N) {
573 if ($this->auth_only) {
574 if ($user->flags & USER_FLAG_AUTH)
588 if ($table == $cur_table)
595 $ret = sprintf('j_tab_act_cont(%d, \'%s\');', $table, $act);
605 define(USER_FLAG_AUTH, 0x02);
608 define(USER_FLAG_S_NORM, 0x000); // done
609 define(USER_FLAG_S_PAU, 0x100); // done
610 define(USER_FLAG_S_OUT, 0x200); // done
611 define(USER_FLAG_S_DOG, 0x300); // done
612 define(USER_FLAG_S_EAT, 0x400); // done
613 define(USER_FLAG_S_WRK, 0x500); // done
614 define(USER_FLAG_S_SMK, 0x600); // done
615 define(USER_FLAG_S_EYE, 0x700); // done
617 define(USER_FLAG_S_ALL, 0xf00); // done
620 var $name; // name of the user
621 var $sess; // session of the user
622 var $ip; // ip of the user
623 var $lacc; // last access (for the cleanup)
624 var $laccwr; // last access (for the cleanup)
625 var $bantime; // timeout to temporary ban
626 var $stat; // status (outdoor, room, table, game, ...)
627 var $subst; // substatus for each status
628 var $step; // step of the current status
629 var $trans_step; // step to enable transition between pages (disable == -1)
630 var $comm; // commands array
633 var $handpt; // Total card points at the beginning of the current hand.
634 var $exitislock; // Player can exit from the table ?
635 var $table; // id of the current table (if in table state)
636 var $table_pos; // idx on the table
637 var $table_token;// token that identify a game on a table
638 var $flags; // Bitfield with: AUTHENTICATE: 0x02
639 var $the_end; // Flag to change the end of the session
641 var $chat_lst; // Last chat line
642 var $chattime; // Array of chat times
643 var $chat_cur; // Current chat line number
644 var $chat_ban; // Time for ban chat
645 var $chat_dlt; // Delta t for ban
649 function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
652 if (($thiz =& new User()) == FALSE)
658 $thiz->lacc = time();
659 $thiz->laccwr = time();
662 $thiz->subst = $subst;
664 $thiz->trans_step = -1;
665 $thiz->comm = array();
666 $thiz->asta_card = -2;
667 $thiz->asta_pnt = -1;
669 $thiz->exitislock = TRUE;
673 $thiz->chattime = array_fill(0, CHAT_N, 0);
675 $thiz->chat_lst = "";
679 $thiz->table = $table;
680 $thiz->table_pos = -1;
681 $thiz->table_token = "";
686 function &clone(&$from)
690 if (($thiz =& new User()) == FALSE)
693 $thiz->name = $from->name;
694 $thiz->sess = $from->sess;
695 $thiz->ip = $from->ip;
696 $thiz->lacc = $from->lacc;
697 $thiz->laccwr = $from->laccwr;
698 $thiz->bantime = $from->bantime;
699 $thiz->stat = $from->stat;
700 $thiz->subst = $from->subst;
701 $thiz->step = $from->step;
702 $thiz->trans_step = $from->trans_step;
703 $thiz->comm = array();
705 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
706 for ($i = $i_start ; $i < $from->step ; $i++) {
708 $thiz->comm[$ii] = $from->comm[$ii];
710 $thiz->asta_card = $from->asta_card;
711 $thiz->asta_pnt = $from->asta_pnt;
712 $thiz->handpt = $from->handpt;
713 $thiz->exitislock = $from->exitislock;
715 $thiz->flags = $from->flags;
717 $thiz->chattime = array();
718 for ($i = 0 ; $i < CHAT_N ; $i++)
719 $thiz->chattime[$i] = $from->chattime[$i];
720 $thiz->chat_cur = $from->chat_cur;
721 $thiz->chat_lst = $from->chat_lst;
722 $thiz->chat_ban = $from->chat_ban;
723 $thiz->chat_dlt = $from->chat_dlt;
725 $thiz->table = $from->table;
726 $thiz->table_pos = $from->table_pos;
727 $thiz->table_token = $from->table_token;
728 $thiz->the_end = $from->the_end;
733 function &spawn(&$from, $table, $table_pos)
737 if (($thiz =& new User()) == FALSE)
740 $thiz->name = $from->name;
741 $thiz->sess = $from->sess;
742 $thiz->ip = $from->ip;
743 $thiz->lacc = $from->lacc;
744 $thiz->laccwr = $from->laccwr;
745 $thiz->bantime = $from->bantime;
746 $thiz->stat = $from->stat;
747 $thiz->subst = $from->subst;
748 $thiz->step = $from->step;
749 $thiz->trans_step = $from->trans_step;
750 $thiz->comm = array();
753 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
754 for ($i = $i_start ; $i < $from->step ; $i++) {
755 log_wr("TRY PUSH:".$i);
757 $thiz->comm[$ii] = $from->comm[$ii];
760 $thiz->asta_card = $from->asta_card;
761 $thiz->asta_pnt = $from->asta_pnt;
762 $thiz->handpt = $from->handpt;
763 $thiz->exitislock = $from->exitislock;
764 $thiz->the_end = $from->the_end;
766 $thiz->flags = $from->flags;
768 $thiz->chattime = array_fill(0, CHAT_N, 0);
770 $thiz->chat_lst = "";
775 $thiz->table = $table;
776 $thiz->table_pos = $table_pos;
777 $thiz->table_token = $from->table_token;
782 function stat_set($stat) {
783 log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]");
784 $this->stat = "$stat";
787 if (validate_sess($this->sess)) {
788 if (file_exists(PROXY_PATH) == FALSE)
790 $fp = fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
791 fwrite($fp, sprintf("%s\n",$this->stat));
797 function step_set($step) {
801 if (validate_sess($this->sess) == FALSE)
803 if (file_exists(PROXY_PATH) == FALSE)
805 if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
807 fwrite($fp, pack("l",$this->step), 4);
816 function step_inc($delta = 1) {
817 $this->step += $delta;
819 if (validate_sess($this->sess)) {
820 if (file_exists(PROXY_PATH) == FALSE)
822 $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
823 fwrite($fp, pack("l",$this->step), 4);
834 log_legal($curtime, $this, "STAT:LOGOUT", '');
836 $tmp_sess = $this->sess;
838 step_unproxy($tmp_sess);
839 $this->name = ""; // OK here
840 while (array_pop($this->comm) != NULL);
842 $this->chattime = array_fill(0, CHAT_N, 0);
844 $this->chat_lst = "";
847 $this->the_end = FALSE;
852 function step_get($sess) {
855 if (validate_sess($sess) == FALSE)
858 if (file_exists(PROXY_PATH) == FALSE)
860 if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
862 if (($s = fread($fp, 4)) == FALSE)
866 $arr = unpack('l', $s);
869 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
876 log_rd2("STEP_GET: return false ");
881 function step_unproxy($sess) {
882 log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
883 if (file_exists(PROXY_PATH) == FALSE)
885 @unlink(PROXY_PATH."/".$sess.".step");
892 var $comm; // commands for many people
893 var $step; // current step of the comm array
894 var $garbage_timeout;
898 $this->user = array();
899 $this->table = array();
901 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
902 $this->user[$i] =& User::create("", "");
905 for ($i = 0 ; $i < TABLES_N ; $i++) {
906 $this->table[$i] =& Table::create($i);
908 $row = ( (((int)($i / 4)) % 2) == 0 );
909 $col = ($i % 2 == 0);
910 $this->table[$i]->auth_only = (($row && $col) || (!$row && !$col));
913 $this->table[$i]->auth_only = FALSE;
916 $this->garbage_timeout = 0;
919 function garbage_manager($force)
923 log_rd2("garbage_manager START");
925 /* Garbage collector degli utenti in timeout */
927 if ($force || $this->garbage_timeout < $curtime) {
929 // Before all align times with table timeout
930 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
931 $table_cur =& $this->table[$table_idx];
932 // if the table is complete and exists its shared mem we get the info about users lacc
934 if ($table_cur->player_n == PLAYERS_N) {
935 log_main("PLAYERS == N TABLE ".$table_idx);
937 if (($sem = Briskin5::lock_data($table_idx)) != FALSE) {
938 log_main("bin5 lock data success");
940 $no_recovery = FALSE;
941 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
942 if ($table_cur->table_token != $bri->table_token) {
943 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
944 log_main("ERROR: not matching table_start. Room: ".$table_cur->table_start." Table: ".$bri->table_start);
954 log_main("garbage_manager: bri loaded successfully.");
955 $bri->garbage_manager(TRUE);
957 $bri_table = &$bri->table[0];
959 // is the end of the table
960 if ($bri->the_end == TRUE) {
962 * DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN
964 log_main("garbage_manager: INSIDE THE END.");
966 $plist = "$table_cur->table_token|$user_cur->table|$table_cur->player_n";
967 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
968 $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
971 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
972 // stat must be "table" by definition
973 $user_cur =& $this->user[$table_cur->player[$i]];
974 $bri_user =& $bri->user[$i];
976 $user_cur->subst = $bri_user->subst;
977 $user_cur->step = $bri_user->step;
978 $user_cur->lacc = $bri_user->lacc;
979 $user_cur->laccwr = $bri_user->lacc;
980 $user_cur->bantime = $bri_user->bantime;
983 log_legal($curtime, $user_cur, "STAT:DESTROY_GAME", $plist);
985 $this->room_join_wakeup(&$user_cur, FALSE, 0);
986 $table_cur->table_token = "";
987 $table_cur->wakeup_time = $curtime + WAKEUP_TIME;
988 Briskin5::destroy_data($table_idx);
991 log_main("gm:: save_data");
993 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
994 $this->user[$table_cur->player[$i]]->lacc = $bri->user[$i]->lacc;
997 Briskin5::save_data(&$bri);
999 } // else if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
1000 else if ($no_recovery == FALSE) {
1001 log_crit("ERROR: table ".$table_idx." unrecoverable join");
1003 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
1004 $user_cur = &$this->user[$table_cur->player[$i]];
1005 $user_cur->subst = "shutdowner";
1006 $user_cur->step_inc();
1008 $ret = sprintf('stat = "%s"; subst = "%s";', $user_cur->stat, $user_cur->subst);
1009 $ret .= "gst.st = ".($user_cur->step+1)."; ";
1010 $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);
1011 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1012 $user_cur->step_inc();
1015 $plist = "$table_cur->table_token|$user_cur->table|$table_cur->player_n";
1016 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
1017 $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
1019 log_legal($curtime, $user_cur, "STAT:DESTROY_GAME(RECOVERY)", $plist);
1021 $this->room_join_wakeup(&$user_cur, TRUE, -2);
1022 $table_cur->table_token = "";
1025 Briskin5::unlock_data($sem);
1027 } // if ($table_cur->player_n == PLAYERS_N) {
1028 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1030 log_rd2("out new loop.");
1032 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1033 $user_cur = &$this->user[$i];
1035 log_rd2("User: ".$user_cur->name." stat: ".$user_cur->stat." subst: ".$user_cur->subst);
1037 if ($user_cur->sess == "")
1040 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
1041 // Auto logout dell'utente
1042 log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
1044 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
1045 log_auth($user_cur->sess, "Autologout session.");
1049 log_rd2("AUTO LOGOUT.");
1050 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
1051 $this->room_wakeup(&$user_cur);
1052 else if ($user_cur->subst == 'standup')
1053 $this->room_outstandup(&$user_cur);
1055 log_rd2("LOGOUT FROM WHAT ???");
1059 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
1060 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
1061 $this->room_wakeup(&$user_cur);
1062 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1063 /* MLANG: <br>Sei stato inattivo per ".(EXPIRE_TIME_SMAMMA/60.0)." minuti. <br><br>Quindi ritorni tra i <b>Giocatori in piedi</b>.", "torna ai tavoli" */
1064 $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);
1065 $user_cur->step_inc();
1069 log_rd2("GARBAGE UPDATED!");
1071 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
1078 function show_room($user_step, &$user)
1080 log_main("show_room: username: ".$user->name);
1083 $ret = sprintf('gst.st = %d; ', $user_step);
1084 if ($user->subst == 'standup')
1085 $ret .= "tra.show(); ";
1087 $ret .= "tra.hide(); ";
1089 $ret .= sprintf('stat = "%s";', $user->stat);
1091 $ret .= root_wellcome($user);
1092 $ret .= sprintf('subst = "%s";', $user->subst);
1093 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
1094 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
1095 $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', $itin, xcape($user->name), $itou);
1096 for ($i = 0 ; $i < TABLES_N ; $i++) {
1097 $ret .= $this->table_content($user, $i);
1098 // $ret .= table_act_content(($user->subst == 'standup'), $this->table[$i]->player_n, $i, $user->table,
1099 // ($this->table[$i]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
1100 $ret .= $this->table[$i]->act_content($user);
1101 if ($this->table[$i]->wag_own != NULL)
1102 $ret .= sprintf('tra.add(%d, "%s: %s"); ', $i, $this->table[$i]->wag_own->name, $this->table[$i]->wag_com);
1104 $ret .= sprintf('tra.rem(%d); ', $i);
1106 $ret .= $this->standup_content($user);
1112 function room_wakeup(&$user)
1114 $table_idx = $user->table;
1115 $table = &$this->table[$table_idx];
1117 log_main("WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
1121 $from_table = ($user->stat == "table");
1123 log_main("WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1125 for ($i = 0 ; $i < $table->player_n ; $i++) {
1126 $user_cur = &$this->user[$table->player[$i]];
1127 log_main("PREIMPOST: INLOOP name: ".$user_cur->name);
1129 if ($user_cur != $user) {
1130 $user_cur->stat_set("room");
1131 $user_cur->subst = "sitdown";
1132 $user_cur->laccwr = $curtime;
1134 else if ($user->sess != "") {
1135 $user_cur->stat_set("room");
1136 $user_cur->subst = "standup";
1137 $user_cur->laccwr = $curtime;
1138 $user_cur->table = -1;
1143 $user->stat_set("room");
1144 $user->subst = "standup";
1145 $user->laccwr = $curtime;
1148 $remove_wagon = false;
1149 if($table->wag_own == $user) {
1150 $remove_wagon = true;
1151 $table->wag_reset($curtime);
1156 /* aggiorna l'array dei giocatori al tavolo. */
1157 $table->user_rem(&$this, &$user);
1159 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1160 $user_cur = &$this->user[$i];
1161 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1164 log_main("VALORI: name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
1166 $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
1167 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
1168 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; window.onbeforeunload = null; document.location.assign("index.php");|';
1169 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
1170 log_main("DOCUMENT.index.php: from table");
1172 else if ($user_cur->stat == "room") {
1173 log_main("DOCUMENT.index.php: from table");
1175 $ret .= $this->table_content($user_cur, $table_idx);
1176 $ret .= $this->standup_content($user_cur);
1178 // $ret .= table_act_content(FALSE, 0, $table_idx, $user->table, FALSE);
1179 $ret .= $table->act_content($user);
1181 if ($user_cur == $user) {
1182 // set the new status
1183 $ret .= 'subst = "standup"; tra.show(); ';
1184 // clean the action buttons in other tables
1185 for ($e = 0 ; $e < TABLES_N ; $e++) {
1186 if ($this->table[$e]->player_n < PLAYERS_N) {
1187 // $ret .= table_act_content(TRUE, 0, $e, $user->table,
1188 // ($this->table[$e]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
1189 $ret .= $this->table[$e]->act_content($user);
1194 // $ret .= table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table,
1195 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1196 $ret .= $table->act_content($user_cur);
1199 log_wr("ROOM_WAKEUP: ".$ret);
1200 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1201 $user_cur->step_inc();
1205 function room_join_wakeup(&$user, $update_lacc = FALSE, $trans_delta)
1207 $table_idx = $user->table;
1208 $table = &$this->table[$table_idx];
1210 log_main("JOIN_WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
1213 $user_wup = array();
1215 $user_tab = array();
1217 log_main("JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1219 for ($i = 0 ; $i < $table->player_n ; $i++) {
1220 $user_cur = &$this->user[$table->player[$i]];
1221 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1222 if ($user_cur->sess != "") {
1223 if ($update_lacc == TRUE) {
1224 $user_cur->laccwr = $curtime;
1226 log_main("cur: ".$user_cur->name." subst: ".$user_cur->subst);
1227 if ($user_cur->subst == "shutdowned") {
1228 $user_cur->stat_set("room");
1229 $user_cur->subst = "sitdown";
1231 else if ($user_cur->subst == "shutdowner") {
1232 $user_cur->stat_set("room");
1233 $user_cur->subst = "standup";
1234 $user_cur->table = -1;
1235 $user_wup[$user_wup_n++] = &$user_cur;
1237 $remove_wagon = false;
1238 if($table->wag_own == $user_cur) {
1239 $remove_wagon = true;
1240 $table->wag_reset($curtime);
1243 $user_tab[$user_tab_n++] = &$user_cur;
1247 for ($wup_idx = 0 ; $wup_idx < $user_wup_n ; $wup_idx++)
1248 $table->user_rem(&$this, &$user_wup[$wup_idx]);
1250 /* aggiorna l'array dei giocatori al tavolo. */
1252 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1253 log_main("START LOOP");
1254 $user_cur = &$this->user[$i];
1255 if ($user_cur->sess == '' || $user_cur->stat != 'room') {
1256 log_main("name: ".$user_cur->name."skip subst: ".$user_cur->subst);
1261 log_main("VALORI name: ".$user_cur->name." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
1263 $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
1264 if ($user_cur->stat == "room") {
1265 log_main("DOCUMENT.index.php from table");
1267 $ret .= $this->table_content($user_cur, $table_idx);
1268 $ret .= $this->standup_content($user_cur);
1270 // $ret .= table_act_content(FALSE, 0, $table_idx, $user_cur->table,
1271 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1272 $ret .= $table->act_content($user_cur);
1275 for ($tab_idx = 0 ; $tab_idx < $user_tab_n ; $tab_idx++)
1276 if ($user_cur == $user_tab[$tab_idx])
1279 // for users that wakeup the room will be reconstructed by index_rd.php
1280 if ($tab_idx < $user_tab_n) {
1281 log_main("PRE show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1283 // ARRAY_POP DISABLED
1284 // if ($trans_delta == 0)
1285 // while (array_pop($user_cur->comm) != NULL);
1287 $user_cur->trans_step = $user_cur->step + 1 + $trans_delta;
1288 $user_cur->comm[$user_cur->step % COMM_N] = "";
1289 $user_cur->step_inc();
1290 $user_cur->comm[$user_cur->step % COMM_N] = $this->show_room(($user_cur->step + 1), &$user_cur);
1291 $user_cur->step_inc();
1292 log_main("POST show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1296 log_main("JOIN_WAKEUP wup_idx ".$wup_idx." wup_n ".$user_wup_n);
1298 log_main("JOIN_WAKEUP more");
1299 // $ret .= table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table,
1300 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1301 $ret .= $table->act_content($user_cur);
1303 log_main("JOIN_WAKEUP end more");
1305 log_wr("ROOM_JOIN_WAKEUP: ".$ret);
1306 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1307 $user_cur->step_inc();
1311 function room_outstandup(&$user)
1313 $this->room_sitdown(&$user, -1);
1316 function table_update(&$user)
1318 log_main("table_update: pre - USER: ".$user->name);
1320 $table_idx = $user->table;
1322 if ($table_idx > -1)
1323 $table = &$this->table[$table_idx];
1325 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1327 $user_cur = &$this->user[$i];
1328 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1331 $ret = "gst.st = ".($user_cur->step+1)."; ";
1332 if ($table_idx > -1)
1333 $ret .= $this->table_content($user_cur, $table_idx);
1335 if ($user_cur == $user) {
1336 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
1337 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
1338 $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>: ";', $itin, xcape($user->name), $itou);
1340 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1341 $user_cur->step_inc();
1344 log_main("table_update: post");
1347 function room_sitdown(&$user, $table_idx)
1349 log_main("room_sitdown ".($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
1353 if ($table_idx > -1 && $table_idx < TABLES_N) {
1354 $table = &$this->table[$table_idx];
1357 if ($table->wag_own != NULL && $table->player_n == PLAYERS_N) {
1358 for ($i = 0 ; $i < TABLES_N ; $i++) {
1359 $user_cur =& $this->user[$table->player[$i]];
1360 if ($user_cur == $table->wag_own) {
1361 $train_app = sprintf("tra.rem(%d); ", $table_idx);
1362 $table->wag_reset(time());
1369 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1371 $user_cur = &$this->user[$i];
1372 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1375 $ret = "gst.st = ".($user_cur->step+1)."; ".$train_app;
1376 if ($table_idx > -1)
1377 $ret .= $this->table_content($user_cur, $table_idx);
1378 $ret .= $this->standup_content($user_cur);
1380 if ($user_cur == $user) {
1381 $ret .= 'subst = "sitdown"; tra.hide(); ';
1382 // clean the action buttons in other tables
1383 for ($e = 0 ; $e < TABLES_N ; $e++) {
1384 // $ret .= table_act_content(FALSE, 0, $e, $user_cur->table, FALSE);
1385 $ret .= $this->table[$e]->act_content($user_cur);
1388 else if ($table_idx > -1) {
1389 if ($table->player_n == PLAYERS_N) {
1390 // $ret .= table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table,
1391 /// ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1392 $ret .= $table->act_content($user_cur);
1395 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1396 $user_cur->step_inc();
1400 function chatt_send(&$user, $mesg)
1402 GLOBAL $G_alarm_passwd;
1406 $msg = substr($mesg, 6, 128);
1408 $dt = date("H:i ", $curtime);
1418 $update_room = FALSE;
1420 if (strcmp($msg, "/tav") == 0 ||
1421 strncmp($msg, "/tav ", 5) == 0) {
1423 if ($user->stat != 'room' || $user->subst != 'sitdown') {
1424 /* MLANG: "<br>Per attivare il messaggio di segnalazione del tavolo occorre essere seduti.<br><br>", "<br>Il messaggio di segnalazione del tavolo è già attivato.<br><br>", "<br>Il messaggio di segnalazione del tavolo<br>è disattivato ancora per %d second%s.<br><br>" */
1425 $msg = "<br>Per attivare il messaggio di segnalazione del tavolo occorre essere seduti.<br><br>";
1426 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1431 $table = &$this->table[$user->table];
1433 if ($table->wag_own != NULL) {
1434 $msg = "<br>Il messaggio di segnalazione del tavolo è già attivato.<br><br>";
1435 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1440 $dtime = $curtime - $table->wag_tout;
1441 if ($dtime < EXPIRE_TIME_WAG) {
1442 $msg = sprintf("<br>Il messaggio di segnalazione del tavolo<br>è disattivato ancora per %d second%s.<br><br>",
1443 EXPIRE_TIME_WAG - $dtime, (EXPIRE_TIME_WAG - $dtime == 1 ? "o" : "i"));
1444 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1449 $msg = substr($msg, 5);
1451 $table->wag_set($user, $msg);
1452 $to_user = sprintf('tra.add(%d, "%s");', $user->table, xcape(sprintf("%s: %s", $user->name, $msg)));
1453 $to_room = $to_user;
1456 } // /tav chat command
1458 else if (strncmp($msg, "/alarm ", 7) == 0) {
1459 if (strncmp($msg, "/alarm to ", 10) == 0) {
1460 $sp_pos = strpos($msg, " ", 10);
1461 $target = substr($msg, 10, $sp_pos - 10);
1462 $alarm_check = "/alarm to ".$target." ".$G_alarm_passwd." ";
1466 $alarm_check = "/alarm ".$G_alarm_passwd." ";
1469 if (strncmp($msg, $alarm_check, strlen($alarm_check)) != 0) {
1470 /* MLANG: "<br>La password digitata non è corretta.<br><br>" */
1471 $msg = "<br>La password digitata non è corretta.<br><br>";
1472 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1477 /* MLANG: "Alarm <b>%s</b> inviato a <b>%s</b>." */
1478 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"Alarm <b>%s</b> inviato a <b>%s</b>.");',
1479 $dt, NICKSERV, xcape(substr($msg, strlen($alarm_check))),
1480 ($target == "" ? "tutti" : xcape($target)) );
1482 $msg = sprintf("<br><b>%s<br><br>%s</b><br><br>",
1483 $dt.NICKSERV, xcape(substr($msg, strlen($alarm_check))));
1484 /* MLANG: "chiudi" */
1485 $to_all = show_notify($msg, 0, "chiudi", 400, 120);
1487 } // /alarm chat command
1488 else if (strncmp($msg, "/garante", 8) == 0) {
1489 if ($user->flags & USER_FLAG_AUTH) {
1490 $to_user = sprintf('authbox(300,200);');
1493 /* MLANG: "<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>", "Il nickname deve contenere almeno una lettera dell\'alfabeto o una cifra.", "Nickname <b>%s</b> già in uso." */
1494 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>");', $dt, NICKSERV);
1497 else if (strncmp($msg, "/nick ", 6) == 0) {
1498 log_main("chatt_send BEGIN");
1501 if (($name_new = validate_name(substr($msg, 6))) == FALSE) {
1502 $to_user = sprintf('chatt_sub("%s", [2,"%s"],"Il nickname deve contenere almeno una lettera dell\'alfabeto o una cifra.");', $dt, NICKSERV);
1506 $msg = "COMMAND ".$msg;
1507 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1508 $user_cur = &$this->user[$i];
1510 if ($user_cur->sess == '')
1512 if ($user_cur->name == $name_new)
1515 if ($i < MAX_PLAYERS) {
1516 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"Nickname <b>%s</b> già in uso.");', $dt, NICKSERV, xcape($name_new));
1520 /* MLANG: "<b>Non puoi cambiare nick a un tavolo per soli autenticati.</b>", "Il nickname <b>\'%s\'</b> è già registrato, <b>se il suo proprietario si autentificherà verrai rinominato d\'ufficio come ghost<i>N</i>.</b>" */
1521 if ($user->flags & USER_FLAG_AUTH) {
1522 if (strcasecmp($user->name,$name_new) != 0) {
1523 if ($user->subst == 'standup' || ($user->subst != 'standup' && $this->table[$user->table]->auth_only == FALSE)) {
1524 $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
1525 for ($i = 0 ; $i < TABLES_N ; $i++) {
1526 $to_user .= $this->table[$i]->act_content($user);
1530 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"<b>Non puoi cambiare nick a un tavolo per soli autenticati.</b>");', $dt, NICKSERV);
1535 $user->name = $name_new; // OK - nick changed
1536 /* se nome gia' in uso, segnala cosa potrebbe capitare */
1537 if (($user->flags & USER_FLAG_AUTH) == 0) {
1538 $userdb = new LoginDB();
1539 /* MLANG: "Il nickname <b>\'%s\'</b> è già registrato, <b>se il suo proprietario si autentificherà verrai rinominato d\'ufficio come ghost<i>N</i>.</b>" */
1540 if ($userdb->login_exists($name_new)) {
1541 $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));
1545 log_main("chatt_send start set");
1547 $update_room = TRUE;
1549 } // nick chat command
1551 else if (strncmp($msg, "/st ", 4) == 0) {
1552 log_main("chatt_send BEGIN");
1555 $st_str = substr($msg, 4);
1557 if (strcasecmp($st_str, "normale") == 0) {
1558 $st = USER_FLAG_S_NORM;
1560 else if (strcasecmp($st_str, "pausa") == 0) {
1561 $st = USER_FLAG_S_PAU;
1563 else if (strcasecmp($st_str, "fuori") == 0) {
1564 $st = USER_FLAG_S_OUT;
1566 else if (strcasecmp($st_str, "cane") == 0) {
1567 $st = USER_FLAG_S_DOG;
1569 else if (strcasecmp($st_str, "cibo") == 0) {
1570 $st = USER_FLAG_S_EAT;
1572 else if (strcasecmp($st_str, "lavoro") == 0) {
1573 $st = USER_FLAG_S_WRK;
1575 else if (strcasecmp($st_str, "sigaretta") == 0) {
1576 $st = USER_FLAG_S_SMK;
1578 else if (strcasecmp($st_str, "presente") == 0) {
1579 $st = USER_FLAG_S_EYE;
1582 /* MLANG: "Questo stato non esiste." */
1583 $to_user = sprintf('chatt_sub("%s", [2,"%s"],"Questo stato non esiste.");', $dt, NICKSERV);
1587 log_main("chatt_send start set");
1588 if (($user->flags & USER_FLAG_S_ALL) != $st) {
1589 $update_room = TRUE;
1590 $user->flags = ($user->flags & ~USER_FLAG_S_ALL) | $st;
1593 } // nick chat command
1595 else { // normal chat line
1596 if ($curtime < ($user->chat_ban + $user->chat_dlt)) {
1598 $user->chat_dlt = $user->chat_dlt * 2;
1599 if ($user->chat_dlt > 120)
1600 $user->chat_dlt = 120;
1602 else if ($user->chat_lst == $msg)
1604 else if ($curtime - $user->chattime[($user->chat_cur + 1) % CHAT_N] < CHAT_ILL_TIME) {
1605 $user->chat_ban = $curtime;
1606 $user->chat_dlt = 5;
1610 $user->chat_ban = 0;
1611 $user->chat_dlt = 0;
1615 $to_user = sprintf('chatt_sub("%s", [%d, "%s"],"%s");', $dt, $user->flags, xcape($user->name), xcape("== chat ban =="));
1618 $to_user = sprintf('chatt_sub("%s", [%d, "%s"],"%s");', $dt, $user->flags, xcape($user->name), xcape($msg));
1619 // temporary silentiation for troll (will became array check)
1620 if (strcasecmp($user->name,'JackRokka') != 0 && $user->sess != '47ea653f602e8')
1621 $to_room = $to_user;
1624 log_legal($curtime, $user,
1625 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$msg);
1627 $user->chat_lst = "$msg";
1628 $user->chattime[$user->chat_cur % CHAT_N] = $curtime;
1638 // Output to clients
1641 if ($to_user != FALSE) {
1642 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1643 $user->comm[$user->step % COMM_N] .= $to_user;
1647 if ($to_room != FALSE) {
1648 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1649 $user_cur = &$this->user[$i];
1650 if ($target != "" && $user_cur->name != $target)
1652 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1653 if ($user_cur->sess == '' || $user_cur->stat == 'table' || $user_cur == $user)
1656 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1657 $user_cur->comm[$user_cur->step % COMM_N] .= $to_room;
1658 $user_cur->step_inc();
1663 // Before all align times with table timeout
1664 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1665 $table_cur =& $this->table[$table_idx];
1666 // if the table is complete and exists its shared mem we get the info about users lacc
1668 if ($table_cur->player_n == PLAYERS_N) {
1669 log_main("PLAYERS == N TABLE ".$table_idx);
1671 if (($sem = Briskin5::lock_data($table_idx)) != FALSE) {
1672 log_main("bin5 lock data success");
1674 $no_recovery = FALSE;
1675 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
1676 if ($table_cur->table_token != $bri->table_token) {
1677 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
1682 if ($bri != FALSE) {
1683 $bri_table = &$bri->table[0];
1684 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
1685 // stat must be "table" by definition
1686 $bri_user =& $bri->user[$i];
1688 if ($target != "" && $bri_user->name != $target)
1690 log_main("writa: ".$user_mesg);
1691 $bri_user->comm[$bri_user->step % COMM_N] = "gst.st = ".($bri_user->step+1)."; ";
1692 $bri_user->comm[$bri_user->step % COMM_N] .= $to_tabl;
1693 $bri_user->step_inc();
1695 Briskin5::save_data(&$bri);
1697 Briskin5::unlock_data($sem);
1699 } // if ($table_cur->player_n == PLAYERS_N) {
1700 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1701 } // if ($to_tabl == true ...
1704 if ($user->stat == 'room' && $user->subst == 'standup') {
1705 $this->standup_update(&$user);
1707 else if ($user->stat == 'room' && $user->subst == 'sitdown') {
1708 log_main("chatt_send pre table update");
1709 $this->table_update(&$user);
1710 log_main("chatt_send post table update");
1712 } // if ($update_room ...
1715 } // function chatt_send( ...
1717 function &get_user($sess, &$idx)
1719 GLOBAL $PHP_SELF, $G_false;
1721 if (validate_sess($sess)) {
1722 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1723 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1726 $ret = &$this->user[$i];
1730 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1731 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
1732 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1735 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1744 * function &add_user(&$room, &$sess, &$idx, $name, $pass, $ip)
1747 * if ($idx > -1 && ret == FALSE) => duplicated nick
1748 * if ($idx == -2 && ret == FALSE) => invalid name
1749 * if ($idx == -3 && ret == FALSE) => wrong password
1750 * if ($idx == -1 && ret == FALSE) => no space left
1751 * if ($idx == 0 && ret == user) => SUCCESS
1752 * if ($idx == -$idx && ret == user) => SUCCESS (but the login exists in the auth db
1757 function &add_user(&$sess, &$idx, $name, $pass, $ip)
1763 $authenticate = FALSE;
1764 $login_exists = FALSE;
1766 $ghost_auth = FALSE;
1770 if (($name_new = validate_name($name)) == FALSE) {
1775 log_auth("XXX", sprintf("ARRIVA: [%s] pass:[%s]", $sess, ($pass == FALSE ? "FALSE" : $pass)));
1776 if (validate_sess($sess) == FALSE)
1779 /* if pass != FALSE verify the login with pass */
1780 log_auth("XXX", "auth1");
1781 $userdb = new LoginDB();
1782 if ($pass != FALSE) {
1783 log_auth("XXX", "auth2");
1784 $authenticate = $userdb->login_verify($name_new, $pass);
1785 log_auth("XXX", "authenticate: ".($authenticate == TRUE ? "TRUE" : "FALSE"));
1787 if ($authenticate == FALSE) {
1793 $login_exists = $userdb->login_exists($name_new);
1795 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1797 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1801 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
1803 continue; // NOTE: CHECK IT !!
1805 if (strcasecmp($this->user[$i]->name, $name_new) == 0) {
1806 if ($authenticate == FALSE) {
1812 $ghost_auth = ($this->user[$i]->flags & USER_FLAG_AUTH);
1819 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1821 if ($ghost > -1 && $authenticate) {
1824 $ghost_user =& $this->user[$ghost];
1826 $ghost_user->step_inc();
1829 $ghost_user->sess = $sess;
1832 $ghost_user->sess = $sess;
1835 // If user at the table we need to update the table data too
1836 $table_idx = $ghost_user->table;
1837 if ($ghost_user->stat == "table" && $this->table[$table_idx]->player_n == PLAYERS_N) {
1838 if (($brisem = Briskin5::lock_data($table_idx)) != FALSE) {
1839 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
1840 if ($bri->the_end != TRUE) {
1841 $bri->user[$ghost_user->table_pos]->step_inc();
1842 $bri->user[$ghost_user->table_pos]->sess = $sess;
1843 Briskin5::save_data(&$bri);
1846 Briskin5::unlock_data($brisem);
1851 return ($this->user[$ghost]);
1853 else if ($idx != -1 && $i == MAX_PLAYERS) {
1858 $this->user[$idx]->sess = $sess;
1861 $this->user[$idx]->sess = $sess;
1863 $this->user[$idx]->name = $name_new; // OK - add new user
1864 $this->user[$idx]->stat_set("room");
1865 $this->user[$idx]->step_set(0);
1866 while (array_pop($this->user[$idx]->comm) != NULL);
1867 $this->user[$idx]->subst = "standup";
1868 $this->user[$idx]->lacc = $curtime;
1869 $this->user[$idx]->laccwr = $curtime;
1870 $this->user[$idx]->bantime = 0;
1871 $this->user[$idx]->ip = $ip;
1873 $this->user[$idx]->flags = ($authenticate ? USER_FLAG_AUTH : 0x00);
1876 log_main("ghost: rename!");
1877 $ghost_user =& $this->user[$ghost];
1879 if ($ghost_auth == FALSE) {
1880 for ($sfx = 1 ; $sfx <= MAX_PLAYERS ; $sfx++) {
1881 $ghostname = 'ghost'.$sfx;
1882 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1883 if (strcmp("", $this->user[$i]->sess) == 0)
1886 if (strcmp($this->user[$i]->name, $ghostname) == 0) {
1891 if ($ghostname != '')
1895 $ghost_user->name = $ghostname;
1897 if ($ghost_user->stat == 'room' && $ghost_user->subst == 'standup') {
1898 $this->standup_update(&$ghost_user);
1901 log_main("chatt_send pre table update");
1902 $this->table_update(&$ghost_user);
1903 log_main("chatt_send post table update");
1905 } // if ($ghost_auth == FALSE
1907 // FIXME: cacciare il vecchio utente room && table (if needed)
1908 $ghost_user->the_end = TRUE;
1909 $ghost_user->lacc = 0;
1910 $this->garbage_manager(TRUE);
1912 } // if ($ghost > -1) {
1914 log_main(sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1918 return ($this->user[$real_idx]);
1924 function standup_update(&$user)
1926 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1927 $user_cur = &$this->user[$i];
1928 if ($user_cur->sess == '')
1931 log_main("STANDUP START: ".$user_cur->stat);
1933 if ($user_cur->stat == 'room') {
1934 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".$this->standup_content($user_cur);
1935 if ($user_cur == $user) {
1936 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
1937 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
1939 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>: ";',
1940 $itin, xcape($user->name), $itou);
1942 log_main("FROM STANDUP: NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
1944 $user_cur->step_inc();
1950 function &init_data()
1952 $room =& new Room();
1958 function &load_data()
1960 GLOBAL $G_false, $sess;
1963 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1964 log_main("ftok failed");
1969 if (($shm_sz = sharedmem_sz($tok)) == -1) {
1970 log_main("shmop_open failed");
1974 $shm_sz = SHM_DIMS_MIN;
1976 if ($shm = shm_attach($tok, $shm_sz)) {
1977 $room = @shm_get_var($shm, $tok);
1979 log_only("bri == ".($room == FALSE ? "FALSE" : "TRUE")." bri === ".($room === FALSE ? "FALSE" : "TRUE")." bri isset ".(isset($room) ? "TRUE" : "FALSE"));
1981 log_only("bri count ".count($room));
1983 if ($room == FALSE) {
1984 log_only("INIT MAIN DATA");
1986 $room =& Room::init_data();
1987 if (@shm_put_var($shm, $tok, $room) == FALSE) {
1988 log_only("PUT_VAR FALLITA ".strlen(serialize($room)));
1989 log_only(serialize($room));
1992 $room->shm_sz = $shm_sz;
2007 function save_data(&$room)
2015 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1)
2018 while ($room->shm_sz < SHM_DIMS_MAX) {
2019 if (($shm = shm_attach($tok, $room->shm_sz)) == FALSE)
2022 // log_only("PUT_VAR DI ".strlen(serialize($room)));
2023 if (shm_put_var($shm, $tok, $room) != FALSE) {
2027 if (shm_remove($shm) === FALSE) {
2028 log_only("REMOVE FALLITA");
2032 $room->shm_sz += SHM_DIMS_DLT;
2041 function lock_data()
2045 // echo "LOCK: ".FTOK_PATH."/main";
2047 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
2050 // echo "FTOK ".$tok."<br>";
2051 if (($res = sem_get($tok)) == FALSE) {
2054 if (sem_acquire($res)) {
2055 log_lock("LOCK room");
2062 function unlock_data($res)
2066 log_lock("UNLOCK room");
2068 return (sem_release($res));
2072 function standup_content($user)
2077 if ($user->stat != 'room')
2080 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
2081 if ($this->user[$e]->sess == "" || $this->user[$e]->stat != "room" || $this->user[$e]->name == "")
2086 // $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
2088 $content = ' j_stand_cont( [ ';
2090 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
2091 if ($this->user[$e]->sess == "" || $this->user[$e]->stat != "room" || $this->user[$e]->name == "")
2094 $flags = $this->user[$e]->flags;
2096 if ($this->user[$e]->subst == "standup") {
2097 if ($this->user[$e] == $user)
2102 $content .= sprintf('%s[ %d, "%s" ]',($ct > 0 ? ', ' : ''), $flags, xcape($this->user[$e]->name));
2111 function table_content_old($user, $table_idx)
2117 // Si possono usare i dati nella classe table
2120 $sess = $user->sess;
2121 $table = &$this->table[$table_idx];
2123 if ($user->stat != 'room')
2126 for ($i = 0 ; $i < $table->player_n ; $i++) {
2127 $user_cur = &$this->user[$table->player[$i]];
2129 if ($user_cur == $user)
2130 { $hilion = "<b>"; $hilioff = "</b>"; }
2132 { $hilion = ""; $hilioff = ""; }
2134 log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
2136 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
2139 for ( ; $i < PLAYERS_N ; $i++)
2143 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
2149 function table_content($user, $table_idx)
2155 // Si possono usare i dati nella classe table
2158 $sess = $user->sess;
2159 $table = &$this->table[$table_idx];
2161 if ($user->stat != 'room')
2165 for ($i = 0 ; $i < $table->player_n ; $i++) {
2166 $user_cur = &$this->user[$table->player[$i]];
2168 $flags = $user_cur->flags;
2170 if ($user_cur == $user)
2173 log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
2175 $content .= sprintf('%s[ %d, "%s" ]',($i == 0 ? '' : ', '), $flags, xcape($user_cur->name));
2180 $ret .= sprintf('j_tab_cont(%d, %s);', $table_idx, $content);
2187 function make_seed()
2189 list($usec, $sec) = explode(' ', microtime());
2190 return (float) $sec + ((float) $usec * 100000);
2194 function log_only2($log) {
2197 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONL2) == 0)
2200 if (isset($sess) == FALSE)
2205 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONL2) == 0)
2208 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2209 fwrite($fp, sprintf("ONL2: [%s] [%s]\n", $ssess, $log));
2214 function log_crit($log) {
2217 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_CRIT) == 0)
2220 if (isset($sess) == FALSE)
2225 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_CRIT) == 0)
2228 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2229 fwrite($fp, sprintf("CRIT: [%s] [%s]\n", $ssess, $log));
2234 function log_only($log) {
2237 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONLY) == 0)
2240 if (isset($sess) == FALSE)
2245 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONLY) == 0)
2248 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2249 fwrite($fp, sprintf("ONLY: [%s] [%s]\n", $ssess, $log));
2254 function log_main($log) {
2257 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_MAIN) == 0)
2260 if (isset($sess) == FALSE)
2265 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_MAIN) == 0)
2268 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2269 fwrite($fp, sprintf("MAIN: [%s] [%s]\n", $ssess, $log));
2274 function log_rd($log) {
2277 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_READ) == 0)
2280 if (isset($sess) == FALSE)
2285 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_READ) == 0)
2288 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2289 fwrite($fp, sprintf("READ: [%s] [%s]\n", $ssess, $log));
2294 function log_rd2($log) {
2297 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_REA2) == 0)
2300 if (isset($sess) == FALSE)
2305 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_REA2) == 0)
2308 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2309 fwrite($fp, sprintf("REA2: [%s] [%s]\n", $ssess, $log));
2314 function log_send($log) {
2317 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SEND) == 0)
2320 if (isset($sess) == FALSE)
2325 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SEND) == 0)
2328 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2329 fwrite($fp, sprintf("SEND: [%s] [%s]\n", $ssess, $log));
2334 function log_lock($log) {
2337 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOCK) == 0)
2340 if (isset($sess) == FALSE)
2345 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOCK) == 0)
2348 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2349 fwrite($fp, sprintf("LOCK: [%s] [%s]\n", $ssess, $log));
2354 function log_wr($log) {
2357 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_WRIT) == 0)
2360 if (isset($sess) == FALSE)
2365 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_WRIT) == 0)
2368 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2369 fwrite($fp, sprintf("WRIT: [%s] [%s]\n", $ssess, $log));
2374 function log_load($log) {
2377 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOAD) == 0)
2380 if (isset($sess) == FALSE)
2385 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOAD) == 0)
2388 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2389 fwrite($fp, sprintf("LOAD: [%s] [%s]\n", $ssess, $log));
2394 function log_auth($sess, $log) {
2395 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_AUTH) == 0)
2398 if (( (BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_AUTH) == 0)
2401 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2402 fwrite($fp, sprintf("AUTH: [%s] [%d] [%s]\n", $sess, time(), $log));
2408 // function log_legal($curtime, $sess, $name, $where, $mesg)
2409 function log_legal($curtime, &$user, $where, $mesg)
2413 if (($fp = @fopen(LEGAL_PATH."/legal.log", 'a')) != FALSE) {
2414 /* Unix time | session | nickname | IP | where was | mesg */
2415 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
2416 ($user->flags & USER_FLAG_AUTH ? 'A' : 'N'),
2417 $user->name, $_SERVER['REMOTE_ADDR'], $where , $mesg));
2425 function lock_banlist()
2427 if (($tok = @ftok(FTOK_PATH."/main", "L")) == -1) {
2431 if (($res = sem_get($tok)) == FALSE) {
2432 echo "SEM_GET FAILED";
2435 if (sem_acquire($res))
2441 function unlock_banlist($res)
2443 return (sem_release($res));
2446 function table_act_content_old($isstanding, $sitted, $table, $cur_table)
2451 if ($sitted < PLAYERS_N) {
2452 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
2456 if ($table == $cur_table)
2457 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
2464 function table_act_content($isstanding, $sitted, $table, $cur_table, $allowed)
2469 if ($sitted < PLAYERS_N) {
2477 if ($table == $cur_table)
2484 $ret = sprintf('j_tab_act_cont(%d, \'%s\');', $table, $act);
2489 function show_notify($text, $tout, $butt, $w, $h)
2491 log_main("SHOW_NOTIFY: ".$text);
2492 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
2495 function briscola_show($room, $table, $user)
2500 if ($table->asta_card == 9)
2501 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
2503 /* text of caller cell */
2504 if ($user->table_pos == $table->asta_win)
2505 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
2507 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
2508 xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
2510 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
2512 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
2513 $ret .= sprintf('$("caller").style.visibility = "visible";');
2514 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
2515 $ret .= sprintf('$("astalascio").style.visibility = "";');
2516 $ret .= sprintf('$("asta").style.visibility = "hidden";');
2517 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
2523 function game_result($asta_pnt, $pnt)
2525 if ($asta_pnt == 61) {
2528 else if ($pnt == 60)
2534 if ($pnt >= $asta_pnt)
2541 function multoval($mult)
2545 else if ($mult == 4)
2546 return ("quadruplo");
2548 return (sprintf("%d-plo", $mult));
2551 function show_table_info(&$room, &$table, $table_pos)
2554 $user = &$room->user[$table->player[$table_pos]];
2556 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
2557 $noty = sprintf('<table class=\"points\"><tr><th></th>');
2560 for ($i = 0 ; $i < PLAYERS_N ; $i++)
2561 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
2562 $noty .= sprintf("</tr>");
2565 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
2567 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
2568 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
2569 for ($e = 0 ; $e < PLAYERS_N ; $e++)
2570 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
2575 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
2576 for ($e = 0 ; $e < PLAYERS_N ; $e++)
2577 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
2578 $noty .= "</tr></table>";
2580 if ($table->old_reason != "") {
2581 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
2584 if ($table->old_win != -1) {
2585 $win = $table->player[$table->old_win];
2586 $fri = $table->player[$table->old_friend];
2588 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
2591 /* 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" */
2592 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
2593 xcape($room->user[$win]->name),
2594 xcape($room->user[$fri]->name));
2595 if ($table->old_pnt == 120) {
2596 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
2599 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
2600 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
2601 'più di 60'), $table->old_pnt,
2602 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
2606 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
2607 xcape($room->user[$win]->name));
2608 if ($table->old_pnt == 120) {
2609 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
2612 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
2613 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
2614 'più di 60'), $table->old_pnt,
2615 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
2619 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
2620 if ($table->mazzo == $table_pos)
2621 $noty .= "Fai <b>tu</b> il mazzo,";
2623 $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
2624 $noty .= "Il mazzo a <b>$unam</b>,";
2627 if ($user->subst == 'asta') {
2628 if ($table->asta_win == -1) // auction case
2629 $curplayer = $table->gstart % PLAYERS_N;
2631 $curplayer = $table->asta_win;
2633 else if ($user->subst == 'game') {
2634 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
2637 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
2638 if ($curplayer == $table_pos) {
2639 $noty .= " tocca a <b>te</b> giocare.";
2642 $unam = xcape($room->user[$table->player[$curplayer]]->name);
2643 $noty .= " tocca a <b>$unam</b> giocare.";
2646 if ($table->mult > 1) {
2647 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
2649 $noty .= "<hr><br>";
2650 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
2651 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
2652 * 130 + ($table->points_n > 0 ? 50 : 0) +
2653 * (120 * ($table->points_n / MAX_POINTS)));
2654 * will be used when we refact notify js function following
2655 * photoo class logic
2661 function root_wellcome($user)
2663 GLOBAL $root_wellarr;
2667 $dt = date("H:i ", $curtime);
2669 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
2670 $ret .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, str_replace('"', '\"', $root_wellarr[$i]));
2675 function table_wellcome($user)
2677 GLOBAL $table_wellarr;
2680 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
2681 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$i]));
2689 is_transition (is from room to table ?)
2690 is_again (is another game)
2692 Examples of $is_transition, $is_again:
2693 from reload of the page: FALSE, FALSE
2694 from sitdown in room: TRUE, FALSE
2695 from table: asta cmd e tutti passano: TRUE, TRUE
2696 from table: fine partita: TRUE, TRUE
2698 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
2700 $table_idx = $user->table;
2701 $table = &$room->table[$table_idx];
2702 $table_pos = $user->table_pos;
2704 $ret = "table_init();";
2705 $ret .= $table->exitlock_show(&$room->user, $table_pos);
2707 /* GENERAL STATUS */
2708 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
2709 $sendstep, $user->stat, $user->subst, $table_pos);
2711 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
2714 $ret .= "background_set();";
2717 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
2718 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
2720 $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', $itin, xcape($user->name), $itou);
2721 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
2722 $room->user[$table->player[($table_pos)%PLAYERS_N]]->flags,
2723 xcape($room->user[$table->player[($table_pos)%PLAYERS_N]]->name),
2725 $room->user[$table->player[($table_pos+1)%PLAYERS_N]]->flags,
2726 xcape($room->user[$table->player[($table_pos+1)%PLAYERS_N]]->name),
2728 $room->user[$table->player[($table_pos+2)%PLAYERS_N]]->flags,
2729 xcape($room->user[$table->player[($table_pos+2)%PLAYERS_N]]->name),
2731 (PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+3)%PLAYERS_N]]->flags),
2732 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+3)%PLAYERS_N]]->name)),
2734 (PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+4)%PLAYERS_N]]->flags),
2735 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+4)%PLAYERS_N]]->name)));
2737 /* NOTIFY FOR THE CARD MAKER */
2738 if ($is_transition) { // && $user->subst == "asta" superfluo
2739 $ret .= show_table_info(&$room, &$table, $table_pos);
2742 $ret .= table_wellcome($user);
2744 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
2745 $ret .= playsound("cow.mp3");
2750 if ($is_transition) { // && $user->subst == "asta" superfluo
2753 for ($i = 0 ; $i < 8 ; $i++) {
2754 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
2756 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
2757 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
2763 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
2765 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
2766 $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
2767 $table->card[$o]->value : -1),
2768 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
2773 $taked = array(0,0,0,0,0);
2774 $inhand = array(0,0,0,0,0);
2775 $ontabl = array(-1,-1,-1,-1,-1);
2778 for ($i = 0 ; $i < 40 ; $i++) {
2779 if ($table->card[$i]->stat == 'hand') {
2780 if ($table->card[$i]->owner == $table_pos) {
2781 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
2783 $inhand[$table->card[$i]->owner]++;
2785 else if ($table->card[$i]->stat == 'take') {
2786 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
2787 $taked[$table->card[$i]->owner]++;
2789 else if ($table->card[$i]->stat == 'table') {
2790 $ontabl[$table->card[$i]->owner] = $i;
2794 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2795 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
2797 log_main("Stat table: ".$logg);
2799 /* Set ours cards. */
2801 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
2802 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
2803 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
2804 $oursarg .= ($i == 0 ? "" : ", ")."-1";
2805 $ret .= sprintf('card_setours(%s);', $oursarg);
2807 /* Dispose all cards */
2808 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2809 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
2810 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
2811 $inhand[$i], $taked[$i]);
2813 if ($ontabl[$i] != -1) {
2814 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
2815 $table->card[$ontabl[$i]]->value,
2816 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
2822 if ($user->subst == 'asta') {
2824 /* show users auction status */
2826 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2827 $user_cur = &$room->user[$table->player[$i]];
2828 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
2829 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
2832 $showst .= ",-2,-2";
2833 $ret .= sprintf('show_astat(%s);', $showst);
2835 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
2836 /* show card chooser */
2837 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
2842 if ($table_pos == ($table->gstart % PLAYERS_N) &&
2843 $table->asta_win == -1)
2844 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2845 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
2847 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2848 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
2852 if ($table->asta_win == -1) { // auction case
2853 if ($table_pos == ($table->gstart % PLAYERS_N))
2854 $ret .= "remark_on();";
2856 $ret .= "remark_off();";
2858 else { // chooseed case
2859 if ($table_pos == $table->asta_win)
2860 $ret .= "remark_on();";
2862 $ret .= "remark_off();";
2865 else if ($user->subst == 'game') {
2867 if (($table->gstart + $table->turn) % PLAYERS_N == $table_pos)
2868 $ret .= "is_my_time = true; remark_on();";
2870 $ret .= "remark_off();";
2872 /* WHO CALL AND WATH */
2873 $ret .= briscola_show($room, $table, $user);
2877 } // end function show_table(...
2879 function calculate_winner(&$table)
2886 $cur_seed = $table->briscola - ($table->briscola % 10);
2888 for ($i = 0 ; $i < 40 ; $i++) {
2889 if ($table->card[$i]->stat != "table")
2892 log_wr(sprintf("Card On table: [%d]", $i));
2894 $v = $table->card[$i]->value;
2895 $ontab[$table->card[$i]->owner] = $v;
2896 $ontid[$table->card[$i]->owner] = $i;
2897 /* se briscola setto il flag */
2898 if (($v - ($v % 10)) == $cur_seed)
2902 if ($briontab == FALSE) {
2903 $cur_win = $table->gstart;
2904 $cur_val = $ontab[$cur_win];
2905 $cur_seed = $cur_val - ($cur_val % 10);
2908 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2909 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
2910 if ($ontab[$i] < $cur_val) {
2911 $cur_val = $ontab[$i];
2917 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2918 $table->card[$ontid[$i]]->owner = $cur_win;
2919 $table->card[$ontid[$i]]->stat = "take"; // Card stat
2924 function calculate_points(&$table)
2926 GLOBAL $G_all_points;
2930 if ($table->asta_pnt == 60)
2931 $table->asta_pnt = 61;
2933 $table->old_reason = "";
2934 $table->old_win = $table->asta_win;
2935 $table->old_friend = $table->friend;
2936 $table->old_asta_pnt = $table->asta_pnt;
2938 for ($i = 0 ; $i < 40 ; $i++) {
2939 $ctt = $table->card[$i]->value % 10;
2940 $own = $table->card[$i]->owner;
2941 if ($own == $table->asta_win || $own == $table->friend)
2942 $pro += $G_all_points[$ctt];
2945 log_wr(sprintf("PRO: [%d]", $pro));
2948 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
2949 $table->points[$table->points_n % MAX_POINTS] = array();
2950 for ($i = 0 ; $i < PLAYERS_N ; $i++)
2951 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
2953 $table->old_pnt = $pro;
2959 if ($pro >= $table->asta_pnt)
2964 $table->points[$table->points_n % MAX_POINTS] = array();
2965 for ($i = 0 ; $i < 5 ; $i++) {
2966 if ($i == $table->asta_win)
2967 $pt = ($i == $table->friend ? 4 : 2);
2968 else if ($i == $table->friend)
2973 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
2975 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
2977 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
2979 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
2980 $table->total[$i] += $pt;
2983 $table->old_pnt = $pro;
2987 function validate_sess($sess)
2989 if (strlen($sess) == SESS_LEN)
2995 function validate_name($name)
2997 $name_new = str_replace(' ', '_', substr(trim($name),0,12));
2999 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
3001 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
3008 function playsound($filename)
3010 return (sprintf('playsound("flasou", "%s");', $filename));
3013 function secstoword($secs)
3015 $mins = floor($secs / 60);
3018 $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
3021 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));
3026 function sharedmem_sz($tok)
3028 if (($shm_id = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
3029 log_main("shmop_open failed");
3032 $shm_sz = shmop_size($shm_id);
3033 shmop_close($shm_id);
3035 log_main("shm_sz: ".$shm_sz." SHM_DIMS: ".SHM_DIMS);
3040 function lock_data()
3044 if (($tok = @ftok(FTOK_PATH."/warrant", "B")) == -1) {
3047 // echo "FTOK ".$tok."<br>";
3048 if (($res = sem_get($tok)) == FALSE) {
3051 if (sem_acquire($res)) {
3052 log_lock("LOCK room");
3059 function unlock_data($res)
3063 log_lock("UNLOCK room");
3065 return (sem_release($res));