5 * Copyright (C) 2006-2011 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.
25 require_once("../Obj/brisk.phh");
26 // require_once("../Obj/proxyscan.phh");
27 require_once("Obj/briskin5.phh");
29 // Use of proxies isn't allowed.
35 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
36 echo "Debugging time!";
40 log_load("bin5/index_wr.php");
45 log_wr('COMM: '.$mesg);
47 if ($table_idx < 0 || $table_idx >= TABLE_N)
50 log_mop(0, 'bin::index_wr.php: COMM: '.xcapemesg($mesg));
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]);
70 log_mop($user->step, 'bin::index_wr.php: after get_user()');
72 if (false && $argz[0] == 'shutdown') {
73 log_auth($user_cur->sess, "Shutdown session. delegate to room gc the autologout");
75 log_rd2("bin5/index_wr.php: AUTO LOGOUT.");
76 if ($user->stat == 'table') {
77 $bri->table_wakeup(&$user);
78 // to force the logout
82 log_rd2("SHUTDOWN FROM WHAT ???");
84 /*********************
88 *********************/
89 else if ($user->stat == 'table') {
90 $user->laccwr = time();
91 $table = &$bri->table[$user->table];
93 if ($argz[0] == 'tableinfo') {
94 log_wr("PER DI TABLEINFO");
95 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
96 $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
97 log_wr($user->comm[$user->step % COMM_N]);
100 else if ($argz[0] == 'chatt') {
101 $bri->chatt_send(&$user,$mesg);
103 else if ($argz[0] == 'preferences_update') {
104 GLOBAL $CO_bin5_pref_ring_endauct;
106 log_wr("PER DI TABLEINFO");
108 if ($CO_bin5_pref_ring_endauct == "true")
109 $user->privflags |= BIN5_USER_FLAG_RING_ENDAUCT;
111 $user->privflags &= ~BIN5_USER_FLAG_RING_ENDAUCT;
113 else if ($argz[0] == 'logout') {
116 if ($user->exitislock == TRUE) {
118 $user->exitislock = FALSE;
123 $lockcalc = $table->exitlock_calc(&$bri->user, $user->table_pos);
125 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
126 $user->comm[$user->step % COMM_N] .= $table->exitlock_show(&$bri->user, $user->table_pos);
127 $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);
129 log_wr($user->comm[$user->step % COMM_N]);
131 $logout_cont = FALSE;
135 require_once("../Obj/hardban.phh");
136 Hardbans::add(($user->flags & USER_FLAG_AUTH ? $user->name : FALSE),
137 $user->ip, $user->sess, $user->laccwr + BAN_TIME);
139 // $user->bantime = $user->laccwr + BAN_TIME;
141 if ($logout_cont == TRUE) {
142 $bri->table_wakeup(&$user);
145 else if ($argz[0] == 'exitlock') {
146 if ($user->exitislock == TRUE) {
147 $user->exitislock = ($user->exitislock == TRUE ? FALSE : TRUE);
148 for ($ct = 0, $i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
149 $user_cur[$i] = &$bri->user[$table->player[$i]];
150 if ($user_cur[$i]->exitislock == FALSE)
153 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
154 $ret = sprintf('gst.st = %d;', $user_cur[$i]->step+1);
155 $ret .= sprintf('exitlock_show(%d, %s);', $ct,
156 ($user_cur[$i]->exitislock ? 'true' : 'false'));
157 $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N] = $ret;
158 log_wr($user_cur[$i]->comm[$user_cur[$i]->step % COMM_N]);
159 $user_cur[$i]->step_inc();
163 else if ($user->subst == 'asta') {
164 if ($argz[0] == 'lascio' && $user->handpt <= 2) {
165 $index_cur = $table->gstart % BRISKIN5_PLAYERS_N;
167 log_wr(sprintf("GIOCO FINITO !!!"));
170 $table->old_reason = sprintf("Ha lasciato %s perché aveva al massimo 2 punti.", xcape($user->name));
173 $table->game_init(&$bri->user);
175 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
176 $user_cur = &$bri->user[$table->player[$i]];
178 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
179 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
180 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
181 $user_cur->step_inc();
184 else if ($argz[0] == 'asta') {
187 $index_cur = $table->gstart % BRISKIN5_PLAYERS_N;
188 if ($user->table_pos == $index_cur &&
189 $table->asta_pla[$index_cur]) {
193 log_wr("CI SIAMO a_card ".$a_card." asta_card ".$table->asta_card);
195 // Abbandono dell'asta
197 log_wr("Abbandona l'asta.");
198 $table->asta_pla[$index_cur] = FALSE;
199 $user->asta_card = -1;
200 $table->asta_pla_n--;
203 else if ($a_card <= 9) {
204 if ($a_card >= 0 && $a_card < 9 && $a_card > $table->asta_card)
206 else if ($a_card == 9 && $a_pnt > ($table->asta_pnt >= 61 ? $table->asta_pnt : 60) && $a_pnt <= 120)
210 if ($again == FALSE) {
211 log_wr("NUOVI ORZI.");
212 $user->asta_card = $a_card;
213 $table->asta_card = $a_card;
215 $user->asta_pnt = $a_pnt;
216 $table->asta_pnt = $a_pnt;
223 if ($again) { // Qualcosa non andato bene, rifare
224 $ret = sprintf('gst.st = %d; asta_pnt_set(%d);', $user->step+1,
225 ($table->asta_pnt > 60 ? $table->asta_pnt + 1 : 61) );
226 $user->comm[$user->step % COMM_N] = $ret;
233 $showst = "show_astat(";
234 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
235 $user_cur = &$bri->user[$table->player[$i]];
236 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
237 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
239 if (BRISKIN5_PLAYERS_N == 3)
244 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
245 $user_cur = &$bri->user[$table->player[$i]];
246 if ($maxcard < $user_cur->asta_card)
247 $maxcard = $user_cur->asta_card;
250 if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
251 !($table->asta_card == 9 && $table->asta_pnt == 120)) {
252 log_wr("ALLOPPA QUI");
253 for ($i = 1 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
254 $index_next = ($table->gstart + $i) % BRISKIN5_PLAYERS_N;
255 if ($table->asta_pla[$index_next]) {
257 $table->gstart += $i;
263 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
264 $user_cur = &$bri->user[$table->player[$i]];
265 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
266 if ($user_cur->table_pos == ($table->gstart % BRISKIN5_PLAYERS_N))
267 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_on();',
268 $table->asta_card + 1, $table->asta_pnt+1, ($user_cur->handpt <= 2 ? "true" : "false"));
270 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_off();',
271 $table->asta_card + 1, -($table->asta_pnt+1), ($user_cur->handpt <= 2 ? "true" : "false"));
272 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
273 $user_cur->step_inc();
276 else if ($table->asta_pla_n == 0) {
277 log_wr("PASSANO TUTTI!");
279 log_wr(sprintf("GIOCO FINITO !!!"));
281 $table->old_reason = "Hanno passato tutti.";
285 $table->game_init(&$bri->user);
287 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
288 $user_cur = &$bri->user[$table->player[$i]];
290 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
291 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
292 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
293 $user_cur->step_inc();
298 // if a_pnt == 120 supergame ! else abbandono
299 if ($a_pnt == 120 || $user->asta_card != -1) {
300 $chooser = $index_cur;
301 for ($i = 1 ; $i < BRISKIN5_PLAYERS_N ; $i++)
303 $table->asta_pla[$i] = FALSE;
307 $user->comm[$user->step % COMM_N] = sprintf( "gst.st = %d; dispose_asta(%d, %d, false); remark_off();",
308 $user->step+1, $table->asta_card + 1,-($table->asta_pnt));
311 for ($i = 1 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
312 $chooser = ($table->gstart + $i) % BRISKIN5_PLAYERS_N;
313 if ($table->asta_pla[$chooser]) {
318 $table->asta_win = $chooser;
320 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
321 $user_cur = &$bri->user[$table->player[$i]];
322 $ret = sprintf('gst.st = %d; %s dispose_asta(%d, %d, false);', $user_cur->step+1, $showst,
323 $table->asta_card + 1,-($table->asta_pnt));
325 if ($i == $chooser) {
326 $ret .= "choose_seed(". $table->asta_card."); remark_on();";
329 $ret .= "remark_off();";
332 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
333 $user_cur->step_inc();
339 log_wr("NON CI SIAMO");
343 else if ($argz[0] == 'choose') {
344 if ($table->asta_win > -1 &&
345 $user->table_pos == $table->asta_win) {
346 $a_brisco = $argz[1];
347 if ($a_brisco >= 0 && $a_brisco < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24)) {
348 $table->briscola = $a_brisco;
349 $table->friend = $table->card[$a_brisco]->owner;
351 $table->gstart = ($table->mazzo+1) % BRISKIN5_PLAYERS_N;
352 log_wr("Setta la briscola a ".$a_brisco);
354 $chooser = $table->asta_win;
355 $user_chooser = &$bri->user[$table->player[$chooser]];
356 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
357 $user_cur = &$bri->user[$table->player[$i]];
358 $user_cur->subst = 'game';
359 $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
361 if ($user_cur->privflags & BIN5_USER_FLAG_RING_ENDAUCT) {
362 // $ret .= "var de_che= 33;";
363 $ret .= playsound("ringbell.mp3");
365 $ret .= sprintf('document.title = "Brisk - Tavolo %d";', $user->table_orig);
367 /* bg of caller cell */
368 $ret .= briscola_show($bri, $table, $user_cur);
371 if ($i == ($table->gstart % BRISKIN5_PLAYERS_N))
372 $ret .= "is_my_time = true; remark_on();";
374 $ret .= "is_my_time = false; remark_off();";
376 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
377 $user_cur->step_inc();
380 TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
381 VAR PER PASSARE ALLA FASE DI GIOCO
388 else if ($user->subst == 'game') {
389 log_wr("state: table::game".$argz[0]);
391 if ($argz[0] == 'play') {
396 if (strpos($a_x, "px") != FALSE)
397 $a_x = substr($a_x,0,-2);
398 if (strpos($a_y, "px") != FALSE)
399 $a_y = substr($a_y,0,-2);
401 $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
402 $a_play, $user->table_pos, ($table->gstart % BRISKIN5_PLAYERS_N),
403 $table->mazzo, $table->gstart,
404 $table->card[$a_play]->stat, $table->card[$a_play]->owner);
405 log_wr("CIC".$loggo);
407 /* se era il suo turno e la carta era sua ed era in mano */
408 if ($a_play >=0 && $a_play < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) &&
409 ($user->table_pos == (($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N)) &&
410 $table->card[$a_play]->stat == 'hand' &&
411 $table->card[$a_play]->owner == $user->table_pos) {
412 log_wr(sprintf("User: %s Play: %d",$user->name, $a_play));
414 /* Change the card status. */
415 $table->card[$a_play]->play($a_x, $a_y);
418 * !!!! TURN INCREMENTED BEFORE !!!!
420 $turn_cur = ($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N;
423 $card_play = sprintf("card_play(%d,%d,%d,%d);|",
424 $user->table_pos, $a_play, $a_x, $a_y);
425 if (($table->turn % BRISKIN5_PLAYERS_N) != 0) { /* manche not finished */
426 $turn_nex = ($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N;
428 $player_cur = "remark_off();";
429 $player_nex = $card_play . "is_my_time = true; remark_on();";
430 $player_oth = $card_play;
432 else if ($table->turn <= (BRISKIN5_PLAYERS_N * 8)) { /* manche finished */
433 $winner = calculate_winner($table);
435 $table->gstart = $winner;
436 $turn_nex = ($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N;
438 log_wr(sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
439 $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|", $winner);
440 $player_cur = "remark_off();" . $card_take;
441 if ($turn_cur != $turn_nex)
442 $player_nex = $card_play . $card_take;
445 if ($table->turn < (BRISKIN5_PLAYERS_N * 8)) /* game NOT finished */
446 $player_nex .= "is_my_time = true; remark_on();";
447 $player_oth = $card_play . $card_take;
450 log_wr(sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
451 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
452 $user_cur = &$bri->user[$table->player[$i]];
454 $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
457 if ($i == $turn_cur) {
460 if ($i == $turn_nex) {
463 if ($i != $turn_cur && $i != $turn_nex) {
470 if ($table->turn == (BRISKIN5_PLAYERS_N * 8)) { /* game finished */
471 log_wr(sprintf("GIOCO FINITO !!!"));
474 /* ************************************************ */
475 /* PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO */
476 /* ************************************************ */
477 $pt_cur = calculate_points(&$table);
479 $plist = "$table->table_token|$user->table_orig|$table->player_n";
481 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
482 $user_cur = &$bri->user[$table->player[$i]];
483 $plist .= '|'.xcapelt($user_cur->name).'|'.$pt_cur[$i];
485 log_legal($curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
486 if ($user->table_orig < TABLES_AUTH_N)
487 log_points($curtime, xcapelt($user->name), "STAT:BRISKIN5:FINISH_GAME", $plist);
490 $table->game_init(&$bri->user);
492 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
493 $user_cur = &$bri->user[$table->player[$i]];
494 $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
499 for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
500 $user_cur = &$bri->user[$table->player[$i]];
502 $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
503 $user_cur->step_inc();
506 log_wr(sprintf("TURN: %d",$table->turn));
507 /* Have played all the players ? */
508 /* NO: switch the focus and enable the next player to play. */
510 /* YES: calculate who win and go to the next turn. */
517 log_wr("before save data");
518 Briskin5::save_data($bri);
519 log_mop($user->step, 'bin::index_wr.php: after save_data()');
521 Briskin5::unlock_data($sem);