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, 900);
52 define(GARBAGE_TIMEOUT, 10);
53 define(NICKSERV, "<i>BriskServ</i>");
55 define(DBG_ONL2, 0x0001);
56 define(DBG_ONLY, 0x0002);
57 define(DBG_MAIN, 0x0004);
58 define(DBG_READ, 0x0008);
59 define(DBG_REA2, 0x0010);
60 define(DBG_SEND, 0x0020);
61 define(DBG_LOCK, 0x0040);
62 define(DBG_WRIT, 0x0080);
63 define(DBG_LOAD, 0x0100);
64 define(DBG_AUTH, 0x0200);
65 define(DBG_CRIT, 0x0400);
67 define(BRISK_DEBUG, 0);
69 define(BRISK_SINGLE_DEBUG,0);
70 define(BRISK_SINGLE_SESS, "");
71 // define(DEBUGGING, "local");
73 require_once("$DOCUMENT_ROOT/Etc/".BRISK_CONF);
77 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
78 $G_brisk_version = "2.1.1 - trusty";
80 $root_wellarr = Array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: Autenticazione e tavoli riservati.',
81 'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
82 $table_wellarr = Array ( 'Benvenuto al tavolo. Se almeno tre giocatori non sbloccano l\'uscita cliccando il lucchetto, chi esce non può risedersi a un qualunque tavolo per '.floor(BAN_TIME/60).' minuti.');
86 <div style=\\"text-align: left; padding: 8px;\\">
87 <b>Descrizione</b><br>
88 Questa รจ un\'implementazione della briscola in cinque, così come è spiegata su
89 <a target=\\"_blank\\" href=\\"http://it.wikipedia.org/wiki/Briscola#Gioco_a_5\\">Wikipedia</a>; in breve è la variante con l\'asta prima sulla carta e poi sui punti.<br><br>
90 <b>Configurazione del browser.</b><br>
91 Occorre abilitare i cookies.<br>
93 <b>Uso del sito</b><br>
94 Potete sedervi a un tavolo o rimanere in piedi.<br>
95 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
98 All\'inizio vengono distribuite le carte e parte l\'asta; per partecipare all\'asta, quando sarà il vostro turno, potrete scegliere se andare avanti o passare cliccando sulle icone corrispondenti. Se si arriva ai punti, scrivete nella textbox il vostro rilancio e cliccate PUNTI.<br><br>
99 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
100 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
101 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
102 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
103 Dopo che è iniziata una partita per uscirne dovete chiedere agli altri giocatori di sbloccarla cliccando sul lucchetto. Se non si segue questa prassi, una volta usciti, non vi potrete sedere a nessun tavolo per '.floor(BAN_TIME/60).' minuti.
105 <dt><b>Comandi della chat</b>
106 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
107 <dd><b>/tav <i><frase di invito></i></b> - invito per gli altri giocatori al tavolo dove si è seduti
113 <div id=\\"header\\" class=\\"header\\">
114 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
115 briscola chiamata in salsa ajax
117 <br><b>version '.$G_brisk_version.'</b><br><br>
118 Copyright 2006-2008 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
123 $from = array ( '\\', '@', '|' );
124 $to = array ( '\\\\', '@', '¦' );
126 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
131 var $value; /* 0 - 39 card value */
132 var $stat; /* 'bunch', 'hand', 'table', 'take' */
133 var $owner; /* (table position 0-4) */
134 // var $pos; /* Pos in hand. */
135 var $x; /* When played the X position on the table of the owner. */
136 var $y; /* When played the Y position on the table of the owner. */
138 function Card($value, $stat, $owner)
140 $this->value = $value;
141 $this->stat = $stat; // Card stat
142 $this->owner = $owner;
145 function assign($stat,$owner)
147 $this->stat = $stat; // Card stat
148 $this->owner = $owner;
151 function setpos($pos)
158 $this->stat = 'table'; // Card stat
163 function take($newown)
165 $this->stat = 'take'; // Card stat
166 $this->owner = $newown;
190 var $points; // points array
191 var $points_n; // number of row of points
213 function &create($idx)
217 if (($thiz =& new Table()) == FALSE)
221 $thiz->player = array();
224 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
225 $thiz->asta_pla_n= -1;
226 $thiz->asta_card = -1;
227 $thiz->asta_pnt = -1;
230 $thiz->auth_only = FALSE;
232 $thiz->points = array( );
234 $thiz->total = array( 0, 0, 0, 0, 0);
235 $thiz->asta_win = -1;
236 $thiz->briscola = -1;
240 $thiz->wag_own = NULL;
244 $thiz->old_reason = "";
245 $thiz->old_asta_pnt = -1;
248 $thiz->old_friend = -1;
250 $thiz->table_token = "";
251 $thiz->table_start = 0;
253 $thiz->wakeup_time = 0;
258 function &clone(&$from)
262 if (($thiz =& new Table()) == FALSE)
265 $thiz->idx = $from->idx;
266 $thiz->player = array();
267 for ($i = 0 ; $i < $from->player_n ; $i++)
268 $thiz->player[$i] = $from->player[$i];
269 $thiz->player_n = $from->player_n;
270 $thiz->card = $from->card;
271 $thiz->mazzo = $from->mazzo; // REVIEW
272 $thiz->gstart = $from->gstart;
273 $thiz->turn = $from->turn;
275 $thiz->auth_only = $from->auth_only;
277 $thiz->wag_own = $from->wag_own;
278 $thiz->wag_com = $from->wag_com;
279 $thiz->wag_tout = $from->wag_taut;
281 $thiz->asta_pla = $from->asta_pla;
282 $thiz->asta_pla_n = $from->asta_pla_n;
283 $thiz->asta_card = $from->asta_card;
284 $thiz->asta_pnt = $from->asta_pnt;
286 $thiz->mult = $from->mult;
287 $thiz->points = $from->points;
288 $thiz->points_n = $from->points_n;
289 $thiz->total = $from->total;
291 $thiz->asta_win = $from->asta_win;
292 $thiz->briscola = $from->briscola;
293 $thiz->friend = $from->friend;
295 $thiz->old_reason = $from->old_reason;
296 $thiz->old_asta_pnt = $from->old_asta_pnt;
297 $thiz->old_pnt = $from->old_pnt;
298 $thiz->old_win = $from->old_win;
299 $thiz->old_friend = $from->old_friend;
301 $thiz->table_token = $from->table_token;
302 $thiz->table_start = $from->table_start;
304 $thiz->wakeup_time = $from->wakeup_time;
309 function &spawn(&$from)
313 if (($thiz =& new Table()) == FALSE)
316 $thiz->idx = $from->idx;
317 $thiz->player_n = $from->player_n;
318 $thiz->card = &$thiz->bunch_create();
319 $thiz->mazzo = $from->mazzo;
320 $thiz->gstart = $from->gstart;
321 $thiz->turn = $from->turn;
323 $thiz->auth_only = $from->auth_only;
325 $thiz->wag_own = $from->wag_own;
326 $thiz->wag_com = $from->wag_com;
327 $thiz->wag_tout = $from->wag_taut;
329 $thiz->asta_pla = $from->asta_pla;
330 $thiz->asta_pla_n = $from->asta_pla_n;
331 $thiz->asta_card = $from->asta_card;
332 $thiz->asta_pnt = $from->asta_pnt;
334 $thiz->mult = $from->mult;
335 $thiz->points = $from->points;
336 $thiz->points_n = $from->points_n;
337 $thiz->total = $from->total;
339 $thiz->asta_win = $from->asta_win;
340 $thiz->briscola = $from->briscola;
341 $thiz->friend = $from->friend;
343 $thiz->old_reason = $from->old_reason;
344 $thiz->old_asta_pnt = $from->old_asta_pnt;
345 $thiz->old_pnt = $from->old_pnt;
346 $thiz->old_win = $from->old_win;
347 $thiz->old_friend = $from->old_friend;
349 // players are rearranged in an dedicated array
350 $thiz->player = array();
351 for ($i = 0 ; $i < $from->player_n ; $i++)
352 $thiz->player[$i] = $i;
354 $thiz->table_token = $from->table_token;
355 $thiz->table_start = $from->table_start;
357 $thiz->wakeup_time = $from->wakeup_time;
362 function &bunch_create()
366 for ($i = 0 ; $i < 40 ; $i++) {
367 $ret[$i] =& new Card($i, 'bunch', 'no_owner');
374 function wag_set(&$user, $mesg)
378 $this->wag_own = &$user;
379 $this->wag_com = $mesg;
383 function wag_reset($timeout)
385 log_main("WAG_RESET");
387 unset($this->wag_own);
388 $this->wag_own = NULL;
390 $this->wag_tout = $timeout;
393 function bunch_make()
395 $ct = array(0,0,0,0,0);
397 mt_srand(make_seed());
399 for ($i = 39 ; $i >= 0 ; $i--)
402 for ($i = 39 ; $i >= 0 ; $i--) {
406 log_main("RND ZERO");
411 $this->card[$id]->assign('hand', $owner);
413 $rest[$rn] = $rest[$i];
414 // $pubbpos[$rn2] = $pubbpos[$i];
418 function init(&$userarr)
420 $this->mazzo = rand(0,PLAYERS_N-1);
424 $this->old_reason = "";
425 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
426 $this->total[$i] = 0;
427 $user_cur = &$userarr[$this->player[$i]];
428 $user_cur->exitislock = TRUE;
431 log_main("table::init: ci siamo");
434 function game_init(&$userarr)
438 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
442 $this->asta_pla_n = PLAYERS_N;
443 $this->asta_card = -1;
444 $this->asta_pnt = 60;
445 $this->asta_win = -1;
446 $this->briscola = -1;
450 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
451 $this->asta_pla[$i] = TRUE;
452 $user_cur = &$userarr[$this->player[$i]];
453 $user_cur->subst = 'asta';
454 $user_cur->asta_card = -2;
455 $user_cur->asta_pnt = -1;
456 $user_cur->handpt = $this->hand_points($i);
457 // SEE function calculate_points(&$table)
463 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
466 function getPlayer($idx)
468 return ($this->player[$idx]);
471 function setPlayer($idx, $player)
473 $this->player[$idx] = $player;
476 function user_add($idx)
478 $this->player[$this->player_n] = $idx;
481 return ($this->player_n - 1);
484 function user_rem(&$room, &$user)
486 $tabpos = $user->table_pos;
488 /* verifico la consistenza dei dati */
489 if ($room->user[$this->player[$tabpos]] == $user) {
491 /* aggiorna l'array dei giocatori al tavolo. */
492 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
493 $this->player[$i] = $this->player[$i+1];
494 $user_cur = &$room->user[$this->player[$i]];
495 $user_cur->table_pos = $i;
500 log_main("INCONSISTENCY ON TABLE.");
504 function hand_points($idx)
506 GLOBAL $G_all_points;
510 for ($i = 0 ; $i < 40 ; $i++) {
511 if ($this->card[$i]->owner != $idx)
514 $ctt = $this->card[$i]->value % 10;
515 $tot += $G_all_points[$ctt];
521 function exitlock_show(&$userarr, $table_pos)
523 $ct = $this->exitlock_calc(&$userarr, $table_pos);
525 $ret = sprintf('exitlock_show(%d, %s);', $ct,
526 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
530 function exitlock_calc(&$userarr, $table_pos)
534 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
535 if ($userarr[$this->player[$i]]->exitislock == FALSE)
545 // $ret .= table_act_content(($user->subst == 'standup'), $this->table[$i]->player_n, $i, $user->table,
546 // ($this->table[$i]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
548 // function act_content($isstanding, $sitted, $table, $cur_table, $allowed)
549 function act_content(&$user)
552 $isstanding = ($user->subst == 'standup');
553 $sitted = $this->player_n;
555 $cur_table = $user->table;
559 if ($sitted < PLAYERS_N) {
560 if ($this->auth_only) {
561 if ($user->flags & USER_FLAG_AUTH)
572 if ($table == $cur_table)
579 $ret = sprintf('j_tab_act_cont(%d, \'%s\');', $table, $act);
589 define(USER_FLAG_AUTH, 0x02);
592 var $name; // name of the user
593 var $sess; // session of the user
594 var $ip; // ip of the user
595 var $lacc; // last access (for the cleanup)
596 var $laccwr; // last access (for the cleanup)
597 var $bantime; // timeout to temporary ban
598 var $stat; // status (outdoor, room, table, game, ...)
599 var $subst; // substatus for each status
600 var $step; // step of the current status
601 var $trans_step; // step to enable transition between pages (disable == -1)
602 var $comm; // commands array
605 var $handpt; // Total card points at the beginning of the current hand.
606 var $exitislock; // Player can exit from the table ?
607 var $table; // id of the current table (if in table state)
608 var $table_pos; // idx on the table
609 var $table_token;// token that identify a game on a table
610 var $flags; // Bitfield with: AUTHENTICATE: 0x02
611 var $the_end; // Flag to change the end of the session
613 var $chat_lst; // Last chat line
614 var $chattime; // Array of chat times
615 var $chat_cur; // Current chat line number
616 var $chat_ban; // Time for ban chat
617 var $chat_dlt; // Delta t for ban
621 function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
624 if (($thiz =& new User()) == FALSE)
630 $thiz->lacc = time();
631 $thiz->laccwr = time();
634 $thiz->subst = $subst;
636 $thiz->trans_step = -1;
637 $thiz->comm = array();
638 $thiz->asta_card = -2;
639 $thiz->asta_pnt = -1;
641 $thiz->exitislock = TRUE;
645 $thiz->chattime = array_fill(0, CHAT_N, 0);
647 $thiz->chat_lst = "";
651 $thiz->table = $table;
652 $thiz->table_pos = -1;
653 $thiz->table_token = "";
658 function &clone(&$from)
662 if (($thiz =& new User()) == FALSE)
665 $thiz->name = $from->name;
666 $thiz->sess = $from->sess;
667 $thiz->ip = $from->ip;
668 $thiz->lacc = $from->lacc;
669 $thiz->laccwr = $from->laccwr;
670 $thiz->bantime = $from->bantime;
671 $thiz->stat = $from->stat;
672 $thiz->subst = $from->subst;
673 $thiz->step = $from->step;
674 $thiz->trans_step = $from->trans_step;
675 $thiz->comm = array();
677 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
678 for ($i = $i_start ; $i < $from->step ; $i++) {
680 $thiz->comm[$ii] = $from->comm[$ii];
682 $thiz->asta_card = $from->asta_card;
683 $thiz->asta_pnt = $from->asta_pnt;
684 $thiz->handpt = $from->handpt;
685 $thiz->exitislock = $from->exitislock;
687 $thiz->flags = $from->flags;
689 $thiz->chattime = array();
690 for ($i = 0 ; $i < CHAT_N ; $i++)
691 $thiz->chattime[$i] = $from->chattime[$i];
692 $thiz->chat_cur = $from->chat_cur;
693 $thiz->chat_lst = $from->chat_lst;
694 $thiz->chat_ban = $from->chat_ban;
695 $thiz->chat_dlt = $from->chat_dlt;
697 $thiz->table = $from->table;
698 $thiz->table_pos = $from->table_pos;
699 $thiz->table_token = $from->table_token;
700 $thiz->the_end = $from->the_end;
705 function &spawn(&$from, $table, $table_pos)
709 if (($thiz =& new User()) == FALSE)
712 $thiz->name = $from->name;
713 $thiz->sess = $from->sess;
714 $thiz->ip = $from->ip;
715 $thiz->lacc = $from->lacc;
716 $thiz->laccwr = $from->laccwr;
717 $thiz->bantime = $from->bantime;
718 $thiz->stat = $from->stat;
719 $thiz->subst = $from->subst;
720 $thiz->step = $from->step;
721 $thiz->trans_step = $from->trans_step;
722 $thiz->comm = array();
725 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
726 for ($i = $i_start ; $i < $from->step ; $i++) {
727 log_wr("TRY PUSH:".$i);
729 $thiz->comm[$ii] = $from->comm[$ii];
732 $thiz->asta_card = $from->asta_card;
733 $thiz->asta_pnt = $from->asta_pnt;
734 $thiz->handpt = $from->handpt;
735 $thiz->exitislock = $from->exitislock;
736 $thiz->the_end = $from->the_end;
738 $thiz->flags = $from->flags;
740 $thiz->chattime = array_fill(0, CHAT_N, 0);
742 $thiz->chat_lst = "";
747 $thiz->table = $table;
748 $thiz->table_pos = $table_pos;
749 $thiz->table_token = $from->table_token;
754 function stat_set($stat) {
755 log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]");
756 $this->stat = "$stat";
759 if (validate_sess($this->sess)) {
760 if (file_exists(PROXY_PATH) == FALSE)
762 $fp = fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
763 fwrite($fp, sprintf("%s\n",$this->stat));
769 function step_set($step) {
773 if (validate_sess($this->sess) == FALSE)
775 if (file_exists(PROXY_PATH) == FALSE)
777 if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
779 fwrite($fp, pack("l",$this->step), 4);
788 function step_inc($delta = 1) {
789 $this->step += $delta;
791 if (validate_sess($this->sess)) {
792 if (file_exists(PROXY_PATH) == FALSE)
794 $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
795 fwrite($fp, pack("l",$this->step), 4);
806 log_legal($curtime, $this->sess, $this->name, "STAT:LOGOUT", '');
808 $tmp_sess = $this->sess;
810 step_unproxy($tmp_sess);
811 $this->name = ""; // OK here
812 while (array_pop($this->comm) != NULL);
814 $this->chattime = array_fill(0, CHAT_N, 0);
816 $this->chat_lst = "";
819 $this->the_end = FALSE;
824 function step_get($sess) {
827 if (validate_sess($sess) == FALSE)
830 if (file_exists(PROXY_PATH) == FALSE)
832 if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
834 if (($s = fread($fp, 4)) == FALSE)
838 $arr = unpack('l', $s);
841 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
848 log_rd2("STEP_GET: return false ");
853 function step_unproxy($sess) {
854 log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
855 if (file_exists(PROXY_PATH) == FALSE)
857 @unlink(PROXY_PATH."/".$sess.".step");
864 var $comm; // commands for many people
865 var $step; // current step of the comm array
866 var $garbage_timeout;
870 $this->user = array();
871 $this->table = array();
873 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
874 $this->user[$i] =& User::create("", "");
877 for ($i = 0 ; $i < TABLES_N ; $i++) {
878 $this->table[$i] =& Table::create($i);
879 $row = ( (((int)($i / 4)) % 2) == 0 );
880 $col = ($i % 2 == 0);
881 $this->table[$i]->auth_only = (($row && $col) || (!$row && !$col));
883 $this->garbage_timeout = 0;
886 function garbage_manager($force)
890 log_rd2("garbage_manager START");
892 /* Garbage collector degli utenti in timeout */
894 if ($force || $this->garbage_timeout < $curtime) {
896 // Before all align times with table timeout
897 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
898 $table_cur =& $this->table[$table_idx];
899 // if the table is complete and exists its shared mem we get the info about users lacc
901 if ($table_cur->player_n == PLAYERS_N) {
902 log_main("PLAYERS == N TABLE ".$table_idx);
904 if (($sem = Briskin5::lock_data($table_idx)) != FALSE) {
905 log_main("bin5 lock data success");
907 $no_recovery = FALSE;
908 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
909 if ($table_cur->table_token != $bri->table_token) {
910 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
911 log_main("ERROR: not matching table_start. Room: ".$table_cur->table_start." Table: ".$bri->table_start);
921 log_main("garbage_manager: bri loaded successfully.");
922 $bri->garbage_manager(TRUE);
924 $bri_table = &$bri->table[0];
926 // is the end of the table
927 if ($bri->the_end == TRUE) {
929 * DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN
931 log_main("garbage_manager: INSIDE THE END.");
933 $plist = "$table_cur->table_token|$user_cur->table|$table_cur->player_n";
934 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
935 $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
938 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
939 // stat must be "table" by definition
940 $user_cur =& $this->user[$table_cur->player[$i]];
941 $bri_user =& $bri->user[$i];
943 $user_cur->subst = $bri_user->subst;
944 $user_cur->step = $bri_user->step;
945 $user_cur->lacc = $bri_user->lacc;
946 $user_cur->laccwr = $bri_user->lacc;
947 $user_cur->bantime = $bri_user->bantime;
950 log_legal($curtime, $user_cur->sess, $user_cur->name, "STAT:DESTROY_GAME", $plist);
952 $this->room_join_wakeup(&$user_cur, FALSE, 0);
953 $table_cur->table_token = "";
954 $table_cur->wakeup_time = $curtime + WAKEUP_TIME;
955 Briskin5::destroy_data($table_idx);
958 log_main("gm:: save_data");
960 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
961 $this->user[$table_cur->player[$i]]->lacc = $bri->user[$i]->lacc;
964 Briskin5::save_data(&$bri);
966 } // else if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
967 else if ($no_recovery == FALSE) {
968 log_crit("ERROR: table ".$table_idx." unrecoverable join");
970 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
971 $user_cur = &$this->user[$table_cur->player[$i]];
972 $user_cur->subst = "shutdowner";
973 $user_cur->step_inc();
975 $ret = sprintf('stat = "%s"; subst = "%s";', $user_cur->stat, $user_cur->subst);
976 $ret .= "gst.st = ".($user_cur->step+1)."; ";
977 $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);
978 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
979 $user_cur->step_inc();
982 $plist = "$table_cur->table_token|$user_cur->table|$table_cur->player_n";
983 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
984 $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
986 log_legal($curtime, $user_cur->sess, $user_cur->name, "STAT:DESTROY_GAME(RECOVERY)", $plist);
988 $this->room_join_wakeup(&$user_cur, TRUE, -2);
989 $table_cur->table_token = "";
992 Briskin5::unlock_data($sem);
994 } // if ($table_cur->player_n == PLAYERS_N) {
995 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
997 log_rd2("out new loop.");
999 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1000 $user_cur = &$this->user[$i];
1002 log_rd2("User: ".$user_cur->name." stat: ".$user_cur->stat." subst: ".$user_cur->subst);
1004 if ($user_cur->sess == "")
1007 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
1008 // Auto logout dell'utente
1009 log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
1011 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
1012 log_auth($user_cur->sess, "Autologout session.");
1016 log_rd2("AUTO LOGOUT.");
1017 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
1018 $this->room_wakeup(&$user_cur);
1019 else if ($user_cur->subst == 'standup')
1020 $this->room_outstandup(&$user_cur);
1022 log_rd2("LOGOUT FROM WHAT ???");
1026 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
1027 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
1028 $this->room_wakeup(&$user_cur);
1029 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1030 $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);
1031 $user_cur->step_inc();
1035 log_rd2("GARBAGE UPDATED!");
1037 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
1044 function show_room($user_step, &$user)
1046 log_main("show_room: username: ".$user->name);
1049 $ret = sprintf('gst.st = %d; ', $user_step);
1050 if ($user->subst == 'standup')
1051 $ret .= "tra.show(); ";
1053 $ret .= "tra.hide(); ";
1055 $ret .= sprintf('stat = "%s";', $user->stat);
1057 $ret .= root_wellcome($user);
1058 $ret .= sprintf('subst = "%s";', $user->subst);
1059 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
1060 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
1061 $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', $itin, xcape($user->name), $itou);
1062 for ($i = 0 ; $i < TABLES_N ; $i++) {
1063 $ret .= $this->table_content($user, $i);
1064 // $ret .= table_act_content(($user->subst == 'standup'), $this->table[$i]->player_n, $i, $user->table,
1065 // ($this->table[$i]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
1066 $ret .= $this->table[$i]->act_content($user);
1067 if ($this->table[$i]->wag_own != NULL)
1068 $ret .= sprintf('tra.add(%d, "%s: %s"); ', $i, $this->table[$i]->wag_own->name, $this->table[$i]->wag_com);
1070 $ret .= sprintf('tra.rem(%d); ', $i);
1072 $ret .= $this->standup_content($user);
1078 function room_wakeup(&$user)
1080 $table_idx = $user->table;
1081 $table = &$this->table[$table_idx];
1083 log_main("WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
1087 $from_table = ($user->stat == "table");
1089 log_main("WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1091 for ($i = 0 ; $i < $table->player_n ; $i++) {
1092 $user_cur = &$this->user[$table->player[$i]];
1093 log_main("PREIMPOST: INLOOP name: ".$user_cur->name);
1095 if ($user_cur != $user) {
1096 $user_cur->stat_set("room");
1097 $user_cur->subst = "sitdown";
1098 $user_cur->laccwr = $curtime;
1100 else if ($user->sess != "") {
1101 $user_cur->stat_set("room");
1102 $user_cur->subst = "standup";
1103 $user_cur->laccwr = $curtime;
1104 $user_cur->table = -1;
1109 $user->stat_set("room");
1110 $user->subst = "standup";
1111 $user->laccwr = $curtime;
1114 $remove_wagon = false;
1115 if($table->wag_own == $user) {
1116 $remove_wagon = true;
1117 $table->wag_reset($curtime);
1122 /* aggiorna l'array dei giocatori al tavolo. */
1123 $table->user_rem(&$this, &$user);
1125 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1126 $user_cur = &$this->user[$i];
1127 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1130 log_main("VALORI: name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
1132 $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
1133 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
1134 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; window.onbeforeunload = null; document.location.assign("index.php");|';
1135 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
1136 log_main("DOCUMENT.index.php: from table");
1138 else if ($user_cur->stat == "room") {
1139 log_main("DOCUMENT.index.php: from table");
1141 $ret .= $this->table_content($user_cur, $table_idx);
1142 $ret .= $this->standup_content($user_cur);
1144 // $ret .= table_act_content(FALSE, 0, $table_idx, $user->table, FALSE);
1145 $ret .= $table->act_content($user);
1147 if ($user_cur == $user) {
1148 // set the new status
1149 $ret .= 'subst = "standup"; tra.show(); ';
1150 // clean the action buttons in other tables
1151 for ($e = 0 ; $e < TABLES_N ; $e++) {
1152 if ($this->table[$e]->player_n < PLAYERS_N) {
1153 // $ret .= table_act_content(TRUE, 0, $e, $user->table,
1154 // ($this->table[$e]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
1155 $ret .= $this->table[$e]->act_content($user);
1160 // $ret .= table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table,
1161 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1162 $ret .= $table->act_content($user_cur);
1165 log_wr("ROOM_WAKEUP: ".$ret);
1166 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1167 $user_cur->step_inc();
1171 function room_join_wakeup(&$user, $update_lacc = FALSE, $trans_delta)
1173 $table_idx = $user->table;
1174 $table = &$this->table[$table_idx];
1176 log_main("JOIN_WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
1179 $user_wup = array();
1181 $user_tab = array();
1183 log_main("JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1185 for ($i = 0 ; $i < $table->player_n ; $i++) {
1186 $user_cur = &$this->user[$table->player[$i]];
1187 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1188 if ($user_cur->sess != "") {
1189 if ($update_lacc == TRUE) {
1190 $user_cur->laccwr = $curtime;
1192 log_main("cur: ".$user_cur->name." subst: ".$user_cur->subst);
1193 if ($user_cur->subst == "shutdowned") {
1194 $user_cur->stat_set("room");
1195 $user_cur->subst = "sitdown";
1197 else if ($user_cur->subst == "shutdowner") {
1198 $user_cur->stat_set("room");
1199 $user_cur->subst = "standup";
1200 $user_cur->table = -1;
1201 $user_wup[$user_wup_n++] = &$user_cur;
1203 $remove_wagon = false;
1204 if($table->wag_own == $user_cur) {
1205 $remove_wagon = true;
1206 $table->wag_reset($curtime);
1209 $user_tab[$user_tab_n++] = &$user_cur;
1213 for ($wup_idx = 0 ; $wup_idx < $user_wup_n ; $wup_idx++)
1214 $table->user_rem(&$this, &$user_wup[$wup_idx]);
1216 /* aggiorna l'array dei giocatori al tavolo. */
1218 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1219 log_main("START LOOP");
1220 $user_cur = &$this->user[$i];
1221 if ($user_cur->sess == '' || $user_cur->stat != 'room') {
1222 log_main("name: ".$user_cur->name."skip subst: ".$user_cur->subst);
1227 log_main("VALORI name: ".$user_cur->name." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
1229 $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
1230 if ($user_cur->stat == "room") {
1231 log_main("DOCUMENT.index.php from table");
1233 $ret .= $this->table_content($user_cur, $table_idx);
1234 $ret .= $this->standup_content($user_cur);
1236 // $ret .= table_act_content(FALSE, 0, $table_idx, $user_cur->table,
1237 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1238 $ret .= $table->act_content($user_cur);
1241 for ($tab_idx = 0 ; $tab_idx < $user_tab_n ; $tab_idx++)
1242 if ($user_cur == $user_tab[$tab_idx])
1245 // for users that wakeup the room will be reconstructed by index_rd.php
1246 if ($tab_idx < $user_tab_n) {
1247 log_main("PRE show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1249 // ARRAY_POP DISABLED
1250 // if ($trans_delta == 0)
1251 // while (array_pop($user_cur->comm) != NULL);
1253 $user_cur->trans_step = $user_cur->step + 1 + $trans_delta;
1254 $user_cur->comm[$user_cur->step % COMM_N] = "";
1255 $user_cur->step_inc();
1256 $user_cur->comm[$user_cur->step % COMM_N] = $this->show_room(($user_cur->step + 1), &$user_cur);
1257 $user_cur->step_inc();
1258 log_main("POST show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1262 log_main("JOIN_WAKEUP wup_idx ".$wup_idx." wup_n ".$user_wup_n);
1264 log_main("JOIN_WAKEUP more");
1265 // $ret .= table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table,
1266 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1267 $ret .= $table->act_content($user_cur);
1269 log_main("JOIN_WAKEUP end more");
1271 log_wr("ROOM_JOIN_WAKEUP: ".$ret);
1272 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1273 $user_cur->step_inc();
1277 function room_outstandup(&$user)
1279 $this->room_sitdown(&$user, -1);
1282 function table_update(&$user)
1284 log_main("table_update: pre - USER: ".$user->name);
1286 $table_idx = $user->table;
1288 if ($table_idx > -1)
1289 $table = &$this->table[$table_idx];
1291 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1293 $user_cur = &$this->user[$i];
1294 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1297 $ret = "gst.st = ".($user_cur->step+1)."; ";
1298 if ($table_idx > -1)
1299 $ret .= $this->table_content($user_cur, $table_idx);
1301 if ($user_cur == $user) {
1302 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
1303 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
1304 $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>: ";', $itin, xcape($user->name), $itou);
1306 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1307 $user_cur->step_inc();
1310 log_main("table_update: post");
1313 function room_sitdown(&$user, $table_idx)
1315 log_main("room_sitdown ".($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
1319 if ($table_idx > -1 && $table_idx < TABLES_N) {
1320 $table = &$this->table[$table_idx];
1323 if ($table->wag_own != NULL && $table->player_n == PLAYERS_N) {
1324 for ($i = 0 ; $i < TABLES_N ; $i++) {
1325 $user_cur =& $this->user[$table->player[$i]];
1326 if ($user_cur == $table->wag_own) {
1327 $train_app = sprintf("tra.rem(%d); ", $table_idx);
1328 $table->wag_reset(time());
1335 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1337 $user_cur = &$this->user[$i];
1338 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1341 $ret = "gst.st = ".($user_cur->step+1)."; ".$train_app;
1342 if ($table_idx > -1)
1343 $ret .= $this->table_content($user_cur, $table_idx);
1344 $ret .= $this->standup_content($user_cur);
1346 if ($user_cur == $user) {
1347 $ret .= 'subst = "sitdown"; tra.hide(); ';
1348 // clean the action buttons in other tables
1349 for ($e = 0 ; $e < TABLES_N ; $e++) {
1350 // $ret .= table_act_content(FALSE, 0, $e, $user_cur->table, FALSE);
1351 $ret .= $this->table[$e]->act_content($user_cur);
1354 else if ($table_idx > -1) {
1355 if ($table->player_n == PLAYERS_N) {
1356 // $ret .= table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table,
1357 /// ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1358 $ret .= $table->act_content($user_cur);
1361 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1362 $user_cur->step_inc();
1366 function chatt_send(&$user, $mesg)
1368 GLOBAL $G_alarm_passwd;
1372 $msg = substr($mesg, 6, 128);
1374 $dt = date("H:i ", $curtime);
1384 $update_room = FALSE;
1386 if (strcmp($msg, "/tav") == 0 ||
1387 strncmp($msg, "/tav ", 5) == 0) {
1389 if ($user->stat != 'room' || $user->subst != 'sitdown') {
1390 $msg = "<br>Per attivare il messaggio di segnalazione del tavolo occorre essere seduti.<br><br>";
1391 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1396 $table = &$this->table[$user->table];
1398 if ($table->wag_own != NULL) {
1399 $msg = "<br>Il messaggio di segnalazione del tavolo è già attivato.<br><br>";
1400 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1405 $dtime = $curtime - $table->wag_tout;
1406 if ($dtime < EXPIRE_TIME_WAG) {
1407 $msg = sprintf("<br>Il messaggio di segnalazione del tavolo<br>è disattivato ancora per %d second%s.<br><br>",
1408 EXPIRE_TIME_WAG - $dtime, (EXPIRE_TIME_WAG - $dtime == 1 ? "o" : "i"));
1409 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1414 $msg = substr($msg, 5);
1416 $table->wag_set($user, $msg);
1417 $to_user = sprintf('tra.add(%d, "%s");', $user->table, xcape(sprintf("%s: %s", $user->name, $msg)));
1418 $to_room = $to_user;
1421 } // /tav chat command
1423 else if (strncmp($msg, "/alarm ", 7) == 0) {
1424 if (strncmp($msg, "/alarm to ", 10) == 0) {
1425 $sp_pos = strpos($msg, " ", 10);
1426 $target = substr($msg, 10, $sp_pos - 10);
1427 $alarm_check = "/alarm to ".$target." ".$G_alarm_passwd." ";
1431 $alarm_check = "/alarm ".$G_alarm_passwd." ";
1434 if (strncmp($msg, $alarm_check, strlen($alarm_check)) != 0) {
1435 $msg = "<br>La password digitata non è corretta.<br><br>";
1436 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1441 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"Alarm <b>%s</b> inviato a <b>%s</b>.");',
1442 $dt, NICKSERV, xcape(substr($msg, strlen($alarm_check))),
1443 ($target == "" ? "tutti" : xcape($target)) );
1445 $msg = sprintf("<br><b>%s<br><br>%s</b><br><br>",
1446 $dt.NICKSERV, xcape(substr($msg, strlen($alarm_check))));
1447 $to_all = show_notify($msg, 0, "chiudi", 400, 120);
1449 } // /alarm chat command
1451 else if (strncmp($msg, "/nick ", 6) == 0) {
1452 log_main("chatt_send BEGIN");
1455 if (($name_new = validate_name(substr($msg, 6))) == FALSE) {
1456 $to_user = sprintf('chatt_sub("%s", [2,"%s"],"Il nickname deve contenere almeno una lettera dell\'alfabeto o una cifra.");', $dt, NICKSERV);
1460 $msg = "COMMAND ".$msg;
1461 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1462 $user_cur = &$this->user[$i];
1464 if ($user_cur->sess == '')
1466 if ($user_cur->name == $name_new)
1469 if ($i < MAX_PLAYERS) {
1470 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"Nickname <b>%s</b> già in uso.");', $dt, NICKSERV, xcape($name_new));
1474 if ($user->flags & USER_FLAG_AUTH) {
1475 if (strcasecmp($user->name,$name_new) != 0) {
1476 if ($user->subst == 'standup' || ($user->subst != 'standup' && $this->table[$user->table]->auth_only == FALSE)) {
1477 $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
1478 for ($i = 0 ; $i < TABLES_N ; $i++) {
1479 $to_user .= $this->table[$i]->act_content($user);
1483 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"<b>Non puoi cambiare nick a un tavolo per soli autenticati.</b>");', $dt, NICKSERV);
1488 $user->name = $name_new; // OK - nick changed
1490 /* se nome gia' in uso, segnala cosa potrebbe capitare */
1491 if (($user->flags & USER_FLAG_AUTH) == 0) {
1492 $userdb = new LoginDB();
1493 if ($userdb->login_exists($name_new)) {
1494 $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));
1498 log_main("chatt_send start set");
1500 $update_room = TRUE;
1502 } // nick chat command
1504 else { // normal chat line
1505 if ($curtime < ($user->chat_ban + $user->chat_dlt)) {
1507 $user->chat_dlt = $user->chat_dlt * 2;
1508 if ($user->chat_dlt > 120)
1509 $user->chat_dlt = 120;
1511 else if ($user->chat_lst == $msg)
1513 else if ($curtime - $user->chattime[($user->chat_cur + 1) % CHAT_N] < CHAT_ILL_TIME) {
1514 $user->chat_ban = $curtime;
1515 $user->chat_dlt = 5;
1519 $user->chat_ban = 0;
1520 $user->chat_dlt = 0;
1524 $to_user = sprintf('chatt_sub("%s", [%d, "%s"],"%s");', $dt, $user->flags, xcape($user->name), xcape("== chat ban =="));
1527 $to_user = sprintf('chatt_sub("%s", [%d, "%s"],"%s");', $dt, $user->flags, xcape($user->name), xcape($msg));
1528 // temporary silentiation for troll (will became array check)
1529 if (strcasecmp($user->name,'JackRokka') != 0 && $user->sess != '47ea653f602e8')
1530 $to_room = $to_user;
1533 log_legal($curtime, $user->sess, $user->name,
1534 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$msg);
1536 $user->chat_lst = "$msg";
1537 $user->chattime[$user->chat_cur % CHAT_N] = $curtime;
1547 // Output to clients
1550 if ($to_user != FALSE) {
1551 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1552 $user->comm[$user->step % COMM_N] .= $to_user;
1556 if ($to_room != FALSE) {
1557 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1558 $user_cur = &$this->user[$i];
1559 if ($target != "" && $user_cur->name != $target)
1561 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1562 if ($user_cur->sess == '' || $user_cur->stat == 'table' || $user_cur == $user)
1565 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1566 $user_cur->comm[$user_cur->step % COMM_N] .= $to_room;
1567 $user_cur->step_inc();
1572 // Before all align times with table timeout
1573 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1574 $table_cur =& $this->table[$table_idx];
1575 // if the table is complete and exists its shared mem we get the info about users lacc
1577 if ($table_cur->player_n == PLAYERS_N) {
1578 log_main("PLAYERS == N TABLE ".$table_idx);
1580 if (($sem = Briskin5::lock_data($table_idx)) != FALSE) {
1581 log_main("bin5 lock data success");
1583 $no_recovery = FALSE;
1584 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
1585 if ($table_cur->table_token != $bri->table_token) {
1586 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
1591 if ($bri != FALSE) {
1592 $bri_table = &$bri->table[0];
1593 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
1594 // stat must be "table" by definition
1595 $bri_user =& $bri->user[$i];
1597 if ($target != "" && $bri_user->name != $target)
1599 log_main("writa: ".$user_mesg);
1600 $bri_user->comm[$bri_user->step % COMM_N] = "gst.st = ".($bri_user->step+1)."; ";
1601 $bri_user->comm[$bri_user->step % COMM_N] .= $to_tabl;
1602 $bri_user->step_inc();
1604 Briskin5::save_data(&$bri);
1606 Briskin5::unlock_data($sem);
1608 } // if ($table_cur->player_n == PLAYERS_N) {
1609 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1610 } // if ($to_tabl == true ...
1613 if ($user->stat == 'room' && $user->subst == 'standup') {
1614 $this->standup_update(&$user);
1616 else if ($user->stat == 'room' && $user->subst == 'sitdown') {
1617 log_main("chatt_send pre table update");
1618 $this->table_update(&$user);
1619 log_main("chatt_send post table update");
1621 } // if ($update_room ...
1624 } // function chatt_send( ...
1626 function &get_user($sess, &$idx)
1628 GLOBAL $PHP_SELF, $G_false;
1630 if (validate_sess($sess)) {
1631 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1632 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1635 $ret = &$this->user[$i];
1639 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1640 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
1641 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1644 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1653 * function &add_user(&$room, &$sess, &$idx, $name, $pass, $ip)
1656 * if ($idx > -1 && ret == FALSE) => duplicated nick
1657 * if ($idx == -2 && ret == FALSE) => invalid name
1658 * if ($idx == -3 && ret == FALSE) => wrong password
1659 * if ($idx == -1 && ret == FALSE) => no space left
1660 * if ($idx == 0 && ret == user) => SUCCESS
1661 * if ($idx == -$idx && ret == user) => SUCCESS (but the login exists in the auth db
1666 function &add_user(&$sess, &$idx, $name, $pass, $ip)
1672 $authenticate = FALSE;
1673 $login_exists = FALSE;
1675 $ghost_auth = FALSE;
1679 if (($name_new = validate_name($name)) == FALSE) {
1684 log_auth("XXX", sprintf("ARRIVA: [%s] pass:[%s]", $sess, ($pass == FALSE ? "FALSE" : $pass)));
1685 if (validate_sess($sess) == FALSE)
1688 /* if pass != FALSE verify the login with pass */
1689 log_auth("XXX", "auth1");
1690 $userdb = new LoginDB();
1691 if ($pass != FALSE) {
1692 log_auth("XXX", "auth2");
1693 $authenticate = $userdb->login_verify($name_new, $pass);
1694 log_auth("XXX", "authenticate: ".($authenticate == TRUE ? "TRUE" : "FALSE"));
1696 if ($authenticate == FALSE) {
1702 $login_exists = $userdb->login_exists($name_new);
1704 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1706 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1710 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
1712 continue; // NOTE: CHECK IT !!
1714 if (strcasecmp($this->user[$i]->name, $name_new) == 0) {
1715 if ($authenticate == FALSE) {
1721 $ghost_auth = ($this->user[$i]->flags & USER_FLAG_AUTH);
1728 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1730 if ($ghost > -1 && $authenticate) {
1733 $ghost_user =& $this->user[$ghost];
1735 $ghost_user->step_inc();
1738 $ghost_user->sess = $sess;
1741 $ghost_user->sess = $sess;
1744 // If user at the table we need to update the table data too
1745 $table_idx = $ghost_user->table;
1746 if ($ghost_user->stat == "table" && $this->table[$table_idx]->player_n == PLAYERS_N) {
1747 if (($brisem = Briskin5::lock_data($table_idx)) != FALSE) {
1748 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
1749 if ($bri->the_end != TRUE) {
1750 $bri->user[$ghost_user->table_pos]->step_inc();
1751 $bri->user[$ghost_user->table_pos]->sess = $sess;
1752 Briskin5::save_data(&$bri);
1755 Briskin5::unlock_data($brisem);
1760 return ($this->user[$ghost]);
1762 else if ($idx != -1 && $i == MAX_PLAYERS) {
1767 $this->user[$idx]->sess = $sess;
1770 $this->user[$idx]->sess = $sess;
1772 $this->user[$idx]->name = $name_new; // OK - add new user
1773 $this->user[$idx]->stat_set("room");
1774 $this->user[$idx]->step_set(0);
1775 while (array_pop($this->user[$idx]->comm) != NULL);
1776 $this->user[$idx]->subst = "standup";
1777 $this->user[$idx]->lacc = $curtime;
1778 $this->user[$idx]->laccwr = $curtime;
1779 $this->user[$idx]->bantime = 0;
1780 $this->user[$idx]->ip = $ip;
1782 $this->user[$idx]->flags = ($authenticate ? USER_FLAG_AUTH : 0x00);
1785 log_main("ghost: rename!");
1786 $ghost_user =& $this->user[$ghost];
1788 if ($ghost_auth == FALSE) {
1789 for ($sfx = 1 ; $sfx <= MAX_PLAYERS ; $sfx++) {
1790 $ghostname = 'ghost'.$sfx;
1791 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1792 if (strcmp("", $this->user[$i]->sess) == 0)
1795 if (strcmp($this->user[$i]->name, $ghostname) == 0) {
1800 if ($ghostname != '')
1804 $ghost_user->name = $ghostname;
1806 if ($ghost_user->stat == 'room' && $ghost_user->subst == 'standup') {
1807 $this->standup_update(&$ghost_user);
1810 log_main("chatt_send pre table update");
1811 $this->table_update(&$ghost_user);
1812 log_main("chatt_send post table update");
1814 } // if ($ghost_auth == FALSE
1816 // FIXME: cacciare il vecchio utente room && table (if needed)
1817 $ghost_user->the_end = TRUE;
1818 $ghost_user->lacc = 0;
1819 $this->garbage_manager(TRUE);
1821 } // if ($ghost > -1) {
1823 log_main(sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1827 return ($this->user[$real_idx]);
1833 function standup_update(&$user)
1835 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1836 $user_cur = &$this->user[$i];
1837 if ($user_cur->sess == '')
1840 log_main("STANDUP START: ".$user_cur->stat);
1842 if ($user_cur->stat == 'room') {
1843 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".$this->standup_content($user_cur);
1844 if ($user_cur == $user) {
1845 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
1846 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
1848 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>: ";',
1849 $itin, xcape($user->name), $itou);
1851 log_main("FROM STANDUP: NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
1853 $user_cur->step_inc();
1859 function &init_data()
1861 $room =& new Room();
1867 function &load_data()
1869 GLOBAL $G_false, $sess;
1872 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1873 log_main("ftok failed");
1878 if (($shm_sz = sharedmem_sz($tok)) == -1) {
1879 log_main("shmop_open failed");
1883 $shm_sz = SHM_DIMS_MIN;
1885 if ($shm = shm_attach($tok, $shm_sz)) {
1886 $room = @shm_get_var($shm, $tok);
1888 log_only("bri == ".($room == FALSE ? "FALSE" : "TRUE")." bri === ".($room === FALSE ? "FALSE" : "TRUE")." bri isset ".(isset($room) ? "TRUE" : "FALSE"));
1890 log_only("bri count ".count($room));
1892 if ($room == FALSE) {
1893 log_only("INIT MAIN DATA");
1895 $room =& Room::init_data();
1896 if (@shm_put_var($shm, $tok, $room) == FALSE) {
1897 log_only("PUT_VAR FALLITA ".strlen(serialize($room)));
1898 log_only(serialize($room));
1901 $room->shm_sz = $shm_sz;
1916 function save_data(&$room)
1924 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1)
1927 while ($room->shm_sz < SHM_DIMS_MAX) {
1928 if (($shm = shm_attach($tok, $room->shm_sz)) == FALSE)
1931 // log_only("PUT_VAR DI ".strlen(serialize($room)));
1932 if (shm_put_var($shm, $tok, $room) != FALSE) {
1936 if (shm_remove($shm) === FALSE) {
1937 log_only("REMOVE FALLITA");
1941 $room->shm_sz += SHM_DIMS_DLT;
1950 function lock_data()
1954 // echo "LOCK: ".FTOK_PATH."/main";
1956 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1960 // echo "FTOK ".$tok."<br>";
1961 if (($res = sem_get($tok)) == FALSE) {
1962 echo "SEM_GET FAILED";
1965 if (sem_acquire($res)) {
1966 log_lock("LOCK room");
1973 function unlock_data($res)
1977 log_lock("UNLOCK room");
1979 return (sem_release($res));
1983 function standup_content($user)
1988 if ($user->stat != 'room')
1991 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
1992 if ($this->user[$e]->sess == "" || $this->user[$e]->stat != "room" || $this->user[$e]->name == "")
1997 // $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
1999 $content = ' j_stand_cont( [ ';
2001 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
2002 if ($this->user[$e]->sess == "" || $this->user[$e]->stat != "room" || $this->user[$e]->name == "")
2005 $flags = $this->user[$e]->flags;
2007 if ($this->user[$e]->subst == "standup") {
2008 if ($this->user[$e] == $user)
2013 $content .= sprintf('%s[ %d, \'%s\' ]',($ct > 0 ? ', ' : ''), $flags, xcape($this->user[$e]->name));
2022 function table_content_old($user, $table_idx)
2028 // Si possono usare i dati nella classe table
2031 $sess = $user->sess;
2032 $table = &$this->table[$table_idx];
2034 if ($user->stat != 'room')
2037 for ($i = 0 ; $i < $table->player_n ; $i++) {
2038 $user_cur = &$this->user[$table->player[$i]];
2040 if ($user_cur == $user)
2041 { $hilion = "<b>"; $hilioff = "</b>"; }
2043 { $hilion = ""; $hilioff = ""; }
2045 log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
2047 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
2050 for ( ; $i < PLAYERS_N ; $i++)
2054 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
2060 function table_content($user, $table_idx)
2066 // Si possono usare i dati nella classe table
2069 $sess = $user->sess;
2070 $table = &$this->table[$table_idx];
2072 if ($user->stat != 'room')
2076 for ($i = 0 ; $i < $table->player_n ; $i++) {
2077 $user_cur = &$this->user[$table->player[$i]];
2079 $flags = $user_cur->flags;
2081 if ($user_cur == $user)
2084 log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
2086 $content .= sprintf('%s[ %d, \'%s\' ]',($i == 0 ? '' : ', '), $flags, xcape($user_cur->name));
2091 $ret .= sprintf('j_tab_cont(%d, %s);', $table_idx, $content);
2098 function make_seed()
2100 list($usec, $sec) = explode(' ', microtime());
2101 return (float) $sec + ((float) $usec * 100000);
2105 function log_only2($log) {
2108 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONL2) == 0)
2111 if (isset($sess) == FALSE)
2116 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONL2) == 0)
2119 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2120 fwrite($fp, sprintf("ONL2: [%s] [%s]\n", $ssess, $log));
2125 function log_crit($log) {
2128 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_CRIT) == 0)
2131 if (isset($sess) == FALSE)
2136 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_CRIT) == 0)
2139 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2140 fwrite($fp, sprintf("CRIT: [%s] [%s]\n", $ssess, $log));
2145 function log_only($log) {
2148 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONLY) == 0)
2151 if (isset($sess) == FALSE)
2156 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONLY) == 0)
2159 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2160 fwrite($fp, sprintf("ONLY: [%s] [%s]\n", $ssess, $log));
2165 function log_main($log) {
2168 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_MAIN) == 0)
2171 if (isset($sess) == FALSE)
2176 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_MAIN) == 0)
2179 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2180 fwrite($fp, sprintf("MAIN: [%s] [%s]\n", $ssess, $log));
2185 function log_rd($log) {
2188 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_READ) == 0)
2191 if (isset($sess) == FALSE)
2196 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_READ) == 0)
2199 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2200 fwrite($fp, sprintf("READ: [%s] [%s]\n", $ssess, $log));
2205 function log_rd2($log) {
2208 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_REA2) == 0)
2211 if (isset($sess) == FALSE)
2216 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_REA2) == 0)
2219 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2220 fwrite($fp, sprintf("REA2: [%s] [%s]\n", $ssess, $log));
2225 function log_send($log) {
2228 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SEND) == 0)
2231 if (isset($sess) == FALSE)
2236 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SEND) == 0)
2239 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2240 fwrite($fp, sprintf("SEND: [%s] [%s]\n", $ssess, $log));
2245 function log_lock($log) {
2248 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOCK) == 0)
2251 if (isset($sess) == FALSE)
2256 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOCK) == 0)
2259 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2260 fwrite($fp, sprintf("LOCK: [%s] [%s]\n", $ssess, $log));
2265 function log_wr($log) {
2268 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_WRIT) == 0)
2271 if (isset($sess) == FALSE)
2276 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_WRIT) == 0)
2279 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2280 fwrite($fp, sprintf("WRIT: [%s] [%s]\n", $ssess, $log));
2285 function log_load($log) {
2288 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOAD) == 0)
2291 if (isset($sess) == FALSE)
2296 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOAD) == 0)
2299 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2300 fwrite($fp, sprintf("LOAD: [%s] [%s]\n", $ssess, $log));
2305 function log_auth($sess, $log) {
2306 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_AUTH) == 0)
2309 if (( (BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_AUTH) == 0)
2312 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2313 fwrite($fp, sprintf("AUTH: [%s] [%d] [%s]\n", $sess, time(), $log));
2319 function log_legal($curtime, $sess, $name, $where, $mesg)
2323 if (($fp = @fopen(LEGAL_PATH."/legal.log", 'a')) != FALSE) {
2324 /* Unix time | session | nickname | IP | where was | mesg */
2325 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|\n", $curtime, $sess, $name, $_SERVER['REMOTE_ADDR'], $where , $mesg));
2333 function lock_banlist()
2335 if (($tok = @ftok(FTOK_PATH."/main", "L")) == -1) {
2339 if (($res = sem_get($tok)) == FALSE) {
2340 echo "SEM_GET FAILED";
2343 if (sem_acquire($res))
2349 function unlock_banlist($res)
2351 return (sem_release($res));
2354 function table_act_content_old($isstanding, $sitted, $table, $cur_table)
2359 if ($sitted < PLAYERS_N) {
2360 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
2364 if ($table == $cur_table)
2365 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
2372 function table_act_content($isstanding, $sitted, $table, $cur_table, $allowed)
2377 if ($sitted < PLAYERS_N) {
2385 if ($table == $cur_table)
2392 $ret = sprintf('j_tab_act_cont(%d, \'%s\');', $table, $act);
2397 function show_notify($text, $tout, $butt, $w, $h)
2399 log_main("SHOW_NOTIFY: ".$text);
2400 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
2403 function briscola_show($room, $table, $user)
2408 if ($table->asta_card == 9)
2409 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
2411 /* text of caller cell */
2412 if ($user->table_pos == $table->asta_win)
2413 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
2415 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
2416 xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
2418 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
2420 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
2421 $ret .= sprintf('$("caller").style.visibility = "visible";');
2422 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
2423 $ret .= sprintf('$("astalascio").style.visibility = "";');
2424 $ret .= sprintf('$("asta").style.visibility = "hidden";');
2425 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
2431 function game_result($asta_pnt, $pnt)
2433 if ($asta_pnt == 61) {
2436 else if ($pnt == 60)
2442 if ($pnt >= $asta_pnt)
2449 function multoval($mult)
2453 else if ($mult == 4)
2454 return ("quadruplo");
2456 return (sprintf("%d-plo", $mult));
2459 function show_table_info(&$room, &$table, $table_pos)
2462 $user = &$room->user[$table->player[$table_pos]];
2464 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
2465 $noty = sprintf('<table class=\"points\"><tr><th></th>');
2468 for ($i = 0 ; $i < PLAYERS_N ; $i++)
2469 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
2470 $noty .= sprintf("</tr>");
2473 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
2475 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
2476 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
2477 for ($e = 0 ; $e < PLAYERS_N ; $e++)
2478 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
2483 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
2484 for ($e = 0 ; $e < PLAYERS_N ; $e++)
2485 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
2486 $noty .= "</tr></table>";
2488 if ($table->old_reason != "") {
2489 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
2492 if ($table->old_win != -1) {
2493 $win = $table->player[$table->old_win];
2494 $fri = $table->player[$table->old_friend];
2496 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
2499 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
2500 xcape($room->user[$win]->name),
2501 xcape($room->user[$fri]->name));
2502 if ($table->old_pnt == 120) {
2503 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
2506 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
2507 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
2508 'più di 60'), $table->old_pnt,
2509 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
2513 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
2514 xcape($room->user[$win]->name));
2515 if ($table->old_pnt == 120) {
2516 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
2519 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
2520 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
2521 'più di 60'), $table->old_pnt,
2522 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
2526 if ($table->mazzo == $table_pos)
2527 $noty .= "Fai <b>tu</b> il mazzo,";
2529 $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
2530 $noty .= "Il mazzo a <b>$unam</b>,";
2533 if ($user->subst == 'asta') {
2534 if ($table->asta_win == -1) // auction case
2535 $curplayer = $table->gstart % PLAYERS_N;
2537 $curplayer = $table->asta_win;
2539 else if ($user->subst == 'game') {
2540 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
2544 if ($curplayer == $table_pos) {
2545 $noty .= " tocca a <b>te</b> giocare.";
2548 $unam = xcape($room->user[$table->player[$curplayer]]->name);
2549 $noty .= " tocca a <b>$unam</b> giocare.";
2552 if ($table->mult > 1) {
2553 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
2555 $noty .= "<hr><br>";
2556 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
2557 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
2558 * 130 + ($table->points_n > 0 ? 50 : 0) +
2559 * (120 * ($table->points_n / MAX_POINTS)));
2560 * will be used when we refact notify js function following
2561 * photoo class logic
2567 function root_wellcome($user)
2569 GLOBAL $root_wellarr;
2573 $dt = date("H:i ", $curtime);
2575 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
2576 $ret .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, str_replace('"', '\"', $root_wellarr[$i]));
2581 function table_wellcome($user)
2583 GLOBAL $table_wellarr;
2586 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
2587 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$i]));
2595 is_transition (is from room to table ?)
2596 is_again (is another game)
2598 Examples of $is_transition, $is_again:
2599 from reload of the page: FALSE, FALSE
2600 from sitdown in room: TRUE, FALSE
2601 from table: asta cmd e tutti passano: TRUE, TRUE
2602 from table: fine partita: TRUE, TRUE
2604 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
2606 $table_idx = $user->table;
2607 $table = &$room->table[$table_idx];
2608 $table_pos = $user->table_pos;
2610 $ret = "table_init();";
2611 $ret .= $table->exitlock_show(&$room->user, $table_pos);
2613 /* GENERAL STATUS */
2614 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
2615 $sendstep, $user->stat, $user->subst, $table_pos);
2617 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
2618 $sendstep, $user->stat, $user->subst, $table_pos));
2621 $ret .= "background_set();";
2624 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
2625 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
2627 $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', $itin, xcape($user->name), $itou);
2628 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
2629 $room->user[$table->player[($table_pos)%PLAYERS_N]]->flags,
2630 xcape($room->user[$table->player[($table_pos)%PLAYERS_N]]->name),
2632 $room->user[$table->player[($table_pos+1)%PLAYERS_N]]->flags,
2633 xcape($room->user[$table->player[($table_pos+1)%PLAYERS_N]]->name),
2635 $room->user[$table->player[($table_pos+2)%PLAYERS_N]]->flags,
2636 xcape($room->user[$table->player[($table_pos+2)%PLAYERS_N]]->name),
2638 (PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+3)%PLAYERS_N]]->flags),
2639 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+3)%PLAYERS_N]]->name)),
2641 (PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+4)%PLAYERS_N]]->flags),
2642 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+4)%PLAYERS_N]]->name)));
2644 /* NOTIFY FOR THE CARD MAKER */
2645 if ($is_transition) { // && $user->subst == "asta" superfluo
2646 $ret .= show_table_info(&$room, &$table, $table_pos);
2649 $ret .= table_wellcome($user);
2651 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
2652 $ret .= playsound("cow.mp3");
2657 if ($is_transition) { // && $user->subst == "asta" superfluo
2660 for ($i = 0 ; $i < 8 ; $i++) {
2661 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
2663 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
2664 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
2670 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
2672 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
2673 $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
2674 $table->card[$o]->value : -1),
2675 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
2680 $taked = array(0,0,0,0,0);
2681 $inhand = array(0,0,0,0,0);
2682 $ontabl = array(-1,-1,-1,-1,-1);
2685 for ($i = 0 ; $i < 40 ; $i++) {
2686 if ($table->card[$i]->stat == 'hand') {
2687 if ($table->card[$i]->owner == $table_pos) {
2688 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
2690 $inhand[$table->card[$i]->owner]++;
2692 else if ($table->card[$i]->stat == 'take') {
2693 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
2694 $taked[$table->card[$i]->owner]++;
2696 else if ($table->card[$i]->stat == 'table') {
2697 $ontabl[$table->card[$i]->owner] = $i;
2701 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2702 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
2704 log_main("Stat table: ".$logg);
2706 /* Set ours cards. */
2708 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
2709 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
2710 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
2711 $oursarg .= ($i == 0 ? "" : ", ")."-1";
2712 $ret .= sprintf('card_setours(%s);', $oursarg);
2714 /* Dispose all cards */
2715 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2716 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
2717 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
2718 $inhand[$i], $taked[$i]);
2720 if ($ontabl[$i] != -1) {
2721 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
2722 $table->card[$ontabl[$i]]->value,
2723 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
2729 if ($user->subst == 'asta') {
2731 /* show users auction status */
2733 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2734 $user_cur = &$room->user[$table->player[$i]];
2735 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
2736 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
2739 $showst .= ",-2,-2";
2740 $ret .= sprintf('show_astat(%s);', $showst);
2742 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
2743 /* show card chooser */
2744 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
2749 if ($table_pos == ($table->gstart % PLAYERS_N) &&
2750 $table->asta_win == -1)
2751 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2752 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
2754 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2755 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
2759 if ($table->asta_win == -1) { // auction case
2760 if ($table_pos == ($table->gstart % PLAYERS_N))
2761 $ret .= "remark_on();";
2763 $ret .= "remark_off();";
2765 else { // chooseed case
2766 if ($table_pos == $table->asta_win)
2767 $ret .= "remark_on();";
2769 $ret .= "remark_off();";
2772 else if ($user->subst == 'game') {
2774 if (($table->gstart + $table->turn) % PLAYERS_N == $table_pos)
2775 $ret .= "is_my_time = true; remark_on();";
2777 $ret .= "remark_off();";
2779 /* WHO CALL AND WATH */
2780 $ret .= briscola_show($room, $table, $user);
2784 } // end function show_table(...
2786 function calculate_winner(&$table)
2793 $cur_seed = $table->briscola - ($table->briscola % 10);
2795 for ($i = 0 ; $i < 40 ; $i++) {
2796 if ($table->card[$i]->stat != "table")
2799 log_wr(sprintf("Card On table: [%d]", $i));
2801 $v = $table->card[$i]->value;
2802 $ontab[$table->card[$i]->owner] = $v;
2803 $ontid[$table->card[$i]->owner] = $i;
2804 /* se briscola setto il flag */
2805 if (($v - ($v % 10)) == $cur_seed)
2809 if ($briontab == FALSE) {
2810 $cur_win = $table->gstart;
2811 $cur_val = $ontab[$cur_win];
2812 $cur_seed = $cur_val - ($cur_val % 10);
2815 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2816 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
2817 if ($ontab[$i] < $cur_val) {
2818 $cur_val = $ontab[$i];
2824 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2825 $table->card[$ontid[$i]]->owner = $cur_win;
2826 $table->card[$ontid[$i]]->stat = "take"; // Card stat
2831 function calculate_points(&$table)
2833 GLOBAL $G_all_points;
2837 if ($table->asta_pnt == 60)
2838 $table->asta_pnt = 61;
2840 $table->old_reason = "";
2841 $table->old_win = $table->asta_win;
2842 $table->old_friend = $table->friend;
2843 $table->old_asta_pnt = $table->asta_pnt;
2845 for ($i = 0 ; $i < 40 ; $i++) {
2846 $ctt = $table->card[$i]->value % 10;
2847 $own = $table->card[$i]->owner;
2848 if ($own == $table->asta_win || $own == $table->friend)
2849 $pro += $G_all_points[$ctt];
2852 log_wr(sprintf("PRO: [%d]", $pro));
2855 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
2856 $table->points[$table->points_n % MAX_POINTS] = array();
2857 for ($i = 0 ; $i < PLAYERS_N ; $i++)
2858 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
2860 $table->old_pnt = $pro;
2866 if ($pro >= $table->asta_pnt)
2871 $table->points[$table->points_n % MAX_POINTS] = array();
2872 for ($i = 0 ; $i < 5 ; $i++) {
2873 if ($i == $table->asta_win)
2874 $pt = ($i == $table->friend ? 4 : 2);
2875 else if ($i == $table->friend)
2880 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
2882 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
2884 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
2886 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
2887 $table->total[$i] += $pt;
2890 $table->old_pnt = $pro;
2894 function validate_sess($sess)
2896 if (strlen($sess) == SESS_LEN)
2902 function validate_name($name)
2904 $name_new = str_replace(' ', '_', substr(trim($name),0,12));
2906 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
2908 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
2915 function playsound($filename)
2917 return (sprintf('playsound("flasou", "%s");', $filename));
2920 function secstoword($secs)
2922 $mins = floor($secs / 60);
2925 $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
2928 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));
2933 function sharedmem_sz($tok)
2935 if (($shm_id = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
2936 log_main("shmop_open failed");
2939 $shm_sz = shmop_size($shm_id);
2940 shmop_close($shm_id);
2942 log_main("shm_sz: ".$shm_sz." SHM_DIMS: ".SHM_DIMS);