2 define(BRISKIN5_PLAYERS_N, 3);
3 define(BRISKIN5_MAX_PLAYERS, BRISKIN5_PLAYERS_N);
4 // define(BRISKIN5_SHM_MIN, (50000 * BRISKIN5_MAX_PLAYERS));
5 define(BRISKIN5_SHM_MIN, 32768);
6 define(BRISKIN5_SHM_MAX, (BRISKIN5_SHM_MIN + 1048576));
7 define(BRISKIN5_SHM_DLT, 32768);
9 $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.');
12 var $value; /* 0 - 39 card value */
13 var $stat; /* 'bunch', 'hand', 'table', 'take' */
14 var $owner; /* (table position 0-4) */
15 // var $pos; /* Pos in hand. */
16 var $x; /* When played the X position on the table of the owner. */
17 var $y; /* When played the Y position on the table of the owner. */
19 function Card($value, $stat, $owner)
21 $this->value = $value;
22 $this->stat = $stat; // Card stat
23 $this->owner = $owner;
26 function assign($stat,$owner)
28 $this->stat = $stat; // Card stat
29 $this->owner = $owner;
39 $this->stat = 'table'; // Card stat
44 function take($newown)
46 $this->stat = 'take'; // Card stat
47 $this->owner = $newown;
51 class Table_briskin5 extends Table {
52 var $card; // il mazzo di carte
53 var $mazzo; // chi e' di mazzo
63 var $points; // points array
64 var $points_n; // number of row of points
77 function Table_briskin5()
83 function &create($idx)
87 if (($thiz =& new Table_briskin5()) == FALSE)
93 $thiz->asta_pla = array(); // TRUE: in auction, FALSE: out of the auction
94 $thiz->asta_pla_n= -1;
95 $thiz->asta_card = -1;
99 $thiz->points = array( );
101 $thiz->total = array( 0, 0, 0, 0, 0);
102 $thiz->asta_win = -1;
103 $thiz->briscola = -1;
107 $thiz->old_reason = "";
108 $thiz->old_asta_pnt = -1;
111 $thiz->old_friend = -1;
118 function &clone(&$from)
122 if (($thiz =& new Table_briskin5()) == FALSE)
127 $thiz->card = $from->card;
128 $thiz->mazzo = $from->mazzo; // REVIEW
129 $thiz->gstart = $from->gstart;
130 $thiz->turn = $from->turn;
132 $thiz->asta_pla = $from->asta_pla;
133 $thiz->asta_pla_n = $from->asta_pla_n;
134 $thiz->asta_card = $from->asta_card;
135 $thiz->asta_pnt = $from->asta_pnt;
137 $thiz->mult = $from->mult;
138 $thiz->points = $from->points;
139 $thiz->points_n = $from->points_n;
140 $thiz->total = $from->total;
142 $thiz->asta_win = $from->asta_win;
143 $thiz->briscola = $from->briscola;
144 $thiz->friend = $from->friend;
146 $thiz->old_reason = $from->old_reason;
147 $thiz->old_asta_pnt = $from->old_asta_pnt;
148 $thiz->old_pnt = $from->old_pnt;
149 $thiz->old_win = $from->old_win;
150 $thiz->old_friend = $from->old_friend;
156 function parentcopy(&$from)
161 function &spawn(&$from)
165 if (($thiz =& new Table_briskin5()) == FALSE)
168 $thiz->parentcopy($from);
170 log_main("PLAYER_N - spawn.".$thiz->player_n);
172 $thiz->card = &$thiz->bunch_create();
173 $thiz->mazzo = rand(0,PLAYERS_N-1);
177 $thiz->old_reason = "";
179 // players are rearranged in an dedicated array
180 $thiz->player = array();
181 for ($i = 0 ; $i < $from->player_n ; $i++)
182 $thiz->player[$i] = $i;
184 log_main("TABLE_OLD_WIN - spawn:".$thiz->old_win);
190 function &bunch_create()
194 for ($i = 0 ; $i < 40 ; $i++) {
195 $ret[$i] =& new Card($i, 'bunch', 'no_owner');
202 function bunch_make()
204 $ct = array(0,0,0,0,0);
206 mt_srand(make_seed());
208 for ($i = 39 ; $i >= 0 ; $i--)
211 for ($i = 39 ; $i >= 0 ; $i--) {
215 log_main("RND ZERO");
219 $owner = $i % PLAYERS_N;
220 $this->card[$id]->assign('hand', $owner);
222 $rest[$rn] = $rest[$i];
223 // $pubbpos[$rn2] = $pubbpos[$i];
227 function init(&$userarr)
230 $this->mazzo = rand(0,PLAYERS_N-1);
234 $this->old_reason = "";
236 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
237 $this->total[$i] = 0;
238 $user_cur = &$userarr[$this->player[$i]];
239 $user_cur->exitislock = TRUE;
242 log_main("table::init: ci siamo");
245 function game_init(&$userarr)
249 $this->gstart = ($this->mazzo+1) % PLAYERS_N;
253 $this->asta_pla_n = PLAYERS_N;
254 $this->asta_card = -1;
255 $this->asta_pnt = 60;
256 $this->asta_win = -1;
257 $this->briscola = -1;
261 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
262 $this->asta_pla[$i] = TRUE;
263 $user_cur = &$userarr[$this->player[$i]];
264 $user_cur->subst = 'asta';
265 $user_cur->asta_card = -2;
266 $user_cur->asta_pnt = -1;
267 $user_cur->handpt = $this->hand_points($i);
268 // SEE function calculate_points(&$table)
274 $this->mazzo = ($this->mazzo + 1) % PLAYERS_N;
278 function hand_points($idx)
280 GLOBAL $G_all_points;
284 for ($i = 0 ; $i < 40 ; $i++) {
285 if ($this->card[$i]->owner != $idx)
288 $ctt = $this->card[$i]->value % 10;
289 $tot += $G_all_points[$ctt];
296 function exitlock_show(&$userarr, $table_pos)
298 $ct = $this->exitlock_calc(&$userarr, $table_pos);
300 $ret = sprintf('exitlock_show(%d, %s);', $ct,
301 ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
305 function exitlock_calc(&$userarr, $table_pos)
309 for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
310 if ($userarr[$this->player[$i]]->exitislock == FALSE)
316 } // end class Table_briskin5
323 class User_briskin5 extends User {
326 var $handpt; // Total card points at the beginning of the current hand.
327 var $exitislock; // Player can exit from the table ?
333 function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
336 if (($thiz =& new User()) == FALSE)
339 $thiz->asta_card = -2;
340 $thiz->asta_pnt = -1;
342 $thiz->exitislock = TRUE;
348 function parentcopy(&$from)
353 function copy(&$from)
355 $this->parentcopy($from);
357 $this->asta_card = $from->asta_card;
358 $this->asta_pnt = $from->asta_pnt;
359 $this->handpt = $from->handpt;
360 $this->exitislock = $from->exitislock;
364 function &clone(&$from)
368 if (($thiz =& new User()) == FALSE)
377 function &spawn(&$from, $table, $table_pos)
381 if (($thiz =& new User_briskin5()) == FALSE)
384 $thiz->parentcopy($from);
386 $thiz->asta_card = -2;
387 $thiz->asta_pnt = -1;
389 $thiz->exitislock = TRUE;
391 $thiz->table_orig = $table;
393 $thiz->table_pos = $table_pos;
397 } // end class User_briskin5
404 var $comm; // commands for many people
405 var $step; // current step of the comm array
406 var $garbage_timeout;
415 function Briskin5 (&$room, $table_idx, $table_token) {
416 $this->user = array();
417 $this->table = array();
419 $this->the_end = FALSE;
420 $this->shm_sz = BRISKIN5_SHM_MIN;
421 if (($this->tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
426 $user =& $room->user;
427 $table =& $room->table[$table_idx];
429 log_wr("Briskin5 constructor");
431 for ($i = 0 ; $i < $table->player_n ; $i++) {
432 $user[$table->player[$i]]->table_token = $table_token;
433 $this->user[$i] =& User_briskin5::spawn(&$user[$table->player[$i]], $table_idx, $i);
435 $this->table[0] =& Table_briskin5::spawn(&$table);
437 log_main("TABLE_OLD_WIN - Briskin5:".$this->table[0]->old_win);
439 $this->table_idx = $table_idx;
440 $this->table_token = $table_token;
441 $this->garbage_timeout = 0;
443 log_wr("Briskin5 constructor end");
447 function &get_user($sess, &$idx)
449 GLOBAL $PHP_SELF, $G_false;
451 if (validate_sess($sess)) {
452 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
453 if (strcmp($sess, $this->user[$i]->sess) == 0) {
456 $ret = &$this->user[$i];
460 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
461 // for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++)
462 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
465 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
472 function garbage_manager($force)
475 /* Garbage collector degli utenti in timeout */
478 if ($force || $this->garbage_timeout < $curtime) {
480 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
481 $user_cur = &$this->user[$i];
482 if ($user_cur->sess == "" ||
483 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
486 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
487 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
489 if ($user_cur->stat == 'table') {
490 log_auth($user_cur->sess," bin5 Autologout session.");
492 /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE
494 $tmp_sess = $user_cur->sess;
495 $user_cur->sess = "";
496 step_unproxy($tmp_sess);
497 $user_cur->name = "";
498 $user_cur->the_end = FALSE;
502 $this->table_wakeup(&$user_cur);
506 log_rd2($user_cur->sess." GARBAGE UPDATED!");
508 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
521 function &load_data($table_idx, $table_token = "")
523 GLOBAL $G_false, $sess;
527 log_wr("TABLE_IDX ".FTOK_PATH."/table".$table_idx);
530 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
531 log_main("ftok failed");
536 if (($shm_sz = sharedmem_sz($tok)) == -1) {
537 log_main("shmop_open failed");
541 if (($shm = shm_attach($tok, $shm_sz)) == FALSE)
544 if (($bri = @shm_get_var($shm, $tok)) == FALSE)
547 if ($table_token != "" && $bri->table_token != $table_token) {
548 log_wr("bri->table_token: ".$bri->table_token."table_token: ".$table_token);
563 log_wr("briskin5 load_data failed");
572 function save_data(&$bri)
579 log_main("SAVE BRISKIN5 DATA");
581 if (!isset($bri->tok))
584 while ($bri->shm_sz < BRISKIN5_SHM_MAX) {
585 if (($shm = shm_attach($bri->tok, $bri->shm_sz)) == FALSE)
588 if (@shm_put_var($shm, $bri->tok, $bri) != FALSE) {
592 if (shm_remove($shm) === FALSE) {
593 log_only("REMOVE FALLITA");
597 $bri->shm_sz += BRISKIN5_SHM_DLT;
600 log_crit("save data failed!");
610 function destroy_data($table_idx)
616 log_main("DESTROY BRISKIN5 DATA");
619 log_main("DESTROY2 BRISKIN5 DATA");
621 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1)
624 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
627 if (shmop_delete($shm) == 0) {
628 log_only("REMOVE FALLITA ");
633 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
635 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
645 function lock_data($table_idx)
649 log_lock("LOCK_DATA ".FTOK_PATH."/table".$table_idx);
650 // echo "LOCK: ".FTOK_PATH."/main";
652 // WARNING monitor this step
653 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
656 // WARNING monitor this step
657 if (($res = @sem_get($tok)) == FALSE) {
660 if (sem_acquire($res)) {
661 log_lock("LOCK table ".$table_idx."[res: ".$res."]");
665 log_lock("LOCK table ".$table_idx.":FAILED");
670 function unlock_data($res)
674 log_lock("UNLOCK table [res: ".$res."]");
676 return (sem_release($res));
680 function chatt_send(&$user, $mesg)
682 if ($user->stat == 'table') {
683 $table = &$this->table[$user->table];
686 $user_mesg = substr($mesg,6);
690 $dt = date("H:i ", $curtime);
691 if (strncmp($user_mesg, "/nick ", 6) == 0) {
692 log_main($user->sess." chatt_send BEGIN");
694 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
695 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
696 $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);
701 $user_mesg = "COMMAND ".$user_mesg;
704 // update local graph
705 // update remote graphs
706 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
707 $user_cur = &$this->user[$i];
708 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
709 if ($user_cur->sess == '')
711 if ($user_cur->name == $name_new) {
712 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
713 $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);
718 if ($i == BRISKIN5_MAX_PLAYERS) {
719 if ($user->flags & USER_FLAG_AUTH && strcasecmp($user->name,$name_new) != 0) {
720 if ($this->table[$user->table]->auth_only == TRUE) {
721 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
722 $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);
727 $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
731 $user->name = $name_new;
733 log_main($user->sess." chatt_send start set");
736 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
737 log_main($user->sess." chatt_send set loop");
739 $user_cur = &$this->user[$i];
740 if ($user_cur->sess == '')
743 if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
744 $table = &$this->table[$user->table];
746 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
747 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
748 $this->user[$table->player[($user_cur->table_pos) % BRISKIN5_PLAYERS_N]]->flags,
749 xcape($this->user[$table->player[($user_cur->table_pos) % BRISKIN5_PLAYERS_N]]->name),
751 $this->user[$table->player[($user_cur->table_pos+1) % BRISKIN5_PLAYERS_N]]->flags,
752 xcape($this->user[$table->player[($user_cur->table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
754 $this->user[$table->player[($user_cur->table_pos+2) % BRISKIN5_PLAYERS_N]]->flags,
755 xcape($this->user[$table->player[($user_cur->table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
757 (BRISKIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+3) % BRISKIN5_PLAYERS_N]]->flags),
758 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
760 (BRISKIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+4) % BRISKIN5_PLAYERS_N]]->flags),
761 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
762 if ($user_cur == $user) {
763 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
764 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
765 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";',
766 $itin, xcape($user->name,ENT_COMPAT,"UTF-8"), $itou);
768 $user_cur->step_inc();
774 for ($i = 0 ; $i < ($user->stat == 'room' ? BRISKIN5_MAX_PLAYERS : BRISKIN5_PLAYERS_N) ; $i++) {
775 if ($user->stat == 'room') {
776 $user_cur = &$this->user[$i];
777 if ($user_cur->sess == '' || $user_cur->stat != 'room')
781 $user_cur = &$this->user[$table->player[$i]];
784 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
785 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
786 $dt, $user->flags, xcape($user->name), xcape($user_mesg));
787 $user_cur->step_inc();
789 log_legal($curtime, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
793 function table_wakeup(&$user)
795 $table = &$this->table[0];
797 log_main("BRISKIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
801 log_main("BRISKIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
803 for ($i = 0 ; $i < $table->player_n ; $i++) {
804 $user_cur = &$this->user[$i];
805 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
807 if ($user_cur == $user)
808 $user_cur->subst = "shutdowner";
810 $user_cur->subst = "shutdowned";
811 $user_cur->laccwr = $curtime;
813 $ret = "gst.st = ".($user_cur->step+1)."; ";
814 $ret .= 'gst.st_loc++; the_end=true; window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
816 log_wr($user_cur->sess." BRISKIN5_WAKEUP: ".$ret);
817 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
818 $user_cur->step_inc();
821 $this->the_end = TRUE;
825 * If all players are freezed the room garbage_manager clean up table and users.
827 function is_abandoned()
832 $table = &$this->table[0];
834 for ($i = 0 ; $i < $table->player_n ; $i++) {
835 $user_cur = &$this->user[$i];
837 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
845 } // end class Briskin5
847 function locshm_exists($tok)
851 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
852 log_main($tok." SHM NOT exists");
858 log_main($tok." SHM exists");
866 function calculate_points(&$table)
868 GLOBAL $G_all_points;
872 if ($table->asta_pnt == 60)
873 $table->asta_pnt = 61;
875 $table->old_reason = "";
876 $table->old_win = $table->asta_win;
877 $table->old_friend = $table->friend;
878 $table->old_asta_pnt = $table->asta_pnt;
880 for ($i = 0 ; $i < 40 ; $i++) {
881 $ctt = $table->card[$i]->value % 10;
882 $own = $table->card[$i]->owner;
883 if ($own == $table->asta_win || $own == $table->friend)
884 $pro += $G_all_points[$ctt];
887 log_wr(sprintf("PRO: [%d]", $pro));
890 if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
891 $table->points[$table->points_n % MAX_POINTS] = array();
892 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++)
893 $table->points[$table->points_n % MAX_POINTS][$i] = 0;
895 $table->old_pnt = $pro;
901 if ($pro >= $table->asta_pnt)
906 $table->points[$table->points_n % MAX_POINTS] = array();
907 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
908 if ($i == $table->asta_win)
909 $pt = ($i == $table->friend ? 4 : 2);
910 else if ($i == $table->friend)
915 log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
917 $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
919 log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
921 $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
922 $table->total[$i] += $pt;
925 $table->old_pnt = $pro;
930 is_transition (is from room to table ?)
931 is_again (is another game)
933 Examples of $is_transition, $is_again:
934 from reload of the page: FALSE, FALSE
935 from sitdown in room: TRUE, FALSE
936 from table: asta cmd e tutti passano: TRUE, TRUE
937 from table: fine partita: TRUE, TRUE
939 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
941 $table_idx = $user->table;
942 $table = &$room->table[$table_idx];
943 $table_pos = $user->table_pos;
945 $ret = "table_init();";
946 $ret .= $table->exitlock_show(&$room->user, $table_pos);
949 $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
950 $sendstep, $user->stat, $user->subst, $table_pos);
952 log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
955 $ret .= "background_set();";
958 $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
959 $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
961 $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', $itin, xcape($user->name), $itou);
962 $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
963 $room->user[$table->player[($table_pos) % BRISKIN5_PLAYERS_N]]->flags,
964 xcape($room->user[$table->player[($table_pos) % BRISKIN5_PLAYERS_N]]->name),
966 $room->user[$table->player[($table_pos+1) % BRISKIN5_PLAYERS_N]]->flags,
967 xcape($room->user[$table->player[($table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
969 $room->user[$table->player[($table_pos+2) % BRISKIN5_PLAYERS_N]]->flags,
970 xcape($room->user[$table->player[($table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
972 (BRISKIN5_PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+3) % BRISKIN5_PLAYERS_N]]->flags),
973 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
975 (BRISKIN5_PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+4) % BRISKIN5_PLAYERS_N]]->flags),
976 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($room->user[$table->player[($table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
978 /* NOTIFY FOR THE CARD MAKER */
979 if ($is_transition) { // && $user->subst == "asta" superfluo
980 $ret .= show_table_info(&$room, &$table, $table_pos);
983 $ret .= table_wellcome($user);
985 if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
986 $ret .= playsound("cow.mp3");
991 if ($is_transition) { // && $user->subst == "asta" superfluo
994 for ($i = 0 ; $i < 8 ; $i++) {
995 for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++) {
997 for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
998 if ($table->card[$o]->owner == (($e + $table->gstart) % BRISKIN5_PLAYERS_N)) {
1004 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1006 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BRISKIN5_PLAYERS_N,
1007 $i, ((($e + BRISKIN5_PLAYERS_N - $table_pos + $table->gstart) % BRISKIN5_PLAYERS_N) == 0 ?
1008 $table->card[$o]->value : -1),
1009 ($i == 7 && $e == (BRISKIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1014 $taked = array(0,0,0,0,0);
1015 $inhand = array(0,0,0,0,0);
1016 $ontabl = array(-1,-1,-1,-1,-1);
1019 for ($i = 0 ; $i < 40 ; $i++) {
1020 if ($table->card[$i]->stat == 'hand') {
1021 if ($table->card[$i]->owner == $table_pos) {
1022 $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1024 $inhand[$table->card[$i]->owner]++;
1026 else if ($table->card[$i]->stat == 'take') {
1027 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1028 $taked[$table->card[$i]->owner]++;
1030 else if ($table->card[$i]->stat == 'table') {
1031 $ontabl[$table->card[$i]->owner] = $i;
1035 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1036 $logg .= sprintf("INHAND: %d IN TABLE %d TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1038 log_main("Stat table: ".$logg);
1040 /* Set ours cards. */
1042 for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1043 $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1044 for ($i = $inhand[$table_pos] ; $i < 8 ; $i++)
1045 $oursarg .= ($i == 0 ? "" : ", ")."-1";
1046 $ret .= sprintf('card_setours(%s);', $oursarg);
1048 /* Dispose all cards */
1049 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1050 /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1051 $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1052 $inhand[$i], $taked[$i]);
1054 if ($ontabl[$i] != -1) {
1055 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1056 $table->card[$ontabl[$i]]->value,
1057 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1063 if ($user->subst == 'asta') {
1065 /* show users auction status */
1067 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1068 $user_cur = &$room->user[$table->player[$i]];
1069 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1070 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1072 if (BRISKIN5_PLAYERS_N == 3)
1073 $showst .= ",-2,-2";
1074 $ret .= sprintf('show_astat(%s);', $showst);
1076 if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1077 /* show card chooser */
1078 $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1083 if ($table_pos == ($table->gstart % BRISKIN5_PLAYERS_N) &&
1084 $table->asta_win == -1)
1085 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1086 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1088 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1089 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ? "true" : "false"));
1093 if ($table->asta_win == -1) { // auction case
1094 if ($table_pos == ($table->gstart % BRISKIN5_PLAYERS_N))
1095 $ret .= "remark_on();";
1097 $ret .= "remark_off();";
1099 else { // chooseed case
1100 if ($table_pos == $table->asta_win)
1101 $ret .= "remark_on();";
1103 $ret .= "remark_off();";
1106 else if ($user->subst == 'game') {
1108 if (($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N == $table_pos)
1109 $ret .= "is_my_time = true; remark_on();";
1111 $ret .= "remark_off();";
1113 /* WHO CALL AND WATH */
1114 $ret .= briscola_show($room, $table, $user);
1118 } // end function show_table(...
1120 function calculate_winner(&$table)
1127 $cur_seed = $table->briscola - ($table->briscola % 10);
1129 for ($i = 0 ; $i < 40 ; $i++) {
1130 if ($table->card[$i]->stat != "table")
1133 log_wr(sprintf("Card On table: [%d]", $i));
1135 $v = $table->card[$i]->value;
1136 $ontab[$table->card[$i]->owner] = $v;
1137 $ontid[$table->card[$i]->owner] = $i;
1138 /* se briscola setto il flag */
1139 if (($v - ($v % 10)) == $cur_seed)
1143 if ($briontab == FALSE) {
1144 $cur_win = $table->gstart;
1145 $cur_val = $ontab[$cur_win];
1146 $cur_seed = $cur_val - ($cur_val % 10);
1149 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1150 if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1151 if ($ontab[$i] < $cur_val) {
1152 $cur_val = $ontab[$i];
1158 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1159 $table->card[$ontid[$i]]->owner = $cur_win;
1160 $table->card[$ontid[$i]]->stat = "take"; // Card stat
1165 function show_table_info(&$room, &$table, $table_pos)
1168 $user = &$room->user[$table->player[$table_pos]];
1170 $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1171 $noty = sprintf('<table class=\"points\"><tr><th></th>');
1174 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++)
1175 $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
1176 $noty .= sprintf("</tr>");
1179 log_main("show_table_info: pnt_min: ".$pnt_min." Points_n: ".$table->points_n);
1181 for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1182 $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1183 for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++)
1184 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1189 $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1190 for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++)
1191 $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1192 $noty .= "</tr></table>";
1194 if ($table->old_reason != "") {
1195 $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1198 if ($table->old_win != -1) {
1199 log_main("TABLE_OLD_WIN:".$table->old_win);
1200 $win = $table->player[$table->old_win];
1201 log_main("TABLE_OLD_FRIEND:".$table->old_friend);
1202 $fri = $table->player[$table->old_friend];
1204 $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1207 /* 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" */
1208 $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>",
1209 xcape($room->user[$win]->name),
1210 xcape($room->user[$fri]->name));
1211 if ($table->old_pnt == 120) {
1212 $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1215 $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1216 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1217 'più di 60'), $table->old_pnt,
1218 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1222 $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si è chiamato in mano,<br>",
1223 xcape($room->user[$win]->name));
1224 if ($table->old_pnt == 120) {
1225 $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1228 $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1229 ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1230 'più di 60'), $table->old_pnt,
1231 ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1235 /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1236 if ($table->mazzo == $table_pos)
1237 $noty .= "Fai <b>tu</b> il mazzo,";
1239 $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
1240 $noty .= "Il mazzo a <b>$unam</b>,";
1243 if ($user->subst == 'asta') {
1244 if ($table->asta_win == -1) // auction case
1245 $curplayer = $table->gstart % BRISKIN5_PLAYERS_N;
1247 $curplayer = $table->asta_win;
1249 else if ($user->subst == 'game') {
1250 $curplayer = ($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N;
1253 /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1254 if ($curplayer == $table_pos) {
1255 $noty .= " tocca a <b>te</b> giocare.";
1258 $unam = xcape($room->user[$table->player[$curplayer]]->name);
1259 $noty .= " tocca a <b>$unam</b> giocare.";
1262 if ($table->mult > 1) {
1263 $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1265 $noty .= "<hr><br>";
1266 $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
1267 /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
1268 * 130 + ($table->points_n > 0 ? 50 : 0) +
1269 * (120 * ($table->points_n / MAX_POINTS)));
1270 * will be used when we refact notify js function following
1271 * photoo class logic
1277 function table_wellcome($user)
1279 GLOBAL $table_wellarr;
1282 for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1283 $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$i]));
1289 function briscola_show($room, $table, $user)
1294 if ($table->asta_card == 9)
1295 $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1297 /* text of caller cell */
1298 if ($user->table_pos == $table->asta_win)
1299 $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1301 $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";',
1302 xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
1304 $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1306 $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1307 $ret .= sprintf('$("caller").style.visibility = "visible";');
1308 $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1309 $ret .= sprintf('$("astalascio").style.visibility = "";');
1310 $ret .= sprintf('$("asta").style.visibility = "hidden";');
1311 $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1317 function game_result($asta_pnt, $pnt)
1319 if ($asta_pnt == 61) {
1322 else if ($pnt == 60)
1328 if ($pnt >= $asta_pnt)