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, 10);
33 define(NICKSERV, "<i>SERVER</i>");
34 define(BRISK_DEBUG, FALSE);
35 // define(DEBUGGING, "local");
39 return (str_replace('\\', '\\\\', str_replace('@', '@', str_replace('|', '¦', htmlentities($s,ENT_COMPAT,"UTF-8")))));
44 var $value; /* 0 - 39 card value */
45 var $stat; /* 'bunch', 'hand', 'table', 'take' */
46 var $owner; /* (table position 0-4) */
47 // var $pos; /* Pos in hand. */
48 var $x; /* When played the X position on the table of the owner. */
49 var $y; /* When played the Y position on the table of the owner. */
51 function Card($value, $stat, $owner)
53 $this->value = $value;
55 $this->owner = $owner;
58 function assign($stat,$owner)
61 $this->owner = $owner;
71 $this->stat = 'table';
76 function take($newown)
79 $this->owner = $newown;
97 var $points; // points array
98 var $points_n; // number of row of points
112 $this->player = array();
114 $this->card = &$this->bunch_create();
115 $this->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
116 $this->asta_pla_n= -1;
117 $this->asta_card = -1;
118 $this->asta_pnt = -1;
120 $this->points = array( );
122 $this->total = array( 0, 0, 0, 0, 0);
123 $this->asta_win = -1;
124 $this->briscola = -1;
127 $this->old_asta_pnt = -1;
130 $this->old_friend= -1;
134 function &bunch_create()
138 for ($i = 0 ; $i < 40 ; $i++) {
139 $ret[$i] = new Card($i, 'bunch', 'no_owner');
145 function bunch_make()
147 $ct = array(0,0,0,0,0);
149 mt_srand(make_seed());
151 for ($i = 39 ; $i >= 0 ; $i--)
154 for ($i = 39 ; $i >= 0 ; $i--) {
158 log_main("RND ZERO", "");
163 $this->card[$id]->assign('hand', $owner);
165 $rest[$rn] = $rest[$i];
166 $pubbpos[$rn2] = $pubbpos[$i];
172 $this->mazzo = rand(0,PLAYERS_N-1);
176 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
177 $this->total[$i] = 0;
180 log_main("table::init","ci siamo");
183 function game_init(&$bri)
185 log_rd2($sess,"GSTART 4");
187 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
191 $this->asta_pla_n = PLAYERS_N;
192 $this->asta_card = -1;
193 $this->asta_pnt = 60;
194 $this->asta_win = -1;
195 $this->briscola = -1;
199 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
200 $this->asta_pla[$i] = TRUE;
201 $user_cur = &$bri->user[$this->player[$i]];
202 $user_cur->subst = 'asta';
203 $user_cur->asta_card = -2;
204 $user_cur->asta_pnt = -1;
210 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
213 function getPlayer($idx)
215 return ($this->player[$idx]);
218 function setPlayer($idx, $player)
220 $this->player[$idx] = $player;
225 var $name; // name of the user
226 var $sess; // session of the user
227 var $lacc; // last access (for the cleanup)
228 var $stat; // status (outdoor, room, table, game, ...)
229 var $subst; // substatus for each status
230 var $step; // step of the current status
231 var $trans_step; // step to enable transition between pages (disable == -1)
232 var $comm; // commands array
236 var $table; // id of the current table (if in table state)
237 var $table_pos; // idx on the table
239 function User($name, $sess, $stat = "", $subst = "", $table = -1) {
242 $this->lacc = time();
244 $this->subst = $subst;
246 $this->trans_step = -1;
247 $this->comm = array();
248 $this->asta_card = -2;
249 $this->asta_pnt = -1;
251 $this->table = $table;
258 var $comm; // commands for many people
259 var $step; // current step of the comm array
260 var $garbage_timeout;
263 $this->user = array();
265 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
266 $this->user[$i] = new User("", "");
268 for ($i = 0 ; $i < TABLES_N ; $i++)
269 $this->table[$i] = new Table();
270 $this->garbage_timeout = 0;
273 function garbage_manager($force)
276 /* Garbage collector degli utenti in timeout */
278 if ($force || $this->garbage_timeout < $curtime) {
280 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
281 $user_cur = &$this->user[$i];
282 if ($user_cur->sess == "")
285 if ($user_cur->lacc < $curtime) { // Auto logout dell'utente
286 log_rd2($user_cur->sess, "AUTO LOGOUT.");
288 if ($user_cur->stat == 'table') {
289 log_rd2($user_cur->sess, "AUTO LOGOUT: Yet not implemented in table stat!");
294 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
295 log_auth($user_cur->sess, "Autologout session.");
297 $user_cur->sess = "";
298 $user_cur->name = "";
299 $user_cur->the_end = FALSE;
301 log_rd2($user_cur->sess, "AUTO LOGOUT.");
302 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
303 $this->room_wakeup(&$user_cur);
304 else if ($user_cur->subst == 'standup')
305 $this->room_outstandup(&$user_cur);
307 log_rd2($sess, "LOGOUT FROM WHAT ???");
313 log_rd2($user_cur->sess, "GARBAGE UPDATED!");
315 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
320 function room_wakeup(&$user)
322 $table_idx = $user->table;
323 $table = &$this->table[$table_idx];
325 log_main("WAKEUP", "begin function table:".$table_idx);
327 $from_table = ($user->stat == "table");
329 log_main("WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
331 for ($i = 0 ; $i < $table->player_n ; $i++) {
332 $user_cur = &$this->user[$table->player[$i]];
333 log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
335 if ($user_cur != $user) {
336 $user_cur->stat = "room";
337 $user_cur->subst = "sitdown";
339 else if ($user->sess != "") {
340 $user_cur->stat = "room";
341 $user_cur->subst = "standup";
342 $user_cur->table = -1;
346 /* aggiorna l'array dei giocatori al tavolo. */
347 for ($i = $user->table_pos ; $i < $table->player_n-1 ; $i++) {
348 $table->player[$i] = $table->player[$i+1];
349 $user_cur = &$this->user[$table->player[$i]];
350 $user_cur->table_pos = $i;
352 $this->table[$table_idx]->player_n--;
354 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
355 $user_cur = &$this->user[$i];
356 if ($user_cur->sess == '' || $user_cur->stat != 'room')
359 log_main("VALORI", "name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
361 // function show_room(&$bri, &$user)
363 $ret = "gst.st = ".($user_cur->step+1)."; ";
364 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
365 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
366 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
367 log_main("DOCUMENT.index.php", "from table");
369 else if ($user_cur->stat == "room") {
370 $ret .= table_content($this, $user_cur, $table_idx);
371 $ret .= standup_content($this, $user_cur);
373 $act_content = table_act_content(FALSE, 0, $e, $user->table);
374 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
377 if ($user_cur == $user) {
378 // set the new status
379 $ret .= 'subst = "standup"; ';
380 // clean the action buttons in other tables
381 for ($e = 0 ; $e < TABLES_N ; $e++) {
382 if ($this->table[$e]->player_n < PLAYERS_N)
383 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
387 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
388 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
391 log_wr($user_cur->sess, "ROOM_WAKEUP: ".$ret);
392 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
400 function room_outstandup(&$user)
402 $this->room_sitdown(&$user, -1);
405 function table_update(&$user)
407 log_main("table_update", "pre - USER: ".$user->name);
409 $table_idx = $user->table;
412 $table = &$this->table[$table_idx];
414 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
416 $user_cur = &$this->user[$i];
417 if ($user_cur->sess == '' || $user_cur->stat != 'room')
420 $ret = "gst.st = ".($user_cur->step+1)."; ";
422 $ret .= table_content($this, $user_cur, $table_idx);
424 if ($user_cur == $user) {
425 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
427 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
431 log_main("table_update", "post");
434 function room_sitdown(&$user, $table_idx)
436 log_main("room_sitdown", ($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
439 $table = &$this->table[$table_idx];
441 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
443 $user_cur = &$this->user[$i];
444 if ($user_cur->sess == '' || $user_cur->stat != 'room')
447 $ret = "gst.st = ".($user_cur->step+1)."; ";
449 $ret .= table_content($this, $user_cur, $table_idx);
450 $ret .= standup_content($this, $user_cur);
452 if ($user_cur == $user) {
453 $ret .= 'subst = "sitdown"; ';
454 // clean the action buttons in other tables
455 for ($e = 0 ; $e < TABLES_N ; $e++) {
456 $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
457 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
460 else if ($table_idx > -1) {
461 if ($table->player_n == PLAYERS_N) {
462 $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
463 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
466 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
471 function chatt_send(&$user, $mesg)
473 if ($user->stat == 'table') {
474 $table = &$this->table[$user->table];
477 $user_mesg = substr($mesg,6);
479 $dt = date("H:i ",time());
480 if (strncmp($user_mesg, "/nick ", 6) == 0) {
481 log_main($user->sess, "chatt_send BEGIN");
483 $name_new = substr(trim(substr($user_mesg, 6)),0,12);
484 $user_mesg = "COMMAND ".$user_mesg;
487 // update local graph
488 // update remote graphs
489 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
490 $user_cur = &$this->user[$i];
491 if ($user_cur->sess == '' || $user_cur->stat != 'room')
493 if ($user_cur->name == $name_new) {
494 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
495 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
500 if ($i == MAX_PLAYERS) {
501 $user->name = $name_new;
503 log_main($user->sess, "chatt_send start set");
506 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
507 log_main($user->sess, "chatt_send set loop");
509 $user_cur = &$this->user[$i];
510 if ($user_cur->sess == '')
512 if ($user_cur->stat == 'room') {
513 if ($user->stat == 'room' && $user->subst == 'standup') {
514 standup_update(&$this,&$user);
516 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
517 $user->stat == 'table') {
518 log_main($user->sess, "chatt_send pre table update");
520 $this->table_update(&$user);
522 log_main($user->sess, "chatt_send post table update");
525 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
526 $table = &$this->table[$user->table];
528 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
529 $user_cur->comm[$user_cur->step % COMM_N] = sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
530 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
531 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
532 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
533 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
534 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
535 if ($user_cur == $user)
536 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
537 xcape($user->name,ENT_COMPAT,"UTF-8"));
544 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
545 if ($user->stat == 'room') {
546 $user_cur = &$this->user[$i];
547 if ($user_cur->sess == '' || $user_cur->stat != 'room')
551 $user_cur = &$this->user[$table->player[$i]];
554 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
555 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
556 $dt.xcape($user->name), xcape($user_mesg));
562 } // end class brisco
566 list($usec, $sec) = explode(' ', microtime());
567 return (float) $sec + ((float) $usec * 100000);
570 function log_main($sess, $log) {
571 if (BRISK_DEBUG != TRUE)
574 $fp = fopen("/tmp/brisk_main.log", 'a');
575 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
579 function log_rd($sess, $log) {
580 if (BRISK_DEBUG != TRUE)
583 $fp = fopen("/tmp/brisk_rd.log", 'a');
584 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
588 function log_rd2($sess, $log) {
589 if (BRISK_DEBUG != TRUE)
592 $fp = fopen("/tmp/brisk_rd2.log", 'a');
593 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
597 function log_send($sess, $log) {
598 if (BRISK_DEBUG != TRUE)
601 $fp = fopen("/tmp/brisk_send.log", 'a');
602 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
606 function log_auth($sess, $log) {
607 if (BRISK_DEBUG != TRUE)
610 $fp = fopen("/tmp/brisk_auth.log", 'a');
611 fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
615 function log_wr($sess, $log) {
616 if (BRISK_DEBUG != TRUE)
619 $fp = fopen("/tmp/brisk_wr.log", 'a');
620 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
624 function log_load($sess, $log) {
625 if (BRISK_DEBUG != TRUE)
628 $fp = fopen("/tmp/brisk_load.log", 'a');
629 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
635 $brisco = new brisco();
642 // echo "LOCK: ".FTOK_PATH."/main";
644 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
648 // echo "FTOK ".$tok."<br>";
649 if (($res = sem_get($tok)) == FALSE) {
650 echo "SEM_GET FAILED";
653 if (sem_acquire($res))
659 function unlock_data($res)
661 return (sem_release($res));
665 function &load_data()
667 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
672 if ($shm = shm_attach($tok,100000 * TABLES_N)) {
673 if(($bri = @shm_get_var($shm, $tok)) == false) {
674 log_main("XXX", "INIT MAIN DATA");
677 shm_put_var($shm, $tok, $bri);
689 function save_data(&$bri)
697 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1)
703 if (($shm = shm_attach($tok,100000 * TABLES_N)) == FALSE)
706 if (shm_put_var($shm, $tok, $bri) == FALSE)
708 // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
718 function &get_user(&$bri, $sess, &$idx)
722 if (strlen($sess) == SESS_LEN) {
723 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
724 if (strcmp($sess, $bri->user[$i]->sess) == 0) {
727 return ($bri->user[$i]);
730 log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
731 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
732 // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$bri->user[$i]->sess));
735 log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
741 * function &add_user(&$bri, &$sess, &$idx, $name)
744 * if ($idx != -1 && ret == FALSE) => duplicated nick
745 * if ($idx == -1 && ret == FALSE) => no space left
746 * if (ret == TRUE) => SUCCESS
748 function &add_user(&$bri, &$sess, &$idx, $name)
752 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
754 if (strcmp($sess, $bri->user[$i]->sess) == 0) {
758 if (strcmp($bri->user[$i]->name, $name) == 0) {
763 if ($idx != -1 && $i == MAX_PLAYERS) {
765 $bri->user[$idx]->sess = uniqid("");
766 $sess = $bri->user[$idx]->sess;
767 $bri->user[$idx]->name = $name;
768 $bri->user[$idx]->stat = "room";
769 $bri->user[$idx]->subst = "standup";
771 log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name));
773 return ($bri->user[$idx]);
780 function table_act_content($isstanding, $sitted, $table, $cur_table)
785 if ($sitted < PLAYERS_N) {
786 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
790 if ($table == $cur_table)
791 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
798 function table_content($bri, $user, $table_idx)
804 // Si possono usare i dati nella classe table
808 $table = &$bri->table[$table_idx];
810 if ($user->stat != 'room')
813 for ($i = 0 ; $i < $table->player_n ; $i++) {
814 $user_cur = &$bri->user[$table->player[$i]];
816 if ($user_cur == $user)
817 { $hilion = "<b>"; $hilioff = "</b>"; }
819 { $hilion = ""; $hilioff = ""; }
821 log_main($bri->user[$e]->name, sprintf("IN TABLE [%d]", $table_idx));
823 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
826 for ( ; $i < PLAYERS_N ; $i++)
830 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
835 function standup_content(&$bri, $user)
840 if ($user->stat != 'room')
843 $content .= '<table class=\\"table_standup\\">';
844 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
845 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
849 if ($bri->user[$e]->subst == "standup") {
850 if (($ct % 4) == 0) {
853 if ($bri->user[$e] == $user)
854 { $hilion = "<b>"; $hilioff = "</b>"; }
856 { $hilion = ""; $hilioff = ""; }
858 $content .= sprintf('<td style=\\"text-align: center\\">%s%s%s</td>',$hilion, xcape($bri->user[$e]->name), $hilioff);
859 if (($ct % 4) == 3) {
865 $content .= '</table>';
867 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
868 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
869 $content, $content2);
874 function standup_update(&$bri, &$user)
876 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
877 $user_cur = &$bri->user[$i];
878 if ($user_cur->sess == '')
881 log_main("STANDUP START", $user_cur->stat);
883 if ($user_cur->stat == 'room') {
884 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($bri, $user_cur);
885 if ($user_cur == $user)
886 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
888 log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
899 function show_notify($text, $tout, $butt)
901 log_main("SHOW_NOTIFY", $text);
902 return sprintf('var noti = new notify(gst,$("bg"),"%s",%d,"%s");', $text, $tout,$butt);
908 function briscola_show($bri, $table, $user)
913 if ($table->asta_card == 9)
914 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
916 /* text of caller cell */
917 if ($user->table_pos == $table->asta_win)
918 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
920 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
921 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
923 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
925 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
926 $ret .= sprintf('$("caller").style.visibility = "visible";');
927 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
928 $ret .= sprintf('$("asta").style.visibility = "hidden";');
929 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
935 function game_result($asta_pnt, $pnt)
937 if ($asta_pnt == 61) {
946 if ($pnt >= $asta_pnt)
953 function multoval($mult)
958 return ("quadruplo");
960 return (sprintf("%d-plo", $mult));
963 function show_table_info(&$bri, &$table, $table_pos)
965 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
966 $noty = sprintf('<table class=\"points\"><tr><th></th>');
969 for ($i = 0 ; $i < PLAYERS_N ; $i++)
970 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
971 $noty .= sprintf("</tr>");
974 log_main("show_table_info", "pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
976 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
977 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
978 for ($e = 0 ; $e < PLAYERS_N ; $e++)
979 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
984 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
985 for ($e = 0 ; $e < PLAYERS_N ; $e++)
986 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
987 $noty .= "</tr></table>";
989 if ($table->old_win != -1) {
990 $win = $table->player[$table->old_win];
991 $fri = $table->player[$table->old_friend];
993 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
996 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
997 xcape($bri->user[$win]->name),
998 xcape($bri->user[$fri]->name));
999 if ($table->old_pnt == 120) {
1000 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1003 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1004 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1005 'più di 60'), $table->old_pnt,
1006 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1010 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1011 xcape($bri->user[$win]->name));
1012 if ($table->old_pnt == 120) {
1013 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1016 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1017 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1018 'più di 60'), $table->old_pnt,
1019 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1023 if ($table->mazzo == $table_pos)
1024 $noty .= "Fai <b>tu</b> il mazzo.";
1026 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1027 $noty .= "Il mazzo a <b>$unam</b>.";
1030 if ($table->mult > 1) {
1031 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1033 $noty .= "<hr><br>";
1035 $ret .= show_notify($noty, 3000, "torna alla partita");
1042 function show_room(&$bri, &$user)
1044 $ret .= sprintf('gst.st = %d;', $user->step);
1045 $ret .= sprintf('stat = "%s";', $user->stat);
1046 $ret .= sprintf('subst = "%s";', $user->subst);
1047 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1048 for ($i = 0 ; $i < TABLES_N ; $i++) {
1049 $ret .= table_content($bri, $user, $i);
1050 $act_content = table_act_content(($user->subst == 'standup'),
1051 $bri->table[$i]->player_n, $i, $user->table);
1052 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1054 $ret .= standup_content($bri, $user);
1062 is_transition (is from room to table ?)
1063 is_again (is another game)
1065 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1067 $table_idx = $user->table;
1068 $table = &$bri->table[$table_idx];
1077 cards dnd (and gameable card if its turn)
1082 $ret = "table_init();";
1085 /* GENERAL STATUS */
1086 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1087 $sendstep, $user->stat, $user->subst, $user->table_pos);
1089 $ret .= "background_set();";
1092 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1093 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1094 xcape($bri->user[$table->player[($user->table_pos)%PLAYERS_N]]->name),
1095 xcape($bri->user[$table->player[($user->table_pos+1)%PLAYERS_N]]->name),
1096 xcape($bri->user[$table->player[($user->table_pos+2)%PLAYERS_N]]->name),
1097 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($user->table_pos+3)%PLAYERS_N]]->name)),
1098 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($user->table_pos+4)%PLAYERS_N]]->name)));
1100 /* NOTIFY FOR THE CARD MAKER */
1101 if ($is_transition) { // && $user->subst == "asta" superfluo
1102 $ret .= show_table_info(&$bri, &$table, $user->table_pos);
1106 if ($is_transition) { // && $user->subst == "asta" superfluo
1109 for ($i = 0 ; $i < 8 ; $i++) {
1110 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1112 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1113 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1119 log_rd($sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1121 $ret .= sprintf( ' card_send(%d,%d,%d,%f,%d);|', ($table->gstart + $e) % PLAYERS_N,
1122 $i, ((($e + PLAYERS_N - $user->table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1123 $table->card[$o]->value : -1),
1124 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1129 $taked = array(0,0,0,0,0);
1130 $inhand = array(0,0,0,0,0);
1131 $ontabl = array(-1,-1,-1,-1,-1);
1134 for ($i = 0 ; $i < 40 ; $i++) {
1135 if ($table->card[$i]->stat == 'hand') {
1136 if ($table->card[$i]->owner == $user->table_pos) {
1137 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1139 $inhand[$table->card[$i]->owner]++;
1141 else if ($table->card[$i]->stat == 'take') {
1142 log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1143 $taked[$table->card[$i]->owner]++;
1145 else if ($table->card[$i]->stat == 'table') {
1146 $ontabl[$table->card[$i]->owner] = $i;
1150 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1151 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1153 log_main("Stat table:", $logg);
1155 /* Set ours cards. */
1157 for ($i = 0 ; $i < $inhand[$user->table_pos] ; $i++)
1158 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1159 for ($i = $inhand[$user->table_pos] ; $i < 8 ; $i++)
1160 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1161 $ret .= sprintf('card_setours(%s);', $oursarg);
1163 /* Dispose all cards */
1164 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1165 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1166 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1167 $inhand[$i], $taked[$i]);
1169 if ($ontabl[$i] != -1) {
1170 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1171 $table->card[$ontabl[$i]]->value,
1172 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1178 if ($user->subst == 'asta') {
1180 /* show users auction status */
1182 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1183 $user_cur = &$bri->user[$table->player[$i]];
1184 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1185 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1188 $showst .= ",-2,-2";
1189 $ret .= sprintf('show_astat(%s);', $showst);
1191 if ($table->asta_win != -1 && $table->asta_win == $user->table_pos) {
1192 /* show card chooser */
1193 $ret .= sprintf('choose_seed(%s); $("asta").style.visibility = "hidden";',
1198 if ($user->table_pos == ($table->gstart % PLAYERS_N) &&
1199 $table->asta_win == -1)
1200 $ret .= sprintf('dispose_asta(%d,%d);',
1201 $table->asta_card + 1, $table->asta_pnt+1);
1203 $ret .= sprintf('dispose_asta(%d,%d);',
1204 $table->asta_card + 1, -($table->asta_pnt+1));
1208 if ($table->asta_win == -1) { // auction case
1209 if ($user->table_pos == ($table->gstart % PLAYERS_N))
1210 $ret .= "remark_on();";
1212 $ret .= "remark_off();";
1214 else { // chooseed case
1215 if ($user->table_pos == $table->asta_win)
1216 $ret .= "remark_on();";
1218 $ret .= "remark_off();";
1221 else if ($user->subst == 'game') {
1223 if (($table->gstart + $table->turn) % PLAYERS_N == $user->table_pos)
1224 $ret .= "is_my_time = true; remark_on();";
1226 $ret .= "remark_off();";
1228 /* WHO CALL AND WATH */
1229 $ret .= briscola_show($bri, $table, $user);
1235 function calculate_winner(&$table)
1242 $cur_seed = $table->briscola - ($table->briscola % 10);
1244 for ($i = 0 ; $i < 40 ; $i++) {
1245 if ($table->card[$i]->stat != "table")
1248 log_wr($sess, sprintf("Card On table: [%d]", $i));
1250 $v = $table->card[$i]->value;
1251 $ontab[$table->card[$i]->owner] = $v;
1252 $ontid[$table->card[$i]->owner] = $i;
1253 /* se briscola setto il flag */
1254 if (($v - ($v % 10)) == $cur_seed)
1258 if ($briontab == FALSE) {
1259 $cur_win = $table->gstart;
1260 $cur_val = $ontab[$cur_win];
1261 $cur_seed = $cur_val - ($cur_val % 10);
1264 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1265 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1266 if ($ontab[$i] < $cur_val) {
1267 $cur_val = $ontab[$i];
1273 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1274 $table->card[$ontid[$i]]->owner = $cur_win;
1275 $table->card[$ontid[$i]]->stat = "take";
1280 function calculate_points(&$table)
1282 $all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
1286 if ($table->asta_pnt == 60)
1287 $table->asta_pnt = 61;
1289 $table->old_win = $table->asta_win;
1290 $table->old_friend = $table->friend;
1291 $table->old_asta_pnt = $table->asta_pnt;
1293 for ($i = 0 ; $i < 40 ; $i++) {
1294 $ctt = $table->card[$i]->value % 10;
1295 $own = $table->card[$i]->owner;
1296 if ($own == $table->asta_win || $own == $table->friend)
1297 $pro += $all_points[$ctt];
1300 log_wr("XXX", sprintf("PRO: [%d]", $pro));
1303 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1304 $table->points[$table->points_n % MAX_POINTS] = array();
1305 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1306 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1308 $table->old_pnt = $pro;
1314 if ($pro >= $table->asta_pnt)
1319 $table->points[$table->points_n % MAX_POINTS] = array();
1320 for ($i = 0 ; $i < 5 ; $i++) {
1321 if ($i == $table->asta_win)
1322 $pt = ($i == $table->friend ? 4 : 2);
1323 else if ($i == $table->friend)
1328 log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1330 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1332 log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1334 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1335 $table->total[$i] += $pt;
1338 $table->old_pnt = $pro;