5 * Copyright (C) 2006-2009 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details. You should have received a
19 * copy of the GNU General Public License along with this program; if
20 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21 * Suite 330, Boston, MA 02111-1307, USA.
27 require_once("../Obj/brisk.phh");
28 // require_once("../Obj/proxyscan.phh");
29 require_once("Obj/briskin5.phh");
31 // Use of proxies isn't allowed.
37 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
38 echo "Debugging time!";
42 log_load("bin5/index_wr.php");
47 log_wr('COMM: '.$mesg);
49 if ($table_idx < 0 || $table_idx >= TABLE_N)
52 $sem = Briskin5::lock_data($table_idx);
54 if (($bri = &Briskin5::load_data($table_idx,$table_token)) == FALSE) {
55 echo "Bin5 Load data error";
56 log_wr("Bin5 Load data error");
57 Briskin5::unlock_data($sem);
61 if (($user = &$bri->get_user($sess, &$idx)) == FALSE) {
62 echo "Get User Error";
63 log_wr("Get User Error");
64 Briskin5::unlock_data($sem);
67 $argz = explode('|', $mesg);
69 log_wr('POSTSPLIT: '.$argz[0]);
71 if ($argz[0] == 'shutdown') {
72 log_auth($user_cur->sess, "Shutdown session. delegate to room gc the autologout");
74 log_rd2("bin5/index_wr.php: AUTO LOGOUT.");
75 if ($user->stat == 'table') {
76 $bri->table_wakeup(&$user);
77 // to force the logout
81 log_rd2("SHUTDOWN FROM WHAT ???");
83 /*********************
87 *********************/
88 else if ($user->stat == 'table') {
89 $user->laccwr = time();
90 $table = &$bri->table[$user->table];
92 if ($argz[0] == 'tableinfo') {
93 log_wr("PER DI TABLEINFO");
94 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
95 $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
96 log_wr($user->comm[$user->step % COMM_N]);
99 else if ($argz[0] == 'chatt') {
100 $bri->chatt_send(&$user,$mesg);
102 else if ($argz[0] == 'logout') {
105 if ($user->exitislock == TRUE) {
107 $user->exitislock = FALSE;
112 $lockcalc = $table->exitlock_calc(&$bri->user, $user->table_pos);
114 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
115 $user->comm[$user->step % COMM_N] .= $table->exitlock_show(&$bri->user, $user->table_pos);
116 $user->comm[$user->step % COMM_N] .= show_notify("<br>I dati presenti sul server non erano allineati con quelli inviati dal tuo browser, adesso lo sono. Riprova ora.", 2000, "torna alla partita.", 400, 100);
118 log_wr($user->comm[$user->step % COMM_N]);
120 $logout_cont = FALSE;
124 require_once("../Obj/hardban.phh");
125 Hardbans::add(($user->flags & USER_FLAG_AUTH ? $user->name : FALSE),
126 $user->ip, $user->sess, $user->laccwr + BAN_TIME);
128 // $user->bantime = $user->laccwr + BAN_TIME;
130 if ($logout_cont == TRUE) {
131 $bri->table_wakeup(&$user);
134 else if ($argz[0] == 'exitlock') {
135 $user->exitislock = ($user->exitislock == TRUE ? FALSE : TRUE);
136 for ($ct = 0, $i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
137 $user_cur[$i] = &$bri->user[$table->player[$i]];
138 if ($user_cur[$i]->exitislock == FALSE)
141 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
142 $ret = sprintf('gst.st = %d;', $user_cur[$i]->step+1);
143 $ret .= sprintf('exitlock_show(%d, %s);', $ct,
144 ($user_cur[$i]->exitislock ? 'true' : 'false'));
145 $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N] = $ret;
146 log_wr($user_cur[$i]->comm[$user_cur[$i]->step % COMM_N]);
147 $user_cur[$i]->step_inc();
150 else if ($user->subst == 'asta') {
151 if ($argz[0] == 'lascio' && $user->handpt <= 2) {
152 $index_cur = $table->gstart % BRISKIN5_PLAYERS_N;
154 log_wr(sprintf("GIOCO FINITO !!!"));
157 $table->old_reason = sprintf("Ha lasciato %s perché aveva al massimo 2 punti.", xcape($user->name));
160 $table->game_init(&$bri->user);
162 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
163 $user_cur = &$bri->user[$table->player[$i]];
165 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
166 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
167 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
168 $user_cur->step_inc();
171 else if ($argz[0] == 'asta') {
174 $index_cur = $table->gstart % BRISKIN5_PLAYERS_N;
175 if ($user->table_pos == $index_cur &&
176 $table->asta_pla[$index_cur]) {
180 log_wr("CI SIAMO a_card ".$a_card." asta_card ".$table->asta_card);
182 // Abbandono dell'asta
184 log_wr("Abbandona l'asta.");
185 $table->asta_pla[$index_cur] = FALSE;
186 $user->asta_card = -1;
187 $table->asta_pla_n--;
190 else if ($a_card <= 9) {
191 if ($a_card >= 0 && $a_card < 9 && $a_card > $table->asta_card)
193 else if ($a_card == 9 && $a_pnt > ($table->asta_pnt >= 61 ? $table->asta_pnt : 60) && $a_pnt <= 120)
197 if ($again == FALSE) {
198 log_wr("NUOVI ORZI.");
199 $user->asta_card = $a_card;
200 $table->asta_card = $a_card;
202 $user->asta_pnt = $a_pnt;
203 $table->asta_pnt = $a_pnt;
210 if ($again) { // Qualcosa non andato bene, rifare
211 $ret = sprintf('gst.st = %d; asta_pnt_set(%d);', $user->step+1,
212 ($table->asta_pnt > 60 ? $table->asta_pnt + 1 : 61) );
213 $user->comm[$user->step % COMM_N] = $ret;
220 $showst = "show_astat(";
221 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
222 $user_cur = &$bri->user[$table->player[$i]];
223 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
224 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
226 if (BRISKIN5_PLAYERS_N == 3)
231 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
232 $user_cur = &$bri->user[$table->player[$i]];
233 if ($maxcard < $user_cur->asta_card)
234 $maxcard = $user_cur->asta_card;
237 if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
238 !($table->asta_card == 9 && $table->asta_pnt == 120)) {
239 log_wr("ALLOPPA QUI");
240 for ($i = 1 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
241 $index_next = ($table->gstart + $i) % BRISKIN5_PLAYERS_N;
242 if ($table->asta_pla[$index_next]) {
244 $table->gstart += $i;
250 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
251 $user_cur = &$bri->user[$table->player[$i]];
252 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
253 if ($user_cur->table_pos == ($table->gstart % BRISKIN5_PLAYERS_N))
254 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_on();',
255 $table->asta_card + 1, $table->asta_pnt+1, ($user_cur->handpt <= 2 ? "true" : "false"));
257 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_off();',
258 $table->asta_card + 1, -($table->asta_pnt+1), ($user_cur->handpt <= 2 ? "true" : "false"));
259 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
260 $user_cur->step_inc();
263 else if ($table->asta_pla_n == 0) {
264 log_wr("PASSANO TUTTI!");
266 log_wr(sprintf("GIOCO FINITO !!!"));
268 $table->old_reason = "Hanno passato tutti.";
272 $table->game_init(&$bri->user);
274 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
275 $user_cur = &$bri->user[$table->player[$i]];
277 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
278 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
279 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
280 $user_cur->step_inc();
285 // if a_pnt == 120 supergame ! else abbandono
286 if ($a_pnt == 120 || $user->asta_card != -1) {
287 $chooser = $index_cur;
288 for ($i = 1 ; $i < BRISKIN5_PLAYERS_N ; $i++)
290 $table->asta_pla[$i] = FALSE;
294 $user->comm[$user->step % COMM_N] = sprintf( "gst.st = %d; dispose_asta(%d, %d, false); remark_off();",
295 $user->step+1, $table->asta_card + 1,-($table->asta_pnt));
298 for ($i = 1 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
299 $chooser = ($table->gstart + $i) % BRISKIN5_PLAYERS_N;
300 if ($table->asta_pla[$chooser]) {
305 $table->asta_win = $chooser;
307 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
308 $user_cur = &$bri->user[$table->player[$i]];
309 $ret = sprintf('gst.st = %d; %s dispose_asta(%d, %d, false);', $user_cur->step+1, $showst,
310 $table->asta_card + 1,-($table->asta_pnt));
312 if ($i == $chooser) {
313 $ret .= "choose_seed(". $table->asta_card."); remark_on();";
316 $ret .= "remark_off();";
319 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
320 $user_cur->step_inc();
326 log_wr("NON CI SIAMO");
330 else if ($argz[0] == 'choose') {
331 if ($table->asta_win > -1 &&
332 $user->table_pos == $table->asta_win) {
333 $a_brisco = $argz[1];
334 if ($a_brisco >= 0 && $a_brisco < 40) {
335 $table->briscola = $a_brisco;
336 $table->friend = $table->card[$a_brisco]->owner;
338 $table->gstart = ($table->mazzo+1) % BRISKIN5_PLAYERS_N;
339 log_wr("Setta la briscola a ".$a_brisco);
341 $chooser = $table->asta_win;
342 $user_chooser = &$bri->user[$table->player[$chooser]];
343 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
344 $user_cur = &$bri->user[$table->player[$i]];
345 $user_cur->subst = 'game';
346 $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
349 /* bg of caller cell */
350 $ret .= briscola_show($bri, $table, $user_cur);
353 if ($i == ($table->gstart % BRISKIN5_PLAYERS_N))
354 $ret .= "is_my_time = true; remark_on();";
356 $ret .= "is_my_time = false; remark_off();";
358 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
359 $user_cur->step_inc();
362 TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
363 VAR PER PASSARE ALLA FASE DI GIOCO
370 else if ($user->subst == 'game') {
371 log_wr("state: table::game".$argz[0]);
373 if ($argz[0] == 'play') {
378 if (strpos($a_x, "px") != FALSE)
379 $a_x = substr($a_x,0,-2);
380 if (strpos($a_y, "px") != FALSE)
381 $a_y = substr($a_y,0,-2);
383 $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
384 $a_play, $user->table_pos, ($table->gstart % BRISKIN5_PLAYERS_N),
385 $table->mazzo, $table->gstart,
386 $table->card[$a_play]->stat, $table->card[$a_play]->owner);
387 log_wr("CIC".$loggo);
389 /* se era il suo turno e la carta era sua ed era in mano */
390 if ($a_play >=0 && $a_play < 40 &&
391 ($user->table_pos == (($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N)) &&
392 $table->card[$a_play]->stat == 'hand' &&
393 $table->card[$a_play]->owner == $user->table_pos) {
394 log_wr(sprintf("User: %s Play: %d",$user->name, $a_play));
396 /* Change the card status. */
397 $table->card[$a_play]->play($a_x, $a_y);
400 * !!!! TURN INCREMENTED BEFORE !!!!
402 $turn_cur = ($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N;
405 $card_play = sprintf("card_play(%d,%d,%d,%d);|",
406 $user->table_pos, $a_play, $a_x, $a_y);
407 if (($table->turn % BRISKIN5_PLAYERS_N) != 0) { /* manche not finished */
408 $turn_nex = ($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N;
410 $player_cur = "remark_off();";
411 $player_nex = $card_play . "is_my_time = true; remark_on();";
412 $player_oth = $card_play;
414 else if ($table->turn <= (BRISKIN5_PLAYERS_N * 8)) { /* manche finished */
415 $winner = calculate_winner($table);
417 $table->gstart = $winner;
418 $turn_nex = ($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N;
420 log_wr(sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
421 $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|", $winner);
422 $player_cur = "remark_off();" . $card_take;
423 if ($turn_cur != $turn_nex)
424 $player_nex = $card_play . $card_take;
427 if ($table->turn < (BRISKIN5_PLAYERS_N * 8)) /* game NOT finished */
428 $player_nex .= "is_my_time = true; remark_on();";
429 $player_oth = $card_play . $card_take;
432 log_wr(sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
433 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
434 $user_cur = &$bri->user[$table->player[$i]];
436 $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
439 if ($i == $turn_cur) {
442 if ($i == $turn_nex) {
445 if ($i != $turn_cur && $i != $turn_nex) {
452 if ($table->turn == (BRISKIN5_PLAYERS_N * 8)) { /* game finished */
453 log_wr(sprintf("GIOCO FINITO !!!"));
455 $plist = "$table->table_token|$user->table|$table->player_n";
457 log_legal($curtime, $user, "STAT:FINISH_GAME", $plist);
459 /* ************************************************ */
460 /* PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO */
461 /* ************************************************ */
462 calculate_points(&$table);
465 $table->game_init(&$bri->user);
467 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
468 $user_cur = &$bri->user[$table->player[$i]];
469 $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
474 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
475 $user_cur = &$bri->user[$table->player[$i]];
477 $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
478 $user_cur->step_inc();
481 log_wr(sprintf("TURN: %d",$table->turn));
482 /* Have played all the players ? */
483 /* NO: switch the focus and enable the next player to play. */
485 /* YES: calculate who win and go to the next turn. */
492 log_wr("before save data");
493 Briskin5::save_data($bri);
495 Briskin5::unlock_data($sem);