5 * Copyright (C) 2006-2007 matteo.nastasi@milug.org
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details. You should have received a
16 * copy of the GNU General Public License along with this program; if
17 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
18 * Suite 330, Boston, MA 02111-1307, USA.
22 define(FTOK_PATH, "/var/lib/brisk");
23 define(LEGAL_PATH, "/tmp/legal_brisk");
24 define(PROXY_PATH, "/var/lib/brisk_proxy");
27 define(MAX_POINTS, 5);
28 define(MAX_PLAYERS, (20 + (PLAYERS_N * TABLES_N)));
29 define(SHM_DIMS, (50000 + 10000 * TABLES_N + 15000 * MAX_PLAYERS));
31 define(COMM_GEN_N, 50);
33 define(STREAM_TIMEOUT, 20);
34 define(EXPIRE_TIME_RD, 180);
35 define(EXPIRE_TIME_SMAMMA, 360);
36 // BAN_TIME da allineare anche in commons.js
37 define(BAN_TIME, 900);
38 define(GARBAGE_TIMEOUT, 10);
39 define(NICKSERV, "<i>SERVER</i>");
41 define(DBG_ONL2, 0x0001);
42 define(DBG_ONLY, 0x0002);
43 define(DBG_MAIN, 0x0004);
44 define(DBG_READ, 0x0008);
45 define(DBG_REA2, 0x0010);
46 define(DBG_SEND, 0x0020);
47 define(DBG_LOCK, 0x0040);
48 define(DBG_WRIT, 0x0080);
49 define(DBG_LOAD, 0x0100);
50 define(DBG_AUTH, 0x0200);
52 define(BRISK_DEBUG, 0);
54 define(BRISK_SINGLE_DEBUG,0);
55 define(BRISK_SINGLE_SESS, "");
56 // define(DEBUGGING, "local");
60 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
61 $G_brisk_version = "spawn-0.9.1";
63 $root_wellarr = Array ( 'Benvenuto in brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: chiuso il <b>bug</b> dei <b>tavoli bianchi</b>, versione beta.',
64 'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
65 $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.');
69 <div style=\\"text-align: left; padding: 8px;\\">
70 <b>Descrizione</b><br>
71 Questa è un\'implementazione della briscola in cinque, così come è spiegata su
72 <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>
73 <b>Configurazione del browser.</b><br>
74 Occorre abilitare i cookies.<br>
76 <b>Uso del sito</b><br>
77 Potete sedervi a un tavolo o rimanere in piedi.<br>
78 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
81 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>
82 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
83 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
84 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
85 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
86 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.
88 <dt><b>Comandi della chat</b>
89 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
90 <dd>.. to be continue ..
96 <div id=\\"header\\" class=\\"header\\">
97 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
98 briscola chiamata in salsa ajax
100 <br><b>version '.$G_brisk_version.'</b><br><br>
101 Copyright 2006-2007 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
106 $from = array ( '\\', '@', '|' );
107 $to = array ( '\\\\', '@', '¦' );
109 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
114 var $value; /* 0 - 39 card value */
115 var $stat; /* 'bunch', 'hand', 'table', 'take' */
116 var $owner; /* (table position 0-4) */
117 // var $pos; /* Pos in hand. */
118 var $x; /* When played the X position on the table of the owner. */
119 var $y; /* When played the Y position on the table of the owner. */
121 function Card($value, $stat, $owner)
123 $this->value = $value;
124 $this->stat = $stat; // Card stat
125 $this->owner = $owner;
128 function assign($stat,$owner)
130 $this->stat = $stat; // Card stat
131 $this->owner = $owner;
134 function setpos($pos)
141 $this->stat = 'table'; // Card stat
146 function take($newown)
148 $this->stat = 'take'; // Card stat
149 $this->owner = $newown;
167 var $points; // points array
168 var $points_n; // number of row of points
192 if (($thiz =& new Table()) == FALSE)
195 $thiz->player = array();
198 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
199 $thiz->asta_pla_n= -1;
200 $thiz->asta_card = -1;
201 $thiz->asta_pnt = -1;
203 $thiz->points = array( );
205 $thiz->total = array( 0, 0, 0, 0, 0);
206 $thiz->asta_win = -1;
207 $thiz->briscola = -1;
210 $thiz->old_reason = "";
211 $thiz->old_asta_pnt = -1;
214 $thiz->old_friend = -1;
216 $thiz->table_token = "";
217 $thiz->table_start = 0;
222 function &clone(&$from)
226 if (($thiz =& new Table()) == FALSE)
229 $thiz->player = array();
230 for ($i = 0 ; $i < $from->player_n ; $i++)
231 $thiz->player[$i] = $from->player[$i];
232 $thiz->player_n = $from->player_n;
233 $thiz->card = $from->card;
234 $thiz->mazzo = $from->mazzo; // REVIEW
235 $thiz->gstart = $from->gstart;
236 $thiz->turn = $from->turn;
238 $thiz->asta_pla = $from->asta_pla;
239 $thiz->asta_pla_n = $from->asta_pla_n;
240 $thiz->asta_card = $from->asta_card;
241 $thiz->asta_pnt = $from->asta_pnt;
243 $thiz->mult = $from->mult;
244 $thiz->points = $from->points;
245 $thiz->points_n = $from->points_n;
246 $thiz->total = $from->total;
248 $thiz->asta_win = $from->asta_win;
249 $thiz->briscola = $from->briscola;
250 $thiz->friend = $from->friend;
252 $thiz->old_reason = $from->old_reason;
253 $thiz->old_asta_pnt = $from->old_asta_pnt;
254 $thiz->old_pnt = $from->old_pnt;
255 $thiz->old_win = $from->old_win;
256 $thiz->old_friend = $from->old_friend;
258 $thiz->table_token = $from->table_token;
259 $thiz->table_start = $from->table_start;
264 function &spawn(&$from)
268 if (($thiz =& new Table()) == FALSE)
271 $thiz->player_n = $from->player_n;
272 $thiz->card = &$thiz->bunch_create();
273 $thiz->mazzo = $from->mazzo;
274 $thiz->gstart = $from->gstart;
275 $thiz->turn = $from->turn;
277 $thiz->asta_pla = $from->asta_pla;
278 $thiz->asta_pla_n = $from->asta_pla_n;
279 $thiz->asta_card = $from->asta_card;
280 $thiz->asta_pnt = $from->asta_pnt;
282 $thiz->mult = $from->mult;
283 $thiz->points = $from->points;
284 $thiz->points_n = $from->points_n;
285 $thiz->total = $from->total;
287 $thiz->asta_win = $from->asta_win;
288 $thiz->briscola = $from->briscola;
289 $thiz->friend = $from->friend;
291 $thiz->old_reason = $from->old_reason;
292 $thiz->old_asta_pnt = $from->old_asta_pnt;
293 $thiz->old_pnt = $from->old_pnt;
294 $thiz->old_win = $from->old_win;
295 $thiz->old_friend = $from->old_friend;
297 // players are rearranged in an dedicated array
298 $thiz->player = array();
299 for ($i = 0 ; $i < $from->player_n ; $i++)
300 $thiz->player[$i] = $i;
302 $thiz->table_token = $from->table_token;
303 $thiz->table_start = $from->table_start;
308 function &bunch_create()
312 for ($i = 0 ; $i < 40 ; $i++) {
313 $ret[$i] =& new Card($i, 'bunch', 'no_owner');
320 function bunch_make()
322 $ct = array(0,0,0,0,0);
324 mt_srand(make_seed());
326 for ($i = 39 ; $i >= 0 ; $i--)
329 for ($i = 39 ; $i >= 0 ; $i--) {
333 log_main("RND ZERO");
338 $this->card[$id]->assign('hand', $owner);
340 $rest[$rn] = $rest[$i];
341 // $pubbpos[$rn2] = $pubbpos[$i];
345 function init(&$userarr)
347 $this->mazzo = rand(0,PLAYERS_N-1);
351 $this->old_reason = "";
352 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
353 $this->total[$i] = 0;
354 $user_cur = &$userarr[$this->player[$i]];
355 $user_cur->exitislock = TRUE;
358 log_main("table::init: ci siamo");
361 function game_init(&$userarr)
365 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
369 $this->asta_pla_n = PLAYERS_N;
370 $this->asta_card = -1;
371 $this->asta_pnt = 60;
372 $this->asta_win = -1;
373 $this->briscola = -1;
377 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
378 $this->asta_pla[$i] = TRUE;
379 $user_cur = &$userarr[$this->player[$i]];
380 $user_cur->subst = 'asta';
381 $user_cur->asta_card = -2;
382 $user_cur->asta_pnt = -1;
383 $user_cur->handpt = $this->hand_points($i);
384 // SEE function calculate_points(&$table)
390 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
393 function getPlayer($idx)
395 return ($this->player[$idx]);
398 function setPlayer($idx, $player)
400 $this->player[$idx] = $player;
403 function user_add($idx)
405 $this->player[$this->player_n] = $idx;
408 return ($this->player_n - 1);
411 function user_rem(&$room, &$user)
413 $tabpos = $user->table_pos;
415 /* verifico la consistenza dei dati */
416 if ($room->user[$this->player[$tabpos]] == $user) {
418 /* aggiorna l'array dei giocatori al tavolo. */
419 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
420 $this->player[$i] = $this->player[$i+1];
421 $user_cur = &$room->user[$this->player[$i]];
422 $user_cur->table_pos = $i;
427 log_main("INCONSISTENCY ON TABLE.");
431 function hand_points($idx)
433 GLOBAL $G_all_points;
437 for ($i = 0 ; $i < 40 ; $i++) {
438 if ($this->card[$i]->owner != $idx)
441 $ctt = $this->card[$i]->value % 10;
442 $tot += $G_all_points[$ctt];
448 function exitlock_show(&$userarr, $table_pos)
450 $ct = $this->exitlock_calc(&$userarr, $table_pos);
452 $ret = sprintf('exitlock_show(%d, %s);', $ct,
453 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
457 function exitlock_calc(&$userarr, $table_pos)
461 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
462 if ($userarr[$this->player[$i]]->exitislock == FALSE)
471 var $name; // name of the user
472 var $sess; // session of the user
473 var $ip; // ip of the user
474 var $lacc; // last access (for the cleanup)
475 var $laccwr; // last access (for the cleanup)
476 var $bantime; // timeout to temporary ban
477 var $stat; // status (outdoor, room, table, game, ...)
478 var $subst; // substatus for each status
479 var $step; // step of the current status
480 var $trans_step; // step to enable transition between pages (disable == -1)
481 var $comm; // commands array
484 var $handpt; // Total card points at the beginning of the current hand.
485 var $exitislock; // Player can exit from the table ?
486 var $table; // id of the current table (if in table state)
487 var $table_pos; // idx on the table
488 var $table_token;// token that identify a game on a table
489 var $the_end; // Flag to change the end of the session
494 function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
497 if (($thiz =& new User()) == FALSE)
503 $thiz->lacc = time();
504 $thiz->laccwr = time();
507 $thiz->subst = $subst;
509 $thiz->trans_step = -1;
510 $thiz->comm = array();
511 $thiz->asta_card = -2;
512 $thiz->asta_pnt = -1;
514 $thiz->exitislock = TRUE;
516 $thiz->table = $table;
517 $thiz->table_pos = -1;
518 $thiz->table_token = "";
523 function &clone(&$from)
527 if (($thiz =& new User()) == FALSE)
530 $thiz->name = $from->name;
531 $thiz->sess = $from->sess;
532 $thiz->ip = $from->ip;
533 $thiz->lacc = $from->lacc;
534 $thiz->laccwr = $from->laccwr;
535 $thiz->bantime = $from->bantime;
536 $thiz->stat = $from->stat;
537 $thiz->subst = $from->subst;
538 $thiz->step = $from->step;
539 $thiz->trans_step = $from->trans_step;
540 $thiz->comm = array();
542 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
543 for ($i = $i_start ; $i < $from->step ; $i++) {
545 $thiz->comm[$ii] = $from->comm[$ii];
547 $thiz->asta_card = $from->asta_card;
548 $thiz->asta_pnt = $from->asta_pnt;
549 $thiz->handpt = $from->handpt;
550 $thiz->exitislock = $from->exitislock;
551 $thiz->table = $from->table;
552 $thiz->table_pos = $from->table_pos;
553 $thiz->table_token = $from->table_token;
554 $thiz->the_end = $from->the_end;
559 function &spawn(&$from, $table, $table_pos)
563 if (($thiz =& new User()) == FALSE)
566 $thiz->name = $from->name;
567 $thiz->sess = $from->sess;
568 $thiz->ip = $from->ip;
569 $thiz->lacc = $from->lacc;
570 $thiz->laccwr = $from->laccwr;
571 $thiz->bantime = $from->bantime;
572 $thiz->stat = $from->stat;
573 $thiz->subst = $from->subst;
574 $thiz->step = $from->step;
575 $thiz->trans_step = $from->trans_step;
576 $thiz->comm = array();
578 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
579 for ($i = $i_start ; $i < $from->step ; $i++) {
580 log_wr("TRY PUSH:".$i);
582 $thiz->comm[$ii] = $from->comm[$ii];
585 $thiz->asta_card = $from->asta_card;
586 $thiz->asta_pnt = $from->asta_pnt;
587 $thiz->handpt = $from->handpt;
588 $thiz->exitislock = $from->exitislock;
589 $thiz->the_end = $from->the_end;
591 $thiz->table = $table;
592 $thiz->table_pos = $table_pos;
593 $thiz->table_token = $from->table_token;
598 function stat_set($stat) {
599 log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]");
600 $this->stat = "$stat";
603 if (validate_sess($this->sess)) {
604 $fp = fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
605 fwrite($fp, sprintf("%s\n",$this->stat));
611 function step_set($step) {
615 if (validate_sess($this->sess) == FALSE)
617 if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
619 fwrite($fp, pack("l",$this->step), 4);
628 function step_inc() {
631 if (validate_sess($this->sess)) {
632 $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
633 fwrite($fp, pack("l",$this->step), 4);
644 function step_get($sess) {
648 if (validate_sess($sess) == FALSE)
651 if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
654 if (($s = fread($fp, 4)) == FALSE)
660 $arr = unpack('l', $s);
663 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
670 log_rd2("STEP_GET: return false ".$ct);
674 function step_unproxy($sess) {
675 log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
676 @unlink(PROXY_PATH."/".$sess.".step");
683 var $comm; // commands for many people
684 var $step; // current step of the comm array
685 var $garbage_timeout;
688 $this->user = array();
689 $this->table = array();
691 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
692 $this->user[$i] =& User::create("", "");
695 for ($i = 0 ; $i < TABLES_N ; $i++)
696 $this->table[$i] =& Table::create();
697 $this->garbage_timeout = 0;
700 function garbage_manager($force)
704 log_rd2("garbage_manager START");
706 /* Garbage collector degli utenti in timeout */
708 if ($force || $this->garbage_timeout < $curtime) {
710 // Before all align times with table timeout
711 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
712 $table_cur =& $this->table[$table_idx];
713 // if the table is complete and exists its shared mem we get the info about users lacc
715 if ($table_cur->player_n == PLAYERS_N) {
716 log_main("PLAYERS == N TABLE ".$table_idx);
718 if (($sem = Briskin5::lock_data($table_idx)) != FALSE) {
719 log_main("bin5 lock data success");
721 $no_recovery = FALSE;
722 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
723 if ($table_cur->table_token != $bri->table_token) {
724 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
725 log_main("ERROR: not matching table_start. Room: ".$table_cur->table_start." Table: ".$bri->table_start);
735 log_main("garbage_manager: bri loaded successfully.");
736 $bri->garbage_manager(TRUE);
738 $bri_table = &$bri->table[0];
740 // is the end of the table
741 if ($bri->the_end == TRUE) {
743 * DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN
745 log_main("garbage_manager: INSIDE THE END.");
747 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
748 // stat must be "table" by definition
749 $user_cur =& $this->user[$table_cur->player[$i]];
750 $bri_user =& $bri->user[$i];
752 $user_cur->subst = $bri_user->subst;
753 $user_cur->step = $bri_user->step;
754 $user_cur->lacc = $bri_user->lacc;
755 $user_cur->laccwr = $bri_user->lacc;
756 $user_cur->bantime = $bri_user->bantime;
759 $this->room_join_wakeup(&$user_cur, FALSE, 0);
760 $table_cur->table_token = "";
762 Briskin5::destroy_data($table_idx);
765 log_main("gm:: save_data");
767 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
768 $this->user[$table_cur->player[$i]]->lacc = $bri->user[$i]->lacc;
771 Briskin5::save_data(&$bri);
773 } // else if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
774 else if ($no_recovery == FALSE) {
775 log_main("ERROR: table ".$table_idx." unrecoverable join");
777 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
778 $user_cur = &$this->user[$table_cur->player[$i]];
779 $user_cur->subst = "shutdowner";
780 $user_cur->step_inc();
782 $ret = sprintf('stat = "%s"; subst = "%s";', $cur_user->stat, $cur_user->subst);
783 $ret .= "gst.st = ".($user_cur->step+1)."; ";
784 $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);
785 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
786 $user_cur->step_inc();
789 $this->room_join_wakeup(&$user_cur, TRUE, -2);
790 $table_cur->table_token = "";
793 Briskin5::unlock_data($sem);
795 } // if ($table_cur->player_n == PLAYERS_N) {
796 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
798 log_rd2("out new loop.");
800 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
801 $user_cur = &$this->user[$i];
803 log_rd2("User: ".$user_cur->name." stat: ".$user_cur->stat." subst: ".$user_cur->subst);
805 if ($user_cur->sess == "")
808 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
809 // Auto logout dell'utente
810 log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
812 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
813 log_auth($user_cur->sess, "Autologout session.");
815 $tmp_sess = $user_cur->sess;
816 $user_cur->sess = "";
817 step_unproxy($tmp_sess);
818 $user_cur->name = "";
820 $user_cur->the_end = FALSE;
822 log_rd2("AUTO LOGOUT.");
823 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
824 $this->room_wakeup(&$user_cur);
825 else if ($user_cur->subst == 'standup')
826 $this->room_outstandup(&$user_cur);
828 log_rd2("LOGOUT FROM WHAT ???");
832 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
833 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
834 $this->room_wakeup(&$user_cur);
835 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
836 $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);
837 $user_cur->step_inc();
841 log_rd2("GARBAGE UPDATED!");
843 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
851 function room_wakeup(&$user)
853 $table_idx = $user->table;
854 $table = &$this->table[$table_idx];
856 log_main("WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
860 $from_table = ($user->stat == "table");
862 log_main("WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
864 for ($i = 0 ; $i < $table->player_n ; $i++) {
865 $user_cur = &$this->user[$table->player[$i]];
866 log_main("PREIMPOST: INLOOP name: ".$user_cur->name);
868 if ($user_cur != $user) {
869 $user_cur->stat_set("room");
870 $user_cur->subst = "sitdown";
871 $user_cur->laccwr = $curtime;
873 else if ($user->sess != "") {
874 $user_cur->stat_set("room");
875 $user_cur->subst = "standup";
876 $user_cur->laccwr = $curtime;
877 $user_cur->table = -1;
882 $user->stat_set("room");
883 $user->subst = "standup";
884 $user->laccwr = $curtime;
886 /* aggiorna l'array dei giocatori al tavolo. */
887 $table->user_rem(&$this, &$user);
889 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
890 $user_cur = &$this->user[$i];
891 if ($user_cur->sess == '' || $user_cur->stat != 'room')
894 log_main("VALORI: name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
896 $ret = "gst.st = ".($user_cur->step+1)."; ";
897 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
898 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
899 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
900 log_main("DOCUMENT.index.php: from table");
902 else if ($user_cur->stat == "room") {
903 log_main("DOCUMENT.index.php: from table");
905 $ret .= table_content($this, $user_cur, $table_idx);
906 $ret .= standup_content($this, $user_cur);
908 $act_content = table_act_content(FALSE, 0, $table_idx, $user->table);
909 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
912 if ($user_cur == $user) {
913 // set the new status
914 $ret .= 'subst = "standup"; ';
915 // clean the action buttons in other tables
916 for ($e = 0 ; $e < TABLES_N ; $e++) {
917 if ($this->table[$e]->player_n < PLAYERS_N)
918 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
922 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
923 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
926 log_wr("ROOM_WAKEUP: ".$ret);
927 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
928 $user_cur->step_inc();
932 function room_join_wakeup(&$user, $update_lacc = FALSE, $trans_delta)
934 $table_idx = $user->table;
935 $table = &$this->table[$table_idx];
937 log_main("JOIN_WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
944 log_main("JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
946 for ($i = 0 ; $i < $table->player_n ; $i++) {
947 $user_cur = &$this->user[$table->player[$i]];
948 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
949 if ($user_cur->sess != "") {
950 if ($update_lacc == TRUE) {
951 $user_cur->laccwr = $curtime;
953 log_main("cur: ".$user_cur->name." subst: ".$user_cur->subst);
954 if ($user_cur->subst == "shutdowned") {
955 $user_cur->stat_set("room");
956 $user_cur->subst = "sitdown";
958 else if ($user_cur->subst == "shutdowner") {
959 $user_cur->stat_set("room");
960 $user_cur->subst = "standup";
961 $user_cur->table = -1;
962 $user_wup[$user_wup_n++] = &$user_cur;
964 $user_tab[$user_tab_n++] = &$user_cur;
968 for ($wup_idx = 0 ; $wup_idx < $user_wup_n ; $wup_idx++)
969 $table->user_rem(&$this, &$user_wup[$wup_idx]);
971 /* aggiorna l'array dei giocatori al tavolo. */
973 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
974 log_main("START LOOP");
975 $user_cur = &$this->user[$i];
976 if ($user_cur->sess == '' || $user_cur->stat != 'room') {
977 log_main("name: ".$user_cur->name."skip subst: ".$user_cur->subst);
982 log_main("VALORI name: ".$user_cur->name." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
984 $ret = "gst.st = ".($user_cur->step+1)."; ";
985 if ($user_cur->stat == "room") {
986 log_main("DOCUMENT.index.php from table");
988 $ret .= table_content($this, $user_cur, $table_idx);
989 $ret .= standup_content($this, $user_cur);
991 $act_content = table_act_content(FALSE, 0, $table_idx, $user_cur->table);
992 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
994 for ($tab_idx = 0 ; $tab_idx < $user_tab_n ; $tab_idx++)
995 if ($user_cur == $user_tab[$tab_idx])
998 // for users that wakeup the room will be reconstructed by index_rd.php
999 if ($tab_idx < $user_tab_n) {
1000 log_main("PRE show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1001 $user_cur->trans_step = $user_cur->step + 1 + $trans_delta;
1002 $user_cur->comm[$user_cur->step % COMM_N] = "";
1003 $user_cur->step_inc();
1004 $user_cur->comm[$user_cur->step % COMM_N] = show_room(&$this, ($user_cur->step + 1), &$user_cur);
1005 $user_cur->step_inc();
1006 log_main("POST show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1010 log_main("JOIN_WAKEUP wup_idx ".$wup_idx." wup_n ".$user_wup_n);
1012 log_main("JOIN_WAKEUP more");
1013 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
1014 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1015 log_main("JOIN_WAKEUP end more");
1017 log_wr("ROOM_JOIN_WAKEUP: ".$ret);
1018 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1019 $user_cur->step_inc();
1023 function room_outstandup(&$user)
1025 $this->room_sitdown(&$user, -1);
1028 function table_update(&$user)
1030 log_main("table_update: pre - USER: ".$user->name);
1032 $table_idx = $user->table;
1034 if ($table_idx > -1)
1035 $table = &$this->table[$table_idx];
1037 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1039 $user_cur = &$this->user[$i];
1040 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1043 $ret = "gst.st = ".($user_cur->step+1)."; ";
1044 if ($table_idx > -1)
1045 $ret .= table_content($this, $user_cur, $table_idx);
1047 if ($user_cur == $user) {
1048 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
1050 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1051 $user_cur->step_inc();
1054 log_main("table_update: post");
1057 function room_sitdown(&$user, $table_idx)
1059 log_main("room_sitdown ".($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
1061 if ($table_idx > -1)
1062 $table = &$this->table[$table_idx];
1064 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1066 $user_cur = &$this->user[$i];
1067 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1070 $ret = "gst.st = ".($user_cur->step+1)."; ";
1071 if ($table_idx > -1)
1072 $ret .= table_content($this, $user_cur, $table_idx);
1073 $ret .= standup_content($this, $user_cur);
1075 if ($user_cur == $user) {
1076 $ret .= 'subst = "sitdown"; ';
1077 // clean the action buttons in other tables
1078 for ($e = 0 ; $e < TABLES_N ; $e++) {
1079 $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
1080 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
1083 else if ($table_idx > -1) {
1084 if ($table->player_n == PLAYERS_N) {
1085 $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
1086 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1089 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1090 $user_cur->step_inc();
1094 function chatt_send(&$user, $mesg)
1096 if ($user->stat == 'table') {
1097 $table = &$this->table[$user->table];
1100 $user_mesg = substr($mesg,6);
1104 $dt = date("H:i ", $timecur);
1105 if (strncmp($user_mesg, "/nick ", 6) == 0) {
1106 log_main("chatt_send BEGIN");
1108 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
1109 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1110 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Il nickname deve contenere almeno una lettera o una cifra.");', $dt.NICKSERV, xcape($name_new));
1115 $user_mesg = "COMMAND ".$user_mesg;
1118 // update local graph
1119 // update remote graphs
1120 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1121 $user_cur = &$this->user[$i];
1122 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1123 if ($user_cur->sess == '')
1125 if ($user_cur->name == $name_new) {
1126 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1127 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
1132 if ($i == MAX_PLAYERS) {
1133 $user->name = $name_new;
1135 log_main("chatt_send start set");
1138 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1139 log_main("chatt_send set loop");
1141 $user_cur = &$this->user[$i];
1142 if ($user_cur->sess == '')
1144 if ($user_cur->stat == 'room') {
1145 if ($user->stat == 'room' && $user->subst == 'standup') {
1146 $this->standup_update(&$user);
1148 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
1149 $user->stat == 'table') {
1150 log_main("chatt_send pre table update");
1152 $this->table_update(&$user);
1154 log_main("chatt_send post table update");
1157 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
1158 $table = &$this->table[$user->table];
1160 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1161 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1162 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
1163 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
1164 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
1165 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
1166 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
1167 if ($user_cur == $user)
1168 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
1169 xcape($user->name,ENT_COMPAT,"UTF-8"));
1170 $user_cur->step_inc();
1176 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
1177 if ($user->stat == 'room') {
1178 $user_cur = &$this->user[$i];
1179 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1183 $user_cur = &$this->user[$table->player[$i]];
1186 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1187 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
1188 $dt.xcape($user->name), xcape($user_mesg));
1189 $user_cur->step_inc();
1191 log_legal($timecur, $user->sess, $user->name,
1192 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
1196 function &get_user($sess, &$idx)
1198 GLOBAL $PHP_SELF, $G_false;
1200 if (validate_sess($sess)) {
1201 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1202 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1205 $ret = &$this->user[$i];
1209 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1210 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
1211 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1214 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1221 * function &add_user(&$room, &$sess, &$idx, $name, $ip)
1224 * if ($idx != -1 && ret == FALSE) => duplicated nick
1225 * if ($idx == -2 && ret == FALSE) => invalid name
1226 * if ($idx == -1 && ret == FALSE) => no space left
1227 * if (ret == TRUE) => SUCCESS
1229 function &add_user(&$sess, &$idx, $name, $ip)
1236 if (($name_new = validate_name($name)) == FALSE) {
1241 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
1242 if (validate_sess($sess) == FALSE)
1245 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1247 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1251 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
1254 if (strcmp($this->user[$i]->name, $name_new) == 0) {
1262 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1264 if ($idx != -1 && $i == MAX_PLAYERS) {
1268 $this->user[$idx]->sess = uniqid("");
1269 $sess = $this->user[$idx]->sess;
1273 $this->user[$idx]->sess = $sess;
1275 $this->user[$idx]->name = $name_new;
1276 $this->user[$idx]->stat_set("room");
1277 $this->user[$idx]->step_set(0);
1278 while (array_pop($this->user[$idx]->comm) != NULL);
1279 $this->user[$idx]->subst = "standup";
1280 $this->user[$idx]->lacc = $curtime;
1281 $this->user[$idx]->laccwr = $curtime;
1282 $this->user[$idx]->bantime = 0;
1283 $this->user[$idx]->ip = $ip;
1284 log_main(sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1286 return ($this->user[$idx]);
1292 function standup_update(&$user)
1294 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1295 $user_cur = &$this->user[$i];
1296 if ($user_cur->sess == '')
1299 log_main("STANDUP START: ".$user_cur->stat);
1301 if ($user_cur->stat == 'room') {
1302 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
1303 if ($user_cur == $user)
1304 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
1306 log_main("FROM STANDUP: NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
1308 $user_cur->step_inc();
1314 function &init_data()
1316 $room =& new Room();
1322 function &load_data()
1324 GLOBAL $G_false, $sess;
1326 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1331 if ($shm = shm_attach($tok, SHM_DIMS)) {
1332 $room = @shm_get_var($shm, $tok);
1334 log_only("bri == ".($room == FALSE ? "FALSE" : "TRUE")." bri === ".($room === FALSE ? "FALSE" : "TRUE")." bri isset ".(isset($room) ? "TRUE" : "FALSE"));
1336 log_only("bri count ".count($room));
1338 if ($room == FALSE) {
1339 log_only("INIT MAIN DATA");
1341 $room =& Room::init_data();
1342 if (shm_put_var($shm, $tok, $room) == FALSE) {
1343 log_only("PUT_VAR FALLITA ".strlen(serialize($room)));
1344 log_only(serialize($room));
1358 function save_data(&$room)
1368 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1)
1374 if (($shm = shm_attach($tok, SHM_DIMS)) == FALSE)
1377 // log_only("PUT_VAR DI ".strlen(serialize($room)));
1378 if (shm_put_var($shm, $tok, $room) == FALSE) {
1379 log_only("PUT_VAR FALLITA ".strlen(serialize($room)));
1380 log_only(serialize($room));
1383 // log_main("QUI CI ARRIVA [".$room->user[0]->name."]");
1393 function lock_data()
1397 // echo "LOCK: ".FTOK_PATH."/main";
1399 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1403 // echo "FTOK ".$tok."<br>";
1404 if (($res = sem_get($tok)) == FALSE) {
1405 echo "SEM_GET FAILED";
1408 if (sem_acquire($res)) {
1409 log_lock("LOCK room");
1416 function unlock_data($res)
1420 log_lock("UNLOCK room");
1422 return (sem_release($res));
1426 function make_seed()
1428 list($usec, $sec) = explode(' ', microtime());
1429 return (float) $sec + ((float) $usec * 100000);
1433 function log_only2($log) {
1436 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONL2) == 0)
1439 if (isset($sess) == FALSE)
1444 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONL2) == 0)
1447 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1448 fwrite($fp, sprintf("ONL2: [%s] [%s]\n", $ssess, $log));
1453 function log_only($log) {
1456 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONLY) == 0)
1459 if (isset($sess) == FALSE)
1464 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONLY) == 0)
1467 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1468 fwrite($fp, sprintf("ONLY: [%s] [%s]\n", $ssess, $log));
1473 function log_main($log) {
1476 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_MAIN) == 0)
1479 if (isset($sess) == FALSE)
1484 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_MAIN) == 0)
1487 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1488 fwrite($fp, sprintf("MAIN: [%s] [%s]\n", $ssess, $log));
1493 function log_rd($log) {
1496 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_READ) == 0)
1499 if (isset($sess) == FALSE)
1504 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_READ) == 0)
1507 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1508 fwrite($fp, sprintf("READ: [%s] [%s]\n", $ssess, $log));
1513 function log_rd2($log) {
1516 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_REA2) == 0)
1519 if (isset($sess) == FALSE)
1524 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_REA2) == 0)
1527 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1528 fwrite($fp, sprintf("REA2: [%s] [%s]\n", $ssess, $log));
1533 function log_send($log) {
1536 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SEND) == 0)
1539 if (isset($sess) == FALSE)
1544 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SEND) == 0)
1547 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1548 fwrite($fp, sprintf("SEND: [%s] [%s]\n", $ssess, $log));
1553 function log_lock($log) {
1556 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOCK) == 0)
1559 if (isset($sess) == FALSE)
1564 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOCK) == 0)
1567 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1568 fwrite($fp, sprintf("LOCK: [%s] [%s]\n", $ssess, $log));
1573 function log_wr($log) {
1576 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_WRIT) == 0)
1579 if (isset($sess) == FALSE)
1584 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_WRIT) == 0)
1587 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1588 fwrite($fp, sprintf("WRIT: [%s] [%s]\n", $ssess, $log));
1593 function log_load($log) {
1596 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOAD) == 0)
1599 if (isset($sess) == FALSE)
1604 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOAD) == 0)
1607 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1608 fwrite($fp, sprintf("LOAD: [%s] [%s]\n", $ssess, $log));
1613 function log_auth($sess, $log) {
1614 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_AUTH) == 0)
1617 if (( (BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_AUTH) == 0)
1620 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1621 fwrite($fp, sprintf("AUTH: [%s] [%d] [%s]\n", $sess, time(), $log));
1627 function log_legal($timecur, $sess, $name, $where, $mesg)
1631 if (($fp = @fopen(LEGAL_PATH."/legal.log", 'a')) != FALSE) {
1632 /* Unix time | session | nickname | IP | where was | mesg */
1633 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|\n", $timecur, $sess, $name, $_SERVER['REMOTE_ADDR'], $where , $mesg));
1641 function lock_banlist()
1643 if (($tok = @ftok(FTOK_PATH."/main", "L")) == -1) {
1647 if (($res = sem_get($tok)) == FALSE) {
1648 echo "SEM_GET FAILED";
1651 if (sem_acquire($res))
1657 function unlock_banlist($res)
1659 return (sem_release($res));
1662 function table_act_content($isstanding, $sitted, $table, $cur_table)
1667 if ($sitted < PLAYERS_N) {
1668 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
1672 if ($table == $cur_table)
1673 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
1680 function table_content($room, $user, $table_idx)
1686 // Si possono usare i dati nella classe table
1689 $sess = $user->sess;
1690 $table = &$room->table[$table_idx];
1692 if ($user->stat != 'room')
1695 for ($i = 0 ; $i < $table->player_n ; $i++) {
1696 $user_cur = &$room->user[$table->player[$i]];
1698 if ($user_cur == $user)
1699 { $hilion = "<b>"; $hilioff = "</b>"; }
1701 { $hilion = ""; $hilioff = ""; }
1703 log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
1705 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
1708 for ( ; $i < PLAYERS_N ; $i++)
1712 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
1717 function standup_content(&$room, $user)
1722 if ($user->stat != 'room')
1725 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
1726 if ($room->user[$e]->sess == "" || $room->user[$e]->stat != "room" || $room->user[$e]->name == "")
1731 $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
1733 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
1734 if ($room->user[$e]->sess == "" || $room->user[$e]->stat != "room" || $room->user[$e]->name == "")
1738 if ($room->user[$e]->subst == "standup") {
1739 if (($ct % 4) == 0) {
1742 if ($room->user[$e] == $user)
1743 { $hilion = "<b>"; $hilioff = "</b>"; }
1745 { $hilion = ""; $hilioff = ""; }
1747 $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($room->user[$e]->name), $hilioff);
1748 if (($ct % 4) == 3) {
1749 $content .= '</tr>';
1754 $content .= '</table>';
1756 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
1757 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
1758 $content, $content2);
1764 function show_notify($text, $tout, $butt, $w, $h)
1766 log_main("SHOW_NOTIFY: ".$text);
1767 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
1770 function briscola_show($room, $table, $user)
1775 if ($table->asta_card == 9)
1776 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1778 /* text of caller cell */
1779 if ($user->table_pos == $table->asta_win)
1780 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1782 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
1783 xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
1785 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1787 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1788 $ret .= sprintf('$("caller").style.visibility = "visible";');
1789 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1790 $ret .= sprintf('$("astalascio").style.visibility = "";');
1791 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1792 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1798 function game_result($asta_pnt, $pnt)
1800 if ($asta_pnt == 61) {
1803 else if ($pnt == 60)
1809 if ($pnt >= $asta_pnt)
1816 function multoval($mult)
1820 else if ($mult == 4)
1821 return ("quadruplo");
1823 return (sprintf("%d-plo", $mult));
1826 function show_table_info(&$room, &$table, $table_pos)
1829 $user = &$room->user[$table->player[$table_pos]];
1831 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1832 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1835 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1836 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
1837 $noty .= sprintf("</tr>");
1840 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1842 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1843 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1844 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1845 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1850 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1851 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1852 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1853 $noty .= "</tr></table>";
1855 if ($table->old_reason != "") {
1856 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1859 if ($table->old_win != -1) {
1860 $win = $table->player[$table->old_win];
1861 $fri = $table->player[$table->old_friend];
1863 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1866 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
1867 xcape($room->user[$win]->name),
1868 xcape($room->user[$fri]->name));
1869 if ($table->old_pnt == 120) {
1870 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1873 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1874 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1875 'più di 60'), $table->old_pnt,
1876 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1880 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1881 xcape($room->user[$win]->name));
1882 if ($table->old_pnt == 120) {
1883 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1886 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1887 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1888 'più di 60'), $table->old_pnt,
1889 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1893 if ($table->mazzo == $table_pos)
1894 $noty .= "Fai <b>tu</b> il mazzo,";
1896 $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
1897 $noty .= "Il mazzo a <b>$unam</b>,";
1900 if ($user->subst == 'asta') {
1901 if ($table->asta_win == -1) // auction case
1902 $curplayer = $table->gstart % PLAYERS_N;
1904 $curplayer = $table->asta_win;
1906 else if ($user->subst == 'game') {
1907 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
1911 if ($curplayer == $table_pos) {
1912 $noty .= " tocca a <b>te</b> giocare.";
1915 $unam = xcape($room->user[$table->player[$curplayer]]->name);
1916 $noty .= " tocca a <b>$unam</b> giocare.";
1919 if ($table->mult > 1) {
1920 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1922 $noty .= "<hr><br>";
1924 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
1929 function root_wellcome($user)
1931 GLOBAL $root_wellarr;
1934 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
1935 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $root_wellarr[$i]));
1940 function table_wellcome($user)
1942 GLOBAL $table_wellarr;
1945 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1946 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
1951 function show_room(&$room, $user_step, &$user)
1953 log_main("show_room: username: ".$user->name);
1956 $ret = sprintf('gst.st = %d;', $user_step);
1957 $ret .= sprintf('stat = "%s";', $user->stat);
1959 $ret .= root_wellcome($user);
1960 $ret .= sprintf('subst = "%s";', $user->subst);
1961 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1962 for ($i = 0 ; $i < TABLES_N ; $i++) {
1963 $ret .= table_content($room, $user, $i);
1964 $act_content = table_act_content(($user->subst == 'standup'),
1965 $room->table[$i]->player_n, $i, $user->table);
1966 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1968 $ret .= standup_content($room, $user);
1976 is_transition (is from room to table ?)
1977 is_again (is another game)
1979 Examples of $is_transition, $is_again:
1980 from reload of the page: FALSE, FALSE
1981 from sitdown in room: TRUE, FALSE
1982 from table: asta cmd e tutti passano: TRUE, TRUE
1983 from table: fine partita: TRUE, TRUE
1985 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
1987 $table_idx = $user->table;
1988 $table = &$room->table[$table_idx];
1989 $table_pos = $user->table_pos;
1991 $ret = "table_init();";
1992 $ret .= $table->exitlock_show(&$room->user, $table_pos);
1994 /* GENERAL STATUS */
1995 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1996 $sendstep, $user->stat, $user->subst, $table_pos);
1998 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1999 $sendstep, $user->stat, $user->subst, $table_pos));
2002 $ret .= "background_set();";
2005 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
2006 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
2007 xcape($room->user[$table->player[($table_pos)%PLAYERS_N]]->name),
2008 xcape($room->user[$table->player[($table_pos+1)%PLAYERS_N]]->name),
2009 xcape($room->user[$table->player[($table_pos+2)%PLAYERS_N]]->name),
2010 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+3)%PLAYERS_N]]->name)),
2011 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+4)%PLAYERS_N]]->name)));
2013 /* NOTIFY FOR THE CARD MAKER */
2014 if ($is_transition) { // && $user->subst == "asta" superfluo
2015 $ret .= show_table_info(&$room, &$table, $table_pos);
2018 $ret .= table_wellcome($user);
2020 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
2021 $ret .= playsound("cow.mp3");
2026 if ($is_transition) { // && $user->subst == "asta" superfluo
2029 for ($i = 0 ; $i < 8 ; $i++) {
2030 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
2032 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
2033 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
2039 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
2041 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
2042 $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
2043 $table->card[$o]->value : -1),
2044 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
2049 $taked = array(0,0,0,0,0);
2050 $inhand = array(0,0,0,0,0);
2051 $ontabl = array(-1,-1,-1,-1,-1);
2054 for ($i = 0 ; $i < 40 ; $i++) {
2055 if ($table->card[$i]->stat == 'hand') {
2056 if ($table->card[$i]->owner == $table_pos) {
2057 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
2059 $inhand[$table->card[$i]->owner]++;
2061 else if ($table->card[$i]->stat == 'take') {
2062 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
2063 $taked[$table->card[$i]->owner]++;
2065 else if ($table->card[$i]->stat == 'table') {
2066 $ontabl[$table->card[$i]->owner] = $i;
2070 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2071 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
2073 log_main("Stat table: ".$logg);
2075 /* Set ours cards. */
2077 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
2078 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
2079 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
2080 $oursarg .= ($i == 0 ? "" : ", ")."-1";
2081 $ret .= sprintf('card_setours(%s);', $oursarg);
2083 /* Dispose all cards */
2084 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2085 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
2086 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
2087 $inhand[$i], $taked[$i]);
2089 if ($ontabl[$i] != -1) {
2090 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
2091 $table->card[$ontabl[$i]]->value,
2092 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
2098 if ($user->subst == 'asta') {
2100 /* show users auction status */
2102 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2103 $user_cur = &$room->user[$table->player[$i]];
2104 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
2105 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
2108 $showst .= ",-2,-2";
2109 $ret .= sprintf('show_astat(%s);', $showst);
2111 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
2112 /* show card chooser */
2113 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
2118 if ($table_pos == ($table->gstart % PLAYERS_N) &&
2119 $table->asta_win == -1)
2120 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2121 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
2123 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2124 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
2128 if ($table->asta_win == -1) { // auction case
2129 if ($table_pos == ($table->gstart % PLAYERS_N))
2130 $ret .= "remark_on();";
2132 $ret .= "remark_off();";
2134 else { // chooseed case
2135 if ($table_pos == $table->asta_win)
2136 $ret .= "remark_on();";
2138 $ret .= "remark_off();";
2141 else if ($user->subst == 'game') {
2143 if (($table->gstart + $table->turn) % PLAYERS_N == $table_pos)
2144 $ret .= "is_my_time = true; remark_on();";
2146 $ret .= "remark_off();";
2148 /* WHO CALL AND WATH */
2149 $ret .= briscola_show($room, $table, $user);
2153 } // end function show_table(...
2155 function calculate_winner(&$table)
2162 $cur_seed = $table->briscola - ($table->briscola % 10);
2164 for ($i = 0 ; $i < 40 ; $i++) {
2165 if ($table->card[$i]->stat != "table")
2168 log_wr(sprintf("Card On table: [%d]", $i));
2170 $v = $table->card[$i]->value;
2171 $ontab[$table->card[$i]->owner] = $v;
2172 $ontid[$table->card[$i]->owner] = $i;
2173 /* se briscola setto il flag */
2174 if (($v - ($v % 10)) == $cur_seed)
2178 if ($briontab == FALSE) {
2179 $cur_win = $table->gstart;
2180 $cur_val = $ontab[$cur_win];
2181 $cur_seed = $cur_val - ($cur_val % 10);
2184 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2185 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
2186 if ($ontab[$i] < $cur_val) {
2187 $cur_val = $ontab[$i];
2193 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2194 $table->card[$ontid[$i]]->owner = $cur_win;
2195 $table->card[$ontid[$i]]->stat = "take"; // Card stat
2200 function calculate_points(&$table)
2202 GLOBAL $G_all_points;
2206 if ($table->asta_pnt == 60)
2207 $table->asta_pnt = 61;
2209 $table->old_reason = "";
2210 $table->old_win = $table->asta_win;
2211 $table->old_friend = $table->friend;
2212 $table->old_asta_pnt = $table->asta_pnt;
2214 for ($i = 0 ; $i < 40 ; $i++) {
2215 $ctt = $table->card[$i]->value % 10;
2216 $own = $table->card[$i]->owner;
2217 if ($own == $table->asta_win || $own == $table->friend)
2218 $pro += $G_all_points[$ctt];
2221 log_wr(sprintf("PRO: [%d]", $pro));
2224 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
2225 $table->points[$table->points_n % MAX_POINTS] = array();
2226 for ($i = 0 ; $i < PLAYERS_N ; $i++)
2227 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
2229 $table->old_pnt = $pro;
2235 if ($pro >= $table->asta_pnt)
2240 $table->points[$table->points_n % MAX_POINTS] = array();
2241 for ($i = 0 ; $i < 5 ; $i++) {
2242 if ($i == $table->asta_win)
2243 $pt = ($i == $table->friend ? 4 : 2);
2244 else if ($i == $table->friend)
2249 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
2251 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
2253 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
2255 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
2256 $table->total[$i] += $pt;
2259 $table->old_pnt = $pro;
2263 function validate_sess($sess)
2265 if (strlen($sess) == SESS_LEN)
2271 function validate_name($name)
2273 $name_new = str_replace(' ', '_', substr(trim($name),0,12));
2275 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
2277 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
2284 function playsound($filename)
2286 return (sprintf('playsound("flasou", "%s");', $filename));
2289 function secstoword($secs)
2291 $mins = floor($secs / 60);
2294 $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
2297 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));