Files
pokerth/src/game.cpp
T

280 lines
8.1 KiB
C++
Raw Normal View History

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"
#include <enginefactory.h>
#include <guiinterface.h>
#include "log.h"
2007-10-14 11:01:44 +00:00
#include "localexception.h"
#include "engine_msg.h"
#include <iostream>
2007-01-13 20:47:48 +00:00
2007-01-13 02:40:33 +00:00
using namespace std;
Game::Game(GuiInterface* gui, boost::shared_ptr<EngineFactory> factory,
2007-09-29 00:08:19 +00:00
const PlayerDataList &playerDataList, const GameData &gameData,
const StartData &startData, int gameId, Log* log)
: myFactory(factory), myGui(gui), myLog(log), currentHand(0), currentBoard(0),
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;
dealerPosition = startData.startDealerPlayerId;
2010-09-08 19:21:15 +00:00
if(DEBUG_MODE) {
2010-09-29 17:43:49 +00:00
startSmallBlind = 10;
2007-10-18 17:40:12 +00:00
currentSmallBlind = startSmallBlind;
2010-09-29 17:43:49 +00:00
dealerPosition = 4;
}
2007-01-13 02:40:33 +00:00
int i;
// determine dealer position
PlayerDataList::const_iterator player_i = playerDataList.begin();
PlayerDataList::const_iterator player_end = playerDataList.end();
while (player_i != player_end)
{
if ((*player_i)->GetUniqueId() == dealerPosition)
break;
++player_i;
}
2007-10-14 17:49:15 +00:00
if (player_i == player_end)
throw LocalException(__FILE__, __LINE__, ERR_DEALER_NOT_FOUND);
// create board
2007-10-18 17:40:12 +00:00
currentBoard = myFactory->createBoard();
2007-01-13 22:29:17 +00:00
// create player lists
seatsList = PlayerList(new std::list<boost::shared_ptr<PlayerInterface> >);
activePlayerList = PlayerList(new std::list<boost::shared_ptr<PlayerInterface> >);
runningPlayerList = PlayerList(new std::list<boost::shared_ptr<PlayerInterface> >);
// create player
player_i = playerDataList.begin();
player_end = playerDataList.end();
for(i=0; i<MAX_NUMBER_OF_PLAYERS; i++) {
2007-01-19 19:13:59 +00:00
string myName;
string myAvatarFile;
unsigned uniqueId = 0;
PlayerType type = PLAYER_TYPE_COMPUTER;
boost::shared_ptr<SessionData> myNetSession;
if (player_i != player_end)
{
uniqueId = (*player_i)->GetUniqueId();
type = (*player_i)->GetType();
myName = (*player_i)->GetName();
myAvatarFile = (*player_i)->GetAvatarFile();
myNetSession = (*player_i)->GetNetSessionData();
++player_i;
}
2007-01-19 19:13:59 +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
tmpPlayer->setNetSessionData(myNetSession);
2007-09-23 21:30:51 +00:00
// fill player lists
seatsList->push_back(tmpPlayer);
2007-09-23 21:30:51 +00:00
if(startQuantityPlayers > i) {
activePlayerList->push_back(tmpPlayer);
2007-09-23 21:30:51 +00:00
}
(*runningPlayerList) = (*activePlayerList);
2007-08-10 23:16:21 +00:00
2007-01-13 02:40:33 +00:00
}
currentBoard->setPlayerLists(seatsList, activePlayerList, runningPlayerList);
2007-09-28 05:23:13 +00:00
// log game data
myLog->logNewGameMsg(myGameID, startCash, startSmallBlind, dealerPosition, seatsList);
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
}
HandInterface *Game::getCurrentHand()
{
2007-10-18 17:40:12 +00:00
return currentHand;
}
const HandInterface *Game::getCurrentHand() const
{
2007-10-18 17:40:12 +00:00
return currentHand;
}
void Game::initHand()
2007-01-13 02:40:33 +00:00
{
2007-05-03 22:44:08 +00:00
size_t i;
PlayerListConstIterator it_c;
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
// set player action none
for(it=seatsList->begin(); it!=seatsList->end(); it++) {
(*it)->setMyAction(PLAYER_ACTION_NONE);
}
// set player with empty cash inactive
it = activePlayerList->begin();
while( it!=activePlayerList->end() ) {
2007-09-23 21:30:51 +00:00
if((*it)->getMyCash() == 0) {
(*it)->setMyActiveStatus(false);
it = activePlayerList->erase(it);
} else {
it++;
2007-09-23 21:30:51 +00:00
}
2007-01-13 02:40:33 +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
}
runningPlayerList->clear();
(*runningPlayerList) = (*activePlayerList);
// create Hand
currentHand = myFactory->createHand(myFactory, myGui, currentBoard, seatsList, activePlayerList, runningPlayerList, currentHandID, startQuantityPlayers, dealerPosition, currentSmallBlind, startCash);
2007-06-08 19:37:21 +00:00
// shifting dealer button -> TODO exception-rule !!!
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()) {
throw LocalException(__FILE__, __LINE__, ERR_SEAT_NOT_FOUND);
2007-10-14 11:01:44 +00:00
}
for(i=0; i<seatsList->size(); i++) {
dealerPositionIt++;
if(dealerPositionIt == seatsList->end()) dealerPositionIt = seatsList->begin();
it_c = currentHand->getActivePlayerIt( (*dealerPositionIt)->getMyUniqueID() );
if(it_c != activePlayerList->end() ) {
nextDealerFound = true;
dealerPosition = (*it_c)->getMyUniqueID();
break;
}
2007-08-10 22:39:44 +00:00
}
2007-10-14 11:01:44 +00:00
if(!nextDealerFound) {
throw LocalException(__FILE__, __LINE__, ERR_NEXT_DEALER_NOT_FOUND);
2007-10-14 11:01:44 +00:00
}
}
void Game::startHand()
{
myGui->nextRoundCleanGui();
2007-01-13 02:40:33 +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-08-10 23:16:21 +00:00
boost::shared_ptr<PlayerInterface> Game::getPlayerByUniqueId(unsigned id)
{
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-09-27 14:17:07 +00:00
if ((*i)->getMyUniqueID() == id)
{
2007-09-27 14:17:07 +00:00
tmpPlayer = *i;
break;
}
2007-09-27 14:17:07 +00:00
++i;
}
return tmpPlayer;
}
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())
throw LocalException(__FILE__, __LINE__, ERR_CURRENT_PLAYER_NOT_FOUND);
2007-09-27 14:17:07 +00:00
return tmpPlayer;
}
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
// 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-09-28 05:23:13 +00:00
}
}
}
}