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_RD, 180);
32 define(EXPIRE_TIME_SMAMMA, 360);
33 // BAN_TIME da allineare anche in commons.js
34 define(BAN_TIME, 900);
35 define(GARBAGE_TIMEOUT, 10);
36 define(NICKSERV, "<i>SERVER</i>");
37 define(BRISK_DEBUG, FALSE);
38 // define(DEBUGGING, "local");
40 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
41 $G_brisk_version = "0.5.0";
43 $root_wellarr = Array ( 'Benvenuto in brisk (Ver. '.$G_brisk_version.').',
44 'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
45 $table_wellarr = Array ( 'Benvenuto al tavolo. Se almeno tre giocatori non sbloccano l\'uscita cliccando il lucchetto, chi esce non può risedersi a un qualunque tavolo per '.floor(BAN_TIME/60).' minuti.');
49 <div style=\\"text-align: left; padding: 8px;\\">
50 <b>Descrizione</b><br>
51 Questa è un\'implementazione della briscola in cinque, così come è spiegata su
52 <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>
53 <b>Uso del sito</b><br>
54 Potete sedervi a un tavolo o rimanere in piedi.<br>
55 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
58 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>
59 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
60 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
61 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno quadrato al centro del vostro schermo.<br><br>
62 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
64 <dt><b>Comandi della chat</b>
65 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
66 <dd>.. to be continue ..
72 <div id=\\"header\\" class=\\"header\\">
73 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
74 briscola chiamata in salsa ajax
76 <br><b>version '.$G_brisk_version.'</b><br><br>
77 Copyright 2006-2007 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
82 $from = array ( '\\', '@', '|' );
83 $to = array ( '\\\\', '@', '¦' );
85 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
90 var $value; /* 0 - 39 card value */
91 var $stat; /* 'bunch', 'hand', 'table', 'take' */
92 var $owner; /* (table position 0-4) */
93 // var $pos; /* Pos in hand. */
94 var $x; /* When played the X position on the table of the owner. */
95 var $y; /* When played the Y position on the table of the owner. */
97 function Card($value, $stat, $owner)
99 $this->value = $value;
101 $this->owner = $owner;
104 function assign($stat,$owner)
107 $this->owner = $owner;
110 function setpos($pos)
117 $this->stat = 'table';
122 function take($newown)
124 $this->stat = 'take';
125 $this->owner = $newown;
143 var $points; // points array
144 var $points_n; // number of row of points
159 $this->player = array();
161 $this->card = &$this->bunch_create();
162 $this->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
163 $this->asta_pla_n= -1;
164 $this->asta_card = -1;
165 $this->asta_pnt = -1;
167 $this->points = array( );
169 $this->total = array( 0, 0, 0, 0, 0);
170 $this->asta_win = -1;
171 $this->briscola = -1;
174 $this->old_reason = "";
175 $this->old_asta_pnt = -1;
178 $this->old_friend= -1;
182 function &bunch_create()
186 for ($i = 0 ; $i < 40 ; $i++) {
187 $ret[$i] = new Card($i, 'bunch', 'no_owner');
193 function bunch_make()
195 $ct = array(0,0,0,0,0);
197 mt_srand(make_seed());
199 for ($i = 39 ; $i >= 0 ; $i--)
202 for ($i = 39 ; $i >= 0 ; $i--) {
206 log_main("RND ZERO", "");
211 $this->card[$id]->assign('hand', $owner);
213 $rest[$rn] = $rest[$i];
214 $pubbpos[$rn2] = $pubbpos[$i];
218 function init(&$userarr)
220 $this->mazzo = rand(0,PLAYERS_N-1);
224 $this->old_reason = "";
225 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
226 $this->total[$i] = 0;
227 $user_cur = &$userarr[$this->player[$i]];
228 $user_cur->exitislock = TRUE;
231 log_main("table::init","ci siamo");
234 function game_init(&$userarr)
236 log_rd2($sess,"GSTART 4");
238 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
242 $this->asta_pla_n = PLAYERS_N;
243 $this->asta_card = -1;
244 $this->asta_pnt = 60;
245 $this->asta_win = -1;
246 $this->briscola = -1;
250 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
251 $this->asta_pla[$i] = TRUE;
252 $user_cur = &$userarr[$this->player[$i]];
253 $user_cur->subst = 'asta';
254 $user_cur->asta_card = -2;
255 $user_cur->asta_pnt = -1;
256 $user_cur->handpt = $this->hand_points($i);
257 // SEE function calculate_points(&$table)
263 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
266 function getPlayer($idx)
268 return ($this->player[$idx]);
271 function setPlayer($idx, $player)
273 $this->player[$idx] = $player;
276 function user_add($idx)
278 $this->player[$this->player_n] = $idx;
281 return ($this->player_n - 1);
284 function user_rem(&$bri, &$user)
286 $tabpos = $user->table_pos;
288 /* verifico la consistenza dei dati */
289 if ($bri->user[$this->player[$tabpos]] == $user) {
291 /* aggiorna l'array dei giocatori al tavolo. */
292 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
293 $this->player[$i] = $this->player[$i+1];
294 $user_cur = &$bri->user[$this->player[$i]];
295 $user_cur->table_pos = $i;
300 log_main($user->sess, "INCONSISTENCY ON TABLE.");
304 function hand_points($idx)
306 GLOBAL $G_all_points;
310 for ($i = 0 ; $i < 40 ; $i++) {
311 if ($this->card[$i]->owner != $idx)
314 $ctt = $this->card[$i]->value % 10;
315 $tot += $G_all_points[$ctt];
321 function exitlock_show(&$userarr, $table_pos)
323 $ct = $this->exitlock_calc(&$userarr, $table_pos);
325 $ret = sprintf('exitlock_show(%d, %s);', $ct,
326 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
330 function exitlock_calc(&$userarr, $table_pos)
334 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
335 if ($userarr[$this->player[$i]]->exitislock == FALSE)
344 var $name; // name of the user
345 var $sess; // session of the user
346 var $lacc; // last access (for the cleanup)
347 var $laccwr; // last access (for the cleanup)
348 var $bantime; // timeout to temporary ban
349 var $stat; // status (outdoor, room, table, game, ...)
350 var $subst; // substatus for each status
351 var $step; // step of the current status
352 var $trans_step; // step to enable transition between pages (disable == -1)
353 var $comm; // commands array
356 var $handpt; // Total card points at the beginning of the current hand.
357 var $exitislock; // Player can exit from the table ?
358 var $table; // id of the current table (if in table state)
359 var $table_pos; // idx on the table
361 function User($name, $sess, $stat = "", $subst = "", $table = -1) {
364 $this->lacc = time();
365 $this->laccwr = time();
368 $this->subst = $subst;
370 $this->trans_step = -1;
371 $this->comm = array();
372 $this->asta_card = -2;
373 $this->asta_pnt = -1;
375 $this->exitislock = TRUE;
377 $this->table = $table;
384 var $comm; // commands for many people
385 var $step; // current step of the comm array
386 var $garbage_timeout;
389 $this->user = array();
391 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
392 $this->user[$i] = new User("", "");
394 for ($i = 0 ; $i < TABLES_N ; $i++)
395 $this->table[$i] = new Table();
396 $this->garbage_timeout = 0;
399 function garbage_manager($force)
402 /* Garbage collector degli utenti in timeout */
404 if ($force || $this->garbage_timeout < $curtime) {
406 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
407 $user_cur = &$this->user[$i];
408 if ($user_cur->sess == "")
411 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
412 log_rd2($user_cur->sess, "AUTO LOGOUT.");
414 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
415 log_auth($user_cur->sess, "Autologout session.");
417 $user_cur->sess = "";
418 $user_cur->name = "";
419 $user_cur->the_end = FALSE;
421 log_rd2($user_cur->sess, "AUTO LOGOUT.");
422 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
423 $this->room_wakeup(&$user_cur);
424 else if ($user_cur->subst == 'standup')
425 $this->room_outstandup(&$user_cur);
427 log_rd2($sess, "LOGOUT FROM WHAT ???");
431 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
432 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
433 $this->room_wakeup(&$user_cur);
434 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
435 $user_cur->comm[$user_cur->step % COMM_N] .= show_notify("<br>Sei stato inattivo per ".(EXPIRE_TIME_SMAMMA/60.0)." minuti. <br><br>Quindi ritorni tra i <b>Giocatori in piedi</b>.", 0, "torna ai tavoli", 400, 100);
440 log_rd2($user_cur->sess, "GARBAGE UPDATED!");
442 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
447 function room_wakeup(&$user)
449 $table_idx = $user->table;
450 $table = &$this->table[$table_idx];
452 log_main("WAKEUP", "begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
456 $from_table = ($user->stat == "table");
458 log_main("WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
460 for ($i = 0 ; $i < $table->player_n ; $i++) {
461 $user_cur = &$this->user[$table->player[$i]];
462 log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
464 if ($user_cur != $user) {
465 $user_cur->stat = "room";
466 $user_cur->subst = "sitdown";
467 $user_cur->laccwr = $curtime;
469 else if ($user->sess != "") {
470 $user_cur->stat = "room";
471 $user_cur->subst = "standup";
472 $user_cur->laccwr = $curtime;
473 $user_cur->table = -1;
478 $user->stat = "room";
479 $user->subst = "standup";
480 $user->laccwr = $curtime;
482 /* aggiorna l'array dei giocatori al tavolo. */
483 $table->user_rem(&$this, &$user);
485 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
486 $user_cur = &$this->user[$i];
487 if ($user_cur->sess == '' || $user_cur->stat != 'room')
490 log_main("VALORI", "name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
492 $ret = "gst.st = ".($user_cur->step+1)."; ";
493 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
494 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
495 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
496 log_main("DOCUMENT.index.php", "from table");
498 else if ($user_cur->stat == "room") {
499 log_main("DOCUMENT.index.php", "from table");
501 $ret .= table_content($this, $user_cur, $table_idx);
502 $ret .= standup_content($this, $user_cur);
504 $act_content = table_act_content(FALSE, 0, $e, $user->table);
505 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
508 if ($user_cur == $user) {
509 // set the new status
510 $ret .= 'subst = "standup"; ';
511 // clean the action buttons in other tables
512 for ($e = 0 ; $e < TABLES_N ; $e++) {
513 if ($this->table[$e]->player_n < PLAYERS_N)
514 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
518 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
519 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
522 log_wr($user_cur->sess, "ROOM_WAKEUP: ".$ret);
523 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
531 function room_outstandup(&$user)
533 $this->room_sitdown(&$user, -1);
536 function table_update(&$user)
538 log_main("table_update", "pre - USER: ".$user->name);
540 $table_idx = $user->table;
543 $table = &$this->table[$table_idx];
545 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
547 $user_cur = &$this->user[$i];
548 if ($user_cur->sess == '' || $user_cur->stat != 'room')
551 $ret = "gst.st = ".($user_cur->step+1)."; ";
553 $ret .= table_content($this, $user_cur, $table_idx);
555 if ($user_cur == $user) {
556 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
558 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
562 log_main("table_update", "post");
565 function room_sitdown(&$user, $table_idx)
567 log_main("room_sitdown", ($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
570 $table = &$this->table[$table_idx];
572 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
574 $user_cur = &$this->user[$i];
575 if ($user_cur->sess == '' || $user_cur->stat != 'room')
578 $ret = "gst.st = ".($user_cur->step+1)."; ";
580 $ret .= table_content($this, $user_cur, $table_idx);
581 $ret .= standup_content($this, $user_cur);
583 if ($user_cur == $user) {
584 $ret .= 'subst = "sitdown"; ';
585 // clean the action buttons in other tables
586 for ($e = 0 ; $e < TABLES_N ; $e++) {
587 $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
588 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
591 else if ($table_idx > -1) {
592 if ($table->player_n == PLAYERS_N) {
593 $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
594 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
597 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
602 function chatt_send(&$user, $mesg)
604 if ($user->stat == 'table') {
605 $table = &$this->table[$user->table];
608 $user_mesg = substr($mesg,6);
610 $dt = date("H:i ",time());
611 if (strncmp($user_mesg, "/nick ", 6) == 0) {
612 log_main($user->sess, "chatt_send BEGIN");
614 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
615 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
616 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Il nickname deve contenere almeno una lettera o una cifra.");', $dt.NICKSERV, xcape($name_new));
621 $user_mesg = "COMMAND ".$user_mesg;
624 // update local graph
625 // update remote graphs
626 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
627 $user_cur = &$this->user[$i];
628 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
629 if ($user_cur->sess == '')
631 if ($user_cur->name == $name_new) {
632 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
633 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
638 if ($i == MAX_PLAYERS) {
639 $user->name = $name_new;
641 log_main($user->sess, "chatt_send start set");
644 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
645 log_main($user->sess, "chatt_send set loop");
647 $user_cur = &$this->user[$i];
648 if ($user_cur->sess == '')
650 if ($user_cur->stat == 'room') {
651 if ($user->stat == 'room' && $user->subst == 'standup') {
652 $this->standup_update(&$user);
654 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
655 $user->stat == 'table') {
656 log_main($user->sess, "chatt_send pre table update");
658 $this->table_update(&$user);
660 log_main($user->sess, "chatt_send post table update");
663 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
664 $table = &$this->table[$user->table];
666 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
667 $user_cur->comm[$user_cur->step % COMM_N] = sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
668 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
669 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
670 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
671 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
672 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
673 if ($user_cur == $user)
674 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
675 xcape($user->name,ENT_COMPAT,"UTF-8"));
682 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
683 if ($user->stat == 'room') {
684 $user_cur = &$this->user[$i];
685 if ($user_cur->sess == '' || $user_cur->stat != 'room')
689 $user_cur = &$this->user[$table->player[$i]];
692 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
693 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
694 $dt.xcape($user->name), xcape($user_mesg));
700 function &get_user($sess, &$idx)
704 if (strlen($sess) == SESS_LEN) {
705 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
706 if (strcmp($sess, $this->user[$i]->sess) == 0) {
709 return ($this->user[$i]);
712 log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
713 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
714 // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
717 log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
723 * function &add_user(&$bri, &$sess, &$idx, $name)
726 * if ($idx != -1 && ret == FALSE) => duplicated nick
727 * if ($idx == -2 && ret == FALSE) => invalid name
728 * if ($idx == -1 && ret == FALSE) => no space left
729 * if (ret == TRUE) => SUCCESS
731 function &add_user(&$sess, &$idx, $name)
736 if (($name_new = validate_name($name)) == FALSE) {
741 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
742 if (validate_sess($sess) == FALSE)
745 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
747 if (strcmp($sess, $this->user[$i]->sess) == 0) {
751 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
754 if (strcmp($this->user[$i]->name, $name_new) == 0) {
762 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
764 if ($idx != -1 && $i == MAX_PLAYERS) {
767 $this->user[$idx]->sess = uniqid("");
768 $sess = $this->user[$idx]->sess;
772 $this->user[$idx]->sess = $sess;
774 $this->user[$idx]->name = $name_new;
775 $this->user[$idx]->stat = "room";
776 $this->user[$idx]->subst = "standup";
777 $this->user[$idx]->laccwr = time();
779 log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
781 return ($this->user[$idx]);
787 function standup_update(&$user)
789 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
790 $user_cur = &$this->user[$i];
791 if ($user_cur->sess == '')
794 log_main("STANDUP START", $user_cur->stat);
796 if ($user_cur->stat == 'room') {
797 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
798 if ($user_cur == $user)
799 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
801 log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
809 } // end class brisco
813 list($usec, $sec) = explode(' ', microtime());
814 return (float) $sec + ((float) $usec * 100000);
817 function log_main($sess, $log) {
818 if (BRISK_DEBUG != TRUE)
821 $fp = fopen("/tmp/brisk_main.log", 'a');
822 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
826 function log_rd($sess, $log) {
827 if (BRISK_DEBUG != TRUE)
830 $fp = fopen("/tmp/brisk_rd.log", 'a');
831 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
835 function log_rd2($sess, $log) {
836 if (BRISK_DEBUG != TRUE)
839 $fp = fopen("/tmp/brisk_rd2.log", 'a');
840 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
844 function log_send($sess, $log) {
845 if (BRISK_DEBUG != TRUE)
848 $fp = fopen("/tmp/brisk_send.log", 'a');
849 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
853 function log_auth($sess, $log) {
854 if (BRISK_DEBUG != TRUE)
857 $fp = fopen("/tmp/brisk_auth.log", 'a');
858 fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
862 function log_wr($sess, $log) {
863 if (BRISK_DEBUG != TRUE)
866 $fp = fopen("/tmp/brisk_wr.log", 'a');
867 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
871 function log_load($sess, $log) {
872 if (BRISK_DEBUG != TRUE)
875 $fp = fopen("/tmp/brisk_load.log", 'a');
876 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
882 $brisco = new brisco();
889 // echo "LOCK: ".FTOK_PATH."/main";
891 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
895 // echo "FTOK ".$tok."<br>";
896 if (($res = sem_get($tok)) == FALSE) {
897 echo "SEM_GET FAILED";
900 if (sem_acquire($res))
906 function unlock_data($res)
908 return (sem_release($res));
912 function &load_data()
914 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
919 if ($shm = shm_attach($tok,100000 * TABLES_N)) {
920 if(($bri = @shm_get_var($shm, $tok)) == false) {
921 log_main("XXX", "INIT MAIN DATA");
924 shm_put_var($shm, $tok, $bri);
936 function save_data(&$bri)
944 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1)
950 if (($shm = shm_attach($tok,100000 * TABLES_N)) == FALSE)
953 if (shm_put_var($shm, $tok, $bri) == FALSE)
955 // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
965 function table_act_content($isstanding, $sitted, $table, $cur_table)
970 if ($sitted < PLAYERS_N) {
971 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
975 if ($table == $cur_table)
976 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
983 function table_content($bri, $user, $table_idx)
989 // Si possono usare i dati nella classe table
993 $table = &$bri->table[$table_idx];
995 if ($user->stat != 'room')
998 for ($i = 0 ; $i < $table->player_n ; $i++) {
999 $user_cur = &$bri->user[$table->player[$i]];
1001 if ($user_cur == $user)
1002 { $hilion = "<b>"; $hilioff = "</b>"; }
1004 { $hilion = ""; $hilioff = ""; }
1006 log_main($bri->user[$e]->name, sprintf("IN TABLE [%d]", $table_idx));
1008 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
1011 for ( ; $i < PLAYERS_N ; $i++)
1015 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
1020 function standup_content(&$bri, $user)
1025 if ($user->stat != 'room')
1028 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
1029 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
1034 $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
1036 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
1037 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
1041 if ($bri->user[$e]->subst == "standup") {
1042 if (($ct % 4) == 0) {
1045 if ($bri->user[$e] == $user)
1046 { $hilion = "<b>"; $hilioff = "</b>"; }
1048 { $hilion = ""; $hilioff = ""; }
1050 $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($bri->user[$e]->name), $hilioff);
1051 if (($ct % 4) == 3) {
1052 $content .= '</tr>';
1057 $content .= '</table>';
1059 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
1060 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
1061 $content, $content2);
1067 function show_notify($text, $tout, $butt, $w, $h)
1069 log_main("SHOW_NOTIFY", $text);
1070 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
1073 function briscola_show($bri, $table, $user)
1078 if ($table->asta_card == 9)
1079 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1081 /* text of caller cell */
1082 if ($user->table_pos == $table->asta_win)
1083 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1085 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
1086 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1088 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1090 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1091 $ret .= sprintf('$("caller").style.visibility = "visible";');
1092 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1093 $ret .= sprintf('$("astalascio").style.visibility = "";');
1094 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1095 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1101 function game_result($asta_pnt, $pnt)
1103 if ($asta_pnt == 61) {
1106 else if ($pnt == 60)
1112 if ($pnt >= $asta_pnt)
1119 function multoval($mult)
1123 else if ($mult == 4)
1124 return ("quadruplo");
1126 return (sprintf("%d-plo", $mult));
1129 function show_table_info(&$bri, &$table, $table_pos)
1131 $user = &$bri->user[$table->player[$table_pos]];
1133 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1134 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1137 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1138 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1139 $noty .= sprintf("</tr>");
1142 log_main("show_table_info", "pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1144 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1145 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1146 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1147 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1152 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1153 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1154 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1155 $noty .= "</tr></table>";
1157 if ($table->old_reason != "") {
1158 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1161 if ($table->old_win != -1) {
1162 $win = $table->player[$table->old_win];
1163 $fri = $table->player[$table->old_friend];
1165 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1168 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
1169 xcape($bri->user[$win]->name),
1170 xcape($bri->user[$fri]->name));
1171 if ($table->old_pnt == 120) {
1172 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1175 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1176 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1177 'più di 60'), $table->old_pnt,
1178 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1182 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1183 xcape($bri->user[$win]->name));
1184 if ($table->old_pnt == 120) {
1185 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1188 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1189 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1190 'più di 60'), $table->old_pnt,
1191 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1195 if ($table->mazzo == $table_pos)
1196 $noty .= "Fai <b>tu</b> il mazzo,";
1198 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1199 $noty .= "Il mazzo a <b>$unam</b>,";
1202 if ($user->subst == 'asta') {
1203 if ($table->asta_win == -1) // auction case
1204 $curplayer = $table->gstart % PLAYERS_N;
1206 $curplayer = $table->asta_win;
1208 else if ($user->subst == 'game') {
1209 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
1213 if ($curplayer == $table_pos) {
1214 $noty .= " tocca a <b>te</b> giocare.";
1217 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1218 $noty .= " tocca a <b>$unam</b> giocare.";
1221 if ($table->mult > 1) {
1222 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1224 $noty .= "<hr><br>";
1226 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
1231 function root_wellcome($user)
1233 GLOBAL $root_wellarr;
1235 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
1236 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $root_wellarr[$i]));
1241 function table_wellcome($user)
1243 GLOBAL $table_wellarr;
1245 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1246 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
1251 function show_room(&$bri, &$user)
1253 $ret .= sprintf('gst.st = %d;', $user->step);
1254 $ret .= sprintf('stat = "%s";', $user->stat);
1256 $ret .= root_wellcome($user);
1257 $ret .= sprintf('subst = "%s";', $user->subst);
1258 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1259 for ($i = 0 ; $i < TABLES_N ; $i++) {
1260 $ret .= table_content($bri, $user, $i);
1261 $act_content = table_act_content(($user->subst == 'standup'),
1262 $bri->table[$i]->player_n, $i, $user->table);
1263 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1265 $ret .= standup_content($bri, $user);
1273 is_transition (is from room to table ?)
1274 is_again (is another game)
1276 Examples of $is_transition, $is_again:
1277 from reload of the page: FALSE, FALSE
1278 from sitdown in room: TRUE, FALSE
1279 from table: asta cmd e tutti passano: TRUE, TRUE
1280 from table: fine partita: TRUE, TRUE
1282 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1284 $table_idx = $user->table;
1285 $table = &$bri->table[$table_idx];
1286 $table_pos = $user->table_pos;
1288 $ret = "table_init();";
1289 $ret .= $table->exitlock_show(&$bri->user, $table_pos);
1291 /* GENERAL STATUS */
1292 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1293 $sendstep, $user->stat, $user->subst, $table_pos);
1295 $ret .= "background_set();";
1298 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1299 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1300 xcape($bri->user[$table->player[($table_pos)%PLAYERS_N]]->name),
1301 xcape($bri->user[$table->player[($table_pos+1)%PLAYERS_N]]->name),
1302 xcape($bri->user[$table->player[($table_pos+2)%PLAYERS_N]]->name),
1303 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+3)%PLAYERS_N]]->name)),
1304 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+4)%PLAYERS_N]]->name)));
1306 /* NOTIFY FOR THE CARD MAKER */
1307 if ($is_transition) { // && $user->subst == "asta" superfluo
1308 $ret .= show_table_info(&$bri, &$table, $table_pos);
1311 $ret .= table_wellcome($user);
1313 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1314 $ret .= playsound("cow.mp3");
1319 if ($is_transition) { // && $user->subst == "asta" superfluo
1322 for ($i = 0 ; $i < 8 ; $i++) {
1323 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1325 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1326 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1332 log_rd($sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1334 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
1335 $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1336 $table->card[$o]->value : -1),
1337 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1342 $taked = array(0,0,0,0,0);
1343 $inhand = array(0,0,0,0,0);
1344 $ontabl = array(-1,-1,-1,-1,-1);
1347 for ($i = 0 ; $i < 40 ; $i++) {
1348 if ($table->card[$i]->stat == 'hand') {
1349 if ($table->card[$i]->owner == $table_pos) {
1350 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1352 $inhand[$table->card[$i]->owner]++;
1354 else if ($table->card[$i]->stat == 'take') {
1355 log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1356 $taked[$table->card[$i]->owner]++;
1358 else if ($table->card[$i]->stat == 'table') {
1359 $ontabl[$table->card[$i]->owner] = $i;
1363 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1364 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1366 log_main("Stat table:", $logg);
1368 /* Set ours cards. */
1370 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1371 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1372 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
1373 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1374 $ret .= sprintf('card_setours(%s);', $oursarg);
1376 /* Dispose all cards */
1377 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1378 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1379 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1380 $inhand[$i], $taked[$i]);
1382 if ($ontabl[$i] != -1) {
1383 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1384 $table->card[$ontabl[$i]]->value,
1385 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1391 if ($user->subst == 'asta') {
1393 /* show users auction status */
1395 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1396 $user_cur = &$bri->user[$table->player[$i]];
1397 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1398 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1401 $showst .= ",-2,-2";
1402 $ret .= sprintf('show_astat(%s);', $showst);
1404 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1405 /* show card chooser */
1406 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1411 if ($table_pos == ($table->gstart % PLAYERS_N) &&
1412 $table->asta_win == -1)
1413 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1414 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1416 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1417 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1421 if ($table->asta_win == -1) { // auction case
1422 if ($table_pos == ($table->gstart % PLAYERS_N))
1423 $ret .= "remark_on();";
1425 $ret .= "remark_off();";
1427 else { // chooseed case
1428 if ($table_pos == $table->asta_win)
1429 $ret .= "remark_on();";
1431 $ret .= "remark_off();";
1434 else if ($user->subst == 'game') {
1436 if (($table->gstart + $table->turn) % PLAYERS_N == $table_pos)
1437 $ret .= "is_my_time = true; remark_on();";
1439 $ret .= "remark_off();";
1441 /* WHO CALL AND WATH */
1442 $ret .= briscola_show($bri, $table, $user);
1448 function calculate_winner(&$table)
1455 $cur_seed = $table->briscola - ($table->briscola % 10);
1457 for ($i = 0 ; $i < 40 ; $i++) {
1458 if ($table->card[$i]->stat != "table")
1461 log_wr($sess, sprintf("Card On table: [%d]", $i));
1463 $v = $table->card[$i]->value;
1464 $ontab[$table->card[$i]->owner] = $v;
1465 $ontid[$table->card[$i]->owner] = $i;
1466 /* se briscola setto il flag */
1467 if (($v - ($v % 10)) == $cur_seed)
1471 if ($briontab == FALSE) {
1472 $cur_win = $table->gstart;
1473 $cur_val = $ontab[$cur_win];
1474 $cur_seed = $cur_val - ($cur_val % 10);
1477 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1478 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1479 if ($ontab[$i] < $cur_val) {
1480 $cur_val = $ontab[$i];
1486 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1487 $table->card[$ontid[$i]]->owner = $cur_win;
1488 $table->card[$ontid[$i]]->stat = "take";
1493 function calculate_points(&$table)
1495 GLOBAL $G_all_points;
1499 if ($table->asta_pnt == 60)
1500 $table->asta_pnt = 61;
1502 $table->old_reason = "";
1503 $table->old_win = $table->asta_win;
1504 $table->old_friend = $table->friend;
1505 $table->old_asta_pnt = $table->asta_pnt;
1507 for ($i = 0 ; $i < 40 ; $i++) {
1508 $ctt = $table->card[$i]->value % 10;
1509 $own = $table->card[$i]->owner;
1510 if ($own == $table->asta_win || $own == $table->friend)
1511 $pro += $G_all_points[$ctt];
1514 log_wr("XXX", sprintf("PRO: [%d]", $pro));
1517 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1518 $table->points[$table->points_n % MAX_POINTS] = array();
1519 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1520 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1522 $table->old_pnt = $pro;
1528 if ($pro >= $table->asta_pnt)
1533 $table->points[$table->points_n % MAX_POINTS] = array();
1534 for ($i = 0 ; $i < 5 ; $i++) {
1535 if ($i == $table->asta_win)
1536 $pt = ($i == $table->friend ? 4 : 2);
1537 else if ($i == $table->friend)
1542 log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1544 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1546 log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1548 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1549 $table->total[$i] += $pt;
1552 $table->old_pnt = $pro;
1556 function validate_sess($sess)
1558 if (strlen($sess) == SESS_LEN)
1564 function validate_name($name)
1566 $name_new = str_replace(' ', '_', substr(trim($name),0,12));
1568 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
1570 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
1577 function playsound($filename)
1579 return (sprintf('playsound("flasou", "%s");', $filename));
1582 function secstoword($secs)
1584 $mins = floor($secs / 60);
1587 $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
1590 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));