LobbyDatabase.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. // SELECT FROM
  139. static const std::string getRecentMessageHistoryText = R"(
  140. SELECT senderName, displayName, messageText, strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',cm.creationTime) AS secondsElapsed
  141. FROM chatMessages cm
  142. LEFT JOIN accounts on accountID = senderName
  143. WHERE secondsElapsed < 60*60*18
  144. ORDER BY cm.creationTime DESC
  145. LIMIT 100
  146. )";
  147. static const std::string getIdleGameRoomText = R"(
  148. SELECT roomID
  149. FROM gameRooms
  150. WHERE hostAccountID = ? AND status = 0
  151. LIMIT 1
  152. )";
  153. static const std::string getGameRoomStatusText = R"(
  154. SELECT status
  155. FROM gameRooms
  156. WHERE roomID = ?
  157. )";
  158. static const std::string getAccountGameRoomText = R"(
  159. SELECT grp.roomID
  160. FROM gameRoomPlayers grp
  161. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  162. WHERE accountID = ? AND status IN (1, 2)
  163. LIMIT 1
  164. )";
  165. static const std::string getActiveAccountsText = R"(
  166. SELECT accountID, displayName
  167. FROM accounts
  168. WHERE online = 1
  169. )";
  170. static const std::string getActiveGameRoomsText = R"(
  171. SELECT roomID, hostAccountID, displayName, description, status, playerLimit
  172. FROM gameRooms
  173. LEFT JOIN accounts ON hostAccountID = accountID
  174. WHERE status = 1
  175. )";
  176. static const std::string countRoomUsedSlotsText = R"(
  177. SELECT COUNT(accountID)
  178. FROM gameRoomPlayers
  179. WHERE roomID = ?
  180. )";
  181. static const std::string countRoomTotalSlotsText = R"(
  182. SELECT playerLimit
  183. FROM gameRooms
  184. WHERE roomID = ?
  185. )";
  186. static const std::string getAccountDisplayNameText = R"(
  187. SELECT displayName
  188. FROM accounts
  189. WHERE accountID = ?
  190. )";
  191. static const std::string isAccountCookieValidText = R"(
  192. SELECT COUNT(accountID)
  193. FROM accountCookies
  194. WHERE accountID = ? AND cookieUUID = ?
  195. )";
  196. static const std::string isGameRoomCookieValidText = R"(
  197. SELECT COUNT(roomID)
  198. FROM gameRooms
  199. LEFT JOIN accountCookies ON accountCookies.accountID = gameRooms.hostAccountID
  200. WHERE roomID = ? AND cookieUUID = ? AND strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',creationTime) < ?
  201. )";
  202. static const std::string isPlayerInGameRoomText = R"(
  203. SELECT COUNT(accountID)
  204. FROM gameRoomPlayers grp
  205. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  206. WHERE accountID = ? AND grp.roomID = ? AND status IN (1, 2)
  207. )";
  208. static const std::string isPlayerInAnyGameRoomText = R"(
  209. SELECT COUNT(accountID)
  210. FROM gameRoomPlayers grp
  211. LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
  212. WHERE accountID = ? AND status IN (1, 2)
  213. )";
  214. static const std::string isAccountIDExistsText = R"(
  215. SELECT COUNT(accountID)
  216. FROM accounts
  217. WHERE accountID = ?
  218. )";
  219. static const std::string isAccountNameExistsText = R"(
  220. SELECT COUNT(displayName)
  221. FROM accounts
  222. WHERE displayName = ?
  223. )";
  224. insertChatMessageStatement = database->prepare(insertChatMessageText);
  225. insertAccountStatement = database->prepare(insertAccountText);
  226. insertAccessCookieStatement = database->prepare(insertAccessCookieText);
  227. insertGameRoomStatement = database->prepare(insertGameRoomText);
  228. insertGameRoomPlayersStatement = database->prepare(insertGameRoomPlayersText);
  229. insertGameRoomInvitesStatement = database->prepare(insertGameRoomInvitesText);
  230. deleteGameRoomPlayersStatement = database->prepare(deleteGameRoomPlayersText);
  231. deleteGameRoomInvitesStatement = database->prepare(deleteGameRoomInvitesText);
  232. setAccountOnlineStatement = database->prepare(setAccountOnlineText);
  233. setGameRoomStatusStatement = database->prepare(setGameRoomStatusText);
  234. updateAccountLoginTimeStatement = database->prepare(updateAccountLoginTimeText);
  235. updateRoomDescriptionStatement = database->prepare(updateRoomDescriptionText);
  236. getRecentMessageHistoryStatement = database->prepare(getRecentMessageHistoryText);
  237. getIdleGameRoomStatement = database->prepare(getIdleGameRoomText);
  238. getGameRoomStatusStatement = database->prepare(getGameRoomStatusText);
  239. getAccountGameRoomStatement = database->prepare(getAccountGameRoomText);
  240. getActiveAccountsStatement = database->prepare(getActiveAccountsText);
  241. getActiveGameRoomsStatement = database->prepare(getActiveGameRoomsText);
  242. getAccountDisplayNameStatement = database->prepare(getAccountDisplayNameText);
  243. countRoomUsedSlotsStatement = database->prepare(countRoomUsedSlotsText);
  244. countRoomTotalSlotsStatement = database->prepare(countRoomTotalSlotsText);
  245. isAccountCookieValidStatement = database->prepare(isAccountCookieValidText);
  246. isPlayerInGameRoomStatement = database->prepare(isPlayerInGameRoomText);
  247. isPlayerInAnyGameRoomStatement = database->prepare(isPlayerInAnyGameRoomText);
  248. isAccountIDExistsStatement = database->prepare(isAccountIDExistsText);
  249. isAccountNameExistsStatement = database->prepare(isAccountNameExistsText);
  250. }
  251. LobbyDatabase::~LobbyDatabase() = default;
  252. LobbyDatabase::LobbyDatabase(const boost::filesystem::path & databasePath)
  253. {
  254. database = SQLiteInstance::open(databasePath, true);
  255. createTables();
  256. clearOldData();
  257. prepareStatements();
  258. }
  259. void LobbyDatabase::insertChatMessage(const std::string & sender, const std::string & roomType, const std::string & roomName, const std::string & messageText)
  260. {
  261. insertChatMessageStatement->executeOnce(sender, messageText);
  262. }
  263. bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountID)
  264. {
  265. bool result = false;
  266. isPlayerInAnyGameRoomStatement->setBinds(accountID);
  267. if(isPlayerInAnyGameRoomStatement->execute())
  268. isPlayerInAnyGameRoomStatement->getColumns(result);
  269. isPlayerInAnyGameRoomStatement->reset();
  270. return result;
  271. }
  272. bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountID, const std::string & roomID)
  273. {
  274. bool result = false;
  275. isPlayerInGameRoomStatement->setBinds(accountID, roomID);
  276. if(isPlayerInGameRoomStatement->execute())
  277. isPlayerInGameRoomStatement->getColumns(result);
  278. isPlayerInGameRoomStatement->reset();
  279. return result;
  280. }
  281. std::vector<LobbyChatMessage> LobbyDatabase::getRecentMessageHistory()
  282. {
  283. std::vector<LobbyChatMessage> result;
  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. void LobbyDatabase::setAccountOnline(const std::string & accountID, bool isOnline)
  294. {
  295. setAccountOnlineStatement->executeOnce(isOnline ? 1 : 0, accountID);
  296. }
  297. void LobbyDatabase::setGameRoomStatus(const std::string & roomID, LobbyRoomState roomStatus)
  298. {
  299. setGameRoomStatusStatement->executeOnce(vstd::to_underlying(roomStatus), roomID);
  300. }
  301. void LobbyDatabase::insertPlayerIntoGameRoom(const std::string & accountID, const std::string & roomID)
  302. {
  303. insertGameRoomPlayersStatement->executeOnce(roomID, accountID);
  304. }
  305. void LobbyDatabase::deletePlayerFromGameRoom(const std::string & accountID, const std::string & roomID)
  306. {
  307. deleteGameRoomPlayersStatement->executeOnce(roomID, accountID);
  308. }
  309. void LobbyDatabase::deleteGameRoomInvite(const std::string & targetAccountID, const std::string & roomID)
  310. {
  311. deleteGameRoomInvitesStatement->executeOnce(roomID, targetAccountID);
  312. }
  313. void LobbyDatabase::insertGameRoomInvite(const std::string & targetAccountID, const std::string & roomID)
  314. {
  315. insertGameRoomInvitesStatement->executeOnce(roomID, targetAccountID);
  316. }
  317. void LobbyDatabase::insertGameRoom(const std::string & roomID, const std::string & hostAccountID)
  318. {
  319. insertGameRoomStatement->executeOnce(roomID, hostAccountID);
  320. }
  321. void LobbyDatabase::insertAccount(const std::string & accountID, const std::string & displayName)
  322. {
  323. insertAccountStatement->executeOnce(accountID, displayName);
  324. }
  325. void LobbyDatabase::insertAccessCookie(const std::string & accountID, const std::string & accessCookieUUID)
  326. {
  327. insertAccessCookieStatement->executeOnce(accountID, accessCookieUUID);
  328. }
  329. void LobbyDatabase::updateAccountLoginTime(const std::string & accountID)
  330. {
  331. updateAccountLoginTimeStatement->executeOnce(accountID);
  332. }
  333. void LobbyDatabase::updateRoomDescription(const std::string & gameRoomID, const std::string & description)
  334. {
  335. updateRoomDescriptionStatement->executeOnce(description, gameRoomID);
  336. }
  337. std::string LobbyDatabase::getAccountDisplayName(const std::string & accountID)
  338. {
  339. std::string result;
  340. getAccountDisplayNameStatement->setBinds(accountID);
  341. if(getAccountDisplayNameStatement->execute())
  342. getAccountDisplayNameStatement->getColumns(result);
  343. getAccountDisplayNameStatement->reset();
  344. return result;
  345. }
  346. LobbyCookieStatus LobbyDatabase::getAccountCookieStatus(const std::string & accountID, const std::string & accessCookieUUID)
  347. {
  348. bool result = false;
  349. isAccountCookieValidStatement->setBinds(accountID, accessCookieUUID);
  350. if(isAccountCookieValidStatement->execute())
  351. isAccountCookieValidStatement->getColumns(result);
  352. isAccountCookieValidStatement->reset();
  353. return result ? LobbyCookieStatus::VALID : LobbyCookieStatus::INVALID;
  354. }
  355. LobbyInviteStatus LobbyDatabase::getAccountInviteStatus(const std::string & accountID, const std::string & roomID)
  356. {
  357. assert(0);
  358. return {};
  359. }
  360. LobbyRoomState LobbyDatabase::getGameRoomStatus(const std::string & roomID)
  361. {
  362. int result = -1;
  363. getGameRoomStatusStatement->setBinds(roomID);
  364. if(getGameRoomStatusStatement->execute())
  365. getGameRoomStatusStatement->getColumns(result);
  366. getGameRoomStatusStatement->reset();
  367. if (result != -1)
  368. return static_cast<LobbyRoomState>(result);
  369. return LobbyRoomState::CLOSED;
  370. }
  371. uint32_t LobbyDatabase::getGameRoomFreeSlots(const std::string & roomID)
  372. {
  373. uint32_t usedSlots = 0;
  374. uint32_t totalSlots = 0;
  375. countRoomUsedSlotsStatement->setBinds(roomID);
  376. if(countRoomUsedSlotsStatement->execute())
  377. countRoomUsedSlotsStatement->getColumns(usedSlots);
  378. countRoomUsedSlotsStatement->reset();
  379. countRoomTotalSlotsStatement->setBinds(roomID);
  380. if(countRoomTotalSlotsStatement->execute())
  381. countRoomTotalSlotsStatement->getColumns(totalSlots);
  382. countRoomTotalSlotsStatement->reset();
  383. if (totalSlots > usedSlots)
  384. return totalSlots - usedSlots;
  385. return 0;
  386. }
  387. bool LobbyDatabase::isAccountNameExists(const std::string & displayName)
  388. {
  389. bool result = false;
  390. isAccountNameExistsStatement->setBinds(displayName);
  391. if(isAccountNameExistsStatement->execute())
  392. isAccountNameExistsStatement->getColumns(result);
  393. isAccountNameExistsStatement->reset();
  394. return result;
  395. }
  396. bool LobbyDatabase::isAccountIDExists(const std::string & accountID)
  397. {
  398. bool result = false;
  399. isAccountIDExistsStatement->setBinds(accountID);
  400. if(isAccountIDExistsStatement->execute())
  401. isAccountIDExistsStatement->getColumns(result);
  402. isAccountIDExistsStatement->reset();
  403. return result;
  404. }
  405. std::vector<LobbyGameRoom> LobbyDatabase::getActiveGameRooms()
  406. {
  407. std::vector<LobbyGameRoom> result;
  408. while(getActiveGameRoomsStatement->execute())
  409. {
  410. LobbyGameRoom entry;
  411. getActiveGameRoomsStatement->getColumns(entry.roomID, entry.hostAccountID, entry.hostAccountDisplayName, entry.description, entry.roomStatus, entry.playersLimit);
  412. result.push_back(entry);
  413. }
  414. getActiveGameRoomsStatement->reset();
  415. for (auto & room : result)
  416. {
  417. countRoomUsedSlotsStatement->setBinds(room.roomID);
  418. if(countRoomUsedSlotsStatement->execute())
  419. countRoomUsedSlotsStatement->getColumns(room.playersCount);
  420. countRoomUsedSlotsStatement->reset();
  421. }
  422. return result;
  423. }
  424. std::vector<LobbyAccount> LobbyDatabase::getActiveAccounts()
  425. {
  426. std::vector<LobbyAccount> result;
  427. while(getActiveAccountsStatement->execute())
  428. {
  429. LobbyAccount entry;
  430. getActiveAccountsStatement->getColumns(entry.accountID, entry.displayName);
  431. result.push_back(entry);
  432. }
  433. getActiveAccountsStatement->reset();
  434. return result;
  435. }
  436. std::string LobbyDatabase::getIdleGameRoom(const std::string & hostAccountID)
  437. {
  438. std::string result;
  439. getIdleGameRoomStatement->setBinds(hostAccountID);
  440. if(getIdleGameRoomStatement->execute())
  441. getIdleGameRoomStatement->getColumns(result);
  442. getIdleGameRoomStatement->reset();
  443. return result;
  444. }
  445. std::string LobbyDatabase::getAccountGameRoom(const std::string & accountID)
  446. {
  447. std::string result;
  448. getAccountGameRoomStatement->setBinds(accountID);
  449. if(getAccountGameRoomStatement->execute())
  450. getAccountGameRoomStatement->getColumns(result);
  451. getAccountGameRoomStatement->reset();
  452. return result;
  453. }