5 * Copyright (C) 2006 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");
25 define(MAX_POINTS, 5);
26 define(MAX_PLAYERS, (PLAYERS_N * TABLES_N));
28 define(COMM_GEN_N, 50);
30 define(STREAM_TIMEOUT, 20);
31 define(EXPIRE_TIME_RD, 180);
32 define(EXPIRE_TIME_SMAMMA, 360);
33 define(GARBAGE_TIMEOUT, 10);
34 define(NICKSERV, "<i>SERVER</i>");
35 define(BRISK_DEBUG, FALSE);
36 // define(DEBUGGING, "local");
38 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
39 $G_brisk_version = "0.4.6";
41 $root_wellarr = Array ( 'Benvenuto in brisk (Ver. '.$G_brisk_version.').',
42 'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
43 $table_wellarr = Array ( 'Benvenuto al tavolo.');
47 <div style=\\"text-align: left; padding: 8px;\\">
48 <b>Descrizione</b><br>
49 Questa è un\'implementazione della briscola in cinque, così come è spiegata su
50 <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>
51 <b>Uso del sito</b><br>
52 Potete sedervi a un tavolo o rimanere in piedi.<br>
53 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
56 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>
57 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
58 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
59 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno quadrato al centro del vostro schermo.<br><br>
60 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
62 <dt><b>Comandi della chat</b>
63 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
64 <dd>.. to be continue ..
70 <div id=\\"header\\" class=\\"header\\">
71 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
72 briscola chiamata in salsa ajax
74 <br><b>version '.$G_brisk_version.'</b><br><br>
75 Copyright 2006-2007 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
80 $from = array ( '\\', '@', '|' );
81 $to = array ( '\\\\', '@', '¦' );
83 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
88 var $value; /* 0 - 39 card value */
89 var $stat; /* 'bunch', 'hand', 'table', 'take' */
90 var $owner; /* (table position 0-4) */
91 // var $pos; /* Pos in hand. */
92 var $x; /* When played the X position on the table of the owner. */
93 var $y; /* When played the Y position on the table of the owner. */
95 function Card($value, $stat, $owner)
97 $this->value = $value;
99 $this->owner = $owner;
102 function assign($stat,$owner)
105 $this->owner = $owner;
108 function setpos($pos)
115 $this->stat = 'table';
120 function take($newown)
122 $this->stat = 'take';
123 $this->owner = $newown;
141 var $points; // points array
142 var $points_n; // number of row of points
157 $this->player = array();
159 $this->card = &$this->bunch_create();
160 $this->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
161 $this->asta_pla_n= -1;
162 $this->asta_card = -1;
163 $this->asta_pnt = -1;
165 $this->points = array( );
167 $this->total = array( 0, 0, 0, 0, 0);
168 $this->asta_win = -1;
169 $this->briscola = -1;
172 $this->old_reason = "";
173 $this->old_asta_pnt = -1;
176 $this->old_friend= -1;
180 function &bunch_create()
184 for ($i = 0 ; $i < 40 ; $i++) {
185 $ret[$i] = new Card($i, 'bunch', 'no_owner');
191 function bunch_make()
193 $ct = array(0,0,0,0,0);
195 mt_srand(make_seed());
197 for ($i = 39 ; $i >= 0 ; $i--)
200 for ($i = 39 ; $i >= 0 ; $i--) {
204 log_main("RND ZERO", "");
209 $this->card[$id]->assign('hand', $owner);
211 $rest[$rn] = $rest[$i];
212 $pubbpos[$rn2] = $pubbpos[$i];
216 function init(&$userarr)
218 $this->mazzo = rand(0,PLAYERS_N-1);
222 $this->old_reason = "";
223 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
224 $this->total[$i] = 0;
225 $user_cur = &$userarr[$this->player[$i]];
226 $user_cur->exitislock = TRUE;
229 log_main("table::init","ci siamo");
232 function game_init(&$userarr)
234 log_rd2($sess,"GSTART 4");
236 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
240 $this->asta_pla_n = PLAYERS_N;
241 $this->asta_card = -1;
242 $this->asta_pnt = 60;
243 $this->asta_win = -1;
244 $this->briscola = -1;
248 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
249 $this->asta_pla[$i] = TRUE;
250 $user_cur = &$userarr[$this->player[$i]];
251 $user_cur->subst = 'asta';
252 $user_cur->asta_card = -2;
253 $user_cur->asta_pnt = -1;
254 $user_cur->handpt = $this->hand_points($i);
255 // SEE function calculate_points(&$table)
261 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
264 function getPlayer($idx)
266 return ($this->player[$idx]);
269 function setPlayer($idx, $player)
271 $this->player[$idx] = $player;
274 function user_add($idx)
276 $this->player[$this->player_n] = $idx;
279 return ($this->player_n - 1);
282 function user_rem(&$bri, &$user)
284 $tabpos = $user->table_pos;
286 /* verifico la consistenza dei dati */
287 if ($bri->user[$this->player[$tabpos]] == $user) {
289 /* aggiorna l'array dei giocatori al tavolo. */
290 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
291 $this->player[$i] = $this->player[$i+1];
292 $user_cur = &$bri->user[$this->player[$i]];
293 $user_cur->table_pos = $i;
298 log_main($user->sess, "INCONSISTENCY ON TABLE.");
302 function hand_points($idx)
304 GLOBAL $G_all_points;
308 for ($i = 0 ; $i < 40 ; $i++) {
309 if ($this->card[$i]->owner != $idx)
312 $ctt = $this->card[$i]->value % 10;
313 $tot += $G_all_points[$ctt];
319 function exitlock_show(&$userarr, $table_pos)
321 $ct = $this->exitlock_calc(&$userarr, $table_pos);
323 $ret = sprintf('exitlock_show(%d, %s);', $ct,
324 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
328 function exitlock_calc(&$userarr, $table_pos)
332 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
333 if ($userarr[$this->player[$i]]->exitislock == FALSE)
342 var $name; // name of the user
343 var $sess; // session of the user
344 var $lacc; // last access (for the cleanup)
345 var $laccwr; // last access (for the cleanup)
346 var $stat; // status (outdoor, room, table, game, ...)
347 var $subst; // substatus for each status
348 var $step; // step of the current status
349 var $trans_step; // step to enable transition between pages (disable == -1)
350 var $comm; // commands array
353 var $handpt; // Total card points at the beginning of the current hand.
354 var $exitislock; // Player can exit from the table ?
355 var $table; // id of the current table (if in table state)
356 var $table_pos; // idx on the table
358 function User($name, $sess, $stat = "", $subst = "", $table = -1) {
361 $this->lacc = time();
362 $this->laccwr = time();
364 $this->subst = $subst;
366 $this->trans_step = -1;
367 $this->comm = array();
368 $this->asta_card = -2;
369 $this->asta_pnt = -1;
371 $this->exitislock = TRUE;
373 $this->table = $table;
380 var $comm; // commands for many people
381 var $step; // current step of the comm array
382 var $garbage_timeout;
385 $this->user = array();
387 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
388 $this->user[$i] = new User("", "");
390 for ($i = 0 ; $i < TABLES_N ; $i++)
391 $this->table[$i] = new Table();
392 $this->garbage_timeout = 0;
395 function garbage_manager($force)
398 /* Garbage collector degli utenti in timeout */
400 if ($force || $this->garbage_timeout < $curtime) {
402 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
403 $user_cur = &$this->user[$i];
404 if ($user_cur->sess == "")
407 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
408 log_rd2($user_cur->sess, "AUTO LOGOUT.");
410 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
411 log_auth($user_cur->sess, "Autologout session.");
413 $user_cur->sess = "";
414 $user_cur->name = "";
415 $user_cur->the_end = FALSE;
417 log_rd2($user_cur->sess, "AUTO LOGOUT.");
418 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
421 else if ($user_cur->subst == 'standup')
422 $this->room_outstandup(&$user_cur);
424 log_rd2($sess, "LOGOUT FROM WHAT ???");
428 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
429 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
430 $this->room_wakeup(&$user_cur);
431 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
432 $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);
437 log_rd2($user_cur->sess, "GARBAGE UPDATED!");
439 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
444 function room_wakeup(&$user)
446 $table_idx = $user->table;
447 $table = &$this->table[$table_idx];
449 log_main("WAKEUP", "begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
453 $from_table = ($user->stat == "table");
455 log_main("WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
456 $lockcalc = $table->exitlock_calc(&$this->user, $table_pos);
462 for ($i = 0 ; $i < $table->player_n ; $i++) {
463 $user_cur = &$this->user[$table->player[$i]];
464 log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
466 if ($user_cur != $user) {
467 $user_cur->stat = "room";
468 $user_cur->subst = "sitdown";
469 $user_cur->laccwr = $curtime;
471 else if ($user->sess != "") {
472 $user_cur->stat = "room";
473 $user_cur->subst = "standup";
474 $user_cur->laccwr = $curtime;
475 $user_cur->table = -1;
480 $user->stat = "room";
481 $user->subst = "standup";
482 $user->laccwr = $curtime;
484 /* aggiorna l'array dei giocatori al tavolo. */
485 $table->user_rem(&$this, &$user);
487 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
488 $user_cur = &$this->user[$i];
489 if ($user_cur->sess == '' || $user_cur->stat != 'room')
492 log_main("VALORI", "name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
494 $ret = "gst.st = ".($user_cur->step+1)."; ";
495 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
496 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
497 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
498 log_main("DOCUMENT.index.php", "from table");
500 else if ($user_cur->stat == "room") {
501 log_main("DOCUMENT.index.php", "from table");
503 $ret .= table_content($this, $user_cur, $table_idx);
504 $ret .= standup_content($this, $user_cur);
506 $act_content = table_act_content(FALSE, 0, $e, $user->table);
507 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
510 if ($user_cur == $user) {
511 // set the new status
512 $ret .= 'subst = "standup"; ';
513 // clean the action buttons in other tables
514 for ($e = 0 ; $e < TABLES_N ; $e++) {
515 if ($this->table[$e]->player_n < PLAYERS_N)
516 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
520 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
521 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
524 log_wr($user_cur->sess, "ROOM_WAKEUP: ".$ret);
525 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
533 function room_outstandup(&$user)
535 $this->room_sitdown(&$user, -1);
538 function table_update(&$user)
540 log_main("table_update", "pre - USER: ".$user->name);
542 $table_idx = $user->table;
545 $table = &$this->table[$table_idx];
547 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
549 $user_cur = &$this->user[$i];
550 if ($user_cur->sess == '' || $user_cur->stat != 'room')
553 $ret = "gst.st = ".($user_cur->step+1)."; ";
555 $ret .= table_content($this, $user_cur, $table_idx);
557 if ($user_cur == $user) {
558 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
560 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
564 log_main("table_update", "post");
567 function room_sitdown(&$user, $table_idx)
569 log_main("room_sitdown", ($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
572 $table = &$this->table[$table_idx];
574 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
576 $user_cur = &$this->user[$i];
577 if ($user_cur->sess == '' || $user_cur->stat != 'room')
580 $ret = "gst.st = ".($user_cur->step+1)."; ";
582 $ret .= table_content($this, $user_cur, $table_idx);
583 $ret .= standup_content($this, $user_cur);
585 if ($user_cur == $user) {
586 $ret .= 'subst = "sitdown"; ';
587 // clean the action buttons in other tables
588 for ($e = 0 ; $e < TABLES_N ; $e++) {
589 $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
590 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
593 else if ($table_idx > -1) {
594 if ($table->player_n == PLAYERS_N) {
595 $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
596 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
599 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
604 function chatt_send(&$user, $mesg)
606 if ($user->stat == 'table') {
607 $table = &$this->table[$user->table];
610 $user_mesg = substr($mesg,6);
612 $dt = date("H:i ",time());
613 if (strncmp($user_mesg, "/nick ", 6) == 0) {
614 log_main($user->sess, "chatt_send BEGIN");
616 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
617 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
618 $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));
623 $user_mesg = "COMMAND ".$user_mesg;
626 // update local graph
627 // update remote graphs
628 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
629 $user_cur = &$this->user[$i];
630 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
631 if ($user_cur->sess == '')
633 if ($user_cur->name == $name_new) {
634 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
635 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
640 if ($i == MAX_PLAYERS) {
641 $user->name = $name_new;
643 log_main($user->sess, "chatt_send start set");
646 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
647 log_main($user->sess, "chatt_send set loop");
649 $user_cur = &$this->user[$i];
650 if ($user_cur->sess == '')
652 if ($user_cur->stat == 'room') {
653 if ($user->stat == 'room' && $user->subst == 'standup') {
654 $this->standup_update(&$user);
656 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
657 $user->stat == 'table') {
658 log_main($user->sess, "chatt_send pre table update");
660 $this->table_update(&$user);
662 log_main($user->sess, "chatt_send post table update");
665 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
666 $table = &$this->table[$user->table];
668 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
669 $user_cur->comm[$user_cur->step % COMM_N] = sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
670 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
671 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
672 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
673 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
674 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
675 if ($user_cur == $user)
676 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
677 xcape($user->name,ENT_COMPAT,"UTF-8"));
684 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
685 if ($user->stat == 'room') {
686 $user_cur = &$this->user[$i];
687 if ($user_cur->sess == '' || $user_cur->stat != 'room')
691 $user_cur = &$this->user[$table->player[$i]];
694 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
695 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
696 $dt.xcape($user->name), xcape($user_mesg));
702 function &get_user($sess, &$idx)
706 if (strlen($sess) == SESS_LEN) {
707 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
708 if (strcmp($sess, $this->user[$i]->sess) == 0) {
711 return ($this->user[$i]);
714 log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
715 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
716 // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
719 log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
725 * function &add_user(&$bri, &$sess, &$idx, $name)
728 * if ($idx != -1 && ret == FALSE) => duplicated nick
729 * if ($idx == -2 && ret == FALSE) => invalid name
730 * if ($idx == -1 && ret == FALSE) => no space left
731 * if (ret == TRUE) => SUCCESS
733 function &add_user(&$sess, &$idx, $name)
738 if (($name_new = validate_name($name)) == FALSE) {
743 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
744 if (validate_sess($sess) == FALSE)
747 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
749 if (strcmp($sess, $this->user[$i]->sess) == 0) {
753 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
756 if (strcmp($this->user[$i]->name, $name_new) == 0) {
764 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
766 if ($idx != -1 && $i == MAX_PLAYERS) {
769 $this->user[$idx]->sess = uniqid("");
770 $sess = $this->user[$idx]->sess;
774 $this->user[$idx]->sess = $sess;
776 $this->user[$idx]->name = $name_new;
777 $this->user[$idx]->stat = "room";
778 $this->user[$idx]->subst = "standup";
779 $this->user[$idx]->laccwr = time();
781 log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
783 return ($this->user[$idx]);
789 function standup_update(&$user)
791 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
792 $user_cur = &$this->user[$i];
793 if ($user_cur->sess == '')
796 log_main("STANDUP START", $user_cur->stat);
798 if ($user_cur->stat == 'room') {
799 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
800 if ($user_cur == $user)
801 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
803 log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
811 } // end class brisco
815 list($usec, $sec) = explode(' ', microtime());
816 return (float) $sec + ((float) $usec * 100000);
819 function log_main($sess, $log) {
820 if (BRISK_DEBUG != TRUE)
823 $fp = fopen("/tmp/brisk_main.log", 'a');
824 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
828 function log_rd($sess, $log) {
829 if (BRISK_DEBUG != TRUE)
832 $fp = fopen("/tmp/brisk_rd.log", 'a');
833 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
837 function log_rd2($sess, $log) {
838 if (BRISK_DEBUG != TRUE)
841 $fp = fopen("/tmp/brisk_rd2.log", 'a');
842 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
846 function log_send($sess, $log) {
847 if (BRISK_DEBUG != TRUE)
850 $fp = fopen("/tmp/brisk_send.log", 'a');
851 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
855 function log_auth($sess, $log) {
856 if (BRISK_DEBUG != TRUE)
859 $fp = fopen("/tmp/brisk_auth.log", 'a');
860 fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
864 function log_wr($sess, $log) {
865 if (BRISK_DEBUG != TRUE)
868 $fp = fopen("/tmp/brisk_wr.log", 'a');
869 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
873 function log_load($sess, $log) {
874 if (BRISK_DEBUG != TRUE)
877 $fp = fopen("/tmp/brisk_load.log", 'a');
878 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
884 $brisco = new brisco();
891 // echo "LOCK: ".FTOK_PATH."/main";
893 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
897 // echo "FTOK ".$tok."<br>";
898 if (($res = sem_get($tok)) == FALSE) {
899 echo "SEM_GET FAILED";
902 if (sem_acquire($res))
908 function unlock_data($res)
910 return (sem_release($res));
914 function &load_data()
916 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
921 if ($shm = shm_attach($tok,100000 * TABLES_N)) {
922 if(($bri = @shm_get_var($shm, $tok)) == false) {
923 log_main("XXX", "INIT MAIN DATA");
926 shm_put_var($shm, $tok, $bri);
938 function save_data(&$bri)
946 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1)
952 if (($shm = shm_attach($tok,100000 * TABLES_N)) == FALSE)
955 if (shm_put_var($shm, $tok, $bri) == FALSE)
957 // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
967 function table_act_content($isstanding, $sitted, $table, $cur_table)
972 if ($sitted < PLAYERS_N) {
973 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
977 if ($table == $cur_table)
978 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
985 function table_content($bri, $user, $table_idx)
991 // Si possono usare i dati nella classe table
995 $table = &$bri->table[$table_idx];
997 if ($user->stat != 'room')
1000 for ($i = 0 ; $i < $table->player_n ; $i++) {
1001 $user_cur = &$bri->user[$table->player[$i]];
1003 if ($user_cur == $user)
1004 { $hilion = "<b>"; $hilioff = "</b>"; }
1006 { $hilion = ""; $hilioff = ""; }
1008 log_main($bri->user[$e]->name, sprintf("IN TABLE [%d]", $table_idx));
1010 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
1013 for ( ; $i < PLAYERS_N ; $i++)
1017 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
1022 function standup_content(&$bri, $user)
1027 if ($user->stat != 'room')
1030 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
1031 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
1036 $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
1038 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
1039 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
1043 if ($bri->user[$e]->subst == "standup") {
1044 if (($ct % 4) == 0) {
1047 if ($bri->user[$e] == $user)
1048 { $hilion = "<b>"; $hilioff = "</b>"; }
1050 { $hilion = ""; $hilioff = ""; }
1052 $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($bri->user[$e]->name), $hilioff);
1053 if (($ct % 4) == 3) {
1054 $content .= '</tr>';
1059 $content .= '</table>';
1061 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
1062 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
1063 $content, $content2);
1069 function show_notify($text, $tout, $butt, $w, $h)
1071 log_main("SHOW_NOTIFY", $text);
1072 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
1075 function briscola_show($bri, $table, $user)
1080 if ($table->asta_card == 9)
1081 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1083 /* text of caller cell */
1084 if ($user->table_pos == $table->asta_win)
1085 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1087 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
1088 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1090 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1092 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1093 $ret .= sprintf('$("caller").style.visibility = "visible";');
1094 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1095 $ret .= sprintf('$("astalascio").style.visibility = "";');
1096 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1097 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1103 function game_result($asta_pnt, $pnt)
1105 if ($asta_pnt == 61) {
1108 else if ($pnt == 60)
1114 if ($pnt >= $asta_pnt)
1121 function multoval($mult)
1125 else if ($mult == 4)
1126 return ("quadruplo");
1128 return (sprintf("%d-plo", $mult));
1131 function show_table_info(&$bri, &$table, $table_pos)
1133 $user = &$bri->user[$table->player[$table_pos]];
1135 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1136 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1139 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1140 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1141 $noty .= sprintf("</tr>");
1144 log_main("show_table_info", "pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1146 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1147 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1148 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1149 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1154 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1155 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1156 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1157 $noty .= "</tr></table>";
1159 if ($table->old_reason != "") {
1160 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1163 if ($table->old_win != -1) {
1164 $win = $table->player[$table->old_win];
1165 $fri = $table->player[$table->old_friend];
1167 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1170 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
1171 xcape($bri->user[$win]->name),
1172 xcape($bri->user[$fri]->name));
1173 if ($table->old_pnt == 120) {
1174 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1177 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1178 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1179 'più di 60'), $table->old_pnt,
1180 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1184 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1185 xcape($bri->user[$win]->name));
1186 if ($table->old_pnt == 120) {
1187 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1190 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1191 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1192 'più di 60'), $table->old_pnt,
1193 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1197 if ($table->mazzo == $table_pos)
1198 $noty .= "Fai <b>tu</b> il mazzo,";
1200 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1201 $noty .= "Il mazzo a <b>$unam</b>,";
1204 if ($user->subst == 'asta') {
1205 if ($table->asta_win == -1) // auction case
1206 $curplayer = $table->gstart % PLAYERS_N;
1208 $curplayer = $table->asta_win;
1210 else if ($user->subst == 'game') {
1211 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
1215 if ($curplayer == $table_pos) {
1216 $noty .= " tocca a <b>te</b> giocare.";
1219 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1220 $noty .= " tocca a <b>$unam</b> giocare.";
1223 if ($table->mult > 1) {
1224 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1226 $noty .= "<hr><br>";
1228 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
1233 function root_wellcome($user)
1235 GLOBAL $root_wellarr;
1237 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
1238 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $root_wellarr[$i]));
1243 function table_wellcome($user)
1245 GLOBAL $table_wellarr;
1247 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1248 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
1253 function show_room(&$bri, &$user)
1255 $ret .= sprintf('gst.st = %d;', $user->step);
1256 $ret .= sprintf('stat = "%s";', $user->stat);
1258 $ret .= root_wellcome($user);
1259 $ret .= sprintf('subst = "%s";', $user->subst);
1260 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1261 for ($i = 0 ; $i < TABLES_N ; $i++) {
1262 $ret .= table_content($bri, $user, $i);
1263 $act_content = table_act_content(($user->subst == 'standup'),
1264 $bri->table[$i]->player_n, $i, $user->table);
1265 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1267 $ret .= standup_content($bri, $user);
1275 is_transition (is from room to table ?)
1276 is_again (is another game)
1278 Examples of $is_transition, $is_again:
1279 from reload of the page: FALSE, FALSE
1280 from sitdown in room: TRUE, FALSE
1281 from table: asta cmd e tutti passano: TRUE, TRUE
1282 from table: fine partita: TRUE, TRUE
1284 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1286 $table_idx = $user->table;
1287 $table = &$bri->table[$table_idx];
1288 $table_pos = $user->table_pos;
1290 $ret = "table_init();";
1291 $ret .= $table->exitlock_show(&$bri->user, $table_pos);
1293 /* GENERAL STATUS */
1294 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1295 $sendstep, $user->stat, $user->subst, $table_pos);
1297 $ret .= "background_set();";
1300 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1301 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1302 xcape($bri->user[$table->player[($table_pos)%PLAYERS_N]]->name),
1303 xcape($bri->user[$table->player[($table_pos+1)%PLAYERS_N]]->name),
1304 xcape($bri->user[$table->player[($table_pos+2)%PLAYERS_N]]->name),
1305 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+3)%PLAYERS_N]]->name)),
1306 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+4)%PLAYERS_N]]->name)));
1308 /* NOTIFY FOR THE CARD MAKER */
1309 if ($is_transition) { // && $user->subst == "asta" superfluo
1310 $ret .= show_table_info(&$bri, &$table, $table_pos);
1313 $ret .= table_wellcome($user);
1315 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1316 $ret .= playsound("cow.mp3");
1321 if ($is_transition) { // && $user->subst == "asta" superfluo
1324 for ($i = 0 ; $i < 8 ; $i++) {
1325 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1327 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1328 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1334 log_rd($sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1336 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
1337 $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1338 $table->card[$o]->value : -1),
1339 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1344 $taked = array(0,0,0,0,0);
1345 $inhand = array(0,0,0,0,0);
1346 $ontabl = array(-1,-1,-1,-1,-1);
1349 for ($i = 0 ; $i < 40 ; $i++) {
1350 if ($table->card[$i]->stat == 'hand') {
1351 if ($table->card[$i]->owner == $table_pos) {
1352 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1354 $inhand[$table->card[$i]->owner]++;
1356 else if ($table->card[$i]->stat == 'take') {
1357 log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1358 $taked[$table->card[$i]->owner]++;
1360 else if ($table->card[$i]->stat == 'table') {
1361 $ontabl[$table->card[$i]->owner] = $i;
1365 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1366 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1368 log_main("Stat table:", $logg);
1370 /* Set ours cards. */
1372 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1373 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1374 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
1375 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1376 $ret .= sprintf('card_setours(%s);', $oursarg);
1378 /* Dispose all cards */
1379 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1380 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1381 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1382 $inhand[$i], $taked[$i]);
1384 if ($ontabl[$i] != -1) {
1385 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1386 $table->card[$ontabl[$i]]->value,
1387 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1393 if ($user->subst == 'asta') {
1395 /* show users auction status */
1397 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1398 $user_cur = &$bri->user[$table->player[$i]];
1399 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1400 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1403 $showst .= ",-2,-2";
1404 $ret .= sprintf('show_astat(%s);', $showst);
1406 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1407 /* show card chooser */
1408 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1413 if ($table_pos == ($table->gstart % PLAYERS_N) &&
1414 $table->asta_win == -1)
1415 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1416 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1418 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1419 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1423 if ($table->asta_win == -1) { // auction case
1424 if ($table_pos == ($table->gstart % PLAYERS_N))
1425 $ret .= "remark_on();";
1427 $ret .= "remark_off();";
1429 else { // chooseed case
1430 if ($table_pos == $table->asta_win)
1431 $ret .= "remark_on();";
1433 $ret .= "remark_off();";
1436 else if ($user->subst == 'game') {
1438 if (($table->gstart + $table->turn) % PLAYERS_N == $table_pos)
1439 $ret .= "is_my_time = true; remark_on();";
1441 $ret .= "remark_off();";
1443 /* WHO CALL AND WATH */
1444 $ret .= briscola_show($bri, $table, $user);
1450 function calculate_winner(&$table)
1457 $cur_seed = $table->briscola - ($table->briscola % 10);
1459 for ($i = 0 ; $i < 40 ; $i++) {
1460 if ($table->card[$i]->stat != "table")
1463 log_wr($sess, sprintf("Card On table: [%d]", $i));
1465 $v = $table->card[$i]->value;
1466 $ontab[$table->card[$i]->owner] = $v;
1467 $ontid[$table->card[$i]->owner] = $i;
1468 /* se briscola setto il flag */
1469 if (($v - ($v % 10)) == $cur_seed)
1473 if ($briontab == FALSE) {
1474 $cur_win = $table->gstart;
1475 $cur_val = $ontab[$cur_win];
1476 $cur_seed = $cur_val - ($cur_val % 10);
1479 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1480 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1481 if ($ontab[$i] < $cur_val) {
1482 $cur_val = $ontab[$i];
1488 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1489 $table->card[$ontid[$i]]->owner = $cur_win;
1490 $table->card[$ontid[$i]]->stat = "take";
1495 function calculate_points(&$table)
1497 GLOBAL $G_all_points;
1501 if ($table->asta_pnt == 60)
1502 $table->asta_pnt = 61;
1504 $table->old_reason = "";
1505 $table->old_win = $table->asta_win;
1506 $table->old_friend = $table->friend;
1507 $table->old_asta_pnt = $table->asta_pnt;
1509 for ($i = 0 ; $i < 40 ; $i++) {
1510 $ctt = $table->card[$i]->value % 10;
1511 $own = $table->card[$i]->owner;
1512 if ($own == $table->asta_win || $own == $table->friend)
1513 $pro += $G_all_points[$ctt];
1516 log_wr("XXX", sprintf("PRO: [%d]", $pro));
1519 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1520 $table->points[$table->points_n % MAX_POINTS] = array();
1521 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1522 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1524 $table->old_pnt = $pro;
1530 if ($pro >= $table->asta_pnt)
1535 $table->points[$table->points_n % MAX_POINTS] = array();
1536 for ($i = 0 ; $i < 5 ; $i++) {
1537 if ($i == $table->asta_win)
1538 $pt = ($i == $table->friend ? 4 : 2);
1539 else if ($i == $table->friend)
1544 log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1546 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1548 log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1550 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1551 $table->total[$i] += $pt;
1554 $table->old_pnt = $pro;
1558 function validate_sess($sess)
1560 if (strlen($sess) == SESS_LEN)
1566 function validate_name($name)
1568 $name_new = str_replace(' ', '_', substr(trim($name),0,12));
1570 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
1572 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
1579 function playsound($filename)
1581 return (sprintf('playsound("flasou", "%s");', $filename));