| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 | 
							- /*
 
-  * LobbyServer.cpp, part of VCMI engine
 
-  *
 
-  * Authors: listed in file AUTHORS in main folder
 
-  *
 
-  * License: GNU General Public License v2.0 or later
 
-  * Full text of license available in license.txt file, in main folder
 
-  *
 
-  */
 
- #include "StdInc.h"
 
- #include "LobbyDatabase.h"
 
- #include "SQLiteConnection.h"
 
- void LobbyDatabase::createTables()
 
- {
 
- 	static const std::string createChatMessages = R"(
 
- 		CREATE TABLE IF NOT EXISTS chatMessages (
 
- 			id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
 
- 			senderName TEXT,
 
- 			roomType TEXT,
 
- 			messageText TEXT,
 
- 			creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
 
- 		);
 
- 	)";
 
- 	static const std::string createTableGameRooms = R"(
 
- 		CREATE TABLE IF NOT EXISTS gameRooms (
 
- 			id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
 
- 			roomID TEXT,
 
- 			hostAccountID TEXT,
 
- 			status INTEGER NOT NULL DEFAULT 0,
 
- 			playerLimit INTEGER NOT NULL,
 
- 			creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
 
- 		);
 
- 	)";
 
- 	static const std::string createTableGameRoomPlayers = R"(
 
- 		CREATE TABLE IF NOT EXISTS gameRoomPlayers (
 
- 			id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
 
- 			roomID TEXT,
 
- 			accountID TEXT
 
- 		);
 
- 	)";
 
- 	static const std::string createTableAccounts = R"(
 
- 		CREATE TABLE IF NOT EXISTS accounts (
 
- 			id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
 
- 			accountID TEXT,
 
- 			displayName TEXT,
 
- 			online INTEGER NOT NULL,
 
- 			lastLoginTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
 
- 			creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
 
- 		);
 
- 	)";
 
- 	static const std::string createTableAccountCookies = R"(
 
- 		CREATE TABLE IF NOT EXISTS accountCookies (
 
- 			id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
 
- 			accountID TEXT,
 
- 			cookieUUID TEXT,
 
- 			creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
 
- 		);
 
- 	)";
 
- 	static const std::string createTableGameRoomInvites = R"(
 
- 		CREATE TABLE IF NOT EXISTS gameRoomInvites (
 
- 			id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
 
- 			roomID TEXT,
 
- 			accountID TEXT
 
- 		);
 
- 	)";
 
- 	database->prepare(createChatMessages)->execute();
 
- 	database->prepare(createTableGameRoomPlayers)->execute();
 
- 	database->prepare(createTableGameRooms)->execute();
 
- 	database->prepare(createTableAccounts)->execute();
 
- 	database->prepare(createTableAccountCookies)->execute();
 
- 	database->prepare(createTableGameRoomInvites)->execute();
 
- }
 
- void LobbyDatabase::prepareStatements()
 
- {
 
- 	// INSERT INTO
 
- 	static const std::string insertChatMessageText = R"(
 
- 		INSERT INTO chatMessages(senderName, messageText) VALUES( ?, ?);
 
- 	)";
 
- 	static const std::string insertAccountText = R"(
 
- 		INSERT INTO accounts(accountID, displayName, online) VALUES(?,?,0);
 
- 	)";
 
- 	static const std::string insertAccessCookieText = R"(
 
- 		INSERT INTO accountCookies(accountID, cookieUUID) VALUES(?,?);
 
- 	)";
 
- 	static const std::string insertGameRoomText = R"(
 
- 		INSERT INTO gameRooms(roomID, hostAccountID, status, playerLimit) VALUES(?, ?, 'empty', 8);
 
- 	)";
 
- 	static const std::string insertGameRoomPlayersText = R"(
 
- 		INSERT INTO gameRoomPlayers(roomID, accountID) VALUES(?,?);
 
- 	)";
 
- 	static const std::string insertGameRoomInvitesText = R"(
 
- 		INSERT INTO gameRoomInvites(roomID, accountID) VALUES(?,?);
 
- 	)";
 
- 	// DELETE FROM
 
- 	static const std::string deleteGameRoomPlayersText = R"(
 
- 		 DELETE FROM gameRoomPlayers WHERE roomID = ? AND accountID = ?
 
- 	)";
 
- 	static const std::string deleteGameRoomInvitesText = R"(
 
- 		DELETE FROM gameRoomInvites WHERE roomID = ? AND accountID = ?
 
- 	)";
 
- 	// UPDATE
 
- 	static const std::string setGameRoomStatusText = R"(
 
