LobbyDatabase.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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::clearOldData()
  76. {
  77. static const std::string removeActiveAccounts = R"(
  78. UPDATE accounts
  79. SET online = 0
  80. WHERE online <> 0
  81. )";
  82. static const std::string removeActiveLobbyRooms = R"(
  83. UPDATE gameRooms
  84. SET status = 4
  85. WHERE status IN (0,1,2)
  86. )";
  87. static const std::string removeActiveGameRooms = R"(
  88. UPDATE gameRooms
  89. SET status = 5
  90. WHERE status = 3
  91. )";
  92. database->prepare(removeActiveAccounts)->execute();
  93. database->prepare(removeActiveLobbyRooms)->execute();
  94. database->prepare(removeActiveGameRooms)->execute();
  95. }
  96. void LobbyDatabase::prepareStatements()
  97. {
  98. // INSERT INTO
  99. insertChatMessageStatement = database->prepare(R"(
  100. INSERT INTO chatMessages(senderName, messageText, channelType, channelName) VALUES( ?, ?, ?, ?);
  101. )");
  102. insertAccountStatement = database->prepare(R"(
  103. INSERT INTO accounts(accountID, displayName, online) VALUES(?,?,0);
  104. )");
  105. insertAccessCookieStatement = database->prepare(R"(
  106. INSERT INTO accountCookies(accountID, cookieUUID) VALUES(?,?);
  107. )");
  108. insertGameRoomStatement = database->prepare(R"(
  109. INSERT INTO gameRooms(roomID, hostAccountID, status, playerLimit) VALUES(?, ?, 0, 8);
  110. )");
  111. insertGameRoomPlayersStatement = database->prepare(R"(
  112. INSERT INTO gameRoomPlayers(roomID, accountID) VALUES(?,?);
  113. )");
  114. insertGameRoomInvitesStatement = database->prepare(R"(
  115. INSERT INTO gameRoomInvites(roomID, accountID) VALUES(?,?);
  116. )");
  117. // DELETE FROM
  118. deleteGameRoomPlayersStatement = database->prepare(R"(
  119. DELETE FROM gameRoomPlayers WHERE roomID = ? AND accountID = ?
  120. )");
  121. // UPDATE
  122. setAccountOnlineStatement = database->prepare(R"(
  123. UPDATE accounts
  124. SET online = ?
  125. WHERE accountID = ?
  126. )");
  127. setGameRoomStatusStatement = database->prepare(R"(
  128. UPDATE gameRooms
  129. SET status = ?
  130. WHERE roomID = ?
  131. )");
  132. updateAccountLoginTimeStatement = database->prepare(R"(
  133. UPDATE accounts
  134. SET lastLoginTime = CURRENT_TIMESTAMP
  135. WHERE accountID = ?
  136. )");
  137. updateRoomDescriptionStatement = database->prepare(R"(
  138. UPDATE gameRooms
  139. SET description = ?
  140. WHERE roomID = ?
  141. )");
  142. updateRoomPlayerLimitStatement = database->prepare(R"(
  143. UPDATE gameRooms
  144. SET playerLimit = ?
  145. WHERE roomID = ?
  146. )");
  147. // SELECT FROM
  148. getRecentMessageHistoryStatement = database->prepare(R"(
  149. SELECT senderName, displayName, messageText, strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',cm.creationTime) AS secondsElapsed
  150. FROM chatMessages cm
  151. LEFT JOIN accounts on accountID = senderName
  152. WHERE secondsElapsed < 60*60*18 AND channelType = ? AND channelName = ?
  153. ORDER BY cm.creationTime DESC
  154. LIMIT 100
  155. )");
  156. getFullMessageHistoryStatement = database->prepare(R"(
  157. SELECT senderName, displayName, messageText, strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',cm.creationTime) AS secondsElapsed
  158. FROM chatMessages cm
  159. LEFT JOIN accounts on accountID = senderName
  160. WHERE channelType = ? AND channelName = ?
  161. ORDER BY cm.creationTime DESC
  162. )");
  163. getIdleGameRoomStatement = database->prepare(R"(
  164. SELECT roomID
  165. FROM gameRooms
  166. WHERE hostAccountID = ? AND status = 0
  167. LIMIT 1
  168. )");
  169. getGameRoomStatusStatement = database->prepare(R"(
  170. SELECT status
  171. FROM gameRooms
  172. WHERE roomID = ?
  173. )");
  174. getAccountInviteStatusStatement = database->prepare(R"(
  175. SELECT COUNT(accountID)
  176. FROM gameRoomInvites
  177. WHERE accountID = ? AND roomID = ?
  178. )");
  179. getAccountGameHistoryStatement = database->prepare(R"(
  180. SELECT gr.roomID, hostAccountID, displayName, description, status, playerLimit, strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',gr.creationTime) AS secondsElapsed
  181. FROM gameRoomPlayers grp
  182. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  183. LEFT JOIN accounts a ON gr.hostAccountID = a.accountID
  184. WHERE grp.accountID = ? AND status = 5
  185. ORDER BY secondsElapsed ASC
  186. )");
  187. getAccountGameRoomStatement = database->prepare(R"(
  188. SELECT grp.roomID
  189. FROM gameRoomPlayers grp
  190. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  191. WHERE accountID = ? AND status IN (1, 2, 3)
  192. LIMIT 1
  193. )");
  194. getActiveAccountsStatement = database->prepare(R"(
  195. SELECT accountID, displayName
  196. FROM accounts
  197. WHERE online = 1
  198. )");
  199. getActiveGameRoomsStatement = database->prepare(R"(
  200. SELECT roomID, hostAccountID, displayName, description, status, playerLimit, strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',gr.creationTime) AS secondsElapsed
  201. FROM gameRooms gr
  202. LEFT JOIN accounts a ON gr.hostAccountID = a.accountID
  203. WHERE status IN (1, 2, 3)
  204. ORDER BY secondsElapsed ASC
  205. )");
  206. countRoomUsedSlotsStatement = database->prepare(R"(
  207. SELECT a.accountID, a.displayName
  208. FROM gameRoomPlayers grp
  209. LEFT JOIN accounts a ON a.accountID = grp.accountID
  210. WHERE roomID = ?
  211. )");
  212. countRoomTotalSlotsStatement = database->prepare(R"(
  213. SELECT playerLimit
  214. FROM gameRooms
  215. WHERE roomID = ?
  216. )");
  217. getAccountDisplayNameStatement = database->prepare(R"(
  218. SELECT displayName
  219. FROM accounts
  220. WHERE accountID = ?
  221. )");
  222. isAccountCookieValidStatement = database->prepare(R"(
  223. SELECT COUNT(accountID)
  224. FROM accountCookies
  225. WHERE accountID = ? AND cookieUUID = ?
  226. )");
  227. isPlayerInGameRoomStatement = database->prepare(R"(
  228. SELECT COUNT(accountID)
  229. FROM gameRoomPlayers grp
  230. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  231. WHERE accountID = ? AND grp.roomID = ?
  232. )");
  233. isPlayerInAnyGameRoomStatement = database->prepare(R"(
  234. SELECT COUNT(accountID)
  235. FROM gameRoomPlayers grp
  236. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  237. WHERE accountID = ? AND status IN (1, 2, 3)
  238. )");
  239. isAccountIDExistsStatement = database->prepare(R"(
  240. SELECT COUNT(accountID)
  241. FROM accounts
  242. WHERE accountID = ?
  243. )");
  244. isAccountNameExistsStatement = database->prepare(R"(
  245. SELECT COUNT(displayName)
  246. FROM accounts
  247. WHERE displayName = ?
  248. )");
  249. }
  250. LobbyDatabase::~LobbyDatabase() = default;
  251. LobbyDatabase::LobbyDatabase(const boost::filesystem::path & databasePath)
  252. {
  253. database = SQLiteInstance::open(databasePath, true);
  254. createTables();
  255. clearOldData();
  256. prepareStatements();
  257. }
  258. void LobbyDatabase::insertChatMessage(const std::string & sender, const std::string & channelType, const std::string & channelName, const std::string & messageText)
  259. {
  260. insertChatMessageStatement->executeOnce(sender, messageText, channelType, channelName);
  261. }
  262. bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountID)
  263. {
  264. bool result = false;
  265. isPlayerInAnyGameRoomStatement->setBinds(accountID);
  266. if(isPlayerInAnyGameRoomStatement->execute())
  267. isPlayerInAnyGameRoomStatement->getColumns(result);
  268. isPlayerInAnyGameRoomStatement->reset();
  269. return result;
  270. }
  271. bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountID, const std::string & roomID)
  272. {
  273. bool result = false;
  274. isPlayerInGameRoomStatement->setBinds(accountID, roomID);
  275. if(isPlayerInGameRoomStatement->execute())
  276. isPlayerInGameRoomStatement->getColumns(result);
  277. isPlayerInGameRoomStatement->reset();
  278. return result;
  279. }
  280. std::vector<LobbyChatMessage> LobbyDatabase::getRecentMessageHistory(const std::string & channelType, const std::string & channelName)
  281. {
  282. std::vector<LobbyChatMessage> result;
  283. getRecentMessageHistoryStatement->setBinds(channelType, channelName);
  284. while(getRecentMessageHistoryStatement->execute())
  285. {
  286. LobbyChatMessage message;
  287. getRecentMessageHistoryStatement->getColumns(message.accountID, message.displayName, message.messageText, message.age);
  288. result.push_back(message);
  289. }
  290. getRecentMessageHistoryStatement->reset();
  291. return result;
  292. }
  293. std::vector<LobbyChatMessage> LobbyDatabase::getFullMessageHistory(const std::string & channelType, const std::string & channelName)
  294. {
  295. std::vector<LobbyChatMessage> result;
  296. getFullMessageHistoryStatement->setBinds(channelType, channelName);
  297. while(getFullMessageHistoryStatement->execute())
  298. {
  299. LobbyChatMessage message;
  300. getFullMessageHistoryStatement->getColumns(message.accountID, message.displayName, message.messageText, message.age);
  301. result.push_back(message);
  302. }
  303. getFullMessageHistoryStatement->reset();
  304. return result;
  305. }
  306. void LobbyDatabase::setAccountOnline(const std::string & accountID, bool isOnline)
  307. {
  308. setAccountOnlineStatement->executeOnce(isOnline ? 1 : 0, accountID);
  309. }
  310. void LobbyDatabase::setGameRoomStatus(const std::string & roomID, LobbyRoomState roomStatus)
  311. {
  312. setGameRoomStatusStatement->executeOnce(vstd::to_underlying(roomStatus), roomID);
  313. }
  314. void LobbyDatabase::insertPlayerIntoGameRoom(const std::string & accountID, const std::string & roomID)
  315. {
  316. insertGameRoomPlayersStatement->executeOnce(roomID, accountID);
  317. }
  318. void LobbyDatabase::deletePlayerFromGameRoom(const std::string & accountID, const std::string & roomID)
  319. {
  320. deleteGameRoomPlayersStatement->executeOnce(roomID, accountID);
  321. }
  322. void LobbyDatabase::deleteGameRoomInvite(const std::string & targetAccountID, const std::string & roomID)
  323. {
  324. deleteGameRoomInvitesStatement->executeOnce(roomID, targetAccountID);
  325. }
  326. void LobbyDatabase::insertGameRoomInvite(const std::string & targetAccountID, const std::string & roomID)
  327. {
  328. insertGameRoomInvitesStatement->executeOnce(roomID, targetAccountID);
  329. }
  330. void LobbyDatabase::insertGameRoom(const std::string & roomID, const std::string & hostAccountID)
  331. {
  332. insertGameRoomStatement->executeOnce(roomID, hostAccountID);
  333. }
  334. void LobbyDatabase::insertAccount(const std::string & accountID, const std::string & displayName)
  335. {
  336. insertAccountStatement->executeOnce(accountID, displayName);
  337. }
  338. void LobbyDatabase::insertAccessCookie(const std::string & accountID, const std::string & accessCookieUUID)
  339. {
  340. insertAccessCookieStatement->executeOnce(accountID, accessCookieUUID);
  341. }
  342. void LobbyDatabase::updateAccountLoginTime(const std::string & accountID)
  343. {
  344. updateAccountLoginTimeStatement->executeOnce(accountID);
  345. }
  346. void LobbyDatabase::updateRoomPlayerLimit(const std::string & gameRoomID, int playerLimit)
  347. {
  348. updateRoomPlayerLimitStatement->executeOnce(playerLimit, gameRoomID);
  349. }
  350. void LobbyDatabase::updateRoomDescription(const std::string & gameRoomID, const std::string & description)
  351. {
  352. updateRoomDescriptionStatement->executeOnce(description, gameRoomID);
  353. }
  354. std::string LobbyDatabase::getAccountDisplayName(const std::string & accountID)
  355. {
  356. std::string result;
  357. getAccountDisplayNameStatement->setBinds(accountID);
  358. if(getAccountDisplayNameStatement->execute())
  359. getAccountDisplayNameStatement->getColumns(result);
  360. getAccountDisplayNameStatement->reset();
  361. return result;
  362. }
  363. LobbyCookieStatus LobbyDatabase::getAccountCookieStatus(const std::string & accountID, const std::string & accessCookieUUID)
  364. {
  365. bool result = false;
  366. isAccountCookieValidStatement->setBinds(accountID, accessCookieUUID);
  367. if(isAccountCookieValidStatement->execute())
  368. isAccountCookieValidStatement->getColumns(result);
  369. isAccountCookieValidStatement->reset();
  370. return result ? LobbyCookieStatus::VALID : LobbyCookieStatus::INVALID;
  371. }
  372. LobbyInviteStatus LobbyDatabase::getAccountInviteStatus(const std::string & accountID, const std::string & roomID)
  373. {
  374. int result = 0;
  375. getAccountInviteStatusStatement->setBinds(accountID, roomID);
  376. if(getAccountInviteStatusStatement->execute())
  377. getAccountInviteStatusStatement->getColumns(result);
  378. getAccountInviteStatusStatement->reset();
  379. if (result > 0)
  380. return LobbyInviteStatus::INVITED;
  381. else
  382. return LobbyInviteStatus::NOT_INVITED;
  383. }
  384. LobbyRoomState LobbyDatabase::getGameRoomStatus(const std::string & roomID)
  385. {
  386. LobbyRoomState result;
  387. getGameRoomStatusStatement->setBinds(roomID);
  388. if(getGameRoomStatusStatement->execute())
  389. getGameRoomStatusStatement->getColumns(result);
  390. else
  391. result = LobbyRoomState::CLOSED;
  392. getGameRoomStatusStatement->reset();
  393. return result;
  394. }
  395. uint32_t LobbyDatabase::getGameRoomFreeSlots(const std::string & roomID)
  396. {
  397. uint32_t usedSlots = 0;
  398. uint32_t totalSlots = 0;
  399. countRoomUsedSlotsStatement->setBinds(roomID);
  400. if(countRoomUsedSlotsStatement->execute())
  401. countRoomUsedSlotsStatement->getColumns(usedSlots);
  402. countRoomUsedSlotsStatement->reset();
  403. countRoomTotalSlotsStatement->setBinds(roomID);
  404. if(countRoomTotalSlotsStatement->execute())
  405. countRoomTotalSlotsStatement->getColumns(totalSlots);
  406. countRoomTotalSlotsStatement->reset();
  407. if (totalSlots > usedSlots)
  408. return totalSlots - usedSlots;
  409. return 0;
  410. }
  411. bool LobbyDatabase::isAccountNameExists(const std::string & displayName)
  412. {
  413. bool result = false;
  414. isAccountNameExistsStatement->setBinds(displayName);
  415. if(isAccountNameExistsStatement->execute())
  416. isAccountNameExistsStatement->getColumns(result);
  417. isAccountNameExistsStatement->reset();
  418. return result;
  419. }
  420. bool LobbyDatabase::isAccountIDExists(const std::string & accountID)
  421. {
  422. bool result = false;
  423. isAccountIDExistsStatement->setBinds(accountID);
  424. if(isAccountIDExistsStatement->execute())
  425. isAccountIDExistsStatement->getColumns(result);
  426. isAccountIDExistsStatement->reset();
  427. return result;
  428. }
  429. std::vector<LobbyGameRoom> LobbyDatabase::getActiveGameRooms()
  430. {
  431. std::vector<LobbyGameRoom> result;
  432. while(getActiveGameRoomsStatement->execute())
  433. {
  434. LobbyGameRoom entry;
  435. getActiveGameRoomsStatement->getColumns(entry.roomID, entry.hostAccountID, entry.hostAccountDisplayName, entry.description, entry.roomState, entry.playerLimit, entry.age);
  436. result.push_back(entry);
  437. }
  438. getActiveGameRoomsStatement->reset();
  439. for (auto & room : result)
  440. {
  441. countRoomUsedSlotsStatement->setBinds(room.roomID);
  442. while(countRoomUsedSlotsStatement->execute())
  443. {
  444. LobbyAccount account;
  445. countRoomUsedSlotsStatement->getColumns(account.accountID, account.displayName);
  446. room.participants.push_back(account);
  447. }
  448. countRoomUsedSlotsStatement->reset();
  449. }
  450. return result;
  451. }
  452. std::vector<LobbyGameRoom> LobbyDatabase::getAccountGameHistory(const std::string & accountID)
  453. {
  454. std::vector<LobbyGameRoom> result;
  455. getAccountGameHistoryStatement->setBinds(accountID);
  456. while(getAccountGameHistoryStatement->execute())
  457. {
  458. LobbyGameRoom entry;
  459. getAccountGameHistoryStatement->getColumns(entry.roomID, entry.hostAccountID, entry.hostAccountDisplayName, entry.description, entry.roomState, entry.playerLimit, entry.age);
  460. result.push_back(entry);
  461. }
  462. getAccountGameHistoryStatement->reset();
  463. for (auto & room : result)
  464. {
  465. countRoomUsedSlotsStatement->setBinds(room.roomID);
  466. while(countRoomUsedSlotsStatement->execute())
  467. {
  468. LobbyAccount account;
  469. countRoomUsedSlotsStatement->getColumns(account.accountID, account.displayName);
  470. room.participants.push_back(account);
  471. }
  472. countRoomUsedSlotsStatement->reset();
  473. }
  474. return result;
  475. }
  476. std::vector<LobbyAccount> LobbyDatabase::getActiveAccounts()
  477. {
  478. std::vector<LobbyAccount> result;
  479. while(getActiveAccountsStatement->execute())
  480. {
  481. LobbyAccount entry;
  482. getActiveAccountsStatement->getColumns(entry.accountID, entry.displayName);
  483. result.push_back(entry);
  484. }
  485. getActiveAccountsStatement->reset();
  486. return result;
  487. }
  488. std::string LobbyDatabase::getIdleGameRoom(const std::string & hostAccountID)
  489. {
  490. std::string result;
  491. getIdleGameRoomStatement->setBinds(hostAccountID);
  492. if(getIdleGameRoomStatement->execute())
  493. getIdleGameRoomStatement->getColumns(result);
  494. getIdleGameRoomStatement->reset();
  495. return result;
  496. }
  497. std::string LobbyDatabase::getAccountGameRoom(const std::string & accountID)
  498. {
  499. std::string result;
  500. getAccountGameRoomStatement->setBinds(accountID);
  501. if(getAccountGameRoomStatement->execute())
  502. getAccountGameRoomStatement->getColumns(result);
  503. getAccountGameRoomStatement->reset();
  504. return result;
  505. }