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_MIN, (50000 + 10000 * TABLES_N + 15000 * MAX_PLAYERS));
30 define(SHM_DIMS_MAX, SHM_DIMS_MIN + 1048576);
31 define(SHM_DIMS_DLT, 65536);
34 define(COMM_GEN_N, 50);
36 define(STREAM_TIMEOUT, 20);
37 define(EXPIRE_TIME_RD, 180);
38 define(EXPIRE_TIME_SMAMMA, 360);
39 // BAN_TIME da allineare anche in commons.js
40 define(BAN_TIME, 900);
41 define(GARBAGE_TIMEOUT, 10);
42 define(NICKSERV, "<i>SERVER</i>");
44 define(DBG_ONL2, 0x0001);
45 define(DBG_ONLY, 0x0002);
46 define(DBG_MAIN, 0x0004);
47 define(DBG_READ, 0x0008);
48 define(DBG_REA2, 0x0010);
49 define(DBG_SEND, 0x0020);
50 define(DBG_LOCK, 0x0040);
51 define(DBG_WRIT, 0x0080);
52 define(DBG_LOAD, 0x0100);
53 define(DBG_AUTH, 0x0200);
55 define(BRISK_DEBUG, 0);
57 define(BRISK_SINGLE_DEBUG,0);
58 define(BRISK_SINGLE_SESS, "");
59 // define(DEBUGGING, "local");
63 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
64 $G_brisk_version = "spawn-0.10.1";
66 $root_wellarr = Array ( 'Benvenuto in brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: dimensionamento dinamico dell\'area dati e ottimizzazione della stessa, versione beta.',
67 'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
68 $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.');
72 <div style=\\"text-align: left; padding: 8px;\\">
73 <b>Descrizione</b><br>
74 Questa è un\'implementazione della briscola in cinque, così come è spiegata su
75 <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>
76 <b>Configurazione del browser.</b><br>
77 Occorre abilitare i cookies.<br>
79 <b>Uso del sito</b><br>
80 Potete sedervi a un tavolo o rimanere in piedi.<br>
81 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
84 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>
85 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
86 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
87 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
88 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
89 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.
91 <dt><b>Comandi della chat</b>
92 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
93 <dd>.. to be continue ..
99 <div id=\\"header\\" class=\\"header\\">
100 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
101 briscola chiamata in salsa ajax
103 <br><b>version '.$G_brisk_version.'</b><br><br>
104 Copyright 2006-2007 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
109 $from = array ( '\\', '@', '|' );
110 $to = array ( '\\\\', '@', '¦' );
112 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
117 var $value; /* 0 - 39 card value */
118 var $stat; /* 'bunch', 'hand', 'table', 'take' */
119 var $owner; /* (table position 0-4) */
120 // var $pos; /* Pos in hand. */
121 var $x; /* When played the X position on the table of the owner. */
122 var $y; /* When played the Y position on the table of the owner. */
124 function Card($value, $stat, $owner)
126 $this->value = $value;
127 $this->stat = $stat; // Card stat
128 $this->owner = $owner;
131 function assign($stat,$owner)
133 $this->stat = $stat; // Card stat
134 $this->owner = $owner;
137 function setpos($pos)
144 $this->stat = 'table'; // Card stat
149 function take($newown)
151 $this->stat = 'take'; // Card stat
152 $this->owner = $newown;
170 var $points; // points array
171 var $points_n; // number of row of points
195 if (($thiz =& new Table()) == FALSE)
198 $thiz->player = array();
201 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
202 $thiz->asta_pla_n= -1;
203 $thiz->asta_card = -1;
204 $thiz->asta_pnt = -1;
206 $thiz->points = array( );
208 $thiz->total = array( 0, 0, 0, 0, 0);
209 $thiz->asta_win = -1;
210 $thiz->briscola = -1;
213 $thiz->old_reason = "";
214 $thiz->old_asta_pnt = -1;
217 $thiz->old_friend = -1;
219 $thiz->table_token = "";
220 $thiz->table_start = 0;
225 function &clone(&$from)
229 if (($thiz =& new Table()) == FALSE)
232 $thiz->player = array();
233 for ($i = 0 ; $i < $from->player_n ; $i++)
234 $thiz->player[$i] = $from->player[$i];
235 $thiz->player_n = $from->player_n;
236 $thiz->card = $from->card;
237 $thiz->mazzo = $from->mazzo; // REVIEW
238 $thiz->gstart = $from->gstart;
239 $thiz->turn = $from->turn;
241 $thiz->asta_pla = $from->asta_pla;
242 $thiz->asta_pla_n = $from->asta_pla_n;
243 $thiz->asta_card = $from->asta_card;
244 $thiz->asta_pnt = $from->asta_pnt;
246 $thiz->mult = $from->mult;
247 $thiz->points = $from->points;
248 $thiz->points_n = $from->points_n;
249 $thiz->total = $from->total;
251 $thiz->asta_win = $from->asta_win;
252 $thiz->briscola = $from->briscola;
253 $thiz->friend = $from->friend;
255 $thiz->old_reason = $from->old_reason;
256 $thiz->old_asta_pnt = $from->old_asta_pnt;
257 $thiz->old_pnt = $from->old_pnt;
258 $thiz->old_win = $from->old_win;
259 $thiz->old_friend = $from->old_friend;
261 $thiz->table_token = $from->table_token;
262 $thiz->table_start = $from->table_start;
267 function &spawn(&$from)
271 if (($thiz =& new Table()) == FALSE)
274 $thiz->player_n = $from->player_n;
275 $thiz->card = &$thiz->bunch_create();
276 $thiz->mazzo = $from->mazzo;
277 $thiz->gstart = $from->gstart;
278 $thiz->turn = $from->turn;
280 $thiz->asta_pla = $from->asta_pla;
281 $thiz->asta_pla_n = $from->asta_pla_n;
282 $thiz->asta_card = $from->asta_card;
283 $thiz->asta_pnt = $from->asta_pnt;
285 $thiz->mult = $from->mult;
286 $thiz->points = $from->points;
287 $thiz->points_n = $from->points_n;
288 $thiz->total = $from->total;
290 $thiz->asta_win = $from->asta_win;
291 $thiz->briscola = $from->briscola;
292 $thiz->friend = $from->friend;
294 $thiz->old_reason = $from->old_reason;
295 $thiz->old_asta_pnt = $from->old_asta_pnt;
296 $thiz->old_pnt = $from->old_pnt;
297 $thiz->old_win = $from->old_win;
298 $thiz->old_friend = $from->old_friend;
300 // players are rearranged in an dedicated array
301 $thiz->player = array();
302 for ($i = 0 ; $i < $from->player_n ; $i++)
303 $thiz->player[$i] = $i;
305 $thiz->table_token = $from->table_token;
306 $thiz->table_start = $from->table_start;
311 function &bunch_create()
315 for ($i = 0 ; $i < 40 ; $i++) {
316 $ret[$i] =& new Card($i, 'bunch', 'no_owner');
323 function bunch_make()
325 $ct = array(0,0,0,0,0);
327 mt_srand(make_seed());
329 for ($i = 39 ; $i >= 0 ; $i--)
332 for ($i = 39 ; $i >= 0 ; $i--) {
336 log_main("RND ZERO");
341 $this->card[$id]->assign('hand', $owner);
343 $rest[$rn] = $rest[$i];
344 // $pubbpos[$rn2] = $pubbpos[$i];
348 function init(&$userarr)
350 $this->mazzo = rand(0,PLAYERS_N-1);
354 $this->old_reason = "";
355 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
356 $this->total[$i] = 0;
357 $user_cur = &$userarr[$this->player[$i]];
358 $user_cur->exitislock = TRUE;
361 log_main("table::init: ci siamo");
364 function game_init(&$userarr)
368 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
372 $this->asta_pla_n = PLAYERS_N;
373 $this->asta_card = -1;
374 $this->asta_pnt = 60;
375 $this->asta_win = -1;
376 $this->briscola = -1;
380 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
381 $this->asta_pla[$i] = TRUE;
382 $user_cur = &$userarr[$this->player[$i]];
383 $user_cur->subst = 'asta';
384 $user_cur->asta_card = -2;
385 $user_cur->asta_pnt = -1;
386 $user_cur->handpt = $this->hand_points($i);
387 // SEE function calculate_points(&$table)
393 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
396 function getPlayer($idx)
398 return ($this->player[$idx]);
401 function setPlayer($idx, $player)
403 $this->player[$idx] = $player;
406 function user_add($idx)
408 $this->player[$this->player_n] = $idx;
411 return ($this->player_n - 1);
414 function user_rem(&$room, &$user)
416 $tabpos = $user->table_pos;
418 /* verifico la consistenza dei dati */
419 if ($room->user[$this->player[$tabpos]] == $user) {
421 /* aggiorna l'array dei giocatori al tavolo. */
422 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
423 $this->player[$i] = $this->player[$i+1];
424 $user_cur = &$room->user[$this->player[$i]];
425 $user_cur->table_pos = $i;
430 log_main("INCONSISTENCY ON TABLE.");
434 function hand_points($idx)
436 GLOBAL $G_all_points;
440 for ($i = 0 ; $i < 40 ; $i++) {
441 if ($this->card[$i]->owner != $idx)
444 $ctt = $this->card[$i]->value % 10;
445 $tot += $G_all_points[$ctt];
451 function exitlock_show(&$userarr, $table_pos)
453 $ct = $this->exitlock_calc(&$userarr, $table_pos);
455 $ret = sprintf('exitlock_show(%d, %s);', $ct,
456 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
460 function exitlock_calc(&$userarr, $table_pos)
464 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
465 if ($userarr[$this->player[$i]]->exitislock == FALSE)
474 var $name; // name of the user
475 var $sess; // session of the user
476 var $ip; // ip of the user
477 var $lacc; // last access (for the cleanup)
478 var $laccwr; // last access (for the cleanup)
479 var $bantime; // timeout to temporary ban
480 var $stat; // status (outdoor, room, table, game, ...)
481 var $subst; // substatus for each status
482 var $step; // step of the current status
483 var $trans_step; // step to enable transition between pages (disable == -1)
484 var $comm; // commands array
487 var $handpt; // Total card points at the beginning of the current hand.
488 var $exitislock; // Player can exit from the table ?
489 var $table; // id of the current table (if in table state)
490 var $table_pos; // idx on the table
491 var $table_token;// token that identify a game on a table
492 var $the_end; // Flag to change the end of the session
497 function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
500 if (($thiz =& new User()) == FALSE)
506 $thiz->lacc = time();
507 $thiz->laccwr = time();
510 $thiz->subst = $subst;
512 $thiz->trans_step = -1;
513 $thiz->comm = array();
514 $thiz->asta_card = -2;
515 $thiz->asta_pnt = -1;
517 $thiz->exitislock = TRUE;
519 $thiz->table = $table;
520 $thiz->table_pos = -1;
521 $thiz->table_token = "";
526 function &clone(&$from)
530 if (($thiz =& new User()) == FALSE)
533 $thiz->name = $from->name;
534 $thiz->sess = $from->sess;
535 $thiz->ip = $from->ip;
536 $thiz->lacc = $from->lacc;
537 $thiz->laccwr = $from->laccwr;
538 $thiz->bantime = $from->bantime;
539 $thiz->stat = $from->stat;
540 $thiz->subst = $from->subst;
541 $thiz->step = $from->step;
542 $thiz->trans_step = $from->trans_step;
543 $thiz->comm = array();
545 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
546 for ($i = $i_start ; $i < $from->step ; $i++) {
548 $thiz->comm[$ii] = $from->comm[$ii];
550 $thiz->asta_card = $from->asta_card;
551 $thiz->asta_pnt = $from->asta_pnt;
552 $thiz->handpt = $from->handpt;
553 $thiz->exitislock = $from->exitislock;
554 $thiz->table = $from->table;
555 $thiz->table_pos = $from->table_pos;
556 $thiz->table_token = $from->table_token;
557 $thiz->the_end = $from->the_end;
562 function &spawn(&$from, $table, $table_pos)
566 if (($thiz =& new User()) == FALSE)
569 $thiz->name = $from->name;
570 $thiz->sess = $from->sess;
571 $thiz->ip = $from->ip;
572 $thiz->lacc = $from->lacc;
573 $thiz->laccwr = $from->laccwr;
574 $thiz->bantime = $from->bantime;
575 $thiz->stat = $from->stat;
576 $thiz->subst = $from->subst;
577 $thiz->step = $from->step;
578 $thiz->trans_step = $from->trans_step;
579 $thiz->comm = array();
582 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
583 for ($i = $i_start ; $i < $from->step ; $i++) {
584 log_wr("TRY PUSH:".$i);
586 $thiz->comm[$ii] = $from->comm[$ii];
589 $thiz->asta_card = $from->asta_card;
590 $thiz->asta_pnt = $from->asta_pnt;
591 $thiz->handpt = $from->handpt;
592 $thiz->exitislock = $from->exitislock;
593 $thiz->the_end = $from->the_end;
595 $thiz->table = $table;
596 $thiz->table_pos = $table_pos;
597 $thiz->table_token = $from->table_token;
602 function stat_set($stat) {
603 log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]");
604 $this->stat = "$stat";
607 if (validate_sess($this->sess)) {
608 $fp = fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
609 fwrite($fp, sprintf("%s\n",$this->stat));
615 function step_set($step) {
619 if (validate_sess($this->sess) == FALSE)
621 if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
623 fwrite($fp, pack("l",$this->step), 4);
632 function step_inc($delta = 1) {
633 $this->step += $delta;
635 if (validate_sess($this->sess)) {
636 $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
637 fwrite($fp, pack("l",$this->step), 4);
648 function step_get($sess) {
652 if (validate_sess($sess) == FALSE)
655 if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
658 if (($s = fread($fp, 4)) == FALSE)
664 $arr = unpack('l', $s);
667 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
674 log_rd2("STEP_GET: return false ".$ct);
678 function step_unproxy($sess) {
679 log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
680 @unlink(PROXY_PATH."/".$sess.".step");
687 var $comm; // commands for many people
688 var $step; // current step of the comm array
689 var $garbage_timeout;
693 $this->user = array();
694 $this->table = array();
696 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
697 $this->user[$i] =& User::create("", "");
700 for ($i = 0 ; $i < TABLES_N ; $i++)
701 $this->table[$i] =& Table::create();
702 $this->garbage_timeout = 0;
705 function garbage_manager($force)
709 log_rd2("garbage_manager START");
711 /* Garbage collector degli utenti in timeout */
713 if ($force || $this->garbage_timeout < $curtime) {
715 // Before all align times with table timeout
716 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
717 $table_cur =& $this->table[$table_idx];
718 // if the table is complete and exists its shared mem we get the info about users lacc
720 if ($table_cur->player_n == PLAYERS_N) {
721 log_main("PLAYERS == N TABLE ".$table_idx);
723 if (($sem = Briskin5::lock_data($table_idx)) != FALSE) {
724 log_main("bin5 lock data success");
726 $no_recovery = FALSE;
727 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
728 if ($table_cur->table_token != $bri->table_token) {
729 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
730 log_main("ERROR: not matching table_start. Room: ".$table_cur->table_start." Table: ".$bri->table_start);
740 log_main("garbage_manager: bri loaded successfully.");
741 $bri->garbage_manager(TRUE);
743 $bri_table = &$bri->table[0];
745 // is the end of the table
746 if ($bri->the_end == TRUE) {
748 * DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN
750 log_main("garbage_manager: INSIDE THE END.");
752 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
753 // stat must be "table" by definition
754 $user_cur =& $this->user[$table_cur->player[$i]];
755 $bri_user =& $bri->user[$i];
757 $user_cur->subst = $bri_user->subst;
758 $user_cur->step = $bri_user->step;
759 $user_cur->lacc = $bri_user->lacc;
760 $user_cur->laccwr = $bri_user->lacc;
761 $user_cur->bantime = $bri_user->bantime;
764 $this->room_join_wakeup(&$user_cur, FALSE, 0);
765 $table_cur->table_token = "";
767 Briskin5::destroy_data($table_idx);
770 log_main("gm:: save_data");
772 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
773 $this->user[$table_cur->player[$i]]->lacc = $bri->user[$i]->lacc;
776 Briskin5::save_data(&$bri);
778 } // else if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
779 else if ($no_recovery == FALSE) {
780 log_main("ERROR: table ".$table_idx." unrecoverable join");
782 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
783 $user_cur = &$this->user[$table_cur->player[$i]];
784 $user_cur->subst = "shutdowner";
785 $user_cur->step_inc();
787 $ret = sprintf('stat = "%s"; subst = "%s";', $cur_user->stat, $cur_user->subst);
788 $ret .= "gst.st = ".($user_cur->step+1)."; ";
789 $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);
790 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
791 $user_cur->step_inc();
794 $this->room_join_wakeup(&$user_cur, TRUE, -2);
795 $table_cur->table_token = "";
798 Briskin5::unlock_data($sem);
800 } // if ($table_cur->player_n == PLAYERS_N) {
801 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
803 log_rd2("out new loop.");
805 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
806 $user_cur = &$this->user[$i];
808 log_rd2("User: ".$user_cur->name." stat: ".$user_cur->stat." subst: ".$user_cur->subst);
810 if ($user_cur->sess == "")
813 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
814 // Auto logout dell'utente
815 log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
817 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
818 log_auth($user_cur->sess, "Autologout session.");
820 $tmp_sess = $user_cur->sess;
821 $user_cur->sess = "";
822 step_unproxy($tmp_sess);
823 $user_cur->name = "";
825 $user_cur->the_end = FALSE;
827 log_rd2("AUTO LOGOUT.");
828 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
829 $this->room_wakeup(&$user_cur);
830 else if ($user_cur->subst == 'standup')
831 $this->room_outstandup(&$user_cur);
833 log_rd2("LOGOUT FROM WHAT ???");
837 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
838 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
839 $this->room_wakeup(&$user_cur);
840 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
841 $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);
842 $user_cur->step_inc();
846 log_rd2("GARBAGE UPDATED!");
848 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
856 function room_wakeup(&$user)
858 $table_idx = $user->table;
859 $table = &$this->table[$table_idx];
861 log_main("WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
865 $from_table = ($user->stat == "table");
867 log_main("WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
869 for ($i = 0 ; $i < $table->player_n ; $i++) {
870 $user_cur = &$this->user[$table->player[$i]];
871 log_main("PREIMPOST: INLOOP name: ".$user_cur->name);
873 if ($user_cur != $user) {
874 $user_cur->stat_set("room");
875 $user_cur->subst = "sitdown";
876 $user_cur->laccwr = $curtime;
878 else if ($user->sess != "") {
879 $user_cur->stat_set("room");
880 $user_cur->subst = "standup";
881 $user_cur->laccwr = $curtime;
882 $user_cur->table = -1;
887 $user->stat_set("room");
888 $user->subst = "standup";
889 $user->laccwr = $curtime;
891 /* aggiorna l'array dei giocatori al tavolo. */
892 $table->user_rem(&$this, &$user);
894 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
895 $user_cur = &$this->user[$i];
896 if ($user_cur->sess == '' || $user_cur->stat != 'room')
899 log_main("VALORI: name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
901 $ret = "gst.st = ".($user_cur->step+1)."; ";
902 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
903 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
904 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
905 log_main("DOCUMENT.index.php: from table");
907 else if ($user_cur->stat == "room") {
908 log_main("DOCUMENT.index.php: from table");
910 $ret .= table_content($this, $user_cur, $table_idx);
911 $ret .= standup_content($this, $user_cur);
913 $act_content = table_act_content(FALSE, 0, $table_idx, $user->table);
914 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
917 if ($user_cur == $user) {
918 // set the new status
919 $ret .= 'subst = "standup"; ';
920 // clean the action buttons in other tables
921 for ($e = 0 ; $e < TABLES_N ; $e++) {
922 if ($this->table[$e]->player_n < PLAYERS_N)
923 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
927 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
928 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
931 log_wr("ROOM_WAKEUP: ".$ret);
932 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
933 $user_cur->step_inc();
937 function room_join_wakeup(&$user, $update_lacc = FALSE, $trans_delta)
939 $table_idx = $user->table;
940 $table = &$this->table[$table_idx];
942 log_main("JOIN_WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
949 log_main("JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
951 for ($i = 0 ; $i < $table->player_n ; $i++) {
952 $user_cur = &$this->user[$table->player[$i]];
953 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
954 if ($user_cur->sess != "") {
955 if ($update_lacc == TRUE) {
956 $user_cur->laccwr = $curtime;
958 log_main("cur: ".$user_cur->name." subst: ".$user_cur->subst);
959 if ($user_cur->subst == "shutdowned") {
960 $user_cur->stat_set("room");
961 $user_cur->subst = "sitdown";
963 else if ($user_cur->subst == "shutdowner") {
964 $user_cur->stat_set("room");
965 $user_cur->subst = "standup";
966 $user_cur->table = -1;
967 $user_wup[$user_wup_n++] = &$user_cur;
969 $user_tab[$user_tab_n++] = &$user_cur;
973 for ($wup_idx = 0 ; $wup_idx < $user_wup_n ; $wup_idx++)
974 $table->user_rem(&$this, &$user_wup[$wup_idx]);
976 /* aggiorna l'array dei giocatori al tavolo. */
978 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
979 log_main("START LOOP");
980 $user_cur = &$this->user[$i];
981 if ($user_cur->sess == '' || $user_cur->stat != 'room') {
982 log_main("name: ".$user_cur->name."skip subst: ".$user_cur->subst);
987 log_main("VALORI name: ".$user_cur->name." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
989 $ret = "gst.st = ".($user_cur->step+1)."; ";
990 if ($user_cur->stat == "room") {
991 log_main("DOCUMENT.index.php from table");
993 $ret .= table_content($this, $user_cur, $table_idx);
994 $ret .= standup_content($this, $user_cur);
996 $act_content = table_act_content(FALSE, 0, $table_idx, $user_cur->table);
997 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
999 for ($tab_idx = 0 ; $tab_idx < $user_tab_n ; $tab_idx++)
1000 if ($user_cur == $user_tab[$tab_idx])
1003 // for users that wakeup the room will be reconstructed by index_rd.php
1004 if ($tab_idx < $user_tab_n) {
1005 log_main("PRE show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1007 while (array_pop($user_cur->comm) != NULL);
1009 $user_cur->trans_step = $user_cur->step + 1 + $trans_delta;
1010 $user_cur->comm[$user_cur->step % COMM_N] = "";
1011 $user_cur->step_inc();
1012 $user_cur->comm[$user_cur->step % COMM_N] = show_room(&$this, ($user_cur->step + 1), &$user_cur);
1013 $user_cur->step_inc();
1014 log_main("POST show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1018 log_main("JOIN_WAKEUP wup_idx ".$wup_idx." wup_n ".$user_wup_n);
1020 log_main("JOIN_WAKEUP more");
1021 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
1022 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1023 log_main("JOIN_WAKEUP end more");
1025 log_wr("ROOM_JOIN_WAKEUP: ".$ret);
1026 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1027 $user_cur->step_inc();
1031 function room_outstandup(&$user)
1033 $this->room_sitdown(&$user, -1);
1036 function table_update(&$user)
1038 log_main("table_update: pre - USER: ".$user->name);
1040 $table_idx = $user->table;
1042 if ($table_idx > -1)
1043 $table = &$this->table[$table_idx];
1045 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1047 $user_cur = &$this->user[$i];
1048 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1051 $ret = "gst.st = ".($user_cur->step+1)."; ";
1052 if ($table_idx > -1)
1053 $ret .= table_content($this, $user_cur, $table_idx);
1055 if ($user_cur == $user) {
1056 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
1058 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1059 $user_cur->step_inc();
1062 log_main("table_update: post");
1065 function room_sitdown(&$user, $table_idx)
1067 log_main("room_sitdown ".($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
1069 if ($table_idx > -1)
1070 $table = &$this->table[$table_idx];
1072 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1074 $user_cur = &$this->user[$i];
1075 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1078 $ret = "gst.st = ".($user_cur->step+1)."; ";
1079 if ($table_idx > -1)
1080 $ret .= table_content($this, $user_cur, $table_idx);
1081 $ret .= standup_content($this, $user_cur);
1083 if ($user_cur == $user) {
1084 $ret .= 'subst = "sitdown"; ';
1085 // clean the action buttons in other tables
1086 for ($e = 0 ; $e < TABLES_N ; $e++) {
1087 $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
1088 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
1091 else if ($table_idx > -1) {
1092 if ($table->player_n == PLAYERS_N) {
1093 $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
1094 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1097 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1098 $user_cur->step_inc();
1102 function chatt_send(&$user, $mesg)
1104 if ($user->stat == 'table') {
1105 $table = &$this->table[$user->table];
1108 $user_mesg = substr($mesg,6);
1112 $dt = date("H:i ", $timecur);
1113 if (strncmp($user_mesg, "/nick ", 6) == 0) {
1114 log_main("chatt_send BEGIN");
1116 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
1117 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1118 $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));
1123 $user_mesg = "COMMAND ".$user_mesg;
1126 // update local graph
1127 // update remote graphs
1128 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1129 $user_cur = &$this->user[$i];
1130 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1131 if ($user_cur->sess == '')
1133 if ($user_cur->name == $name_new) {
1134 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1135 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
1140 if ($i == MAX_PLAYERS) {
1141 $user->name = $name_new;
1143 log_main("chatt_send start set");
1146 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1147 log_main("chatt_send set loop");
1149 $user_cur = &$this->user[$i];
1150 if ($user_cur->sess == '')
1152 if ($user_cur->stat == 'room') {
1153 if ($user->stat == 'room' && $user->subst == 'standup') {
1154 $this->standup_update(&$user);
1156 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
1157 $user->stat == 'table') {
1158 log_main("chatt_send pre table update");
1160 $this->table_update(&$user);
1162 log_main("chatt_send post table update");
1165 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
1166 $table = &$this->table[$user->table];
1168 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1169 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1170 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
1171 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
1172 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
1173 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
1174 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
1175 if ($user_cur == $user)
1176 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
1177 xcape($user->name,ENT_COMPAT,"UTF-8"));
1178 $user_cur->step_inc();
1184 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
1185 if ($user->stat == 'room') {
1186 $user_cur = &$this->user[$i];
1187 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1191 $user_cur = &$this->user[$table->player[$i]];
1194 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1195 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
1196 $dt.xcape($user->name), xcape($user_mesg));
1197 $user_cur->step_inc();
1199 log_legal($timecur, $user->sess, $user->name,
1200 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
1204 function &get_user($sess, &$idx)
1206 GLOBAL $PHP_SELF, $G_false;
1208 if (validate_sess($sess)) {
1209 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1210 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1213 $ret = &$this->user[$i];
1217 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1218 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
1219 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1222 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1229 * function &add_user(&$room, &$sess, &$idx, $name, $ip)
1232 * if ($idx != -1 && ret == FALSE) => duplicated nick
1233 * if ($idx == -2 && ret == FALSE) => invalid name
1234 * if ($idx == -1 && ret == FALSE) => no space left
1235 * if (ret == TRUE) => SUCCESS
1237 function &add_user(&$sess, &$idx, $name, $ip)
1244 if (($name_new = validate_name($name)) == FALSE) {
1249 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
1250 if (validate_sess($sess) == FALSE)
1253 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1255 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1259 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
1262 if (strcmp($this->user[$i]->name, $name_new) == 0) {
1270 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1272 if ($idx != -1 && $i == MAX_PLAYERS) {
1276 $this->user[$idx]->sess = uniqid("");
1277 $sess = $this->user[$idx]->sess;
1281 $this->user[$idx]->sess = $sess;
1283 $this->user[$idx]->name = $name_new;
1284 $this->user[$idx]->stat_set("room");
1285 $this->user[$idx]->step_set(0);
1286 while (array_pop($this->user[$idx]->comm) != NULL);
1287 $this->user[$idx]->subst = "standup";
1288 $this->user[$idx]->lacc = $curtime;
1289 $this->user[$idx]->laccwr = $curtime;
1290 $this->user[$idx]->bantime = 0;
1291 $this->user[$idx]->ip = $ip;
1292 log_main(sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1294 return ($this->user[$idx]);
1300 function standup_update(&$user)
1302 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1303 $user_cur = &$this->user[$i];
1304 if ($user_cur->sess == '')
1307 log_main("STANDUP START: ".$user_cur->stat);
1309 if ($user_cur->stat == 'room') {
1310 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
1311 if ($user_cur == $user)
1312 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
1314 log_main("FROM STANDUP: NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
1316 $user_cur->step_inc();
1322 function &init_data()
1324 $room =& new Room();
1330 function &load_data()
1332 GLOBAL $G_false, $sess;
1335 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1336 log_main("ftok failed");
1341 if (($shm_sz = sharedmem_sz($tok)) == -1) {
1342 log_main("shmop_open failed");
1346 $shm_sz = SHM_DIMS_MIN;
1348 if ($shm = shm_attach($tok, $shm_sz)) {
1349 $room = @shm_get_var($shm, $tok);
1351 log_only("bri == ".($room == FALSE ? "FALSE" : "TRUE")." bri === ".($room === FALSE ? "FALSE" : "TRUE")." bri isset ".(isset($room) ? "TRUE" : "FALSE"));
1353 log_only("bri count ".count($room));
1355 if ($room == FALSE) {
1356 log_only("INIT MAIN DATA");
1358 $room =& Room::init_data();
1359 if (@shm_put_var($shm, $tok, $room) == FALSE) {
1360 log_only("PUT_VAR FALLITA ".strlen(serialize($room)));
1361 log_only(serialize($room));
1364 $room->shm_sz = $shm_sz;
1379 function save_data(&$room)
1388 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1)
1391 while ($room->shm_sz < SHM_DIMS_MAX) {
1392 if (($shm = shm_attach($tok, $room->shm_sz)) == FALSE)
1395 // log_only("PUT_VAR DI ".strlen(serialize($room)));
1396 if (shm_put_var($shm, $tok, $room) != FALSE) {
1400 if (shm_remove($shm) === FALSE) {
1401 log_only("REMOVE FALLITA");
1405 $room->shm_sz += SHM_DIMS_DLT;
1414 function lock_data()
1418 // echo "LOCK: ".FTOK_PATH."/main";
1420 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1424 // echo "FTOK ".$tok."<br>";
1425 if (($res = sem_get($tok)) == FALSE) {
1426 echo "SEM_GET FAILED";
1429 if (sem_acquire($res)) {
1430 log_lock("LOCK room");
1437 function unlock_data($res)
1441 log_lock("UNLOCK room");
1443 return (sem_release($res));
1447 function make_seed()
1449 list($usec, $sec) = explode(' ', microtime());
1450 return (float) $sec + ((float) $usec * 100000);
1454 function log_only2($log) {
1457 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONL2) == 0)
1460 if (isset($sess) == FALSE)
1465 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONL2) == 0)
1468 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1469 fwrite($fp, sprintf("ONL2: [%s] [%s]\n", $ssess, $log));
1474 function log_only($log) {
1477 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONLY) == 0)
1480 if (isset($sess) == FALSE)
1485 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONLY) == 0)
1488 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1489 fwrite($fp, sprintf("ONLY: [%s] [%s]\n", $ssess, $log));
1494 function log_main($log) {
1497 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_MAIN) == 0)
1500 if (isset($sess) == FALSE)
1505 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_MAIN) == 0)
1508 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1509 fwrite($fp, sprintf("MAIN: [%s] [%s]\n", $ssess, $log));
1514 function log_rd($log) {
1517 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_READ) == 0)
1520 if (isset($sess) == FALSE)
1525 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_READ) == 0)
1528 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1529 fwrite($fp, sprintf("READ: [%s] [%s]\n", $ssess, $log));
1534 function log_rd2($log) {
1537 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_REA2) == 0)
1540 if (isset($sess) == FALSE)
1545 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_REA2) == 0)
1548 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1549 fwrite($fp, sprintf("REA2: [%s] [%s]\n", $ssess, $log));
1554 function log_send($log) {
1557 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SEND) == 0)
1560 if (isset($sess) == FALSE)
1565 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SEND) == 0)
1568 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1569 fwrite($fp, sprintf("SEND: [%s] [%s]\n", $ssess, $log));
1574 function log_lock($log) {
1577 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOCK) == 0)
1580 if (isset($sess) == FALSE)
1585 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOCK) == 0)
1588 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1589 fwrite($fp, sprintf("LOCK: [%s] [%s]\n", $ssess, $log));
1594 function log_wr($log) {
1597 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_WRIT) == 0)
1600 if (isset($sess) == FALSE)
1605 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_WRIT) == 0)
1608 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1609 fwrite($fp, sprintf("WRIT: [%s] [%s]\n", $ssess, $log));
1614 function log_load($log) {
1617 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOAD) == 0)
1620 if (isset($sess) == FALSE)
1625 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOAD) == 0)
1628 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1629 fwrite($fp, sprintf("LOAD: [%s] [%s]\n", $ssess, $log));
1634 function log_auth($sess, $log) {
1635 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_AUTH) == 0)
1638 if (( (BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_AUTH) == 0)
1641 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1642 fwrite($fp, sprintf("AUTH: [%s] [%d] [%s]\n", $sess, time(), $log));
1648 function log_legal($timecur, $sess, $name, $where, $mesg)
1652 if (($fp = @fopen(LEGAL_PATH."/legal.log", 'a')) != FALSE) {
1653 /* Unix time | session | nickname | IP | where was | mesg */
1654 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|\n", $timecur, $sess, $name, $_SERVER['REMOTE_ADDR'], $where , $mesg));
1662 function lock_banlist()
1664 if (($tok = @ftok(FTOK_PATH."/main", "L")) == -1) {
1668 if (($res = sem_get($tok)) == FALSE) {
1669 echo "SEM_GET FAILED";
1672 if (sem_acquire($res))
1678 function unlock_banlist($res)
1680 return (sem_release($res));
1683 function table_act_content($isstanding, $sitted, $table, $cur_table)
1688 if ($sitted < PLAYERS_N) {
1689 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
1693 if ($table == $cur_table)
1694 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
1701 function table_content($room, $user, $table_idx)
1707 // Si possono usare i dati nella classe table
1710 $sess = $user->sess;
1711 $table = &$room->table[$table_idx];
1713 if ($user->stat != 'room')
1716 for ($i = 0 ; $i < $table->player_n ; $i++) {
1717 $user_cur = &$room->user[$table->player[$i]];
1719 if ($user_cur == $user)
1720 { $hilion = "<b>"; $hilioff = "</b>"; }
1722 { $hilion = ""; $hilioff = ""; }
1724 log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
1726 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
1729 for ( ; $i < PLAYERS_N ; $i++)
1733 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
1738 function standup_content(&$room, $user)
1743 if ($user->stat != 'room')
1746 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
1747 if ($room->user[$e]->sess == "" || $room->user[$e]->stat != "room" || $room->user[$e]->name == "")
1752 $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
1754 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
1755 if ($room->user[$e]->sess == "" || $room->user[$e]->stat != "room" || $room->user[$e]->name == "")
1759 if ($room->user[$e]->subst == "standup") {
1760 if (($ct % 4) == 0) {
1763 if ($room->user[$e] == $user)
1764 { $hilion = "<b>"; $hilioff = "</b>"; }
1766 { $hilion = ""; $hilioff = ""; }
1768 $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($room->user[$e]->name), $hilioff);
1769 if (($ct % 4) == 3) {
1770 $content .= '</tr>';
1775 $content .= '</table>';
1777 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
1778 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
1779 $content, $content2);
1785 function show_notify($text, $tout, $butt, $w, $h)
1787 log_main("SHOW_NOTIFY: ".$text);
1788 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
1791 function briscola_show($room, $table, $user)
1796 if ($table->asta_card == 9)
1797 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1799 /* text of caller cell */
1800 if ($user->table_pos == $table->asta_win)
1801 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1803 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
1804 xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
1806 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1808 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1809 $ret .= sprintf('$("caller").style.visibility = "visible";');
1810 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1811 $ret .= sprintf('$("astalascio").style.visibility = "";');
1812 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1813 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1819 function game_result($asta_pnt, $pnt)
1821 if ($asta_pnt == 61) {
1824 else if ($pnt == 60)
1830 if ($pnt >= $asta_pnt)
1837 function multoval($mult)
1841 else if ($mult == 4)
1842 return ("quadruplo");
1844 return (sprintf("%d-plo", $mult));
1847 function show_table_info(&$room, &$table, $table_pos)
1850 $user = &$room->user[$table->player[$table_pos]];
1852 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1853 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1856 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1857 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
1858 $noty .= sprintf("</tr>");
1861 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1863 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1864 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1865 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1866 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1871 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1872 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1873 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1874 $noty .= "</tr></table>";
1876 if ($table->old_reason != "") {
1877 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1880 if ($table->old_win != -1) {
1881 $win = $table->player[$table->old_win];
1882 $fri = $table->player[$table->old_friend];
1884 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1887 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
1888 xcape($room->user[$win]->name),
1889 xcape($room->user[$fri]->name));
1890 if ($table->old_pnt == 120) {
1891 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1894 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1895 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1896 'più di 60'), $table->old_pnt,
1897 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1901 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1902 xcape($room->user[$win]->name));
1903 if ($table->old_pnt == 120) {
1904 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1907 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1908 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1909 'più di 60'), $table->old_pnt,
1910 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1914 if ($table->mazzo == $table_pos)
1915 $noty .= "Fai <b>tu</b> il mazzo,";
1917 $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
1918 $noty .= "Il mazzo a <b>$unam</b>,";
1921 if ($user->subst == 'asta') {
1922 if ($table->asta_win == -1) // auction case
1923 $curplayer = $table->gstart % PLAYERS_N;
1925 $curplayer = $table->asta_win;
1927 else if ($user->subst == 'game') {
1928 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
1932 if ($curplayer == $table_pos) {
1933 $noty .= " tocca a <b>te</b> giocare.";
1936 $unam = xcape($room->user[$table->player[$curplayer]]->name);
1937 $noty .= " tocca a <b>$unam</b> giocare.";
1940 if ($table->mult > 1) {
1941 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1943 $noty .= "<hr><br>";
1945 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
1950 function root_wellcome($user)
1952 GLOBAL $root_wellarr;
1955 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
1956 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $root_wellarr[$i]));
1961 function table_wellcome($user)
1963 GLOBAL $table_wellarr;
1966 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1967 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
1972 function show_room(&$room, $user_step, &$user)
1974 log_main("show_room: username: ".$user->name);
1977 $ret = sprintf('gst.st = %d;', $user_step);
1978 $ret .= sprintf('stat = "%s";', $user->stat);
1980 $ret .= root_wellcome($user);
1981 $ret .= sprintf('subst = "%s";', $user->subst);
1982 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1983 for ($i = 0 ; $i < TABLES_N ; $i++) {
1984 $ret .= table_content($room, $user, $i);
1985 $act_content = table_act_content(($user->subst == 'standup'),
1986 $room->table[$i]->player_n, $i, $user->table);
1987 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1989 $ret .= standup_content($room, $user);
1997 is_transition (is from room to table ?)
1998 is_again (is another game)
2000 Examples of $is_transition, $is_again:
2001 from reload of the page: FALSE, FALSE
2002 from sitdown in room: TRUE, FALSE
2003 from table: asta cmd e tutti passano: TRUE, TRUE
2004 from table: fine partita: TRUE, TRUE
2006 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
2008 $table_idx = $user->table;
2009 $table = &$room->table[$table_idx];
2010 $table_pos = $user->table_pos;
2012 $ret = "table_init();";
2013 $ret .= $table->exitlock_show(&$room->user, $table_pos);
2015 /* GENERAL STATUS */
2016 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
2017 $sendstep, $user->stat, $user->subst, $table_pos);
2019 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
2020 $sendstep, $user->stat, $user->subst, $table_pos));
2023 $ret .= "background_set();";
2026 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
2027 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
2028 xcape($room->user[$table->player[($table_pos)%PLAYERS_N]]->name),
2029 xcape($room->user[$table->player[($table_pos+1)%PLAYERS_N]]->name),
2030 xcape($room->user[$table->player[($table_pos+2)%PLAYERS_N]]->name),
2031 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+3)%PLAYERS_N]]->name)),
2032 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+4)%PLAYERS_N]]->name)));
2034 /* NOTIFY FOR THE CARD MAKER */
2035 if ($is_transition) { // && $user->subst == "asta" superfluo
2036 $ret .= show_table_info(&$room, &$table, $table_pos);
2039 $ret .= table_wellcome($user);
2041 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
2042 $ret .= playsound("cow.mp3");
2047 if ($is_transition) { // && $user->subst == "asta" superfluo
2050 for ($i = 0 ; $i < 8 ; $i++) {
2051 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
2053 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
2054 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
2060 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
2062 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
2063 $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
2064 $table->card[$o]->value : -1),
2065 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
2070 $taked = array(0,0,0,0,0);
2071 $inhand = array(0,0,0,0,0);
2072 $ontabl = array(-1,-1,-1,-1,-1);
2075 for ($i = 0 ; $i < 40 ; $i++) {
2076 if ($table->card[$i]->stat == 'hand') {
2077 if ($table->card[$i]->owner == $table_pos) {
2078 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
2080 $inhand[$table->card[$i]->owner]++;
2082 else if ($table->card[$i]->stat == 'take') {
2083 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
2084 $taked[$table->card[$i]->owner]++;
2086 else if ($table->card[$i]->stat == 'table') {
2087 $ontabl[$table->card[$i]->owner] = $i;
2091 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2092 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
2094 log_main("Stat table: ".$logg);
2096 /* Set ours cards. */
2098 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
2099 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
2100 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
2101 $oursarg .= ($i == 0 ? "" : ", ")."-1";
2102 $ret .= sprintf('card_setours(%s);', $oursarg);
2104 /* Dispose all cards */
2105 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2106 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
2107 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
2108 $inhand[$i], $taked[$i]);
2110 if ($ontabl[$i] != -1) {
2111 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
2112 $table->card[$ontabl[$i]]->value,
2113 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
2119 if ($user->subst == 'asta') {
2121 /* show users auction status */
2123 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2124 $user_cur = &$room->user[$table->player[$i]];
2125 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
2126 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
2129 $showst .= ",-2,-2";
2130 $ret .= sprintf('show_astat(%s);', $showst);
2132 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
2133 /* show card chooser */
2134 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
2139 if ($table_pos == ($table->gstart % PLAYERS_N) &&
2140 $table->asta_win == -1)
2141 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2142 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
2144 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2145 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
2149 if ($table->asta_win == -1) { // auction case
2150 if ($table_pos == ($table->gstart % PLAYERS_N))
2151 $ret .= "remark_on();";
2153 $ret .= "remark_off();";
2155 else { // chooseed case
2156 if ($table_pos == $table->asta_win)
2157 $ret .= "remark_on();";
2159 $ret .= "remark_off();";
2162 else if ($user->subst == 'game') {
2164 if (($table->gstart + $table->turn) % PLAYERS_N == $table_pos)
2165 $ret .= "is_my_time = true; remark_on();";
2167 $ret .= "remark_off();";
2169 /* WHO CALL AND WATH */
2170 $ret .= briscola_show($room, $table, $user);
2174 } // end function show_table(...
2176 function calculate_winner(&$table)
2183 $cur_seed = $table->briscola - ($table->briscola % 10);
2185 for ($i = 0 ; $i < 40 ; $i++) {
2186 if ($table->card[$i]->stat != "table")
2189 log_wr(sprintf("Card On table: [%d]", $i));
2191 $v = $table->card[$i]->value;
2192 $ontab[$table->card[$i]->owner] = $v;
2193 $ontid[$table->card[$i]->owner] = $i;
2194 /* se briscola setto il flag */
2195 if (($v - ($v % 10)) == $cur_seed)
2199 if ($briontab == FALSE) {
2200 $cur_win = $table->gstart;
2201 $cur_val = $ontab[$cur_win];
2202 $cur_seed = $cur_val - ($cur_val % 10);
2205 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2206 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
2207 if ($ontab[$i] < $cur_val) {
2208 $cur_val = $ontab[$i];
2214 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2215 $table->card[$ontid[$i]]->owner = $cur_win;
2216 $table->card[$ontid[$i]]->stat = "take"; // Card stat
2221 function calculate_points(&$table)
2223 GLOBAL $G_all_points;
2227 if ($table->asta_pnt == 60)
2228 $table->asta_pnt = 61;
2230 $table->old_reason = "";
2231 $table->old_win = $table->asta_win;
2232 $table->old_friend = $table->friend;
2233 $table->old_asta_pnt = $table->asta_pnt;
2235 for ($i = 0 ; $i < 40 ; $i++) {
2236 $ctt = $table->card[$i]->value % 10;
2237 $own = $table->card[$i]->owner;
2238 if ($own == $table->asta_win || $own == $table->friend)
2239 $pro += $G_all_points[$ctt];
2242 log_wr(sprintf("PRO: [%d]", $pro));
2245 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
2246 $table->points[$table->points_n % MAX_POINTS] = array();
2247 for ($i = 0 ; $i < PLAYERS_N ; $i++)
2248 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
2250 $table->old_pnt = $pro;
2256 if ($pro >= $table->asta_pnt)
2261 $table->points[$table->points_n % MAX_POINTS] = array();
2262 for ($i = 0 ; $i < 5 ; $i++) {
2263 if ($i == $table->asta_win)
2264 $pt = ($i == $table->friend ? 4 : 2);
2265 else if ($i == $table->friend)
2270 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
2272 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
2274 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
2276 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
2277 $table->total[$i] += $pt;
2280 $table->old_pnt = $pro;
2284 function validate_sess($sess)
2286 if (strlen($sess) == SESS_LEN)
2292 function validate_name($name)
2294 $name_new = str_replace(' ', '_', substr(trim($name),0,12));
2296 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
2298 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
2305 function playsound($filename)
2307 return (sprintf('playsound("flasou", "%s");', $filename));
2310 function secstoword($secs)
2312 $mins = floor($secs / 60);
2315 $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
2318 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));
2323 function sharedmem_sz($tok)
2325 if (($shm_id = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
2326 log_main("shmop_open failed");
2329 $shm_sz = shmop_size($shm_id);
2330 shmop_close($shm_id);
2332 log_main("shm_sz: ".$shm_sz." SHM_DIMS: ".SHM_DIMS);