- 		UPDATE gameRooms
 
- 		SET status = ?
 
- 		WHERE roomID = ?
 
- 	)";
 
- 	static const std::string setGameRoomPlayerLimitText = R"(
 
- 		UPDATE gameRooms
 
- 		SET playerLimit = ?
 
- 		WHERE roomID = ?
 
- 	)";
 
- 	// SELECT FROM
 
- 	static const std::string getRecentMessageHistoryText = R"(
 
- 		SELECT senderName, displayName, messageText, strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',cm.creationTime)  AS secondsElapsed
 
- 		FROM chatMessages cm
 
- 		LEFT JOIN accounts on accountID = senderName
 
- 		WHERE secondsElapsed < 60*60*18
 
- 		ORDER BY cm.creationTime DESC
 
- 		LIMIT 100
 
- 	)";
 
- 	static const std::string getIdleGameRoomText = R"(
 
- 		SELECT roomID
 
- 		FROM gameRooms
 
- 		WHERE hostAccountID = ? AND status = 'idle'
 
- 		LIMIT 1
 
- 	)";
 
- 	static const std::string getAccountGameRoomText = R"(
 
- 		SELECT grp.roomID
 
- 		FROM gameRoomPlayers grp
 
- 		LEFT JOIN gameRooms gr ON gr.roomID = grp.roomID
 
- 		WHERE accountID = ? AND status IN ('public', 'private', 'busy')
 
- 		LIMIT 1
 
- 	)";
 
- 	static const std::string getActiveAccountsText = R"(
 
- 		SELECT accountID, displayName
 
- 		FROM accounts
 
- 		WHERE online = 1
 
- 	)";
 
- 	static const std::string getAccountDisplayNameText = R"(
 
- 		SELECT displayName
 
- 		FROM accounts
 
- 		WHERE accountID = ?
 
- 	)";
 
- 	static const std::string isAccountCookieValidText = R"(
 
- 		SELECT COUNT(accountID)
 
- 		FROM accountCookies
 
- 		WHERE accountID = ? AND cookieUUID = ? AND strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',creationTime) < ?
 
- 	)";
 
- 	static const std::string isGameRoomCookieValidText = R"(
 
- 		SELECT COUNT(roomID)
 
- 		FROM gameRooms
 
- 		LEFT JOIN accountCookies ON accountCookies.accountID = gameRooms.hostAccountID
 
- 		WHERE roomID = ? AND cookieUUID = ? AND strftime('%s',CURRENT_TIMESTAMP)- strftime('%s',creationTime) < ?
 
- 	)";
 
- 	static const std::string isPlayerInGameRoomText = R"(
 
- 		SELECT COUNT(accountID)
 
- 		FROM gameRoomPlayers
 
- 		WHERE accountID = ? AND roomID = ?
 
- 	)";
 
- 	static const std::string isPlayerInAnyGameRoomText = R"(
 
- 		SELECT COUNT(accountID)
 
- 		FROM gameRoomPlayers
 
- 		WHERE accountID = ?
 
- 	)";
 
- 	static const std::string isAccountIDExistsText = R"(
 
- 		SELECT COUNT(accountID)
 
- 		FROM accounts
 
- 		WHERE accountID = ?
 
- 	)";
 
- 	static const std::string isAccountNameExistsText = R"(
 
- 		SELECT COUNT(displayName)
 
- 		FROM accounts
 
- 		WHERE displayName = ?
 
- 	)";
 
- 	insertChatMessageStatement = database->prepare(insertChatMessageText);
 
- 	insertAccountStatement = database->prepare(insertAccountText);
 
- 	insertAccessCookieStatement = database->prepare(insertAccessCookieText);
 
- 	insertGameRoomStatement = database->prepare(insertGameRoomText);
 
- 	insertGameRoomPlayersStatement = database->prepare(insertGameRoomPlayersText);
 
- 	insertGameRoomInvitesStatement = database->prepare(insertGameRoomInvitesText);
 
- 	deleteGameRoomPlayersStatement = database->prepare(deleteGameRoomPlayersText);
 
- 	deleteGameRoomInvitesStatement = database->prepare(deleteGameRoomInvitesText);
 
- 	setGameRoomStatusStatement = database->prepare(setGameRoomStatusText);
 
- 	setGameRoomPlayerLimitStatement = database->prepare(setGameRoomPlayerLimitText);
 
- 	getRecentMessageHistoryStatement = database->prepare(getRecentMessageHistoryText);
 
- 	getIdleGameRoomStatement = database->prepare(getIdleGameRoomText);
 
