/** @file /world/history.cpp Implementace hlavickoveho souboru /world/history.h @author Petr Wolf */ #include "history.h" using namespace World; TAction::TAction() { _type = AT_NONE; _data = NULL; } TAction::TAction(ACTION_TYPE type, void * data) { _type = type; _data = data; } ACTION_TYPE TAction::type() { return _type; } void * TAction::data() { return _data; } TActionContainer::TActionContainer() { _data = NULL; for (int i = 0; i < MAX_PLAYERS; i++) _synchronized[i] = false; } TActionContainer::TActionContainer(TPlayerIdContainer & players) { _data = NULL; for (int i = 0; i < MAX_PLAYERS; i++) _synchronized[i] = true; for (TPlayerIdContainer::iterator j = players.begin(); j != players.end(); j++) _synchronized[*j - 1] = false; } TActionContainer::TActionContainer(TAction * data, TPlayerIdContainer & players) { _data = data; for (int i = 0; i < MAX_PLAYERS; i++) _synchronized[i] = true; for (TPlayerIdContainer::iterator j = players.begin(); j != players.end(); j++) _synchronized[*j - 1] = false; } TAction & TActionContainer::data() { return * _data; } bool TActionContainer::isPlayerSynchronized(const PLAYER_ID player_id) { if (player_id <= MAX_PLAYERS) return _synchronized[player_id - 1]; else THROW(E_8K_OUT_OF_RANGE, "Unknown player id"); } void TActionContainer::setPlayerSynchronized(const PLAYER_ID player_id) { if (player_id <= MAX_PLAYERS) _synchronized[player_id - 1] = true; else THROW(E_8K_OUT_OF_RANGE, "Unknown player id"); } bool TActionContainer::arePlayersSynchronized() { for (int i = 0; i < MAX_PLAYERS; i++) if (_synchronized[i] == false) return false; return true; } THistory::THistory() { _maxId = 0; } THistory::THistory(TPlayerContainer & players) { for (TPlayerContainer::iterator i = players.begin(); i != players.end(); i++) _players.push_back(i->first); _maxId = 0; } void THistory::init(TPlayerContainer & players) { reset(); _players.clear(); for (TPlayerContainer::iterator i = players.begin(); i != players.end(); i++) _players.push_back(i->first); } void THistory::reset() { clear(); _maxId = 0; } ACTION_ID THistory::add(TAction * action) { _maxId++; insert(THistoryPair(_maxId, new TActionContainer(action, _players))); return _maxId; } ACTION_ID THistory::add(ACTION_TYPE type, void * data) { return add(new TAction(type, data)); } TActionContainer * THistory::get(const ACTION_ID action_id) { return (*this)[action_id]; } int THistory::maxId() { return _maxId; } TClientHistory::TClientHistory() { _maxId = 0; } TClientHistory::~TClientHistory() { } void TClientHistory::init() { } void TClientHistory::reset() { _maxId = 0; } ACTION_ID TClientHistory::add(ACTION_ID action_id, TAction * action) { // ulozim do historie operator[](action_id) = action; // aktualizuji pocitadlo if (action_id > (ACTION_ID)_maxId) _maxId = action_id; return _maxId; } bool TClientHistory::synchronized(ACTION_ID action_id) { return (count(action_id) > 0); }