LobbyDatabase.cpp 15 KB

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