5 * Copyright (C) 2006-2012 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");
30 define('TABLES_N', 36);
31 define('TABLES_AUTH_N', 4);
32 define('PLAYERS_N', 3);
33 define('MAX_POINTS', 5);
34 define('MAX_PLAYERS', (20 + (PLAYERS_N * TABLES_N)));
35 define('SHM_DIMS_MIN', (50000 + 10000 * TABLES_N + 15000 * MAX_PLAYERS));
36 define('SHM_DIMS_MAX', SHM_DIMS_MIN + 1048576);
37 define('SHM_DIMS_DLT', 65536);
39 define('SHM_DIMS_U_MIN', 4096);
40 define('SHM_DIMS_U_MAX', 65536);
41 define('SHM_DIMS_U_DLT', 4096);
44 define('COMM_GEN_N', 50);
47 define('CHAT_ILL_TIME', 6);
48 define('CHAT_ENABLED', TRUE);
50 define('SESS_LEN', 13);
51 define('STREAM_TIMEOUT', 60);
52 /* FIXME: move to sac-a-push .phh */
53 /* TIME_RD define the server-side timeout, after half of it a ping request
54 is sent to client, after this time the client is log out */
55 define('EXPIRE_TIME_RD', 180);
56 define('EXPIRE_TIME_SMAMMA', 360);
57 define('EXPIRE_TIME_WAG', 10);
58 define('WAKEUP_TIME', 12);
59 // BAN_TIME da allineare anche in commons.js
60 define('BAN_TIME', 3600);
61 define('GARBAGE_TIMEOUT', 5);
62 define('NICKSERV', "<i>BriskServ</i>");
64 define('LOCK_SHARE_MAX', 10000);
66 define('DBG_ONL2', 0x0001);
67 define('DBG_ONLY', 0x0002);
68 define('DBG_MAIN', 0x0004);
69 define('DBG_READ', 0x0008);
70 define('DBG_REA2', 0x0010);
71 define('DBG_SEND', 0x0020);
72 define('DBG_LOCK', 0x0040);
73 define('DBG_WRIT', 0x0080);
74 define('DBG_LOAD', 0x0100);
75 define('DBG_AUTH', 0x0200);
76 define('DBG_CRIT', 0x0400);
77 define('DBG_LMOP', 0x0800);
78 define('DBG_TRAC', 0x1000);
79 define('DBG_SHME', 0x2000);
80 // NOTE: BRISK DEBUG must be a numerical constant, not the result of operations on symbols
81 define('BRISK_DEBUG', 0x0800);
83 define('BRISK_SINGLE_DEBUG',0);
84 define('BRISK_SINGLE_SESS', "");
85 define('DEBUGGING', "no-debugging");
87 require_once("$DOCUMENT_ROOT/Etc/".BRISK_CONF);
89 $mlang_brisk = array( 'btn_backstand'=> array( 'it' => 'torna in piedi',
90 'en' => 'back standing' ),
91 'btn_close' => array( 'it' => 'chiudi',
94 'tit_all' => array( 'it' => 'tutti',
97 'tabtout_a'=> array( 'it' => '<br>Sei stato inattivo per ',
98 'en' => '<br>You are being idle for ' ),
99 'tabtout_b'=> array( 'it' => ' minuti. <br><br>Quindi ritorni tra i <b>Giocatori in piedi</b>.',
100 'en' => ' minutes. <br><br>Then you return with the <b>standing players</b>.'),
101 'tickmust' => array( 'it' => '<br>Per attivare il messaggio di segnalazione del tavolo occorre essere seduti.<br><br>',
102 'en' => '<br>To activate the signalling message of the table it\'s necessary to be sitting<br><br>'),
103 'tickjust' => array( 'it' => '<br>Il messaggio di segnalazione del tavolo è già attivato.<br><br> ',
104 'en' => 'EN<br>Il messaggio di segnalazione del tavolo è già attivato.<br><br> '),
105 'tickwait' => array( 'it' => '<br>Il messaggio di segnalazione del tavolo<br>è disattivato ancora per %d second%s.<br><br>',
106 'en' => 'EN<br>The signalling message of the table<br>will be deactivated for %d second%s.<br><br>'),
107 'alarpass' => array( 'it' => '<br>La password digitata non è corretta.<br><br>',
108 'en' => '<br>The entered password is not correct.<br><br>'),
109 'alarret' => array( 'it' => '"Alarm \\"<b>%s</b>\\" inviato a <b>%s</b>."',
110 'en' => '"Alarm \\"<b>%s</b>\\" sent to <b>%s</b>."'),
111 'authmust' => array( 'it' => '<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>',
112 'en' => '<b>To authenticate someone you have to be authenticated.</b>'), // on your turn
113 'mesgmust' => array( 'it' => '<b>Per inviare un messaggio devi essere autenticato.</b>',
114 'en' => '<b>To send a message you have to be authenticated.</b>'),
115 'nickmust' => array( 'it' => 'Il nickname deve contenere almeno una lettera dell\'alfabeto o una cifra.',
116 'en' => 'The nickname have to contain at least one letter or one number.'),
117 'nickdupl' => array( 'it' => 'Nickname <b>%s</b> già in uso.',
118 'en' => 'The nickname <b>%s</b> is already in use.'),
119 'authchan' => array( 'it' => '<b>Non puoi cambiare nick a un tavolo per soli autenticati o se sei in modalità isolata.</b>',
120 'en' => '<b>You can\'t change your nickname into a table for only authenticated or if you are in isolation mode.</b>'),
121 'nickjust' => array( 'it' => 'Il nickname <b>\'%s\'</b> è già registrato, <b>se il suo proprietario si autentificherà verrai rinominato d\'ufficio come ghost<i>N</i>.</b>',
122 'en' => 'The nickname <b>\'%s\'</b> is already registered, <b>if its proprietary will authenticate you will named again officially ghost<i>N</i>.</b>'), // FIXME: him ???
123 'statunkn' => array( 'it' => 'Questo stato non esiste.',
124 'en' => 'This state don\'t exists.'),
125 'tabincon' => array( 'it' => '<br>I dati del tavolo n° %d sono inconsistenti, verranno resettati.<br><br>Torni in piedi.<br><br>',
126 'en' => 'EN <br>I dati del tavolo n° %d sono inconsistenti, verranno resettati.<br><br>Torni in piedi.<br><br>'),
127 'listmust' => array( 'it' => '<b>Per andare in isolamento non bisogna essere seduti a tavoli non riservati.</b>',
128 'en' => '<b>To go to isolation you must don\'t stay on not reserved tables</b>'),
130 'tit_onauth'=>array( 'it' => '(solo aut.)',
131 'en' => '(only aut.)'),
132 'tit_onisol'=>array( 'it' => '(isolam.to)',
133 'en' => '(isolation)'),
134 'db_failed' =>array('it' => '<br>Il collegamento al database è fallito.<br>Temporaneamente tutte le autenticazioni verranno sospese, accederai a Brisk come un utente normale.<br><br>Ci scusiamo per il disagio.',
135 'en' => 'Connection to the database failed<br>All authentications are suspended temporarly, you login as normal user.<br>We are about the limitation')
140 $G_lng = langtolng($G_lang);
142 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
143 $G_brisk_version = "4.5.5";
145 /* MLANG: ALL THE INFO STRINGS IN brisk.phh */
146 $root_wellarr = array( 'it' => array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: nuova visualizzazione dello stato della connessione dati, ... , migliorata la gestione dei tavoli in caso di piccoli blocchi del server, gestione data di login.',
147 'Se vuoi iscriverti alla <a target="_blank" href="mailto:ml-briscola+subscribe@milug.org">Mailing List</a>, cliccala!' ),
148 'en' => array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NEWS</b>: usage of reader/writer locking instead of generic exclusive locking.',
149 'If you want to subscribe our <a target="_blank" href="ml-briscola+subscribe@milug.org">Mailing List</a>, click it!' ) );
151 $G_room_help = array( 'it' => '
152 <div style=\\"text-align: left; padding: 8px;\\">
153 <b>Descrizione</b><br>
154 Questa è un\'implementazione della briscola in cinque, così come è spiegata su
155 <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>
156 <b>Configurazione del browser.</b><br>
157 Occorre abilitare i cookies.<br>
159 <b>Uso del sito</b><br>
160 Potete sedervi a un tavolo o rimanere in piedi.<br>
161 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
164 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>
165 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
166 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
167 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
168 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
169 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.
171 <dt><b>Comandi della chat</b>
172 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
173 <dd><b>/tav <i><frase di invito></i></b> - invito per gli altri giocatori al tavolo dove si è seduti
174 <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\\"
175 <dd><b>/authreq</b> - se si è autenticati permette di garantire per un utente fidato
176 <dd><b>/mesgtoadm</b> - se si è autenticati permette di lasciare un messaggio all\'amministratore del sito
177 <dd><b>/listen <all or auth></b> - se si è autenticati permette leggere solo i messaggi degli altri autenticati (auth) o di tutti (all)
183 <div style=\\"text-align: left; padding: 8px;\\">
184 <b>EN Descrizione</b><br>
185 EN Questa è un\'implementazione della briscola in cinque, così come è spiegata su
186 <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>
187 <b>EN Configurazione del browser.</b><br>
188 Occorre abilitare i cookies.<br>
190 <b>Uso del sito</b><br>
191 Potete sedervi a un tavolo o rimanere in piedi.<br>
192 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
195 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>
196 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
197 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
198 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
199 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
200 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.
202 <dt><b>Comandi della chat</b>
203 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
204 <dd><b>/tav <i><frase di invito></i></b> - invito per gli altri giocatori al tavolo dove si è seduti
205 <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\\"
206 <dd><b>/authreq</b> - se si è autenticati permette di garantire per un utente fidato
207 <dd><b>/mesgtoadm</b> - se si è autenticati permette di lasciare un messaggio all\'amministratore del sito
208 <dd><b>/listen <all or auth></b> - se si è autenticati permette leggere solo i messaggi degli altri autenticati (auth) o di tutti (all)
214 $G_room_passwdhowto = array( 'it' => '<br><h2>Come registrarsi su Brisk</h2>
215 <div style=\\"text-align: left; padding: 8px;\\">
216 Attualmente ci sono due metodi per ottenere una password sul sito:<br><br>
218 <li><b>Facendosi garantire da un utente di Brisk che già possidede una password</b><br><br>
219 <li><b>Auto-garantendosi utilizzando uno dei seguenti sistemi di identificazione digitale:</b><br><br>
221 <li>Carta Regionale dei Servizi della Lombardia (la tessera sanitaria)
222 <li>Carta Regionale dei Servizi del Friuli Venezia Giulia (la tessera sanitaria)
223 <li>Smart card di InfoCamere
226 <b>Per auto-garantisi occorre possedere:</b><br><br>
228 <li>il codice PIN della propria carta
229 <li>il lettore di smart-card per collegare la carta al PC (acquistabile di solito presso le edicole)
232 <b>Per effettuare la registrazione collegarsi al sito:</b><br><br>
234 <dd><a class=\\"flat\\" target=\\"_blank\\" href=\\"https://brisk.mine.nu\\">https://brisk.mine.nu</a>
237 Se sei in possesso di una carta che permette l\'identificazione via internet che non è nell\'elenco qui sopra
238 <a class=\\"flat\\" href=\\"mailto:authadmbrisk@alternativeoutput.it\\">fai una segnalazione</a>.
243 'en' => '<br><h2>EN Come registrarsi su Brisk</h2>
244 <div style=\\"text-align: left; padding: 8px;\\">
245 EN Attualmente ci sono due metodi per ottenere una password sul sito:<br><br>
247 <li><b>Facendosi garantire da un utente di Brisk che già possidede una password</b><br><br>
248 <li><b>Auto-garantendosi utilizzando uno dei seguenti sistemi di identificazione digitale:</b><br><br>
250 <li>Carta Regionale dei Servizi della Lombardia (la tessera sanitaria)
251 <li>Carta Regionale dei Servizi del Friuli Venezia Giulia (la tessera sanitaria)
254 <b>Per auto-garantisi occorre possedere:</b><br><br>
256 <li>il codice PIN della propria carta
257 <li>il lettore di smart-card per collegare la carta al PC (acquistabile di solito presso le edicole)
260 <b>Per effettuare la registrazione collegarsi al sito:</b><br><br>
262 <dd><a class=\\"flat\\" target=\\"_blank\\" href=\\"https://brisk.mine.nu\\">https://brisk.mine.nu</a>
265 Se sei in possesso di una carta che permette l\'identificazione via internet che non è nell\'elenco qui sopra
266 <a class=\\"flat\\" href=\\"mailto:authadmbrisk@alternativeoutput.it\\">fai una segnalazione</a>.
272 <dd>Seguendo la procedura di auto-garanzia all\'url: <a href="https://brisk.mine.nu">https://brisk.mine.nu</a>
276 $G_room_about = array( 'it' => '<br>
277 <div id=\\"header\\" class=\\"header\\">
278 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
279 briscola chiamata in salsa ajax
281 <br><b>version '.$G_brisk_version.'</b><br><br>
282 Copyright 2006-2012 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>',
284 <div id=\\"header\\" class=\\"header\\">
285 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
286 declaration briscola in ajax sauce <b>(Beta)</b>
288 <br><b>version '.$G_brisk_version.'</b><br><br>
289 Copyright 2006-2012 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>');
291 function addrtoipv4($addr)
293 $ipv4addr_arr = explode(':' , $addr);
294 if (isset($ipv4addr_arr[3])) {
295 $ipv4addr = $ipv4addr_arr[3];
305 for ($i = 0; $i < ob_get_level(); $i++)
307 ob_implicit_flush(1);
311 function force_no_cache(&$header_out)
313 $header_out['Pragma'] = 'no-cache, must-revalidate';
314 $header_out['Cache-Control'] = 'no-cache';
315 $header_out['Expires'] = '-1';
318 function file_lock($fname, $is_exclusive)
320 if (($res = @fopen($fname, "r+")) == FALSE) {
324 if (flock($res, ($is_exclusive ? LOCK_EX : LOCK_SH)) == FALSE) {
332 function file_unlock($res)
335 flock($res, LOCK_UN);
340 $escpush_from = array("\\", "\"");
341 $escpush_to = array("\\\\", "\\\"");
344 GLOBAL $escpush_from, $escpush_to;
346 return str_replace($escpush_from, $escpush_to, $s);
349 $escinp_from = array( "\"" );
350 $escinp_to = array( """ );
352 function escinput($s)
354 GLOBAL $escinp_from, $escinp_to;
356 return str_replace($escinp_from, $escinp_to, $s);
361 return htmlentities($s);
364 function esclfhtml($s)
366 return str_replace("\n", "<br>\n", htmlentities($s));
370 function langtolng($lang)
374 return ($G_lang == 'en' ? '-en' : '');
377 function csplitter($in, $sep)
383 for ($i = 0 ; $i < strlen($in) ; $i++) {
384 $ini = substr($in, $i, 1);
388 else if ($ini == $sep) {
407 $from = array ( '\\', '@', '|' );
408 $to = array ( '\\\\', '@', '¦' );
410 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
415 $from = array ( '\\', '|', "\t", "\n");
416 $to = array ( '\\\\', '\\|', "\\t", "\\n");
418 return (str_replace($from, $to, $s));
421 function xcapemesg($s)
423 $from = array ( "\n");
424 $to = array ( "\\n");
426 return (str_replace($from, $to, $s));
436 function getbyid($idx)
438 return ($this->el[$idx]);
441 function setbyid($idx, $v)
443 $this->el[$idx] = $v;
452 var $auth_only; // se tavolo riservato o libero
459 var $table_start; // information field
467 function create($idx)
469 if (($thiz = new Table()) == FALSE)
473 $thiz->player = array();
475 $thiz->auth_only = FALSE;
481 $thiz->table_token = "";
482 $thiz->table_start = 0;
484 $thiz->wakeup_time = 0;
491 $this->idx = $from->idx;
492 $this->player = array();
493 for ($i = 0 ; $i < $from->player_n ; $i++)
494 $this->player[$i] = $from->player[$i];
495 $this->player_n = $from->player_n;
497 log_main("PLAYER_N - parent::copy.".$this->player_n);
499 $this->auth_only = $from->auth_only;
501 $this->wag_own = $from->wag_own;
502 $this->wag_com = $from->wag_com;
503 $this->wag_tout = $from->wag_tout;
505 $this->table_token = $from->table_token;
506 $this->table_start = $from->table_start;
508 $this->wakeup_time = $from->wakeup_time;
511 function myclone($from)
513 if (($thiz = new Table()) == FALSE)
521 function spawn($from)
523 if (($thiz = new Table()) == FALSE)
526 $thiz->idx = $from->idx;
527 $thiz->player = array();
528 for ($i = 0 ; $i < $from->player_n ; $i++)
529 $thiz->player[$i] = $i;
530 $thiz->player_n = $from->player_n;
532 $thiz->auth_only = $from->auth_only;
534 $thiz->wag_own = $from->wag_own;
535 $thiz->wag_com = $from->wag_com;
536 $thiz->wag_tout = $from->wag_tout;
538 $thiz->table_token = $from->table_token;
539 $thiz->table_start = $from->table_start;
541 $thiz->wakeup_time = $from->wakeup_time;
546 function wag_set($user_idx, $mesg)
550 $this->wag_own = $user_idx;
551 $this->wag_com = $mesg;
555 function wag_reset($timeout)
557 log_main("WAG_RESET");
559 unset($this->wag_own);
562 $this->wag_tout = $timeout;
565 function player_get($idx)
567 return ($this->player[$idx]);
570 function player_set($idx, $player)
572 $this->player[$idx] = $player;
575 function user_add($idx)
577 $this->player[$this->player_n] = $idx;
580 return ($this->player_n - 1);
583 function user_rem($room, $user)
585 $tabpos = $user->table_pos;
587 /* verifico la consistenza dei dati */
588 if ($room->user[$this->player[$tabpos]] == $user) {
590 /* aggiorna l'array dei giocatori al tavolo. */
591 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
592 $this->player[$i] = $this->player[$i+1];
593 $user_cur = $room->user[$this->player[$i]];
594 $user_cur->table_pos = $i;
599 log_main("INCONSISTENCY ON TABLE.");
605 // $ret .= table_act_content(($user->subst == 'standup'), $this->table[$i]->player_n, $i, $user->table,
606 // ($this->table[$i]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
608 // function act_content($isstanding, $sitted, $table, $cur_table, $allowed)
609 function act_content($user)
612 $isstanding = ($user->subst == 'standup');
613 $sitted = $this->player_n;
615 $cur_table = $user->table;
619 if ($sitted < PLAYERS_N) {
620 if ($this->auth_only) {
621 if ($user->flags & USER_FLAG_AUTH)
635 if ($table == $cur_table)
642 $ret = sprintf('j_tab_act_cont(%d, \'%s\');', $table, $act);
655 function Delay_Manager($triglevel)
657 $this->triglevel = $triglevel;
658 $this->delta = array();
659 $this->lastcheck = 0;
662 function delta_get($curtime)
664 // clean too old delta items
665 for ($i = 0 ; $i < count($this->delta) ; $i++) {
666 if ($this->delta[$i][0] < $curtime) {
667 array_splice($this->delta, $i, 1);
672 // add new delta items if delay exceeded $this->triglevel sec
673 if ($curtime > $this->lastcheck + $this->triglevel && $curtime < $this->lastcheck + 1200.0) {
674 $delta = $curtime - $this->lastcheck - $this->triglevel;
675 array_push($this->delta, array($curtime + $delta , $delta));
676 // fprintf(STDERR, "DELTA: add new delta [%f] [%f] [%f]\n", $this->triglevel, $curtime + $delta, $delta);
679 // extract the maximum valid delta
681 for ($i = 0 ; $i < count($this->delta) ; $i++) {
682 $delta_cur = $this->delta[$i][1];
683 if ($delta_max < $delta_cur)
684 $delta_max = $delta_cur;
687 // fprintf(STDERR, "DELTA: status %d, delta_max: %f\n", count($this->delta), $delta_max);
692 function lastcheck_set($curtime)
694 $this->lastcheck = $curtime;
702 var $crystal_filename;
706 var $comm; // commands for many people
707 var $step; // current step of the comm array
708 var $garbage_timeout;
713 function Room ($crystal_filename) {
714 $this->crystal_filename = $crystal_filename;
715 $this->user = array();
716 $this->table = array();
717 $this->match = array();
719 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
720 $this->user[$i] = User::create($this, $i, "", "");
723 for ($i = 0 ; $i < TABLES_N ; $i++) {
724 $this->table[$i] = Table::create($i);
727 $row = ( (((int)($i / 4)) % 2) == 0 );
728 $col = ($i % 2 == 0);
729 $this->table[$i]->auth_only = (($row && $col) || (!$row && !$col));
732 $this->table[$i]->auth_only = FALSE;
735 if ($i < TABLES_AUTH_N)
736 $this->table[$i]->auth_only = TRUE;
738 $this->table[$i]->auth_only = FALSE;
740 $this->garbage_timeout = 0;
741 $this->shm_sz = SHM_DIMS_MIN;
743 $this->delay_mgr = new Delay_Manager(1.5);
746 function garbage_manager($force)
748 GLOBAL $G_lang, $mlang_brisk, $G_base;
752 log_rd2("garbage_manager START");
754 /* Garbage collector degli utenti in timeout */
755 $curtime = microtime(TRUE);
757 $delta = $this->delay_mgr->delta_get($curtime);
759 if (!$force && !($this->garbage_timeout < $curtime)) {
760 $this->delay_mgr->lastcheck_set($curtime);
764 // Before all align times with table timeout
765 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
766 $table_cur = $this->table[$table_idx];
767 // if the table is complete and exists its shared mem we get the info about users lacc
769 if ($table_cur->player_n == PLAYERS_N) {
770 log_main("PLAYERS == N TABLE ".$table_idx);
773 $no_recovery = FALSE;
774 if (isset($this->match[$table_idx])) {
775 $bri = $this->match[$table_idx];
777 if ($table_cur->table_token != $bri->table_token) {
778 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
779 log_main("ERROR: not matching table_start. Room: ".$table_cur->table_start." Table: ".$bri->table_start);
788 log_main("garbage_manager: bri loaded successfully.");
789 $bri->garbage_manager(TRUE);
791 $bri_table = $bri->table[0];
793 // is the end of the table
795 if ($bri->the_end == TRUE) {
797 * DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN
799 log_main("garbage_manager: INSIDE THE END.");
801 $plist = "$table_cur->table_token|$table_cur->idx|$table_cur->player_n";
802 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
803 $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
806 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
807 // stat must be "table" by definition
808 $user_cur = $this->user[$table_cur->player[$i]];
809 $bri_user = $bri->user[$i];
811 $user_cur->subst = $bri_user->subst;
812 $user_cur->step = $bri_user->step;
813 $user_cur->lacc = $bri_user->lacc;
814 $user_cur->laccwr = $bri_user->lacc;
815 $user_cur->bantime = $bri_user->bantime;
818 log_legal($curtime, $user_cur->ip, $user_cur, "STAT:DESTROY_GAME", $plist);
820 $this->room_join_wakeup($user_cur, FALSE, 0);
821 $table_cur->table_token = "";
822 $table_cur->wakeup_time = $curtime + WAKEUP_TIME;
824 $this->match_del($table_idx);
827 log_main("gm:: save_data");
829 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
830 $this->user[$table_cur->player[$i]]->lacc = $bri->user[$i]->lacc;
833 } // if ($bri == FALSE
834 else if ($no_recovery == FALSE) {
835 log_crit("ERROR: table ".$table_idx." unrecoverable join");
837 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
838 $user_cur = $this->user[$table_cur->player[$i]];
839 $user_cur->subst = "shutdowner";
840 $user_cur->step_inc();
842 $ret = sprintf('stat = "%s"; subst = "%s";', $user_cur->stat, $user_cur->subst);
843 $ret .= "gst.st = ".($user_cur->step+1)."; ";
844 // MLANG <br>I dati del tavolo n° ".$user_cur->table." sono inconsistenti, verranno resettati.<br><br>Torni in piedi.<br><br>
845 $prestr = sprintf($mlang_brisk['tabincon'][$G_lang], $user_cur->table);
846 $ret .= show_notify($prestr, 2000, $mlang_brisk['btn_close'][$G_lang], 400, 110);
847 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
848 $user_cur->step_inc();
851 $plist = "$table_cur->table_token|$user_cur->table|$table_cur->player_n";
852 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
853 $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
855 log_legal($curtime, $user_cur->ip, $user_cur, "STAT:DESTROY_GAME(RECOVERY)", $plist);
857 $this->room_join_wakeup($user_cur, TRUE, -2);
858 $table_cur->table_token = "";
861 } // if ($table_cur->player_n == PLAYERS_N) {
862 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
864 log_rd2("out new loop.");
866 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
867 $user_cur = $this->user[$i];
869 log_rd2("User: ".$user_cur->name." stat: ".$user_cur->stat." subst: ".$user_cur->subst);
871 if ($user_cur->sess == "")
874 if ($user_cur->lacc + EXPIRE_TIME_RD < ($curtime - $delta)) {
875 // Auto logout dell'utente
876 log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime - delta ".($curtime - $delta));
878 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
879 log_auth($user_cur->sess, "Autologout session.");
883 log_rd2("AUTO LOGOUT.");
884 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
885 $this->room_wakeup($user_cur);
886 else if ($user_cur->subst == 'standup')
887 $this->room_outstandup($user_cur);
889 log_rd2("LOGOUT FROM WHAT ???");
893 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < ($curtime - $delta)) { // lo rimettiamo in piedi
894 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
895 $this->room_wakeup($user_cur);
896 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
897 /* 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" */
898 $user_cur->comm[$user_cur->step % COMM_N] .= show_notify($mlang_brisk['tabtout_a'][$G_lang].(EXPIRE_TIME_SMAMMA/60.0).$mlang_brisk['tabtout_b'][$G_lang], 0, $mlang_brisk['btn_backstand'][$G_lang], 400, 100);
899 $user_cur->step_inc();
903 log_rd2("GARBAGE UPDATED!");
905 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
908 $this->delay_mgr->lastcheck_set($curtime);
912 function show_room($user_step, $user)
914 GLOBAL $G_lang, $mlang_brisk;
915 log_main("show_room: username: ".$user->name);
917 $ret = sprintf('gst.st = %d; ', $user_step);
919 if ($user->flags & USER_FLAG_ISOLAUTH) {
920 $ret .= 'list_set(\'isolation\', false, \''.$mlang_brisk['tit_onisol'][$G_lang].'\' ); ';
922 else if ($user->flags & USER_FLAG_LISTAUTH) {
923 $ret .= 'list_set(\'auth\', false, \''.$mlang_brisk['tit_onauth'][$G_lang].'\' ); ';
926 $ret .= 'list_set(\'all\', false, \'\' ); ';
929 if ($user->subst == 'standup')
930 $ret .= "tra.show(); ";
932 $ret .= "tra.hide(); ";
934 $ret .= sprintf('stat = "%s";', $user->stat);
936 $ret .= root_welcome($user);
937 if ($user->flags & USER_FLAG_DBFAILED) {
938 $ret .= "gst.st = ".($user->step+1)."; ";
939 $ret .= show_notify($mlang_brisk['db_failed'][$G_lang], 0, $mlang_brisk['btn_close'][$G_lang], 400, 140);
942 $ret .= sprintf('subst = "%s";', $user->subst);
943 $ret .= $user->myname_innerHTML();
945 for ($i = 0 ; $i < TABLES_N ; $i++) {
947 $ret .= $this->table_content($user, $i);
948 // $ret .= table_act_content(($user->subst == 'standup'), $this->table[$i]->player_n, $i, $user->table,
949 // ($this->table[$i]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
950 $ret .= $this->table[$i]->act_content($user);
951 if ($this->table[$i]->wag_own != -1)
952 $ret .= sprintf('tra.add(%d, "%s: %s"); ', $i, $this->user[$this->table[$i]->wag_own]->name, $this->table[$i]->wag_com);
954 $ret .= sprintf('tra.rem(%d); ', $i);
956 $ret .= $this->standup_content($user);
957 $ret .= "setTimeout(preload_images, 0, g_preload_img_arr, g_imgct); ";
963 function room_wakeup($user)
965 $table_idx = $user->table;
966 $table = $this->table[$table_idx];
968 log_main("WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
972 $from_table = ($user->stat == "table");
974 log_main("WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
976 for ($i = 0 ; $i < $table->player_n ; $i++) {
977 $user_cur = $this->user[$table->player[$i]];
978 log_main("PREIMPOST: INLOOP name: ".$user_cur->name);
980 if ($user->idx_get() != $table->player[$i]) {
981 $user_cur->stat_set("room");
982 $user_cur->subst = "sitdown";
983 $user_cur->laccwr = $curtime;
985 else if ($user->sess != "") {
986 $user_cur->stat_set("room");
987 $user_cur->subst = "standup";
988 $user_cur->laccwr = $curtime;
989 $user_cur->table = -1;
994 $user->stat_set("room");
995 $user->subst = "standup";
996 $user->laccwr = $curtime;
999 $remove_wagon = FALSE;
1000 if($table->wag_own == $user->idx_get()) {
1001 $table->wag_reset($curtime);
1002 $remove_wagon = TRUE;
1006 /* aggiorna l'array dei giocatori al tavolo. */
1007 $table->user_rem($this, $user);
1009 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1010 $user_cur = $this->user[$i];
1011 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1014 // log_main("VALORI: name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
1016 $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
1017 if ($from_table && ($user_cur->table == $table_idx || $user->idx_get() == $i)) {
1018 $ret .= 'gst.st_loc++; xstm.stop(); window.onunload = null; window.onbeforeunload = null; document.location.assign("index.php");|';
1019 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
1020 log_main("DOCUMENT.index.php: from table");
1022 else if ($user_cur->stat == "room") {
1023 log_main("DOCUMENT.index.php: from table");
1025 $ret .= $this->table_content($user_cur, $table_idx);
1026 $ret .= $this->standup_content($user_cur);
1028 // $ret .= table_act_content(FALSE, 0, $table_idx, $user->table, FALSE);
1029 $ret .= $table->act_content($user);
1031 if ($user->idx_get() == $i) {
1032 // set the new status
1033 $ret .= 'subst = "standup"; tra.show(); ';
1034 // clean the action buttons in other tables
1035 for ($e = 0 ; $e < TABLES_N ; $e++) {
1036 if ($this->table[$e]->player_n < PLAYERS_N) {
1037 // $ret .= table_act_content(TRUE, 0, $e, $user->table,
1038 // ($this->table[$e]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
1039 $ret .= $this->table[$e]->act_content($user);
1044 // $ret .= table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table,
1045 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1046 $ret .= $table->act_content($user_cur);
1049 log_wr("ROOM_WAKEUP: ".$ret);
1050 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1051 $user_cur->step_inc();
1055 function room_join_wakeup($user, $update_lacc = FALSE, $trans_delta)
1057 $table_idx = $user->table;
1058 $table = $this->table[$table_idx];
1060 log_main("JOIN_WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
1063 $user_wup = array();
1065 $user_tab = array();
1067 log_main("JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1069 for ($i = 0 ; $i < $table->player_n ; $i++) {
1070 $user_cur = $this->user[$table->player[$i]];
1071 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1072 if ($user_cur->sess != "") {
1073 if ($update_lacc == TRUE) {
1074 $user_cur->laccwr = $curtime;
1076 log_main("cur: ".$user_cur->name." subst: ".$user_cur->subst);
1077 if ($user_cur->subst == "shutdowned") {
1078 $user_cur->stat_set("room");
1079 $user_cur->subst = "sitdown";
1081 else if ($user_cur->subst == "shutdowner") {
1082 $user_cur->stat_set("room");
1083 $user_cur->subst = "standup";
1084 $user_cur->table = -1;
1085 $user_wup[$user_wup_n++] = $user_cur;
1087 $remove_wagon = FALSE;
1088 if($table->wag_own == $table->player[$i]) {
1089 $remove_wagon = TRUE;
1090 $table->wag_reset($curtime);
1093 $user_tab[$user_tab_n++] = $table->player[$i];
1097 for ($wup_idx = 0 ; $wup_idx < $user_wup_n ; $wup_idx++)
1098 $table->user_rem($this, $user_wup[$wup_idx]);
1100 /* aggiorna l'array dei giocatori al tavolo. */
1102 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1103 log_main("START LOOP");
1104 $user_cur = $this->user[$i];
1105 if ($user_cur->sess == '' || $user_cur->stat != 'room') {
1106 log_main("name: ".$user_cur->name."skip subst: ".$user_cur->subst);
1111 log_main("VALORI name: ".$user_cur->name." tab: ".$user_cur->table." taix: ".$table_idx);
1113 $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
1114 if ($user_cur->stat == "room") {
1115 log_main("DOCUMENT.index.php from table");
1117 $ret .= $this->table_content($user_cur, $table_idx);
1118 $ret .= $this->standup_content($user_cur);
1120 // $ret .= table_act_content(FALSE, 0, $table_idx, $user_cur->table,
1121 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1122 $ret .= $table->act_content($user_cur);
1125 for ($tab_idx = 0 ; $tab_idx < $user_tab_n ; $tab_idx++)
1126 if ($user_tab[$tab_idx] == $i)
1129 // for users that wakeup the room will be reconstructed by index_rd.php
1130 if ($tab_idx < $user_tab_n) {
1131 log_main("PRE show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1133 // ARRAY_POP DISABLED
1134 // if ($trans_delta == 0)
1135 // while (array_pop($user_cur->comm) != NULL);
1137 $user_cur->trans_step = $user_cur->step + 1 + $trans_delta;
1138 $user_cur->comm[$user_cur->step % COMM_N] = "";
1139 $user_cur->step_inc();
1140 $user_cur->comm[$user_cur->step % COMM_N] = $this->show_room(($user_cur->step + 1), $user_cur);
1141 $user_cur->step_inc();
1142 log_main("POST show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1146 log_main("JOIN_WAKEUP wup_idx ".$wup_idx." wup_n ".$user_wup_n);
1148 log_main("JOIN_WAKEUP more");
1149 // $ret .= table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table,
1150 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1151 $ret .= $table->act_content($user_cur);
1153 log_main("JOIN_WAKEUP end more");
1155 log_wr("ROOM_JOIN_WAKEUP: ".$ret);
1156 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1157 $user_cur->step_inc();
1161 function room_outstandup($user)
1163 $this->room_sitdown($user, -1);
1166 function table_update($user)
1168 log_main("table_update: pre - USER: ".$user->name);
1170 $table_idx = $user->table;
1172 if ($table_idx > -1)
1173 $table = $this->table[$table_idx];
1175 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1177 $user_cur = $this->user[$i];
1178 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1181 $ret = "gst.st = ".($user_cur->step+1)."; ";
1182 if ($table_idx > -1)
1183 $ret .= $this->table_content($user_cur, $table_idx);
1185 if ($user->idx_get() == $i) {
1186 $ret .= $user->myname_innerHTML();
1188 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1189 $user_cur->step_inc();
1192 log_main("table_update: post");
1195 function room_sitdown($user, $table_idx)
1197 log_main("room_sitdown ".($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
1201 if ($table_idx > -1 && $table_idx < TABLES_N) {
1202 $table = $this->table[$table_idx];
1205 if ($table->wag_own != -1 && $table->player_n == PLAYERS_N) {
1206 for ($i = 0 ; $i < TABLES_N ; $i++) {
1207 if ($table->wag_own == $table->player[$i]) {
1208 $train_app = sprintf("tra.rem(%d); ", $table_idx);
1209 $table->wag_reset(time());
1216 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1218 $user_cur = $this->user[$i];
1219 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1222 $ret = "gst.st = ".($user_cur->step+1)."; ".$train_app;
1223 if ($table_idx > -1)
1224 $ret .= $this->table_content($user_cur, $table_idx);
1225 $ret .= $this->standup_content($user_cur);
1227 if ($user->idx_get() == $i) {
1228 $ret .= 'subst = "sitdown"; tra.hide(); ';
1229 // clean the action buttons in other tables
1230 for ($e = 0 ; $e < TABLES_N ; $e++) {
1231 // $ret .= table_act_content(FALSE, 0, $e, $user_cur->table, FALSE);
1232 $ret .= $this->table[$e]->act_content($user_cur);
1235 else if ($table_idx > -1) {
1236 if ($table->player_n == PLAYERS_N) {
1237 // $ret .= table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table,
1238 /// ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1239 $ret .= $table->act_content($user_cur);
1242 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1243 $user_cur->step_inc();
1247 function chatt_send($user, $mesg)
1249 GLOBAL $G_base, $G_alarm_passwd, $mlang_brisk, $G_lang;
1253 $msg = mb_substr($mesg, 6, 128, "UTF-8");
1255 $dt = date("H:i ", $curtime);
1266 $is_normchat = FALSE;
1267 /* for old isolation management $is_ticker = FALSE; */
1268 $update_room = FALSE;
1270 if (strcmp($msg, "/tav") == 0 ||
1271 strncmp($msg, "/tav ", 5) == 0) {
1273 if ($user->stat != 'room' || $user->subst != 'sitdown') {
1274 /* 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>" */
1275 $msg = $mlang_brisk['tickmust'][$G_lang];
1276 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1281 $table = $this->table[$user->table];
1283 if ($table->wag_own != -1) {
1284 // MLANG <br>Il messaggio di segnalazione del tavolo è già attivato.<br><br>
1285 $msg = $mlang_brisk['tickjust'][$G_lang];
1286 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1291 $dtime = $curtime - $table->wag_tout;
1292 if ($dtime < EXPIRE_TIME_WAG) {
1293 // MLANG - <br>Il messaggio di segnalazione del tavolo<br>è disattivato ancora per %d second%s.<br><br>
1294 $msg = sprintf($mlang_brisk['tickwait'][$G_lang],
1295 EXPIRE_TIME_WAG - $dtime, (EXPIRE_TIME_WAG - $dtime == 1 ? ($G_lang == 'en' ? "" : "o") : ($G_lang == 'en' ? "s" : "i")));
1296 $to_user = show_notify($msg, 0, $mlang_brisk['btn_close'][$G_lang] , 400, 100);
1301 $msg = substr($msg, 5);
1303 $table->wag_set($user->idx_get(), $msg);
1304 $to_user = sprintf('tra.add(%d, "%s");', $user->table, xcape(sprintf("%s: %s", $user->name, $msg)));
1305 $to_room = $to_user;
1306 /* for old isolation management $is_ticker = TRUE; */
1308 } // /tav chat command
1310 else if (strncmp($msg, "/alarm ", 7) == 0) {
1311 if (strncmp($msg, "/alarm to ", 10) == 0) {
1312 $sp_pos = strpos($msg, " ", 10);
1313 $target = substr($msg, 10, $sp_pos - 10);
1314 $alarm_check = "/alarm to ".$target." ".$G_alarm_passwd." ";
1318 $alarm_check = "/alarm ".$G_alarm_passwd." ";
1321 if (strncmp($msg, $alarm_check, strlen($alarm_check)) != 0) {
1322 /* MLANG: "<br>La password digitata non è corretta.<br><br>" */
1323 $msg = $mlang_brisk['alarpass'][$G_lang];
1324 $to_user = show_notify($msg, 0, $mlang_brisk['btn_close'][$G_lang], 400, 100);
1329 /* MLANG: "Alarm <b>%s</b> inviato a <b>%s</b>." */
1330 $prestr = sprintf($mlang_brisk['alarret'][$G_lang], xcape(substr($msg, strlen($alarm_check))),
1331 ($target == "" ? $mlang_brisk['tit_all'][$G_lang] : xcape($target)) );
1332 $to_user = sprintf('chatt_sub("%s", [2, "%s"],%s);',
1333 $dt, NICKSERV, $prestr);
1335 $msg = sprintf("<br><b>%s<br><br>%s</b><br><br>",
1336 $dt.NICKSERV, xcape(substr($msg, strlen($alarm_check))));
1337 /* MLANG: "chiudi" */
1338 $to_all = show_notify($msg, 0, $mlang_brisk['btn_close'][$G_lang], 400, 120);
1340 } // /alarm chat command
1341 else if (strncmp($msg, "/listen ", 8) == 0) {
1342 $arg = substr($msg, 8);
1344 if (strcasecmp($arg, "isolation") == 0) {
1346 if ($user->stat == 'room' && $user->subst == 'sitdown' &&
1347 $user->table >= TABLES_AUTH_N) {
1348 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['listmust'][$G_lang]);
1352 $user->flags &= ~USER_FLAG_MAP_AUTH;
1353 $user->flags |= USER_FLAG_ISOLAUTH;
1354 $to_user = 'list_set(\'isolation\', true, \''.$mlang_brisk['tit_onisol'][$G_lang].'\'); ';
1357 else if (strcasecmp($arg, "auth") == 0) {
1358 $flags_old = $user->flags;
1359 $user->flags &= ~USER_FLAG_MAP_AUTH;
1360 $user->flags |= USER_FLAG_LISTAUTH;
1361 $to_user = 'list_set(\'auth\', true, \''.$mlang_brisk['tit_onauth'][$G_lang].'\'); ';
1364 $flags_old = $user->flags;
1365 $user->flags &= ~USER_FLAG_MAP_AUTH;
1366 $to_user = 'list_set(\'all\', true, \'\'); ';
1369 // if from isolation redraw standup area
1370 if (($flags_old ^ $user->flags) & USER_FLAG_ISOLAUTH) {
1371 $to_user .= 'standup_data_old = null; '.$this->standup_content($user);
1375 else if (strcmp($msg, "/authreq") == 0) {
1376 if ($user->flags & USER_FLAG_AUTH) {
1377 $to_user = sprintf('authbox(300,200);');
1380 /* 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." */
1381 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['authmust'][$G_lang]);
1384 else if (strncmp($msg, "/mesgtoadm", 8) == 0) {
1385 if ($user->flags & USER_FLAG_AUTH) {
1386 $to_user = sprintf('mesgtoadmbox(500,300);');
1389 /* MLANG: "<b>Per inviare un messaggio devi essere autenticato.</b>" */
1390 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['mesgmust'][$G_lang]);
1393 else if (strncmp($msg, "/nick ", 6) == 0) {
1394 log_main("chatt_send BEGIN");
1397 if (($name_new = validate_name(substr($msg, 6))) == FALSE) {
1398 $to_user = sprintf('chatt_sub("%s", [2,"%s"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
1402 $msg = "COMMAND ".$msg;
1403 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1404 $user_cur = $this->user[$i];
1406 if ($user_cur->sess == '')
1408 if (strcasecmp($user_cur->name,$name_new) == 0)
1411 if ($i < MAX_PLAYERS) {
1412 $prestr = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
1413 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $prestr);
1417 /* 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>" */
1418 if ($user->flags & USER_FLAG_AUTH) {
1419 if (strcasecmp($user->name,$name_new) != 0) {
1420 if (( ($user->flags & USER_FLAG_MAP_AUTH) != USER_FLAG_ISOLAUTH) &&
1421 ($user->subst == 'standup' ||
1422 ($user->subst != 'standup' && $this->table[$user->table]->auth_only == FALSE)
1425 $user->flags &= ~(USER_FLAG_AUTH | USER_FLAG_TY_ALL); // Remove auth if name changed
1426 for ($i = 0 ; $i < TABLES_N ; $i++) {
1427 $to_user .= $this->table[$i]->act_content($user);
1431 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
1436 $user->name = $name_new; // OK - nick changed
1437 /* se nome gia' in uso, segnala cosa potrebbe capitare */
1438 if (($user->flags & USER_FLAG_AUTH) == 0) {
1439 if (($bdb = BriskDB::create()) != FALSE) {
1441 /* 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>" */
1442 if ($bdb->login_exists($name_new)) {
1443 $prestr = sprintf($mlang_brisk['nickjust'][$G_lang], xcape($name_new));
1444 $to_user .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $prestr);
1449 log_main("chatt_send start set");
1451 $update_room = TRUE;
1453 } // nick chat command
1455 else if (strncmp($msg, "/st ", 4) == 0) {
1456 log_main("chatt_send BEGIN");
1459 $st_str = substr($msg, 4);
1461 if (strcasecmp($st_str, "normale") == 0) {
1462 $st = USER_FLAG_S_NORM;
1464 else if (strcasecmp($st_str, "pausa") == 0) {
1465 $st = USER_FLAG_S_PAU;
1467 else if (strcasecmp($st_str, "fuori") == 0) {
1468 $st = USER_FLAG_S_OUT;
1470 else if (strcasecmp($st_str, "cane") == 0) {
1471 $st = USER_FLAG_S_DOG;
1473 else if (strcasecmp($st_str, "cibo") == 0) {
1474 $st = USER_FLAG_S_EAT;
1476 else if (strcasecmp($st_str, "lavoro") == 0) {
1477 $st = USER_FLAG_S_WRK;
1479 else if (strcasecmp($st_str, "sigaretta") == 0) {
1480 $st = USER_FLAG_S_SMK;
1482 else if (strcasecmp($st_str, "presente") == 0) {
1483 $st = USER_FLAG_S_EYE;
1485 else if (strcasecmp($st_str, "coniglio") == 0) {
1486 $st = USER_FLAG_S_RABB;
1488 else if (strcasecmp($st_str, "calcio") == 0) {
1489 $st = USER_FLAG_S_SOCC;
1491 else if (strcasecmp($st_str, "pupo") == 0) {
1492 $st = USER_FLAG_S_BABY;
1494 else if (strcasecmp($st_str, "pulizie") == 0) {
1495 $st = USER_FLAG_S_MOP;
1497 else if (strcasecmp($st_str, "babbo") == 0) {
1498 $st = USER_FLAG_S_BABBO;
1500 else if (strcasecmp($st_str, "renna") == 0) {
1501 $st = USER_FLAG_S_RENNA;
1503 else if (strcasecmp($st_str, "pupazzo") == 0) {
1504 $st = USER_FLAG_S_PUPAZ;
1506 else if (strcasecmp($st_str, "vischio") == 0) {
1507 $st = USER_FLAG_S_VISCH;
1510 /* MLANG: "Questo stato non esiste." */
1511 $to_user = sprintf('chatt_sub("%s", [2,"%s"],"%s");', $dt, NICKSERV, $mlang_brisk['statunkn'][$G_lang]);
1515 log_main("chatt_send start set");
1516 if (($user->flags & USER_FLAG_S_ALL) != $st) {
1517 $update_room = TRUE;
1518 $user->flags = ($user->flags & ~USER_FLAG_S_ALL) | $st;
1521 } // nick chat command
1523 else { // normal chat line
1524 $is_normchat = TRUE;
1525 if (CHAT_ENABLED && $curtime < ($user->chat_ban + $user->chat_dlt)) {
1527 $user->chat_dlt = $user->chat_dlt * 2;
1528 if ($user->chat_dlt > 120)
1529 $user->chat_dlt = 120;
1531 else if ($user->chat_lst == $msg)
1533 else if (CHAT_ENABLED && $curtime - $user->chattime[($user->chat_cur + 1) % CHAT_N] < CHAT_ILL_TIME) {
1534 $user->chat_ban = $curtime;
1535 $user->chat_dlt = 5;
1539 $user->chat_ban = 0;
1540 $user->chat_dlt = 0;
1544 $to_user = sprintf('chatt_sub("%s", [%d, "%s"],"%s");', $dt, $user->flags, xcape($user->name), xcape("== chat ban =="));
1547 $to_user = sprintf('chatt_sub("%s", [%d, "%s"],"%s");', $dt, $user->flags, xcape($user->name), xcape($msg));
1548 // temporary silentiation for troll (will became array check)
1549 // if (strcasecmp($user->name,'JackRokka') != 0 && $user->sess != '47ea653f602e8')
1550 $to_room = $to_user;
1553 log_legal($curtime, $user->ip, $user,
1554 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$msg);
1556 $user->chat_lst = "$msg";
1557 $user->chattime[$user->chat_cur % CHAT_N] = $curtime;
1567 // Output to clients
1570 if ($to_user != FALSE) {
1571 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1572 $user->comm[$user->step % COMM_N] .= $to_user;
1576 if ($to_room != FALSE) {
1577 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1578 $user_cur = $this->user[$i];
1579 if ($target != "" && $user_cur->name != $target)
1581 if ($user_cur->sess == '' || $user_cur->stat == 'table' || $user->idx_get() == $i)
1584 if ($is_normchat == TRUE) {
1585 // use MAP_AUTH to check if auth or isolation
1586 if ($user_cur->flags & USER_FLAG_MAP_AUTH) {
1587 if (($user->flags & USER_FLAG_AUTH) == 0) {
1593 else if ($is_ticker) {
1594 if (($user_cur->flags & USER_FLAG_MAP_AUTH) == USER_FLAG_ISOLAUTH) {
1595 if ($user->table >= TABLES_AUTH_N)
1600 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1601 $user_cur->comm[$user_cur->step % COMM_N] .= $to_room;
1602 $user_cur->step_inc();
1607 // FIXME BRISK4: include for each kind of table
1608 require_once("${G_base}briskin5/Obj/briskin5.phh");
1609 // Before all align times with table timeout
1610 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1611 if (isset($this->match[$table_idx])) {
1612 $bri = $this->match[$table_idx];
1614 $bri_table = $bri->table[0];
1615 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
1616 // stat must be "table" by definition
1617 $bri_user = $bri->user[$i];
1619 if ($target != "" && $bri_user->name != $target)
1621 log_main("writa: ".$user_mesg);
1622 $bri_user->comm[$bri_user->step % COMM_N] = "gst.st = ".($bri_user->step+1)."; ";
1623 $bri_user->comm[$bri_user->step % COMM_N] .= $to_tabl;
1624 $bri_user->step_inc();
1626 } // if (isset($this->match
1627 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1628 } // if ($to_tabl == true ...
1631 if ($user->stat == 'room' && $user->subst == 'standup') {
1632 $this->standup_update($user);
1634 else if ($user->stat == 'room' && $user->subst == 'sitdown') {
1635 log_main("chatt_send pre table update");
1636 $this->table_update($user);
1637 log_main("chatt_send post table update");
1639 } // if ($update_room ...
1642 } // function chatt_send( ...
1644 function get_user($sess, &$idx)
1648 if (validate_sess($sess)) {
1649 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1650 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1653 $ret = $this->user[$i];
1657 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1658 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
1659 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1662 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1669 * function add_user(&$room, &$sess, &$idx, $name, $pass, $ip)
1672 * if ($idx > -1 && ret == FALSE) => duplicated nick
1673 * if ($idx == -2 && ret == FALSE) => invalid name
1674 * if ($idx == -3 && ret == FALSE) => wrong password
1675 * if ($idx == -1 && ret == FALSE) => no space left
1676 * if ($idx == 0 && ret == user) => SUCCESS
1677 * if ($idx == -$idx && ret == user) => SUCCESS (but the login exists in the auth db)
1680 function add_user(&$sess, &$idx, $name, $pass, $ip, $cookie)
1686 $authenticate = FALSE;
1688 $login_exists = FALSE;
1690 $ghost_auth = FALSE;
1695 if (($name_new = validate_name($name)) == FALSE) {
1700 log_auth("XXX", sprintf("ARRIVA: [%s] pass:[%s]", $sess, ($pass == FALSE ? "FALSE" : $pass)));
1701 if (validate_sess($sess) == FALSE)
1704 /* if pass != FALSE verify the login with pass */
1705 log_auth("XXX", "auth1");
1707 if (($bdb = BriskDB::create()) != FALSE) {
1709 if ($pass != FALSE) { // TODO: here add a method to $bdb to check if the db is available.
1710 log_auth("XXX", "auth2");
1711 $authenticate = $bdb->login_verify($name_new, $pass, $code);
1712 log_auth("XXX", "authenticate: ".($authenticate != FALSE ? "TRUE" : "FALSE"));
1714 if ($authenticate != FALSE) {
1715 $user_type = $authenticate->type_get();
1723 $login_exists = $bdb->login_exists($name_new);
1727 // if db is down, send a warning and verify only current users
1728 // no actions at this moment
1730 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1732 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1736 if ($idfree == -1 && strcmp($this->user[$i]->sess, "") == 0) {
1738 continue; // NOTE: CHECK IT !!
1740 if (strcasecmp($this->user[$i]->name, $name_new) == 0) {
1741 if ($authenticate != FALSE) {
1743 $ghost_auth = ($this->user[$i]->flags & USER_FLAG_AUTH);
1754 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1756 /* there is another user logged with your account and you and him have authenticated => new user
1757 get the session of the old user */
1758 if ($ghost > -1 && $ghost_auth && ($authenticate != FALSE)) {
1761 $ghost_user = $this->user[$ghost];
1763 $ghost_user->comm[$ghost_user->step % COMM_N] = "";
1764 $ghost_user->step_inc();
1767 $ghost_user->sess = $sess;
1770 $ghost_user->sess = $sess;
1773 // If user at the table we need to update the table data too
1774 $table_idx = $ghost_user->table;
1775 if ($ghost_user->stat == "table" && $this->table[$table_idx]->player_n == PLAYERS_N) {
1776 require_once("${G_base}briskin5/Obj/briskin5.phh");
1777 if (isset($this->match[$table_idx])) {
1778 $bri = $this->match[$table_idx];
1780 if ($bri->the_end != TRUE) {
1781 $bri->user[$ghost_user->table_pos]->comm[$bri->user[$ghost_user->table_pos]->step % COMM_N] = "";
1782 $bri->user[$ghost_user->table_pos]->step_inc();
1783 $bri->user[$ghost_user->table_pos]->sess = $sess;
1789 return ($this->user[$ghost]);
1791 else if ($idx != -1 && $i == MAX_PLAYERS) {
1796 $this->user[$idx]->sess = $sess;
1799 $this->user[$idx]->sess = $sess;
1801 $this->user[$idx]->name = $name_new; // OK - add new user
1802 $this->user[$idx]->stat_set("room");
1803 $this->user[$idx]->step_set(0);
1804 while (array_pop($this->user[$idx]->comm) != NULL);
1805 $this->user[$idx]->subst = "standup";
1806 $this->user[$idx]->lacc = $curtime;
1807 $this->user[$idx]->laccwr = $curtime;
1808 $this->user[$idx]->bantime = 0;
1809 $this->user[$idx]->ip = $ip;
1811 $this->user[$idx]->flags = $user_type;
1812 $this->user[$idx]->flags |= ($authenticate != FALSE ? USER_FLAG_AUTH : 0x00);
1813 $this->user[$idx]->flags |= ( ($pass != FALSE && $bdb == FALSE) ? USER_FLAG_DBFAILED : 0x00);
1814 log_auth("XXX", sprintf("FLAGS: [%x]", $this->user[$idx]->flags));
1817 if ($authenticate != FALSE) {
1818 $this->user[$idx]->code = $authenticate->code_get();
1819 $this->user[$idx]->flags |= USER_FLAG_LISTAUTH;
1821 if (isset($cookie['CO_list'])) {
1822 if (strcmp($cookie['CO_list'], "auth") == 0) {
1823 $this->user[$idx]->flags &= ~USER_FLAG_MAP_AUTH;
1824 $this->user[$idx]->flags |= USER_FLAG_LISTAUTH;
1826 if (strcmp($cookie['CO_list'], "isolation") == 0) {
1827 $this->user[$idx]->flags &= ~USER_FLAG_MAP_AUTH;
1828 $this->user[$idx]->flags |= USER_FLAG_ISOLAUTH;
1831 $this->user[$idx]->flags &= ~USER_FLAG_MAP_AUTH;
1837 log_main("ghost: rename!");
1838 $ghost_user = $this->user[$ghost];
1840 if ($ghost_auth == FALSE) {
1841 for ($sfx = 1 ; $sfx <= MAX_PLAYERS ; $sfx++) {
1842 $ghostname = 'ghost'.$sfx;
1843 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1844 if (strcmp("", $this->user[$i]->sess) == 0)
1847 if (strcasecmp($this->user[$i]->name, $ghostname) == 0) {
1852 if ($ghostname != '')
1856 $ghost_user->name = $ghostname;
1858 if ($ghost_user->stat == 'room' && $ghost_user->subst == 'standup') {
1859 $this->standup_update($ghost_user);
1862 log_main("chatt_send pre table update");
1863 $this->table_update($ghost_user);
1864 log_main("chatt_send post table update");
1866 } // if ($ghost_auth == FALSE
1868 // FIXME: cacciare il vecchio utente room && table (if needed)
1869 $ghost_user->the_end = TRUE;
1870 $ghost_user->lacc = 0;
1871 $this->garbage_manager(TRUE);
1873 } // if ($ghost > -1) {
1878 log_main(sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s] count [%d] name [%s] code [%s]", $idx, $sess, $name_new, count($this->user),$this->user[$real_idx]->name, $this->user[$real_idx]->code));
1880 $ret = $this->user[$real_idx];
1887 function standup_update($user)
1889 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1890 $user_cur = $this->user[$i];
1891 if ($user_cur->sess == '')
1894 log_main("STANDUP START: ".$user_cur->stat);
1896 if ($user_cur->stat == 'room') {
1897 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".$this->standup_content($user_cur);
1898 if ($user->idx_get() == $i) {
1899 $user_cur->comm[$user_cur->step % COMM_N] .= $user->myname_innerHTML();
1901 log_main("FROM STANDUP: NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
1903 $user_cur->step_inc();
1909 static function create($crystal_filename)
1911 if (($room_ser = @file_get_contents($crystal_filename)) == FALSE ||
1912 ($room = unserialize($room_ser)) == FALSE) {
1913 fprintf(STDERR, "NEW ROOM\n");
1914 $room = new Room($crystal_filename);
1917 fprintf(STDERR, "ROOM FROM FILE\n");
1918 rename($crystal_filename, $crystal_filename.".old");
1925 function load_data()
1930 if (($tok = @ftok(FTOK_PATH."/main", "C")) == -1) {
1931 log_main("ftok failed");
1935 if (($shm_sz = sharedmem_sz($tok)) == -1) {
1936 log_main("shmop_open failed");
1940 $shm_sz = SHM_DIMS_MIN;
1942 if ($shm = shm_attach($tok, $shm_sz)) {
1943 $room = @shm_get_var($shm, $tok); // CHECKED BELOW
1945 log_only("bri == ".($room == FALSE ? "FALSE" : "TRUE")." bri === ".($room === FALSE ? "FALSE" : "TRUE")." bri isset ".(isset($room) ? "TRUE" : "FALSE"));
1947 log_only("bri count ".count($room));
1949 if ($room == FALSE) {
1950 log_only("INIT MAIN DATA");
1953 $room = Room::create();
1955 log_shme("Room::create");
1957 if (Room::save_data($room) == FALSE)
1962 $room->shm_sz = $shm_sz;
1966 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1967 if (($room->user[$i] = User::load_data($i, FALSE)) == FALSE) {
1968 log_crit("User::load_data failed");
1972 if ($i < MAX_PLAYERS) {
1978 // SHSPLIT: load users from the shared memory
1987 function save_data_orig($room)
1995 if (($tok = @ftok(FTOK_PATH."/main", "C")) == -1)
1998 while ($room->shm_sz < SHM_DIMS_MAX) {
1999 if (($shm = shm_attach($tok, $room->shm_sz)) == FALSE)
2002 // log_only("PUT_VAR DI ".strlen(serialize($room)));
2003 if (@shm_put_var($shm, $tok, $room) != FALSE) {
2007 if (shm_remove($shm) === FALSE) {
2008 log_only("REMOVE FALLITA");
2012 $room->shm_sz += SHM_DIMS_DLT;
2020 function dump_data()
2022 $room_ser = serialize($this);
2023 $room_ser_len = mb_strlen($room_ser, "ASCII");
2024 if (file_put_contents($this->crystal_filename, $room_ser) == $room_ser_len) {
2031 function save_data($room)
2038 if (($tok = @ftok(FTOK_PATH."/main", "C")) == -1)
2041 // SHSPLIT: before save the $room you must save users,
2042 // detach from main struct and (then) reattach
2043 $user_park = array();
2044 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
2045 $user_park[$i] = $room->user[$i];
2046 $room->user[$i] = FALSE;
2049 while ($room->shm_sz < SHM_DIMS_MAX) {
2050 if (($shm = shm_attach($tok, $room->shm_sz)) == FALSE)
2053 // log_only("PUT_VAR DI ".strlen(serialize($room)));
2054 if (@shm_put_var($shm, $tok, $room) != FALSE) {
2055 log_shme("Room::save_data");
2059 if (shm_remove($shm) === FALSE) {
2060 log_only("REMOVE FALLITA");
2064 $room->shm_sz += SHM_DIMS_DLT;
2070 // SHSPLIT: reattach users to the room class
2071 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
2072 User::save_data($user_park[$i], $i);
2073 $room->user[$i] = $user_park[$i];
2075 log_load("FINISH: ".($ret == TRUE ? "TRUE" : "FALSE"));
2080 static function lock_data($is_exclusive)
2082 if (($res = file_lock(FTOK_PATH."/main", $is_exclusive)) != FALSE) {
2083 self::$delta_t = microtime(TRUE);
2084 log_lock("LOCK room [".self::$delta_t."]");
2092 static function unlock_data($res)
2096 log_lock("UNLOCK room [".(microtime(TRUE) - (self::$delta_t))."]");
2102 function standup_content($user)
2107 if ($user->stat != 'room')
2110 for ($i = 0 , $ct = 0 ; $ct < 4 && $i < MAX_PLAYERS ; $i++) {
2111 if ($this->user[$i]->sess == "" || $this->user[$i]->stat != "room" || $this->user[$i]->name == "")
2116 // $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
2118 $content = ' j_stand_cont( [ ';
2120 for ($i = 0 , $ct = 0 ; $i < MAX_PLAYERS ; $i++) {
2121 if ($this->user[$i]->sess == "" || $this->user[$i]->stat != "room" || $this->user[$i]->name == "")
2124 $flags = $this->user[$i]->flags;
2126 if ($this->user[$i]->subst == "standup") {
2127 if ($user->idx_get() == $i) {
2131 $content .= sprintf('%s[ %d, "%s" ]',($ct > 0 ? ', ' : ''), $flags, xcape($this->user[$i]->name));
2140 function table_content($user, $table_idx)
2146 // Si possono usare i dati nella classe table
2149 $sess = $user->sess;
2150 $table = $this->table[$table_idx];
2152 if ($user->stat != 'room')
2156 for ($i = 0 ; $i < $table->player_n ; $i++) {
2157 $user_cur = $this->user[$table->player[$i]];
2159 $flags = $user_cur->flags;
2161 if ($user->idx_get() == $table->player[$i])
2164 log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
2166 $content .= sprintf('%s[ %d, "%s" ]',($i == 0 ? '' : ', '), $flags, xcape($user_cur->name));
2171 $ret .= sprintf('j_tab_cont(%d, %s);', $table_idx, $content);
2176 function request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
2178 printf("NEW_SOCKET (root): %d\n", intval($new_socket));
2180 $enc = get_encoding($header);
2181 if (isset($header['User-Agent'])) {
2182 if (strstr($header['User-Agent'], "MSIE")) {
2183 $transp_type = "htmlfile";
2186 $transp_type = "xhr";
2190 $transp_type = "iframe";
2192 force_no_cache($header_out);
2198 index_main($this, $transp_type, $header_out, $addr, $get, $post, $cookie);
2199 $content = ob_get_contents();
2202 $s_a_p->pgflush_try_add($enc, $new_socket, 20, $header_out, $content);
2206 case "index_wr.php":
2208 index_wr_main($this, $addr, $get, $post, $cookie);
2209 $content = ob_get_contents();
2212 $s_a_p->pgflush_try_add($enc, $new_socket, 20, $header_out, $content);
2216 case "index_rd_ifra.php":
2218 if (!isset($cookie['sess'])
2219 || (($user = $this->get_user($cookie['sess'], $idx)) == FALSE)) {
2220 if (($transp = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
2223 $content = User::stream_fini($transp, $s_a_p->rndstr, TRUE);
2225 $s_a_p->pgflush_try_add($enc, $new_socket, 20, $header_out, $content);
2230 // close a previous opened index_read_ifra socket, if exists
2231 if (($prev = $user->rd_socket_get()) != NULL) {
2232 $s_a_p->socks_unset($user->rd_socket_get());
2233 fclose($user->rd_socket_get());
2234 printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
2235 $user->rd_socket_set(NULL);
2239 $user->stream_init($s_a_p->rndstr, $enc, $header_out, $content, $get, $post, $cookie);
2241 $response = headers_render($header_out, -1).chunked_content($user->rd_zls_get(), $content);
2242 $response_l = mb_strlen($response, "ASCII");
2244 $wret = @fwrite($new_socket, $response, $response_l);
2245 if ($wret < $response_l) {
2246 printf("TROUBLES WITH FWRITE: %d\n", $wret);
2247 $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
2250 $user->rd_cache_set("");
2252 fflush($new_socket);
2255 $s_a_p->socks_set($new_socket, $user);
2256 $user->rd_socket_set($new_socket);
2257 printf(" - qui ci siamo - ");
2265 /* FAR TODO: move all into an array of registered sub-apps */
2266 $subs = "briskin5/";
2267 $subs_l = strlen($subs);
2268 if (!strncmp($path, $subs, $subs_l)) {
2269 $ret = Bin5::request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, substr($path, $subs_l) , $addr, $get, $post, $cookie);
2278 function match_add($idx, $match)
2280 $this->match[$idx] = $match;
2283 function match_del($idx)
2285 unset($this->match[$idx]);
2288 function match_get($idx, $token)
2290 if (isset($this->match[$idx])) {
2292 || $token == $this->match[$idx]->table_token) {
2293 return ($this->match[$idx]);
2301 function make_seed()
2303 list($usec, $sec) = explode(' ', microtime());
2304 return (float) $sec + ((float) $usec * 100000);
2307 function btrace_line($ar)
2309 GLOBAL $G_btrace_pref_sub;
2312 for ($i = 0 ; $i < count($ar) ; $i++) {
2313 $with_class = isset($ar[$i]['class']);
2314 $with_file = isset($ar[$i]['file']);
2315 $ret .= sprintf("%s%s%s (%s:%d)", ($i == 0 ? "" : ", "),
2316 ($with_class ? $ar[$i]['class'].$ar[$i]['type'] : ""),
2317 $ar[$i]['function'], ($with_file ? str_replace($G_btrace_pref_sub, "", $ar[$i]['file']) : ""),
2318 ($with_file ? $ar[$i]['line'] : ""));
2324 function trace_ftok($id, $add)
2326 // NOTE: without space to use sed to substitute "= @ftok(" with "= @ftok("
2327 $tok=@ftok($id, $add);
2329 log_shme($tok.": ".$id." + ".$add);
2334 function log_mop($step, $log)
2336 GLOBAL $sess, $PHP_SELF;
2338 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LMOP) == 0)
2341 if (isset($sess) == FALSE)
2346 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LMOP) == 0)
2349 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2350 $btrace = btrace_line(debug_backtrace());
2353 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2354 fwrite($fp, sprintf("LMOP: [%f] [%05d] [%s] [%s]\n", gettimeofday(TRUE), $step, $log, $btrace));
2360 function log_only2($log)
2362 GLOBAL $sess, $PHP_SELF;
2364 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONL2) == 0)
2367 if (isset($sess) == FALSE)
2372 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONL2) == 0)
2375 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2376 $btrace = btrace_line(debug_backtrace());
2379 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2380 fwrite($fp, sprintf("ONL2: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2385 function log_crit($log)
2387 GLOBAL $sess, $PHP_SELF;
2389 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_CRIT) == 0)
2392 if (isset($sess) == FALSE)
2397 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_CRIT) == 0)
2400 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2401 $btrace = btrace_line(debug_backtrace());
2404 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2405 fwrite($fp, sprintf("CRIT: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2410 function log_only($log)
2412 GLOBAL $sess, $PHP_SELF;
2414 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONLY) == 0)
2417 if (isset($sess) == FALSE)
2422 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONLY) == 0)
2425 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2426 $btrace = btrace_line(debug_backtrace());
2429 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2430 fwrite($fp, sprintf("ONLY: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2435 function log_main($log)
2437 GLOBAL $sess, $PHP_SELF;
2439 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_MAIN) == 0)
2442 if (isset($sess) == FALSE)
2447 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_MAIN) == 0)
2450 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2451 $btrace = btrace_line(debug_backtrace());
2454 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2455 fwrite($fp, sprintf("MAIN: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2460 function log_rd($log)
2462 GLOBAL $sess, $PHP_SELF;
2464 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_READ) == 0)
2467 if (isset($sess) == FALSE)
2472 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_READ) == 0)
2475 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2476 $btrace = btrace_line(debug_backtrace());
2479 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2480 fwrite($fp, sprintf("READ: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2485 function log_rd2($log)
2487 GLOBAL $sess, $PHP_SELF;
2489 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_REA2) == 0)
2492 if (isset($sess) == FALSE)
2497 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_REA2) == 0)
2500 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2501 $btrace = btrace_line(debug_backtrace());
2505 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2506 fwrite($fp, sprintf("REA2: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2511 function log_send($log)
2513 GLOBAL $sess, $PHP_SELF;
2515 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SEND) == 0)
2518 if (isset($sess) == FALSE)
2523 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SEND) == 0)
2526 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2527 $btrace = btrace_line(debug_backtrace());
2530 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2531 fwrite($fp, sprintf("SEND: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2536 function log_lock($log)
2538 GLOBAL $sess, $PHP_SELF;
2540 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOCK) == 0)
2543 if (isset($sess) == FALSE)
2548 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOCK) == 0)
2551 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2552 $btrace = btrace_line(debug_backtrace());
2555 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2556 fwrite($fp, sprintf("LOCK: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2561 function log_wr($log)
2563 GLOBAL $sess, $PHP_SELF;
2565 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_WRIT) == 0)
2568 if (isset($sess) == FALSE)
2573 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_WRIT) == 0)
2576 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2577 $btrace = btrace_line(debug_backtrace());
2580 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2581 fwrite($fp, sprintf("WRIT: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2586 function log_load($log)
2588 GLOBAL $sess, $PHP_SELF;
2590 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOAD) == 0)
2593 if (isset($sess) == FALSE)
2598 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOAD) == 0)
2601 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2602 $btrace = btrace_line(debug_backtrace());
2605 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2606 fwrite($fp, sprintf("LOAD: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2611 function log_auth($sess, $log)
2615 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_AUTH) == 0)
2618 if (( (BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_AUTH) == 0)
2621 if ((BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2622 $btrace = btrace_line(debug_backtrace());
2625 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2626 fwrite($fp, sprintf("LOAD: [%s] [%d] [%s] [%s]\n", $sess, time(), $log, $btrace));
2631 function log_shme($log)
2633 GLOBAL $sess, $PHP_SELF;
2635 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SHME) == 0)
2638 if (isset($sess) == FALSE)
2643 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SHME) == 0)
2646 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2647 $btrace = btrace_line(debug_backtrace());
2650 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2651 fwrite($fp, sprintf("SHME: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2658 // function log_legal($curtime, $sess, $name, $where, $mesg)
2659 function log_legal($curtime, $addr, $user, $where, $mesg)
2662 if (($fp = @fopen(LEGAL_PATH."/legal.log", 'a')) != FALSE) {
2663 /* Unix time | session | nickname | IP | where was | mesg */
2664 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
2665 ($user->flags & USER_FLAG_AUTH ? 'A' : 'N'),
2666 $user->name, $addr, $where , $mesg));
2671 function table_act_content($isstanding, $sitted, $table, $cur_table, $allowed)
2676 if ($sitted < PLAYERS_N) {
2684 if ($table == $cur_table)
2691 $ret = sprintf('j_tab_act_cont(%d, \'%s\');', $table, $act);
2696 function show_notify($text, $tout, $butt, $w, $h)
2698 log_main("SHOW_NOTIFY: ".$text);
2699 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
2702 function show_notify_ex($text, $tout, $butt, $w, $h, $is_opaque, $block_time)
2704 log_main("SHOW_NOTIFY OPAQUE: ".$text);
2705 return sprintf('var noti = new notify_ex(gst,"%s",%d,"%s",%d,%d, %s, %d);', $text, $tout, $butt, $w, $h, ($is_opaque ? "true" : "false"), $block_time);
2709 function root_welcome($user)
2711 GLOBAL $root_wellarr, $G_lang;
2715 $dt = date("H:i ", $curtime);
2717 for ($i = 0 ; $i < count($root_wellarr[$G_lang]) ; $i++)
2718 $ret .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, str_replace('"', '\"', $root_wellarr[$G_lang][$i]));
2725 function validate_sess($sess)
2727 if (strlen($sess) == SESS_LEN)
2733 function validate_name($name)
2735 $name_new = str_replace(' ', '_', mb_substr(trim($name),0,12, "UTF-8"));
2737 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
2739 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
2746 function playsound($filename)
2748 return (sprintf('playsound("flasou", "%s");', $filename));
2751 function secstoword($secs)
2757 $mins = floor($secs / 60);
2759 if ($G_lang == 'en') {
2761 $ret = sprintf("%d minute%s%s", $mins, ($mins > 1 ? "s" : ""), ($secs > 0 ? " and " : ""));
2764 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "s" : ""));
2768 $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
2771 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));
2776 function sharedmem_sz($tok)
2778 if (($shm_id = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
2779 log_main("shmop_open failed");
2782 $shm_sz = shmop_size($shm_id);
2783 shmop_close($shm_id);
2785 // log_main("shm_sz: ".$shm_sz." SHM_DIMS: ".SHM_DIMS);
2792 static function lock_data($is_exclusive)
2794 if (($res = file_lock(FTOK_PATH."/warrant", $is_exclusive)) != FALSE) {
2795 self::$delta_t = microtime(TRUE);
2796 log_lock("LOCK warrant [".self::$delta_t."]");
2804 static function unlock_data($res)
2808 log_lock("UNLOCK warrant [".(microtime(TRUE) - (self::$delta_t))."]");
2817 static function lock_data($is_exclusive)
2819 if (($res = file_lock(FTOK_PATH."/poll", $is_exclusive)) != FALSE) {
2820 self::$delta_t = microtime(TRUE);
2821 log_lock("LOCK poll [".self::$delta_t."]");
2829 static function unlock_data($res)
2833 log_lock("UNLOCK poll [".(microtime(TRUE) - (self::$delta_t))."]");