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

boom

parent d329376e
[Dolphin]
Timestamp=2009,10,26,13,45,44
ViewMode=1
######################################################################
# Automatically generated by qmake (2.01a) So. Okt 25 12:02:12 2009
######################################################################
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
MOC_DIR = build
OBJECTS_DIR = build
# Input
HEADERS += protocol.h widget.h protocol_items.h
SOURCES += main.cpp protocol.cpp widget.cpp protocol_items.cpp
CONFIG += qt debug
#include <QApplication>
#include <QTextCodec>
#include "widget.h"
int main(int argc, char *argv[])
{
// qInstallMsgHandler(myMessageOutput);
QApplication app(argc, argv);
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
Widget *widget = new Widget;
widget->show();
return app.exec();
}
...@@ -63,6 +63,9 @@ ProtocolItem *ProtocolItem::getNewItem(const QString &name) ...@@ -63,6 +63,9 @@ ProtocolItem *ProtocolItem::getNewItem(const QString &name)
void ProtocolItem::initializeHash() void ProtocolItem::initializeHash()
{ {
if (!itemNameHash.isEmpty())
return;
initializeHashAuto(); initializeHashAuto();
itemNameHash.insert("resp", ProtocolResponse::newItem); itemNameHash.insert("resp", ProtocolResponse::newItem);
ProtocolResponse::initializeHash(); ProtocolResponse::initializeHash();
......
...@@ -28,6 +28,7 @@ protected: ...@@ -28,6 +28,7 @@ protected:
private: private:
static void initializeHashAuto(); static void initializeHashAuto();
public: public:
static const int protocolVersion = 4;
ProtocolItem(const QString &_itemName); ProtocolItem(const QString &_itemName);
static void initializeHash(); static void initializeHash();
static ProtocolItem *getNewItem(const QString &name); static ProtocolItem *getNewItem(const QString &name);
...@@ -36,6 +37,7 @@ public: ...@@ -36,6 +37,7 @@ public:
}; };
class Command : public ProtocolItem { class Command : public ProtocolItem {
Q_OBJECT
private: private:
int cmdId; int cmdId;
static int lastCmdId; static int lastCmdId;
......
#include "rng_abstract.h"
#include "rng_qt.h"
RNG_Abstract *rng = new RNG_Qt;
#ifndef ABSTRACTRNG_H #ifndef RNG_ABSTRACT_H
#define ABSTRACTRNG_H #define RNG_ABSTRACT_H
#include <QObject> #include <QObject>
class AbstractRNG : public QObject { class RNG_Abstract : public QObject {
Q_OBJECT Q_OBJECT
public: public:
AbstractRNG(QObject *parent = 0) : QObject(parent) { } RNG_Abstract(QObject *parent = 0) : QObject(parent) { }
virtual unsigned int getNumber(unsigned int min, unsigned int max) = 0; virtual unsigned int getNumber(unsigned int min, unsigned int max) = 0;
}; };
extern AbstractRNG *rng; extern RNG_Abstract *rng;
#endif #endif
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
RNG_Qt::RNG_Qt(QObject *parent) RNG_Qt::RNG_Qt(QObject *parent)
: AbstractRNG(parent) : RNG_Abstract(parent)
{ {
int seed = QDateTime::currentDateTime().toTime_t(); int seed = QDateTime::currentDateTime().toTime_t();
qDebug(QString("qsrand(%1)").arg(seed).toLatin1()); qDebug(QString("qsrand(%1)").arg(seed).toLatin1());
......
#ifndef RNG_QT_H #ifndef RNG_QT_H
#define RNG_QT_H #define RNG_QT_H
#include "abstractrng.h" #include "rng_abstract.h"
class RNG_Qt : public AbstractRNG { class RNG_Qt : public RNG_Abstract {
Q_OBJECT Q_OBJECT
public: public:
RNG_Qt(QObject *parent = 0); RNG_Qt(QObject *parent = 0);
......
/***************************************************************************
* Copyright (C) 2008 by Max-Wilhelm Bruker *
* brukie@laptop *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "server.h"
#include "server_game.h"
#include "server_counter.h"
#include "server_chatchannel.h"
Server::Server(QObject *parent)
: QObject(parent), nextGameId(0)
{
}
Server::~Server()
{
}
Server_Game *Server::createGame(const QString &description, const QString &password, int maxPlayers, bool spectatorsAllowed, const QString &creator)
{
Server_Game *newGame = new Server_Game(creator, nextGameId++, description, password, maxPlayers, spectatorsAllowed, this);
games.insert(newGame->getGameId(), newGame);
connect(newGame, SIGNAL(gameClosing()), this, SLOT(gameClosing()));
broadcastGameListUpdate(newGame);
return newGame;
}
void Server::addClient(Server_ProtocolHandler *client)
{
clients << client;
}
void Server::removeClient(Server_ProtocolHandler *client)
{
clients.removeAt(clients.indexOf(client));
qDebug(QString("Server::removeClient: %1 clients left").arg(clients.size()).toLatin1());
}
Server_Game *Server::getGame(int gameId) const
{
return games.value(gameId);
}
void Server::broadcastGameListUpdate(Server_Game *game)
{
/* QString line = game->getGameListLine();
for (int i = 0; i < clients.size(); i++)
if (clients[i]->getAcceptsGameListChanges())
clients[i]->msg(line);
*/}
void Server::broadcastChannelUpdate()
{
/* QString line = qobject_cast<Server_ChatChannel *>(sender())->getChannelListLine();
for (int i = 0; i < players.size(); ++i)
if (players[i]->getAcceptsChatChannelListChanges())
players[i]->msg(line);
*/}
void Server::gameClosing()
{
qDebug("Server::gameClosing");
Server_Game *game = static_cast<Server_Game *>(sender());
broadcastGameListUpdate(game);
games.remove(games.key(game));
}
void Server::addChatChannel(Server_ChatChannel *newChannel)
{
chatChannels.insert(newChannel->getName(), newChannel);
connect(newChannel, SIGNAL(channelInfoChanged()), this, SLOT(broadcastChannelUpdate()));
}
#ifndef SERVER_H
#define SERVER_H
#include <QObject>
#include <QStringList>
#include <QMap>
class Server_Game;
class Server_ChatChannel;
class Server_ProtocolHandler;
enum AuthenticationResult { PasswordWrong = 0, PasswordRight = 1, UnknownUser = 2 };
class Server : public QObject
{
Q_OBJECT
private slots:
void gameClosing();
void broadcastChannelUpdate();
public:
Server(QObject *parent = 0);
~Server();
virtual AuthenticationResult checkUserPassword(const QString &user, const QString &password) = 0;
QList<Server_Game *> getGames() const { return games.values(); }
Server_Game *getGame(int gameId) const;
const QMap<QString, Server_ChatChannel *> &getChatChannels() { return chatChannels; }
void broadcastGameListUpdate(Server_Game *game);
void addClient(Server_ProtocolHandler *player);
void removeClient(Server_ProtocolHandler *player);
virtual QStringList getLoginMessage() const = 0;
Server_Game *createGame(const QString &description, const QString &password, int maxPlayers, bool spectatorsAllowed, const QString &playerName);
private:
QMap<int, Server_Game *> games;
QList<Server_ProtocolHandler *> clients;
QMap<QString, Server_ChatChannel *> chatChannels;
protected:
int nextGameId;
void addChatChannel(Server_ChatChannel *newChannel);
};
#endif
#ifndef ARROW_H #ifndef SERVER_ARROW_H
#define ARROW_H #define SERVER_ARROW_H
class Card; class Server_Card;
class Arrow { class Server_Arrow {
private: private:
int id; int id;
Card *startCard, *targetCard; Server_Card *startCard, *targetCard;
int color; int color;
public: public:
Arrow(int _id, Card *_startCard, Card *_targetCard, int _color) Server_Arrow(int _id, Server_Card *_startCard, Server_Card *_targetCard, int _color)
: id(_id), startCard(_startCard), targetCard(_targetCard), color(_color) { } : id(_id), startCard(_startCard), targetCard(_targetCard), color(_color) { }
int getId() const { return id; } int getId() const { return id; }
Card *getStartCard() const { return startCard; } Server_Card *getStartCard() const { return startCard; }
Card *getTargetCard() const { return targetCard; } Server_Card *getTargetCard() const { return targetCard; }
int getColor() const { return color; } int getColor() const { return color; }
}; };
......
...@@ -17,19 +17,19 @@ ...@@ -17,19 +17,19 @@
* Free Software Foundation, Inc., * * Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/ ***************************************************************************/
#include "card.h" #include "server_card.h"
Card::Card(QString _name, int _id, int _coord_x, int _coord_y) Server_Card::Server_Card(QString _name, int _id, int _coord_x, int _coord_y)
: id(_id), coord_x(_coord_x), coord_y(_coord_y), name(_name), counters(0), tapped(false), attacking(false), facedown(false), annotation(QString()), doesntUntap(false) : id(_id), coord_x(_coord_x), coord_y(_coord_y), name(_name), counters(0), tapped(false), attacking(false), facedown(false), annotation(QString()), doesntUntap(false)
{ {
} }
Card::~Card() Server_Card::~Server_Card()
{ {
} }
void Card::resetState() void Server_Card::resetState()
{ {
setCoords(0, 0); setCoords(0, 0);
setCounters(0); setCounters(0);
...@@ -40,7 +40,7 @@ void Card::resetState() ...@@ -40,7 +40,7 @@ void Card::resetState()
setDoesntUntap(false); setDoesntUntap(false);
} }
bool Card::setAttribute(const QString &aname, const QString &avalue, bool allCards) bool Server_Card::setAttribute(const QString &aname, const QString &avalue, bool allCards)
{ {
if (aname == "counters") { if (aname == "counters") {
bool ok; bool ok;
......
...@@ -17,16 +17,16 @@ ...@@ -17,16 +17,16 @@
* Free Software Foundation, Inc., * * Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/ ***************************************************************************/
#ifndef CARD_H #ifndef SERVER_CARD_H
#define CARD_H #define SERVER_CARD_H
#include <QString> #include <QString>
class PlayerZone; class Server_CardZone;
class Card { class Server_Card {
private: private:
PlayerZone *zone; Server_CardZone *zone;
int id; int id;
int coord_x, coord_y; int coord_x, coord_y;
QString name; QString name;
...@@ -37,11 +37,11 @@ private: ...@@ -37,11 +37,11 @@ private:
QString annotation; QString annotation;
bool doesntUntap; bool doesntUntap;
public: public:
Card(QString _name, int _id, int _coord_x, int _coord_y); Server_Card(QString _name, int _id, int _coord_x, int _coord_y);
~Card(); ~Server_Card();
PlayerZone *getZone() const { return zone; } Server_CardZone *getZone() const { return zone; }
void setZone(PlayerZone *_zone) { zone = _zone; } void setZone(Server_CardZone *_zone) { zone = _zone; }
int getId() const { return id; } int getId() const { return id; }
int getX() const { return coord_x; } int getX() const { return coord_x; }
......
...@@ -17,36 +17,36 @@ ...@@ -17,36 +17,36 @@
* Free Software Foundation, Inc., * * Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/ ***************************************************************************/
#include "playerzone.h" #include "server_cardzone.h"
#include "abstractrng.h" #include "server_card.h"
#include "card.h" #include "rng_abstract.h"
PlayerZone::PlayerZone(Player *_player, const QString &_name, bool _has_coords, ZoneType _type) Server_CardZone::Server_CardZone(Server_Player *_player, const QString &_name, bool _has_coords, ZoneType _type)
: player(_player), name(_name), has_coords(_has_coords), type(_type), cardsBeingLookedAt(0) : player(_player), name(_name), has_coords(_has_coords), type(_type), cardsBeingLookedAt(0)
{ {
} }
PlayerZone::~PlayerZone() Server_CardZone::~Server_CardZone()
{ {
qDebug(QString("PlayerZone destructor: %1").arg(name).toLatin1()); qDebug(QString("Server_CardZone destructor: %1").arg(name).toLatin1());
clear(); clear();
} }
void PlayerZone::shuffle() void Server_CardZone::shuffle()
{ {
QList<Card *> temp; QList<Server_Card *> temp;
for (int i = cards.size(); i; i--) for (int i = cards.size(); i; i--)
temp.append(cards.takeAt(rng->getNumber(0, i - 1))); temp.append(cards.takeAt(rng->getNumber(0, i - 1)));
cards = temp; cards = temp;
} }
Card *PlayerZone::getCard(int id, bool remove, int *position) Server_Card *Server_CardZone::getCard(int id, bool remove, int *position)
{ {
if (type != HiddenZone) { if (type != HiddenZone) {
QListIterator<Card *> CardIterator(cards); QListIterator<Server_Card *> CardIterator(cards);
int i = 0; int i = 0;
while (CardIterator.hasNext()) { while (CardIterator.hasNext()) {
Card *tmp = CardIterator.next(); Server_Card *tmp = CardIterator.next();
if (tmp->getId() == id) { if (tmp->getId() == id) {
if (remove) { if (remove) {
cards.removeAt(i); cards.removeAt(i);
...@@ -62,7 +62,7 @@ Card *PlayerZone::getCard(int id, bool remove, int *position) ...@@ -62,7 +62,7 @@ Card *PlayerZone::getCard(int id, bool remove, int *position)
} else { } else {
if ((id >= cards.size()) || (id < 0)) if ((id >= cards.size()) || (id < 0))
return NULL; return NULL;
Card *tmp = cards[id]; Server_Card *tmp = cards[id];
if (remove) { if (remove) {
cards.removeAt(id); cards.removeAt(id);
tmp->setZone(0); tmp->setZone(0);
...@@ -73,7 +73,7 @@ Card *PlayerZone::getCard(int id, bool remove, int *position) ...@@ -73,7 +73,7 @@ Card *PlayerZone::getCard(int id, bool remove, int *position)
} }
} }
void PlayerZone::insertCard(Card *card, int x, int y) void Server_CardZone::insertCard(Server_Card *card, int x, int y)
{ {
if (hasCoords()) { if (hasCoords()) {
card->setCoords(x, y); card->setCoords(x, y);
...@@ -85,7 +85,7 @@ void PlayerZone::insertCard(Card *card, int x, int y) ...@@ -85,7 +85,7 @@ void PlayerZone::insertCard(Card *card, int x, int y)
card->setZone(this); card->setZone(this);
} }
void PlayerZone::clear() void Server_CardZone::clear()
{ {
for (int i = 0; i < cards.size(); i++) for (int i = 0; i < cards.size(); i++)
delete cards.at(i); delete cards.at(i);
......
...@@ -17,17 +17,16 @@ ...@@ -17,17 +17,16 @@
* Free Software Foundation, Inc., * * Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/ ***************************************************************************/
#ifndef PLAYERZONE_H #ifndef SERVER_CARDZONE_H
#define PLAYERZONE_H #define SERVER_CARDZONE_H
#include <QList> #include <QList>
#include <QString> #include <QString>
class Card; class Server_Card;
class ServerSocket; class Server_Player;
class Player;
class PlayerZone { class Server_CardZone {
public: public:
// PrivateZone: Contents of the zone are always visible to the owner, // PrivateZone: Contents of the zone are always visible to the owner,
// but not to anyone else. // but not to anyone else.
...@@ -39,26 +38,26 @@ public: ...@@ -39,26 +38,26 @@ public:
// list index, whereas cards in any other zone are referenced by their ids. // list index, whereas cards in any other zone are referenced by their ids.
enum ZoneType { PrivateZone, PublicZone, HiddenZone }; enum ZoneType { PrivateZone, PublicZone, HiddenZone };
private: private:
Player *player; Server_Player *player;
QString name; QString name;
bool has_coords; bool has_coords;
ZoneType type; ZoneType type;
int cardsBeingLookedAt; int cardsBeingLookedAt;
public: public:
PlayerZone(Player *_player, const QString &_name, bool _has_coords, ZoneType _type); Server_CardZone(Server_Player *_player, const QString &_name, bool _has_coords, ZoneType _type);
~PlayerZone(); ~Server_CardZone();
Card *getCard(int id, bool remove, int *position = NULL); Server_Card *getCard(int id, bool remove, int *position = NULL);
int getCardsBeingLookedAt() const { return cardsBeingLookedAt; } int getCardsBeingLookedAt() const { return cardsBeingLookedAt; }
void setCardsBeingLookedAt(int _cardsBeingLookedAt) { cardsBeingLookedAt = _cardsBeingLookedAt; } void setCardsBeingLookedAt(int _cardsBeingLookedAt) { cardsBeingLookedAt = _cardsBeingLookedAt; }
bool hasCoords() const { return has_coords; } bool hasCoords() const { return has_coords; }
ZoneType getType() const { return type; } ZoneType getType() const { return type; }
QString getName() const { return name; } QString getName() const { return name; }
Player *getPlayer() const { return player; } Server_Player *getPlayer() const { return player; }
QList<Card *> cards; QList<Server_Card *> cards;
void insertCard(Card *card, int x, int y); void insertCard(Server_Card *card, int x, int y);
void shuffle(); void shuffle();
void clear(); void clear();
}; };
......
#include "chatchannel.h" #include "server_chatchannel.h"
#include "serversocket.h" #include "server_protocolhandler.h"
ChatChannel::ChatChannel(const QString &_name, const QString &_description, bool _autoJoin, const QStringList &_joinMessage) Server_ChatChannel::Server_ChatChannel(const QString &_name, const QString &_description, bool _autoJoin, const QStringList &_joinMessage)
: name(_name), description(_description), autoJoin(_autoJoin), joinMessage(_joinMessage) : name(_name), description(_description), autoJoin(_autoJoin), joinMessage(_joinMessage)
{ {
} }
void ChatChannel::addPlayer(ServerSocket *player) void Server_ChatChannel::addClient(Server_ProtocolHandler *client)
{ {
QString str = QString("chat|join_channel|%1|%2").arg(name).arg(player->getPlayerName()); /* QString str = QString("chat|join_channel|%1|%2").arg(name).arg(player->getPlayerName());
for (int i = 0; i < size(); ++i) for (int i = 0; i < size(); ++i)
at(i)->msg(str); at(i)->msg(str);
...@@ -20,27 +20,27 @@ void ChatChannel::addPlayer(ServerSocket *player) ...@@ -20,27 +20,27 @@ void ChatChannel::addPlayer(ServerSocket *player)
player->msg(QString("chat|server_message|%1|%2").arg(name).arg(joinMessage[i])); player->msg(QString("chat|server_message|%1|%2").arg(name).arg(joinMessage[i]));
emit channelInfoChanged(); emit channelInfoChanged();
} */}
void ChatChannel::removePlayer(ServerSocket *player) void Server_ChatChannel::removeClient(Server_ProtocolHandler *client)
{ {
QString str = QString("chat|leave_channel|%1|%2").arg(name).arg(player->getPlayerName()); /* QString str = QString("chat|leave_channel|%1|%2").arg(name).arg(player->getPlayerName());
removeAt(indexOf(player)); removeAt(indexOf(player));
for (int i = 0; i < size(); ++i) for (int i = 0; i < size(); ++i)
at(i)->msg(str); at(i)->msg(str);
emit channelInfoChanged(); emit channelInfoChanged();
} */}
void ChatChannel::say(ServerSocket *player, const QString &s) void Server_ChatChannel::say(Server_ProtocolHandler *client, const QString &s)
{ {
QString str = QString("chat|say|%1|%2|%3").arg(name).arg(player->getPlayerName()).arg(s); /* QString str = QString("chat|say|%1|%2|%3").arg(name).arg(player->getPlayerName()).arg(s);
for (int i = 0; i < size(); ++i) for (int i = 0; i < size(); ++i)
at(i)->msg(str); at(i)->msg(str);
} */}
QString ChatChannel::getChannelListLine() const QString Server_ChatChannel::getChannelListLine() const
{ {
return QString("chat|list_channels|%1|%2|%3|%4").arg(name).arg(description).arg(size()).arg(autoJoin ? 1 : 0); // return QString("chat|list_channels|%1|%2|%3|%4").arg(name).arg(description).arg(size()).arg(autoJoin ? 1 : 0);
} }
...@@ -5,9 +5,9 @@ ...@@ -5,9 +5,9 @@
#include <QObject> #include <QObject>
#include <QStringList> #include <QStringList>
class ServerSocket; class Server_ProtocolHandler;
class ChatChannel : public QObject, public QList<ServerSocket *> { class Server_ChatChannel : public QObject, public QList<Server_ProtocolHandler *> {
Q_OBJECT Q_OBJECT
signals: signals:
void channelInfoChanged(); void channelInfoChanged();
...@@ -17,13 +17,13 @@ private: ...@@ -17,13 +17,13 @@ private:
bool autoJoin; bool autoJoin;
QStringList joinMessage; QStringList joinMessage;
public: public:
ChatChannel(const QString &_name, const QString &_description, bool _autoJoin, const QStringList &_joinMessage); Server_ChatChannel(const QString &_name, const QString &_description, bool _autoJoin, const QStringList &_joinMessage);
QString getName() const { return name; } QString getName() const { return name; }
QString getDescription() const { return description; } QString getDescription() const { return description; }
bool getAutoJoin() const { return autoJoin; } bool getAutoJoin() const { return autoJoin; }
void addPlayer(ServerSocket *player); void addClient(Server_ProtocolHandler *client);
void removePlayer(ServerSocket *player); void removeClient(Server_ProtocolHandler *client);
void say(ServerSocket *player, const QString &s); void say(Server_ProtocolHandler *client, const QString &s);
QString getChannelListLine() const; QString getChannelListLine() const;
}; };
......
...@@ -17,12 +17,12 @@ ...@@ -17,12 +17,12 @@
* Free Software Foundation, Inc., * * Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/ ***************************************************************************/
#ifndef COUNTER_H #ifndef SERVER_COUNTER_H
#define COUNTER_H #define SERVER_COUNTER_H
#include <QString> #include <QString>
class Counter { class Server_Counter {
protected: protected:
int id; int id;
QString name; QString name;
...@@ -30,8 +30,8 @@ protected: ...@@ -30,8 +30,8 @@ protected:
int radius; int radius;
int count; int count;
public: public:
Counter(int _id, const QString &_name, int _color, int _radius, int _count = 0) : id(_id), name(_name), color(_color), radius(_radius), count(_count) { } Server_Counter(int _id, const QString &_name, int _color, int _radius, int _count = 0) : id(_id), name(_name), color(_color), radius(_radius), count(_count) { }
~Counter() { } ~Server_Counter() { }
int getId() const { return id; } int getId() const { return id; }
QString getName() const { return name; } QString getName() const { return name; }
int getColor() const { return color; } int getColor() const { return color; }
......
...@@ -18,22 +18,22 @@ ...@@ -18,22 +18,22 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/ ***************************************************************************/
#include "server.h" #include "server.h"
#include "servergame.h" #include "server_game.h"
#include "serversocket.h" #include "server_protocolhandler.h"
#include "arrow.h" #include "server_arrow.h"
#include <QSqlQuery> #include <QSqlQuery>
ServerGame::ServerGame(const QString &_creator, int _gameId, const QString &_description, const QString &_password, int _maxPlayers, bool _spectatorsAllowed, QObject *parent) Server_Game::Server_Game(const QString &_creator, int _gameId, const QString &_description, const QString &_password, int _maxPlayers, bool _spectatorsAllowed, QObject *parent)
: QObject(parent), gameStarted(false), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers), spectatorsAllowed(_spectatorsAllowed) : QObject(parent), gameStarted(false), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers), spectatorsAllowed(_spectatorsAllowed)
{ {
creator = addPlayer(_creator, false); creator = addPlayer(_creator, false);
} }
ServerGame::~ServerGame() Server_Game::~Server_Game()
{ {
broadcastEvent("game_closed", 0); broadcastEvent("game_closed", 0);
QMapIterator<int, Player *> playerIterator(players); QMapIterator<int, Server_Player *> playerIterator(players);
while (playerIterator.hasNext()) while (playerIterator.hasNext())
delete playerIterator.next().value(); delete playerIterator.next().value();
players.clear(); players.clear();
...@@ -42,10 +42,10 @@ ServerGame::~ServerGame() ...@@ -42,10 +42,10 @@ ServerGame::~ServerGame()
spectators.clear(); spectators.clear();
emit gameClosing(); emit gameClosing();
qDebug("ServerGame destructor"); qDebug("Server_Game destructor");
} }
QString ServerGame::getGameListLine() const QString Server_Game::getGameListLine() const
{ {
if (players.isEmpty()) if (players.isEmpty())
return QString("list_games|%1|||0|%2||0|0").arg(gameId).arg(maxPlayers); return QString("list_games|%1|||0|%2||0|0").arg(gameId).arg(maxPlayers);
...@@ -62,32 +62,32 @@ QString ServerGame::getGameListLine() const ...@@ -62,32 +62,32 @@ QString ServerGame::getGameListLine() const
} }
} }
void ServerGame::broadcastEvent(const QString &eventStr, Player *player) void Server_Game::broadcastEvent(const QString &eventStr, Server_Player *player)
{ {
QList<Player *> allClients = QList<Player *>() << players.values() << spectators; QList<Server_Player *> allClients = QList<Server_Player *>() << players.values() << spectators;
for (int i = 0; i < allClients.size(); ++i) for (int i = 0; i < allClients.size(); ++i)
allClients[i]->publicEvent(eventStr, player); allClients[i]->publicEvent(eventStr, player);
} }
void ServerGame::startGameIfReady() void Server_Game::startGameIfReady()
{ {
if (players.size() < maxPlayers) if (players.size() < maxPlayers)
return; return;
QMapIterator<int, Player *> playerIterator(players); QMapIterator<int, Server_Player *> playerIterator(players);
while (playerIterator.hasNext()) while (playerIterator.hasNext())
if (playerIterator.next().value()->getStatus() != StatusReadyStart) if (playerIterator.next().value()->getStatus() != StatusReadyStart)
return; return;
QSqlQuery query; /* QSqlQuery query;
query.prepare("insert into games (id, descr, password, time_started) values(:id, :descr, :password, now())"); query.prepare("insert into games (id, descr, password, time_started) values(:id, :descr, :password, now())");
query.bindValue(":id", gameId); query.bindValue(":id", gameId);
query.bindValue(":descr", description); query.bindValue(":descr", description);
query.bindValue(":password", !password.isEmpty()); query.bindValue(":password", !password.isEmpty());
query.exec(); query.exec();
QMapIterator<int, Player *> playerIterator2(players); QMapIterator<int, Server_Player *> playerIterator2(players);
while (playerIterator2.hasNext()) { while (playerIterator2.hasNext()) {
Player *player = playerIterator2.next().value(); Server_Player *player = playerIterator2.next().value();
query.prepare("insert into games_players (id_game, player) values(:id, :player)"); query.prepare("insert into games_players (id_game, player) values(:id, :player)");
query.bindValue(":id", gameId); query.bindValue(":id", gameId);
query.bindValue(":player", player->getPlayerName()); query.bindValue(":player", player->getPlayerName());
...@@ -95,13 +95,13 @@ void ServerGame::startGameIfReady() ...@@ -95,13 +95,13 @@ void ServerGame::startGameIfReady()
player->setupZones(); player->setupZones();
} }
*/
gameStarted = true; gameStarted = true;
broadcastEvent("game_start", NULL); broadcastEvent("game_start", NULL);
setActivePlayer(0); setActivePlayer(0);
} }
ReturnMessage::ReturnCode ServerGame::checkJoin(const QString &_password, bool spectator) ReturnMessage::ReturnCode Server_Game::checkJoin(const QString &_password, bool spectator)
{ {
if (_password != password) if (_password != password)
return ReturnMessage::ReturnPasswordWrong; return ReturnMessage::ReturnPasswordWrong;
...@@ -114,12 +114,12 @@ ReturnMessage::ReturnCode ServerGame::checkJoin(const QString &_password, bool s ...@@ -114,12 +114,12 @@ ReturnMessage::ReturnCode ServerGame::checkJoin(const QString &_password, bool s
return ReturnMessage::ReturnOk; return ReturnMessage::ReturnOk;
} }
Player *ServerGame::addPlayer(const QString &playerName, bool spectator) Server_Player *Server_Game::addPlayer(const QString &playerName, bool spectator)
{ {
int playerId; int playerId;
if (!spectator) { if (!spectator) {
int max = -1; int max = -1;
QMapIterator<int, Player *> i(players); QMapIterator<int, Server_Player *> i(players);
while (i.hasNext()) { while (i.hasNext()) {
int tmp = i.next().value()->getPlayerId(); int tmp = i.next().value()->getPlayerId();
if (tmp > max) if (tmp > max)
...@@ -129,7 +129,7 @@ Player *ServerGame::addPlayer(const QString &playerName, bool spectator) ...@@ -129,7 +129,7 @@ Player *ServerGame::addPlayer(const QString &playerName, bool spectator)
} else } else
playerId = -1; playerId = -1;
Player *newPlayer = new Player(this, playerId, playerName, spectator); Server_Player *newPlayer = new Server_Player(this, playerId, playerName, spectator);
broadcastEvent(QString("join|%1").arg(spectator ? 1 : 0), newPlayer); broadcastEvent(QString("join|%1").arg(spectator ? 1 : 0), newPlayer);
if (spectator) if (spectator)
...@@ -142,7 +142,7 @@ Player *ServerGame::addPlayer(const QString &playerName, bool spectator) ...@@ -142,7 +142,7 @@ Player *ServerGame::addPlayer(const QString &playerName, bool spectator)
return newPlayer; return newPlayer;
} }
void ServerGame::removePlayer(Player *player) void Server_Game::removePlayer(Server_Player *player)
{ {
if (player->getSpectator()) if (player->getSpectator())
spectators.removeAt(spectators.indexOf(player)); spectators.removeAt(spectators.indexOf(player));
...@@ -156,21 +156,21 @@ void ServerGame::removePlayer(Player *player) ...@@ -156,21 +156,21 @@ void ServerGame::removePlayer(Player *player)
qobject_cast<Server *>(parent())->broadcastGameListUpdate(this); qobject_cast<Server *>(parent())->broadcastGameListUpdate(this);
} }
void ServerGame::setActivePlayer(int _activePlayer) void Server_Game::setActivePlayer(int _activePlayer)
{ {
activePlayer = _activePlayer; activePlayer = _activePlayer;
broadcastEvent(QString("set_active_player|%1").arg(_activePlayer), NULL); broadcastEvent(QString("set_active_player|%1").arg(_activePlayer), NULL);
setActivePhase(0); setActivePhase(0);
} }
void ServerGame::setActivePhase(int _activePhase) void Server_Game::setActivePhase(int _activePhase)
{ {
QMapIterator<int, Player *> playerIterator(players); QMapIterator<int, Server_Player *> playerIterator(players);
while (playerIterator.hasNext()) { while (playerIterator.hasNext()) {
Player *player = playerIterator.next().value(); Server_Player *player = playerIterator.next().value();
QList<Arrow *> toDelete = player->getArrows().values(); QList<Server_Arrow *> toDelete = player->getArrows().values();
for (int i = 0; i < toDelete.size(); ++i) { for (int i = 0; i < toDelete.size(); ++i) {
Arrow *a = toDelete[i]; Server_Arrow *a = toDelete[i];
broadcastEvent(QString("delete_arrow|%1").arg(a->getId()), player); broadcastEvent(QString("delete_arrow|%1").arg(a->getId()), player);
player->deleteArrow(a->getId()); player->deleteArrow(a->getId());
} }
......
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