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);
53 // define(DEBUGGING, "local");
57 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
58 $G_brisk_version = "spawn-0.0.6";
60 $root_wellarr = Array ( 'Benvenuto in brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: riscritta tutta l\'architettura dati del server, tutto piè veloce e leggero.',
61 'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
62 $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.');
66 <div style=\\"text-align: left; padding: 8px;\\">
67 <b>Descrizione</b><br>
68 Questa è un\'implementazione della briscola in cinque, così come è spiegata su
69 <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>
70 <b>Configurazione del browser.</b><br>
71 Occorre abilitare i cookies.<br>
73 <b>Uso del sito</b><br>
74 Potete sedervi a un tavolo o rimanere in piedi.<br>
75 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
78 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>
79 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
80 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
81 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
82 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
83 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.
85 <dt><b>Comandi della chat</b>
86 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
87 <dd>.. to be continue ..
93 <div id=\\"header\\" class=\\"header\\">
94 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
95 briscola chiamata in salsa ajax
97 <br><b>version '.$G_brisk_version.'</b><br><br>
98 Copyright 2006-2007 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
103 $from = array ( '\\', '@', '|' );
104 $to = array ( '\\\\', '@', '¦' );
106 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
111 var $value; /* 0 - 39 card value */
112 var $stat; /* 'bunch', 'hand', 'table', 'take' */
113 var $owner; /* (table position 0-4) */
114 // var $pos; /* Pos in hand. */
115 var $x; /* When played the X position on the table of the owner. */
116 var $y; /* When played the Y position on the table of the owner. */
118 function Card($value, $stat, $owner)
120 $this->value = $value;
121 $this->stat = $stat; // Card stat
122 $this->owner = $owner;
125 function assign($stat,$owner)
127 $this->stat = $stat; // Card stat
128 $this->owner = $owner;
131 function setpos($pos)
138 $this->stat = 'table'; // Card stat
143 function take($newown)
145 $this->stat = 'take'; // Card stat
146 $this->owner = $newown;
164 var $points; // points array
165 var $points_n; // number of row of points
189 if (($thiz =& new Table()) == FALSE)
192 $thiz->player = array();
195 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
196 $thiz->asta_pla_n= -1;
197 $thiz->asta_card = -1;
198 $thiz->asta_pnt = -1;
200 $thiz->points = array( );
202 $thiz->total = array( 0, 0, 0, 0, 0);
203 $thiz->asta_win = -1;
204 $thiz->briscola = -1;
207 $thiz->old_reason = "";
208 $thiz->old_asta_pnt = -1;
211 $thiz->old_friend = -1;
213 $thiz->table_token = "";
214 $thiz->table_start = 0;
219 function &clone(&$from)
223 if (($thiz =& new Table()) == FALSE)
226 $thiz->player = array();
227 for ($i = 0 ; $i < $from->player_n ; $i++)
228 $thiz->player[$i] = $from->player[$i];
229 $thiz->player_n = $from->player_n;
230 $thiz->card = $from->card;
231 $thiz->mazzo = $from->mazzo; // REVIEW
232 $thiz->gstart = $from->gstart;
233 $thiz->turn = $from->turn;
235 $thiz->asta_pla = $from->asta_pla;
236 $thiz->asta_pla_n = $from->asta_pla_n;
237 $thiz->asta_card = $from->asta_card;
238 $thiz->asta_pnt = $from->asta_pnt;
240 $thiz->mult = $from->mult;
241 $thiz->points = $from->points;
242 $thiz->points_n = $from->points_n;
243 $thiz->total = $from->total;
245 $thiz->asta_win = $from->asta_win;
246 $thiz->briscola = $from->briscola;
247 $thiz->friend = $from->friend;
249 $thiz->old_reason = $from->old_reason;
250 $thiz->old_asta_pnt = $from->old_asta_pnt;
251 $thiz->old_pnt = $from->old_pnt;
252 $thiz->old_win = $from->old_win;
253 $thiz->old_friend = $from->old_friend;
255 $thiz->table_token = $from->table_token;
256 $thiz->table_start = $from->table_start;
261 function &spawn(&$from)
265 if (($thiz =& new Table()) == FALSE)
268 $thiz->player_n = $from->player_n;
269 $thiz->card = &$thiz->bunch_create();
270 $thiz->mazzo = $from->mazzo;
271 $thiz->gstart = $from->gstart;
272 $thiz->turn = $from->turn;
274 $thiz->asta_pla = $from->asta_pla;
275 $thiz->asta_pla_n = $from->asta_pla_n;
276 $thiz->asta_card = $from->asta_card;
277 $thiz->asta_pnt = $from->asta_pnt;
279 $thiz->mult = $from->mult;
280 $thiz->points = $from->points;
281 $thiz->points_n = $from->points_n;
282 $thiz->total = $from->total;
284 $thiz->asta_win = $from->asta_win;
285 $thiz->briscola = $from->briscola;
286 $thiz->friend = $from->friend;
288 $thiz->old_reason = $from->old_reason;
289 $thiz->old_asta_pnt = $from->old_asta_pnt;
290 $thiz->old_pnt = $from->old_pnt;
291 $thiz->old_win = $from->old_win;
292 $thiz->old_friend = $from->old_friend;
294 // players are rearranged in an dedicated array
295 $thiz->player = array();
296 for ($i = 0 ; $i < $from->player_n ; $i++)
297 $thiz->player[$i] = $i;
299 $thiz->table_token = $from->table_token;
300 $thiz->table_start = $from->table_start;
305 function &bunch_create()
309 for ($i = 0 ; $i < 40 ; $i++) {
310 $ret[$i] =& new Card($i, 'bunch', 'no_owner');
317 function bunch_make()
319 $ct = array(0,0,0,0,0);
321 mt_srand(make_seed());
323 for ($i = 39 ; $i >= 0 ; $i--)
326 for ($i = 39 ; $i >= 0 ; $i--) {
330 log_main("RND ZERO");
335 $this->card[$id]->assign('hand', $owner);
337 $rest[$rn] = $rest[$i];
338 // $pubbpos[$rn2] = $pubbpos[$i];
342 function init(&$userarr)
344 $this->mazzo = rand(0,PLAYERS_N-1);
348 $this->old_reason = "";
349 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
350 $this->total[$i] = 0;
351 $user_cur = &$userarr[$this->player[$i]];
352 $user_cur->exitislock = TRUE;
355 log_main("table::init: ci siamo");
358 function game_init(&$userarr)
362 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
366 $this->asta_pla_n = PLAYERS_N;
367 $this->asta_card = -1;
368 $this->asta_pnt = 60;
369 $this->asta_win = -1;
370 $this->briscola = -1;
374 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
375 $this->asta_pla[$i] = TRUE;
376 $user_cur = &$userarr[$this->player[$i]];
377 $user_cur->subst = 'asta';
378 $user_cur->asta_card = -2;
379 $user_cur->asta_pnt = -1;
380 $user_cur->handpt = $this->hand_points($i);
381 // SEE function calculate_points(&$table)
387 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
390 function getPlayer($idx)
392 return ($this->player[$idx]);
395 function setPlayer($idx, $player)
397 $this->player[$idx] = $player;
400 function user_add($idx)
402 $this->player[$this->player_n] = $idx;
405 return ($this->player_n - 1);
408 function user_rem(&$room, &$user)
410 $tabpos = $user->table_pos;
412 /* verifico la consistenza dei dati */
413 if ($room->user[$this->player[$tabpos]] == $user) {
415 /* aggiorna l'array dei giocatori al tavolo. */
416 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
417 $this->player[$i] = $this->player[$i+1];
418 $user_cur = &$room->user[$this->player[$i]];
419 $user_cur->table_pos = $i;
424 log_main("INCONSISTENCY ON TABLE.");
428 function hand_points($idx)
430 GLOBAL $G_all_points;
434 for ($i = 0 ; $i < 40 ; $i++) {
435 if ($this->card[$i]->owner != $idx)
438 $ctt = $this->card[$i]->value % 10;
439 $tot += $G_all_points[$ctt];
445 function exitlock_show(&$userarr, $table_pos)
447 $ct = $this->exitlock_calc(&$userarr, $table_pos);
449 $ret = sprintf('exitlock_show(%d, %s);', $ct,
450 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
454 function exitlock_calc(&$userarr, $table_pos)
458 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
459 if ($userarr[$this->player[$i]]->exitislock == FALSE)
468 var $name; // name of the user
469 var $sess; // session of the user
470 var $ip; // ip of the user
471 var $lacc; // last access (for the cleanup)
472 var $laccwr; // last access (for the cleanup)
473 var $bantime; // timeout to temporary ban
474 var $stat; // status (outdoor, room, table, game, ...)
475 var $subst; // substatus for each status
476 var $step; // step of the current status
477 var $trans_step; // step to enable transition between pages (disable == -1)
478 var $comm; // commands array
481 var $handpt; // Total card points at the beginning of the current hand.
482 var $exitislock; // Player can exit from the table ?
483 var $table; // id of the current table (if in table state)
484 var $table_pos; // idx on the table
485 var $table_token;// token that identify a game on a table
486 var $the_end; // Flag to change the end of the session
491 function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
494 if (($thiz =& new User()) == FALSE)
500 $thiz->lacc = time();
501 $thiz->laccwr = time();
504 $thiz->subst = $subst;
506 $thiz->trans_step = -1;
507 $thiz->comm = array();
508 $thiz->asta_card = -2;
509 $thiz->asta_pnt = -1;
511 $thiz->exitislock = TRUE;
513 $thiz->table = $table;
514 $thiz->table_pos = -1;
515 $thiz->table_token = "";
520 function &clone(&$from)
524 if (($thiz =& new User()) == FALSE)
527 $thiz->name = $from->name;
528 $thiz->sess = $from->sess;
529 $thiz->ip = $from->ip;
530 $thiz->lacc = $from->lacc;
531 $thiz->laccwr = $from->laccwr;
532 $thiz->bantime = $from->bantime;
533 $thiz->stat = $from->stat;
534 $thiz->subst = $from->subst;
535 $thiz->step = $from->step;
536 $thiz->trans_step = $from->trans_step;
537 $thiz->comm = array();
539 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
540 for ($i = $i_start ; $i < $from->step ; $i++) {
542 $thiz->comm[$ii] = $from->comm[$ii];
544 $thiz->asta_card = $from->asta_card;
545 $thiz->asta_pnt = $from->asta_pnt;
546 $thiz->handpt = $from->handpt;
547 $thiz->exitislock = $from->exitislock;
548 $thiz->table = $from->table;
549 $thiz->table_pos = $from->table_pos;
550 $thiz->table_token = $from->table_token;
551 $thiz->the_end = $from->the_end;
556 function &spawn(&$from, $table, $table_pos)
560 if (($thiz =& new User()) == FALSE)
563 $thiz->name = $from->name;
564 $thiz->sess = $from->sess;
565 $thiz->ip = $from->ip;
566 $thiz->lacc = $from->lacc;
567 $thiz->laccwr = $from->laccwr;
568 $thiz->bantime = $from->bantime;
569 $thiz->stat = $from->stat;
570 $thiz->subst = $from->subst;
571 $thiz->step = $from->step;
572 $thiz->trans_step = $from->trans_step;
573 $thiz->comm = array();
575 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
576 for ($i = $i_start ; $i < $from->step ; $i++) {
577 log_wr("TRY PUSH:".$i);
579 $thiz->comm[$ii] = $from->comm[$ii];
582 $thiz->asta_card = $from->asta_card;
583 $thiz->asta_pnt = $from->asta_pnt;
584 $thiz->handpt = $from->handpt;
585 $thiz->exitislock = $from->exitislock;
586 $thiz->the_end = $from->the_end;
588 $thiz->table = $table;
589 $thiz->table_pos = $table_pos;
590 $thiz->table_token = $from->table_token;
595 function stat_set($stat) {
596 log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]");
597 $this->stat = "$stat";
600 if (validate_sess($this->sess)) {
601 $fp = fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
602 fwrite($fp, sprintf("%s\n",$this->stat));
608 function step_set($step) {
612 if (validate_sess($this->sess) == FALSE)
614 if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
616 fwrite($fp, pack("l",$this->step), 4);
625 function step_inc() {
628 if (validate_sess($this->sess)) {
629 $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
630 fwrite($fp, pack("l",$this->step), 4);
641 function step_get($sess) {
645 if (validate_sess($sess) == FALSE)
648 if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
651 if (($s = fread($fp, 4)) == FALSE)
657 $arr = unpack('l', $s);
660 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
667 log_rd2("STEP_GET: return false ".$ct);
671 function step_unproxy($sess) {
672 log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
673 @unlink(PROXY_PATH."/".$sess.".step");
680 var $comm; // commands for many people
681 var $step; // current step of the comm array
682 var $garbage_timeout;
685 $this->user = array();
686 $this->table = array();
688 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
689 $this->user[$i] =& User::create("", "");
692 for ($i = 0 ; $i < TABLES_N ; $i++)
693 $this->table[$i] =& Table::create();
694 $this->garbage_timeout = 0;
697 function garbage_manager($force)
701 log_rd2("garbage_manager START");
703 /* Garbage collector degli utenti in timeout */
705 if ($force || $this->garbage_timeout < $curtime) {
707 // Before all align times with table timeout
708 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
709 $table_cur =& $this->table[$table_idx];
710 // if the table is complete and exists its shared mem we get the info about users lacc
712 if ($table_cur->player_n == PLAYERS_N) {
713 log_main("PLAYERS == N TABLE ".$table_idx);
715 if (($sem = Briskin5::lock_data($table_idx)) != FALSE) {
716 log_main("bin5 lock data success");
718 $no_recovery = FALSE;
719 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
720 if ($table_cur->table_token != $bri->table_token) {
721 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
722 log_main("ERROR: not matching table_start. Room: ".$table_cur->table_start." Table: ".$bri->table_start);
732 log_main("garbage_manager: bri loaded successfully.");
733 $bri->garbage_manager(TRUE);
735 $bri_table = &$bri->table[0];
737 // is the end of the table
738 if ($bri->the_end == TRUE) {
740 * DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN
742 log_main("garbage_manager: INSIDE THE END.");
744 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
745 // stat must be "table" by definition
746 $user_cur =& $this->user[$table_cur->player[$i]];
747 $bri_user =& $bri->user[$i];
749 $user_cur->subst = $bri_user->subst;
750 $user_cur->step = $bri_user->step;
751 $user_cur->lacc = $bri_user->lacc;
752 $user_cur->laccwr = $bri_user->lacc;
753 $user_cur->bantime = $bri_user->bantime;
756 $this->room_join_wakeup(&$user_cur, FALSE, 0);
757 $table_cur->table_token = "";
759 Briskin5::destroy_data($table_idx);
762 log_main("gm:: save_data");
764 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
765 $this->user[$table_cur->player[$i]]->lacc = $bri->user[$i]->lacc;
768 Briskin5::save_data(&$bri);
770 } // else if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
771 else if ($no_recovery == FALSE) {
772 log_main("ERROR: table ".$table_idx." unrecoverable join");
774 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
775 $user_cur = &$this->user[$table_cur->player[$i]];
776 $user_cur->subst = "shutdowner";
777 $user_cur->step_inc();
779 $ret = sprintf('stat = "%s"; subst = "%s";', $cur_user->stat, $cur_user->subst);
780 $ret .= "gst.st = ".($user_cur->step+1)."; ";
781 $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);
782 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
783 $user_cur->step_inc();
786 $this->room_join_wakeup(&$user_cur, TRUE, -2);
787 $table_cur->table_token = "";
790 Briskin5::unlock_data($sem);
792 } // if ($table_cur->player_n == PLAYERS_N) {
793 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
795 log_rd2("out new loop.");
797 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
798 $user_cur = &$this->user[$i];
800 log_rd2("User: ".$user_cur->name." stat: ".$user_cur->stat." subst: ".$user_cur->subst);
802 if ($user_cur->sess == "")
805 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
806 // Auto logout dell'utente
807 log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
809 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
810 log_auth($user_cur->sess, "Autologout session.");
812 $tmp_sess = $user_cur->sess;
813 $user_cur->sess = "";
814 step_unproxy($tmp_sess);
815 $user_cur->name = "";
817 $user_cur->the_end = FALSE;
819 log_rd2("AUTO LOGOUT.");
820 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
821 $this->room_wakeup(&$user_cur);
822 else if ($user_cur->subst == 'standup')
823 $this->room_outstandup(&$user_cur);
825 log_rd2("LOGOUT FROM WHAT ???");
829 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
830 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
831 $this->room_wakeup(&$user_cur);
832 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
833 $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);
834 $user_cur->step_inc();
838 log_rd2("GARBAGE UPDATED!");
840 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
848 function room_wakeup(&$user)
850 $table_idx = $user->table;
851 $table = &$this->table[$table_idx];
853 log_main("WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
857 $from_table = ($user->stat == "table");
859 log_main("WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
861 for ($i = 0 ; $i < $table->player_n ; $i++) {
862 $user_cur = &$this->user[$table->player[$i]];
863 log_main("PREIMPOST: INLOOP name: ".$user_cur->name);
865 if ($user_cur != $user) {
866 $user_cur->stat_set("room");
867 $user_cur->subst = "sitdown";
868 $user_cur->laccwr = $curtime;
870 else if ($user->sess != "") {
871 $user_cur->stat_set("room");
872 $user_cur->subst = "standup";
873 $user_cur->laccwr = $curtime;
874 $user_cur->table = -1;
879 $user->stat_set("room");
880 $user->subst = "standup";
881 $user->laccwr = $curtime;
883 /* aggiorna l'array dei giocatori al tavolo. */
884 $table->user_rem(&$this, &$user);
886 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
887 $user_cur = &$this->user[$i];
888 if ($user_cur->sess == '' || $user_cur->stat != 'room')
891 log_main("VALORI: name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
893 $ret = "gst.st = ".($user_cur->step+1)."; ";
894 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
895 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
896 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
897 log_main("DOCUMENT.index.php: from table");
899 else if ($user_cur->stat == "room") {
900 log_main("DOCUMENT.index.php: from table");
902 $ret .= table_content($this, $user_cur, $table_idx);
903 $ret .= standup_content($this, $user_cur);
905 $act_content = table_act_content(FALSE, 0, $table_idx, $user->table);
906 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
909 if ($user_cur == $user) {
910 // set the new status
911 $ret .= 'subst = "standup"; ';
912 // clean the action buttons in other tables
913 for ($e = 0 ; $e < TABLES_N ; $e++) {
914 if ($this->table[$e]->player_n < PLAYERS_N)
915 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
919 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
920 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
923 log_wr("ROOM_WAKEUP: ".$ret);
924 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
925 $user_cur->step_inc();
929 function room_join_wakeup(&$user, $update_lacc = FALSE, $trans_delta)
931 $table_idx = $user->table;
932 $table = &$this->table[$table_idx];
934 log_main("JOIN_WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
941 log_main("JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
943 for ($i = 0 ; $i < $table->player_n ; $i++) {
944 $user_cur = &$this->user[$table->player[$i]];
945 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
946 if ($user_cur->sess != "") {
947 if ($update_lacc == TRUE) {
948 $user_cur->laccwr = $curtime;
950 log_main("cur: ".$user_cur->name." subst: ".$user_cur->subst);
951 if ($user_cur->subst == "shutdowned") {
952 $user_cur->stat_set("room");
953 $user_cur->subst = "sitdown";
955 else if ($user_cur->subst == "shutdowner") {
956 $user_cur->stat_set("room");
957 $user_cur->subst = "standup";
958 $user_cur->table = -1;
959 $user_wup[$user_wup_n++] = &$user_cur;
961 $user_tab[$user_tab_n++] = &$user_cur;
965 for ($wup_idx = 0 ; $wup_idx < $user_wup_n ; $wup_idx++)
966 $table->user_rem(&$this, &$user_wup[$wup_idx]);
968 /* aggiorna l'array dei giocatori al tavolo. */
970 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
971 log_main("START LOOP");
972 $user_cur = &$this->user[$i];
973 if ($user_cur->sess == '' || $user_cur->stat != 'room') {
974 log_main("name: ".$user_cur->name."skip subst: ".$user_cur->subst);
979 log_main("VALORI name: ".$user_cur->name." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
981 $ret = "gst.st = ".($user_cur->step+1)."; ";
982 if ($user_cur->stat == "room") {
983 log_main("DOCUMENT.index.php from table");
985 $ret .= table_content($this, $user_cur, $table_idx);
986 $ret .= standup_content($this, $user_cur);
988 $act_content = table_act_content(FALSE, 0, $table_idx, $user_cur->table);
989 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
991 for ($tab_idx = 0 ; $tab_idx < $user_tab_n ; $tab_idx++)
992 if ($user_cur == $user_tab[$tab_idx])
995 // for users that wakeup the room will be reconstructed by index_rd.php
996 if ($tab_idx < $user_tab_n) {
997 log_main("PRE show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
998 $user_cur->trans_step = $user_cur->step + 1 + $trans_delta;
999 $user_cur->comm[$user_cur->step % COMM_N] = "";
1000 $user_cur->step_inc();
1001 $user_cur->comm[$user_cur->step % COMM_N] = show_room(&$this, ($user_cur->step + 1), &$user_cur);
1002 $user_cur->step_inc();
1003 log_main("POST show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1007 log_main("JOIN_WAKEUP wup_idx ".$wup_idx." wup_n ".$user_wup_n);
1009 log_main("JOIN_WAKEUP more");
1010 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
1011 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1012 log_main("JOIN_WAKEUP end more");
1014 log_wr("ROOM_JOIN_WAKEUP: ".$ret);
1015 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1016 $user_cur->step_inc();
1020 function room_outstandup(&$user)
1022 $this->room_sitdown(&$user, -1);
1025 function table_update(&$user)
1027 log_main("table_update: pre - USER: ".$user->name);
1029 $table_idx = $user->table;
1031 if ($table_idx > -1)
1032 $table = &$this->table[$table_idx];
1034 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1036 $user_cur = &$this->user[$i];
1037 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1040 $ret = "gst.st = ".($user_cur->step+1)."; ";
1041 if ($table_idx > -1)
1042 $ret .= table_content($this, $user_cur, $table_idx);
1044 if ($user_cur == $user) {
1045 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
1047 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1048 $user_cur->step_inc();
1051 log_main("table_update: post");
1054 function room_sitdown(&$user, $table_idx)
1056 log_main("room_sitdown ".($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
1058 if ($table_idx > -1)
1059 $table = &$this->table[$table_idx];
1061 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1063 $user_cur = &$this->user[$i];
1064 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1067 $ret = "gst.st = ".($user_cur->step+1)."; ";
1068 if ($table_idx > -1)
1069 $ret .= table_content($this, $user_cur, $table_idx);
1070 $ret .= standup_content($this, $user_cur);
1072 if ($user_cur == $user) {
1073 $ret .= 'subst = "sitdown"; ';
1074 // clean the action buttons in other tables
1075 for ($e = 0 ; $e < TABLES_N ; $e++) {
1076 $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
1077 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
1080 else if ($table_idx > -1) {
1081 if ($table->player_n == PLAYERS_N) {
1082 $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
1083 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1086 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1087 $user_cur->step_inc();
1091 function chatt_send(&$user, $mesg)
1093 if ($user->stat == 'table') {
1094 $table = &$this->table[$user->table];
1097 $user_mesg = substr($mesg,6);
1101 $dt = date("H:i ", $timecur);
1102 if (strncmp($user_mesg, "/nick ", 6) == 0) {
1103 log_main("chatt_send BEGIN");
1105 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
1106 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1107 $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));
1112 $user_mesg = "COMMAND ".$user_mesg;
1115 // update local graph
1116 // update remote graphs
1117 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1118 $user_cur = &$this->user[$i];
1119 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1120 if ($user_cur->sess == '')
1122 if ($user_cur->name == $name_new) {
1123 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1124 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
1129 if ($i == MAX_PLAYERS) {
1130 $user->name = $name_new;
1132 log_main("chatt_send start set");
1135 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1136 log_main("chatt_send set loop");
1138 $user_cur = &$this->user[$i];
1139 if ($user_cur->sess == '')
1141 if ($user_cur->stat == 'room') {
1142 if ($user->stat == 'room' && $user->subst == 'standup') {
1143 $this->standup_update(&$user);
1145 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
1146 $user->stat == 'table') {
1147 log_main("chatt_send pre table update");
1149 $this->table_update(&$user);
1151 log_main("chatt_send post table update");
1154 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
1155 $table = &$this->table[$user->table];
1157 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1158 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1159 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
1160 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
1161 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
1162 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
1163 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
1164 if ($user_cur == $user)
1165 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
1166 xcape($user->name,ENT_COMPAT,"UTF-8"));
1167 $user_cur->step_inc();
1173 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
1174 if ($user->stat == 'room') {
1175 $user_cur = &$this->user[$i];
1176 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1180 $user_cur = &$this->user[$table->player[$i]];
1183 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1184 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
1185 $dt.xcape($user->name), xcape($user_mesg));
1186 $user_cur->step_inc();
1188 log_legal($timecur, $user->sess, $user->name,
1189 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
1193 function &get_user($sess, &$idx)
1195 GLOBAL $PHP_SELF, $G_false;
1197 if (validate_sess($sess)) {
1198 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1199 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1202 $ret = &$this->user[$i];
1206 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1207 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
1208 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1211 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1218 * function &add_user(&$room, &$sess, &$idx, $name, $ip)
1221 * if ($idx != -1 && ret == FALSE) => duplicated nick
1222 * if ($idx == -2 && ret == FALSE) => invalid name
1223 * if ($idx == -1 && ret == FALSE) => no space left
1224 * if (ret == TRUE) => SUCCESS
1226 function &add_user(&$sess, &$idx, $name, $ip)
1233 if (($name_new = validate_name($name)) == FALSE) {
1238 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
1239 if (validate_sess($sess) == FALSE)
1242 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1244 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1248 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
1251 if (strcmp($this->user[$i]->name, $name_new) == 0) {
1259 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1261 if ($idx != -1 && $i == MAX_PLAYERS) {
1265 $this->user[$idx]->sess = uniqid("");
1266 $sess = $this->user[$idx]->sess;
1270 $this->user[$idx]->sess = $sess;
1272 $this->user[$idx]->name = $name_new;
1273 $this->user[$idx]->stat_set("room");
1274 $this->user[$idx]->step_set(0);
1275 while (array_pop($this->user[$idx]->comm) != NULL);
1276 $this->user[$idx]->subst = "standup";
1277 $this->user[$idx]->lacc = $curtime;
1278 $this->user[$idx]->laccwr = $curtime;
1279 $this->user[$idx]->bantime = 0;
1280 $this->user[$idx]->ip = $ip;
1281 log_main(sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1283 return ($this->user[$idx]);
1289 function standup_update(&$user)
1291 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1292 $user_cur = &$this->user[$i];
1293 if ($user_cur->sess == '')
1296 log_main("STANDUP START: ".$user_cur->stat);
1298 if ($user_cur->stat == 'room') {
1299 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
1300 if ($user_cur == $user)
1301 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
1303 log_main("FROM STANDUP: NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
1305 $user_cur->step_inc();
1311 function &init_data()
1313 $room =& new Room();
1319 function &load_data()
1321 GLOBAL $G_false, $sess;
1323 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
1328 if ($shm = shm_attach($tok, SHM_DIMS)) {
1329 $room = @shm_get_var($shm, $tok);
1331 log_only("bri == ".($room == FALSE ? "FALSE" : "TRUE")." bri === ".($room === FALSE ? "FALSE" : "TRUE")." bri isset ".(isset($room) ? "TRUE" : "FALSE"));
1333 log_only("bri count ".count($room));
1335 if ($room == FALSE) {
1336 log_only("INIT MAIN DATA");
1338 $room =& Room::init_data();
1339 if (shm_put_var($shm, $tok, $room) == FALSE) {
1340 log_only("PUT_VAR FALLITA ".strlen(serialize($room)));
1341 log_only(serialize($room));
1355 function save_data(&$room)
1365 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1)
1371 if (($shm = shm_attach($tok, SHM_DIMS)) == FALSE)
1374 // log_only("PUT_VAR DI ".strlen(serialize($room)));
1375 if (shm_put_var($shm, $tok, $room) == FALSE) {
1376 log_only("PUT_VAR FALLITA ".strlen(serialize($room)));
1377 log_only(serialize($room));
1380 // log_main("QUI CI ARRIVA [".$room->user[0]->name."]");
1390 function lock_data()
1394 // echo "LOCK: ".FTOK_PATH."/main";
1396 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
1400 // echo "FTOK ".$tok."<br>";
1401 if (($res = sem_get($tok)) == FALSE) {
1402 echo "SEM_GET FAILED";
1405 if (sem_acquire($res)) {
1406 log_lock("LOCK room");
1413 function unlock_data($res)
1417 log_lock("UNLOCK room");
1419 return (sem_release($res));
1423 function make_seed()
1425 list($usec, $sec) = explode(' ', microtime());
1426 return (float) $sec + ((float) $usec * 100000);
1430 function log_only2($log) {
1431 if ((BRISK_DEBUG & DBG_ONL2) == 0)
1436 if (isset($sess) == FALSE)
1441 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1442 fwrite($fp, sprintf("ONL2: [%s] [%s]\n", $ssess, $log));
1447 function log_only($log) {
1448 if ((BRISK_DEBUG & DBG_ONLY) == 0)
1453 if (isset($sess) == FALSE)
1458 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1459 fwrite($fp, sprintf("ONLY: [%s] [%s]\n", $ssess, $log));
1464 function log_main($log) {
1465 if ((BRISK_DEBUG & DBG_MAIN) == 0)
1470 if (isset($sess) == FALSE)
1475 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1476 fwrite($fp, sprintf("MAIN: [%s] [%s]\n", $ssess, $log));
1481 function log_rd($log) {
1482 if ((BRISK_DEBUG & DBG_READ) == 0)
1487 if (isset($sess) == FALSE)
1492 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1493 fwrite($fp, sprintf("READ: [%s] [%s]\n", $ssess, $log));
1498 function log_rd2($log) {
1499 if ((BRISK_DEBUG & DBG_REA2) == 0)
1504 if (isset($sess) == FALSE)
1510 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1511 fwrite($fp, sprintf("REA2: [%s] [%s]\n", $ssess, $log));
1516 function log_send($log) {
1517 if ((BRISK_DEBUG & DBG_SEND) == 0)
1522 if (isset($sess) == FALSE)
1527 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1528 fwrite($fp, sprintf("SEND: [%s] [%s]\n", $ssess, $log));
1533 function log_lock($log) {
1534 if ((BRISK_DEBUG & DBG_LOCK) == 0)
1539 if (isset($sess) == FALSE)
1544 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1545 fwrite($fp, sprintf("LOCK: [%s] [%s]\n", $ssess, $log));
1550 function log_wr($log) {
1551 if ((BRISK_DEBUG & DBG_WRIT) == 0)
1556 if (isset($sess) == FALSE)
1561 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1562 fwrite($fp, sprintf("WRIT: [%s] [%s]\n", $ssess, $log));
1567 function log_load($log) {
1568 if ((BRISK_DEBUG & DBG_LOAD) == 0)
1573 if (isset($sess) == FALSE)
1578 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1579 fwrite($fp, sprintf("LOAD: [%s] [%s]\n", $ssess, $log));
1584 function log_auth($sess, $log) {
1585 if ((BRISK_DEBUG & DBG_AUTH) == 0)
1588 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1589 fwrite($fp, sprintf("AUTH: [%s] [%d] [%s]\n", $sess, time(), $log));
1595 function log_legal($timecur, $sess, $name, $where, $mesg)
1599 if (($fp = @fopen(LEGAL_PATH."/legal.log", 'a')) != FALSE) {
1600 /* Unix time | session | nickname | IP | where was | mesg */
1601 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|\n", $timecur, $sess, $name, $_SERVER['REMOTE_ADDR'], $where , $mesg));
1609 function lock_banlist()
1611 if (($tok = ftok(FTOK_PATH."/main", "L")) == -1) {
1615 if (($res = sem_get($tok)) == FALSE) {
1616 echo "SEM_GET FAILED";
1619 if (sem_acquire($res))
1625 function unlock_banlist($res)
1627 return (sem_release($res));
1630 function table_act_content($isstanding, $sitted, $table, $cur_table)
1635 if ($sitted < PLAYERS_N) {
1636 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
1640 if ($table == $cur_table)
1641 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
1648 function table_content($room, $user, $table_idx)
1654 // Si possono usare i dati nella classe table
1657 $sess = $user->sess;
1658 $table = &$room->table[$table_idx];
1660 if ($user->stat != 'room')
1663 for ($i = 0 ; $i < $table->player_n ; $i++) {
1664 $user_cur = &$room->user[$table->player[$i]];
1666 if ($user_cur == $user)
1667 { $hilion = "<b>"; $hilioff = "</b>"; }
1669 { $hilion = ""; $hilioff = ""; }
1671 log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
1673 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
1676 for ( ; $i < PLAYERS_N ; $i++)
1680 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
1685 function standup_content(&$room, $user)
1690 if ($user->stat != 'room')
1693 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
1694 if ($room->user[$e]->sess == "" || $room->user[$e]->stat != "room" || $room->user[$e]->name == "")
1699 $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
1701 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
1702 if ($room->user[$e]->sess == "" || $room->user[$e]->stat != "room" || $room->user[$e]->name == "")
1706 if ($room->user[$e]->subst == "standup") {
1707 if (($ct % 4) == 0) {
1710 if ($room->user[$e] == $user)
1711 { $hilion = "<b>"; $hilioff = "</b>"; }
1713 { $hilion = ""; $hilioff = ""; }
1715 $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($room->user[$e]->name), $hilioff);
1716 if (($ct % 4) == 3) {
1717 $content .= '</tr>';
1722 $content .= '</table>';
1724 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
1725 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
1726 $content, $content2);
1732 function show_notify($text, $tout, $butt, $w, $h)
1734 log_main("SHOW_NOTIFY: ".$text);
1735 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
1738 function briscola_show($room, $table, $user)
1743 if ($table->asta_card == 9)
1744 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1746 /* text of caller cell */
1747 if ($user->table_pos == $table->asta_win)
1748 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1750 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
1751 xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
1753 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1755 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1756 $ret .= sprintf('$("caller").style.visibility = "visible";');
1757 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1758 $ret .= sprintf('$("astalascio").style.visibility = "";');
1759 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1760 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1766 function game_result($asta_pnt, $pnt)
1768 if ($asta_pnt == 61) {
1771 else if ($pnt == 60)
1777 if ($pnt >= $asta_pnt)
1784 function multoval($mult)
1788 else if ($mult == 4)
1789 return ("quadruplo");
1791 return (sprintf("%d-plo", $mult));
1794 function show_table_info(&$room, &$table, $table_pos)
1797 $user = &$room->user[$table->player[$table_pos]];
1799 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1800 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1803 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1804 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
1805 $noty .= sprintf("</tr>");
1808 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1810 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1811 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1812 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1813 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1818 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1819 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1820 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1821 $noty .= "</tr></table>";
1823 if ($table->old_reason != "") {
1824 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1827 if ($table->old_win != -1) {
1828 $win = $table->player[$table->old_win];
1829 $fri = $table->player[$table->old_friend];
1831 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1834 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
1835 xcape($room->user[$win]->name),
1836 xcape($room->user[$fri]->name));
1837 if ($table->old_pnt == 120) {
1838 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1841 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1842 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1843 'più di 60'), $table->old_pnt,
1844 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1848 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1849 xcape($room->user[$win]->name));
1850 if ($table->old_pnt == 120) {
1851 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1854 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1855 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1856 'più di 60'), $table->old_pnt,
1857 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1861 if ($table->mazzo == $table_pos)
1862 $noty .= "Fai <b>tu</b> il mazzo,";
1864 $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
1865 $noty .= "Il mazzo a <b>$unam</b>,";
1868 if ($user->subst == 'asta') {
1869 if ($table->asta_win == -1) // auction case
1870 $curplayer = $table->gstart % PLAYERS_N;
1872 $curplayer = $table->asta_win;
1874 else if ($user->subst == 'game') {
1875 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
1879 if ($curplayer == $table_pos) {
1880 $noty .= " tocca a <b>te</b> giocare.";
1883 $unam = xcape($room->user[$table->player[$curplayer]]->name);
1884 $noty .= " tocca a <b>$unam</b> giocare.";
1887 if ($table->mult > 1) {
1888 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1890 $noty .= "<hr><br>";
1892 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
1897 function root_wellcome($user)
1899 GLOBAL $root_wellarr;
1902 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
1903 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $root_wellarr[$i]));
1908 function table_wellcome($user)
1910 GLOBAL $table_wellarr;
1913 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1914 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
1919 function show_room(&$room, $user_step, &$user)
1921 log_main("show_room: username: ".$user->name);
1924 $ret = sprintf('gst.st = %d;', $user_step);
1925 $ret .= sprintf('stat = "%s";', $user->stat);
1927 $ret .= root_wellcome($user);
1928 $ret .= sprintf('subst = "%s";', $user->subst);
1929 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1930 for ($i = 0 ; $i < TABLES_N ; $i++) {
1931 $ret .= table_content($room, $user, $i);
1932 $act_content = table_act_content(($user->subst == 'standup'),
1933 $room->table[$i]->player_n, $i, $user->table);
1934 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1936 $ret .= standup_content($room, $user);
1944 is_transition (is from room to table ?)
1945 is_again (is another game)
1947 Examples of $is_transition, $is_again:
1948 from reload of the page: FALSE, FALSE
1949 from sitdown in room: TRUE, FALSE
1950 from table: asta cmd e tutti passano: TRUE, TRUE
1951 from table: fine partita: TRUE, TRUE
1953 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
1955 $table_idx = $user->table;
1956 $table = &$room->table[$table_idx];
1957 $table_pos = $user->table_pos;
1959 $ret = "table_init();";
1960 $ret .= $table->exitlock_show(&$room->user, $table_pos);
1962 /* GENERAL STATUS */
1963 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1964 $sendstep, $user->stat, $user->subst, $table_pos);
1966 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1967 $sendstep, $user->stat, $user->subst, $table_pos));
1970 $ret .= "background_set();";
1973 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1974 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1975 xcape($room->user[$table->player[($table_pos)%PLAYERS_N]]->name),
1976 xcape($room->user[$table->player[($table_pos+1)%PLAYERS_N]]->name),
1977 xcape($room->user[$table->player[($table_pos+2)%PLAYERS_N]]->name),
1978 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+3)%PLAYERS_N]]->name)),
1979 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+4)%PLAYERS_N]]->name)));
1981 /* NOTIFY FOR THE CARD MAKER */
1982 if ($is_transition) { // && $user->subst == "asta" superfluo
1983 $ret .= show_table_info(&$room, &$table, $table_pos);
1986 $ret .= table_wellcome($user);
1988 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1989 $ret .= playsound("cow.mp3");
1994 if ($is_transition) { // && $user->subst == "asta" superfluo
1997 for ($i = 0 ; $i < 8 ; $i++) {
1998 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
2000 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
2001 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
2007 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
2009 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
2010 $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
2011 $table->card[$o]->value : -1),
2012 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
2017 $taked = array(0,0,0,0,0);
2018 $inhand = array(0,0,0,0,0);
2019 $ontabl = array(-1,-1,-1,-1,-1);
2022 for ($i = 0 ; $i < 40 ; $i++) {
2023 if ($table->card[$i]->stat == 'hand') {
2024 if ($table->card[$i]->owner == $table_pos) {
2025 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
2027 $inhand[$table->card[$i]->owner]++;
2029 else if ($table->card[$i]->stat == 'take') {
2030 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
2031 $taked[$table->card[$i]->owner]++;
2033 else if ($table->card[$i]->stat == 'table') {
2034 $ontabl[$table->card[$i]->owner] = $i;
2038 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2039 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
2041 log_main("Stat table: ".$logg);
2043 /* Set ours cards. */
2045 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
2046 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
2047 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
2048 $oursarg .= ($i == 0 ? "" : ", ")."-1";
2049 $ret .= sprintf('card_setours(%s);', $oursarg);
2051 /* Dispose all cards */
2052 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2053 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
2054 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
2055 $inhand[$i], $taked[$i]);
2057 if ($ontabl[$i] != -1) {
2058 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
2059 $table->card[$ontabl[$i]]->value,
2060 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
2066 if ($user->subst == 'asta') {
2068 /* show users auction status */
2070 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2071 $user_cur = &$room->user[$table->player[$i]];
2072 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
2073 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
2076 $showst .= ",-2,-2";
2077 $ret .= sprintf('show_astat(%s);', $showst);
2079 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
2080 /* show card chooser */
2081 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
2086 if ($table_pos == ($table->gstart % PLAYERS_N) &&
2087 $table->asta_win == -1)
2088 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2089 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
2091 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2092 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
2096 if ($table->asta_win == -1) { // auction case
2097 if ($table_pos == ($table->gstart % PLAYERS_N))
2098 $ret .= "remark_on();";
2100 $ret .= "remark_off();";
2102 else { // chooseed case
2103 if ($table_pos == $table->asta_win)
2104 $ret .= "remark_on();";
2106 $ret .= "remark_off();";
2109 else if ($user->subst == 'game') {
2111 if (($table->gstart + $table->turn) % PLAYERS_N == $table_pos)
2112 $ret .= "is_my_time = true; remark_on();";
2114 $ret .= "remark_off();";
2116 /* WHO CALL AND WATH */
2117 $ret .= briscola_show($room, $table, $user);
2121 } // end function show_table(...
2123 function calculate_winner(&$table)
2130 $cur_seed = $table->briscola - ($table->briscola % 10);
2132 for ($i = 0 ; $i < 40 ; $i++) {
2133 if ($table->card[$i]->stat != "table")
2136 log_wr(sprintf("Card On table: [%d]", $i));
2138 $v = $table->card[$i]->value;
2139 $ontab[$table->card[$i]->owner] = $v;
2140 $ontid[$table->card[$i]->owner] = $i;
2141 /* se briscola setto il flag */
2142 if (($v - ($v % 10)) == $cur_seed)
2146 if ($briontab == FALSE) {
2147 $cur_win = $table->gstart;
2148 $cur_val = $ontab[$cur_win];
2149 $cur_seed = $cur_val - ($cur_val % 10);
2152 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2153 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
2154 if ($ontab[$i] < $cur_val) {
2155 $cur_val = $ontab[$i];
2161 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2162 $table->card[$ontid[$i]]->owner = $cur_win;
2163 $table->card[$ontid[$i]]->stat = "take"; // Card stat
2168 function calculate_points(&$table)
2170 GLOBAL $G_all_points;
2174 if ($table->asta_pnt == 60)
2175 $table->asta_pnt = 61;
2177 $table->old_reason = "";
2178 $table->old_win = $table->asta_win;
2179 $table->old_friend = $table->friend;
2180 $table->old_asta_pnt = $table->asta_pnt;
2182 for ($i = 0 ; $i < 40 ; $i++) {
2183 $ctt = $table->card[$i]->value % 10;
2184 $own = $table->card[$i]->owner;
2185 if ($own == $table->asta_win || $own == $table->friend)
2186 $pro += $G_all_points[$ctt];
2189 log_wr(sprintf("PRO: [%d]", $pro));
2192 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
2193 $table->points[$table->points_n % MAX_POINTS] = array();
2194 for ($i = 0 ; $i < PLAYERS_N ; $i++)
2195 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
2197 $table->old_pnt = $pro;
2203 if ($pro >= $table->asta_pnt)
2208 $table->points[$table->points_n % MAX_POINTS] = array();
2209 for ($i = 0 ; $i < 5 ; $i++) {
2210 if ($i == $table->asta_win)
2211 $pt = ($i == $table->friend ? 4 : 2);
2212 else if ($i == $table->friend)
2217 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
2219 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
2221 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
2223 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
2224 $table->total[$i] += $pt;
2227 $table->old_pnt = $pro;
2231 function validate_sess($sess)
2233 if (strlen($sess) == SESS_LEN)
2239 function validate_name($name)
2241 $name_new = str_replace(' ', '_', substr(trim($name),0,12));
2243 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
2245 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
2252 function playsound($filename)
2254 return (sprintf('playsound("flasou", "%s");', $filename));
2257 function secstoword($secs)
2259 $mins = floor($secs / 60);
2262 $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
2265 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));