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, 180);
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.4";
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 $name_new = str_replace(' ', '_', substr(trim(substr($user_mesg, 6)),0,12));
562 $user_mesg = "COMMAND ".$user_mesg;
565 // update local graph
566 // update remote graphs
567 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
568 $user_cur = &$this->user[$i];
569 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
570 if ($user_cur->sess == '')
572 if ($user_cur->name == $name_new) {
573 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
574 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
579 if ($i == MAX_PLAYERS) {
580 $user->name = $name_new;
582 log_main($user->sess, "chatt_send start set");
585 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
586 log_main($user->sess, "chatt_send set loop");
588 $user_cur = &$this->user[$i];
589 if ($user_cur->sess == '')
591 if ($user_cur->stat == 'room') {
592 if ($user->stat == 'room' && $user->subst == 'standup') {
593 $this->standup_update(&$user);
595 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
596 $user->stat == 'table') {
597 log_main($user->sess, "chatt_send pre table update");
599 $this->table_update(&$user);
601 log_main($user->sess, "chatt_send post table update");
604 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
605 $table = &$this->table[$user->table];
607 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
608 $user_cur->comm[$user_cur->step % COMM_N] = sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
609 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
610 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
611 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
612 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
613 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
614 if ($user_cur == $user)
615 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
616 xcape($user->name,ENT_COMPAT,"UTF-8"));
623 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
624 if ($user->stat == 'room') {
625 $user_cur = &$this->user[$i];
626 if ($user_cur->sess == '' || $user_cur->stat != 'room')
630 $user_cur = &$this->user[$table->player[$i]];
633 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
634 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
635 $dt.xcape($user->name), xcape($user_mesg));
641 function &get_user($sess, &$idx)
645 if (strlen($sess) == SESS_LEN) {
646 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
647 if (strcmp($sess, $this->user[$i]->sess) == 0) {
650 return ($this->user[$i]);
653 log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
654 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
655 // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
658 log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
664 * function &add_user(&$bri, &$sess, &$idx, $name)
667 * if ($idx != -1 && ret == FALSE) => duplicated nick
668 * if ($idx == -1 && ret == FALSE) => no space left
669 * if (ret == TRUE) => SUCCESS
671 function &add_user(&$sess, &$idx, $name)
676 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
677 if (validate_sess($sess) == FALSE)
680 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
682 if (strcmp($sess, $this->user[$i]->sess) == 0) {
686 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
689 if (strcmp($this->user[$i]->name, $name) == 0) {
697 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name));
699 if ($idx != -1 && $i == MAX_PLAYERS) {
702 $this->user[$idx]->sess = uniqid("");
703 $sess = $this->user[$idx]->sess;
707 $this->user[$idx]->sess = $sess;
709 $this->user[$idx]->name = $name;
710 $this->user[$idx]->stat = "room";
711 $this->user[$idx]->subst = "standup";
712 $this->user[$idx]->laccwr = time();
714 log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name));
716 return ($this->user[$idx]);
722 function standup_update(&$user)
724 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
725 $user_cur = &$this->user[$i];
726 if ($user_cur->sess == '')
729 log_main("STANDUP START", $user_cur->stat);
731 if ($user_cur->stat == 'room') {
732 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
733 if ($user_cur == $user)
734 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
736 log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
744 } // end class brisco
748 list($usec, $sec) = explode(' ', microtime());
749 return (float) $sec + ((float) $usec * 100000);
752 function log_main($sess, $log) {
753 if (BRISK_DEBUG != TRUE)
756 $fp = fopen("/tmp/brisk_main.log", 'a');
757 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
761 function log_rd($sess, $log) {
762 if (BRISK_DEBUG != TRUE)
765 $fp = fopen("/tmp/brisk_rd.log", 'a');
766 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
770 function log_rd2($sess, $log) {
771 if (BRISK_DEBUG != TRUE)
774 $fp = fopen("/tmp/brisk_rd2.log", 'a');
775 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
779 function log_send($sess, $log) {
780 if (BRISK_DEBUG != TRUE)
783 $fp = fopen("/tmp/brisk_send.log", 'a');
784 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
788 function log_auth($sess, $log) {
789 if (BRISK_DEBUG != TRUE)
792 $fp = fopen("/tmp/brisk_auth.log", 'a');
793 fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
797 function log_wr($sess, $log) {
798 if (BRISK_DEBUG != TRUE)
801 $fp = fopen("/tmp/brisk_wr.log", 'a');
802 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
806 function log_load($sess, $log) {
807 if (BRISK_DEBUG != TRUE)
810 $fp = fopen("/tmp/brisk_load.log", 'a');
811 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
817 $brisco = new brisco();
824 // echo "LOCK: ".FTOK_PATH."/main";
826 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
830 // echo "FTOK ".$tok."<br>";
831 if (($res = sem_get($tok)) == FALSE) {
832 echo "SEM_GET FAILED";
835 if (sem_acquire($res))
841 function unlock_data($res)
843 return (sem_release($res));
847 function &load_data()
849 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
854 if ($shm = shm_attach($tok,100000 * TABLES_N)) {
855 if(($bri = @shm_get_var($shm, $tok)) == false) {
856 log_main("XXX", "INIT MAIN DATA");
859 shm_put_var($shm, $tok, $bri);
871 function save_data(&$bri)
879 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1)
885 if (($shm = shm_attach($tok,100000 * TABLES_N)) == FALSE)
888 if (shm_put_var($shm, $tok, $bri) == FALSE)
890 // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
900 function table_act_content($isstanding, $sitted, $table, $cur_table)
905 if ($sitted < PLAYERS_N) {
906 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
910 if ($table == $cur_table)
911 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
918 function table_content($bri, $user, $table_idx)
924 // Si possono usare i dati nella classe table
928 $table = &$bri->table[$table_idx];
930 if ($user->stat != 'room')
933 for ($i = 0 ; $i < $table->player_n ; $i++) {
934 $user_cur = &$bri->user[$table->player[$i]];
936 if ($user_cur == $user)
937 { $hilion = "<b>"; $hilioff = "</b>"; }
939 { $hilion = ""; $hilioff = ""; }
941 log_main($bri->user[$e]->name, sprintf("IN TABLE [%d]", $table_idx));
943 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
946 for ( ; $i < PLAYERS_N ; $i++)
950 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
955 function standup_content(&$bri, $user)
960 if ($user->stat != 'room')
963 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
964 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
969 $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
971 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
972 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
976 if ($bri->user[$e]->subst == "standup") {
977 if (($ct % 4) == 0) {
980 if ($bri->user[$e] == $user)
981 { $hilion = "<b>"; $hilioff = "</b>"; }
983 { $hilion = ""; $hilioff = ""; }
985 $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($bri->user[$e]->name), $hilioff);
986 if (($ct % 4) == 3) {
992 $content .= '</table>';
994 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
995 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
996 $content, $content2);
1002 function show_notify($text, $tout, $butt, $w, $h)
1004 log_main("SHOW_NOTIFY", $text);
1005 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
1008 function briscola_show($bri, $table, $user)
1013 if ($table->asta_card == 9)
1014 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1016 /* text of caller cell */
1017 if ($user->table_pos == $table->asta_win)
1018 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1020 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
1021 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1023 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1025 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1026 $ret .= sprintf('$("caller").style.visibility = "visible";');
1027 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1028 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1029 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1035 function game_result($asta_pnt, $pnt)
1037 if ($asta_pnt == 61) {
1040 else if ($pnt == 60)
1046 if ($pnt >= $asta_pnt)
1053 function multoval($mult)
1057 else if ($mult == 4)
1058 return ("quadruplo");
1060 return (sprintf("%d-plo", $mult));
1063 function show_table_info(&$bri, &$table, $table_pos)
1065 $user = &$bri->user[$table->player[$table_pos]];
1067 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1068 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1071 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1072 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1073 $noty .= sprintf("</tr>");
1076 log_main("show_table_info", "pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1078 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1079 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1080 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1081 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1086 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1087 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1088 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1089 $noty .= "</tr></table>";
1091 if ($table->old_win != -1) {
1092 $win = $table->player[$table->old_win];
1093 $fri = $table->player[$table->old_friend];
1095 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1098 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
1099 xcape($bri->user[$win]->name),
1100 xcape($bri->user[$fri]->name));
1101 if ($table->old_pnt == 120) {
1102 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1105 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1106 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1107 'più di 60'), $table->old_pnt,
1108 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1112 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1113 xcape($bri->user[$win]->name));
1114 if ($table->old_pnt == 120) {
1115 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1118 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1119 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1120 'più di 60'), $table->old_pnt,
1121 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1125 if ($table->mazzo == $table_pos)
1126 $noty .= "Fai <b>tu</b> il mazzo,";
1128 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1129 $noty .= "Il mazzo a <b>$unam</b>,";
1132 if ($user->subst == 'asta') {
1133 if ($table->asta_win == -1) // auction case
1134 $curplayer = $table->gstart % PLAYERS_N;
1136 $curplayer = $table->asta_win;
1138 else if ($user->subst == 'game') {
1139 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
1143 if ($curplayer == $table_pos) {
1144 $noty .= " tocca a <b>te</b> giocare.";
1147 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1148 $noty .= " tocca a <b>$unam</b> giocare.";
1151 if ($table->mult > 1) {
1152 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1154 $noty .= "<hr><br>";
1156 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
1161 function root_wellcome($user)
1163 GLOBAL $root_wellarr;
1165 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
1166 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $root_wellarr[$i]));
1171 function table_wellcome($user)
1173 GLOBAL $table_wellarr;
1175 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1176 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
1181 function show_room(&$bri, &$user)
1183 $ret .= sprintf('gst.st = %d;', $user->step);
1184 $ret .= sprintf('stat = "%s";', $user->stat);
1186 $ret .= root_wellcome($user);
1187 $ret .= sprintf('subst = "%s";', $user->subst);
1188 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1189 for ($i = 0 ; $i < TABLES_N ; $i++) {
1190 $ret .= table_content($bri, $user, $i);
1191 $act_content = table_act_content(($user->subst == 'standup'),
1192 $bri->table[$i]->player_n, $i, $user->table);
1193 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1195 $ret .= standup_content($bri, $user);
1203 is_transition (is from room to table ?)
1204 is_again (is another game)
1206 Examles of $is_transition, $is_again:
1207 from reload of the page: FALSE, FALSE
1208 from sitdown in room: TRUE, FALSE
1209 from table: asta cmd e tutti passano: TRUE, TRUE
1210 from table: fine partita: TRUE, TRUE
1212 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1214 $table_idx = $user->table;
1215 $table = &$bri->table[$table_idx];
1224 cards dnd (and gameable card if its turn)
1229 $ret = "table_init();";
1232 /* GENERAL STATUS */
1233 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1234 $sendstep, $user->stat, $user->subst, $user->table_pos);
1236 $ret .= "background_set();";
1239 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1240 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1241 xcape($bri->user[$table->player[($user->table_pos)%PLAYERS_N]]->name),
1242 xcape($bri->user[$table->player[($user->table_pos+1)%PLAYERS_N]]->name),
1243 xcape($bri->user[$table->player[($user->table_pos+2)%PLAYERS_N]]->name),
1244 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($user->table_pos+3)%PLAYERS_N]]->name)),
1245 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($user->table_pos+4)%PLAYERS_N]]->name)));
1247 /* NOTIFY FOR THE CARD MAKER */
1248 if ($is_transition) { // && $user->subst == "asta" superfluo
1249 $ret .= show_table_info(&$bri, &$table, $user->table_pos);
1252 $ret .= table_wellcome($user);
1255 if ($is_transition) { // && $user->subst == "asta" superfluo
1258 for ($i = 0 ; $i < 8 ; $i++) {
1259 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1261 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1262 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1268 log_rd($sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1270 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
1271 $i, ((($e + PLAYERS_N - $user->table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1272 $table->card[$o]->value : -1),
1273 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1278 $taked = array(0,0,0,0,0);
1279 $inhand = array(0,0,0,0,0);
1280 $ontabl = array(-1,-1,-1,-1,-1);
1283 for ($i = 0 ; $i < 40 ; $i++) {
1284 if ($table->card[$i]->stat == 'hand') {
1285 if ($table->card[$i]->owner == $user->table_pos) {
1286 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1288 $inhand[$table->card[$i]->owner]++;
1290 else if ($table->card[$i]->stat == 'take') {
1291 log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1292 $taked[$table->card[$i]->owner]++;
1294 else if ($table->card[$i]->stat == 'table') {
1295 $ontabl[$table->card[$i]->owner] = $i;
1299 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1300 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1302 log_main("Stat table:", $logg);
1304 /* Set ours cards. */
1306 for ($i = 0 ; $i < $inhand[$user->table_pos] ; $i++)
1307 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1308 for ($i = $inhand[$user->table_pos] ; $i < 8 ; $i++)
1309 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1310 $ret .= sprintf('card_setours(%s);', $oursarg);
1312 /* Dispose all cards */
1313 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1314 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1315 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1316 $inhand[$i], $taked[$i]);
1318 if ($ontabl[$i] != -1) {
1319 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1320 $table->card[$ontabl[$i]]->value,
1321 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1327 if ($user->subst == 'asta') {
1329 /* show users auction status */
1331 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1332 $user_cur = &$bri->user[$table->player[$i]];
1333 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1334 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1337 $showst .= ",-2,-2";
1338 $ret .= sprintf('show_astat(%s);', $showst);
1340 if ($table->asta_win != -1 && $table->asta_win == $user->table_pos) {
1341 /* show card chooser */
1342 $ret .= sprintf('choose_seed(%s); $("asta").style.visibility = "hidden";',
1347 if ($user->table_pos == ($table->gstart % PLAYERS_N) &&
1348 $table->asta_win == -1)
1349 $ret .= sprintf('dispose_asta(%d,%d);',
1350 $table->asta_card + 1, $table->asta_pnt+1);
1352 $ret .= sprintf('dispose_asta(%d,%d);',
1353 $table->asta_card + 1, -($table->asta_pnt+1));
1357 if ($table->asta_win == -1) { // auction case
1358 if ($user->table_pos == ($table->gstart % PLAYERS_N))
1359 $ret .= "remark_on();";
1361 $ret .= "remark_off();";
1363 else { // chooseed case
1364 if ($user->table_pos == $table->asta_win)
1365 $ret .= "remark_on();";
1367 $ret .= "remark_off();";
1370 else if ($user->subst == 'game') {
1372 if (($table->gstart + $table->turn) % PLAYERS_N == $user->table_pos)
1373 $ret .= "is_my_time = true; remark_on();";
1375 $ret .= "remark_off();";
1377 /* WHO CALL AND WATH */
1378 $ret .= briscola_show($bri, $table, $user);
1384 function calculate_winner(&$table)
1391 $cur_seed = $table->briscola - ($table->briscola % 10);
1393 for ($i = 0 ; $i < 40 ; $i++) {
1394 if ($table->card[$i]->stat != "table")
1397 log_wr($sess, sprintf("Card On table: [%d]", $i));
1399 $v = $table->card[$i]->value;
1400 $ontab[$table->card[$i]->owner] = $v;
1401 $ontid[$table->card[$i]->owner] = $i;
1402 /* se briscola setto il flag */
1403 if (($v - ($v % 10)) == $cur_seed)
1407 if ($briontab == FALSE) {
1408 $cur_win = $table->gstart;
1409 $cur_val = $ontab[$cur_win];
1410 $cur_seed = $cur_val - ($cur_val % 10);
1413 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1414 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1415 if ($ontab[$i] < $cur_val) {
1416 $cur_val = $ontab[$i];
1422 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1423 $table->card[$ontid[$i]]->owner = $cur_win;
1424 $table->card[$ontid[$i]]->stat = "take";
1429 function calculate_points(&$table)
1431 $all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
1435 if ($table->asta_pnt == 60)
1436 $table->asta_pnt = 61;
1438 $table->old_win = $table->asta_win;
1439 $table->old_friend = $table->friend;
1440 $table->old_asta_pnt = $table->asta_pnt;
1442 for ($i = 0 ; $i < 40 ; $i++) {
1443 $ctt = $table->card[$i]->value % 10;
1444 $own = $table->card[$i]->owner;
1445 if ($own == $table->asta_win || $own == $table->friend)
1446 $pro += $all_points[$ctt];
1449 log_wr("XXX", sprintf("PRO: [%d]", $pro));
1452 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1453 $table->points[$table->points_n % MAX_POINTS] = array();
1454 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1455 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1457 $table->old_pnt = $pro;
1463 if ($pro >= $table->asta_pnt)
1468 $table->points[$table->points_n % MAX_POINTS] = array();
1469 for ($i = 0 ; $i < 5 ; $i++) {
1470 if ($i == $table->asta_win)
1471 $pt = ($i == $table->friend ? 4 : 2);
1472 else if ($i == $table->friend)
1477 log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1479 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1481 log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1483 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1484 $table->total[$i] += $pt;
1487 $table->old_pnt = $pro;
1491 function validate_sess($sess) {
1492 if (strlen($sess) == SESS_LEN)