- 	getAccountGameRoomStatement = database->prepare(getAccountGameRoomText);
 
- 	getActiveAccountsStatement = database->prepare(getActiveAccountsText);
 
- 	getAccountDisplayNameStatement = database->prepare(getAccountDisplayNameText);
 
- 	isAccountCookieValidStatement = database->prepare(isAccountCookieValidText);
 
- 	isPlayerInGameRoomStatement = database->prepare(isPlayerInGameRoomText);
 
- 	isPlayerInAnyGameRoomStatement = database->prepare(isPlayerInAnyGameRoomText);
 
- 	isAccountIDExistsStatement = database->prepare(isAccountIDExistsText);
 
- 	isAccountNameExistsStatement = database->prepare(isAccountNameExistsText);
 
- }
 
- LobbyDatabase::~LobbyDatabase() = default;
 
- LobbyDatabase::LobbyDatabase(const boost::filesystem::path & databasePath)
 
- {
 
- 	database = SQLiteInstance::open(databasePath, true);
 
- 	createTables();
 
- 	prepareStatements();
 
- }
 
- void LobbyDatabase::insertChatMessage(const std::string & sender, const std::string & roomType, const std::string & roomName, const std::string & messageText)
 
- {
 
- 	insertChatMessageStatement->executeOnce(sender, messageText);
 
- }
 
- bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountID)
 
- {
 
- 	bool result = false;
 
- 	isPlayerInAnyGameRoomStatement->setBinds(accountID);
 
- 	if(isPlayerInAnyGameRoomStatement->execute())
 
- 		isPlayerInAnyGameRoomStatement->getColumns(result);
 
- 	isPlayerInAnyGameRoomStatement->reset();
 
- 	return result;
 
- }
 
- bool LobbyDatabase::isPlayerInGameRoom(const std::string & accountID, const std::string & roomID)
 
- {
 
- 	bool result = false;
 
- 	isPlayerInGameRoomStatement->setBinds(accountID, roomID);
 
- 	if(isPlayerInGameRoomStatement->execute())
 
- 		isPlayerInGameRoomStatement->getColumns(result);
 
- 	isPlayerInGameRoomStatement->reset();
 
- 	return result;
 
- }
 
- std::vector<LobbyChatMessage> LobbyDatabase::getRecentMessageHistory()
 
- {
 
- 	std::vector<LobbyChatMessage> result;
 
- 	while(getRecentMessageHistoryStatement->execute())
 
- 	{
 
- 		LobbyChatMessage message;
 
- 		getRecentMessageHistoryStatement->getColumns(message.accountID, message.displayName, message.messageText, message.age);
 
- 		result.push_back(message);
 
- 	}
 
- 	getRecentMessageHistoryStatement->reset();
 
- 	return result;
 
- }
 
- void LobbyDatabase::setGameRoomStatus(const std::string & roomID, LobbyRoomState roomStatus)
 
- {
 
- 	setGameRoomStatusStatement->executeOnce(vstd::to_underlying(roomStatus), roomID);
 
- }
 
- void LobbyDatabase::setGameRoomPlayerLimit(const std::string & roomID, uint32_t playerLimit)
 
- {
 
- 	setGameRoomPlayerLimitStatement->executeOnce(playerLimit, roomID);
 
- }
 
- void LobbyDatabase::insertPlayerIntoGameRoom(const std::string & accountID, const std::string & roomID)
 
- {
 
- 	insertGameRoomPlayersStatement->executeOnce(roomID, accountID);
 
- }
 
- void LobbyDatabase::deletePlayerFromGameRoom(const std::string & accountID, const std::string & roomID)
 
- {
 
- 	deleteGameRoomPlayersStatement->executeOnce(roomID, accountID);
 
- }
 
- void LobbyDatabase::deleteGameRoomInvite(const std::string & targetAccountID, const std::string & roomID)
 
- {
 
- 	deleteGameRoomInvitesStatement->executeOnce(roomID, targetAccountID);
 
- }
 
- void LobbyDatabase::insertGameRoomInvite(const std::string & targetAccountID, const std::string & roomID)
 
- {
 
- 	insertGameRoomInvitesStatement->executeOnce(roomID, targetAccountID);
 
- }
 
- void LobbyDatabase::insertGameRoom(const std::string & roomID, const std::string & hostAccountID)
 
- {
 
- 	insertGameRoomStatement->executeOnce(roomID, hostAccountID);
 
- }
 
- void LobbyDatabase::insertAccount(const std::string & accountID, const std::string & displayName)
 
