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.
28 require_once("../Obj/brisk.phh");
29 // require_once("../Obj/proxyscan.phh");
30 require_once("Obj/briskin5.phh");
32 // Use of proxies isn't allowed.
38 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
39 echo "Debugging time!";
43 log_load("bin5/index_wr.php");
48 log_wr('COMM: '.$mesg);
50 $table_idx = (int)$table_idx;
51 if ($table_idx < 0 || $table_idx >= TABLE_N)
54 log_wr(0, 'bin::index_wr.php: COMM: '.xcapemesg($mesg));
56 $sem = Bin5::lock_data(TRUE, $table_idx);
58 if (($bri = Bin5::load_data($table_idx,$table_token)) == FALSE) {
59 echo "Bin5 Load data error";
60 log_wr("Bin5 Load data error");
61 Bin5::unlock_data($sem);
65 if (($user = &$bri->get_user($sess, &$idx)) == FALSE) {
66 echo "Get User Error";
67 log_wr("Get User Error");
68 Bin5::unlock_data($sem);
71 $argz = explode('|', $mesg);
73 log_wr('POSTSPLIT: '.$argz[0].' user->stat: ['.$user->stat.']');
74 log_wr($user->step, 'bin::index_wr.php: after get_user()');
76 if (false && $argz[0] == 'shutdown') {
77 log_auth($user_cur->sess, "Shutdown session. delegate to room gc the autologout");
79 log_rd2("bin5/index_wr.php: AUTO LOGOUT.");
80 if ($user->stat == 'table') {
81 $bri->table_wakeup($user);
82 // to force the logout
86 log_rd2("SHUTDOWN FROM WHAT ???");
88 /*********************
92 *********************/
93 else if ($user->stat == 'table') {
94 $user->laccwr = time();
95 $table = $bri->table[$user->table];
97 if ($argz[0] == 'tableinfo') {
98 log_wr("PER DI TABLEINFO");
99 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
100 $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
101 log_wr($user->comm[$user->step % COMM_N]);
104 else if ($argz[0] == 'chatt') {
105 $bri->chatt_send(&$user,$mesg);
107 else if ($argz[0] == 'preferences_update') {
108 GLOBAL $CO_bin5_pref_ring_endauct;
110 log_wr("PER DI TABLEINFO");
112 if ($CO_bin5_pref_ring_endauct == "true")
113 $user->privflags |= BIN5_USER_FLAG_RING_ENDAUCT;
115 $user->privflags &= ~BIN5_USER_FLAG_RING_ENDAUCT;
117 else if ($argz[0] == 'logout') {
120 if ($user->exitislock == TRUE) {
122 $user->exitislock = FALSE;
127 $lockcalc = $table->exitlock_calc(&$bri->user, $user->table_pos);
129 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
130 $user->comm[$user->step % COMM_N] .= $table->exitlock_show(&$bri->user, $user->table_pos);
131 $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);
133 log_wr($user->comm[$user->step % COMM_N]);
135 $logout_cont = FALSE;
139 require_once("../Obj/hardban.phh");
140 Hardbans::add(($user->flags & USER_FLAG_AUTH ? $user->name : FALSE),
141 $user->ip, $user->sess, $user->laccwr + BAN_TIME);
143 // $user->bantime = $user->laccwr + BAN_TIME;
145 if ($logout_cont == TRUE) {
146 $bri->table_wakeup(&$user);
149 else if ($argz[0] == 'exitlock') {
150 if ($user->exitislock == TRUE) {
151 $user->exitislock = ($user->exitislock == TRUE ? FALSE : TRUE);
152 for ($ct = 0, $i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
153 $user_cur[$i] = &$bri->user[$table->player[$i]];
154 if ($user_cur[$i]->exitislock == FALSE)
157 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
158 $ret = sprintf('gst.st = %d;', $user_cur[$i]->step+1);
159 $ret .= sprintf('exitlock_show(%d, %s);', $ct,
160 ($user_cur[$i]->exitislock ? 'true' : 'false'));
161 $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N] = $ret;
162 log_wr($user_cur[$i]->comm[$user_cur[$i]->step % COMM_N]);
163 $user_cur[$i]->step_inc();
167 else if ($user->subst == 'asta') {
168 if ($argz[0] == 'lascio' && $user->handpt <= 2) {
169 $index_cur = $table->gstart % BIN5_PLAYERS_N;
171 log_wr(sprintf("GIOCO FINITO !!!"));
174 $table->old_reason = sprintf("Ha lasciato %s perché aveva al massimo 2 punti.", xcape($user->name));
177 $table->game_init(&$bri->user);
179 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
180 $user_cur = &$bri->user[$table->player[$i]];
182 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
183 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
184 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
185 $user_cur->step_inc();
188 else if ($argz[0] == 'asta') {
191 $index_cur = $table->gstart % BIN5_PLAYERS_N;
192 if ($user->table_pos == $index_cur &&
193 $table->asta_pla[$index_cur]) {
197 log_wr("CI SIAMO a_card ".$a_card." asta_card ".$table->asta_card);
199 // Abbandono dell'asta
201 log_wr("Abbandona l'asta.");
202 $table->asta_pla[$index_cur] = FALSE;
203 $user->asta_card = -1;
204 $table->asta_pla_n--;
207 else if ($a_card <= 9) {
208 if ($a_card >= 0 && $a_card < 9 && $a_card > $table->asta_card)
210 else if ($a_card == 9 && $a_pnt > ($table->asta_pnt >= 61 ? $table->asta_pnt : 60) && $a_pnt <= 120)
214 if ($again == FALSE) {
215 log_wr("NUOVI ORZI.");
216 $user->asta_card = $a_card;
217 $table->asta_card = $a_card;
219 $user->asta_pnt = $a_pnt;
220 $table->asta_pnt = $a_pnt;
227 if ($again) { // Qualcosa non andato bene, rifare
228 $ret = sprintf('gst.st = %d; asta_pnt_set(%d);', $user->step+1,
229 ($table->asta_pnt > 60 ? $table->asta_pnt + 1 : 61) );
230 $user->comm[$user->step % COMM_N] = $ret;
237 $showst = "show_astat(";
238 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
239 $user_cur = &$bri->user[$table->player[$i]];
240 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
241 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
243 if (BIN5_PLAYERS_N == 3)
248 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
249 $user_cur = &$bri->user[$table->player[$i]];
250 if ($maxcard < $user_cur->asta_card)
251 $maxcard = $user_cur->asta_card;
254 if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
255 !($table->asta_card == 9 && $table->asta_pnt == 120)) {
256 log_wr("ALLOPPA QUI");
257 for ($i = 1 ; $i < BIN5_PLAYERS_N ; $i++) {
258 $index_next = ($table->gstart + $i) % BIN5_PLAYERS_N;
259 if ($table->asta_pla[$index_next]) {
261 $table->gstart += $i;
267 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
268 $user_cur = &$bri->user[$table->player[$i]];
269 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
270 if ($user_cur->table_pos == ($table->gstart % BIN5_PLAYERS_N))
271 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_on();',
272 $table->asta_card + 1, $table->asta_pnt+1, ($user_cur->handpt <= 2 ? "true" : "false"));
274 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_off();',
275 $table->asta_card + 1, -($table->asta_pnt+1), ($user_cur->handpt <= 2 ? "true" : "false"));
276 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
277 $user_cur->step_inc();
280 else if ($table->asta_pla_n == 0) {
281 log_wr("PASSANO TUTTI!");
283 log_wr(sprintf("GIOCO FINITO !!!"));
285 $table->old_reason = "Hanno passato tutti.";
289 $table->game_init(&$bri->user);
291 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
292 $user_cur = &$bri->user[$table->player[$i]];
294 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
295 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
296 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
297 $user_cur->step_inc();
302 // if a_pnt == 120 supergame ! else abbandono
303 if ($a_pnt == 120 || $user->asta_card != -1) {
304 $chooser = $index_cur;
305 for ($i = 1 ; $i < BIN5_PLAYERS_N ; $i++)
307 $table->asta_pla[$i] = FALSE;
311 $user->comm[$user->step % COMM_N] = sprintf( "gst.st = %d; dispose_asta(%d, %d, false); remark_off();",
312 $user->step+1, $table->asta_card + 1,-($table->asta_pnt));
315 for ($i = 1 ; $i < BIN5_PLAYERS_N ; $i++) {
316 $chooser = ($table->gstart + $i) % BIN5_PLAYERS_N;
317 if ($table->asta_pla[$chooser]) {
322 $table->asta_win = $chooser;
324 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
325 $user_cur = &$bri->user[$table->player[$i]];
326 $ret = sprintf('gst.st = %d; %s dispose_asta(%d, %d, false);', $user_cur->step+1, $showst,
327 $table->asta_card + 1,-($table->asta_pnt));
329 if ($i == $chooser) {
330 $ret .= "choose_seed(". $table->asta_card."); remark_on();";
333 $ret .= "remark_off();";
336 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
337 $user_cur->step_inc();
343 log_wr("NON CI SIAMO");
347 else if ($argz[0] == 'choose') {
348 if ($table->asta_win > -1 &&
349 $user->table_pos == $table->asta_win) {
350 $a_brisco = $argz[1];
351 if ($a_brisco >= 0 && $a_brisco < 40) {
352 $table->briscola = $a_brisco;
353 $table->friend = $table->card[$a_brisco]->owner;
355 $table->gstart = ($table->mazzo+1) % BIN5_PLAYERS_N;
356 log_wr("Setta la briscola a ".$a_brisco);
358 $chooser = $table->asta_win;
359 $user_chooser = &$bri->user[$table->player[$chooser]];
360 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
361 $user_cur = &$bri->user[$table->player[$i]];
362 $user_cur->subst = 'game';
363 $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
365 if ($user_cur->privflags & BIN5_USER_FLAG_RING_ENDAUCT) {
366 // $ret .= "var de_che= 33;";
367 $ret .= playsound("ringbell.mp3");
369 $ret .= sprintf('document.title = "Brisk - Tavolo %d";', $user->table_orig);
371 /* bg of caller cell */
372 $ret .= briscola_show($bri, $table, $user_cur);
375 if ($i == ($table->gstart % BIN5_PLAYERS_N))
376 $ret .= "is_my_time = true; remark_on();";
378 $ret .= "is_my_time = false; remark_off();";
380 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
381 $user_cur->step_inc();
384 TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
385 VAR PER PASSARE ALLA FASE DI GIOCO
392 else if ($user->subst == 'game') {
393 log_wr("state: table::game".$argz[0]);
395 if ($argz[0] == 'play') {
400 if (strpos($a_x, "px") != FALSE)
401 $a_x = substr($a_x,0,-2);
402 if (strpos($a_y, "px") != FALSE)
403 $a_y = substr($a_y,0,-2);
405 $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
406 $a_play, $user->table_pos, ($table->gstart % BIN5_PLAYERS_N),
407 $table->mazzo, $table->gstart,
408 $table->card[$a_play]->stat, $table->card[$a_play]->owner);
409 log_wr("CIC".$loggo);
411 /* se era il suo turno e la carta era sua ed era in mano */
412 if ($a_play >=0 && $a_play < (BIN5_PLAYERS_N == 5 ? 40 : 24) &&
413 ($user->table_pos == (($table->gstart + $table->turn) % BIN5_PLAYERS_N)) &&
414 $table->card[$a_play]->stat == 'hand' &&
415 $table->card[$a_play]->owner == $user->table_pos) {
416 log_wr(sprintf("User: %s Play: %d",$user->name, $a_play));
418 /* Change the card status. */
419 $table->card[$a_play]->play($a_x, $a_y);
422 * !!!! TURN INCREMENTED BEFORE !!!!
424 $turn_cur = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
427 $card_play = sprintf("card_play(%d,%d,%d,%d);|",
428 $user->table_pos, $a_play, $a_x, $a_y);
429 if (($table->turn % BIN5_PLAYERS_N) != 0) { /* manche not finished */
430 $turn_nex = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
432 $player_cur = "remark_off();";
433 $player_nex = $card_play . "is_my_time = true; remark_on();";
434 $player_oth = $card_play;
436 else if ($table->turn <= (BIN5_PLAYERS_N * 8)) { /* manche finished */
437 $winner = calculate_winner($table);
439 $table->gstart = $winner;
440 $turn_nex = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
442 log_wr(sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
443 $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|", $winner);
444 $player_cur = "remark_off();" . $card_take;
445 if ($turn_cur != $turn_nex)
446 $player_nex = $card_play . $card_take;
449 if ($table->turn < (BIN5_PLAYERS_N * 8)) /* game NOT finished */
450 $player_nex .= "is_my_time = true; remark_on();";
451 $player_oth = $card_play . $card_take;
454 log_wr(sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
455 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
456 $user_cur = &$bri->user[$table->player[$i]];
458 $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
461 if ($i == $turn_cur) {
464 if ($i == $turn_nex) {
467 if ($i != $turn_cur && $i != $turn_nex) {
474 if ($table->turn == (BIN5_PLAYERS_N * 8)) { /* game finished */
475 log_wr(sprintf("GIOCO FINITO !!!"));
478 /* ************************************************ */
479 /* PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO */
480 /* ************************************************ */
481 $pt_cur = calculate_points(&$table);
483 $plist = "$table->table_token|$user->table_orig|$table->player_n";
486 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
487 $user_cur = &$bri->user[$table->player[$i]];
488 $plist .= '|'.xcapelt($user_cur->name).'|'.$pt_cur[$i];
489 $ucodes[$i] = $user_cur->code_get();
491 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
492 $plist .= '|'.xcapelt($ucodes[$i]);
494 log_legal($curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
495 if ($user->table_orig < TABLES_AUTH_N) {
496 require_once("../Obj/dbase_".$G_dbasetype.".phh");
498 if (($bdb = BriskDB::create()) != FALSE) {
499 $bdb->bin5_points_save($curtime, $table->table_token, $user->table_orig, $ucodes, $pt_cur);
503 log_points($curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED");
505 log_points($curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
509 $table->game_init(&$bri->user);
511 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
512 $user_cur = &$bri->user[$table->player[$i]];
513 $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
518 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
519 $user_cur = &$bri->user[$table->player[$i]];
521 $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
522 $user_cur->step_inc();
525 log_wr(sprintf("TURN: %d",$table->turn));
526 /* Have played all the players ? */
527 /* NO: switch the focus and enable the next player to play. */
529 /* YES: calculate who win and go to the next turn. */
536 log_wr("before save data");
537 Bin5::save_data($bri);
538 log_wr($user->step, 'bin::index_wr.php: after save_data()');
540 Bin5::unlock_data($sem);