mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
some changes
This commit is contained in:
parent
9b1adf2dc1
commit
c8a2ed9415
26 changed files with 243 additions and 613 deletions
|
|
@ -1,12 +1,31 @@
|
|||
#include <QTimer>
|
||||
#include "client.h"
|
||||
|
||||
PendingCommand::PendingCommand(const QString &_cmd, int _msgid, QObject *parent)
|
||||
: QObject(parent), cmd(_cmd), msgid(_msgid), time(0)
|
||||
{
|
||||
}
|
||||
|
||||
void PendingCommand::responseReceived(int _msgid, ServerResponse _resp)
|
||||
{
|
||||
if (_msgid == msgid) {
|
||||
emit finished(_resp);
|
||||
deleteLater();
|
||||
}
|
||||
}
|
||||
|
||||
void PendingCommand::checkTimeout()
|
||||
{
|
||||
if (++time > 5)
|
||||
emit timeout();
|
||||
}
|
||||
|
||||
Client::Client(QObject *parent)
|
||||
: QObject(parent), status(StatusDisconnected), MsgId(0)
|
||||
{
|
||||
timer = new QTimer(this);
|
||||
timer->setInterval(1000);
|
||||
connect(timer, SIGNAL(timeout()), this, SLOT(checkTimeout()));
|
||||
connect(timer, SIGNAL(timeout()), this, SLOT(ping()));
|
||||
|
||||
socket = new QTcpSocket(this);
|
||||
socket->setTextModeEnabled(true);
|
||||
|
|
@ -20,22 +39,10 @@ Client::~Client()
|
|||
disconnectFromServer();
|
||||
}
|
||||
|
||||
void Client::checkTimeout()
|
||||
void Client::timeout()
|
||||
{
|
||||
bool timeout = false;
|
||||
QListIterator<PendingCommand *> i(PendingCommands);
|
||||
while (i.hasNext()) {
|
||||
PendingCommand *c = i.next();
|
||||
if (c->timeout()) {
|
||||
timeout = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (timeout) {
|
||||
disconnectFromServer();
|
||||
emit serverTimeout();
|
||||
} else
|
||||
ping();
|
||||
emit serverTimeout();
|
||||
disconnectFromServer();
|
||||
}
|
||||
|
||||
void Client::slotSocketError(QAbstractSocket::SocketError /*error*/)
|
||||
|
|
@ -50,6 +57,33 @@ void Client::slotConnected()
|
|||
setStatus(StatusAwaitingWelcome);
|
||||
}
|
||||
|
||||
void Client::removePendingCommand()
|
||||
{
|
||||
PendingCommands.removeAt(PendingCommands.indexOf(static_cast<PendingCommand *>(sender())));
|
||||
}
|
||||
|
||||
void Client::loginResponse(ServerResponse response)
|
||||
{
|
||||
if (response == RespOk)
|
||||
setStatus(StatusIdle);
|
||||
else {
|
||||
emit serverError(response);
|
||||
disconnectFromServer();
|
||||
}
|
||||
}
|
||||
|
||||
void Client::enterGameResponse(ServerResponse response)
|
||||
{
|
||||
if (response == RespOk)
|
||||
setStatus(StatusPlaying);
|
||||
}
|
||||
|
||||
void Client::leaveGameResponse(ServerResponse response)
|
||||
{
|
||||
if (response == RespOk)
|
||||
setStatus(StatusIdle);
|
||||
}
|
||||
|
||||
void Client::readLine()
|
||||
{
|
||||
while (socket->canReadLine()) {
|
||||
|
|
@ -57,7 +91,7 @@ void Client::readLine()
|
|||
|
||||
if (line.isNull())
|
||||
break;
|
||||
qDebug(QString("readLine: %1").arg(line).toLatin1());
|
||||
qDebug(QString("<< %1").arg(line).toLatin1());
|
||||
QStringList values = line.split("|");
|
||||
QString prefix = values.takeFirst();
|
||||
// prefix is one of {welcome, private, public, resp, list_games, list_players, list_counters, list_zones, dump_zone}
|
||||
|
|
@ -87,29 +121,14 @@ void Client::readLine()
|
|||
// XXX
|
||||
}
|
||||
|
||||
ok = !values.takeFirst().compare("ok");
|
||||
|
||||
// XXX
|
||||
ServerErrorMessage message = msgNone;
|
||||
|
||||
// Update list of pending commands
|
||||
QListIterator<PendingCommand *> i(PendingCommands);
|
||||
bool found = false;
|
||||
PendingCommand *c;
|
||||
while (i.hasNext()) {
|
||||
c = i.next();
|
||||
if (c->getMsgId() == msgid) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found) {
|
||||
PendingCommands.removeAt(PendingCommands.indexOf(c));
|
||||
delete c;
|
||||
} else
|
||||
qDebug(QString("msgid unknown: %1").arg(msgid).toLatin1());
|
||||
|
||||
emit responseReceived(new ServerResponse(msgid, ok, message));
|
||||
ServerResponse resp;
|
||||
if (values[0] == "ok")
|
||||
resp = RespOk;
|
||||
else if (values[1] == "password")
|
||||
resp = RespPassword;
|
||||
else
|
||||
resp = RespErr;
|
||||
emit responseReceived(msgid, resp);
|
||||
} else if (prefix == "list_games") {
|
||||
emit gameListEvent(new ServerGame(values[0].toInt(), values[5], values[1], values[2].toInt(), values[3].toInt(), values[4].toInt()));
|
||||
} else if ((prefix == "list_players") || (prefix == "list_counters") || (prefix == "list_zones") || (prefix == "dump_zone") || (prefix == "welcome")) {
|
||||
|
|
@ -124,29 +143,29 @@ void Client::readLine()
|
|||
QStringList val = i.next();
|
||||
|
||||
// XXX Parametergültigkeit überprüfen
|
||||
if (!prefix.compare("list_players"))
|
||||
if (prefix == "list_players")
|
||||
playerlist << new ServerPlayer(val[0].toInt(), val[1]);
|
||||
else if (!prefix.compare("list_counters"))
|
||||
else if (prefix == "list_counters")
|
||||
{ }
|
||||
else if (!prefix.compare("list_zones"))
|
||||
else if (prefix == "list_zones")
|
||||
zonelist << new ServerZone(val[0], val[1] == "1", val[2] == "1", val[3].toInt());
|
||||
else if (!prefix.compare("dump_zone"))
|
||||
else if (prefix == "dump_zone")
|
||||
zonedump << new ServerZoneCard(val[0].toInt(), val[1], val[2].toInt(), val[3].toInt(), val[4].toInt(), val[5] == "1", val[6]);
|
||||
else if (!prefix.compare("welcome"))
|
||||
else if (prefix == "welcome")
|
||||
welcomemsg << val[0];
|
||||
}
|
||||
if (!prefix.compare("list_players"))
|
||||
if (prefix == "list_players")
|
||||
emit playerListReceived(playerlist);
|
||||
else if (!prefix.compare("list_counters"))
|
||||
else if (prefix == "list_counters")
|
||||
{ }
|
||||
else if (!prefix.compare("list_zones"))
|
||||
else if (prefix == "list_zones")
|
||||
emit zoneListReceived(cmdid, zonelist);
|
||||
else if (!prefix.compare("dump_zone"))
|
||||
else if (prefix == "dump_zone")
|
||||
emit zoneDumpReceived(cmdid, zonedump);
|
||||
else if (!prefix.compare("welcome")) {
|
||||
else if (prefix == "welcome") {
|
||||
emit welcomeMsgReceived(welcomemsg);
|
||||
setStatus(StatusConnected);
|
||||
login(PlayerName, password);
|
||||
setStatus(StatusLoggingIn);
|
||||
login(playerName, password);
|
||||
}
|
||||
msgbuf.clear();
|
||||
} else
|
||||
|
|
@ -165,23 +184,27 @@ void Client::setStatus(const ProtocolStatus _status)
|
|||
|
||||
void Client::msg(const QString &s)
|
||||
{
|
||||
qDebug(QString("msg gibt aus: %1").arg(s).toLatin1());
|
||||
qDebug(QString(">> %1").arg(s).toLatin1());
|
||||
QTextStream stream(socket);
|
||||
stream.setCodec("UTF-8");
|
||||
stream << s << endl;
|
||||
stream.flush();
|
||||
}
|
||||
|
||||
int Client::cmd(const QString &s)
|
||||
PendingCommand *Client::cmd(const QString &s)
|
||||
{
|
||||
msg(QString("%1|%2").arg(++MsgId).arg(s));
|
||||
PendingCommands << new PendingCommand(s, MsgId);
|
||||
return MsgId;
|
||||
PendingCommand *pc = new PendingCommand(s, MsgId, this);
|
||||
PendingCommands << pc;
|
||||
connect(this, SIGNAL(responseReceived(int, ServerResponse)), pc, SLOT(responseReceived(int, ServerResponse)));
|
||||
connect(pc, SIGNAL(finished(ServerResponse)), this, SLOT(removePendingCommand()));
|
||||
connect(timer, SIGNAL(timeout()), pc, SLOT(checkTimeout()));
|
||||
return pc;
|
||||
}
|
||||
|
||||
void Client::connectToServer(const QString &hostname, unsigned int port, const QString &playername, const QString &_password)
|
||||
void Client::connectToServer(const QString &hostname, unsigned int port, const QString &_playerName, const QString &_password)
|
||||
{
|
||||
PlayerName = playername;
|
||||
playerName = _playerName;
|
||||
password = _password;
|
||||
socket->connectToHost(hostname, port);
|
||||
setStatus(StatusConnecting);
|
||||
|
|
@ -199,74 +222,82 @@ void Client::disconnectFromServer()
|
|||
socket->close();
|
||||
}
|
||||
|
||||
int Client::ping()
|
||||
void Client::ping()
|
||||
{
|
||||
return cmd("ping");
|
||||
cmd("ping");
|
||||
}
|
||||
|
||||
int Client::listGames()
|
||||
PendingCommand *Client::listGames()
|
||||
{
|
||||
return cmd("list_games");
|
||||
}
|
||||
|
||||
int Client::listPlayers()
|
||||
PendingCommand *Client::listPlayers()
|
||||
{
|
||||
return cmd("list_players");
|
||||
}
|
||||
|
||||
int Client::createGame(const QString &description, const QString &password, unsigned int maxPlayers)
|
||||
PendingCommand *Client::createGame(const QString &description, const QString &password, unsigned int maxPlayers)
|
||||
{
|
||||
return cmd(QString("create_game|%1|%2|%3").arg(description).arg(password).arg(maxPlayers));
|
||||
PendingCommand *pc = cmd(QString("create_game|%1|%2|%3").arg(description).arg(password).arg(maxPlayers));
|
||||
connect(pc, SIGNAL(finished(ServerResponse)), this, SLOT(enterGameResponse(ServerResponse)));
|
||||
return pc;
|
||||
}
|
||||
|
||||
int Client::joinGame(int gameId, const QString &password)
|
||||
PendingCommand *Client::joinGame(int gameId, const QString &password)
|
||||
{
|
||||
return cmd(QString("join_game|%1|%2").arg(gameId).arg(password));
|
||||
PendingCommand *pc = cmd(QString("join_game|%1|%2").arg(gameId).arg(password));
|
||||
connect(pc, SIGNAL(finished(ServerResponse)), this, SLOT(enterGameResponse(ServerResponse)));
|
||||
return pc;
|
||||
}
|
||||
|
||||
int Client::leaveGame()
|
||||
PendingCommand *Client::leaveGame()
|
||||
{
|
||||
return cmd("leave_game");
|
||||
PendingCommand *pc = cmd("leave_game");
|
||||
connect(pc, SIGNAL(finished(ServerResponse)), this, SLOT(leaveGameResponse(ServerResponse)));
|
||||
return pc;
|
||||
}
|
||||
|
||||
int Client::login(const QString &name, const QString &pass)
|
||||
PendingCommand *Client::login(const QString &name, const QString &pass)
|
||||
{
|
||||
return cmd(QString("login|%1|%2").arg(name).arg(pass));
|
||||
PendingCommand *pc = cmd(QString("login|%1|%2").arg(name).arg(pass));
|
||||
connect(pc, SIGNAL(finished(ServerResponse)), this, SLOT(loginResponse(ServerResponse)));
|
||||
return pc;
|
||||
}
|
||||
|
||||
int Client::say(const QString &s)
|
||||
PendingCommand *Client::say(const QString &s)
|
||||
{
|
||||
return cmd(QString("say|%1").arg(s));
|
||||
}
|
||||
|
||||
int Client::shuffle()
|
||||
PendingCommand *Client::shuffle()
|
||||
{
|
||||
return cmd("shuffle");
|
||||
}
|
||||
|
||||
int Client::rollDice(unsigned int sides)
|
||||
PendingCommand *Client::rollDice(unsigned int sides)
|
||||
{
|
||||
return cmd(QString("roll_dice|%1").arg(sides));
|
||||
}
|
||||
|
||||
int Client::drawCards(unsigned int number)
|
||||
PendingCommand *Client::drawCards(unsigned int number)
|
||||
{
|
||||
return cmd(QString("draw_cards|%1").arg(number));
|
||||
}
|
||||
|
||||
int Client::moveCard(int cardid, const QString &startzone, const QString &targetzone, int x, int y, bool faceDown)
|
||||
PendingCommand *Client::moveCard(int cardid, const QString &startzone, const QString &targetzone, int x, int y, bool faceDown)
|
||||
{
|
||||
// if startzone is public: cardid is the card's id
|
||||
// else: cardid is the position of the card in the zone (e.g. deck)
|
||||
return cmd(QString("move_card|%1|%2|%3|%4|%5|%6").arg(cardid).arg(startzone).arg(targetzone).arg(x).arg(y).arg(faceDown ? 1 : 0));
|
||||
}
|
||||
|
||||
int Client::createToken(const QString &zone, const QString &name, const QString &powtough, int x, int y)
|
||||
PendingCommand *Client::createToken(const QString &zone, const QString &name, const QString &powtough, int x, int y)
|
||||
{
|
||||
return cmd(QString("create_token|%1|%2|%3|%4|%5").arg(zone).arg(name).arg(powtough).arg(x).arg(y));
|
||||
}
|
||||
|
||||
int Client::setCardAttr(const QString &zone, int cardid, const QString &aname, const QString &avalue)
|
||||
PendingCommand *Client::setCardAttr(const QString &zone, int cardid, const QString &aname, const QString &avalue)
|
||||
{
|
||||
return cmd(QString("set_card_attr|%1|%2|%3|%4").arg(zone).arg(cardid).arg(aname).arg(avalue));
|
||||
}
|
||||
|
|
@ -280,42 +311,42 @@ void Client::submitDeck(const QStringList &deck)
|
|||
msg(".");
|
||||
}
|
||||
|
||||
int Client::readyStart()
|
||||
PendingCommand *Client::readyStart()
|
||||
{
|
||||
return cmd("ready_start");
|
||||
}
|
||||
|
||||
int Client::incCounter(const QString &counter, int delta)
|
||||
PendingCommand *Client::incCounter(const QString &counter, int delta)
|
||||
{
|
||||
return cmd(QString("inc_counter|%1|%2").arg(counter).arg(delta));
|
||||
}
|
||||
|
||||
int Client::addCounter(const QString &counter, QColor color, int value)
|
||||
PendingCommand *Client::addCounter(const QString &counter, QColor color, int value)
|
||||
{
|
||||
return cmd(QString("add_counter|%1|%2|%3").arg(counter).arg(color.red() * 65536 + color.green() * 256 + color.blue()).arg(value));
|
||||
}
|
||||
|
||||
int Client::setCounter(const QString &counter, int value)
|
||||
PendingCommand *Client::setCounter(const QString &counter, int value)
|
||||
{
|
||||
return cmd(QString("set_counter|%1|%2").arg(counter).arg(value));
|
||||
}
|
||||
|
||||
int Client::delCounter(const QString &counter)
|
||||
PendingCommand *Client::delCounter(const QString &counter)
|
||||
{
|
||||
return cmd(QString("del_counter|%1").arg(counter));
|
||||
}
|
||||
|
||||
int Client::setActivePlayer(int player)
|
||||
PendingCommand *Client::setActivePlayer(int player)
|
||||
{
|
||||
return cmd(QString("set_active_player|%1").arg(player));
|
||||
}
|
||||
|
||||
int Client::setActivePhase(int phase)
|
||||
PendingCommand *Client::setActivePhase(int phase)
|
||||
{
|
||||
return cmd(QString("set_active_phase|%1").arg(phase));
|
||||
}
|
||||
|
||||
int Client::dumpZone(int player, const QString &zone, int numberCards)
|
||||
PendingCommand *Client::dumpZone(int player, const QString &zone, int numberCards)
|
||||
{
|
||||
return cmd(QString("dump_zone|%1|%2|%3").arg(player).arg(zone).arg(numberCards));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue