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 if ($table_idx < 0 || $table_idx >= TABLE_N)
53 log_mop(0, 'bin::index_wr.php: COMM: '.xcapemesg($mesg));
55 $sem = Bin5::lock_data($table_idx);
57 if (($bri = Bin5::load_data($table_idx,$table_token)) == FALSE) {
58 echo "Bin5 Load data error";
59 log_wr("Bin5 Load data error");
60 Bin5::unlock_data($sem);
64 if (($user = &$bri->get_user($sess, &$idx)) == FALSE) {
65 echo "Get User Error";
66 log_wr("Get User Error");
67 Bin5::unlock_data($sem);
70 $argz = explode('|', $mesg);
72 log_wr('POSTSPLIT: '.$argz[0].' user->stat: ['.$user->stat.']');
73 log_mop($user->step, 'bin::index_wr.php: after get_user()');
75 if (false && $argz[0] == 'shutdown') {
76 log_auth($user_cur->sess, "Shutdown session. delegate to room gc the autologout");
78 log_rd2("bin5/index_wr.php: AUTO LOGOUT.");
79 if ($user->stat == 'table') {
80 $bri->table_wakeup($user);
81 // to force the logout
85 log_rd2("SHUTDOWN FROM WHAT ???");
87 /*********************
91 *********************/
92 else if ($user->stat == 'table') {
93 $user->laccwr = time();
94 $table = $bri->table[$user->table];
96 if ($argz[0] == 'tableinfo') {
97 log_wr("PER DI TABLEINFO");
98 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
99 $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
100 log_wr($user->comm[$user->step % COMM_N]);
103 else if ($argz[0] == 'chatt') {
104 $bri->chatt_send(&$user,$mesg);
106 else if ($argz[0] == 'preferences_update') {
107 GLOBAL $CO_bin5_pref_ring_endauct;
109 log_wr("PER DI TABLEINFO");
111 if ($CO_bin5_pref_ring_endauct == "true")
112 $user->privflags |= BIN5_USER_FLAG_RING_ENDAUCT;
114 $user->privflags &= ~BIN5_USER_FLAG_RING_ENDAUCT;
116 else if ($argz[0] == 'logout') {
119 if ($user->exitislock == TRUE) {
121 $user->exitislock = FALSE;
126 $lockcalc = $table->exitlock_calc(&$bri->user, $user->table_pos);
128 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
129 $user->comm[$user->step % COMM_N] .= $table->exitlock_show(&$bri->user, $user->table_pos);
130 $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);
132 log_wr($user->comm[$user->step % COMM_N]);
134 $logout_cont = FALSE;
138 require_once("../Obj/hardban.phh");
139 Hardbans::add(($user->flags & USER_FLAG_AUTH ? $user->name : FALSE),
140 $user->ip, $user->sess, $user->laccwr + BAN_TIME);
142 // $user->bantime = $user->laccwr + BAN_TIME;
144 if ($logout_cont == TRUE) {
145 $bri->table_wakeup(&$user);
148 else if ($argz[0] == 'exitlock') {
149 if ($user->exitislock == TRUE) {
150 $user->exitislock = ($user->exitislock == TRUE ? FALSE : TRUE);
151 for ($ct = 0, $i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
152 $user_cur[$i] = &$bri->user[$table->player[$i]];
153 if ($user_cur[$i]->exitislock == FALSE)
156 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
157 $ret = sprintf('gst.st = %d;', $user_cur[$i]->step+1);
158 $ret .= sprintf('exitlock_show(%d, %s);', $ct,
159 ($user_cur[$i]->exitislock ? 'true' : 'false'));
160 $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N] = $ret;
161 log_wr($user_cur[$i]->comm[$user_cur[$i]->step % COMM_N]);
162 $user_cur[$i]->step_inc();
166 else if ($user->subst == 'asta') {
167 if ($argz[0] == 'lascio' && $user->handpt <= 2) {
168 $index_cur = $table->gstart % BIN5_PLAYERS_N;
170 log_wr(sprintf("GIOCO FINITO !!!"));
173 $table->old_reason = sprintf("Ha lasciato %s perché aveva al massimo 2 punti.", xcape($user->name));
176 $table->game_init(&$bri->user);
178 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
179 $user_cur = &$bri->user[$table->player[$i]];
181 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
182 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
183 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
184 $user_cur->step_inc();
187 else if ($argz[0] == 'asta') {
190 $index_cur = $table->gstart % BIN5_PLAYERS_N;
191 if ($user->table_pos == $index_cur &&
192 $table->asta_pla[$index_cur]) {
196 log_wr("CI SIAMO a_card ".$a_card." asta_card ".$table->asta_card);
198 // Abbandono dell'asta
200 log_wr("Abbandona l'asta.");
201 $table->asta_pla[$index_cur] = FALSE;
202 $user->asta_card = -1;
203 $table->asta_pla_n--;
206 else if ($a_card <= 9) {
207 if ($a_card >= 0 && $a_card < 9 && $a_card > $table->asta_card)
209 else if ($a_card == 9 && $a_pnt > ($table->asta_pnt >= 61 ? $table->asta_pnt : 60) && $a_pnt <= 120)
213 if ($again == FALSE) {
214 log_wr("NUOVI ORZI.");
215 $user->asta_card = $a_card;
216 $table->asta_card = $a_card;
218 $user->asta_pnt = $a_pnt;
219 $table->asta_pnt = $a_pnt;
226 if ($again) { // Qualcosa non andato bene, rifare
227 $ret = sprintf('gst.st = %d; asta_pnt_set(%d);', $user->step+1,
228 ($table->asta_pnt > 60 ? $table->asta_pnt + 1 : 61) );
229 $user->comm[$user->step % COMM_N] = $ret;
236 $showst = "show_astat(";
237 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
238 $user_cur = &$bri->user[$table->player[$i]];
239 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
240 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
242 if (BIN5_PLAYERS_N == 3)
247 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
248 $user_cur = &$bri->user[$table->player[$i]];
249 if ($maxcard < $user_cur->asta_card)
250 $maxcard = $user_cur->asta_card;
253 if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
254 !($table->asta_card == 9 && $table->asta_pnt == 120)) {
255 log_wr("ALLOPPA QUI");
256 for ($i = 1 ; $i < BIN5_PLAYERS_N ; $i++) {
257 $index_next = ($table->gstart + $i) % BIN5_PLAYERS_N;
258 if ($table->asta_pla[$index_next]) {
260 $table->gstart += $i;
266 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
267 $user_cur = &$bri->user[$table->player[$i]];
268 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
269 if ($user_cur->table_pos == ($table->gstart % BIN5_PLAYERS_N))
270 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_on();',
271 $table->asta_card + 1, $table->asta_pnt+1, ($user_cur->handpt <= 2 ? "true" : "false"));
273 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_off();',
274 $table->asta_card + 1, -($table->asta_pnt+1), ($user_cur->handpt <= 2 ? "true" : "false"));
275 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
276 $user_cur->step_inc();
279 else if ($table->asta_pla_n == 0) {
280 log_wr("PASSANO TUTTI!");
282 log_wr(sprintf("GIOCO FINITO !!!"));
284 $table->old_reason = "Hanno passato tutti.";
288 $table->game_init(&$bri->user);
290 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
291 $user_cur = &$bri->user[$table->player[$i]];
293 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
294 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
295 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
296 $user_cur->step_inc();
301 // if a_pnt == 120 supergame ! else abbandono
302 if ($a_pnt == 120 || $user->asta_card != -1) {
303 $chooser = $index_cur;
304 for ($i = 1 ; $i < BIN5_PLAYERS_N ; $i++)
306 $table->asta_pla[$i] = FALSE;
310 $user->comm[$user->step % COMM_N] = sprintf( "gst.st = %d; dispose_asta(%d, %d, false); remark_off();",
311 $user->step+1, $table->asta_card + 1,-($table->asta_pnt));
314 for ($i = 1 ; $i < BIN5_PLAYERS_N ; $i++) {
315 $chooser = ($table->gstart + $i) % BIN5_PLAYERS_N;
316 if ($table->asta_pla[$chooser]) {
321 $table->asta_win = $chooser;
323 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
324 $user_cur = &$bri->user[$table->player[$i]];
325 $ret = sprintf('gst.st = %d; %s dispose_asta(%d, %d, false);', $user_cur->step+1, $showst,
326 $table->asta_card + 1,-($table->asta_pnt));
328 if ($i == $chooser) {
329 $ret .= "choose_seed(". $table->asta_card."); remark_on();";
332 $ret .= "remark_off();";
335 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
336 $user_cur->step_inc();
342 log_wr("NON CI SIAMO");
346 else if ($argz[0] == 'choose') {
347 if ($table->asta_win > -1 &&
348 $user->table_pos == $table->asta_win) {
349 $a_brisco = $argz[1];
350 if ($a_brisco >= 0 && $a_brisco < 40) {
351 $table->briscola = $a_brisco;
352 $table->friend = $table->card[$a_brisco]->owner;
354 $table->gstart = ($table->mazzo+1) % BIN5_PLAYERS_N;
355 log_wr("Setta la briscola a ".$a_brisco);
357 $chooser = $table->asta_win;
358 $user_chooser = &$bri->user[$table->player[$chooser]];
359 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
360 $user_cur = &$bri->user[$table->player[$i]];
361 $user_cur->subst = 'game';
362 $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
364 if ($user_cur->privflags & BIN5_USER_FLAG_RING_ENDAUCT) {
365 // $ret .= "var de_che= 33;";
366 $ret .= playsound("ringbell.mp3");
368 $ret .= sprintf('document.title = "Brisk - Tavolo %d";', $user->table_orig);
370 /* bg of caller cell */
371 $ret .= briscola_show($bri, $table, $user_cur);
374 if ($i == ($table->gstart % BIN5_PLAYERS_N))
375 $ret .= "is_my_time = true; remark_on();";
377 $ret .= "is_my_time = false; remark_off();";
379 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
380 $user_cur->step_inc();
383 TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
384 VAR PER PASSARE ALLA FASE DI GIOCO
391 else if ($user->subst == 'game') {
392 log_wr("state: table::game".$argz[0]);
394 if ($argz[0] == 'play') {
399 if (strpos($a_x, "px") != FALSE)
400 $a_x = substr($a_x,0,-2);
401 if (strpos($a_y, "px") != FALSE)
402 $a_y = substr($a_y,0,-2);
404 $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
405 $a_play, $user->table_pos, ($table->gstart % BIN5_PLAYERS_N),
406 $table->mazzo, $table->gstart,
407 $table->card[$a_play]->stat, $table->card[$a_play]->owner);
408 log_wr("CIC".$loggo);
410 /* se era il suo turno e la carta era sua ed era in mano */
411 if ($a_play >=0 && $a_play < (BIN5_PLAYERS_N == 5 ? 40 : 24) &&
412 ($user->table_pos == (($table->gstart + $table->turn) % BIN5_PLAYERS_N)) &&
413 $table->card[$a_play]->stat == 'hand' &&
414 $table->card[$a_play]->owner == $user->table_pos) {
415 log_wr(sprintf("User: %s Play: %d",$user->name, $a_play));
417 /* Change the card status. */
418 $table->card[$a_play]->play($a_x, $a_y);
421 * !!!! TURN INCREMENTED BEFORE !!!!
423 $turn_cur = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
426 $card_play = sprintf("card_play(%d,%d,%d,%d);|",
427 $user->table_pos, $a_play, $a_x, $a_y);
428 if (($table->turn % BIN5_PLAYERS_N) != 0) { /* manche not finished */
429 $turn_nex = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
431 $player_cur = "remark_off();";
432 $player_nex = $card_play . "is_my_time = true; remark_on();";
433 $player_oth = $card_play;
435 else if ($table->turn <= (BIN5_PLAYERS_N * 8)) { /* manche finished */
436 $winner = calculate_winner($table);
438 $table->gstart = $winner;
439 $turn_nex = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
441 log_wr(sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
442 $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|", $winner);
443 $player_cur = "remark_off();" . $card_take;
444 if ($turn_cur != $turn_nex)
445 $player_nex = $card_play . $card_take;
448 if ($table->turn < (BIN5_PLAYERS_N * 8)) /* game NOT finished */
449 $player_nex .= "is_my_time = true; remark_on();";
450 $player_oth = $card_play . $card_take;
453 log_wr(sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
454 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
455 $user_cur = &$bri->user[$table->player[$i]];
457 $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
460 if ($i == $turn_cur) {
463 if ($i == $turn_nex) {
466 if ($i != $turn_cur && $i != $turn_nex) {
473 if ($table->turn == (BIN5_PLAYERS_N * 8)) { /* game finished */
474 log_wr(sprintf("GIOCO FINITO !!!"));
477 /* ************************************************ */
478 /* PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO */
479 /* ************************************************ */
480 $pt_cur = calculate_points(&$table);
482 $plist = "$table->table_token|$user->table_orig|$table->player_n";
485 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
486 $user_cur = &$bri->user[$table->player[$i]];
487 $plist .= '|'.xcapelt($user_cur->name).'|'.$pt_cur[$i];
488 $ucodes[$i] = $user_cur->code_get();
490 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
491 $plist .= '|'.xcapelt($ucodes[$i]);
493 log_legal($curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
494 if ($user->table_orig < TABLES_AUTH_N) {
495 require_once("../Obj/dbase_".$G_dbasetype.".phh");
497 if (($bdb = BriskDB::create()) != FALSE) {
498 $bdb->bin5_points_save($curtime, $table->table_token, $user->table_orig, $ucodes, $pt_cur);
502 log_points($curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED");
504 log_points($curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
508 $table->game_init(&$bri->user);
510 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
511 $user_cur = &$bri->user[$table->player[$i]];
512 $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
517 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
518 $user_cur = &$bri->user[$table->player[$i]];
520 $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
521 $user_cur->step_inc();
524 log_wr(sprintf("TURN: %d",$table->turn));
525 /* Have played all the players ? */
526 /* NO: switch the focus and enable the next player to play. */
528 /* YES: calculate who win and go to the next turn. */
535 log_wr("before save data");
536 Bin5::save_data($bri);
537 log_mop($user->step, 'bin::index_wr.php: after save_data()');
539 Bin5::unlock_data($sem);