2019-07-07 20:56:15 +00:00
|
|
|
#include <StdAfx.h>
|
2018-08-12 13:53:40 +12:00
|
|
|
#include "ChessManager.h"
|
|
|
|
|
#include "Player.h"
|
|
|
|
|
#include "Game.h"
|
|
|
|
|
|
|
|
|
|
namespace GDLE::Chess
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
ChessMatch* GameInformation::NewMatch()
|
|
|
|
|
{
|
2018-08-18 01:42:53 +12:00
|
|
|
assert(m_match == nullptr);
|
|
|
|
|
m_match = new ChessMatch(m_weenie);
|
|
|
|
|
return m_match;
|
2018-08-12 13:53:40 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ChessManager::RegisterGameBoard(GameWeenie* game)
|
|
|
|
|
{
|
2018-08-18 01:42:53 +12:00
|
|
|
assert(m_games.find(game->GetID()) == m_games.end());
|
|
|
|
|
m_games.emplace(game->GetID(), game);
|
2018-08-12 13:53:40 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ChessManager::UnregisterGameBoard(GameWeenie* game)
|
|
|
|
|
{
|
2018-08-18 01:42:53 +12:00
|
|
|
GameStore::iterator const itr = m_games.find(game->GetID());
|
|
|
|
|
assert(itr != m_games.end());
|
2018-08-12 13:53:40 +12:00
|
|
|
|
2018-08-18 01:42:53 +12:00
|
|
|
// a landblock with an active match on it should never be unloaded
|
|
|
|
|
assert(!itr->second.GetMatch());
|
2018-08-12 13:53:40 +12:00
|
|
|
|
2018-08-18 01:42:53 +12:00
|
|
|
m_games.erase(itr);
|
2018-08-12 13:53:40 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ChessManager::Update()
|
|
|
|
|
{
|
2018-08-18 01:42:53 +12:00
|
|
|
for (GameStore::value_type& pair : m_games)
|
|
|
|
|
{
|
|
|
|
|
ChessMatch* match = pair.second.GetMatch();
|
|
|
|
|
if (!match)
|
|
|
|
|
continue;
|
2018-08-12 13:53:40 +12:00
|
|
|
|
2018-08-18 01:42:53 +12:00
|
|
|
match->Update();
|
2018-08-12 13:53:40 +12:00
|
|
|
|
2018-08-18 01:42:53 +12:00
|
|
|
if (match->GetState() == ChessStateFinished)
|
|
|
|
|
pair.second.DeleteMatch();
|
|
|
|
|
}
|
2018-08-12 13:53:40 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ChessManager::Join(CPlayerWeenie* player, CWeenieObject* object)
|
|
|
|
|
{
|
2018-08-18 01:42:53 +12:00
|
|
|
GameWeenie* game = object->AsGame();
|
|
|
|
|
if (!game)
|
|
|
|
|
{
|
|
|
|
|
ChessMatch::SendJoinGameResponse(player, object->GetID(), std::nullopt);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (GetGame(player->GetID()))
|
|
|
|
|
{
|
|
|
|
|
ChessMatch::SendJoinGameResponse(player, game->GetID(), std::nullopt);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GameStore::iterator const itr = m_games.find(game->GetID());
|
|
|
|
|
assert(itr != m_games.end());
|
|
|
|
|
|
|
|
|
|
ChessMatch* match = itr->second.GetMatch();
|
|
|
|
|
if (!match)
|
|
|
|
|
match = itr->second.NewMatch();
|
|
|
|
|
|
|
|
|
|
match->Join(player);
|
2018-08-12 13:53:40 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ChessManager::Move(CPlayerWeenie* player, ChessPieceCoord const& from, ChessPieceCoord const& to) const
|
|
|
|
|
{
|
2018-08-18 01:42:53 +12:00
|
|
|
if (ChessMatch* match = GetGame(player->GetID()))
|
|
|
|
|
match->MoveEnqueue(player, from, to);
|
2018-08-12 13:53:40 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ChessManager::MovePass(CPlayerWeenie* player) const
|
|
|
|
|
{
|
2018-08-18 01:42:53 +12:00
|
|
|
if (ChessMatch* match = GetGame(player->GetID()))
|
|
|
|
|
match->MovePassEnqueue(player);
|
2018-08-12 13:53:40 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ChessManager::Quit(CPlayerWeenie* player) const
|
|
|
|
|
{
|
2018-08-18 01:42:53 +12:00
|
|
|
if (ChessMatch* match = GetGame(player->GetID()))
|
|
|
|
|
match->QuitEnqueue(player);
|
2018-08-12 13:53:40 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ChessManager::Stalemate(CPlayerWeenie* player, bool const on) const
|
|
|
|
|
{
|
2018-08-18 01:42:53 +12:00
|
|
|
if (ChessMatch* match = GetGame(player->GetID()))
|
|
|
|
|
match->StalemateEnqueue(player, on);
|
2018-08-12 13:53:40 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ChessManager::ChallengeAi(CPlayerWeenie* player) const
|
|
|
|
|
{
|
2018-08-18 01:42:53 +12:00
|
|
|
if (ChessMatch* match = GetGame(player->GetID()))
|
|
|
|
|
match->AddAi();
|
2018-08-12 13:53:40 +12:00
|
|
|
}
|
|
|
|
|
|
2018-08-16 01:21:13 +12:00
|
|
|
void ChessManager::PieceReady(uint32_t const gameGuid, uint32_t const pieceGuid)
|
|
|
|
|
{
|
2018-08-18 01:42:53 +12:00
|
|
|
GameStore::iterator const itr = m_games.find(gameGuid);
|
|
|
|
|
assert(itr != m_games.end());
|
2018-08-16 01:21:13 +12:00
|
|
|
|
2018-08-18 01:42:53 +12:00
|
|
|
ChessMatch* match = itr->second.GetMatch();
|
|
|
|
|
assert(match);
|
2018-08-16 01:21:13 +12:00
|
|
|
|
2018-08-18 01:42:53 +12:00
|
|
|
match->PieceReady(pieceGuid);
|
2018-08-16 01:21:13 +12:00
|
|
|
}
|
|
|
|
|
|
2018-08-12 13:53:40 +12:00
|
|
|
ChessMatch* ChessManager::GetGame(uint32_t const guid) const
|
|
|
|
|
{
|
2018-08-18 01:42:53 +12:00
|
|
|
for (GameStore::value_type const& pair : m_games)
|
|
|
|
|
if (ChessMatch* match = pair.second.GetMatch())
|
|
|
|
|
if (match->IsInMatch(guid))
|
|
|
|
|
return match;
|
|
|
|
|
return nullptr;
|
2018-08-12 13:53:40 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // GDLE::Chess
|