5 * Copyright (C) 2006-2008 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details. You should have received a
19 * copy of the GNU General Public License along with this program; if
20 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21 * Suite 330, Boston, MA 02111-1307, USA.
26 define(BRISK_CONF, "brisk.conf.pho");
27 define(FTOK_PATH, "/var/lib/brisk");
28 define(LEGAL_PATH, "/tmp/legal_brisk");
29 define(PROXY_PATH, "/var/lib/brisk_proxy");
32 define(MAX_POINTS, 5);
33 define(MAX_PLAYERS, (20 + (PLAYERS_N * TABLES_N)));
34 define(SHM_DIMS_MIN, (50000 + 10000 * TABLES_N + 15000 * MAX_PLAYERS));
35 define(SHM_DIMS_MAX, SHM_DIMS_MIN + 1048576);
36 define(SHM_DIMS_DLT, 65536);
39 define(COMM_GEN_N, 50);
42 define(CHAT_ILL_TIME, 6);
45 define(STREAM_TIMEOUT, 20);
46 define(EXPIRE_TIME_RD, 180);
47 define(EXPIRE_TIME_SMAMMA, 360);
48 define(EXPIRE_TIME_WAG, 10);
49 define(WAKEUP_TIME, 12);
50 // BAN_TIME da allineare anche in commons.js
51 define(BAN_TIME, 900);
52 define(GARBAGE_TIMEOUT, 10);
53 define(NICKSERV, "<i>BriskServ</i>");
55 define(DBG_ONL2, 0x0001);
56 define(DBG_ONLY, 0x0002);
57 define(DBG_MAIN, 0x0004);
58 define(DBG_READ, 0x0008);
59 define(DBG_REA2, 0x0010);
60 define(DBG_SEND, 0x0020);
61 define(DBG_LOCK, 0x0040);
62 define(DBG_WRIT, 0x0080);
63 define(DBG_LOAD, 0x0100);
64 define(DBG_AUTH, 0x0200);
65 define(DBG_CRIT, 0x0400);
67 define(BRISK_DEBUG, 0);
69 define(BRISK_SINGLE_DEBUG,0);
70 define(BRISK_SINGLE_SESS, "");
71 // define(DEBUGGING, "local");
73 require_once("$DOCUMENT_ROOT/Etc/".BRISK_CONF);
78 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
79 $G_brisk_version = "spawn-0.12.0";
81 $root_wellarr = Array ( 'Benvenuto in brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: aggiunta messaggistica a tutti i tavoli - versione beta2.',
82 'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
83 $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.');
87 <div style=\\"text-align: left; padding: 8px;\\">
88 <b>Descrizione</b><br>
89 Questa è un\'implementazione della briscola in cinque, così come è spiegata su
90 <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>
91 <b>Configurazione del browser.</b><br>
92 Occorre abilitare i cookies.<br>
94 <b>Uso del sito</b><br>
95 Potete sedervi a un tavolo o rimanere in piedi.<br>
96 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
99 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>
100 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
101 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
102 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
103 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
104 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.
106 <dt><b>Comandi della chat</b>
107 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
108 <dd><b>/tav <i><frase di invitogt;</i></b> - invito per gli altri giocatori al tavolo dove si è seduti
114 <div id=\\"header\\" class=\\"header\\">
115 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
116 briscola chiamata in salsa ajax
118 <br><b>version '.$G_brisk_version.'</b><br><br>
119 Copyright 2006-2007 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
124 $from = array ( '\\', '@', '|' );
125 $to = array ( '\\\\', '@', '¦' );
127 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
132 var $value; /* 0 - 39 card value */
133 var $stat; /* 'bunch', 'hand', 'table', 'take' */
134 var $owner; /* (table position 0-4) */
135 // var $pos; /* Pos in hand. */
136 var $x; /* When played the X position on the table of the owner. */
137 var $y; /* When played the Y position on the table of the owner. */
139 function Card($value, $stat, $owner)
141 $this->value = $value;
142 $this->stat = $stat; // Card stat
143 $this->owner = $owner;
146 function assign($stat,$owner)
148 $this->stat = $stat; // Card stat
149 $this->owner = $owner;
152 function setpos($pos)
159 $this->stat = 'table'; // Card stat
164 function take($newown)
166 $this->stat = 'take'; // Card stat
167 $this->owner = $newown;
189 var $points; // points array
190 var $points_n; // number of row of points
216 if (($thiz =& new Table()) == FALSE)
219 $thiz->player = array();
222 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
223 $thiz->asta_pla_n= -1;
224 $thiz->asta_card = -1;
225 $thiz->asta_pnt = -1;
227 $thiz->points = array( );
229 $thiz->total = array( 0, 0, 0, 0, 0);
230 $thiz->asta_win = -1;
231 $thiz->briscola = -1;
235 $thiz->wag_own = NULL;
239 $thiz->old_reason = "";
240 $thiz->old_asta_pnt = -1;
243 $thiz->old_friend = -1;
245 $thiz->table_token = "";
246 $thiz->table_start = 0;
248 $thiz->wakeup_time = 0;
253 function &clone(&$from)
257 if (($thiz =& new Table()) == FALSE)
260 $thiz->player = array();
261 for ($i = 0 ; $i < $from->player_n ; $i++)
262 $thiz->player[$i] = $from->player[$i];
263 $thiz->player_n = $from->player_n;
264 $thiz->card = $from->card;
265 $thiz->mazzo = $from->mazzo; // REVIEW
266 $thiz->gstart = $from->gstart;
267 $thiz->turn = $from->turn;
269 $thiz->wag_own = $from->wag_own;
270 $thiz->wag_com = $from->wag_com;
271 $thiz->wag_tout = $from->wag_taut;
273 $thiz->asta_pla = $from->asta_pla;
274 $thiz->asta_pla_n = $from->asta_pla_n;
275 $thiz->asta_card = $from->asta_card;
276 $thiz->asta_pnt = $from->asta_pnt;
278 $thiz->mult = $from->mult;
279 $thiz->points = $from->points;
280 $thiz->points_n = $from->points_n;
281 $thiz->total = $from->total;
283 $thiz->asta_win = $from->asta_win;
284 $thiz->briscola = $from->briscola;
285 $thiz->friend = $from->friend;
287 $thiz->old_reason = $from->old_reason;
288 $thiz->old_asta_pnt = $from->old_asta_pnt;
289 $thiz->old_pnt = $from->old_pnt;
290 $thiz->old_win = $from->old_win;
291 $thiz->old_friend = $from->old_friend;
293 $thiz->table_token = $from->table_token;
294 $thiz->table_start = $from->table_start;
296 $thiz->wakeup_time = $from->wakeup_time;
301 function &spawn(&$from)
305 if (($thiz =& new Table()) == FALSE)
308 $thiz->player_n = $from->player_n;
309 $thiz->card = &$thiz->bunch_create();
310 $thiz->mazzo = $from->mazzo;
311 $thiz->gstart = $from->gstart;
312 $thiz->turn = $from->turn;
314 $thiz->wag_own = $from->wag_own;
315 $thiz->wag_com = $from->wag_com;
316 $thiz->wag_tout = $from->wag_taut;
318 $thiz->asta_pla = $from->asta_pla;
319 $thiz->asta_pla_n = $from->asta_pla_n;
320 $thiz->asta_card = $from->asta_card;
321 $thiz->asta_pnt = $from->asta_pnt;
323 $thiz->mult = $from->mult;
324 $thiz->points = $from->points;
325 $thiz->points_n = $from->points_n;
326 $thiz->total = $from->total;
328 $thiz->asta_win = $from->asta_win;
329 $thiz->briscola = $from->briscola;
330 $thiz->friend = $from->friend;
332 $thiz->old_reason = $from->old_reason;
333 $thiz->old_asta_pnt = $from->old_asta_pnt;
334 $thiz->old_pnt = $from->old_pnt;
335 $thiz->old_win = $from->old_win;
336 $thiz->old_friend = $from->old_friend;
338 // players are rearranged in an dedicated array
339 $thiz->player = array();
340 for ($i = 0 ; $i < $from->player_n ; $i++)
341 $thiz->player[$i] = $i;
343 $thiz->table_token = $from->table_token;
344 $thiz->table_start = $from->table_start;
346 $thiz->wakeup_time = $from->wakeup_time;
351 function &bunch_create()
355 for ($i = 0 ; $i < 40 ; $i++) {
356 $ret[$i] =& new Card($i, 'bunch', 'no_owner');
363 function wag_set(&$user, $mesg)
367 $this->wag_own = &$user;
368 $this->wag_com = $mesg;
372 function wag_reset($timeout)
374 log_main("WAG_RESET");
376 unset($this->wag_own);
377 $this->wag_own = NULL;
379 $this->wag_tout = $timeout;
382 function bunch_make()
384 $ct = array(0,0,0,0,0);
386 mt_srand(make_seed());
388 for ($i = 39 ; $i >= 0 ; $i--)
391 for ($i = 39 ; $i >= 0 ; $i--) {
395 log_main("RND ZERO");
400 $this->card[$id]->assign('hand', $owner);
402 $rest[$rn] = $rest[$i];
403 // $pubbpos[$rn2] = $pubbpos[$i];
407 function init(&$userarr)
409 $this->mazzo = rand(0,PLAYERS_N-1);
413 $this->old_reason = "";
414 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
415 $this->total[$i] = 0;
416 $user_cur = &$userarr[$this->player[$i]];
417 $user_cur->exitislock = TRUE;
420 log_main("table::init: ci siamo");
423 function game_init(&$userarr)
427 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
431 $this->asta_pla_n = PLAYERS_N;
432 $this->asta_card = -1;
433 $this->asta_pnt = 60;
434 $this->asta_win = -1;
435 $this->briscola = -1;
439 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
440 $this->asta_pla[$i] = TRUE;
441 $user_cur = &$userarr[$this->player[$i]];
442 $user_cur->subst = 'asta';
443 $user_cur->asta_card = -2;
444 $user_cur->asta_pnt = -1;
445 $user_cur->handpt = $this->hand_points($i);
446 // SEE function calculate_points(&$table)
452 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
455 function getPlayer($idx)
457 return ($this->player[$idx]);
460 function setPlayer($idx, $player)
462 $this->player[$idx] = $player;
465 function user_add($idx)
467 $this->player[$this->player_n] = $idx;
470 return ($this->player_n - 1);
473 function user_rem(&$room, &$user)
475 $tabpos = $user->table_pos;
477 /* verifico la consistenza dei dati */
478 if ($room->user[$this->player[$tabpos]] == $user) {
480 /* aggiorna l'array dei giocatori al tavolo. */
481 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
482 $this->player[$i] = $this->player[$i+1];
483 $user_cur = &$room->user[$this->player[$i]];
484 $user_cur->table_pos = $i;
489 log_main("INCONSISTENCY ON TABLE.");
493 function hand_points($idx)
495 GLOBAL $G_all_points;
499 for ($i = 0 ; $i < 40 ; $i++) {
500 if ($this->card[$i]->owner != $idx)
503 $ctt = $this->card[$i]->value % 10;
504 $tot += $G_all_points[$ctt];
510 function exitlock_show(&$userarr, $table_pos)
512 $ct = $this->exitlock_calc(&$userarr, $table_pos);
514 $ret = sprintf('exitlock_show(%d, %s);', $ct,
515 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
519 function exitlock_calc(&$userarr, $table_pos)
523 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
524 if ($userarr[$this->player[$i]]->exitislock == FALSE)
533 var $name; // name of the user
534 var $sess; // session of the user
535 var $ip; // ip of the user
536 var $lacc; // last access (for the cleanup)
537 var $laccwr; // last access (for the cleanup)
538 var $bantime; // timeout to temporary ban
539 var $stat; // status (outdoor, room, table, game, ...)
540 var $subst; // substatus for each status
541 var $step; // step of the current status
542 var $trans_step; // step to enable transition between pages (disable == -1)
543 var $comm; // commands array
546 var $handpt; // Total card points at the beginning of the current hand.
547 var $exitislock; // Player can exit from the table ?
548 var $table; // id of the current table (if in table state)
549 var $table_pos; // idx on the table
550 var $table_token;// token that identify a game on a table
551 var $the_end; // Flag to change the end of the session
553 var $chat_lst; // Last chat line
554 var $chattime; // Array of chat times
555 var $chat_cur; // Current chat line number
556 var $chat_ban; // Time for ban chat
557 var $chat_dlt; // Delta t for ban
561 function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
564 if (($thiz =& new User()) == FALSE)
570 $thiz->lacc = time();
571 $thiz->laccwr = time();
574 $thiz->subst = $subst;
576 $thiz->trans_step = -1;
577 $thiz->comm = array();
578 $thiz->asta_card = -2;
579 $thiz->asta_pnt = -1;
581 $thiz->exitislock = TRUE;
583 $thiz->chattime = array_fill(0, CHAT_N, 0);
585 $thiz->chat_lst = "";
589 $thiz->table = $table;
590 $thiz->table_pos = -1;
591 $thiz->table_token = "";
596 function &clone(&$from)
600 if (($thiz =& new User()) == FALSE)
603 $thiz->name = $from->name;
604 $thiz->sess = $from->sess;
605 $thiz->ip = $from->ip;
606 $thiz->lacc = $from->lacc;
607 $thiz->laccwr = $from->laccwr;
608 $thiz->bantime = $from->bantime;
609 $thiz->stat = $from->stat;
610 $thiz->subst = $from->subst;
611 $thiz->step = $from->step;
612 $thiz->trans_step = $from->trans_step;
613 $thiz->comm = array();
615 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
616 for ($i = $i_start ; $i < $from->step ; $i++) {
618 $thiz->comm[$ii] = $from->comm[$ii];
620 $thiz->asta_card = $from->asta_card;
621 $thiz->asta_pnt = $from->asta_pnt;
622 $thiz->handpt = $from->handpt;
623 $thiz->exitislock = $from->exitislock;
625 $thiz->chattime = array();
626 for ($i = 0 ; $i < CHAT_N ; $i++)
627 $thiz->chattime[$i] = $from->chattime[$i];
628 $thiz->chat_cur = $from->chat_cur;
629 $thiz->chat_lst = $from->chat_lst;
630 $thiz->chat_ban = $from->chat_ban;
631 $thiz->chat_dlt = $from->chat_dlt;
633 $thiz->table = $from->table;
634 $thiz->table_pos = $from->table_pos;
635 $thiz->table_token = $from->table_token;
636 $thiz->the_end = $from->the_end;
641 function &spawn(&$from, $table, $table_pos)
645 if (($thiz =& new User()) == FALSE)
648 $thiz->name = $from->name;
649 $thiz->sess = $from->sess;
650 $thiz->ip = $from->ip;
651 $thiz->lacc = $from->lacc;
652 $thiz->laccwr = $from->laccwr;
653 $thiz->bantime = $from->bantime;
654 $thiz->stat = $from->stat;
655 $thiz->subst = $from->subst;
656 $thiz->step = $from->step;
657 $thiz->trans_step = $from->trans_step;
658 $thiz->comm = array();
661 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
662 for ($i = $i_start ; $i < $from->step ; $i++) {
663 log_wr("TRY PUSH:".$i);
665 $thiz->comm[$ii] = $from->comm[$ii];
668 $thiz->asta_card = $from->asta_card;
669 $thiz->asta_pnt = $from->asta_pnt;
670 $thiz->handpt = $from->handpt;
671 $thiz->exitislock = $from->exitislock;
672 $thiz->the_end = $from->the_end;
674 $thiz->chattime = array_fill(0, CHAT_N, 0);
676 $thiz->chat_lst = "";
680 $thiz->table = $table;
681 $thiz->table_pos = $table_pos;
682 $thiz->table_token = $from->table_token;
687 function stat_set($stat) {
688 log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]");
689 $this->stat = "$stat";
692 if (validate_sess($this->sess)) {
693 $fp = fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
694 fwrite($fp, sprintf("%s\n",$this->stat));
700 function step_set($step) {
704 if (validate_sess($this->sess) == FALSE)
706 if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
708 fwrite($fp, pack("l",$this->step), 4);
717 function step_inc($delta = 1) {
718 $this->step += $delta;
720 if (validate_sess($this->sess)) {
721 $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
722 fwrite($fp, pack("l",$this->step), 4);
732 $tmp_sess = $this->sess;
734 step_unproxy($tmp_sess);
736 while (array_pop($this->comm) != NULL);
738 $this->the_end = FALSE;
743 function step_get($sess) {
747 if (validate_sess($sess) == FALSE)
750 if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
753 if (($s = fread($fp, 4)) == FALSE)
759 $arr = unpack('l', $s);
762 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
769 log_rd2("STEP_GET: return false ".$ct);
773 function step_unproxy($sess) {
774 log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
775 @unlink(PROXY_PATH."/".$sess.".step");
782 var $comm; // commands for many people
783 var $step; // current step of the comm array
784 var $garbage_timeout;
788 $this->user = array();
789 $this->table = array();
791 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
792 $this->user[$i] =& User::create("", "");
795 for ($i = 0 ; $i < TABLES_N ; $i++)
796 $this->table[$i] =& Table::create();
797 $this->garbage_timeout = 0;
800 function garbage_manager($force)
804 log_rd2("garbage_manager START");
806 /* Garbage collector degli utenti in timeout */
808 if ($force || $this->garbage_timeout < $curtime) {
810 // Before all align times with table timeout
811 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
812 $table_cur =& $this->table[$table_idx];
813 // if the table is complete and exists its shared mem we get the info about users lacc
815 if ($table_cur->player_n == PLAYERS_N) {
816 log_main("PLAYERS == N TABLE ".$table_idx);
818 if (($sem = Briskin5::lock_data($table_idx)) != FALSE) {
819 log_main("bin5 lock data success");
821 $no_recovery = FALSE;
822 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
823 if ($table_cur->table_token != $bri->table_token) {
824 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
825 log_main("ERROR: not matching table_start. Room: ".$table_cur->table_start." Table: ".$bri->table_start);
835 log_main("garbage_manager: bri loaded successfully.");
836 $bri->garbage_manager(TRUE);
838 $bri_table = &$bri->table[0];
840 // is the end of the table
841 if ($bri->the_end == TRUE) {
843 * DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN
845 log_main("garbage_manager: INSIDE THE END.");
847 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
848 // stat must be "table" by definition
849 $user_cur =& $this->user[$table_cur->player[$i]];
850 $bri_user =& $bri->user[$i];
852 $user_cur->subst = $bri_user->subst;
853 $user_cur->step = $bri_user->step;
854 $user_cur->lacc = $bri_user->lacc;
855 $user_cur->laccwr = $bri_user->lacc;
856 $user_cur->bantime = $bri_user->bantime;
859 $this->room_join_wakeup(&$user_cur, FALSE, 0);
860 $table_cur->table_token = "";
861 $table_cur->wakeup_time = $curtime + WAKEUP_TIME;
862 Briskin5::destroy_data($table_idx);
865 log_main("gm:: save_data");
867 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
868 $this->user[$table_cur->player[$i]]->lacc = $bri->user[$i]->lacc;
871 Briskin5::save_data(&$bri);
873 } // else if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
874 else if ($no_recovery == FALSE) {
875 log_crit("ERROR: table ".$table_idx." unrecoverable join");
877 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
878 $user_cur = &$this->user[$table_cur->player[$i]];
879 $user_cur->subst = "shutdowner";
880 $user_cur->step_inc();
882 $ret = sprintf('stat = "%s"; subst = "%s";', $user_cur->stat, $user_cur->subst);
883 $ret .= "gst.st = ".($user_cur->step+1)."; ";
884 $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);
885 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
886 $user_cur->step_inc();
889 $this->room_join_wakeup(&$user_cur, TRUE, -2);
890 $table_cur->table_token = "";
893 Briskin5::unlock_data($sem);
895 } // if ($table_cur->player_n == PLAYERS_N) {
896 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
898 log_rd2("out new loop.");
900 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
901 $user_cur = &$this->user[$i];
903 log_rd2("User: ".$user_cur->name." stat: ".$user_cur->stat." subst: ".$user_cur->subst);
905 if ($user_cur->sess == "")
908 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
909 // Auto logout dell'utente
910 log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
912 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
913 log_auth($user_cur->sess, "Autologout session.");
916 /* factorized with ->reset()
917 $tmp_sess = $user_cur->sess;
918 $user_cur->sess = "";
919 step_unproxy($tmp_sess);
920 $user_cur->name = "";
921 while (array_pop($user_cur->comm) != NULL);
923 $user_cur->the_end = FALSE;
926 log_rd2("AUTO LOGOUT.");
927 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
928 $this->room_wakeup(&$user_cur);
929 else if ($user_cur->subst == 'standup')
930 $this->room_outstandup(&$user_cur);
932 log_rd2("LOGOUT FROM WHAT ???");
936 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
937 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
938 $this->room_wakeup(&$user_cur);
939 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
940 $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);
941 $user_cur->step_inc();
945 log_rd2("GARBAGE UPDATED!");
947 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
954 function show_room($user_step, &$user)
956 log_main("show_room: username: ".$user->name);
959 $ret = sprintf('gst.st = %d; ', $user_step);
960 if ($user->subst == 'standup')
961 $ret .= "tra.show(); ";
963 $ret .= "tra.hide(); ";
965 $ret .= sprintf('stat = "%s";', $user->stat);
967 $ret .= root_wellcome($user);
968 $ret .= sprintf('subst = "%s";', $user->subst);
969 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name));
970 for ($i = 0 ; $i < TABLES_N ; $i++) {
971 $ret .= $this->table_content($user, $i);
972 $act_content = table_act_content(($user->subst == 'standup'),
973 $this->table[$i]->player_n, $i, $user->table);
974 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
975 if ($this->table[$i]->wag_own != NULL)
976 $ret .= sprintf('tra.add(%d, "%s: %s"); ', $i, $this->table[$i]->wag_own->name, $this->table[$i]->wag_com);
978 $ret .= sprintf('tra.rem(%d); ', $i);
980 $ret .= $this->standup_content($user);
986 function room_wakeup(&$user)
988 $table_idx = $user->table;
989 $table = &$this->table[$table_idx];
991 log_main("WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
995 $from_table = ($user->stat == "table");
997 log_main("WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
999 for ($i = 0 ; $i < $table->player_n ; $i++) {
1000 $user_cur = &$this->user[$table->player[$i]];
1001 log_main("PREIMPOST: INLOOP name: ".$user_cur->name);
1003 if ($user_cur != $user) {
1004 $user_cur->stat_set("room");
1005 $user_cur->subst = "sitdown";
1006 $user_cur->laccwr = $curtime;
1008 else if ($user->sess != "") {
1009 $user_cur->stat_set("room");
1010 $user_cur->subst = "standup";
1011 $user_cur->laccwr = $curtime;
1012 $user_cur->table = -1;
1017 $user->stat_set("room");
1018 $user->subst = "standup";
1019 $user->laccwr = $curtime;
1022 $remove_wagon = false;
1023 if($table->wag_own == $user) {
1024 $remove_wagon = true;
1025 $table->wag_reset($curtime);
1030 /* aggiorna l'array dei giocatori al tavolo. */
1031 $table->user_rem(&$this, &$user);
1033 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1034 $user_cur = &$this->user[$i];
1035 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1038 log_main("VALORI: name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
1040 $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
1041 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
1042 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
1043 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
1044 log_main("DOCUMENT.index.php: from table");
1046 else if ($user_cur->stat == "room") {
1047 log_main("DOCUMENT.index.php: from table");
1049 $ret .= $this->table_content($user_cur, $table_idx);
1050 $ret .= $this->standup_content($user_cur);
1052 $act_content = table_act_content(FALSE, 0, $table_idx, $user->table);
1053 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1056 if ($user_cur == $user) {
1057 // set the new status
1058 $ret .= 'subst = "standup"; tra.show(); ';
1059 // clean the action buttons in other tables
1060 for ($e = 0 ; $e < TABLES_N ; $e++) {
1061 if ($this->table[$e]->player_n < PLAYERS_N)
1062 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
1066 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
1067 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1070 log_wr("ROOM_WAKEUP: ".$ret);
1071 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1072 $user_cur->step_inc();
1076 function room_join_wakeup(&$user, $update_lacc = FALSE, $trans_delta)
1078 $table_idx = $user->table;
1079 $table = &$this->table[$table_idx];
1081 log_main("JOIN_WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
1084 $user_wup = array();
1086 $user_tab = array();
1088 log_main("JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1090 for ($i = 0 ; $i < $table->player_n ; $i++) {
1091 $user_cur = &$this->user[$table->player[$i]];
1092 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1093 if ($user_cur->sess != "") {
1094 if ($update_lacc == TRUE) {
1095 $user_cur->laccwr = $curtime;
1097 log_main("cur: ".$user_cur->name." subst: ".$user_cur->subst);
1098 if ($user_cur->subst == "shutdowned") {
1099 $user_cur->stat_set("room");
1100 $user_cur->subst = "sitdown";
1102 else if ($user_cur->subst == "shutdowner") {
1103 $user_cur->stat_set("room");
1104 $user_cur->subst = "standup";
1105 $user_cur->table = -1;
1106 $user_wup[$user_wup_n++] = &$user_cur;
1108 $remove_wagon = false;
1109 if($table->wag_own == $user_cur) {
1110 $remove_wagon = true;
1111 $table->wag_reset($curtime);
1114 $user_tab[$user_tab_n++] = &$user_cur;
1118 for ($wup_idx = 0 ; $wup_idx < $user_wup_n ; $wup_idx++)
1119 $table->user_rem(&$this, &$user_wup[$wup_idx]);
1121 /* aggiorna l'array dei giocatori al tavolo. */
1123 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1124 log_main("START LOOP");
1125 $user_cur = &$this->user[$i];
1126 if ($user_cur->sess == '' || $user_cur->stat != 'room') {
1127 log_main("name: ".$user_cur->name."skip subst: ".$user_cur->subst);
1132 log_main("VALORI name: ".$user_cur->name." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
1134 $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
1135 if ($user_cur->stat == "room") {
1136 log_main("DOCUMENT.index.php from table");
1138 $ret .= $this->table_content($user_cur, $table_idx);
1139 $ret .= $this->standup_content($user_cur);
1141 $act_content = table_act_content(FALSE, 0, $table_idx, $user_cur->table);
1142 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1144 for ($tab_idx = 0 ; $tab_idx < $user_tab_n ; $tab_idx++)
1145 if ($user_cur == $user_tab[$tab_idx])
1148 // for users that wakeup the room will be reconstructed by index_rd.php
1149 if ($tab_idx < $user_tab_n) {
1150 log_main("PRE show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1152 // ARRAY_POP DISABLED
1153 // if ($trans_delta == 0)
1154 // while (array_pop($user_cur->comm) != NULL);
1156 $user_cur->trans_step = $user_cur->step + 1 + $trans_delta;
1157 $user_cur->comm[$user_cur->step % COMM_N] = "";
1158 $user_cur->step_inc();
1159 $user_cur->comm[$user_cur->step % COMM_N] = $this->show_room(($user_cur->step + 1), &$user_cur);
1160 $user_cur->step_inc();
1161 log_main("POST show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1165 log_main("JOIN_WAKEUP wup_idx ".$wup_idx." wup_n ".$user_wup_n);
1167 log_main("JOIN_WAKEUP more");
1168 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
1169 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1170 log_main("JOIN_WAKEUP end more");
1172 log_wr("ROOM_JOIN_WAKEUP: ".$ret);
1173 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1174 $user_cur->step_inc();
1178 function room_outstandup(&$user)
1180 $this->room_sitdown(&$user, -1);
1183 function table_update(&$user)
1185 log_main("table_update: pre - USER: ".$user->name);
1187 $table_idx = $user->table;
1189 if ($table_idx > -1)
1190 $table = &$this->table[$table_idx];
1192 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1194 $user_cur = &$this->user[$i];
1195 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1198 $ret = "gst.st = ".($user_cur->step+1)."; ";
1199 if ($table_idx > -1)
1200 $ret .= $this->table_content($user_cur, $table_idx);
1202 if ($user_cur == $user) {
1203 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
1205 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1206 $user_cur->step_inc();
1209 log_main("table_update: post");
1212 function room_sitdown(&$user, $table_idx)
1214 log_main("room_sitdown ".($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
1216 if ($table_idx > -1 && $table_idx < TABLES_N) {
1217 $table = &$this->table[$table_idx];
1221 if ($table->wag_own != NULL && $table->player_n == PLAYERS_N) {
1222 for ($i = 0 ; $i < TABLES_N ; $i++) {
1223 $user_cur =& $this->user[$table->player[$i]];
1224 if ($user_cur == $table->wag_own) {
1225 $train_app = sprintf("tra.rem(%d); ", $table_idx);
1226 $table->wag_reset(time());
1233 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1235 $user_cur = &$this->user[$i];
1236 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1239 $ret = "gst.st = ".($user_cur->step+1)."; ".$train_app;
1240 if ($table_idx > -1)
1241 $ret .= $this->table_content($user_cur, $table_idx);
1242 $ret .= $this->standup_content($user_cur);
1244 if ($user_cur == $user) {
1245 $ret .= 'subst = "sitdown"; tra.hide(); ';
1246 // clean the action buttons in other tables
1247 for ($e = 0 ; $e < TABLES_N ; $e++) {
1248 $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
1249 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
1252 else if ($table_idx > -1) {
1253 if ($table->player_n == PLAYERS_N) {
1254 $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
1255 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1258 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1259 $user_cur->step_inc();
1263 function chatt_send(&$user, $mesg)
1265 GLOBAL $G_alarm_passwd;
1269 $msg = substr($mesg,6);
1271 $dt = date("H:i ", $timecur);
1281 $update_room = FALSE;
1283 if (strcmp($msg, "/tav") == 0 ||
1284 strncmp($msg, "/tav ", 5) == 0) {
1286 if ($user->stat != 'room' || $user->subst != 'sitdown') {
1287 $msg = "<br>Per attivare il messaggio di segnalazione del tavolo occorre essere seduti.<br><br>";
1288 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1293 $table = &$this->table[$user->table];
1295 if ($table->wag_own != NULL) {
1296 $msg = "<br>Il messaggio di segnalazione del tavolo è già attivato.<br><br>";
1297 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1302 $dtime = $timecur - $table->wag_tout;
1303 if ($dtime < EXPIRE_TIME_WAG) {
1304 $msg = sprintf("<br>Il messaggio di segnalazione del tavolo<br>è disattivato ancora per %d second%s.<br><br>",
1305 EXPIRE_TIME_WAG - $dtime, (EXPIRE_TIME_WAG - $dtime == 1 ? "o" : "i"));
1306 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1311 $msg = substr($msg, 5);
1313 $table->wag_set($user, $msg);
1314 $to_room = sprintf('tra.add(%d, "%s");', $user->table, xcape(sprintf("%s: %s", $user->name, $msg)));
1317 } // /tav chat command
1319 else if (strncmp($msg, "/alarm ", 7) == 0) {
1320 $alarm_check = "/alarm ".$G_alarm_passwd." ";
1322 if (strncmp($msg, $alarm_check, strlen($alarm_check)) != 0) {
1323 $msg = "<br>La password digitata non è corretta.<br><br>";
1324 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1329 $to_user = sprintf('chatt_sub("%s","Alarm <b>%s</b> inviato a tutti.");',
1330 $dt.NICKSERV, xcape(substr($msg, strlen($alarm_check))));
1332 // $msg = "<br><b>".$dt.NICKSERV."<br><br>".xcape(substr($msg, strlen($alarm_check)))."</b><br><br>";
1333 $msg = sprintf("<br><b>%s%s<br><br>%s</b><br><br>",
1334 $dt, NICKSERV, xcape(substr($msg, strlen($alarm_check))));
1335 $to_all = show_notify($msg, 0, "chiudi", 400, 120);
1337 } // /alarm chat command
1339 else if (strncmp($msg, "/nick ", 6) == 0) {
1340 log_main("chatt_send BEGIN");
1343 if (($name_new = validate_name(substr($msg, 6))) == FALSE) {
1344 $to_user = sprintf('chatt_sub("%s","Il nickname deve contenere almeno una lettera o una cifra.");', $dt.NICKSERV, xcape($name_new));
1348 $msg = "COMMAND ".$msg;
1349 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1350 $user_cur = &$this->user[$i];
1352 if ($user_cur->sess == '')
1354 if ($user_cur->name == $name_new)
1357 if ($i < MAX_PLAYERS) {
1358 $to_user = sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
1362 $user->name = $name_new;
1364 log_main("chatt_send start set");
1366 $update_room = TRUE;
1368 } // nick chat command
1370 else { // normal chat line
1371 if ($timecur < ($user->chat_ban + $user->chat_dlt)) {
1373 $user->chat_dlt = $user->chat_dlt * 2;
1374 if ($user->chat_dlt > 120)
1375 $user->chat_dlt = 120;
1377 else if ($user->chat_lst == $msg)
1379 else if ($timecur - $user->chattime[($user->chat_cur + 1) % CHAT_N] < CHAT_ILL_TIME) {
1380 $user->chat_ban = $timecur;
1381 $user->chat_dlt = 5;
1385 $user->chat_ban = 0;
1386 $user->chat_dlt = 0;
1390 $to_user = sprintf('chatt_sub("%s","%s");', $dt.xcape($user->name), xcape("== chat ban =="));
1393 $to_user = sprintf('chatt_sub("%s","%s");', $dt.xcape($user->name), xcape($msg));
1394 $to_room = $to_user;
1397 log_legal($timecur, $user->sess, $user->name,
1398 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$msg);
1400 $user->chat_lst = "$msg";
1401 $user->chattime[$user->chat_cur % CHAT_N] = $timecur;
1411 // Output to clients
1414 if ($to_user != FALSE) {
1415 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1416 $user->comm[$user->step % COMM_N] .= $to_user;
1420 if ($to_room != FALSE) {
1421 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1422 $user_cur = &$this->user[$i];
1423 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1424 if ($user_cur->sess == '' || $user_cur->stat == 'table' || $user_cur == $user)
1427 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1428 $user_cur->comm[$user_cur->step % COMM_N] .= $to_room;
1429 $user_cur->step_inc();
1434 // Before all align times with table timeout
1435 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1436 $table_cur =& $this->table[$table_idx];
1437 // if the table is complete and exists its shared mem we get the info about users lacc
1439 if ($table_cur->player_n == PLAYERS_N) {
1440 log_main("PLAYERS == N TABLE ".$table_idx);
1442 if (($sem = Briskin5::lock_data($table_idx)) != FALSE) {
1443 log_main("bin5 lock data success");
1445 $no_recovery = FALSE;
1446 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
1447 if ($table_cur->table_token != $bri->table_token) {
1448 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
1453 if ($bri != FALSE) {
1454 $bri_table = &$bri->table[0];
1455 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
1456 // stat must be "table" by definition
1457 $bri_user =& $bri->user[$i];
1459 log_main("writa: ".$user_mesg);
1460 $bri_user->comm[$bri_user->step % COMM_N] = "gst.st = ".($bri_user->step+1)."; ";
1461 $bri_user->comm[$bri_user->step % COMM_N] .= $to_tabl;
1462 $bri_user->step_inc();
1464 Briskin5::save_data(&$bri);
1466 Briskin5::unlock_data($sem);
1468 } // if ($table_cur->player_n == PLAYERS_N) {
1469 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1470 } // if ($to_tabl == true ...
1473 if ($user->stat == 'room' && $user->subst == 'standup') {
1474 $this->standup_update(&$user);
1476 else if ($user->stat == 'room' && $user->subst == 'sitdown') {
1477 log_main("chatt_send pre table update");
1478 $this->table_update(&$user);
1479 log_main("chatt_send post table update");
1481 } // if ($update_room ...
1484 } // function chatt_send( ...
1486 function &get_user($sess, &$idx)
1488 GLOBAL $PHP_SELF, $G_false;
1490 if (validate_sess($sess)) {
1491 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1492 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1495 $ret = &$this->user[$i];
1499 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1500 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
1501 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1504 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1511 * function &add_user(&$room, &$sess, &$idx, $name, $ip)
1514 * if ($idx != -1 && ret == FALSE) => duplicated nick
1515 * if ($idx == -2 && ret == FALSE) => invalid name
1516 * if ($idx == -1 && ret == FALSE) => no space left
1517 * if (ret == TRUE) => SUCCESS
1519 function &add_user(&$sess, &$idx, $name, $ip)
1526 if (($name_new = validate_name($name)) == FALSE) {
1531 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
1532 if (validate_sess($sess) == FALSE)
1535 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1537 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1541 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
1544 if (strcmp($this->user[$i]->name, $name_new) == 0) {
1552 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1554 if ($idx != -1 && $i == MAX_PLAYERS) {
1558 $this->user[$idx]->sess = uniqid("");
1559 $sess = $this->user[$idx]->sess;
1563 $this->user[$idx]->sess = $sess;
1565 $this->user[$idx]->name = $name_new;
1566 $this->user[$idx]->stat_set("room");
1567 $this->user[$idx]->step_set(0);
1568 while (array_pop($this->user[$idx]->comm) != NULL);
1569 $this->user[$idx]->subst = "standup";
1570 $this->user[$idx]->lacc = $curtime;
1571 $this->user[$idx]->laccwr = $curtime;
1572 $this->user[$idx]->bantime = 0;
1573 $this->user[$idx]->ip = $ip;
1574 log_main(sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1576 return ($this->user[$idx]);
1582 function standup_update(&$user)
1584 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1585 $user_cur = &$this->user[$i];
1586 if ($user_cur->sess == '')
1589 log_main("STANDUP START: ".$user_cur->stat);
1591 if ($user_cur->stat == 'room') {
1592 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".$this->standup_content($user_cur);
1593 if ($user_cur == $user)
1594 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
1596 log_main("FROM STANDUP: NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
1598 $user_cur->step_inc();
1604 function &init_data()
1606 $room =& new Room();
1612 function &load_data()
1614 GLOBAL $G_false, $sess;
1617 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1618 log_main("ftok failed");
1623 if (($shm_sz = sharedmem_sz($tok)) == -1) {
1624 log_main("shmop_open failed");
1628 $shm_sz = SHM_DIMS_MIN;
1630 if ($shm = shm_attach($tok, $shm_sz)) {
1631 $room = @shm_get_var($shm, $tok);
1633 log_only("bri == ".($room == FALSE ? "FALSE" : "TRUE")." bri === ".($room === FALSE ? "FALSE" : "TRUE")." bri isset ".(isset($room) ? "TRUE" : "FALSE"));
1635 log_only("bri count ".count($room));
1637 if ($room == FALSE) {
1638 log_only("INIT MAIN DATA");
1640 $room =& Room::init_data();
1641 if (@shm_put_var($shm, $tok, $room) == FALSE) {
1642 log_only("PUT_VAR FALLITA ".strlen(serialize($room)));
1643 log_only(serialize($room));
1646 $room->shm_sz = $shm_sz;
1661 function save_data(&$room)
1670 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1)
1673 while ($room->shm_sz < SHM_DIMS_MAX) {
1674 if (($shm = shm_attach($tok, $room->shm_sz)) == FALSE)
1677 // log_only("PUT_VAR DI ".strlen(serialize($room)));
1678 if (shm_put_var($shm, $tok, $room) != FALSE) {
1682 if (shm_remove($shm) === FALSE) {
1683 log_only("REMOVE FALLITA");
1687 $room->shm_sz += SHM_DIMS_DLT;
1696 function lock_data()
1700 // echo "LOCK: ".FTOK_PATH."/main";
1702 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1706 // echo "FTOK ".$tok."<br>";
1707 if (($res = sem_get($tok)) == FALSE) {
1708 echo "SEM_GET FAILED";
1711 if (sem_acquire($res)) {
1712 log_lock("LOCK room");
1719 function unlock_data($res)
1723 log_lock("UNLOCK room");
1725 return (sem_release($res));
1728 function standup_content($user)
1733 if ($user->stat != 'room')
1736 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
1737 if ($this->user[$e]->sess == "" || $this->user[$e]->stat != "room" || $this->user[$e]->name == "")
1742 $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
1744 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
1745 if ($this->user[$e]->sess == "" || $this->user[$e]->stat != "room" || $this->user[$e]->name == "")
1749 if ($this->user[$e]->subst == "standup") {
1750 if (($ct % 4) == 0) {
1753 if ($this->user[$e] == $user)
1754 { $hilion = "<b>"; $hilioff = "</b>"; }
1756 { $hilion = ""; $hilioff = ""; }
1758 $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($this->user[$e]->name), $hilioff);
1759 if (($ct % 4) == 3) {
1760 $content .= '</tr>';
1765 $content .= '</table>';
1767 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
1768 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
1769 $content, $content2);
1774 function table_content($user, $table_idx)
1780 // Si possono usare i dati nella classe table
1783 $sess = $user->sess;
1784 $table = &$this->table[$table_idx];
1786 if ($user->stat != 'room')
1789 for ($i = 0 ; $i < $table->player_n ; $i++) {
1790 $user_cur = &$this->user[$table->player[$i]];
1792 if ($user_cur == $user)
1793 { $hilion = "<b>"; $hilioff = "</b>"; }
1795 { $hilion = ""; $hilioff = ""; }
1797 log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
1799 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
1802 for ( ; $i < PLAYERS_N ; $i++)
1806 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
1814 function make_seed()
1816 list($usec, $sec) = explode(' ', microtime());
1817 return (float) $sec + ((float) $usec * 100000);
1821 function log_only2($log) {
1824 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONL2) == 0)
1827 if (isset($sess) == FALSE)
1832 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONL2) == 0)
1835 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1836 fwrite($fp, sprintf("ONL2: [%s] [%s]\n", $ssess, $log));
1841 function log_crit($log) {
1844 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_CRIT) == 0)
1847 if (isset($sess) == FALSE)
1852 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_CRIT) == 0)
1855 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1856 fwrite($fp, sprintf("CRIT: [%s] [%s]\n", $ssess, $log));
1861 function log_only($log) {
1864 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONLY) == 0)
1867 if (isset($sess) == FALSE)
1872 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONLY) == 0)
1875 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1876 fwrite($fp, sprintf("ONLY: [%s] [%s]\n", $ssess, $log));
1881 function log_main($log) {
1884 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_MAIN) == 0)
1887 if (isset($sess) == FALSE)
1892 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_MAIN) == 0)
1895 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1896 fwrite($fp, sprintf("MAIN: [%s] [%s]\n", $ssess, $log));
1901 function log_rd($log) {
1904 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_READ) == 0)
1907 if (isset($sess) == FALSE)
1912 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_READ) == 0)
1915 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1916 fwrite($fp, sprintf("READ: [%s] [%s]\n", $ssess, $log));
1921 function log_rd2($log) {
1924 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_REA2) == 0)
1927 if (isset($sess) == FALSE)
1932 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_REA2) == 0)
1935 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1936 fwrite($fp, sprintf("REA2: [%s] [%s]\n", $ssess, $log));
1941 function log_send($log) {
1944 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SEND) == 0)
1947 if (isset($sess) == FALSE)
1952 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SEND) == 0)
1955 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1956 fwrite($fp, sprintf("SEND: [%s] [%s]\n", $ssess, $log));
1961 function log_lock($log) {
1964 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOCK) == 0)
1967 if (isset($sess) == FALSE)
1972 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOCK) == 0)
1975 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1976 fwrite($fp, sprintf("LOCK: [%s] [%s]\n", $ssess, $log));
1981 function log_wr($log) {
1984 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_WRIT) == 0)
1987 if (isset($sess) == FALSE)
1992 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_WRIT) == 0)
1995 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1996 fwrite($fp, sprintf("WRIT: [%s] [%s]\n", $ssess, $log));
2001 function log_load($log) {
2004 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOAD) == 0)
2007 if (isset($sess) == FALSE)
2012 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOAD) == 0)
2015 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2016 fwrite($fp, sprintf("LOAD: [%s] [%s]\n", $ssess, $log));
2021 function log_auth($sess, $log) {
2022 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_AUTH) == 0)
2025 if (( (BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_AUTH) == 0)
2028 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2029 fwrite($fp, sprintf("AUTH: [%s] [%d] [%s]\n", $sess, time(), $log));
2035 function log_legal($timecur, $sess, $name, $where, $mesg)
2039 if (($fp = @fopen(LEGAL_PATH."/legal.log", 'a')) != FALSE) {
2040 /* Unix time | session | nickname | IP | where was | mesg */
2041 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|\n", $timecur, $sess, $name, $_SERVER['REMOTE_ADDR'], $where , $mesg));
2049 function lock_banlist()
2051 if (($tok = @ftok(FTOK_PATH."/main", "L")) == -1) {
2055 if (($res = sem_get($tok)) == FALSE) {
2056 echo "SEM_GET FAILED";
2059 if (sem_acquire($res))
2065 function unlock_banlist($res)
2067 return (sem_release($res));
2070 function table_act_content($isstanding, $sitted, $table, $cur_table)
2075 if ($sitted < PLAYERS_N) {
2076 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
2080 if ($table == $cur_table)
2081 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
2089 function show_notify($text, $tout, $butt, $w, $h)
2091 log_main("SHOW_NOTIFY: ".$text);
2092 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
2095 function briscola_show($room, $table, $user)
2100 if ($table->asta_card == 9)
2101 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
2103 /* text of caller cell */
2104 if ($user->table_pos == $table->asta_win)
2105 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
2107 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
2108 xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
2110 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
2112 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
2113 $ret .= sprintf('$("caller").style.visibility = "visible";');
2114 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
2115 $ret .= sprintf('$("astalascio").style.visibility = "";');
2116 $ret .= sprintf('$("asta").style.visibility = "hidden";');
2117 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
2123 function game_result($asta_pnt, $pnt)
2125 if ($asta_pnt == 61) {
2128 else if ($pnt == 60)
2134 if ($pnt >= $asta_pnt)
2141 function multoval($mult)
2145 else if ($mult == 4)
2146 return ("quadruplo");
2148 return (sprintf("%d-plo", $mult));
2151 function show_table_info(&$room, &$table, $table_pos)
2154 $user = &$room->user[$table->player[$table_pos]];
2156 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
2157 $noty = sprintf('<table class=\"points\"><tr><th></th>');
2160 for ($i = 0 ; $i < PLAYERS_N ; $i++)
2161 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
2162 $noty .= sprintf("</tr>");
2165 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
2167 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
2168 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
2169 for ($e = 0 ; $e < PLAYERS_N ; $e++)
2170 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
2175 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
2176 for ($e = 0 ; $e < PLAYERS_N ; $e++)
2177 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
2178 $noty .= "</tr></table>";
2180 if ($table->old_reason != "") {
2181 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
2184 if ($table->old_win != -1) {
2185 $win = $table->player[$table->old_win];
2186 $fri = $table->player[$table->old_friend];
2188 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
2191 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
2192 xcape($room->user[$win]->name),
2193 xcape($room->user[$fri]->name));
2194 if ($table->old_pnt == 120) {
2195 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
2198 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
2199 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
2200 'più di 60'), $table->old_pnt,
2201 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
2205 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
2206 xcape($room->user[$win]->name));
2207 if ($table->old_pnt == 120) {
2208 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
2211 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
2212 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
2213 'più di 60'), $table->old_pnt,
2214 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
2218 if ($table->mazzo == $table_pos)
2219 $noty .= "Fai <b>tu</b> il mazzo,";
2221 $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
2222 $noty .= "Il mazzo a <b>$unam</b>,";
2225 if ($user->subst == 'asta') {
2226 if ($table->asta_win == -1) // auction case
2227 $curplayer = $table->gstart % PLAYERS_N;
2229 $curplayer = $table->asta_win;
2231 else if ($user->subst == 'game') {
2232 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
2236 if ($curplayer == $table_pos) {
2237 $noty .= " tocca a <b>te</b> giocare.";
2240 $unam = xcape($room->user[$table->player[$curplayer]]->name);
2241 $noty .= " tocca a <b>$unam</b> giocare.";
2244 if ($table->mult > 1) {
2245 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
2247 $noty .= "<hr><br>";
2249 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
2254 function root_wellcome($user)
2256 GLOBAL $root_wellarr;
2260 $dt = date("H:i ", $timecur);
2262 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
2263 $ret .= sprintf('chatt_sub("%s","%s");', $dt.NICKSERV, str_replace('"', '\"', $root_wellarr[$i]));
2268 function table_wellcome($user)
2270 GLOBAL $table_wellarr;
2273 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
2274 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
2282 is_transition (is from room to table ?)
2283 is_again (is another game)
2285 Examples of $is_transition, $is_again:
2286 from reload of the page: FALSE, FALSE
2287 from sitdown in room: TRUE, FALSE
2288 from table: asta cmd e tutti passano: TRUE, TRUE
2289 from table: fine partita: TRUE, TRUE
2291 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
2293 $table_idx = $user->table;
2294 $table = &$room->table[$table_idx];
2295 $table_pos = $user->table_pos;
2297 $ret = "table_init();";
2298 $ret .= $table->exitlock_show(&$room->user, $table_pos);
2300 /* GENERAL STATUS */
2301 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
2302 $sendstep, $user->stat, $user->subst, $table_pos);
2304 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
2305 $sendstep, $user->stat, $user->subst, $table_pos));
2308 $ret .= "background_set();";
2311 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name));
2312 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
2313 xcape($room->user[$table->player[($table_pos)%PLAYERS_N]]->name),
2314 xcape($room->user[$table->player[($table_pos+1)%PLAYERS_N]]->name),
2315 xcape($room->user[$table->player[($table_pos+2)%PLAYERS_N]]->name),
2316 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+3)%PLAYERS_N]]->name)),
2317 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+4)%PLAYERS_N]]->name)));
2319 /* NOTIFY FOR THE CARD MAKER */
2320 if ($is_transition) { // && $user->subst == "asta" superfluo
2321 $ret .= show_table_info(&$room, &$table, $table_pos);
2324 $ret .= table_wellcome($user);
2326 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
2327 $ret .= playsound("cow.mp3");
2332 if ($is_transition) { // && $user->subst == "asta" superfluo
2335 for ($i = 0 ; $i < 8 ; $i++) {
2336 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
2338 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
2339 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
2345 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
2347 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
2348 $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
2349 $table->card[$o]->value : -1),
2350 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
2355 $taked = array(0,0,0,0,0);
2356 $inhand = array(0,0,0,0,0);
2357 $ontabl = array(-1,-1,-1,-1,-1);
2360 for ($i = 0 ; $i < 40 ; $i++) {
2361 if ($table->card[$i]->stat == 'hand') {
2362 if ($table->card[$i]->owner == $table_pos) {
2363 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
2365 $inhand[$table->card[$i]->owner]++;
2367 else if ($table->card[$i]->stat == 'take') {
2368 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
2369 $taked[$table->card[$i]->owner]++;
2371 else if ($table->card[$i]->stat == 'table') {
2372 $ontabl[$table->card[$i]->owner] = $i;
2376 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2377 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
2379 log_main("Stat table: ".$logg);
2381 /* Set ours cards. */
2383 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
2384 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
2385 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
2386 $oursarg .= ($i == 0 ? "" : ", ")."-1";
2387 $ret .= sprintf('card_setours(%s);', $oursarg);
2389 /* Dispose all cards */
2390 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2391 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
2392 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
2393 $inhand[$i], $taked[$i]);
2395 if ($ontabl[$i] != -1) {
2396 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
2397 $table->card[$ontabl[$i]]->value,
2398 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
2404 if ($user->subst == 'asta') {
2406 /* show users auction status */
2408 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2409 $user_cur = &$room->user[$table->player[$i]];
2410 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
2411 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
2414 $showst .= ",-2,-2";
2415 $ret .= sprintf('show_astat(%s);', $showst);
2417 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
2418 /* show card chooser */
2419 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
2424 if ($table_pos == ($table->gstart % PLAYERS_N) &&
2425 $table->asta_win == -1)
2426 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2427 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
2429 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2430 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
2434 if ($table->asta_win == -1) { // auction case
2435 if ($table_pos == ($table->gstart % PLAYERS_N))
2436 $ret .= "remark_on();";
2438 $ret .= "remark_off();";
2440 else { // chooseed case
2441 if ($table_pos == $table->asta_win)
2442 $ret .= "remark_on();";
2444 $ret .= "remark_off();";
2447 else if ($user->subst == 'game') {
2449 if (($table->gstart + $table->turn) % PLAYERS_N == $table_pos)
2450 $ret .= "is_my_time = true; remark_on();";
2452 $ret .= "remark_off();";
2454 /* WHO CALL AND WATH */
2455 $ret .= briscola_show($room, $table, $user);
2459 } // end function show_table(...
2461 function calculate_winner(&$table)
2468 $cur_seed = $table->briscola - ($table->briscola % 10);
2470 for ($i = 0 ; $i < 40 ; $i++) {
2471 if ($table->card[$i]->stat != "table")
2474 log_wr(sprintf("Card On table: [%d]", $i));
2476 $v = $table->card[$i]->value;
2477 $ontab[$table->card[$i]->owner] = $v;
2478 $ontid[$table->card[$i]->owner] = $i;
2479 /* se briscola setto il flag */
2480 if (($v - ($v % 10)) == $cur_seed)
2484 if ($briontab == FALSE) {
2485 $cur_win = $table->gstart;
2486 $cur_val = $ontab[$cur_win];
2487 $cur_seed = $cur_val - ($cur_val % 10);
2490 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2491 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
2492 if ($ontab[$i] < $cur_val) {
2493 $cur_val = $ontab[$i];
2499 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2500 $table->card[$ontid[$i]]->owner = $cur_win;
2501 $table->card[$ontid[$i]]->stat = "take"; // Card stat
2506 function calculate_points(&$table)
2508 GLOBAL $G_all_points;
2512 if ($table->asta_pnt == 60)
2513 $table->asta_pnt = 61;
2515 $table->old_reason = "";
2516 $table->old_win = $table->asta_win;
2517 $table->old_friend = $table->friend;
2518 $table->old_asta_pnt = $table->asta_pnt;
2520 for ($i = 0 ; $i < 40 ; $i++) {
2521 $ctt = $table->card[$i]->value % 10;
2522 $own = $table->card[$i]->owner;
2523 if ($own == $table->asta_win || $own == $table->friend)
2524 $pro += $G_all_points[$ctt];
2527 log_wr(sprintf("PRO: [%d]", $pro));
2530 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
2531 $table->points[$table->points_n % MAX_POINTS] = array();
2532 for ($i = 0 ; $i < PLAYERS_N ; $i++)
2533 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
2535 $table->old_pnt = $pro;
2541 if ($pro >= $table->asta_pnt)
2546 $table->points[$table->points_n % MAX_POINTS] = array();
2547 for ($i = 0 ; $i < 5 ; $i++) {
2548 if ($i == $table->asta_win)
2549 $pt = ($i == $table->friend ? 4 : 2);
2550 else if ($i == $table->friend)
2555 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
2557 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
2559 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
2561 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
2562 $table->total[$i] += $pt;
2565 $table->old_pnt = $pro;
2569 function validate_sess($sess)
2571 if (strlen($sess) == SESS_LEN)
2577 function validate_name($name)
2579 $name_new = str_replace(' ', '_', substr(trim($name),0,12));
2581 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
2583 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
2590 function playsound($filename)
2592 return (sprintf('playsound("flasou", "%s");', $filename));
2595 function secstoword($secs)
2597 $mins = floor($secs / 60);
2600 $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
2603 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));
2608 function sharedmem_sz($tok)
2610 if (($shm_id = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
2611 log_main("shmop_open failed");
2614 $shm_sz = shmop_size($shm_id);
2615 shmop_close($shm_id);
2617 log_main("shm_sz: ".$shm_sz." SHM_DIMS: ".SHM_DIMS);