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->player_n;
93 $table->player[$table->player_n] = $idx;
96 if ($table->player_n == PLAYERS_N) {
97 // Start game for this table.
98 log_wr($sess, "Start game!");
101 $table->game_init(&$bri);
103 for ($i = 0 ; $i < $table->player_n ; $i++) {
104 $user_cur = &$bri->user[$table->player[$i]];
105 log_wr($sess, "Pre if!");
108 $ret .= sprintf('gst.st_loc++; gst.st=%d; the_end=true; window.onunload = null ; document.location.assign("table.php");|', $user_cur->step+1);
110 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
111 $user_cur->trans_step = $user_cur->step + 1;
112 log_wr($sess, "TRANS ATTIVATO");
114 $user_cur->stat = 'table';
115 $user_cur->subst = 'asta';
118 $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, FALSE);
123 $bri->room_sitdown(&$user, $table_idx);
125 else if ($argz[0] == 'wakeup') {
126 if ($user->stat != 'room' || $user->subst != 'sitdown') {
127 log_wr($sess, "Warning ! wakeup out fsm.");
134 $user->subst = "standup";
136 $bri->room_wakeup(&$user);
144 else if ($user->stat == 'table' && $user->subst == 'asta') {
145 $table = &$bri->table[$user->table];
147 if ($argz[0] == 'logout') {
149 // document.location.assign("index.php");
151 $bri->room_wakeup(&$user);
153 else if ($argz[0] == 'tableinfo') {
154 log_wr($sess, "PER DI TABLEINFO");
155 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
156 $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
158 log_wr($sess, $user->comm[$user->step % COMM_N]);
163 else if ($argz[0] == 'chatt') {
164 $bri->chatt_send(&$user,$mesg);
166 else if ($argz[0] == 'asta') {
169 $index_cur = $table->gstart % PLAYERS_N;
170 if ($user->table_pos == $index_cur &&
171 $table->asta_pla[$index_cur]) {
175 log_wr($sess, "CI SIAMO a_card ".$a_card." asta_card ".$table->asta_card);
177 // Abbandono dell'asta
179 log_wr($sess, "Abbandona l'asta.");
180 $table->asta_pla[$index_cur] = FALSE;
181 $user->asta_card = -1;
182 $table->asta_pla_n--;
185 else if ($a_card <= 9) {
186 if ($table->asta_card == 9) {
187 if ($a_card == 9 && $a_pnt <= 120 && $a_pnt > $table->asta_pnt)
191 if ($a_card >= 0 && $a_card <= 9 && $a_card > $table->asta_card)
195 if ($again == FALSE) {
196 log_wr($sess, "NUOVI ORZI.");
197 $user->asta_card = $a_card;
198 $table->asta_card = $a_card;
200 $user->asta_pnt = $a_pnt;
201 $table->asta_pnt = $a_pnt;
208 if ($again) { // Qualcosa non andato bene, rifare
209 log_wr($sess, "Ripetere.");
213 $showst = "show_astat(";
214 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
215 $user_cur = &$bri->user[$table->player[$i]];
216 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
217 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
224 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
225 $user_cur = &$bri->user[$table->player[$i]];
226 if ($maxcard < $user_cur->asta_card)
227 $maxcard = $user_cur->asta_card;
230 if ($table->asta_pla_n > ($maxcard > -1 ? 1 : 0) &&
231 !($table->asta_card == 9 && $table->asta_pnt == 120)) {
232 for ($i = 1 ; $i < PLAYERS_N ; $i++) {
233 $index_next = ($table->gstart + $i) % PLAYERS_N;
234 if ($table->asta_pla[$index_next]) {
235 log_wr($sess,"GSTART 1");
236 $table->gstart += $i;
242 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
243 $user_cur = &$bri->user[$table->player[$i]];
244 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
245 if ($user_cur->table_pos == ($table->gstart % PLAYERS_N))
246 $ret .= sprintf('dispose_asta(%d,%d); remark_on();',
247 $table->asta_card + 1, $table->asta_pnt+1);
249 $ret .= sprintf('dispose_asta(%d,%d); remark_off();',
250 $table->asta_card + 1, -($table->asta_pnt+1));
251 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
255 else if ($table->asta_pla_n == 0) {
256 log_wr($sess, "MOLLANO TUTTI!");
258 log_wr($sess, sprintf("GIOCO FINITO !!!"));
263 $table->game_init(&$bri);
265 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
266 $user_cur = &$bri->user[$table->player[$i]];
268 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
269 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
270 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
275 log_wr($sess, "FINITA !");
276 // if a_pnt == 120 supergame ! else abbandono
278 $chooser = $index_cur;
279 for ($i = 1 ; $i < PLAYERS_N ; $i++)
281 $table->asta_pla[$i] = FALSE;
284 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; dispose_asta(".($table->asta_card + 1).",".-($table->asta_pnt)."); remark_off();";
286 for ($i = 1 ; $i < PLAYERS_N ; $i++) {
287 $chooser = ($table->gstart + $i) % PLAYERS_N;
288 if ($table->asta_pla[$chooser]) {
293 $table->asta_win = $chooser;
295 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
296 $user_cur = &$bri->user[$table->player[$i]];
297 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
299 if ($i == $chooser) {
300 $ret .= "choose_seed(". $table->asta_card."); \$(\"asta\").style.visibility = \"hidden\"; remark_on();";
303 $ret .= "remark_off();";
306 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
313 log_wr($sess, "NON CI SIAMO");
317 else if ($argz[0] == 'choose') {
318 if ($table->asta_win > -1 &&
319 $user->table_pos == $table->asta_win) {
320 $a_brisco = $argz[1];
321 if ($a_brisco >= 0 && $a_brisco < 40) {
322 $table->briscola = $a_brisco;
323 $table->friend = $table->card[$a_brisco]->owner;
324 log_wr($sess,"GSTART 2");
325 $table->gstart = ($table->mazzo+1) % PLAYERS_N;
326 log_wr($sess, "Setta la briscola a ".$a_brisco);
328 $chooser = $table->asta_win;
329 $user_chooser = &$bri->user[$table->player[$chooser]];
330 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
331 $user_cur = &$bri->user[$table->player[$i]];
332 $user_cur->subst = 'game';
333 $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
336 /* bg of caller cell */
337 $ret .= briscola_show($bri, $table, $user_cur);
340 if ($i == ($table->gstart % PLAYERS_N))
341 $ret .= "is_my_time = true; remark_on();";
343 $ret .= "is_my_time = false; remark_off();";
345 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
349 TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
350 VAR PER PASSARE ALLA FASE DI GIOCO
357 else if ($user->stat == 'table' && $user->subst == 'game') {
358 $table = &$bri->table[$user->table];
362 log_wr($sess, "state: table::game".$argz[0]);
364 if ($argz[0] == 'logout') {
365 $bri->room_wakeup(&$user);
367 else if ($argz[0] == 'tableinfo') {
368 log_wr($sess, "PER DI TABLEINFO");
369 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
370 $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
372 log_wr($sess, $user->comm[$user->step % COMM_N]);
377 else if ($argz[0] == 'chatt') {
378 $bri->chatt_send(&$user,$mesg);
380 else if ($argz[0] == 'play') {
385 if (strpos($a_x, "px") != FALSE)
386 $a_x = substr($a_x,0,-2);
387 if (strpos($a_y, "px") != FALSE)
388 $a_y = substr($a_y,0,-2);
390 $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
391 $a_play, $user->table_pos, ($table->gstart % PLAYERS_N),
392 $table->mazzo, $table->gstart,
393 $table->card[$a_play]->stat, $table->card[$a_play]->owner);
394 log_wr($sess, "CIC".$loggo);
396 /* se era il suo turno e la carta era sua ed era in mano */
397 if ($a_play >=0 && $a_play < 40 &&
398 ($user->table_pos == (($table->gstart + $table->turn) % PLAYERS_N)) &&
399 $table->card[$a_play]->stat == 'hand' &&
400 $table->card[$a_play]->owner == $user->table_pos) {
401 log_wr($sess, sprintf("User: %s Play: %d",$user->name, $a_play));
403 /* Change the card status. */
404 $table->card[$a_play]->play($a_x, $a_y);
407 * !!!! TURN INCREMENTED BEFORE !!!!
409 $turn_cur = ($table->gstart + $table->turn) % PLAYERS_N;
412 $card_play = sprintf("card_play(%d,%d,%d,%d);|",
413 $user->table_pos, $a_play, $a_x, $a_y);
414 if (($table->turn % PLAYERS_N) != 0) { /* manche not finished */
415 $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
417 $player_cur = "remark_off();";
418 $player_nex = $card_play . "is_my_time = true; remark_on();";
419 $player_oth = $card_play;
421 else if ($table->turn <= (PLAYERS_N * 8)) { /* manche finished */
422 $winner = calculate_winner($table);
423 log_wr($sess,"GSTART 3");
424 $table->gstart = $winner;
425 $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
427 log_wr($sess, sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
428 $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|cards_hidetake($d);",
430 $player_cur = "remark_off();" . $card_take . "|";
431 if ($turn_cur != $turn_nex)
432 $player_nex = $card_play . $card_take . "|";
435 if ($table->turn < (PLAYERS_N * 8)) /* game NOT finished */
436 $player_nex .= "is_my_time = true; remark_on();";
437 $player_oth = $card_play . $card_take;
440 log_wr($sess, sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
441 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
442 $user_cur = &$bri->user[$table->player[$i]];
444 $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
447 if ($i == $turn_cur) {
450 if ($i == $turn_nex) {
453 if ($i != $turn_cur && $i != $turn_nex) {
463 if ($table->turn == (PLAYERS_N * 8)) { /* game finished */
464 log_wr($sess, sprintf("GIOCO FINITO !!!"));
466 /* ************************************************ */
467 /* PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO */
468 /* ************************************************ */
469 calculate_points(&$table);
472 $table->game_init(&$bri);
474 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
475 $user_cur = &$bri->user[$table->player[$i]];
476 $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
481 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
482 $user_cur = &$bri->user[$table->player[$i]];
484 $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
488 log_wr($sess, sprintf("TURN: %d",$table->turn));
489 /* Have played all the players ? */
490 /* NO: switch the focus and enable the next player to play. */
492 /* YES: calculate who win and go to the next turn. */
496 log_wr($sess, "NOSENSE");
499 log_wr($sess, "before save data");