LobbyDatabase.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. roomType TEXT,
  20. messageText TEXT,
  21. creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
  22. );
  23. )";
  24. static const std::string createTableGameRooms = R"(
  25. CREATE TABLE IF NOT EXISTS gameRooms (
  26. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  27. roomID TEXT,
  28. hostAccountID TEXT,
  29. description TEXT NOT NULL DEFAULT '',
  30. status INTEGER NOT NULL DEFAULT 0,
  31. playerLimit INTEGER NOT NULL,
  32. creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
  33. );
  34. )";
  35. static const std::string createTableGameRoomPlayers = R"(
  36. CREATE TABLE IF NOT EXISTS gameRoomPlayers (
  37. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  38. roomID TEXT,
  39. accountID TEXT
  40. );
  41. )";
  42. static const std::string createTableAccounts = R"(
  43. CREATE TABLE IF NOT EXISTS accounts (
  44. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  45. accountID TEXT,
  46. displayName TEXT,
  47. online INTEGER NOT NULL,
  48. lastLoginTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
  49. creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
  50. );
  51. )";
  52. static const std::string createTableAccountCookies = R"(
  53. CREATE TABLE IF NOT EXISTS accountCookies (
  54. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  55. accountID TEXT,
  56. cookieUUID TEXT,
  57. creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
  58. );
  59. )";
  60. static const std::string createTableGameRoomInvites = R"(
  61. CREATE TABLE IF NOT EXISTS gameRoomInvites (
  62. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  63. roomID TEXT,
  64. accountID TEXT
  65. );
  66. )";
  67. database->prepare(createChatMessages)->execute();
  68. database->prepare(createTableGameRoomPlayers)->execute();
  69. database->prepare(createTableGameRooms)->execute();
  70. database->prepare(createTableAccounts)->execute();
  71. database->prepare(createTableAccountCookies)->execute();
  72. database->prepare(createTableGameRoomInvites)->execute();
  73. }
  74. void LobbyDatabase::clearOldData()
  75. {
  76. static const std::string removeActiveAccounts = R"(
  77. UPDATE accounts
  78. SET online = 0
  79. WHERE online <> 0
  80. )";
  81. static const std::string removeActiveRooms = R"(
  82. UPDATE gameRooms
  83. SET status = 5
  84. WHERE status <> 5
  85. )";
  86. database->prepare(removeActiveAccounts)->execute();
  87. database->prepare(removeActiveRooms)->execute();
  88. }
  89. void LobbyDatabase::prepareStatements()
  90. {
  91. // INSERT INTO
  92. static const std::string insertChatMessageText = R"(
  93. INSERT INTO chatMessages(senderName, messageText) VALUES( ?, ?);
  94. )";
  95. static const std::string insertAccountText = R"(
  96. INSERT INTO accounts(accountID, displayName, online) VALUES(?,?,0);
  97. )";
  98. static const std::string insertAccessCookieText = R"(
  99. INSERT INTO accountCookies(accountID, cookieUUID) VALUES(?,?);
  100. )";
  101. static const std::string insertGameRoomText = R"(
  102. INSERT INTO gameRooms(roomID, hostAccountID, status, playerLimit) VALUES(?, ?, 0, 8);
  103. )";
  104. static const std::string insertGameRoomPlayersText = R"(
  105. INSERT INTO gameRoomPlayers(roomID, accountID) VALUES(?,?);
  106. )";
  107. static const std::string insertGameRoomInvitesText = R"(
  108. INSERT INTO gameRoomInvites(roomID, accountID) VALUES(?,?);
  109. )";
  110. // DELETE FROM
  111. static const std::string deleteGameRoomPlayersText = R"(
  112. DELETE FROM gameRoomPlayers WHERE roomID = ? AND accountID = ?
  113. )";
  114. static const std::string deleteGameRoomInvitesText = R"(
  115. DELETE FROM gameRoomInvites WHERE roomID = ? AND accountID = ?
  116. )";
  117. // UPDATE
  118. static const std::string setAccountOnlineText = R"(
  119. UPDATE accounts
  120. SET online = ?
  121. WHERE accountID = ?
  122. )";
  123. static const std::string setGameRoomStatusText = R"(
  124. UPDATE gameRooms
  125. SET status = ?
  126. WHERE roomID = ?
  127. )";
  128. static const std::string updateAccountLoginTimeText = R"(
  129. UPDATE accounts
  130. SET lastLoginTime = CURRENT_TIMESTAMP
  131. WHERE accountID = ?
  132. )";
  133. static const std::string updateRoomDescriptionText = R"(
  134. UPDATE gameRooms
  135. SET description = ?
  136. WHERE roomID = ?
  137. )";
  138. static const std::string updateRoomPlayerLimitText = R"(
  139. UPDATE gameRooms
  140. SET playerLimit = ?
  141. WHERE roomID = ?
  142. )";
  143. // SELECT FROM
  144. static const std::string getRecentMessageHistoryText = R"(
  145. SELECT senderName, displayName, messageText, strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',cm.creationTime) AS secondsElapsed
  146. FROM chatMessages cm
  147. LEFT JOIN accounts on accountID = senderName
  148. WHERE secondsElapsed < 60*60*18
  149. ORDER BY cm.creationTime DESC
  150. LIMIT 100
  151. )";
  152. static const std::string getIdleGameRoomText = R"(
  153. SELECT roomID
  154. FROM gameRooms
  155. WHERE hostAccountID = ? AND status = 0
  156. LIMIT 1
  157. )";
  158. static const std::string getGameRoomStatusText = R"(
  159. SELECT status
  160. FROM gameRooms
  161. WHERE roomID = ?
  162. )";
  163. static const std::string getAccountGameRoomText = R"(
  164. SELECT grp.roomID
  165. FROM gameRoomPlayers grp
  166. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  167. WHERE accountID = ? AND status IN (1, 2, 3)
  168. LIMIT 1
  169. )";
  170. static const std::string getActiveAccountsText = R"(
  171. SELECT accountID, displayName
  172. FROM accounts
  173. WHERE online = 1
  174. )";
  175. static const std::string getActiveGameRoomsText = R"(
  176. SELECT roomID, hostAccountID, displayName, description, status, playerLimit
  177. FROM gameRooms
  178. LEFT JOIN accounts ON hostAccountID = accountID
  179. WHERE status IN (1, 2, 3)
  180. )";
  181. static const std::string countRoomUsedSlotsText = R"(
  182. SELECT COUNT(accountID)
  183. FROM gameRoomPlayers
  184. WHERE roomID = ?
  185. )";
  186. static const std::string countRoomTotalSlotsText = R"(
  187. SELECT playerLimit
  188. FROM gameRooms
  189. WHERE roomID = ?
  190. )";
  191. static const std::string getAccountDisplayNameText = R"(
  192. SELECT displayName
  193. FROM accounts
  194. WHERE accountID = ?
  195. )";
  196. static const std::string isAccountCookieValidText = R"(
  197. SELECT COUNT(accountID)
  198. FROM accountCookies
  199. WHERE accountID = ? AND cookieUUID = ?
  200. )";
  201. static const std::string isGameRoomCookieValidText = R"(
  202. SELECT COUNT(roomID)
  203. FROM gameRooms
  204. LEFT JOIN accountCookies ON accountCookies.accountID = gameRooms.hostAccountID
  205. WHERE roomID = ? AND cookieUUID = ? AND strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',creationTime) < ?
  206. )";
  207. static const std::string isPlayerInGameRoomText = R"(
  208. SELECT COUNT(accountID)
  209. FROM gameRoomPlayers grp
  210. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  211. WHERE accountID = ? AND grp.roomID = ? AND status IN (1, 2, 3)
  212. )";
  213. static const std::string isPlayerInAnyGameRoomText = R"(
  214. SELECT COUNT(accountID)
  215. FROM gameRoomPlayers grp
  216. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  217. WHERE accountID = ? AND status IN (1, 2, 3)
  218. )";
  219. static const std::string isAccountIDExistsText = R"(
  220. SELECT COUNT(accountID)
  221. FROM accounts
  222. WHERE accountID = ?
  223. )";
  224. static const std::string isAccountNameExistsText = R"(
  225. SELECT COUNT(displayName)
  226. FROM accounts
  227. WHERE displayName = ?
  228. )";
  229. insertChatMessageStatement = database->prepare(insertChatMessageText);
  230. insertAccountStatement = database->prepare(insertAccountText);
  231. insertAccessCookieStatement = database->prepare(insertAccessCookieText);
  232. insertGameRoomStatement = database->prepare(insertGameRoomText);
  233. insertGameRoomPlayersStatement = database->prepare(insertGameRoomPlayersText);
  234. insertGameRoomInvitesStatement = database->prepare(insertGameRoomInvitesText);
  235. deleteGameRoomPlayersStatement = database->prepare(deleteGameRoomPlayersText);
  236. deleteGameRoomInvitesStatement = database->prepare(deleteGameRoomInvitesText);
  237. setAccountOnlineStatement = database->prepare(setAccountOnlineText);
  238. setGameRoomStatusStatement = database->prepare(setGameRoomStatusText);
  239. updateAccountLoginTimeStatement = database->prepare(updateAccountLoginTimeText);
  240. updateRoomDescriptionStatement = database->prepare(updateRoomDescriptionText);
  241. updateRoomPlayerLimitStatement = database->prepare(updateRoomPlayerLimitText);
  242. getRecentMessageHistoryStatement = database->prepare(getRecentMessageHistoryText);
  243. getIdleGameRoomStatement = database->prepare(getIdleGameRoomText);
  244. getGameRoomStatusStatement = database->prepare(getGameRoomStatusText);
  245. getAccountGameRoomStatement = database->prepare(getAccountGameRoomText);
  246. getActiveAccountsStatement = database->prepare(getActiveAccountsText);
  247. getActiveGameRoomsStatement = database->prepare(getActiveGameRoomsText);
  248. getAccountDisplayNameStatement = database->prepare(getAccountDisplayNameText);
  249. countRoomUsedSlotsStatement = database->prepare(countRoomUsedSlotsText);
  250. countRoomTotalSlotsStatement = database->prepare(countRoomTotalSlotsText);
  251. isAccountCookieValidStatement = database->prepare(isAccountCookieValidText);
  252. isPlayerInGameRoomStatement = database->prepare(isPlayerInGameRoomText);
  253. isPlayerInAnyGameRoomStatement = database->prepare(isPlayerInAnyGameRoomText);
  254. isAccountIDExistsStatement = database->prepare(isAccountIDExistsText);
  255. isAccountNameExistsStatement = database->prepare(isAccountNameExistsText);
  256. }
  257. LobbyDatabase::~LobbyDatabase() = default;
  258. LobbyDatabase::LobbyDatabase(const boost::filesystem::path & databasePath)
  259. {
  260. database = SQLiteInstance::open(databasePath, true);
  261. createTables();
  262. clearOldData();
  263. prepareStatements();
  264. }
  265. void LobbyDatabase::insertChatMessage(const std::string & sender, const std::string & roomType, const std::string & roomName, const std::string & messageText)
  266. {
  267. insertChatMessageStatement->executeOnce(sender, messageText);
  268. }
  269. bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountID)
  270. {
  271. bool result = false;
  272. isPlayerInAnyGameRoomStatement->setBinds(accountID);
  273. if(isPlayerInAnyGameRoomStatement->execute())
  274. isPlayerInAnyGameRoomStatement->getColumns(result);
  275. isPlayerInAnyGameRoomStatement->reset();
  276. return result;
  277. }
  278. bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountID, const std::string & roomID)
  279. {
  280. bool result = false;
  281. isPlayerInGameRoomStatement->setBinds(accountID, roomID);
  282. if(isPlayerInGameRoomStatement->execute())
  283. isPlayerInGameRoomStatement->getColumns(result);
  284. isPlayerInGameRoomStatement->reset();
  285. return result;
  286. }
  287. std::vector<LobbyChatMessage> LobbyDatabase::getRecentMessageHistory()
  288. {
  289. std::vector<LobbyChatMessage> result;
  290. while(getRecentMessageHistoryStatement->execute())
  291. {
  292. LobbyChatMessage message;
  293. getRecentMessageHistoryStatement->getColumns(message.accountID, message.displayName, message.messageText, message.age);
  294. result.push_back(message);
  295. }
  296. getRecentMessageHistoryStatement->reset();
  297. return result;
  298. }
  299. void LobbyDatabase::setAccountOnline(const std::string & accountID, bool isOnline)
  300. {
  301. setAccountOnlineStatement->executeOnce(isOnline ? 1 : 0, accountID);
  302. }
  303. void LobbyDatabase::setGameRoomStatus(const std::string & roomID, LobbyRoomState roomStatus)
  304. {
  305. setGameRoomStatusStatement->executeOnce(vstd::to_underlying(roomStatus), roomID);
  306. }
  307. void LobbyDatabase::insertPlayerIntoGameRoom(const std::string & accountID, const std::string & roomID)
  308. {
  309. insertGameRoomPlayersStatement->executeOnce(roomID, accountID);
  310. }
  311. void LobbyDatabase::deletePlayerFromGameRoom(const std::string & accountID, const std::string & roomID)
  312. {
  313. deleteGameRoomPlayersStatement->executeOnce(roomID, accountID);
  314. }
  315. void LobbyDatabase::deleteGameRoomInvite(const std::string & targetAccountID, const std::string & roomID)
  316. {
  317. deleteGameRoomInvitesStatement->executeOnce(roomID, targetAccountID);
  318. }
  319. void LobbyDatabase::insertGameRoomInvite(const std::string & targetAccountID, const std::string & roomID)
  320. {
  321. insertGameRoomInvitesStatement->executeOnce(roomID, targetAccountID);
  322. }
  323. void LobbyDatabase::insertGameRoom(const std::string & roomID, const std::string & hostAccountID)
  324. {
  325. insertGameRoomStatement->executeOnce(roomID, hostAccountID);
  326. }
  327. void LobbyDatabase::insertAccount(const std::string & accountID, const std::string & displayName)
  328. {
  329. insertAccountStatement->executeOnce(accountID, displayName);
  330. }
  331. void LobbyDatabase::insertAccessCookie(const std::string & accountID, const std::string & accessCookieUUID)
  332. {
  333. insertAccessCookieStatement->executeOnce(accountID, accessCookieUUID);
  334. }
  335. void LobbyDatabase::updateAccountLoginTime(const std::string & accountID)
  336. {
  337. updateAccountLoginTimeStatement->executeOnce(accountID);
  338. }
  339. void LobbyDatabase::updateRoomPlayerLimit(const std::string & gameRoomID, int playerLimit)
  340. {
  341. updateRoomPlayerLimitStatement->executeOnce(playerLimit, gameRoomID);
  342. }
  343. void LobbyDatabase::updateRoomDescription(const std::string & gameRoomID, const std::string & description)
  344. {
  345. updateRoomDescriptionStatement->executeOnce(description, gameRoomID);
  346. }
  347. std::string LobbyDatabase::getAccountDisplayName(const std::string & accountID)
  348. {
  349. std::string result;
  350. getAccountDisplayNameStatement->setBinds(accountID);
  351. if(getAccountDisplayNameStatement->execute())
  352. getAccountDisplayNameStatement->getColumns(result);
  353. getAccountDisplayNameStatement->reset();
  354. return result;
  355. }
  356. LobbyCookieStatus LobbyDatabase::getAccountCookieStatus(const std::string & accountID, const std::string & accessCookieUUID)
  357. {
  358. bool result = false;
  359. isAccountCookieValidStatement->setBinds(accountID, accessCookieUUID);
  360. if(isAccountCookieValidStatement->execute())
  361. isAccountCookieValidStatement->getColumns(result);
  362. isAccountCookieValidStatement->reset();
  363. return result ? LobbyCookieStatus::VALID : LobbyCookieStatus::INVALID;
  364. }
  365. LobbyInviteStatus LobbyDatabase::getAccountInviteStatus(const std::string & accountID, const std::string & roomID)
  366. {
  367. assert(0);
  368. return {};
  369. }
  370. LobbyRoomState LobbyDatabase::getGameRoomStatus(const std::string & roomID)
  371. {
  372. LobbyRoomState result;
  373. getGameRoomStatusStatement->setBinds(roomID);
  374. if(getGameRoomStatusStatement->execute())
  375. getGameRoomStatusStatement->getColumns(result);
  376. else
  377. result = LobbyRoomState::CLOSED;
  378. getGameRoomStatusStatement->reset();
  379. return result;
  380. }
  381. uint32_t LobbyDatabase::getGameRoomFreeSlots(const std::string & roomID)
  382. {
  383. uint32_t usedSlots = 0;
  384. uint32_t totalSlots = 0;
  385. countRoomUsedSlotsStatement->setBinds(roomID);
  386. if(countRoomUsedSlotsStatement->execute())
  387. countRoomUsedSlotsStatement->getColumns(usedSlots);
  388. countRoomUsedSlotsStatement->reset();
  389. countRoomTotalSlotsStatement->setBinds(roomID);
  390. if(countRoomTotalSlotsStatement->execute())
  391. countRoomTotalSlotsStatement->getColumns(totalSlots);
  392. countRoomTotalSlotsStatement->reset();
  393. if (totalSlots > usedSlots)
  394. return totalSlots - usedSlots;
  395. return 0;
  396. }
  397. bool LobbyDatabase::isAccountNameExists(const std::string & displayName)
  398. {
  399. bool result = false;
  400. isAccountNameExistsStatement->setBinds(displayName);
  401. if(isAccountNameExistsStatement->execute())
  402. isAccountNameExistsStatement->getColumns(result);
  403. isAccountNameExistsStatement->reset();
  404. return result;
  405. }
  406. bool LobbyDatabase::isAccountIDExists(const std::string & accountID)
  407. {
  408. bool result = false;
  409. isAccountIDExistsStatement->setBinds(accountID);
  410. if(isAccountIDExistsStatement->execute())
  411. isAccountIDExistsStatement->getColumns(result);
  412. isAccountIDExistsStatement->reset();
  413. return result;
  414. }
  415. std::vector<LobbyGameRoom> LobbyDatabase::getActiveGameRooms()
  416. {
  417. std::vector<LobbyGameRoom> result;
  418. while(getActiveGameRoomsStatement->execute())
  419. {
  420. LobbyGameRoom entry;
  421. getActiveGameRoomsStatement->getColumns(entry.roomID, entry.hostAccountID, entry.hostAccountDisplayName, entry.description, entry.roomState, entry.playerLimit);
  422. result.push_back(entry);
  423. }
  424. getActiveGameRoomsStatement->reset();
  425. for (auto & room : result)
  426. {
  427. countRoomUsedSlotsStatement->setBinds(room.roomID);
  428. if(countRoomUsedSlotsStatement->execute())
  429. countRoomUsedSlotsStatement->getColumns(room.playersCount);
  430. countRoomUsedSlotsStatement->reset();
  431. }
  432. return result;
  433. }
  434. std::vector<LobbyAccount> LobbyDatabase::getActiveAccounts()
  435. {
  436. std::vector<LobbyAccount> result;
  437. while(getActiveAccountsStatement->execute())
  438. {
  439. LobbyAccount entry;
  440. getActiveAccountsStatement->getColumns(entry.accountID, entry.displayName);
  441. result.push_back(entry);
  442. }
  443. getActiveAccountsStatement->reset();
  444. return result;
  445. }
  446. std::string LobbyDatabase::getIdleGameRoom(const std::string & hostAccountID)
  447. {
  448. std::string result;
  449. getIdleGameRoomStatement->setBinds(hostAccountID);
  450. if(getIdleGameRoomStatement->execute())
  451. getIdleGameRoomStatement->getColumns(result);
  452. getIdleGameRoomStatement->reset();
  453. return result;
  454. }
  455. std::string LobbyDatabase::getAccountGameRoom(const std::string & accountID)
  456. {
  457. std::string result;
  458. getAccountGameRoomStatement->setBinds(accountID);
  459. if(getAccountGameRoomStatement->execute())
  460. getAccountGameRoomStatement->getColumns(result);
  461. getAccountGameRoomStatement->reset();
  462. return result;
  463. }