5 * Copyright (C) 2006-2012 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.
41 function bin5_index_wr_main(&$bri, $remote_addr_full, $get, $post, $cookie)
43 GLOBAL $G_base, $G_dbasetype;
45 $remote_addr = addrtoipv4($remote_addr_full);
52 if (($mesg = gpcs_var('mesg', $get, $post, $cookie)) === FALSE)
55 if (($sess = gpcs_var('sess', $get, $post, $cookie)) === FALSE)
58 log_wr('COMM: '.$mesg);
61 if (($CO_bin5_pref_ring_endauct = gpcs_var('CO_bin5_pref_ring_endauct', $get, $post, $cookie)) === FALSE)
62 $CO_bin5_pref_ring_endauct = "";
66 log_wr(0, 'bin::index_wr.php: COMM: '.xcapemesg($mesg));
69 if (($user = &$bri->get_user($sess, &$idx)) == FALSE) {
70 echo "Get User Error";
71 log_wr("Get User Error");
74 $argz = explode('|', $mesg);
76 log_wr('POSTSPLIT: '.$argz[0].' user->stat: ['.$user->stat.']');
77 log_wr($user->step, 'bin::index_wr.php: after get_user()');
79 $user->lacc = $curtime;
81 if ($argz[0] == 'ping') {
82 log_wr("PING RECEIVED");
84 else if (false && $argz[0] == 'shutdown') {
85 log_auth($user_cur->sess, "Shutdown session. delegate to room gc the autologout");
87 log_rd2("bin5/index_wr.php: AUTO LOGOUT.");
88 if ($user->stat == 'table') {
89 $bri->table_wakeup($user);
90 // to force the logout
94 log_rd2("SHUTDOWN FROM WHAT ???");
96 /*********************
100 *********************/
101 else if ($user->stat == 'table') {
102 $user->laccwr = time();
103 $table = $bri->table[$user->table];
105 if ($argz[0] == 'tableinfo') {
106 log_wr("PER DI TABLEINFO");
107 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
108 $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
109 log_wr($user->comm[$user->step % COMM_N]);
112 else if ($argz[0] == 'chatt') {
113 $bri->chatt_send(&$user,$mesg);
115 else if ($argz[0] == 'preferences_update') {
116 log_wr("PER DI PREFERENCES_UPDATE");
118 if ($CO_bin5_pref_ring_endauct == "true")
119 $user->privflags |= BIN5_USER_FLAG_RING_ENDAUCT;
121 $user->privflags &= ~BIN5_USER_FLAG_RING_ENDAUCT;
123 else if ($argz[0] == 'logout') {
126 if ($user->exitislock == TRUE) {
128 $user->exitislock = FALSE;
133 $lockcalc = $table->exitlock_calc(&$bri->user, $user->table_pos);
135 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
136 $user->comm[$user->step % COMM_N] .= $table->exitlock_show(&$bri->user, $user->table_pos);
137 $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);
139 log_wr($user->comm[$user->step % COMM_N]);
141 $logout_cont = FALSE;
145 require_once("../Obj/hardban.phh");
146 Hardbans::add(($user->flags & USER_FLAG_AUTH ? $user->name : FALSE),
147 $user->ip, $user->sess, $user->laccwr + BAN_TIME);
149 // $user->bantime = $user->laccwr + BAN_TIME;
151 if ($logout_cont == TRUE) {
152 $bri->table_wakeup(&$user);
155 else if ($argz[0] == 'exitlock') {
156 if ($user->exitislock == TRUE) {
157 $user->exitislock = ($user->exitislock == TRUE ? FALSE : TRUE);
158 for ($ct = 0, $i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
159 $user_cur[$i] = &$bri->user[$table->player[$i]];
160 if ($user_cur[$i]->exitislock == FALSE)
163 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
164 $ret = sprintf('gst.st = %d;', $user_cur[$i]->step+1);
165 $ret .= sprintf('exitlock_show(%d, %s);', $ct,
166 ($user_cur[$i]->exitislock ? 'true' : 'false'));
167 $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N] = $ret;
168 log_wr($user_cur[$i]->comm[$user_cur[$i]->step % COMM_N]);
169 $user_cur[$i]->step_inc();
173 else if ($user->subst == 'asta') {
174 if ($argz[0] == 'lascio' && $user->handpt <= 2) {
175 $index_cur = $table->gstart % BIN5_PLAYERS_N;
177 log_wr(sprintf("GIOCO FINITO !!!"));
180 $table->old_reason = sprintf("Ha lasciato %s perché aveva al massimo 2 punti.", xcape($user->name));
182 // Non si cambia mazzo se si abbandona la partita
183 // $table->game_next();
184 $table->game_init(&$bri->user);
186 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
187 $user_cur = &$bri->user[$table->player[$i]];
189 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
190 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
191 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
192 $user_cur->step_inc();
195 else if ($argz[0] == 'asta') {
198 $index_cur = $table->gstart % BIN5_PLAYERS_N;
199 if ($user->table_pos == $index_cur &&
200 $table->asta_pla[$index_cur]) {
204 log_wr("CI SIAMO a_card ".$a_card." asta_card ".$table->asta_card);
206 // Abbandono dell'asta
208 log_wr("Abbandona l'asta.");
209 $table->asta_pla[$index_cur] = FALSE;
210 $user->asta_card = -1;
211 $table->asta_pla_n--;
214 else if ($a_card <= 9) {
215 if ($a_card >= 0 && $a_card < 9 && $a_card > $table->asta_card)
217 else if ($a_card == 9 && $a_pnt > ($table->asta_pnt >= 61 ? $table->asta_pnt : 60) && $a_pnt <= 120)
221 if ($again == FALSE) {
222 log_wr("NUOVI ORZI.");
223 $user->asta_card = $a_card;
224 $table->asta_card = $a_card;
226 $user->asta_pnt = $a_pnt;
227 $table->asta_pnt = $a_pnt;
234 if ($again) { // Qualcosa non andato bene, rifare
235 $ret = sprintf('gst.st = %d; asta_pnt_set(%d);', $user->step+1,
236 ($table->asta_pnt > 60 ? $table->asta_pnt + 1 : 61) );
237 $user->comm[$user->step % COMM_N] = $ret;
244 $showst = "show_astat(";
245 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
246 $user_cur = &$bri->user[$table->player[$i]];
247 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
248 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
250 if (BIN5_PLAYERS_N == 3)
255 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
256 $user_cur = &$bri->user[$table->player[$i]];
257 if ($maxcard < $user_cur->asta_card)
258 $maxcard = $user_cur->asta_card;
261 if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
262 !($table->asta_card == 9 && $table->asta_pnt == 120)) {
263 log_wr("ALLOPPA QUI");
264 for ($i = 1 ; $i < BIN5_PLAYERS_N ; $i++) {
265 $index_next = ($table->gstart + $i) % BIN5_PLAYERS_N;
266 if ($table->asta_pla[$index_next]) {
268 $table->gstart += $i;
274 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
275 $user_cur = &$bri->user[$table->player[$i]];
276 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
277 if ($user_cur->table_pos == ($table->gstart % BIN5_PLAYERS_N))
278 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_on();',
279 $table->asta_card + 1, $table->asta_pnt+1, ($user_cur->handpt <= 2 ? "true" : "false"));
281 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_off();',
282 $table->asta_card + 1, -($table->asta_pnt+1), ($user_cur->handpt <= 2 ? "true" : "false"));
283 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
284 $user_cur->step_inc();
287 else if ($table->asta_pla_n == 0) {
288 log_wr("PASSANO TUTTI!");
290 log_wr(sprintf("GIOCO FINITO !!!"));
292 $table->old_reason = "Hanno passato tutti.";
296 $table->game_init(&$bri->user);
298 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
299 $user_cur = &$bri->user[$table->player[$i]];
301 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
302 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
303 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
304 $user_cur->step_inc();
309 // if a_pnt == 120 supergame ! else abbandono
310 if ($a_pnt == 120 || $user->asta_card != -1) {
311 $chooser = $index_cur;
312 for ($i = 1 ; $i < BIN5_PLAYERS_N ; $i++)
314 $table->asta_pla[$i] = FALSE;
318 $user->comm[$user->step % COMM_N] = sprintf( "gst.st = %d; dispose_asta(%d, %d, false); remark_off();",
319 $user->step+1, $table->asta_card + 1,-($table->asta_pnt));
322 for ($i = 1 ; $i < BIN5_PLAYERS_N ; $i++) {
323 $chooser = ($table->gstart + $i) % BIN5_PLAYERS_N;
324 if ($table->asta_pla[$chooser]) {
329 $table->asta_win = $chooser;
331 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
332 $user_cur = &$bri->user[$table->player[$i]];
333 $ret = sprintf('gst.st = %d; %s dispose_asta(%d, %d, false);', $user_cur->step+1, $showst,
334 $table->asta_card + 1,-($table->asta_pnt));
336 if ($i == $chooser) {
337 $ret .= "choose_seed(". $table->asta_card."); remark_on();";
340 $ret .= "remark_off();";
343 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
344 $user_cur->step_inc();
350 log_wr("NON CI SIAMO");
354 else if ($argz[0] == 'choose') {
355 if ($table->asta_win > -1 &&
356 $user->table_pos == $table->asta_win) {
357 $a_brisco = $argz[1];
358 if ($a_brisco >= 0 && $a_brisco < 40) {
359 $table->briscola = $a_brisco;
360 $table->friend = $table->card[$a_brisco]->owner;
362 $table->gstart = ($table->mazzo+1) % BIN5_PLAYERS_N;
363 log_wr("Setta la briscola a ".$a_brisco);
365 $chooser = $table->asta_win;
366 $user_chooser = &$bri->user[$table->player[$chooser]];
367 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
368 $user_cur = &$bri->user[$table->player[$i]];
369 $user_cur->subst = 'game';
370 $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
372 if ($user_cur->privflags & BIN5_USER_FLAG_RING_ENDAUCT) {
373 // $ret .= "var de_che= 33;";
374 $ret .= playsound("ringbell.mp3");
376 $ret .= sprintf('document.title = "Brisk - Tavolo %d";', $user->table_orig);
378 /* bg of caller cell */
379 $ret .= briscola_show($bri, $table, $user_cur);
382 if ($i == ($table->gstart % BIN5_PLAYERS_N))
383 $ret .= "is_my_time = true; remark_on();";
385 $ret .= "is_my_time = false; remark_off();";
387 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
388 $user_cur->step_inc();
391 TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
392 VAR PER PASSARE ALLA FASE DI GIOCO
399 else if ($user->subst == 'game') {
400 log_wr("state: table::game".$argz[0]);
402 if ($argz[0] == 'play') {
407 if (strpos($a_x, "px") != FALSE)
408 $a_x = substr($a_x,0,-2);
409 if (strpos($a_y, "px") != FALSE)
410 $a_y = substr($a_y,0,-2);
412 $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
413 $a_play, $user->table_pos, ($table->gstart % BIN5_PLAYERS_N),
414 $table->mazzo, $table->gstart,
415 $table->card[$a_play]->stat, $table->card[$a_play]->owner);
416 log_wr("CIC".$loggo);
418 /* se era il suo turno e la carta era sua ed era in mano */
419 if ($a_play >=0 && $a_play < (BIN5_PLAYERS_N == 5 ? 40 : 24) &&
420 ($user->table_pos == (($table->gstart + $table->turn) % BIN5_PLAYERS_N)) &&
421 $table->card[$a_play]->stat == 'hand' &&
422 $table->card[$a_play]->owner == $user->table_pos) {
423 log_wr(sprintf("User: %s Play: %d",$user->name, $a_play));
425 /* Change the card status. */
426 $table->card[$a_play]->play($a_x, $a_y);
429 * !!!! TURN INCREMENTED BEFORE !!!!
431 $turn_cur = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
434 $card_play = sprintf("card_play(%d,%d,%d,%d);|",
435 $user->table_pos, $a_play, $a_x, $a_y);
436 if (($table->turn % BIN5_PLAYERS_N) != 0) { /* manche not finished */
437 $turn_nex = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
439 $player_cur = "remark_off();";
440 $player_nex = $card_play . "is_my_time = true; remark_on();";
441 $player_oth = $card_play;
443 else if ($table->turn <= (BIN5_PLAYERS_N * 8)) { /* manche finished */
444 $winner = calculate_winner($table);
446 $table->gstart = $winner;
447 $turn_nex = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
449 log_wr(sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
450 $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|", $winner);
451 $player_cur = "remark_off();" . $card_take;
452 if ($turn_cur != $turn_nex)
453 $player_nex = $card_play . $card_take;
456 if ($table->turn < (BIN5_PLAYERS_N * 8)) /* game NOT finished */
457 $player_nex .= "is_my_time = true; remark_on();";
458 $player_oth = $card_play . $card_take;
461 log_wr(sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
462 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
463 $user_cur = &$bri->user[$table->player[$i]];
465 $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
468 if ($i == $turn_cur) {
471 if ($i == $turn_nex) {
474 if ($i != $turn_cur && $i != $turn_nex) {
481 if ($table->turn == (BIN5_PLAYERS_N * 8)) { /* game finished */
482 log_wr(sprintf("GIOCO FINITO !!!"));
485 /* ************************************************ */
486 /* PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO */
487 /* ************************************************ */
488 $pt_cur = calculate_points(&$table);
490 $plist = "$table->table_token|$user->table_orig|$table->player_n";
492 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
493 $user_cur = &$bri->user[$table->player[$i]];
494 $plist .= '|'.xcapelt($user_cur->name).'|'.$pt_cur[$i];
495 $ucodes[$i] = $user_cur->code_get();
497 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
498 $plist .= '|'.xcapelt($ucodes[$i]);
500 log_legal($curtime, $user->ip, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
501 if ($user->table_orig < TABLES_AUTH_N) {
502 require_once("../Obj/dbase_".$G_dbasetype.".phh");
504 if (($bdb = BriskDB::create()) != FALSE) {
505 $bdb->bin5_points_save($curtime, $table->table_token, $user->table_orig, $ucodes, $pt_cur);
509 log_points($remote_addr, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED");
511 log_points($curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
515 $table->game_init(&$bri->user);
517 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
518 $user_cur = &$bri->user[$table->player[$i]];
519 $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
524 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
525 $user_cur = &$bri->user[$table->player[$i]];
527 $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
528 $user_cur->step_inc();
531 log_wr(sprintf("TURN: %d",$table->turn));
532 /* Have played all the players ? */
533 /* NO: switch the focus and enable the next player to play. */
535 /* YES: calculate who win and go to the next turn. */
542 log_wr("before save data");
543 log_wr($user->step, 'bin::index_wr.php: after save_data()');