Skip to content

Commit

Permalink
Store additional fields in the database.
Browse files Browse the repository at this point in the history
* Whether the game allows observers.
* Whether the game's replay should be publicly available (currently the same as whether observers are allowed pending the second part of #3909).
* Whether the game had a password set.
* The side's name - this differs from what can be retrieved from USER_ID since it will have the name of any AI used.
  • Loading branch information
Pentarctagon committed Oct 28, 2019
1 parent ad25a20 commit 81e2cae
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/server/forum_user_handler.cpp
Expand Up @@ -427,10 +427,10 @@ void fuh::db_insert_game_info(const std::string& uuid, int game_id, const std::s
}
}

void fuh::db_update_game_start(const std::string& uuid, int game_id, const std::string& map_name, const std::string& era_name, int reload){
void fuh::db_update_game_start(const std::string& uuid, int game_id, const std::string& map_name, const std::string& era_name, int reload, int observers, int is_public, int has_password){
try {
prepared_statement<void>("UPDATE `" + db_game_info_table_ + "` SET START_TIME = CURRENT_TIMESTAMP, MAP_NAME = ?, ERA_NAME = ?, RELOAD = ? WHERE INSTANCE_UUID = ? AND GAME_ID = ?",
map_name, era_name, reload, uuid, game_id);
prepared_statement<void>("UPDATE `" + db_game_info_table_ + "` SET START_TIME = CURRENT_TIMESTAMP, MAP_NAME = ?, ERA_NAME = ?, RELOAD = ?, OBSERVERS = ?, PUBLIC = ?, PASSWORD = ? WHERE INSTANCE_UUID = ? AND GAME_ID = ?",
map_name, era_name, reload, observers, is_public, has_password, uuid, game_id);
} catch (const sql_error& e) {
ERR_UH << "Could not update the game's starting information on table `" + db_game_info_table_ + "`:" << e.message << std::endl;
}
Expand All @@ -445,10 +445,10 @@ void fuh::db_update_game_end(const std::string& uuid, int game_id, const std::st
}
}

void fuh::db_insert_game_player_info(const std::string& uuid, int game_id, const std::string& username, int side_number, int is_host, const std::string& faction, const std::string& version, const std::string& source){
void fuh::db_insert_game_player_info(const std::string& uuid, int game_id, const std::string& username, int side_number, int is_host, const std::string& faction, const std::string& version, const std::string& source, const std::string& current_user){
try {
prepared_statement<void>("INSERT INTO `" + db_game_player_info_table_ + "`(INSTANCE_UUID, GAME_ID, USER_ID, SIDE_NUMBER, IS_HOST, FACTION, CLIENT_VERSION, CLIENT_SOURCE) VALUES(?, ?, IFNULL((SELECT user_id FROM `"+db_users_table_+"` WHERE username = ?), -1), ?, ?, ?, ?, ?)",
uuid, game_id, username, side_number, is_host, faction, version, source);
prepared_statement<void>("INSERT INTO `" + db_game_player_info_table_ + "`(INSTANCE_UUID, GAME_ID, USER_ID, SIDE_NUMBER, IS_HOST, FACTION, CLIENT_VERSION, CLIENT_SOURCE, USER_NAME) VALUES(?, ?, IFNULL((SELECT user_id FROM `"+db_users_table_+"` WHERE username = ?), -1), ?, ?, ?, ?, ?, ?)",
uuid, game_id, username, side_number, is_host, faction, version, source, current_user);
} catch (const sql_error& e) {
ERR_UH << "Could not insert the game's player information on table `" + db_game_player_info_table_ + "`:" << e.message << std::endl;
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/forum_user_handler.hpp
Expand Up @@ -71,9 +71,9 @@ class fuh : public user_handler {

std::string get_uuid();
void db_insert_game_info(const std::string& uuid, int game_id, const std::string& version, const std::string& name);
void db_update_game_start(const std::string& uuid, int game_id, const std::string& map_name, const std::string& era_name, int reload);
void db_update_game_start(const std::string& uuid, int game_id, const std::string& map_name, const std::string& era_name, int reload, int observers, int is_public, int has_password);
void db_update_game_end(const std::string& uuid, int game_id, const std::string& replay_location);
void db_insert_game_player_info(const std::string& uuid, int game_id, const std::string& username, int side_number, int is_host, const std::string& faction, const std::string& version, const std::string& source);
void db_insert_game_player_info(const std::string& uuid, int game_id, const std::string& username, int side_number, int is_host, const std::string& faction, const std::string& version, const std::string& source, const std::string& current_user);
void db_insert_modification_info(const std::string& uuid, int game_id, const std::string& modification_name);
void db_set_oos_flag(const std::string& uuid, int game_id);

Expand Down
5 changes: 5 additions & 0 deletions src/server/game.hpp
Expand Up @@ -294,6 +294,11 @@ class game
return password_.empty() || passwd == password_;
}

bool has_password() const
{
return !password_.empty();
}

const std::string& termination_reason() const
{
static const std::string aborted = "aborted";
Expand Down
4 changes: 2 additions & 2 deletions src/server/server.cpp
Expand Up @@ -1620,7 +1620,7 @@ void server::handle_player_in_game(socket_ptr socket, std::shared_ptr<simple_wml

if(user_handler_) {
const simple_wml::node& multiplayer = *g.level().root().child("multiplayer");
user_handler_->db_update_game_start(uuid_, g.id(), multiplayer["mp_scenario"].to_string(), multiplayer["mp_era"].to_string(), g.is_reload());
user_handler_->db_update_game_start(uuid_, g.id(), multiplayer["mp_scenario"].to_string(), multiplayer["mp_era"].to_string(), g.is_reload(), multiplayer["observer"].to_bool(), multiplayer["observer"].to_bool(), g.has_password());

const simple_wml::node::child_list& sides = g.get_sides_list();
for(unsigned side_index = 0; side_index < sides.size(); ++side_index) {
Expand All @@ -1637,7 +1637,7 @@ void server::handle_player_in_game(socket_ptr socket, std::shared_ptr<simple_wml
version = player->info().version();
source = player->info().source();
}
user_handler_->db_insert_game_player_info(uuid_, g.id(), side["player_id"].to_string(), side["side"].to_int(), side["is_host"].to_bool(), side["faction"].to_string(), version, source);
user_handler_->db_insert_game_player_info(uuid_, g.id(), side["player_id"].to_string(), side["side"].to_int(), side["is_host"].to_bool(), side["faction"].to_string(), version, source, side["current_player"].to_string());
}

const std::string mods = multiplayer["active_mods"].to_string();
Expand Down
4 changes: 2 additions & 2 deletions src/server/user_handler.hpp
Expand Up @@ -136,9 +136,9 @@ class user_handler {

virtual std::string get_uuid() =0;
virtual void db_insert_game_info(const std::string& uuid, int game_id, const std::string& version, const std::string& name) =0;
virtual void db_update_game_start(const std::string& uuid, int game_id, const std::string& map_name, const std::string& era_name, int reload) =0;
virtual void db_update_game_start(const std::string& uuid, int game_id, const std::string& map_name, const std::string& era_name, int reload, int observers, int is_public, int has_password) =0;
virtual void db_update_game_end(const std::string& uuid, int game_id, const std::string& replay_location) =0;
virtual void db_insert_game_player_info(const std::string& uuid, int game_id, const std::string& username, int side_number, int is_host, const std::string& faction, const std::string& version, const std::string& source) =0;
virtual void db_insert_game_player_info(const std::string& uuid, int game_id, const std::string& username, int side_number, int is_host, const std::string& faction, const std::string& version, const std::string& source, const std::string& current_user) =0;
virtual void db_insert_modification_info(const std::string& uuid, int game_id, const std::string& modification_name) =0;
virtual void db_set_oos_flag(const std::string& uuid, int game_id) =0;
};
4 changes: 4 additions & 0 deletions utils/mp-server/table_definitions.sql
Expand Up @@ -69,6 +69,9 @@ create table game_info
REPLAY_NAME VARCHAR(255),
OOS BIT(1) NOT NULL DEFAULT 0,
RELOAD BIT(1),
OBSERVERS BIT(1),
PASSWORD BIT(1),
PUBLIC BIT(1),
PRIMARY KEY (INSTANCE_UUID, GAME_ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Expand All @@ -91,6 +94,7 @@ create table game_player_info
FACTION VARCHAR(255) NOT NULL,
CLIENT_VERSION VARCHAR(255) NOT NULL DEFAULT '',
CLIENT_SOURCE VARCHAR(255) NOT NULL DEFAULT '',
USER_NAME VARCHAR(255) NOT NULL DEFAULT '',
PRIMARY KEY (INSTANCE_UUID, GAME_ID, SIDE_NUMBER)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Expand Down

0 comments on commit 81e2cae

Please sign in to comment.