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);
39 if (($user = &$bri->get_user($sess, &$idx)) == FALSE) {
40 echo "Get User Error";
41 log_wr($sess, "Get User Error");
45 $argz = explode('|', $mesg);
47 if ($argz[0] == 'shutdown') {
48 log_auth($user_cur->sess, "Shutdown session.");
52 $user->the_end = FALSE;
54 log_rd2($user->sess, "AUTO LOGOUT.");
55 if ($user->subst == 'sitdown' || $user->stat == 'table')
56 $bri->room_wakeup(&$user);
57 else if ($user->subst == 'standup')
58 $bri->room_outstandup(&$user);
60 log_rd2($sess, "SHUTDOWN FROM WHAT ???");
62 else if ($user->stat == 'room') {
63 if ($argz[0] == 'help') {
64 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
65 $user->comm[$user->step % COMM_N] .= show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
67 log_wr($sess, $user->comm[$user->step % COMM_N]);
71 else if ($argz[0] == 'about') {
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_about), 0, "torna ai tavoli", 400, 200);
75 log_wr($sess, $user->comm[$user->step % COMM_N]);
79 else if ($argz[0] == 'logout') {
80 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
81 $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
82 $user->the_end = TRUE;
85 else if ($argz[0] == 'chatt') {
86 $bri->chatt_send(&$user,$mesg);
88 else if ($argz[0] == 'sitdown') {
89 if ($user->stat != 'room' || $user->subst != 'standup') {
90 log_wr($sess, "Warning ! sitdown out fsm");
96 $table_idx = $argz[1];
97 $table = &$bri->table[$table_idx];
99 if ($table->player_n == PLAYERS_N) {
100 log_wr($sess, "Warning ! unreachable, table full.");
106 $user->subst = "sitdown";
107 $user->table = $table_idx;
108 $user->table_pos = $table->user_add($idx);
110 if ($table->player_n == PLAYERS_N) {
111 // Start game for this table.
112 log_wr($sess, "Start game!");
115 $table->game_init(&$bri);
117 for ($i = 0 ; $i < $table->player_n ; $i++) {
118 $user_cur = &$bri->user[$table->player[$i]];
119 log_wr($sess, "Pre if!");
122 $ret .= sprintf('gst.st_loc++; gst.st=%d; the_end=true; window.onunload = null ; document.location.assign("table.php");|', $user_cur->step+1);
124 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
125 $user_cur->trans_step = $user_cur->step + 1;
126 log_wr($sess, "TRANS ATTIVATO");
128 $user_cur->stat = 'table';
129 $user_cur->subst = 'asta';
132 $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, FALSE);
137 $bri->room_sitdown(&$user, $table_idx);
139 else if ($argz[0] == 'wakeup') {
140 if ($user->stat != 'room' || $user->subst != 'sitdown') {
141 log_wr($sess, "Warning ! wakeup out fsm.");
148 $user->subst = "standup";
150 $bri->room_wakeup(&$user);
158 else if ($user->stat == 'table' && $user->subst == 'asta') {
159 $table = &$bri->table[$user->table];
161 if ($argz[0] == 'logout') {
163 // document.location.assign("index.php");
165 $bri->room_wakeup(&$user);
167 else if ($argz[0] == 'tableinfo') {
168 log_wr($sess, "PER DI TABLEINFO");
169 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
170 $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
171 log_wr($sess, $user->comm[$user->step % COMM_N]);
174 else if ($argz[0] == 'chatt') {
175 $bri->chatt_send(&$user,$mesg);
177 else if ($argz[0] == 'asta') {
180 $index_cur = $table->gstart % PLAYERS_N;
181 if ($user->table_pos == $index_cur &&
182 $table->asta_pla[$index_cur]) {
186 log_wr($sess, "CI SIAMO a_card ".$a_card." asta_card ".$table->asta_card);
188 // Abbandono dell'asta
190 log_wr($sess, "Abbandona l'asta.");
191 $table->asta_pla[$index_cur] = FALSE;
192 $user->asta_card = -1;
193 $table->asta_pla_n--;
196 else if ($a_card <= 9) {
197 if ($table->asta_card == 9) {
198 if ($a_card == 9 && $a_pnt <= 120 && $a_pnt > $table->asta_pnt)
202 if ($a_card >= 0 && $a_card <= 9 && $a_card > $table->asta_card)
206 if ($again == FALSE) {
207 log_wr($sess, "NUOVI ORZI.");
208 $user->asta_card = $a_card;
209 $table->asta_card = $a_card;
211 $user->asta_pnt = $a_pnt;
212 $table->asta_pnt = $a_pnt;
219 if ($again) { // Qualcosa non andato bene, rifare
220 log_wr($sess, "Ripetere.");
224 $showst = "show_astat(";
225 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
226 $user_cur = &$bri->user[$table->player[$i]];
227 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
228 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
235 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
236 $user_cur = &$bri->user[$table->player[$i]];
237 if ($maxcard < $user_cur->asta_card)
238 $maxcard = $user_cur->asta_card;
241 if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
242 !($table->asta_card == 9 && $table->asta_pnt == 120)) {
243 log_wr($sess,"ALLOPPA QUI");
244 for ($i = 1 ; $i < PLAYERS_N ; $i++) {
245 $index_next = ($table->gstart + $i) % PLAYERS_N;
246 if ($table->asta_pla[$index_next]) {
247 log_wr($sess,"GSTART 1");
248 $table->gstart += $i;
254 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
255 $user_cur = &$bri->user[$table->player[$i]];
256 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
257 if ($user_cur->table_pos == ($table->gstart % PLAYERS_N))
258 $ret .= sprintf('dispose_asta(%d,%d); remark_on();',
259 $table->asta_card + 1, $table->asta_pnt+1);
261 $ret .= sprintf('dispose_asta(%d,%d); remark_off();',
262 $table->asta_card + 1, -($table->asta_pnt+1));
263 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
267 else if ($table->asta_pla_n == 0) {
268 log_wr($sess, "MOLLANO TUTTI!");
270 log_wr($sess, sprintf("GIOCO FINITO !!!"));
275 $table->game_init(&$bri);
277 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
278 $user_cur = &$bri->user[$table->player[$i]];
280 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
281 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
282 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
287 log_wr($sess, "FINITA !");
288 // if a_pnt == 120 supergame ! else abbandono
289 if ($a_pnt == 120 || $user->asta_card != -1) {
290 $chooser = $index_cur;
291 for ($i = 1 ; $i < PLAYERS_N ; $i++)
293 $table->asta_pla[$i] = FALSE;
296 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; dispose_asta(".($table->asta_card + 1).",".-($table->asta_pnt)."); remark_off();";
298 for ($i = 1 ; $i < PLAYERS_N ; $i++) {
299 $chooser = ($table->gstart + $i) % PLAYERS_N;
300 if ($table->asta_pla[$chooser]) {
305 $table->asta_win = $chooser;
307 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
308 $user_cur = &$bri->user[$table->player[$i]];
309 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
311 if ($i == $chooser) {
312 $ret .= "choose_seed(". $table->asta_card."); \$(\"asta\").style.visibility = \"hidden\"; remark_on();";
315 $ret .= "remark_off();";
318 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
325 log_wr($sess, "NON CI SIAMO");
329 else if ($argz[0] == 'choose') {
330 if ($table->asta_win > -1 &&
331 $user->table_pos == $table->asta_win) {
332 $a_brisco = $argz[1];
333 if ($a_brisco >= 0 && $a_brisco < 40) {
334 $table->briscola = $a_brisco;
335 $table->friend = $table->card[$a_brisco]->owner;
336 log_wr($sess,"GSTART 2");
337 $table->gstart = ($table->mazzo+1) % PLAYERS_N;
338 log_wr($sess, "Setta la briscola a ".$a_brisco);
340 $chooser = $table->asta_win;
341 $user_chooser = &$bri->user[$table->player[$chooser]];
342 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
343 $user_cur = &$bri->user[$table->player[$i]];
344 $user_cur->subst = 'game';
345 $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
348 /* bg of caller cell */
349 $ret .= briscola_show($bri, $table, $user_cur);
352 if ($i == ($table->gstart % PLAYERS_N))
353 $ret .= "is_my_time = true; remark_on();";
355 $ret .= "is_my_time = false; remark_off();";
357 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
361 TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
362 VAR PER PASSARE ALLA FASE DI GIOCO
369 else if ($user->stat == 'table' && $user->subst == 'game') {
370 $table = &$bri->table[$user->table];
374 log_wr($sess, "state: table::game".$argz[0]);
376 if ($argz[0] == 'logout') {
377 $bri->room_wakeup(&$user);
379 else if ($argz[0] == 'tableinfo') {
380 log_wr($sess, "PER DI TABLEINFO");
381 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
382 $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
384 log_wr($sess, $user->comm[$user->step % COMM_N]);
389 else if ($argz[0] == 'chatt') {
390 $bri->chatt_send(&$user,$mesg);
392 else if ($argz[0] == 'play') {
397 if (strpos($a_x, "px") != FALSE)
398 $a_x = substr($a_x,0,-2);
399 if (strpos($a_y, "px") != FALSE)
400 $a_y = substr($a_y,0,-2);
402 $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
403 $a_play, $user->table_pos, ($table->gstart % PLAYERS_N),
404 $table->mazzo, $table->gstart,
405 $table->card[$a_play]->stat, $table->card[$a_play]->owner);
406 log_wr($sess, "CIC".$loggo);
408 /* se era il suo turno e la carta era sua ed era in mano */
409 if ($a_play >=0 && $a_play < 40 &&
410 ($user->table_pos == (($table->gstart + $table->turn) % PLAYERS_N)) &&
411 $table->card[$a_play]->stat == 'hand' &&
412 $table->card[$a_play]->owner == $user->table_pos) {
413 log_wr($sess, sprintf("User: %s Play: %d",$user->name, $a_play));
415 /* Change the card status. */
416 $table->card[$a_play]->play($a_x, $a_y);
419 * !!!! TURN INCREMENTED BEFORE !!!!
421 $turn_cur = ($table->gstart + $table->turn) % PLAYERS_N;
424 $card_play = sprintf("card_play(%d,%d,%d,%d);|",
425 $user->table_pos, $a_play, $a_x, $a_y);
426 if (($table->turn % PLAYERS_N) != 0) { /* manche not finished */
427 $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
429 $player_cur = "remark_off();";
430 $player_nex = $card_play . "is_my_time = true; remark_on();";
431 $player_oth = $card_play;
433 else if ($table->turn <= (PLAYERS_N * 8)) { /* manche finished */
434 $winner = calculate_winner($table);
435 log_wr($sess,"GSTART 3");
436 $table->gstart = $winner;
437 $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
439 log_wr($sess, sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
440 $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|cards_hidetake($d);",
442 $player_cur = "remark_off();" . $card_take . "|";
443 if ($turn_cur != $turn_nex)
444 $player_nex = $card_play . $card_take . "|";
447 if ($table->turn < (PLAYERS_N * 8)) /* game NOT finished */
448 $player_nex .= "is_my_time = true; remark_on();";
449 $player_oth = $card_play . $card_take;
452 log_wr($sess, sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
453 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
454 $user_cur = &$bri->user[$table->player[$i]];
456 $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
459 if ($i == $turn_cur) {
462 if ($i == $turn_nex) {
465 if ($i != $turn_cur && $i != $turn_nex) {
475 if ($table->turn == (PLAYERS_N * 8)) { /* game finished */
476 log_wr($sess, sprintf("GIOCO FINITO !!!"));
478 /* ************************************************ */
479 /* PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO */
480 /* ************************************************ */
481 calculate_points(&$table);
484 $table->game_init(&$bri);
486 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
487 $user_cur = &$bri->user[$table->player[$i]];
488 $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
493 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
494 $user_cur = &$bri->user[$table->player[$i]];
496 $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
500 log_wr($sess, sprintf("TURN: %d",$table->turn));
501 /* Have played all the players ? */
502 /* NO: switch the focus and enable the next player to play. */
504 /* YES: calculate who win and go to the next turn. */
508 log_wr($sess, "NOSENSE");
511 log_wr($sess, "before save data");