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("briskin5/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");
39 log_wr($sess, 'COMM: '.$mesg);
41 $sem = Room::lock_data();
42 $room = &Room::load_data();
43 if (($user = &$room->get_user($sess, &$idx)) == FALSE) {
44 echo "Get User Error";
45 log_wr($sess, "Get User Error");
46 Room::unlock_data($sem);
49 $argz = explode('|', $mesg);
51 log_wr($sess, 'POSTSPLIT: '.$argz[0]);
53 if ($argz[0] == 'shutdown') {
54 log_auth($user_cur->sess, "Shutdown session.");
55 $tmp_sess = $user->sess;
57 step_unproxy($tmp_sess);
59 $user->the_end = FALSE;
61 log_rd2($user->sess, "AUTO LOGOUT.");
62 if ($user->subst == 'sitdown' || $user->stat == 'table')
63 $room->room_wakeup(&$user);
64 else if ($user->subst == 'standup')
65 $room->room_outstandup(&$user);
67 log_rd2($sess, "SHUTDOWN FROM WHAT ???");
74 else if ($user->stat == 'room') {
75 $user->laccwr = time();
77 if ($argz[0] == 'help') {
78 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
79 $user->comm[$user->step % COMM_N] .= show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
81 log_wr($sess, $user->comm[$user->step % COMM_N]);
85 else if ($argz[0] == 'about') {
86 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
87 $user->comm[$user->step % COMM_N] .= show_notify(str_replace("\n", " ", $G_room_about), 0, "torna ai tavoli", 400, 200);
89 log_wr($sess, $user->comm[$user->step % COMM_N]);
93 else if ($argz[0] == 'chatt') {
94 $room->chatt_send(&$user,$mesg);
96 /**********************
100 **********************/
101 else if ($user->subst == 'standup') {
103 if ($argz[0] == 'sitdown') {
104 log_wr($sess, "SITDOWN command");
106 if ($user->the_end == TRUE) {
107 log_wr($sess, "INFO:SKIP:argz == sitdown && the_end == TRUE => ignore request.");
108 Room::unlock_data($sem);
111 /* TODO: refact to a function */
112 if ($user->bantime > $user->laccwr) {
113 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
114 $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);
117 Room::save_data($room);
118 Room::unlock_data($sem);
123 $table_idx = $argz[1];
124 $table = &$room->table[$table_idx];
126 if ($table->player_n == PLAYERS_N) {
127 log_wr($sess, "WARN:FSM: Sitdown unreachable, table full.");
128 Room::unlock_data($sem);
133 $user->subst = "sitdown";
134 $user->table = $table_idx;
135 $user->table_pos = $table->user_add($idx);
137 log_wr($sess, "MOP before");
139 if ($table->player_n == PLAYERS_N) {
140 log_wr($sess, "MOP inall");
142 // Start game for this table.
143 log_wr($sess, "Start game!");
146 // START THE SPAWN HERE!!!!
149 if (TRUE) { // WITH SPAWN
151 // Create new spawned table
152 $bri_sem = Briskin5::lock_data($table_idx);
153 $table_token = uniqid("");
154 if (($bri =& new Briskin5(&$room, $table_idx, $table_token)) == FALSE)
155 log_wr($sess, "bri create: FALSE");
157 log_wr($sess, "bri create: ".serialize($bri));
160 $bri_table =& $bri->table[0];
161 $bri_table->init(&$bri->user);
162 $bri_table->game_init(&$bri->user);
166 // Init spawned users.
168 for ($i = 0 ; $i < $table->player_n ; $i++) {
169 $bri_user_cur = &$bri->user[$i];
170 $user_cur = &$room->user[$table->player[$i]];
172 $bri_user_cur->trans_step = $user_cur->step + 1;
173 $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = "";
174 $bri_user_cur->step_inc();
175 $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = show_table(&$bri,&$bri_user_cur,$bri_user_cur->step+1,TRUE, FALSE);
177 $bri_user_cur->stat_set('table');
178 $bri_user_cur->subst = 'asta';
179 $bri_user_cur->laccwr = $curtime;
181 $bri_user_cur->step_inc();
183 log_wr($bri_user_cur->sess, "TRY PRESAVE: ".$bri_user_cur->step." TRANS STEP: ".$bri_user_cur->trans_step);
185 log_wr($sess, "Pre if!");
188 $ret .= sprintf('gst.st_loc++; gst.st=%d; createCookie("table_idx", %d, 24*365, cookiepath); createCookie("table_token", "%s", 24*365, cookiepath); the_end=true; window.onunload = null ; document.location.assign("briskin5/briskin5.php");|', $user_cur->step+1, $table_idx, $table_token);
190 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
191 $user_cur->trans_step = $user_cur->step + 1;
192 log_wr($sess, "TRANS ATTIVATO");
195 $user_cur->stat_set('table');
196 $user_cur->subst = 'asta';
197 $user_cur->laccwr = $curtime;
198 $user_cur->step_inc();
200 log_wr($sess, "presave bri");
201 Briskin5::save_data($bri);
202 Briskin5::unlock_data($bri_sem);
203 log_wr($sess, "postsave bri");
205 else { // BEFORE SPAWN
207 $table->init(&$room->user);
208 $table->game_init(&$room->user);
212 for ($i = 0 ; $i < $table->player_n ; $i++) {
213 $user_cur = &$room->user[$table->player[$i]];
214 log_wr($sess, "Pre if!");
217 $ret .= sprintf('gst.st_loc++; gst.st=%d; the_end=true; window.onunload = null ; document.location.assign("table.php");|', $user_cur->step+1);
219 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
220 $user_cur->trans_step = $user_cur->step + 1;
221 log_wr($sess, "TRANS ATTIVATO");
224 $user_cur->stat_set('table');
225 $user_cur->subst = 'asta';
226 $user_cur->laccwr = $curtime;
227 $user_cur->step_inc();
229 $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$room,&$user_cur,$user_cur->step+1,TRUE, FALSE);
230 $user_cur->step_inc();
232 } // end else { BEFORE SPAWN
234 log_wr($sess, "MOP after");
238 $room->room_sitdown(&$user, $table_idx);
240 log_wr($sess, "MOP finish");
244 else if ($argz[0] == 'logout') {
245 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
246 $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
247 $user->the_end = TRUE;
251 /**********************
255 **********************/
256 else if ($user->subst == 'sitdown') {
257 if ($argz[0] == 'wakeup') {
258 $room->room_wakeup(&$user);
260 else if ($argz[0] == 'logout') {
261 $room->room_wakeup(&$user);
262 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
263 $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
264 $user->the_end = TRUE;
269 /*********************
273 *********************/
274 else if ($user->stat == 'table') {
275 $user->laccwr = time();
276 $table = &$room->table[$user->table];
278 if ($argz[0] == 'tableinfo') {
279 log_wr($sess, "PER DI TABLEINFO");
280 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
281 $user->comm[$user->step % COMM_N] .= show_table_info(&$room, &$table, $user->table_pos);
282 log_wr($sess, $user->comm[$user->step % COMM_N]);
285 else if ($argz[0] == 'chatt') {
286 $room->chatt_send(&$user,$mesg);
288 else if ($argz[0] == 'logout') {
291 if ($user->exitislock == TRUE) {
293 $user->exitislock = FALSE;
298 $lockcalc = $table->exitlock_calc(&$room->user, $user->table_pos);
300 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
301 $user->comm[$user->step % COMM_N] .= $table->exitlock_show(&$room->user, $user->table_pos);
302 $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);
304 log_wr($sess, $user->comm[$user->step % COMM_N]);
306 $logout_cont = FALSE;
310 $user->bantime = $user->laccwr + BAN_TIME;
312 if ($logout_cont == TRUE) {
313 $room->room_wakeup(&$user);
316 else if ($argz[0] == 'exitlock') {
317 $user->exitislock = ($user->exitislock == TRUE ? FALSE : TRUE);
318 for ($ct = 0, $i = 0 ; $i < PLAYERS_N ; $i++) {
319 $user_cur[$i] = &$room->user[$table->player[$i]];
320 if ($user_cur[$i]->exitislock == FALSE)
323 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
324 $ret = sprintf('gst.st = %d;', $user_cur[$i]->step+1);
325 $ret .= sprintf('exitlock_show(%d, %s);', $ct,
326 ($user_cur[$i]->exitislock ? 'true' : 'false'));
327 $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N] = $ret;
328 log_wr($sess, $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N]);
329 $user_cur[$i]->step_inc();
332 else if ($user->subst == 'asta') {
333 if ($argz[0] == 'lascio' && $user->handpt <= 2) {
334 $index_cur = $table->gstart % PLAYERS_N;
336 log_wr($sess, sprintf("GIOCO FINITO !!!"));
339 $table->old_reason = sprintf("Ha lasciato %s perche` aveva al massimo 2 punti.", $user->name);
342 $table->game_init(&$room->user);
344 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
345 $user_cur = &$room->user[$table->player[$i]];
347 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
348 $ret .= show_table(&$room,&$user_cur,$user_cur->step+1, TRUE, TRUE);
349 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
350 $user_cur->step_inc();
353 else if ($argz[0] == 'asta') {
356 $index_cur = $table->gstart % PLAYERS_N;
357 if ($user->table_pos == $index_cur &&
358 $table->asta_pla[$index_cur]) {
362 log_wr($sess, "CI SIAMO a_card ".$a_card." asta_card ".$table->asta_card);
364 // Abbandono dell'asta
366 log_wr($sess, "Abbandona l'asta.");
367 $table->asta_pla[$index_cur] = FALSE;
368 $user->asta_card = -1;
369 $table->asta_pla_n--;
372 else if ($a_card <= 9) {
373 if ($a_card >= 0 && $a_card < 9 && $a_card > $table->asta_card)
375 else if ($a_card == 9 && $a_pnt > ($table->asta_pnt >= 61 ? $table->asta_pnt : 60) && $a_pnt <= 120)
379 if ($again == FALSE) {
380 log_wr($sess, "NUOVI ORZI.");
381 $user->asta_card = $a_card;
382 $table->asta_card = $a_card;
384 $user->asta_pnt = $a_pnt;
385 $table->asta_pnt = $a_pnt;
392 if ($again) { // Qualcosa non andato bene, rifare
393 $ret = sprintf('gst.st = %d; asta_pnt_set(%d);', $user->step+1,
394 ($table->asta_pnt > 60 ? $table->asta_pnt + 1 : 61) );
395 $user->comm[$user->step % COMM_N] = $ret;
398 log_wr($sess, "Ripetere.");
402 $showst = "show_astat(";
403 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
404 $user_cur = &$room->user[$table->player[$i]];
405 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
406 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
413 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
414 $user_cur = &$room->user[$table->player[$i]];
415 if ($maxcard < $user_cur->asta_card)
416 $maxcard = $user_cur->asta_card;
419 if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
420 !($table->asta_card == 9 && $table->asta_pnt == 120)) {
421 log_wr($sess,"ALLOPPA QUI");
422 for ($i = 1 ; $i < PLAYERS_N ; $i++) {
423 $index_next = ($table->gstart + $i) % PLAYERS_N;
424 if ($table->asta_pla[$index_next]) {
425 log_wr($sess,"GSTART 1");
426 $table->gstart += $i;
432 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
433 $user_cur = &$room->user[$table->player[$i]];
434 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
435 if ($user_cur->table_pos == ($table->gstart % PLAYERS_N))
436 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_on();',
437 $table->asta_card + 1, $table->asta_pnt+1, ($user_cur->handpt <= 2 ? "true" : "false"));
439 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_off();',
440 $table->asta_card + 1, -($table->asta_pnt+1), ($user_cur->handpt <= 2 ? "true" : "false"));
441 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
442 $user_cur->step_inc();
445 else if ($table->asta_pla_n == 0) {
446 log_wr($sess, "PASSANO TUTTI!");
448 log_wr($sess, sprintf("GIOCO FINITO !!!"));
450 $table->old_reason = "Hanno passato tutti.";
454 $table->game_init(&$room->user);
456 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
457 $user_cur = &$room->user[$table->player[$i]];
459 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
460 $ret .= show_table(&$room,&$user_cur,$user_cur->step+1, TRUE, TRUE);
461 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
462 $user_cur->step_inc();
466 log_wr($sess, "FINITA !");
467 // if a_pnt == 120 supergame ! else abbandono
468 if ($a_pnt == 120 || $user->asta_card != -1) {
469 $chooser = $index_cur;
470 for ($i = 1 ; $i < PLAYERS_N ; $i++)
472 $table->asta_pla[$i] = FALSE;
475 //"gst.st = ".($user->step+1)."; dispose_asta(".($table->asta_card + 1).",".-($table->asta_pnt).", true); remark_off();";
476 $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));
478 for ($i = 1 ; $i < PLAYERS_N ; $i++) {
479 $chooser = ($table->gstart + $i) % PLAYERS_N;
480 if ($table->asta_pla[$chooser]) {
485 $table->asta_win = $chooser;
487 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
488 $user_cur = &$room->user[$table->player[$i]];
489 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
491 if ($i == $chooser) {
492 $ret .= "choose_seed(". $table->asta_card."); \$(\"asta\").style.visibility = \"hidden\"; remark_on();";
495 $ret .= "remark_off();";
498 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
499 $user_cur->step_inc();
505 log_wr($sess, "NON CI SIAMO");
509 else if ($argz[0] == 'choose') {
510 if ($table->asta_win > -1 &&
511 $user->table_pos == $table->asta_win) {
512 $a_brisco = $argz[1];
513 if ($a_brisco >= 0 && $a_brisco < 40) {
514 $table->briscola = $a_brisco;
515 $table->friend = $table->card[$a_brisco]->owner;
516 log_wr($sess,"GSTART 2");
517 $table->gstart = ($table->mazzo+1) % PLAYERS_N;
518 log_wr($sess, "Setta la briscola a ".$a_brisco);
520 $chooser = $table->asta_win;
521 $user_chooser = &$room->user[$table->player[$chooser]];
522 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
523 $user_cur = &$room->user[$table->player[$i]];
524 $user_cur->subst = 'game';
525 $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
528 /* bg of caller cell */
529 $ret .= briscola_show($room, $table, $user_cur);
532 if ($i == ($table->gstart % PLAYERS_N))
533 $ret .= "is_my_time = true; remark_on();";
535 $ret .= "is_my_time = false; remark_off();";
537 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
538 $user_cur->step_inc();
541 TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
542 VAR PER PASSARE ALLA FASE DI GIOCO
549 else if ($user->subst == 'game') {
550 log_wr($sess, "state: table::game".$argz[0]);
552 if ($argz[0] == 'play') {
557 if (strpos($a_x, "px") != FALSE)
558 $a_x = substr($a_x,0,-2);
559 if (strpos($a_y, "px") != FALSE)
560 $a_y = substr($a_y,0,-2);
562 $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
563 $a_play, $user->table_pos, ($table->gstart % PLAYERS_N),
564 $table->mazzo, $table->gstart,
565 $table->card[$a_play]->stat, $table->card[$a_play]->owner);
566 log_wr($sess, "CIC".$loggo);
568 /* se era il suo turno e la carta era sua ed era in mano */
569 if ($a_play >=0 && $a_play < 40 &&
570 ($user->table_pos == (($table->gstart + $table->turn) % PLAYERS_N)) &&
571 $table->card[$a_play]->stat == 'hand' &&
572 $table->card[$a_play]->owner == $user->table_pos) {
573 log_wr($sess, sprintf("User: %s Play: %d",$user->name, $a_play));
575 /* Change the card status. */
576 $table->card[$a_play]->play($a_x, $a_y);
579 * !!!! TURN INCREMENTED BEFORE !!!!
581 $turn_cur = ($table->gstart + $table->turn) % PLAYERS_N;
584 $card_play = sprintf("card_play(%d,%d,%d,%d);|",
585 $user->table_pos, $a_play, $a_x, $a_y);
586 if (($table->turn % PLAYERS_N) != 0) { /* manche not finished */
587 $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
589 $player_cur = "remark_off();";
590 $player_nex = $card_play . "is_my_time = true; remark_on();";
591 $player_oth = $card_play;
593 else if ($table->turn <= (PLAYERS_N * 8)) { /* manche finished */
594 $winner = calculate_winner($table);
595 log_wr($sess,"GSTART 3");
596 $table->gstart = $winner;
597 $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
599 log_wr($sess, sprintf("The winner is: [%d] [%s]", $winner, $room->user[$table->player[$winner]]->name));
600 $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|cards_hidetake($d);",
602 $player_cur = "remark_off();" . $card_take . "|";
603 if ($turn_cur != $turn_nex)
604 $player_nex = $card_play . $card_take . "|";
607 if ($table->turn < (PLAYERS_N * 8)) /* game NOT finished */
608 $player_nex .= "is_my_time = true; remark_on();";
609 $player_oth = $card_play . $card_take;
612 log_wr($sess, sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
613 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
614 $user_cur = &$room->user[$table->player[$i]];
616 $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
619 if ($i == $turn_cur) {
622 if ($i == $turn_nex) {
625 if ($i != $turn_cur && $i != $turn_nex) {
635 if ($table->turn == (PLAYERS_N * 8)) { /* game finished */
636 log_wr($sess, sprintf("GIOCO FINITO !!!"));
638 /* ************************************************ */
639 /* PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO */
640 /* ************************************************ */
641 calculate_points(&$table);
644 $table->game_init(&$room->user);
646 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
647 $user_cur = &$room->user[$table->player[$i]];
648 $retar[$i] .= show_table(&$room,&$user_cur,$user_cur->step+1,TRUE, TRUE);
653 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
654 $user_cur = &$room->user[$table->player[$i]];
656 $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
657 $user_cur->step_inc();
660 log_wr($sess, sprintf("TURN: %d",$table->turn));
661 /* Have played all the players ? */
662 /* NO: switch the focus and enable the next player to play. */
664 /* YES: calculate who win and go to the next turn. */
668 log_wr($sess, "NOSENSE");
671 log_wr($sess, "before save data");
672 Room::save_data($room);
674 Room::unlock_data($sem);