LobbyDatabase.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 countAccountsInRoomText = R"(
  151. SELECT COUNT(accountID)
  152. FROM gameRoomPlayers
  153. WHERE roomID = ?
  154. )";
  155. static const std::string getAccountDisplayNameText = R"(
  156. SELECT displayName
  157. FROM accounts
  158. WHERE accountID = ?
  159. )";
  160. static const std::string isAccountCookieValidText = R"(
  161. SELECT COUNT(accountID)
  162. FROM accountCookies
  163. WHERE accountID = ? AND cookieUUID = ? AND strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',creationTime) < ?
  164. )";
  165. static const std::string isGameRoomCookieValidText = R"(
  166. SELECT COUNT(roomID)
  167. FROM gameRooms
  168. LEFT JOIN accountCookies ON accountCookies.accountID = gameRooms.hostAccountID
  169. WHERE roomID = ? AND cookieUUID = ? AND strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',creationTime) < ?
  170. )";
  171. static const std::string isPlayerInGameRoomText = R"(
  172. SELECT COUNT(accountID)
  173. FROM gameRoomPlayers
  174. WHERE accountID = ? AND roomID = ?
  175. )";
  176. static const std::string isPlayerInAnyGameRoomText = R"(
  177. SELECT COUNT(accountID)
  178. FROM gameRoomPlayers
  179. WHERE accountID = ?
  180. )";
  181. static const std::string isAccountIDExistsText = R"(
  182. SELECT COUNT(accountID)
  183. FROM accounts
  184. WHERE accountID = ?
  185. )";
  186. static const std::string isAccountNameExistsText = R"(
  187. SELECT COUNT(displayName)
  188. FROM accounts
  189. WHERE displayName = ?
  190. )";
  191. insertChatMessageStatement = database->prepare(insertChatMessageText);
  192. insertAccountStatement = database->prepare(insertAccountText);
  193. insertAccessCookieStatement = database->prepare(insertAccessCookieText);
  194. insertGameRoomStatement = database->prepare(insertGameRoomText);
  195. insertGameRoomPlayersStatement = database->prepare(insertGameRoomPlayersText);
  196. insertGameRoomInvitesStatement = database->prepare(insertGameRoomInvitesText);
  197. deleteGameRoomPlayersStatement = database->prepare(deleteGameRoomPlayersText);
  198. deleteGameRoomInvitesStatement = database->prepare(deleteGameRoomInvitesText);
  199. setAccountOnlineStatement = database->prepare(setAccountOnlineText);
  200. setGameRoomStatusStatement = database->prepare(setGameRoomStatusText);
  201. setGameRoomPlayerLimitStatement = database->prepare(setGameRoomPlayerLimitText);
  202. getRecentMessageHistoryStatement = database->prepare(getRecentMessageHistoryText);
  203. getIdleGameRoomStatement = database->prepare(getIdleGameRoomText);
  204. getAccountGameRoomStatement = database->prepare(getAccountGameRoomText);
  205. getActiveAccountsStatement = database->prepare(getActiveAccountsText);
  206. getActiveGameRoomsStatement = database->prepare(getActiveGameRoomsText);
  207. getAccountDisplayNameStatement = database->prepare(getAccountDisplayNameText);
  208. countAccountsInRoomStatement = database->prepare(countAccountsInRoomText);
  209. isAccountCookieValidStatement = database->prepare(isAccountCookieValidText);
  210. isPlayerInGameRoomStatement = database->prepare(isPlayerInGameRoomText);
  211. isPlayerInAnyGameRoomStatement = database->prepare(isPlayerInAnyGameRoomText);
  212. isAccountIDExistsStatement = database->prepare(isAccountIDExistsText);
  213. isAccountNameExistsStatement = database->prepare(isAccountNameExistsText);
  214. }
  215. LobbyDatabase::~LobbyDatabase() = default;
  216. LobbyDatabase::LobbyDatabase(const boost::filesystem::path & databasePath)
  217. {
  218. database = SQLiteInstance::open(databasePath, true);
  219. createTables();
  220. prepareStatements();
  221. }
  222. void LobbyDatabase::insertChatMessage(const std::string & sender, const std::string & roomType, const std::string & roomName, const std::string & messageText)
  223. {
  224. insertChatMessageStatement->executeOnce(sender, messageText);
  225. }
  226. bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountID)
  227. {
  228. bool result = false;
  229. isPlayerInAnyGameRoomStatement->setBinds(accountID);
  230. if(isPlayerInAnyGameRoomStatement->execute())
  231. isPlayerInAnyGameRoomStatement->getColumns(result);
  232. isPlayerInAnyGameRoomStatement->reset();
  233. return result;
  234. }
  235. bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountID, const std::string & roomID)
  236. {
  237. bool result = false;
  238. isPlayerInGameRoomStatement->setBinds(accountID, roomID);
  239. if(isPlayerInGameRoomStatement->execute())
  240. isPlayerInGameRoomStatement->getColumns(result);
  241. isPlayerInGameRoomStatement->reset();
  242. return result;
  243. }
  244. std::vector<LobbyChatMessage> LobbyDatabase::getRecentMessageHistory()
  245. {
  246. std::vector<LobbyChatMessage> result;
  247. while(getRecentMessageHistoryStatement->execute())
  248. {
  249. LobbyChatMessage message;
  250. getRecentMessageHistoryStatement->getColumns(message.accountID, message.displayName, message.messageText, message.age);
  251. result.push_back(message);
  252. }
  253. getRecentMessageHistoryStatement->reset();
  254. return result;
  255. }
  256. void LobbyDatabase::setAccountOnline(const std::string & accountID, bool isOnline)
  257. {
  258. setAccountOnlineStatement->executeOnce(isOnline ? 1 : 0, accountID);
  259. }
  260. void LobbyDatabase::setGameRoomStatus(const std::string & roomID, LobbyRoomState roomStatus)
  261. {
  262. setGameRoomStatusStatement->executeOnce(vstd::to_underlying(roomStatus), roomID);
  263. }
  264. void LobbyDatabase::setGameRoomPlayerLimit(const std::string & roomID, uint32_t playerLimit)
  265. {
  266. setGameRoomPlayerLimitStatement->executeOnce(playerLimit, roomID);
  267. }
  268. void LobbyDatabase::insertPlayerIntoGameRoom(const std::string & accountID, const std::string & roomID)
  269. {
  270. insertGameRoomPlayersStatement->executeOnce(roomID, accountID);
  271. }
  272. void LobbyDatabase::deletePlayerFromGameRoom(const std::string & accountID, const std::string & roomID)
  273. {
  274. deleteGameRoomPlayersStatement->executeOnce(roomID, accountID);
  275. }
  276. void LobbyDatabase::deleteGameRoomInvite(const std::string & targetAccountID, const std::string & roomID)
  277. {
  278. deleteGameRoomInvitesStatement->executeOnce(roomID, targetAccountID);
  279. }
  280. void LobbyDatabase::insertGameRoomInvite(const std::string & targetAccountID, const std::string & roomID)
  281. {
  282. insertGameRoomInvitesStatement->executeOnce(roomID, targetAccountID);
  283. }
  284. void LobbyDatabase::insertGameRoom(const std::string & roomID, const std::string & hostAccountID)
  285. {
  286. insertGameRoomStatement->executeOnce(roomID, hostAccountID);
  287. }
  288. void LobbyDatabase::insertAccount(const std::string & accountID, const std::string & displayName)
  289. {
  290. insertAccountStatement->executeOnce(accountID, displayName);
  291. }
  292. void LobbyDatabase::insertAccessCookie(const std::string & accountID, const std::string & accessCookieUUID)
  293. {
  294. insertAccessCookieStatement->executeOnce(accountID, accessCookieUUID);
  295. }
  296. void LobbyDatabase::updateAccessCookie(const std::string & accountID, const std::string & accessCookieUUID) {}
  297. void LobbyDatabase::updateAccountLoginTime(const std::string & accountID) {}
  298. void LobbyDatabase::updateActiveAccount(const std::string & accountID, bool isActive) {}
  299. std::string LobbyDatabase::getAccountDisplayName(const std::string & accountID)
  300. {
  301. std::string result;
  302. getAccountDisplayNameStatement->setBinds(accountID);
  303. if(getAccountDisplayNameStatement->execute())
  304. getAccountDisplayNameStatement->getColumns(result);
  305. getAccountDisplayNameStatement->reset();
  306. return result;
  307. }
  308. LobbyCookieStatus LobbyDatabase::getGameRoomCookieStatus(const std::string & accountID, const std::string & accessCookieUUID, std::chrono::seconds cookieLifetime)
  309. {
  310. return {};
  311. }
  312. LobbyCookieStatus LobbyDatabase::getAccountCookieStatus(const std::string & accountID, const std::string & accessCookieUUID, std::chrono::seconds cookieLifetime)
  313. {
  314. bool result = false;
  315. isAccountCookieValidStatement->setBinds(accountID, accessCookieUUID, cookieLifetime.count());
  316. if(isAccountCookieValidStatement->execute())
  317. isAccountCookieValidStatement->getColumns(result);
  318. isAccountCookieValidStatement->reset();
  319. return result ? LobbyCookieStatus::VALID : LobbyCookieStatus::INVALID;
  320. }
  321. LobbyInviteStatus LobbyDatabase::getAccountInviteStatus(const std::string & accountID, const std::string & roomID)
  322. {
  323. return {};
  324. }
  325. LobbyRoomState LobbyDatabase::getGameRoomStatus(const std::string & roomID)
  326. {
  327. return {};
  328. }
  329. uint32_t LobbyDatabase::getGameRoomFreeSlots(const std::string & roomID)
  330. {
  331. return 0;
  332. }
  333. bool LobbyDatabase::isAccountNameExists(const std::string & displayName)
  334. {
  335. bool result = false;
  336. isAccountNameExistsStatement->setBinds(displayName);
  337. if(isAccountNameExistsStatement->execute())
  338. isAccountNameExistsStatement->getColumns(result);
  339. isAccountNameExistsStatement->reset();
  340. return result;
  341. }
  342. bool LobbyDatabase::isAccountIDExists(const std::string & accountID)
  343. {
  344. bool result = false;
  345. isAccountIDExistsStatement->setBinds(accountID);
  346. if(isAccountIDExistsStatement->execute())
  347. isAccountIDExistsStatement->getColumns(result);
  348. isAccountIDExistsStatement->reset();
  349. return result;
  350. }
  351. std::vector<LobbyGameRoom> LobbyDatabase::getActiveGameRooms()
  352. {
  353. std::vector<LobbyGameRoom> result;
  354. while(getActiveGameRoomsStatement->execute())
  355. {
  356. LobbyGameRoom entry;
  357. getActiveGameRoomsStatement->getColumns(entry.roomID, entry.hostAccountID, entry.hostAccountDisplayName, entry.roomStatus, entry.playersLimit);
  358. result.push_back(entry);
  359. }
  360. getActiveGameRoomsStatement->reset();
  361. for (auto & room : result)
  362. {
  363. countAccountsInRoomStatement->setBinds(room.roomID);
  364. if(countAccountsInRoomStatement->execute())
  365. countAccountsInRoomStatement->getColumns(room.playersCount);
  366. countAccountsInRoomStatement->reset();
  367. }
  368. return result;
  369. }
  370. std::vector<LobbyAccount> LobbyDatabase::getActiveAccounts()
  371. {
  372. std::vector<LobbyAccount> result;
  373. while(getActiveAccountsStatement->execute())
  374. {
  375. LobbyAccount entry;
  376. getActiveAccountsStatement->getColumns(entry.accountID, entry.displayName);
  377. result.push_back(entry);
  378. }
  379. getActiveAccountsStatement->reset();
  380. return result;
  381. }
  382. std::string LobbyDatabase::getIdleGameRoom(const std::string & hostAccountID)
  383. {
  384. std::string result;
  385. getIdleGameRoomStatement->setBinds(hostAccountID);
  386. if(getIdleGameRoomStatement->execute())
  387. getIdleGameRoomStatement->getColumns(result);
  388. getIdleGameRoomStatement->reset();
  389. return result;
  390. }
  391. std::string LobbyDatabase::getAccountGameRoom(const std::string & accountID)
  392. {
  393. std::string result;
  394. getAccountGameRoomStatement->setBinds(accountID);
  395. if(getAccountGameRoomStatement->execute())
  396. getAccountGameRoomStatement->getColumns(result);
  397. getAccountGameRoomStatement->reset();
  398. return result;
  399. }