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");
38 $root_wellarr = Array ( '<a href="http://www.google.com">GOOGLE</a>',
42 $root_wellarr = Array ( );
43 $table_wellarr = Array ( );
45 $G_brisk_version = "0.4.2";
48 Brisk - Ver. '.$G_brisk_version.'<br><br>
49 Copyright 2006 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a><br><br>';
51 $G_room_help = '<b>Brisk - Ver. '.$G_brisk_version.'</b><br><br>Copyright 2006 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a><br><br>';
56 $from = array ( '\\', '@', '|' );
57 $to = array ( '\\\\', '@', '¦' );
59 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
64 var $value; /* 0 - 39 card value */
65 var $stat; /* 'bunch', 'hand', 'table', 'take' */
66 var $owner; /* (table position 0-4) */
67 // var $pos; /* Pos in hand. */
68 var $x; /* When played the X position on the table of the owner. */
69 var $y; /* When played the Y position on the table of the owner. */
71 function Card($value, $stat, $owner)
73 $this->value = $value;
75 $this->owner = $owner;
78 function assign($stat,$owner)
81 $this->owner = $owner;
91 $this->stat = 'table';
96 function take($newown)
99 $this->owner = $newown;
117 var $points; // points array
118 var $points_n; // number of row of points
132 $this->player = array();
134 $this->card = &$this->bunch_create();
135 $this->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
136 $this->asta_pla_n= -1;
137 $this->asta_card = -1;
138 $this->asta_pnt = -1;
140 $this->points = array( );
142 $this->total = array( 0, 0, 0, 0, 0);
143 $this->asta_win = -1;
144 $this->briscola = -1;
147 $this->old_asta_pnt = -1;
150 $this->old_friend= -1;
154 function &bunch_create()
158 for ($i = 0 ; $i < 40 ; $i++) {
159 $ret[$i] = new Card($i, 'bunch', 'no_owner');
165 function bunch_make()
167 $ct = array(0,0,0,0,0);
169 mt_srand(make_seed());
171 for ($i = 39 ; $i >= 0 ; $i--)
174 for ($i = 39 ; $i >= 0 ; $i--) {
178 log_main("RND ZERO", "");
183 $this->card[$id]->assign('hand', $owner);
185 $rest[$rn] = $rest[$i];
186 $pubbpos[$rn2] = $pubbpos[$i];
192 $this->mazzo = rand(0,PLAYERS_N-1);
196 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
197 $this->total[$i] = 0;
200 log_main("table::init","ci siamo");
203 function game_init(&$bri)
205 log_rd2($sess,"GSTART 4");
207 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
211 $this->asta_pla_n = PLAYERS_N;
212 $this->asta_card = -1;
213 $this->asta_pnt = 60;
214 $this->asta_win = -1;
215 $this->briscola = -1;
219 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
220 $this->asta_pla[$i] = TRUE;
221 $user_cur = &$bri->user[$this->player[$i]];
222 $user_cur->subst = 'asta';
223 $user_cur->asta_card = -2;
224 $user_cur->asta_pnt = -1;
230 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
233 function getPlayer($idx)
235 return ($this->player[$idx]);
238 function setPlayer($idx, $player)
240 $this->player[$idx] = $player;
243 function user_add($idx)
245 $this->player[$this->player_n] = $idx;
248 return ($this->player_n - 1);
251 function user_rem(&$bri, &$user)
253 $tabpos = $user->table_pos;
255 /* verifico la consistenza dei dati */
256 if ($bri->user[$this->player[$tabpos]] == $user) {
258 /* aggiorna l'array dei giocatori al tavolo. */
259 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
260 $this->player[$i] = $this->player[$i+1];
261 $user_cur = &$bri->user[$this->player[$i]];
262 $user_cur->table_pos = $i;
267 log_main($user->sess, "INCONSISTENCY ON TABLE.");
274 var $name; // name of the user
275 var $sess; // session of the user
276 var $lacc; // last access (for the cleanup)
277 var $stat; // status (outdoor, room, table, game, ...)
278 var $subst; // substatus for each status
279 var $step; // step of the current status
280 var $trans_step; // step to enable transition between pages (disable == -1)
281 var $comm; // commands array
285 var $table; // id of the current table (if in table state)
286 var $table_pos; // idx on the table
288 function User($name, $sess, $stat = "", $subst = "", $table = -1) {
291 $this->lacc = time();
293 $this->subst = $subst;
295 $this->trans_step = -1;
296 $this->comm = array();
297 $this->asta_card = -2;
298 $this->asta_pnt = -1;
300 $this->table = $table;
307 var $comm; // commands for many people
308 var $step; // current step of the comm array
309 var $garbage_timeout;
312 $this->user = array();
314 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
315 $this->user[$i] = new User("", "");
317 for ($i = 0 ; $i < TABLES_N ; $i++)
318 $this->table[$i] = new Table();
319 $this->garbage_timeout = 0;
322 function garbage_manager($force)
325 /* Garbage collector degli utenti in timeout */
327 if ($force || $this->garbage_timeout < $curtime) {
329 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
330 $user_cur = &$this->user[$i];
331 if ($user_cur->sess == "")
334 if ($user_cur->lacc < $curtime) { // Auto logout dell'utente
335 log_rd2($user_cur->sess, "AUTO LOGOUT.");
337 if ($user_cur->stat == 'table') {
338 log_rd2($user_cur->sess, "AUTO LOGOUT: Yet not implemented in table stat!");
343 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
344 log_auth($user_cur->sess, "Autologout session.");
346 $user_cur->sess = "";
347 $user_cur->name = "";
348 $user_cur->the_end = FALSE;
350 log_rd2($user_cur->sess, "AUTO LOGOUT.");
351 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
352 $this->room_wakeup(&$user_cur);
353 else if ($user_cur->subst == 'standup')
354 $this->room_outstandup(&$user_cur);
356 log_rd2($sess, "LOGOUT FROM WHAT ???");
362 log_rd2($user_cur->sess, "GARBAGE UPDATED!");
364 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
369 function room_wakeup(&$user)
371 $table_idx = $user->table;
372 $table = &$this->table[$table_idx];
374 log_main("WAKEUP", "begin function table:".$table_idx);
376 $from_table = ($user->stat == "table");
378 log_main("WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
380 for ($i = 0 ; $i < $table->player_n ; $i++) {
381 $user_cur = &$this->user[$table->player[$i]];
382 log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
384 if ($user_cur != $user) {
385 $user_cur->stat = "room";
386 $user_cur->subst = "sitdown";
388 else if ($user->sess != "") {
389 $user_cur->stat = "room";
390 $user_cur->subst = "standup";
391 $user_cur->table = -1;
396 /* aggiorna l'array dei giocatori al tavolo. */
397 $table->user_rem(&$this, &$user);
399 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
400 $user_cur = &$this->user[$i];
401 if ($user_cur->sess == '' || $user_cur->stat != 'room')
404 log_main("VALORI", "name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
406 // function show_room(&$bri, &$user)
408 $ret = "gst.st = ".($user_cur->step+1)."; ";
409 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
410 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
411 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
412 log_main("DOCUMENT.index.php", "from table");
414 else if ($user_cur->stat == "room") {
415 $ret .= table_content($this, $user_cur, $table_idx);
416 $ret .= standup_content($this, $user_cur);
418 $act_content = table_act_content(FALSE, 0, $e, $user->table);
419 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
422 if ($user_cur == $user) {
423 // set the new status
424 $ret .= 'subst = "standup"; ';
425 // clean the action buttons in other tables
426 for ($e = 0 ; $e < TABLES_N ; $e++) {
427 if ($this->table[$e]->player_n < PLAYERS_N)
428 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
432 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
433 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
436 log_wr($user_cur->sess, "ROOM_WAKEUP: ".$ret);
437 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
445 function room_outstandup(&$user)
447 $this->room_sitdown(&$user, -1);
450 function table_update(&$user)
452 log_main("table_update", "pre - USER: ".$user->name);
454 $table_idx = $user->table;
457 $table = &$this->table[$table_idx];
459 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
461 $user_cur = &$this->user[$i];
462 if ($user_cur->sess == '' || $user_cur->stat != 'room')
465 $ret = "gst.st = ".($user_cur->step+1)."; ";
467 $ret .= table_content($this, $user_cur, $table_idx);
469 if ($user_cur == $user) {
470 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
472 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
476 log_main("table_update", "post");
479 function room_sitdown(&$user, $table_idx)
481 log_main("room_sitdown", ($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
484 $table = &$this->table[$table_idx];
486 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
488 $user_cur = &$this->user[$i];
489 if ($user_cur->sess == '' || $user_cur->stat != 'room')
492 $ret = "gst.st = ".($user_cur->step+1)."; ";
494 $ret .= table_content($this, $user_cur, $table_idx);
495 $ret .= standup_content($this, $user_cur);
497 if ($user_cur == $user) {
498 $ret .= 'subst = "sitdown"; ';
499 // clean the action buttons in other tables
500 for ($e = 0 ; $e < TABLES_N ; $e++) {
501 $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
502 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
505 else if ($table_idx > -1) {
506 if ($table->player_n == PLAYERS_N) {
507 $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
508 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
511 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
516 function chatt_send(&$user, $mesg)
518 if ($user->stat == 'table') {
519 $table = &$this->table[$user->table];
522 $user_mesg = substr($mesg,6);
524 $dt = date("H:i ",time());
525 if (strncmp($user_mesg, "/nick ", 6) == 0) {
526 log_main($user->sess, "chatt_send BEGIN");
528 $name_new = str_replace(' ', '_', substr(trim(substr($user_mesg, 6)),0,12));
529 $user_mesg = "COMMAND ".$user_mesg;
532 // update local graph
533 // update remote graphs
534 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
535 $user_cur = &$this->user[$i];
536 if ($user_cur->sess == '' || $user_cur->stat != 'room')
538 if ($user_cur->name == $name_new) {
539 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
540 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
545 if ($i == MAX_PLAYERS) {
546 $user->name = $name_new;
548 log_main($user->sess, "chatt_send start set");
551 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
552 log_main($user->sess, "chatt_send set loop");
554 $user_cur = &$this->user[$i];
555 if ($user_cur->sess == '')
557 if ($user_cur->stat == 'room') {
558 if ($user->stat == 'room' && $user->subst == 'standup') {
559 $this->standup_update(&$user);
561 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
562 $user->stat == 'table') {
563 log_main($user->sess, "chatt_send pre table update");
565 $this->table_update(&$user);
567 log_main($user->sess, "chatt_send post table update");
570 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
571 $table = &$this->table[$user->table];
573 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
574 $user_cur->comm[$user_cur->step % COMM_N] = sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
575 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
576 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
577 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
578 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
579 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
580 if ($user_cur == $user)
581 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
582 xcape($user->name,ENT_COMPAT,"UTF-8"));
589 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
590 if ($user->stat == 'room') {
591 $user_cur = &$this->user[$i];
592 if ($user_cur->sess == '' || $user_cur->stat != 'room')
596 $user_cur = &$this->user[$table->player[$i]];
599 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
600 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
601 $dt.xcape($user->name), xcape($user_mesg));
607 function &get_user($sess, &$idx)
611 if (strlen($sess) == SESS_LEN) {
612 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
613 if (strcmp($sess, $this->user[$i]->sess) == 0) {
616 return ($this->user[$i]);
619 log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
620 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
621 // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
624 log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
630 * function &add_user(&$bri, &$sess, &$idx, $name)
633 * if ($idx != -1 && ret == FALSE) => duplicated nick
634 * if ($idx == -1 && ret == FALSE) => no space left
635 * if (ret == TRUE) => SUCCESS
637 function &add_user(&$sess, &$idx, $name)
642 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
643 if (validate_sess($sess) == FALSE)
646 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
648 if (strcmp($sess, $this->user[$i]->sess) == 0) {
652 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
655 if (strcmp($this->user[$i]->name, $name) == 0) {
663 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name));
665 if ($idx != -1 && $i == MAX_PLAYERS) {
668 $this->user[$idx]->sess = uniqid("");
669 $sess = $this->user[$idx]->sess;
673 $this->user[$idx]->sess = $sess;
675 $this->user[$idx]->name = $name;
676 $this->user[$idx]->stat = "room";
677 $this->user[$idx]->subst = "standup";
679 log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name));
681 return ($this->user[$idx]);
687 function standup_update(&$user)
689 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
690 $user_cur = &$this->user[$i];
691 if ($user_cur->sess == '')
694 log_main("STANDUP START", $user_cur->stat);
696 if ($user_cur->stat == 'room') {
697 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
698 if ($user_cur == $user)
699 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
701 log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
709 } // end class brisco
713 list($usec, $sec) = explode(' ', microtime());
714 return (float) $sec + ((float) $usec * 100000);
717 function log_main($sess, $log) {
718 if (BRISK_DEBUG != TRUE)
721 $fp = fopen("/tmp/brisk_main.log", 'a');
722 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
726 function log_rd($sess, $log) {
727 if (BRISK_DEBUG != TRUE)
730 $fp = fopen("/tmp/brisk_rd.log", 'a');
731 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
735 function log_rd2($sess, $log) {
736 if (BRISK_DEBUG != TRUE)
739 $fp = fopen("/tmp/brisk_rd2.log", 'a');
740 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
744 function log_send($sess, $log) {
745 if (BRISK_DEBUG != TRUE)
748 $fp = fopen("/tmp/brisk_send.log", 'a');
749 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
753 function log_auth($sess, $log) {
754 if (BRISK_DEBUG != TRUE)
757 $fp = fopen("/tmp/brisk_auth.log", 'a');
758 fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
762 function log_wr($sess, $log) {
763 if (BRISK_DEBUG != TRUE)
766 $fp = fopen("/tmp/brisk_wr.log", 'a');
767 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
771 function log_load($sess, $log) {
772 if (BRISK_DEBUG != TRUE)
775 $fp = fopen("/tmp/brisk_load.log", 'a');
776 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
782 $brisco = new brisco();
789 // echo "LOCK: ".FTOK_PATH."/main";
791 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
795 // echo "FTOK ".$tok."<br>";
796 if (($res = sem_get($tok)) == FALSE) {
797 echo "SEM_GET FAILED";
800 if (sem_acquire($res))
806 function unlock_data($res)
808 return (sem_release($res));
812 function &load_data()
814 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
819 if ($shm = shm_attach($tok,100000 * TABLES_N)) {
820 if(($bri = @shm_get_var($shm, $tok)) == false) {
821 log_main("XXX", "INIT MAIN DATA");
824 shm_put_var($shm, $tok, $bri);
836 function save_data(&$bri)
844 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1)
850 if (($shm = shm_attach($tok,100000 * TABLES_N)) == FALSE)
853 if (shm_put_var($shm, $tok, $bri) == FALSE)
855 // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
865 function table_act_content($isstanding, $sitted, $table, $cur_table)
870 if ($sitted < PLAYERS_N) {
871 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
875 if ($table == $cur_table)
876 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
883 function table_content($bri, $user, $table_idx)
889 // Si possono usare i dati nella classe table
893 $table = &$bri->table[$table_idx];
895 if ($user->stat != 'room')
898 for ($i = 0 ; $i < $table->player_n ; $i++) {
899 $user_cur = &$bri->user[$table->player[$i]];
901 if ($user_cur == $user)
902 { $hilion = "<b>"; $hilioff = "</b>"; }
904 { $hilion = ""; $hilioff = ""; }
906 log_main($bri->user[$e]->name, sprintf("IN TABLE [%d]", $table_idx));
908 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
911 for ( ; $i < PLAYERS_N ; $i++)
915 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
920 function standup_content(&$bri, $user)
925 if ($user->stat != 'room')
928 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
929 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
934 $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
936 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
937 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
941 if ($bri->user[$e]->subst == "standup") {
942 if (($ct % 4) == 0) {
945 if ($bri->user[$e] == $user)
946 { $hilion = "<b>"; $hilioff = "</b>"; }
948 { $hilion = ""; $hilioff = ""; }
950 $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($bri->user[$e]->name), $hilioff);
951 if (($ct % 4) == 3) {
957 $content .= '</table>';
959 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
960 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
961 $content, $content2);
970 function show_notify($anc, $text, $tout, $butt)
972 log_main("SHOW_NOTIFY", $text);
973 return sprintf('var noti = new notify(gst,$("%s"),"%s",%d,"%s");', $anc, $text, $tout,$butt);
979 function briscola_show($bri, $table, $user)
984 if ($table->asta_card == 9)
985 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
987 /* text of caller cell */
988 if ($user->table_pos == $table->asta_win)
989 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
991 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
992 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
994 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
996 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
997 $ret .= sprintf('$("caller").style.visibility = "visible";');
998 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
999 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1000 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1006 function game_result($asta_pnt, $pnt)
1008 if ($asta_pnt == 61) {
1011 else if ($pnt == 60)
1017 if ($pnt >= $asta_pnt)
1024 function multoval($mult)
1028 else if ($mult == 4)
1029 return ("quadruplo");
1031 return (sprintf("%d-plo", $mult));
1034 function show_table_info(&$bri, &$table, $table_pos)
1036 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1037 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1040 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1041 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1042 $noty .= sprintf("</tr>");
1045 log_main("show_table_info", "pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1047 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1048 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1049 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1050 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1055 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1056 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1057 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1058 $noty .= "</tr></table>";
1060 if ($table->old_win != -1) {
1061 $win = $table->player[$table->old_win];
1062 $fri = $table->player[$table->old_friend];
1064 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1067 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
1068 xcape($bri->user[$win]->name),
1069 xcape($bri->user[$fri]->name));
1070 if ($table->old_pnt == 120) {
1071 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1074 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1075 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1076 'più di 60'), $table->old_pnt,
1077 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1081 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1082 xcape($bri->user[$win]->name));
1083 if ($table->old_pnt == 120) {
1084 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1087 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1088 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1089 'più di 60'), $table->old_pnt,
1090 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1094 if ($table->mazzo == $table_pos)
1095 $noty .= "Fai <b>tu</b> il mazzo.";
1097 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1098 $noty .= "Il mazzo a <b>$unam</b>.";
1101 if ($table->mult > 1) {
1102 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1104 $noty .= "<hr><br>";
1106 $ret .= show_notify("bg", $noty, 3000, "torna alla partita");
1111 function root_wellcome($user)
1113 GLOBAL $root_wellarr;
1115 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
1116 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $root_wellarr[$i]));
1121 function table_wellcome($user)
1123 GLOBAL $table_wellarr;
1125 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1126 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
1131 function show_room(&$bri, &$user)
1133 $ret .= sprintf('gst.st = %d;', $user->step);
1134 $ret .= sprintf('stat = "%s";', $user->stat);
1136 $ret .= root_wellcome($user);
1137 $ret .= sprintf('subst = "%s";', $user->subst);
1138 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1139 for ($i = 0 ; $i < TABLES_N ; $i++) {
1140 $ret .= table_content($bri, $user, $i);
1141 $act_content = table_act_content(($user->subst == 'standup'),
1142 $bri->table[$i]->player_n, $i, $user->table);
1143 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1145 $ret .= standup_content($bri, $user);
1153 is_transition (is from room to table ?)
1154 is_again (is another game)
1156 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1158 $table_idx = $user->table;
1159 $table = &$bri->table[$table_idx];
1168 cards dnd (and gameable card if its turn)
1173 $ret = "table_init();";
1176 /* GENERAL STATUS */
1177 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1178 $sendstep, $user->stat, $user->subst, $user->table_pos);
1180 $ret .= "background_set();";
1183 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1184 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1185 xcape($bri->user[$table->player[($user->table_pos)%PLAYERS_N]]->name),
1186 xcape($bri->user[$table->player[($user->table_pos+1)%PLAYERS_N]]->name),
1187 xcape($bri->user[$table->player[($user->table_pos+2)%PLAYERS_N]]->name),
1188 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($user->table_pos+3)%PLAYERS_N]]->name)),
1189 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($user->table_pos+4)%PLAYERS_N]]->name)));
1191 /* NOTIFY FOR THE CARD MAKER */
1192 if ($is_transition) { // && $user->subst == "asta" superfluo
1193 $ret .= show_table_info(&$bri, &$table, $user->table_pos);
1194 $ret .= table_wellcome($user);
1198 if ($is_transition) { // && $user->subst == "asta" superfluo
1201 for ($i = 0 ; $i < 8 ; $i++) {
1202 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1204 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1205 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1211 log_rd($sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1213 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
1214 $i, ((($e + PLAYERS_N - $user->table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1215 $table->card[$o]->value : -1),
1216 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1221 $taked = array(0,0,0,0,0);
1222 $inhand = array(0,0,0,0,0);
1223 $ontabl = array(-1,-1,-1,-1,-1);
1226 for ($i = 0 ; $i < 40 ; $i++) {
1227 if ($table->card[$i]->stat == 'hand') {
1228 if ($table->card[$i]->owner == $user->table_pos) {
1229 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1231 $inhand[$table->card[$i]->owner]++;
1233 else if ($table->card[$i]->stat == 'take') {
1234 log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1235 $taked[$table->card[$i]->owner]++;
1237 else if ($table->card[$i]->stat == 'table') {
1238 $ontabl[$table->card[$i]->owner] = $i;
1242 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1243 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1245 log_main("Stat table:", $logg);
1247 /* Set ours cards. */
1249 for ($i = 0 ; $i < $inhand[$user->table_pos] ; $i++)
1250 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1251 for ($i = $inhand[$user->table_pos] ; $i < 8 ; $i++)
1252 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1253 $ret .= sprintf('card_setours(%s);', $oursarg);
1255 /* Dispose all cards */
1256 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1257 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1258 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1259 $inhand[$i], $taked[$i]);
1261 if ($ontabl[$i] != -1) {
1262 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1263 $table->card[$ontabl[$i]]->value,
1264 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1270 if ($user->subst == 'asta') {
1272 /* show users auction status */
1274 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1275 $user_cur = &$bri->user[$table->player[$i]];
1276 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1277 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1280 $showst .= ",-2,-2";
1281 $ret .= sprintf('show_astat(%s);', $showst);
1283 if ($table->asta_win != -1 && $table->asta_win == $user->table_pos) {
1284 /* show card chooser */
1285 $ret .= sprintf('choose_seed(%s); $("asta").style.visibility = "hidden";',
1290 if ($user->table_pos == ($table->gstart % PLAYERS_N) &&
1291 $table->asta_win == -1)
1292 $ret .= sprintf('dispose_asta(%d,%d);',
1293 $table->asta_card + 1, $table->asta_pnt+1);
1295 $ret .= sprintf('dispose_asta(%d,%d);',
1296 $table->asta_card + 1, -($table->asta_pnt+1));
1300 if ($table->asta_win == -1) { // auction case
1301 if ($user->table_pos == ($table->gstart % PLAYERS_N))
1302 $ret .= "remark_on();";
1304 $ret .= "remark_off();";
1306 else { // chooseed case
1307 if ($user->table_pos == $table->asta_win)
1308 $ret .= "remark_on();";
1310 $ret .= "remark_off();";
1313 else if ($user->subst == 'game') {
1315 if (($table->gstart + $table->turn) % PLAYERS_N == $user->table_pos)
1316 $ret .= "is_my_time = true; remark_on();";
1318 $ret .= "remark_off();";
1320 /* WHO CALL AND WATH */
1321 $ret .= briscola_show($bri, $table, $user);
1327 function calculate_winner(&$table)
1334 $cur_seed = $table->briscola - ($table->briscola % 10);
1336 for ($i = 0 ; $i < 40 ; $i++) {
1337 if ($table->card[$i]->stat != "table")
1340 log_wr($sess, sprintf("Card On table: [%d]", $i));
1342 $v = $table->card[$i]->value;
1343 $ontab[$table->card[$i]->owner] = $v;
1344 $ontid[$table->card[$i]->owner] = $i;
1345 /* se briscola setto il flag */
1346 if (($v - ($v % 10)) == $cur_seed)
1350 if ($briontab == FALSE) {
1351 $cur_win = $table->gstart;
1352 $cur_val = $ontab[$cur_win];
1353 $cur_seed = $cur_val - ($cur_val % 10);
1356 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1357 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1358 if ($ontab[$i] < $cur_val) {
1359 $cur_val = $ontab[$i];
1365 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1366 $table->card[$ontid[$i]]->owner = $cur_win;
1367 $table->card[$ontid[$i]]->stat = "take";
1372 function calculate_points(&$table)
1374 $all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
1378 if ($table->asta_pnt == 60)
1379 $table->asta_pnt = 61;
1381 $table->old_win = $table->asta_win;
1382 $table->old_friend = $table->friend;
1383 $table->old_asta_pnt = $table->asta_pnt;
1385 for ($i = 0 ; $i < 40 ; $i++) {
1386 $ctt = $table->card[$i]->value % 10;
1387 $own = $table->card[$i]->owner;
1388 if ($own == $table->asta_win || $own == $table->friend)
1389 $pro += $all_points[$ctt];
1392 log_wr("XXX", sprintf("PRO: [%d]", $pro));
1395 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1396 $table->points[$table->points_n % MAX_POINTS] = array();
1397 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1398 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1400 $table->old_pnt = $pro;
1406 if ($pro >= $table->asta_pnt)
1411 $table->points[$table->points_n % MAX_POINTS] = array();
1412 for ($i = 0 ; $i < 5 ; $i++) {
1413 if ($i == $table->asta_win)
1414 $pt = ($i == $table->friend ? 4 : 2);
1415 else if ($i == $table->friend)
1420 log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1422 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1424 log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1426 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1427 $table->total[$i] += $pt;
1430 $table->old_pnt = $pro;
1434 function validate_sess($sess) {
1435 if (strlen($sess) == SESS_LEN)