command '/cont' added to already started tables, it replace the turned off '/nick...
authorMatteo Nastasi (mop) <nastasi@alternativeoutput.it>
Thu, 31 Oct 2013 17:19:31 +0000 (18:19 +0100)
committerMatteo Nastasi (mop) <nastasi@alternativeoutput.it>
Thu, 31 Oct 2013 17:19:31 +0000 (18:19 +0100)
web/briskin5/Obj/briskin5.phh
web/briskin5/stat-day.php

index 211678a..99085a8 100644 (file)
@@ -295,6 +295,8 @@ class Bin5_table extends Table {
         $thiz->card = array();
         $thiz->bunch_create();
         $thiz->mazzo    = rand(0,PLAYERS_N-1);
+        $thiz->points = array();
+        $thiz->total  = array();
         $thiz->points_n = 0;
         $thiz->mult     = 0;
 
@@ -649,6 +651,137 @@ class Bin5_table extends Table {
         return (TRUE);
     }
 
+    function match_continue(&$bri, $user, $match_id_s)
+    {
+        $ret = FALSE;
+        $curtime = time();
+
+        do {
+            /* - verify if match_id and user are both valid to accept
+               the match_continue request - */
+            $match_id = (int)$match_id_s;
+            if ($match_id <= 0) {
+                $msg = "questa partita non esiste";
+                break;
+            }
+
+            if ($user->continue_get() == BIN5_USER_CONTINUE_ALREADY) {
+                $msg = "Hai già richiesto di continuare una partita.";
+                break;
+            }
+
+            if ($user->continue_get() == $match_id) {
+                $msg = "Hai già richiesto di continuare questa partita.";
+                break;
+            }
+            // retrieves users list for this match
+            $match_data = array();
+            if (($bdb = BriskDB::create()) != FALSE) {
+                // match_order_get return FALSE for old matches
+                $ucodes = $bdb->match_order_get($match_data, $match_id, BIN5_PLAYERS_N);
+                unset($bdb);
+            }
+            if ($ucodes == FALSE) {
+                $msg = "questa partita non è stata memorizzata correttamente";
+                break;
+            }
+
+            // if current user code must be in the users list
+            if (array_search($user->code, $ucodes) === FALSE) {
+                $msg = sprintf("Questo utente non compare nella partita che si vuole continuare [%d].", $user->code);
+                break;
+            }
+
+            /* - the user is in the list, add the match_id to his profile
+               and check if he is the N'th to require continue - */
+
+            // set the match_id for the current user
+            $user->continue_set($match_id);
+
+            for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
+                $user_cur = &$bri->user[$this->player[$i]];
+                if ($user_cur->continue_get() != $match_id) {
+                    break;
+                }
+            }
+            $ret = TRUE;
+            // not all players set the continue match than we exit
+            if ($i < BIN5_PLAYERS_N) {
+                $msg = sprintf("<b>L'utente <i>%s</i> vorrebbe continuare la partita n° %d.</b>",
+                               xcape($user->name), $match_id);
+                break;
+            }
+
+            /* - all users decide to continue the same match, update all infos and rearrange users
+               to the right positions on the table - */
+
+            /*  reset users table order */
+            for ($i = 0 ; $i < BIN5_PLAYERS_N - 1 ; $i++) {
+                if ($bri->user[$this->player[$i]]->code == $ucodes[$i]) {
+                    continue;
+                }
+                for ($e = $i + 1 ; $e < BIN5_PLAYERS_N ; $e++) {
+                    if ($bri->user[$this->player[$e]]->code == $ucodes[$i]) {
+                        $swap = $this->player[$i];
+                        $this->player[$i] = $this->player[$e];
+                        $this->player[$e] = $swap;
+                        $bri->user[$this->player[$i]]->table_pos = $i;
+                        $bri->user[$this->player[$e]]->table_pos = $e;
+                    }
+                }
+            }
+            for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
+                fprintf(STDERR, "USERZ: [%s]\n", $bri->user[$this->player[$i]]->name);
+            }
+
+            // update database info to be aligned with current table (ttok and table_idx
+            if (($bdb = BriskDB::create()) != FALSE) {
+                if ($bdb->match_continue($match_id, $this, $user->table_orig) == FALSE) {
+                    unset($bdb);
+                    $msg = "aggiornamento dei dati della partita fallito";
+                    break;
+                }
+                unset($bdb);
+            }
+
+            /* bunch and multiplier status set */
+            $this->mazzo = $match_data['mazzo_next'];
+            $this->mult  = $match_data['mult_next'];
+            $this->match_id = $match_id;
+            $this->game_init(&$bri->user);
+
+            /* reload of the page with the new layout */
+            for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
+                $user_cur = &$bri->user[$this->player[$i]];
+                $user_cur->continue_set(BIN5_USER_CONTINUE_ALREADY);
+
+                $user_cur->trans_step = $user_cur->step + 1;
+                $user_cur->comm[$user_cur->step % COMM_N] = sprintf('gst.st_loc++; gst.st=%d; xstm.stop(); window.onunload = null ; window.onbeforeunload = null ; document.location.assign("index.php");|', $user_cur->step+1);
+                $user_cur->step_inc();
+
+                // a void command force xynt-streamer to flush all data to client
+                $user_cur->trans_step = $user_cur->step + 1;
+                $user_cur->comm[$user_cur->step % COMM_N] = "";
+                $user_cur->step_inc();
+
+                $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$bri, &$user_cur, $user_cur->step+1, TRUE, FALSE);
+                $user_cur->step_inc();
+            }
+            return (TRUE);
+        } while (FALSE);
+
+        $dt = date("H:i ", $curtime);
+        for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
+            if ($ret == FALSE && $this->player[$i] != $user->idx)
+                continue;
+            $user_cur = &$bri->user[$this->player[$i]];
+            $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
+            $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");',
+                                                                 $dt, NICKSERV, $msg);
+            $user_cur->step_inc();
+        }
+    }
+
 } // end class Bin5_table
 
 
