mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-12 00:54:53 -07:00
unimportant changes
This commit is contained in:
parent
4054afc759
commit
7f659573bc
21 changed files with 89 additions and 51 deletions
13
servatrice/src/abstractrng.h
Normal file
13
servatrice/src/abstractrng.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef ABSTRACTRNG_H
|
||||
#define ABSTRACTRNG_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class AbstractRNG : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
AbstractRNG(QObject *parent = 0) : QObject(parent) { }
|
||||
virtual unsigned int getNumber(unsigned int min, unsigned int max) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -18,7 +18,7 @@
|
|||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
#include "playerzone.h"
|
||||
#include "random.h"
|
||||
#include "abstractrng.h"
|
||||
#include "card.h"
|
||||
|
||||
PlayerZone::PlayerZone(QString _name, bool _has_coords, bool _is_public, bool _is_private, bool _id_access)
|
||||
|
|
@ -32,7 +32,7 @@ PlayerZone::~PlayerZone()
|
|||
clear();
|
||||
}
|
||||
|
||||
void PlayerZone::shuffle(Random *rnd)
|
||||
void PlayerZone::shuffle(AbstractRNG *rnd)
|
||||
{
|
||||
QList<Card *> temp;
|
||||
for (int i = cards.size(); i; i--)
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@
|
|||
#include <QString>
|
||||
|
||||
class Card;
|
||||
class Random;
|
||||
class AbstractRNG;
|
||||
|
||||
class PlayerZone {
|
||||
private:
|
||||
|
|
@ -52,7 +52,7 @@ public:
|
|||
|
||||
QList<Card *> cards;
|
||||
void insertCard(Card *card, int x, int y);
|
||||
void shuffle(Random *rnd);
|
||||
void shuffle(AbstractRNG *rnd);
|
||||
void clear();
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +0,0 @@
|
|||
#ifndef RANDOM_H
|
||||
#define RANDOM_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class Random : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
Random(QObject *parent = 0);
|
||||
unsigned int getNumber(unsigned int min, unsigned int max);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,16 +1,16 @@
|
|||
#include "random.h"
|
||||
#include "rng_qt.h"
|
||||
#include <QDateTime>
|
||||
#include <stdlib.h>
|
||||
|
||||
Random::Random(QObject *parent)
|
||||
: QObject(parent)
|
||||
RNG_Qt::RNG_Qt(QObject *parent)
|
||||
: AbstractRNG(parent)
|
||||
{
|
||||
int seed = QDateTime::currentDateTime().toTime_t();
|
||||
qDebug(QString("qsrand(%1)").arg(seed).toLatin1());
|
||||
qsrand(seed);
|
||||
}
|
||||
|
||||
unsigned int Random::getNumber(unsigned int min, unsigned int max)
|
||||
unsigned int RNG_Qt::getNumber(unsigned int min, unsigned int max)
|
||||
{
|
||||
int r = qrand();
|
||||
return min + (unsigned int) (((double) (max + 1 - min)) * r / (RAND_MAX + 1.0));
|
||||
13
servatrice/src/rng_qt.h
Normal file
13
servatrice/src/rng_qt.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef RNG_QT_H
|
||||
#define RNG_QT_H
|
||||
|
||||
#include "abstractrng.h"
|
||||
|
||||
class RNG_Qt : public AbstractRNG {
|
||||
Q_OBJECT
|
||||
public:
|
||||
RNG_Qt(QObject *parent = 0);
|
||||
unsigned int getNumber(unsigned int min, unsigned int max);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -21,12 +21,15 @@
|
|||
#include "servergame.h"
|
||||
#include "serversocket.h"
|
||||
#include "counter.h"
|
||||
#include "rng_qt.h"
|
||||
#include <QtSql>
|
||||
#include <QSettings>
|
||||
|
||||
Server::Server(QObject *parent)
|
||||
: QTcpServer(parent), nextGameId(0)
|
||||
{
|
||||
rng = new RNG_Qt(this);
|
||||
|
||||
settings = new QSettings("servatrice.ini", QSettings::IniFormat, this);
|
||||
|
||||
QString dbType = settings->value("database/type").toString();
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ class ServerGame;
|
|||
class ServerSocket;
|
||||
class QSqlDatabase;
|
||||
class QSettings;
|
||||
class AbstractRNG;
|
||||
|
||||
enum AuthenticationResult { PasswordWrong = 0, PasswordRight = 1, UnknownUser = 2 };
|
||||
|
||||
|
|
@ -45,10 +46,12 @@ public:
|
|||
AuthenticationResult checkUserPassword(const QString &user, const QString &password);
|
||||
QList<ServerGame *> listOpenGames();
|
||||
ServerGame *getGame(int gameId);
|
||||
AbstractRNG *getRNG() const { return rng; }
|
||||
private:
|
||||
void incomingConnection(int SocketId);
|
||||
QList<ServerGame *> games;
|
||||
int nextGameId;
|
||||
AbstractRNG *rng;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -18,19 +18,17 @@
|
|||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
#include "servergame.h"
|
||||
#include "random.h"
|
||||
#include "serversocket.h"
|
||||
#include <QSqlQuery>
|
||||
|
||||
ServerGame::ServerGame(ServerSocket *_creator, int _gameId, QString _description, QString _password, int _maxPlayers, QObject *parent)
|
||||
: QObject(parent), gameStarted(false), rnd(0), creator(_creator), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers)
|
||||
: QObject(parent), gameStarted(false), creator(_creator), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers)
|
||||
{
|
||||
}
|
||||
|
||||
ServerGame::~ServerGame()
|
||||
{
|
||||
emit gameClosing();
|
||||
delete rnd;
|
||||
qDebug("ServerGame destructor");
|
||||
}
|
||||
|
||||
|
|
@ -103,11 +101,6 @@ void ServerGame::startGameIfReady()
|
|||
query.exec();
|
||||
}
|
||||
|
||||
if (!rnd) {
|
||||
rnd = new Random(this);
|
||||
rnd->init();
|
||||
}
|
||||
|
||||
for (int i = 0; i < players.size(); i++)
|
||||
players.at(i)->setupZones();
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@
|
|||
#include <QStringList>
|
||||
|
||||
class ServerSocket;
|
||||
class Random;
|
||||
|
||||
class ServerGame : public QObject {
|
||||
Q_OBJECT
|
||||
|
|
@ -37,7 +36,6 @@ signals:
|
|||
public slots:
|
||||
void broadcastEvent(const QString &event, ServerSocket *player);
|
||||
public:
|
||||
Random *rnd;
|
||||
ServerSocket *creator;
|
||||
int gameId;
|
||||
QString description;
|
||||
|
|
@ -57,7 +55,6 @@ public:
|
|||
int getActivePhase() { return activePhase; }
|
||||
void setActivePlayer(int _activePlayer);
|
||||
void setActivePhase(int _activePhase);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@
|
|||
#include "playerzone.h"
|
||||
#include "counter.h"
|
||||
#include "card.h"
|
||||
#include "random.h"
|
||||
#include "abstractrng.h"
|
||||
|
||||
ServerSocket::ServerSocket(Server *_server, QObject *parent)
|
||||
: QTcpSocket(parent), server(_server), game(0), authState(PasswordWrong)
|
||||
|
|
@ -105,7 +105,7 @@ void ServerSocket::setupZones()
|
|||
int i = 0;
|
||||
while (DeckIterator.hasNext())
|
||||
deck->cards.append(new Card(DeckIterator.next(), i++, 0, 0));
|
||||
deck->shuffle(game->rnd);
|
||||
deck->shuffle(server->getRNG());
|
||||
|
||||
QListIterator<QString> SBIterator(SideboardList);
|
||||
while (SBIterator.hasNext())
|
||||
|
|
@ -315,7 +315,7 @@ ReturnMessage::ReturnCode ServerSocket::cmdReadyStart(const QList<QVariant> &par
|
|||
ReturnMessage::ReturnCode ServerSocket::cmdShuffle(const QList<QVariant> ¶ms)
|
||||
{
|
||||
Q_UNUSED(params);
|
||||
getZone("deck")->shuffle(game->rnd);
|
||||
getZone("deck")->shuffle(server->getRNG());
|
||||
emit broadcastEvent("shuffle", this);
|
||||
return ReturnMessage::ReturnOk;
|
||||
}
|
||||
|
|
@ -367,6 +367,8 @@ ReturnMessage::ReturnCode ServerSocket::cmdMoveCard(const QList<QVariant> ¶m
|
|||
if (!card)
|
||||
return ReturnMessage::ReturnContextError;
|
||||
int x = params[3].toInt();
|
||||
if (x == -1)
|
||||
x = targetzone->cards.size();
|
||||
int y = 0;
|
||||
if (targetzone->hasCoords())
|
||||
y = params[4].toInt();
|
||||
|
|
@ -571,7 +573,7 @@ ReturnMessage::ReturnCode ServerSocket::cmdDumpZone(const QList<QVariant> ¶m
|
|||
ReturnMessage::ReturnCode ServerSocket::cmdRollDice(const QList<QVariant> ¶ms)
|
||||
{
|
||||
int sides = params[0].toInt();
|
||||
emit broadcastEvent(QString("roll_dice|%1|%2").arg(sides).arg(game->rnd->getNumber(1, sides)), this);
|
||||
emit broadcastEvent(QString("roll_dice|%1|%2").arg(sides).arg(server->getRNG()->getNumber(1, sides)), this);
|
||||
return ReturnMessage::ReturnOk;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue