Chat works!

This commit is contained in:
Max-Wilhelm Bruker 2009-11-13 18:27:06 +01:00
parent 1baa188067
commit 64aa68cd26
28 changed files with 714 additions and 424 deletions

View file

@ -25,6 +25,7 @@ bool ProtocolItem::read(QXmlStreamReader *xml)
QString tagName = xml->name().toString();
if (parameters.contains(tagName))
parameters[tagName] = currentElementText;
currentElementText.clear();
}
} else if (xml->isCharacters() && !xml->isWhitespace())
currentElementText = xml->text().toString();
@ -62,8 +63,13 @@ void ProtocolItem::initializeHash()
return;
initializeHashAuto();
itemNameHash.insert("resp", ProtocolResponse::newItem);
ProtocolResponse::initializeHash();
itemNameHash.insert("generic_eventlist_games", Event_ListGames::newItem);
itemNameHash.insert("generic_eventlist_chat_channels", Event_ListChatChannels::newItem);
itemNameHash.insert("chat_eventchat_list_players", Event_ChatListPlayers::newItem);
}
int Command::lastCmdId = 0;
@ -153,7 +159,7 @@ ChatEvent::ChatEvent(const QString &_eventName, const QString &_channel)
setParameter("channel", channel);
}
bool Event_ChatListChannels::readElement(QXmlStreamReader *xml)
bool Event_ListChatChannels::readElement(QXmlStreamReader *xml)
{
if (xml->isStartElement() && (xml->name() == "channel")) {
channelList.append(ServerChatChannelInfo(
@ -167,7 +173,7 @@ bool Event_ChatListChannels::readElement(QXmlStreamReader *xml)
return false;
}
void Event_ChatListChannels::writeElement(QXmlStreamWriter *xml)
void Event_ListChatChannels::writeElement(QXmlStreamWriter *xml)
{
for (int i = 0; i < channelList.size(); ++i) {
xml->writeStartElement("channel");

View file

@ -16,7 +16,7 @@ class QXmlStreamAttributes;
class ProtocolResponse;
enum ItemId {
ItemId_Event_ChatListChannels = ItemId_Other + 1,
ItemId_Event_ListChatChannels = ItemId_Other + 1,
ItemId_Event_ChatListPlayers = ItemId_Other + 2,
ItemId_Event_ListGames = ItemId_Other + 3
};
@ -162,15 +162,17 @@ protected:
void extractParameters();
public:
ChatEvent(const QString &_eventName, const QString &_channel);
QString getChannel() const { return channel; }
};
class Event_ChatListChannels : public ChatEvent {
class Event_ListChatChannels : public GenericEvent {
Q_OBJECT
private:
QList<ServerChatChannelInfo> channelList;
public:
Event_ChatListChannels() : ChatEvent("chat_list_channels", QString()) { }
int getItemId() const { return ItemId_Event_ChatListChannels; }
Event_ListChatChannels() : GenericEvent("list_chat_channels") { }
int getItemId() const { return ItemId_Event_ListChatChannels; }
static ProtocolItem *newItem() { return new Event_ListChatChannels; }
void addChannel(const QString &_name, const QString &_description, int _playerCount, bool _autoJoin)
{
channelList.append(ServerChatChannelInfo(_name, _description, _playerCount, _autoJoin));
@ -186,8 +188,9 @@ class Event_ChatListPlayers : public ChatEvent {
private:
QList<ServerPlayerInfo> playerList;
public:
Event_ChatListPlayers(const QString &_channel) : ChatEvent("chat_list_players", _channel) { }
Event_ChatListPlayers(const QString &_channel = QString()) : ChatEvent("chat_list_players", _channel) { }
int getItemId() const { return ItemId_Event_ChatListPlayers; }
static ProtocolItem *newItem() { return new Event_ChatListPlayers; }
void addPlayer(const QString &_name)
{
playerList.append(ServerPlayerInfo(_name));
@ -205,6 +208,7 @@ private:
public:
Event_ListGames() : GenericEvent("list_games") { }
int getItemId() const { return ItemId_Event_ListGames; }
static ProtocolItem *newItem() { return new Event_ListGames; }
void addGame(int _gameId, const QString &_description, bool _hasPassword, int _playerCount, int _maxPlayers, const QString &_creatorName, bool _spectatorsAllowed, int _spectatorCount)
{
gameList.append(ServerGameInfo(_gameId, _description, _hasPassword, _playerCount, _maxPlayers, _creatorName, _spectatorsAllowed, _spectatorCount));

View file

@ -1,7 +1,7 @@
enum AutoItemId {
ItemId_Command_Ping = 1001,
ItemId_Command_Login = 1002,
ItemId_Command_ChatListChannels = 1003,
ItemId_Command_ListChatChannels = 1003,
ItemId_Command_ChatJoinChannel = 1004,
ItemId_Command_ChatLeaveChannel = 1005,
ItemId_Command_ChatSay = 1006,
@ -50,7 +50,7 @@ ItemId_Event_SetActivePlayer = 1048,
ItemId_Event_SetActivePhase = 1049,
ItemId_Event_DumpZone = 1050,
ItemId_Event_StopDumpZone = 1051,
ItemId_Event_ChatServerMessage = 1052,
ItemId_Event_ServerMessage = 1052,
ItemId_Event_ChatJoinChannel = 1053,
ItemId_Event_ChatLeaveChannel = 1054,
ItemId_Event_ChatSay = 1055,

View file

@ -17,8 +17,8 @@ void Command_Login::extractParameters()
username = parameters["username"];
password = parameters["password"];
}
Command_ChatListChannels::Command_ChatListChannels()
: Command("chat_list_channels")
Command_ListChatChannels::Command_ListChatChannels()
: Command("list_chat_channels")
{
}
Command_ChatJoinChannel::Command_ChatJoinChannel(const QString &_channel)
@ -551,14 +551,14 @@ void Event_StopDumpZone::extractParameters()
zoneOwnerId = parameters["zone_owner_id"].toInt();
zone = parameters["zone"];
}
Event_ChatServerMessage::Event_ChatServerMessage(const QString &_channel, const QString &_message)
: ChatEvent("chat_server_message", _channel), message(_message)
Event_ServerMessage::Event_ServerMessage(const QString &_message)
: GenericEvent("server_message"), message(_message)
{
setParameter("message", message);
}
void Event_ChatServerMessage::extractParameters()
void Event_ServerMessage::extractParameters()
{
ChatEvent::extractParameters();
GenericEvent::extractParameters();
message = parameters["message"];
}
Event_ChatJoinChannel::Event_ChatJoinChannel(const QString &_channel, const QString &_playerName)
@ -597,7 +597,7 @@ void ProtocolItem::initializeHashAuto()
{
itemNameHash.insert("cmdping", Command_Ping::newItem);
itemNameHash.insert("cmdlogin", Command_Login::newItem);
itemNameHash.insert("cmdchat_list_channels", Command_ChatListChannels::newItem);
itemNameHash.insert("cmdlist_chat_channels", Command_ListChatChannels::newItem);
itemNameHash.insert("cmdchat_join_channel", Command_ChatJoinChannel::newItem);
itemNameHash.insert("cmdchat_leave_channel", Command_ChatLeaveChannel::newItem);
itemNameHash.insert("cmdchat_say", Command_ChatSay::newItem);
@ -646,7 +646,7 @@ void ProtocolItem::initializeHashAuto()
itemNameHash.insert("game_eventset_active_phase", Event_SetActivePhase::newItem);
itemNameHash.insert("game_eventdump_zone", Event_DumpZone::newItem);
itemNameHash.insert("game_eventstop_dump_zone", Event_StopDumpZone::newItem);
itemNameHash.insert("chat_eventchat_server_message", Event_ChatServerMessage::newItem);
itemNameHash.insert("generic_eventserver_message", Event_ServerMessage::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);

View file

@ -1,6 +1,6 @@
0:ping
0:login:s,username:s,password
0:chat_list_channels
0:list_chat_channels
0:chat_join_channel:s,channel
1:chat_leave_channel
1:chat_say:s,message
@ -49,7 +49,7 @@
3:set_active_phase:i,phase
3:dump_zone:i,zone_owner_id:s,zone:i,number_cards
3:stop_dump_zone:i,zone_owner_id:s,zone
5:chat_server_message:s,message
4:server_message:s,message
5:chat_join_channel:s,player_name
5:chat_leave_channel:s,player_name
5:chat_say:s,player_name:s,message

View file

@ -25,13 +25,13 @@ public:
protected:
void extractParameters();
};
class Command_ChatListChannels : public Command {
class Command_ListChatChannels : public Command {
Q_OBJECT
private:
public:
Command_ChatListChannels();
static ProtocolItem *newItem() { return new Command_ChatListChannels; }
int getItemId() const { return ItemId_Command_ChatListChannels; }
Command_ListChatChannels();
static ProtocolItem *newItem() { return new Command_ListChatChannels; }
int getItemId() const { return ItemId_Command_ListChatChannels; }
};
class Command_ChatJoinChannel : public Command {
Q_OBJECT
@ -685,15 +685,15 @@ public:
protected:
void extractParameters();
};
class Event_ChatServerMessage : public ChatEvent {
class Event_ServerMessage : public GenericEvent {
Q_OBJECT
private:
QString message;
public:
Event_ChatServerMessage(const QString &_channel = QString(), const QString &_message = QString());
Event_ServerMessage(const QString &_message = QString());
QString getMessage() const { return message; }
static ProtocolItem *newItem() { return new Event_ChatServerMessage; }
int getItemId() const { return ItemId_Event_ChatServerMessage; }
static ProtocolItem *newItem() { return new Event_ServerMessage; }
int getItemId() const { return ItemId_Event_ServerMessage; }
protected:
void extractParameters();
};

View file

@ -87,7 +87,7 @@ void Server::broadcastGameListUpdate(Server_Game *game)
void Server::broadcastChannelUpdate()
{
Server_ChatChannel *channel = static_cast<Server_ChatChannel *>(sender());
Event_ChatListChannels *event = new Event_ChatListChannels;
Event_ListChatChannels *event = new Event_ListChatChannels;
event->addChannel(channel->getName(), channel->getDescription(), channel->size(), channel->getAutoJoin());
for (int i = 0; i < clients.size(); ++i)

View file

@ -16,7 +16,7 @@ void Server_ChatChannel::addClient(Server_ProtocolHandler *client)
eventCLP->addPlayer(at(i)->getPlayerName());
client->enqueueProtocolItem(eventCLP);
client->enqueueProtocolItem(new Event_ChatServerMessage(name, joinMessage));
client->enqueueProtocolItem(new Event_ChatSay(name, QString(), joinMessage));
emit channelInfoChanged();
}

View file

@ -18,6 +18,15 @@ Server_ProtocolHandler::Server_ProtocolHandler(Server *_server, QObject *parent)
Server_ProtocolHandler::~Server_ProtocolHandler()
{
// The socket has to be removed from the server's list before it is removed from the game's list
// so it will not receive the game update event.
server->removeClient(this);
// if (game)
// game->removePlayer(this);
QMapIterator<QString, Server_ChatChannel *> chatChannelIterator(chatChannels);
while (chatChannelIterator.hasNext())
chatChannelIterator.next().value()->removeClient(this);
}
void Server_ProtocolHandler::processCommand(Command *command)
@ -75,7 +84,7 @@ void Server_ProtocolHandler::processCommand(Command *command)
switch (command->getItemId()) {
case ItemId_Command_Ping: response = cmdPing(qobject_cast<Command_Ping *>(command)); break;
case ItemId_Command_Login: response = cmdLogin(qobject_cast<Command_Login *>(command)); break;
case ItemId_Command_ChatListChannels: response = cmdChatListChannels(qobject_cast<Command_ChatListChannels *>(command)); break;
case ItemId_Command_ListChatChannels: response = cmdListChatChannels(qobject_cast<Command_ListChatChannels *>(command)); break;
case ItemId_Command_ChatJoinChannel: response = cmdChatJoinChannel(qobject_cast<Command_ChatJoinChannel *>(command)); break;
case ItemId_Command_ListGames: response = cmdListGames(qobject_cast<Command_ListGames *>(command)); break;
case ItemId_Command_CreateGame: response = cmdCreateGame(qobject_cast<Command_CreateGame *>(command)); break;
@ -115,13 +124,13 @@ ResponseCode Server_ProtocolHandler::cmdLogin(Command_Login *cmd)
return RespWrongPassword;
playerName = cmd->getUsername();
enqueueProtocolItem(new Event_ChatServerMessage(QString(), server->getLoginMessage()));
enqueueProtocolItem(new Event_ServerMessage(server->getLoginMessage()));
return RespOk;
}
ResponseCode Server_ProtocolHandler::cmdChatListChannels(Command_ChatListChannels * /*cmd*/)
ResponseCode Server_ProtocolHandler::cmdListChatChannels(Command_ListChatChannels * /*cmd*/)
{
Event_ChatListChannels *event = new Event_ChatListChannels;
Event_ListChatChannels *event = new Event_ListChatChannels;
QMapIterator<QString, Server_ChatChannel *> channelIterator(server->getChatChannels());
while (channelIterator.hasNext()) {
Server_ChatChannel *c = channelIterator.next().value();

View file

@ -28,7 +28,7 @@ private:
ResponseCode cmdPing(Command_Ping *cmd);
ResponseCode cmdLogin(Command_Login *cmd);
ResponseCode cmdChatListChannels(Command_ChatListChannels *cmd);
ResponseCode cmdListChatChannels(Command_ListChatChannels *cmd);
ResponseCode cmdChatJoinChannel(Command_ChatJoinChannel *cmd);
ResponseCode cmdChatLeaveChannel(Command_ChatLeaveChannel *cmd, Server_ChatChannel *channel);
ResponseCode cmdChatSay(Command_ChatSay *cmd, Server_ChatChannel *channel);