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, $G_black_list;
45 $remote_addr = addrtoipv4($remote_addr_full);
47 if (array_search($remote_addr, $G_black_list) !== FALSE) {
48 // TODO: waiting async 5 sec before close
57 if (($mesg = gpcs_var('mesg', $get, $post, $cookie)) === FALSE)
60 if (($sess = gpcs_var('sess', $get, $post, $cookie)) === FALSE)
63 log_wr('COMM: '.$mesg);
66 if (($CO_bin5_pref_ring_endauct = gpcs_var('CO_bin5_pref_ring_endauct', $get, $post, $cookie)) === FALSE)
67 $CO_bin5_pref_ring_endauct = "";
71 log_wr(0, 'bin::index_wr.php: COMM: '.xcapemesg($mesg));
74 if (($user = &$bri->get_user($sess, &$idx)) == FALSE) {
75 echo "Get User Error";
76 log_wr("Get User Error");
80 if (array_search($user->ip, $G_black_list) !== FALSE) {
81 // TODO: waiting async 5 sec before close
85 $argz = explode('|', $mesg);
87 log_wr('POSTSPLIT: '.$argz[0].' user->stat: ['.$user->stat.']');
88 log_wr($user->step, 'bin::index_wr.php: after get_user()');
90 $user->lacc = $curtime;
92 if ($argz[0] == 'ping') {
93 log_wr("PING RECEIVED");
95 else if (false && $argz[0] == 'shutdown') {
96 log_auth($user_cur->sess, "Shutdown session. delegate to room gc the autologout");
98 log_rd2("bin5/index_wr.php: AUTO LOGOUT.");
99 if ($user->stat == 'table') {
100 $bri->table_wakeup($user);
101 // to force the logout
105 log_rd2("SHUTDOWN FROM WHAT ???");
107 /*********************
111 *********************/
112 else if ($user->stat == 'table') {
113 $user->laccwr = time();
114 $table = $bri->table[$user->table];
116 if ($argz[0] == 'tableinfo') {
117 log_wr("PER DI TABLEINFO");
118 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
119 $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
120 log_wr($user->comm[$user->step % COMM_N]);
123 else if ($argz[0] == 'chatt') {
124 $bri->chatt_send(&$user,$mesg);
126 else if ($argz[0] == 'preferences_update') {
127 log_wr("PER DI PREFERENCES_UPDATE");
129 if ($CO_bin5_pref_ring_endauct == "true")
130 $user->privflags |= BIN5_USER_FLAG_RING_ENDAUCT;
132 $user->privflags &= ~BIN5_USER_FLAG_RING_ENDAUCT;
134 else if ($argz[0] == 'logout') {
137 if ($user->exitislock == TRUE) {
139 $user->exitislock = FALSE;
144 $lockcalc = $table->exitlock_calc(&$bri->user, $user->table_pos);
146 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
147 $user->comm[$user->step % COMM_N] .= $table->exitlock_show(&$bri->user, $user->table_pos);
148 $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);
150 log_wr($user->comm[$user->step % COMM_N]);
152 $logout_cont = FALSE;
156 require_once("../Obj/hardban.phh");
157 Hardbans::add(($user->flags & USER_FLAG_AUTH ? $user->name : FALSE),
158 $user->ip, $user->sess, $user->laccwr + BAN_TIME);
160 // $user->bantime = $user->laccwr + BAN_TIME;
162 if ($logout_cont == TRUE) {
163 $bri->table_wakeup(&$user);
166 else if ($argz[0] == 'exitlock') {
167 if ($user->exitislock == TRUE) {
168 $user->exitislock = ($user->exitislock == TRUE ? FALSE : TRUE);
169 for ($ct = 0, $i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
170 $user_cur[$i] = &$bri->user[$table->player[$i]];
171 if ($user_cur[$i]->exitislock == FALSE)
174 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
175 $ret = sprintf('gst.st = %d;', $user_cur[$i]->step+1);
176 $ret .= sprintf('exitlock_show(%d, %s);', $ct,
177 ($user_cur[$i]->exitislock ? 'true' : 'false'));
178 $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N] = $ret;
179 log_wr($user_cur[$i]->comm[$user_cur[$i]->step % COMM_N]);
180 $user_cur[$i]->step_inc();
184 else if ($user->subst == 'asta') {
185 if ($argz[0] == 'lascio') {
186 // && $user->handpt <= 2) {
187 /* $index_cur = $table->gstart % BIN5_PLAYERS_N; */
189 /* log_wr(sprintf("GIOCO FINITO !!!")); */
191 /* $table->mult += 1; */
192 /* $table->old_reason = sprintf("Ha lasciato %s perché aveva al massimo 2 punti.", xcape($user->name)); */
194 /* // Non si cambia mazzo se si abbandona la partita */
195 /* $table->game_next(0); */
197 /* if ($user->table_orig < TABLES_AUTH_N) { */
198 /* require_once("../Obj/dbase_".$G_dbasetype.".phh"); */
200 /* if (($bdb = BriskDB::create()) != FALSE) { */
201 /* $bdb->bin5_points_save($curtime, $table, $user->table_orig, $ucodes, $pt_cur); */
205 /* log_points($remote_addr, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED"); */
207 /* log_points($curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist); */
210 /* $table->game_init(&$bri->user); */
212 if ($table->rules_engine(&$bri, $curtime, BIN5_RULES_ABANDON, $user)) {
213 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
214 $user_cur = &$bri->user[$table->player[$i]];
216 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
217 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
218 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
219 $user_cur->step_inc();
223 else if ($argz[0] == 'asta') {
226 $index_cur = $table->gstart % BIN5_PLAYERS_N;
227 if ($user->table_pos == $index_cur &&
228 $table->asta_pla[$index_cur]) {
232 log_wr("CI SIAMO a_card ".$a_card." asta_card ".$table->asta_card);
234 // Abbandono dell'asta
236 log_wr("Abbandona l'asta.");
237 $table->asta_pla[$index_cur] = FALSE;
238 $user->asta_card = -1;
239 $table->asta_pla_n--;
242 else if ($a_card <= 9) {
243 if ($a_card >= 0 && $a_card < 9 && $a_card > $table->asta_card)
245 else if ($a_card == 9 && $a_pnt > ($table->asta_pnt >= 61 ? $table->asta_pnt : 60) && $a_pnt <= 120)
249 if ($again == FALSE) {
250 log_wr("NUOVI ORZI.");
251 $user->asta_card = $a_card;
252 $table->asta_card = $a_card;
254 $user->asta_pnt = $a_pnt;
255 $table->asta_pnt = $a_pnt;
262 if ($again) { // Qualcosa non andato bene, rifare
263 $ret = sprintf('gst.st = %d; asta_pnt_set(%d);', $user->step+1,
264 ($table->asta_pnt > 60 ? $table->asta_pnt + 1 : 61) );
265 $user->comm[$user->step % COMM_N] = $ret;
272 $showst = "show_astat(";
273 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
274 $user_cur = &$bri->user[$table->player[$i]];
275 $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
276 ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
278 if (BIN5_PLAYERS_N == 3)
283 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
284 $user_cur = &$bri->user[$table->player[$i]];
285 if ($maxcard < $user_cur->asta_card)
286 $maxcard = $user_cur->asta_card;
289 if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
290 !($table->asta_card == 9 && $table->asta_pnt == 120)) {
291 log_wr("ALLOPPA QUI");
292 for ($i = 1 ; $i < BIN5_PLAYERS_N ; $i++) {
293 $index_next = ($table->gstart + $i) % BIN5_PLAYERS_N;
294 if ($table->asta_pla[$index_next]) {
296 $table->gstart += $i;
302 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
303 $user_cur = &$bri->user[$table->player[$i]];
304 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
305 if ($user_cur->table_pos == ($table->gstart % BIN5_PLAYERS_N))
306 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_on();',
307 $table->asta_card + 1, $table->asta_pnt+1, ($user_cur->handpt <= 2 ? "true" : "false"));
309 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_off();',
310 $table->asta_card + 1, -($table->asta_pnt+1), ($user_cur->handpt <= 2 ? "true" : "false"));
311 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
312 $user_cur->step_inc();
315 else if ($table->asta_pla_n == 0) {
316 log_wr("PASSANO TUTTI!");
318 if ($table->rules_engine(&$bri, $curtime, BIN5_RULES_ALLPASS, $user)) {
319 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
320 $user_cur = &$bri->user[$table->player[$i]];
322 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
323 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
324 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
325 $user_cur->step_inc();
331 // if a_pnt == 120 supergame ! else abbandono
332 if ($a_pnt == 120 || $user->asta_card != -1) {
333 $chooser = $index_cur;
334 for ($i = 1 ; $i < BIN5_PLAYERS_N ; $i++)
336 $table->asta_pla[$i] = FALSE;
340 $user->comm[$user->step % COMM_N] = sprintf( "gst.st = %d; dispose_asta(%d, %d, false); remark_off();",
341 $user->step+1, $table->asta_card + 1,-($table->asta_pnt));
344 for ($i = 1 ; $i < BIN5_PLAYERS_N ; $i++) {
345 $chooser = ($table->gstart + $i) % BIN5_PLAYERS_N;
346 if ($table->asta_pla[$chooser]) {
351 $table->asta_win = $chooser;
353 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
354 $user_cur = &$bri->user[$table->player[$i]];
355 $ret = sprintf('gst.st = %d; %s dispose_asta(%d, %d, false);', $user_cur->step+1, $showst,
356 $table->asta_card + 1,-($table->asta_pnt));
358 if ($i == $chooser) {
359 $ret .= "choose_seed(". $table->asta_card."); remark_on();";
362 $ret .= "remark_off();";
365 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
366 $user_cur->step_inc();
372 log_wr("NON CI SIAMO");
376 else if ($argz[0] == 'choose') {
377 if ($table->asta_win > -1 &&
378 $user->table_pos == $table->asta_win) {
379 $a_brisco = $argz[1];
380 if ($a_brisco >= 0 && $a_brisco < 40) {
381 $table->briscola = $a_brisco;
382 $table->friend = $table->card[$a_brisco]->owner;
384 $table->gstart = ($table->mazzo+1) % BIN5_PLAYERS_N;
385 log_wr("Setta la briscola a ".$a_brisco);
387 $chooser = $table->asta_win;
388 $user_chooser = &$bri->user[$table->player[$chooser]];
389 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
390 $user_cur = &$bri->user[$table->player[$i]];
391 $user_cur->subst = 'game';
392 $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
394 if ($user_cur->privflags & BIN5_USER_FLAG_RING_ENDAUCT) {
395 // $ret .= "var de_che= 33;";
396 $ret .= playsound("ringbell.mp3");
398 $ret .= sprintf('document.title = "Brisk - Tavolo %d";', $user->table_orig);
400 /* bg of caller cell */
401 $ret .= briscola_show($bri, $table, $user_cur);
404 if ($i == ($table->gstart % BIN5_PLAYERS_N))
405 $ret .= "is_my_time = true; remark_on();";
407 $ret .= "is_my_time = false; remark_off();";
409 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
410 $user_cur->step_inc();
413 TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
414 VAR PER PASSARE ALLA FASE DI GIOCO
421 else if ($user->subst == 'game') {
422 log_wr("state: table::game".$argz[0]);
424 if ($argz[0] == 'play') {
429 if (strpos($a_x, "px") != FALSE)
430 $a_x = substr($a_x,0,-2);
431 if (strpos($a_y, "px") != FALSE)
432 $a_y = substr($a_y,0,-2);
434 $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
435 $a_play, $user->table_pos, ($table->gstart % BIN5_PLAYERS_N),
436 $table->mazzo, $table->gstart,
437 $table->card[$a_play]->stat, $table->card[$a_play]->owner);
438 log_wr("CIC".$loggo);
440 /* se era il suo turno e la carta era sua ed era in mano */
441 if ($a_play >=0 && $a_play < (BIN5_CARD_HAND * BIN5_PLAYERS_N) &&
442 ($user->table_pos == (($table->gstart + $table->turn) % BIN5_PLAYERS_N)) &&
443 $table->card[$a_play]->stat == 'hand' &&
444 $table->card[$a_play]->owner == $user->table_pos) {
445 log_wr(sprintf("User: %s Play: %d",$user->name, $a_play));
447 /* Change the card status. */
448 $table->card[$a_play]->play($a_x, $a_y);
451 * !!!! TURN INCREMENTED BEFORE !!!!
453 $turn_cur = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
456 $card_play = sprintf("card_play(%d,%d,%d,%d);|",
457 $user->table_pos, $a_play, $a_x, $a_y);
458 if (($table->turn % BIN5_PLAYERS_N) != 0) { /* manche not finished */
459 $turn_nex = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
461 $player_cur = "remark_off();";
462 $player_nex = $card_play . "is_my_time = true; remark_on();";
463 $player_oth = $card_play;
465 else if ($table->turn <= (BIN5_PLAYERS_N * BIN5_CARD_HAND)) { /* manche finished */
466 $winner = calculate_winner($table);
468 $table->gstart = $winner;
469 $turn_nex = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
471 log_wr(sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
472 $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|", $winner);
473 $player_cur = "remark_off();" . $card_take;
474 if ($turn_cur != $turn_nex)
475 $player_nex = $card_play . $card_take;
478 if ($table->turn < (BIN5_PLAYERS_N * BIN5_CARD_HAND)) /* game NOT finished */
479 $player_nex .= "is_my_time = true; remark_on();";
480 $player_oth = $card_play . $card_take;
483 log_wr(sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
484 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
485 $user_cur = &$bri->user[$table->player[$i]];
487 $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
490 if ($i == $turn_cur) {
493 if ($i == $turn_nex) {
496 if ($i != $turn_cur && $i != $turn_nex) {
503 if ($table->turn == (BIN5_PLAYERS_N * BIN5_CARD_HAND)) { /* game finished */
504 log_wr(sprintf("GIOCO FINITO !!!"));
506 if ($table->rules_engine(&$bri, $curtime, BIN5_RULES_FINISH, $user)) {
507 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
508 $user_cur = &$bri->user[$table->player[$i]];
509 $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
514 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
515 $user_cur = &$bri->user[$table->player[$i]];
517 $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
518 $user_cur->step_inc();
521 log_wr(sprintf("TURN: %d",$table->turn));
522 /* Have played all the players ? */
523 /* NO: switch the focus and enable the next player to play. */
525 /* YES: calculate who win and go to the next turn. */
532 log_wr("before save data");
533 log_wr($user->step, 'bin::index_wr.php: after save_data()');