5 * Copyright (C) 2006-2009 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details. You should have received a
19 * copy of the GNU General Public License along with this program; if
20 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21 * Suite 330, Boston, MA 02111-1307, USA.
26 define(BRISKIN5_PLAYERS_N, 3);
27 define(BRISKIN5_MAX_PLAYERS, BRISKIN5_PLAYERS_N);
28 // define(BRISKIN5_SHM_MIN, (50000 * BRISKIN5_MAX_PLAYERS));
29 define(BRISKIN5_SHM_MIN, 32768);
30 define(BRISKIN5_SHM_MAX, (BRISKIN5_SHM_MIN + 1048576));
31 define(BRISKIN5_SHM_DLT, 32768);
33 $mlang_bin5_bin5 = array(
34 'info_part' => array( 'it' => '<hr>Nell\'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>',
35 'en' => '<hr>In the last hand the declarer was <b>%s</b>, the partner was <b>%s</b>,<br>'),
36 'info_capp' => array( 'it' => 'hanno fatto <b>cappotto</b> EBBRAVI!.<br>',
37 'en' => 'and they made <b>capot</b> WELL DONE!.<br>'),
38 'info_pnt' => array( 'it' => 'dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno %s.<br>',
39 'en' => 'they had to do <b>%s</b> points and they had made <b>%d</b>: they have %s.<br>'),
40 'info_alea' => array( 'it' => 'almeno ',
42 'info_more' => array( 'it' => 'più di 60',
44 'info_win' => array( 'it' => '<b>vinto</b>',
45 'en' => '<b>win</b>'),
46 'info_peer' => array( 'it' => '<b>pareggiato</b>',
47 'en' => '<b>drew</b>'),
48 'info_lost' => array( 'it' => '<b>perso</b>',
49 'en' => '<b>lost</b>'),
51 'info_alon' => array( 'it' => '<hr>Nell\'ultima partita <b>%s</b> si è chiamato in mano,<br>',
52 'en' => '<hr>In the last hand <b>%s</b> play alone against each other,<br>'),
53 'info_apnt' => array( 'it' => 'doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha %s.<br>',
54 'en' => 'he/she had to do <b>%s</b> points and they had made <b>%d</b>: he/she had %s.<br>'),
55 'info_acap' => array( 'it' => 'ha fatto <b>cappotto</b> EBBRAVO!.<hr>',
56 'en' => 'and he/she made <b>capot</b> WELL DONE!.<hr>'),
58 'info_omul' => array( 'it' => ' La partita valeva <b>%s</b>.',
59 'en' => ' EN: The game was worth <b>%s</b>.' ),
61 'info_yturn'=> array( 'it' => ' tocca a <b>te</b> giocare.',
62 'en' => ' it\'s <b>your</b> turn.'),
63 'info_turn' => array( 'it' => 'tocca a <b>%s</b> giocare.',
64 'en' => 'it\'s the <b>%s</b>\'s turn.'),
65 'info_mult' => array( 'it' => ' La partita vale <b>%s</b>.',
66 'en' => ' The game worth <b>%s</b>.' ),
67 'info_yshuf'=> array( 'it' => 'Fai <b>tu</b> il mazzo, ',
68 'en' => 'It\'s <b>your</b> shuffled the cards, '),
69 'info_shuf' => array( 'it' => 'Il mazzo a <b>%s</b>, ',
70 'en' => '<b>%s</b> shuffled the cards, '),
71 'btn_bkgame'=> array( 'it' => 'torna alla partita',
72 'en' => 'back to the game'),
73 'call_wptn' => array( 'it' => '<br>con %d punti',
74 'en' => '<br>with %d points'),
75 'call_ycall'=> array( 'it' => 'Chiami%s:',
77 'call_call' => array( 'it' => 'Chiama %s%s:',
78 'en' => 'The declarer is %s%s:')
84 $table_wellarr = Array( 'it' => 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.'),
85 'en' => Array ( 'EN 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.') );
87 function multoval($mult)
92 return ($G_lang == 'en' ? 'double' : 'doppio');
94 return ($G_lang == 'en' ? 'triple' : 'triplo');
96 return ($G_lang == 'en' ? 'quadruple' : 'quadruplo');
98 return (sprintf(($G_lang == 'en' ? "%d-ple" : "%d-plo"), $mult));
103 var $value; /* 0 - 39 card value */
104 var $stat; /* 'bunch', 'hand', 'table', 'take' */
105 var $owner; /* (table position 0-4) */
106 // var $pos; /* Pos in hand. */
107 var $x; /* When played the X position on the table of the owner. */
108 var $y; /* When played the Y position on the table of the owner. */
110 function Card($value, $stat, $owner)
112 $this->value = $value;
113 $this->stat = $stat; // Card stat
114 $this->owner = $owner;
117 function assign($stat,$owner)
119 $this->stat = $stat; // Card stat
120 $this->owner = $owner;
123 function setpos($pos)
130 $this->stat = 'table'; // Card stat
135 function take($newown)
137 $this->stat = 'take'; // Card stat
138 $this->owner = $newown;
142 class Table_briskin5 extends Table {
143 var $card; // il mazzo di carte
144 var $mazzo; // chi e' di mazzo
154 var $points; // points array
155 var $points_n; // number of row of points
169 function Table_briskin5()
175 function &create($idx)
179 if (($thiz =& new Table_briskin5()) == FALSE)
185 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
186 $thiz->asta_pla_n= -1;
187 $thiz->asta_card = -1;
188 $thiz->asta_pnt = -1;
191 $thiz->points = array( );
193 $thiz->total = array( 0, 0, 0, 0, 0);
194 $thiz->asta_win = -1;
195 $thiz->briscola = -1;
199 $thiz->old_reason = "";
200 $thiz->old_asta_pnt = -1;
201 $thiz->old_mult = -1;
204 $thiz->old_friend = -1;
211 function &myclone(&$from)
215 if (($thiz =& new Table_briskin5()) == FALSE)
220 $thiz->card = $from->card;
221 $thiz->mazzo = $from->mazzo; // REVIEW
222 $thiz->gstart = $from->gstart;
223 $thiz->turn = $from->turn;
225 $thiz->asta_pla = $from->asta_pla;
226 $thiz->asta_pla_n = $from->asta_pla_n;
227 $thiz->asta_card = $from->asta_card;
228 $thiz->asta_pnt = $from->asta_pnt;
230 $thiz->mult = $from->mult;
231 $thiz->points = $from->points;
232 $thiz->points_n = $from->points_n;
233 $thiz->total = $from->total;
235 $thiz->asta_win = $from->asta_win;
236 $thiz->briscola = $from->briscola;
237 $thiz->friend = $from->friend;
239 $thiz->old_reason = $from->old_reason;
240 $thiz->old_asta_pnt = $from->old_asta_pnt;
241 $thiz->old_mult = $from->mult;
242 $thiz->old_pnt = $from->old_pnt;
243 $thiz->old_win = $from->old_win;
244 $thiz->old_friend = $from->old_friend;
250 function parentcopy(&$from)
255 function &spawn(&$from)
259 if (($thiz =& new Table_briskin5()) == FALSE)
262 $thiz->parentcopy($from);
264 log_main("PLAYER_N - spawn.".$thiz->player_n);
266 $thiz->card = &$thiz->bunch_create();
267 $thiz->mazzo = rand(0,PLAYERS_N-1);
271 $thiz->old_reason = "";
273 // players are rearranged in an dedicated array
274 $thiz->player = array();
275 for ($i = 0 ; $i < $from->player_n ; $i++)
276 $thiz->player[$i] = $i;
278 log_main("TABLE_OLD_WIN - spawn:".$thiz->old_win);
284 function &bunch_create()
288 for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
289 // for ($i = 0 ; $i < 40 ; $i++) {
290 $ret[$i] =& new Card($i, 'bunch', 'no_owner');
297 function bunch_make()
299 log_main("bunch_make start");
300 $ct = array(0,0,0,0,0);
302 mt_srand(make_seed());
304 for ($i = (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) - 1 ; $i >= 0 ; $i--)
307 for ($i = (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) - 1 ; $i >= 0 ; $i--) {
311 log_main("RND ZERO");
315 $owner = $i % BRISKIN5_PLAYERS_N;
316 $this->card[$id]->assign('hand', $owner);
318 $rest[$rn] = $rest[$i];
319 // $pubbpos[$rn2] = $pubbpos[$i];
321 log_main("bunch_make end");
324 function init(&$userarr)
327 $this->mazzo = rand(0,PLAYERS_N-1);
331 $this->old_reason = "";
333 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
334 $this->total[$i] = 0;
335 $user_cur = &$userarr[$this->player[$i]];
336 $user_cur->exitislock = TRUE;
339 log_main("table::init: ci siamo");
342 function game_init(&$userarr)
346 $this->gstart = ($this->mazzo+1) % BRISKIN5_PLAYERS_N;
350 $this->asta_pla_n = BRISKIN5_PLAYERS_N;
351 $this->asta_card = -1;
352 $this->asta_pnt = 60;
353 $this->asta_win = -1;
354 $this->briscola = -1;
358 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
359 $this->asta_pla[$i] = TRUE;
360 $user_cur = &$userarr[$this->player[$i]];
361 $user_cur->subst = 'asta';
362 $user_cur->asta_card = -2;
363 $user_cur->asta_pnt = -1;
364 $user_cur->handpt = $this->hand_points($i);
365 // SEE function calculate_points(&$table)
372 $this->mazzo = ($this->mazzo + 1) % BRISKIN5_PLAYERS_N;
376 function hand_points($idx)
378 GLOBAL $G_all_points;
382 for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
383 // for ($i = 0 ; $i < 40 ; $i++) {
384 if ($this->card[$i]->owner != $idx)
387 $ctt = $this->card[$i]->value % 10;
388 $tot += $G_all_points[$ctt];
395 function exitlock_show(&$userarr, $table_pos)
397 $ct = $this->exitlock_calc(&$userarr, $table_pos);
399 $ret = sprintf('exitlock_show(%d, %s);', $ct,
400 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
404 function exitlock_calc(&$userarr, $table_pos)
408 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
409 if ($userarr[$this->player[$i]]->exitislock == FALSE)
415 } // end class Table_briskin5
420 define(BIN5_USER_FLAG_RING_ENDAUCT, 0x01);
422 class User_briskin5 extends User {
425 var $handpt; // Total card points at the beginning of the current hand.
426 var $exitislock; // Player can exit from the table ?
427 var $privflags; // Flags for briskin5 only
433 function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
436 if (($thiz =& new User()) == FALSE)
439 $thiz->asta_card = -2;
440 $thiz->asta_pnt = -1;
442 $thiz->exitislock = TRUE;
443 $thiz->privflags = 0;
449 function parentcopy(&$from)
454 function copy(&$from)
456 $this->parentcopy($from);
458 $this->asta_card = $from->asta_card;
459 $this->asta_pnt = $from->asta_pnt;
460 $this->handpt = $from->handpt;
461 $this->exitislock = $from->exitislock;
462 $this->privflags = $from->privflags;
466 function &myclone(&$from)
470 if (($thiz =& new User()) == FALSE)
479 function &spawn(&$from, $table, $table_pos)
481 GLOBAL $G_false, $CO_bin5_pref_ring_endauct;
483 if (($thiz =& new User_briskin5()) == FALSE)
486 $thiz->parentcopy($from);
488 $thiz->asta_card = -2;
489 $thiz->asta_pnt = -1;
491 $thiz->exitislock = TRUE;
493 log_wr("Briskin5 constructor");
495 $this->privflags = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
497 $thiz->table_orig = $table;
499 $thiz->table_pos = $table_pos;
503 } // end class User_briskin5
510 var $comm; // commands for many people
511 var $step; // current step of the comm array
512 var $garbage_timeout;
521 function Briskin5 (&$room, $table_idx, $table_token) {
522 $this->user = array();
523 $this->table = array();
525 $this->the_end = FALSE;
526 $this->shm_sz = BRISKIN5_SHM_MIN;
527 if (($this->tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
532 $user =& $room->user;
533 $table =& $room->table[$table_idx];
535 log_wr("Briskin5 constructor");
537 for ($i = 0 ; $i < $table->player_n ; $i++) {
538 $user[$table->player[$i]]->table_token = $table_token;
539 $this->user[$i] =& User_briskin5::spawn(&$user[$table->player[$i]], $table_idx, $i);
541 $this->table[0] =& Table_briskin5::spawn(&$table);
543 log_main("TABLE_OLD_WIN - Briskin5:".$this->table[0]->old_win);
545 $this->table_idx = $table_idx;
546 $this->table_token = $table_token;
547 $this->garbage_timeout = 0;
549 log_wr("Briskin5 constructor end");
553 function &get_user($sess, &$idx)
555 GLOBAL $PHP_SELF, $G_false;
557 if (validate_sess($sess)) {
558 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
559 if (strcmp($sess, $this->user[$i]->sess) == 0) {
562 $ret = &$this->user[$i];
566 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
567 // for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++)
568 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
571 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
578 function garbage_manager($force)
581 /* Garbage collector degli utenti in timeout */
584 if ($force || $this->garbage_timeout < $curtime) {
586 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
587 $user_cur = &$this->user[$i];
588 if ($user_cur->sess == "" ||
589 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
592 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
593 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
595 if ($user_cur->stat == 'table') {
596 log_auth($user_cur->sess," bin5 Autologout session.");
598 /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE
600 $tmp_sess = $user_cur->sess;
601 $user_cur->sess = "";
602 step_unproxy($tmp_sess);
603 $user_cur->name = "";
604 $user_cur->the_end = FALSE;
608 /* se gli altri utenti non erano d'accordo questo utente viene bannato */
609 $remcalc = $this->table[0]->exitlock_calc(&$this->user, $user_cur->table_pos);
611 require_once("/var/www/webspace/trusty/Obj/hardban.phh");
612 Hardbans::add(($user_cur->flags & USER_FLAG_AUTH ? $user_cur->name : FALSE),
613 $user_cur->ip, $user_cur->sess, $user_cur->laccwr + BAN_TIME);
615 // $user->bantime = $user->laccwr + BAN_TIME;
617 $this->table_wakeup(&$user_cur);
621 log_rd2($user_cur->sess." GARBAGE UPDATED!");
623 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
636 function &load_data($table_idx, $table_token = "")
638 GLOBAL $G_false, $sess;
642 log_wr("TABLE_IDX ".FTOK_PATH."/table".$table_idx);
645 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
646 log_main("ftok failed");
651 if (($shm_sz = sharedmem_sz($tok)) == -1) {
652 log_main("shmop_open failed");
656 if (($shm = shm_attach($tok, $shm_sz)) == FALSE)
659 if (($bri = @shm_get_var($shm, $tok)) == FALSE)
662 if ($table_token != "" && $bri->table_token != $table_token) {
663 log_wr("bri->table_token: ".$bri->table_token."table_token: ".$table_token);
678 log_wr("briskin5 load_data failed");
687 function save_data(&$bri)
694 log_main("SAVE BRISKIN5 DATA");
696 if (!isset($bri->tok))
699 while ($bri->shm_sz < BRISKIN5_SHM_MAX) {
700 if (($shm = shm_attach($bri->tok, $bri->shm_sz)) == FALSE)
703 if (@shm_put_var($shm, $bri->tok, $bri) != FALSE) {
707 if (shm_remove($shm) === FALSE) {
708 log_only("REMOVE FALLITA");
712 $bri->shm_sz += BRISKIN5_SHM_DLT;
715 log_crit("save data failed!");
725 function destroy_data($table_idx)
731 log_main("DESTROY BRISKIN5 DATA");
734 log_main("DESTROY2 BRISKIN5 DATA");
736 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1)
739 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
742 if (shmop_delete($shm) == 0) {
743 log_only("REMOVE FALLITA ");
748 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
750 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
760 function lock_data($table_idx)
764 log_lock("LOCK_DATA ".FTOK_PATH."/table".$table_idx);
765 // echo "LOCK: ".FTOK_PATH."/main";
767 // WARNING monitor this step
768 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
771 // WARNING monitor this step
772 if (($res = @sem_get($tok)) == FALSE) {
775 if (sem_acquire($res)) {
776 log_lock("LOCK table ".$table_idx."[res: ".$res."]");
780 log_lock("LOCK table ".$table_idx.":FAILED");
785 function unlock_data($res)
789 log_lock("UNLOCK table [res: ".$res."]");
791 return (sem_release($res));
795 function chatt_send(&$user, $mesg)
797 GLOBAL $mlang_brisk, $G_lang;
799 if ($user->stat == 'table') {
800 $table = &$this->table[$user->table];
803 $user_mesg = substr($mesg,6);
807 $dt = date("H:i ", $curtime);
808 if (strncmp($user_mesg, "/nick ", 6) == 0) {
809 log_main($user->sess." chatt_send BEGIN");
811 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
812 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
813 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
818 $user_mesg = "COMMAND ".$user_mesg;
821 // update local graph
822 // update remote graphs
823 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
824 $user_cur = &$this->user[$i];
825 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
826 if ($user_cur->sess == '')
828 if ($user_cur->name == $name_new) {
829 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
831 $premsg = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
832 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $premsg);
837 if ($i == BRISKIN5_MAX_PLAYERS) {
838 if ($user->flags & USER_FLAG_AUTH && strcasecmp($user->name,$name_new) != 0) {
839 if ($this->table[$user->table]->auth_only == TRUE) {
840 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
841 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>%s</b>");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
846 $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
850 $user->name = $name_new;
852 log_main($user->sess." chatt_send start set");
855 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
856 log_main($user->sess." chatt_send set loop");
858 $user_cur = &$this->user[$i];
859 if ($user_cur->sess == '')
862 if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
863 $table = &$this->table[$user->table];
865 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
866 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
867 $this->user[$table->player[($user_cur->table_pos) % BRISKIN5_PLAYERS_N]]->flags,
868 xcape($this->user[$table->player[($user_cur->table_pos) % BRISKIN5_PLAYERS_N]]->name),
870 $this->user[$table->player[($user_cur->table_pos+1) % BRISKIN5_PLAYERS_N]]->flags,
871 xcape($this->user[$table->player[($user_cur->table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
873 $this->user[$table->player[($user_cur->table_pos+2) % BRISKIN5_PLAYERS_N]]->flags,
874 xcape($this->user[$table->player[($user_cur->table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
876 (BRISKIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+3) % BRISKIN5_PLAYERS_N]]->flags),
877 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
879 (BRISKIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+4) % BRISKIN5_PLAYERS_N]]->flags),
880 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
881 if ($user_cur == $user) {
882 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
883 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
884 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";',
885 $itin, xcape($user->name,ENT_COMPAT,"UTF-8"), $itou);
887 $user_cur->step_inc();
893 for ($i = 0 ; $i < ($user->stat == 'room' ? BRISKIN5_MAX_PLAYERS : BRISKIN5_PLAYERS_N) ; $i++) {
894 if ($user->stat == 'room') {
895 $user_cur = &$this->user[$i];
896 if ($user_cur->sess == '' || $user_cur->stat != 'room')
900 $user_cur = &$this->user[$table->player[$i]];
903 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
904 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
905 $dt, $user->flags, xcape($user->name), xcape($user_mesg));
906 $user_cur->step_inc();
908 log_legal($curtime, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
912 function table_wakeup(&$user)
914 $table = &$this->table[0];
916 log_main("BRISKIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
920 log_main("BRISKIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
922 for ($i = 0 ; $i < $table->player_n ; $i++) {
923 $user_cur = &$this->user[$i];
924 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
926 if ($user_cur == $user)
927 $user_cur->subst = "shutdowner";
929 $user_cur->subst = "shutdowned";
930 $user_cur->laccwr = $curtime;
932 $ret = "gst.st = ".($user_cur->step+1)."; ";
933 $ret .= 'gst.st_loc++; the_end=true; window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
935 log_wr($user_cur->sess." BRISKIN5_WAKEUP: ".$ret);
936 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
937 $user_cur->step_inc();
940 $this->the_end = TRUE;
944 * If all players are freezed the room garbage_manager clean up table and users.
946 function is_abandoned()
951 $table = &$this->table[0];
953 for ($i = 0 ; $i < $table->player_n ; $i++) {
954 $user_cur = &$this->user[$i];
956 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
964 } // end class Briskin5
966 function locshm_exists($tok)
970 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
971 log_main($tok." SHM NOT exists");
977 log_main($tok." SHM exists");
984 function asta2mult($asta_pnt)
988 else if ($asta_pnt > 100)
990 else if ($asta_pnt > 90)
992 else if ($asta_pnt > 80)
994 else if ($asta_pnt > 70)
1000 // rendiamo qui l'elenco dei punti come return della func
1001 function calculate_points(&$table)
1003 GLOBAL $G_all_points;
1008 if ($table->asta_pnt == 60)
1009 $table->asta_pnt = 61;
1011 $table->old_reason = "";
1012 $table->old_win = $table->asta_win;
1013 $table->old_friend = $table->friend;
1014 $table->old_asta_pnt = $table->asta_pnt;
1015 $table->old_mult = $table->mult;
1017 for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1018 // for ($i = 0 ; $i < 40 ; $i++) {
1019 $ctt = $table->card[$i]->value % 10;
1020 $own = $table->card[$i]->owner;
1021 if ($own == $table->asta_win || $own == $table->friend)
1022 $pro += $G_all_points[$ctt];
1025 log_wr(sprintf("PRO: [%d]", $pro));
1028 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1029 $table->points[$table->points_n % MAX_POINTS] = array();
1030 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1031 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1035 $table->old_pnt = $pro;
1041 if ($pro >= $table->asta_pnt)
1046 $gamult = asta2mult($table->asta_pnt);
1048 $table->points[$table->points_n % MAX_POINTS] = array();
1049 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1050 if ($i == $table->asta_win)
1051 $pt = ($i == $table->friend ? 4 : 2);
1052 else if ($i == $table->friend)
1059 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1061 $pt = $pt * $sig * ($gamult + $table->mult) * ($pro == 120 ? 2 : 1);
1063 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1065 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1066 $table->total[$i] += $pt;
1070 $table->old_pnt = $pro;
1077 is_transition (is from room to table ?)
1078 is_again (is another game)
1080 Examples of $is_transition, $is_again:
1081 from reload of the page: FALSE, FALSE
1082 from sitdown in room: TRUE, FALSE
1083 from table: asta cmd e tutti passano: TRUE, TRUE
1084 from table: fine partita: TRUE, TRUE
1086 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
1088 $table_idx = $user->table;
1089 $table = &$room->table[$table_idx];
1090 $table_pos = $user->table_pos;
1092 $ret = "table_init();";
1093 $ret .= $table->exitlock_show(&$room->user, $table_pos);
1095 /* GENERAL STATUS */
1096 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1097 $sendstep, $user->stat, $user->subst, $table_pos);
1099 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1102 $ret .= "background_set();";
1105 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
1106 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
1108 $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', $itin, xcape($user->name), $itou);
1109 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1110 $room->user[$table->player[($table_pos) % BRISKIN5_PLAYERS_N]]->flags,
1111 xcape($room->user[$table->player[($table_pos) % BRISKIN5_PLAYERS_N]]->name),
1113 $room->user[$table->player[($table_pos+1) % BRISKIN5_PLAYERS_N]]->flags,
1114 xcape($room->user[$table->player[($table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
1116 $room->user[$table->player[($table_pos+2) % BRISKIN5_PLAYERS_N]]->flags,
1117 xcape($room->user[$table->player[($table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
1119 (BRISKIN5_PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+3) % BRISKIN5_PLAYERS_N]]->flags),
1120 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
1122 (BRISKIN5_PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+4) % BRISKIN5_PLAYERS_N]]->flags),
1123 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
1125 /* NOTIFY FOR THE CARD MAKER */
1126 if ($is_transition) { // && $user->subst == "asta" superfluo
1127 $ret .= show_table_info(&$room, &$table, $table_pos);
1130 $ret .= table_wellcome($user);
1132 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1133 $ret .= playsound("cow.mp3");
1138 if ($is_transition) { // && $user->subst == "asta" superfluo
1141 for ($i = 0 ; $i < 8 ; $i++) {
1142 for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++) {
1144 for ($o = 0 ; $o < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) && $ct < $i+1 ; $o++) {
1145 // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1146 if ($table->card[$o]->owner == (($e + $table->gstart) % BRISKIN5_PLAYERS_N)) {
1152 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1154 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BRISKIN5_PLAYERS_N,
1155 $i, ((($e + BRISKIN5_PLAYERS_N - $table_pos + $table->gstart) % BRISKIN5_PLAYERS_N) == 0 ?
1156 $table->card[$o]->value : -1),
1157 ($i == 7 && $e == (BRISKIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1162 $taked = array(0,0,0,0,0);
1163 $inhand = array(0,0,0,0,0);
1164 $ontabl = array(-1,-1,-1,-1,-1);
1167 for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1168 // for ($i = 0 ; $i < 40 ; $i++) {
1169 if ($table->card[$i]->stat == 'hand') {
1170 if ($table->card[$i]->owner == $table_pos) {
1171 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1173 $inhand[$table->card[$i]->owner]++;
1175 else if ($table->card[$i]->stat == 'take') {
1176 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1177 $taked[$table->card[$i]->owner]++;
1179 else if ($table->card[$i]->stat == 'table') {
1180 $ontabl[$table->card[$i]->owner] = $i;
1184 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1185 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1187 log_main("Stat table: ".$logg);
1189 /* Set ours cards. */
1191 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1192 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1193 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
1194 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1195 $ret .= sprintf('card_setours(%s);', $oursarg);
1197 /* Dispose all cards */
1198 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1199 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1200 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1201 ($inhand[$i] <= 8 ? $inhand[$i] : 8) , $taked[$i]);
1203 if ($ontabl[$i] != -1) {
1204 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1205 $table->card[$ontabl[$i]]->value,
1206 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1212 if ($user->subst == 'asta') {
1214 /* show users auction status */
1216 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1217 $user_cur = &$room->user[$table->player[$i]];
1218 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1219 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1221 if (BRISKIN5_PLAYERS_N == 3)
1222 $showst .= ",-2,-2";
1223 $ret .= sprintf('document.title = "Brisk - Tavolo %d (asta)";', $user->table_orig);
1224 $ret .= sprintf('show_astat(%s);', $showst);
1226 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1227 /* show card chooser */
1228 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1233 if ($table_pos == ($table->gstart % BRISKIN5_PLAYERS_N) &&
1234 $table->asta_win == -1)
1235 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1236 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1238 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1239 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1243 if ($table->asta_win == -1) { // auction case
1244 if ($table_pos == ($table->gstart % BRISKIN5_PLAYERS_N))
1245 $ret .= "remark_on();";
1247 $ret .= "remark_off();";
1249 else { // chooseed case
1250 if ($table_pos == $table->asta_win)
1251 $ret .= "remark_on();";
1253 $ret .= "remark_off();";
1256 else if ($user->subst == 'game') {
1258 if (($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N == $table_pos)
1259 $ret .= "is_my_time = true; remark_on();";
1261 $ret .= "remark_off();";
1263 /* WHO CALL AND WHAT */
1264 $ret .= briscola_show($room, $table, $user);
1268 } // end function show_table(...
1270 function calculate_winner(&$table)
1277 $cur_seed = $table->briscola - ($table->briscola % 10);
1279 for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1280 // for ($i = 0 ; $i < 40 ; $i++) {
1281 if ($table->card[$i]->stat != "table")
1284 log_wr(sprintf("Card On table: [%d]", $i));
1286 $v = $table->card[$i]->value;
1287 $ontab[$table->card[$i]->owner] = $v;
1288 $ontid[$table->card[$i]->owner] = $i;
1289 /* se briscola setto il flag */
1290 if (($v - ($v % 10)) == $cur_seed)
1294 if ($briontab == FALSE) {
1295 $cur_win = $table->gstart;
1296 $cur_val = $ontab[$cur_win];
1297 $cur_seed = $cur_val - ($cur_val % 10);
1300 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1301 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1302 if ($ontab[$i] < $cur_val) {
1303 $cur_val = $ontab[$i];
1309 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1310 $table->card[$ontid[$i]]->owner = $cur_win;
1311 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1316 function show_table_info(&$room, &$table, $table_pos)
1318 GLOBAL $G_lang, $mlang_bin5_bin5;
1321 $user = &$room->user[$table->player[$table_pos]];
1323 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1324 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1327 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++)
1328 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
1329 $noty .= sprintf("</tr>");
1332 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1334 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1335 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1336 for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++)
1337 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1342 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1343 for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++)
1344 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1345 $noty .= "</tr></table>";
1347 if ($table->old_reason != "") {
1348 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1351 if ($table->old_win != -1) {
1352 log_main("TABLE_OLD_WIN:".$table->old_win);
1353 $win = $table->player[$table->old_win];
1354 log_main("TABLE_OLD_FRIEND:".$table->old_friend);
1355 $fri = $table->player[$table->old_friend];
1357 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1360 /* MLANG: "<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>", "hanno fatto <b>cappotto</b> EBBRAVI!.<hr>", "dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>", "<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>", "ha fatto <b>cappotto</b> EBBRAVO!.<hr>", "doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>", ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt : 'più di 60'), $table->old_pnt, ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso" */
1362 $noty .= sprintf($mlang_bin5_bin5['info_part'][$G_lang],
1363 xcape($room->user[$win]->name),
1364 xcape($room->user[$fri]->name));
1365 if ($table->old_pnt == 120) {
1366 $noty .= sprintf($mlang_bin5_bin5['info_capp'][$G_lang]);
1369 $noty .= sprintf($mlang_bin5_bin5['info_pnt'][$G_lang],
1370 ($table->old_asta_pnt > 61 ? $mlang_bin5_bin5['info_alea'][$G_lang].$table->old_asta_pnt :
1371 $mlang_bin5_bin5['info_more'][$G_lang]), $table->old_pnt,
1372 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] : ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] : $mlang_bin5_bin5['info_lost'][$G_lang])));
1376 $noty .= sprintf($mlang_bin5_bin5['info_alon'][$G_lang],
1377 xcape($room->user[$win]->name));
1378 if ($table->old_pnt == 120) {
1379 $noty .= sprintf($mlang_bin5_bin5['info_acap'][$G_lang]);
1382 $noty .= sprintf($mlang_bin5_bin5['info_apnt'][$G_lang],
1384 ($table->old_asta_pnt > 61 ? $mlang_bin5_bin5['info_alea'][$G_lang].$table->old_asta_pnt :
1385 $mlang_bin5_bin5['info_more'][$G_lang]), $table->old_pnt,
1387 ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] : ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] : $mlang_bin5_bin5['info_lost'][$G_lang])));
1390 if (($table->old_mult + asta2mult($table->old_asta_pnt)) > 1) {
1391 $noty .= sprintf($mlang_bin5_bin5['info_omul'][$G_lang], multoval($table->old_mult + asta2mult($table->old_asta_pnt)));
1393 $noty .= "<hr><br>";
1395 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1396 if ($table->mazzo == $table_pos)
1397 $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
1399 $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
1400 $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
1403 if ($user->subst == 'asta') {
1404 if ($table->asta_win == -1) // auction case
1405 $curplayer = $table->gstart % BRISKIN5_PLAYERS_N;
1407 $curplayer = $table->asta_win;
1409 else if ($user->subst == 'game') {
1410 $curplayer = ($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N;
1413 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1414 if ($curplayer == $table_pos) {
1415 $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
1418 $unam = xcape($room->user[$table->player[$curplayer]]->name);
1419 $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
1422 if (($table->mult + asta2mult($table->asta_pnt)) > 1) {
1423 $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($table->mult + asta2mult($table->asta_pnt)));
1425 $noty .= "<hr><br>";
1426 $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
1427 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
1428 * 130 + ($table->points_n > 0 ? 50 : 0) +
1429 * (120 * ($table->points_n / MAX_POINTS)));
1430 * will be used when we refact notify js function following
1431 * photoo class logic
1437 function table_wellcome($user)
1439 GLOBAL $table_wellarr, $G_lang;
1442 for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
1443 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
1449 function briscola_show($room, $table, $user)
1451 GLOBAL $G_lang, $mlang_bin5_bin5;
1455 if ($table->asta_card == 9)
1456 $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
1458 /* text of caller cell */
1459 if ($user->table_pos == $table->asta_win) {
1460 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
1461 $ret .= sprintf($prestr, $ptnadd);
1464 $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
1465 $ret .= sprintf($prestr,
1466 xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
1468 $ret .= sprintf('set_iscalling(%d);', ($table->asta_win - $user->table_pos + BRISKIN5_PLAYERS_N) % BRISKIN5_PLAYERS_N);
1470 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1472 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1473 $ret .= sprintf('$("caller").style.visibility = "visible";');
1474 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1475 $ret .= sprintf('$("astalascio").style.visibility = "";');
1476 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1477 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1483 function game_result($asta_pnt, $pnt)
1485 if ($asta_pnt == 61) {
1488 else if ($pnt == 60)
1494 if ($pnt >= $asta_pnt)