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);
77 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
78 $G_brisk_version = "2.0.0";
80 $root_wellarr = Array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: Nuova versione stabile.',
81 'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
82 $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.');
86 <div style=\\"text-align: left; padding: 8px;\\">
87 <b>Descrizione</b><br>
88 Questa รจ un\'implementazione della briscola in cinque, così come è spiegata su
89 <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>
90 <b>Configurazione del browser.</b><br>
91 Occorre abilitare i cookies.<br>
93 <b>Uso del sito</b><br>
94 Potete sedervi a un tavolo o rimanere in piedi.<br>
95 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
98 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>
99 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
100 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
101 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
102 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
103 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.
105 <dt><b>Comandi della chat</b>
106 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
107 <dd><b>/tav <i><frase di invito></i></b> - invito per gli altri giocatori al tavolo dove si è seduti
113 <div id=\\"header\\" class=\\"header\\">
114 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
115 briscola chiamata in salsa ajax
117 <br><b>version '.$G_brisk_version.'</b><br><br>
118 Copyright 2006-2008 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
123 $from = array ( '\\', '@', '|' );
124 $to = array ( '\\\\', '@', '¦' );
126 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
131 var $value; /* 0 - 39 card value */
132 var $stat; /* 'bunch', 'hand', 'table', 'take' */
133 var $owner; /* (table position 0-4) */
134 // var $pos; /* Pos in hand. */
135 var $x; /* When played the X position on the table of the owner. */
136 var $y; /* When played the Y position on the table of the owner. */
138 function Card($value, $stat, $owner)
140 $this->value = $value;
141 $this->stat = $stat; // Card stat
142 $this->owner = $owner;
145 function assign($stat,$owner)
147 $this->stat = $stat; // Card stat
148 $this->owner = $owner;
151 function setpos($pos)
158 $this->stat = 'table'; // Card stat
163 function take($newown)
165 $this->stat = 'take'; // Card stat
166 $this->owner = $newown;
188 var $points; // points array
189 var $points_n; // number of row of points
215 if (($thiz =& new Table()) == FALSE)
218 $thiz->player = array();
221 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
222 $thiz->asta_pla_n= -1;
223 $thiz->asta_card = -1;
224 $thiz->asta_pnt = -1;
226 $thiz->points = array( );
228 $thiz->total = array( 0, 0, 0, 0, 0);
229 $thiz->asta_win = -1;
230 $thiz->briscola = -1;
234 $thiz->wag_own = NULL;
238 $thiz->old_reason = "";
239 $thiz->old_asta_pnt = -1;
242 $thiz->old_friend = -1;
244 $thiz->table_token = "";
245 $thiz->table_start = 0;
247 $thiz->wakeup_time = 0;
252 function &clone(&$from)
256 if (($thiz =& new Table()) == FALSE)
259 $thiz->player = array();
260 for ($i = 0 ; $i < $from->player_n ; $i++)
261 $thiz->player[$i] = $from->player[$i];
262 $thiz->player_n = $from->player_n;
263 $thiz->card = $from->card;
264 $thiz->mazzo = $from->mazzo; // REVIEW
265 $thiz->gstart = $from->gstart;
266 $thiz->turn = $from->turn;
268 $thiz->wag_own = $from->wag_own;
269 $thiz->wag_com = $from->wag_com;
270 $thiz->wag_tout = $from->wag_taut;
272 $thiz->asta_pla = $from->asta_pla;
273 $thiz->asta_pla_n = $from->asta_pla_n;
274 $thiz->asta_card = $from->asta_card;
275 $thiz->asta_pnt = $from->asta_pnt;
277 $thiz->mult = $from->mult;
278 $thiz->points = $from->points;
279 $thiz->points_n = $from->points_n;
280 $thiz->total = $from->total;
282 $thiz->asta_win = $from->asta_win;
283 $thiz->briscola = $from->briscola;
284 $thiz->friend = $from->friend;
286 $thiz->old_reason = $from->old_reason;
287 $thiz->old_asta_pnt = $from->old_asta_pnt;
288 $thiz->old_pnt = $from->old_pnt;
289 $thiz->old_win = $from->old_win;
290 $thiz->old_friend = $from->old_friend;
292 $thiz->table_token = $from->table_token;
293 $thiz->table_start = $from->table_start;
295 $thiz->wakeup_time = $from->wakeup_time;
300 function &spawn(&$from)
304 if (($thiz =& new Table()) == FALSE)
307 $thiz->player_n = $from->player_n;
308 $thiz->card = &$thiz->bunch_create();
309 $thiz->mazzo = $from->mazzo;
310 $thiz->gstart = $from->gstart;
311 $thiz->turn = $from->turn;
313 $thiz->wag_own = $from->wag_own;
314 $thiz->wag_com = $from->wag_com;
315 $thiz->wag_tout = $from->wag_taut;
317 $thiz->asta_pla = $from->asta_pla;
318 $thiz->asta_pla_n = $from->asta_pla_n;
319 $thiz->asta_card = $from->asta_card;
320 $thiz->asta_pnt = $from->asta_pnt;
322 $thiz->mult = $from->mult;
323 $thiz->points = $from->points;
324 $thiz->points_n = $from->points_n;
325 $thiz->total = $from->total;
327 $thiz->asta_win = $from->asta_win;
328 $thiz->briscola = $from->briscola;
329 $thiz->friend = $from->friend;
331 $thiz->old_reason = $from->old_reason;
332 $thiz->old_asta_pnt = $from->old_asta_pnt;
333 $thiz->old_pnt = $from->old_pnt;
334 $thiz->old_win = $from->old_win;
335 $thiz->old_friend = $from->old_friend;
337 // players are rearranged in an dedicated array
338 $thiz->player = array();
339 for ($i = 0 ; $i < $from->player_n ; $i++)
340 $thiz->player[$i] = $i;
342 $thiz->table_token = $from->table_token;
343 $thiz->table_start = $from->table_start;
345 $thiz->wakeup_time = $from->wakeup_time;
350 function &bunch_create()
354 for ($i = 0 ; $i < 40 ; $i++) {
355 $ret[$i] =& new Card($i, 'bunch', 'no_owner');
362 function wag_set(&$user, $mesg)
366 $this->wag_own = &$user;
367 $this->wag_com = $mesg;
371 function wag_reset($timeout)
373 log_main("WAG_RESET");
375 unset($this->wag_own);
376 $this->wag_own = NULL;
378 $this->wag_tout = $timeout;
381 function bunch_make()
383 $ct = array(0,0,0,0,0);
385 mt_srand(make_seed());
387 for ($i = 39 ; $i >= 0 ; $i--)
390 for ($i = 39 ; $i >= 0 ; $i--) {
394 log_main("RND ZERO");
399 $this->card[$id]->assign('hand', $owner);
401 $rest[$rn] = $rest[$i];
402 // $pubbpos[$rn2] = $pubbpos[$i];
406 function init(&$userarr)
408 $this->mazzo = rand(0,PLAYERS_N-1);
412 $this->old_reason = "";
413 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
414 $this->total[$i] = 0;
415 $user_cur = &$userarr[$this->player[$i]];
416 $user_cur->exitislock = TRUE;
419 log_main("table::init: ci siamo");
422 function game_init(&$userarr)
426 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
430 $this->asta_pla_n = PLAYERS_N;
431 $this->asta_card = -1;
432 $this->asta_pnt = 60;
433 $this->asta_win = -1;
434 $this->briscola = -1;
438 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
439 $this->asta_pla[$i] = TRUE;
440 $user_cur = &$userarr[$this->player[$i]];
441 $user_cur->subst = 'asta';
442 $user_cur->asta_card = -2;
443 $user_cur->asta_pnt = -1;
444 $user_cur->handpt = $this->hand_points($i);
445 // SEE function calculate_points(&$table)
451 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
454 function getPlayer($idx)
456 return ($this->player[$idx]);
459 function setPlayer($idx, $player)
461 $this->player[$idx] = $player;
464 function user_add($idx)
466 $this->player[$this->player_n] = $idx;
469 return ($this->player_n - 1);
472 function user_rem(&$room, &$user)
474 $tabpos = $user->table_pos;
476 /* verifico la consistenza dei dati */
477 if ($room->user[$this->player[$tabpos]] == $user) {
479 /* aggiorna l'array dei giocatori al tavolo. */
480 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
481 $this->player[$i] = $this->player[$i+1];
482 $user_cur = &$room->user[$this->player[$i]];
483 $user_cur->table_pos = $i;
488 log_main("INCONSISTENCY ON TABLE.");
492 function hand_points($idx)
494 GLOBAL $G_all_points;
498 for ($i = 0 ; $i < 40 ; $i++) {
499 if ($this->card[$i]->owner != $idx)
502 $ctt = $this->card[$i]->value % 10;
503 $tot += $G_all_points[$ctt];
509 function exitlock_show(&$userarr, $table_pos)
511 $ct = $this->exitlock_calc(&$userarr, $table_pos);
513 $ret = sprintf('exitlock_show(%d, %s);', $ct,
514 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
518 function exitlock_calc(&$userarr, $table_pos)
522 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
523 if ($userarr[$this->player[$i]]->exitislock == FALSE)
532 var $name; // name of the user
533 var $sess; // session of the user
534 var $ip; // ip of the user
535 var $lacc; // last access (for the cleanup)
536 var $laccwr; // last access (for the cleanup)
537 var $bantime; // timeout to temporary ban
538 var $stat; // status (outdoor, room, table, game, ...)
539 var $subst; // substatus for each status
540 var $step; // step of the current status
541 var $trans_step; // step to enable transition between pages (disable == -1)
542 var $comm; // commands array
545 var $handpt; // Total card points at the beginning of the current hand.
546 var $exitislock; // Player can exit from the table ?
547 var $table; // id of the current table (if in table state)
548 var $table_pos; // idx on the table
549 var $table_token;// token that identify a game on a table
550 var $the_end; // Flag to change the end of the session
552 var $chat_lst; // Last chat line
553 var $chattime; // Array of chat times
554 var $chat_cur; // Current chat line number
555 var $chat_ban; // Time for ban chat
556 var $chat_dlt; // Delta t for ban
560 function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
563 if (($thiz =& new User()) == FALSE)
569 $thiz->lacc = time();
570 $thiz->laccwr = time();
573 $thiz->subst = $subst;
575 $thiz->trans_step = -1;
576 $thiz->comm = array();
577 $thiz->asta_card = -2;
578 $thiz->asta_pnt = -1;
580 $thiz->exitislock = TRUE;
582 $thiz->chattime = array_fill(0, CHAT_N, 0);
584 $thiz->chat_lst = "";
588 $thiz->table = $table;
589 $thiz->table_pos = -1;
590 $thiz->table_token = "";
595 function &clone(&$from)
599 if (($thiz =& new User()) == FALSE)
602 $thiz->name = $from->name;
603 $thiz->sess = $from->sess;
604 $thiz->ip = $from->ip;
605 $thiz->lacc = $from->lacc;
606 $thiz->laccwr = $from->laccwr;
607 $thiz->bantime = $from->bantime;
608 $thiz->stat = $from->stat;
609 $thiz->subst = $from->subst;
610 $thiz->step = $from->step;
611 $thiz->trans_step = $from->trans_step;
612 $thiz->comm = array();
614 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
615 for ($i = $i_start ; $i < $from->step ; $i++) {
617 $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;
624 $thiz->chattime = array();
625 for ($i = 0 ; $i < CHAT_N ; $i++)
626 $thiz->chattime[$i] = $from->chattime[$i];
627 $thiz->chat_cur = $from->chat_cur;
628 $thiz->chat_lst = $from->chat_lst;
629 $thiz->chat_ban = $from->chat_ban;
630 $thiz->chat_dlt = $from->chat_dlt;
632 $thiz->table = $from->table;
633 $thiz->table_pos = $from->table_pos;
634 $thiz->table_token = $from->table_token;
635 $thiz->the_end = $from->the_end;
640 function &spawn(&$from, $table, $table_pos)
644 if (($thiz =& new User()) == FALSE)
647 $thiz->name = $from->name;
648 $thiz->sess = $from->sess;
649 $thiz->ip = $from->ip;
650 $thiz->lacc = $from->lacc;
651 $thiz->laccwr = $from->laccwr;
652 $thiz->bantime = $from->bantime;
653 $thiz->stat = $from->stat;
654 $thiz->subst = $from->subst;
655 $thiz->step = $from->step;
656 $thiz->trans_step = $from->trans_step;
657 $thiz->comm = array();
660 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
661 for ($i = $i_start ; $i < $from->step ; $i++) {
662 log_wr("TRY PUSH:".$i);
664 $thiz->comm[$ii] = $from->comm[$ii];
667 $thiz->asta_card = $from->asta_card;
668 $thiz->asta_pnt = $from->asta_pnt;
669 $thiz->handpt = $from->handpt;
670 $thiz->exitislock = $from->exitislock;
671 $thiz->the_end = $from->the_end;
673 $thiz->chattime = array_fill(0, CHAT_N, 0);
675 $thiz->chat_lst = "";
679 $thiz->table = $table;
680 $thiz->table_pos = $table_pos;
681 $thiz->table_token = $from->table_token;
686 function stat_set($stat) {
687 log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]");
688 $this->stat = "$stat";
691 if (validate_sess($this->sess)) {
692 if (file_exists(PROXY_PATH) == FALSE)
694 $fp = fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
695 fwrite($fp, sprintf("%s\n",$this->stat));
701 function step_set($step) {
705 if (validate_sess($this->sess) == FALSE)
707 if (file_exists(PROXY_PATH) == FALSE)
709 if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
711 fwrite($fp, pack("l",$this->step), 4);
720 function step_inc($delta = 1) {
721 $this->step += $delta;
723 if (validate_sess($this->sess)) {
724 if (file_exists(PROXY_PATH) == FALSE)
726 $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
727 fwrite($fp, pack("l",$this->step), 4);
738 log_legal($curtime, $this->sess, $this->name, "STAT:LOGOUT", '');
740 $tmp_sess = $this->sess;
742 step_unproxy($tmp_sess);
743 $this->name = ""; // OK here
744 while (array_pop($this->comm) != NULL);
746 $this->chattime = array_fill(0, CHAT_N, 0);
748 $this->chat_lst = "";
751 $this->the_end = FALSE;
756 function step_get($sess) {
759 if (validate_sess($sess) == FALSE)
762 if (file_exists(PROXY_PATH) == FALSE)
764 if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
766 if (($s = fread($fp, 4)) == FALSE)
770 $arr = unpack('l', $s);
773 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
780 log_rd2("STEP_GET: return false ");
785 function step_unproxy($sess) {
786 log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
787 if (file_exists(PROXY_PATH) == FALSE)
789 @unlink(PROXY_PATH."/".$sess.".step");
796 var $comm; // commands for many people
797 var $step; // current step of the comm array
798 var $garbage_timeout;
802 $this->user = array();
803 $this->table = array();
805 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
806 $this->user[$i] =& User::create("", "");
809 for ($i = 0 ; $i < TABLES_N ; $i++)
810 $this->table[$i] =& Table::create();
811 $this->garbage_timeout = 0;
814 function garbage_manager($force)
818 log_rd2("garbage_manager START");
820 /* Garbage collector degli utenti in timeout */
822 if ($force || $this->garbage_timeout < $curtime) {
824 // Before all align times with table timeout
825 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
826 $table_cur =& $this->table[$table_idx];
827 // if the table is complete and exists its shared mem we get the info about users lacc
829 if ($table_cur->player_n == PLAYERS_N) {
830 log_main("PLAYERS == N TABLE ".$table_idx);
832 if (($sem = Briskin5::lock_data($table_idx)) != FALSE) {
833 log_main("bin5 lock data success");
835 $no_recovery = FALSE;
836 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
837 if ($table_cur->table_token != $bri->table_token) {
838 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
839 log_main("ERROR: not matching table_start. Room: ".$table_cur->table_start." Table: ".$bri->table_start);
849 log_main("garbage_manager: bri loaded successfully.");
850 $bri->garbage_manager(TRUE);
852 $bri_table = &$bri->table[0];
854 // is the end of the table
855 if ($bri->the_end == TRUE) {
857 * DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN
859 log_main("garbage_manager: INSIDE THE END.");
861 $plist = "$table_cur->table_token|$user_cur->table|$table_cur->player_n";
862 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
863 $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
866 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
867 // stat must be "table" by definition
868 $user_cur =& $this->user[$table_cur->player[$i]];
869 $bri_user =& $bri->user[$i];
871 $user_cur->subst = $bri_user->subst;
872 $user_cur->step = $bri_user->step;
873 $user_cur->lacc = $bri_user->lacc;
874 $user_cur->laccwr = $bri_user->lacc;
875 $user_cur->bantime = $bri_user->bantime;
878 log_legal($curtime, $user_cur->sess, $user_cur->name, "STAT:DESTROY_GAME", $plist);
880 $this->room_join_wakeup(&$user_cur, FALSE, 0);
881 $table_cur->table_token = "";
882 $table_cur->wakeup_time = $curtime + WAKEUP_TIME;
883 Briskin5::destroy_data($table_idx);
886 log_main("gm:: save_data");
888 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
889 $this->user[$table_cur->player[$i]]->lacc = $bri->user[$i]->lacc;
892 Briskin5::save_data(&$bri);
894 } // else if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
895 else if ($no_recovery == FALSE) {
896 log_crit("ERROR: table ".$table_idx." unrecoverable join");
898 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
899 $user_cur = &$this->user[$table_cur->player[$i]];
900 $user_cur->subst = "shutdowner";
901 $user_cur->step_inc();
903 $ret = sprintf('stat = "%s"; subst = "%s";', $user_cur->stat, $user_cur->subst);
904 $ret .= "gst.st = ".($user_cur->step+1)."; ";
905 $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);
906 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
907 $user_cur->step_inc();
910 $plist = "$table_cur->table_token|$user_cur->table|$table_cur->player_n";
911 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
912 $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
914 log_legal($curtime, $user_cur->sess, $user_cur->name, "STAT:DESTROY_GAME(RECOVERY)", $plist);
916 $this->room_join_wakeup(&$user_cur, TRUE, -2);
917 $table_cur->table_token = "";
920 Briskin5::unlock_data($sem);
922 } // if ($table_cur->player_n == PLAYERS_N) {
923 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
925 log_rd2("out new loop.");
927 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
928 $user_cur = &$this->user[$i];
930 log_rd2("User: ".$user_cur->name." stat: ".$user_cur->stat." subst: ".$user_cur->subst);
932 if ($user_cur->sess == "")
935 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
936 // Auto logout dell'utente
937 log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
939 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
940 log_auth($user_cur->sess, "Autologout session.");
944 log_rd2("AUTO LOGOUT.");
945 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
946 $this->room_wakeup(&$user_cur);
947 else if ($user_cur->subst == 'standup')
948 $this->room_outstandup(&$user_cur);
950 log_rd2("LOGOUT FROM WHAT ???");
954 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
955 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
956 $this->room_wakeup(&$user_cur);
957 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
958 $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);
959 $user_cur->step_inc();
963 log_rd2("GARBAGE UPDATED!");
965 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
972 function show_room($user_step, &$user)
974 log_main("show_room: username: ".$user->name);
977 $ret = sprintf('gst.st = %d; ', $user_step);
978 if ($user->subst == 'standup')
979 $ret .= "tra.show(); ";
981 $ret .= "tra.hide(); ";
983 $ret .= sprintf('stat = "%s";', $user->stat);
985 $ret .= root_wellcome($user);
986 $ret .= sprintf('subst = "%s";', $user->subst);
987 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name));
988 for ($i = 0 ; $i < TABLES_N ; $i++) {
989 $ret .= $this->table_content($user, $i);
990 $act_content = table_act_content(($user->subst == 'standup'),
991 $this->table[$i]->player_n, $i, $user->table);
992 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
993 if ($this->table[$i]->wag_own != NULL)
994 $ret .= sprintf('tra.add(%d, "%s: %s"); ', $i, $this->table[$i]->wag_own->name, $this->table[$i]->wag_com);
996 $ret .= sprintf('tra.rem(%d); ', $i);
998 $ret .= $this->standup_content($user);
1004 function room_wakeup(&$user)
1006 $table_idx = $user->table;
1007 $table = &$this->table[$table_idx];
1009 log_main("WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
1013 $from_table = ($user->stat == "table");
1015 log_main("WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1017 for ($i = 0 ; $i < $table->player_n ; $i++) {
1018 $user_cur = &$this->user[$table->player[$i]];
1019 log_main("PREIMPOST: INLOOP name: ".$user_cur->name);
1021 if ($user_cur != $user) {
1022 $user_cur->stat_set("room");
1023 $user_cur->subst = "sitdown";
1024 $user_cur->laccwr = $curtime;
1026 else if ($user->sess != "") {
1027 $user_cur->stat_set("room");
1028 $user_cur->subst = "standup";
1029 $user_cur->laccwr = $curtime;
1030 $user_cur->table = -1;
1035 $user->stat_set("room");
1036 $user->subst = "standup";
1037 $user->laccwr = $curtime;
1040 $remove_wagon = false;
1041 if($table->wag_own == $user) {
1042 $remove_wagon = true;
1043 $table->wag_reset($curtime);
1048 /* aggiorna l'array dei giocatori al tavolo. */
1049 $table->user_rem(&$this, &$user);
1051 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1052 $user_cur = &$this->user[$i];
1053 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1056 log_main("VALORI: name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
1058 $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
1059 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
1060 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
1061 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
1062 log_main("DOCUMENT.index.php: from table");
1064 else if ($user_cur->stat == "room") {
1065 log_main("DOCUMENT.index.php: from table");
1067 $ret .= $this->table_content($user_cur, $table_idx);
1068 $ret .= $this->standup_content($user_cur);
1070 $act_content = table_act_content(FALSE, 0, $table_idx, $user->table);
1071 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1074 if ($user_cur == $user) {
1075 // set the new status
1076 $ret .= 'subst = "standup"; tra.show(); ';
1077 // clean the action buttons in other tables
1078 for ($e = 0 ; $e < TABLES_N ; $e++) {
1079 if ($this->table[$e]->player_n < PLAYERS_N)
1080 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
1084 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
1085 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1088 log_wr("ROOM_WAKEUP: ".$ret);
1089 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1090 $user_cur->step_inc();
1094 function room_join_wakeup(&$user, $update_lacc = FALSE, $trans_delta)
1096 $table_idx = $user->table;
1097 $table = &$this->table[$table_idx];
1099 log_main("JOIN_WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
1102 $user_wup = array();
1104 $user_tab = array();
1106 log_main("JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1108 for ($i = 0 ; $i < $table->player_n ; $i++) {
1109 $user_cur = &$this->user[$table->player[$i]];
1110 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1111 if ($user_cur->sess != "") {
1112 if ($update_lacc == TRUE) {
1113 $user_cur->laccwr = $curtime;
1115 log_main("cur: ".$user_cur->name." subst: ".$user_cur->subst);
1116 if ($user_cur->subst == "shutdowned") {
1117 $user_cur->stat_set("room");
1118 $user_cur->subst = "sitdown";
1120 else if ($user_cur->subst == "shutdowner") {
1121 $user_cur->stat_set("room");
1122 $user_cur->subst = "standup";
1123 $user_cur->table = -1;
1124 $user_wup[$user_wup_n++] = &$user_cur;
1126 $remove_wagon = false;
1127 if($table->wag_own == $user_cur) {
1128 $remove_wagon = true;
1129 $table->wag_reset($curtime);
1132 $user_tab[$user_tab_n++] = &$user_cur;
1136 for ($wup_idx = 0 ; $wup_idx < $user_wup_n ; $wup_idx++)
1137 $table->user_rem(&$this, &$user_wup[$wup_idx]);
1139 /* aggiorna l'array dei giocatori al tavolo. */
1141 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1142 log_main("START LOOP");
1143 $user_cur = &$this->user[$i];
1144 if ($user_cur->sess == '' || $user_cur->stat != 'room') {
1145 log_main("name: ".$user_cur->name."skip subst: ".$user_cur->subst);
1150 log_main("VALORI name: ".$user_cur->name." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
1152 $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
1153 if ($user_cur->stat == "room") {
1154 log_main("DOCUMENT.index.php from table");
1156 $ret .= $this->table_content($user_cur, $table_idx);
1157 $ret .= $this->standup_content($user_cur);
1159 $act_content = table_act_content(FALSE, 0, $table_idx, $user_cur->table);
1160 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1162 for ($tab_idx = 0 ; $tab_idx < $user_tab_n ; $tab_idx++)
1163 if ($user_cur == $user_tab[$tab_idx])
1166 // for users that wakeup the room will be reconstructed by index_rd.php
1167 if ($tab_idx < $user_tab_n) {
1168 log_main("PRE show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1170 // ARRAY_POP DISABLED
1171 // if ($trans_delta == 0)
1172 // while (array_pop($user_cur->comm) != NULL);
1174 $user_cur->trans_step = $user_cur->step + 1 + $trans_delta;
1175 $user_cur->comm[$user_cur->step % COMM_N] = "";
1176 $user_cur->step_inc();
1177 $user_cur->comm[$user_cur->step % COMM_N] = $this->show_room(($user_cur->step + 1), &$user_cur);
1178 $user_cur->step_inc();
1179 log_main("POST show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1183 log_main("JOIN_WAKEUP wup_idx ".$wup_idx." wup_n ".$user_wup_n);
1185 log_main("JOIN_WAKEUP more");
1186 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
1187 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1188 log_main("JOIN_WAKEUP end more");
1190 log_wr("ROOM_JOIN_WAKEUP: ".$ret);
1191 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1192 $user_cur->step_inc();
1196 function room_outstandup(&$user)
1198 $this->room_sitdown(&$user, -1);
1201 function table_update(&$user)
1203 log_main("table_update: pre - USER: ".$user->name);
1205 $table_idx = $user->table;
1207 if ($table_idx > -1)
1208 $table = &$this->table[$table_idx];
1210 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1212 $user_cur = &$this->user[$i];
1213 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1216 $ret = "gst.st = ".($user_cur->step+1)."; ";
1217 if ($table_idx > -1)
1218 $ret .= $this->table_content($user_cur, $table_idx);
1220 if ($user_cur == $user) {
1221 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
1223 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1224 $user_cur->step_inc();
1227 log_main("table_update: post");
1230 function room_sitdown(&$user, $table_idx)
1232 log_main("room_sitdown ".($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
1236 if ($table_idx > -1 && $table_idx < TABLES_N) {
1237 $table = &$this->table[$table_idx];
1240 if ($table->wag_own != NULL && $table->player_n == PLAYERS_N) {
1241 for ($i = 0 ; $i < TABLES_N ; $i++) {
1242 $user_cur =& $this->user[$table->player[$i]];
1243 if ($user_cur == $table->wag_own) {
1244 $train_app = sprintf("tra.rem(%d); ", $table_idx);
1245 $table->wag_reset(time());
1252 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1254 $user_cur = &$this->user[$i];
1255 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1258 $ret = "gst.st = ".($user_cur->step+1)."; ".$train_app;
1259 if ($table_idx > -1)
1260 $ret .= $this->table_content($user_cur, $table_idx);
1261 $ret .= $this->standup_content($user_cur);
1263 if ($user_cur == $user) {
1264 $ret .= 'subst = "sitdown"; tra.hide(); ';
1265 // clean the action buttons in other tables
1266 for ($e = 0 ; $e < TABLES_N ; $e++) {
1267 $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
1268 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
1271 else if ($table_idx > -1) {
1272 if ($table->player_n == PLAYERS_N) {
1273 $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
1274 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1277 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1278 $user_cur->step_inc();
1282 function chatt_send(&$user, $mesg)
1284 GLOBAL $G_alarm_passwd;
1288 $msg = substr($mesg, 6, 128);
1290 $dt = date("H:i ", $curtime);
1300 $update_room = FALSE;
1302 if (strcmp($msg, "/tav") == 0 ||
1303 strncmp($msg, "/tav ", 5) == 0) {
1305 if ($user->stat != 'room' || $user->subst != 'sitdown') {
1306 $msg = "<br>Per attivare il messaggio di segnalazione del tavolo occorre essere seduti.<br><br>";
1307 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1312 $table = &$this->table[$user->table];
1314 if ($table->wag_own != NULL) {
1315 $msg = "<br>Il messaggio di segnalazione del tavolo è già attivato.<br><br>";
1316 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1321 $dtime = $curtime - $table->wag_tout;
1322 if ($dtime < EXPIRE_TIME_WAG) {
1323 $msg = sprintf("<br>Il messaggio di segnalazione del tavolo<br>è disattivato ancora per %d second%s.<br><br>",
1324 EXPIRE_TIME_WAG - $dtime, (EXPIRE_TIME_WAG - $dtime == 1 ? "o" : "i"));
1325 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1330 $msg = substr($msg, 5);
1332 $table->wag_set($user, $msg);
1333 $to_user = sprintf('tra.add(%d, "%s");', $user->table, xcape(sprintf("%s: %s", $user->name, $msg)));
1334 $to_room = $to_user;
1337 } // /tav chat command
1339 else if (strncmp($msg, "/alarm ", 7) == 0) {
1340 $alarm_check = "/alarm ".$G_alarm_passwd." ";
1342 if (strncmp($msg, $alarm_check, strlen($alarm_check)) != 0) {
1343 $msg = "<br>La password digitata non è corretta.<br><br>";
1344 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1349 $to_user = sprintf('chatt_sub("%s","Alarm <b>%s</b> inviato a tutti.");',
1350 $dt.NICKSERV, xcape(substr($msg, strlen($alarm_check))));
1352 $msg = sprintf("<br><b>%s<br><br>%s</b><br><br>",
1353 $dt.NICKSERV, xcape(substr($msg, strlen($alarm_check))));
1354 $to_all = show_notify($msg, 0, "chiudi", 400, 120);
1356 } // /alarm chat command
1358 else if (strncmp($msg, "/nick ", 6) == 0) {
1359 log_main("chatt_send BEGIN");
1362 if (($name_new = validate_name(substr($msg, 6))) == FALSE) {
1363 $to_user = sprintf('chatt_sub("%s","Il nickname deve contenere almeno una lettera dell\'alfabeto o una cifra.");', $dt.NICKSERV);
1367 $msg = "COMMAND ".$msg;
1368 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1369 $user_cur = &$this->user[$i];
1371 if ($user_cur->sess == '')
1373 if ($user_cur->name == $name_new)
1376 if ($i < MAX_PLAYERS) {
1377 $to_user = sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
1381 $user->name = $name_new; // OK - nick changed
1383 log_main("chatt_send start set");
1385 $update_room = TRUE;
1387 } // nick chat command
1389 else { // normal chat line
1390 if ($curtime < ($user->chat_ban + $user->chat_dlt)) {
1392 $user->chat_dlt = $user->chat_dlt * 2;
1393 if ($user->chat_dlt > 120)
1394 $user->chat_dlt = 120;
1396 else if ($user->chat_lst == $msg)
1398 else if ($curtime - $user->chattime[($user->chat_cur + 1) % CHAT_N] < CHAT_ILL_TIME) {
1399 $user->chat_ban = $curtime;
1400 $user->chat_dlt = 5;
1404 $user->chat_ban = 0;
1405 $user->chat_dlt = 0;
1409 $to_user = sprintf('chatt_sub("%s","%s");', $dt.xcape($user->name), xcape("== chat ban =="));
1412 $to_user = sprintf('chatt_sub("%s","%s");', $dt.xcape($user->name), xcape($msg));
1413 // temporary silentiation for troll (will became array check)
1414 if (strcasecmp($user->name,'JackRokka') != 0 && $user->sess != '47ea653f602e8')
1415 $to_room = $to_user;
1418 log_legal($curtime, $user->sess, $user->name,
1419 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$msg);
1421 $user->chat_lst = "$msg";
1422 $user->chattime[$user->chat_cur % CHAT_N] = $curtime;
1432 // Output to clients
1435 if ($to_user != FALSE) {
1436 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1437 $user->comm[$user->step % COMM_N] .= $to_user;
1441 if ($to_room != FALSE) {
1442 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1443 $user_cur = &$this->user[$i];
1444 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1445 if ($user_cur->sess == '' || $user_cur->stat == 'table' || $user_cur == $user)
1448 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1449 $user_cur->comm[$user_cur->step % COMM_N] .= $to_room;
1450 $user_cur->step_inc();
1455 // Before all align times with table timeout
1456 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1457 $table_cur =& $this->table[$table_idx];
1458 // if the table is complete and exists its shared mem we get the info about users lacc
1460 if ($table_cur->player_n == PLAYERS_N) {
1461 log_main("PLAYERS == N TABLE ".$table_idx);
1463 if (($sem = Briskin5::lock_data($table_idx)) != FALSE) {
1464 log_main("bin5 lock data success");
1466 $no_recovery = FALSE;
1467 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
1468 if ($table_cur->table_token != $bri->table_token) {
1469 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
1474 if ($bri != FALSE) {
1475 $bri_table = &$bri->table[0];
1476 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
1477 // stat must be "table" by definition
1478 $bri_user =& $bri->user[$i];
1480 log_main("writa: ".$user_mesg);
1481 $bri_user->comm[$bri_user->step % COMM_N] = "gst.st = ".($bri_user->step+1)."; ";
1482 $bri_user->comm[$bri_user->step % COMM_N] .= $to_tabl;
1483 $bri_user->step_inc();
1485 Briskin5::save_data(&$bri);
1487 Briskin5::unlock_data($sem);
1489 } // if ($table_cur->player_n == PLAYERS_N) {
1490 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1491 } // if ($to_tabl == true ...
1494 if ($user->stat == 'room' && $user->subst == 'standup') {
1495 $this->standup_update(&$user);
1497 else if ($user->stat == 'room' && $user->subst == 'sitdown') {
1498 log_main("chatt_send pre table update");
1499 $this->table_update(&$user);
1500 log_main("chatt_send post table update");
1502 } // if ($update_room ...
1505 } // function chatt_send( ...
1507 function &get_user($sess, &$idx)
1509 GLOBAL $PHP_SELF, $G_false;
1511 if (validate_sess($sess)) {
1512 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1513 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1516 $ret = &$this->user[$i];
1520 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1521 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
1522 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1525 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1532 * function &add_user(&$room, &$sess, &$idx, $name, $ip)
1535 * if ($idx != -1 && ret == FALSE) => duplicated nick
1536 * if ($idx == -2 && ret == FALSE) => invalid name
1537 * if ($idx == -1 && ret == FALSE) => no space left
1538 * if (ret == TRUE) => SUCCESS
1540 function &add_user(&$sess, &$idx, $name, $ip)
1547 if (($name_new = validate_name($name)) == FALSE) {
1552 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
1553 if (validate_sess($sess) == FALSE)
1556 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1558 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1562 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
1565 if (strcmp($this->user[$i]->name, $name_new) == 0) {
1573 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1575 if ($idx != -1 && $i == MAX_PLAYERS) {
1579 $this->user[$idx]->sess = uniqid("");
1580 $sess = $this->user[$idx]->sess;
1584 $this->user[$idx]->sess = $sess;
1586 $this->user[$idx]->name = $name_new; // OK - add new user
1587 $this->user[$idx]->stat_set("room");
1588 $this->user[$idx]->step_set(0);
1589 while (array_pop($this->user[$idx]->comm) != NULL);
1590 $this->user[$idx]->subst = "standup";
1591 $this->user[$idx]->lacc = $curtime;
1592 $this->user[$idx]->laccwr = $curtime;
1593 $this->user[$idx]->bantime = 0;
1594 $this->user[$idx]->ip = $ip;
1595 log_main(sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1597 return ($this->user[$idx]);
1603 function standup_update(&$user)
1605 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1606 $user_cur = &$this->user[$i];
1607 if ($user_cur->sess == '')
1610 log_main("STANDUP START: ".$user_cur->stat);
1612 if ($user_cur->stat == 'room') {
1613 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".$this->standup_content($user_cur);
1614 if ($user_cur == $user)
1615 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
1617 log_main("FROM STANDUP: NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
1619 $user_cur->step_inc();
1625 function &init_data()
1627 $room =& new Room();
1633 function &load_data()
1635 GLOBAL $G_false, $sess;
1638 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1639 log_main("ftok failed");
1644 if (($shm_sz = sharedmem_sz($tok)) == -1) {
1645 log_main("shmop_open failed");
1649 $shm_sz = SHM_DIMS_MIN;
1651 if ($shm = shm_attach($tok, $shm_sz)) {
1652 $room = @shm_get_var($shm, $tok);
1654 log_only("bri == ".($room == FALSE ? "FALSE" : "TRUE")." bri === ".($room === FALSE ? "FALSE" : "TRUE")." bri isset ".(isset($room) ? "TRUE" : "FALSE"));
1656 log_only("bri count ".count($room));
1658 if ($room == FALSE) {
1659 log_only("INIT MAIN DATA");
1661 $room =& Room::init_data();
1662 if (@shm_put_var($shm, $tok, $room) == FALSE) {
1663 log_only("PUT_VAR FALLITA ".strlen(serialize($room)));
1664 log_only(serialize($room));
1667 $room->shm_sz = $shm_sz;
1682 function save_data(&$room)
1691 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1)
1694 while ($room->shm_sz < SHM_DIMS_MAX) {
1695 if (($shm = shm_attach($tok, $room->shm_sz)) == FALSE)
1698 // log_only("PUT_VAR DI ".strlen(serialize($room)));
1699 if (shm_put_var($shm, $tok, $room) != FALSE) {
1703 if (shm_remove($shm) === FALSE) {
1704 log_only("REMOVE FALLITA");
1708 $room->shm_sz += SHM_DIMS_DLT;
1717 function lock_data()
1721 // echo "LOCK: ".FTOK_PATH."/main";
1723 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1727 // echo "FTOK ".$tok."<br>";
1728 if (($res = sem_get($tok)) == FALSE) {
1729 echo "SEM_GET FAILED";
1732 if (sem_acquire($res)) {
1733 log_lock("LOCK room");
1740 function unlock_data($res)
1744 log_lock("UNLOCK room");
1746 return (sem_release($res));
1749 function standup_content($user)
1754 if ($user->stat != 'room')
1757 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
1758 if ($this->user[$e]->sess == "" || $this->user[$e]->stat != "room" || $this->user[$e]->name == "")
1763 $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
1765 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
1766 if ($this->user[$e]->sess == "" || $this->user[$e]->stat != "room" || $this->user[$e]->name == "")
1770 if ($this->user[$e]->subst == "standup") {
1771 if (($ct % 4) == 0) {
1774 if ($this->user[$e] == $user)
1775 { $hilion = "<b>"; $hilioff = "</b>"; }
1777 { $hilion = ""; $hilioff = ""; }
1779 $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($this->user[$e]->name), $hilioff);
1780 if (($ct % 4) == 3) {
1781 $content .= '</tr>';
1786 $content .= '</table>';
1788 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
1789 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
1790 $content, $content2);
1795 function table_content($user, $table_idx)
1801 // Si possono usare i dati nella classe table
1804 $sess = $user->sess;
1805 $table = &$this->table[$table_idx];
1807 if ($user->stat != 'room')
1810 for ($i = 0 ; $i < $table->player_n ; $i++) {
1811 $user_cur = &$this->user[$table->player[$i]];
1813 if ($user_cur == $user)
1814 { $hilion = "<b>"; $hilioff = "</b>"; }
1816 { $hilion = ""; $hilioff = ""; }
1818 log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
1820 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
1823 for ( ; $i < PLAYERS_N ; $i++)
1827 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
1835 function make_seed()
1837 list($usec, $sec) = explode(' ', microtime());
1838 return (float) $sec + ((float) $usec * 100000);
1842 function log_only2($log) {
1845 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONL2) == 0)
1848 if (isset($sess) == FALSE)
1853 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONL2) == 0)
1856 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1857 fwrite($fp, sprintf("ONL2: [%s] [%s]\n", $ssess, $log));
1862 function log_crit($log) {
1865 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_CRIT) == 0)
1868 if (isset($sess) == FALSE)
1873 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_CRIT) == 0)
1876 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1877 fwrite($fp, sprintf("CRIT: [%s] [%s]\n", $ssess, $log));
1882 function log_only($log) {
1885 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONLY) == 0)
1888 if (isset($sess) == FALSE)
1893 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONLY) == 0)
1896 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1897 fwrite($fp, sprintf("ONLY: [%s] [%s]\n", $ssess, $log));
1902 function log_main($log) {
1905 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_MAIN) == 0)
1908 if (isset($sess) == FALSE)
1913 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_MAIN) == 0)
1916 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1917 fwrite($fp, sprintf("MAIN: [%s] [%s]\n", $ssess, $log));
1922 function log_rd($log) {
1925 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_READ) == 0)
1928 if (isset($sess) == FALSE)
1933 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_READ) == 0)
1936 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1937 fwrite($fp, sprintf("READ: [%s] [%s]\n", $ssess, $log));
1942 function log_rd2($log) {
1945 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_REA2) == 0)
1948 if (isset($sess) == FALSE)
1953 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_REA2) == 0)
1956 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1957 fwrite($fp, sprintf("REA2: [%s] [%s]\n", $ssess, $log));
1962 function log_send($log) {
1965 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SEND) == 0)
1968 if (isset($sess) == FALSE)
1973 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SEND) == 0)
1976 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1977 fwrite($fp, sprintf("SEND: [%s] [%s]\n", $ssess, $log));
1982 function log_lock($log) {
1985 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOCK) == 0)
1988 if (isset($sess) == FALSE)
1993 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOCK) == 0)
1996 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1997 fwrite($fp, sprintf("LOCK: [%s] [%s]\n", $ssess, $log));
2002 function log_wr($log) {
2005 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_WRIT) == 0)
2008 if (isset($sess) == FALSE)
2013 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_WRIT) == 0)
2016 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2017 fwrite($fp, sprintf("WRIT: [%s] [%s]\n", $ssess, $log));
2022 function log_load($log) {
2025 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOAD) == 0)
2028 if (isset($sess) == FALSE)
2033 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOAD) == 0)
2036 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2037 fwrite($fp, sprintf("LOAD: [%s] [%s]\n", $ssess, $log));
2042 function log_auth($sess, $log) {
2043 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_AUTH) == 0)
2046 if (( (BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_AUTH) == 0)
2049 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2050 fwrite($fp, sprintf("AUTH: [%s] [%d] [%s]\n", $sess, time(), $log));
2056 function log_legal($curtime, $sess, $name, $where, $mesg)
2060 if (($fp = @fopen(LEGAL_PATH."/legal.log", 'a')) != FALSE) {
2061 /* Unix time | session | nickname | IP | where was | mesg */
2062 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|\n", $curtime, $sess, $name, $_SERVER['REMOTE_ADDR'], $where , $mesg));
2070 function lock_banlist()
2072 if (($tok = @ftok(FTOK_PATH."/main", "L")) == -1) {
2076 if (($res = sem_get($tok)) == FALSE) {
2077 echo "SEM_GET FAILED";
2080 if (sem_acquire($res))
2086 function unlock_banlist($res)
2088 return (sem_release($res));
2091 function table_act_content($isstanding, $sitted, $table, $cur_table)
2096 if ($sitted < PLAYERS_N) {
2097 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
2101 if ($table == $cur_table)
2102 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
2110 function show_notify($text, $tout, $butt, $w, $h)
2112 log_main("SHOW_NOTIFY: ".$text);
2113 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
2116 function briscola_show($room, $table, $user)
2121 if ($table->asta_card == 9)
2122 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
2124 /* text of caller cell */
2125 if ($user->table_pos == $table->asta_win)
2126 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
2128 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
2129 xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
2131 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
2133 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
2134 $ret .= sprintf('$("caller").style.visibility = "visible";');
2135 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
2136 $ret .= sprintf('$("astalascio").style.visibility = "";');
2137 $ret .= sprintf('$("asta").style.visibility = "hidden";');
2138 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
2144 function game_result($asta_pnt, $pnt)
2146 if ($asta_pnt == 61) {
2149 else if ($pnt == 60)
2155 if ($pnt >= $asta_pnt)
2162 function multoval($mult)
2166 else if ($mult == 4)
2167 return ("quadruplo");
2169 return (sprintf("%d-plo", $mult));
2172 function show_table_info(&$room, &$table, $table_pos)
2175 $user = &$room->user[$table->player[$table_pos]];
2177 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
2178 $noty = sprintf('<table class=\"points\"><tr><th></th>');
2181 for ($i = 0 ; $i < PLAYERS_N ; $i++)
2182 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
2183 $noty .= sprintf("</tr>");
2186 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
2188 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
2189 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
2190 for ($e = 0 ; $e < PLAYERS_N ; $e++)
2191 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
2196 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
2197 for ($e = 0 ; $e < PLAYERS_N ; $e++)
2198 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
2199 $noty .= "</tr></table>";
2201 if ($table->old_reason != "") {
2202 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
2205 if ($table->old_win != -1) {
2206 $win = $table->player[$table->old_win];
2207 $fri = $table->player[$table->old_friend];
2209 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
2212 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
2213 xcape($room->user[$win]->name),
2214 xcape($room->user[$fri]->name));
2215 if ($table->old_pnt == 120) {
2216 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
2219 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
2220 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
2221 'più di 60'), $table->old_pnt,
2222 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
2226 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
2227 xcape($room->user[$win]->name));
2228 if ($table->old_pnt == 120) {
2229 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
2232 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
2233 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
2234 'più di 60'), $table->old_pnt,
2235 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
2239 if ($table->mazzo == $table_pos)
2240 $noty .= "Fai <b>tu</b> il mazzo,";
2242 $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
2243 $noty .= "Il mazzo a <b>$unam</b>,";
2246 if ($user->subst == 'asta') {
2247 if ($table->asta_win == -1) // auction case
2248 $curplayer = $table->gstart % PLAYERS_N;
2250 $curplayer = $table->asta_win;
2252 else if ($user->subst == 'game') {
2253 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
2257 if ($curplayer == $table_pos) {
2258 $noty .= " tocca a <b>te</b> giocare.";
2261 $unam = xcape($room->user[$table->player[$curplayer]]->name);
2262 $noty .= " tocca a <b>$unam</b> giocare.";
2265 if ($table->mult > 1) {
2266 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
2268 $noty .= "<hr><br>";
2269 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
2270 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
2271 * 130 + ($table->points_n > 0 ? 50 : 0) +
2272 * (120 * ($table->points_n / MAX_POINTS)));
2273 * will be used when we refact notify js function following
2274 * photoo class logic
2280 function root_wellcome($user)
2282 GLOBAL $root_wellarr;
2286 $dt = date("H:i ", $curtime);
2288 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
2289 $ret .= sprintf('chatt_sub("%s","%s");', $dt.NICKSERV, str_replace('"', '\"', $root_wellarr[$i]));
2294 function table_wellcome($user)
2296 GLOBAL $table_wellarr;
2299 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
2300 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
2308 is_transition (is from room to table ?)
2309 is_again (is another game)
2311 Examples of $is_transition, $is_again:
2312 from reload of the page: FALSE, FALSE
2313 from sitdown in room: TRUE, FALSE
2314 from table: asta cmd e tutti passano: TRUE, TRUE
2315 from table: fine partita: TRUE, TRUE
2317 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
2319 $table_idx = $user->table;
2320 $table = &$room->table[$table_idx];
2321 $table_pos = $user->table_pos;
2323 $ret = "table_init();";
2324 $ret .= $table->exitlock_show(&$room->user, $table_pos);
2326 /* GENERAL STATUS */
2327 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
2328 $sendstep, $user->stat, $user->subst, $table_pos);
2330 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
2331 $sendstep, $user->stat, $user->subst, $table_pos));
2334 $ret .= "background_set();";
2337 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name));
2338 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
2339 xcape($room->user[$table->player[($table_pos)%PLAYERS_N]]->name),
2340 xcape($room->user[$table->player[($table_pos+1)%PLAYERS_N]]->name),
2341 xcape($room->user[$table->player[($table_pos+2)%PLAYERS_N]]->name),
2342 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+3)%PLAYERS_N]]->name)),
2343 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+4)%PLAYERS_N]]->name)));
2345 /* NOTIFY FOR THE CARD MAKER */
2346 if ($is_transition) { // && $user->subst == "asta" superfluo
2347 $ret .= show_table_info(&$room, &$table, $table_pos);
2350 $ret .= table_wellcome($user);
2352 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
2353 $ret .= playsound("cow.mp3");
2358 if ($is_transition) { // && $user->subst == "asta" superfluo
2361 for ($i = 0 ; $i < 8 ; $i++) {
2362 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
2364 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
2365 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
2371 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
2373 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
2374 $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
2375 $table->card[$o]->value : -1),
2376 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
2381 $taked = array(0,0,0,0,0);
2382 $inhand = array(0,0,0,0,0);
2383 $ontabl = array(-1,-1,-1,-1,-1);
2386 for ($i = 0 ; $i < 40 ; $i++) {
2387 if ($table->card[$i]->stat == 'hand') {
2388 if ($table->card[$i]->owner == $table_pos) {
2389 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
2391 $inhand[$table->card[$i]->owner]++;
2393 else if ($table->card[$i]->stat == 'take') {
2394 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
2395 $taked[$table->card[$i]->owner]++;
2397 else if ($table->card[$i]->stat == 'table') {
2398 $ontabl[$table->card[$i]->owner] = $i;
2402 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2403 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
2405 log_main("Stat table: ".$logg);
2407 /* Set ours cards. */
2409 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
2410 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
2411 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
2412 $oursarg .= ($i == 0 ? "" : ", ")."-1";
2413 $ret .= sprintf('card_setours(%s);', $oursarg);
2415 /* Dispose all cards */
2416 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2417 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
2418 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
2419 $inhand[$i], $taked[$i]);
2421 if ($ontabl[$i] != -1) {
2422 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
2423 $table->card[$ontabl[$i]]->value,
2424 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
2430 if ($user->subst == 'asta') {
2432 /* show users auction status */
2434 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2435 $user_cur = &$room->user[$table->player[$i]];
2436 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
2437 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
2440 $showst .= ",-2,-2";
2441 $ret .= sprintf('show_astat(%s);', $showst);
2443 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
2444 /* show card chooser */
2445 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
2450 if ($table_pos == ($table->gstart % PLAYERS_N) &&
2451 $table->asta_win == -1)
2452 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2453 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
2455 $ret .= sprintf('dispose_asta(%d,%d, %s);',
2456 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
2460 if ($table->asta_win == -1) { // auction case
2461 if ($table_pos == ($table->gstart % PLAYERS_N))
2462 $ret .= "remark_on();";
2464 $ret .= "remark_off();";
2466 else { // chooseed case
2467 if ($table_pos == $table->asta_win)
2468 $ret .= "remark_on();";
2470 $ret .= "remark_off();";
2473 else if ($user->subst == 'game') {
2475 if (($table->gstart + $table->turn) % PLAYERS_N == $table_pos)
2476 $ret .= "is_my_time = true; remark_on();";
2478 $ret .= "remark_off();";
2480 /* WHO CALL AND WATH */
2481 $ret .= briscola_show($room, $table, $user);
2485 } // end function show_table(...
2487 function calculate_winner(&$table)
2494 $cur_seed = $table->briscola - ($table->briscola % 10);
2496 for ($i = 0 ; $i < 40 ; $i++) {
2497 if ($table->card[$i]->stat != "table")
2500 log_wr(sprintf("Card On table: [%d]", $i));
2502 $v = $table->card[$i]->value;
2503 $ontab[$table->card[$i]->owner] = $v;
2504 $ontid[$table->card[$i]->owner] = $i;
2505 /* se briscola setto il flag */
2506 if (($v - ($v % 10)) == $cur_seed)
2510 if ($briontab == FALSE) {
2511 $cur_win = $table->gstart;
2512 $cur_val = $ontab[$cur_win];
2513 $cur_seed = $cur_val - ($cur_val % 10);
2516 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2517 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
2518 if ($ontab[$i] < $cur_val) {
2519 $cur_val = $ontab[$i];
2525 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2526 $table->card[$ontid[$i]]->owner = $cur_win;
2527 $table->card[$ontid[$i]]->stat = "take"; // Card stat
2532 function calculate_points(&$table)
2534 GLOBAL $G_all_points;
2538 if ($table->asta_pnt == 60)
2539 $table->asta_pnt = 61;
2541 $table->old_reason = "";
2542 $table->old_win = $table->asta_win;
2543 $table->old_friend = $table->friend;
2544 $table->old_asta_pnt = $table->asta_pnt;
2546 for ($i = 0 ; $i < 40 ; $i++) {
2547 $ctt = $table->card[$i]->value % 10;
2548 $own = $table->card[$i]->owner;
2549 if ($own == $table->asta_win || $own == $table->friend)
2550 $pro += $G_all_points[$ctt];
2553 log_wr(sprintf("PRO: [%d]", $pro));
2556 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
2557 $table->points[$table->points_n % MAX_POINTS] = array();
2558 for ($i = 0 ; $i < PLAYERS_N ; $i++)
2559 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
2561 $table->old_pnt = $pro;
2567 if ($pro >= $table->asta_pnt)
2572 $table->points[$table->points_n % MAX_POINTS] = array();
2573 for ($i = 0 ; $i < 5 ; $i++) {
2574 if ($i == $table->asta_win)
2575 $pt = ($i == $table->friend ? 4 : 2);
2576 else if ($i == $table->friend)
2581 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
2583 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
2585 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
2587 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
2588 $table->total[$i] += $pt;
2591 $table->old_pnt = $pro;
2595 function validate_sess($sess)
2597 if (strlen($sess) == SESS_LEN)
2603 function validate_name($name)
2605 $name_new = str_replace(' ', '_', substr(trim($name),0,12));
2607 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
2609 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
2616 function playsound($filename)
2618 return (sprintf('playsound("flasou", "%s");', $filename));
2621 function secstoword($secs)
2623 $mins = floor($secs / 60);
2626 $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
2629 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));
2634 function sharedmem_sz($tok)
2636 if (($shm_id = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
2637 log_main("shmop_open failed");
2640 $shm_sz = shmop_size($shm_id);
2641 shmop_close($shm_id);
2643 log_main("shm_sz: ".$shm_sz." SHM_DIMS: ".SHM_DIMS);