mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-13 01:24:46 -07:00
generalized user information
This commit is contained in:
parent
adaa1d5323
commit
f9446f9822
61 changed files with 3710 additions and 175 deletions
|
|
@ -15,7 +15,7 @@ void ProtocolItem::initializeHash()
|
|||
|
||||
registerSerializableItem("move_card_to_zone", MoveCardToZone::newItem);
|
||||
registerSerializableItem("chat_channel", ServerInfo_ChatChannel::newItem);
|
||||
registerSerializableItem("chat_user", ServerInfo_ChatUser::newItem);
|
||||
registerSerializableItem("user", ServerInfo_User::newItem);
|
||||
registerSerializableItem("game", ServerInfo_Game::newItem);
|
||||
registerSerializableItem("card_counter", ServerInfo_CardCounter::newItem);
|
||||
registerSerializableItem("card", ServerInfo_Card::newItem);
|
||||
|
|
@ -52,6 +52,7 @@ void ProtocolItem::initializeHash()
|
|||
registerSerializableItem("game_eventdraw_cards", Event_DrawCards::newItem);
|
||||
registerSerializableItem("game_eventping", Event_Ping::newItem);
|
||||
registerSerializableItem("chat_eventchat_list_players", Event_ChatListPlayers::newItem);
|
||||
registerSerializableItem("chat_eventchat_join_channel", Event_ChatJoinChannel::newItem);
|
||||
}
|
||||
|
||||
TopLevelProtocolItem::TopLevelProtocolItem()
|
||||
|
|
@ -275,13 +276,21 @@ Event_ListChatChannels::Event_ListChatChannels(const QList<ServerInfo_ChatChanne
|
|||
itemList.append(_channelList[i]);
|
||||
}
|
||||
|
||||
Event_ChatListPlayers::Event_ChatListPlayers(const QString &_channel, const QList<ServerInfo_ChatUser *> &_playerList)
|
||||
Event_ChatListPlayers::Event_ChatListPlayers(const QString &_channel, const QList<ServerInfo_User *> &_playerList)
|
||||
: ChatEvent("chat_list_players", _channel)
|
||||
{
|
||||
for (int i = 0; i < _playerList.size(); ++i)
|
||||
itemList.append(_playerList[i]);
|
||||
}
|
||||
|
||||
Event_ChatJoinChannel::Event_ChatJoinChannel(const QString &_channel, ServerInfo_User *_info)
|
||||
: ChatEvent("chat_join_channel", _channel)
|
||||
{
|
||||
if (!_info)
|
||||
_info = new ServerInfo_User;
|
||||
insertItem(_info);
|
||||
}
|
||||
|
||||
Event_ListGames::Event_ListGames(const QList<ServerInfo_Game *> &_gameList)
|
||||
: GenericEvent("list_games")
|
||||
{
|
||||
|
|
|
|||
|
|
@ -27,14 +27,15 @@ enum ItemId {
|
|||
ItemId_Command_SetSideboardPlan = ItemId_Other + 102,
|
||||
ItemId_Event_ListChatChannels = ItemId_Other + 200,
|
||||
ItemId_Event_ChatListPlayers = ItemId_Other + 201,
|
||||
ItemId_Event_ListGames = ItemId_Other + 202,
|
||||
ItemId_Event_GameStateChanged = ItemId_Other + 203,
|
||||
ItemId_Event_PlayerPropertiesChanged = ItemId_Other + 204,
|
||||
ItemId_Event_CreateArrows = ItemId_Other + 205,
|
||||
ItemId_Event_CreateCounters = ItemId_Other + 206,
|
||||
ItemId_Event_DrawCards = ItemId_Other + 207,
|
||||
ItemId_Event_Join = ItemId_Other + 208,
|
||||
ItemId_Event_Ping = ItemId_Other + 209,
|
||||
ItemId_Event_ChatJoinChannel = ItemId_Other + 202,
|
||||
ItemId_Event_ListGames = ItemId_Other + 203,
|
||||
ItemId_Event_GameStateChanged = ItemId_Other + 204,
|
||||
ItemId_Event_PlayerPropertiesChanged = ItemId_Other + 205,
|
||||
ItemId_Event_CreateArrows = ItemId_Other + 206,
|
||||
ItemId_Event_CreateCounters = ItemId_Other + 207,
|
||||
ItemId_Event_DrawCards = ItemId_Other + 208,
|
||||
ItemId_Event_Join = ItemId_Other + 209,
|
||||
ItemId_Event_Ping = ItemId_Other + 210,
|
||||
ItemId_Response_DeckList = ItemId_Other + 300,
|
||||
ItemId_Response_DeckDownload = ItemId_Other + 301,
|
||||
ItemId_Response_DeckUpload = ItemId_Other + 302,
|
||||
|
|
@ -298,10 +299,19 @@ public:
|
|||
class Event_ChatListPlayers : public ChatEvent {
|
||||
Q_OBJECT
|
||||
public:
|
||||
Event_ChatListPlayers(const QString &_channel = QString(), const QList<ServerInfo_ChatUser *> &_playerList = QList<ServerInfo_ChatUser *>());
|
||||
Event_ChatListPlayers(const QString &_channel = QString(), const QList<ServerInfo_User *> &_playerList = QList<ServerInfo_User *>());
|
||||
int getItemId() const { return ItemId_Event_ChatListPlayers; }
|
||||
static SerializableItem *newItem() { return new Event_ChatListPlayers; }
|
||||
QList<ServerInfo_ChatUser *> getPlayerList() const { return typecastItemList<ServerInfo_ChatUser *>(); }
|
||||
QList<ServerInfo_User *> getPlayerList() const { return typecastItemList<ServerInfo_User *>(); }
|
||||
};
|
||||
|
||||
class Event_ChatJoinChannel : public ChatEvent {
|
||||
Q_OBJECT
|
||||
public:
|
||||
Event_ChatJoinChannel(const QString &_channel = QString(), ServerInfo_User *_info = 0);
|
||||
int getItemId() const { return ItemId_Event_ChatJoinChannel; }
|
||||
static SerializableItem *newItem() { return new Event_ChatJoinChannel; }
|
||||
ServerInfo_User *getUserInfo() const { return static_cast<ServerInfo_User *>(itemMap.value("user")); }
|
||||
};
|
||||
|
||||
class Event_ListGames : public GenericEvent {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue