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 * MAX_PLAYERS));
32 define(COMM_GEN_N, 50);
34 define(STREAM_TIMEOUT, 20);
35 define(EXPIRE_TIME_RD, 180);
36 define(EXPIRE_TIME_SMAMMA, 360);
37 // BAN_TIME da allineare anche in commons.js
38 define(BAN_TIME, 900);
39 define(GARBAGE_TIMEOUT, 10);
40 define(NICKSERV, "<i>SERVER</i>");
42 define(DBG_ONL2, 0x0001);
43 define(DBG_ONLY, 0x0002);
44 define(DBG_MAIN, 0x0004);
45 define(DBG_READ, 0x0008);
46 define(DBG_REA2, 0x0010);
47 define(DBG_SEND, 0x0020);
48 define(DBG_LOCK, 0x0040);
49 define(DBG_WRIT, 0x0080);
50 define(DBG_LOAD, 0x0100);
51 define(DBG_AUTH, 0x0200);
53 define(BRISK_DEBUG, 0);
54 // define(DEBUGGING, "local");
58 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
59 $G_brisk_version = "spawn-0.0.3";
61 $root_wellarr = Array ( 'Benvenuto in brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: nuovo layout che permette più tavoli, più tavoli.',
62 'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
63 $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.');
67 <div style=\\"text-align: left; padding: 8px;\\">
68 <b>Descrizione</b><br>
69 Questa è un\'implementazione della briscola in cinque, così come è spiegata su
70 <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>
71 <b>Configurazione del browser.</b><br>
72 Occorre abilitare i cookies.<br>
74 <b>Uso del sito</b><br>
75 Potete sedervi a un tavolo o rimanere in piedi.<br>
76 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
79 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>
80 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
81 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
82 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
83 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
84 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.
86 <dt><b>Comandi della chat</b>
87 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
88 <dd>.. to be continue ..
94 <div id=\\"header\\" class=\\"header\\">
95 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
96 briscola chiamata in salsa ajax
98 <br><b>version '.$G_brisk_version.'</b><br><br>
99 Copyright 2006-2007 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
104 $from = array ( '\\', '@', '|' );
105 $to = array ( '\\\\', '@', '¦' );
107 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
112 var $value; /* 0 - 39 card value */
113 var $stat; /* 'bunch', 'hand', 'table', 'take' */
114 var $owner; /* (table position 0-4) */
115 // var $pos; /* Pos in hand. */
116 var $x; /* When played the X position on the table of the owner. */
117 var $y; /* When played the Y position on the table of the owner. */
119 function Card($value, $stat, $owner)
121 $this->value = $value;
122 $this->stat = $stat; // Card stat
123 $this->owner = $owner;
126 function assign($stat,$owner)
128 $this->stat = $stat; // Card stat
129 $this->owner = $owner;
132 function setpos($pos)
139 $this->stat = 'table'; // Card stat
144 function take($newown)
146 $this->stat = 'take'; // Card stat
147 $this->owner = $newown;
165 var $points; // points array
166 var $points_n; // number of row of points
190 if (($thiz =& new Table()) == FALSE)
193 $thiz->player = array();
196 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
197 $thiz->asta_pla_n= -1;
198 $thiz->asta_card = -1;
199 $thiz->asta_pnt = -1;
201 $thiz->points = array( );
203 $thiz->total = array( 0, 0, 0, 0, 0);
204 $thiz->asta_win = -1;
205 $thiz->briscola = -1;
208 $thiz->old_reason = "";
209 $thiz->old_asta_pnt = -1;
212 $thiz->old_friend = -1;
214 $thiz->table_token = "";
215 $thiz->table_start = 0;
220 function &clone(&$from)
224 if (($thiz =& new Table()) == FALSE)
227 $thiz->player = array();
228 for ($i = 0 ; $i < $from->player_n ; $i++)
229 $thiz->player[$i] = $from->player[$i];
230 $thiz->player_n = $from->player_n;
231 $thiz->card = $from->card;
232 $thiz->mazzo = $from->mazzo; // REVIEW
233 $thiz->gstart = $from->gstart;
234 $thiz->turn = $from->turn;
236 $thiz->asta_pla = $from->asta_pla;
237 $thiz->asta_pla_n = $from->asta_pla_n;
238 $thiz->asta_card = $from->asta_card;
239 $thiz->asta_pnt = $from->asta_pnt;
241 $thiz->mult = $from->mult;
242 $thiz->points = $from->points;
243 $thiz->points_n = $from->points_n;
244 $thiz->total = $from->total;
246 $thiz->asta_win = $from->asta_win;
247 $thiz->briscola = $from->briscola;
248 $thiz->friend = $from->friend;
250 $thiz->old_reason = $from->old_reason;
251 $thiz->old_asta_pnt = $from->old_asta_pnt;
252 $thiz->old_pnt = $from->old_pnt;
253 $thiz->old_win = $from->old_win;
254 $thiz->old_friend = $from->old_friend;
256 $thiz->table_token = $from->table_token;
257 $thiz->table_start = $from->table_start;
262 function &spawn(&$from)
266 if (($thiz =& new Table()) == FALSE)
269 $thiz->player_n = $from->player_n;
270 $thiz->card = &$thiz->bunch_create();
271 $thiz->mazzo = $from->mazzo;
272 $thiz->gstart = $from->gstart;
273 $thiz->turn = $from->turn;
275 $thiz->asta_pla = $from->asta_pla;
276 $thiz->asta_pla_n = $from->asta_pla_n;
277 $thiz->asta_card = $from->asta_card;
278 $thiz->asta_pnt = $from->asta_pnt;
280 $thiz->mult = $from->mult;
281 $thiz->points = $from->points;
282 $thiz->points_n = $from->points_n;
283 $thiz->total = $from->total;
285 $thiz->asta_win = $from->asta_win;
286 $thiz->briscola = $from->briscola;
287 $thiz->friend = $from->friend;
289 $thiz->old_reason = $from->old_reason;
290 $thiz->old_asta_pnt = $from->old_asta_pnt;
291 $thiz->old_pnt = $from->old_pnt;
292 $thiz->old_win = $from->old_win;
293 $thiz->old_friend = $from->old_friend;
295 // players are rearranged in an dedicated array
296 $thiz->player = array();
297 for ($i = 0 ; $i < $from->player_n ; $i++)
298 $thiz->player[$i] = $i;
300 $thiz->table_token = $from->table_token;
301 $thiz->table_start = $from->table_start;
306 function &bunch_create()
310 for ($i = 0 ; $i < 40 ; $i++) {
311 $ret[$i] =& new Card($i, 'bunch', 'no_owner');
318 function bunch_make()
320 $ct = array(0,0,0,0,0);
322 mt_srand(make_seed());
324 for ($i = 39 ; $i >= 0 ; $i--)
327 for ($i = 39 ; $i >= 0 ; $i--) {
331 log_main("RND ZERO");
336 $this->card[$id]->assign('hand', $owner);
338 $rest[$rn] = $rest[$i];
339 // $pubbpos[$rn2] = $pubbpos[$i];
343 function init(&$userarr)
345 $this->mazzo = rand(0,PLAYERS_N-1);
349 $this->old_reason = "";
350 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
351 $this->total[$i] = 0;
352 $user_cur = &$userarr[$this->player[$i]];
353 $user_cur->exitislock = TRUE;
356 log_main("table::init: ci siamo");
359 function game_init(&$userarr)
363 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
367 $this->asta_pla_n = PLAYERS_N;
368 $this->asta_card = -1;
369 $this->asta_pnt = 60;
370 $this->asta_win = -1;
371 $this->briscola = -1;
375 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
376 $this->asta_pla[$i] = TRUE;
377 $user_cur = &$userarr[$this->player[$i]];
378 $user_cur->subst = 'asta';
379 $user_cur->asta_card = -2;
380 $user_cur->asta_pnt = -1;
381 $user_cur->handpt = $this->hand_points($i);
382 // SEE function calculate_points(&$table)
388 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
391 function getPlayer($idx)
393 return ($this->player[$idx]);
396 function setPlayer($idx, $player)
398 $this->player[$idx] = $player;
401 function user_add($idx)
403 $this->player[$this->player_n] = $idx;
406 return ($this->player_n - 1);
409 function user_rem(&$room, &$user)
411 $tabpos = $user->table_pos;
413 /* verifico la consistenza dei dati */
414 if ($room->user[$this->player[$tabpos]] == $user) {
416 /* aggiorna l'array dei giocatori al tavolo. */
417 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
418 $this->player[$i] = $this->player[$i+1];
419 $user_cur = &$room->user[$this->player[$i]];
420 $user_cur->table_pos = $i;
425 log_main("INCONSISTENCY ON TABLE.");
429 function hand_points($idx)
431 GLOBAL $G_all_points;
435 for ($i = 0 ; $i < 40 ; $i++) {
436 if ($this->card[$i]->owner != $idx)
439 $ctt = $this->card[$i]->value % 10;
440 $tot += $G_all_points[$ctt];
446 function exitlock_show(&$userarr, $table_pos)
448 $ct = $this->exitlock_calc(&$userarr, $table_pos);
450 $ret = sprintf('exitlock_show(%d, %s);', $ct,
451 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
455 function exitlock_calc(&$userarr, $table_pos)
459 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
460 if ($userarr[$this->player[$i]]->exitislock == FALSE)
469 var $name; // name of the user
470 var $sess; // session of the user
471 var $ip; // ip of the user
472 var $lacc; // last access (for the cleanup)
473 var $laccwr; // last access (for the cleanup)
474 var $bantime; // timeout to temporary ban
475 var $stat; // status (outdoor, room, table, game, ...)
476 var $subst; // substatus for each status
477 var $step; // step of the current status
478 var $trans_step; // step to enable transition between pages (disable == -1)
479 var $comm; // commands array
482 var $handpt; // Total card points at the beginning of the current hand.
483 var $exitislock; // Player can exit from the table ?
484 var $table; // id of the current table (if in table state)
485 var $table_pos; // idx on the table
486 var $table_token;// token that identify a game on a table
487 var $the_end; // Flag to change the end of the session
492 function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
495 if (($thiz =& new User()) == FALSE)
501 $thiz->lacc = time();
502 $thiz->laccwr = time();
505 $thiz->subst = $subst;
507 $thiz->trans_step = -1;
508 $thiz->comm = array();
509 $thiz->asta_card = -2;
510 $thiz->asta_pnt = -1;
512 $thiz->exitislock = TRUE;
514 $thiz->table = $table;
515 $thiz->table_pos = -1;
516 $thiz->table_token = "";
521 function &clone(&$from)
525 if (($thiz =& new User()) == FALSE)
528 $thiz->name = $from->name;
529 $thiz->sess = $from->sess;
530 $thiz->ip = $from->ip;
531 $thiz->lacc = $from->lacc;
532 $thiz->laccwr = $from->laccwr;
533 $thiz->bantime = $from->bantime;
534 $thiz->stat = $from->stat;
535 $thiz->subst = $from->subst;
536 $thiz->step = $from->step;
537 $thiz->trans_step = $from->trans_step;
538 $thiz->comm = array();
540 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
541 for ($i = $i_start ; $i < $from->step ; $i++) {
543 $thiz->comm[$ii] = $from->comm[$ii];
545 $thiz->asta_card = $from->asta_card;
546 $thiz->asta_pnt = $from->asta_pnt;
547 $thiz->handpt = $from->handpt;
548 $thiz->exitislock = $from->exitislock;
549 $thiz->table = $from->table;
550 $thiz->table_pos = $from->table_pos;
551 $thiz->table_token = $from->table_token;
552 $thiz->the_end = $from->the_end;
557 function &spawn(&$from, $table, $table_pos)
561 if (($thiz =& new User()) == FALSE)
564 $thiz->name = $from->name;
565 $thiz->sess = $from->sess;
566 $thiz->ip = $from->ip;
567 $thiz->lacc = $from->lacc;
568 $thiz->laccwr = $from->laccwr;
569 $thiz->bantime = $from->bantime;
570 $thiz->stat = $from->stat;
571 $thiz->subst = $from->subst;
572 $thiz->step = $from->step;
573 $thiz->trans_step = $from->trans_step;
574 $thiz->comm = array();
576 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
577 for ($i = $i_start ; $i < $from->step ; $i++) {
578 log_wr("TRY PUSH:".$i);
580 $thiz->comm[$ii] = $from->comm[$ii];
583 $thiz->asta_card = $from->asta_card;
584 $thiz->asta_pnt = $from->asta_pnt;
585 $thiz->handpt = $from->handpt;
586 $thiz->exitislock = $from->exitislock;
587 $thiz->the_end = $from->the_end;
589 $thiz->table = $table;
590 $thiz->table_pos = $table_pos;
591 $thiz->table_token = $from->table_token;
596 function stat_set($stat) {
597 log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]");
598 $this->stat = "$stat";
601 if (validate_sess($this->sess)) {
602 $fp = fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
603 fwrite($fp, sprintf("%s\n",$this->stat));
609 function step_set($step) {
613 if (validate_sess($this->sess) == FALSE)
615 if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
617 fwrite($fp, pack("l",$this->step), 4);
626 function step_inc() {
629 if (validate_sess($this->sess)) {
630 $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
631 fwrite($fp, pack("l",$this->step), 4);
642 function step_get($sess) {
646 if (validate_sess($sess) == FALSE)
649 if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
652 if (($s = fread($fp, 4)) == FALSE)
658 $arr = unpack('l', $s);
661 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
668 log_rd2("STEP_GET: return false ".$ct);
672 function step_unproxy($sess) {
673 log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
674 @unlink(PROXY_PATH."/".$sess.".step");
681 var $comm; // commands for many people
682 var $step; // current step of the comm array
683 var $garbage_timeout;
686 $this->user = array();
687 $this->table = array();
689 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
690 $this->user[$i] =& User::create("", "");
693 for ($i = 0 ; $i < TABLES_N ; $i++)
694 $this->table[$i] =& Table::create();
695 $this->garbage_timeout = 0;
698 function garbage_manager($force)
702 log_rd2("garbage_manager START");
704 /* Garbage collector degli utenti in timeout */
706 if ($force || $this->garbage_timeout < $curtime) {
708 // Before all align times with table timeout
709 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
710 $table_cur =& $this->table[$table_idx];
711 // if the table is complete and exists its shared mem we get the info about users lacc
713 if ($table_cur->player_n == PLAYERS_N) {
714 log_main("PLAYERS == N TABLE ".$table_idx);
716 if (($sem = Briskin5::lock_data($table_idx)) != FALSE) {
717 log_main("bin5 lock data success");
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);
731 log_main("garbage_manager: bri loaded successfully.");
732 $bri->garbage_manager(TRUE);
734 $bri_table = &$bri->table[0];
736 // is the end of the table
737 if ($bri->the_end == TRUE) {
739 * DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN
741 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
742 // stat must be "table" by definition
743 $user_cur =& $this->user[$table_cur->player[$i]];
744 $bri_user =& $bri->user[$i];
746 $user_cur->subst = $bri_user->subst;
747 $user_cur->step = $bri_user->step;
748 $user_cur->trans_step = $bri_user->step;
749 $user_cur->lacc = $bri_user->lacc;
750 $user_cur->laccwr = $bri_user->lacc;
751 $user_cur->bantime = $bri_user->bantime;
754 $this->room_join_wakeup(&$user_cur);
755 $table_cur->table_token = "";
757 Briskin5::destroy_data($table_idx);
760 log_main("gm:: save_data");
762 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
763 $this->user[$table_cur->player[$i]]->lacc = $bri->user[$i]->lacc;
766 Briskin5::save_data(&$bri);
768 } // else if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
770 log_main("ERROR: table ".$table_idx." unrecoverable join");
773 } // if ($table_cur->player_n == PLAYERS_N) {
774 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
776 log_rd2("out new loop.");
778 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
779 $user_cur = &$this->user[$i];
781 log_rd2("User: ".$user_cur->name." stat: ".$user_cur->stat." subst: ".$user_cur->subst);
783 if ($user_cur->sess == "")
786 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
787 // Auto logout dell'utente
788 log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
790 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
791 log_auth($user_cur->sess, "Autologout session.");
793 $tmp_sess = $user_cur->sess;
794 $user_cur->sess = "";
795 step_unproxy($tmp_sess);
796 $user_cur->name = "";
798 $user_cur->the_end = FALSE;
800 log_rd2("AUTO LOGOUT.");
801 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
802 $this->room_wakeup(&$user_cur);
803 else if ($user_cur->subst == 'standup')
804 $this->room_outstandup(&$user_cur);
806 log_rd2("LOGOUT FROM WHAT ???");
810 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
811 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
812 $this->room_wakeup(&$user_cur);
813 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
814 $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);
815 $user_cur->step_inc();
819 log_rd2("GARBAGE UPDATED!");
821 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
829 function room_wakeup(&$user)
831 $table_idx = $user->table;
832 $table = &$this->table[$table_idx];
834 log_main("WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
838 $from_table = ($user->stat == "table");
840 log_main("WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
842 for ($i = 0 ; $i < $table->player_n ; $i++) {
843 $user_cur = &$this->user[$table->player[$i]];
844 log_main("PREIMPOST: INLOOP name: ".$user_cur->name);
846 if ($user_cur != $user) {
847 $user_cur->stat_set("room");
848 $user_cur->subst = "sitdown";
849 $user_cur->laccwr = $curtime;
851 else if ($user->sess != "") {
852 $user_cur->stat_set("room");
853 $user_cur->subst = "standup";
854 $user_cur->laccwr = $curtime;
855 $user_cur->table = -1;
860 $user->stat_set("room");
861 $user->subst = "standup";
862 $user->laccwr = $curtime;
864 /* aggiorna l'array dei giocatori al tavolo. */
865 $table->user_rem(&$this, &$user);
867 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
868 $user_cur = &$this->user[$i];
869 if ($user_cur->sess == '' || $user_cur->stat != 'room')
872 log_main("VALORI: name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
874 $ret = "gst.st = ".($user_cur->step+1)."; ";
875 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
876 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
877 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
878 log_main("DOCUMENT.index.php: from table");
880 else if ($user_cur->stat == "room") {
881 log_main("DOCUMENT.index.php: from table");
883 $ret .= table_content($this, $user_cur, $table_idx);
884 $ret .= standup_content($this, $user_cur);
886 $act_content = table_act_content(FALSE, 0, $table_idx, $user->table);
887 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
890 if ($user_cur == $user) {
891 // set the new status
892 $ret .= 'subst = "standup"; ';
893 // clean the action buttons in other tables
894 for ($e = 0 ; $e < TABLES_N ; $e++) {
895 if ($this->table[$e]->player_n < PLAYERS_N)
896 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
900 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
901 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
904 log_wr("ROOM_WAKEUP: ".$ret);
905 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
906 $user_cur->step_inc();
910 function room_join_wakeup(&$user, $update_lacc = FALSE)
912 $table_idx = $user->table;
913 $table = &$this->table[$table_idx];
915 log_main("JOIN_WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
920 log_main("JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
922 for ($i = 0 ; $i < $table->player_n ; $i++) {
923 $user_cur = &$this->user[$table->player[$i]];
924 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
925 if ($user_cur->sess != "") {
926 if ($update_lacc == TRUE) {
927 $user_cur->laccwr = $curtime;
929 log_main("cur: ".$user_cur->name." subst: ".$user_cur->subst);
930 if ($user_cur->subst == "shutdowned") {
931 $user_cur->stat_set("room");
932 $user_cur->subst = "sitdown";
934 else if ($user_cur->subst == "shutdowner") {
935 $user_cur->stat_set("room");
936 $user_cur->subst = "standup";
937 $user_cur->table = -1;
938 $user_wup[$user_wup_n++] = &$user_cur;
943 for ($wup_idx = 0 ; $wup_idx < $user_wup_n ; $wup_idx++)
944 $table->user_rem(&$this, &$user_wup[$wup_idx]);
946 /* aggiorna l'array dei giocatori al tavolo. */
948 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
949 log_main("START LOOP");
950 $user_cur = &$this->user[$i];
951 if ($user_cur->sess == '' || $user_cur->stat != 'room') {
952 log_main("name: ".$user_cur->name."skip subst: ".$user_cur->subst);
957 log_main("VALORI name: ".$user_cur->name." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
959 $ret = "gst.st = ".($user_cur->step+1)."; ";
960 if ($user_cur->stat == "room") {
961 log_main("DOCUMENT.index.php from table");
963 $ret .= table_content($this, $user_cur, $table_idx);
964 $ret .= standup_content($this, $user_cur);
966 $act_content = table_act_content(FALSE, 0, $table_idx, $user_cur->table);
967 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
969 for ($wup_idx = 0 ; $wup_idx < $user_wup_n ; $wup_idx++)
970 if ($user_cur == $user_wup[$wup_idx])
973 log_main("JOIN_WAKEUP wup_idx ".$wup_idx." wup_n ".$user_wup_n);
975 if ($wup_idx < $user_wup_n) {
976 log_main("JOIN_WAKEUP less");
977 // set the new status
978 $ret .= 'subst = "standup"; ';
979 // clean the action buttons in other tables
980 for ($e = 0 ; $e < TABLES_N ; $e++) {
981 if ($this->table[$e]->player_n < PLAYERS_N)
982 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user_cur->table));
984 log_main("JOIN_WAKEUP end less");
987 log_main("JOIN_WAKEUP more");
988 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
989 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
990 log_main("JOIN_WAKEUP end more");
993 log_wr("ROOM_JOIN_WAKEUP: ".$ret);
994 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
995 $user_cur->step_inc();
999 function room_outstandup(&$user)
1001 $this->room_sitdown(&$user, -1);
1004 function table_update(&$user)
1006 log_main("table_update: pre - USER: ".$user->name);
1008 $table_idx = $user->table;
1010 if ($table_idx > -1)
1011 $table = &$this->table[$table_idx];
1013 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1015 $user_cur = &$this->user[$i];
1016 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1019 $ret = "gst.st = ".($user_cur->step+1)."; ";
1020 if ($table_idx > -1)
1021 $ret .= table_content($this, $user_cur, $table_idx);
1023 if ($user_cur == $user) {
1024 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
1026 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1027 $user_cur->step_inc();
1030 log_main("table_update: post");
1033 function room_sitdown(&$user, $table_idx)
1035 log_main("room_sitdown ".($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
1037 if ($table_idx > -1)
1038 $table = &$this->table[$table_idx];
1040 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1042 $user_cur = &$this->user[$i];
1043 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1046 $ret = "gst.st = ".($user_cur->step+1)."; ";
1047 if ($table_idx > -1)
1048 $ret .= table_content($this, $user_cur, $table_idx);
1049 $ret .= standup_content($this, $user_cur);
1051 if ($user_cur == $user) {
1052 $ret .= 'subst = "sitdown"; ';
1053 // clean the action buttons in other tables
1054 for ($e = 0 ; $e < TABLES_N ; $e++) {
1055 $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
1056 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
1059 else if ($table_idx > -1) {
1060 if ($table->player_n == PLAYERS_N) {
1061 $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
1062 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1065 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1066 $user_cur->step_inc();
1070 function chatt_send(&$user, $mesg)
1072 if ($user->stat == 'table') {
1073 $table = &$this->table[$user->table];
1076 $user_mesg = substr($mesg,6);
1080 $dt = date("H:i ", $timecur);
1081 if (strncmp($user_mesg, "/nick ", 6) == 0) {
1082 log_main("chatt_send BEGIN");
1084 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
1085 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1086 $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));
1091 $user_mesg = "COMMAND ".$user_mesg;
1094 // update local graph
1095 // update remote graphs
1096 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1097 $user_cur = &$this->user[$i];
1098 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1099 if ($user_cur->sess == '')
1101 if ($user_cur->name == $name_new) {
1102 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1103 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
1108 if ($i == MAX_PLAYERS) {
1109 $user->name = $name_new;
1111 log_main("chatt_send start set");
1114 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1115 log_main("chatt_send set loop");
1117 $user_cur = &$this->user[$i];
1118 if ($user_cur->sess == '')
1120 if ($user_cur->stat == 'room') {
1121 if ($user->stat == 'room' && $user->subst == 'standup') {
1122 $this->standup_update(&$user);
1124 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
1125 $user->stat == 'table') {
1126 log_main("chatt_send pre table update");
1128 $this->table_update(&$user);
1130 log_main("chatt_send post table update");
1133 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
1134 $table = &$this->table[$user->table];
1136 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1137 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1138 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
1139 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
1140 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
1141 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
1142 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
1143 if ($user_cur == $user)
1144 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
1145 xcape($user->name,ENT_COMPAT,"UTF-8"));
1146 $user_cur->step_inc();
1152 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
1153 if ($user->stat == 'room') {
1154 $user_cur = &$this->user[$i];
1155 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1159 $user_cur = &$this->user[$table->player[$i]];
1162 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1163 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
1164 $dt.xcape($user->name), xcape($user_mesg));
1165 $user_cur->step_inc();
1167 log_legal($timecur, $user->sess, $user->name,
1168 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
1172 function &get_user($sess, &$idx)
1174 GLOBAL $PHP_SELF, $G_false;
1176 if (validate_sess($sess)) {
1177 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1178 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1181 $ret = &$this->user[$i];
1185 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1186 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
1187 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1190 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1197 * function &add_user(&$room, &$sess, &$idx, $name, $ip)
1200 * if ($idx != -1 && ret == FALSE) => duplicated nick
1201 * if ($idx == -2 && ret == FALSE) => invalid name
1202 * if ($idx == -1 && ret == FALSE) => no space left
1203 * if (ret == TRUE) => SUCCESS
1205 function &add_user(&$sess, &$idx, $name, $ip)
1212 if (($name_new = validate_name($name)) == FALSE) {
1217 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
1218 if (validate_sess($sess) == FALSE)
1221 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1223 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1227 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
1230 if (strcmp($this->user[$i]->name, $name_new) == 0) {
1238 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1240 if ($idx != -1 && $i == MAX_PLAYERS) {
1244 $this->user[$idx]->sess = uniqid("");
1245 $sess = $this->user[$idx]->sess;
1249 $this->user[$idx]->sess = $sess;
1251 $this->user[$idx]->name = $name_new;
1252 $this->user[$idx]->stat_set("room");
1253 // MOP $this->user[$idx]->step_set(0);
1254 $this->user[$idx]->subst = "standup";
1255 $this->user[$idx]->lacc = $curtime;
1256 $this->user[$idx]->laccwr = $curtime;
1257 $this->user[$idx]->bantime = 0;
1258 $this->user[$idx]->ip = $ip;
1259 log_main(sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1261 return ($this->user[$idx]);
1267 function standup_update(&$user)
1269 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1270 $user_cur = &$this->user[$i];
1271 if ($user_cur->sess == '')
1274 log_main("STANDUP START: ".$user_cur->stat);
1276 if ($user_cur->stat == 'room') {
1277 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
1278 if ($user_cur == $user)
1279 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
1281 log_main("FROM STANDUP: NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
1283 $user_cur->step_inc();
1289 function &init_data()
1291 $room =& new Room();
1297 function &load_data()
1299 GLOBAL $G_false, $sess;
1301 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
1306 if ($shm = shm_attach($tok, SHM_DIMS)) {
1307 $room = @shm_get_var($shm, $tok);
1309 log_only("bri == ".($room == FALSE ? "FALSE" : "TRUE")." bri === ".($room === FALSE ? "FALSE" : "TRUE")." bri isset ".(isset($room) ? "TRUE" : "FALSE"));
1311 log_only("bri count ".count($room));
1313 if ($room == FALSE) {
1314 log_only("INIT MAIN DATA");
1316 $room =& Room::init_data();
1317 if (shm_put_var($shm, $tok, $room) == FALSE) {
1318 log_only("PUT_VAR FALLITA ".strlen(serialize($room)));
1319 log_only(serialize($room));
1333 function save_data(&$room)
1343 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1)
1349 if (($shm = shm_attach($tok, SHM_DIMS)) == FALSE)
1352 // log_only("PUT_VAR DI ".strlen(serialize($room)));
1353 if (shm_put_var($shm, $tok, $room) == FALSE) {
1354 log_only("PUT_VAR FALLITA ".strlen(serialize($room)));
1355 log_only(serialize($room));
1358 // log_main("QUI CI ARRIVA [".$room->user[0]->name."]");
1368 function lock_data()
1372 // echo "LOCK: ".FTOK_PATH."/main";
1374 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
1378 // echo "FTOK ".$tok."<br>";
1379 if (($res = sem_get($tok)) == FALSE) {
1380 echo "SEM_GET FAILED";
1383 if (sem_acquire($res)) {
1384 log_lock("LOCK room");
1391 function unlock_data($res)
1395 log_lock("UNLOCK room");
1397 return (sem_release($res));
1401 function make_seed()
1403 list($usec, $sec) = explode(' ', microtime());
1404 return (float) $sec + ((float) $usec * 100000);
1408 function log_only2($log) {
1409 if ((BRISK_DEBUG & DBG_ONL2) == 0)
1414 if (isset($sess) == FALSE)
1420 // if (($fp = @fopen("/tmp/brisk_only2.log", 'a')) != FALSE) {
1421 if (($fp = @fopen("/tmp/brisk.log", 'a')) != FALSE) {
1422 fwrite($fp, sprintf("ONL2: [%s] [%s]\n", $ssess, $log));
1427 function log_only($log) {
1428 if ((BRISK_DEBUG & DBG_ONLY) == 0)
1433 if (isset($sess) == FALSE)
1439 // if (($fp = @fopen("/tmp/brisk_only.log", 'a')) != FALSE) {
1440 if (($fp = @fopen("/tmp/brisk.log", 'a')) != FALSE) {
1441 fwrite($fp, sprintf("ONLY: [%s] [%s]\n", $ssess, $log));
1446 function log_main($log) {
1447 if ((BRISK_DEBUG & DBG_MAIN) == 0)
1452 if (isset($sess) == FALSE)
1458 // if (($fp = @fopen("/tmp/brisk_main.log", 'a')) != FALSE) {
1459 if (($fp = @fopen("/tmp/brisk.log", 'a')) != FALSE) {
1460 fwrite($fp, sprintf("MAIN: [%s] [%s]\n", $ssess, $log));
1465 function log_rd($log) {
1466 if ((BRISK_DEBUG & DBG_READ) == 0)
1471 if (isset($sess) == FALSE)
1477 // if (($fp = @fopen("/tmp/brisk_rd.log", 'a')) != FALSE) {
1478 if (($fp = @fopen("/tmp/brisk.log", 'a')) != FALSE) {
1479 fwrite($fp, sprintf("READ: [%s] [%s]\n", $ssess, $log));
1484 function log_rd2($log) {
1485 if ((BRISK_DEBUG & DBG_REA2) == 0)
1490 if (isset($sess) == FALSE)
1496 // if (($fp = @fopen("/tmp/brisk_rd2.log", 'a')) != FALSE) {
1497 if (($fp = @fopen("/tmp/brisk.log", 'a')) != FALSE) {
1498 fwrite($fp, sprintf("REA2: [%s] [%s]\n", $ssess, $log));
1503 function log_send($log) {
1504 if ((BRISK_DEBUG & DBG_SEND) == 0)
1509 if (isset($sess) == FALSE)
1515 // if (($fp = @fopen("/tmp/brisk_send.log", 'a')) != FALSE) {
1516 if (($fp = @fopen("/tmp/brisk.log", 'a')) != FALSE) {
1517 fwrite($fp, sprintf("SEND: [%s] [%s]\n", $ssess, $log));
1522 function log_lock($log) {
1523 if ((BRISK_DEBUG & DBG_LOCK) == 0)
1528 if (isset($sess) == FALSE)
1534 // if (($fp = @fopen("/tmp/brisk_lock.log", 'a')) != FALSE) {
1535 if (($fp = @fopen("/tmp/brisk.log", 'a')) != FALSE) {
1536 fwrite($fp, sprintf("LOCK: [%s] [%s]\n", $ssess, $log));
1541 function log_wr($log) {
1542 if ((BRISK_DEBUG & DBG_WRIT) == 0)
1547 if (isset($sess) == FALSE)
1553 // if (($fp = @fopen("/tmp/brisk_wr.log", 'a')) != FALSE) {
1554 if (($fp = @fopen("/tmp/brisk.log", 'a')) != FALSE) {
1555 fwrite($fp, sprintf("WRIT: [%s] [%s]\n", $ssess, $log));
1560 function log_load($log) {
1561 if ((BRISK_DEBUG & DBG_LOAD) == 0)
1566 if (isset($sess) == FALSE)
1572 // if (($fp = @fopen("/tmp/brisk_load.log", 'a')) != FALSE) {
1573 if (($fp = @fopen("/tmp/brisk.log", 'a')) != FALSE) {
1574 fwrite($fp, sprintf("LOAD: [%s] [%s]\n", $ssess, $log));
1579 function log_auth($sess, $log) {
1580 if ((BRISK_DEBUG & DBG_AUTH) == 0)
1583 // if (($fp = @fopen("/tmp/brisk_auth.log", 'a')) != FALSE) {
1584 if (($fp = @fopen("/tmp/brisk.log", 'a')) != FALSE) {
1585 fwrite($fp, sprintf("AUTH: [%s] [%d] [%s]\n", $sess, time(), $log));
1591 function log_legal($timecur, $sess, $name, $where, $mesg)
1595 if (($fp = @fopen(LEGAL_PATH, 'a')) != FALSE) {
1596 /* Unix time | session | nickname | IP | where was | mesg */
1597 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|\n", $timecur, $sess, $name, $_SERVER['REMOTE_ADDR'], $where , $mesg));
1605 function lock_banlist()
1607 if (($tok = ftok(FTOK_PATH."/main", "L")) == -1) {
1611 if (($res = sem_get($tok)) == FALSE) {
1612 echo "SEM_GET FAILED";
1615 if (sem_acquire($res))
1621 function unlock_banlist($res)
1623 return (sem_release($res));
1626 function table_act_content($isstanding, $sitted, $table, $cur_table)
1631 if ($sitted < PLAYERS_N) {
1632 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
1636 if ($table == $cur_table)
1637 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
1644 function table_content($room, $user, $table_idx)
1650 // Si possono usare i dati nella classe table
1653 $sess = $user->sess;
1654 $table = &$room->table[$table_idx];
1656 if ($user->stat != 'room')
1659 for ($i = 0 ; $i < $table->player_n ; $i++) {
1660 $user_cur = &$room->user[$table->player[$i]];
1662 if ($user_cur == $user)
1663 { $hilion = "<b>"; $hilioff = "</b>"; }
1665 { $hilion = ""; $hilioff = ""; }
1667 log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
1669 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
1672 for ( ; $i < PLAYERS_N ; $i++)
1676 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
1681 function standup_content(&$room, $user)
1686 if ($user->stat != 'room')
1689 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
1690 if ($room->user[$e]->sess == "" || $room->user[$e]->stat != "room" || $room->user[$e]->name == "")
1695 $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
1697 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
1698 if ($room->user[$e]->sess == "" || $room->user[$e]->stat != "room" || $room->user[$e]->name == "")
1702 if ($room->user[$e]->subst == "standup") {
1703 if (($ct % 4) == 0) {
1706 if ($room->user[$e] == $user)
1707 { $hilion = "<b>"; $hilioff = "</b>"; }
1709 { $hilion = ""; $hilioff = ""; }
1711 $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($room->user[$e]->name), $hilioff);
1712 if (($ct % 4) == 3) {
1713 $content .= '</tr>';
1718 $content .= '</table>';
1720 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
1721 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
1722 $content, $content2);
1728 function show_notify($text, $tout, $butt, $w, $h)
1730 log_main("SHOW_NOTIFY: ".$text);
1731 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
1734 function briscola_show($room, $table, $user)
1739 if ($table->asta_card == 9)
1740 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1742 /* text of caller cell */
1743 if ($user->table_pos == $table->asta_win)
1744 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1746 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
1747 xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
1749 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1751 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1752 $ret .= sprintf('$("caller").style.visibility = "visible";');
1753 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1754 $ret .= sprintf('$("astalascio").style.visibility = "";');
1755 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1756 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1762 function game_result($asta_pnt, $pnt)
1764 if ($asta_pnt == 61) {
1767 else if ($pnt == 60)
1773 if ($pnt >= $asta_pnt)
1780 function multoval($mult)
1784 else if ($mult == 4)
1785 return ("quadruplo");
1787 return (sprintf("%d-plo", $mult));
1790 function show_table_info(&$room, &$table, $table_pos)
1793 $user = &$room->user[$table->player[$table_pos]];
1795 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1796 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1799 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1800 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
1801 $noty .= sprintf("</tr>");
1804 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1806 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1807 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1808 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1809 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1814 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1815 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1816 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1817 $noty .= "</tr></table>";
1819 if ($table->old_reason != "") {
1820 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1823 if ($table->old_win != -1) {
1824 $win = $table->player[$table->old_win];
1825 $fri = $table->player[$table->old_friend];
1827 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1830 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
1831 xcape($room->user[$win]->name),
1832 xcape($room->user[$fri]->name));
1833 if ($table->old_pnt == 120) {
1834 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1837 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1838 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1839 'più di 60'), $table->old_pnt,
1840 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1844 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1845 xcape($room->user[$win]->name));
1846 if ($table->old_pnt == 120) {
1847 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1850 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1851 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1852 'più di 60'), $table->old_pnt,
1853 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1857 if ($table->mazzo == $table_pos)
1858 $noty .= "Fai <b>tu</b> il mazzo,";
1860 $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
1861 $noty .= "Il mazzo a <b>$unam</b>,";
1864 if ($user->subst == 'asta') {
1865 if ($table->asta_win == -1) // auction case
1866 $curplayer = $table->gstart % PLAYERS_N;
1868 $curplayer = $table->asta_win;
1870 else if ($user->subst == 'game') {
1871 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
1875 if ($curplayer == $table_pos) {
1876 $noty .= " tocca a <b>te</b> giocare.";
1879 $unam = xcape($room->user[$table->player[$curplayer]]->name);
1880 $noty .= " tocca a <b>$unam</b> giocare.";
1883 if ($table->mult > 1) {
1884 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1886 $noty .= "<hr><br>";
1888 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
1893 function root_wellcome($user)
1895 GLOBAL $root_wellarr;
1898 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
1899 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $root_wellarr[$i]));
1904 function table_wellcome($user)
1906 GLOBAL $table_wellarr;
1909 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1910 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
1915 function show_room(&$room, &$user)
1917 $ret = sprintf('gst.st = %d;', $user->step);
1918 $ret .= sprintf('stat = "%s";', $user->stat);
1920 $ret .= root_wellcome($user);
1921 $ret .= sprintf('subst = "%s";', $user->subst);
1922 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1923 for ($i = 0 ; $i < TABLES_N ; $i++) {
1924 $ret .= table_content($room, $user, $i);
1925 $act_content = table_act_content(($user->subst == 'standup'),
1926 $room->table[$i]->player_n, $i, $user->table);
1927 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1929 $ret .= standup_content($room, $user);
1937 is_transition (is from room to table ?)
1938 is_again (is another game)
1940 Examples of $is_transition, $is_again:
1941 from reload of the page: FALSE, FALSE
1942 from sitdown in room: TRUE, FALSE
1943 from table: asta cmd e tutti passano: TRUE, TRUE
1944 from table: fine partita: TRUE, TRUE
1946 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
1948 $table_idx = $user->table;
1949 $table = &$room->table[$table_idx];
1950 $table_pos = $user->table_pos;
1952 $ret = "table_init();";
1953 $ret .= $table->exitlock_show(&$room->user, $table_pos);
1955 /* GENERAL STATUS */
1956 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1957 $sendstep, $user->stat, $user->subst, $table_pos);
1959 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1960 $sendstep, $user->stat, $user->subst, $table_pos));
1963 $ret .= "background_set();";
1966 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1967 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1968 xcape($room->user[$table->player[($table_pos)%PLAYERS_N]]->name),
1969 xcape($room->user[$table->player[($table_pos+1)%PLAYERS_N]]->name),
1970 xcape($room->user[$table->player[($table_pos+2)%PLAYERS_N]]->name),
1971 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+3)%PLAYERS_N]]->name)),
1972 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+4)%PLAYERS_N]]->name)));
1974 /* NOTIFY FOR THE CARD MAKER */
1975 if ($is_transition) { // && $user->subst == "asta" superfluo
1976 $ret .= show_table_info(&$room, &$table, $table_pos);
1979 $ret .= table_wellcome($user);
1981 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1982 $ret .= playsound("cow.mp3");
1987 if ($is_transition) { // && $user->subst == "asta" superfluo
1990 for ($i = 0 ; $i < 8 ; $i++) {
1991 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1993 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1994 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
2000 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
2002 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
2003 $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
2004 $table->card[$o]->value : -1),
2005 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
2010 $taked = array(0,0,0,0,0);
2011 $inhand = array(0,0,0,0,0);
2012 $ontabl = array(-1,-1,-1,-1,-1);
2015 for ($i = 0 ; $i < 40 ; $i++) {
2016 if ($table->card[$i]->stat == 'hand') {
2017 if ($table->card[$i]->owner == $table_pos) {
2018 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
2020 $inhand[$table->card[$i]->owner]++;
2022 else if ($table->card[$i]->stat == 'take') {
2023 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
2024 $taked[$table->card[$i]->owner]++;
2026 else if ($table->card[$i]->stat == 'table') {
2027 $ontabl[$table->card[$i]->owner] = $i;
2031 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2032 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
2034 log_main("Stat table: ".$logg);
2036 /* Set ours cards. */
2038 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
2039 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
2040 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
2041 $oursarg .= ($i == 0 ? "" : ", ")."-1";
2042 $ret .= sprintf('card_setours(%s);', $oursarg);
2044 /* Dispose all cards */
2045 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2046 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
2047 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
2048 $inhand[$i], $taked[$i]);
2050 if ($ontabl[$i] != -1) {
2051 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
2052 $table->card[$ontabl[$i]]->value,
2053 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
2059 if ($user->subst == 'asta') {
2061 /* show users auction status */
2063 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2064 $user_cur = &$room->user[$table->player[$i]];
2065 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
2066 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
2069 $showst .= ",-2,-2";
2070 $ret .= sprintf('show_astat(%s);', $showst);
2072 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
2073 /* show card chooser */
2074 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
2079 if ($table_pos == ($table->gstart % PLAYERS_N) &&
2080 $table->asta_win == -1)
2081 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2082 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
2084 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2085 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
2089 if ($table->asta_win == -1) { // auction case
2090 if ($table_pos == ($table->gstart % PLAYERS_N))
2091 $ret .= "remark_on();";
2093 $ret .= "remark_off();";
2095 else { // chooseed case
2096 if ($table_pos == $table->asta_win)
2097 $ret .= "remark_on();";
2099 $ret .= "remark_off();";
2102 else if ($user->subst == 'game') {
2104 if (($table->gstart + $table->turn) % PLAYERS_N == $table_pos)
2105 $ret .= "is_my_time = true; remark_on();";
2107 $ret .= "remark_off();";
2109 /* WHO CALL AND WATH */
2110 $ret .= briscola_show($room, $table, $user);
2114 } // end function show_table(...
2116 function calculate_winner(&$table)
2123 $cur_seed = $table->briscola - ($table->briscola % 10);
2125 for ($i = 0 ; $i < 40 ; $i++) {
2126 if ($table->card[$i]->stat != "table")
2129 log_wr(sprintf("Card On table: [%d]", $i));
2131 $v = $table->card[$i]->value;
2132 $ontab[$table->card[$i]->owner] = $v;
2133 $ontid[$table->card[$i]->owner] = $i;
2134 /* se briscola setto il flag */
2135 if (($v - ($v % 10)) == $cur_seed)
2139 if ($briontab == FALSE) {
2140 $cur_win = $table->gstart;
2141 $cur_val = $ontab[$cur_win];
2142 $cur_seed = $cur_val - ($cur_val % 10);
2145 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2146 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
2147 if ($ontab[$i] < $cur_val) {
2148 $cur_val = $ontab[$i];
2154 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2155 $table->card[$ontid[$i]]->owner = $cur_win;
2156 $table->card[$ontid[$i]]->stat = "take"; // Card stat
2161 function calculate_points(&$table)
2163 GLOBAL $G_all_points;
2167 if ($table->asta_pnt == 60)
2168 $table->asta_pnt = 61;
2170 $table->old_reason = "";
2171 $table->old_win = $table->asta_win;
2172 $table->old_friend = $table->friend;
2173 $table->old_asta_pnt = $table->asta_pnt;
2175 for ($i = 0 ; $i < 40 ; $i++) {
2176 $ctt = $table->card[$i]->value % 10;
2177 $own = $table->card[$i]->owner;
2178 if ($own == $table->asta_win || $own == $table->friend)
2179 $pro += $G_all_points[$ctt];
2182 log_wr(sprintf("PRO: [%d]", $pro));
2185 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
2186 $table->points[$table->points_n % MAX_POINTS] = array();
2187 for ($i = 0 ; $i < PLAYERS_N ; $i++)
2188 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
2190 $table->old_pnt = $pro;
2196 if ($pro >= $table->asta_pnt)
2201 $table->points[$table->points_n % MAX_POINTS] = array();
2202 for ($i = 0 ; $i < 5 ; $i++) {
2203 if ($i == $table->asta_win)
2204 $pt = ($i == $table->friend ? 4 : 2);
2205 else if ($i == $table->friend)
2210 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
2212 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
2214 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
2216 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
2217 $table->total[$i] += $pt;
2220 $table->old_pnt = $pro;
2224 function validate_sess($sess)
2226 if (strlen($sess) == SESS_LEN)
2232 function validate_name($name)
2234 $name_new = str_replace(' ', '_', substr(trim($name),0,12));
2236 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
2238 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
2245 function playsound($filename)
2247 return (sprintf('playsound("flasou", "%s");', $filename));
2250 function secstoword($secs)
2252 $mins = floor($secs / 60);
2255 $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
2258 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));