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 $user->laccwr = time();
65 if ($argz[0] == 'help') {
66 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
67 $user->comm[$user->step % COMM_N] .= show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
69 log_wr($sess, $user->comm[$user->step % COMM_N]);
73 else if ($argz[0] == 'about') {
74 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
75 $user->comm[$user->step % COMM_N] .= show_notify(str_replace("\n", " ", $G_room_about), 0, "torna ai tavoli", 400, 200);
77 log_wr($sess, $user->comm[$user->step % COMM_N]);
81 else if ($argz[0] == 'logout') {
82 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
83 $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
84 $user->the_end = TRUE;
87 else if ($argz[0] == 'chatt') {
88 $bri->chatt_send(&$user,$mesg);
90 else if ($argz[0] == 'sitdown') {
91 if ($user->stat != 'room' || $user->subst != 'standup') {
92 log_wr($sess, "Warning ! sitdown out fsm");
98 $table_idx = $argz[1];
99 $table = &$bri->table[$table_idx];
101 if ($table->player_n == PLAYERS_N) {
102 log_wr($sess, "Warning ! unreachable, table full.");
108 $user->subst = "sitdown";
109 $user->table = $table_idx;
110 $user->table_pos = $table->user_add($idx);
112 if ($table->player_n == PLAYERS_N) {
113 // Start game for this table.
114 log_wr($sess, "Start game!");
117 $table->game_init(&$bri);
119 for ($i = 0 ; $i < $table->player_n ; $i++) {
120 $user_cur = &$bri->user[$table->player[$i]];
121 log_wr($sess, "Pre if!");
124 $ret .= sprintf('gst.st_loc++; gst.st=%d; the_end=true; window.onunload = null ; document.location.assign("table.php");|', $user_cur->step+1);
126 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
127 $user_cur->trans_step = $user_cur->step + 1;
128 log_wr($sess, "TRANS ATTIVATO");
130 $user_cur->stat = 'table';
131 $user_cur->subst = 'asta';
134 $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, FALSE);
139 $bri->room_sitdown(&$user, $table_idx);
141 else if ($argz[0] == 'wakeup') {
142 if ($user->stat != 'room' || $user->subst != 'sitdown') {
143 log_wr($sess, "Warning ! wakeup out fsm.");
150 $user->subst = "standup";
152 $bri->room_wakeup(&$user);
160 else if ($user->stat == 'table' && $user->subst == 'asta') {
161 $user->laccwr = time();
162 $table = &$bri->table[$user->table];
164 if ($argz[0] == 'logout') {
166 // document.location.assign("index.php");
168 $bri->room_wakeup(&$user);
170 else if ($argz[0] == 'tableinfo') {
171 log_wr($sess, "PER DI TABLEINFO");
172 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
173 $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
174 log_wr($sess, $user->comm[$user->step % COMM_N]);
177 else if ($argz[0] == 'chatt') {
178 $bri->chatt_send(&$user,$mesg);
180 else if ($argz[0] == 'asta') {
183 $index_cur = $table->gstart % PLAYERS_N;
184 if ($user->table_pos == $index_cur &&
185 $table->asta_pla[$index_cur]) {
189 log_wr($sess, "CI SIAMO a_card ".$a_card." asta_card ".$table->asta_card);
191 // Abbandono dell'asta
193 log_wr($sess, "Abbandona l'asta.");
194 $table->asta_pla[$index_cur] = FALSE;
195 $user->asta_card = -1;
196 $table->asta_pla_n--;
199 else if ($a_card <= 9) {
200 if ($table->asta_card == 9) {
201 if ($a_card == 9 && $a_pnt <= 120 && $a_pnt > $table->asta_pnt)
205 if ($a_card >= 0 && $a_card <= 9 && $a_card > $table->asta_card)
209 if ($again == FALSE) {
210 log_wr($sess, "NUOVI ORZI.");
211 $user->asta_card = $a_card;
212 $table->asta_card = $a_card;
214 $user->asta_pnt = $a_pnt;
215 $table->asta_pnt = $a_pnt;
222 if ($again) { // Qualcosa non andato bene, rifare
223 log_wr($sess, "Ripetere.");
227 $showst = "show_astat(";
228 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
229 $user_cur = &$bri->user[$table->player[$i]];
230 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
231 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
238 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
239 $user_cur = &$bri->user[$table->player[$i]];
240 if ($maxcard < $user_cur->asta_card)
241 $maxcard = $user_cur->asta_card;
244 if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
245 !($table->asta_card == 9 && $table->asta_pnt == 120)) {
246 log_wr($sess,"ALLOPPA QUI");
247 for ($i = 1 ; $i < PLAYERS_N ; $i++) {
248 $index_next = ($table->gstart + $i) % PLAYERS_N;
249 if ($table->asta_pla[$index_next]) {
250 log_wr($sess,"GSTART 1");
251 $table->gstart += $i;
257 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
258 $user_cur = &$bri->user[$table->player[$i]];
259 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
260 if ($user_cur->table_pos == ($table->gstart % PLAYERS_N))
261 $ret .= sprintf('dispose_asta(%d,%d); remark_on();',
262 $table->asta_card + 1, $table->asta_pnt+1);
264 $ret .= sprintf('dispose_asta(%d,%d); remark_off();',
265 $table->asta_card + 1, -($table->asta_pnt+1));
266 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
270 else if ($table->asta_pla_n == 0) {
271 log_wr($sess, "MOLLANO TUTTI!");
273 log_wr($sess, sprintf("GIOCO FINITO !!!"));
278 $table->game_init(&$bri);
280 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
281 $user_cur = &$bri->user[$table->player[$i]];
283 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
284 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
285 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
290 log_wr($sess, "FINITA !");
291 // if a_pnt == 120 supergame ! else abbandono
292 if ($a_pnt == 120 || $user->asta_card != -1) {
293 $chooser = $index_cur;
294 for ($i = 1 ; $i < PLAYERS_N ; $i++)
296 $table->asta_pla[$i] = FALSE;
299 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; dispose_asta(".($table->asta_card + 1).",".-($table->asta_pnt)."); remark_off();";
301 for ($i = 1 ; $i < PLAYERS_N ; $i++) {
302 $chooser = ($table->gstart + $i) % PLAYERS_N;
303 if ($table->asta_pla[$chooser]) {
308 $table->asta_win = $chooser;
310 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
311 $user_cur = &$bri->user[$table->player[$i]];
312 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
314 if ($i == $chooser) {
315 $ret .= "choose_seed(". $table->asta_card."); \$(\"asta\").style.visibility = \"hidden\"; remark_on();";
318 $ret .= "remark_off();";
321 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
328 log_wr($sess, "NON CI SIAMO");
332 else if ($argz[0] == 'choose') {
333 if ($table->asta_win > -1 &&
334 $user->table_pos == $table->asta_win) {
335 $a_brisco = $argz[1];
336 if ($a_brisco >= 0 && $a_brisco < 40) {
337 $table->briscola = $a_brisco;
338 $table->friend = $table->card[$a_brisco]->owner;
339 log_wr($sess,"GSTART 2");
340 $table->gstart = ($table->mazzo+1) % PLAYERS_N;
341 log_wr($sess, "Setta la briscola a ".$a_brisco);
343 $chooser = $table->asta_win;
344 $user_chooser = &$bri->user[$table->player[$chooser]];
345 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
346 $user_cur = &$bri->user[$table->player[$i]];
347 $user_cur->subst = 'game';
348 $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
351 /* bg of caller cell */
352 $ret .= briscola_show($bri, $table, $user_cur);
355 if ($i == ($table->gstart % PLAYERS_N))
356 $ret .= "is_my_time = true; remark_on();";
358 $ret .= "is_my_time = false; remark_off();";
360 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
364 TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
365 VAR PER PASSARE ALLA FASE DI GIOCO
372 else if ($user->stat == 'table' && $user->subst == 'game') {
373 $table = &$bri->table[$user->table];
377 log_wr($sess, "state: table::game".$argz[0]);
379 if ($argz[0] == 'logout') {
380 $bri->room_wakeup(&$user);
382 else if ($argz[0] == 'tableinfo') {
383 log_wr($sess, "PER DI TABLEINFO");
384 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
385 $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
387 log_wr($sess, $user->comm[$user->step % COMM_N]);
392 else if ($argz[0] == 'chatt') {
393 $bri->chatt_send(&$user,$mesg);
395 else if ($argz[0] == 'play') {
400 if (strpos($a_x, "px") != FALSE)
401 $a_x = substr($a_x,0,-2);
402 if (strpos($a_y, "px") != FALSE)
403 $a_y = substr($a_y,0,-2);
405 $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
406 $a_play, $user->table_pos, ($table->gstart % PLAYERS_N),
407 $table->mazzo, $table->gstart,
408 $table->card[$a_play]->stat, $table->card[$a_play]->owner);
409 log_wr($sess, "CIC".$loggo);
411 /* se era il suo turno e la carta era sua ed era in mano */
412 if ($a_play >=0 && $a_play < 40 &&
413 ($user->table_pos == (($table->gstart + $table->turn) % PLAYERS_N)) &&
414 $table->card[$a_play]->stat == 'hand' &&
415 $table->card[$a_play]->owner == $user->table_pos) {
416 log_wr($sess, sprintf("User: %s Play: %d",$user->name, $a_play));
418 /* Change the card status. */
419 $table->card[$a_play]->play($a_x, $a_y);
422 * !!!! TURN INCREMENTED BEFORE !!!!
424 $turn_cur = ($table->gstart + $table->turn) % PLAYERS_N;
427 $card_play = sprintf("card_play(%d,%d,%d,%d);|",
428 $user->table_pos, $a_play, $a_x, $a_y);
429 if (($table->turn % PLAYERS_N) != 0) { /* manche not finished */
430 $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
432 $player_cur = "remark_off();";
433 $player_nex = $card_play . "is_my_time = true; remark_on();";
434 $player_oth = $card_play;
436 else if ($table->turn <= (PLAYERS_N * 8)) { /* manche finished */
437 $winner = calculate_winner($table);
438 log_wr($sess,"GSTART 3");
439 $table->gstart = $winner;
440 $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
442 log_wr($sess, sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
443 $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|cards_hidetake($d);",
445 $player_cur = "remark_off();" . $card_take . "|";
446 if ($turn_cur != $turn_nex)
447 $player_nex = $card_play . $card_take . "|";
450 if ($table->turn < (PLAYERS_N * 8)) /* game NOT finished */
451 $player_nex .= "is_my_time = true; remark_on();";
452 $player_oth = $card_play . $card_take;
455 log_wr($sess, sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
456 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
457 $user_cur = &$bri->user[$table->player[$i]];
459 $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
462 if ($i == $turn_cur) {
465 if ($i == $turn_nex) {
468 if ($i != $turn_cur && $i != $turn_nex) {
478 if ($table->turn == (PLAYERS_N * 8)) { /* game finished */
479 log_wr($sess, sprintf("GIOCO FINITO !!!"));
481 /* ************************************************ */
482 /* PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO */
483 /* ************************************************ */
484 calculate_points(&$table);
487 $table->game_init(&$bri);
489 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
490 $user_cur = &$bri->user[$table->player[$i]];
491 $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
496 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
497 $user_cur = &$bri->user[$table->player[$i]];
499 $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
503 log_wr($sess, sprintf("TURN: %d",$table->turn));
504 /* Have played all the players ? */
505 /* NO: switch the focus and enable the next player to play. */
507 /* YES: calculate who win and go to the next turn. */
511 log_wr($sess, "NOSENSE");
514 log_wr($sess, "before save data");