5 * Copyright (C) 2006-2007 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");
23 define(LEGAL_PATH, "/tmp/legal_brisk");
24 define(PROXY_PATH, "/var/lib/brisk_proxy");
27 define(MAX_POINTS, 5);
28 define(MAX_PLAYERS, (20 + (PLAYERS_N * TABLES_N)));
29 define(SHM_DIMS, (50000 * MAX_PLAYERS));
32 define(COMM_GEN_N, 50);
34 define(STREAM_TIMEOUT, 20);
35 define(EXPIRE_TIME_RD, 180);
36 define(EXPIRE_TIME_SMAMMA, 360);
37 // BAN_TIME da allineare anche in commons.js
38 define(BAN_TIME, 900);
39 define(GARBAGE_TIMEOUT, 10);
40 define(NICKSERV, "<i>SERVER</i>");
41 define(BRISK_DEBUG, FALSE);
42 // define(DEBUGGING, "local");
46 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
47 $G_brisk_version = "0.8.0";
49 $root_wellarr = Array ( 'Benvenuto in brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: nuovo layout che permette più tavoli, più tavoli.',
50 'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
51 $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.');
55 <div style=\\"text-align: left; padding: 8px;\\">
56 <b>Descrizione</b><br>
57 Questa è un\'implementazione della briscola in cinque, così come è spiegata su
58 <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>
59 <b>Configurazione del browser.</b><br>
60 Occorre abilitare i cookies.<br>
62 <b>Uso del sito</b><br>
63 Potete sedervi a un tavolo o rimanere in piedi.<br>
64 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
67 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>
68 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
69 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
70 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
71 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
72 Dopo che è iniziata una partita per uscirne dovete chiedere agli altri giocatori di sbloccarla cliccando sul lucchetto. Se non si segue questa prassi, una volta usciti, non vi potrete sedere a nessun tavolo per '.floor(BAN_TIME/60).' minuti.
74 <dt><b>Comandi della chat</b>
75 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
76 <dd>.. to be continue ..
82 <div id=\\"header\\" class=\\"header\\">
83 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
84 briscola chiamata in salsa ajax
86 <br><b>version '.$G_brisk_version.'</b><br><br>
87 Copyright 2006-2007 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
92 $from = array ( '\\', '@', '|' );
93 $to = array ( '\\\\', '@', '¦' );
95 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
100 var $value; /* 0 - 39 card value */
101 var $stat; /* 'bunch', 'hand', 'table', 'take' */
102 var $owner; /* (table position 0-4) */
103 // var $pos; /* Pos in hand. */
104 var $x; /* When played the X position on the table of the owner. */
105 var $y; /* When played the Y position on the table of the owner. */
107 function Card($value, $stat, $owner)
109 $this->value = $value;
110 $this->stat = $stat; // Card stat
111 $this->owner = $owner;
114 function assign($stat,$owner)
116 $this->stat = $stat; // Card stat
117 $this->owner = $owner;
120 function setpos($pos)
127 $this->stat = 'table'; // Card stat
132 function take($newown)
134 $this->stat = 'take'; // Card stat
135 $this->owner = $newown;
153 var $points; // points array
154 var $points_n; // number of row of points
169 $this->player = array();
171 $this->card = &$this->bunch_create();
172 $this->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
173 $this->asta_pla_n= -1;
174 $this->asta_card = -1;
175 $this->asta_pnt = -1;
177 $this->points = array( );
179 $this->total = array( 0, 0, 0, 0, 0);
180 $this->asta_win = -1;
181 $this->briscola = -1;
184 $this->old_reason = "";
185 $this->old_asta_pnt = -1;
188 $this->old_friend= -1;
192 function &bunch_create()
196 for ($i = 0 ; $i < 40 ; $i++) {
197 $ret[$i] =& new Card($i, 'bunch', 'no_owner');
204 function bunch_make()
206 $ct = array(0,0,0,0,0);
208 mt_srand(make_seed());
210 for ($i = 39 ; $i >= 0 ; $i--)
213 for ($i = 39 ; $i >= 0 ; $i--) {
217 log_main("RND ZERO", "");
222 $this->card[$id]->assign('hand', $owner);
224 $rest[$rn] = $rest[$i];
225 // $pubbpos[$rn2] = $pubbpos[$i];
229 function init(&$userarr)
231 $this->mazzo = rand(0,PLAYERS_N-1);
235 $this->old_reason = "";
236 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
237 $this->total[$i] = 0;
238 $user_cur = &$userarr[$this->player[$i]];
239 $user_cur->exitislock = TRUE;
242 log_main("table::init","ci siamo");
245 function game_init(&$userarr)
247 log_rd2("XXX", "GSTART 4");
249 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
253 $this->asta_pla_n = PLAYERS_N;
254 $this->asta_card = -1;
255 $this->asta_pnt = 60;
256 $this->asta_win = -1;
257 $this->briscola = -1;
261 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
262 $this->asta_pla[$i] = TRUE;
263 $user_cur = &$userarr[$this->player[$i]];
264 $user_cur->subst = 'asta';
265 $user_cur->asta_card = -2;
266 $user_cur->asta_pnt = -1;
267 $user_cur->handpt = $this->hand_points($i);
268 // SEE function calculate_points(&$table)
274 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
277 function getPlayer($idx)
279 return ($this->player[$idx]);
282 function setPlayer($idx, $player)
284 $this->player[$idx] = $player;
287 function user_add($idx)
289 $this->player[$this->player_n] = $idx;
292 return ($this->player_n - 1);
295 function user_rem(&$bri, &$user)
297 $tabpos = $user->table_pos;
299 /* verifico la consistenza dei dati */
300 if ($bri->user[$this->player[$tabpos]] == $user) {
302 /* aggiorna l'array dei giocatori al tavolo. */
303 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
304 $this->player[$i] = $this->player[$i+1];
305 $user_cur = &$bri->user[$this->player[$i]];
306 $user_cur->table_pos = $i;
311 log_main($user->sess, "INCONSISTENCY ON TABLE.");
315 function hand_points($idx)
317 GLOBAL $G_all_points;
321 for ($i = 0 ; $i < 40 ; $i++) {
322 if ($this->card[$i]->owner != $idx)
325 $ctt = $this->card[$i]->value % 10;
326 $tot += $G_all_points[$ctt];
332 function exitlock_show(&$userarr, $table_pos)
334 $ct = $this->exitlock_calc(&$userarr, $table_pos);
336 $ret = sprintf('exitlock_show(%d, %s);', $ct,
337 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
341 function exitlock_calc(&$userarr, $table_pos)
345 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
346 if ($userarr[$this->player[$i]]->exitislock == FALSE)
355 var $name; // name of the user
356 var $sess; // session of the user
357 var $ip; // ip of the user
358 var $lacc; // last access (for the cleanup)
359 var $laccwr; // last access (for the cleanup)
360 var $bantime; // timeout to temporary ban
361 var $stat; // status (outdoor, room, table, game, ...)
362 var $subst; // substatus for each status
363 var $step; // step of the current status
364 var $trans_step; // step to enable transition between pages (disable == -1)
365 var $comm; // commands array
368 var $handpt; // Total card points at the beginning of the current hand.
369 var $exitislock; // Player can exit from the table ?
370 var $table; // id of the current table (if in table state)
371 var $table_pos; // idx on the table
372 var $the_end; // Flag to change the end of the session
374 function User($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
378 $this->lacc = time();
379 $this->laccwr = time();
382 $this->subst = $subst;
384 $this->trans_step = -1;
385 $this->comm = array();
386 $this->asta_card = -2;
387 $this->asta_pnt = -1;
389 $this->exitislock = TRUE;
391 $this->table = $table;
394 function stat_set($stat) {
395 $this->stat = "$stat";
398 if (validate_sess($this->sess)) {
399 $fp = fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
400 fwrite($fp, sprintf("%s\n",$this->stat));
406 function step_set($step) {
410 if (validate_sess($this->sess) == FALSE)
412 if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
414 fwrite($fp, pack("l",$this->step), 4);
423 function step_inc() {
426 if (validate_sess($this->sess)) {
427 $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
428 fwrite($fp, pack("l",$this->step), 4);
438 function step_get($sess) {
442 if (validate_sess($sess) == FALSE)
445 if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
448 if (($s = fread($fp, 4)) == FALSE)
454 $arr = unpack('l', $s);
457 // log_rd2($sess, "A0: ".$arr[0]." A1: ".$arr[1]);
464 log_rd2($sess, "STEP_GET: return false ".$ct);
468 function step_unproxy($sess) {
469 log_rd2($sess, "UNPROXY: ".PROXY_PATH."/".$sess.".step");
470 @unlink(PROXY_PATH."/".$sess.".step");
477 var $comm; // commands for many people
478 var $step; // current step of the comm array
479 var $garbage_timeout;
482 $this->user = array();
484 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
485 $this->user[$i] =& new User("", "");
487 for ($i = 0 ; $i < TABLES_N ; $i++)
488 $this->table[$i] =& new Table();
489 $this->garbage_timeout = 0;
492 function garbage_manager($force)
495 /* Garbage collector degli utenti in timeout */
497 if ($force || $this->garbage_timeout < $curtime) {
499 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
500 $user_cur = &$this->user[$i];
501 if ($user_cur->sess == "")
504 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
505 log_rd2($user_cur->sess, "AUTO LOGOUT.");
507 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
508 log_auth($user_cur->sess, "Autologout session.");
510 $tmp_sess = $user_cur->sess;
511 $user_cur->sess = "";
512 step_unproxy($tmp_sess);
513 $user_cur->name = "";
514 $user_cur->the_end = FALSE;
516 log_rd2($user_cur->sess, "AUTO LOGOUT.");
517 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
518 $this->room_wakeup(&$user_cur);
519 else if ($user_cur->subst == 'standup')
520 $this->room_outstandup(&$user_cur);
522 log_rd2($sess, "LOGOUT FROM WHAT ???");
526 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
527 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
528 $this->room_wakeup(&$user_cur);
529 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
530 $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);
531 $user_cur->step_inc();
535 log_rd2($user_cur->sess, "GARBAGE UPDATED!");
537 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
545 function room_wakeup(&$user)
547 $table_idx = $user->table;
548 $table = &$this->table[$table_idx];
550 log_main("WAKEUP", "begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
554 $from_table = ($user->stat == "table");
556 log_main("WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
558 for ($i = 0 ; $i < $table->player_n ; $i++) {
559 $user_cur = &$this->user[$table->player[$i]];
560 log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
562 if ($user_cur != $user) {
563 $user_cur->stat_set("room");
564 $user_cur->subst = "sitdown";
565 $user_cur->laccwr = $curtime;
567 else if ($user->sess != "") {
568 $user_cur->stat_set("room");
569 $user_cur->subst = "standup";
570 $user_cur->laccwr = $curtime;
571 $user_cur->table = -1;
576 $user->stat_set("room");
577 $user->subst = "standup";
578 $user->laccwr = $curtime;
580 /* aggiorna l'array dei giocatori al tavolo. */
581 $table->user_rem(&$this, &$user);
583 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
584 $user_cur = &$this->user[$i];
585 if ($user_cur->sess == '' || $user_cur->stat != 'room')
588 log_main("VALORI", "name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
590 $ret = "gst.st = ".($user_cur->step+1)."; ";
591 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
592 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
593 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
594 log_main("DOCUMENT.index.php", "from table");
596 else if ($user_cur->stat == "room") {
597 log_main("DOCUMENT.index.php", "from table");
599 $ret .= table_content($this, $user_cur, $table_idx);
600 $ret .= standup_content($this, $user_cur);
602 $act_content = table_act_content(FALSE, 0, $table_idx, $user->table);
603 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
606 if ($user_cur == $user) {
607 // set the new status
608 $ret .= 'subst = "standup"; ';
609 // clean the action buttons in other tables
610 for ($e = 0 ; $e < TABLES_N ; $e++) {
611 if ($this->table[$e]->player_n < PLAYERS_N)
612 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
616 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
617 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
620 log_wr($user_cur->sess, "ROOM_WAKEUP: ".$ret);
621 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
622 $user_cur->step_inc();
629 function room_outstandup(&$user)
631 $this->room_sitdown(&$user, -1);
634 function table_update(&$user)
636 log_main("table_update", "pre - USER: ".$user->name);
638 $table_idx = $user->table;
641 $table = &$this->table[$table_idx];
643 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
645 $user_cur = &$this->user[$i];
646 if ($user_cur->sess == '' || $user_cur->stat != 'room')
649 $ret = "gst.st = ".($user_cur->step+1)."; ";
651 $ret .= table_content($this, $user_cur, $table_idx);
653 if ($user_cur == $user) {
654 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
656 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
657 $user_cur->step_inc();
660 log_main("table_update", "post");
663 function room_sitdown(&$user, $table_idx)
665 log_main("room_sitdown", ($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
668 $table = &$this->table[$table_idx];
670 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
672 $user_cur = &$this->user[$i];
673 if ($user_cur->sess == '' || $user_cur->stat != 'room')
676 $ret = "gst.st = ".($user_cur->step+1)."; ";
678 $ret .= table_content($this, $user_cur, $table_idx);
679 $ret .= standup_content($this, $user_cur);
681 if ($user_cur == $user) {
682 $ret .= 'subst = "sitdown"; ';
683 // clean the action buttons in other tables
684 for ($e = 0 ; $e < TABLES_N ; $e++) {
685 $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
686 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
689 else if ($table_idx > -1) {
690 if ($table->player_n == PLAYERS_N) {
691 $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
692 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
695 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
696 $user_cur->step_inc();
700 function chatt_send(&$user, $mesg)
702 if ($user->stat == 'table') {
703 $table = &$this->table[$user->table];
706 $user_mesg = substr($mesg,6);
710 $dt = date("H:i ", $timecur);
711 if (strncmp($user_mesg, "/nick ", 6) == 0) {
712 log_main($user->sess, "chatt_send BEGIN");
714 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
715 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
716 $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));
721 $user_mesg = "COMMAND ".$user_mesg;
724 // update local graph
725 // update remote graphs
726 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
727 $user_cur = &$this->user[$i];
728 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
729 if ($user_cur->sess == '')
731 if ($user_cur->name == $name_new) {
732 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
733 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
738 if ($i == MAX_PLAYERS) {
739 $user->name = $name_new;
741 log_main($user->sess, "chatt_send start set");
744 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
745 log_main($user->sess, "chatt_send set loop");
747 $user_cur = &$this->user[$i];
748 if ($user_cur->sess == '')
750 if ($user_cur->stat == 'room') {
751 if ($user->stat == 'room' && $user->subst == 'standup') {
752 $this->standup_update(&$user);
754 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
755 $user->stat == 'table') {
756 log_main($user->sess, "chatt_send pre table update");
758 $this->table_update(&$user);
760 log_main($user->sess, "chatt_send post table update");
763 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
764 $table = &$this->table[$user->table];
766 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
767 $user_cur->comm[$user_cur->step % COMM_N] = sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
768 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
769 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
770 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
771 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
772 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
773 if ($user_cur == $user)
774 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
775 xcape($user->name,ENT_COMPAT,"UTF-8"));
776 $user_cur->step_inc();
782 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
783 if ($user->stat == 'room') {
784 $user_cur = &$this->user[$i];
785 if ($user_cur->sess == '' || $user_cur->stat != 'room')
789 $user_cur = &$this->user[$table->player[$i]];
792 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
793 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
794 $dt.xcape($user->name), xcape($user_mesg));
795 $user_cur->step_inc();
797 log_legal($timecur, $user->sess, $user->name,
798 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
802 function &get_user($sess, &$idx)
804 GLOBAL $PHP_SELF, $G_false;
806 if (validate_sess($sess)) {
807 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
808 if (strcmp($sess, $this->user[$i]->sess) == 0) {
811 $ret = &$this->user[$i];
815 log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
816 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
817 // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
820 log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
827 * function &add_user(&$bri, &$sess, &$idx, $name, $ip)
830 * if ($idx != -1 && ret == FALSE) => duplicated nick
831 * if ($idx == -2 && ret == FALSE) => invalid name
832 * if ($idx == -1 && ret == FALSE) => no space left
833 * if (ret == TRUE) => SUCCESS
835 function &add_user(&$sess, &$idx, $name, $ip)
842 if (($name_new = validate_name($name)) == FALSE) {
847 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
848 if (validate_sess($sess) == FALSE)
851 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
853 if (strcmp($sess, $this->user[$i]->sess) == 0) {
857 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
860 if (strcmp($this->user[$i]->name, $name_new) == 0) {
868 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
870 if ($idx != -1 && $i == MAX_PLAYERS) {
874 $this->user[$idx]->sess = uniqid("");
875 $sess = $this->user[$idx]->sess;
879 $this->user[$idx]->sess = $sess;
881 $this->user[$idx]->name = $name_new;
882 $this->user[$idx]->stat_set("room");
883 // MOP $this->user[$idx]->step_set(0);
884 $this->user[$idx]->subst = "standup";
885 $this->user[$idx]->lacc = $curtime;
886 $this->user[$idx]->laccwr = $curtime;
887 $this->user[$idx]->bantime = 0;
888 $this->user[$idx]->ip = $ip;
889 log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
891 return ($this->user[$idx]);
897 function standup_update(&$user)
899 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
900 $user_cur = &$this->user[$i];
901 if ($user_cur->sess == '')
904 log_main("STANDUP START", $user_cur->stat);
906 if ($user_cur->stat == 'room') {
907 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
908 if ($user_cur == $user)
909 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
911 log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
913 $user_cur->step_inc();
919 } // end class brisco
923 list($usec, $sec) = explode(' ', microtime());
924 return (float) $sec + ((float) $usec * 100000);
927 function log_only($sess, $log) {
928 if (BRISK_DEBUG != TRUE)
932 if (($fp = @fopen("/tmp/brisk_only.log", 'a')) != FALSE) {
933 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
938 function log_main($sess, $log) {
939 if (BRISK_DEBUG != TRUE)
942 if (($fp = @fopen("/tmp/brisk_main.log", 'a')) != FALSE) {
943 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
948 function log_rd($sess, $log) {
949 if (BRISK_DEBUG != TRUE)
952 if (($fp = @fopen("/tmp/brisk_rd.log", 'a')) != FALSE) {
953 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
958 function log_rd2($sess, $log) {
959 if (BRISK_DEBUG != TRUE)
962 if (($fp = @fopen("/tmp/brisk_rd2.log", 'a')) != FALSE) {
963 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
968 function log_send($sess, $log) {
969 if (BRISK_DEBUG != TRUE)
972 if (($fp = @fopen("/tmp/brisk_send.log", 'a')) != FALSE) {
973 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
978 function log_auth($sess, $log) {
979 if (BRISK_DEBUG != TRUE)
982 if (($fp = @fopen("/tmp/brisk_auth.log", 'a')) != FALSE) {
983 fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
988 function log_lock($sess, $log) {
989 if (BRISK_DEBUG != TRUE)
992 if (($fp = @fopen("/tmp/brisk_lock.log", 'a')) != FALSE) {
993 fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
998 function log_wr($sess, $log) {
999 if (BRISK_DEBUG != TRUE)
1002 if (($fp = @fopen("/tmp/brisk_wr.log", 'a')) != FALSE) {
1003 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1008 function log_load($sess, $log) {
1009 if (BRISK_DEBUG != TRUE)
1012 if (($fp = @fopen("/tmp/brisk_load.log", 'a')) != FALSE) {
1013 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1018 function log_legal($timecur, $sess, $name, $where, $mesg)
1022 if (($fp = @fopen(LEGAL_PATH, 'a')) != FALSE) {
1023 /* Unix time | session | nickname | IP | where was | mesg */
1024 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|\n", $timecur, $sess, $name, $_SERVER['REMOTE_ADDR'], $where , $mesg));
1031 function init_data()
1033 $brisco =& new brisco();
1038 function lock_data()
1042 // echo "LOCK: ".FTOK_PATH."/main";
1044 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
1048 // echo "FTOK ".$tok."<br>";
1049 if (($res = sem_get($tok)) == FALSE) {
1050 echo "SEM_GET FAILED";
1053 if (sem_acquire($res)) {
1054 log_only($sess, "LOCK");
1061 function unlock_data($res)
1065 log_only($sess, "UNLOCK");
1066 return (sem_release($res));
1070 function lock_banlist()
1072 if (($tok = ftok(FTOK_PATH."/main", "L")) == -1) {
1076 if (($res = sem_get($tok)) == FALSE) {
1077 echo "SEM_GET FAILED";
1080 if (sem_acquire($res))
1086 function unlock_banlist($res)
1088 return (sem_release($res));
1092 function &load_data()
1094 GLOBAL $G_false, $sess;
1096 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
1101 if ($shm = shm_attach($tok, SHM_DIMS)) {
1102 $bri = @shm_get_var($shm, $tok);
1104 log_only($sess, "bri == ".($bri == FALSE ? "FALSE" : "TRUE")." bri === ".($bri === FALSE ? "FALSE" : "TRUE")." bri isset ".(isset($bri) ? "TRUE" : "FALSE"));
1106 log_only($sess, "bri count ".count($bri));
1108 if ($bri == FALSE) {
1109 log_only($sess, "INIT MAIN DATA");
1112 if (shm_put_var($shm, $tok, $bri) == FALSE) {
1113 log_only($sess, "PUT_VAR FALLITA ".strlen(serialize($bri)));
1114 log_only($sess, serialize($bri));
1128 function save_data(&$bri)
1138 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1)
1144 if (($shm = shm_attach($tok, SHM_DIMS)) == FALSE)
1147 log_only($sess, "PUT_VAR DI ".strlen(serialize($bri)));
1148 if (shm_put_var($shm, $tok, $bri) == FALSE) {
1149 log_only($sess, "PUT_VAR FALLITA ".strlen(serialize($bri)));
1150 log_only($sess, serialize($bri));
1153 // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
1163 function table_act_content($isstanding, $sitted, $table, $cur_table)
1168 if ($sitted < PLAYERS_N) {
1169 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
1173 if ($table == $cur_table)
1174 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
1181 function table_content($bri, $user, $table_idx)
1187 // Si possono usare i dati nella classe table
1190 $sess = $user->sess;
1191 $table = &$bri->table[$table_idx];
1193 if ($user->stat != 'room')
1196 for ($i = 0 ; $i < $table->player_n ; $i++) {
1197 $user_cur = &$bri->user[$table->player[$i]];
1199 if ($user_cur == $user)
1200 { $hilion = "<b>"; $hilioff = "</b>"; }
1202 { $hilion = ""; $hilioff = ""; }
1204 log_main($user_cur->name, sprintf("IN TABLE [%d]", $table_idx));
1206 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
1209 for ( ; $i < PLAYERS_N ; $i++)
1213 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
1218 function standup_content(&$bri, $user)
1223 if ($user->stat != 'room')
1226 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
1227 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
1232 $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
1234 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
1235 if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
1239 if ($bri->user[$e]->subst == "standup") {
1240 if (($ct % 4) == 0) {
1243 if ($bri->user[$e] == $user)
1244 { $hilion = "<b>"; $hilioff = "</b>"; }
1246 { $hilion = ""; $hilioff = ""; }
1248 $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($bri->user[$e]->name), $hilioff);
1249 if (($ct % 4) == 3) {
1250 $content .= '</tr>';
1255 $content .= '</table>';
1257 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
1258 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
1259 $content, $content2);
1265 function show_notify($text, $tout, $butt, $w, $h)
1267 log_main("SHOW_NOTIFY", $text);
1268 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
1271 function briscola_show($bri, $table, $user)
1276 if ($table->asta_card == 9)
1277 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1279 /* text of caller cell */
1280 if ($user->table_pos == $table->asta_win)
1281 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1283 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
1284 xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1286 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1288 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1289 $ret .= sprintf('$("caller").style.visibility = "visible";');
1290 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1291 $ret .= sprintf('$("astalascio").style.visibility = "";');
1292 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1293 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1299 function game_result($asta_pnt, $pnt)
1301 if ($asta_pnt == 61) {
1304 else if ($pnt == 60)
1310 if ($pnt >= $asta_pnt)
1317 function multoval($mult)
1321 else if ($mult == 4)
1322 return ("quadruplo");
1324 return (sprintf("%d-plo", $mult));
1327 function show_table_info(&$bri, &$table, $table_pos)
1330 $user = &$bri->user[$table->player[$table_pos]];
1332 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1333 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1336 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1337 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1338 $noty .= sprintf("</tr>");
1341 log_main("show_table_info", "pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1343 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1344 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1345 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1346 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1351 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1352 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1353 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1354 $noty .= "</tr></table>";
1356 if ($table->old_reason != "") {
1357 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1360 if ($table->old_win != -1) {
1361 $win = $table->player[$table->old_win];
1362 $fri = $table->player[$table->old_friend];
1364 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1367 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
1368 xcape($bri->user[$win]->name),
1369 xcape($bri->user[$fri]->name));
1370 if ($table->old_pnt == 120) {
1371 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1374 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1375 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1376 'più di 60'), $table->old_pnt,
1377 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1381 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1382 xcape($bri->user[$win]->name));
1383 if ($table->old_pnt == 120) {
1384 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1387 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1388 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1389 'più di 60'), $table->old_pnt,
1390 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1394 if ($table->mazzo == $table_pos)
1395 $noty .= "Fai <b>tu</b> il mazzo,";
1397 $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1398 $noty .= "Il mazzo a <b>$unam</b>,";
1401 if ($user->subst == 'asta') {
1402 if ($table->asta_win == -1) // auction case
1403 $curplayer = $table->gstart % PLAYERS_N;
1405 $curplayer = $table->asta_win;
1407 else if ($user->subst == 'game') {
1408 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
1412 if ($curplayer == $table_pos) {
1413 $noty .= " tocca a <b>te</b> giocare.";
1416 $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1417 $noty .= " tocca a <b>$unam</b> giocare.";
1420 if ($table->mult > 1) {
1421 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1423 $noty .= "<hr><br>";
1425 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
1430 function root_wellcome($user)
1432 GLOBAL $root_wellarr;
1435 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
1436 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $root_wellarr[$i]));
1441 function table_wellcome($user)
1443 GLOBAL $table_wellarr;
1446 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1447 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
1452 function show_room(&$bri, &$user)
1454 $ret = sprintf('gst.st = %d;', $user->step);
1455 $ret .= sprintf('stat = "%s";', $user->stat);
1457 $ret .= root_wellcome($user);
1458 $ret .= sprintf('subst = "%s";', $user->subst);
1459 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1460 for ($i = 0 ; $i < TABLES_N ; $i++) {
1461 $ret .= table_content($bri, $user, $i);
1462 $act_content = table_act_content(($user->subst == 'standup'),
1463 $bri->table[$i]->player_n, $i, $user->table);
1464 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1466 $ret .= standup_content($bri, $user);
1474 is_transition (is from room to table ?)
1475 is_again (is another game)
1477 Examples of $is_transition, $is_again:
1478 from reload of the page: FALSE, FALSE
1479 from sitdown in room: TRUE, FALSE
1480 from table: asta cmd e tutti passano: TRUE, TRUE
1481 from table: fine partita: TRUE, TRUE
1483 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1485 $table_idx = $user->table;
1486 $table = &$bri->table[$table_idx];
1487 $table_pos = $user->table_pos;
1489 $ret = "table_init();";
1490 $ret .= $table->exitlock_show(&$bri->user, $table_pos);
1492 /* GENERAL STATUS */
1493 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1494 $sendstep, $user->stat, $user->subst, $table_pos);
1496 $ret .= "background_set();";
1499 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1500 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1501 xcape($bri->user[$table->player[($table_pos)%PLAYERS_N]]->name),
1502 xcape($bri->user[$table->player[($table_pos+1)%PLAYERS_N]]->name),
1503 xcape($bri->user[$table->player[($table_pos+2)%PLAYERS_N]]->name),
1504 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+3)%PLAYERS_N]]->name)),
1505 (PLAYERS_N == 3 ? "" : xcape($bri->user[$table->player[($table_pos+4)%PLAYERS_N]]->name)));
1507 /* NOTIFY FOR THE CARD MAKER */
1508 if ($is_transition) { // && $user->subst == "asta" superfluo
1509 $ret .= show_table_info(&$bri, &$table, $table_pos);
1512 $ret .= table_wellcome($user);
1514 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1515 $ret .= playsound("cow.mp3");
1520 if ($is_transition) { // && $user->subst == "asta" superfluo
1523 for ($i = 0 ; $i < 8 ; $i++) {
1524 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1526 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1527 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1533 log_rd($user->sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1535 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
1536 $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1537 $table->card[$o]->value : -1),
1538 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1543 $taked = array(0,0,0,0,0);
1544 $inhand = array(0,0,0,0,0);
1545 $ontabl = array(-1,-1,-1,-1,-1);
1548 for ($i = 0 ; $i < 40 ; $i++) {
1549 if ($table->card[$i]->stat == 'hand') {
1550 if ($table->card[$i]->owner == $table_pos) {
1551 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1553 $inhand[$table->card[$i]->owner]++;
1555 else if ($table->card[$i]->stat == 'take') {
1556 log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1557 $taked[$table->card[$i]->owner]++;
1559 else if ($table->card[$i]->stat == 'table') {
1560 $ontabl[$table->card[$i]->owner] = $i;
1564 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1565 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1567 log_main("Stat table:", $logg);
1569 /* Set ours cards. */
1571 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1572 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1573 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
1574 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1575 $ret .= sprintf('card_setours(%s);', $oursarg);
1577 /* Dispose all cards */
1578 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1579 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1580 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1581 $inhand[$i], $taked[$i]);
1583 if ($ontabl[$i] != -1) {
1584 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1585 $table->card[$ontabl[$i]]->value,
1586 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1592 if ($user->subst == 'asta') {
1594 /* show users auction status */
1596 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1597 $user_cur = &$bri->user[$table->player[$i]];
1598 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1599 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1602 $showst .= ",-2,-2";
1603 $ret .= sprintf('show_astat(%s);', $showst);
1605 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1606 /* show card chooser */
1607 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1612 if ($table_pos == ($table->gstart % PLAYERS_N) &&
1613 $table->asta_win == -1)
1614 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1615 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1617 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1618 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1622 if ($table->asta_win == -1) { // auction case
1623 if ($table_pos == ($table->gstart % PLAYERS_N))
1624 $ret .= "remark_on();";
1626 $ret .= "remark_off();";
1628 else { // chooseed case
1629 if ($table_pos == $table->asta_win)
1630 $ret .= "remark_on();";
1632 $ret .= "remark_off();";
1635 else if ($user->subst == 'game') {
1637 if (($table->gstart + $table->turn) % PLAYERS_N == $table_pos)
1638 $ret .= "is_my_time = true; remark_on();";
1640 $ret .= "remark_off();";
1642 /* WHO CALL AND WATH */
1643 $ret .= briscola_show($bri, $table, $user);
1649 function calculate_winner(&$table)
1656 $cur_seed = $table->briscola - ($table->briscola % 10);
1658 for ($i = 0 ; $i < 40 ; $i++) {
1659 if ($table->card[$i]->stat != "table")
1662 log_wr($sess, sprintf("Card On table: [%d]", $i));
1664 $v = $table->card[$i]->value;
1665 $ontab[$table->card[$i]->owner] = $v;
1666 $ontid[$table->card[$i]->owner] = $i;
1667 /* se briscola setto il flag */
1668 if (($v - ($v % 10)) == $cur_seed)
1672 if ($briontab == FALSE) {
1673 $cur_win = $table->gstart;
1674 $cur_val = $ontab[$cur_win];
1675 $cur_seed = $cur_val - ($cur_val % 10);
1678 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1679 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1680 if ($ontab[$i] < $cur_val) {
1681 $cur_val = $ontab[$i];
1687 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1688 $table->card[$ontid[$i]]->owner = $cur_win;
1689 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1694 function calculate_points(&$table)
1696 GLOBAL $G_all_points;
1700 if ($table->asta_pnt == 60)
1701 $table->asta_pnt = 61;
1703 $table->old_reason = "";
1704 $table->old_win = $table->asta_win;
1705 $table->old_friend = $table->friend;
1706 $table->old_asta_pnt = $table->asta_pnt;
1708 for ($i = 0 ; $i < 40 ; $i++) {
1709 $ctt = $table->card[$i]->value % 10;
1710 $own = $table->card[$i]->owner;
1711 if ($own == $table->asta_win || $own == $table->friend)
1712 $pro += $G_all_points[$ctt];
1715 log_wr("XXX", sprintf("PRO: [%d]", $pro));
1718 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1719 $table->points[$table->points_n % MAX_POINTS] = array();
1720 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1721 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1723 $table->old_pnt = $pro;
1729 if ($pro >= $table->asta_pnt)
1734 $table->points[$table->points_n % MAX_POINTS] = array();
1735 for ($i = 0 ; $i < 5 ; $i++) {
1736 if ($i == $table->asta_win)
1737 $pt = ($i == $table->friend ? 4 : 2);
1738 else if ($i == $table->friend)
1743 log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1745 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1747 log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1749 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1750 $table->total[$i] += $pt;
1753 $table->old_pnt = $pro;
1757 function validate_sess($sess)
1759 if (strlen($sess) == SESS_LEN)
1765 function validate_name($name)
1767 $name_new = str_replace(' ', '_', substr(trim($name),0,12));
1769 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
1771 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
1778 function playsound($filename)
1780 return (sprintf('playsound("flasou", "%s");', $filename));
1783 function secstoword($secs)
1785 $mins = floor($secs / 60);
1788 $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
1791 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));