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 $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.');
36 var $value; /* 0 - 39 card value */
37 var $stat; /* 'bunch', 'hand', 'table', 'take' */
38 var $owner; /* (table position 0-4) */
39 // var $pos; /* Pos in hand. */
40 var $x; /* When played the X position on the table of the owner. */
41 var $y; /* When played the Y position on the table of the owner. */
43 function Card($value, $stat, $owner)
45 $this->value = $value;
46 $this->stat = $stat; // Card stat
47 $this->owner = $owner;
50 function assign($stat,$owner)
52 $this->stat = $stat; // Card stat
53 $this->owner = $owner;
63 $this->stat = 'table'; // Card stat
68 function take($newown)
70 $this->stat = 'take'; // Card stat
71 $this->owner = $newown;
75 class Table_briskin5 extends Table {
76 var $card; // il mazzo di carte
77 var $mazzo; // chi e' di mazzo
87 var $points; // points array
88 var $points_n; // number of row of points
101 function Table_briskin5()
107 function &create($idx)
111 if (($thiz =& new Table_briskin5()) == FALSE)
117 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
118 $thiz->asta_pla_n= -1;
119 $thiz->asta_card = -1;
120 $thiz->asta_pnt = -1;
123 $thiz->points = array( );
125 $thiz->total = array( 0, 0, 0, 0, 0);
126 $thiz->asta_win = -1;
127 $thiz->briscola = -1;
131 $thiz->old_reason = "";
132 $thiz->old_asta_pnt = -1;
135 $thiz->old_friend = -1;
142 function &clone(&$from)
146 if (($thiz =& new Table_briskin5()) == FALSE)
151 $thiz->card = $from->card;
152 $thiz->mazzo = $from->mazzo; // REVIEW
153 $thiz->gstart = $from->gstart;
154 $thiz->turn = $from->turn;
156 $thiz->asta_pla = $from->asta_pla;
157 $thiz->asta_pla_n = $from->asta_pla_n;
158 $thiz->asta_card = $from->asta_card;
159 $thiz->asta_pnt = $from->asta_pnt;
161 $thiz->mult = $from->mult;
162 $thiz->points = $from->points;
163 $thiz->points_n = $from->points_n;
164 $thiz->total = $from->total;
166 $thiz->asta_win = $from->asta_win;
167 $thiz->briscola = $from->briscola;
168 $thiz->friend = $from->friend;
170 $thiz->old_reason = $from->old_reason;
171 $thiz->old_asta_pnt = $from->old_asta_pnt;
172 $thiz->old_pnt = $from->old_pnt;
173 $thiz->old_win = $from->old_win;
174 $thiz->old_friend = $from->old_friend;
180 function parentcopy(&$from)
185 function &spawn(&$from)
189 if (($thiz =& new Table_briskin5()) == FALSE)
192 $thiz->parentcopy($from);
194 log_main("PLAYER_N - spawn.".$thiz->player_n);
196 $thiz->card = &$thiz->bunch_create();
197 $thiz->mazzo = rand(0,PLAYERS_N-1);
201 $thiz->old_reason = "";
203 // players are rearranged in an dedicated array
204 $thiz->player = array();
205 for ($i = 0 ; $i < $from->player_n ; $i++)
206 $thiz->player[$i] = $i;
208 log_main("TABLE_OLD_WIN - spawn:".$thiz->old_win);
214 function &bunch_create()
218 for ($i = 0 ; $i < 40 ; $i++) {
219 $ret[$i] =& new Card($i, 'bunch', 'no_owner');
226 function bunch_make()
228 $ct = array(0,0,0,0,0);
230 mt_srand(make_seed());
232 for ($i = 39 ; $i >= 0 ; $i--)
235 for ($i = 39 ; $i >= 0 ; $i--) {
239 log_main("RND ZERO");
243 $owner = $i % PLAYERS_N;
244 $this->card[$id]->assign('hand', $owner);
246 $rest[$rn] = $rest[$i];
247 // $pubbpos[$rn2] = $pubbpos[$i];
251 function init(&$userarr)
254 $this->mazzo = rand(0,PLAYERS_N-1);
258 $this->old_reason = "";
260 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
261 $this->total[$i] = 0;
262 $user_cur = &$userarr[$this->player[$i]];
263 $user_cur->exitislock = TRUE;
266 log_main("table::init: ci siamo");
269 function game_init(&$userarr)
273 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
277 $this->asta_pla_n = PLAYERS_N;
278 $this->asta_card = -1;
279 $this->asta_pnt = 60;
280 $this->asta_win = -1;
281 $this->briscola = -1;
285 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
286 $this->asta_pla[$i] = TRUE;
287 $user_cur = &$userarr[$this->player[$i]];
288 $user_cur->subst = 'asta';
289 $user_cur->asta_card = -2;
290 $user_cur->asta_pnt = -1;
291 $user_cur->handpt = $this->hand_points($i);
292 // SEE function calculate_points(&$table)
298 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
302 function hand_points($idx)
304 GLOBAL $G_all_points;
308 for ($i = 0 ; $i < 40 ; $i++) {
309 if ($this->card[$i]->owner != $idx)
312 $ctt = $this->card[$i]->value % 10;
313 $tot += $G_all_points[$ctt];
320 function exitlock_show(&$userarr, $table_pos)
322 $ct = $this->exitlock_calc(&$userarr, $table_pos);
324 $ret = sprintf('exitlock_show(%d, %s);', $ct,
325 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
329 function exitlock_calc(&$userarr, $table_pos)
333 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
334 if ($userarr[$this->player[$i]]->exitislock == FALSE)
340 } // end class Table_briskin5
347 class User_briskin5 extends User {
350 var $handpt; // Total card points at the beginning of the current hand.
351 var $exitislock; // Player can exit from the table ?
357 function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
360 if (($thiz =& new User()) == FALSE)
363 $thiz->asta_card = -2;
364 $thiz->asta_pnt = -1;
366 $thiz->exitislock = TRUE;
372 function parentcopy(&$from)
377 function copy(&$from)
379 $this->parentcopy($from);
381 $this->asta_card = $from->asta_card;
382 $this->asta_pnt = $from->asta_pnt;
383 $this->handpt = $from->handpt;
384 $this->exitislock = $from->exitislock;
388 function &clone(&$from)
392 if (($thiz =& new User()) == FALSE)
401 function &spawn(&$from, $table, $table_pos)
405 if (($thiz =& new User_briskin5()) == FALSE)
408 $thiz->parentcopy($from);
410 $thiz->asta_card = -2;
411 $thiz->asta_pnt = -1;
413 $thiz->exitislock = TRUE;
415 $thiz->table_orig = $table;
417 $thiz->table_pos = $table_pos;
421 } // end class User_briskin5
428 var $comm; // commands for many people
429 var $step; // current step of the comm array
430 var $garbage_timeout;
439 function Briskin5 (&$room, $table_idx, $table_token) {
440 $this->user = array();
441 $this->table = array();
443 $this->the_end = FALSE;
444 $this->shm_sz = BRISKIN5_SHM_MIN;
445 if (($this->tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
450 $user =& $room->user;
451 $table =& $room->table[$table_idx];
453 log_wr("Briskin5 constructor");
455 for ($i = 0 ; $i < $table->player_n ; $i++) {
456 $user[$table->player[$i]]->table_token = $table_token;
457 $this->user[$i] =& User_briskin5::spawn(&$user[$table->player[$i]], $table_idx, $i);
459 $this->table[0] =& Table_briskin5::spawn(&$table);
461 log_main("TABLE_OLD_WIN - Briskin5:".$this->table[0]->old_win);
463 $this->table_idx = $table_idx;
464 $this->table_token = $table_token;
465 $this->garbage_timeout = 0;
467 log_wr("Briskin5 constructor end");
471 function &get_user($sess, &$idx)
473 GLOBAL $PHP_SELF, $G_false;
475 if (validate_sess($sess)) {
476 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
477 if (strcmp($sess, $this->user[$i]->sess) == 0) {
480 $ret = &$this->user[$i];
484 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
485 // for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++)
486 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
489 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
496 function garbage_manager($force)
499 /* Garbage collector degli utenti in timeout */
502 if ($force || $this->garbage_timeout < $curtime) {
504 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
505 $user_cur = &$this->user[$i];
506 if ($user_cur->sess == "" ||
507 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
510 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
511 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
513 if ($user_cur->stat == 'table') {
514 log_auth($user_cur->sess," bin5 Autologout session.");
516 /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE
518 $tmp_sess = $user_cur->sess;
519 $user_cur->sess = "";
520 step_unproxy($tmp_sess);
521 $user_cur->name = "";
522 $user_cur->the_end = FALSE;
526 $this->table_wakeup(&$user_cur);
530 log_rd2($user_cur->sess." GARBAGE UPDATED!");
532 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
545 function &load_data($table_idx, $table_token = "")
547 GLOBAL $G_false, $sess;
551 log_wr("TABLE_IDX ".FTOK_PATH."/table".$table_idx);
554 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
555 log_main("ftok failed");
560 if (($shm_sz = sharedmem_sz($tok)) == -1) {
561 log_main("shmop_open failed");
565 if (($shm = shm_attach($tok, $shm_sz)) == FALSE)
568 if (($bri = @shm_get_var($shm, $tok)) == FALSE)
571 if ($table_token != "" && $bri->table_token != $table_token) {
572 log_wr("bri->table_token: ".$bri->table_token."table_token: ".$table_token);
587 log_wr("briskin5 load_data failed");
596 function save_data(&$bri)
603 log_main("SAVE BRISKIN5 DATA");
605 if (!isset($bri->tok))
608 while ($bri->shm_sz < BRISKIN5_SHM_MAX) {
609 if (($shm = shm_attach($bri->tok, $bri->shm_sz)) == FALSE)
612 if (@shm_put_var($shm, $bri->tok, $bri) != FALSE) {
616 if (shm_remove($shm) === FALSE) {
617 log_only("REMOVE FALLITA");
621 $bri->shm_sz += BRISKIN5_SHM_DLT;
624 log_crit("save data failed!");
634 function destroy_data($table_idx)
640 log_main("DESTROY BRISKIN5 DATA");
643 log_main("DESTROY2 BRISKIN5 DATA");
645 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1)
648 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
651 if (shmop_delete($shm) == 0) {
652 log_only("REMOVE FALLITA ");
657 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
659 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
669 function lock_data($table_idx)
673 log_lock("LOCK_DATA ".FTOK_PATH."/table".$table_idx);
674 // echo "LOCK: ".FTOK_PATH."/main";
676 // WARNING monitor this step
677 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
680 // WARNING monitor this step
681 if (($res = @sem_get($tok)) == FALSE) {
684 if (sem_acquire($res)) {
685 log_lock("LOCK table ".$table_idx."[res: ".$res."]");
689 log_lock("LOCK table ".$table_idx.":FAILED");
694 function unlock_data($res)
698 log_lock("UNLOCK table [res: ".$res."]");
700 return (sem_release($res));
704 function chatt_send(&$user, $mesg)
706 if ($user->stat == 'table') {
707 $table = &$this->table[$user->table];
710 $user_mesg = substr($mesg,6);
714 $dt = date("H:i ", $curtime);
715 if (strncmp($user_mesg, "/nick ", 6) == 0) {
716 log_main($user->sess." chatt_send BEGIN");
718 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
719 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
720 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"Il nickname deve contenere almeno una lettera dell\'alfabeto o una cifra.");', $dt, NICKSERV);
725 $user_mesg = "COMMAND ".$user_mesg;
728 // update local graph
729 // update remote graphs
730 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
731 $user_cur = &$this->user[$i];
732 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
733 if ($user_cur->sess == '')
735 if ($user_cur->name == $name_new) {
736 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
737 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"Nickname <b>%s</b> già in uso.%d");', $dt, NICKSERV, xcape($name_new), $this->table[$user->table]->auth_only == TRUE);
742 if ($i == BRISKIN5_MAX_PLAYERS) {
743 if ($user->flags & USER_FLAG_AUTH && strcasecmp($user->name,$name_new) != 0) {
744 if ($this->table[$user->table]->auth_only == TRUE) {
745 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
746 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>Non puoi cambiare nick a un tavolo per soli autenticati.</b>");', $dt, NICKSERV);
751 $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
755 $user->name = $name_new;
757 log_main($user->sess." chatt_send start set");
760 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
761 log_main($user->sess." chatt_send set loop");
763 $user_cur = &$this->user[$i];
764 if ($user_cur->sess == '')
767 if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
768 $table = &$this->table[$user->table];
770 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
771 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
772 $this->user[$table->player[($user_cur->table_pos) % BRISKIN5_PLAYERS_N]]->flags,
773 xcape($this->user[$table->player[($user_cur->table_pos) % BRISKIN5_PLAYERS_N]]->name),
775 $this->user[$table->player[($user_cur->table_pos+1) % BRISKIN5_PLAYERS_N]]->flags,
776 xcape($this->user[$table->player[($user_cur->table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
778 $this->user[$table->player[($user_cur->table_pos+2) % BRISKIN5_PLAYERS_N]]->flags,
779 xcape($this->user[$table->player[($user_cur->table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
781 (BRISKIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+3) % BRISKIN5_PLAYERS_N]]->flags),
782 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
784 (BRISKIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+4) % BRISKIN5_PLAYERS_N]]->flags),
785 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
786 if ($user_cur == $user) {
787 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
788 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
789 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";',
790 $itin, xcape($user->name,ENT_COMPAT,"UTF-8"), $itou);
792 $user_cur->step_inc();
798 for ($i = 0 ; $i < ($user->stat == 'room' ? BRISKIN5_MAX_PLAYERS : BRISKIN5_PLAYERS_N) ; $i++) {
799 if ($user->stat == 'room') {
800 $user_cur = &$this->user[$i];
801 if ($user_cur->sess == '' || $user_cur->stat != 'room')
805 $user_cur = &$this->user[$table->player[$i]];
808 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
809 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
810 $dt, $user->flags, xcape($user->name), xcape($user_mesg));
811 $user_cur->step_inc();
813 log_legal($curtime, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
817 function table_wakeup(&$user)
819 $table = &$this->table[0];
821 log_main("BRISKIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
825 log_main("BRISKIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
827 for ($i = 0 ; $i < $table->player_n ; $i++) {
828 $user_cur = &$this->user[$i];
829 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
831 if ($user_cur == $user)
832 $user_cur->subst = "shutdowner";
834 $user_cur->subst = "shutdowned";
835 $user_cur->laccwr = $curtime;
837 $ret = "gst.st = ".($user_cur->step+1)."; ";
838 $ret .= 'gst.st_loc++; the_end=true; window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
840 log_wr($user_cur->sess." BRISKIN5_WAKEUP: ".$ret);
841 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
842 $user_cur->step_inc();
845 $this->the_end = TRUE;
849 * If all players are freezed the room garbage_manager clean up table and users.
851 function is_abandoned()
856 $table = &$this->table[0];
858 for ($i = 0 ; $i < $table->player_n ; $i++) {
859 $user_cur = &$this->user[$i];
861 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
869 } // end class Briskin5
871 function locshm_exists($tok)
875 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
876 log_main($tok." SHM NOT exists");
882 log_main($tok." SHM exists");
890 function calculate_points(&$table)
892 GLOBAL $G_all_points;
896 if ($table->asta_pnt == 60)
897 $table->asta_pnt = 61;
899 $table->old_reason = "";
900 $table->old_win = $table->asta_win;
901 $table->old_friend = $table->friend;
902 $table->old_asta_pnt = $table->asta_pnt;
904 for ($i = 0 ; $i < 40 ; $i++) {
905 $ctt = $table->card[$i]->value % 10;
906 $own = $table->card[$i]->owner;
907 if ($own == $table->asta_win || $own == $table->friend)
908 $pro += $G_all_points[$ctt];
911 log_wr(sprintf("PRO: [%d]", $pro));
914 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
915 $table->points[$table->points_n % MAX_POINTS] = array();
916 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++)
917 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
919 $table->old_pnt = $pro;
925 if ($pro >= $table->asta_pnt)
930 $table->points[$table->points_n % MAX_POINTS] = array();
931 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
932 if ($i == $table->asta_win)
933 $pt = ($i == $table->friend ? 4 : 2);
934 else if ($i == $table->friend)
939 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
941 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
943 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
945 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
946 $table->total[$i] += $pt;
949 $table->old_pnt = $pro;
954 is_transition (is from room to table ?)
955 is_again (is another game)
957 Examples of $is_transition, $is_again:
958 from reload of the page: FALSE, FALSE
959 from sitdown in room: TRUE, FALSE
960 from table: asta cmd e tutti passano: TRUE, TRUE
961 from table: fine partita: TRUE, TRUE
963 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
965 $table_idx = $user->table;
966 $table = &$room->table[$table_idx];
967 $table_pos = $user->table_pos;
969 $ret = "table_init();";
970 $ret .= $table->exitlock_show(&$room->user, $table_pos);
973 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
974 $sendstep, $user->stat, $user->subst, $table_pos);
976 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
979 $ret .= "background_set();";
982 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
983 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
985 $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', $itin, xcape($user->name), $itou);
986 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
987 $room->user[$table->player[($table_pos) % BRISKIN5_PLAYERS_N]]->flags,
988 xcape($room->user[$table->player[($table_pos) % BRISKIN5_PLAYERS_N]]->name),
990 $room->user[$table->player[($table_pos+1) % BRISKIN5_PLAYERS_N]]->flags,
991 xcape($room->user[$table->player[($table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
993 $room->user[$table->player[($table_pos+2) % BRISKIN5_PLAYERS_N]]->flags,
994 xcape($room->user[$table->player[($table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
996 (BRISKIN5_PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+3) % BRISKIN5_PLAYERS_N]]->flags),
997 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
999 (BRISKIN5_PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+4) % BRISKIN5_PLAYERS_N]]->flags),
1000 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
1002 /* NOTIFY FOR THE CARD MAKER */
1003 if ($is_transition) { // && $user->subst == "asta" superfluo
1004 $ret .= show_table_info(&$room, &$table, $table_pos);
1007 $ret .= table_wellcome($user);
1009 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1010 $ret .= playsound("cow.mp3");
1015 if ($is_transition) { // && $user->subst == "asta" superfluo
1018 for ($i = 0 ; $i < 8 ; $i++) {
1019 for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++) {
1021 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1022 if ($table->card[$o]->owner == (($e + $table->gstart) % BRISKIN5_PLAYERS_N)) {
1028 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1030 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BRISKIN5_PLAYERS_N,
1031 $i, ((($e + BRISKIN5_PLAYERS_N - $table_pos + $table->gstart) % BRISKIN5_PLAYERS_N) == 0 ?
1032 $table->card[$o]->value : -1),
1033 ($i == 7 && $e == (BRISKIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1038 $taked = array(0,0,0,0,0);
1039 $inhand = array(0,0,0,0,0);
1040 $ontabl = array(-1,-1,-1,-1,-1);
1043 for ($i = 0 ; $i < 40 ; $i++) {
1044 if ($table->card[$i]->stat == 'hand') {
1045 if ($table->card[$i]->owner == $table_pos) {
1046 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1048 $inhand[$table->card[$i]->owner]++;
1050 else if ($table->card[$i]->stat == 'take') {
1051 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1052 $taked[$table->card[$i]->owner]++;
1054 else if ($table->card[$i]->stat == 'table') {
1055 $ontabl[$table->card[$i]->owner] = $i;
1059 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1060 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1062 log_main("Stat table: ".$logg);
1064 /* Set ours cards. */
1066 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1067 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1068 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
1069 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1070 $ret .= sprintf('card_setours(%s);', $oursarg);
1072 /* Dispose all cards */
1073 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1074 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1075 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1076 $inhand[$i], $taked[$i]);
1078 if ($ontabl[$i] != -1) {
1079 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1080 $table->card[$ontabl[$i]]->value,
1081 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1087 if ($user->subst == 'asta') {
1089 /* show users auction status */
1091 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1092 $user_cur = &$room->user[$table->player[$i]];
1093 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1094 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1096 if (BRISKIN5_PLAYERS_N == 3)
1097 $showst .= ",-2,-2";
1098 $ret .= sprintf('show_astat(%s);', $showst);
1100 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1101 /* show card chooser */
1102 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1107 if ($table_pos == ($table->gstart % BRISKIN5_PLAYERS_N) &&
1108 $table->asta_win == -1)
1109 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1110 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1112 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1113 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1117 if ($table->asta_win == -1) { // auction case
1118 if ($table_pos == ($table->gstart % BRISKIN5_PLAYERS_N))
1119 $ret .= "remark_on();";
1121 $ret .= "remark_off();";
1123 else { // chooseed case
1124 if ($table_pos == $table->asta_win)
1125 $ret .= "remark_on();";
1127 $ret .= "remark_off();";
1130 else if ($user->subst == 'game') {
1132 if (($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N == $table_pos)
1133 $ret .= "is_my_time = true; remark_on();";
1135 $ret .= "remark_off();";
1137 /* WHO CALL AND WATH */
1138 $ret .= briscola_show($room, $table, $user);
1142 } // end function show_table(...
1144 function calculate_winner(&$table)
1151 $cur_seed = $table->briscola - ($table->briscola % 10);
1153 for ($i = 0 ; $i < 40 ; $i++) {
1154 if ($table->card[$i]->stat != "table")
1157 log_wr(sprintf("Card On table: [%d]", $i));
1159 $v = $table->card[$i]->value;
1160 $ontab[$table->card[$i]->owner] = $v;
1161 $ontid[$table->card[$i]->owner] = $i;
1162 /* se briscola setto il flag */
1163 if (($v - ($v % 10)) == $cur_seed)
1167 if ($briontab == FALSE) {
1168 $cur_win = $table->gstart;
1169 $cur_val = $ontab[$cur_win];
1170 $cur_seed = $cur_val - ($cur_val % 10);
1173 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1174 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1175 if ($ontab[$i] < $cur_val) {
1176 $cur_val = $ontab[$i];
1182 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1183 $table->card[$ontid[$i]]->owner = $cur_win;
1184 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1189 function show_table_info(&$room, &$table, $table_pos)
1192 $user = &$room->user[$table->player[$table_pos]];
1194 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1195 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1198 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++)
1199 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
1200 $noty .= sprintf("</tr>");
1203 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1205 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1206 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1207 for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++)
1208 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1213 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1214 for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++)
1215 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1216 $noty .= "</tr></table>";
1218 if ($table->old_reason != "") {
1219 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1222 if ($table->old_win != -1) {
1223 log_main("TABLE_OLD_WIN:".$table->old_win);
1224 $win = $table->player[$table->old_win];
1225 log_main("TABLE_OLD_FRIEND:".$table->old_friend);
1226 $fri = $table->player[$table->old_friend];
1228 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1231 /* 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" */
1232 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
1233 xcape($room->user[$win]->name),
1234 xcape($room->user[$fri]->name));
1235 if ($table->old_pnt == 120) {
1236 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1239 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1240 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1241 'più di 60'), $table->old_pnt,
1242 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1246 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1247 xcape($room->user[$win]->name));
1248 if ($table->old_pnt == 120) {
1249 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1252 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1253 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1254 'più di 60'), $table->old_pnt,
1255 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1259 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1260 if ($table->mazzo == $table_pos)
1261 $noty .= "Fai <b>tu</b> il mazzo,";
1263 $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
1264 $noty .= "Il mazzo a <b>$unam</b>,";
1267 if ($user->subst == 'asta') {
1268 if ($table->asta_win == -1) // auction case
1269 $curplayer = $table->gstart % BRISKIN5_PLAYERS_N;
1271 $curplayer = $table->asta_win;
1273 else if ($user->subst == 'game') {
1274 $curplayer = ($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N;
1277 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1278 if ($curplayer == $table_pos) {
1279 $noty .= " tocca a <b>te</b> giocare.";
1282 $unam = xcape($room->user[$table->player[$curplayer]]->name);
1283 $noty .= " tocca a <b>$unam</b> giocare.";
1286 if ($table->mult > 1) {
1287 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1289 $noty .= "<hr><br>";
1290 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
1291 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
1292 * 130 + ($table->points_n > 0 ? 50 : 0) +
1293 * (120 * ($table->points_n / MAX_POINTS)));
1294 * will be used when we refact notify js function following
1295 * photoo class logic
1301 function table_wellcome($user)
1303 GLOBAL $table_wellarr;
1306 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1307 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$i]));
1313 function briscola_show($room, $table, $user)
1318 if ($table->asta_card == 9)
1319 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1321 /* text of caller cell */
1322 if ($user->table_pos == $table->asta_win)
1323 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1325 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
1326 xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
1328 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1330 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1331 $ret .= sprintf('$("caller").style.visibility = "visible";');
1332 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1333 $ret .= sprintf('$("astalascio").style.visibility = "";');
1334 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1335 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1341 function game_result($asta_pnt, $pnt)
1343 if ($asta_pnt == 61) {
1346 else if ($pnt == 60)
1352 if ($pnt >= $asta_pnt)