- {
 
- 	insertAccountStatement->executeOnce(accountID, displayName);
 
- }
 
- void LobbyDatabase::insertAccessCookie(const std::string & accountID, const std::string & accessCookieUUID)
 
- {
 
- 	insertAccessCookieStatement->executeOnce(accountID, accessCookieUUID);
 
- }
 
- void LobbyDatabase::updateAccessCookie(const std::string & accountID, const std::string & accessCookieUUID) {}
 
- void LobbyDatabase::updateAccountLoginTime(const std::string & accountID) {}
 
- void LobbyDatabase::updateActiveAccount(const std::string & accountID, bool isActive) {}
 
- std::string LobbyDatabase::getAccountDisplayName(const std::string & accountID)
 
- {
 
- 	std::string result;
 
- 	getAccountDisplayNameStatement->setBinds(accountID);
 
- 	if(getAccountDisplayNameStatement->execute())
 
- 		getAccountDisplayNameStatement->getColumns(result);
 
- 	getAccountDisplayNameStatement->reset();
 
- 	return result;
 
- }
 
- LobbyCookieStatus LobbyDatabase::getGameRoomCookieStatus(const std::string & accountID, const std::string & accessCookieUUID, std::chrono::seconds cookieLifetime)
 
- {
 
- 	return {};
 
- }
 
- LobbyCookieStatus LobbyDatabase::getAccountCookieStatus(const std::string & accountID, const std::string & accessCookieUUID, std::chrono::seconds cookieLifetime)
 
- {
 
- 	bool result = false;
 
- 	isAccountCookieValidStatement->setBinds(accountID, accessCookieUUID, cookieLifetime.count());
 
- 	if(isAccountCookieValidStatement->execute())
 
- 		isAccountCookieValidStatement->getColumns(result);
 
- 	isAccountCookieValidStatement->reset();
 
- 	return result ? LobbyCookieStatus::VALID : LobbyCookieStatus::INVALID;
 
- }
 
- LobbyInviteStatus LobbyDatabase::getAccountInviteStatus(const std::string & accountID, const std::string & roomID)
 
- {
 
- 	return {};
 
- }
 
- LobbyRoomState LobbyDatabase::getGameRoomStatus(const std::string & roomID)
 
- {
 
- 	return {};
 
- }
 
- uint32_t LobbyDatabase::getGameRoomFreeSlots(const std::string & roomID)
 
- {
 
- 	return 0;
 
- }
 
- bool LobbyDatabase::isAccountNameExists(const std::string & displayName)
 
- {
 
- 	bool result = false;
 
- 	isAccountNameExistsStatement->setBinds(displayName);
 
- 	if(isAccountNameExistsStatement->execute())
 
- 		isAccountNameExistsStatement->getColumns(result);
 
- 	isAccountNameExistsStatement->reset();
 
- 	return result;
 
- }
 
- bool LobbyDatabase::isAccountIDExists(const std::string & accountID)
 
- {
 
- 	bool result = false;
 
- 	isAccountIDExistsStatement->setBinds(accountID);
 
- 	if(isAccountIDExistsStatement->execute())
 
- 		isAccountIDExistsStatement->getColumns(result);
 
- 	isAccountIDExistsStatement->reset();
 
- 	return result;
 
- }
 
- std::vector<LobbyGameRoom> LobbyDatabase::getActiveGameRooms()
 
- {
 
- 	return {};
 
- }
 
- std::vector<LobbyAccount> LobbyDatabase::getActiveAccounts()
 
- {
 
- 	std::vector<LobbyAccount> result;
 
- 	while(getActiveAccountsStatement->execute())
 
- 	{
 
- 		LobbyAccount entry;
 
- 		getActiveAccountsStatement->getColumns(entry.accountID, entry.displayName);
 
- 		result.push_back(entry);
 
- 	}
 
- 	getActiveAccountsStatement->reset();
 
- 	return result;
 
- }
 
- std::string LobbyDatabase::getIdleGameRoom(const std::string & hostAccountID)
 
- {
 
- 	std::string result;
 
- 	if(getIdleGameRoomStatement->execute())
 
- 		getIdleGameRoomStatement->getColumns(result);
 
- 	getIdleGameRoomStatement->reset();
 
- 	return result;
 
- }
 
- std::string LobbyDatabase::getAccountGameRoom(const std::string & accountID)
 
- {
 
- 	std::string result;
 
- 	if(getAccountGameRoomStatement->execute())
 
- 		getAccountGameRoomStatement->getColumns(result);
 
- 	getAccountGameRoomStatement->reset();
 
- 	return result;
 
- }
 
 
  |