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 = &get_user($bri, $sess, &$idx)) == FALSE) {
40 echo "Get User Error";
41 log_wr($sess, "Get User Error");
45 $argz = explode('|', $mesg);
47 if ($user->stat == 'room') {
48 if ($argz[0] == 'logout') {
49 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
50 $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
51 $user->the_end = TRUE;
54 else if ($argz[0] == 'chatt') {
55 $bri->chatt_send(&$user,$mesg);
57 else if ($argz[0] == 'sitdown') {
58 if ($user->stat != 'room' || $user->subst != 'standup') {
59 log_wr($sess, "Warning ! sitdown out fsm");
65 $table_idx = $argz[1];
66 $table = &$bri->table[$table_idx];
68 if ($table->player_n == PLAYERS_N) {
69 log_wr($sess, "Warning ! unreachable, table full.");
75 $user->subst = "sitdown";
76 $user->table = $table_idx;
77 $user->table_pos = $table->player_n;
78 $table->player[$table->player_n] = $idx;
81 if ($table->player_n == PLAYERS_N) {
82 // Start game for this table.
83 log_wr($sess, "Start game!");
86 $table->game_init(&$bri);
88 for ($i = 0 ; $i < $table->player_n ; $i++) {
89 $user_cur = &$bri->user[$table->player[$i]];
90 log_wr($sess, "Pre if!");
93 $ret .= sprintf('gst.st_loc++; gst.st=%d; the_end=true; document.location.assign("table.php");|', $user_cur->step+1);
95 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
96 $user_cur->trans_step = $user_cur->step + 1;
97 log_wr($sess, "TRANS ATTIVATO");
99 $user_cur->stat = 'table';
100 $user_cur->subst = 'asta';
103 $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, FALSE);
108 $bri->room_sitdown(&$user, $table_idx);
110 else if ($argz[0] == 'wakeup') {
111 if ($user->stat != 'room' || $user->subst != 'sitdown') {
112 log_wr($sess, "Warning ! wakeup out fsm.");
119 $user->subst = "standup";
121 $bri->room_wakeup(&$user);
129 else if ($user->stat == 'table' && $user->subst == 'asta') {
130 $table = &$bri->table[$user->table];
132 if ($argz[0] == 'logout') {
134 // document.location.assign("index.php");
136 $bri->room_wakeup(&$user);
138 else if ($argz[0] == 'tableinfo') {
139 log_wr($sess, "PER DI TABLEINFO");
140 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
141 $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
143 log_wr($sess, $user->comm[$user->step % COMM_N]);
148 else if ($argz[0] == 'chatt') {
149 $bri->chatt_send(&$user,$mesg);
151 else if ($argz[0] == 'asta') {
154 $index_cur = $table->gstart % PLAYERS_N;
155 if ($user->table_pos == $index_cur &&
156 $table->asta_pla[$index_cur]) {
160 log_wr($sess, "CI SIAMO a_card ".$a_card." asta_card ".$table->asta_card);
162 // Abbandono dell'asta
164 log_wr($sess, "Abbandona l'asta.");
165 $table->asta_pla[$index_cur] = FALSE;
166 $user->asta_card = -1;
167 $table->asta_pla_n--;
170 else if ($a_card <= 9) {
171 if ($table->asta_card == 9) {
172 if ($a_card == 9 && $a_pnt <= 120 && $a_pnt > $table->asta_pnt)
176 if ($a_card >= 0 && $a_card <= 9 && $a_card > $table->asta_card)
180 if ($again == FALSE) {
181 log_wr($sess, "NUOVI ORZI.");
182 $user->asta_card = $a_card;
183 $table->asta_card = $a_card;
185 $user->asta_pnt = $a_pnt;
186 $table->asta_pnt = $a_pnt;
193 if ($again) { // Qualcosa non andato bene, rifare
194 log_wr($sess, "Ripetere.");
198 $showst = "show_astat(";
199 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
200 $user_cur = &$bri->user[$table->player[$i]];
201 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
202 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
209 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
210 $user_cur = &$bri->user[$table->player[$i]];
211 if ($maxcard < $user_cur->asta_card)
212 $maxcard = $user_cur->asta_card;
215 if ($table->asta_pla_n > ($maxcard > -1 ? 1 : 0) &&
216 !($table->asta_card == 9 && $table->asta_pnt == 120)) {
217 for ($i = 1 ; $i < PLAYERS_N ; $i++) {
218 $index_next = ($table->gstart + $i) % PLAYERS_N;
219 if ($table->asta_pla[$index_next]) {
220 log_wr($sess,"GSTART 1");
221 $table->gstart += $i;
227 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
228 $user_cur = &$bri->user[$table->player[$i]];
229 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
230 if ($user_cur->table_pos == ($table->gstart % PLAYERS_N))
231 $ret .= sprintf('dispose_asta(%d,%d); remark_on();',
232 $table->asta_card + 1, $table->asta_pnt+1);
234 $ret .= sprintf('dispose_asta(%d,%d); remark_off();',
235 $table->asta_card + 1, -($table->asta_pnt+1));
236 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
240 else if ($table->asta_pla_n == 0) {
241 log_wr($sess, "MOLLANO TUTTI!");
243 log_wr($sess, sprintf("GIOCO FINITO !!!"));
248 $table->game_init(&$bri);
250 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
251 $user_cur = &$bri->user[$table->player[$i]];
253 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
254 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
255 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
260 log_wr($sess, "FINITA !");
261 // if a_pnt == 120 supergame ! else abbandono
263 $chooser = $index_cur;
264 for ($i = 1 ; $i < PLAYERS_N ; $i++)
266 $table->asta_pla[$i] = FALSE;
269 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; dispose_asta(".($table->asta_card + 1).",".-($table->asta_pnt)."); remark_off();";
271 for ($i = 1 ; $i < PLAYERS_N ; $i++) {
272 $chooser = ($table->gstart + $i) % PLAYERS_N;
273 if ($table->asta_pla[$chooser]) {
278 $table->asta_win = $chooser;
280 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
281 $user_cur = &$bri->user[$table->player[$i]];
282 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
284 if ($i == $chooser) {
285 $ret .= "choose_seed(". $table->asta_card."); \$(\"asta\").style.visibility = \"hidden\"; remark_on();";
288 $ret .= "remark_off();";
291 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
298 log_wr($sess, "NON CI SIAMO");
302 else if ($argz[0] == 'choose') {
303 if ($table->asta_win > -1 &&
304 $user->table_pos == $table->asta_win) {
305 $a_brisco = $argz[1];
306 if ($a_brisco >= 0 && $a_brisco < 40) {
307 $table->briscola = $a_brisco;
308 $table->friend = $table->card[$a_brisco]->owner;
309 log_wr($sess,"GSTART 2");
310 $table->gstart = ($table->mazzo+1) % PLAYERS_N;
311 log_wr($sess, "Setta la briscola a ".$a_brisco);
313 $chooser = $table->asta_win;
314 $user_chooser = &$bri->user[$table->player[$chooser]];
315 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
316 $user_cur = &$bri->user[$table->player[$i]];
317 $user_cur->subst = 'game';
318 $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
321 /* bg of caller cell */
322 $ret .= briscola_show($bri, $table, $user_cur);
325 if ($i == ($table->gstart % PLAYERS_N))
326 $ret .= "is_my_time = true; remark_on();";
328 $ret .= "is_my_time = false; remark_off();";
330 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
334 TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
335 VAR PER PASSARE ALLA FASE DI GIOCO
342 else if ($user->stat == 'table' && $user->subst == 'game') {
343 $table = &$bri->table[$user->table];
347 log_wr($sess, "state: table::game".$argz[0]);
349 if ($argz[0] == 'logout') {
350 $bri->room_wakeup(&$user);
352 else if ($argz[0] == 'tableinfo') {
353 log_wr($sess, "PER DI TABLEINFO");
354 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
355 $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
357 log_wr($sess, $user->comm[$user->step % COMM_N]);
362 else if ($argz[0] == 'chatt') {
363 $bri->chatt_send(&$user,$mesg);
365 else if ($argz[0] == 'play') {
370 if (strpos($a_x, "px") != FALSE)
371 $a_x = substr($a_x,0,-2);
372 if (strpos($a_y, "px") != FALSE)
373 $a_y = substr($a_y,0,-2);
375 $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
376 $a_play, $user->table_pos, ($table->gstart % PLAYERS_N),
377 $table->mazzo, $table->gstart,
378 $table->card[$a_play]->stat, $table->card[$a_play]->owner);
379 log_wr($sess, "CIC".$loggo);
381 /* se era il suo turno e la carta era sua ed era in mano */
382 if ($a_play >=0 && $a_play < 40 &&
383 ($user->table_pos == (($table->gstart + $table->turn) % PLAYERS_N)) &&
384 $table->card[$a_play]->stat == 'hand' &&
385 $table->card[$a_play]->owner == $user->table_pos) {
386 log_wr($sess, sprintf("User: %s Play: %d",$user->name, $a_play));
388 /* Change the card status. */
389 $table->card[$a_play]->play($a_x, $a_y);
392 * !!!! TURN INCREMENTED BEFORE !!!!
394 $turn_cur = ($table->gstart + $table->turn) % PLAYERS_N;
397 $card_play = sprintf("card_play(%d,%d,%d,%d);|",
398 $user->table_pos, $a_play, $a_x, $a_y);
399 if (($table->turn % PLAYERS_N) != 0) { /* manche not finished */
400 $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
402 $player_cur = "remark_off();";
403 $player_nex = $card_play . "is_my_time = true; remark_on();";
404 $player_oth = $card_play;
406 else if ($table->turn <= (PLAYERS_N * 8)) { /* manche finished */
407 $winner = calculate_winner($table);
408 log_wr($sess,"GSTART 3");
409 $table->gstart = $winner;
410 $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
412 log_wr($sess, sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
413 $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|cards_hidetake($d);",
415 $player_cur = "remark_off();" . $card_take . "|";
416 if ($turn_cur != $turn_nex)
417 $player_nex = $card_play . $card_take . "|";
420 if ($table->turn < (PLAYERS_N * 8)) /* game NOT finished */
421 $player_nex .= "is_my_time = true; remark_on();";
422 $player_oth = $card_play . $card_take;
425 log_wr($sess, sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
426 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
427 $user_cur = &$bri->user[$table->player[$i]];
429 $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
432 if ($i == $turn_cur) {
435 if ($i == $turn_nex) {
438 if ($i != $turn_cur && $i != $turn_nex) {
448 if ($table->turn == (PLAYERS_N * 8)) { /* game finished */
449 log_wr($sess, sprintf("GIOCO FINITO !!!"));
451 /* ************************************************ */
452 /* PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO */
453 /* ************************************************ */
454 calculate_points(&$table);
457 $table->game_init(&$bri);
459 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
460 $user_cur = &$bri->user[$table->player[$i]];
461 $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
466 for ($i = 0 ; $i < PLAYERS_N ; $i++) {
467 $user_cur = &$bri->user[$table->player[$i]];
469 $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
473 log_wr($sess, sprintf("TURN: %d",$table->turn));
474 /* Have played all the players ? */
475 /* NO: switch the focus and enable the next player to play. */
477 /* YES: calculate who win and go to the next turn. */
481 log_wr($sess, "NOSENSE");
484 log_wr($sess, "before save data");