2007-01-13 02:40:33 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
* Copyright (C) 2006 by FThauer FHammer *
|
|
|
|
|
* f.thauer@web.de *
|
|
|
|
|
* *
|
|
|
|
|
* 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 "game.h"
|
|
|
|
|
|
2007-05-18 17:13:27 +00:00
|
|
|
#include <enginefactory.h>
|
2007-05-15 20:37:37 +00:00
|
|
|
#include <guiinterface.h>
|
2007-05-18 17:13:27 +00:00
|
|
|
|
2007-10-14 11:01:44 +00:00
|
|
|
#include "localexception.h"
|
|
|
|
|
#include "engine_msg.h"
|
|
|
|
|
|
2007-05-18 17:13:27 +00:00
|
|
|
#include <iostream>
|
2007-01-13 20:47:48 +00:00
|
|
|
|
2007-01-13 02:40:33 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
2007-05-18 17:13:27 +00:00
|
|
|
Game::Game(GuiInterface* gui, boost::shared_ptr<EngineFactory> factory,
|
2007-09-29 00:08:19 +00:00
|
|
|
const PlayerDataList &playerDataList, const GameData &gameData,
|
2007-05-20 00:16:29 +00:00
|
|
|
const StartData &startData, int gameId)
|
2007-10-18 17:40:12 +00:00
|
|
|
: myFactory(factory), myGui(gui), currentHand(0), currentBoard(0),
|
2007-06-11 19:46:18 +00:00
|
|
|
startQuantityPlayers(startData.numberOfPlayers),
|
2007-10-11 09:06:06 +00:00
|
|
|
startCash(gameData.startMoney), startSmallBlind(gameData.firstSmallBlind),
|
2007-10-18 17:40:12 +00:00
|
|
|
myGameID(gameId), currentSmallBlind(gameData.firstSmallBlind), currentHandID(0), dealerPosition(0), lastHandBlindsRaised(1), lastTimeBlindsRaised(0), myGameData(gameData)
|
2007-01-13 02:40:33 +00:00
|
|
|
{
|
2007-10-06 12:25:31 +00:00
|
|
|
|
|
|
|
|
blindsList = myGameData.manualBlindsList;
|
|
|
|
|
|
2007-09-28 15:04:52 +00:00
|
|
|
if(DEBUG_MODE) {
|
2009-04-12 18:59:35 +00:00
|
|
|
startSmallBlind = 320;
|
2007-10-18 17:40:12 +00:00
|
|
|
currentSmallBlind = startSmallBlind;
|
2007-09-28 15:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
2007-01-13 02:40:33 +00:00
|
|
|
int i;
|
|
|
|
|
|
2007-10-19 14:03:12 +00:00
|
|
|
// determine dealer position
|
2007-05-21 19:00:06 +00:00
|
|
|
PlayerDataList::const_iterator player_i = playerDataList.begin();
|
|
|
|
|
PlayerDataList::const_iterator player_end = playerDataList.end();
|
|
|
|
|
|
|
|
|
|
while (player_i != player_end)
|
|
|
|
|
{
|
|
|
|
|
if ((*player_i)->GetUniqueId() == startData.startDealerPlayerId)
|
|
|
|
|
break;
|
|
|
|
|
++player_i;
|
|
|
|
|
}
|
2007-10-14 17:49:15 +00:00
|
|
|
if (player_i == player_end)
|
2007-10-18 20:36:49 +00:00
|
|
|
throw LocalException(__FILE__, __LINE__, ERR_DEALER_NOT_FOUND);
|
2007-10-19 14:03:12 +00:00
|
|
|
|
2007-09-27 14:17:07 +00:00
|
|
|
dealerPosition = startData.startDealerPlayerId;
|
2007-01-13 02:40:33 +00:00
|
|
|
|
2007-10-19 14:03:12 +00:00
|
|
|
// create board
|
2007-10-18 17:40:12 +00:00
|
|
|
currentBoard = myFactory->createBoard();
|
2007-01-13 22:29:17 +00:00
|
|
|
|
2007-10-19 14:03:12 +00:00
|
|
|
// create player lists
|
2007-09-27 00:37:01 +00:00
|
|
|
seatsList = PlayerList(new std::list<boost::shared_ptr<PlayerInterface> >);
|
2007-09-24 21:09:36 +00:00
|
|
|
activePlayerList = PlayerList(new std::list<boost::shared_ptr<PlayerInterface> >);
|
|
|
|
|
runningPlayerList = PlayerList(new std::list<boost::shared_ptr<PlayerInterface> >);
|
|
|
|
|
|
2007-10-19 14:03:12 +00:00
|
|
|
// create player
|
2007-05-21 19:00:06 +00:00
|
|
|
player_i = playerDataList.begin();
|
|
|
|
|
player_end = playerDataList.end();
|
2007-04-17 07:28:32 +00:00
|
|
|
for(i=0; i<MAX_NUMBER_OF_PLAYERS; i++) {
|
2007-01-19 19:13:59 +00:00
|
|
|
|
2007-04-26 21:11:13 +00:00
|
|
|
string myName;
|
|
|
|
|
string myAvatarFile;
|
2007-05-08 06:45:46 +00:00
|
|
|
unsigned uniqueId = 0;
|
|
|
|
|
PlayerType type = PLAYER_TYPE_COMPUTER;
|
2007-05-13 22:25:59 +00:00
|
|
|
boost::shared_ptr<SessionData> myNetSession;
|
2007-04-26 21:11:13 +00:00
|
|
|
|
|
|
|
|
if (player_i != player_end)
|
|
|
|
|
{
|
2007-05-08 06:45:46 +00:00
|
|
|
uniqueId = (*player_i)->GetUniqueId();
|
|
|
|
|
type = (*player_i)->GetType();
|
2007-04-26 21:11:13 +00:00
|
|
|
myName = (*player_i)->GetName();
|
|
|
|
|
myAvatarFile = (*player_i)->GetAvatarFile();
|
2007-05-13 22:25:59 +00:00
|
|
|
myNetSession = (*player_i)->GetNetSessionData();
|
2007-04-26 21:11:13 +00:00
|
|
|
// TODO: set player type
|
|
|
|
|
++player_i;
|
|
|
|
|
}
|
2007-01-19 19:13:59 +00:00
|
|
|
|
2007-10-19 14:03:12 +00:00
|
|
|
// create player objects
|
2007-10-18 17:40:12 +00:00
|
|
|
boost::shared_ptr<PlayerInterface> tmpPlayer = myFactory->createPlayer(currentBoard, i, uniqueId, type, myName, myAvatarFile, startCash, startQuantityPlayers > i, 0);
|
2007-09-23 21:30:51 +00:00
|
|
|
|
2007-09-27 00:37:01 +00:00
|
|
|
tmpPlayer->setNetSessionData(myNetSession);
|
|
|
|
|
|
2007-09-23 21:30:51 +00:00
|
|
|
// fill player lists
|
2007-09-27 00:37:01 +00:00
|
|
|
seatsList->push_back(tmpPlayer);
|
2007-09-23 21:30:51 +00:00
|
|
|
if(startQuantityPlayers > i) {
|
2007-09-24 21:09:36 +00:00
|
|
|
activePlayerList->push_back(tmpPlayer);
|
2007-09-23 21:30:51 +00:00
|
|
|
}
|
2007-09-25 09:07:36 +00:00
|
|
|
(*runningPlayerList) = (*activePlayerList);
|
2007-08-10 23:16:21 +00:00
|
|
|
|
2007-09-27 00:37:01 +00:00
|
|
|
|
2007-01-13 02:40:33 +00:00
|
|
|
}
|
2007-10-19 14:03:12 +00:00
|
|
|
|
|
|
|
|
currentBoard->setPlayerLists(seatsList, activePlayerList, runningPlayerList);
|
2007-09-28 05:23:13 +00:00
|
|
|
|
|
|
|
|
//start timer
|
|
|
|
|
blindsTimer.reset();
|
|
|
|
|
blindsTimer.start();
|
2007-01-13 02:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Game::~Game()
|
|
|
|
|
{
|
2007-10-18 17:40:12 +00:00
|
|
|
delete currentBoard;
|
|
|
|
|
currentBoard = 0;
|
|
|
|
|
delete currentHand;
|
|
|
|
|
currentHand = 0;
|
2007-01-13 02:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
2007-08-20 15:30:22 +00:00
|
|
|
HandInterface *Game::getCurrentHand()
|
|
|
|
|
{
|
2007-10-18 17:40:12 +00:00
|
|
|
return currentHand;
|
2007-08-20 15:30:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const HandInterface *Game::getCurrentHand() const
|
2007-05-11 12:01:13 +00:00
|
|
|
{
|
2007-10-18 17:40:12 +00:00
|
|
|
return currentHand;
|
2007-05-11 12:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
2007-05-12 22:00:59 +00:00
|
|
|
void Game::initHand()
|
2007-01-13 02:40:33 +00:00
|
|
|
{
|
2007-05-03 22:44:08 +00:00
|
|
|
|
2007-09-27 00:37:01 +00:00
|
|
|
size_t i;
|
2007-09-26 10:13:10 +00:00
|
|
|
PlayerListConstIterator it_c;
|
2007-10-19 14:03:12 +00:00
|
|
|
PlayerListIterator it;
|
2007-01-13 02:40:33 +00:00
|
|
|
|
2007-10-18 17:40:12 +00:00
|
|
|
currentHandID++;
|
2007-01-13 02:40:33 +00:00
|
|
|
|
2007-09-28 05:23:13 +00:00
|
|
|
// calculate smallBlind
|
|
|
|
|
raiseBlinds();
|
2007-01-13 02:40:33 +00:00
|
|
|
|
2007-10-19 14:03:12 +00:00
|
|
|
// set player action none
|
|
|
|
|
for(it=seatsList->begin(); it!=seatsList->end(); it++) {
|
|
|
|
|
(*it)->setMyAction(PLAYER_ACTION_NONE);
|
2007-09-26 10:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-19 14:03:12 +00:00
|
|
|
// set player with empty cash inactive
|
2007-10-02 22:36:04 +00:00
|
|
|
it = activePlayerList->begin();
|
2007-09-26 10:13:10 +00:00
|
|
|
while( it!=activePlayerList->end() ) {
|
2007-09-25 11:01:48 +00:00
|
|
|
|
2007-09-23 21:30:51 +00:00
|
|
|
if((*it)->getMyCash() == 0) {
|
2007-10-19 14:03:12 +00:00
|
|
|
(*it)->setMyActiveStatus(false);
|
2007-09-24 21:09:36 +00:00
|
|
|
it = activePlayerList->erase(it);
|
2007-09-25 12:43:58 +00:00
|
|
|
} else {
|
|
|
|
|
it++;
|
2007-09-23 21:30:51 +00:00
|
|
|
}
|
2007-01-13 02:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-19 14:03:12 +00:00
|
|
|
// delete possible existing hands
|
2007-10-18 17:40:12 +00:00
|
|
|
if(currentHand) {
|
|
|
|
|
delete currentHand;
|
|
|
|
|
currentHand = 0;
|
2007-10-03 12:28:10 +00:00
|
|
|
}
|
2007-10-02 22:36:04 +00:00
|
|
|
|
2007-09-25 09:07:36 +00:00
|
|
|
runningPlayerList->clear();
|
2007-09-25 09:52:58 +00:00
|
|
|
(*runningPlayerList) = (*activePlayerList);
|
2007-09-25 09:07:36 +00:00
|
|
|
|
2007-10-19 14:03:12 +00:00
|
|
|
// create Hand
|
|
|
|
|
currentHand = myFactory->createHand(myFactory, myGui, currentBoard, seatsList, activePlayerList, runningPlayerList, currentHandID, startQuantityPlayers, dealerPosition, currentSmallBlind, startCash);
|
2007-06-08 19:37:21 +00:00
|
|
|
|
2007-10-19 14:03:12 +00:00
|
|
|
// shifting dealer button -> TODO exception-rule !!!
|
2007-09-27 00:37:01 +00:00
|
|
|
bool nextDealerFound = false;
|
2007-10-18 17:40:12 +00:00
|
|
|
PlayerListConstIterator dealerPositionIt = currentHand->getSeatIt(dealerPosition);
|
2007-10-14 11:01:44 +00:00
|
|
|
if(dealerPositionIt == seatsList->end()) {
|
2007-10-18 20:36:49 +00:00
|
|
|
throw LocalException(__FILE__, __LINE__, ERR_SEAT_NOT_FOUND);
|
2007-10-14 11:01:44 +00:00
|
|
|
}
|
2007-09-27 00:37:01 +00:00
|
|
|
|
|
|
|
|
for(i=0; i<seatsList->size(); i++) {
|
|
|
|
|
|
|
|
|
|
dealerPositionIt++;
|
|
|
|
|
if(dealerPositionIt == seatsList->end()) dealerPositionIt = seatsList->begin();
|
|
|
|
|
|
2007-10-19 14:03:12 +00:00
|
|
|
it_c = currentHand->getActivePlayerIt( (*dealerPositionIt)->getMyUniqueID() );
|
|
|
|
|
if(it_c != activePlayerList->end() ) {
|
2007-09-27 00:37:01 +00:00
|
|
|
nextDealerFound = true;
|
2007-10-19 14:03:12 +00:00
|
|
|
dealerPosition = (*it_c)->getMyUniqueID();
|
2007-09-27 00:37:01 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-10 22:39:44 +00:00
|
|
|
}
|
2007-10-14 11:01:44 +00:00
|
|
|
if(!nextDealerFound) {
|
2007-10-18 20:36:49 +00:00
|
|
|
throw LocalException(__FILE__, __LINE__, ERR_NEXT_DEALER_NOT_FOUND);
|
2007-10-14 11:01:44 +00:00
|
|
|
}
|
2007-09-27 00:37:01 +00:00
|
|
|
|
|
|
|
|
|
2007-05-12 22:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Game::startHand()
|
|
|
|
|
{
|
|
|
|
|
myGui->nextRoundCleanGui();
|
2007-01-13 02:40:33 +00:00
|
|
|
|
2007-10-19 14:03:12 +00:00
|
|
|
// log new hand
|
2007-10-18 17:40:12 +00:00
|
|
|
myGui->logNewGameHandMsg(myGameID, currentHandID);
|
2007-10-03 22:42:24 +00:00
|
|
|
myGui->flushLogAtGame(myGameID);
|
|
|
|
|
|
2007-10-18 17:40:12 +00:00
|
|
|
currentHand->start();
|
2007-01-13 02:40:33 +00:00
|
|
|
}
|
2007-05-21 22:16:32 +00:00
|
|
|
|
2007-08-10 23:16:21 +00:00
|
|
|
boost::shared_ptr<PlayerInterface> Game::getPlayerByUniqueId(unsigned id)
|
2007-05-21 22:16:32 +00:00
|
|
|
{
|
2007-08-10 23:16:21 +00:00
|
|
|
boost::shared_ptr<PlayerInterface> tmpPlayer;
|
2007-09-27 14:17:07 +00:00
|
|
|
PlayerListIterator i = getSeatsList()->begin();
|
|
|
|
|
PlayerListIterator end = getSeatsList()->end();
|
|
|
|
|
while (i != end)
|
2007-05-21 22:16:32 +00:00
|
|
|
{
|
2007-09-27 14:17:07 +00:00
|
|
|
if ((*i)->getMyUniqueID() == id)
|
2007-05-21 22:16:32 +00:00
|
|
|
{
|
2007-09-27 14:17:07 +00:00
|
|
|
tmpPlayer = *i;
|
2007-05-21 22:16:32 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2007-09-27 14:17:07 +00:00
|
|
|
++i;
|
2007-05-21 22:16:32 +00:00
|
|
|
}
|
|
|
|
|
return tmpPlayer;
|
|
|
|
|
}
|
2007-08-11 10:02:04 +00:00
|
|
|
|
|
|
|
|
boost::shared_ptr<PlayerInterface> Game::getCurrentPlayer()
|
|
|
|
|
{
|
2007-09-27 14:17:07 +00:00
|
|
|
boost::shared_ptr<PlayerInterface> tmpPlayer = getPlayerByUniqueId(getCurrentHand()->getCurrentBeRo()->getCurrentPlayersTurnId());
|
2007-10-14 17:49:15 +00:00
|
|
|
if (!tmpPlayer.get())
|
2007-10-18 20:36:49 +00:00
|
|
|
throw LocalException(__FILE__, __LINE__, ERR_CURRENT_PLAYER_NOT_FOUND);
|
2007-09-27 14:17:07 +00:00
|
|
|
return tmpPlayer;
|
2007-08-11 10:02:04 +00:00
|
|
|
}
|
|
|
|
|
|
2007-09-28 05:23:13 +00:00
|
|
|
void Game::raiseBlinds() {
|
|
|
|
|
|
|
|
|
|
bool raiseBlinds = false;
|
|
|
|
|
|
2007-09-29 19:23:14 +00:00
|
|
|
if (myGameData.raiseIntervalMode == RAISE_ON_HANDNUMBER) {
|
2007-10-18 17:40:12 +00:00
|
|
|
if (lastHandBlindsRaised + myGameData.raiseSmallBlindEveryHandsValue <= currentHandID) {
|
2007-09-28 05:23:13 +00:00
|
|
|
raiseBlinds = true;
|
2007-10-18 17:40:12 +00:00
|
|
|
lastHandBlindsRaised = currentHandID;
|
2007-09-28 05:23:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (lastTimeBlindsRaised + myGameData.raiseSmallBlindEveryMinutesValue <= blindsTimer.elapsed().total_seconds()/60) {
|
|
|
|
|
raiseBlinds = true;
|
|
|
|
|
lastTimeBlindsRaised = blindsTimer.elapsed().total_seconds()/60;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (raiseBlinds) {
|
|
|
|
|
// At this point, the blinds must be raised
|
2007-10-19 14:03:12 +00:00
|
|
|
// Now we check how the blinds should be raised
|
2007-09-28 05:23:13 +00:00
|
|
|
if (myGameData.raiseMode == DOUBLE_BLINDS) {
|
2007-10-18 17:40:12 +00:00
|
|
|
currentSmallBlind *= 2;
|
2007-09-28 05:23:13 +00:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if(!blindsList.empty()) {
|
2007-10-18 17:40:12 +00:00
|
|
|
currentSmallBlind = blindsList.front();
|
2007-10-06 12:25:31 +00:00
|
|
|
blindsList.pop_front();
|
2007-09-28 05:23:13 +00:00
|
|
|
}
|
2007-10-06 12:25:31 +00:00
|
|
|
else {
|
2007-09-28 05:23:13 +00:00
|
|
|
// The position exceeds the list
|
|
|
|
|
if (myGameData.afterManualBlindsMode == AFTERMB_DOUBLE_BLINDS) {
|
2007-10-18 17:40:12 +00:00
|
|
|
currentSmallBlind *= 2;
|
2007-09-28 05:23:13 +00:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if(myGameData.afterManualBlindsMode == AFTERMB_RAISE_ABOUT) {
|
2007-10-18 17:40:12 +00:00
|
|
|
currentSmallBlind += myGameData.afterMBAlwaysRaiseValue;
|
2007-09-28 05:23:13 +00:00
|
|
|
}
|
2007-10-19 14:03:12 +00:00
|
|
|
}
|
2007-09-28 05:23:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|