LobbyDatabase.cpp 15 KB

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