5 * Copyright (C) 2006 matteo.nastasi@milug.org
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details. You should have received a
16 * copy of the GNU General Public License along with this program; if
17 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
18 * Suite 330, Boston, MA 02111-1307, USA.
24 require_once("../Obj/brisk.phh");
25 require_once("Obj/briskin5.phh");
27 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
28 echo "Debugging time!";
32 log_load($sess, "LOAD: index_wr.php");
37 log_wr($sess, 'COMM: '.$mesg);
39 if ($table_idx < 0 || $table_idx >= TABLE_N)
42 $sem = Briskin5::lock_data($table_idx);
43 $bri = &Briskin5::load_data($table_idx);
44 if (($user = &$bri->get_user($sess, &$idx)) == FALSE) {
45 echo "Get User Error";
46 log_wr($sess, "Get User Error");
47 Briskin5::unlock_data($sem);
50 $argz = explode('|', $mesg);
52 log_wr($sess, 'POSTSPLIT: '.$argz[0]);
54 if ($argz[0] == 'shutdown') {
55 log_auth($user_cur->sess, "Shutdown session.");
56 $tmp_sess = $user->sess;
58 step_unproxy($tmp_sess);
60 $user->the_end = FALSE;
62 log_rd2($user->sess, "AUTO LOGOUT.");
63 if ($user->subst == 'sitdown' || $user->stat == 'table')
64 $bri->room_wakeup(&$user);
65 else if ($user->subst == 'standup')
66 $bri->room_outstandup(&$user);
68 log_rd2($sess, "SHUTDOWN FROM WHAT ???");
75 else if ($user->stat == 'room') {
76 $user->laccwr = time();
78 if ($argz[0] == 'help') {
79 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
80 $user->comm[$user->step % COMM_N] .= show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
82 log_wr($sess, $user->comm[$user->step % COMM_N]);
86 else if ($argz[0] == 'about') {
87 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
88 $user->comm[$user->step % COMM_N] .= show_notify(str_replace("\n", " ", $G_room_about), 0, "torna ai tavoli", 400, 200);
90 log_wr($sess, $user->comm[$user->step % COMM_N]);
94 else if ($argz[0] == 'chatt') {
95 $bri->chatt_send(&$user,$mesg);
97 /**********************
101 **********************/
102 else if ($user->subst == 'standup') {
104 if ($argz[0] == 'sitdown') {
105 log_wr($sess, "SITDOWN command");
107 if ($user->the_end == TRUE) {
108 log_wr($sess, "INFO:SKIP:argz == sitdown && the_end == TRUE => ignore request.");
109 Briskin5::unlock_data($sem);
112 /* TODO: refact to a function */
113 if ($user->bantime > $user->laccwr) {
114 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
115 $user->comm[$user->step % COMM_N] .= show_notify("<br>Ti sei alzato da un tavolo senza il consenso degli altri giocatori. Dovrai aspettare ancora ".secstoword($user->bantime - $user->laccwr)." prima di poterti sedere nuovamente.", 2000, "Torna in piedi.", 400, 100);
118 Briskin5::save_data($bri);
119 Briskin5::unlock_data($sem);
124 $table_idx = $argz[1];
125 $table = &$bri->table[$table_idx];
127 if ($table->player_n == PLAYERS_N) {
128 log_wr($sess, "WARN:FSM: Sitdown unreachable, table full.");
129 Briskin5::unlock_data($sem);
134 $user->subst = "sitdown";
135 $user->table = $table_idx;
136 $user->table_pos = $table->user_add($idx);
138 log_wr($sess, "MOP before");
140 if ($table->player_n == PLAYERS_N) {
141 log_wr($sess, "MOP inall");
143 // Start game for this table.
144 log_wr($sess, "Start game!");
147 // START THE SPAWN HERE!!!!
150 if (TRUE) { // WITH SPAWN
152 // Create new spawned table
154 for ($i = 0 ; $i < BRISCOLAIN5_PLAYERS_N ; $i++)
155 $us[$i] = &$bri->user[$table->player[$i]];
156 if (($bri =& new Briskin5(&$us, &$table, $table_idx)) == FALSE)
157 log_wr($sess, "bri create: FALSE");
159 log_wr($sess, "bri create: ".serialize($bri));
162 // Set root table and users
164 else { // BEFORE SPAWN
166 $table->init(&$bri->user);
167 $table->game_init(&$bri->user);
171 for ($i = 0 ; $i < $table->player_n ; $i++) {
172 $user_cur = &$bri->user[$table->player[$i]];
173 log_wr($sess, "Pre if!");
176 $ret .= sprintf('gst.st_loc++; gst.st=%d; the_end=true; window.onunload = null ; document.location.assign("table.php");|', $user_cur->step+1);
178 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
179 $user_cur->trans_step = $user_cur->step + 1;
180 log_wr($sess, "TRANS ATTIVATO");
183 $user_cur->stat_set('table');
184 $user_cur->subst = 'asta';
185 $user_cur->laccwr = $curtime;
186 $user_cur->step_inc();
188 $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, FALSE);
189 $user_cur->step_inc();
191 } // end else { BEFORE SPAWN
193 log_wr($sess, "MOP after");
197 $bri->room_sitdown(&$user, $table_idx);
199 log_wr($sess, "MOP finish");
203 else if ($argz[0] == 'logout') {
204 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
205 $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
206 $user->the_end = TRUE;
210 /**********************
214 **********************/
215 else if ($user->subst == 'sitdown') {
216 if ($argz[0] == 'wakeup') {
217 $bri->room_wakeup(&$user);
219 else if ($argz[0] == 'logout') {
220 $bri->room_wakeup(&$user);
221 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
222 $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
223 $user->the_end = TRUE;
228 /*********************
232 *********************/
233 else if ($user->stat == 'table') {
234 $user->laccwr = time();
235 $table = &$bri->table[$user->table];
237 if ($argz[0] == 'tableinfo') {
238 log_wr($sess, "PER DI TABLEINFO");
239 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
240 $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
241 log_wr($sess, $user->comm[$user->step % COMM_N]);
244 else if ($argz[0] == 'chatt') {
245 $bri->chatt_send(&$user,$mesg);
247 else if ($argz[0] == 'logout') {
250 if ($user->exitislock == TRUE) {
252 $user->exitislock = FALSE;
257 $lockcalc = $table->exitlock_calc(&$bri->user, $user->table_pos);
259 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
260 $user->comm[$user->step % COMM_N] .= $table->exitlock_show(&$bri->user, $user->table_pos);
261 $user->comm[$user->step % COMM_N] .= show_notify("<br>I dati presenti sul server non erano allineati con quelli inviati dal tuo browser, adesso lo sono. Riprova ora.", 2000, "Torna alla partita.", 400, 100);
263 log_wr($sess, $user->comm[$user->step % COMM_N]);
265 $logout_cont = FALSE;
269 $user->bantime = $user->laccwr + BAN_TIME;
271 if ($logout_cont == TRUE) {
272 $bri->table_wakeup(&$user);
275 else if ($argz[0] == 'exitlock') {
276 $user->exitislock = ($user->exitislock == TRUE ? FALSE : TRUE);
277 for ($ct = 0, $i = 0 ; $i < PLAYERS_N ; $i++) {
278 $user_cur[$i] = &$bri->user[$table->player[$i]];
279 if ($user_cur[$i]->exitislock == FALSE)
282 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
283 $ret = sprintf('gst.st = %d;', $user_cur[$i]->step+1);
284 $ret .= sprintf('exitlock_show(%d, %s);', $ct,
285 ($user_cur[$i]->exitislock ? 'true' : 'false'));
286 $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N] = $ret;
287 log_wr($sess, $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N]);
288 $user_cur[$i]->step_inc();
291 else if ($user->subst == 'asta') {
292 if ($argz[0] == 'lascio' && $user->handpt <= 2) {
293 $index_cur = $table->gstart % PLAYERS_N;
295 log_wr($sess, sprintf("GIOCO FINITO !!!"));
298 $table->old_reason = sprintf("Ha lasciato %s perche` aveva al massimo 2 punti.", $user->name);
301 $table->game_init(&$bri->user);
303 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
304 $user_cur = &$bri->user[$table->player[$i]];
306 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
307 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
308 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
309 $user_cur->step_inc();
312 else if ($argz[0] == 'asta') {
315 $index_cur = $table->gstart % PLAYERS_N;
316 if ($user->table_pos == $index_cur &&
317 $table->asta_pla[$index_cur]) {
321 log_wr($sess, "CI SIAMO a_card ".$a_card." asta_card ".$table->asta_card);
323 // Abbandono dell'asta
325 log_wr($sess, "Abbandona l'asta.");
326 $table->asta_pla[$index_cur] = FALSE;
327 $user->asta_card = -1;
328 $table->asta_pla_n--;
331 else if ($a_card <= 9) {
332 if ($a_card >= 0 && $a_card < 9 && $a_card > $table->asta_card)
334 else if ($a_card == 9 && $a_pnt > ($table->asta_pnt >= 61 ? $table->asta_pnt : 60) && $a_pnt <= 120)
338 if ($again == FALSE) {
339 log_wr($sess, "NUOVI ORZI.");
340 $user->asta_card = $a_card;
341 $table->asta_card = $a_card;
343 $user->asta_pnt = $a_pnt;
344 $table->asta_pnt = $a_pnt;
351 if ($again) { // Qualcosa non andato bene, rifare
352 $ret = sprintf('gst.st = %d; asta_pnt_set(%d);', $user->step+1,
353 ($table->asta_pnt > 60 ? $table->asta_pnt + 1 : 61) );
354 $user->comm[$user->step % COMM_N] = $ret;
357 log_wr($sess, "Ripetere.");
361 $showst = "show_astat(";
362 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
363 $user_cur = &$bri->user[$table->player[$i]];
364 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
365 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
372 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
373 $user_cur = &$bri->user[$table->player[$i]];
374 if ($maxcard < $user_cur->asta_card)
375 $maxcard = $user_cur->asta_card;
378 if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
379 !($table->asta_card == 9 && $table->asta_pnt == 120)) {
380 log_wr($sess,"ALLOPPA QUI");
381 for ($i = 1 ; $i < PLAYERS_N ; $i++) {
382 $index_next = ($table->gstart + $i) % PLAYERS_N;
383 if ($table->asta_pla[$index_next]) {
384 log_wr($sess,"GSTART 1");
385 $table->gstart += $i;
391 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
392 $user_cur = &$bri->user[$table->player[$i]];
393 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
394 if ($user_cur->table_pos == ($table->gstart % PLAYERS_N))
395 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_on();',
396 $table->asta_card + 1, $table->asta_pnt+1, ($user_cur->handpt <= 2 ? "true" : "false"));
398 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_off();',
399 $table->asta_card + 1, -($table->asta_pnt+1), ($user_cur->handpt <= 2 ? "true" : "false"));
400 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
401 $user_cur->step_inc();
404 else if ($table->asta_pla_n == 0) {
405 log_wr($sess, "PASSANO TUTTI!");
407 log_wr($sess, sprintf("GIOCO FINITO !!!"));
409 $table->old_reason = "Hanno passato tutti.";
413 $table->game_init(&$bri->user);
415 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
416 $user_cur = &$bri->user[$table->player[$i]];
418 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
419 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
420 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
421 $user_cur->step_inc();
425 log_wr($sess, "FINITA !");
426 // if a_pnt == 120 supergame ! else abbandono
427 if ($a_pnt == 120 || $user->asta_card != -1) {
428 $chooser = $index_cur;
429 for ($i = 1 ; $i < PLAYERS_N ; $i++)
431 $table->asta_pla[$i] = FALSE;
434 //"gst.st = ".($user->step+1)."; dispose_asta(".($table->asta_card + 1).",".-($table->asta_pnt).", true); remark_off();";
435 $user->comm[$user->step % COMM_N] = sprintf( "gst.st = %d; dispose_asta(%d, %d, false); remark_off();", $user->step+1, $table->asta_card + 1,-($table->asta_pnt));
437 for ($i = 1 ; $i < PLAYERS_N ; $i++) {
438 $chooser = ($table->gstart + $i) % PLAYERS_N;
439 if ($table->asta_pla[$chooser]) {
444 $table->asta_win = $chooser;
446 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
447 $user_cur = &$bri->user[$table->player[$i]];
448 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
450 if ($i == $chooser) {
451 $ret .= "choose_seed(". $table->asta_card."); \$(\"asta\").style.visibility = \"hidden\"; remark_on();";
454 $ret .= "remark_off();";
457 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
458 $user_cur->step_inc();
464 log_wr($sess, "NON CI SIAMO");
468 else if ($argz[0] == 'choose') {
469 if ($table->asta_win > -1 &&
470 $user->table_pos == $table->asta_win) {
471 $a_brisco = $argz[1];
472 if ($a_brisco >= 0 && $a_brisco < 40) {
473 $table->briscola = $a_brisco;
474 $table->friend = $table->card[$a_brisco]->owner;
475 log_wr($sess,"GSTART 2");
476 $table->gstart = ($table->mazzo+1) % PLAYERS_N;
477 log_wr($sess, "Setta la briscola a ".$a_brisco);
479 $chooser = $table->asta_win;
480 $user_chooser = &$bri->user[$table->player[$chooser]];
481 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
482 $user_cur = &$bri->user[$table->player[$i]];
483 $user_cur->subst = 'game';
484 $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
487 /* bg of caller cell */
488 $ret .= briscola_show($bri, $table, $user_cur);
491 if ($i == ($table->gstart % PLAYERS_N))
492 $ret .= "is_my_time = true; remark_on();";
494 $ret .= "is_my_time = false; remark_off();";
496 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
497 $user_cur->step_inc();
500 TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
501 VAR PER PASSARE ALLA FASE DI GIOCO
508 else if ($user->subst == 'game') {
509 log_wr($sess, "state: table::game".$argz[0]);
511 if ($argz[0] == 'play') {
516 if (strpos($a_x, "px") != FALSE)
517 $a_x = substr($a_x,0,-2);
518 if (strpos($a_y, "px") != FALSE)
519 $a_y = substr($a_y,0,-2);
521 $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
522 $a_play, $user->table_pos, ($table->gstart % PLAYERS_N),
523 $table->mazzo, $table->gstart,
524 $table->card[$a_play]->stat, $table->card[$a_play]->owner);
525 log_wr($sess, "CIC".$loggo);
527 /* se era il suo turno e la carta era sua ed era in mano */
528 if ($a_play >=0 && $a_play < 40 &&
529 ($user->table_pos == (($table->gstart + $table->turn) % PLAYERS_N)) &&
530 $table->card[$a_play]->stat == 'hand' &&
531 $table->card[$a_play]->owner == $user->table_pos) {
532 log_wr($sess, sprintf("User: %s Play: %d",$user->name, $a_play));
534 /* Change the card status. */
535 $table->card[$a_play]->play($a_x, $a_y);
538 * !!!! TURN INCREMENTED BEFORE !!!!
540 $turn_cur = ($table->gstart + $table->turn) % PLAYERS_N;
543 $card_play = sprintf("card_play(%d,%d,%d,%d);|",
544 $user->table_pos, $a_play, $a_x, $a_y);
545 if (($table->turn % PLAYERS_N) != 0) { /* manche not finished */
546 $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
548 $player_cur = "remark_off();";
549 $player_nex = $card_play . "is_my_time = true; remark_on();";
550 $player_oth = $card_play;
552 else if ($table->turn <= (PLAYERS_N * 8)) { /* manche finished */
553 $winner = calculate_winner($table);
554 log_wr($sess,"GSTART 3");
555 $table->gstart = $winner;
556 $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
558 log_wr($sess, sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
559 $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|cards_hidetake($d);",
561 $player_cur = "remark_off();" . $card_take . "|";
562 if ($turn_cur != $turn_nex)
563 $player_nex = $card_play . $card_take . "|";
566 if ($table->turn < (PLAYERS_N * 8)) /* game NOT finished */
567 $player_nex .= "is_my_time = true; remark_on();";
568 $player_oth = $card_play . $card_take;
571 log_wr($sess, sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
572 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
573 $user_cur = &$bri->user[$table->player[$i]];
575 $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
578 if ($i == $turn_cur) {
581 if ($i == $turn_nex) {
584 if ($i != $turn_cur && $i != $turn_nex) {
594 if ($table->turn == (PLAYERS_N * 8)) { /* game finished */
595 log_wr($sess, sprintf("GIOCO FINITO !!!"));
597 /* ************************************************ */
598 /* PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO */
599 /* ************************************************ */
600 calculate_points(&$table);
603 $table->game_init(&$bri->user);
605 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
606 $user_cur = &$bri->user[$table->player[$i]];
607 $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
612 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
613 $user_cur = &$bri->user[$table->player[$i]];
615 $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
616 $user_cur->step_inc();
619 log_wr($sess, sprintf("TURN: %d",$table->turn));
620 /* Have played all the players ? */
621 /* NO: switch the focus and enable the next player to play. */
623 /* YES: calculate who win and go to the next turn. */
627 log_wr($sess, "NOSENSE");
630 log_wr($sess, "before save data");
631 Briskin5::save_data($bri);
633 Briskin5::unlock_data($sem);