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')
558 if ($user_cur->sess == '')
560 if ($user_cur->name == $name_new) {
561 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
562 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
567 if ($i == MAX_PLAYERS) {
568 $user->name = $name_new;
570 log_main($user->sess, "chatt_send start set");
573 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
574 log_main($user->sess, "chatt_send set loop");
576 $user_cur = &$this->user[$i];
577 if ($user_cur->sess == '')
579 if ($user_cur->stat == 'room') {
580 if ($user->stat == 'room' && $user->subst == 'standup') {
581 $this->standup_update(&$user);
583 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
584 $user->stat == 'table') {
585 log_main($user->sess, "chatt_send pre table update");
587 $this->table_update(&$user);
589 log_main($user->sess, "chatt_send post table update");
592 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
593 $table = &$this->table[$user->table];
595 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
596 $user_cur->comm[$user_cur->step % COMM_N] = sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
597 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
598 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
599 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
600 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
601 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
602 if ($user_cur == $user)
603 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
604 xcape($user->name,ENT_COMPAT,"UTF-8"));
611 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
612 if ($user->stat == 'room') {
613 $user_cur = &$this->user[$i];
614 if ($user_cur->sess == '' || $user_cur->stat != 'room')
618 $user_cur = &$this->user[$table->player[$i]];
621 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
622 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
623 $dt.xcape($user->name), xcape($user_mesg));
629 function &get_user($sess, &$idx)
633 if (strlen($sess) == SESS_LEN) {
634 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
635 if (strcmp($sess, $this->user[$i]->sess) == 0) {
638 return ($this->user[$i]);
641 log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
642 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
643 // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
646 log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
652 * function &add_user(&$bri, &$sess, &$idx, $name)
655 * if ($idx != -1 && ret == FALSE) => duplicated nick
656 * if ($idx == -1 && ret == FALSE) => no space left
657 * if (ret == TRUE) => SUCCESS
659 function &add_user(&$sess, &$idx, $name)
664 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
665 if (validate_sess($sess) == FALSE)
668 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
670 if (strcmp($sess, $this->user[$i]->sess) == 0) {
674 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
677 if (strcmp($this->user[$i]->name, $name) == 0) {
685 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name));
687 if ($idx != -1 && $i == MAX_PLAYERS) {
690 $this->user[$idx]->sess = uniqid("");
691 $sess = $this->user[$idx]->sess;
695 $this->user[$idx]->sess = $sess;
697 $this->user[$idx]->name = $name;
698 $this->user[$idx]->stat = "room";
699 $this->user[$idx]->subst = "standup";
701 log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name));
703 return ($this->user[$idx]);
709 function standup_update(&$user)
711 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
712 $user_cur = &$this->user[$i];
713 if ($user_cur->sess == '')
716 log_main("STANDUP START", $user_cur->stat);
718 if ($user_cur->stat == 'room') {
719 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
720 if ($user_cur == $user)
721 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
723 log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
731 } // end class brisco
735 list($usec, $sec) = explode(' ', microtime());
736 return (float) $sec + ((float) $usec * 100000);
739 function log_main($sess, $log) {
740 if (BRISK_DEBUG != TRUE)
743 $fp = fopen("/tmp/brisk_main.log", 'a');
744 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
748 function log_rd($sess, $log) {
749 if (BRISK_DEBUG != TRUE)
752 $fp = fopen("/tmp/brisk_rd.log", 'a');
753 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
757 function log_rd2($sess, $log) {
758 if (BRISK_DEBUG != TRUE)
761 $fp = fopen("/tmp/brisk_rd2.log", 'a');
762 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
766 function log_send($sess, $log) {
767 if (BRISK_DEBUG != TRUE)
770 $fp = fopen("/tmp/brisk_send.log", 'a');
771 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
775 function log_auth($sess, $log) {
776 if (BRISK_DEBUG != TRUE)
779 $fp = fopen("/tmp/brisk_auth.log", 'a');
780 fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
784 function log_wr($sess, $log) {
785 if (BRISK_DEBUG != TRUE)
788 $fp = fopen("/tmp/brisk_wr.log", 'a');
789 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
793 function log_load($sess, $log) {
794 if (BRISK_DEBUG != TRUE)
797 $fp = fopen("/tmp/brisk_load.log", 'a');
798 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
804 $brisco = new brisco();
811 // echo "LOCK: ".FTOK_PATH."/main";
813 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
817 // echo "FTOK ".$tok."<br>";
818 if (($res = sem_get($tok)) == FALSE) {
819 echo "SEM_GET FAILED";
822 if (sem_acquire($res))
828 function unlock_data($res)
830 return (sem_release($res));
834 function &load_data()
836 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
841 if ($shm = shm_attach($tok,100000 * TABLES_N)) {
842 if(($bri = @shm_get_var($shm, $tok)) == false) {
843 log_main("XXX", "INIT MAIN DATA");
846 shm_put_var($shm, $tok, $bri);
858 function save_data(&$bri)
866 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1)
872 if (($shm = shm_attach($tok,100000 * TABLES_N)) == FALSE)
875 if (shm_put_var($shm, $tok, $bri) == FALSE)
877 // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
887 function table_act_content($isstanding, $sitted, $table, $cur_table)
892 if ($sitted < PLAYERS_N) {
893 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
897 if ($table == $cur_table)
898 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
905 function table_content($bri, $user, $table_idx)
911 // Si possono usare i dati nella classe table
915 $table = &$bri->table[$table_idx];
917 if ($user->stat != 'room')
920 for ($i = 0 ; $i < $table->player_n ; $i++) {
921 $user_cur = &$bri->user[$table->player[$i]];
923 if ($user_cur == $user)
924 { $hilion = "<b>"; $hilioff = "</b>"; }
926 { $hilion = ""; $hilioff = ""; }
928 log_main($bri->user[$e]->name, sprintf("IN TABLE [%d]", $table_idx));
930 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
933 for ( ; $i < PLAYERS_N ; $i++)
937 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
942 function standup_content(&$bri, $user)
947 if ($user->stat != 'room')
950 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
951 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
956 $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
958 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
959 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
963 if ($bri->user[$e]->subst == "standup") {
964 if (($ct % 4) == 0) {
967 if ($bri->user[$e] == $user)
968 { $hilion = "<b>"; $hilioff = "</b>"; }
970 { $hilion = ""; $hilioff = ""; }
972 $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($bri->user[$e]->name), $hilioff);
973 if (($ct % 4) == 3) {
979 $content .= '</table>';
981 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
982 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
983 $content, $content2);
989 function show_notify($text, $tout, $butt, $w, $h)
991 log_main("SHOW_NOTIFY", $text);
992 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
995 function briscola_show($bri, $table, $user)
1000 if ($table->asta_card == 9)
1001 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1003 /* text of caller cell */
1004 if ($user->table_pos == $table->asta_win)
1005 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1007 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
1008 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1010 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1012 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1013 $ret .= sprintf('$("caller").style.visibility = "visible";');
1014 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1015 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1016 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1022 function game_result($asta_pnt, $pnt)
1024 if ($asta_pnt == 61) {
1027 else if ($pnt == 60)
1033 if ($pnt >= $asta_pnt)
1040 function multoval($mult)
1044 else if ($mult == 4)
1045 return ("quadruplo");
1047 return (sprintf("%d-plo", $mult));
1050 function show_table_info(&$bri, &$table, $table_pos)
1052 $user = &$bri->user[$table->player[$table_pos]];
1054 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1055 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1058 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1059 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1060 $noty .= sprintf("</tr>");
1063 log_main("show_table_info", "pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1065 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1066 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1067 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1068 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1073 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1074 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1075 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1076 $noty .= "</tr></table>";
1078 if ($table->old_win != -1) {
1079 $win = $table->player[$table->old_win];
1080 $fri = $table->player[$table->old_friend];
1082 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1085 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
1086 xcape($bri->user[$win]->name),
1087 xcape($bri->user[$fri]->name));
1088 if ($table->old_pnt == 120) {
1089 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1092 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1093 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1094 'più di 60'), $table->old_pnt,
1095 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1099 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1100 xcape($bri->user[$win]->name));
1101 if ($table->old_pnt == 120) {
1102 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1105 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1106 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1107 'più di 60'), $table->old_pnt,
1108 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1112 if ($table->mazzo == $table_pos)
1113 $noty .= "Fai <b>tu</b> il mazzo,";
1115 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1116 $noty .= "Il mazzo a <b>$unam</b>,";
1119 if ($user->subst == 'asta') {
1120 if ($table->asta_win == -1) // auction case
1121 $curplayer = $table->gstart % PLAYERS_N;
1123 $curplayer = $table->asta_win;
1125 else if ($user->subst == 'game') {
1126 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
1130 if ($curplayer == $table_pos) {
1131 $noty .= " tocca a <b>te</b> giocare.";
1134 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1135 $noty .= " tocca a <b>$unam</b> giocare.";
1138 if ($table->mult > 1) {
1139 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1141 $noty .= "<hr><br>";
1143 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
1148 function root_wellcome($user)
1150 GLOBAL $root_wellarr;
1152 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
1153 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $root_wellarr[$i]));
1158 function table_wellcome($user)
1160 GLOBAL $table_wellarr;
1162 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1163 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
1168 function show_room(&$bri, &$user)
1170 $ret .= sprintf('gst.st = %d;', $user->step);
1171 $ret .= sprintf('stat = "%s";', $user->stat);
1173 $ret .= root_wellcome($user);
1174 $ret .= sprintf('subst = "%s";', $user->subst);
1175 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1176 for ($i = 0 ; $i < TABLES_N ; $i++) {
1177 $ret .= table_content($bri, $user, $i);
1178 $act_content = table_act_content(($user->subst == 'standup'),
1179 $bri->table[$i]->player_n, $i, $user->table);
1180 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1182 $ret .= standup_content($bri, $user);
1190 is_transition (is from room to table ?)
1191 is_again (is another game)
1193 Examles of $is_transition, $is_again:
1194 from reload of the page: FALSE, FALSE
1195 from sitdown in room: TRUE, FALSE
1196 from table: asta cmd e tutti passano: TRUE, TRUE
1197 from table: fine partita: TRUE, TRUE
1199 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1201 $table_idx = $user->table;
1202 $table = &$bri->table[$table_idx];
1211 cards dnd (and gameable card if its turn)
1216 $ret = "table_init();";
1219 /* GENERAL STATUS */
1220 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1221 $sendstep, $user->stat, $user->subst, $user->table_pos);
1223 $ret .= "background_set();";
1226 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1227 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1228 xcape($bri->user[$table->player[($user->table_pos)%PLAYERS_N]]->name),
1229 xcape($bri->user[$table->player[($user->table_pos+1)%PLAYERS_N]]->name),
1230 xcape($bri->user[$table->player[($user->table_pos+2)%PLAYERS_N]]->name),
1231 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($user->table_pos+3)%PLAYERS_N]]->name)),
1232 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($user->table_pos+4)%PLAYERS_N]]->name)));
1234 /* NOTIFY FOR THE CARD MAKER */
1235 if ($is_transition) { // && $user->subst == "asta" superfluo
1236 $ret .= show_table_info(&$bri, &$table, $user->table_pos);
1239 $ret .= table_wellcome($user);
1242 if ($is_transition) { // && $user->subst == "asta" superfluo
1245 for ($i = 0 ; $i < 8 ; $i++) {
1246 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1248 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1249 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1255 log_rd($sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1257 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
1258 $i, ((($e + PLAYERS_N - $user->table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1259 $table->card[$o]->value : -1),
1260 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1265 $taked = array(0,0,0,0,0);
1266 $inhand = array(0,0,0,0,0);
1267 $ontabl = array(-1,-1,-1,-1,-1);
1270 for ($i = 0 ; $i < 40 ; $i++) {
1271 if ($table->card[$i]->stat == 'hand') {
1272 if ($table->card[$i]->owner == $user->table_pos) {
1273 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1275 $inhand[$table->card[$i]->owner]++;
1277 else if ($table->card[$i]->stat == 'take') {
1278 log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1279 $taked[$table->card[$i]->owner]++;
1281 else if ($table->card[$i]->stat == 'table') {
1282 $ontabl[$table->card[$i]->owner] = $i;
1286 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1287 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1289 log_main("Stat table:", $logg);
1291 /* Set ours cards. */
1293 for ($i = 0 ; $i < $inhand[$user->table_pos] ; $i++)
1294 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1295 for ($i = $inhand[$user->table_pos] ; $i < 8 ; $i++)
1296 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1297 $ret .= sprintf('card_setours(%s);', $oursarg);
1299 /* Dispose all cards */
1300 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1301 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1302 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1303 $inhand[$i], $taked[$i]);
1305 if ($ontabl[$i] != -1) {
1306 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1307 $table->card[$ontabl[$i]]->value,
1308 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1314 if ($user->subst == 'asta') {
1316 /* show users auction status */
1318 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1319 $user_cur = &$bri->user[$table->player[$i]];
1320 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1321 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1324 $showst .= ",-2,-2";
1325 $ret .= sprintf('show_astat(%s);', $showst);
1327 if ($table->asta_win != -1 && $table->asta_win == $user->table_pos) {
1328 /* show card chooser */
1329 $ret .= sprintf('choose_seed(%s); $("asta").style.visibility = "hidden";',
1334 if ($user->table_pos == ($table->gstart % PLAYERS_N) &&
1335 $table->asta_win == -1)
1336 $ret .= sprintf('dispose_asta(%d,%d);',
1337 $table->asta_card + 1, $table->asta_pnt+1);
1339 $ret .= sprintf('dispose_asta(%d,%d);',
1340 $table->asta_card + 1, -($table->asta_pnt+1));
1344 if ($table->asta_win == -1) { // auction case
1345 if ($user->table_pos == ($table->gstart % PLAYERS_N))
1346 $ret .= "remark_on();";
1348 $ret .= "remark_off();";
1350 else { // chooseed case
1351 if ($user->table_pos == $table->asta_win)
1352 $ret .= "remark_on();";
1354 $ret .= "remark_off();";
1357 else if ($user->subst == 'game') {
1359 if (($table->gstart + $table->turn) % PLAYERS_N == $user->table_pos)
1360 $ret .= "is_my_time = true; remark_on();";
1362 $ret .= "remark_off();";
1364 /* WHO CALL AND WATH */
1365 $ret .= briscola_show($bri, $table, $user);
1371 function calculate_winner(&$table)
1378 $cur_seed = $table->briscola - ($table->briscola % 10);
1380 for ($i = 0 ; $i < 40 ; $i++) {
1381 if ($table->card[$i]->stat != "table")
1384 log_wr($sess, sprintf("Card On table: [%d]", $i));
1386 $v = $table->card[$i]->value;
1387 $ontab[$table->card[$i]->owner] = $v;
1388 $ontid[$table->card[$i]->owner] = $i;
1389 /* se briscola setto il flag */
1390 if (($v - ($v % 10)) == $cur_seed)
1394 if ($briontab == FALSE) {
1395 $cur_win = $table->gstart;
1396 $cur_val = $ontab[$cur_win];
1397 $cur_seed = $cur_val - ($cur_val % 10);
1400 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1401 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1402 if ($ontab[$i] < $cur_val) {
1403 $cur_val = $ontab[$i];
1409 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1410 $table->card[$ontid[$i]]->owner = $cur_win;
1411 $table->card[$ontid[$i]]->stat = "take";
1416 function calculate_points(&$table)
1418 $all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
1422 if ($table->asta_pnt == 60)
1423 $table->asta_pnt = 61;
1425 $table->old_win = $table->asta_win;
1426 $table->old_friend = $table->friend;
1427 $table->old_asta_pnt = $table->asta_pnt;
1429 for ($i = 0 ; $i < 40 ; $i++) {
1430 $ctt = $table->card[$i]->value % 10;
1431 $own = $table->card[$i]->owner;
1432 if ($own == $table->asta_win || $own == $table->friend)
1433 $pro += $all_points[$ctt];
1436 log_wr("XXX", sprintf("PRO: [%d]", $pro));
1439 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1440 $table->points[$table->points_n % MAX_POINTS] = array();
1441 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1442 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1444 $table->old_pnt = $pro;
1450 if ($pro >= $table->asta_pnt)
1455 $table->points[$table->points_n % MAX_POINTS] = array();
1456 for ($i = 0 ; $i < 5 ; $i++) {
1457 if ($i == $table->asta_win)
1458 $pt = ($i == $table->friend ? 4 : 2);
1459 else if ($i == $table->friend)
1464 log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1466 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1468 log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1470 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1471 $table->total[$i] += $pt;
1474 $table->old_pnt = $pro;
1478 function validate_sess($sess) {
1479 if (strlen($sess) == SESS_LEN)