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.2";
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
175 if (($thiz = new Table()) == FALSE)
178 $thiz->player = array();
180 $thiz->card = &$thiz->bunch_create();
181 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
182 $thiz->asta_pla_n= -1;
183 $thiz->asta_card = -1;
184 $thiz->asta_pnt = -1;
186 $thiz->points = array( );
188 $thiz->total = array( 0, 0, 0, 0, 0);
189 $thiz->asta_win = -1;
190 $thiz->briscola = -1;
193 $thiz->old_reason = "";
194 $thiz->old_asta_pnt = -1;
197 $thiz->old_friend= -1;
202 function &bunch_create()
206 for ($i = 0 ; $i < 40 ; $i++) {
207 $ret[$i] =& new Card($i, 'bunch', 'no_owner');
214 function bunch_make()
216 $ct = array(0,0,0,0,0);
218 mt_srand(make_seed());
220 for ($i = 39 ; $i >= 0 ; $i--)
223 for ($i = 39 ; $i >= 0 ; $i--) {
227 log_main("RND ZERO", "");
232 $this->card[$id]->assign('hand', $owner);
234 $rest[$rn] = $rest[$i];
235 // $pubbpos[$rn2] = $pubbpos[$i];
239 function init(&$userarr)
241 $this->mazzo = rand(0,PLAYERS_N-1);
245 $this->old_reason = "";
246 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
247 $this->total[$i] = 0;
248 $user_cur = &$userarr[$this->player[$i]];
249 $user_cur->exitislock = TRUE;
252 log_main("table::init","ci siamo");
255 function game_init(&$userarr)
257 log_rd2("XXX", "GSTART 4");
259 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
263 $this->asta_pla_n = PLAYERS_N;
264 $this->asta_card = -1;
265 $this->asta_pnt = 60;
266 $this->asta_win = -1;
267 $this->briscola = -1;
271 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
272 $this->asta_pla[$i] = TRUE;
273 $user_cur = &$userarr[$this->player[$i]];
274 $user_cur->subst = 'asta';
275 $user_cur->asta_card = -2;
276 $user_cur->asta_pnt = -1;
277 $user_cur->handpt = $this->hand_points($i);
278 // SEE function calculate_points(&$table)
284 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
287 function getPlayer($idx)
289 return ($this->player[$idx]);
292 function setPlayer($idx, $player)
294 $this->player[$idx] = $player;
297 function user_add($idx)
299 $this->player[$this->player_n] = $idx;
302 return ($this->player_n - 1);
305 function user_rem(&$room, &$user)
307 $tabpos = $user->table_pos;
309 /* verifico la consistenza dei dati */
310 if ($room->user[$this->player[$tabpos]] == $user) {
312 /* aggiorna l'array dei giocatori al tavolo. */
313 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
314 $this->player[$i] = $this->player[$i+1];
315 $user_cur = &$room->user[$this->player[$i]];
316 $user_cur->table_pos = $i;
321 log_main($user->sess, "INCONSISTENCY ON TABLE.");
325 function hand_points($idx)
327 GLOBAL $G_all_points;
331 for ($i = 0 ; $i < 40 ; $i++) {
332 if ($this->card[$i]->owner != $idx)
335 $ctt = $this->card[$i]->value % 10;
336 $tot += $G_all_points[$ctt];
342 function exitlock_show(&$userarr, $table_pos)
344 $ct = $this->exitlock_calc(&$userarr, $table_pos);
346 $ret = sprintf('exitlock_show(%d, %s);', $ct,
347 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
351 function exitlock_calc(&$userarr, $table_pos)
355 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
356 if ($userarr[$this->player[$i]]->exitislock == FALSE)
365 var $name; // name of the user
366 var $sess; // session of the user
367 var $ip; // ip of the user
368 var $lacc; // last access (for the cleanup)
369 var $laccwr; // last access (for the cleanup)
370 var $bantime; // timeout to temporary ban
371 var $stat; // status (outdoor, room, table, game, ...)
372 var $subst; // substatus for each status
373 var $step; // step of the current status
374 var $trans_step; // step to enable transition between pages (disable == -1)
375 var $comm; // commands array
378 var $handpt; // Total card points at the beginning of the current hand.
379 var $exitislock; // Player can exit from the table ?
380 var $table; // id of the current table (if in table state)
381 var $table_pos; // idx on the table
382 var $the_end; // Flag to change the end of the session
387 function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
390 if (($thiz = new User()) == FALSE)
396 $thiz->lacc = time();
397 $thiz->laccwr = time();
400 $thiz->subst = $subst;
402 $thiz->trans_step = -1;
403 $thiz->comm = array();
404 $thiz->asta_card = -2;
405 $thiz->asta_pnt = -1;
407 $thiz->exitislock = TRUE;
409 $thiz->table = $table;
414 function stat_set($stat) {
415 $this->stat = "$stat";
418 if (validate_sess($this->sess)) {
419 $fp = fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
420 fwrite($fp, sprintf("%s\n",$this->stat));
426 function step_set($step) {
430 if (validate_sess($this->sess) == FALSE)
432 if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
434 fwrite($fp, pack("l",$this->step), 4);
443 function step_inc() {
446 if (validate_sess($this->sess)) {
447 $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
448 fwrite($fp, pack("l",$this->step), 4);
458 function step_get($sess) {
462 if (validate_sess($sess) == FALSE)
465 if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
468 if (($s = fread($fp, 4)) == FALSE)
474 $arr = unpack('l', $s);
477 // log_rd2($sess, "A0: ".$arr[0]." A1: ".$arr[1]);
484 log_rd2($sess, "STEP_GET: return false ".$ct);
488 function step_unproxy($sess) {
489 log_rd2($sess, "UNPROXY: ".PROXY_PATH."/".$sess.".step");
490 @unlink(PROXY_PATH."/".$sess.".step");
497 var $comm; // commands for many people
498 var $step; // current step of the comm array
499 var $garbage_timeout;
502 $this->user = array();
504 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
505 $this->user[$i] =& User::create("", "");
508 for ($i = 0 ; $i < TABLES_N ; $i++)
509 $this->table[$i] =& Table::create();
510 $this->garbage_timeout = 0;
513 function garbage_manager($force)
516 /* Garbage collector degli utenti in timeout */
518 if ($force || $this->garbage_timeout < $curtime) {
520 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
521 $user_cur = &$this->user[$i];
522 if ($user_cur->sess == "")
525 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
526 log_rd2($user_cur->sess, "AUTO LOGOUT.");
528 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
529 log_auth($user_cur->sess, "Autologout session.");
531 $tmp_sess = $user_cur->sess;
532 $user_cur->sess = "";
533 step_unproxy($tmp_sess);
534 $user_cur->name = "";
535 $user_cur->the_end = FALSE;
537 log_rd2($user_cur->sess, "AUTO LOGOUT.");
538 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
539 $this->room_wakeup(&$user_cur);
540 else if ($user_cur->subst == 'standup')
541 $this->room_outstandup(&$user_cur);
543 log_rd2($sess, "LOGOUT FROM WHAT ???");
547 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
548 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
549 $this->room_wakeup(&$user_cur);
550 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
551 $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);
552 $user_cur->step_inc();
556 log_rd2($user_cur->sess, "GARBAGE UPDATED!");
558 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
566 function room_wakeup(&$user)
568 $table_idx = $user->table;
569 $table = &$this->table[$table_idx];
571 log_main("WAKEUP", "begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
575 $from_table = ($user->stat == "table");
577 log_main("WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
579 for ($i = 0 ; $i < $table->player_n ; $i++) {
580 $user_cur = &$this->user[$table->player[$i]];
581 log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
583 if ($user_cur != $user) {
584 $user_cur->stat_set("room");
585 $user_cur->subst = "sitdown";
586 $user_cur->laccwr = $curtime;
588 else if ($user->sess != "") {
589 $user_cur->stat_set("room");
590 $user_cur->subst = "standup";
591 $user_cur->laccwr = $curtime;
592 $user_cur->table = -1;
597 $user->stat_set("room");
598 $user->subst = "standup";
599 $user->laccwr = $curtime;
601 /* aggiorna l'array dei giocatori al tavolo. */
602 $table->user_rem(&$this, &$user);
604 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
605 $user_cur = &$this->user[$i];
606 if ($user_cur->sess == '' || $user_cur->stat != 'room')
609 log_main("VALORI", "name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
611 $ret = "gst.st = ".($user_cur->step+1)."; ";
612 if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
613 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
614 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
615 log_main("DOCUMENT.index.php", "from table");
617 else if ($user_cur->stat == "room") {
618 log_main("DOCUMENT.index.php", "from table");
620 $ret .= table_content($this, $user_cur, $table_idx);
621 $ret .= standup_content($this, $user_cur);
623 $act_content = table_act_content(FALSE, 0, $table_idx, $user->table);
624 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
627 if ($user_cur == $user) {
628 // set the new status
629 $ret .= 'subst = "standup"; ';
630 // clean the action buttons in other tables
631 for ($e = 0 ; $e < TABLES_N ; $e++) {
632 if ($this->table[$e]->player_n < PLAYERS_N)
633 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
637 $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
638 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
641 log_wr($user_cur->sess, "ROOM_WAKEUP: ".$ret);
642 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
643 $user_cur->step_inc();
650 function room_outstandup(&$user)
652 $this->room_sitdown(&$user, -1);
655 function table_update(&$user)
657 log_main("table_update", "pre - USER: ".$user->name);
659 $table_idx = $user->table;
662 $table = &$this->table[$table_idx];
664 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
666 $user_cur = &$this->user[$i];
667 if ($user_cur->sess == '' || $user_cur->stat != 'room')
670 $ret = "gst.st = ".($user_cur->step+1)."; ";
672 $ret .= table_content($this, $user_cur, $table_idx);
674 if ($user_cur == $user) {
675 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
677 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
678 $user_cur->step_inc();
681 log_main("table_update", "post");
684 function room_sitdown(&$user, $table_idx)
686 log_main("room_sitdown", ($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
689 $table = &$this->table[$table_idx];
691 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
693 $user_cur = &$this->user[$i];
694 if ($user_cur->sess == '' || $user_cur->stat != 'room')
697 $ret = "gst.st = ".($user_cur->step+1)."; ";
699 $ret .= table_content($this, $user_cur, $table_idx);
700 $ret .= standup_content($this, $user_cur);
702 if ($user_cur == $user) {
703 $ret .= 'subst = "sitdown"; ';
704 // clean the action buttons in other tables
705 for ($e = 0 ; $e < TABLES_N ; $e++) {
706 $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
707 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
710 else if ($table_idx > -1) {
711 if ($table->player_n == PLAYERS_N) {
712 $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
713 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
716 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
717 $user_cur->step_inc();
721 function chatt_send(&$user, $mesg)
723 if ($user->stat == 'table') {
724 $table = &$this->table[$user->table];
727 $user_mesg = substr($mesg,6);
731 $dt = date("H:i ", $timecur);
732 if (strncmp($user_mesg, "/nick ", 6) == 0) {
733 log_main($user->sess, "chatt_send BEGIN");
735 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
736 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
737 $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));
742 $user_mesg = "COMMAND ".$user_mesg;
745 // update local graph
746 // update remote graphs
747 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
748 $user_cur = &$this->user[$i];
749 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
750 if ($user_cur->sess == '')
752 if ($user_cur->name == $name_new) {
753 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
754 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
759 if ($i == MAX_PLAYERS) {
760 $user->name = $name_new;
762 log_main($user->sess, "chatt_send start set");
765 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
766 log_main($user->sess, "chatt_send set loop");
768 $user_cur = &$this->user[$i];
769 if ($user_cur->sess == '')
771 if ($user_cur->stat == 'room') {
772 if ($user->stat == 'room' && $user->subst == 'standup') {
773 $this->standup_update(&$user);
775 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
776 $user->stat == 'table') {
777 log_main($user->sess, "chatt_send pre table update");
779 $this->table_update(&$user);
781 log_main($user->sess, "chatt_send post table update");
784 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
785 $table = &$this->table[$user->table];
787 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
788 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
789 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
790 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
791 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
792 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
793 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
794 if ($user_cur == $user)
795 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
796 xcape($user->name,ENT_COMPAT,"UTF-8"));
797 $user_cur->step_inc();
803 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
804 if ($user->stat == 'room') {
805 $user_cur = &$this->user[$i];
806 if ($user_cur->sess == '' || $user_cur->stat != 'room')
810 $user_cur = &$this->user[$table->player[$i]];
813 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
814 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
815 $dt.xcape($user->name), xcape($user_mesg));
816 $user_cur->step_inc();
818 log_legal($timecur, $user->sess, $user->name,
819 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
823 function &get_user($sess, &$idx)
825 GLOBAL $PHP_SELF, $G_false;
827 if (validate_sess($sess)) {
828 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
829 if (strcmp($sess, $this->user[$i]->sess) == 0) {
832 $ret = &$this->user[$i];
836 log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
837 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
838 // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
841 log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
848 * function &add_user(&$room, &$sess, &$idx, $name, $ip)
851 * if ($idx != -1 && ret == FALSE) => duplicated nick
852 * if ($idx == -2 && ret == FALSE) => invalid name
853 * if ($idx == -1 && ret == FALSE) => no space left
854 * if (ret == TRUE) => SUCCESS
856 function &add_user(&$sess, &$idx, $name, $ip)
863 if (($name_new = validate_name($name)) == FALSE) {
868 log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
869 if (validate_sess($sess) == FALSE)
872 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
874 if (strcmp($sess, $this->user[$i]->sess) == 0) {
878 if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
881 if (strcmp($this->user[$i]->name, $name_new) == 0) {
889 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
891 if ($idx != -1 && $i == MAX_PLAYERS) {
895 $this->user[$idx]->sess = uniqid("");
896 $sess = $this->user[$idx]->sess;
900 $this->user[$idx]->sess = $sess;
902 $this->user[$idx]->name = $name_new;
903 $this->user[$idx]->stat_set("room");
904 // MOP $this->user[$idx]->step_set(0);
905 $this->user[$idx]->subst = "standup";
906 $this->user[$idx]->lacc = $curtime;
907 $this->user[$idx]->laccwr = $curtime;
908 $this->user[$idx]->bantime = 0;
909 $this->user[$idx]->ip = $ip;
910 log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
912 return ($this->user[$idx]);
918 function standup_update(&$user)
920 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
921 $user_cur = &$this->user[$i];
922 if ($user_cur->sess == '')
925 log_main("STANDUP START", $user_cur->stat);
927 if ($user_cur->stat == 'room') {
928 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
929 if ($user_cur == $user)
930 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";', xcape($user->name));
932 log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
934 $user_cur->step_inc();
940 function &init_data()
948 function &load_data()
950 GLOBAL $G_false, $sess;
952 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
957 if ($shm = shm_attach($tok, SHM_DIMS)) {
958 $room = @shm_get_var($shm, $tok);
960 log_only($sess, "bri == ".($room == FALSE ? "FALSE" : "TRUE")." bri === ".($room === FALSE ? "FALSE" : "TRUE")." bri isset ".(isset($room) ? "TRUE" : "FALSE"));
962 log_only($sess, "bri count ".count($room));
964 if ($room == FALSE) {
965 log_only($sess, "INIT MAIN DATA");
967 $room =& Room::init_data();
968 if (shm_put_var($shm, $tok, $room) == FALSE) {
969 log_only($sess, "PUT_VAR FALLITA ".strlen(serialize($room)));
970 log_only($sess, serialize($room));
984 function save_data(&$room)
994 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1)
1000 if (($shm = shm_attach($tok, SHM_DIMS)) == FALSE)
1003 // log_only($sess, "PUT_VAR DI ".strlen(serialize($room)));
1004 if (shm_put_var($shm, $tok, $room) == FALSE) {
1005 log_only($sess, "PUT_VAR FALLITA ".strlen(serialize($room)));
1006 log_only($sess, serialize($room));
1009 // log_main("XXX", "QUI CI ARRIVA [".$room->user[0]->name."]");
1019 function lock_data()
1023 // echo "LOCK: ".FTOK_PATH."/main";
1025 if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
1029 // echo "FTOK ".$tok."<br>";
1030 if (($res = sem_get($tok)) == FALSE) {
1031 echo "SEM_GET FAILED";
1034 if (sem_acquire($res)) {
1035 log_only($sess, "LOCK");
1042 function unlock_data($res)
1046 log_only($sess, "UNLOCK");
1048 return (sem_release($res));
1052 function make_seed()
1054 list($usec, $sec) = explode(' ', microtime());
1055 return (float) $sec + ((float) $usec * 100000);
1058 function log_only2($sess, $log) {
1059 if (BRISK_DEBUG != TRUE)
1063 if (($fp = @fopen("/tmp/brisk_only2.log", 'a')) != FALSE) {
1064 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1069 function log_only($sess, $log) {
1070 if (BRISK_DEBUG != TRUE)
1074 if (($fp = @fopen("/tmp/brisk_only.log", 'a')) != FALSE) {
1075 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1080 function log_main($sess, $log) {
1081 if (BRISK_DEBUG != TRUE)
1084 if (($fp = @fopen("/tmp/brisk_main.log", 'a')) != FALSE) {
1085 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1090 function log_rd($sess, $log) {
1091 if (BRISK_DEBUG != TRUE)
1094 if (($fp = @fopen("/tmp/brisk_rd.log", 'a')) != FALSE) {
1095 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1100 function log_rd2($sess, $log) {
1101 if (BRISK_DEBUG != TRUE)
1104 if (($fp = @fopen("/tmp/brisk_rd2.log", 'a')) != FALSE) {
1105 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1110 function log_send($sess, $log) {
1111 if (BRISK_DEBUG != TRUE)
1114 if (($fp = @fopen("/tmp/brisk_send.log", 'a')) != FALSE) {
1115 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1120 function log_auth($sess, $log) {
1121 if (BRISK_DEBUG != TRUE)
1124 if (($fp = @fopen("/tmp/brisk_auth.log", 'a')) != FALSE) {
1125 fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
1130 function log_lock($sess, $log) {
1131 if (BRISK_DEBUG != TRUE)
1134 if (($fp = @fopen("/tmp/brisk_lock.log", 'a')) != FALSE) {
1135 fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
1140 function log_wr($sess, $log) {
1141 if (BRISK_DEBUG != TRUE)
1144 if (($fp = @fopen("/tmp/brisk_wr.log", 'a')) != FALSE) {
1145 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1150 function log_load($sess, $log) {
1151 if (BRISK_DEBUG != TRUE)
1154 if (($fp = @fopen("/tmp/brisk_load.log", 'a')) != FALSE) {
1155 fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1160 function log_legal($timecur, $sess, $name, $where, $mesg)
1164 if (($fp = @fopen(LEGAL_PATH, 'a')) != FALSE) {
1165 /* Unix time | session | nickname | IP | where was | mesg */
1166 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|\n", $timecur, $sess, $name, $_SERVER['REMOTE_ADDR'], $where , $mesg));
1174 function lock_banlist()
1176 if (($tok = ftok(FTOK_PATH."/main", "L")) == -1) {
1180 if (($res = sem_get($tok)) == FALSE) {
1181 echo "SEM_GET FAILED";
1184 if (sem_acquire($res))
1190 function unlock_banlist($res)
1192 return (sem_release($res));
1195 function table_act_content($isstanding, $sitted, $table, $cur_table)
1200 if ($sitted < PLAYERS_N) {
1201 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\" value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
1205 if ($table == $cur_table)
1206 $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\" value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
1213 function table_content($room, $user, $table_idx)
1219 // Si possono usare i dati nella classe table
1222 $sess = $user->sess;
1223 $table = &$room->table[$table_idx];
1225 if ($user->stat != 'room')
1228 for ($i = 0 ; $i < $table->player_n ; $i++) {
1229 $user_cur = &$room->user[$table->player[$i]];
1231 if ($user_cur == $user)
1232 { $hilion = "<b>"; $hilioff = "</b>"; }
1234 { $hilion = ""; $hilioff = ""; }
1236 log_main($user_cur->name, sprintf("IN TABLE [%d]", $table_idx));
1238 $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
1241 for ( ; $i < PLAYERS_N ; $i++)
1245 $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
1250 function standup_content(&$room, $user)
1255 if ($user->stat != 'room')
1258 for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
1259 if ($room->user[$e]->sess == "" || $room->user[$e]->stat != "room" || $room->user[$e]->name == "")
1264 $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
1266 for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
1267 if ($room->user[$e]->sess == "" || $room->user[$e]->stat != "room" || $room->user[$e]->name == "")
1271 if ($room->user[$e]->subst == "standup") {
1272 if (($ct % 4) == 0) {
1275 if ($room->user[$e] == $user)
1276 { $hilion = "<b>"; $hilioff = "</b>"; }
1278 { $hilion = ""; $hilioff = ""; }
1280 $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($room->user[$e]->name), $hilioff);
1281 if (($ct % 4) == 3) {
1282 $content .= '</tr>';
1287 $content .= '</table>';
1289 $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
1290 $ret .= sprintf('$("standup").innerHTML = "%s"; $("esco").innerHTML = "%s";',
1291 $content, $content2);
1297 function show_notify($text, $tout, $butt, $w, $h)
1299 log_main("SHOW_NOTIFY", $text);
1300 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
1303 function briscola_show($room, $table, $user)
1308 if ($table->asta_card == 9)
1309 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1311 /* text of caller cell */
1312 if ($user->table_pos == $table->asta_win)
1313 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1315 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
1316 xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
1318 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1320 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1321 $ret .= sprintf('$("caller").style.visibility = "visible";');
1322 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1323 $ret .= sprintf('$("astalascio").style.visibility = "";');
1324 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1325 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1331 function game_result($asta_pnt, $pnt)
1333 if ($asta_pnt == 61) {
1336 else if ($pnt == 60)
1342 if ($pnt >= $asta_pnt)
1349 function multoval($mult)
1353 else if ($mult == 4)
1354 return ("quadruplo");
1356 return (sprintf("%d-plo", $mult));
1359 function show_table_info(&$room, &$table, $table_pos)
1362 $user = &$room->user[$table->player[$table_pos]];
1364 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1365 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1368 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1369 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
1370 $noty .= sprintf("</tr>");
1373 log_main("show_table_info", "pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1375 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1376 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1377 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1378 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1383 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1384 for ($e = 0 ; $e < PLAYERS_N ; $e++)
1385 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1386 $noty .= "</tr></table>";
1388 if ($table->old_reason != "") {
1389 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1392 if ($table->old_win != -1) {
1393 $win = $table->player[$table->old_win];
1394 $fri = $table->player[$table->old_friend];
1396 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1399 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
1400 xcape($room->user[$win]->name),
1401 xcape($room->user[$fri]->name));
1402 if ($table->old_pnt == 120) {
1403 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1406 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1407 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1408 'più di 60'), $table->old_pnt,
1409 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1413 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1414 xcape($room->user[$win]->name));
1415 if ($table->old_pnt == 120) {
1416 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1419 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1420 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1421 'più di 60'), $table->old_pnt,
1422 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1426 if ($table->mazzo == $table_pos)
1427 $noty .= "Fai <b>tu</b> il mazzo,";
1429 $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
1430 $noty .= "Il mazzo a <b>$unam</b>,";
1433 if ($user->subst == 'asta') {
1434 if ($table->asta_win == -1) // auction case
1435 $curplayer = $table->gstart % PLAYERS_N;
1437 $curplayer = $table->asta_win;
1439 else if ($user->subst == 'game') {
1440 $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
1444 if ($curplayer == $table_pos) {
1445 $noty .= " tocca a <b>te</b> giocare.";
1448 $unam = xcape($room->user[$table->player[$curplayer]]->name);
1449 $noty .= " tocca a <b>$unam</b> giocare.";
1452 if ($table->mult > 1) {
1453 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1455 $noty .= "<hr><br>";
1457 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
1462 function root_wellcome($user)
1464 GLOBAL $root_wellarr;
1467 for ($i = 0 ; $i < count($root_wellarr) ; $i++)
1468 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $root_wellarr[$i]));
1473 function table_wellcome($user)
1475 GLOBAL $table_wellarr;
1478 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1479 $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
1484 function show_room(&$room, &$user)
1486 $ret = sprintf('gst.st = %d;', $user->step);
1487 $ret .= sprintf('stat = "%s";', $user->stat);
1489 $ret .= root_wellcome($user);
1490 $ret .= sprintf('subst = "%s";', $user->subst);
1491 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1492 for ($i = 0 ; $i < TABLES_N ; $i++) {
1493 $ret .= table_content($room, $user, $i);
1494 $act_content = table_act_content(($user->subst == 'standup'),
1495 $room->table[$i]->player_n, $i, $user->table);
1496 $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1498 $ret .= standup_content($room, $user);
1506 is_transition (is from room to table ?)
1507 is_again (is another game)
1509 Examples of $is_transition, $is_again:
1510 from reload of the page: FALSE, FALSE
1511 from sitdown in room: TRUE, FALSE
1512 from table: asta cmd e tutti passano: TRUE, TRUE
1513 from table: fine partita: TRUE, TRUE
1515 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
1517 $table_idx = $user->table;
1518 $table = &$room->table[$table_idx];
1519 $table_pos = $user->table_pos;
1521 $ret = "table_init();";
1522 $ret .= $table->exitlock_show(&$room->user, $table_pos);
1524 /* GENERAL STATUS */
1525 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1526 $sendstep, $user->stat, $user->subst, $table_pos);
1528 $ret .= "background_set();";
1531 $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1532 $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1533 xcape($room->user[$table->player[($table_pos)%PLAYERS_N]]->name),
1534 xcape($room->user[$table->player[($table_pos+1)%PLAYERS_N]]->name),
1535 xcape($room->user[$table->player[($table_pos+2)%PLAYERS_N]]->name),
1536 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+3)%PLAYERS_N]]->name)),
1537 (PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+4)%PLAYERS_N]]->name)));
1539 /* NOTIFY FOR THE CARD MAKER */
1540 if ($is_transition) { // && $user->subst == "asta" superfluo
1541 $ret .= show_table_info(&$room, &$table, $table_pos);
1544 $ret .= table_wellcome($user);
1546 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1547 $ret .= playsound("cow.mp3");
1552 if ($is_transition) { // && $user->subst == "asta" superfluo
1555 for ($i = 0 ; $i < 8 ; $i++) {
1556 for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1558 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1559 if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1565 log_rd($user->sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1567 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N,
1568 $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1569 $table->card[$o]->value : -1),
1570 ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1575 $taked = array(0,0,0,0,0);
1576 $inhand = array(0,0,0,0,0);
1577 $ontabl = array(-1,-1,-1,-1,-1);
1580 for ($i = 0 ; $i < 40 ; $i++) {
1581 if ($table->card[$i]->stat == 'hand') {
1582 if ($table->card[$i]->owner == $table_pos) {
1583 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1585 $inhand[$table->card[$i]->owner]++;
1587 else if ($table->card[$i]->stat == 'take') {
1588 log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1589 $taked[$table->card[$i]->owner]++;
1591 else if ($table->card[$i]->stat == 'table') {
1592 $ontabl[$table->card[$i]->owner] = $i;
1596 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1597 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1599 log_main("Stat table:", $logg);
1601 /* Set ours cards. */
1603 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1604 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1605 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
1606 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1607 $ret .= sprintf('card_setours(%s);', $oursarg);
1609 /* Dispose all cards */
1610 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1611 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1612 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1613 $inhand[$i], $taked[$i]);
1615 if ($ontabl[$i] != -1) {
1616 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1617 $table->card[$ontabl[$i]]->value,
1618 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1624 if ($user->subst == 'asta') {
1626 /* show users auction status */
1628 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1629 $user_cur = &$room->user[$table->player[$i]];
1630 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1631 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1634 $showst .= ",-2,-2";
1635 $ret .= sprintf('show_astat(%s);', $showst);
1637 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1638 /* show card chooser */
1639 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1644 if ($table_pos == ($table->gstart % PLAYERS_N) &&
1645 $table->asta_win == -1)
1646 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1647 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1649 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1650 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1654 if ($table->asta_win == -1) { // auction case
1655 if ($table_pos == ($table->gstart % PLAYERS_N))
1656 $ret .= "remark_on();";
1658 $ret .= "remark_off();";
1660 else { // chooseed case
1661 if ($table_pos == $table->asta_win)
1662 $ret .= "remark_on();";
1664 $ret .= "remark_off();";
1667 else if ($user->subst == 'game') {
1669 if (($table->gstart + $table->turn) % PLAYERS_N == $table_pos)
1670 $ret .= "is_my_time = true; remark_on();";
1672 $ret .= "remark_off();";
1674 /* WHO CALL AND WATH */
1675 $ret .= briscola_show($room, $table, $user);
1681 function calculate_winner(&$table)
1688 $cur_seed = $table->briscola - ($table->briscola % 10);
1690 for ($i = 0 ; $i < 40 ; $i++) {
1691 if ($table->card[$i]->stat != "table")
1694 log_wr($sess, sprintf("Card On table: [%d]", $i));
1696 $v = $table->card[$i]->value;
1697 $ontab[$table->card[$i]->owner] = $v;
1698 $ontid[$table->card[$i]->owner] = $i;
1699 /* se briscola setto il flag */
1700 if (($v - ($v % 10)) == $cur_seed)
1704 if ($briontab == FALSE) {
1705 $cur_win = $table->gstart;
1706 $cur_val = $ontab[$cur_win];
1707 $cur_seed = $cur_val - ($cur_val % 10);
1710 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1711 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1712 if ($ontab[$i] < $cur_val) {
1713 $cur_val = $ontab[$i];
1719 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1720 $table->card[$ontid[$i]]->owner = $cur_win;
1721 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1726 function calculate_points(&$table)
1728 GLOBAL $G_all_points;
1732 if ($table->asta_pnt == 60)
1733 $table->asta_pnt = 61;
1735 $table->old_reason = "";
1736 $table->old_win = $table->asta_win;
1737 $table->old_friend = $table->friend;
1738 $table->old_asta_pnt = $table->asta_pnt;
1740 for ($i = 0 ; $i < 40 ; $i++) {
1741 $ctt = $table->card[$i]->value % 10;
1742 $own = $table->card[$i]->owner;
1743 if ($own == $table->asta_win || $own == $table->friend)
1744 $pro += $G_all_points[$ctt];
1747 log_wr("XXX", sprintf("PRO: [%d]", $pro));
1750 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1751 $table->points[$table->points_n % MAX_POINTS] = array();
1752 for ($i = 0 ; $i < PLAYERS_N ; $i++)
1753 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1755 $table->old_pnt = $pro;
1761 if ($pro >= $table->asta_pnt)
1766 $table->points[$table->points_n % MAX_POINTS] = array();
1767 for ($i = 0 ; $i < 5 ; $i++) {
1768 if ($i == $table->asta_win)
1769 $pt = ($i == $table->friend ? 4 : 2);
1770 else if ($i == $table->friend)
1775 log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1777 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1779 log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1781 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1782 $table->total[$i] += $pt;
1785 $table->old_pnt = $pro;
1789 function validate_sess($sess)
1791 if (strlen($sess) == SESS_LEN)
1797 function validate_name($name)
1799 $name_new = str_replace(' ', '_', substr(trim($name),0,12));
1801 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
1803 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
1810 function playsound($filename)
1812 return (sprintf('playsound("flasou", "%s");', $filename));
1815 function secstoword($secs)
1817 $mins = floor($secs / 60);
1820 $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
1823 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));