+ for ($i = 0 ; $i < $n ; $i++) {
+ $codes_where .= sprintf("%scode = %d", ($i == 0 ? "" : " OR "), $ucodes[$i]);
+ }
+ $cnt_sql = sprintf("UPDATE %susers SET (game_cnt)
+ = (game_cnt+1) WHERE %s;",
+ $G_dbpfx, $codes_where);
+ error_log($cnt_sql);
+ if (($cnt_pg = $this->query($cnt_sql)) == FALSE || pg_affected_rows($cnt_pg) != $n) {
+ log_crit(sprintf("bin5_points_save: failed increment game [%s]", $cnt_sql));
+ break;
+ }
+ }
+
+ /*
+ * games management
+ */
+ $gam_sql = sprintf("INSERT INTO %sbin5_games (mcode, tstamp, act, asta_pnt, pnt, asta_win, friend, mazzo, mult)
+ VALUES (%d, to_timestamp(%d), %d, %d, %d, %d, %d, %d, %d) RETURNING *;",
+ $G_dbpfx, $mtc_obj->code, $date, $action,
+ $table->old_asta_pnt, $table->old_pnt,
+ $table->old_asta_win,
+ $table->old_friend,
+ $table->old_mazzo, $table->old_mult);
+ if (($gam_pg = $this->query($gam_sql)) == FALSE || pg_affected_rows($gam_pg) != 1) {
+ log_crit(sprintf("bin5_points_save: failed at insert game [%s]", $gam_sql));
+ break;
+ }
+
+ $gam_obj = pg_fetch_object($gam_pg,0);
+
+ /*
+ * points management
+ */
+ for ($i = 0 ; $i < $n ; $i++) {
+ /* put points */
+ $pts_sql = sprintf("INSERT INTO %sbin5_points (gcode, ucode, pts)
+ VALUES (%d, %d, %d);",
+ $G_dbpfx, $gam_obj->code, $ucodes[$i], $pts[$i]);
+ if (($pts_pg = $this->query($pts_sql)) == FALSE || pg_affected_rows($pts_pg) != 1) {
+ log_crit(sprintf("bin5_points_save: failed at insert point [%s]", $pts_sql));
+ break;
+ }
+ }
+ if ($i < $n)
+ break;
+
+ if ($this->query("COMMIT") == FALSE) {
+ break;
+ }
+
+ $is_trans = FALSE;
+
+ $table->match_id = $mtc_obj->code;
+ $ret = TRUE;
+ } while (0);
+
+ if ($is_trans)
+ $this->query("ROLLBACK");
+
+ return $ret;
+ }
+
+ function mail_add_fromitem($mail) {
+ GLOBAL $G_dbpfx;
+
+ $usr_sql = sprintf("
+INSERT INTO %smails (code, ucode, type, tstamp, subj, body_txt, body_htm, hash)
+ VALUES (%d, %d, %d, to_timestamp(%d), '%s', '%s', '%s', '%s') RETURNING *;",
+ $G_dbpfx, $mail->code, $mail->ucode, $mail->type, $mail->tstamp,
+ escsql($mail->subj), escsql($mail->body_txt), escsql($mail->body_htm),
+ ($mail->hash == NULL ? "" : escsql($mail->hash))
+ );
+
+ if (! (($usr_pg = $this->query($usr_sql)) != FALSE && pg_affected_rows($usr_pg) == 1) ) {
+ return FALSE;
+ }
+ $usr_obj = pg_fetch_object($usr_pg, 0);
+
+ return $usr_obj;
+ }
+
+ function mail_check($code, $type, $hash)
+ {
+ GLOBAL $G_dbpfx;
+
+ $mai_sql = sprintf("SELECT * FROM %smails WHERE code = %d AND type = %d AND hash = '%s';",
+ $G_dbpfx, $code, $type, escsql($hash));
+ if (($mai_pg = $this->query($mai_sql)) == FALSE || pg_numrows($mai_pg) != 1) {
+ // check failed
+ return (FALSE);
+ }
+
+ $mai_obj = pg_fetch_object($mai_pg, 0);
+ return ($mai_obj);
+ }
+
+ function mail_delete($code)
+ {
+ GLOBAL $G_dbpfx;
+
+ $mai_sql = sprintf("DELETE FROM %smails WHERE code = %d;", $G_dbpfx, $code);
+
+ if (($mai_pg = $this->query($mai_sql)) == FALSE || pg_affected_rows($mai_pg) != 1) {
+ return (FALSE);
+ }
+ return (TRUE);
+ }
+
+ function friendship_default()
+ {
+ return (array(usersnet_friend_getlabel(1) => "0",
+ usersnet_friend_getlabel(2) => "0",
+ usersnet_friend_getlabel(3) => "0",
+ usersnet_friend_getlabel(4) => "0",
+ usersnet_friend_getlabel(5) => "0"));
+ }
+
+ function usersnet_widefriend($owner, $target)
+ {
+ GLOBAL $G_dbpfx;
+
+ $widefriend = $this->friendship_default();
+
+ $wfri_sql = sprintf("SELECT * FROM %susersnet_widefriend WHERE owner = %d AND target = %d;",
+ $G_dbpfx, $owner, $target);
+ if (($wfri_pg = $this->query($wfri_sql)) == FALSE) {
+ return ($widefriend);
+ }
+
+ for ($i = 0 ; $i < pg_numrows($wfri_pg) ; $i++) {
+ $wfri_obj = pg_fetch_object($wfri_pg, $i);
+ $widefriend[usersnet_friend_getlabel(intval($wfri_obj->friend))] = $wfri_obj->count;
+ }
+
+ return ($widefriend);
+ }
+
+ function usersnet_wideskill($owner, $target)
+ {
+ GLOBAL $G_dbpfx;
+
+ $wideskill = "//";
+
+ $wskl_sql = sprintf("SELECT * FROM %susersnet_wideskill WHERE owner = %d AND target = %d;",
+ $G_dbpfx, $owner, $target);
+ if (($wskl_pg = $this->query($wskl_sql)) == FALSE) {
+ return ($wideskill);
+ }
+
+ if (pg_numrows($wskl_pg) > 0) {
+ $wskl_obj = pg_fetch_object($wskl_pg, 0);
+ // TODO: UNCOMMENT IF THE NETWORK WORKS VERY WELL
+ // if ($wskl_obj->count >= 3)
+ $wideskill = sprintf("%3.2f", $wskl_obj->skill);
+ }
+ return ($wideskill);
+ }
+
+ function usersnet_narrowfriend($owner, $target)
+ {
+ GLOBAL $G_dbpfx;
+
+ $narrowfriend = $this->friendship_default();
+
+ $nfri_sql = sprintf("SELECT * FROM %susersnet_narrowfriend WHERE owner = %d AND target = %d;",
+ $G_dbpfx, $owner, $target);
+ if (($nfri_pg = $this->query($nfri_sql)) == FALSE) {
+ return $narrowfriend;