5 * Copyright (C) 2006-2007 matteo.nastasi@milug.org
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details. You should have received a
16 * copy of the GNU General Public License along with this program; if
17 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
18 * Suite 330, Boston, MA 02111-1307, USA.
22 define(FTOK_PATH, "/var/lib/brisk");
23 define(LEGAL_PATH, "/tmp/legal_brisk");
24 define(PROXY_PATH, "/var/lib/brisk_proxy");
27 define(MAX_POINTS, 5);
28 define(MAX_PLAYERS, (20 + (PLAYERS_N * TABLES_N)));
29 define(SHM_DIMS, (50000 * MAX_PLAYERS));
32 define(COMM_GEN_N, 50);
34 define(STREAM_TIMEOUT, 20);
35 // FIXME original: define(EXPIRE_TIME_RD, 180);
36 define(EXPIRE_TIME_RD, 60);
37 define(EXPIRE_TIME_SMAMMA, 360);
38 // BAN_TIME da allineare anche in commons.js
39 define(BAN_TIME, 900);
40 define(GARBAGE_TIMEOUT, 10);
41 define(NICKSERV, "<i>SERVER</i>");
42 define(BRISK_DEBUG, FALSE);
43 // define(DEBUGGING, "local");
47 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
48 $G_brisk_version = "spawn-0.0.3";
50 $root_wellarr = Array ( 'Benvenuto in brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: nuovo layout che permette più tavoli, più tavoli.',
51 'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
52 $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.');
56 <div style=\\"text-align: left; padding: 8px;\\">
57 <b>Descrizione</b><br>
58 Questa è un\'implementazione della briscola in cinque, così come è spiegata su
59 <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>
60 <b>Configurazione del browser.</b><br>
61 Occorre abilitare i cookies.<br>
63 <b>Uso del sito</b><br>
64 Potete sedervi a un tavolo o rimanere in piedi.<br>
65 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
68 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>
69 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
70 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
71 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
72 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
73 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.
75 <dt><b>Comandi della chat</b>
76 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
77 <dd>.. to be continue ..
83 <div id=\\"header\\" class=\\"header\\">
84 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
85 briscola chiamata in salsa ajax
87 <br><b>version '.$G_brisk_version.'</b><br><br>
88 Copyright 2006-2007 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
93 $from = array ( '\\', '@', '|' );
94 $to = array ( '\\\\', '@', '¦' );
96 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
101 var $value; /* 0 - 39 card value */
102 var $stat; /* 'bunch', 'hand', 'table', 'take' */
103 var $owner; /* (table position 0-4) */
104 // var $pos; /* Pos in hand. */
105 var $x; /* When played the X position on the table of the owner. */
106 var $y; /* When played the Y position on the table of the owner. */
108 function Card($value, $stat, $owner)
110 $this->value = $value;
111 $this->stat = $stat; // Card stat
112 $this->owner = $owner;
115 function assign($stat,$owner)
117 $this->stat = $stat; // Card stat
118 $this->owner = $owner;
121 function setpos($pos)
128 $this->stat = 'table'; // Card stat
133 function take($newown)
135 $this->stat = 'take'; // Card stat
136 $this->owner = $newown;
154 var $points; // points array
155 var $points_n; // number of row of points
176 if (($thiz = new Table()) == FALSE)
179 $thiz->player = array();
181 $thiz->card = &$thiz->bunch_create();
182 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
183 $thiz->asta_pla_n= -1;
184 $thiz->asta_card = -1;
185 $thiz->asta_pnt = -1;
187 $thiz->points = array( );
189 $thiz->total = array( 0, 0, 0, 0, 0);
190 $thiz->asta_win = -1;
191 $thiz->briscola = -1;
194 $thiz->old_reason = "";
195 $thiz->old_asta_pnt = -1;
198 $thiz->old_friend= -1;
203 function &clone(&$from)
207 if (($thiz = new Table()) == FALSE)
210 $thiz->player = array();
211 for ($i = 0 ; $i < $from->player_n ; $i++)
212 $thiz->player[$i] = $from->player[$i];
213 $thiz->player_n = $from->player_n;
214 $thiz->card = $from->card;
215 $thiz->mazzo = $from->mazzo; // REVIEW
216 $thiz->gstart = $from->gstart;
217 $thiz->turn = $from->turn;
219 $thiz->asta_pla = $from->asta_pla;
220 $thiz->asta_pla_n = $from->asta_pla_n;
221 $thiz->asta_card = $from->asta_card;
222 $thiz->asta_pnt = $from->asta_pnt;
224 $thiz->mult = $from->mult;
225 $thiz->points = $from->points;
226 $thiz->points_n = $from->points_n;
227 $thiz->total = $from->total;
229 $thiz->asta_win = $from->asta_win;
230 $thiz->briscola = $from->briscola;
231 $thiz->friend = $from->friend;
233 $thiz->old_reason = $from->old_reason;
234 $thiz->old_asta_pnt = $from->old_asta_pnt;
235 $thiz->old_pnt = $from->old_pnt;
236 $thiz->old_win = $from->old_win;
237 $thiz->old_friend = $from->old_friend;
242 function &spawn(&$from)
246 if (($thiz =& new Table()) == FALSE)
249 $thiz->player_n = $from->player_n;
250 $thiz->card = $from->card;
251 $thiz->mazzo = $from->mazzo;
252 $thiz->gstart = $from->gstart;
253 $thiz->turn = $from->turn;
255 $thiz->asta_pla = $from->asta_pla;
256 $thiz->asta_pla_n = $from->asta_pla_n;
257 $thiz->asta_card = $from->asta_card;
258 $thiz->asta_pnt = $from->asta_pnt;
260 $thiz->mult = $from->mult;
261 $thiz->points = $from->points;
262 $thiz->points_n = $from->points_n;
263 $thiz->total = $from->total;
265 $thiz->asta_win = $from->asta_win;
266 $thiz->briscola = $from->briscola;
267 $thiz->friend = $from->friend;
269 $thiz->old_reason = $from->old_reason;
270 $thiz->old_asta_pnt = $from->old_asta_pnt;
271 $thiz->old_pnt = $from->old_pnt;
272 $thiz->old_win = $from->old_win;
273 $thiz->old_friend = $from->old_friend;
275 // players are rearranged in an dedicated array
276 $thiz->player = array();
277 for ($i = 0 ; $i < $from->player_n ; $i++)
278 $thiz->player[$i] = $i;
283 function &bunch_create()
287 for ($i = 0 ; $i < 40 ; $i++) {
288 $ret[$i] =& new Card($i, 'bunch', 'no_owner');
295 function bunch_make()
297 $ct = array(0,0,0,0,0);
299 mt_srand(make_seed());
301 for ($i = 39 ; $i >= 0 ; $i--)
304 for ($i = 39 ; $i >= 0 ; $i--) {
308 log_main("RND ZERO", "");
313 $this->card[$id]->assign('hand', $owner);
315 $rest[$rn] = $rest[$i];
316 // $pubbpos[$rn2] = $pubbpos[$i];
320 function init(&$userarr)
322 $this->mazzo = rand(0,PLAYERS_N-1);
326 $this->old_reason = "";
327 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
328 $this->total[$i] = 0;
329 $user_cur = &$userarr[$this->player[$i]];
330 $user_cur->exitislock = TRUE;
333 log_main("table::init","ci siamo");
336 function game_init(&$userarr)
338 log_rd2("XXX", "GSTART 4");
340 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
344 $this->asta_pla_n = PLAYERS_N;
345 $this->asta_card = -1;
346 $this->asta_pnt = 60;
347 $this->asta_win = -1;
348 $this->briscola = -1;
352 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
353 $this->asta_pla[$i] = TRUE;
354 $user_cur = &$userarr[$this->player[$i]];
355 $user_cur->subst = 'asta';
356 $user_cur->asta_card = -2;
357 $user_cur->asta_pnt = -1;
358 $user_cur->handpt = $this->hand_points($i);
359 // SEE function calculate_points(&$table)
365 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
368 function getPlayer($idx)
370 return ($this->player[$idx]);
373 function setPlayer($idx, $player)
375 $this->player[$idx] = $player;
378 function user_add($idx)
380 $this->player[$this->player_n] = $idx;
383 return ($this->player_n - 1);
386 function user_rem(&$room, &$user)
388 $tabpos = $user->table_pos;
390 /* verifico la consistenza dei dati */
391 if ($room->user[$this->player[$tabpos]] == $user) {
393 /* aggiorna l'array dei giocatori al tavolo. */
394 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
395 $this->player[$i] = $this->player[$i+1];
396 $user_cur = &$room->user[$this->player[$i]];
397 $user_cur->table_pos = $i;
402 log_main($user->sess, "INCONSISTENCY ON TABLE.");
406 function hand_points($idx)
408 GLOBAL $G_all_points;
412 for ($i = 0 ; $i < 40 ; $i++) {
413 if ($this->card[$i]->owner != $idx)
416 $ctt = $this->card[$i]->value % 10;
417 $tot += $G_all_points[$ctt];
423 function exitlock_show(&$userarr, $table_pos)
425 $ct = $this->exitlock_calc(&$userarr, $table_pos);
427 $ret = sprintf('exitlock_show(%d, %s);', $ct,
428 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
432 function exitlock_calc(&$userarr, $table_pos)
436 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
437 if ($userarr[$this->player[$i]]->exitislock == FALSE)
446 var $name; // name of the user
447 var $sess; // session of the user
448 var $ip; // ip of the user
449 var $lacc; // last access (for the cleanup)
450 var $laccwr; // last access (for the cleanup)
451 var $bantime; // timeout to temporary ban
452 var $stat; // status (outdoor, room, table, game, ...)
453 var $subst; // substatus for each status
454 var $step; // step of the current status
455 var $trans_step; // step to enable transition between pages (disable == -1)
456 var $comm; // commands array
459 var $handpt; // Total card points at the beginning of the current hand.
460 var $exitislock; // Player can exit from the table ?
461 var $table; // id of the current table (if in table state)
462 var $table_pos; // idx on the table
463 var $table_token;// token that identify a game on a table
464 var $the_end; // Flag to change the end of the session
469 function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
472 if (($thiz =& new User()) == FALSE)
478 $thiz->lacc = time();
479 $thiz->laccwr = time();
482 $thiz->subst = $subst;
484 $thiz->trans_step = -1;
485 $thiz->comm = array();
486 $thiz->asta_card = -2;
487 $thiz->asta_pnt = -1;
489 $thiz->exitislock = TRUE;
491 $thiz->table = $table;
492 $thiz->table_pos = -1;
493 $thiz->table_token = "";
498 function &clone(&$from)
502 if (($thiz = new User()) == FALSE)
505 $thiz->name = $from->name;
506 $thiz->sess = $from->sess;
507 $thiz->ip = $from->ip;
508 $thiz->lacc = $from->lacc;
509 $thiz->laccwr = $from->laccwr;
510 $thiz->bantime = $from->bantime;
511 $thiz->stat = $from->stat;
512 $thiz->subst = $from->subst;
513 $thiz->step = $from->step;
514 $thiz->trans_step = $from->trans_step;
515 $thiz->comm = array();
517 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
518 for ($i = $i_start ; $i < $from->step ; $i++) {
520 $thiz->comm[$ii] = $from->comm[$ii];
522 $thiz->asta_card = $from->asta_card;
523 $thiz->asta_pnt = $from->asta_pnt;
524 $thiz->handpt = $from->handpt;
525 $thiz->exitislock = $from->exitislock;
526 $thiz->table = $from->table;
527 $thiz->table_pos = $from->table_pos;
528 $thiz->table_token = $from->table_token;
529 $thiz->the_end = $from->the_end;
534 function &spawn(&$from, $table, $table_pos)
538 if (($thiz =& new User()) == FALSE)
541 $thiz->name = $from->name;
542 $thiz->sess = $from->sess;
543 $thiz->ip = $from->ip;
544 $thiz->lacc = $from->lacc;
545 $thiz->laccwr = $from->laccwr;
546 $thiz->bantime = $from->bantime;
547 $thiz->stat = $from->stat;
548 $thiz->subst = $from->subst;
549 $thiz->step = $from->step;
550 $thiz->trans_step = $from->trans_step;
551 $thiz->comm = array();
553 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
554 for ($i = $i_start ; $i < $from->step ; $i++) {
555 log_wr("XX", "TRY PUSH:".$i);
557 $thiz->comm[$ii] = $from->comm[$ii];
560 $thiz->asta_card = $from->asta_card;
561 $thiz->asta_pnt = $from->asta_pnt;
562 $thiz->handpt = $from->handpt;
563 $thiz->exitislock = $from->exitislock;
564 $thiz->the_end = $from->the_end;
566 $thiz->table = $table;
567 $thiz->table_pos = $table_pos;
568 $thiz->table_token = $from->table_token;
573 function stat_set($stat) {
574 $this->stat = "$stat";
577 if (validate_sess($this->sess)) {
578 $fp = fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
579 fwrite($fp, sprintf("%s\n",$this->stat));
585 function step_set($step) {
589 if (validate_sess($this->sess) == FALSE)
591 if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
593 fwrite($fp, pack("l",$this->step), 4);
602 function step_inc() {
605 if (validate_sess($this->sess)) {
606 $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
607 fwrite($fp, pack("l",$this->step), 4);
617 function step_get($sess) {
621 if (validate_sess($sess) == FALSE)
624 if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
627 if (($s = fread($fp, 4)) == FALSE)
633 $arr = unpack('l', $s);
636 // log_rd2($sess, "A0: ".$arr[0]." A1: ".$arr[1]);
643 log_rd2($sess, "STEP_GET: return false ".$ct);
647 function step_unproxy($sess) {
648 log_rd2($sess, "UNPROXY: ".PROXY_PATH."/".$sess.".step");
649 @unlink(PROXY_PATH."/".$sess.".step");
656 var $comm; // commands for many people
657 var $step; // current step of the comm array
658 var $garbage_timeout;
661 $this->user = array();
662 $this->table = array();
664 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
665 $this->user[$i] =& User::create("", "");
668 for ($i = 0 ; $i < TABLES_N ; $i++)
669 $this->table[$i] =& Table::create();
670 $this->garbage_timeout = 0;
673 function garbage_manager($force)
677 /* Garbage collector degli utenti in timeout */
679 if ($force || $this->garbage_timeout < $curtime) {
681 // Before all align times with table timeout
682 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
683 $table_cur =& $this->table[$table_idx];
684 // if the table is complete and exists its shared mem we get the info about users lacc
685 if ($table_cur->player_n == PLAYERS_N) {
686 if (($sem = Briskin5::lock_data($table_idx)) != FALSE) {
687 if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
688 if ($bri->garbage_manager(TRUE) == TRUE) {
689 // if garbaged you need to save data
690 Briskin5::save_data(&$bri);
691 if ($bri->is_abandoned()) {
692 // FIXME recovery of room and clean of the table
699 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
700 log_rd2($user_cur->sess, "AUTO LOGOUT.");
702 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
703 log_auth($user_cur->sess, "Autologout session.");
705 $tmp_sess = $user_cur->sess;
706 $user_cur->sess = "";
707 step_unproxy($tmp_sess);
708 $user_cur->name = "";
710 $user_cur->the_end = FALSE;
712 log_rd2($user_cur->sess, "AUTO LOGOUT.");
713 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
714 $this->room_wakeup(&$user_cur);
715 else if ($user_cur->subst == 'standup')
716 $this->room_outstandup(&$user_cur);
718 log_rd2($sess, "LOGOUT FROM WHAT ???");
726 Briskin5::destroy_data($user->table);
732 $table_bri = &$bri->table[0];
733 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
734 $this->user[$table_cur->player[$i]]->lacc =
735 $bri->user[$table_bri->player[$i]]->lacc;
738 Briskin5::unlock_data($sem);
743 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
744 $user_cur = &$this->user[$i];
745 if ($user_cur->sess == "")
748 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
749 log_rd2($user_cur->sess, "AUTO LOGOUT.");
751 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
752 log_auth($user_cur->sess, "Autologout session.");
754 $tmp_sess = $user_cur->sess;
755 $user_cur->sess = "";
756 step_unproxy($tmp_sess);
757 $user_cur->name = "";
759 $user_cur->the_end = FALSE;
761 log_rd2($user_cur->sess, "AUTO LOGOUT.");
762 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
763 $this->room_wakeup(&$user_cur);
764 else if ($user_cur->subst == 'standup')
765 $this->room_outstandup(&$user_cur);
767 log_rd2($sess, "LOGOUT FROM WHAT ???");
771 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
772 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
773 $this->room_wakeup(&$user_cur);
774 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
775 $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);
776 $user_cur->step_inc();
780 log_rd2($user_cur->sess, "GARBAGE UPDATED!");
782 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
790 function room_wakeup(&$user)
792 $table_idx = $user->table;
793 $table = &$this->table[$table_idx];
795 log_main("WAKEUP", "begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
799 $from_table = ($user->stat == "table");
801 log_main("WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
803 for ($i = 0 ; $i < $table->player_n ; $i++) {
804 $user_cur = &$this->user[$table->player[$i]];
805 log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
807 if ($user_cur != $user) {
808 $user_cur->stat_set("room");
809 $user_cur->subst = "sitdown";
810 $user_cur->laccwr = $curtime;
812 else if ($user->sess != "") {
813 $user_cur->stat_set("room");
814 $user_cur->subst = "standup";
815 $user_cur->laccwr = $curtime;
816 $user_cur->table = -1;
821 $user->stat_set("room");
822 $user->subst = "standup";
823 $user->laccwr = $curtime;
825 /* aggiorna l'array dei giocatori al tavolo. */
826 $table->user_rem(&$this, &$user);
828 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
829 $user_cur = &$this->user[$i];
830 if ($user_cur->sess == '' || $user_cur->stat != 'room')
833 log_main("VALORI", "name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
835 $ret = "gst.st = ".($user_cur->step+1)."; ";
836 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
837 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
838 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
839 log_main("DOCUMENT.index.php", "from table");
841 else if ($user_cur->stat == "room") {
842 log_main("DOCUMENT.index.php", "from table");
844 $ret .= table_content($this, $user_cur, $table_idx);
845 $ret .= standup_content($this, $user_cur);
847 $act_content = table_act_content(FALSE, 0, $table_idx, $user->table);
848 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
851 if ($user_cur == $user) {
852 // set the new status
853 $ret .= 'subst = "standup"; ';
854 // clean the action buttons in other tables
855 for ($e = 0 ; $e < TABLES_N ; $e++) {
856 if ($this->table[$e]->player_n < PLAYERS_N)
857 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
861 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
862 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
865 log_wr($user_cur->sess, "ROOM_WAKEUP: ".$ret);
866 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
867 $user_cur->step_inc();
871 function room_join_wakeup(&$user, $update_lacc = FALSE)
873 $table_idx = $user->table;
874 $table = &$this->table[$table_idx];
876 log_main($user->sess, "JOIN_WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
881 log_main($user->sess, "JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
883 for ($i = 0 ; $i < $table->player_n ; $i++) {
884 $user_cur = &$this->user[$table->player[$i]];
885 log_main($user->sess, "PREIMPOST INLOOP name: ".$user_cur->name);
886 if ($user_cur->sess != "") {
887 if ($update_lacc == TRUE) {
888 $user_cur->laccwr = $curtime;
890 log_load($user->sess, "cur: ".$user_cur->name." subst: ".$user_cur->subst);
891 if ($user_cur->subst == "shutdowned") {
892 $user_cur->stat_set("room");
893 $user_cur->subst = "sitdown";
895 else if ($user_cur->subst == "shutdowner") {
896 $user_cur->stat_set("room");
897 $user_cur->subst = "standup";
898 $user_cur->table = -1;
899 $user_wup[$user_wup_n++] = &$user_cur;
904 for ($wup_idx = 0 ; $wup_idx < $user_wup_n ; $wup_idx++)
905 $table->user_rem(&$this, &$user_wup[$wup_idx]);
907 /* aggiorna l'array dei giocatori al tavolo. */
909 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
910 log_main($user_cur->sess, "START LOOP");
911 $user_cur = &$this->user[$i];
912 if ($user_cur->sess == '' || $user_cur->stat != 'room') {
913 log_main($user_cur->sess, "name: ".$user_cur->name."skip subst: ".$user_cur->subst);
917 log_main($user->sess, "___");
918 log_main($user->sess, "VALORI name: ".$user_cur->name." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
920 $ret = "gst.st = ".($user_cur->step+1)."; ";
921 if ($user_cur->stat == "room") {
922 log_main($user->sess, "DOCUMENT.index.php from table");
924 $ret .= table_content($this, $user_cur, $table_idx);
925 $ret .= standup_content($this, $user_cur);
927 $act_content = table_act_content(FALSE, 0, $table_idx, $user_cur->table);
928 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
930 for ($wup_idx = 0 ; $wup_idx < $user_wup_n ; $wup_idx++)
931 if ($user_cur == $user_wup[$wup_idx])
934 log_main($user->sess, "JOIN_WAKEUP wup_idx ".$wup_idx." wup_n ".$user_wup_n);
936 if ($wup_idx < $user_wup_n) {
937 log_main($user->sess, "JOIN_WAKEUP less");
938 // set the new status
939 $ret .= 'subst = "standup"; ';
940 // clean the action buttons in other tables
941 for ($e = 0 ; $e < TABLES_N ; $e++) {
942 if ($this->table[$e]->player_n < PLAYERS_N)
943 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user_cur->table));
945 log_main($user->sess, "JOIN_WAKEUP end less");
948 log_main($user->sess, "JOIN_WAKEUP more");
949 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
950 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
951 log_main($user->sess, "JOIN_WAKEUP end more");
954 log_wr($user->sess, "ROOM_JOIN_WAKEUP: ".$ret);
955 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
956 $user_cur->step_inc();
960 function room_outstandup(&$user)
962 $this->room_sitdown(&$user, -1);
965 function table_update(&$user)
967 log_main("table_update", "pre - USER: ".$user->name);
969 $table_idx = $user->table;
972 $table = &$this->table[$table_idx];
974 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
976 $user_cur = &$this->user[$i];
977 if ($user_cur->sess == '' || $user_cur->stat != 'room')
980 $ret = "gst.st = ".($user_cur->step+1)."; ";
982 $ret .= table_content($this, $user_cur, $table_idx);
984 if ($user_cur == $user) {
985 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
987 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
988 $user_cur->step_inc();
991 log_main("table_update", "post");
994 function room_sitdown(&$user, $table_idx)
996 log_main("room_sitdown", ($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
999 $table = &$this->table[$table_idx];
1001 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1003 $user_cur = &$this->user[$i];
1004 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1007 $ret = "gst.st = ".($user_cur->step+1)."; ";
1008 if ($table_idx > -1)
1009 $ret .= table_content($this, $user_cur, $table_idx);
1010 $ret .= standup_content($this, $user_cur);
1012 if ($user_cur == $user) {
1013 $ret .= 'subst = "sitdown"; ';
1014 // clean the action buttons in other tables
1015 for ($e = 0 ; $e < TABLES_N ; $e++) {
1016 $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
1017 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
1020 else if ($table_idx > -1) {
1021 if ($table->player_n == PLAYERS_N) {
1022 $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
1023 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1026 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1027 $user_cur->step_inc();
1031 function chatt_send(&$user, $mesg)
1033 if ($user->stat == 'table') {
1034 $table = &$this->table[$user->table];
1037 $user_mesg = substr($mesg,6);
1041 $dt = date("H:i ", $timecur);
1042 if (strncmp($user_mesg, "/nick ", 6) == 0) {
1043 log_main($user->sess, "chatt_send BEGIN");
1045 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
1046 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1047 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Il nickname deve contenere almeno una lettera o una cifra.");', $dt.NICKSERV, xcape($name_new));
1052 $user_mesg = "COMMAND ".$user_mesg;
1055 // update local graph
1056 // update remote graphs
1057 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1058 $user_cur = &$this->user[$i];
1059 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
1060 if ($user_cur->sess == '')
1062 if ($user_cur->name == $name_new) {
1063 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1064 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
1069 if ($i == MAX_PLAYERS) {
1070 $user->name = $name_new;
1072 log_main($user->sess, "chatt_send start set");
1075 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1076 log_main($user->sess, "chatt_send set loop");
1078 $user_cur = &$this->user[$i];
1079 if ($user_cur->sess == '')
1081 if ($user_cur->stat == 'room') {
1082 if ($user->stat == 'room' && $user->subst == 'standup') {
1083 $this->standup_update(&$user);
1085 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
1086 $user->stat == 'table') {
1087 log_main($user->sess, "chatt_send pre table update");
1089 $this->table_update(&$user);
1091 log_main($user->sess, "chatt_send post table update");
1094 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
1095 $table = &$this->table[$user->table];
1097 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1098 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1099 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
1100 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
1101 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
1102 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
1103 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
1104 if ($user_cur == $user)
1105 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
1106 xcape($user->name,ENT_COMPAT,"UTF-8"));
1107 $user_cur->step_inc();
1113 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
1114 if ($user->stat == 'room') {
1115 $user_cur = &$this->user[$i];
1116 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1120 $user_cur = &$this->user[$table->player[$i]];
1123 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1124 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
1125 $dt.xcape($user->name), xcape($user_mesg));
1126 $user_cur->step_inc();
1128 log_legal($timecur, $user->sess, $user->name,
1129 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
1133 function &get_user($sess, &$idx)
1135 GLOBAL $PHP_SELF, $G_false;
1137 if (validate_sess($sess)) {
1138 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1139 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1142 $ret = &$this->user[$i];
1146 log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1147 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
1148 // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1151 log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
1158 * function &add_user(&$room, &$sess, &$idx, $name, $ip)
1161 * if ($idx != -1 && ret == FALSE) => duplicated nick
1162 * if ($idx == -2 && ret == FALSE) => invalid name
1163 * if ($idx == -1 && ret == FALSE) => no space left
1164 * if (ret == TRUE) => SUCCESS
1166 function &add_user(&$sess, &$idx, $name, $ip)
1173 if (($name_new = validate_name($name)) == FALSE) {
1178 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
1179 if (validate_sess($sess) == FALSE)
1182 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1184 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1188 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
1191 if (strcmp($this->user[$i]->name, $name_new) == 0) {
1199 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1201 if ($idx != -1 && $i == MAX_PLAYERS) {
1205 $this->user[$idx]->sess = uniqid("");
1206 $sess = $this->user[$idx]->sess;
1210 $this->user[$idx]->sess = $sess;
1212 $this->user[$idx]->name = $name_new;
1213 $this->user[$idx]->stat_set("room");
1214 // MOP $this->user[$idx]->step_set(0);
1215 $this->user[$idx]->subst = "standup";
1216 $this->user[$idx]->lacc = $curtime;
1217 $this->user[$idx]->laccwr = $curtime;
1218 $this->user[$idx]->bantime = 0;
1219 $this->user[$idx]->ip = $ip;
1220 log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1222 return ($this->user[$idx]);
1228 function standup_update(&$user)
1230 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1231 $user_cur = &$this->user[$i];
1232 if ($user_cur->sess == '')
1235 log_main("STANDUP START", $user_cur->stat);
1237 if ($user_cur->stat == 'room') {
1238 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
1239 if ($user_cur == $user)
1240 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
1242 log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
1244 $user_cur->step_inc();
1250 function &init_data()
1252 $room =& new Room();
1258 function &load_data()
1260 GLOBAL $G_false, $sess;
1262 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
1267 if ($shm = shm_attach($tok, SHM_DIMS)) {
1268 $room = @shm_get_var($shm, $tok);
1270 log_only($sess, "bri == ".($room == FALSE ? "FALSE" : "TRUE")." bri === ".($room === FALSE ? "FALSE" : "TRUE")." bri isset ".(isset($room) ? "TRUE" : "FALSE"));
1272 log_only($sess, "bri count ".count($room));
1274 if ($room == FALSE) {
1275 log_only($sess, "INIT MAIN DATA");
1277 $room =& Room::init_data();
1278 if (shm_put_var($shm, $tok, $room) == FALSE) {
1279 log_only($sess, "PUT_VAR FALLITA ".strlen(serialize($room)));
1280 log_only($sess, serialize($room));
1294 function save_data(&$room)
1304 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1)
1310 if (($shm = shm_attach($tok, SHM_DIMS)) == FALSE)
1313 // log_only($sess, "PUT_VAR DI ".strlen(serialize($room)));
1314 if (shm_put_var($shm, $tok, $room) == FALSE) {
1315 log_only($sess, "PUT_VAR FALLITA ".strlen(serialize($room)));
1316 log_only($sess, serialize($room));
1319 // log_main("XXX", "QUI CI ARRIVA [".$room->user[0]->name."]");
1329 function lock_data()
1333 // echo "LOCK: ".FTOK_PATH."/main";
1335 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
1339 // echo "FTOK ".$tok."<br>";
1340 if (($res = sem_get($tok)) == FALSE) {
1341 echo "SEM_GET FAILED";
1344 if (sem_acquire($res)) {
1345 log_only($sess, "LOCK");
1352 function unlock_data($res)
1356 log_only($sess, "UNLOCK");
1358 return (sem_release($res));
1362 function make_seed()
1364 list($usec, $sec) = explode(' ', microtime());
1365 return (float) $sec + ((float) $usec * 100000);
1368 function log_only2($sess, $log) {
1369 if (BRISK_DEBUG != TRUE)
1373 if (($fp = @fopen("/tmp/brisk_only2.log", 'a')) != FALSE) {
1374 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1379 function log_only($sess, $log) {
1380 if (BRISK_DEBUG != TRUE)
1384 if (($fp = @fopen("/tmp/brisk_only.log", 'a')) != FALSE) {
1385 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1390 function log_main($sess, $log) {
1391 if (BRISK_DEBUG != TRUE)
1394 if (($fp = @fopen("/tmp/brisk_main.log", 'a')) != FALSE) {
1395 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1400 function log_rd($sess, $log) {
1401 if (BRISK_DEBUG != TRUE)
1404 if (($fp = @fopen("/tmp/brisk_rd.log", 'a')) != FALSE) {
1405 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1410 function log_rd2($sess, $log) {
1411 if (BRISK_DEBUG != TRUE)
1414 if (($fp = @fopen("/tmp/brisk_rd2.log", 'a')) != FALSE) {
1415 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1420 function log_send($sess, $log) {
1421 if (BRISK_DEBUG != TRUE)
1424 if (($fp = @fopen("/tmp/brisk_send.log", 'a')) != FALSE) {
1425 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1430 function log_auth($sess, $log) {
1431 if (BRISK_DEBUG != TRUE)
1434 if (($fp = @fopen("/tmp/brisk_auth.log", 'a')) != FALSE) {
1435 fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
1440 function log_lock($sess, $log) {
1441 if (BRISK_DEBUG != TRUE)
1444 if (($fp = @fopen("/tmp/brisk_lock.log", 'a')) != FALSE) {
1445 fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
1450 function log_wr($sess, $log) {
1451 if (BRISK_DEBUG != TRUE)
1454 if (($fp = @fopen("/tmp/brisk_wr.log", 'a')) != FALSE) {
1455 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1460 function log_load($sess, $log) {
1461 if (BRISK_DEBUG != TRUE)
1464 if (($fp = @fopen("/tmp/brisk_load.log", 'a')) != FALSE) {
1465 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1470 function log_legal($timecur, $sess, $name, $where, $mesg)
1474 if (($fp = @fopen(LEGAL_PATH, 'a')) != FALSE) {
1475 /* Unix time | session | nickname | IP | where was | mesg */
1476 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|\n", $timecur, $sess, $name, $_SERVER['REMOTE_ADDR'], $where , $mesg));
1484 function lock_banlist()
1486 if (($tok = ftok(FTOK_PATH."/main", "L")) == -1) {
1490 if (($res = sem_get($tok)) == FALSE) {
1491 echo "SEM_GET FAILED";
1494 if (sem_acquire($res))
1500 function unlock_banlist($res)
1502 return (sem_release($res));
1505 function table_act_content($isstanding, $sitted, $table, $cur_table)
1510 if ($sitted < PLAYERS_N) {
1511 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
1515 if ($table == $cur_table)
1516 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
1523 function table_content($room, $user, $table_idx)
1529 // Si possono usare i dati nella classe table
1532 $sess = $user->sess;
1533 $table = &$room->table[$table_idx];
1535 if ($user->stat != 'room')
1538 for ($i = 0 ; $i < $table->player_n ; $i++) {
1539 $user_cur = &$room->user[$table->player[$i]];
1541 if ($user_cur == $user)
1542 { $hilion = "<b>"; $hilioff = "</b>"; }
1544 { $hilion = ""; $hilioff = ""; }
1546 log_main($user_cur->name, sprintf("IN TABLE [%d]", $table_idx));
1548 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
1551 for ( ; $i < PLAYERS_N ; $i++)
1555 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
1560 function standup_content(&$room, $user)
1565 if ($user->stat != 'room')
1568 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
1569 if ($room->user[$e]->sess == "" || $room->user[$e]->stat != "room" || $room->user[$e]->name == "")
1574 $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
1576 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
1577 if ($room->user[$e]->sess == "" || $room->user[$e]->stat != "room" || $room->user[$e]->name == "")
1581 if ($room->user[$e]->subst == "standup") {
1582 if (($ct % 4) == 0) {
1585 if ($room->user[$e] == $user)
1586 { $hilion = "<b>"; $hilioff = "</b>"; }
1588 { $hilion = ""; $hilioff = ""; }
1590 $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($room->user[$e]->name), $hilioff);
1591 if (($ct % 4) == 3) {
1592 $content .= '</tr>';
1597 $content .= '</table>';
1599 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
1600 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
1601 $content, $content2);
1607 function show_notify($text, $tout, $butt, $w, $h)
1609 log_main("SHOW_NOTIFY", $text);
1610 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
1613 function briscola_show($room, $table, $user)
1618 if ($table->asta_card == 9)
1619 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1621 /* text of caller cell */
1622 if ($user->table_pos == $table->asta_win)
1623 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1625 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
1626 xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
1628 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1630 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1631 $ret .= sprintf('$("caller").style.visibility = "visible";');
1632 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1633 $ret .= sprintf('$("astalascio").style.visibility = "";');
1634 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1635 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1641 function game_result($asta_pnt, $pnt)
1643 if ($asta_pnt == 61) {
1646 else if ($pnt == 60)
1652 if ($pnt >= $asta_pnt)
1659 function multoval($mult)
1663 else if ($mult == 4)
1664 return ("quadruplo");
1666 return (sprintf("%d-plo", $mult));
1669 function show_table_info(&$room, &$table, $table_pos)
1672 $user = &$room->user[$table->player[$table_pos]];
1674 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1675 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1678 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1679 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
1680 $noty .= sprintf("</tr>");
1683 log_main("show_table_info", "pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1685 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1686 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1687 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1688 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1693 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1694 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1695 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1696 $noty .= "</tr></table>";
1698 if ($table->old_reason != "") {
1699 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1702 if ($table->old_win != -1) {
1703 $win = $table->player[$table->old_win];
1704 $fri = $table->player[$table->old_friend];
1706 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1709 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
1710 xcape($room->user[$win]->name),
1711 xcape($room->user[$fri]->name));
1712 if ($table->old_pnt == 120) {
1713 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1716 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1717 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1718 'più di 60'), $table->old_pnt,
1719 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1723 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1724 xcape($room->user[$win]->name));
1725 if ($table->old_pnt == 120) {
1726 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1729 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1730 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1731 'più di 60'), $table->old_pnt,
1732 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1736 if ($table->mazzo == $table_pos)
1737 $noty .= "Fai <b>tu</b> il mazzo,";
1739 $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
1740 $noty .= "Il mazzo a <b>$unam</b>,";
1743 if ($user->subst == 'asta') {
1744 if ($table->asta_win == -1) // auction case
1745 $curplayer = $table->gstart % PLAYERS_N;
1747 $curplayer = $table->asta_win;
1749 else if ($user->subst == 'game') {
1750 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
1754 if ($curplayer == $table_pos) {
1755 $noty .= " tocca a <b>te</b> giocare.";
1758 $unam = xcape($room->user[$table->player[$curplayer]]->name);
1759 $noty .= " tocca a <b>$unam</b> giocare.";
1762 if ($table->mult > 1) {
1763 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1765 $noty .= "<hr><br>";
1767 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
1772 function root_wellcome($user)
1774 GLOBAL $root_wellarr;
1777 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
1778 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $root_wellarr[$i]));
1783 function table_wellcome($user)
1785 GLOBAL $table_wellarr;
1788 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1789 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
1794 function show_room(&$room, &$user)
1796 $ret = sprintf('gst.st = %d;', $user->step);
1797 $ret .= sprintf('stat = "%s";', $user->stat);
1799 $ret .= root_wellcome($user);
1800 $ret .= sprintf('subst = "%s";', $user->subst);
1801 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1802 for ($i = 0 ; $i < TABLES_N ; $i++) {
1803 $ret .= table_content($room, $user, $i);
1804 $act_content = table_act_content(($user->subst == 'standup'),
1805 $room->table[$i]->player_n, $i, $user->table);
1806 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1808 $ret .= standup_content($room, $user);
1816 is_transition (is from room to table ?)
1817 is_again (is another game)
1819 Examples of $is_transition, $is_again:
1820 from reload of the page: FALSE, FALSE
1821 from sitdown in room: TRUE, FALSE
1822 from table: asta cmd e tutti passano: TRUE, TRUE
1823 from table: fine partita: TRUE, TRUE
1825 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
1827 $table_idx = $user->table;
1828 $table = &$room->table[$table_idx];
1829 $table_pos = $user->table_pos;
1831 $ret = "table_init();";
1832 $ret .= $table->exitlock_show(&$room->user, $table_pos);
1834 /* GENERAL STATUS */
1835 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1836 $sendstep, $user->stat, $user->subst, $table_pos);
1838 log_rd($user->sess, sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1839 $sendstep, $user->stat, $user->subst, $table_pos));
1842 $ret .= "background_set();";
1845 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1846 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1847 xcape($room->user[$table->player[($table_pos)%PLAYERS_N]]->name),
1848 xcape($room->user[$table->player[($table_pos+1)%PLAYERS_N]]->name),
1849 xcape($room->user[$table->player[($table_pos+2)%PLAYERS_N]]->name),
1850 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+3)%PLAYERS_N]]->name)),
1851 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+4)%PLAYERS_N]]->name)));
1853 /* NOTIFY FOR THE CARD MAKER */
1854 if ($is_transition) { // && $user->subst == "asta" superfluo
1855 $ret .= show_table_info(&$room, &$table, $table_pos);
1858 $ret .= table_wellcome($user);
1860 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1861 $ret .= playsound("cow.mp3");
1866 if ($is_transition) { // && $user->subst == "asta" superfluo
1869 for ($i = 0 ; $i < 8 ; $i++) {
1870 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1872 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1873 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1879 log_rd($user->sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1881 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
1882 $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1883 $table->card[$o]->value : -1),
1884 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1889 $taked = array(0,0,0,0,0);
1890 $inhand = array(0,0,0,0,0);
1891 $ontabl = array(-1,-1,-1,-1,-1);
1894 for ($i = 0 ; $i < 40 ; $i++) {
1895 if ($table->card[$i]->stat == 'hand') {
1896 if ($table->card[$i]->owner == $table_pos) {
1897 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1899 $inhand[$table->card[$i]->owner]++;
1901 else if ($table->card[$i]->stat == 'take') {
1902 log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1903 $taked[$table->card[$i]->owner]++;
1905 else if ($table->card[$i]->stat == 'table') {
1906 $ontabl[$table->card[$i]->owner] = $i;
1910 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1911 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1913 log_main("Stat table:", $logg);
1915 /* Set ours cards. */
1917 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1918 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1919 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
1920 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1921 $ret .= sprintf('card_setours(%s);', $oursarg);
1923 /* Dispose all cards */
1924 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1925 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1926 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1927 $inhand[$i], $taked[$i]);
1929 if ($ontabl[$i] != -1) {
1930 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1931 $table->card[$ontabl[$i]]->value,
1932 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1938 if ($user->subst == 'asta') {
1940 /* show users auction status */
1942 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1943 $user_cur = &$room->user[$table->player[$i]];
1944 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1945 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1948 $showst .= ",-2,-2";
1949 $ret .= sprintf('show_astat(%s);', $showst);
1951 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1952 /* show card chooser */
1953 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1958 if ($table_pos == ($table->gstart % PLAYERS_N) &&
1959 $table->asta_win == -1)
1960 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1961 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1963 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1964 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1968 if ($table->asta_win == -1) { // auction case
1969 if ($table_pos == ($table->gstart % PLAYERS_N))
1970 $ret .= "remark_on();";
1972 $ret .= "remark_off();";
1974 else { // chooseed case
1975 if ($table_pos == $table->asta_win)
1976 $ret .= "remark_on();";
1978 $ret .= "remark_off();";
1981 else if ($user->subst == 'game') {
1983 if (($table->gstart + $table->turn) % PLAYERS_N == $table_pos)
1984 $ret .= "is_my_time = true; remark_on();";
1986 $ret .= "remark_off();";
1988 /* WHO CALL AND WATH */
1989 $ret .= briscola_show($room, $table, $user);
1993 } // end function show_table(...
1995 function calculate_winner(&$table)
2002 $cur_seed = $table->briscola - ($table->briscola % 10);
2004 for ($i = 0 ; $i < 40 ; $i++) {
2005 if ($table->card[$i]->stat != "table")
2008 log_wr($sess, sprintf("Card On table: [%d]", $i));
2010 $v = $table->card[$i]->value;
2011 $ontab[$table->card[$i]->owner] = $v;
2012 $ontid[$table->card[$i]->owner] = $i;
2013 /* se briscola setto il flag */
2014 if (($v - ($v % 10)) == $cur_seed)
2018 if ($briontab == FALSE) {
2019 $cur_win = $table->gstart;
2020 $cur_val = $ontab[$cur_win];
2021 $cur_seed = $cur_val - ($cur_val % 10);
2024 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2025 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
2026 if ($ontab[$i] < $cur_val) {
2027 $cur_val = $ontab[$i];
2033 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2034 $table->card[$ontid[$i]]->owner = $cur_win;
2035 $table->card[$ontid[$i]]->stat = "take"; // Card stat
2040 function calculate_points(&$table)
2042 GLOBAL $G_all_points;
2046 if ($table->asta_pnt == 60)
2047 $table->asta_pnt = 61;
2049 $table->old_reason = "";
2050 $table->old_win = $table->asta_win;
2051 $table->old_friend = $table->friend;
2052 $table->old_asta_pnt = $table->asta_pnt;
2054 for ($i = 0 ; $i < 40 ; $i++) {
2055 $ctt = $table->card[$i]->value % 10;
2056 $own = $table->card[$i]->owner;
2057 if ($own == $table->asta_win || $own == $table->friend)
2058 $pro += $G_all_points[$ctt];
2061 log_wr("XXX", sprintf("PRO: [%d]", $pro));
2064 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
2065 $table->points[$table->points_n % MAX_POINTS] = array();
2066 for ($i = 0 ; $i < PLAYERS_N ; $i++)
2067 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
2069 $table->old_pnt = $pro;
2075 if ($pro >= $table->asta_pnt)
2080 $table->points[$table->points_n % MAX_POINTS] = array();
2081 for ($i = 0 ; $i < 5 ; $i++) {
2082 if ($i == $table->asta_win)
2083 $pt = ($i == $table->friend ? 4 : 2);
2084 else if ($i == $table->friend)
2089 log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
2091 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
2093 log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
2095 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
2096 $table->total[$i] += $pt;
2099 $table->old_pnt = $pro;
2103 function validate_sess($sess)
2105 if (strlen($sess) == SESS_LEN)
2111 function validate_name($name)
2113 $name_new = str_replace(' ', '_', substr(trim($name),0,12));
2115 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
2117 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
2124 function playsound($filename)
2126 return (sprintf('playsound("flasou", "%s");', $filename));
2129 function secstoword($secs)
2131 $mins = floor($secs / 60);
2134 $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
2137 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));