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, 180);
32 define(GARBAGE_TIMEOUT, 30);
33 define(NICKSERV, "<i>SERVER</i>");
34 // define(DEBUGGING, "local");
38 return (str_replace('@', '@', str_replace('|', '¦', htmlentities($s,ENT_COMPAT,"UTF-8"))));
43 var $value; /* 0 - 39 card value */
44 var $stat; /* 'bunch', 'hand', 'table', 'take' */
45 var $owner; /* (table position 0-4) */
46 // var $pos; /* Pos in hand. */
47 var $x; /* When played the X position on the table of the owner. */
48 var $y; /* When played the Y position on the table of the owner. */
50 function Card($value, $stat, $owner)
52 $this->value = $value;
54 $this->owner = $owner;
57 function assign($stat,$owner)
60 $this->owner = $owner;
70 $this->stat = 'table';
75 function take($newown)
78 $this->owner = $newown;
96 var $points; // points array
97 var $points_n; // number of row of points
111 $this->player = array();
113 $this->card = &$this->bunch_create();
114 $this->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
115 $this->asta_pla_n= -1;
116 $this->asta_card = -1;
117 $this->asta_pnt = -1;
119 $this->points = array( );
121 $this->total = array( 0, 0, 0, 0, 0);
122 $this->asta_win = -1;
123 $this->briscola = -1;
126 $this->old_asta_pnt = -1;
129 $this->old_friend= -1;
133 function &bunch_create()
137 for ($i = 0 ; $i < 40 ; $i++) {
138 $ret[$i] = new Card($i, 'bunch', 'no_owner');
144 function bunch_make()
146 $ct = array(0,0,0,0,0);
148 mt_srand(make_seed());
150 for ($i = 39 ; $i >= 0 ; $i--)
153 for ($i = 39 ; $i >= 0 ; $i--) {
157 log_main("RND ZERO", "");
162 $this->card[$id]->assign('hand', $owner);
164 $rest[$rn] = $rest[$i];
165 $pubbpos[$rn2] = $pubbpos[$i];
171 $this->mazzo = rand(0,PLAYERS_N-1);
175 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
176 $this->total[$i] = 0;
179 log_main("table::init","ci siamo");
182 function game_init(&$bri)
184 log_rd2($sess,"GSTART 4");
186 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
190 $this->asta_pla_n = PLAYERS_N;
191 $this->asta_card = -1;
192 $this->asta_pnt = 60;
193 $this->asta_win = -1;
194 $this->briscola = -1;
198 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
199 $this->asta_pla[$i] = TRUE;
200 $user_cur = &$bri->user[$this->player[$i]];
201 $user_cur->subst = 'asta';
202 $user_cur->asta_card = -2;
203 $user_cur->asta_pnt = -1;
209 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
212 function getPlayer($idx)
214 return ($this->player[$idx]);
217 function setPlayer($idx, $player)
219 $this->player[$idx] = $player;
224 var $name; // name of the user
225 var $sess; // session of the user
226 var $lacc; // last access (for the cleanup)
227 var $stat; // status (outdoor, room, table, game, ...)
228 var $subst; // substatus for each status
229 var $step; // step of the current status
230 var $trans_step; // step to enable transition between pages (disable == -1)
231 var $comm; // commands array
235 var $table; // id of the current table (if in table state)
236 var $table_pos; // idx on the table
238 function User($name, $sess, $stat = "", $subst = "", $table = -1) {
241 $this->lacc = time();
243 $this->subst = $subst;
245 $this->trans_step = -1;
246 $this->comm = array();
247 $this->asta_card = -2;
248 $this->asta_pnt = -1;
250 $this->table = $table;
257 var $comm; // commands for many people
258 var $step; // current step of the comm array
259 var $garbage_timeout;
262 $this->user = array();
264 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
265 $this->user[$i] = new User("", "");
267 for ($i = 0 ; $i < TABLES_N ; $i++)
268 $this->table[$i] = new Table();
269 $this->garbage_timeout = 0;
272 function garbage_manager($force)
275 /* Garbage collector degli utenti in timeout */
277 if ($force || $this->garbage_timeout < $curtime) {
279 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
280 $user_cur = &$this->user[$i];
281 if ($user_cur->sess == "")
284 if ($user_cur->lacc < $curtime) { // Auto logout dell'utente
285 log_rd2($user_cur->sess, "AUTO LOGOUT.");
287 if ($user_cur->stat == 'table') {
288 log_rd2($user_cur->sess, "AUTO LOGOUT: Yet not implemented in table stat!");
293 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
294 log_auth($user_cur->sess, "Autologout session.");
296 $user_cur->sess = "";
297 $user_cur->name = "";
298 $user_cur->the_end = FALSE;
300 log_rd2($user_cur->sess, "AUTO LOGOUT.");
301 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
302 $this->room_wakeup(&$user_cur);
303 else if ($user_cur->subst == 'standup')
304 $this->room_outstandup(&$user_cur);
306 log_rd2($sess, "LOGOUT FROM WHAT ???");
312 log_rd2($user_cur->sess, "GARBAGE UPDATED!");
314 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
319 function room_wakeup(&$user)
321 $table_idx = $user->table;
322 $table = &$this->table[$table_idx];
324 log_main("WAKEUP", "begin function table:".$table_idx);
326 $from_table = ($user->stat == "table");
328 log_main("WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
330 for ($i = 0 ; $i < $table->player_n ; $i++) {
331 $user_cur = &$this->user[$table->player[$i]];
332 log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
334 if ($user_cur != $user) {
335 $user_cur->stat = "room";
336 $user_cur->subst = "sitdown";
338 else if ($user->sess != "") {
339 $user_cur->stat = "room";
340 $user_cur->subst = "standup";
341 $user_cur->table = -1;
345 /* aggiorna l'array dei giocatori al tavolo. */
346 for ($i = $user->table_pos ; $i < $table->player_n-1 ; $i++) {
347 $table->player[$i] = $table->player[$i+1];
348 $user_cur = &$this->user[$table->player[$i]];
349 $user_cur->table_pos = $i;
351 $this->table[$table_idx]->player_n--;
353 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
354 $user_cur = &$this->user[$i];
355 if ($user_cur->sess == '' || $user_cur->stat != 'room')
358 log_main("VALORI", "name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
360 // function show_room(&$bri, &$user)
362 $ret = "gst.st = ".($user_cur->step+1)."; ";
363 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
364 $ret .= 'gst.st_loc++; the_end=true; document.location.assign("index.php");|';
365 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
366 log_main("DOCUMENT.index.php", "from table");
368 else if ($user_cur->stat == "room") {
369 $ret .= table_content($this, $user_cur, $table_idx);
370 $ret .= standup_content($this, $user_cur);
372 $act_content = table_act_content(FALSE, 0, $e, $user->table);
373 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
376 if ($user_cur == $user) {
377 // set the new status
378 $ret .= 'subst = "standup"; ';
379 // clean the action buttons in other tables
380 for ($e = 0 ; $e < TABLES_N ; $e++) {
381 if ($this->table[$e]->player_n < PLAYERS_N)
382 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
386 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
387 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
390 log_wr($user_cur->sess, "ROOM_WAKEUP: ".$ret);
391 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
399 function room_outstandup(&$user)
401 $this->room_sitdown(&$user, -1);
404 function table_update(&$user)
406 log_main("table_update", "pre - USER: ".$user->name);
408 $table_idx = $user->table;
411 $table = &$this->table[$table_idx];
413 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
415 $user_cur = &$this->user[$i];
416 if ($user_cur->sess == '' || $user_cur->stat != 'room')
419 $ret = "gst.st = ".($user_cur->step+1)."; ";
421 $ret .= table_content($this, $user_cur, $table_idx);
423 if ($user_cur == $user) {
424 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
426 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
430 log_main("table_update", "post");
433 function room_sitdown(&$user, $table_idx)
435 log_main("room_sitdown", ($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
438 $table = &$this->table[$table_idx];
440 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
442 $user_cur = &$this->user[$i];
443 if ($user_cur->sess == '' || $user_cur->stat != 'room')
446 $ret = "gst.st = ".($user_cur->step+1)."; ";
448 $ret .= table_content($this, $user_cur, $table_idx);
449 $ret .= standup_content($this, $user_cur);
451 if ($user_cur == $user) {
452 $ret .= 'subst = "sitdown"; ';
453 // clean the action buttons in other tables
454 for ($e = 0 ; $e < TABLES_N ; $e++) {
455 $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
456 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
459 else if ($table_idx > -1) {
460 if ($table->player_n == PLAYERS_N) {
461 $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
462 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
465 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
470 function chatt_send(&$user, $mesg)
472 if ($user->stat == 'table') {
473 $table = &$this->table[$user->table];
476 $user_mesg = substr($mesg,6);
478 $dt = date("H:i ",time());
479 if (strncmp($user_mesg, "/nick ", 6) == 0) {
480 log_main($user->sess, "chatt_send BEGIN");
482 $name_new = substr(trim(substr($user_mesg, 6)),0,12);
483 $user_mesg = "COMMAND ".$user_mesg;
486 // update local graph
487 // update remote graphs
488 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
489 $user_cur = &$this->user[$i];
490 if ($user_cur->sess == '' || $user_cur->stat != 'room')
492 if ($user_cur->name == $name_new) {
493 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
494 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
499 if ($i == MAX_PLAYERS) {
500 $user->name = $name_new;
502 log_main($user->sess, "chatt_send start set");
505 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
506 log_main($user->sess, "chatt_send set loop");
508 $user_cur = &$this->user[$i];
509 if ($user_cur->sess == '')
511 if ($user_cur->stat == 'room') {
512 if ($user->stat == 'room' && $user->subst == 'standup') {
513 standup_update(&$this,&$user);
515 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
516 $user->stat == 'table') {
517 log_main($user->sess, "chatt_send pre table update");
519 $this->table_update(&$user);
521 log_main($user->sess, "chatt_send post table update");
524 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
525 $table = &$this->table[$user->table];
527 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
528 $user_cur->comm[$user_cur->step % COMM_N] = sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
529 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
530 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
531 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
532 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
533 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
534 if ($user_cur == $user)
535 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
536 xcape($user->name,ENT_COMPAT,"UTF-8"));
543 $user_mesg = xcape($user_mesg);
545 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
546 if ($user->stat == 'room') {
547 $user_cur = &$this->user[$i];
548 if ($user_cur->sess == '' || $user_cur->stat != 'room')
552 $user_cur = &$this->user[$table->player[$i]];
555 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
556 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
557 $dt.xcape($user->name), $user_mesg);
563 } // end class brisco
567 list($usec, $sec) = explode(' ', microtime());
568 return (float) $sec + ((float) $usec * 100000);
571 function log_main($sess, $log) {
572 $fp = fopen("/tmp/brisk_main.log", 'a');
573 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
577 function log_rd($sess, $log) {
578 // $fp = fopen("/tmp/brisk_rd.log", 'a');
579 // fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
584 function log_rd2($sess, $log) {
585 $fp = fopen("/tmp/brisk_rd2.log", 'a');
586 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
590 function log_send($sess, $log) {
591 $fp = fopen("/tmp/brisk_send.log", 'a');
592 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
596 function log_auth($sess, $log) {
597 $fp = fopen("/tmp/brisk_auth.log", 'a');
598 fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
602 function log_wr($sess, $log) {
603 $fp = fopen("/tmp/brisk_wr.log", 'a');
604 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
608 function log_load($sess, $log) {
609 $fp = fopen("/tmp/brisk_load.log", 'a');
610 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
616 $brisco = new brisco();
623 // echo "LOCK: ".FTOK_PATH."/main";
625 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
629 // echo "FTOK ".$tok."<br>";
630 if (($res = sem_get($tok)) == FALSE) {
631 echo "SEM_GET FAILED";
634 if (sem_acquire($res))
640 function unlock_data($res)
642 return (sem_release($res));
646 function &load_data()
648 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
653 if ($shm = shm_attach($tok,100000 * TABLES_N)) {
654 if(($bri = @shm_get_var($shm, $tok)) == false) {
655 log_main("XXX", "INIT MAIN DATA");
658 shm_put_var($shm, $tok, $bri);
670 function save_data(&$bri)
678 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1)
684 if (($shm = shm_attach($tok,100000 * TABLES_N)) == FALSE)
687 if (shm_put_var($shm, $tok, $bri) == FALSE)
689 // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
699 function &get_user(&$bri, $sess, &$idx)
703 if (strlen($sess) == SESS_LEN) {
704 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
705 if (strcmp($sess, $bri->user[$i]->sess) == 0) {
708 return ($bri->user[$i]);
711 log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
712 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
713 // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$bri->user[$i]->sess));
716 log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
722 * function &add_user(&$bri, &$sess, &$idx, $name)
725 * if ($idx != -1 && ret == FALSE) => duplicated nick
726 * if ($idx == -1 && ret == FALSE) => no space left
727 * if (ret == TRUE) => SUCCESS
729 function &add_user(&$bri, &$sess, &$idx, $name)
733 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
735 if (strcmp($sess, $bri->user[$i]->sess) == 0) {
739 if (strcmp($bri->user[$i]->name, $name) == 0) {
744 if ($idx != -1 && $i == MAX_PLAYERS) {
746 $bri->user[$idx]->sess = uniqid("");
747 $sess = $bri->user[$idx]->sess;
748 $bri->user[$idx]->name = $name;
749 $bri->user[$idx]->stat = "room";
750 $bri->user[$idx]->subst = "standup";
752 log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name));
754 return ($bri->user[$idx]);
761 function table_act_content($isstanding, $sitted, $table, $cur_table)
766 if ($sitted < PLAYERS_N) {
767 $ret = sprintf('<input type=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
771 if ($table == $cur_table)
772 $ret = sprintf('<input type=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
779 function table_content($bri, $user, $table_idx)
785 // Si possono usare i dati nella classe table
789 $table = &$bri->table[$table_idx];
791 if ($user->stat != 'room')
794 for ($i = 0 ; $i < $table->player_n ; $i++) {
795 $user_cur = &$bri->user[$table->player[$i]];
797 if ($user_cur == $user)
798 { $hilion = "<b>"; $hilioff = "</b>"; }
800 { $hilion = ""; $hilioff = ""; }
802 log_main($bri->user[$e]->name, sprintf("IN TABLE [%d]", $table_idx));
804 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
806 for ( ; $i < PLAYERS_N ; $i++)
809 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
814 function standup_content(&$bri, $user)
819 if ($user->stat != 'room')
822 $content .= '<table width=\\"100%\\">';
823 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
824 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
828 if ($bri->user[$e]->subst == "standup") {
829 if (($ct % 4) == 0) {
832 if ($bri->user[$e] == $user)
833 { $hilion = "<b>"; $hilioff = "</b>"; }
835 { $hilion = ""; $hilioff = ""; }
837 $content .= sprintf('<td style=\\"text-align: center\\">%s%s%s</td>',$hilion, xcape($bri->user[$e]->name), $hilioff);
838 if (($ct % 4) == 3) {
844 $content .= '</table>';
846 $content .= '<div class=\\"esco\\"><input name=\\"logout\\" value=\\"Esco.\\" onclick=\\"act_logout();\\" type=\\"button\\"></div>';
848 $ret .= sprintf('$("standup").innerHTML = "%s";', $content);
853 function standup_update(&$bri, &$user)
855 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
856 $user_cur = &$bri->user[$i];
857 if ($user_cur->sess == '')
860 log_main("STANDUP START", $user_cur->stat);
862 if ($user_cur->stat == 'room') {
863 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($bri, $user_cur);
864 if ($user_cur == $user)
865 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
867 log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
878 function show_notify($text, $tout, $butt)
880 log_main("SHOW_NOTIFY", $text);
881 return sprintf('var noti = new notify(gst,$("bg"),"%s",%d,"%s");', $text, $tout,$butt);
887 function briscola_show($bri, $table, $user)
892 if ($table->asta_card == 9)
893 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
895 /* text of caller cell */
896 if ($user->table_pos == $table->asta_win)
897 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
899 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
900 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
902 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
904 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
905 $ret .= sprintf('$("caller").style.visibility = "visible";');
906 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
907 $ret .= sprintf('$("asta").style.visibility = "hidden";');
908 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
914 function game_result($asta_pnt, $pnt)
916 if ($asta_pnt == 61) {
925 if ($pnt >= $asta_pnt)
932 function multoval($mult)
937 return ("quadruplo");
939 return (sprintf("%d-plo", $mult));
942 function show_table_info(&$bri, &$table, $table_pos)
944 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
945 $noty = sprintf('<table class=\"points\"><tr><th></th>');
948 for ($i = 0 ; $i < PLAYERS_N ; $i++)
949 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
950 $noty .= sprintf("</tr>");
953 log_main("show_table_info", "pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
955 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
956 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
957 for ($e = 0 ; $e < PLAYERS_N ; $e++)
958 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
963 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
964 for ($e = 0 ; $e < PLAYERS_N ; $e++)
965 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
966 $noty .= "</tr></table>";
968 if ($table->old_win != -1) {
969 $win = $table->player[$table->old_win];
970 $fri = $table->player[$table->old_friend];
972 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
975 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
976 xcape($bri->user[$win]->name),
977 xcape($bri->user[$fri]->name));
978 if ($table->old_pnt == 120) {
979 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
982 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
983 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
984 'più di 60'), $table->old_pnt,
985 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
989 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
990 xcape($bri->user[$win]->name));
991 if ($table->old_pnt == 120) {
992 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
995 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
996 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
997 'più di 60'), $table->old_pnt,
998 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1002 if ($table->mazzo == $table_pos)
1003 $noty .= "Fai <b>tu</b> il mazzo.";
1005 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1006 $noty .= "Il mazzo a <b>$unam</b>.";
1009 if ($table->mult > 1) {
1010 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1012 $noty .= "<hr><br>";
1014 $ret .= show_notify($noty, 3000, "torna alla partita");
1021 function show_room(&$bri, &$user)
1023 $ret .= sprintf('gst.st = %d;', $user->step);
1024 $ret .= sprintf('stat = "%s";', $user->stat);
1025 $ret .= sprintf('subst = "%s";', $user->subst);
1026 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1027 for ($i = 0 ; $i < TABLES_N ; $i++) {
1028 $ret .= table_content($bri, $user, $i);
1029 $act_content = table_act_content(($user->subst == 'standup'),
1030 $bri->table[$i]->player_n, $i, $user->table);
1031 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1033 $ret .= standup_content($bri, $user);
1041 is_transition (is from room to table ?)
1042 is_again (is another game)
1044 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1046 $table_idx = $user->table;
1047 $table = &$bri->table[$table_idx];
1056 cards dnd (and gameable card if its turn)
1061 $ret = "table_init();";
1064 /* GENERAL STATUS */
1065 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1066 $sendstep, $user->stat, $user->subst, $user->table_pos);
1068 $ret .= "background_set();";
1071 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1072 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1073 xcape($bri->user[$table->player[($user->table_pos)%PLAYERS_N]]->name),
1074 xcape($bri->user[$table->player[($user->table_pos+1)%PLAYERS_N]]->name),
1075 xcape($bri->user[$table->player[($user->table_pos+2)%PLAYERS_N]]->name),
1076 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($user->table_pos+3)%PLAYERS_N]]->name)),
1077 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($user->table_pos+4)%PLAYERS_N]]->name)));
1079 /* NOTIFY FOR THE CARD MAKER */
1080 if ($is_transition) { // && $user->subst == "asta" superfluo
1081 $ret .= show_table_info(&$bri, &$table, $user->table_pos);
1085 if ($is_transition) { // && $user->subst == "asta" superfluo
1088 for ($i = 0 ; $i < 8 ; $i++) {
1089 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1091 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1092 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1098 log_rd($sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1100 $ret .= sprintf( ' card_send(%d,%d,%d,%f,%d);|', ($table->gstart + $e) % PLAYERS_N,
1101 $i, ((($e + PLAYERS_N - $user->table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1102 $table->card[$o]->value : -1),
1103 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1108 $taked = array(0,0,0,0,0);
1109 $inhand = array(0,0,0,0,0);
1110 $ontabl = array(-1,-1,-1,-1,-1);
1113 for ($i = 0 ; $i < 40 ; $i++) {
1114 if ($table->card[$i]->stat == 'hand') {
1115 if ($table->card[$i]->owner == $user->table_pos) {
1116 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1118 $inhand[$table->card[$i]->owner]++;
1120 else if ($table->card[$i]->stat == 'take') {
1121 log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1122 $taked[$table->card[$i]->owner]++;
1124 else if ($table->card[$i]->stat == 'table') {
1125 $ontabl[$table->card[$i]->owner] = $i;
1129 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1130 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1132 log_main("Stat table:", $logg);
1134 /* Set ours cards. */
1136 for ($i = 0 ; $i < $inhand[$user->table_pos] ; $i++)
1137 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1138 for ($i = $inhand[$user->table_pos] ; $i < 8 ; $i++)
1139 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1140 $ret .= sprintf('card_setours(%s);', $oursarg);
1142 /* Dispose all cards */
1143 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1144 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1145 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1146 $inhand[$i], $taked[$i]);
1148 if ($ontabl[$i] != -1) {
1149 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1150 $table->card[$ontabl[$i]]->value,
1151 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1157 if ($user->subst == 'asta') {
1159 /* show users auction status */
1161 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1162 $user_cur = &$bri->user[$table->player[$i]];
1163 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1164 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1167 $showst .= ",-2,-2";
1168 $ret .= sprintf('show_astat(%s);', $showst);
1170 if ($table->asta_win != -1 && $table->asta_win == $user->table_pos) {
1171 /* show card chooser */
1172 $ret .= sprintf('choose_seed(%s); $("asta").style.visibility = "hidden";',
1177 if ($user->table_pos == ($table->gstart % PLAYERS_N) &&
1178 $table->asta_win == -1)
1179 $ret .= sprintf('dispose_asta(%d,%d);',
1180 $table->asta_card + 1, $table->asta_pnt+1);
1182 $ret .= sprintf('dispose_asta(%d,%d);',
1183 $table->asta_card + 1, -($table->asta_pnt+1));
1187 if ($table->asta_win == -1) { // auction case
1188 if ($user->table_pos == ($table->gstart % PLAYERS_N))
1189 $ret .= "remark_on();";
1191 $ret .= "remark_off();";
1193 else { // chooseed case
1194 if ($user->table_pos == $table->asta_win)
1195 $ret .= "remark_on();";
1197 $ret .= "remark_off();";
1200 else if ($user->subst == 'game') {
1202 if (($table->gstart + $table->turn) % PLAYERS_N == $user->table_pos)
1203 $ret .= "is_my_time = true; remark_on();";
1205 $ret .= "remark_off();";
1207 /* WHO CALL AND WATH */
1208 $ret .= briscola_show($bri, $table, $user);
1214 function calculate_winner(&$table)
1221 $cur_seed = $table->briscola - ($table->briscola % 10);
1223 for ($i = 0 ; $i < 40 ; $i++) {
1224 if ($table->card[$i]->stat != "table")
1227 log_wr($sess, sprintf("Card On table: [%d]", $i));
1229 $v = $table->card[$i]->value;
1230 $ontab[$table->card[$i]->owner] = $v;
1231 $ontid[$table->card[$i]->owner] = $i;
1232 /* se briscola setto il flag */
1233 if (($v - ($v % 10)) == $cur_seed)
1237 if ($briontab == FALSE) {
1238 $cur_win = $table->gstart;
1239 $cur_val = $ontab[$cur_win];
1240 $cur_seed = $cur_val - ($cur_val % 10);
1243 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1244 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1245 if ($ontab[$i] < $cur_val) {
1246 $cur_val = $ontab[$i];
1252 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1253 $table->card[$ontid[$i]]->owner = $cur_win;
1254 $table->card[$ontid[$i]]->stat = "take";
1259 function calculate_points(&$table)
1261 $all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
1265 if ($table->asta_pnt == 60)
1266 $table->asta_pnt = 61;
1268 $table->old_win = $table->asta_win;
1269 $table->old_friend = $table->friend;
1270 $table->old_asta_pnt = $table->asta_pnt;
1272 for ($i = 0 ; $i < 40 ; $i++) {
1273 $ctt = $table->card[$i]->value % 10;
1274 $own = $table->card[$i]->owner;
1275 if ($own == $table->asta_win || $own == $table->friend)
1276 $pro += $all_points[$ctt];
1279 log_wr("XXX", sprintf("PRO: [%d]", $pro));
1282 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1283 $table->points[$table->points_n % MAX_POINTS] = array();
1284 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1285 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1287 $table->old_pnt = $pro;
1293 if ($pro >= $table->asta_pnt)
1298 $table->points[$table->points_n % MAX_POINTS] = array();
1299 for ($i = 0 ; $i < 5 ; $i++) {
1300 if ($i == $table->asta_win)
1301 $pt = ($i == $table->friend ? 4 : 2);
1302 else if ($i == $table->friend)
1307 log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1309 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1311 log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1313 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1314 $table->total[$i] += $pt;
1317 $table->old_pnt = $pro;