Skip to content

Commit

Permalink
[Database] Allow to save kart color, kart characteristics and powerup…
Browse files Browse the repository at this point in the history
… config names, disconnection time

Please note that the results table schema may change later before merging #5. In this commit, the changes are:
- added items TEXT, kart_color REAL, is_quit INTEGER
- result stores kart's race time independently of whether it's a quit or not
- config stores kart characteristics file name, items stores powerup file name, empty if the name is default.
One may need to change NULL values in config and items to an empty string.
  • Loading branch information
kimden committed Jun 1, 2023
1 parent 005ea99 commit 53f8184
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/karts/kart_properties_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class KartPropertiesManager: public NoCopy
std::map<std::string, std::unique_ptr<AbstractCharacteristic> > m_kart_type_characteristics;
std::map<std::string, std::unique_ptr<AbstractCharacteristic> > m_player_characteristics;

std::string m_file_name;

protected:

typedef PtrVector<KartProperties> KartPropertiesVector;
Expand Down Expand Up @@ -149,6 +151,11 @@ class KartPropertiesManager: public NoCopy
// ------------------------------------------------------------------------
void onDemandLoadKartTextures(const std::set<std::string>& kart_list,
bool unload_unused = true);
// ------------------------------------------------------------------------
void setFileName(const std::string file_name) { m_file_name = file_name; }
// ------------------------------------------------------------------------
std::string getFileName() { return m_file_name; }

};

extern KartPropertiesManager *kart_properties_manager;
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1960,6 +1960,7 @@ void initRest()
std::string char_file;
if (!CommandLine::has("--char-file", &char_file))
char_file = "kart_characteristics.xml";
kart_properties_manager->setFileName(char_file);
XMLNode characteristicsNode(file_manager->getAsset(char_file));
kart_properties_manager->loadCharacteristics(&characteristicsNode);

Expand Down
33 changes: 26 additions & 7 deletions src/network/protocols/server_lobby.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6763,6 +6763,14 @@ void ServerLobby::updateGnuElimination()
void ServerLobby::storeResults()
{
#ifdef ENABLE_SQLITE3
std::string powerup_string = powerup_manager->getFileName();
std::string kc_string = kart_properties_manager->getFileName();
// TODO: don't use file names as constants
if (powerup_string == "powerup.xml")
powerup_string = "";
if (kc_string == "kart_characteristics.xml")
kc_string = "";

bool racing_mode = false;
bool ffa = RaceManager::get()->getMinorMode() ==
RaceManager::MINOR_MODE_FREE_FOR_ALL;
Expand Down Expand Up @@ -6792,9 +6800,11 @@ void ServerLobby::storeResults()
{
std::string get_query = StringUtils::insertValues("SELECT username, "
"result FROM '%s' WHERE venue = '%s' and reverse = '%s' "
"and mode = '%s' and laps = %d order by result asc, time asc limit 1;",
"and mode = '%s' and laps = %d and config = '%s' "
"and items = '%s' and is_quit = 0 "
"order by result asc, time asc limit 1;",
records_table_name.c_str(), track_name.c_str(), reverse_string.c_str(), mode_name.c_str(),
laps_number);
laps_number, kc_string.c_str(), powerup_string.c_str());
auto ret = vectorSQLQuery(get_query, 2);
record_fetched = ret.first;
if (record_fetched && ret.second[0].size() > 0)
Expand All @@ -6821,8 +6831,11 @@ void ServerLobby::storeResults()
std::vector<std::string> scores;
std::vector<std::string> karts;
std::vector<int> lap_counts;
std::vector<float> colors;
std::vector<int> has_quit;
for (int i = 0; i < player_count; i++)
{
// TODO why I cannot use get()->getPlayerName?
std::string username = StringUtils::wideToUtf8(
RaceManager::get()->getKartInfo(i).getPlayerName());
for (std::string& username: usernames) {
Expand All @@ -6835,11 +6848,12 @@ void ServerLobby::storeResults()
double score = DISCONNECT_TIME;
std::string kart_name = w->getKart(i)->getIdent();
std::stringstream elapsed_string;
float kart_color = RaceManager::get()->getKartColor(i);

if (racing_mode)
{
if (!w->getKart(i)->isEliminated())
score = RaceManager::get()->getKartRaceTime(i);
score = RaceManager::get()->getKartRaceTime(i);
has_quit.push_back(w->getKart(i)->isEliminated() ? 1 : 0);
elapsed_string << std::setprecision(4) << std::fixed << score;
if (best_cur_player_idx == -1 || score < best_cur_time)
{
Expand All @@ -6852,6 +6866,7 @@ void ServerLobby::storeResults()
{
if (w->getKart(i)->isEliminated())
continue;
has_quit.push_back(0);
if (ffa_world)
{
score = ffa_world->getKartScore(i);
Expand All @@ -6863,6 +6878,7 @@ void ServerLobby::storeResults()
scores.push_back(elapsed_string.str());
karts.push_back(kart_name);
lap_counts.push_back(laps_number);
colors.push_back(kart_color);
}
if (ServerConfig::m_preserve_battle_scores)
{
Expand All @@ -6872,6 +6888,8 @@ void ServerLobby::storeResults()
lap_counts.push_back(0);
scores.push_back(std::to_string(p.second));
karts.push_back("unknown");
colors.push_back(-1);
has_quit.push_back(1);
}
}
m_saved_ffa_points.clear();
Expand All @@ -6881,18 +6899,19 @@ void ServerLobby::storeResults()
"INSERT INTO %s "
"(username, venue, reverse, mode, laps, result"
#ifdef ENABLE_RECORDS_V2
", difficulty, kart, config"
", difficulty, kart, config, items, kart_color, is_quit"
#endif
") "
"VALUES (?, '%s', '%s', '%s', %d, '%s'"
#ifdef ENABLE_RECORDS_V2
", %d, '%s', '%s'"
", %d, '%s', '%s', '%s', %f, %d"
#endif
");",
m_results_table_name.c_str(), track_name.c_str(),
reverse_string.c_str(), mode_name.c_str(), lap_counts[i], scores[i].c_str()
#ifdef ENABLE_RECORDS_V2
, getDifficulty(), karts[i].c_str(), ""
, getDifficulty(), karts[i].c_str(), kc_string.c_str(),
powerup_string.c_str(), colors[i], has_quit[i]
#endif
);
std::string name = usernames[i];
Expand Down

0 comments on commit 53f8184

Please sign in to comment.