5 * Copyright (C) 2006-2008 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.
23 define(BRISK_CONF, "brisk.conf.pho");
24 define(FTOK_PATH, "/var/lib/brisk");
25 define(LEGAL_PATH, "/tmp/legal_brisk");
26 define(PROXY_PATH, "/var/lib/brisk_proxy");
29 define(MAX_POINTS, 5);
30 define(MAX_PLAYERS, (20 + (PLAYERS_N * TABLES_N)));
31 define(SHM_DIMS_MIN, (50000 + 10000 * TABLES_N + 15000 * MAX_PLAYERS));
32 define(SHM_DIMS_MAX, SHM_DIMS_MIN + 1048576);
33 define(SHM_DIMS_DLT, 65536);
36 define(COMM_GEN_N, 50);
39 define(CHAT_ILL_TIME, 6);
42 define(STREAM_TIMEOUT, 20);
43 define(EXPIRE_TIME_RD, 180);
44 define(EXPIRE_TIME_SMAMMA, 360);
45 // BAN_TIME da allineare anche in commons.js
46 define(BAN_TIME, 900);
47 define(GARBAGE_TIMEOUT, 10);
48 define(NICKSERV, "<i>BriskServ</i>");
50 define(DBG_ONL2, 0x0001);
51 define(DBG_ONLY, 0x0002);
52 define(DBG_MAIN, 0x0004);
53 define(DBG_READ, 0x0008);
54 define(DBG_REA2, 0x0010);
55 define(DBG_SEND, 0x0020);
56 define(DBG_LOCK, 0x0040);
57 define(DBG_WRIT, 0x0080);
58 define(DBG_LOAD, 0x0100);
59 define(DBG_AUTH, 0x0200);
60 define(DBG_CRIT, 0x0400);
62 define(BRISK_DEBUG, 0);
64 define(BRISK_SINGLE_DEBUG,0);
65 define(BRISK_SINGLE_SESS, "");
66 // define(DEBUGGING, "local");
68 require_once("$DOCUMENT_ROOT/Etc/".BRISK_CONF);
73 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
74 $G_brisk_version = "spawn-0.12.0";
76 $root_wellarr = Array ( 'Benvenuto in brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: aggiunta messaggistica a tutti i tavoli - versione beta2.',
77 'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
78 $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.');
82 <div style=\\"text-align: left; padding: 8px;\\">
83 <b>Descrizione</b><br>
84 Questa è un\'implementazione della briscola in cinque, così come è spiegata su
85 <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>
86 <b>Configurazione del browser.</b><br>
87 Occorre abilitare i cookies.<br>
89 <b>Uso del sito</b><br>
90 Potete sedervi a un tavolo o rimanere in piedi.<br>
91 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
94 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>
95 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
96 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
97 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
98 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
99 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.
101 <dt><b>Comandi della chat</b>
102 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
103 <dd>.. to be continue ..
109 <div id=\\"header\\" class=\\"header\\">
110 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
111 briscola chiamata in salsa ajax
113 <br><b>version '.$G_brisk_version.'</b><br><br>
114 Copyright 2006-2007 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
119 $from = array ( '\\', '@', '|' );
120 $to = array ( '\\\\', '@', '¦' );
122 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
127 var $value; /* 0 - 39 card value */
128 var $stat; /* 'bunch', 'hand', 'table', 'take' */
129 var $owner; /* (table position 0-4) */
130 // var $pos; /* Pos in hand. */
131 var $x; /* When played the X position on the table of the owner. */
132 var $y; /* When played the Y position on the table of the owner. */
134 function Card($value, $stat, $owner)
136 $this->value = $value;
137 $this->stat = $stat; // Card stat
138 $this->owner = $owner;
141 function assign($stat,$owner)
143 $this->stat = $stat; // Card stat
144 $this->owner = $owner;
147 function setpos($pos)
154 $this->stat = 'table'; // Card stat
159 function take($newown)
161 $this->stat = 'take'; // Card stat
162 $this->owner = $newown;
180 var $points; // points array
181 var $points_n; // number of row of points
205 if (($thiz =& new Table()) == FALSE)
208 $thiz->player = array();
211 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
212 $thiz->asta_pla_n= -1;
213 $thiz->asta_card = -1;
214 $thiz->asta_pnt = -1;
216 $thiz->points = array( );
218 $thiz->total = array( 0, 0, 0, 0, 0);
219 $thiz->asta_win = -1;
220 $thiz->briscola = -1;
223 $thiz->old_reason = "";
224 $thiz->old_asta_pnt = -1;
227 $thiz->old_friend = -1;
229 $thiz->table_token = "";
230 $thiz->table_start = 0;
235 function &clone(&$from)
239 if (($thiz =& new Table()) == FALSE)
242 $thiz->player = array();
243 for ($i = 0 ; $i < $from->player_n ; $i++)
244 $thiz->player[$i] = $from->player[$i];
245 $thiz->player_n = $from->player_n;
246 $thiz->card = $from->card;
247 $thiz->mazzo = $from->mazzo; // REVIEW
248 $thiz->gstart = $from->gstart;
249 $thiz->turn = $from->turn;
251 $thiz->asta_pla = $from->asta_pla;
252 $thiz->asta_pla_n = $from->asta_pla_n;
253 $thiz->asta_card = $from->asta_card;
254 $thiz->asta_pnt = $from->asta_pnt;
256 $thiz->mult = $from->mult;
257 $thiz->points = $from->points;
258 $thiz->points_n = $from->points_n;
259 $thiz->total = $from->total;
261 $thiz->asta_win = $from->asta_win;
262 $thiz->briscola = $from->briscola;
263 $thiz->friend = $from->friend;
265 $thiz->old_reason = $from->old_reason;
266 $thiz->old_asta_pnt = $from->old_asta_pnt;
267 $thiz->old_pnt = $from->old_pnt;
268 $thiz->old_win = $from->old_win;
269 $thiz->old_friend = $from->old_friend;
271 $thiz->table_token = $from->table_token;
272 $thiz->table_start = $from->table_start;
277 function &spawn(&$from)
281 if (($thiz =& new Table()) == FALSE)
284 $thiz->player_n = $from->player_n;
285 $thiz->card = &$thiz->bunch_create();
286 $thiz->mazzo = $from->mazzo;
287 $thiz->gstart = $from->gstart;
288 $thiz->turn = $from->turn;
290 $thiz->asta_pla = $from->asta_pla;
291 $thiz->asta_pla_n = $from->asta_pla_n;
292 $thiz->asta_card = $from->asta_card;
293 $thiz->asta_pnt = $from->asta_pnt;
295 $thiz->mult = $from->mult;
296 $thiz->points = $from->points;
297 $thiz->points_n = $from->points_n;
298 $thiz->total = $from->total;
300 $thiz->asta_win = $from->asta_win;
301 $thiz->briscola = $from->briscola;
302 $thiz->friend = $from->friend;
304 $thiz->old_reason = $from->old_reason;
305 $thiz->old_asta_pnt = $from->old_asta_pnt;
306 $thiz->old_pnt = $from->old_pnt;
307 $thiz->old_win = $from->old_win;
308 $thiz->old_friend = $from->old_friend;
310 // players are rearranged in an dedicated array
311 $thiz->player = array();
312 for ($i = 0 ; $i < $from->player_n ; $i++)
313 $thiz->player[$i] = $i;
315 $thiz->table_token = $from->table_token;
316 $thiz->table_start = $from->table_start;
321 function &bunch_create()
325 for ($i = 0 ; $i < 40 ; $i++) {
326 $ret[$i] =& new Card($i, 'bunch', 'no_owner');
333 function bunch_make()
335 $ct = array(0,0,0,0,0);
337 mt_srand(make_seed());
339 for ($i = 39 ; $i >= 0 ; $i--)
342 for ($i = 39 ; $i >= 0 ; $i--) {
346 log_main("RND ZERO");
351 $this->card[$id]->assign('hand', $owner);
353 $rest[$rn] = $rest[$i];
354 // $pubbpos[$rn2] = $pubbpos[$i];
358 function init(&$userarr)
360 $this->mazzo = rand(0,PLAYERS_N-1);
364 $this->old_reason = "";
365 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
366 $this->total[$i] = 0;
367 $user_cur = &$userarr[$this->player[$i]];
368 $user_cur->exitislock = TRUE;
371 log_main("table::init: ci siamo");
374 function game_init(&$userarr)
378 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
382 $this->asta_pla_n = PLAYERS_N;
383 $this->asta_card = -1;
384 $this->asta_pnt = 60;
385 $this->asta_win = -1;
386 $this->briscola = -1;
390 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
391 $this->asta_pla[$i] = TRUE;
392 $user_cur = &$userarr[$this->player[$i]];
393 $user_cur->subst = 'asta';
394 $user_cur->asta_card = -2;
395 $user_cur->asta_pnt = -1;
396 $user_cur->handpt = $this->hand_points($i);
397 // SEE function calculate_points(&$table)
403 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
406 function getPlayer($idx)
408 return ($this->player[$idx]);
411 function setPlayer($idx, $player)
413 $this->player[$idx] = $player;
416 function user_add($idx)
418 $this->player[$this->player_n] = $idx;
421 return ($this->player_n - 1);
424 function user_rem(&$room, &$user)
426 $tabpos = $user->table_pos;
428 /* verifico la consistenza dei dati */
429 if ($room->user[$this->player[$tabpos]] == $user) {
431 /* aggiorna l'array dei giocatori al tavolo. */
432 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
433 $this->player[$i] = $this->player[$i+1];
434 $user_cur = &$room->user[$this->player[$i]];
435 $user_cur->table_pos = $i;
440 log_main("INCONSISTENCY ON TABLE.");
444 function hand_points($idx)
446 GLOBAL $G_all_points;
450 for ($i = 0 ; $i < 40 ; $i++) {
451 if ($this->card[$i]->owner != $idx)
454 $ctt = $this->card[$i]->value % 10;
455 $tot += $G_all_points[$ctt];
461 function exitlock_show(&$userarr, $table_pos)
463 $ct = $this->exitlock_calc(&$userarr, $table_pos);
465 $ret = sprintf('exitlock_show(%d, %s);', $ct,
466 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
470 function exitlock_calc(&$userarr, $table_pos)
474 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
475 if ($userarr[$this->player[$i]]->exitislock == FALSE)
484 var $name; // name of the user
485 var $sess; // session of the user
486 var $ip; // ip of the user
487 var $lacc; // last access (for the cleanup)
488 var $laccwr; // last access (for the cleanup)
489 var $bantime; // timeout to temporary ban
490 var $stat; // status (outdoor, room, table, game, ...)
491 var $subst; // substatus for each status
492 var $step; // step of the current status
493 var $trans_step; // step to enable transition between pages (disable == -1)
494 var $comm; // commands array
497 var $handpt; // Total card points at the beginning of the current hand.
498 var $exitislock; // Player can exit from the table ?
499 var $table; // id of the current table (if in table state)
500 var $table_pos; // idx on the table
501 var $table_token;// token that identify a game on a table
502 var $the_end; // Flag to change the end of the session
504 var $chat_lst; // Last chat line
505 var $chattime; // Array of chat times
506 var $chat_cur; // Current chat line number
507 var $chat_ban; // Time for ban chat
508 var $chat_dlt; // Delta t for ban
512 function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
515 if (($thiz =& new User()) == FALSE)
521 $thiz->lacc = time();
522 $thiz->laccwr = time();
525 $thiz->subst = $subst;
527 $thiz->trans_step = -1;
528 $thiz->comm = array();
529 $thiz->asta_card = -2;
530 $thiz->asta_pnt = -1;
532 $thiz->exitislock = TRUE;
534 $thiz->chattime = array_fill(0, CHAT_N, 0);
536 $thiz->chat_lst = "";
540 $thiz->table = $table;
541 $thiz->table_pos = -1;
542 $thiz->table_token = "";
547 function &clone(&$from)
551 if (($thiz =& new User()) == FALSE)
554 $thiz->name = $from->name;
555 $thiz->sess = $from->sess;
556 $thiz->ip = $from->ip;
557 $thiz->lacc = $from->lacc;
558 $thiz->laccwr = $from->laccwr;
559 $thiz->bantime = $from->bantime;
560 $thiz->stat = $from->stat;
561 $thiz->subst = $from->subst;
562 $thiz->step = $from->step;
563 $thiz->trans_step = $from->trans_step;
564 $thiz->comm = array();
566 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
567 for ($i = $i_start ; $i < $from->step ; $i++) {
569 $thiz->comm[$ii] = $from->comm[$ii];
571 $thiz->asta_card = $from->asta_card;
572 $thiz->asta_pnt = $from->asta_pnt;
573 $thiz->handpt = $from->handpt;
574 $thiz->exitislock = $from->exitislock;
576 $thiz->chattime = array();
577 for ($i = 0 ; $i < CHAT_N ; $i++)
578 $thiz->chattime[$i] = $from->chattime[$i];
579 $thiz->chat_cur = $from->chat_cur;
580 $thiz->chat_lst = $from->chat_lst;
581 $thiz->chat_ban = $from->chat_ban;
582 $thiz->chat_dlt = $from->chat_dlt;
584 $thiz->table = $from->table;
585 $thiz->table_pos = $from->table_pos;
586 $thiz->table_token = $from->table_token;
587 $thiz->the_end = $from->the_end;
592 function &spawn(&$from, $table, $table_pos)
596 if (($thiz =& new User()) == FALSE)
599 $thiz->name = $from->name;
600 $thiz->sess = $from->sess;
601 $thiz->ip = $from->ip;
602 $thiz->lacc = $from->lacc;
603 $thiz->laccwr = $from->laccwr;
604 $thiz->bantime = $from->bantime;
605 $thiz->stat = $from->stat;
606 $thiz->subst = $from->subst;
607 $thiz->step = $from->step;
608 $thiz->trans_step = $from->trans_step;
609 $thiz->comm = array();
612 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
613 for ($i = $i_start ; $i < $from->step ; $i++) {
614 log_wr("TRY PUSH:".$i);
616 $thiz->comm[$ii] = $from->comm[$ii];
619 $thiz->asta_card = $from->asta_card;
620 $thiz->asta_pnt = $from->asta_pnt;
621 $thiz->handpt = $from->handpt;
622 $thiz->exitislock = $from->exitislock;
623 $thiz->the_end = $from->the_end;
625 $thiz->chattime = array_fill(0, CHAT_N, 0);
627 $thiz->chat_lst = "";
631 $thiz->table = $table;
632 $thiz->table_pos = $table_pos;
633 $thiz->table_token = $from->table_token;
638 function stat_set($stat) {
639 log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]");
640 $this->stat = "$stat";
643 if (validate_sess($this->sess)) {
644 $fp = fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
645 fwrite($fp, sprintf("%s\n",$this->stat));
651 function step_set($step) {
655 if (validate_sess($this->sess) == FALSE)
657 if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
659 fwrite($fp, pack("l",$this->step), 4);
668 function step_inc($delta = 1) {
669 $this->step += $delta;
671 if (validate_sess($this->sess)) {
672 $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
673 fwrite($fp, pack("l",$this->step), 4);
683 $tmp_sess = $this->sess;
685 step_unproxy($tmp_sess);
687 while (array_pop($this->comm) != NULL);
689 $this->the_end = FALSE;
694 function step_get($sess) {
698 if (validate_sess($sess) == FALSE)
701 if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
704 if (($s = fread($fp, 4)) == FALSE)
710 $arr = unpack('l', $s);
713 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
720 log_rd2("STEP_GET: return false ".$ct);
724 function step_unproxy($sess) {
725 log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
726 @unlink(PROXY_PATH."/".$sess.".step");
733 var $comm; // commands for many people
734 var $step; // current step of the comm array
735 var $garbage_timeout;
739 $this->user = array();
740 $this->table = array();
742 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
743 $this->user[$i] =& User::create("", "");
746 for ($i = 0 ; $i < TABLES_N ; $i++)
747 $this->table[$i] =& Table::create();
748 $this->garbage_timeout = 0;
751 function garbage_manager($force)
755 log_rd2("garbage_manager START");
757 /* Garbage collector degli utenti in timeout */
759 if ($force || $this->garbage_timeout < $curtime) {
761 // Before all align times with table timeout
762 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
763 $table_cur =& $this->table[$table_idx];
764 // if the table is complete and exists its shared mem we get the info about users lacc
766 if ($table_cur->player_n == PLAYERS_N) {
767 log_main("PLAYERS == N TABLE ".$table_idx);
769 if (($sem = Briskin5::lock_data($table_idx)) != FALSE) {
770 log_main("bin5 lock data success");
772 $no_recovery = FALSE;
773 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
774 if ($table_cur->table_token != $bri->table_token) {
775 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
776 log_main("ERROR: not matching table_start. Room: ".$table_cur->table_start." Table: ".$bri->table_start);
786 log_main("garbage_manager: bri loaded successfully.");
787 $bri->garbage_manager(TRUE);
789 $bri_table = &$bri->table[0];
791 // is the end of the table
792 if ($bri->the_end == TRUE) {
794 * DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN
796 log_main("garbage_manager: INSIDE THE END.");
798 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
799 // stat must be "table" by definition
800 $user_cur =& $this->user[$table_cur->player[$i]];
801 $bri_user =& $bri->user[$i];
803 $user_cur->subst = $bri_user->subst;
804 $user_cur->step = $bri_user->step;
805 $user_cur->lacc = $bri_user->lacc;
806 $user_cur->laccwr = $bri_user->lacc;
807 $user_cur->bantime = $bri_user->bantime;
810 $this->room_join_wakeup(&$user_cur, FALSE, 0);
811 $table_cur->table_token = "";
813 Briskin5::destroy_data($table_idx);
816 log_main("gm:: save_data");
818 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
819 $this->user[$table_cur->player[$i]]->lacc = $bri->user[$i]->lacc;
822 Briskin5::save_data(&$bri);
824 } // else if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
825 else if ($no_recovery == FALSE) {
826 log_crit("ERROR: table ".$table_idx." unrecoverable join");
828 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
829 $user_cur = &$this->user[$table_cur->player[$i]];
830 $user_cur->subst = "shutdowner";
831 $user_cur->step_inc();
833 $ret = sprintf('stat = "%s"; subst = "%s";', $user_cur->stat, $user_cur->subst);
834 $ret .= "gst.st = ".($user_cur->step+1)."; ";
835 $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);
836 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
837 $user_cur->step_inc();
840 $this->room_join_wakeup(&$user_cur, TRUE, -2);
841 $table_cur->table_token = "";
844 Briskin5::unlock_data($sem);
846 } // if ($table_cur->player_n == PLAYERS_N) {
847 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
849 log_rd2("out new loop.");
851 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
852 $user_cur = &$this->user[$i];
854 log_rd2("User: ".$user_cur->name." stat: ".$user_cur->stat." subst: ".$user_cur->subst);
856 if ($user_cur->sess == "")
859 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
860 // Auto logout dell'utente
861 log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
863 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
864 log_auth($user_cur->sess, "Autologout session.");
867 /* factorized with ->reset()
868 $tmp_sess = $user_cur->sess;
869 $user_cur->sess = "";
870 step_unproxy($tmp_sess);
871 $user_cur->name = "";
872 while (array_pop($user_cur->comm) != NULL);
874 $user_cur->the_end = FALSE;
877 log_rd2("AUTO LOGOUT.");
878 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
879 $this->room_wakeup(&$user_cur);
880 else if ($user_cur->subst == 'standup')
881 $this->room_outstandup(&$user_cur);
883 log_rd2("LOGOUT FROM WHAT ???");
887 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
888 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
889 $this->room_wakeup(&$user_cur);
890 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
891 $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);
892 $user_cur->step_inc();
896 log_rd2("GARBAGE UPDATED!");
898 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
906 function room_wakeup(&$user)
908 $table_idx = $user->table;
909 $table = &$this->table[$table_idx];
911 log_main("WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
915 $from_table = ($user->stat == "table");
917 log_main("WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
919 for ($i = 0 ; $i < $table->player_n ; $i++) {
920 $user_cur = &$this->user[$table->player[$i]];
921 log_main("PREIMPOST: INLOOP name: ".$user_cur->name);
923 if ($user_cur != $user) {
924 $user_cur->stat_set("room");
925 $user_cur->subst = "sitdown";
926 $user_cur->laccwr = $curtime;
928 else if ($user->sess != "") {
929 $user_cur->stat_set("room");
930 $user_cur->subst = "standup";
931 $user_cur->laccwr = $curtime;
932 $user_cur->table = -1;
937 $user->stat_set("room");
938 $user->subst = "standup";
939 $user->laccwr = $curtime;
941 /* aggiorna l'array dei giocatori al tavolo. */
942 $table->user_rem(&$this, &$user);
944 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
945 $user_cur = &$this->user[$i];
946 if ($user_cur->sess == '' || $user_cur->stat != 'room')
949 log_main("VALORI: name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
951 $ret = "gst.st = ".($user_cur->step+1)."; ";
952 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
953 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
954 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
955 log_main("DOCUMENT.index.php: from table");
957 else if ($user_cur->stat == "room") {
958 log_main("DOCUMENT.index.php: from table");
960 $ret .= table_content($this, $user_cur, $table_idx);
961 $ret .= standup_content($this, $user_cur);
963 $act_content = table_act_content(FALSE, 0, $table_idx, $user->table);
964 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
967 if ($user_cur == $user) {
968 // set the new status
969 $ret .= 'subst = "standup"; ';
970 // clean the action buttons in other tables
971 for ($e = 0 ; $e < TABLES_N ; $e++) {
972 if ($this->table[$e]->player_n < PLAYERS_N)
973 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
977 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
978 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
981 log_wr("ROOM_WAKEUP: ".$ret);
982 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
983 $user_cur->step_inc();
987 function room_join_wakeup(&$user, $update_lacc = FALSE, $trans_delta)
989 $table_idx = $user->table;
990 $table = &$this->table[$table_idx];
992 log_main("JOIN_WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
999 log_main("JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1001 for ($i = 0 ; $i < $table->player_n ; $i++) {
1002 $user_cur = &$this->user[$table->player[$i]];
1003 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1004 if ($user_cur->sess != "") {
1005 if ($update_lacc == TRUE) {
1006 $user_cur->laccwr = $curtime;
1008 log_main("cur: ".$user_cur->name." subst: ".$user_cur->subst);
1009 if ($user_cur->subst == "shutdowned") {
1010 $user_cur->stat_set("room");
1011 $user_cur->subst = "sitdown";
1013 else if ($user_cur->subst == "shutdowner") {
1014 $user_cur->stat_set("room");
1015 $user_cur->subst = "standup";
1016 $user_cur->table = -1;
1017 $user_wup[$user_wup_n++] = &$user_cur;
1019 $user_tab[$user_tab_n++] = &$user_cur;
1023 for ($wup_idx = 0 ; $wup_idx < $user_wup_n ; $wup_idx++)
1024 $table->user_rem(&$this, &$user_wup[$wup_idx]);
1026 /* aggiorna l'array dei giocatori al tavolo. */
1028 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1029 log_main("START LOOP");
1030 $user_cur = &$this->user[$i];
1031 if ($user_cur->sess == '' || $user_cur->stat != 'room') {
1032 log_main("name: ".$user_cur->name."skip subst: ".$user_cur->subst);
1037 log_main("VALORI name: ".$user_cur->name." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
1039 $ret = "gst.st = ".($user_cur->step+1)."; ";
1040 if ($user_cur->stat == "room") {
1041 log_main("DOCUMENT.index.php from table");
1043 $ret .= table_content($this, $user_cur, $table_idx);
1044 $ret .= standup_content($this, $user_cur);
1046 $act_content = table_act_content(FALSE, 0, $table_idx, $user_cur->table);
1047 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1049 for ($tab_idx = 0 ; $tab_idx < $user_tab_n ; $tab_idx++)
1050 if ($user_cur == $user_tab[$tab_idx])
1053 // for users that wakeup the room will be reconstructed by index_rd.php
1054 if ($tab_idx < $user_tab_n) {
1055 log_main("PRE show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1057 // ARRAY_POP DISABLED
1058 // if ($trans_delta == 0)
1059 // while (array_pop($user_cur->comm) != NULL);
1061 $user_cur->trans_step = $user_cur->step + 1 + $trans_delta;
1062 $user_cur->comm[$user_cur->step % COMM_N] = "";
1063 $user_cur->step_inc();
1064 $user_cur->comm[$user_cur->step % COMM_N] = show_room(&$this, ($user_cur->step + 1), &$user_cur);
1065 $user_cur->step_inc();
1066 log_main("POST show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1070 log_main("JOIN_WAKEUP wup_idx ".$wup_idx." wup_n ".$user_wup_n);
1072 log_main("JOIN_WAKEUP more");
1073 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
1074 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1075 log_main("JOIN_WAKEUP end more");
1077 log_wr("ROOM_JOIN_WAKEUP: ".$ret);
1078 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1079 $user_cur->step_inc();
1083 function room_outstandup(&$user)
1085 $this->room_sitdown(&$user, -1);
1088 function table_update(&$user)
1090 log_main("table_update: pre - USER: ".$user->name);
1092 $table_idx = $user->table;
1094 if ($table_idx > -1)
1095 $table = &$this->table[$table_idx];
1097 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1099 $user_cur = &$this->user[$i];
1100 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1103 $ret = "gst.st = ".($user_cur->step+1)."; ";
1104 if ($table_idx > -1)
1105 $ret .= table_content($this, $user_cur, $table_idx);
1107 if ($user_cur == $user) {
1108 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
1110 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1111 $user_cur->step_inc();
1114 log_main("table_update: post");
1117 function room_sitdown(&$user, $table_idx)
1119 log_main("room_sitdown ".($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
1121 if ($table_idx > -1)
1122 $table = &$this->table[$table_idx];
1124 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1126 $user_cur = &$this->user[$i];
1127 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1130 $ret = "gst.st = ".($user_cur->step+1)."; ";
1131 if ($table_idx > -1)
1132 $ret .= table_content($this, $user_cur, $table_idx);
1133 $ret .= standup_content($this, $user_cur);
1135 if ($user_cur == $user) {
1136 $ret .= 'subst = "sitdown"; ';
1137 // clean the action buttons in other tables
1138 for ($e = 0 ; $e < TABLES_N ; $e++) {
1139 $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
1140 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
1143 else if ($table_idx > -1) {
1144 if ($table->player_n == PLAYERS_N) {
1145 $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
1146 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1149 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1150 $user_cur->step_inc();
1154 function chatt_send(&$user, $mesg)
1156 GLOBAL $G_alarm_passwd;
1159 // if ($user->stat == 'table') {
1160 // $table = &$this->table[$user->table];
1163 $user_mesg = substr($mesg,6);
1166 $dt = date("H:i ", $timecur);
1167 if (strncmp($user_mesg, "/alarm ", 7) == 0) {
1168 $alarm_check = "/alarm ".$G_alarm_passwd." ";
1169 if (strncmp($user_mesg, $alarm_check, strlen($alarm_check)) != 0)
1172 $user_mesg = "<br><b>".$dt.NICKSERV."<br><br>".xcape(substr($user_mesg, strlen($alarm_check)))."</b><br><br>";
1173 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1174 $user_cur = &$this->user[$i];
1175 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1176 if ($user_cur->sess == '' || $user_cur->stat == 'table' || $user_cur == $user)
1179 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1180 // $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
1181 // $dt.NICKSERV, xcape($user_mesg));
1184 $user_cur->comm[$user_cur->step % COMM_N] .= show_notify($user_mesg, 0, "chiudi", 400, 120);
1185 $user_cur->step_inc();
1189 // Before all align times with table timeout
1190 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1191 $table_cur =& $this->table[$table_idx];
1192 // if the table is complete and exists its shared mem we get the info about users lacc
1194 if ($table_cur->player_n == PLAYERS_N) {
1195 log_main("PLAYERS == N TABLE ".$table_idx);
1197 if (($sem = Briskin5::lock_data($table_idx)) != FALSE) {
1198 log_main("bin5 lock data success");
1200 $no_recovery = FALSE;
1201 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
1202 if ($table_cur->table_token != $bri->table_token) {
1203 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
1208 if ($bri != FALSE) {
1209 $bri_table = &$bri->table[0];
1210 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
1211 // stat must be "table" by definition
1212 $bri_user =& $bri->user[$i];
1214 log_main("writa: ".$user_mesg);
1215 $bri_user->comm[$bri_user->step % COMM_N] = "gst.st = ".($bri_user->step+1)."; ";
1216 // $bri_user->comm[$bri_user->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
1217 // $dt.NICKSERV, xcape($user_mesg));
1218 $bri_user->comm[$bri_user->step % COMM_N] .= show_notify($user_mesg, 0, "chiudi", 400, 120);
1219 $bri_user->step_inc();
1221 Briskin5::save_data(&$bri);
1223 Briskin5::unlock_data($sem);
1225 } // if ($table_cur->player_n == PLAYERS_N) {
1226 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1228 else if (strncmp($user_mesg, "/nick ", 6) == 0) {
1229 log_main("chatt_send BEGIN");
1231 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
1232 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1233 $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));
1238 $user_mesg = "COMMAND ".$user_mesg;
1241 // update local graph
1242 // update remote graphs
1243 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1244 $user_cur = &$this->user[$i];
1245 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1246 if ($user_cur->sess == '')
1248 if ($user_cur->name == $name_new) {
1249 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1250 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
1255 if ($i == MAX_PLAYERS) {
1256 $user->name = $name_new;
1258 log_main("chatt_send start set");
1261 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1262 log_main("chatt_send set loop");
1264 $user_cur = &$this->user[$i];
1265 if ($user_cur->sess == '')
1267 if ($user_cur->stat == 'room') {
1268 if ($user->stat == 'room' && $user->subst == 'standup') {
1269 $this->standup_update(&$user);
1271 else if ($user->stat == 'room' && $user->subst == 'sitdown') {
1272 log_main("chatt_send pre table update");
1274 $this->table_update(&$user);
1276 log_main("chatt_send post table update");
1279 // else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
1280 // $table = &$this->table[$user->table];
1282 // $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1283 // $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1284 // xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
1285 // xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
1286 // xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
1287 // (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
1288 // (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
1289 // if ($user_cur == $user)
1290 // $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
1291 // xcape($user->name,ENT_COMPAT,"UTF-8"));
1292 // $user_cur->step_inc();
1298 if ($timecur < ($user->chat_ban + $user->chat_dlt)) {
1300 $user->chat_dlt = $user->chat_dlt * 2;
1301 if ($user->chat_dlt > 120)
1302 $user->chat_dlt = 120;
1304 else if ($user->chat_lst == $user_mesg)
1306 else if ($timecur - $user->chattime[($user->chat_cur + 1) % CHAT_N] < CHAT_ILL_TIME) {
1307 $user->chat_ban = $timecur;
1308 $user->chat_dlt = 5;
1312 $user->chat_ban = 0;
1313 $user->chat_dlt = 0;
1316 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
1317 if ($user->stat == 'room') {
1318 $user_cur = &$this->user[$i];
1319 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1324 if ($user_cur != $user)
1327 $user_mesg = "== chat ban ==";
1330 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1331 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
1332 $dt.xcape($user->name), xcape($user_mesg));
1333 $user_cur->step_inc();
1335 log_legal($timecur, $user->sess, $user->name,
1336 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
1338 $user->chat_lst = "$user_mesg";
1339 $user->chattime[$user->chat_cur % CHAT_N] = $timecur;
1344 function &get_user($sess, &$idx)
1346 GLOBAL $PHP_SELF, $G_false;
1348 if (validate_sess($sess)) {
1349 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1350 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1353 $ret = &$this->user[$i];
1357 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1358 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
1359 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1362 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1369 * function &add_user(&$room, &$sess, &$idx, $name, $ip)
1372 * if ($idx != -1 && ret == FALSE) => duplicated nick
1373 * if ($idx == -2 && ret == FALSE) => invalid name
1374 * if ($idx == -1 && ret == FALSE) => no space left
1375 * if (ret == TRUE) => SUCCESS
1377 function &add_user(&$sess, &$idx, $name, $ip)
1384 if (($name_new = validate_name($name)) == FALSE) {
1389 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
1390 if (validate_sess($sess) == FALSE)
1393 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1395 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1399 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
1402 if (strcmp($this->user[$i]->name, $name_new) == 0) {
1410 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1412 if ($idx != -1 && $i == MAX_PLAYERS) {
1416 $this->user[$idx]->sess = uniqid("");
1417 $sess = $this->user[$idx]->sess;
1421 $this->user[$idx]->sess = $sess;
1423 $this->user[$idx]->name = $name_new;
1424 $this->user[$idx]->stat_set("room");
1425 $this->user[$idx]->step_set(0);
1426 while (array_pop($this->user[$idx]->comm) != NULL);
1427 $this->user[$idx]->subst = "standup";
1428 $this->user[$idx]->lacc = $curtime;
1429 $this->user[$idx]->laccwr = $curtime;
1430 $this->user[$idx]->bantime = 0;
1431 $this->user[$idx]->ip = $ip;
1432 log_main(sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1434 return ($this->user[$idx]);
1440 function standup_update(&$user)
1442 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1443 $user_cur = &$this->user[$i];
1444 if ($user_cur->sess == '')
1447 log_main("STANDUP START: ".$user_cur->stat);
1449 if ($user_cur->stat == 'room') {
1450 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
1451 if ($user_cur == $user)
1452 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
1454 log_main("FROM STANDUP: NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
1456 $user_cur->step_inc();
1462 function &init_data()
1464 $room =& new Room();
1470 function &load_data()
1472 GLOBAL $G_false, $sess;
1475 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1476 log_main("ftok failed");
1481 if (($shm_sz = sharedmem_sz($tok)) == -1) {
1482 log_main("shmop_open failed");
1486 $shm_sz = SHM_DIMS_MIN;
1488 if ($shm = shm_attach($tok, $shm_sz)) {
1489 $room = @shm_get_var($shm, $tok);
1491 log_only("bri == ".($room == FALSE ? "FALSE" : "TRUE")." bri === ".($room === FALSE ? "FALSE" : "TRUE")." bri isset ".(isset($room) ? "TRUE" : "FALSE"));
1493 log_only("bri count ".count($room));
1495 if ($room == FALSE) {
1496 log_only("INIT MAIN DATA");
1498 $room =& Room::init_data();
1499 if (@shm_put_var($shm, $tok, $room) == FALSE) {
1500 log_only("PUT_VAR FALLITA ".strlen(serialize($room)));
1501 log_only(serialize($room));
1504 $room->shm_sz = $shm_sz;
1519 function save_data(&$room)
1528 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1)
1531 while ($room->shm_sz < SHM_DIMS_MAX) {
1532 if (($shm = shm_attach($tok, $room->shm_sz)) == FALSE)
1535 // log_only("PUT_VAR DI ".strlen(serialize($room)));
1536 if (shm_put_var($shm, $tok, $room) != FALSE) {
1540 if (shm_remove($shm) === FALSE) {
1541 log_only("REMOVE FALLITA");
1545 $room->shm_sz += SHM_DIMS_DLT;
1554 function lock_data()
1558 // echo "LOCK: ".FTOK_PATH."/main";
1560 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1564 // echo "FTOK ".$tok."<br>";
1565 if (($res = sem_get($tok)) == FALSE) {
1566 echo "SEM_GET FAILED";
1569 if (sem_acquire($res)) {
1570 log_lock("LOCK room");
1577 function unlock_data($res)
1581 log_lock("UNLOCK room");
1583 return (sem_release($res));
1587 function make_seed()
1589 list($usec, $sec) = explode(' ', microtime());
1590 return (float) $sec + ((float) $usec * 100000);
1594 function log_only2($log) {
1597 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONL2) == 0)
1600 if (isset($sess) == FALSE)
1605 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONL2) == 0)
1608 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1609 fwrite($fp, sprintf("ONL2: [%s] [%s]\n", $ssess, $log));
1614 function log_crit($log) {
1617 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_CRIT) == 0)
1620 if (isset($sess) == FALSE)
1625 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_CRIT) == 0)
1628 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1629 fwrite($fp, sprintf("CRIT: [%s] [%s]\n", $ssess, $log));
1634 function log_only($log) {
1637 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONLY) == 0)
1640 if (isset($sess) == FALSE)
1645 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONLY) == 0)
1648 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1649 fwrite($fp, sprintf("ONLY: [%s] [%s]\n", $ssess, $log));
1654 function log_main($log) {
1657 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_MAIN) == 0)
1660 if (isset($sess) == FALSE)
1665 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_MAIN) == 0)
1668 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1669 fwrite($fp, sprintf("MAIN: [%s] [%s]\n", $ssess, $log));
1674 function log_rd($log) {
1677 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_READ) == 0)
1680 if (isset($sess) == FALSE)
1685 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_READ) == 0)
1688 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1689 fwrite($fp, sprintf("READ: [%s] [%s]\n", $ssess, $log));
1694 function log_rd2($log) {
1697 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_REA2) == 0)
1700 if (isset($sess) == FALSE)
1705 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_REA2) == 0)
1708 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1709 fwrite($fp, sprintf("REA2: [%s] [%s]\n", $ssess, $log));
1714 function log_send($log) {
1717 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SEND) == 0)
1720 if (isset($sess) == FALSE)
1725 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SEND) == 0)
1728 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1729 fwrite($fp, sprintf("SEND: [%s] [%s]\n", $ssess, $log));
1734 function log_lock($log) {
1737 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOCK) == 0)
1740 if (isset($sess) == FALSE)
1745 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOCK) == 0)
1748 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1749 fwrite($fp, sprintf("LOCK: [%s] [%s]\n", $ssess, $log));
1754 function log_wr($log) {
1757 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_WRIT) == 0)
1760 if (isset($sess) == FALSE)
1765 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_WRIT) == 0)
1768 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1769 fwrite($fp, sprintf("WRIT: [%s] [%s]\n", $ssess, $log));
1774 function log_load($log) {
1777 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOAD) == 0)
1780 if (isset($sess) == FALSE)
1785 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOAD) == 0)
1788 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1789 fwrite($fp, sprintf("LOAD: [%s] [%s]\n", $ssess, $log));
1794 function log_auth($sess, $log) {
1795 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_AUTH) == 0)
1798 if (( (BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_AUTH) == 0)
1801 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1802 fwrite($fp, sprintf("AUTH: [%s] [%d] [%s]\n", $sess, time(), $log));
1808 function log_legal($timecur, $sess, $name, $where, $mesg)
1812 if (($fp = @fopen(LEGAL_PATH."/legal.log", 'a')) != FALSE) {
1813 /* Unix time | session | nickname | IP | where was | mesg */
1814 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|\n", $timecur, $sess, $name, $_SERVER['REMOTE_ADDR'], $where , $mesg));
1822 function lock_banlist()
1824 if (($tok = @ftok(FTOK_PATH."/main", "L")) == -1) {
1828 if (($res = sem_get($tok)) == FALSE) {
1829 echo "SEM_GET FAILED";
1832 if (sem_acquire($res))
1838 function unlock_banlist($res)
1840 return (sem_release($res));
1843 function table_act_content($isstanding, $sitted, $table, $cur_table)
1848 if ($sitted < PLAYERS_N) {
1849 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
1853 if ($table == $cur_table)
1854 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
1861 function table_content($room, $user, $table_idx)
1867 // Si possono usare i dati nella classe table
1870 $sess = $user->sess;
1871 $table = &$room->table[$table_idx];
1873 if ($user->stat != 'room')
1876 for ($i = 0 ; $i < $table->player_n ; $i++) {
1877 $user_cur = &$room->user[$table->player[$i]];
1879 if ($user_cur == $user)
1880 { $hilion = "<b>"; $hilioff = "</b>"; }
1882 { $hilion = ""; $hilioff = ""; }
1884 log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
1886 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
1889 for ( ; $i < PLAYERS_N ; $i++)
1893 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
1898 function standup_content(&$room, $user)
1903 if ($user->stat != 'room')
1906 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
1907 if ($room->user[$e]->sess == "" || $room->user[$e]->stat != "room" || $room->user[$e]->name == "")
1912 $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
1914 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
1915 if ($room->user[$e]->sess == "" || $room->user[$e]->stat != "room" || $room->user[$e]->name == "")
1919 if ($room->user[$e]->subst == "standup") {
1920 if (($ct % 4) == 0) {
1923 if ($room->user[$e] == $user)
1924 { $hilion = "<b>"; $hilioff = "</b>"; }
1926 { $hilion = ""; $hilioff = ""; }
1928 $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($room->user[$e]->name), $hilioff);
1929 if (($ct % 4) == 3) {
1930 $content .= '</tr>';
1935 $content .= '</table>';
1937 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
1938 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
1939 $content, $content2);
1945 function show_notify($text, $tout, $butt, $w, $h)
1947 log_main("SHOW_NOTIFY: ".$text);
1948 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
1951 function briscola_show($room, $table, $user)
1956 if ($table->asta_card == 9)
1957 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1959 /* text of caller cell */
1960 if ($user->table_pos == $table->asta_win)
1961 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1963 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
1964 xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
1966 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1968 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1969 $ret .= sprintf('$("caller").style.visibility = "visible";');
1970 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1971 $ret .= sprintf('$("astalascio").style.visibility = "";');
1972 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1973 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1979 function game_result($asta_pnt, $pnt)
1981 if ($asta_pnt == 61) {
1984 else if ($pnt == 60)
1990 if ($pnt >= $asta_pnt)
1997 function multoval($mult)
2001 else if ($mult == 4)
2002 return ("quadruplo");
2004 return (sprintf("%d-plo", $mult));
2007 function show_table_info(&$room, &$table, $table_pos)
2010 $user = &$room->user[$table->player[$table_pos]];
2012 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
2013 $noty = sprintf('<table class=\"points\"><tr><th></th>');
2016 for ($i = 0 ; $i < PLAYERS_N ; $i++)
2017 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
2018 $noty .= sprintf("</tr>");
2021 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
2023 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
2024 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
2025 for ($e = 0 ; $e < PLAYERS_N ; $e++)
2026 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
2031 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
2032 for ($e = 0 ; $e < PLAYERS_N ; $e++)
2033 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
2034 $noty .= "</tr></table>";
2036 if ($table->old_reason != "") {
2037 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
2040 if ($table->old_win != -1) {
2041 $win = $table->player[$table->old_win];
2042 $fri = $table->player[$table->old_friend];
2044 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
2047 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
2048 xcape($room->user[$win]->name),
2049 xcape($room->user[$fri]->name));
2050 if ($table->old_pnt == 120) {
2051 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
2054 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
2055 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
2056 'più di 60'), $table->old_pnt,
2057 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
2061 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
2062 xcape($room->user[$win]->name));
2063 if ($table->old_pnt == 120) {
2064 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
2067 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
2068 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
2069 'più di 60'), $table->old_pnt,
2070 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
2074 if ($table->mazzo == $table_pos)
2075 $noty .= "Fai <b>tu</b> il mazzo,";
2077 $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
2078 $noty .= "Il mazzo a <b>$unam</b>,";
2081 if ($user->subst == 'asta') {
2082 if ($table->asta_win == -1) // auction case
2083 $curplayer = $table->gstart % PLAYERS_N;
2085 $curplayer = $table->asta_win;
2087 else if ($user->subst == 'game') {
2088 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
2092 if ($curplayer == $table_pos) {
2093 $noty .= " tocca a <b>te</b> giocare.";
2096 $unam = xcape($room->user[$table->player[$curplayer]]->name);
2097 $noty .= " tocca a <b>$unam</b> giocare.";
2100 if ($table->mult > 1) {
2101 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
2103 $noty .= "<hr><br>";
2105 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
2110 function root_wellcome($user)
2112 GLOBAL $root_wellarr;
2116 $dt = date("H:i ", $timecur);
2118 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
2119 $ret .= sprintf('chatt_sub("%s","%s");', $dt.NICKSERV, str_replace('"', '\"', $root_wellarr[$i]));
2124 function table_wellcome($user)
2126 GLOBAL $table_wellarr;
2129 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
2130 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
2135 function show_room(&$room, $user_step, &$user)
2137 log_main("show_room: username: ".$user->name);
2140 $ret = sprintf('gst.st = %d;', $user_step);
2141 $ret .= sprintf('stat = "%s";', $user->stat);
2143 $ret .= root_wellcome($user);
2144 $ret .= sprintf('subst = "%s";', $user->subst);
2145 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
2146 for ($i = 0 ; $i < TABLES_N ; $i++) {
2147 $ret .= table_content($room, $user, $i);
2148 $act_content = table_act_content(($user->subst == 'standup'),
2149 $room->table[$i]->player_n, $i, $user->table);
2150 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
2152 $ret .= standup_content($room, $user);
2160 is_transition (is from room to table ?)
2161 is_again (is another game)
2163 Examples of $is_transition, $is_again:
2164 from reload of the page: FALSE, FALSE
2165 from sitdown in room: TRUE, FALSE
2166 from table: asta cmd e tutti passano: TRUE, TRUE
2167 from table: fine partita: TRUE, TRUE
2169 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
2171 $table_idx = $user->table;
2172 $table = &$room->table[$table_idx];
2173 $table_pos = $user->table_pos;
2175 $ret = "table_init();";
2176 $ret .= $table->exitlock_show(&$room->user, $table_pos);
2178 /* GENERAL STATUS */
2179 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
2180 $sendstep, $user->stat, $user->subst, $table_pos);
2182 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
2183 $sendstep, $user->stat, $user->subst, $table_pos));
2186 $ret .= "background_set();";
2189 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
2190 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
2191 xcape($room->user[$table->player[($table_pos)%PLAYERS_N]]->name),
2192 xcape($room->user[$table->player[($table_pos+1)%PLAYERS_N]]->name),
2193 xcape($room->user[$table->player[($table_pos+2)%PLAYERS_N]]->name),
2194 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+3)%PLAYERS_N]]->name)),
2195 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+4)%PLAYERS_N]]->name)));
2197 /* NOTIFY FOR THE CARD MAKER */
2198 if ($is_transition) { // && $user->subst == "asta" superfluo
2199 $ret .= show_table_info(&$room, &$table, $table_pos);
2202 $ret .= table_wellcome($user);
2204 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
2205 $ret .= playsound("cow.mp3");
2210 if ($is_transition) { // && $user->subst == "asta" superfluo
2213 for ($i = 0 ; $i < 8 ; $i++) {
2214 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
2216 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
2217 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
2223 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
2225 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
2226 $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
2227 $table->card[$o]->value : -1),
2228 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
2233 $taked = array(0,0,0,0,0);
2234 $inhand = array(0,0,0,0,0);
2235 $ontabl = array(-1,-1,-1,-1,-1);
2238 for ($i = 0 ; $i < 40 ; $i++) {
2239 if ($table->card[$i]->stat == 'hand') {
2240 if ($table->card[$i]->owner == $table_pos) {
2241 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
2243 $inhand[$table->card[$i]->owner]++;
2245 else if ($table->card[$i]->stat == 'take') {
2246 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
2247 $taked[$table->card[$i]->owner]++;
2249 else if ($table->card[$i]->stat == 'table') {
2250 $ontabl[$table->card[$i]->owner] = $i;
2254 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2255 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
2257 log_main("Stat table: ".$logg);
2259 /* Set ours cards. */
2261 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
2262 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
2263 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
2264 $oursarg .= ($i == 0 ? "" : ", ")."-1";
2265 $ret .= sprintf('card_setours(%s);', $oursarg);
2267 /* Dispose all cards */
2268 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2269 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
2270 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
2271 $inhand[$i], $taked[$i]);
2273 if ($ontabl[$i] != -1) {
2274 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
2275 $table->card[$ontabl[$i]]->value,
2276 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
2282 if ($user->subst == 'asta') {
2284 /* show users auction status */
2286 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2287 $user_cur = &$room->user[$table->player[$i]];
2288 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
2289 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
2292 $showst .= ",-2,-2";
2293 $ret .= sprintf('show_astat(%s);', $showst);
2295 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
2296 /* show card chooser */
2297 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
2302 if ($table_pos == ($table->gstart % PLAYERS_N) &&
2303 $table->asta_win == -1)
2304 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2305 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
2307 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2308 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
2312 if ($table->asta_win == -1) { // auction case
2313 if ($table_pos == ($table->gstart % PLAYERS_N))
2314 $ret .= "remark_on();";
2316 $ret .= "remark_off();";
2318 else { // chooseed case
2319 if ($table_pos == $table->asta_win)
2320 $ret .= "remark_on();";
2322 $ret .= "remark_off();";
2325 else if ($user->subst == 'game') {
2327 if (($table->gstart + $table->turn) % PLAYERS_N == $table_pos)
2328 $ret .= "is_my_time = true; remark_on();";
2330 $ret .= "remark_off();";
2332 /* WHO CALL AND WATH */
2333 $ret .= briscola_show($room, $table, $user);
2337 } // end function show_table(...
2339 function calculate_winner(&$table)
2346 $cur_seed = $table->briscola - ($table->briscola % 10);
2348 for ($i = 0 ; $i < 40 ; $i++) {
2349 if ($table->card[$i]->stat != "table")
2352 log_wr(sprintf("Card On table: [%d]", $i));
2354 $v = $table->card[$i]->value;
2355 $ontab[$table->card[$i]->owner] = $v;
2356 $ontid[$table->card[$i]->owner] = $i;
2357 /* se briscola setto il flag */
2358 if (($v - ($v % 10)) == $cur_seed)
2362 if ($briontab == FALSE) {
2363 $cur_win = $table->gstart;
2364 $cur_val = $ontab[$cur_win];
2365 $cur_seed = $cur_val - ($cur_val % 10);
2368 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2369 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
2370 if ($ontab[$i] < $cur_val) {
2371 $cur_val = $ontab[$i];
2377 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2378 $table->card[$ontid[$i]]->owner = $cur_win;
2379 $table->card[$ontid[$i]]->stat = "take"; // Card stat
2384 function calculate_points(&$table)
2386 GLOBAL $G_all_points;
2390 if ($table->asta_pnt == 60)
2391 $table->asta_pnt = 61;
2393 $table->old_reason = "";
2394 $table->old_win = $table->asta_win;
2395 $table->old_friend = $table->friend;
2396 $table->old_asta_pnt = $table->asta_pnt;
2398 for ($i = 0 ; $i < 40 ; $i++) {
2399 $ctt = $table->card[$i]->value % 10;
2400 $own = $table->card[$i]->owner;
2401 if ($own == $table->asta_win || $own == $table->friend)
2402 $pro += $G_all_points[$ctt];
2405 log_wr(sprintf("PRO: [%d]", $pro));
2408 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
2409 $table->points[$table->points_n % MAX_POINTS] = array();
2410 for ($i = 0 ; $i < PLAYERS_N ; $i++)
2411 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
2413 $table->old_pnt = $pro;
2419 if ($pro >= $table->asta_pnt)
2424 $table->points[$table->points_n % MAX_POINTS] = array();
2425 for ($i = 0 ; $i < 5 ; $i++) {
2426 if ($i == $table->asta_win)
2427 $pt = ($i == $table->friend ? 4 : 2);
2428 else if ($i == $table->friend)
2433 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
2435 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
2437 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
2439 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
2440 $table->total[$i] += $pt;
2443 $table->old_pnt = $pro;
2447 function validate_sess($sess)
2449 if (strlen($sess) == SESS_LEN)
2455 function validate_name($name)
2457 $name_new = str_replace(' ', '_', substr(trim($name),0,12));
2459 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
2461 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
2468 function playsound($filename)
2470 return (sprintf('playsound("flasou", "%s");', $filename));
2473 function secstoword($secs)
2475 $mins = floor($secs / 60);
2478 $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
2481 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));
2486 function sharedmem_sz($tok)
2488 if (($shm_id = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
2489 log_main("shmop_open failed");
2492 $shm_sz = shmop_size($shm_id);
2493 shmop_close($shm_id);
2495 log_main("shm_sz: ".$shm_sz." SHM_DIMS: ".SHM_DIMS);