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");
37 $root_wellarr = Array ( 'Benvenuto in brisk (Ver. 0.4.3).',
38 'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
39 $table_wellarr = Array ( 'In bocca al lupo!');
41 $G_brisk_version = "0.4.3";
44 <div style=\\"text-align: left; padding: 8px;\\">
45 <b>Descrizione</b><br>
46 Questa è un\'implementazione della briscola in cinque, così come è spiegata su
47 <a target=\\"_blank\\" href=\\"http://it.wikipedia.org/wiki/Briscola#Gioco_a_5\\">Wikipedia</a>; in breve è la variante con l\'asta prima sulla carta e poi sui punti.<br><br>
48 <b>Uso del sito</b><br>
49 Potete sedervi a un tavolo o rimanere in piedi.<br>
50 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
53 All\'inizio vengono distribuite le carte e parte l\'asta; per partecipare all\'asta, quando sarà il vostro turno, potrete scegliere se andare avanti o passare cliccando sulle icone corrispondenti. Se si arriva ai punti, scrivete nella textbox il vostro rilancio e cliccate PUNTI.<br><br>
54 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
55 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
56 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno quadrato al centro del vostro schermo.<br><br>
57 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
59 <dt><b>Comandi della chat</b>
60 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
61 <dd>.. to be continue ..
67 <div id=\\"header\\" class=\\"header\\">
68 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
69 briscola chiamata in salsa ajax
71 <br><b>version '.$G_brisk_version.'</b><br><br>
72 Copyright 2006-2007 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
77 $from = array ( '\\', '@', '|' );
78 $to = array ( '\\\\', '@', '¦' );
80 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
85 var $value; /* 0 - 39 card value */
86 var $stat; /* 'bunch', 'hand', 'table', 'take' */
87 var $owner; /* (table position 0-4) */
88 // var $pos; /* Pos in hand. */
89 var $x; /* When played the X position on the table of the owner. */
90 var $y; /* When played the Y position on the table of the owner. */
92 function Card($value, $stat, $owner)
94 $this->value = $value;
96 $this->owner = $owner;
99 function assign($stat,$owner)
102 $this->owner = $owner;
105 function setpos($pos)
112 $this->stat = 'table';
117 function take($newown)
119 $this->stat = 'take';
120 $this->owner = $newown;
138 var $points; // points array
139 var $points_n; // number of row of points
153 $this->player = array();
155 $this->card = &$this->bunch_create();
156 $this->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
157 $this->asta_pla_n= -1;
158 $this->asta_card = -1;
159 $this->asta_pnt = -1;
161 $this->points = array( );
163 $this->total = array( 0, 0, 0, 0, 0);
164 $this->asta_win = -1;
165 $this->briscola = -1;
168 $this->old_asta_pnt = -1;
171 $this->old_friend= -1;
175 function &bunch_create()
179 for ($i = 0 ; $i < 40 ; $i++) {
180 $ret[$i] = new Card($i, 'bunch', 'no_owner');
186 function bunch_make()
188 $ct = array(0,0,0,0,0);
190 mt_srand(make_seed());
192 for ($i = 39 ; $i >= 0 ; $i--)
195 for ($i = 39 ; $i >= 0 ; $i--) {
199 log_main("RND ZERO", "");
204 $this->card[$id]->assign('hand', $owner);
206 $rest[$rn] = $rest[$i];
207 $pubbpos[$rn2] = $pubbpos[$i];
213 $this->mazzo = rand(0,PLAYERS_N-1);
217 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
218 $this->total[$i] = 0;
221 log_main("table::init","ci siamo");
224 function game_init(&$bri)
226 log_rd2($sess,"GSTART 4");
228 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
232 $this->asta_pla_n = PLAYERS_N;
233 $this->asta_card = -1;
234 $this->asta_pnt = 60;
235 $this->asta_win = -1;
236 $this->briscola = -1;
240 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
241 $this->asta_pla[$i] = TRUE;
242 $user_cur = &$bri->user[$this->player[$i]];
243 $user_cur->subst = 'asta';
244 $user_cur->asta_card = -2;
245 $user_cur->asta_pnt = -1;
251 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
254 function getPlayer($idx)
256 return ($this->player[$idx]);
259 function setPlayer($idx, $player)
261 $this->player[$idx] = $player;
264 function user_add($idx)
266 $this->player[$this->player_n] = $idx;
269 return ($this->player_n - 1);
272 function user_rem(&$bri, &$user)
274 $tabpos = $user->table_pos;
276 /* verifico la consistenza dei dati */
277 if ($bri->user[$this->player[$tabpos]] == $user) {
279 /* aggiorna l'array dei giocatori al tavolo. */
280 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
281 $this->player[$i] = $this->player[$i+1];
282 $user_cur = &$bri->user[$this->player[$i]];
283 $user_cur->table_pos = $i;
288 log_main($user->sess, "INCONSISTENCY ON TABLE.");
295 var $name; // name of the user
296 var $sess; // session of the user
297 var $lacc; // last access (for the cleanup)
298 var $stat; // status (outdoor, room, table, game, ...)
299 var $subst; // substatus for each status
300 var $step; // step of the current status
301 var $trans_step; // step to enable transition between pages (disable == -1)
302 var $comm; // commands array
306 var $table; // id of the current table (if in table state)
307 var $table_pos; // idx on the table
309 function User($name, $sess, $stat = "", $subst = "", $table = -1) {
312 $this->lacc = time();
314 $this->subst = $subst;
316 $this->trans_step = -1;
317 $this->comm = array();
318 $this->asta_card = -2;
319 $this->asta_pnt = -1;
321 $this->table = $table;
328 var $comm; // commands for many people
329 var $step; // current step of the comm array
330 var $garbage_timeout;
333 $this->user = array();
335 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
336 $this->user[$i] = new User("", "");
338 for ($i = 0 ; $i < TABLES_N ; $i++)
339 $this->table[$i] = new Table();
340 $this->garbage_timeout = 0;
343 function garbage_manager($force)
346 /* Garbage collector degli utenti in timeout */
348 if ($force || $this->garbage_timeout < $curtime) {
350 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
351 $user_cur = &$this->user[$i];
352 if ($user_cur->sess == "")
355 if ($user_cur->lacc < $curtime) { // Auto logout dell'utente
356 log_rd2($user_cur->sess, "AUTO LOGOUT.");
358 if ($user_cur->stat == 'table') {
359 log_rd2($user_cur->sess, "AUTO LOGOUT: Yet not implemented in table stat!");
364 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
365 log_auth($user_cur->sess, "Autologout session.");
367 $user_cur->sess = "";
368 $user_cur->name = "";
369 $user_cur->the_end = FALSE;
371 log_rd2($user_cur->sess, "AUTO LOGOUT.");
372 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
373 $this->room_wakeup(&$user_cur);
374 else if ($user_cur->subst == 'standup')
375 $this->room_outstandup(&$user_cur);
377 log_rd2($sess, "LOGOUT FROM WHAT ???");
383 log_rd2($user_cur->sess, "GARBAGE UPDATED!");
385 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
390 function room_wakeup(&$user)
392 $table_idx = $user->table;
393 $table = &$this->table[$table_idx];
395 log_main("WAKEUP", "begin function table:".$table_idx);
397 $from_table = ($user->stat == "table");
399 log_main("WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
401 for ($i = 0 ; $i < $table->player_n ; $i++) {
402 $user_cur = &$this->user[$table->player[$i]];
403 log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
405 if ($user_cur != $user) {
406 $user_cur->stat = "room";
407 $user_cur->subst = "sitdown";
409 else if ($user->sess != "") {
410 $user_cur->stat = "room";
411 $user_cur->subst = "standup";
412 $user_cur->table = -1;
417 /* aggiorna l'array dei giocatori al tavolo. */
418 $table->user_rem(&$this, &$user);
420 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
421 $user_cur = &$this->user[$i];
422 if ($user_cur->sess == '' || $user_cur->stat != 'room')
425 log_main("VALORI", "name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
427 // function show_room(&$bri, &$user)
429 $ret = "gst.st = ".($user_cur->step+1)."; ";
430 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
431 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
432 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
433 log_main("DOCUMENT.index.php", "from table");
435 else if ($user_cur->stat == "room") {
436 $ret .= table_content($this, $user_cur, $table_idx);
437 $ret .= standup_content($this, $user_cur);
439 $act_content = table_act_content(FALSE, 0, $e, $user->table);
440 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
443 if ($user_cur == $user) {
444 // set the new status
445 $ret .= 'subst = "standup"; ';
446 // clean the action buttons in other tables
447 for ($e = 0 ; $e < TABLES_N ; $e++) {
448 if ($this->table[$e]->player_n < PLAYERS_N)
449 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
453 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
454 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
457 log_wr($user_cur->sess, "ROOM_WAKEUP: ".$ret);
458 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
466 function room_outstandup(&$user)
468 $this->room_sitdown(&$user, -1);
471 function table_update(&$user)
473 log_main("table_update", "pre - USER: ".$user->name);
475 $table_idx = $user->table;
478 $table = &$this->table[$table_idx];
480 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
482 $user_cur = &$this->user[$i];
483 if ($user_cur->sess == '' || $user_cur->stat != 'room')
486 $ret = "gst.st = ".($user_cur->step+1)."; ";
488 $ret .= table_content($this, $user_cur, $table_idx);
490 if ($user_cur == $user) {
491 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
493 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
497 log_main("table_update", "post");
500 function room_sitdown(&$user, $table_idx)
502 log_main("room_sitdown", ($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
505 $table = &$this->table[$table_idx];
507 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
509 $user_cur = &$this->user[$i];
510 if ($user_cur->sess == '' || $user_cur->stat != 'room')
513 $ret = "gst.st = ".($user_cur->step+1)."; ";
515 $ret .= table_content($this, $user_cur, $table_idx);
516 $ret .= standup_content($this, $user_cur);
518 if ($user_cur == $user) {
519 $ret .= 'subst = "sitdown"; ';
520 // clean the action buttons in other tables
521 for ($e = 0 ; $e < TABLES_N ; $e++) {
522 $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
523 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
526 else if ($table_idx > -1) {
527 if ($table->player_n == PLAYERS_N) {
528 $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
529 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
532 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
537 function chatt_send(&$user, $mesg)
539 if ($user->stat == 'table') {
540 $table = &$this->table[$user->table];
543 $user_mesg = substr($mesg,6);
545 $dt = date("H:i ",time());
546 if (strncmp($user_mesg, "/nick ", 6) == 0) {
547 log_main($user->sess, "chatt_send BEGIN");
549 $name_new = str_replace(' ', '_', substr(trim(substr($user_mesg, 6)),0,12));
550 $user_mesg = "COMMAND ".$user_mesg;
553 // update local graph
554 // update remote graphs
555 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
556 $user_cur = &$this->user[$i];
557 if ($user_cur->sess == '' || $user_cur->stat != 'room')
559 if ($user_cur->name == $name_new) {
560 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
561 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
566 if ($i == MAX_PLAYERS) {
567 $user->name = $name_new;
569 log_main($user->sess, "chatt_send start set");
572 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
573 log_main($user->sess, "chatt_send set loop");
575 $user_cur = &$this->user[$i];
576 if ($user_cur->sess == '')
578 if ($user_cur->stat == 'room') {
579 if ($user->stat == 'room' && $user->subst == 'standup') {
580 $this->standup_update(&$user);
582 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
583 $user->stat == 'table') {
584 log_main($user->sess, "chatt_send pre table update");
586 $this->table_update(&$user);
588 log_main($user->sess, "chatt_send post table update");
591 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
592 $table = &$this->table[$user->table];
594 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
595 $user_cur->comm[$user_cur->step % COMM_N] = sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
596 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
597 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
598 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
599 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
600 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
601 if ($user_cur == $user)
602 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
603 xcape($user->name,ENT_COMPAT,"UTF-8"));
610 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
611 if ($user->stat == 'room') {
612 $user_cur = &$this->user[$i];
613 if ($user_cur->sess == '' || $user_cur->stat != 'room')
617 $user_cur = &$this->user[$table->player[$i]];
620 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
621 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
622 $dt.xcape($user->name), xcape($user_mesg));
628 function &get_user($sess, &$idx)
632 if (strlen($sess) == SESS_LEN) {
633 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
634 if (strcmp($sess, $this->user[$i]->sess) == 0) {
637 return ($this->user[$i]);
640 log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
641 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
642 // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
645 log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
651 * function &add_user(&$bri, &$sess, &$idx, $name)
654 * if ($idx != -1 && ret == FALSE) => duplicated nick
655 * if ($idx == -1 && ret == FALSE) => no space left
656 * if (ret == TRUE) => SUCCESS
658 function &add_user(&$sess, &$idx, $name)
663 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
664 if (validate_sess($sess) == FALSE)
667 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
669 if (strcmp($sess, $this->user[$i]->sess) == 0) {
673 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
676 if (strcmp($this->user[$i]->name, $name) == 0) {
684 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name));
686 if ($idx != -1 && $i == MAX_PLAYERS) {
689 $this->user[$idx]->sess = uniqid("");
690 $sess = $this->user[$idx]->sess;
694 $this->user[$idx]->sess = $sess;
696 $this->user[$idx]->name = $name;
697 $this->user[$idx]->stat = "room";
698 $this->user[$idx]->subst = "standup";
700 log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name));
702 return ($this->user[$idx]);
708 function standup_update(&$user)
710 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
711 $user_cur = &$this->user[$i];
712 if ($user_cur->sess == '')
715 log_main("STANDUP START", $user_cur->stat);
717 if ($user_cur->stat == 'room') {
718 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
719 if ($user_cur == $user)
720 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
722 log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
730 } // end class brisco
734 list($usec, $sec) = explode(' ', microtime());
735 return (float) $sec + ((float) $usec * 100000);
738 function log_main($sess, $log) {
739 if (BRISK_DEBUG != TRUE)
742 $fp = fopen("/tmp/brisk_main.log", 'a');
743 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
747 function log_rd($sess, $log) {
748 if (BRISK_DEBUG != TRUE)
751 $fp = fopen("/tmp/brisk_rd.log", 'a');
752 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
756 function log_rd2($sess, $log) {
757 if (BRISK_DEBUG != TRUE)
760 $fp = fopen("/tmp/brisk_rd2.log", 'a');
761 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
765 function log_send($sess, $log) {
766 if (BRISK_DEBUG != TRUE)
769 $fp = fopen("/tmp/brisk_send.log", 'a');
770 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
774 function log_auth($sess, $log) {
775 if (BRISK_DEBUG != TRUE)
778 $fp = fopen("/tmp/brisk_auth.log", 'a');
779 fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
783 function log_wr($sess, $log) {
784 if (BRISK_DEBUG != TRUE)
787 $fp = fopen("/tmp/brisk_wr.log", 'a');
788 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
792 function log_load($sess, $log) {
793 if (BRISK_DEBUG != TRUE)
796 $fp = fopen("/tmp/brisk_load.log", 'a');
797 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
803 $brisco = new brisco();
810 // echo "LOCK: ".FTOK_PATH."/main";
812 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
816 // echo "FTOK ".$tok."<br>";
817 if (($res = sem_get($tok)) == FALSE) {
818 echo "SEM_GET FAILED";
821 if (sem_acquire($res))
827 function unlock_data($res)
829 return (sem_release($res));
833 function &load_data()
835 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
840 if ($shm = shm_attach($tok,100000 * TABLES_N)) {
841 if(($bri = @shm_get_var($shm, $tok)) == false) {
842 log_main("XXX", "INIT MAIN DATA");
845 shm_put_var($shm, $tok, $bri);
857 function save_data(&$bri)
865 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1)
871 if (($shm = shm_attach($tok,100000 * TABLES_N)) == FALSE)
874 if (shm_put_var($shm, $tok, $bri) == FALSE)
876 // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
886 function table_act_content($isstanding, $sitted, $table, $cur_table)
891 if ($sitted < PLAYERS_N) {
892 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
896 if ($table == $cur_table)
897 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
904 function table_content($bri, $user, $table_idx)
910 // Si possono usare i dati nella classe table
914 $table = &$bri->table[$table_idx];
916 if ($user->stat != 'room')
919 for ($i = 0 ; $i < $table->player_n ; $i++) {
920 $user_cur = &$bri->user[$table->player[$i]];
922 if ($user_cur == $user)
923 { $hilion = "<b>"; $hilioff = "</b>"; }
925 { $hilion = ""; $hilioff = ""; }
927 log_main($bri->user[$e]->name, sprintf("IN TABLE [%d]", $table_idx));
929 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
932 for ( ; $i < PLAYERS_N ; $i++)
936 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
941 function standup_content(&$bri, $user)
946 if ($user->stat != 'room')
949 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
950 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
955 $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
957 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
958 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
962 if ($bri->user[$e]->subst == "standup") {
963 if (($ct % 4) == 0) {
966 if ($bri->user[$e] == $user)
967 { $hilion = "<b>"; $hilioff = "</b>"; }
969 { $hilion = ""; $hilioff = ""; }
971 $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($bri->user[$e]->name), $hilioff);
972 if (($ct % 4) == 3) {
978 $content .= '</table>';
980 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
981 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
982 $content, $content2);
988 function show_notify($text, $tout, $butt, $w, $h)
990 log_main("SHOW_NOTIFY", $text);
991 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
994 function briscola_show($bri, $table, $user)
999 if ($table->asta_card == 9)
1000 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1002 /* text of caller cell */
1003 if ($user->table_pos == $table->asta_win)
1004 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1006 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
1007 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1009 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1011 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1012 $ret .= sprintf('$("caller").style.visibility = "visible";');
1013 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1014 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1015 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1021 function game_result($asta_pnt, $pnt)
1023 if ($asta_pnt == 61) {
1026 else if ($pnt == 60)
1032 if ($pnt >= $asta_pnt)
1039 function multoval($mult)
1043 else if ($mult == 4)
1044 return ("quadruplo");
1046 return (sprintf("%d-plo", $mult));
1049 function show_table_info(&$bri, &$table, $table_pos)
1051 $user = &$bri->user[$table->player[$table_pos]];
1053 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1054 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1057 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1058 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1059 $noty .= sprintf("</tr>");
1062 log_main("show_table_info", "pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1064 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1065 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1066 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1067 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1072 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1073 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1074 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1075 $noty .= "</tr></table>";
1077 if ($table->old_win != -1) {
1078 $win = $table->player[$table->old_win];
1079 $fri = $table->player[$table->old_friend];
1081 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1084 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
1085 xcape($bri->user[$win]->name),
1086 xcape($bri->user[$fri]->name));
1087 if ($table->old_pnt == 120) {
1088 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1091 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1092 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1093 'più di 60'), $table->old_pnt,
1094 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1098 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1099 xcape($bri->user[$win]->name));
1100 if ($table->old_pnt == 120) {
1101 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1104 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1105 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1106 'più di 60'), $table->old_pnt,
1107 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1111 if ($table->mazzo == $table_pos)
1112 $noty .= "Fai <b>tu</b> il mazzo,";
1114 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1115 $noty .= "Il mazzo a <b>$unam</b>,";
1118 if ($user->subst == 'asta') {
1119 if ($table->asta_win == -1) // auction case
1120 $curplayer = $table->gstart % PLAYERS_N;
1122 $curplayer = $table->asta_win;
1124 else if ($user->subst == 'game') {
1125 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
1129 if ($curplayer == $table_pos) {
1130 $noty .= " tocca a <b>te</b> giocare.";
1133 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1134 $noty .= " tocca a <b>$unam</b> giocare.";
1137 if ($table->mult > 1) {
1138 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1140 $noty .= "<hr><br>";
1142 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
1147 function root_wellcome($user)
1149 GLOBAL $root_wellarr;
1151 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
1152 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $root_wellarr[$i]));
1157 function table_wellcome($user)
1159 GLOBAL $table_wellarr;
1161 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1162 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
1167 function show_room(&$bri, &$user)
1169 $ret .= sprintf('gst.st = %d;', $user->step);
1170 $ret .= sprintf('stat = "%s";', $user->stat);
1172 $ret .= root_wellcome($user);
1173 $ret .= sprintf('subst = "%s";', $user->subst);
1174 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1175 for ($i = 0 ; $i < TABLES_N ; $i++) {
1176 $ret .= table_content($bri, $user, $i);
1177 $act_content = table_act_content(($user->subst == 'standup'),
1178 $bri->table[$i]->player_n, $i, $user->table);
1179 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1181 $ret .= standup_content($bri, $user);
1189 is_transition (is from room to table ?)
1190 is_again (is another game)
1192 Examles of $is_transition, $is_again:
1193 from reload of the page: FALSE, FALSE
1194 from sitdown in room: TRUE, FALSE
1195 from table: asta cmd e tutti passano: TRUE, TRUE
1196 from table: fine partita: TRUE, TRUE
1198 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1200 $table_idx = $user->table;
1201 $table = &$bri->table[$table_idx];
1210 cards dnd (and gameable card if its turn)
1215 $ret = "table_init();";
1218 /* GENERAL STATUS */
1219 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1220 $sendstep, $user->stat, $user->subst, $user->table_pos);
1222 $ret .= "background_set();";
1225 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1226 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1227 xcape($bri->user[$table->player[($user->table_pos)%PLAYERS_N]]->name),
1228 xcape($bri->user[$table->player[($user->table_pos+1)%PLAYERS_N]]->name),
1229 xcape($bri->user[$table->player[($user->table_pos+2)%PLAYERS_N]]->name),
1230 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($user->table_pos+3)%PLAYERS_N]]->name)),
1231 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($user->table_pos+4)%PLAYERS_N]]->name)));
1233 /* NOTIFY FOR THE CARD MAKER */
1234 if ($is_transition) { // && $user->subst == "asta" superfluo
1235 $ret .= show_table_info(&$bri, &$table, $user->table_pos);
1238 $ret .= table_wellcome($user);
1241 if ($is_transition) { // && $user->subst == "asta" superfluo
1244 for ($i = 0 ; $i < 8 ; $i++) {
1245 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1247 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1248 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1254 log_rd($sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1256 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
1257 $i, ((($e + PLAYERS_N - $user->table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1258 $table->card[$o]->value : -1),
1259 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1264 $taked = array(0,0,0,0,0);
1265 $inhand = array(0,0,0,0,0);
1266 $ontabl = array(-1,-1,-1,-1,-1);
1269 for ($i = 0 ; $i < 40 ; $i++) {
1270 if ($table->card[$i]->stat == 'hand') {
1271 if ($table->card[$i]->owner == $user->table_pos) {
1272 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1274 $inhand[$table->card[$i]->owner]++;
1276 else if ($table->card[$i]->stat == 'take') {
1277 log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1278 $taked[$table->card[$i]->owner]++;
1280 else if ($table->card[$i]->stat == 'table') {
1281 $ontabl[$table->card[$i]->owner] = $i;
1285 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1286 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1288 log_main("Stat table:", $logg);
1290 /* Set ours cards. */
1292 for ($i = 0 ; $i < $inhand[$user->table_pos] ; $i++)
1293 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1294 for ($i = $inhand[$user->table_pos] ; $i < 8 ; $i++)
1295 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1296 $ret .= sprintf('card_setours(%s);', $oursarg);
1298 /* Dispose all cards */
1299 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1300 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1301 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1302 $inhand[$i], $taked[$i]);
1304 if ($ontabl[$i] != -1) {
1305 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1306 $table->card[$ontabl[$i]]->value,
1307 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1313 if ($user->subst == 'asta') {
1315 /* show users auction status */
1317 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1318 $user_cur = &$bri->user[$table->player[$i]];
1319 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1320 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1323 $showst .= ",-2,-2";
1324 $ret .= sprintf('show_astat(%s);', $showst);
1326 if ($table->asta_win != -1 && $table->asta_win == $user->table_pos) {
1327 /* show card chooser */
1328 $ret .= sprintf('choose_seed(%s); $("asta").style.visibility = "hidden";',
1333 if ($user->table_pos == ($table->gstart % PLAYERS_N) &&
1334 $table->asta_win == -1)
1335 $ret .= sprintf('dispose_asta(%d,%d);',
1336 $table->asta_card + 1, $table->asta_pnt+1);
1338 $ret .= sprintf('dispose_asta(%d,%d);',
1339 $table->asta_card + 1, -($table->asta_pnt+1));
1343 if ($table->asta_win == -1) { // auction case
1344 if ($user->table_pos == ($table->gstart % PLAYERS_N))
1345 $ret .= "remark_on();";
1347 $ret .= "remark_off();";
1349 else { // chooseed case
1350 if ($user->table_pos == $table->asta_win)
1351 $ret .= "remark_on();";
1353 $ret .= "remark_off();";
1356 else if ($user->subst == 'game') {
1358 if (($table->gstart + $table->turn) % PLAYERS_N == $user->table_pos)
1359 $ret .= "is_my_time = true; remark_on();";
1361 $ret .= "remark_off();";
1363 /* WHO CALL AND WATH */
1364 $ret .= briscola_show($bri, $table, $user);
1370 function calculate_winner(&$table)
1377 $cur_seed = $table->briscola - ($table->briscola % 10);
1379 for ($i = 0 ; $i < 40 ; $i++) {
1380 if ($table->card[$i]->stat != "table")
1383 log_wr($sess, sprintf("Card On table: [%d]", $i));
1385 $v = $table->card[$i]->value;
1386 $ontab[$table->card[$i]->owner] = $v;
1387 $ontid[$table->card[$i]->owner] = $i;
1388 /* se briscola setto il flag */
1389 if (($v - ($v % 10)) == $cur_seed)
1393 if ($briontab == FALSE) {
1394 $cur_win = $table->gstart;
1395 $cur_val = $ontab[$cur_win];
1396 $cur_seed = $cur_val - ($cur_val % 10);
1399 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1400 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1401 if ($ontab[$i] < $cur_val) {
1402 $cur_val = $ontab[$i];
1408 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1409 $table->card[$ontid[$i]]->owner = $cur_win;
1410 $table->card[$ontid[$i]]->stat = "take";
1415 function calculate_points(&$table)
1417 $all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
1421 if ($table->asta_pnt == 60)
1422 $table->asta_pnt = 61;
1424 $table->old_win = $table->asta_win;
1425 $table->old_friend = $table->friend;
1426 $table->old_asta_pnt = $table->asta_pnt;
1428 for ($i = 0 ; $i < 40 ; $i++) {
1429 $ctt = $table->card[$i]->value % 10;
1430 $own = $table->card[$i]->owner;
1431 if ($own == $table->asta_win || $own == $table->friend)
1432 $pro += $all_points[$ctt];
1435 log_wr("XXX", sprintf("PRO: [%d]", $pro));
1438 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1439 $table->points[$table->points_n % MAX_POINTS] = array();
1440 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1441 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1443 $table->old_pnt = $pro;
1449 if ($pro >= $table->asta_pnt)
1454 $table->points[$table->points_n % MAX_POINTS] = array();
1455 for ($i = 0 ; $i < 5 ; $i++) {
1456 if ($i == $table->asta_win)
1457 $pt = ($i == $table->friend ? 4 : 2);
1458 else if ($i == $table->friend)
1463 log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1465 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1467 log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1469 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1470 $table->total[$i] += $pt;
1473 $table->old_pnt = $pro;
1477 function validate_sess($sess) {
1478 if (strlen($sess) == SESS_LEN)