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("brisk.phh");
25 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
26 echo "Debugging time!";
30 log_load($sess, "LOAD: index_wr.php");
35 log_wr($sess, 'COMM: '.$mesg);
37 $sem = Room::lock_data();
38 $room = &Room::load_data();
39 if (($user = &$room->get_user($sess, &$idx)) == FALSE) {
40 echo "Get User Error";
41 log_wr($sess, "Get User Error");
42 Room::unlock_data($sem);
45 $argz = explode('|', $mesg);
47 if ($argz[0] == 'shutdown') {
48 log_auth($user_cur->sess, "Shutdown session.");
49 $tmp_sess = $user->sess;
51 step_unproxy($tmp_sess);
53 $user->the_end = FALSE;
55 log_rd2($user->sess, "AUTO LOGOUT.");
56 if ($user->subst == 'sitdown' || $user->stat == 'table')
57 $room->room_wakeup(&$user);
58 else if ($user->subst == 'standup')
59 $room->room_outstandup(&$user);
61 log_rd2($sess, "SHUTDOWN FROM WHAT ???");
68 else if ($user->stat == 'room') {
69 $user->laccwr = time();
71 if ($argz[0] == 'help') {
72 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
73 $user->comm[$user->step % COMM_N] .= show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
75 log_wr($sess, $user->comm[$user->step % COMM_N]);
79 else if ($argz[0] == 'about') {
80 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
81 $user->comm[$user->step % COMM_N] .= show_notify(str_replace("\n", " ", $G_room_about), 0, "torna ai tavoli", 400, 200);
83 log_wr($sess, $user->comm[$user->step % COMM_N]);
87 else if ($argz[0] == 'chatt') {
88 $room->chatt_send(&$user,$mesg);
90 /**********************
94 **********************/
95 else if ($user->subst == 'standup') {
97 if ($argz[0] == 'sitdown') {
98 if ($user->the_end == TRUE) {
99 log_wr($sess, "INFO:SKIP:argz == sitdown && the_end == TRUE => ignore request.");
100 Room::unlock_data($sem);
103 /* TODO: refact to a function */
104 if ($user->bantime > $user->laccwr) {
105 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
106 $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);
109 Room::save_data($room);
110 Room::unlock_data($sem);
115 $table_idx = $argz[1];
116 $table = &$room->table[$table_idx];
118 if ($table->player_n == PLAYERS_N) {
119 log_wr($sess, "WARN:FSM: Sitdown unreachable, table full.");
120 Room::unlock_data($sem);
125 $user->subst = "sitdown";
126 $user->table = $table_idx;
127 $user->table_pos = $table->user_add($idx);
129 if ($table->player_n == PLAYERS_N) {
130 // Start game for this table.
131 log_wr($sess, "Start game!");
134 // START THE SPAWN HERE!!!!
137 if (TRUE) { // WITH SPAWN
139 else { // BEFORE SPAWN
141 $table->init(&$room->user);
142 $table->game_init(&$room->user);
146 for ($i = 0 ; $i < $table->player_n ; $i++) {
147 $user_cur = &$room->user[$table->player[$i]];
148 log_wr($sess, "Pre if!");
151 $ret .= sprintf('gst.st_loc++; gst.st=%d; the_end=true; window.onunload = null ; document.location.assign("table.php");|', $user_cur->step+1);
153 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
154 $user_cur->trans_step = $user_cur->step + 1;
155 log_wr($sess, "TRANS ATTIVATO");
158 $user_cur->stat_set('table');
159 $user_cur->subst = 'asta';
160 $user_cur->laccwr = $curtime;
161 $user_cur->step_inc();
163 $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$room,&$user_cur,$user_cur->step+1,TRUE, FALSE);
164 $user_cur->step_inc();
166 } // end else { BEFORE SPAWN
169 $room->room_sitdown(&$user, $table_idx);
172 else if ($argz[0] == 'logout') {
173 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
174 $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
175 $user->the_end = TRUE;
179 /**********************
183 **********************/
184 else if ($user->subst == 'sitdown') {
185 if ($argz[0] == 'wakeup') {
186 $room->room_wakeup(&$user);
188 else if ($argz[0] == 'logout') {
189 $room->room_wakeup(&$user);
190 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
191 $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
192 $user->the_end = TRUE;
197 /*********************
201 *********************/
202 else if ($user->stat == 'table') {
203 $user->laccwr = time();
204 $table = &$room->table[$user->table];
206 if ($argz[0] == 'tableinfo') {
207 log_wr($sess, "PER DI TABLEINFO");
208 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
209 $user->comm[$user->step % COMM_N] .= show_table_info(&$room, &$table, $user->table_pos);
210 log_wr($sess, $user->comm[$user->step % COMM_N]);
213 else if ($argz[0] == 'chatt') {
214 $room->chatt_send(&$user,$mesg);
216 else if ($argz[0] == 'logout') {
219 if ($user->exitislock == TRUE) {
221 $user->exitislock = FALSE;
226 $lockcalc = $table->exitlock_calc(&$room->user, $user->table_pos);
228 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
229 $user->comm[$user->step % COMM_N] .= $table->exitlock_show(&$room->user, $user->table_pos);
230 $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);
232 log_wr($sess, $user->comm[$user->step % COMM_N]);
234 $logout_cont = FALSE;
238 $user->bantime = $user->laccwr + BAN_TIME;
240 if ($logout_cont == TRUE) {
241 $room->room_wakeup(&$user);
244 else if ($argz[0] == 'exitlock') {
245 $user->exitislock = ($user->exitislock == TRUE ? FALSE : TRUE);
246 for ($ct = 0, $i = 0 ; $i < PLAYERS_N ; $i++) {
247 $user_cur[$i] = &$room->user[$table->player[$i]];
248 if ($user_cur[$i]->exitislock == FALSE)
251 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
252 $ret = sprintf('gst.st = %d;', $user_cur[$i]->step+1);
253 $ret .= sprintf('exitlock_show(%d, %s);', $ct,
254 ($user_cur[$i]->exitislock ? 'true' : 'false'));
255 $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N] = $ret;
256 log_wr($sess, $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N]);
257 $user_cur[$i]->step_inc();
260 else if ($user->subst == 'asta') {
261 if ($argz[0] == 'lascio' && $user->handpt <= 2) {
262 $index_cur = $table->gstart % PLAYERS_N;
264 log_wr($sess, sprintf("GIOCO FINITO !!!"));
267 $table->old_reason = sprintf("Ha lasciato %s perche` aveva al massimo 2 punti.", $user->name);
270 $table->game_init(&$room->user);
272 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
273 $user_cur = &$room->user[$table->player[$i]];
275 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
276 $ret .= show_table(&$room,&$user_cur,$user_cur->step+1, TRUE, TRUE);
277 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
278 $user_cur->step_inc();
281 else if ($argz[0] == 'asta') {
284 $index_cur = $table->gstart % PLAYERS_N;
285 if ($user->table_pos == $index_cur &&
286 $table->asta_pla[$index_cur]) {
290 log_wr($sess, "CI SIAMO a_card ".$a_card." asta_card ".$table->asta_card);
292 // Abbandono dell'asta
294 log_wr($sess, "Abbandona l'asta.");
295 $table->asta_pla[$index_cur] = FALSE;
296 $user->asta_card = -1;
297 $table->asta_pla_n--;
300 else if ($a_card <= 9) {
301 if ($a_card >= 0 && $a_card < 9 && $a_card > $table->asta_card)
303 else if ($a_card == 9 && $a_pnt > ($table->asta_pnt >= 61 ? $table->asta_pnt : 60) && $a_pnt <= 120)
307 if ($again == FALSE) {
308 log_wr($sess, "NUOVI ORZI.");
309 $user->asta_card = $a_card;
310 $table->asta_card = $a_card;
312 $user->asta_pnt = $a_pnt;
313 $table->asta_pnt = $a_pnt;
320 if ($again) { // Qualcosa non andato bene, rifare
321 $ret = sprintf('gst.st = %d; asta_pnt_set(%d);', $user->step+1,
322 ($table->asta_pnt > 60 ? $table->asta_pnt + 1 : 61) );
323 $user->comm[$user->step % COMM_N] = $ret;
326 log_wr($sess, "Ripetere.");
330 $showst = "show_astat(";
331 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
332 $user_cur = &$room->user[$table->player[$i]];
333 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
334 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
341 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
342 $user_cur = &$room->user[$table->player[$i]];
343 if ($maxcard < $user_cur->asta_card)
344 $maxcard = $user_cur->asta_card;
347 if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
348 !($table->asta_card == 9 && $table->asta_pnt == 120)) {
349 log_wr($sess,"ALLOPPA QUI");
350 for ($i = 1 ; $i < PLAYERS_N ; $i++) {
351 $index_next = ($table->gstart + $i) % PLAYERS_N;
352 if ($table->asta_pla[$index_next]) {
353 log_wr($sess,"GSTART 1");
354 $table->gstart += $i;
360 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
361 $user_cur = &$room->user[$table->player[$i]];
362 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
363 if ($user_cur->table_pos == ($table->gstart % PLAYERS_N))
364 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_on();',
365 $table->asta_card + 1, $table->asta_pnt+1, ($user_cur->handpt <= 2 ? "true" : "false"));
367 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_off();',
368 $table->asta_card + 1, -($table->asta_pnt+1), ($user_cur->handpt <= 2 ? "true" : "false"));
369 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
370 $user_cur->step_inc();
373 else if ($table->asta_pla_n == 0) {
374 log_wr($sess, "PASSANO TUTTI!");
376 log_wr($sess, sprintf("GIOCO FINITO !!!"));
378 $table->old_reason = "Hanno passato tutti.";
382 $table->game_init(&$room->user);
384 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
385 $user_cur = &$room->user[$table->player[$i]];
387 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
388 $ret .= show_table(&$room,&$user_cur,$user_cur->step+1, TRUE, TRUE);
389 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
390 $user_cur->step_inc();
394 log_wr($sess, "FINITA !");
395 // if a_pnt == 120 supergame ! else abbandono
396 if ($a_pnt == 120 || $user->asta_card != -1) {
397 $chooser = $index_cur;
398 for ($i = 1 ; $i < PLAYERS_N ; $i++)
400 $table->asta_pla[$i] = FALSE;
403 //"gst.st = ".($user->step+1)."; dispose_asta(".($table->asta_card + 1).",".-($table->asta_pnt).", true); remark_off();";
404 $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));
406 for ($i = 1 ; $i < PLAYERS_N ; $i++) {
407 $chooser = ($table->gstart + $i) % PLAYERS_N;
408 if ($table->asta_pla[$chooser]) {
413 $table->asta_win = $chooser;
415 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
416 $user_cur = &$room->user[$table->player[$i]];
417 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
419 if ($i == $chooser) {
420 $ret .= "choose_seed(". $table->asta_card."); \$(\"asta\").style.visibility = \"hidden\"; remark_on();";
423 $ret .= "remark_off();";
426 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
427 $user_cur->step_inc();
433 log_wr($sess, "NON CI SIAMO");
437 else if ($argz[0] == 'choose') {
438 if ($table->asta_win > -1 &&
439 $user->table_pos == $table->asta_win) {
440 $a_brisco = $argz[1];
441 if ($a_brisco >= 0 && $a_brisco < 40) {
442 $table->briscola = $a_brisco;
443 $table->friend = $table->card[$a_brisco]->owner;
444 log_wr($sess,"GSTART 2");
445 $table->gstart = ($table->mazzo+1) % PLAYERS_N;
446 log_wr($sess, "Setta la briscola a ".$a_brisco);
448 $chooser = $table->asta_win;
449 $user_chooser = &$room->user[$table->player[$chooser]];
450 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
451 $user_cur = &$room->user[$table->player[$i]];
452 $user_cur->subst = 'game';
453 $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
456 /* bg of caller cell */
457 $ret .= briscola_show($room, $table, $user_cur);
460 if ($i == ($table->gstart % PLAYERS_N))
461 $ret .= "is_my_time = true; remark_on();";
463 $ret .= "is_my_time = false; remark_off();";
465 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
466 $user_cur->step_inc();
469 TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
470 VAR PER PASSARE ALLA FASE DI GIOCO
477 else if ($user->subst == 'game') {
478 log_wr($sess, "state: table::game".$argz[0]);
480 if ($argz[0] == 'play') {
485 if (strpos($a_x, "px") != FALSE)
486 $a_x = substr($a_x,0,-2);
487 if (strpos($a_y, "px") != FALSE)
488 $a_y = substr($a_y,0,-2);
490 $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
491 $a_play, $user->table_pos, ($table->gstart % PLAYERS_N),
492 $table->mazzo, $table->gstart,
493 $table->card[$a_play]->stat, $table->card[$a_play]->owner);
494 log_wr($sess, "CIC".$loggo);
496 /* se era il suo turno e la carta era sua ed era in mano */
497 if ($a_play >=0 && $a_play < 40 &&
498 ($user->table_pos == (($table->gstart + $table->turn) % PLAYERS_N)) &&
499 $table->card[$a_play]->stat == 'hand' &&
500 $table->card[$a_play]->owner == $user->table_pos) {
501 log_wr($sess, sprintf("User: %s Play: %d",$user->name, $a_play));
503 /* Change the card status. */
504 $table->card[$a_play]->play($a_x, $a_y);
507 * !!!! TURN INCREMENTED BEFORE !!!!
509 $turn_cur = ($table->gstart + $table->turn) % PLAYERS_N;
512 $card_play = sprintf("card_play(%d,%d,%d,%d);|",
513 $user->table_pos, $a_play, $a_x, $a_y);
514 if (($table->turn % PLAYERS_N) != 0) { /* manche not finished */
515 $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
517 $player_cur = "remark_off();";
518 $player_nex = $card_play . "is_my_time = true; remark_on();";
519 $player_oth = $card_play;
521 else if ($table->turn <= (PLAYERS_N * 8)) { /* manche finished */
522 $winner = calculate_winner($table);
523 log_wr($sess,"GSTART 3");
524 $table->gstart = $winner;
525 $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
527 log_wr($sess, sprintf("The winner is: [%d] [%s]", $winner, $room->user[$table->player[$winner]]->name));
528 $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|cards_hidetake($d);",
530 $player_cur = "remark_off();" . $card_take . "|";
531 if ($turn_cur != $turn_nex)
532 $player_nex = $card_play . $card_take . "|";
535 if ($table->turn < (PLAYERS_N * 8)) /* game NOT finished */
536 $player_nex .= "is_my_time = true; remark_on();";
537 $player_oth = $card_play . $card_take;
540 log_wr($sess, sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
541 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
542 $user_cur = &$room->user[$table->player[$i]];
544 $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
547 if ($i == $turn_cur) {
550 if ($i == $turn_nex) {
553 if ($i != $turn_cur && $i != $turn_nex) {
563 if ($table->turn == (PLAYERS_N * 8)) { /* game finished */
564 log_wr($sess, sprintf("GIOCO FINITO !!!"));
566 /* ************************************************ */
567 /* PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO */
568 /* ************************************************ */
569 calculate_points(&$table);
572 $table->game_init(&$room->user);
574 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
575 $user_cur = &$room->user[$table->player[$i]];
576 $retar[$i] .= show_table(&$room,&$user_cur,$user_cur->step+1,TRUE, TRUE);
581 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
582 $user_cur = &$room->user[$table->player[$i]];
584 $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
585 $user_cur->step_inc();
588 log_wr($sess, sprintf("TURN: %d",$table->turn));
589 /* Have played all the players ? */
590 /* NO: switch the focus and enable the next player to play. */
592 /* YES: calculate who win and go to the next turn. */
596 log_wr($sess, "NOSENSE");
599 log_wr($sess, "before save data");
600 Room::save_data($room);
602 Room::unlock_data($sem);