mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-15 11:38:49 -07:00
initial commit for chat channels
This commit is contained in:
parent
0d84de2384
commit
947cd1736c
23 changed files with 408 additions and 66 deletions
39
servatrice/src/chatchannel.cpp
Normal file
39
servatrice/src/chatchannel.cpp
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#include "chatchannel.h"
|
||||
#include "serversocket.h"
|
||||
|
||||
ChatChannel::ChatChannel(const QString &_name, const QString &_description)
|
||||
: name(_name), description(_description)
|
||||
{
|
||||
}
|
||||
|
||||
void ChatChannel::addPlayer(ServerSocket *player)
|
||||
{
|
||||
QString str = QString("chat|join_channel|%1|%2").arg(name).arg(player->getPlayerName());
|
||||
for (int i = 0; i < size(); ++i)
|
||||
at(i)->msg(str);
|
||||
|
||||
append(player);
|
||||
|
||||
for (int i = 0; i < size(); ++i)
|
||||
player->msg(QString("chat|list_players|%1|%2").arg(name).arg(at(i)->getPlayerName()));
|
||||
|
||||
emit channelInfoChanged();
|
||||
}
|
||||
|
||||
void ChatChannel::removePlayer(ServerSocket *player)
|
||||
{
|
||||
QString str = QString("chat|leave_channel|%1|%2").arg(name).arg(player->getPlayerName());
|
||||
|
||||
removeAt(indexOf(player));
|
||||
for (int i = 0; i < size(); ++i)
|
||||
at(i)->msg(str);
|
||||
|
||||
emit channelInfoChanged();
|
||||
}
|
||||
|
||||
void ChatChannel::say(ServerSocket *player, const QString &s)
|
||||
{
|
||||
QString str = QString("chat|say|%1|%2|%3").arg(name).arg(player->getPlayerName()).arg(s);
|
||||
for (int i = 0; i < size(); ++i)
|
||||
at(i)->msg(str);
|
||||
}
|
||||
25
servatrice/src/chatchannel.h
Normal file
25
servatrice/src/chatchannel.h
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#ifndef CHATCHANNEL_H
|
||||
#define CHATCHANNEL_H
|
||||
|
||||
#include <QList>
|
||||
#include <QObject>
|
||||
|
||||
class ServerSocket;
|
||||
|
||||
class ChatChannel : public QObject, public QList<ServerSocket *> {
|
||||
Q_OBJECT
|
||||
signals:
|
||||
void channelInfoChanged();
|
||||
private:
|
||||
QString name;
|
||||
QString description;
|
||||
public:
|
||||
ChatChannel(const QString &_name, const QString &_description);
|
||||
QString getName() const { return name; }
|
||||
QString getDescription() const { return description; }
|
||||
void addPlayer(ServerSocket *player);
|
||||
void removePlayer(ServerSocket *player);
|
||||
void say(ServerSocket *player, const QString &s);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -10,6 +10,7 @@ bool ReturnMessage::send(ReturnCode code)
|
|||
switch (code) {
|
||||
case ReturnNothing: return true;
|
||||
case ReturnOk: returnCodeString = "ok"; break;
|
||||
case ReturnNameNotFound: returnCodeString = "name_not_found"; break;
|
||||
case ReturnLoginNeeded: returnCodeString = "login_needed"; break;
|
||||
case ReturnSyntaxError: returnCodeString = "syntax"; break;
|
||||
case ReturnContextError: returnCodeString = "context"; break;
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ private:
|
|||
unsigned int msg_id;
|
||||
QString cmd;
|
||||
public:
|
||||
enum ReturnCode { ReturnNothing, ReturnOk, ReturnLoginNeeded, ReturnSyntaxError, ReturnContextError, ReturnPasswordWrong };
|
||||
enum ReturnCode { ReturnNothing, ReturnOk, ReturnNameNotFound, ReturnLoginNeeded, ReturnSyntaxError, ReturnContextError, ReturnPasswordWrong };
|
||||
ReturnMessage(QObject *parent = 0) : QObject(parent), msg_id(0) { }
|
||||
unsigned int getMsgId() const { return msg_id; }
|
||||
void setMsgId(unsigned int _msg_id) { msg_id = _msg_id; }
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@
|
|||
#include "serversocket.h"
|
||||
#include "counter.h"
|
||||
#include "rng_qt.h"
|
||||
#include "chatchannel.h"
|
||||
#include <QtSql>
|
||||
#include <QSettings>
|
||||
|
||||
|
|
@ -35,6 +36,9 @@ Server::Server(QObject *parent)
|
|||
QString dbType = settings->value("database/type").toString();
|
||||
if (dbType == "mysql")
|
||||
openDatabase();
|
||||
|
||||
chatChannelList << new ChatChannel("channel1", "testchannel 1");
|
||||
chatChannelList << new ChatChannel("channel2", "testchannel 2");
|
||||
}
|
||||
|
||||
Server::~Server()
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ class ServerSocket;
|
|||
class QSqlDatabase;
|
||||
class QSettings;
|
||||
class AbstractRNG;
|
||||
class ChatChannel;
|
||||
|
||||
enum AuthenticationResult { PasswordWrong = 0, PasswordRight = 1, UnknownUser = 2 };
|
||||
|
||||
|
|
@ -45,6 +46,7 @@ public:
|
|||
bool checkGamePassword(int gameId, const QString &password);
|
||||
AuthenticationResult checkUserPassword(const QString &user, const QString &password);
|
||||
QList<ServerGame *> listOpenGames();
|
||||
QList<ChatChannel *> getChatChannelList() { return chatChannelList; }
|
||||
ServerGame *getGame(int gameId);
|
||||
AbstractRNG *getRNG() const { return rng; }
|
||||
void broadcastGameListUpdate(ServerGame *game);
|
||||
|
|
@ -53,6 +55,7 @@ private:
|
|||
void incomingConnection(int SocketId);
|
||||
QList<ServerGame *> games;
|
||||
QList<ServerSocket *> players;
|
||||
QList<ChatChannel *> chatChannelList;
|
||||
int nextGameId;
|
||||
AbstractRNG *rng;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@
|
|||
#include "counter.h"
|
||||
#include "card.h"
|
||||
#include "abstractrng.h"
|
||||
#include "chatchannel.h"
|
||||
|
||||
ServerSocket::ServerSocket(Server *_server, QObject *parent)
|
||||
: QTcpSocket(parent), server(_server), game(0), PlayerStatus(StatusNormal), authState(PasswordWrong), acceptsGameListChanges(false)
|
||||
|
|
@ -166,6 +167,11 @@ const ServerSocket::CommandProperties ServerSocket::commandList[ServerSocket::nu
|
|||
{"ping", false, false, false, QList<QVariant::Type>(), &ServerSocket::cmdPing},
|
||||
{"login", false, false, false, QList<QVariant::Type>() << QVariant::String
|
||||
<< QVariant::String, &ServerSocket::cmdLogin},
|
||||
{"chat_list_channels", true, false, false, QList<QVariant::Type>(), &ServerSocket::cmdChatListChannels},
|
||||
{"chat_join_channel", true, false, false, QList<QVariant::Type>() << QVariant::String, &ServerSocket::cmdChatJoinChannel},
|
||||
{"chat_leave_channel", true, false, false, QList<QVariant::Type>() << QVariant::String, &ServerSocket::cmdChatLeaveChannel},
|
||||
{"chat_say", true, false, false, QList<QVariant::Type>() << QVariant::String
|
||||
<< QVariant::String, &ServerSocket::cmdChatSay},
|
||||
{"list_games", true, false, false, QList<QVariant::Type>(), &ServerSocket::cmdListGames},
|
||||
{"create_game", true, false, false, QList<QVariant::Type>() << QVariant::String
|
||||
<< QVariant::String
|
||||
|
|
@ -231,6 +237,55 @@ ReturnMessage::ReturnCode ServerSocket::cmdLogin(const QList<QVariant> ¶ms)
|
|||
return ReturnMessage::ReturnOk;
|
||||
}
|
||||
|
||||
ReturnMessage::ReturnCode ServerSocket::cmdChatListChannels(const QList<QVariant> &/*params*/)
|
||||
{
|
||||
QList<ChatChannel *> chatChannelList = server->getChatChannelList();
|
||||
for (int i = 0; i < chatChannelList.size(); ++i)
|
||||
msg(QString("chat|list_channels|%1|%2|%3").arg(chatChannelList[i]->getName()).arg(chatChannelList[i]->getDescription()).arg(chatChannelList[i]->size()));
|
||||
|
||||
acceptsChatChannelListChanges = true;
|
||||
return ReturnMessage::ReturnOk;
|
||||
}
|
||||
|
||||
ReturnMessage::ReturnCode ServerSocket::cmdChatJoinChannel(const QList<QVariant> ¶ms)
|
||||
{
|
||||
for (int i = 0; i < chatChannels.size(); ++i)
|
||||
if (chatChannels[i]->getName() == params[0])
|
||||
return ReturnMessage::ReturnContextError;
|
||||
|
||||
QList<ChatChannel *> allChannels = server->getChatChannelList();
|
||||
for (int i = 0; i < allChannels.size(); ++i)
|
||||
if (allChannels[i]->getName() == params[0]) {
|
||||
allChannels[i]->addPlayer(this);
|
||||
chatChannels << allChannels[i];
|
||||
return ReturnMessage::ReturnOk;
|
||||
}
|
||||
return ReturnMessage::ReturnNameNotFound;
|
||||
}
|
||||
|
||||
ReturnMessage::ReturnCode ServerSocket::cmdChatLeaveChannel(const QList<QVariant> ¶ms)
|
||||
{
|
||||
for (int i = 0; i < chatChannels.size(); ++i) {
|
||||
ChatChannel *c = chatChannels[i];
|
||||
if (c->getName() == params[0]) {
|
||||
chatChannels.removeAt(i);
|
||||
c->removePlayer(this);
|
||||
return ReturnMessage::ReturnOk;
|
||||
}
|
||||
}
|
||||
return ReturnMessage::ReturnNameNotFound;
|
||||
}
|
||||
|
||||
ReturnMessage::ReturnCode ServerSocket::cmdChatSay(const QList<QVariant> ¶ms)
|
||||
{
|
||||
for (int i = 0; i < chatChannels.size(); ++i)
|
||||
if (chatChannels[i]->getName() == params[0]) {
|
||||
chatChannels[i]->say(this, params[1].toString());
|
||||
return ReturnMessage::ReturnOk;
|
||||
}
|
||||
return ReturnMessage::ReturnNameNotFound;
|
||||
}
|
||||
|
||||
ReturnMessage::ReturnCode ServerSocket::cmdListGames(const QList<QVariant> &/*params*/)
|
||||
{
|
||||
QList<ServerGame *> gameList = server->listOpenGames();
|
||||
|
|
|
|||
|
|
@ -55,11 +55,15 @@ private:
|
|||
QList<QVariant::Type> paramTypes;
|
||||
CommandHandler handler;
|
||||
};
|
||||
static const int numberCommands = 27;
|
||||
static const int numberCommands = 31;
|
||||
static const CommandProperties commandList[numberCommands];
|
||||
|
||||
ReturnMessage::ReturnCode cmdPing(const QList<QVariant> ¶ms);
|
||||
ReturnMessage::ReturnCode cmdLogin(const QList<QVariant> ¶ms);
|
||||
ReturnMessage::ReturnCode cmdChatListChannels(const QList<QVariant> ¶ms);
|
||||
ReturnMessage::ReturnCode cmdChatJoinChannel(const QList<QVariant> ¶ms);
|
||||
ReturnMessage::ReturnCode cmdChatLeaveChannel(const QList<QVariant> ¶ms);
|
||||
ReturnMessage::ReturnCode cmdChatSay(const QList<QVariant> ¶ms);
|
||||
ReturnMessage::ReturnCode cmdListGames(const QList<QVariant> ¶ms);
|
||||
ReturnMessage::ReturnCode cmdCreateGame(const QList<QVariant> ¶ms);
|
||||
ReturnMessage::ReturnCode cmdJoinGame(const QList<QVariant> ¶ms);
|
||||
|
|
@ -88,6 +92,7 @@ private:
|
|||
|
||||
Server *server;
|
||||
ServerGame *game;
|
||||
QList<ChatChannel *> chatChannels;
|
||||
QList<QString> DeckList;
|
||||
QList<QString> SideboardList;
|
||||
QList<PlayerZone *> zones;
|
||||
|
|
@ -105,6 +110,7 @@ private:
|
|||
ReturnMessage *remsg;
|
||||
AuthenticationResult authState;
|
||||
bool acceptsGameListChanges;
|
||||
bool acceptsChatChannelListChanges;
|
||||
public:
|
||||
ServerSocket(Server *_server, QObject *parent = 0);
|
||||
~ServerSocket();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue