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] == 'logout') {
64 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
65 $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
66 $user->the_end = TRUE;
69 else if ($argz[0] == 'chatt') {
70 $bri->chatt_send(&$user,$mesg);
72 else if ($argz[0] == 'sitdown') {
73 if ($user->stat != 'room' || $user->subst != 'standup') {
74 log_wr($sess, "Warning ! sitdown out fsm");
80 $table_idx = $argz[1];
81 $table = &$bri->table[$table_idx];
83 if ($table->player_n == PLAYERS_N) {
84 log_wr($sess, "Warning ! unreachable, table full.");
90 $user->subst = "sitdown";
91 $user->table = $table_idx;
92 $user->table_pos = $table->user_add($idx);
94 if ($table->player_n == PLAYERS_N) {
95 // Start game for this table.
96 log_wr($sess, "Start game!");
99 $table->game_init(&$bri);
101 for ($i = 0 ; $i < $table->player_n ; $i++) {
102 $user_cur = &$bri->user[$table->player[$i]];
103 log_wr($sess, "Pre if!");
106 $ret .= sprintf('gst.st_loc++; gst.st=%d; the_end=true; window.onunload = null ; document.location.assign("table.php");|', $user_cur->step+1);
108 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
109 $user_cur->trans_step = $user_cur->step + 1;
110 log_wr($sess, "TRANS ATTIVATO");
112 $user_cur->stat = 'table';
113 $user_cur->subst = 'asta';
116 $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, FALSE);
121 $bri->room_sitdown(&$user, $table_idx);
123 else if ($argz[0] == 'wakeup') {
124 if ($user->stat != 'room' || $user->subst != 'sitdown') {
125 log_wr($sess, "Warning ! wakeup out fsm.");
132 $user->subst = "standup";
134 $bri->room_wakeup(&$user);
142 else if ($user->stat == 'table' && $user->subst == 'asta') {
143 $table = &$bri->table[$user->table];
145 if ($argz[0] == 'logout') {
147 // document.location.assign("index.php");
149 $bri->room_wakeup(&$user);
151 else if ($argz[0] == 'tableinfo') {
152 log_wr($sess, "PER DI TABLEINFO");
153 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
154 $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
156 log_wr($sess, $user->comm[$user->step % COMM_N]);
161 else if ($argz[0] == 'chatt') {
162 $bri->chatt_send(&$user,$mesg);
164 else if ($argz[0] == 'asta') {
167 $index_cur = $table->gstart % PLAYERS_N;
168 if ($user->table_pos == $index_cur &&
169 $table->asta_pla[$index_cur]) {
173 log_wr($sess, "CI SIAMO a_card ".$a_card." asta_card ".$table->asta_card);
175 // Abbandono dell'asta
177 log_wr($sess, "Abbandona l'asta.");
178 $table->asta_pla[$index_cur] = FALSE;
179 $user->asta_card = -1;
180 $table->asta_pla_n--;
183 else if ($a_card <= 9) {
184 if ($table->asta_card == 9) {
185 if ($a_card == 9 && $a_pnt <= 120 && $a_pnt > $table->asta_pnt)
189 if ($a_card >= 0 && $a_card <= 9 && $a_card > $table->asta_card)
193 if ($again == FALSE) {
194 log_wr($sess, "NUOVI ORZI.");
195 $user->asta_card = $a_card;
196 $table->asta_card = $a_card;
198 $user->asta_pnt = $a_pnt;
199 $table->asta_pnt = $a_pnt;
206 if ($again) { // Qualcosa non andato bene, rifare
207 log_wr($sess, "Ripetere.");
211 $showst = "show_astat(";
212 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
213 $user_cur = &$bri->user[$table->player[$i]];
214 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
215 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
222 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
223 $user_cur = &$bri->user[$table->player[$i]];
224 if ($maxcard < $user_cur->asta_card)
225 $maxcard = $user_cur->asta_card;
228 if ($table->asta_pla_n > ($maxcard > -1 ? 1 : 0) &&
229 !($table->asta_card == 9 && $table->asta_pnt == 120)) {
230 for ($i = 1 ; $i < PLAYERS_N ; $i++) {
231 $index_next = ($table->gstart + $i) % PLAYERS_N;
232 if ($table->asta_pla[$index_next]) {
233 log_wr($sess,"GSTART 1");
234 $table->gstart += $i;
240 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
241 $user_cur = &$bri->user[$table->player[$i]];
242 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
243 if ($user_cur->table_pos == ($table->gstart % PLAYERS_N))
244 $ret .= sprintf('dispose_asta(%d,%d); remark_on();',
245 $table->asta_card + 1, $table->asta_pnt+1);
247 $ret .= sprintf('dispose_asta(%d,%d); remark_off();',
248 $table->asta_card + 1, -($table->asta_pnt+1));
249 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
253 else if ($table->asta_pla_n == 0) {
254 log_wr($sess, "MOLLANO TUTTI!");
256 log_wr($sess, sprintf("GIOCO FINITO !!!"));
261 $table->game_init(&$bri);
263 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
264 $user_cur = &$bri->user[$table->player[$i]];
266 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
267 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
268 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
273 log_wr($sess, "FINITA !");
274 // if a_pnt == 120 supergame ! else abbandono
276 $chooser = $index_cur;
277 for ($i = 1 ; $i < PLAYERS_N ; $i++)
279 $table->asta_pla[$i] = FALSE;
282 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; dispose_asta(".($table->asta_card + 1).",".-($table->asta_pnt)."); remark_off();";
284 for ($i = 1 ; $i < PLAYERS_N ; $i++) {
285 $chooser = ($table->gstart + $i) % PLAYERS_N;
286 if ($table->asta_pla[$chooser]) {
291 $table->asta_win = $chooser;
293 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
294 $user_cur = &$bri->user[$table->player[$i]];
295 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
297 if ($i == $chooser) {
298 $ret .= "choose_seed(". $table->asta_card."); \$(\"asta\").style.visibility = \"hidden\"; remark_on();";
301 $ret .= "remark_off();";
304 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
311 log_wr($sess, "NON CI SIAMO");
315 else if ($argz[0] == 'choose') {
316 if ($table->asta_win > -1 &&
317 $user->table_pos == $table->asta_win) {
318 $a_brisco = $argz[1];
319 if ($a_brisco >= 0 && $a_brisco < 40) {
320 $table->briscola = $a_brisco;
321 $table->friend = $table->card[$a_brisco]->owner;
322 log_wr($sess,"GSTART 2");
323 $table->gstart = ($table->mazzo+1) % PLAYERS_N;
324 log_wr($sess, "Setta la briscola a ".$a_brisco);
326 $chooser = $table->asta_win;
327 $user_chooser = &$bri->user[$table->player[$chooser]];
328 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
329 $user_cur = &$bri->user[$table->player[$i]];
330 $user_cur->subst = 'game';
331 $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
334 /* bg of caller cell */
335 $ret .= briscola_show($bri, $table, $user_cur);
338 if ($i == ($table->gstart % PLAYERS_N))
339 $ret .= "is_my_time = true; remark_on();";
341 $ret .= "is_my_time = false; remark_off();";
343 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
347 TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
348 VAR PER PASSARE ALLA FASE DI GIOCO
355 else if ($user->stat == 'table' && $user->subst == 'game') {
356 $table = &$bri->table[$user->table];
360 log_wr($sess, "state: table::game".$argz[0]);
362 if ($argz[0] == 'logout') {
363 $bri->room_wakeup(&$user);
365 else if ($argz[0] == 'tableinfo') {
366 log_wr($sess, "PER DI TABLEINFO");
367 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
368 $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
370 log_wr($sess, $user->comm[$user->step % COMM_N]);
375 else if ($argz[0] == 'chatt') {
376 $bri->chatt_send(&$user,$mesg);
378 else if ($argz[0] == 'play') {
383 if (strpos($a_x, "px") != FALSE)
384 $a_x = substr($a_x,0,-2);
385 if (strpos($a_y, "px") != FALSE)
386 $a_y = substr($a_y,0,-2);
388 $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
389 $a_play, $user->table_pos, ($table->gstart % PLAYERS_N),
390 $table->mazzo, $table->gstart,
391 $table->card[$a_play]->stat, $table->card[$a_play]->owner);
392 log_wr($sess, "CIC".$loggo);
394 /* se era il suo turno e la carta era sua ed era in mano */
395 if ($a_play >=0 && $a_play < 40 &&
396 ($user->table_pos == (($table->gstart + $table->turn) % PLAYERS_N)) &&
397 $table->card[$a_play]->stat == 'hand' &&
398 $table->card[$a_play]->owner == $user->table_pos) {
399 log_wr($sess, sprintf("User: %s Play: %d",$user->name, $a_play));
401 /* Change the card status. */
402 $table->card[$a_play]->play($a_x, $a_y);
405 * !!!! TURN INCREMENTED BEFORE !!!!
407 $turn_cur = ($table->gstart + $table->turn) % PLAYERS_N;
410 $card_play = sprintf("card_play(%d,%d,%d,%d);|",
411 $user->table_pos, $a_play, $a_x, $a_y);
412 if (($table->turn % PLAYERS_N) != 0) { /* manche not finished */
413 $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
415 $player_cur = "remark_off();";
416 $player_nex = $card_play . "is_my_time = true; remark_on();";
417 $player_oth = $card_play;
419 else if ($table->turn <= (PLAYERS_N * 8)) { /* manche finished */
420 $winner = calculate_winner($table);
421 log_wr($sess,"GSTART 3");
422 $table->gstart = $winner;
423 $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
425 log_wr($sess, sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
426 $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|cards_hidetake($d);",
428 $player_cur = "remark_off();" . $card_take . "|";
429 if ($turn_cur != $turn_nex)
430 $player_nex = $card_play . $card_take . "|";
433 if ($table->turn < (PLAYERS_N * 8)) /* game NOT finished */
434 $player_nex .= "is_my_time = true; remark_on();";
435 $player_oth = $card_play . $card_take;
438 log_wr($sess, sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
439 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
440 $user_cur = &$bri->user[$table->player[$i]];
442 $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
445 if ($i == $turn_cur) {
448 if ($i == $turn_nex) {
451 if ($i != $turn_cur && $i != $turn_nex) {
461 if ($table->turn == (PLAYERS_N * 8)) { /* game finished */
462 log_wr($sess, sprintf("GIOCO FINITO !!!"));
464 /* ************************************************ */
465 /* PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO */
466 /* ************************************************ */
467 calculate_points(&$table);
470 $table->game_init(&$bri);
472 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
473 $user_cur = &$bri->user[$table->player[$i]];
474 $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
479 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
480 $user_cur = &$bri->user[$table->player[$i]];
482 $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
486 log_wr($sess, sprintf("TURN: %d",$table->turn));
487 /* Have played all the players ? */
488 /* NO: switch the focus and enable the next player to play. */
490 /* YES: calculate who win and go to the next turn. */
494 log_wr($sess, "NOSENSE");
497 log_wr($sess, "before save data");