LobbyDatabase.cpp 16 KB

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