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 ( );
47 $from = array ( '\\', '@', '|' );
48 $to = array ( '\\\\', '@', '¦' );
50 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
55 var $value; /* 0 - 39 card value */
56 var $stat; /* 'bunch', 'hand', 'table', 'take' */
57 var $owner; /* (table position 0-4) */
58 // var $pos; /* Pos in hand. */
59 var $x; /* When played the X position on the table of the owner. */
60 var $y; /* When played the Y position on the table of the owner. */
62 function Card($value, $stat, $owner)
64 $this->value = $value;
66 $this->owner = $owner;
69 function assign($stat,$owner)
72 $this->owner = $owner;
82 $this->stat = 'table';
87 function take($newown)
90 $this->owner = $newown;
108 var $points; // points array
109 var $points_n; // number of row of points
123 $this->player = array();
125 $this->card = &$this->bunch_create();
126 $this->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
127 $this->asta_pla_n= -1;
128 $this->asta_card = -1;
129 $this->asta_pnt = -1;
131 $this->points = array( );
133 $this->total = array( 0, 0, 0, 0, 0);
134 $this->asta_win = -1;
135 $this->briscola = -1;
138 $this->old_asta_pnt = -1;
141 $this->old_friend= -1;
145 function &bunch_create()
149 for ($i = 0 ; $i < 40 ; $i++) {
150 $ret[$i] = new Card($i, 'bunch', 'no_owner');
156 function bunch_make()
158 $ct = array(0,0,0,0,0);
160 mt_srand(make_seed());
162 for ($i = 39 ; $i >= 0 ; $i--)
165 for ($i = 39 ; $i >= 0 ; $i--) {
169 log_main("RND ZERO", "");
174 $this->card[$id]->assign('hand', $owner);
176 $rest[$rn] = $rest[$i];
177 $pubbpos[$rn2] = $pubbpos[$i];
183 $this->mazzo = rand(0,PLAYERS_N-1);
187 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
188 $this->total[$i] = 0;
191 log_main("table::init","ci siamo");
194 function game_init(&$bri)
196 log_rd2($sess,"GSTART 4");
198 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
202 $this->asta_pla_n = PLAYERS_N;
203 $this->asta_card = -1;
204 $this->asta_pnt = 60;
205 $this->asta_win = -1;
206 $this->briscola = -1;
210 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
211 $this->asta_pla[$i] = TRUE;
212 $user_cur = &$bri->user[$this->player[$i]];
213 $user_cur->subst = 'asta';
214 $user_cur->asta_card = -2;
215 $user_cur->asta_pnt = -1;
221 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
224 function getPlayer($idx)
226 return ($this->player[$idx]);
229 function setPlayer($idx, $player)
231 $this->player[$idx] = $player;
234 function user_add($idx)
236 $this->player[$this->player_n] = $idx;
239 return ($this->player_n - 1);
242 function user_rem(&$bri, &$user)
244 $tabpos = $user->table_pos;
246 /* verifico la consistenza dei dati */
247 if ($bri->user[$this->player[$tabpos]] == $user) {
249 /* aggiorna l'array dei giocatori al tavolo. */
250 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
251 $this->player[$i] = $this->player[$i+1];
252 $user_cur = &$bri->user[$this->player[$i]];
253 $user_cur->table_pos = $i;
258 log_main($user->sess, "INCONSISTENCY ON TABLE.");
265 var $name; // name of the user
266 var $sess; // session of the user
267 var $lacc; // last access (for the cleanup)
268 var $stat; // status (outdoor, room, table, game, ...)
269 var $subst; // substatus for each status
270 var $step; // step of the current status
271 var $trans_step; // step to enable transition between pages (disable == -1)
272 var $comm; // commands array
276 var $table; // id of the current table (if in table state)
277 var $table_pos; // idx on the table
279 function User($name, $sess, $stat = "", $subst = "", $table = -1) {
282 $this->lacc = time();
284 $this->subst = $subst;
286 $this->trans_step = -1;
287 $this->comm = array();
288 $this->asta_card = -2;
289 $this->asta_pnt = -1;
291 $this->table = $table;
298 var $comm; // commands for many people
299 var $step; // current step of the comm array
300 var $garbage_timeout;
303 $this->user = array();
305 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
306 $this->user[$i] = new User("", "");
308 for ($i = 0 ; $i < TABLES_N ; $i++)
309 $this->table[$i] = new Table();
310 $this->garbage_timeout = 0;
313 function garbage_manager($force)
316 /* Garbage collector degli utenti in timeout */
318 if ($force || $this->garbage_timeout < $curtime) {
320 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
321 $user_cur = &$this->user[$i];
322 if ($user_cur->sess == "")
325 if ($user_cur->lacc < $curtime) { // Auto logout dell'utente
326 log_rd2($user_cur->sess, "AUTO LOGOUT.");
328 if ($user_cur->stat == 'table') {
329 log_rd2($user_cur->sess, "AUTO LOGOUT: Yet not implemented in table stat!");
334 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
335 log_auth($user_cur->sess, "Autologout session.");
337 $user_cur->sess = "";
338 $user_cur->name = "";
339 $user_cur->the_end = FALSE;
341 log_rd2($user_cur->sess, "AUTO LOGOUT.");
342 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
343 $this->room_wakeup(&$user_cur);
344 else if ($user_cur->subst == 'standup')
345 $this->room_outstandup(&$user_cur);
347 log_rd2($sess, "LOGOUT FROM WHAT ???");
353 log_rd2($user_cur->sess, "GARBAGE UPDATED!");
355 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
360 function room_wakeup(&$user)
362 $table_idx = $user->table;
363 $table = &$this->table[$table_idx];
365 log_main("WAKEUP", "begin function table:".$table_idx);
367 $from_table = ($user->stat == "table");
369 log_main("WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
371 for ($i = 0 ; $i < $table->player_n ; $i++) {
372 $user_cur = &$this->user[$table->player[$i]];
373 log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
375 if ($user_cur != $user) {
376 $user_cur->stat = "room";
377 $user_cur->subst = "sitdown";
379 else if ($user->sess != "") {
380 $user_cur->stat = "room";
381 $user_cur->subst = "standup";
382 $user_cur->table = -1;
387 /* aggiorna l'array dei giocatori al tavolo. */
388 $table->user_rem(&$this, &$user);
390 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
391 $user_cur = &$this->user[$i];
392 if ($user_cur->sess == '' || $user_cur->stat != 'room')
395 log_main("VALORI", "name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
397 // function show_room(&$bri, &$user)
399 $ret = "gst.st = ".($user_cur->step+1)."; ";
400 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
401 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
402 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
403 log_main("DOCUMENT.index.php", "from table");
405 else if ($user_cur->stat == "room") {
406 $ret .= table_content($this, $user_cur, $table_idx);
407 $ret .= standup_content($this, $user_cur);
409 $act_content = table_act_content(FALSE, 0, $e, $user->table);
410 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
413 if ($user_cur == $user) {
414 // set the new status
415 $ret .= 'subst = "standup"; ';
416 // clean the action buttons in other tables
417 for ($e = 0 ; $e < TABLES_N ; $e++) {
418 if ($this->table[$e]->player_n < PLAYERS_N)
419 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
423 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
424 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
427 log_wr($user_cur->sess, "ROOM_WAKEUP: ".$ret);
428 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
436 function room_outstandup(&$user)
438 $this->room_sitdown(&$user, -1);
441 function table_update(&$user)
443 log_main("table_update", "pre - USER: ".$user->name);
445 $table_idx = $user->table;
448 $table = &$this->table[$table_idx];
450 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
452 $user_cur = &$this->user[$i];
453 if ($user_cur->sess == '' || $user_cur->stat != 'room')
456 $ret = "gst.st = ".($user_cur->step+1)."; ";
458 $ret .= table_content($this, $user_cur, $table_idx);
460 if ($user_cur == $user) {
461 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
463 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
467 log_main("table_update", "post");
470 function room_sitdown(&$user, $table_idx)
472 log_main("room_sitdown", ($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
475 $table = &$this->table[$table_idx];
477 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
479 $user_cur = &$this->user[$i];
480 if ($user_cur->sess == '' || $user_cur->stat != 'room')
483 $ret = "gst.st = ".($user_cur->step+1)."; ";
485 $ret .= table_content($this, $user_cur, $table_idx);
486 $ret .= standup_content($this, $user_cur);
488 if ($user_cur == $user) {
489 $ret .= 'subst = "sitdown"; ';
490 // clean the action buttons in other tables
491 for ($e = 0 ; $e < TABLES_N ; $e++) {
492 $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
493 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
496 else if ($table_idx > -1) {
497 if ($table->player_n == PLAYERS_N) {
498 $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
499 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
502 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
507 function chatt_send(&$user, $mesg)
509 if ($user->stat == 'table') {
510 $table = &$this->table[$user->table];
513 $user_mesg = substr($mesg,6);
515 $dt = date("H:i ",time());
516 if (strncmp($user_mesg, "/nick ", 6) == 0) {
517 log_main($user->sess, "chatt_send BEGIN");
519 $name_new = str_replace(' ', '_', substr(trim(substr($user_mesg, 6)),0,12));
520 $user_mesg = "COMMAND ".$user_mesg;
523 // update local graph
524 // update remote graphs
525 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
526 $user_cur = &$this->user[$i];
527 if ($user_cur->sess == '' || $user_cur->stat != 'room')
529 if ($user_cur->name == $name_new) {
530 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
531 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
536 if ($i == MAX_PLAYERS) {
537 $user->name = $name_new;
539 log_main($user->sess, "chatt_send start set");
542 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
543 log_main($user->sess, "chatt_send set loop");
545 $user_cur = &$this->user[$i];
546 if ($user_cur->sess == '')
548 if ($user_cur->stat == 'room') {
549 if ($user->stat == 'room' && $user->subst == 'standup') {
550 $this->standup_update(&$user);
552 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
553 $user->stat == 'table') {
554 log_main($user->sess, "chatt_send pre table update");
556 $this->table_update(&$user);
558 log_main($user->sess, "chatt_send post table update");
561 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
562 $table = &$this->table[$user->table];
564 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
565 $user_cur->comm[$user_cur->step % COMM_N] = sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
566 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
567 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
568 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
569 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
570 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
571 if ($user_cur == $user)
572 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
573 xcape($user->name,ENT_COMPAT,"UTF-8"));
580 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
581 if ($user->stat == 'room') {
582 $user_cur = &$this->user[$i];
583 if ($user_cur->sess == '' || $user_cur->stat != 'room')
587 $user_cur = &$this->user[$table->player[$i]];
590 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
591 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
592 $dt.xcape($user->name), xcape($user_mesg));
598 function &get_user($sess, &$idx)
602 if (strlen($sess) == SESS_LEN) {
603 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
604 if (strcmp($sess, $this->user[$i]->sess) == 0) {
607 return ($this->user[$i]);
610 log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
611 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
612 // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
615 log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
621 * function &add_user(&$bri, &$sess, &$idx, $name)
624 * if ($idx != -1 && ret == FALSE) => duplicated nick
625 * if ($idx == -1 && ret == FALSE) => no space left
626 * if (ret == TRUE) => SUCCESS
628 function &add_user(&$sess, &$idx, $name)
633 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
634 if (validate_sess($sess) == FALSE)
637 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
639 if (strcmp($sess, $this->user[$i]->sess) == 0) {
643 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
646 if (strcmp($this->user[$i]->name, $name) == 0) {
654 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name));
656 if ($idx != -1 && $i == MAX_PLAYERS) {
659 $this->user[$idx]->sess = uniqid("");
660 $sess = $this->user[$idx]->sess;
664 $this->user[$idx]->sess = $sess;
666 $this->user[$idx]->name = $name;
667 $this->user[$idx]->stat = "room";
668 $this->user[$idx]->subst = "standup";
670 log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name));
672 return ($this->user[$idx]);
678 function standup_update(&$user)
680 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
681 $user_cur = &$this->user[$i];
682 if ($user_cur->sess == '')
685 log_main("STANDUP START", $user_cur->stat);
687 if ($user_cur->stat == 'room') {
688 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
689 if ($user_cur == $user)
690 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
692 log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
700 } // end class brisco
704 list($usec, $sec) = explode(' ', microtime());
705 return (float) $sec + ((float) $usec * 100000);
708 function log_main($sess, $log) {
709 if (BRISK_DEBUG != TRUE)
712 $fp = fopen("/tmp/brisk_main.log", 'a');
713 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
717 function log_rd($sess, $log) {
718 if (BRISK_DEBUG != TRUE)
721 $fp = fopen("/tmp/brisk_rd.log", 'a');
722 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
726 function log_rd2($sess, $log) {
727 if (BRISK_DEBUG != TRUE)
730 $fp = fopen("/tmp/brisk_rd2.log", 'a');
731 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
735 function log_send($sess, $log) {
736 if (BRISK_DEBUG != TRUE)
739 $fp = fopen("/tmp/brisk_send.log", 'a');
740 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
744 function log_auth($sess, $log) {
745 if (BRISK_DEBUG != TRUE)
748 $fp = fopen("/tmp/brisk_auth.log", 'a');
749 fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
753 function log_wr($sess, $log) {
754 if (BRISK_DEBUG != TRUE)
757 $fp = fopen("/tmp/brisk_wr.log", 'a');
758 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
762 function log_load($sess, $log) {
763 if (BRISK_DEBUG != TRUE)
766 $fp = fopen("/tmp/brisk_load.log", 'a');
767 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
773 $brisco = new brisco();
780 // echo "LOCK: ".FTOK_PATH."/main";
782 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
786 // echo "FTOK ".$tok."<br>";
787 if (($res = sem_get($tok)) == FALSE) {
788 echo "SEM_GET FAILED";
791 if (sem_acquire($res))
797 function unlock_data($res)
799 return (sem_release($res));
803 function &load_data()
805 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
810 if ($shm = shm_attach($tok,100000 * TABLES_N)) {
811 if(($bri = @shm_get_var($shm, $tok)) == false) {
812 log_main("XXX", "INIT MAIN DATA");
815 shm_put_var($shm, $tok, $bri);
827 function save_data(&$bri)
835 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1)
841 if (($shm = shm_attach($tok,100000 * TABLES_N)) == FALSE)
844 if (shm_put_var($shm, $tok, $bri) == FALSE)
846 // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
856 function table_act_content($isstanding, $sitted, $table, $cur_table)
861 if ($sitted < PLAYERS_N) {
862 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
866 if ($table == $cur_table)
867 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
874 function table_content($bri, $user, $table_idx)
880 // Si possono usare i dati nella classe table
884 $table = &$bri->table[$table_idx];
886 if ($user->stat != 'room')
889 for ($i = 0 ; $i < $table->player_n ; $i++) {
890 $user_cur = &$bri->user[$table->player[$i]];
892 if ($user_cur == $user)
893 { $hilion = "<b>"; $hilioff = "</b>"; }
895 { $hilion = ""; $hilioff = ""; }
897 log_main($bri->user[$e]->name, sprintf("IN TABLE [%d]", $table_idx));
899 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
902 for ( ; $i < PLAYERS_N ; $i++)
906 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
911 function standup_content(&$bri, $user)
916 if ($user->stat != 'room')
919 $content .= '<table class=\\"table_standup\\">';
920 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
921 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
925 if ($bri->user[$e]->subst == "standup") {
926 if (($ct % 4) == 0) {
929 if ($bri->user[$e] == $user)
930 { $hilion = "<b>"; $hilioff = "</b>"; }
932 { $hilion = ""; $hilioff = ""; }
934 $content .= sprintf('<td style=\\"text-align: center\\">%s%s%s</td>',$hilion, xcape($bri->user[$e]->name), $hilioff);
935 if (($ct % 4) == 3) {
941 $content .= '</table>';
943 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
944 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
945 $content, $content2);
954 function show_notify($text, $tout, $butt)
956 log_main("SHOW_NOTIFY", $text);
957 return sprintf('var noti = new notify(gst,$("bg"),"%s",%d,"%s");', $text, $tout,$butt);
963 function briscola_show($bri, $table, $user)
968 if ($table->asta_card == 9)
969 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
971 /* text of caller cell */
972 if ($user->table_pos == $table->asta_win)
973 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
975 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
976 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
978 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
980 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
981 $ret .= sprintf('$("caller").style.visibility = "visible";');
982 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
983 $ret .= sprintf('$("asta").style.visibility = "hidden";');
984 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
990 function game_result($asta_pnt, $pnt)
992 if ($asta_pnt == 61) {
1001 if ($pnt >= $asta_pnt)
1008 function multoval($mult)
1012 else if ($mult == 4)
1013 return ("quadruplo");
1015 return (sprintf("%d-plo", $mult));
1018 function show_table_info(&$bri, &$table, $table_pos)
1020 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1021 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1024 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1025 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1026 $noty .= sprintf("</tr>");
1029 log_main("show_table_info", "pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1031 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1032 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1033 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1034 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1039 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1040 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1041 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1042 $noty .= "</tr></table>";
1044 if ($table->old_win != -1) {
1045 $win = $table->player[$table->old_win];
1046 $fri = $table->player[$table->old_friend];
1048 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1051 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
1052 xcape($bri->user[$win]->name),
1053 xcape($bri->user[$fri]->name));
1054 if ($table->old_pnt == 120) {
1055 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1058 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1059 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1060 'più di 60'), $table->old_pnt,
1061 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1065 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1066 xcape($bri->user[$win]->name));
1067 if ($table->old_pnt == 120) {
1068 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1071 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1072 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1073 'più di 60'), $table->old_pnt,
1074 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1078 if ($table->mazzo == $table_pos)
1079 $noty .= "Fai <b>tu</b> il mazzo.";
1081 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1082 $noty .= "Il mazzo a <b>$unam</b>.";
1085 if ($table->mult > 1) {
1086 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1088 $noty .= "<hr><br>";
1090 $ret .= show_notify($noty, 3000, "torna alla partita");
1095 function root_wellcome($user)
1097 GLOBAL $root_wellarr;
1099 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
1100 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $root_wellarr[$i]));
1105 function table_wellcome($user)
1107 GLOBAL $table_wellarr;
1109 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1110 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
1115 function show_room(&$bri, &$user)
1117 $ret .= sprintf('gst.st = %d;', $user->step);
1118 $ret .= sprintf('stat = "%s";', $user->stat);
1120 $ret .= root_wellcome($user);
1121 $ret .= sprintf('subst = "%s";', $user->subst);
1122 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1123 for ($i = 0 ; $i < TABLES_N ; $i++) {
1124 $ret .= table_content($bri, $user, $i);
1125 $act_content = table_act_content(($user->subst == 'standup'),
1126 $bri->table[$i]->player_n, $i, $user->table);
1127 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1129 $ret .= standup_content($bri, $user);
1137 is_transition (is from room to table ?)
1138 is_again (is another game)
1140 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1142 $table_idx = $user->table;
1143 $table = &$bri->table[$table_idx];
1152 cards dnd (and gameable card if its turn)
1157 $ret = "table_init();";
1160 /* GENERAL STATUS */
1161 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1162 $sendstep, $user->stat, $user->subst, $user->table_pos);
1164 $ret .= "background_set();";
1167 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1168 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1169 xcape($bri->user[$table->player[($user->table_pos)%PLAYERS_N]]->name),
1170 xcape($bri->user[$table->player[($user->table_pos+1)%PLAYERS_N]]->name),
1171 xcape($bri->user[$table->player[($user->table_pos+2)%PLAYERS_N]]->name),
1172 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($user->table_pos+3)%PLAYERS_N]]->name)),
1173 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($user->table_pos+4)%PLAYERS_N]]->name)));
1175 /* NOTIFY FOR THE CARD MAKER */
1176 if ($is_transition) { // && $user->subst == "asta" superfluo
1177 $ret .= show_table_info(&$bri, &$table, $user->table_pos);
1178 $ret .= table_wellcome($user);
1182 if ($is_transition) { // && $user->subst == "asta" superfluo
1185 for ($i = 0 ; $i < 8 ; $i++) {
1186 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1188 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1189 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1195 log_rd($sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1197 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
1198 $i, ((($e + PLAYERS_N - $user->table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1199 $table->card[$o]->value : -1),
1200 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1205 $taked = array(0,0,0,0,0);
1206 $inhand = array(0,0,0,0,0);
1207 $ontabl = array(-1,-1,-1,-1,-1);
1210 for ($i = 0 ; $i < 40 ; $i++) {
1211 if ($table->card[$i]->stat == 'hand') {
1212 if ($table->card[$i]->owner == $user->table_pos) {
1213 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1215 $inhand[$table->card[$i]->owner]++;
1217 else if ($table->card[$i]->stat == 'take') {
1218 log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1219 $taked[$table->card[$i]->owner]++;
1221 else if ($table->card[$i]->stat == 'table') {
1222 $ontabl[$table->card[$i]->owner] = $i;
1226 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1227 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1229 log_main("Stat table:", $logg);
1231 /* Set ours cards. */
1233 for ($i = 0 ; $i < $inhand[$user->table_pos] ; $i++)
1234 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1235 for ($i = $inhand[$user->table_pos] ; $i < 8 ; $i++)
1236 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1237 $ret .= sprintf('card_setours(%s);', $oursarg);
1239 /* Dispose all cards */
1240 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1241 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1242 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1243 $inhand[$i], $taked[$i]);
1245 if ($ontabl[$i] != -1) {
1246 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1247 $table->card[$ontabl[$i]]->value,
1248 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1254 if ($user->subst == 'asta') {
1256 /* show users auction status */
1258 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1259 $user_cur = &$bri->user[$table->player[$i]];
1260 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1261 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1264 $showst .= ",-2,-2";
1265 $ret .= sprintf('show_astat(%s);', $showst);
1267 if ($table->asta_win != -1 && $table->asta_win == $user->table_pos) {
1268 /* show card chooser */
1269 $ret .= sprintf('choose_seed(%s); $("asta").style.visibility = "hidden";',
1274 if ($user->table_pos == ($table->gstart % PLAYERS_N) &&
1275 $table->asta_win == -1)
1276 $ret .= sprintf('dispose_asta(%d,%d);',
1277 $table->asta_card + 1, $table->asta_pnt+1);
1279 $ret .= sprintf('dispose_asta(%d,%d);',
1280 $table->asta_card + 1, -($table->asta_pnt+1));
1284 if ($table->asta_win == -1) { // auction case
1285 if ($user->table_pos == ($table->gstart % PLAYERS_N))
1286 $ret .= "remark_on();";
1288 $ret .= "remark_off();";
1290 else { // chooseed case
1291 if ($user->table_pos == $table->asta_win)
1292 $ret .= "remark_on();";
1294 $ret .= "remark_off();";
1297 else if ($user->subst == 'game') {
1299 if (($table->gstart + $table->turn) % PLAYERS_N == $user->table_pos)
1300 $ret .= "is_my_time = true; remark_on();";
1302 $ret .= "remark_off();";
1304 /* WHO CALL AND WATH */
1305 $ret .= briscola_show($bri, $table, $user);
1311 function calculate_winner(&$table)
1318 $cur_seed = $table->briscola - ($table->briscola % 10);
1320 for ($i = 0 ; $i < 40 ; $i++) {
1321 if ($table->card[$i]->stat != "table")
1324 log_wr($sess, sprintf("Card On table: [%d]", $i));
1326 $v = $table->card[$i]->value;
1327 $ontab[$table->card[$i]->owner] = $v;
1328 $ontid[$table->card[$i]->owner] = $i;
1329 /* se briscola setto il flag */
1330 if (($v - ($v % 10)) == $cur_seed)
1334 if ($briontab == FALSE) {
1335 $cur_win = $table->gstart;
1336 $cur_val = $ontab[$cur_win];
1337 $cur_seed = $cur_val - ($cur_val % 10);
1340 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1341 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1342 if ($ontab[$i] < $cur_val) {
1343 $cur_val = $ontab[$i];
1349 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1350 $table->card[$ontid[$i]]->owner = $cur_win;
1351 $table->card[$ontid[$i]]->stat = "take";
1356 function calculate_points(&$table)
1358 $all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
1362 if ($table->asta_pnt == 60)
1363 $table->asta_pnt = 61;
1365 $table->old_win = $table->asta_win;
1366 $table->old_friend = $table->friend;
1367 $table->old_asta_pnt = $table->asta_pnt;
1369 for ($i = 0 ; $i < 40 ; $i++) {
1370 $ctt = $table->card[$i]->value % 10;
1371 $own = $table->card[$i]->owner;
1372 if ($own == $table->asta_win || $own == $table->friend)
1373 $pro += $all_points[$ctt];
1376 log_wr("XXX", sprintf("PRO: [%d]", $pro));
1379 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1380 $table->points[$table->points_n % MAX_POINTS] = array();
1381 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1382 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1384 $table->old_pnt = $pro;
1390 if ($pro >= $table->asta_pnt)
1395 $table->points[$table->points_n % MAX_POINTS] = array();
1396 for ($i = 0 ; $i < 5 ; $i++) {
1397 if ($i == $table->asta_win)
1398 $pt = ($i == $table->friend ? 4 : 2);
1399 else if ($i == $table->friend)
1404 log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1406 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1408 log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1410 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1411 $table->total[$i] += $pt;
1414 $table->old_pnt = $pro;
1418 function validate_sess($sess) {
1419 if (strlen($sess) == SESS_LEN)