LobbyDatabase.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*
  2. * LobbyServer.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "LobbyDatabase.h"
  12. #include "SQLiteConnection.h"
  13. void LobbyDatabase::createTables()
  14. {
  15. static const std::string createChatMessages = R"(
  16. CREATE TABLE IF NOT EXISTS chatMessages (
  17. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  18. senderName TEXT,
  19. channelType TEXT,
  20. channelName TEXT,
  21. messageText TEXT,
  22. creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
  23. );
  24. )";
  25. static const std::string createTableGameRooms = R"(
  26. CREATE TABLE IF NOT EXISTS gameRooms (
  27. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  28. roomID TEXT,
  29. hostAccountID TEXT,
  30. description TEXT NOT NULL DEFAULT '',
  31. status INTEGER NOT NULL DEFAULT 0,
  32. playerLimit INTEGER NOT NULL,
  33. creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
  34. );
  35. )";
  36. static const std::string createTableGameRoomPlayers = R"(
  37. CREATE TABLE IF NOT EXISTS gameRoomPlayers (
  38. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  39. roomID TEXT,
  40. accountID TEXT
  41. );
  42. )";
  43. static const std::string createTableAccounts = R"(
  44. CREATE TABLE IF NOT EXISTS accounts (
  45. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  46. accountID TEXT,
  47. displayName TEXT,
  48. online INTEGER NOT NULL,
  49. lastLoginTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
  50. creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
  51. );
  52. )";
  53. static const std::string createTableAccountCookies = R"(
  54. CREATE TABLE IF NOT EXISTS accountCookies (
  55. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  56. accountID TEXT,
  57. cookieUUID TEXT,
  58. creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
  59. );
  60. )";
  61. static const std::string createTableGameRoomInvites = R"(
  62. CREATE TABLE IF NOT EXISTS gameRoomInvites (
  63. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  64. roomID TEXT,
  65. accountID TEXT
  66. );
  67. )";
  68. database->prepare(createChatMessages)->execute();
  69. database->prepare(createTableGameRoomPlayers)->execute();
  70. database->prepare(createTableGameRooms)->execute();
  71. database->prepare(createTableAccounts)->execute();
  72. database->prepare(createTableAccountCookies)->execute();
  73. database->prepare(createTableGameRoomInvites)->execute();
  74. }
  75. void LobbyDatabase::upgradeDatabase()
  76. {
  77. auto getDatabaseVersionStatement = database->prepare(R"(
  78. PRAGMA user_version
  79. )");
  80. auto upgradeDatabaseVersionStatement = database->prepare(R"(
  81. PRAGMA user_version = 10501
  82. )");
  83. int databaseVersion;
  84. getDatabaseVersionStatement->execute();
  85. getDatabaseVersionStatement->getColumns(databaseVersion);
  86. getDatabaseVersionStatement->reset();
  87. if (databaseVersion < 10501)
  88. {
  89. database->prepare(R"(
  90. ALTER TABLE gameRooms
  91. ADD COLUMN mods TEXT NOT NULL DEFAULT '{}'
  92. )")->execute();
  93. database->prepare(R"(
  94. ALTER TABLE gameRooms
  95. ADD COLUMN version TEXT NOT NULL DEFAULT ''
  96. )")->execute();
  97. upgradeDatabaseVersionStatement->execute();
  98. upgradeDatabaseVersionStatement->reset();
  99. }
  100. }
  101. void LobbyDatabase::clearOldData()
  102. {
  103. static const std::string removeActiveAccounts = R"(
  104. UPDATE accounts
  105. SET online = 0
  106. WHERE online <> 0
  107. )";
  108. static const std::string removeActiveLobbyRooms = R"(
  109. UPDATE gameRooms
  110. SET status = 4
  111. WHERE status IN (0,1,2)
  112. )";
  113. static const std::string removeActiveGameRooms = R"(
  114. UPDATE gameRooms
  115. SET status = 5
  116. WHERE status = 3
  117. )";
  118. database->prepare(removeActiveAccounts)->execute();
  119. database->prepare(removeActiveLobbyRooms)->execute();
  120. database->prepare(removeActiveGameRooms)->execute();
  121. }
  122. void LobbyDatabase::prepareStatements()
  123. {
  124. // INSERT INTO
  125. insertChatMessageStatement = database->prepare(R"(
  126. INSERT INTO chatMessages(senderName, messageText, channelType, channelName) VALUES( ?, ?, ?, ?);
  127. )");
  128. insertAccountStatement = database->prepare(R"(
  129. INSERT INTO accounts(accountID, displayName, online) VALUES(?,?,0);
  130. )");
  131. insertAccessCookieStatement = database->prepare(R"(
  132. INSERT INTO accountCookies(accountID, cookieUUID) VALUES(?,?);
  133. )");
  134. insertGameRoomStatement = database->prepare(R"(
  135. INSERT INTO gameRooms(roomID, hostAccountID, status, playerLimit, version, mods) VALUES(?, ?, 0, 8, ?, ?);
  136. )");
  137. insertGameRoomPlayersStatement = database->prepare(R"(
  138. INSERT INTO gameRoomPlayers(roomID, accountID) VALUES(?,?);
  139. )");
  140. insertGameRoomInvitesStatement = database->prepare(R"(
  141. INSERT INTO gameRoomInvites(roomID, accountID) VALUES(?,?);
  142. )");
  143. // DELETE FROM
  144. deleteGameRoomPlayersStatement = database->prepare(R"(
  145. DELETE FROM gameRoomPlayers WHERE roomID = ? AND accountID = ?
  146. )");
  147. // UPDATE
  148. setAccountOnlineStatement = database->prepare(R"(
  149. UPDATE accounts
  150. SET online = ?
  151. WHERE accountID = ?
  152. )");
  153. setGameRoomStatusStatement = database->prepare(R"(
  154. UPDATE gameRooms
  155. SET status = ?
  156. WHERE roomID = ?
  157. )");
  158. updateAccountLoginTimeStatement = database->prepare(R"(
  159. UPDATE accounts
  160. SET lastLoginTime = CURRENT_TIMESTAMP
  161. WHERE accountID = ?
  162. )");
  163. updateRoomDescriptionStatement = database->prepare(R"(
  164. UPDATE gameRooms
  165. SET description = ?
  166. WHERE roomID = ?
  167. )");
  168. updateRoomPlayerLimitStatement = database->prepare(R"(
  169. UPDATE gameRooms
  170. SET playerLimit = ?
  171. WHERE roomID = ?
  172. )");
  173. // SELECT FROM
  174. getRecentMessageHistoryStatement = database->prepare(R"(
  175. SELECT senderName, displayName, messageText, strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',cm.creationTime) AS secondsElapsed
  176. FROM chatMessages cm
  177. LEFT JOIN accounts on accountID = senderName
  178. WHERE secondsElapsed < 60*60*18 AND channelType = ? AND channelName = ?
  179. ORDER BY cm.creationTime DESC
  180. LIMIT 100
  181. )");
  182. getFullMessageHistoryStatement = database->prepare(R"(
  183. SELECT senderName, displayName, messageText, strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',cm.creationTime) AS secondsElapsed
  184. FROM chatMessages cm
  185. LEFT JOIN accounts on accountID = senderName
  186. WHERE channelType = ? AND channelName = ?
  187. ORDER BY cm.creationTime DESC
  188. )");
  189. getIdleGameRoomStatement = database->prepare(R"(
  190. SELECT roomID
  191. FROM gameRooms
  192. WHERE hostAccountID = ? AND status = 0
  193. LIMIT 1
  194. )");
  195. getGameRoomStatusStatement = database->prepare(R"(
  196. SELECT status
  197. FROM gameRooms
  198. WHERE roomID = ?
  199. )");
  200. getAccountInviteStatusStatement = database->prepare(R"(
  201. SELECT COUNT(accountID)
  202. FROM gameRoomInvites
  203. WHERE accountID = ? AND roomID = ?
  204. )");
  205. getAccountGameHistoryStatement = database->prepare(R"(
  206. SELECT gr.roomID, hostAccountID, displayName, description, status, playerLimit, strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',gr.creationTime) AS secondsElapsed
  207. FROM gameRoomPlayers grp
  208. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  209. LEFT JOIN accounts a ON gr.hostAccountID = a.accountID
  210. WHERE grp.accountID = ? AND status = 5
  211. ORDER BY secondsElapsed ASC
  212. )");
  213. getAccountGameRoomStatement = database->prepare(R"(
  214. SELECT grp.roomID
  215. FROM gameRoomPlayers grp
  216. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  217. WHERE accountID = ? AND status IN (1, 2, 3)
  218. LIMIT 1
  219. )");
  220. getActiveAccountsStatement = database->prepare(R"(
  221. SELECT accountID, displayName
  222. FROM accounts
  223. WHERE online = 1
  224. )");
  225. getActiveGameRoomsStatement = database->prepare(R"(
  226. SELECT roomID, hostAccountID, displayName, description, status, playerLimit, version, mods, strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',gr.creationTime) AS secondsElapsed
  227. FROM gameRooms gr
  228. LEFT JOIN accounts a ON gr.hostAccountID = a.accountID
  229. WHERE status IN (1, 2, 3)
  230. ORDER BY secondsElapsed ASC
  231. )");
  232. getGameRoomInvitesStatement = database->prepare(R"(
  233. SELECT a.accountID, a.displayName
  234. FROM gameRoomInvites gri
  235. LEFT JOIN accounts a ON a.accountID = gri.accountID
  236. WHERE roomID = ?
  237. )");
  238. getGameRoomPlayersStatement = database->prepare(R"(
  239. SELECT a.accountID, a.displayName
  240. FROM gameRoomPlayers grp
  241. LEFT JOIN accounts a ON a.accountID = grp.accountID
  242. WHERE roomID = ?
  243. )");
  244. countRoomUsedSlotsStatement = database->prepare(R"(
  245. SELECT COUNT(grp.accountID)
  246. FROM gameRoomPlayers grp
  247. WHERE roomID = ?
  248. )");
  249. countRoomTotalSlotsStatement = database->prepare(R"(
  250. SELECT playerLimit
  251. FROM gameRooms
  252. WHERE roomID = ?
  253. )");
  254. getAccountDisplayNameStatement = database->prepare(R"(
  255. SELECT displayName
  256. FROM accounts
  257. WHERE accountID = ?
  258. )");
  259. isAccountCookieValidStatement = database->prepare(R"(
  260. SELECT COUNT(accountID)
  261. FROM accountCookies
  262. WHERE accountID = ? AND cookieUUID = ?
  263. )");
  264. isPlayerInGameRoomStatement = database->prepare(R"(
  265. SELECT COUNT(accountID)
  266. FROM gameRoomPlayers grp
  267. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  268. WHERE accountID = ? AND grp.roomID = ?
  269. )");
  270. isPlayerInAnyGameRoomStatement = database->prepare(R"(
  271. SELECT COUNT(accountID)
  272. FROM gameRoomPlayers grp
  273. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  274. WHERE accountID = ? AND status IN (1, 2, 3)
  275. )");
  276. isAccountIDExistsStatement = database->prepare(R"(
  277. SELECT COUNT(accountID)
  278. FROM accounts
  279. WHERE accountID = ?
  280. )");
  281. isAccountNameExistsStatement = database->prepare(R"(
  282. SELECT COUNT(displayName)
  283. FROM accounts
  284. WHERE displayName = ? COLLATE NOCASE
  285. )");
  286. }
  287. LobbyDatabase::~LobbyDatabase() = default;
  288. LobbyDatabase::LobbyDatabase(const boost::filesystem::path & databasePath)
  289. {
  290. database = SQLiteInstance::open(databasePath, true);
  291. createTables();
  292. upgradeDatabase();
  293. clearOldData();
  294. prepareStatements();
  295. }
  296. void LobbyDatabase::insertChatMessage(const std::string & sender, const std::string & channelType, const std::string & channelName, const std::string & messageText)
  297. {
  298. insertChatMessageStatement->executeOnce(sender, messageText, channelType, channelName);
  299. }
  300. bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountID)
  301. {
  302. bool result = false;
  303. isPlayerInAnyGameRoomStatement->setBinds(accountID);
  304. if(isPlayerInAnyGameRoomStatement->execute())
  305. isPlayerInAnyGameRoomStatement->getColumns(result);
  306. isPlayerInAnyGameRoomStatement->reset();
  307. return result;
  308. }
  309. bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountID, const std::string & roomID)
  310. {
  311. bool result = false;
  312. isPlayerInGameRoomStatement->setBinds(accountID, roomID);
  313. if(isPlayerInGameRoomStatement->execute())
  314. isPlayerInGameRoomStatement->getColumns(result);
  315. isPlayerInGameRoomStatement->reset();
  316. return result;
  317. }
  318. std::vector<LobbyChatMessage> LobbyDatabase::getRecentMessageHistory(const std::string & channelType, const std::string & channelName)
  319. {
  320. std::vector<LobbyChatMessage> result;
  321. getRecentMessageHistoryStatement->setBinds(channelType, channelName);
  322. while(getRecentMessageHistoryStatement->execute())
  323. {
  324. LobbyChatMessage message;
  325. getRecentMessageHistoryStatement->getColumns(message.accountID, message.displayName, message.messageText, message.age);
  326. result.push_back(message);
  327. }
  328. getRecentMessageHistoryStatement->reset();
  329. return result;
  330. }
  331. std::vector<LobbyChatMessage> LobbyDatabase::getFullMessageHistory(const std::string & channelType, const std::string & channelName)
  332. {
  333. std::vector<LobbyChatMessage> result;
  334. getFullMessageHistoryStatement->setBinds(channelType, channelName);
  335. while(getFullMessageHistoryStatement->execute())
  336. {
  337. LobbyChatMessage message;
  338. getFullMessageHistoryStatement->getColumns(message.accountID, message.displayName, message.messageText, message.age);
  339. result.push_back(message);
  340. }
  341. getFullMessageHistoryStatement->reset();
  342. return result;
  343. }
  344. void LobbyDatabase::setAccountOnline(const std::string & accountID, bool isOnline)
  345. {
  346. setAccountOnlineStatement->executeOnce(isOnline ? 1 : 0, accountID);
  347. }
  348. void LobbyDatabase::setGameRoomStatus(const std::string & roomID, LobbyRoomState roomStatus)
  349. {
  350. setGameRoomStatusStatement->executeOnce(vstd::to_underlying(roomStatus), roomID);
  351. }
  352. void LobbyDatabase::insertPlayerIntoGameRoom(const std::string & accountID, const std::string & roomID)
  353. {
  354. insertGameRoomPlayersStatement->executeOnce(roomID, accountID);
  355. }
  356. void LobbyDatabase::deletePlayerFromGameRoom(const std::string & accountID, const std::string & roomID)
  357. {
  358. deleteGameRoomPlayersStatement->executeOnce(roomID, accountID);
  359. }
  360. void LobbyDatabase::deleteGameRoomInvite(const std::string & targetAccountID, const std::string & roomID)
  361. {
  362. deleteGameRoomInvitesStatement->executeOnce(roomID, targetAccountID);
  363. }
  364. void LobbyDatabase::insertGameRoomInvite(const std::string & targetAccountID, const std::string & roomID)
  365. {
  366. insertGameRoomInvitesStatement->executeOnce(roomID, targetAccountID);
  367. }
  368. void LobbyDatabase::insertGameRoom(const std::string & roomID, const std::string & hostAccountID, const std::string & serverVersion, const std::string & modListJson)
  369. {
  370. insertGameRoomStatement->executeOnce(roomID, hostAccountID, serverVersion, modListJson);
  371. }
  372. void LobbyDatabase::insertAccount(const std::string & accountID, const std::string & displayName)
  373. {
  374. insertAccountStatement->executeOnce(accountID, displayName);
  375. }
  376. void LobbyDatabase::insertAccessCookie(const std::string & accountID, const std::string & accessCookieUUID)
  377. {
  378. insertAccessCookieStatement->executeOnce(accountID, accessCookieUUID);
  379. }
  380. void LobbyDatabase::updateAccountLoginTime(const std::string & accountID)
  381. {
  382. updateAccountLoginTimeStatement->executeOnce(accountID);
  383. }
  384. void LobbyDatabase::updateRoomPlayerLimit(const std::string & gameRoomID, int playerLimit)
  385. {
  386. updateRoomPlayerLimitStatement->executeOnce(playerLimit, gameRoomID);
  387. }
  388. void LobbyDatabase::updateRoomDescription(const std::string & gameRoomID, const std::string & description)
  389. {
  390. updateRoomDescriptionStatement->executeOnce(description, gameRoomID);
  391. }
  392. std::string LobbyDatabase::getAccountDisplayName(const std::string & accountID)
  393. {
  394. std::string result;
  395. getAccountDisplayNameStatement->setBinds(accountID);
  396. if(getAccountDisplayNameStatement->execute())
  397. getAccountDisplayNameStatement->getColumns(result);
  398. getAccountDisplayNameStatement->reset();
  399. return result;
  400. }
  401. LobbyCookieStatus LobbyDatabase::getAccountCookieStatus(const std::string & accountID, const std::string & accessCookieUUID)
  402. {
  403. bool result = false;
  404. isAccountCookieValidStatement->setBinds(accountID, accessCookieUUID);
  405. if(isAccountCookieValidStatement->execute())
  406. isAccountCookieValidStatement->getColumns(result);
  407. isAccountCookieValidStatement->reset();
  408. return result ? LobbyCookieStatus::VALID : LobbyCookieStatus::INVALID;
  409. }
  410. LobbyInviteStatus LobbyDatabase::getAccountInviteStatus(const std::string & accountID, const std::string & roomID)
  411. {
  412. int result = 0;
  413. getAccountInviteStatusStatement->setBinds(accountID, roomID);
  414. if(getAccountInviteStatusStatement->execute())
  415. getAccountInviteStatusStatement->getColumns(result);
  416. getAccountInviteStatusStatement->reset();
  417. if (result > 0)
  418. return LobbyInviteStatus::INVITED;
  419. else
  420. return LobbyInviteStatus::NOT_INVITED;
  421. }
  422. LobbyRoomState LobbyDatabase::getGameRoomStatus(const std::string & roomID)
  423. {
  424. LobbyRoomState result;
  425. getGameRoomStatusStatement->setBinds(roomID);
  426. if(getGameRoomStatusStatement->execute())
  427. getGameRoomStatusStatement->getColumns(result);
  428. else
  429. result = LobbyRoomState::CLOSED;
  430. getGameRoomStatusStatement->reset();
  431. return result;
  432. }
  433. uint32_t LobbyDatabase::getGameRoomFreeSlots(const std::string & roomID)
  434. {
  435. uint32_t usedSlots = 0;
  436. uint32_t totalSlots = 0;
  437. countRoomUsedSlotsStatement->setBinds(roomID);
  438. if(countRoomUsedSlotsStatement->execute())
  439. countRoomUsedSlotsStatement->getColumns(usedSlots);
  440. countRoomUsedSlotsStatement->reset();
  441. countRoomTotalSlotsStatement->setBinds(roomID);
  442. if(countRoomTotalSlotsStatement->execute())
  443. countRoomTotalSlotsStatement->getColumns(totalSlots);
  444. countRoomTotalSlotsStatement->reset();
  445. if (totalSlots > usedSlots)
  446. return totalSlots - usedSlots;
  447. return 0;
  448. }
  449. bool LobbyDatabase::isAccountNameExists(const std::string & displayName)
  450. {
  451. bool result = false;
  452. isAccountNameExistsStatement->setBinds(displayName);
  453. if(isAccountNameExistsStatement->execute())
  454. isAccountNameExistsStatement->getColumns(result);
  455. isAccountNameExistsStatement->reset();
  456. return result;
  457. }
  458. bool LobbyDatabase::isAccountIDExists(const std::string & accountID)
  459. {
  460. bool result = false;
  461. isAccountIDExistsStatement->setBinds(accountID);
  462. if(isAccountIDExistsStatement->execute())
  463. isAccountIDExistsStatement->getColumns(result);
  464. isAccountIDExistsStatement->reset();
  465. return result;
  466. }
  467. std::vector<LobbyGameRoom> LobbyDatabase::getActiveGameRooms()
  468. {
  469. std::vector<LobbyGameRoom> result;
  470. while(getActiveGameRoomsStatement->execute())
  471. {
  472. LobbyGameRoom entry;
  473. getActiveGameRoomsStatement->getColumns(entry.roomID, entry.hostAccountID, entry.hostAccountDisplayName, entry.description, entry.roomState, entry.playerLimit, entry.version, entry.modsJson, entry.age);
  474. result.push_back(entry);
  475. }
  476. getActiveGameRoomsStatement->reset();
  477. for (auto & room : result)
  478. {
  479. getGameRoomPlayersStatement->setBinds(room.roomID);
  480. while(getGameRoomPlayersStatement->execute())
  481. {
  482. LobbyAccount account;
  483. getGameRoomPlayersStatement->getColumns(account.accountID, account.displayName);
  484. room.participants.push_back(account);
  485. }
  486. getGameRoomPlayersStatement->reset();
  487. }
  488. for (auto & room : result)
  489. {
  490. getGameRoomInvitesStatement->setBinds(room.roomID);
  491. while(getGameRoomInvitesStatement->execute())
  492. {
  493. LobbyAccount account;
  494. getGameRoomInvitesStatement->getColumns(account.accountID, account.displayName);
  495. room.invited.push_back(account);
  496. }
  497. getGameRoomInvitesStatement->reset();
  498. }
  499. return result;
  500. }
  501. std::vector<LobbyGameRoom> LobbyDatabase::getAccountGameHistory(const std::string & accountID)
  502. {
  503. std::vector<LobbyGameRoom> result;
  504. getAccountGameHistoryStatement->setBinds(accountID);
  505. while(getAccountGameHistoryStatement->execute())
  506. {
  507. LobbyGameRoom entry;
  508. getAccountGameHistoryStatement->getColumns(entry.roomID, entry.hostAccountID, entry.hostAccountDisplayName, entry.description, entry.roomState, entry.playerLimit, entry.age);
  509. result.push_back(entry);
  510. }
  511. getAccountGameHistoryStatement->reset();
  512. for (auto & room : result)
  513. {
  514. getGameRoomPlayersStatement->setBinds(room.roomID);
  515. while(getGameRoomPlayersStatement->execute())
  516. {
  517. LobbyAccount account;
  518. getGameRoomPlayersStatement->getColumns(account.accountID, account.displayName);
  519. room.participants.push_back(account);
  520. }
  521. getGameRoomPlayersStatement->reset();
  522. }
  523. return result;
  524. }
  525. std::vector<LobbyAccount> LobbyDatabase::getActiveAccounts()
  526. {
  527. std::vector<LobbyAccount> result;
  528. while(getActiveAccountsStatement->execute())
  529. {
  530. LobbyAccount entry;
  531. getActiveAccountsStatement->getColumns(entry.accountID, entry.displayName);
  532. result.push_back(entry);
  533. }
  534. getActiveAccountsStatement->reset();
  535. return result;
  536. }
  537. std::string LobbyDatabase::getIdleGameRoom(const std::string & hostAccountID)
  538. {
  539. std::string result;
  540. getIdleGameRoomStatement->setBinds(hostAccountID);
  541. if(getIdleGameRoomStatement->execute())
  542. getIdleGameRoomStatement->getColumns(result);
  543. getIdleGameRoomStatement->reset();
  544. return result;
  545. }
  546. std::string LobbyDatabase::getAccountGameRoom(const std::string & accountID)
  547. {
  548. std::string result;
  549. getAccountGameRoomStatement->setBinds(accountID);
  550. if(getAccountGameRoomStatement->execute())
  551. getAccountGameRoomStatement->getColumns(result);
  552. getAccountGameRoomStatement->reset();
  553. return result;
  554. }