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_brisk_version = "0.4.6";
40 $root_wellarr = Array ( 'Benvenuto in brisk (Ver. '.$G_brisk_version.').',
41 'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
42 $table_wellarr = Array ( 'Benvenuto al tavolo.');
46 <div style=\\"text-align: left; padding: 8px;\\">
47 <b>Descrizione</b><br>
48 Questa è un\'implementazione della briscola in cinque, così come è spiegata su
49 <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>
50 <b>Uso del sito</b><br>
51 Potete sedervi a un tavolo o rimanere in piedi.<br>
52 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
55 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>
56 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
57 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
58 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno quadrato al centro del vostro schermo.<br><br>
59 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
61 <dt><b>Comandi della chat</b>
62 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
63 <dd>.. to be continue ..
69 <div id=\\"header\\" class=\\"header\\">
70 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
71 briscola chiamata in salsa ajax
73 <br><b>version '.$G_brisk_version.'</b><br><br>
74 Copyright 2006-2007 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
79 $from = array ( '\\', '@', '|' );
80 $to = array ( '\\\\', '@', '¦' );
82 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
87 var $value; /* 0 - 39 card value */
88 var $stat; /* 'bunch', 'hand', 'table', 'take' */
89 var $owner; /* (table position 0-4) */
90 // var $pos; /* Pos in hand. */
91 var $x; /* When played the X position on the table of the owner. */
92 var $y; /* When played the Y position on the table of the owner. */
94 function Card($value, $stat, $owner)
96 $this->value = $value;
98 $this->owner = $owner;
101 function assign($stat,$owner)
104 $this->owner = $owner;
107 function setpos($pos)
114 $this->stat = 'table';
119 function take($newown)
121 $this->stat = 'take';
122 $this->owner = $newown;
140 var $points; // points array
141 var $points_n; // number of row of points
155 $this->player = array();
157 $this->card = &$this->bunch_create();
158 $this->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
159 $this->asta_pla_n= -1;
160 $this->asta_card = -1;
161 $this->asta_pnt = -1;
163 $this->points = array( );
165 $this->total = array( 0, 0, 0, 0, 0);
166 $this->asta_win = -1;
167 $this->briscola = -1;
170 $this->old_asta_pnt = -1;
173 $this->old_friend= -1;
177 function &bunch_create()
181 for ($i = 0 ; $i < 40 ; $i++) {
182 $ret[$i] = new Card($i, 'bunch', 'no_owner');
188 function bunch_make()
190 $ct = array(0,0,0,0,0);
192 mt_srand(make_seed());
194 for ($i = 39 ; $i >= 0 ; $i--)
197 for ($i = 39 ; $i >= 0 ; $i--) {
201 log_main("RND ZERO", "");
206 $this->card[$id]->assign('hand', $owner);
208 $rest[$rn] = $rest[$i];
209 $pubbpos[$rn2] = $pubbpos[$i];
215 $this->mazzo = rand(0,PLAYERS_N-1);
219 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
220 $this->total[$i] = 0;
223 log_main("table::init","ci siamo");
226 function game_init(&$bri)
228 log_rd2($sess,"GSTART 4");
230 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
234 $this->asta_pla_n = PLAYERS_N;
235 $this->asta_card = -1;
236 $this->asta_pnt = 60;
237 $this->asta_win = -1;
238 $this->briscola = -1;
242 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
243 $this->asta_pla[$i] = TRUE;
244 $user_cur = &$bri->user[$this->player[$i]];
245 $user_cur->subst = 'asta';
246 $user_cur->asta_card = -2;
247 $user_cur->asta_pnt = -1;
253 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
256 function getPlayer($idx)
258 return ($this->player[$idx]);
261 function setPlayer($idx, $player)
263 $this->player[$idx] = $player;
266 function user_add($idx)
268 $this->player[$this->player_n] = $idx;
271 return ($this->player_n - 1);
274 function user_rem(&$bri, &$user)
276 $tabpos = $user->table_pos;
278 /* verifico la consistenza dei dati */
279 if ($bri->user[$this->player[$tabpos]] == $user) {
281 /* aggiorna l'array dei giocatori al tavolo. */
282 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
283 $this->player[$i] = $this->player[$i+1];
284 $user_cur = &$bri->user[$this->player[$i]];
285 $user_cur->table_pos = $i;
290 log_main($user->sess, "INCONSISTENCY ON TABLE.");
297 var $name; // name of the user
298 var $sess; // session of the user
299 var $lacc; // last access (for the cleanup)
300 var $laccwr; // last access (for the cleanup)
301 var $stat; // status (outdoor, room, table, game, ...)
302 var $subst; // substatus for each status
303 var $step; // step of the current status
304 var $trans_step; // step to enable transition between pages (disable == -1)
305 var $comm; // commands array
309 var $table; // id of the current table (if in table state)
310 var $table_pos; // idx on the table
312 function User($name, $sess, $stat = "", $subst = "", $table = -1) {
315 $this->lacc = time();
316 $this->laccwr = time();
318 $this->subst = $subst;
320 $this->trans_step = -1;
321 $this->comm = array();
322 $this->asta_card = -2;
323 $this->asta_pnt = -1;
325 $this->table = $table;
332 var $comm; // commands for many people
333 var $step; // current step of the comm array
334 var $garbage_timeout;
337 $this->user = array();
339 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
340 $this->user[$i] = new User("", "");
342 for ($i = 0 ; $i < TABLES_N ; $i++)
343 $this->table[$i] = new Table();
344 $this->garbage_timeout = 0;
347 function garbage_manager($force)
350 /* Garbage collector degli utenti in timeout */
352 if ($force || $this->garbage_timeout < $curtime) {
354 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
355 $user_cur = &$this->user[$i];
356 if ($user_cur->sess == "")
359 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
360 log_rd2($user_cur->sess, "AUTO LOGOUT.");
362 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
363 log_auth($user_cur->sess, "Autologout session.");
365 $user_cur->sess = "";
366 $user_cur->name = "";
367 $user_cur->the_end = FALSE;
369 log_rd2($user_cur->sess, "AUTO LOGOUT.");
370 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
371 $this->room_wakeup(&$user_cur);
372 else if ($user_cur->subst == 'standup')
373 $this->room_outstandup(&$user_cur);
375 log_rd2($sess, "LOGOUT FROM WHAT ???");
379 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
380 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
381 $this->room_wakeup(&$user_cur);
382 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
383 $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);
388 log_rd2($user_cur->sess, "GARBAGE UPDATED!");
390 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
395 function room_wakeup(&$user)
397 $table_idx = $user->table;
398 $table = &$this->table[$table_idx];
400 log_main("WAKEUP", "begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
404 $from_table = ($user->stat == "table");
406 log_main("WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
407 for ($i = 0 ; $i < $table->player_n ; $i++) {
408 $user_cur = &$this->user[$table->player[$i]];
409 log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
411 if ($user_cur != $user) {
412 $user_cur->stat = "room";
413 $user_cur->subst = "sitdown";
414 $user_cur->laccwr = $curtime;
416 else if ($user->sess != "") {
417 $user_cur->stat = "room";
418 $user_cur->subst = "standup";
419 $user_cur->laccwr = $curtime;
420 $user_cur->table = -1;
425 $user->stat = "room";
426 $user->subst = "standup";
427 $user->laccwr = $curtime;
429 /* aggiorna l'array dei giocatori al tavolo. */
430 $table->user_rem(&$this, &$user);
432 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
433 $user_cur = &$this->user[$i];
434 if ($user_cur->sess == '' || $user_cur->stat != 'room')
437 log_main("VALORI", "name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
439 $ret = "gst.st = ".($user_cur->step+1)."; ";
440 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
441 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
442 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
443 log_main("DOCUMENT.index.php", "from table");
445 else if ($user_cur->stat == "room") {
446 log_main("DOCUMENT.index.php", "from table");
448 $ret .= table_content($this, $user_cur, $table_idx);
449 $ret .= standup_content($this, $user_cur);
451 $act_content = table_act_content(FALSE, 0, $e, $user->table);
452 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
455 if ($user_cur == $user) {
456 // set the new status
457 $ret .= 'subst = "standup"; ';
458 // clean the action buttons in other tables
459 for ($e = 0 ; $e < TABLES_N ; $e++) {
460 if ($this->table[$e]->player_n < PLAYERS_N)
461 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
465 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
466 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
469 log_wr($user_cur->sess, "ROOM_WAKEUP: ".$ret);
470 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
478 function room_outstandup(&$user)
480 $this->room_sitdown(&$user, -1);
483 function table_update(&$user)
485 log_main("table_update", "pre - USER: ".$user->name);
487 $table_idx = $user->table;
490 $table = &$this->table[$table_idx];
492 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
494 $user_cur = &$this->user[$i];
495 if ($user_cur->sess == '' || $user_cur->stat != 'room')
498 $ret = "gst.st = ".($user_cur->step+1)."; ";
500 $ret .= table_content($this, $user_cur, $table_idx);
502 if ($user_cur == $user) {
503 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
505 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
509 log_main("table_update", "post");
512 function room_sitdown(&$user, $table_idx)
514 log_main("room_sitdown", ($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
517 $table = &$this->table[$table_idx];
519 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
521 $user_cur = &$this->user[$i];
522 if ($user_cur->sess == '' || $user_cur->stat != 'room')
525 $ret = "gst.st = ".($user_cur->step+1)."; ";
527 $ret .= table_content($this, $user_cur, $table_idx);
528 $ret .= standup_content($this, $user_cur);
530 if ($user_cur == $user) {
531 $ret .= 'subst = "sitdown"; ';
532 // clean the action buttons in other tables
533 for ($e = 0 ; $e < TABLES_N ; $e++) {
534 $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
535 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
538 else if ($table_idx > -1) {
539 if ($table->player_n == PLAYERS_N) {
540 $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
541 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
544 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
549 function chatt_send(&$user, $mesg)
551 if ($user->stat == 'table') {
552 $table = &$this->table[$user->table];
555 $user_mesg = substr($mesg,6);
557 $dt = date("H:i ",time());
558 if (strncmp($user_mesg, "/nick ", 6) == 0) {
559 log_main($user->sess, "chatt_send BEGIN");
561 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
562 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
563 $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));
568 $user_mesg = "COMMAND ".$user_mesg;
571 // update local graph
572 // update remote graphs
573 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
574 $user_cur = &$this->user[$i];
575 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
576 if ($user_cur->sess == '')
578 if ($user_cur->name == $name_new) {
579 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
580 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
585 if ($i == MAX_PLAYERS) {
586 $user->name = $name_new;
588 log_main($user->sess, "chatt_send start set");
591 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
592 log_main($user->sess, "chatt_send set loop");
594 $user_cur = &$this->user[$i];
595 if ($user_cur->sess == '')
597 if ($user_cur->stat == 'room') {
598 if ($user->stat == 'room' && $user->subst == 'standup') {
599 $this->standup_update(&$user);
601 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
602 $user->stat == 'table') {
603 log_main($user->sess, "chatt_send pre table update");
605 $this->table_update(&$user);
607 log_main($user->sess, "chatt_send post table update");
610 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
611 $table = &$this->table[$user->table];
613 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
614 $user_cur->comm[$user_cur->step % COMM_N] = sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
615 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
616 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
617 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
618 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
619 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
620 if ($user_cur == $user)
621 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
622 xcape($user->name,ENT_COMPAT,"UTF-8"));
629 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
630 if ($user->stat == 'room') {
631 $user_cur = &$this->user[$i];
632 if ($user_cur->sess == '' || $user_cur->stat != 'room')
636 $user_cur = &$this->user[$table->player[$i]];
639 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
640 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
641 $dt.xcape($user->name), xcape($user_mesg));
647 function &get_user($sess, &$idx)
651 if (strlen($sess) == SESS_LEN) {
652 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
653 if (strcmp($sess, $this->user[$i]->sess) == 0) {
656 return ($this->user[$i]);
659 log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
660 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
661 // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
664 log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
670 * function &add_user(&$bri, &$sess, &$idx, $name)
673 * if ($idx != -1 && ret == FALSE) => duplicated nick
674 * if ($idx == -2 && ret == FALSE) => invalid name
675 * if ($idx == -1 && ret == FALSE) => no space left
676 * if (ret == TRUE) => SUCCESS
678 function &add_user(&$sess, &$idx, $name)
683 if (($name_new = validate_name($name)) == FALSE) {
688 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
689 if (validate_sess($sess) == FALSE)
692 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
694 if (strcmp($sess, $this->user[$i]->sess) == 0) {
698 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
701 if (strcmp($this->user[$i]->name, $name_new) == 0) {
709 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
711 if ($idx != -1 && $i == MAX_PLAYERS) {
714 $this->user[$idx]->sess = uniqid("");
715 $sess = $this->user[$idx]->sess;
719 $this->user[$idx]->sess = $sess;
721 $this->user[$idx]->name = $name_new;
722 $this->user[$idx]->stat = "room";
723 $this->user[$idx]->subst = "standup";
724 $this->user[$idx]->laccwr = time();
726 log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
728 return ($this->user[$idx]);
734 function standup_update(&$user)
736 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
737 $user_cur = &$this->user[$i];
738 if ($user_cur->sess == '')
741 log_main("STANDUP START", $user_cur->stat);
743 if ($user_cur->stat == 'room') {
744 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
745 if ($user_cur == $user)
746 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
748 log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
756 } // end class brisco
760 list($usec, $sec) = explode(' ', microtime());
761 return (float) $sec + ((float) $usec * 100000);
764 function log_main($sess, $log) {
765 if (BRISK_DEBUG != TRUE)
768 $fp = fopen("/tmp/brisk_main.log", 'a');
769 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
773 function log_rd($sess, $log) {
774 if (BRISK_DEBUG != TRUE)
777 $fp = fopen("/tmp/brisk_rd.log", 'a');
778 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
782 function log_rd2($sess, $log) {
783 if (BRISK_DEBUG != TRUE)
786 $fp = fopen("/tmp/brisk_rd2.log", 'a');
787 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
791 function log_send($sess, $log) {
792 if (BRISK_DEBUG != TRUE)
795 $fp = fopen("/tmp/brisk_send.log", 'a');
796 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
800 function log_auth($sess, $log) {
801 if (BRISK_DEBUG != TRUE)
804 $fp = fopen("/tmp/brisk_auth.log", 'a');
805 fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
809 function log_wr($sess, $log) {
810 if (BRISK_DEBUG != TRUE)
813 $fp = fopen("/tmp/brisk_wr.log", 'a');
814 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
818 function log_load($sess, $log) {
819 if (BRISK_DEBUG != TRUE)
822 $fp = fopen("/tmp/brisk_load.log", 'a');
823 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
829 $brisco = new brisco();
836 // echo "LOCK: ".FTOK_PATH."/main";
838 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
842 // echo "FTOK ".$tok."<br>";
843 if (($res = sem_get($tok)) == FALSE) {
844 echo "SEM_GET FAILED";
847 if (sem_acquire($res))
853 function unlock_data($res)
855 return (sem_release($res));
859 function &load_data()
861 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
866 if ($shm = shm_attach($tok,100000 * TABLES_N)) {
867 if(($bri = @shm_get_var($shm, $tok)) == false) {
868 log_main("XXX", "INIT MAIN DATA");
871 shm_put_var($shm, $tok, $bri);
883 function save_data(&$bri)
891 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1)
897 if (($shm = shm_attach($tok,100000 * TABLES_N)) == FALSE)
900 if (shm_put_var($shm, $tok, $bri) == FALSE)
902 // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
912 function table_act_content($isstanding, $sitted, $table, $cur_table)
917 if ($sitted < PLAYERS_N) {
918 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
922 if ($table == $cur_table)
923 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
930 function table_content($bri, $user, $table_idx)
936 // Si possono usare i dati nella classe table
940 $table = &$bri->table[$table_idx];
942 if ($user->stat != 'room')
945 for ($i = 0 ; $i < $table->player_n ; $i++) {
946 $user_cur = &$bri->user[$table->player[$i]];
948 if ($user_cur == $user)
949 { $hilion = "<b>"; $hilioff = "</b>"; }
951 { $hilion = ""; $hilioff = ""; }
953 log_main($bri->user[$e]->name, sprintf("IN TABLE [%d]", $table_idx));
955 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
958 for ( ; $i < PLAYERS_N ; $i++)
962 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
967 function standup_content(&$bri, $user)
972 if ($user->stat != 'room')
975 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
976 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
981 $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
983 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
984 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
988 if ($bri->user[$e]->subst == "standup") {
989 if (($ct % 4) == 0) {
992 if ($bri->user[$e] == $user)
993 { $hilion = "<b>"; $hilioff = "</b>"; }
995 { $hilion = ""; $hilioff = ""; }
997 $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($bri->user[$e]->name), $hilioff);
998 if (($ct % 4) == 3) {
1004 $content .= '</table>';
1006 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
1007 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
1008 $content, $content2);
1014 function show_notify($text, $tout, $butt, $w, $h)
1016 log_main("SHOW_NOTIFY", $text);
1017 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
1020 function briscola_show($bri, $table, $user)
1025 if ($table->asta_card == 9)
1026 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1028 /* text of caller cell */
1029 if ($user->table_pos == $table->asta_win)
1030 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1032 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
1033 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1035 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1037 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1038 $ret .= sprintf('$("caller").style.visibility = "visible";');
1039 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1040 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1041 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1047 function game_result($asta_pnt, $pnt)
1049 if ($asta_pnt == 61) {
1052 else if ($pnt == 60)
1058 if ($pnt >= $asta_pnt)
1065 function multoval($mult)
1069 else if ($mult == 4)
1070 return ("quadruplo");
1072 return (sprintf("%d-plo", $mult));
1075 function show_table_info(&$bri, &$table, $table_pos)
1077 $user = &$bri->user[$table->player[$table_pos]];
1079 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1080 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1083 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1084 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1085 $noty .= sprintf("</tr>");
1088 log_main("show_table_info", "pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1090 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1091 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1092 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1093 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1098 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1099 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1100 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1101 $noty .= "</tr></table>";
1103 if ($table->old_win != -1) {
1104 $win = $table->player[$table->old_win];
1105 $fri = $table->player[$table->old_friend];
1107 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1110 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
1111 xcape($bri->user[$win]->name),
1112 xcape($bri->user[$fri]->name));
1113 if ($table->old_pnt == 120) {
1114 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1117 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1118 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1119 'più di 60'), $table->old_pnt,
1120 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1124 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1125 xcape($bri->user[$win]->name));
1126 if ($table->old_pnt == 120) {
1127 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1130 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1131 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1132 'più di 60'), $table->old_pnt,
1133 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1137 if ($table->mazzo == $table_pos)
1138 $noty .= "Fai <b>tu</b> il mazzo,";
1140 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1141 $noty .= "Il mazzo a <b>$unam</b>,";
1144 if ($user->subst == 'asta') {
1145 if ($table->asta_win == -1) // auction case
1146 $curplayer = $table->gstart % PLAYERS_N;
1148 $curplayer = $table->asta_win;
1150 else if ($user->subst == 'game') {
1151 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
1155 if ($curplayer == $table_pos) {
1156 $noty .= " tocca a <b>te</b> giocare.";
1159 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1160 $noty .= " tocca a <b>$unam</b> giocare.";
1163 if ($table->mult > 1) {
1164 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1166 $noty .= "<hr><br>";
1168 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
1173 function root_wellcome($user)
1175 GLOBAL $root_wellarr;
1177 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
1178 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $root_wellarr[$i]));
1183 function table_wellcome($user)
1185 GLOBAL $table_wellarr;
1187 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1188 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
1193 function show_room(&$bri, &$user)
1195 $ret .= sprintf('gst.st = %d;', $user->step);
1196 $ret .= sprintf('stat = "%s";', $user->stat);
1198 $ret .= root_wellcome($user);
1199 $ret .= sprintf('subst = "%s";', $user->subst);
1200 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1201 for ($i = 0 ; $i < TABLES_N ; $i++) {
1202 $ret .= table_content($bri, $user, $i);
1203 $act_content = table_act_content(($user->subst == 'standup'),
1204 $bri->table[$i]->player_n, $i, $user->table);
1205 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1207 $ret .= standup_content($bri, $user);
1215 is_transition (is from room to table ?)
1216 is_again (is another game)
1218 Examles of $is_transition, $is_again:
1219 from reload of the page: FALSE, FALSE
1220 from sitdown in room: TRUE, FALSE
1221 from table: asta cmd e tutti passano: TRUE, TRUE
1222 from table: fine partita: TRUE, TRUE
1224 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1226 $table_idx = $user->table;
1227 $table = &$bri->table[$table_idx];
1236 cards dnd (and gameable card if its turn)
1241 $ret = "table_init();";
1244 /* GENERAL STATUS */
1245 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1246 $sendstep, $user->stat, $user->subst, $user->table_pos);
1248 $ret .= "background_set();";
1251 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1252 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1253 xcape($bri->user[$table->player[($user->table_pos)%PLAYERS_N]]->name),
1254 xcape($bri->user[$table->player[($user->table_pos+1)%PLAYERS_N]]->name),
1255 xcape($bri->user[$table->player[($user->table_pos+2)%PLAYERS_N]]->name),
1256 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($user->table_pos+3)%PLAYERS_N]]->name)),
1257 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($user->table_pos+4)%PLAYERS_N]]->name)));
1259 /* NOTIFY FOR THE CARD MAKER */
1260 if ($is_transition) { // && $user->subst == "asta" superfluo
1261 $ret .= show_table_info(&$bri, &$table, $user->table_pos);
1264 $ret .= table_wellcome($user);
1266 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1267 $ret .= playsound("cow.mp3");
1272 if ($is_transition) { // && $user->subst == "asta" superfluo
1275 for ($i = 0 ; $i < 8 ; $i++) {
1276 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1278 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1279 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1285 log_rd($sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1287 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
1288 $i, ((($e + PLAYERS_N - $user->table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1289 $table->card[$o]->value : -1),
1290 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1295 $taked = array(0,0,0,0,0);
1296 $inhand = array(0,0,0,0,0);
1297 $ontabl = array(-1,-1,-1,-1,-1);
1300 for ($i = 0 ; $i < 40 ; $i++) {
1301 if ($table->card[$i]->stat == 'hand') {
1302 if ($table->card[$i]->owner == $user->table_pos) {
1303 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1305 $inhand[$table->card[$i]->owner]++;
1307 else if ($table->card[$i]->stat == 'take') {
1308 log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1309 $taked[$table->card[$i]->owner]++;
1311 else if ($table->card[$i]->stat == 'table') {
1312 $ontabl[$table->card[$i]->owner] = $i;
1316 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1317 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1319 log_main("Stat table:", $logg);
1321 /* Set ours cards. */
1323 for ($i = 0 ; $i < $inhand[$user->table_pos] ; $i++)
1324 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1325 for ($i = $inhand[$user->table_pos] ; $i < 8 ; $i++)
1326 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1327 $ret .= sprintf('card_setours(%s);', $oursarg);
1329 /* Dispose all cards */
1330 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1331 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1332 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1333 $inhand[$i], $taked[$i]);
1335 if ($ontabl[$i] != -1) {
1336 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1337 $table->card[$ontabl[$i]]->value,
1338 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1344 if ($user->subst == 'asta') {
1346 /* show users auction status */
1348 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1349 $user_cur = &$bri->user[$table->player[$i]];
1350 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1351 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1354 $showst .= ",-2,-2";
1355 $ret .= sprintf('show_astat(%s);', $showst);
1357 if ($table->asta_win != -1 && $table->asta_win == $user->table_pos) {
1358 /* show card chooser */
1359 $ret .= sprintf('choose_seed(%s); $("asta").style.visibility = "hidden";',
1364 if ($user->table_pos == ($table->gstart % PLAYERS_N) &&
1365 $table->asta_win == -1)
1366 $ret .= sprintf('dispose_asta(%d,%d);',
1367 $table->asta_card + 1, $table->asta_pnt+1);
1369 $ret .= sprintf('dispose_asta(%d,%d);',
1370 $table->asta_card + 1, -($table->asta_pnt+1));
1374 if ($table->asta_win == -1) { // auction case
1375 if ($user->table_pos == ($table->gstart % PLAYERS_N))
1376 $ret .= "remark_on();";
1378 $ret .= "remark_off();";
1380 else { // chooseed case
1381 if ($user->table_pos == $table->asta_win)
1382 $ret .= "remark_on();";
1384 $ret .= "remark_off();";
1387 else if ($user->subst == 'game') {
1389 if (($table->gstart + $table->turn) % PLAYERS_N == $user->table_pos)
1390 $ret .= "is_my_time = true; remark_on();";
1392 $ret .= "remark_off();";
1394 /* WHO CALL AND WATH */
1395 $ret .= briscola_show($bri, $table, $user);
1401 function calculate_winner(&$table)
1408 $cur_seed = $table->briscola - ($table->briscola % 10);
1410 for ($i = 0 ; $i < 40 ; $i++) {
1411 if ($table->card[$i]->stat != "table")
1414 log_wr($sess, sprintf("Card On table: [%d]", $i));
1416 $v = $table->card[$i]->value;
1417 $ontab[$table->card[$i]->owner] = $v;
1418 $ontid[$table->card[$i]->owner] = $i;
1419 /* se briscola setto il flag */
1420 if (($v - ($v % 10)) == $cur_seed)
1424 if ($briontab == FALSE) {
1425 $cur_win = $table->gstart;
1426 $cur_val = $ontab[$cur_win];
1427 $cur_seed = $cur_val - ($cur_val % 10);
1430 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1431 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1432 if ($ontab[$i] < $cur_val) {
1433 $cur_val = $ontab[$i];
1439 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1440 $table->card[$ontid[$i]]->owner = $cur_win;
1441 $table->card[$ontid[$i]]->stat = "take";
1446 function calculate_points(&$table)
1448 $all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
1452 if ($table->asta_pnt == 60)
1453 $table->asta_pnt = 61;
1455 $table->old_win = $table->asta_win;
1456 $table->old_friend = $table->friend;
1457 $table->old_asta_pnt = $table->asta_pnt;
1459 for ($i = 0 ; $i < 40 ; $i++) {
1460 $ctt = $table->card[$i]->value % 10;
1461 $own = $table->card[$i]->owner;
1462 if ($own == $table->asta_win || $own == $table->friend)
1463 $pro += $all_points[$ctt];
1466 log_wr("XXX", sprintf("PRO: [%d]", $pro));
1469 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1470 $table->points[$table->points_n % MAX_POINTS] = array();
1471 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1472 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1474 $table->old_pnt = $pro;
1480 if ($pro >= $table->asta_pnt)
1485 $table->points[$table->points_n % MAX_POINTS] = array();
1486 for ($i = 0 ; $i < 5 ; $i++) {
1487 if ($i == $table->asta_win)
1488 $pt = ($i == $table->friend ? 4 : 2);
1489 else if ($i == $table->friend)
1494 log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1496 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1498 log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1500 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1501 $table->total[$i] += $pt;
1504 $table->old_pnt = $pro;
1508 function validate_sess($sess)
1510 if (strlen($sess) == SESS_LEN)
1516 function validate_name($name)
1518 $name_new = str_replace(' ', '_', substr(trim($name),0,12));
1520 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
1522 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
1529 function playsound($filename)
1531 return (sprintf('playsound("flasou", "%s");', $filename));