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 function &get_user($sess, &$idx)
566 if (strlen($sess) == SESS_LEN) {
567 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
568 if (strcmp($sess, $this->user[$i]->sess) == 0) {
571 return ($this->user[$i]);
574 log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
575 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
576 // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
579 log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
585 * function &add_user(&$bri, &$sess, &$idx, $name)
588 * if ($idx != -1 && ret == FALSE) => duplicated nick
589 * if ($idx == -1 && ret == FALSE) => no space left
590 * if (ret == TRUE) => SUCCESS
592 function &add_user(&$sess, &$idx, $name)
597 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
598 if (validate_sess($sess) == FALSE)
601 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
603 if (strcmp($sess, $this->user[$i]->sess) == 0) {
607 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
610 if (strcmp($this->user[$i]->name, $name) == 0) {
618 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name));
620 if ($idx != -1 && $i == MAX_PLAYERS) {
623 $this->user[$idx]->sess = uniqid("");
624 $sess = $this->user[$idx]->sess;
628 $this->user[$idx]->sess = $sess;
630 $this->user[$idx]->name = $name;
631 $this->user[$idx]->stat = "room";
632 $this->user[$idx]->subst = "standup";
634 log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name));
636 return ($this->user[$idx]);
644 } // end class brisco
648 list($usec, $sec) = explode(' ', microtime());
649 return (float) $sec + ((float) $usec * 100000);
652 function log_main($sess, $log) {
653 if (BRISK_DEBUG != TRUE)
656 $fp = fopen("/tmp/brisk_main.log", 'a');
657 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
661 function log_rd($sess, $log) {
662 if (BRISK_DEBUG != TRUE)
665 $fp = fopen("/tmp/brisk_rd.log", 'a');
666 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
670 function log_rd2($sess, $log) {
671 if (BRISK_DEBUG != TRUE)
674 $fp = fopen("/tmp/brisk_rd2.log", 'a');
675 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
679 function log_send($sess, $log) {
680 if (BRISK_DEBUG != TRUE)
683 $fp = fopen("/tmp/brisk_send.log", 'a');
684 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
688 function log_auth($sess, $log) {
689 if (BRISK_DEBUG != TRUE)
692 $fp = fopen("/tmp/brisk_auth.log", 'a');
693 fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
697 function log_wr($sess, $log) {
698 if (BRISK_DEBUG != TRUE)
701 $fp = fopen("/tmp/brisk_wr.log", 'a');
702 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
706 function log_load($sess, $log) {
707 if (BRISK_DEBUG != TRUE)
710 $fp = fopen("/tmp/brisk_load.log", 'a');
711 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
717 $brisco = new brisco();
724 // echo "LOCK: ".FTOK_PATH."/main";
726 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
730 // echo "FTOK ".$tok."<br>";
731 if (($res = sem_get($tok)) == FALSE) {
732 echo "SEM_GET FAILED";
735 if (sem_acquire($res))
741 function unlock_data($res)
743 return (sem_release($res));
747 function &load_data()
749 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
754 if ($shm = shm_attach($tok,100000 * TABLES_N)) {
755 if(($bri = @shm_get_var($shm, $tok)) == false) {
756 log_main("XXX", "INIT MAIN DATA");
759 shm_put_var($shm, $tok, $bri);
771 function save_data(&$bri)
779 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1)
785 if (($shm = shm_attach($tok,100000 * TABLES_N)) == FALSE)
788 if (shm_put_var($shm, $tok, $bri) == FALSE)
790 // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
800 function table_act_content($isstanding, $sitted, $table, $cur_table)
805 if ($sitted < PLAYERS_N) {
806 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
810 if ($table == $cur_table)
811 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
818 function table_content($bri, $user, $table_idx)
824 // Si possono usare i dati nella classe table
828 $table = &$bri->table[$table_idx];
830 if ($user->stat != 'room')
833 for ($i = 0 ; $i < $table->player_n ; $i++) {
834 $user_cur = &$bri->user[$table->player[$i]];
836 if ($user_cur == $user)
837 { $hilion = "<b>"; $hilioff = "</b>"; }
839 { $hilion = ""; $hilioff = ""; }
841 log_main($bri->user[$e]->name, sprintf("IN TABLE [%d]", $table_idx));
843 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
846 for ( ; $i < PLAYERS_N ; $i++)
850 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
855 function standup_content(&$bri, $user)
860 if ($user->stat != 'room')
863 $content .= '<table class=\\"table_standup\\">';
864 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
865 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
869 if ($bri->user[$e]->subst == "standup") {
870 if (($ct % 4) == 0) {
873 if ($bri->user[$e] == $user)
874 { $hilion = "<b>"; $hilioff = "</b>"; }
876 { $hilion = ""; $hilioff = ""; }
878 $content .= sprintf('<td style=\\"text-align: center\\">%s%s%s</td>',$hilion, xcape($bri->user[$e]->name), $hilioff);
879 if (($ct % 4) == 3) {
885 $content .= '</table>';
887 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
888 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
889 $content, $content2);
894 function standup_update(&$bri, &$user)
896 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
897 $user_cur = &$bri->user[$i];
898 if ($user_cur->sess == '')
901 log_main("STANDUP START", $user_cur->stat);
903 if ($user_cur->stat == 'room') {
904 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($bri, $user_cur);
905 if ($user_cur == $user)
906 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
908 log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
919 function show_notify($text, $tout, $butt)
921 log_main("SHOW_NOTIFY", $text);
922 return sprintf('var noti = new notify(gst,$("bg"),"%s",%d,"%s");', $text, $tout,$butt);
928 function briscola_show($bri, $table, $user)
933 if ($table->asta_card == 9)
934 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
936 /* text of caller cell */
937 if ($user->table_pos == $table->asta_win)
938 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
940 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
941 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
943 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
945 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
946 $ret .= sprintf('$("caller").style.visibility = "visible";');
947 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
948 $ret .= sprintf('$("asta").style.visibility = "hidden";');
949 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
955 function game_result($asta_pnt, $pnt)
957 if ($asta_pnt == 61) {
966 if ($pnt >= $asta_pnt)
973 function multoval($mult)
978 return ("quadruplo");
980 return (sprintf("%d-plo", $mult));
983 function show_table_info(&$bri, &$table, $table_pos)
985 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
986 $noty = sprintf('<table class=\"points\"><tr><th></th>');
989 for ($i = 0 ; $i < PLAYERS_N ; $i++)
990 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
991 $noty .= sprintf("</tr>");
994 log_main("show_table_info", "pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
996 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
997 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
998 for ($e = 0 ; $e < PLAYERS_N ; $e++)
999 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1004 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1005 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1006 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1007 $noty .= "</tr></table>";
1009 if ($table->old_win != -1) {
1010 $win = $table->player[$table->old_win];
1011 $fri = $table->player[$table->old_friend];
1013 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1016 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
1017 xcape($bri->user[$win]->name),
1018 xcape($bri->user[$fri]->name));
1019 if ($table->old_pnt == 120) {
1020 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1023 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1024 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1025 'più di 60'), $table->old_pnt,
1026 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1030 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1031 xcape($bri->user[$win]->name));
1032 if ($table->old_pnt == 120) {
1033 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1036 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1037 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1038 'più di 60'), $table->old_pnt,
1039 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1043 if ($table->mazzo == $table_pos)
1044 $noty .= "Fai <b>tu</b> il mazzo.";
1046 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1047 $noty .= "Il mazzo a <b>$unam</b>.";
1050 if ($table->mult > 1) {
1051 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1053 $noty .= "<hr><br>";
1055 $ret .= show_notify($noty, 3000, "torna alla partita");
1062 function show_room(&$bri, &$user)
1064 $ret .= sprintf('gst.st = %d;', $user->step);
1065 $ret .= sprintf('stat = "%s";', $user->stat);
1066 $ret .= sprintf('subst = "%s";', $user->subst);
1067 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1068 for ($i = 0 ; $i < TABLES_N ; $i++) {
1069 $ret .= table_content($bri, $user, $i);
1070 $act_content = table_act_content(($user->subst == 'standup'),
1071 $bri->table[$i]->player_n, $i, $user->table);
1072 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1074 $ret .= standup_content($bri, $user);
1082 is_transition (is from room to table ?)
1083 is_again (is another game)
1085 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1087 $table_idx = $user->table;
1088 $table = &$bri->table[$table_idx];
1097 cards dnd (and gameable card if its turn)
1102 $ret = "table_init();";
1105 /* GENERAL STATUS */
1106 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1107 $sendstep, $user->stat, $user->subst, $user->table_pos);
1109 $ret .= "background_set();";
1112 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1113 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1114 xcape($bri->user[$table->player[($user->table_pos)%PLAYERS_N]]->name),
1115 xcape($bri->user[$table->player[($user->table_pos+1)%PLAYERS_N]]->name),
1116 xcape($bri->user[$table->player[($user->table_pos+2)%PLAYERS_N]]->name),
1117 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($user->table_pos+3)%PLAYERS_N]]->name)),
1118 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($user->table_pos+4)%PLAYERS_N]]->name)));
1120 /* NOTIFY FOR THE CARD MAKER */
1121 if ($is_transition) { // && $user->subst == "asta" superfluo
1122 $ret .= show_table_info(&$bri, &$table, $user->table_pos);
1126 if ($is_transition) { // && $user->subst == "asta" superfluo
1129 for ($i = 0 ; $i < 8 ; $i++) {
1130 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1132 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1133 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1139 log_rd($sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1141 $ret .= sprintf( ' card_send(%d,%d,%d,%f,%d);|', ($table->gstart + $e) % PLAYERS_N,
1142 $i, ((($e + PLAYERS_N - $user->table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1143 $table->card[$o]->value : -1),
1144 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1149 $taked = array(0,0,0,0,0);
1150 $inhand = array(0,0,0,0,0);
1151 $ontabl = array(-1,-1,-1,-1,-1);
1154 for ($i = 0 ; $i < 40 ; $i++) {
1155 if ($table->card[$i]->stat == 'hand') {
1156 if ($table->card[$i]->owner == $user->table_pos) {
1157 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1159 $inhand[$table->card[$i]->owner]++;
1161 else if ($table->card[$i]->stat == 'take') {
1162 log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1163 $taked[$table->card[$i]->owner]++;
1165 else if ($table->card[$i]->stat == 'table') {
1166 $ontabl[$table->card[$i]->owner] = $i;
1170 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1171 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1173 log_main("Stat table:", $logg);
1175 /* Set ours cards. */
1177 for ($i = 0 ; $i < $inhand[$user->table_pos] ; $i++)
1178 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1179 for ($i = $inhand[$user->table_pos] ; $i < 8 ; $i++)
1180 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1181 $ret .= sprintf('card_setours(%s);', $oursarg);
1183 /* Dispose all cards */
1184 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1185 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1186 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1187 $inhand[$i], $taked[$i]);
1189 if ($ontabl[$i] != -1) {
1190 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1191 $table->card[$ontabl[$i]]->value,
1192 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1198 if ($user->subst == 'asta') {
1200 /* show users auction status */
1202 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1203 $user_cur = &$bri->user[$table->player[$i]];
1204 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1205 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1208 $showst .= ",-2,-2";
1209 $ret .= sprintf('show_astat(%s);', $showst);
1211 if ($table->asta_win != -1 && $table->asta_win == $user->table_pos) {
1212 /* show card chooser */
1213 $ret .= sprintf('choose_seed(%s); $("asta").style.visibility = "hidden";',
1218 if ($user->table_pos == ($table->gstart % PLAYERS_N) &&
1219 $table->asta_win == -1)
1220 $ret .= sprintf('dispose_asta(%d,%d);',
1221 $table->asta_card + 1, $table->asta_pnt+1);
1223 $ret .= sprintf('dispose_asta(%d,%d);',
1224 $table->asta_card + 1, -($table->asta_pnt+1));
1228 if ($table->asta_win == -1) { // auction case
1229 if ($user->table_pos == ($table->gstart % PLAYERS_N))
1230 $ret .= "remark_on();";
1232 $ret .= "remark_off();";
1234 else { // chooseed case
1235 if ($user->table_pos == $table->asta_win)
1236 $ret .= "remark_on();";
1238 $ret .= "remark_off();";
1241 else if ($user->subst == 'game') {
1243 if (($table->gstart + $table->turn) % PLAYERS_N == $user->table_pos)
1244 $ret .= "is_my_time = true; remark_on();";
1246 $ret .= "remark_off();";
1248 /* WHO CALL AND WATH */
1249 $ret .= briscola_show($bri, $table, $user);
1255 function calculate_winner(&$table)
1262 $cur_seed = $table->briscola - ($table->briscola % 10);
1264 for ($i = 0 ; $i < 40 ; $i++) {
1265 if ($table->card[$i]->stat != "table")
1268 log_wr($sess, sprintf("Card On table: [%d]", $i));
1270 $v = $table->card[$i]->value;
1271 $ontab[$table->card[$i]->owner] = $v;
1272 $ontid[$table->card[$i]->owner] = $i;
1273 /* se briscola setto il flag */
1274 if (($v - ($v % 10)) == $cur_seed)
1278 if ($briontab == FALSE) {
1279 $cur_win = $table->gstart;
1280 $cur_val = $ontab[$cur_win];
1281 $cur_seed = $cur_val - ($cur_val % 10);
1284 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1285 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1286 if ($ontab[$i] < $cur_val) {
1287 $cur_val = $ontab[$i];
1293 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1294 $table->card[$ontid[$i]]->owner = $cur_win;
1295 $table->card[$ontid[$i]]->stat = "take";
1300 function calculate_points(&$table)
1302 $all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
1306 if ($table->asta_pnt == 60)
1307 $table->asta_pnt = 61;
1309 $table->old_win = $table->asta_win;
1310 $table->old_friend = $table->friend;
1311 $table->old_asta_pnt = $table->asta_pnt;
1313 for ($i = 0 ; $i < 40 ; $i++) {
1314 $ctt = $table->card[$i]->value % 10;
1315 $own = $table->card[$i]->owner;
1316 if ($own == $table->asta_win || $own == $table->friend)
1317 $pro += $all_points[$ctt];
1320 log_wr("XXX", sprintf("PRO: [%d]", $pro));
1323 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1324 $table->points[$table->points_n % MAX_POINTS] = array();
1325 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1326 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1328 $table->old_pnt = $pro;
1334 if ($pro >= $table->asta_pnt)
1339 $table->points[$table->points_n % MAX_POINTS] = array();
1340 for ($i = 0 ; $i < 5 ; $i++) {
1341 if ($i == $table->asta_win)
1342 $pt = ($i == $table->friend ? 4 : 2);
1343 else if ($i == $table->friend)
1348 log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1350 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1352 log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1354 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1355 $table->total[$i] += $pt;
1358 $table->old_pnt = $pro;
1362 function validate_sess($sess) {
1363 if (strlen($sess) == SESS_LEN)