Commit f9446f98 authored by Max-Wilhelm Bruker's avatar Max-Wilhelm Bruker
Browse files

generalized user information

parent adaa1d53
......@@ -12,13 +12,23 @@ ServerInfo_ChatChannel::ServerInfo_ChatChannel(const QString &_name, const QStri
insertItem(new SerializableItem_Bool("auto_join", _autoJoin));
}
ServerInfo_ChatUser::ServerInfo_ChatUser(const QString &_name)
: SerializableItem_Map("chat_user")
ServerInfo_User::ServerInfo_User(const QString &_name, int _userLevel, const QString &_country)
: SerializableItem_Map("user")
{
insertItem(new SerializableItem_String("name", _name));
insertItem(new SerializableItem_Int("userlevel", _userLevel));
insertItem(new SerializableItem_String("country", _country));
}
ServerInfo_Game::ServerInfo_Game(int _gameId, const QString &_description, bool _hasPassword, int _playerCount, int _maxPlayers, const QString &_creatorName, bool _spectatorsAllowed, bool _spectatorsNeedPassword, int _spectatorCount)
ServerInfo_User::ServerInfo_User(const ServerInfo_User *other)
: SerializableItem_Map("user")
{
insertItem(new SerializableItem_String("name", other->getName()));
insertItem(new SerializableItem_Int("userlevel", other->getUserLevel()));
insertItem(new SerializableItem_String("country", other->getCountry()));
}
ServerInfo_Game::ServerInfo_Game(int _gameId, const QString &_description, bool _hasPassword, int _playerCount, int _maxPlayers, ServerInfo_User *_creatorInfo, bool _spectatorsAllowed, bool _spectatorsNeedPassword, int _spectatorCount)
: SerializableItem_Map("game")
{
insertItem(new SerializableItem_Int("game_id", _gameId));
......@@ -26,7 +36,9 @@ ServerInfo_Game::ServerInfo_Game(int _gameId, const QString &_description, bool
insertItem(new SerializableItem_Bool("has_password", _hasPassword));
insertItem(new SerializableItem_Int("player_count", _playerCount));
insertItem(new SerializableItem_Int("max_players", _maxPlayers));
insertItem(new SerializableItem_String("creator_name", _creatorName));
if (!_creatorInfo)
_creatorInfo = new ServerInfo_User;
insertItem(_creatorInfo);
insertItem(new SerializableItem_Bool("spectators_allowed", _spectatorsAllowed));
insertItem(new SerializableItem_Bool("spectators_need_password", _spectatorsNeedPassword));
insertItem(new SerializableItem_Int("spectator_count", _spectatorCount));
......@@ -124,11 +136,13 @@ ServerInfo_Arrow::ServerInfo_Arrow(int _id, int _startPlayerId, const QString &_
insertItem(new SerializableItem_Color("color", _color));
}
ServerInfo_PlayerProperties::ServerInfo_PlayerProperties(int _playerId, const QString &_name, bool _spectator, bool _conceded, bool _readyStart, int _deckId)
ServerInfo_PlayerProperties::ServerInfo_PlayerProperties(int _playerId, ServerInfo_User *_userInfo, bool _spectator, bool _conceded, bool _readyStart, int _deckId)
: SerializableItem_Map("player_properties")
{
insertItem(new SerializableItem_Int("player_id", _playerId));
insertItem(new SerializableItem_String("name", _name));
if (!_userInfo)
_userInfo = new ServerInfo_User;
insertItem(_userInfo);
insertItem(new SerializableItem_Bool("spectator", _spectator));
insertItem(new SerializableItem_Bool("conceded", _conceded));
insertItem(new SerializableItem_Bool("ready_start", _readyStart));
......
......@@ -30,23 +30,33 @@ public:
bool getAutoJoin() const { return static_cast<SerializableItem_Bool *>(itemMap.value("auto_join"))->getData(); }
};
class ServerInfo_ChatUser : public SerializableItem_Map {
class ServerInfo_User : public SerializableItem_Map {
public:
ServerInfo_ChatUser(const QString &_name = QString());
static SerializableItem *newItem() { return new ServerInfo_ChatUser; }
enum UserLevelFlags {
IsNothing = 0x00,
IsUser = 0x01,
IsRegistered = 0x02,
IsAdmin = 0x04
};
ServerInfo_User(const QString &_name = QString(), int _userLevel = IsNothing, const QString &_country = QString());
ServerInfo_User(const ServerInfo_User *other);
static SerializableItem *newItem() { return new ServerInfo_User; }
QString getName() const { return static_cast<SerializableItem_String *>(itemMap.value("name"))->getData(); }
int getUserLevel() const { return static_cast<SerializableItem_Int *>(itemMap.value("userlevel"))->getData(); }
void setUserLevel(int _userLevel) { static_cast<SerializableItem_Int *>(itemMap.value("userlevel"))->setData(_userLevel); }
QString getCountry() const { return static_cast<SerializableItem_String *>(itemMap.value("country"))->getData(); }
};
class ServerInfo_Game : public SerializableItem_Map {
public:
ServerInfo_Game(int _gameId = -1, const QString &_description = QString(), bool _hasPassword = false, int _playerCount = -1, int _maxPlayers = -1, const QString &_creatorName = QString(), bool _spectatorsAllowed = false, bool _spectatorsNeedPassword = false, int _spectatorCount = -1);
ServerInfo_Game(int _gameId = -1, const QString &_description = QString(), bool _hasPassword = false, int _playerCount = -1, int _maxPlayers = -1, ServerInfo_User *creatorInfo = 0, bool _spectatorsAllowed = false, bool _spectatorsNeedPassword = false, int _spectatorCount = -1);
static SerializableItem *newItem() { return new ServerInfo_Game; }
int getGameId() const { return static_cast<SerializableItem_Int *>(itemMap.value("game_id"))->getData(); }
QString getDescription() const { return static_cast<SerializableItem_String *>(itemMap.value("description"))->getData(); }
bool getHasPassword() const { return static_cast<SerializableItem_Bool *>(itemMap.value("has_password"))->getData(); }
int getPlayerCount() const { return static_cast<SerializableItem_Int *>(itemMap.value("player_count"))->getData(); }
int getMaxPlayers() const { return static_cast<SerializableItem_Int *>(itemMap.value("max_players"))->getData(); }
QString getCreatorName() const { return static_cast<SerializableItem_String *>(itemMap.value("creator_name"))->getData(); }
ServerInfo_User *getCreatorInfo() const { return static_cast<ServerInfo_User *>(itemMap.value("user")); }
bool getSpectatorsAllowed() const { return static_cast<SerializableItem_Bool *>(itemMap.value("spectators_allowed"))->getData(); }
bool getSpectatorsNeedPassword() const { return static_cast<SerializableItem_Bool *>(itemMap.value("spectators_need_password"))->getData(); }
int getSpectatorCount() const { return static_cast<SerializableItem_Int *>(itemMap.value("spectator_count"))->getData(); }
......@@ -121,10 +131,10 @@ public:
class ServerInfo_PlayerProperties : public SerializableItem_Map {
public:
ServerInfo_PlayerProperties(int _playerId = -1, const QString &_name = QString(), bool _spectator = false, bool _conceded = false, bool _readyStart = false, int _deckId = -1);
ServerInfo_PlayerProperties(int _playerId = -1, ServerInfo_User *_userInfo = 0, bool _spectator = false, bool _conceded = false, bool _readyStart = false, int _deckId = -1);
static SerializableItem *newItem() { return new ServerInfo_PlayerProperties; }
int getPlayerId() const { return static_cast<SerializableItem_Int *>(itemMap.value("player_id"))->getData(); }
QString getName() const { return static_cast<SerializableItem_String *>(itemMap.value("name"))->getData(); }
ServerInfo_User *getUserInfo() const { return static_cast<ServerInfo_User *>(itemMap.value("user")); }
bool getSpectator() const { return static_cast<SerializableItem_Bool *>(itemMap.value("spectator"))->getData(); }
bool getConceded() const { return static_cast<SerializableItem_Bool *>(itemMap.value("conceded"))->getData(); }
bool getReadyStart() const { return static_cast<SerializableItem_Bool *>(itemMap.value("ready_start"))->getData(); }
......
......@@ -59,11 +59,10 @@ ItemId_Event_DumpZone = 1057,
ItemId_Event_StopDumpZone = 1058,
ItemId_Event_ServerMessage = 1059,
ItemId_Event_GameJoined = 1060,
ItemId_Event_ChatJoinChannel = 1061,
ItemId_Event_ChatLeaveChannel = 1062,
ItemId_Event_ChatSay = 1063,
ItemId_Context_ReadyStart = 1064,
ItemId_Context_Concede = 1065,
ItemId_Context_DeckSelect = 1066,
ItemId_Other = 1067
ItemId_Event_ChatLeaveChannel = 1061,
ItemId_Event_ChatSay = 1062,
ItemId_Context_ReadyStart = 1063,
ItemId_Context_Concede = 1064,
ItemId_Context_DeckSelect = 1065,
ItemId_Other = 1066
};
......@@ -381,11 +381,6 @@ Event_GameJoined::Event_GameJoined(int _gameId, const QString &_gameDescription,
insertItem(new SerializableItem_Bool("spectators_see_everything", _spectatorsSeeEverything));
insertItem(new SerializableItem_Bool("resuming", _resuming));
}
Event_ChatJoinChannel::Event_ChatJoinChannel(const QString &_channel, const QString &_playerName)
: ChatEvent("chat_join_channel", _channel)
{
insertItem(new SerializableItem_String("player_name", _playerName));
}
Event_ChatLeaveChannel::Event_ChatLeaveChannel(const QString &_channel, const QString &_playerName)
: ChatEvent("chat_leave_channel", _channel)
{
......@@ -472,7 +467,6 @@ void ProtocolItem::initializeHashAuto()
itemNameHash.insert("game_eventstop_dump_zone", Event_StopDumpZone::newItem);
itemNameHash.insert("generic_eventserver_message", Event_ServerMessage::newItem);
itemNameHash.insert("generic_eventgame_joined", Event_GameJoined::newItem);
itemNameHash.insert("chat_eventchat_join_channel", Event_ChatJoinChannel::newItem);
itemNameHash.insert("chat_eventchat_leave_channel", Event_ChatLeaveChannel::newItem);
itemNameHash.insert("chat_eventchat_say", Event_ChatSay::newItem);
itemNameHash.insert("game_event_contextready_start", Context_ReadyStart::newItem);
......
......@@ -58,7 +58,6 @@
3:stop_dump_zone:i,zone_owner_id:s,zone
4:server_message:s,message
4:game_joined:i,game_id:s,game_description:i,player_id:b,spectator:b,spectators_can_talk:b,spectators_see_everything:b,resuming
5:chat_join_channel:s,player_name
5:chat_leave_channel:s,player_name
5:chat_say:s,player_name:s,message
6:ready_start
......
......@@ -563,14 +563,6 @@ public:
static SerializableItem *newItem() { return new Event_GameJoined; }
int getItemId() const { return ItemId_Event_GameJoined; }
};
class Event_ChatJoinChannel : public ChatEvent {
Q_OBJECT
public:
Event_ChatJoinChannel(const QString &_channel = QString(), const QString &_playerName = QString());
QString getPlayerName() const { return static_cast<SerializableItem_String *>(itemMap.value("player_name"))->getData(); };
static SerializableItem *newItem() { return new Event_ChatJoinChannel; }
int getItemId() const { return ItemId_Event_ChatJoinChannel; }
};
class Event_ChatLeaveChannel : public ChatEvent {
Q_OBJECT
public:
......
......@@ -22,6 +22,7 @@
#include "server_counter.h"
#include "server_chatchannel.h"
#include "server_protocolhandler.h"
#include "protocol_datastructures.h"
#include <QDebug>
Server::Server(QObject *parent)
......@@ -35,6 +36,35 @@ Server::~Server()
delete clients.takeFirst();
}
AuthenticationResult Server::loginUser(Server_ProtocolHandler *session, QString &name, const QString &password)
{
AuthenticationResult authState = checkUserPassword(name, password);
if (authState == PasswordWrong)
return authState;
if (authState == PasswordRight) {
Server_ProtocolHandler *oldSession = users.value(name);
if (oldSession)
delete oldSession; // ~Server_ProtocolHandler() will call Server::removeClient
} else if (authState == UnknownUser) {
// Change user name so that no two users have the same names
QString tempName = name;
int i = 0;
while (users.contains(tempName))
tempName = name + "_" + QString::number(++i);
name = tempName;
}
ServerInfo_User *data = getUserData(name);
if (authState == PasswordRight)
data->setUserLevel(data->getUserLevel() | ServerInfo_User::IsRegistered);
session->setUserInfo(data);
users.insert(name, session);
return authState;
}
Server_Game *Server::createGame(const QString &description, const QString &password, int maxPlayers, bool spectatorsAllowed, bool spectatorsNeedPassword, bool spectatorsCanTalk, bool spectatorsSeeEverything, Server_ProtocolHandler *creator)
{
Server_Game *newGame = new Server_Game(creator, nextGameId++, description, password, maxPlayers, spectatorsAllowed, spectatorsNeedPassword, spectatorsCanTalk, spectatorsSeeEverything, this);
......@@ -54,19 +84,10 @@ void Server::addClient(Server_ProtocolHandler *client)
void Server::removeClient(Server_ProtocolHandler *client)
{
clients.removeAt(clients.indexOf(client));
qDebug(QString("Server::removeClient: %1 clients left").arg(clients.size()).toLatin1());
}
void Server::closeOldSession(const QString &playerName)
{
Server_ProtocolHandler *session = 0;
for (int i = 0; i < clients.size(); ++i)
if (clients[i]->getPlayerName() == playerName) {
session = clients[i];
break;
}
if (session)
delete session; // ~Server_ProtocolHandler() will call Server::removeClient
ServerInfo_User *data = client->getUserInfo();
if (data)
users.remove(data->getName());
qDebug() << "Server::removeClient: " << clients.size() << "clients; " << users.size() << "users left";
}
Server_Game *Server::getGame(int gameId) const
......@@ -85,14 +106,14 @@ void Server::broadcastGameListUpdate(Server_Game *game)
!game->getPassword().isEmpty(),
game->getPlayerCount(),
game->getMaxPlayers(),
game->getCreatorName(),
new ServerInfo_User(game->getCreatorInfo()),
game->getSpectatorsAllowed(),
game->getSpectatorsNeedPassword(),
game->getSpectatorCount()
));
else
// Game is closing
eventGameList.append(new ServerInfo_Game(game->getGameId(), QString(), false, 0, game->getMaxPlayers(), QString(), false, 0));
eventGameList.append(new ServerInfo_Game(game->getGameId(), QString(), false, 0, game->getMaxPlayers(), 0, false, 0));
Event_ListGames *event = new Event_ListGames(eventGameList);
for (int i = 0; i < clients.size(); i++)
......
......@@ -8,6 +8,7 @@
class Server_Game;
class Server_ChatChannel;
class Server_ProtocolHandler;
class ServerInfo_User;
enum AuthenticationResult { PasswordWrong = 0, PasswordRight = 1, UnknownUser = 2 };
......@@ -22,7 +23,7 @@ private slots:
public:
Server(QObject *parent = 0);
~Server();
virtual AuthenticationResult checkUserPassword(const QString &user, const QString &password) = 0;
AuthenticationResult loginUser(Server_ProtocolHandler *session, QString &name, const QString &password);
QList<Server_Game *> getGames() const { return games.values(); }
Server_Game *getGame(int gameId) const;
const QMap<QString, Server_ChatChannel *> &getChatChannels() { return chatChannels; }
......@@ -30,7 +31,6 @@ public:
void addClient(Server_ProtocolHandler *player);
void removeClient(Server_ProtocolHandler *player);
void closeOldSession(const QString &playerName);
virtual QString getLoginMessage() const = 0;
Server_Game *createGame(const QString &description, const QString &password, int maxPlayers, bool spectatorsAllowed, bool spectatorsNeedPassword, bool spectatorsCanTalk, bool spectatorsSeeEverything, Server_ProtocolHandler *creator);
......@@ -40,8 +40,11 @@ public:
private:
QMap<int, Server_Game *> games;
QList<Server_ProtocolHandler *> clients;
QMap<QString, Server_ProtocolHandler *> users;
QMap<QString, Server_ChatChannel *> chatChannels;
protected:
virtual AuthenticationResult checkUserPassword(const QString &user, const QString &password) = 0;
virtual ServerInfo_User *getUserData(const QString &name) = 0;
int nextGameId;
void addChatChannel(Server_ChatChannel *newChannel);
};
......
......@@ -8,12 +8,12 @@ Server_ChatChannel::Server_ChatChannel(const QString &_name, const QString &_des
void Server_ChatChannel::addClient(Server_ProtocolHandler *client)
{
sendChatEvent(new Event_ChatJoinChannel(name, client->getPlayerName()));
sendChatEvent(new Event_ChatJoinChannel(name, new ServerInfo_User(client->getUserInfo())));
append(client);
QList<ServerInfo_ChatUser *> eventUserList;
QList<ServerInfo_User *> eventUserList;
for (int i = 0; i < size(); ++i)
eventUserList.append(new ServerInfo_ChatUser(at(i)->getPlayerName()));
eventUserList.append(new ServerInfo_User(at(i)->getUserInfo()));
Event_ChatListPlayers *eventCLP = new Event_ChatListPlayers(name, eventUserList);
client->enqueueProtocolItem(eventCLP);
......@@ -25,13 +25,13 @@ void Server_ChatChannel::addClient(Server_ProtocolHandler *client)
void Server_ChatChannel::removeClient(Server_ProtocolHandler *client)
{
removeAt(indexOf(client));
sendChatEvent(new Event_ChatLeaveChannel(name, client->getPlayerName()));
sendChatEvent(new Event_ChatLeaveChannel(name, client->getUserInfo()->getName()));
emit channelInfoChanged();
}
void Server_ChatChannel::say(Server_ProtocolHandler *client, const QString &s)
{
sendChatEvent(new Event_ChatSay(name, client->getPlayerName(), s));
sendChatEvent(new Event_ChatSay(name, client->getUserInfo()->getName(), s));
}
void Server_ChatChannel::sendChatEvent(ChatEvent *event)
......
......@@ -28,9 +28,9 @@
#include <QDebug>
Server_Game::Server_Game(Server_ProtocolHandler *_creator, int _gameId, const QString &_description, const QString &_password, int _maxPlayers, bool _spectatorsAllowed, bool _spectatorsNeedPassword, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, Server *parent)
: QObject(parent), gameStarted(false), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers), activePlayer(-1), activePhase(-1), spectatorsAllowed(_spectatorsAllowed), spectatorsNeedPassword(_spectatorsNeedPassword), spectatorsCanTalk(_spectatorsCanTalk), spectatorsSeeEverything(_spectatorsSeeEverything), inactivityCounter(0)
: QObject(parent), creatorInfo(new ServerInfo_User(_creator->getUserInfo())), gameStarted(false), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers), activePlayer(-1), activePhase(-1), spectatorsAllowed(_spectatorsAllowed), spectatorsNeedPassword(_spectatorsNeedPassword), spectatorsCanTalk(_spectatorsCanTalk), spectatorsSeeEverything(_spectatorsSeeEverything), inactivityCounter(0)
{
creator = addPlayer(_creator, false, false);
addPlayer(_creator, false, false);
if (parent->getGameShouldPing()) {
pingClock = new QTimer(this);
......@@ -49,6 +49,7 @@ Server_Game::~Server_Game()
players.clear();
emit gameClosing();
delete creatorInfo;
qDebug("Server_Game destructor");
}
......@@ -187,7 +188,7 @@ Server_Player *Server_Game::addPlayer(Server_ProtocolHandler *handler, bool spec
const QList<int> &keyList = players.keys();
int playerId = keyList.isEmpty() ? 0 : (keyList.last() + 1);
Server_Player *newPlayer = new Server_Player(this, playerId, handler->getPlayerName(), spectator, handler);
Server_Player *newPlayer = new Server_Player(this, playerId, handler->getUserInfo(), spectator, handler);
sendGameEvent(new Event_Join(newPlayer->getProperties()));
players.insert(playerId, newPlayer);
......@@ -350,8 +351,7 @@ QList<ServerInfo_Player *> Server_Game::getGameState(Server_Player *playerWhosAs
zoneList.append(new ServerInfo_Zone(zone->getName(), zone->getType(), zone->hasCoords(), zone->cards.size(), cardList));
}
ServerInfo_PlayerProperties *properties = new ServerInfo_PlayerProperties(player->getPlayerId(), player->getPlayerName(), player->getSpectator(), player->getConceded(), player->getReadyStart(), player->getDeckId());
result.append(new ServerInfo_Player(properties, player == playerWhosAsking ? player->getDeck() : 0, zoneList, counterList, arrowList));
result.append(new ServerInfo_Player(player->getProperties(), player == playerWhosAsking ? player->getDeck() : 0, zoneList, counterList, arrowList));
}
return result;
}
......
......@@ -28,11 +28,12 @@
class QTimer;
class Server;
class ServerInfo_User;
class Server_Game : public QObject {
Q_OBJECT
private:
QPointer<Server_Player> creator;
ServerInfo_User *creatorInfo;
QMap<int, Server_Player *> players;
bool gameStarted;
int gameId;
......@@ -53,8 +54,7 @@ private slots:
public:
Server_Game(Server_ProtocolHandler *_creator, int _gameId, const QString &_description, const QString &_password, int _maxPlayers, bool _spectatorsAllowed, bool _spectatorsNeedPassword, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, Server *parent);
~Server_Game();
Server_Player *getCreator() const { return creator; }
QString getCreatorName() const { return creator ? creator->getPlayerName() : QString(); }
ServerInfo_User *getCreatorInfo() const { return creatorInfo; }
bool getGameStarted() const { return gameStarted; }
int getPlayerCount() const;
int getSpectatorCount() const;
......
......@@ -9,8 +9,8 @@
#include "protocol_items.h"
#include "decklist.h"
Server_Player::Server_Player(Server_Game *_game, int _playerId, const QString &_playerName, bool _spectator, Server_ProtocolHandler *_handler)
: game(_game), handler(_handler), deck(0), playerId(_playerId), playerName(_playerName), spectator(_spectator), nextCardId(0), readyStart(false), conceded(false), deckId(-2)
Server_Player::Server_Player(Server_Game *_game, int _playerId, ServerInfo_User *_userInfo, bool _spectator, Server_ProtocolHandler *_handler)
: game(_game), handler(_handler), userInfo(new ServerInfo_User(_userInfo)), deck(0), playerId(_playerId), spectator(_spectator), nextCardId(0), readyStart(false), conceded(false), deckId(-2)
{
}
......@@ -20,6 +20,7 @@ Server_Player::~Server_Player()
if (handler)
handler->playerRemovedFromGame(game);
delete userInfo;
}
int Server_Player::newCardId()
......@@ -151,7 +152,7 @@ void Server_Player::clearZones()
ServerInfo_PlayerProperties *Server_Player::getProperties()
{
return new ServerInfo_PlayerProperties(playerId, playerName, spectator, conceded, readyStart, deckId);
return new ServerInfo_PlayerProperties(playerId, new ServerInfo_User(userInfo), spectator, conceded, readyStart, deckId);
}
void Server_Player::setDeck(DeckList *_deck, int _deckId)
......
......@@ -13,6 +13,7 @@ class Server_Counter;
class Server_Arrow;
class Server_ProtocolHandler;
class ProtocolItem;
class ServerInfo_User;
class ServerInfo_PlayerProperties;
class Server_Player : public Server_ArrowTarget {
......@@ -20,12 +21,12 @@ class Server_Player : public Server_ArrowTarget {
private:
Server_Game *game;
Server_ProtocolHandler *handler;
ServerInfo_User *userInfo;
DeckList *deck;
QMap<QString, Server_CardZone *> zones;
QMap<int, Server_Counter *> counters;
QMap<int, Server_Arrow *> arrows;
int playerId;
QString playerName;
bool spectator;
int initialCards;
int nextCardId;
......@@ -33,7 +34,7 @@ private:
bool conceded;
int deckId;
public:
Server_Player(Server_Game *_game, int _playerId, const QString &_playerName, bool _spectator, Server_ProtocolHandler *_handler);
Server_Player(Server_Game *_game, int _playerId, ServerInfo_User *_userInfo, bool _spectator, Server_ProtocolHandler *_handler);
~Server_Player();
Server_ProtocolHandler *getProtocolHandler() const { return handler; }
void setProtocolHandler(Server_ProtocolHandler *_handler) { handler = _handler; }
......@@ -48,7 +49,7 @@ public:
bool getConceded() const { return conceded; }
void setConceded(bool _conceded) { conceded = _conceded; }
int getDeckId() const { return deckId; }
QString getPlayerName() const { return playerName; }
ServerInfo_User *getUserInfo() const { return userInfo; }
void setDeck(DeckList *_deck, int _deckId);
DeckList *getDeck() const { return deck; }
const QMap<QString, Server_CardZone *> &getZones() const { return zones; }
......
......@@ -14,7 +14,7 @@
#include <QDateTime>
Server_ProtocolHandler::Server_ProtocolHandler(Server *_server, QObject *parent)
: QObject(parent), server(_server), authState(PasswordWrong), acceptsGameListChanges(false), lastCommandTime(QDateTime::currentDateTime())
: QObject(parent), server(_server), authState(PasswordWrong), acceptsGameListChanges(false), userInfo(0), lastCommandTime(QDateTime::currentDateTime())
{
connect(server, SIGNAL(pingClockTimeout()), this, SLOT(pingClockTimeout()));
}
......@@ -40,6 +40,8 @@ Server_ProtocolHandler::~Server_ProtocolHandler()
QMapIterator<QString, Server_ChatChannel *> chatChannelIterator(chatChannels);
while (chatChannelIterator.hasNext())
chatChannelIterator.next().value()->removeClient(this);
delete userInfo;
}
void Server_ProtocolHandler::playerRemovedFromGame(Server_Game *game)
......@@ -200,15 +202,12 @@ ResponseCode Server_ProtocolHandler::cmdPing(Command_Ping * /*cmd*/, CommandCont
ResponseCode Server_ProtocolHandler::cmdLogin(Command_Login *cmd, CommandContainer *cont)
{
QString userName = cmd->getUsername().simplified();
if (userName.isEmpty())
if (userName.isEmpty() || (userInfo != 0))
return RespContextError;
authState = server->checkUserPassword(userName, cmd->getPassword());
authState = server->loginUser(this, userName, cmd->getPassword());
if (authState == PasswordWrong)
return RespWrongPassword;
if (authState == PasswordRight)
server->closeOldSession(userName);
playerName = userName;
enqueueProtocolItem(new Event_ServerMessage(server->getLoginMessage()));
if (authState == PasswordRight) {
......@@ -217,7 +216,7 @@ ResponseCode Server_ProtocolHandler::cmdLogin(Command_Login *cmd, CommandContain
for (int i = 0; i < serverGames.size(); ++i) {
const QList<Server_Player *> &gamePlayers = serverGames[i]->getPlayers().values();
for (int j = 0; j < gamePlayers.size(); ++j)
if (gamePlayers[j]->getPlayerName() == playerName) {
if (gamePlayers[j]->getUserInfo()->getName() == userInfo->getName()) {
gamePlayers[j]->setProtocolHandler(this);
games.insert(serverGames[i]->getGameId(), QPair<Server_Game *, Server_Player *>(serverGames[i], gamePlayers[j]));
......@@ -293,7 +292,7 @@ ResponseCode Server_ProtocolHandler::cmdListGames(Command_ListGames * /*cmd*/, C
!g->getPassword().isEmpty(),
g->getPlayerCount(),
g->getMaxPlayers(),
g->getCreatorName(),
g->getCreatorInfo(),
g->getSpectatorsAllowed(),
g->getSpectatorsNeedPassword(),
g->getSpectatorCount()
......@@ -311,7 +310,7 @@ ResponseCode Server_ProtocolHandler::cmdCreateGame(Command_CreateGame *cmd, Comm
return RespLoginNeeded;
Server_Game *game = server->createGame(cmd->getDescription(), cmd->getPassword(), cmd->getMaxPlayers(), cmd->getSpectatorsAllowed(), cmd->getSpectatorsNeedPassword(), cmd->getSpectatorsCanTalk(), cmd->getSpectatorsSeeEverything(), this);
Server_Player *creator = game->getCreator();
Server_Player *creator = game->getPlayers().values().first();
games.insert(game->getGameId(), QPair<Server_Game *, Server_Player *>(game, creator));
enqueueProtocolItem(new Event_GameJoined(game->getGameId(), game->getDescription(), creator->getPlayerId(), false, game->getSpectatorsCanTalk(), game->getSpectatorsSeeEverything(), false));
......
......@@ -9,6 +9,7 @@
class Server_Player;
class Server_Card;
class ServerInfo_User;
class QTimer;
class Server_ProtocolHandler : public QObject {
......@@ -17,7 +18,6 @@ protected:
Server *server;
QMap<int, QPair<Server_Game *, Server_Player *> > games;
QMap<QString, Server_ChatChannel *> chatChannels;
QString playerName;
Server *getServer() const { return server; }
QPair<Server_Game *, Server_Player *> getGame(int gameId) const;
......@@ -25,6 +25,7 @@ protected:
AuthenticationResult authState;
bool acceptsGameListChanges;
bool acceptsChatChannelListChanges;
ServerInfo_User *userInfo;
private:
QList<ProtocolItem *> itemQueue;
......@@ -91,7 +92,8 @@ public:
bool getAcceptsGameListChanges() const { return acceptsGameListChanges; }
bool getAcceptsChatChannelListChanges() const { return acceptsChatChannelListChanges; }
const QString &getPlayerName() const { return playerName; }
ServerInfo_User *getUserInfo() const { return userInfo; }
void setUserInfo(ServerInfo_User *_userInfo) { userInfo = _userInfo; }
const QDateTime &getLastCommandTime() const { return lastCommandTime; }
void processCommandContainer(CommandContainer *cont);
......
......@@ -48,7 +48,6 @@ void testRNG()
numbers[max - minMax] = rng->makeNumbersVector(n * (max - min + 1), min, max);
chisq[max - minMax] = rng->testRandom(numbers[max - minMax]);
}
qDebug() << numbers;
for (int i = 0; i <= maxMax - min; ++i) {
std::cerr << (min + i);
for (int j = 0; j < numbers.size(); ++j) {
......
......@@ -19,6 +19,7 @@
***************************************************************************/
#include <QtSql>
#include <QSettings>
#include <QDebug>
#include "servatrice.h"
#include "server_chatchannel.h"
#include "serversocketinterface.h"
......@@ -88,7 +89,7 @@ bool Servatrice::openDatabase()
if (!query.next())
return false;
nextGameId = query.value(0).toInt() + 1;
qDebug(QString("set nextGameId to %1").arg(nextGameId).toLatin1());
qDebug() << "set nextGameId to " << nextGameId;
}
return true;
}
......@@ -103,7 +104,7 @@ bool Servatrice::execSqlQuery(QSqlQuery &query)
{
if (query.exec())
return true;
qCritical(QString("Database error: %1").arg(query.lastError().text()).toLatin1());
qCritical() << "Database error:" << query.lastError().text();
return false;
}
......@@ -139,4 +140,35 @@ AuthenticationResult Servatrice::checkUserPassword(const QString &user, const QS
return UnknownUser;
}
const QString Servatrice::versionString = "Servatrice 0.20100915";
ServerInfo_User *Servatrice::getUserData(const QString &name)
{
const QString method = settings->value("authentication/method").toString();
if (method == "sql") {
checkSql();
QSqlQuery query;
query.prepare("select admin, country from users where name = :name");
query.bindValue(":name", name);
if (!execSqlQuery(query))
return new ServerInfo_User(name);
if (query.next()) {
bool is_admin = query.value(0).toInt();
QString country = query.value(1).toString();
int userLevel = ServerInfo_User::IsUser;
if (is_admin)
userLevel |= ServerInfo_User::IsAdmin;
return new ServerInfo_User(
name,
userLevel,
country
);
} else
return new ServerInfo_User(name);
} else
return new ServerInfo_User(name);
}
const QString Servatrice::versionString = "Servatrice 0.20100918";
......@@ -40,11 +40,13 @@ public:
bool openDatabase();
void checkSql();
bool execSqlQuery(QSqlQuery &query);
AuthenticationResult checkUserPassword(const QString &user, const QString &password);
QString getLoginMessage() const { return loginMessage; }
bool getGameShouldPing() const { return true; }
int getMaxGameInactivityTime() const { return maxGameInactivityTime; }
int getMaxPlayerInactivityTime() const { return maxPlayerInactivityTime; }
protected:
AuthenticationResult checkUserPassword(const QString &user, const QString &password);
ServerInfo_User *getUserData(const QString &name);
private:
QTimer *pingClock;
QTcpServer *tcpServer;
......
......@@ -109,7 +109,7 @@ int ServerSocketInterface::getDeckPathId(int basePathId, QStringList path)
query.prepare("select id from decklist_folders where id_parent = :id_parent and name = :name and user = :user");
query.bindValue(":id_parent", basePathId);
query.bindValue(":name", path.takeFirst());
query.bindValue(":user", playerName);
query.bindValue(":user", userInfo->getName());
if (!servatrice->execSqlQuery(query))
return -1;
if (!query.next())
......@@ -131,7 +131,7 @@ bool ServerSocketInterface::deckListHelper(DeckList_Directory *folder)
QSqlQuery query;
query.prepare("select id, name from decklist_folders where id_parent = :id_parent and user = :user");
query.bindValue(":id_parent", folder->getId());
query.bindValue(":user", playerName);
query.bindValue(":user", userInfo->getName());
if (!servatrice->execSqlQuery(query))
return false;
......@@ -144,7 +144,7 @@ bool ServerSocketInterface::deckListHelper(DeckList_Directory *folder)
query.prepare("select id, name, upload_time from decklist_files where id_folder = :id_folder and user = :user");
query.bindValue(":id_folder", folder->getId());
query.bindValue(":user", playerName);
query.bindValue(":user", userInfo->getName());
if (!servatrice->execSqlQuery(query))
return false;
......@@ -190,7 +190,7 @@ ResponseCode ServerSocketInterface::cmdDeckNewDir(Command_DeckNewDir *cmd, Comma
QSqlQuery query;
query.prepare("insert into decklist_folders (id_parent, user, name) values(:id_parent, :user, :name)");
query.bindValue(":id_parent", folderId);
query.bindValue(":user", playerName);
query.bindValue(":user", userInfo->getName());
query.bindValue(":name", cmd->getDirName());
if (!servatrice->execSqlQuery(query))
return RespContextError;
......@@ -243,7 +243,7 @@ ResponseCode ServerSocketInterface::cmdDeckDel(Command_DeckDel *cmd, CommandCont
query.prepare("select id from decklist_files where id = :id and user = :user");
query.bindValue(":id", cmd->getDeckId());
query.bindValue(":user", playerName);
query.bindValue(":user", userInfo->getName());
servatrice->execSqlQuery(query);
if (!query.next())
return RespNameNotFound;
......@@ -281,7 +281,7 @@ ResponseCode ServerSocketInterface::cmdDeckUpload(Command_DeckUpload *cmd, Comma
QSqlQuery query;
query.prepare("insert into decklist_files (id_folder, user, name, upload_time, content) values(:id_folder, :user, :name, NOW(), :content)");
query.bindValue(":id_folder", folderId);
query.bindValue(":user", playerName);
query.bindValue(":user", userInfo->getName());
query.bindValue(":name", deckName);
query.bindValue(":content", deckContents);
servatrice->execSqlQuery(query);
......@@ -298,7 +298,7 @@ DeckList *ServerSocketInterface::getDeckFromDatabase(int deckId)
query.prepare("select content from decklist_files where id = :id and user = :user");
query.bindValue(":id", deckId);
query.bindValue(":user", playerName);
query.bindValue(":user", userInfo->getName());
servatrice->execSqlQuery(query);
if (!query.next())
throw RespNameNotFound;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment