Skip to content

Commit 9794f9e

Browse files
committed
update leaderboard to have name, and peer db to have name.
1 parent ac65446 commit 9794f9e

2 files changed

Lines changed: 22 additions & 18 deletions

File tree

src/database/database.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ void Database::connect_to_db() {
8080
"CREATE TABLE IF NOT EXISTS peers ("
8181
"id TEXT NOT NULL, "
8282
"peer_id TEXT NOT NULL, "
83-
"name TEXT, "
83+
"name TEXT NOT NULL, "
8484
"game_id TEXT NOT NULL, "
8585
"timestamp TIMESTAMPTZ DEFAULT NOW(), "
86-
"PRIMARY KEY (id, game_id)"
86+
"PRIMARY KEY (id, game_id),"
87+
"UNIQUE (peer_id, game_id)"
8788
");");
8889
txn.exec("CREATE INDEX IF NOT EXISTS idx_peers_peer_game ON peers (peer_id, game_id);");
8990
txn.commit();
@@ -139,27 +140,34 @@ void Database::leaderboard_set_score(const std::string& leaderboard_id, const st
139140
txn.commit();
140141
}
141142

142-
// Set or update user data
143+
// Update user data if row exists
143144
void Database::set_user_data(const std::string& game_id, const std::string& peer_id,
144145
const boost::container::flat_map<std::string, AnyElement>& user_data) {
145146
ensure_connection();
147+
146148
// Extract optional name from user_data key "n"
147-
std::string name;
149+
std::string name = "";
148150
auto it = user_data.find("n");
149151
if (it != user_data.end()) {
150152
const AnyElement& nElem = it->second;
151153
if (std::holds_alternative<std::string>(nElem.value)) {
152154
name = std::get<std::string>(nElem.value);
153155
}
154156
}
157+
158+
if (name.empty()) {
159+
std::cerr << "Name is required for user data" << std::endl;
160+
return;
161+
}
162+
155163
pqxx::work txn(*connection);
156-
txn.exec(pqxx::zview("INSERT INTO peers (id, peer_id, name, game_id) "
157-
"VALUES ($1, $2, $3, $4) "
158-
"ON CONFLICT (id, game_id) DO UPDATE SET "
159-
"peer_id = EXCLUDED.peer_id, "
160-
"name = EXCLUDED.name, "
161-
"timestamp = NOW();"),
162-
pqxx::params(peer_id, peer_id, name, game_id));
164+
165+
// Only update existing row
166+
txn.exec(pqxx::zview("UPDATE peers "
167+
"SET name = $1, timestamp = NOW() "
168+
"WHERE peer_id = $2 AND game_id = $3;"),
169+
pqxx::params(name, peer_id, game_id));
170+
163171
txn.commit();
164172
}
165173

@@ -233,8 +241,8 @@ std::string Database::get_peer_or_insert(const std::string& reconnection_token,
233241
}
234242

235243
// Not found, insert new peer
236-
txn.exec(pqxx::zview("INSERT INTO peers (id, peer_id, game_id) VALUES ($1, $2, $3);"),
237-
pqxx::params(reconnection_token, peer_id, game_id));
244+
txn.exec(pqxx::zview("INSERT INTO peers (id, peer_id, name, game_id) VALUES ($1, $2, $3, $4);"),
245+
pqxx::params(reconnection_token, peer_id, "", game_id));
238246
txn.commit();
239247
return peer_id;
240248
}

src/main.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,7 @@ int main(int argc, char *argv[]) {
112112
}
113113
moodycamel::BlockingReaderWriterQueue<WebSocketReceivedMessage> receive_queue(
114114
message_queue_length);
115-
moodycamel::BlockingReaderWriterQueue<DatabaseReceivedMessage> &database_queue =
116-
config_reader.GetBoolean("database", "enabled", false)
117-
? *new moodycamel::BlockingReaderWriterQueue<DatabaseReceivedMessage>(
118-
message_queue_length)
119-
: *new moodycamel::BlockingReaderWriterQueue<DatabaseReceivedMessage>(0);
115+
moodycamel::BlockingReaderWriterQueue<DatabaseReceivedMessage> database_queue(message_queue_length);
120116
std::cout << "Starting webserver" << std::endl;
121117
uWS::App app = uWS::App();
122118
WebSocketServer webserver(

0 commit comments

Comments
 (0)