@@ -1212,95 +1345,23 @@ class Bin5 {
     {
         GLOBAL $mlang_brisk, $G_lang;
 
-        if ($user->stat == 'table') {
-            $table = &$this->table[$user->table];
+        if ($user->stat != 'table') {
+            return;
         }
+        $curtime = time();
 
+        $table = &$this->table[$user->table];
         $user_mesg = substr($mesg,6);
 
-        $curtime = time();
+        $ret = FALSE;
+        $mesg = "";
 
         $dt = date("H:i ", $curtime);
-        if (strncmp($user_mesg, "/nick ", 6) == 0) {
+        if (strncmp($user_mesg, "/cont ", 6) == 0) {
             log_main($user->sess." chatt_send BEGIN");
 
-            if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
-                $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
-                $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
-                $user->step_inc();
-
-                return;
-            }
-            $user_mesg = "COMMAND ".$user_mesg;
-            // Search dup name
-            // change
-            // update local graph
-            // update remote graphs
-            for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
-                $user_cur = $this->user[$i];
-                //     if ($user_cur->sess == '' || $user_cur->stat != 'room')
-                if ($user_cur->sess == '')
-                    continue;
-                if (strcasecmp($user_cur->name, $name_new) == 0) {
-                    $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
-
-                    $premsg = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
-                    $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $premsg);
-                    $user->step_inc();
-                    break;
-                }
-            }
-            if ($i == BIN5_MAX_PLAYERS) {
-                if ($user->flags & USER_FLAG_AUTH && strcasecmp($user->name,$name_new) != 0) {
-                    if ($this->table[$user->table]->auth_only == TRUE) {
-                        $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
-                        $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>%s</b>");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
-                        $user->step_inc();
-                        return;
-                    }
-                    else {
-                        $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
-                    }
-                }
-
-                $user->name = $name_new;
-
-                log_main($user->sess." chatt_send start set");
-
-
-                for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
-                    log_main($user->sess." chatt_send set loop");
-
-                    $user_cur = &$this->user[$i];
-                    if ($user_cur->sess == '')
-                        continue;
-
-                    if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
-                        $table = &$this->table[$user->table];
-
-                        $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
-                        $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
-                                                                             $this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->flags,
-                                                                             xcape($this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->name),
-
-                                                                             $this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->flags,
-                                                                             xcape($this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->name),
-
-                                                                             $this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->flags,
-                                                                             xcape($this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->name),
-
-                                                                             (BIN5_PLAYERS_N == 3 ? 0:  $this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->flags),
-                                                                             (BIN5_PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->name)),
-
-                                                                             (BIN5_PLAYERS_N == 3 ? 0:  $this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->flags),
-                                                                             (BIN5_PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->name)));
-                        if ($user_cur == $user) {
-                            $user_cur->comm[$user_cur->step % COMM_N] .= $user_cur->myname_innerHTML();
-                        }
-                        $user_cur->step_inc();
-                    }
-                }
-            }
+            $match_id = substr($user_mesg, 6);
+            $table->match_continue($this, $user, $match_id);
         }
         else {
             for ($i = 0 ; $i < ($user->stat == 'room' ? BIN5_MAX_PLAYERS : BIN5_PLAYERS_N) ; $i++) {
index 4bd60ce..be148cf 100644 (file)
@@ -297,22 +297,8 @@ function main_pgsql($from, $to)
                 fprintf($fpexp, "<br>");
                 $tmt_obj = pg_fetch_object($tmt_pg, $m);
 
-                if ($tmt_obj->minus_one_is_old > -1) { // is new
-                    $usr_sql = sprintf("
-SELECT u.code AS code, u.login AS login, min(g.tstamp) AS first, max(g.tstamp) AS last, m.tidx AS tidx FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p, %susers AS u, %sbin5_table_orders AS o WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode AND m.code = %d AND m.code = o.mcode AND u.code = o.ucode GROUP BY u.code, u.login, m.tidx, o.pos ORDER BY o.pos;", $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $tmt_obj->code);
-                }
-                else { // is old
-                    $usr_sql = sprintf("
-SELECT u.code AS code, u.login AS login, min(g.tstamp) AS first, max(g.tstamp) AS last, m.tidx AS tidx FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p, %susers AS u WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode AND m.code = %d GROUP BY u.code, u.login, m.tidx;", $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $tmt_obj->code);
-                }
-
-                if (($usr_pg  = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE ) {
-                    log_crit("stat-day: pg_query usr_sql failed");
-                    break;
-                }
-                $usr_n = pg_numrows($usr_pg);
-                if ($usr_n != BIN5_PLAYERS_N) {
-                    log_crit("stat-day: wrong number of players");
+                if (($users = $bdb->users_get($tmt_obj->code, TRUE, ($tmt_obj->minus_one_is_old > -1))) == FALSE) {
+                    log_crit("%s: users_get failed", __FUNCTION__);
                     break;
                 }
 
@@ -322,19 +308,19 @@ SELECT u.code AS code, u.login AS login, min(g.tstamp) AS first, max(g.tstamp) A
                     break;
                 }
 
-                $usr_obj = array();
                 for ($u = 0 ; $u < $usr_n ; $u++) {
-                    $usr_obj[$u] = pg_fetch_object($usr_pg, $u);
                     if ($u == 0) {
-                        fprintf($fpexp, "<h3>Codice: %d (%s - %s), Tavolo: %s</h3>\n", $tmt_obj->code, $usr_obj[$u]->first, $usr_obj[$u]->last, $usr_obj[$u]->tidx);
+                        fprintf($fpexp, "<h3>Codice: %d (%s - %s), Tavolo: %s</h3>\n", $tmt_obj->code, $users[$u]['first'], $users[$u]['last'], $users[$u]['tidx']);
                         fprintf($fpexp, "<table align='center' class='placing'><tr>\n");
                     }
-                    fprintf($fpexp, "<th>%s</th>", $usr_obj[$u]->login);
-                    $pts_sql = sprintf("SELECT p.pts as pts from %sbin5_matches as m, %sbin5_games as g, %sbin5_points as p, %susers as u WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode AND m.code = %d AND u.code = %d ORDER BY g.code", $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx,
-                                       $tmt_obj->code, $usr_obj[$u]->code);
+                    fprintf($fpexp, "<th>%s</th>", $users[$u]['login']);
+                    // note: we are looping on users, order on them not needed
+                    $pts_sql = sprintf("SELECT p.pts as pts from %sbin5_games as g, %sbin5_points as p WHERE g.code = p.gcode AND g.mcode = %d AND p.ucode = %d ORDER BY g.code",
+                                       $G_dbpfx, $G_dbpfx, $G_dbpfx,
+                                       $tmt_obj->code, $users[$u]['code']);
 
                     // points of the match for each user
-                    if (($pts_pg[$u]  = pg_query($bdb->dbconn->db(), $pts_sql)) == FALSE) {
+                    if (($pts_pg[$u] = pg_query($bdb->dbconn->db(), $pts_sql)) == FALSE) {
                         break;
                     }
                     if ($u == 0) {
@@ -365,10 +351,10 @@ SELECT u.code AS code, u.login AS login, min(g.tstamp) AS first, max(g.tstamp) A
                                 ($tmt_obj->minus_one_is_old == -1 ? "td" : "th"));
                     }
                     if ($tmt_obj->minus_one_is_old != -1) {
-                        fprintf($fpexp, "<td>%s</td><td>%s</td>", $usr_obj[$gam_obj->mazzo]->login,
+                        fprintf($fpexp, "<td>%s</td><td>%s</td>", $users[$gam_obj->mazzo]['login'],
                                 xcape( game_description($gam_obj->act, 'plain', $gam_obj->mult,
-                                                        $gam_obj->asta_win, $usr_obj[$gam_obj->asta_win]->login,
-                                                        $gam_obj->friend, $usr_obj[$gam_obj->friend]->login,
+                                                        $gam_obj->asta_win, $users[$gam_obj->asta_win]['login'],
+                                                        $gam_obj->friend, $users[$gam_obj->friend]['login'],
                                                         $gam_obj->pnt, $gam_obj->asta_pnt) )
                                 );
                     }
@@ -380,7 +366,7 @@ SELECT u.code AS code, u.login AS login, min(g.tstamp) AS first, max(g.tstamp) A
                 for ($u = 0 ; $u < BIN5_PLAYERS_N ; $u++) {
                     $tot_sql = sprintf("
 SELECT SUM(p.pts) AS pts FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p, %susers AS u WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode AND m.code = %d AND u.code = %d", $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx,
-                                       $tmt_obj->code, $usr_obj[$u]->code);
+                                       $tmt_obj->code, $users[$u]['code']);
                     if (($tot_pg  = pg_query($bdb->dbconn->db(), $tot_sql)) == FALSE ) {
                         break;
                     }