Game configuration now placed in a struct. Prepared to send/receive game configuration over tcp.
This commit is contained in:
@@ -147,3 +147,5 @@ Server Notification: Player's Turn
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
|
||||
|
||||
|
||||
2007 by Lothar May
|
||||
|
||||
@@ -55,6 +55,7 @@ INCLUDEPATH += . \
|
||||
HEADERS += src/game.h \
|
||||
src/session.h \
|
||||
src/playerdata.h \
|
||||
src/gamedata.h \
|
||||
src/config/configfile.h \
|
||||
src/core/rand.h \
|
||||
src/core/thread.h \
|
||||
|
||||
+9
-9
@@ -18,6 +18,7 @@
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
#include "game.h"
|
||||
#include "gamedata.h"
|
||||
|
||||
#include "enginefactory.h"
|
||||
#include "localenginefactory.h"
|
||||
@@ -29,14 +30,15 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
Game::Game(GuiInterface* gui, const PlayerDataList &playerData, int sC, int sB, int hbrsB, int gameId)
|
||||
: myGui(gui), actualHand(0), actualBoard(0), startCash(sC),
|
||||
startSmallBlind(sB), startHandsBeforeRaiseSmallBlind(hbrsB),myGameID(gameId),
|
||||
actualSmallBlind(sB), actualHandID(0), dealerPosition(0)
|
||||
Game::Game(GuiInterface* gui, const PlayerDataList &playerDataList, const GameData &gameData, int gameId)
|
||||
: myGui(gui), actualHand(0), actualBoard(0), startQuantityPlayers(gameData.numberOfPlayers),
|
||||
startCash(gameData.startCash), startSmallBlind(gameData.smallBlind),
|
||||
startHandsBeforeRaiseSmallBlind(gameData.handsBeforeRaise),
|
||||
myGameID(gameId), actualQuantityPlayers(gameData.numberOfPlayers),
|
||||
actualSmallBlind(gameData.smallBlind), actualHandID(0), dealerPosition(0)
|
||||
{
|
||||
// cout << "Create Game Object" << "\n";
|
||||
int i;
|
||||
startQuantityPlayers = actualQuantityPlayers = playerData.size();
|
||||
|
||||
actualHandID = 0;
|
||||
|
||||
@@ -59,8 +61,8 @@ Game::Game(GuiInterface* gui, const PlayerDataList &playerData, int sC, int sB,
|
||||
myTool.getRandNumber(0, startQuantityPlayers-1, 1, &dealerPosition, 0);
|
||||
|
||||
// Player erstellen
|
||||
PlayerDataList::const_iterator player_i = playerData.begin();
|
||||
PlayerDataList::const_iterator player_end = playerData.end();
|
||||
PlayerDataList::const_iterator player_i = playerDataList.begin();
|
||||
PlayerDataList::const_iterator player_end = playerDataList.end();
|
||||
for(i=0; i<MAX_NUMBER_OF_PLAYERS; i++) {
|
||||
|
||||
string myName;
|
||||
@@ -84,8 +86,6 @@ Game::Game(GuiInterface* gui, const PlayerDataList &playerData, int sC, int sB,
|
||||
startHand();
|
||||
|
||||
// SPIEL-SCHLEIFE
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
+2
-2
@@ -32,12 +32,12 @@ class HandInterface;
|
||||
class PlayerInterface;
|
||||
class BoardInterface;
|
||||
class EngineFactory;
|
||||
|
||||
struct GameData;
|
||||
|
||||
class Game {
|
||||
|
||||
public:
|
||||
Game(GuiInterface *gui, const PlayerDataList &playerData, int sC, int sB, int hbrsB, int gameId);
|
||||
Game(GuiInterface *gui, const PlayerDataList &playerDataList, const GameData &gameData, int gameId);
|
||||
|
||||
~Game();
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Lothar May *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
/* Game data. */
|
||||
|
||||
#ifndef _GAMEDATA_H_
|
||||
#define _GAMEDATA_H_
|
||||
|
||||
// For the sake of simplicity, this is a struct.
|
||||
|
||||
struct GameData
|
||||
{
|
||||
GameData() : numberOfPlayers(0), startCash(0), smallBlind(0), handsBeforeRaise(1) {}
|
||||
unsigned numberOfPlayers;
|
||||
unsigned startCash;
|
||||
unsigned smallBlind;
|
||||
unsigned handsBeforeRaise;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -601,14 +601,18 @@ void mainWindowImpl::startNewLocalGame(newGameDialogImpl *v) {
|
||||
}
|
||||
|
||||
//get values from local game dialog
|
||||
GameData gameData;
|
||||
if(v) {
|
||||
//Speeds
|
||||
guiGameSpeed = v->spinBox_gameSpeed->value();
|
||||
setSpeeds();
|
||||
//positioning Slider
|
||||
horizontalSlider_speed->setValue(guiGameSpeed);
|
||||
//Start Game!!!
|
||||
mySession->startGame(v->spinBox_quantityPlayers->value(), v->spinBox_startCash->value(), v->spinBox_smallBlind->value(), v->spinBox_handsBeforeRaiseSmallBlind->value());
|
||||
// Set Game Data
|
||||
gameData.numberOfPlayers = v->spinBox_quantityPlayers->value();
|
||||
gameData.startCash = v->spinBox_startCash->value();
|
||||
gameData.smallBlind = v->spinBox_smallBlind->value();
|
||||
gameData.handsBeforeRaise = v->spinBox_handsBeforeRaiseSmallBlind->value();
|
||||
}
|
||||
// start with default values
|
||||
else {
|
||||
@@ -617,9 +621,14 @@ void mainWindowImpl::startNewLocalGame(newGameDialogImpl *v) {
|
||||
setSpeeds();
|
||||
//positioning Slider
|
||||
horizontalSlider_speed->setValue(guiGameSpeed);
|
||||
//Start Game!!!
|
||||
mySession->startGame(myConfig->readConfigInt("NumberOfPlayers"), myConfig->readConfigInt("StartCash"), myConfig->readConfigInt("SmallBlind"), myConfig->readConfigInt("HandsBeforeRaiseSmallBlind"));
|
||||
// Set Game Data
|
||||
gameData.numberOfPlayers = myConfig->readConfigInt("NumberOfPlayers");
|
||||
gameData.startCash = myConfig->readConfigInt("StartCash");
|
||||
gameData.smallBlind = myConfig->readConfigInt("SmallBlind");
|
||||
gameData.handsBeforeRaise = myConfig->readConfigInt("HandsBeforeRaiseSmallBlind");
|
||||
}
|
||||
//Start Game!!!
|
||||
mySession->startGame(gameData);
|
||||
}
|
||||
|
||||
|
||||
@@ -638,7 +647,13 @@ void mainWindowImpl::callCreateNetworkGameDialog() {
|
||||
mySession->terminateNetworkClient();
|
||||
mySession->terminateNetworkServer();
|
||||
|
||||
mySession->startNetworkServer();
|
||||
GameData gameData;
|
||||
gameData.numberOfPlayers = myCreateNetworkGameDialog->spinBox_quantityPlayers->value();
|
||||
gameData.startCash = myCreateNetworkGameDialog->spinBox_startCash->value();
|
||||
gameData.smallBlind = myCreateNetworkGameDialog->spinBox_smallBlind->value();
|
||||
gameData.handsBeforeRaise = myCreateNetworkGameDialog->spinBox_handsBeforeRaiseSmallBlind->value();
|
||||
|
||||
mySession->startNetworkServer(gameData);
|
||||
mySession->startNetworkClientForLocalServer();
|
||||
|
||||
myStartNetworkGameDialog->exec();
|
||||
|
||||
@@ -81,6 +81,9 @@ ServerRecvStateInit::Process(ServerRecvThread &server)
|
||||
tmpPacket->GetData(playerData);
|
||||
server.GetCallback().SignalNetServerPlayerJoined(playerData.playerName);
|
||||
boost::shared_ptr<NetPacket> answer(new NetPacketJoinGameAck);
|
||||
NetPacketJoinGameAck::Data joinGameAckData;
|
||||
// TODO: set data.
|
||||
static_cast<NetPacketJoinGameAck *>(answer.get())->SetData(joinGameAckData);
|
||||
server.GetSender().Send(answer, recvSock);
|
||||
session->SetState(SessionData::Established);
|
||||
}
|
||||
|
||||
@@ -24,6 +24,9 @@
|
||||
#include <net/sendercallback.h>
|
||||
#include <net/receiverhelper.h>
|
||||
#include <net/socket_msg.h>
|
||||
#include <gamedata.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class ServerSenderCallback : public SenderCallback
|
||||
{
|
||||
@@ -47,6 +50,7 @@ ServerRecvThread::ServerRecvThread(ServerCallback &cb)
|
||||
m_senderCallback.reset(new ServerSenderCallback(*this));
|
||||
m_sender.reset(new SenderThread(GetSenderCallback()));
|
||||
m_receiver.reset(new ReceiverHelper);
|
||||
m_gameData.reset(new GameData);
|
||||
}
|
||||
|
||||
ServerRecvThread::~ServerRecvThread()
|
||||
@@ -55,6 +59,13 @@ ServerRecvThread::~ServerRecvThread()
|
||||
CleanupSessionMap();
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::Init(const string &pwd, const GameData &gameData)
|
||||
{
|
||||
m_password = pwd;
|
||||
*m_gameData = gameData;
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::SendToAllPlayers(boost::shared_ptr<NetPacket> packet)
|
||||
{
|
||||
@@ -297,6 +308,19 @@ ServerRecvThread::GetReceiver()
|
||||
return *m_receiver;
|
||||
}
|
||||
|
||||
const GameData &
|
||||
ServerRecvThread::GetGameData()
|
||||
{
|
||||
assert(m_gameData.get());
|
||||
return *m_gameData;
|
||||
}
|
||||
|
||||
bool
|
||||
ServerRecvThread::CheckPassword(const std::string &password)
|
||||
{
|
||||
return (password == m_password);
|
||||
}
|
||||
|
||||
ServerSenderCallback &
|
||||
ServerRecvThread::GetSenderCallback()
|
||||
{
|
||||
|
||||
@@ -41,7 +41,8 @@ ServerThread::~ServerThread()
|
||||
}
|
||||
|
||||
void
|
||||
ServerThread::Init(unsigned serverPort, bool ipv6, const std::string &pwd)
|
||||
ServerThread::Init(unsigned serverPort, bool ipv6, const std::string &pwd,
|
||||
const GameData &gameData)
|
||||
{
|
||||
if (IsRunning())
|
||||
return; // TODO: throw exception
|
||||
@@ -50,7 +51,8 @@ ServerThread::Init(unsigned serverPort, bool ipv6, const std::string &pwd)
|
||||
|
||||
context.SetAddrFamily(ipv6 ? AF_INET6 : AF_INET);
|
||||
context.SetServerPort(serverPort);
|
||||
context.SetPassword(pwd);
|
||||
|
||||
GetRecvThread().Init(pwd, gameData);
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -43,10 +43,6 @@ public:
|
||||
{return m_serverPort;}
|
||||
void SetServerPort(unsigned serverPort)
|
||||
{m_serverPort = serverPort;}
|
||||
const std::string &GetPassword() const
|
||||
{return m_password;}
|
||||
void SetPassword(const std::string &password)
|
||||
{m_password = password;}
|
||||
const sockaddr_storage *GetServerSockaddr() const
|
||||
{return &m_serverSockaddr;}
|
||||
sockaddr_storage *GetServerSockaddr()
|
||||
@@ -59,7 +55,6 @@ private:
|
||||
SOCKET m_sockfd;
|
||||
int m_addrFamily;
|
||||
unsigned m_serverPort;
|
||||
std::string m_password;
|
||||
sockaddr_storage m_serverSockaddr;
|
||||
};
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include <core/thread.h>
|
||||
#include <deque>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
#include <net/connectdata.h>
|
||||
@@ -40,6 +41,7 @@ class SenderThread;
|
||||
class ReceiverHelper;
|
||||
class ServerSenderCallback;
|
||||
class NetPacket;
|
||||
struct GameData;
|
||||
|
||||
class ServerRecvThread : public Thread
|
||||
{
|
||||
@@ -47,6 +49,8 @@ public:
|
||||
ServerRecvThread(ServerCallback &gui);
|
||||
virtual ~ServerRecvThread();
|
||||
|
||||
void Init(const std::string &pwd, const GameData &gameData);
|
||||
|
||||
void SendToAllPlayers(boost::shared_ptr<NetPacket> packet);
|
||||
void AddConnection(boost::shared_ptr<ConnectData> data);
|
||||
void AddNotification(unsigned notification);
|
||||
@@ -78,6 +82,9 @@ protected:
|
||||
SenderThread &GetSender();
|
||||
ReceiverHelper &GetReceiver();
|
||||
|
||||
const GameData &GetGameData();
|
||||
bool CheckPassword(const std::string &password);
|
||||
|
||||
ServerSenderCallback &GetSenderCallback();
|
||||
|
||||
private:
|
||||
@@ -96,6 +103,9 @@ private:
|
||||
std::auto_ptr<SenderThread> m_sender;
|
||||
|
||||
std::auto_ptr<ServerSenderCallback> m_senderCallback;
|
||||
std::auto_ptr<GameData> m_gameData;
|
||||
|
||||
std::string m_password;
|
||||
|
||||
ServerCallback &m_callback;
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ class ServerContext;
|
||||
class ServerRecvThread;
|
||||
class ServerSenderCallback;
|
||||
class SenderThread;
|
||||
struct GameData;
|
||||
|
||||
class ServerThread : public Thread
|
||||
{
|
||||
@@ -38,7 +39,8 @@ public:
|
||||
virtual ~ServerThread();
|
||||
|
||||
// Set the parameters.
|
||||
void Init(unsigned serverPort, bool ipv6, const std::string &pwd);
|
||||
void Init(unsigned serverPort, bool ipv6, const std::string &pwd,
|
||||
const GameData &gameData);
|
||||
void StartGame();
|
||||
|
||||
ServerCallback &GetCallback();
|
||||
|
||||
+2
-2
@@ -19,8 +19,8 @@
|
||||
|
||||
#include <playerdata.h>
|
||||
|
||||
PlayerData::PlayerData()
|
||||
: m_type(PLAYER_TYPE_COMPUTER)
|
||||
PlayerData::PlayerData(unsigned uniqueId)
|
||||
: m_uniqueId(uniqueId), m_type(PLAYER_TYPE_COMPUTER)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
+4
-1
@@ -34,7 +34,7 @@ enum PlayerType
|
||||
class PlayerData
|
||||
{
|
||||
public:
|
||||
PlayerData();
|
||||
PlayerData(unsigned uniqueId);
|
||||
~PlayerData();
|
||||
|
||||
const std::string &GetName() const
|
||||
@@ -49,8 +49,11 @@ public:
|
||||
{return m_type;}
|
||||
void SetPlayerType(PlayerType type)
|
||||
{m_type = type;}
|
||||
unsigned GetUniqueId() const
|
||||
{return m_uniqueId;}
|
||||
|
||||
private:
|
||||
unsigned m_uniqueId;
|
||||
std::string m_name;
|
||||
std::string m_avatarFile;
|
||||
PlayerType m_type;
|
||||
|
||||
+8
-6
@@ -46,12 +46,12 @@ Session::~Session()
|
||||
}
|
||||
|
||||
|
||||
void Session::startGame(int qP, int sC, int sB, int hbrsB) {
|
||||
void Session::startGame(const GameData &gameData) {
|
||||
|
||||
currentGameID++;
|
||||
|
||||
PlayerDataList playerDataList;
|
||||
for(int i=0; i<qP; i++) {
|
||||
for(unsigned i=0; i<gameData.numberOfPlayers; i++) {
|
||||
|
||||
//Namen und Avatarpfad abfragen
|
||||
ostringstream myName;
|
||||
@@ -63,7 +63,8 @@ void Session::startGame(int qP, int sC, int sB, int hbrsB) {
|
||||
|
||||
//PlayerData erzeugen
|
||||
// TODO: PlayerType setzen
|
||||
boost::shared_ptr<PlayerData> playerData(new PlayerData);
|
||||
// UniqueId = PlayerNumber for local games.
|
||||
boost::shared_ptr<PlayerData> playerData(new PlayerData(i));
|
||||
playerData->SetName(myConfig->readConfigString(myName.str()));
|
||||
playerData->SetAvatarFile(myConfig->readConfigString(myAvatar.str()));
|
||||
|
||||
@@ -71,7 +72,7 @@ void Session::startGame(int qP, int sC, int sB, int hbrsB) {
|
||||
}
|
||||
|
||||
|
||||
actualGame = new Game(myGui, playerDataList, sC, sB, hbrsB, currentGameID);
|
||||
actualGame = new Game(myGui, playerDataList, gameData, currentGameID);
|
||||
}
|
||||
|
||||
void Session::deleteGame() {
|
||||
@@ -123,7 +124,7 @@ void Session::terminateNetworkClient()
|
||||
myNetClient = 0;
|
||||
}
|
||||
|
||||
void Session::startNetworkServer()
|
||||
void Session::startNetworkServer(const GameData &gameData)
|
||||
{
|
||||
if (myNetServer)
|
||||
return; // TODO: throw exception
|
||||
@@ -131,7 +132,8 @@ void Session::startNetworkServer()
|
||||
myNetServer->Init(
|
||||
myConfig->readConfigInt("ServerPort"),
|
||||
myConfig->readConfigInt("ServerUseIpv6") == 1,
|
||||
myConfig->readConfigString("ServerPassword"));
|
||||
myConfig->readConfigString("ServerPassword"),
|
||||
gameData);
|
||||
myNetServer->Run();
|
||||
}
|
||||
|
||||
|
||||
+5
-4
@@ -19,6 +19,7 @@
|
||||
***************************************************************************/
|
||||
#ifndef STDSESSION_H
|
||||
#define STDSESSION_H
|
||||
#include "gamedata.h"
|
||||
#include <string>
|
||||
|
||||
class GuiInterface;
|
||||
@@ -30,18 +31,18 @@ class ServerThread;
|
||||
class Session{
|
||||
public:
|
||||
|
||||
Session(GuiInterface*);
|
||||
Session(GuiInterface*);
|
||||
|
||||
~Session();
|
||||
~Session();
|
||||
|
||||
void startGame(int qP, int sC, int sB, int hbrsB);
|
||||
void startGame(const GameData &gameData);
|
||||
void deleteGame();
|
||||
|
||||
void startNetworkClient(const std::string &serverAddress, unsigned serverPort, bool ipv6, const std::string &pwd);
|
||||
void startNetworkClientForLocalServer();
|
||||
void terminateNetworkClient();
|
||||
|
||||
void startNetworkServer();
|
||||
void startNetworkServer(const GameData &gameData);
|
||||
void initiateNetworkServerGame();
|
||||
void terminateNetworkServer();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user