Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions server/abstractcard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "abstractcard.h"

AbstractCard::AbstractCard(int id, std::string info):
_id(id), _info(info)
{

}

std::string AbstractCard::getInfo() const
{
return _info;
}

int AbstractCard::getId() const
{
return _id;
}
28 changes: 28 additions & 0 deletions server/abstractcard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef ABSTRACTCARD_H
#define ABSTRACTCARD_H

class Deck;

#include "config.h"
#include <string>

class AbstractCard
{
public:
AbstractCard(int id, std::string info);

int getId() const;
std::string getInfo() const;

virtual ENTITY_TYPE getEntityType() const = 0;
virtual int getStrength() const = 0;
virtual void setStrength(int strength) = 0;
virtual PROPERTY_TYPE getPropertyType() const = 0;

private:
int _id;
std::string _info;

};

#endif // ABSTRACTCARD_H
10 changes: 10 additions & 0 deletions server/cardfactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "cardfactory.h"

// счетчик сгенерированных карт
// для создания карт в партии с уникальным id
int CardFactory::cards = 0;

CardFactory::CardFactory()
{

}
19 changes: 19 additions & 0 deletions server/cardfactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef CARDFACTORY_H
#define CARDFACTORY_H

#include "abstractcard.h"
#include "config.h"

#include <memory>

class CardFactory
{
public:
CardFactory();
virtual std::shared_ptr<AbstractCard> createCard(ENTITY_TYPE entity_type, int strength, PROPERTY_TYPE property_type) = 0;
virtual ~CardFactory() {}

static int cards;
};

#endif // CARDFACTORY_H
79 changes: 79 additions & 0 deletions server/cardproperty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "cardproperty.h"

#include "field.h"
#include "player.h"
#include "abstractcard.h"
#include "config.h"

CardProperty::CardProperty()
{

}

void CardProperty::activate(std::shared_ptr<Player> player1,
std::shared_ptr<Player> player2,
std::shared_ptr<AbstractCard> card,
std::shared_ptr<Field> field)
{
switch (card->getPropertyType()) {
case Buff:
_buff(player1, card, field);
break;

case Spy:
_spy(player1, player2, card, field);
break;

case RollCall:
_rollCall(player1, card, field);
break;

default:
_default(player1, card, field);
break;
}
player1->ReestablishHand();
}

void CardProperty::_buff(std::shared_ptr<Player> player1,
std::shared_ptr<AbstractCard> card,
std::shared_ptr<Field> field) {

std::shared_ptr<AbstractCard> same_card = field->getDeck(player1)->findByEntityType(card->getEntityType());
if (same_card)
{
same_card->setStrength(same_card->getStrength() * Config::Instance().BUFF_RATE);
card->setStrength(card->getStrength() * Config::Instance().BUFF_RATE);
}
field->addCardToDeck(player1, card);
}

void CardProperty::_rollCall(std::shared_ptr<Player> player1,
std::shared_ptr<AbstractCard> card,
std::shared_ptr<Field> field) {

field->addCardToDeck(player1, card);
auto newCard = player1->removeFromDeckByEntityType(card->getEntityType());
if (newCard) {
field->addCardToDeck(player1, newCard);
}
}

void CardProperty::_spy(std::shared_ptr<Player> player1,
std::shared_ptr<Player> player2,
std::shared_ptr<AbstractCard> card,
std::shared_ptr<Field> field) {

field->addCardToDeck(player2, card);
player1->addCardToHand();
player1->addCardToHand();

}

void CardProperty::_default(std::shared_ptr<Player> player1,
std::shared_ptr<AbstractCard> card,
std::shared_ptr<Field> field) {

field->addCardToDeck(player1, card);

}
35 changes: 35 additions & 0 deletions server/cardproperty.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef CARDPROPERTY_H
#define CARDPROPERTY_H

class Field;
class Player;
class AbstractCard;

#include <memory>

class CardProperty
{
public:
CardProperty();
void activate(std::shared_ptr<Player> player1,
std::shared_ptr<Player> player2,
std::shared_ptr<AbstractCard> card,
std::shared_ptr<Field> field);
private:
void _buff(std::shared_ptr<Player> player1,
std::shared_ptr<AbstractCard> card,
std::shared_ptr<Field> field);
void _rollCall(std::shared_ptr<Player> player1,
std::shared_ptr<AbstractCard> card,
std::shared_ptr<Field> field);
void _spy(std::shared_ptr<Player> player1,
std::shared_ptr<Player> player2,
std::shared_ptr<AbstractCard> card,
std::shared_ptr<Field> field);

void _default(std::shared_ptr<Player> player1,
std::shared_ptr<AbstractCard> card,
std::shared_ptr<Field> field);
};

#endif // CARDPROPERTY_H
29 changes: 29 additions & 0 deletions server/config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "config.h"

Config::Config(const std::string& filename):
_xml(ConfigParser(filename)),
_entityInfo(_xml.getEntitiesInfo()),
_propertyInfo(_xml.getPropertiesInfo()),
PORT(_xml.getPort()),
HAND_SIZE(_xml.getHandSize()),
DECK_SIZE(_xml.getDeckSize()),
MAX_STRENGTH(_xml.getMaxStrength()),
BUFF_RATE(_xml.getBuffRate()),
WIN_SCORE(_xml.getWinScore())

{}

std::string Config::getInfo(ENTITY_TYPE entityType) const
{
return _entityInfo.at(entityType);
}

std::string Config::getInfo(PROPERTY_TYPE propertyType) const
{
return _propertyInfo.at(propertyType);
}

std::string Config::getInfo(ENTITY_TYPE entityType, PROPERTY_TYPE propertyType) const
{
return getInfo(entityType) + getInfo(propertyType);
}
67 changes: 67 additions & 0 deletions server/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef CONFIG_H
#define CONFIG_H

#include "configparser.h"

#include <string>
#include <map>

enum PROPERTY_TYPE: int {
NO_PROPERTY,
Buff,
Spy,
RollCall,
PROPERTIES};

enum ENTITY_TYPE: int {
NO_ENTITY, BEAR,
AGLAIS, BLUE_BOY,
CERYS, CIRI,
CORAL, EITHNEL,
EMISSARY, ENCHANT,
EREDIN, GERALT,
GREEN, IDA,
IORVETH, KEIRA,
LAMIA, MORENN,
PIRATEL, ROAR_BEAR,
SCOUT, SIREN,
STENNIS, SUPPORT,
TEMERIAN, TRIDAM,
TRISS, YENNEFER,
YOANA, ZIGRIN,
ZOLTAN,
ENTITIES
};


//singleton
class Config {
private:
ConfigParser _xml;

Config(const std::string& filename);
Config(Config const&) = delete;
Config& operator= (Config const&) = delete;

std::map<ENTITY_TYPE, std::string> _entityInfo;
std::map<PROPERTY_TYPE, std::string> _propertyInfo;

public:
static Config& Instance(const std::string& filename = "") {
static Config instance(filename);
return instance;
}

const int PORT;
const int HAND_SIZE;
const int DECK_SIZE;
const int MAX_STRENGTH;
const int BUFF_RATE;
const int WIN_SCORE;

std::string getInfo(ENTITY_TYPE) const;
std::string getInfo(PROPERTY_TYPE) const;
std::string getInfo(ENTITY_TYPE, PROPERTY_TYPE) const;

};
#endif // CONFIG_H
91 changes: 91 additions & 0 deletions server/configparser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "configparser.h"
#include "config.h"
#include "log.h"

#include "pugixml.h"
#include "pugiconfig.h"

ConfigParser::ConfigParser(std::string xml): doc(std::make_shared<pugi::xml_document>())

{
doc->load_file(xml.c_str());
if (doc->empty())
Log::Instance().error("Config load - fail");
else
Log::Instance().log("Config load - success");
}

ConfigParser::~ConfigParser() {}

int ConfigParser::_parseGetInt2(std::string ancestor, std::string parent) const
{
pugi::xml_node node = doc->child(ancestor.c_str()).child(parent.c_str());
if (!node.empty()){
Log::Instance().log(ancestor + " " + parent + " parsing " + std::to_string(node.text().as_int()));
return node.text().as_int();
}

Log::Instance().error(ancestor + " " + parent + " parsing - fail");
return 0;
}

int ConfigParser::getPort() const {

return _parseGetInt2("server", "port");
}

int ConfigParser::getHandSize() const
{
return _parseGetInt2("server", "handsize");
}

int ConfigParser::getDeckSize() const
{
return _parseGetInt2("server", "decksize");
}

int ConfigParser::getMaxStrength() const
{
return _parseGetInt2("server", "maxstrength");
}

int ConfigParser::getBuffRate() const {
return _parseGetInt2("server", "buffrate");
}

int ConfigParser::getWinScore() const {
return _parseGetInt2("server", "winscore");
}

std::map<ENTITY_TYPE, std::string> ConfigParser::getEntitiesInfo() const
{

std::map<ENTITY_TYPE, std::string> buff;
int entity = NO_ENTITY;
pugi::xml_node entities = doc->child("server").child("entities");

if (!entities.empty())
for (pugi::xml_node entityNode: entities.children()) {
buff.insert(std::make_pair((ENTITY_TYPE)entity++, entityNode.text().as_string()));
}
else
Log::Instance().error("Entities parsing - fail");

return buff;
}

std::map<PROPERTY_TYPE, std::string> ConfigParser::getPropertiesInfo() const {
std::map<PROPERTY_TYPE, std::string> buff;
int property = NO_PROPERTY;
pugi::xml_node properties = doc->child("server").child("properties");

if (!properties.empty()){
for (pugi::xml_node propetyNode: properties.children())
buff.insert(std::make_pair((PROPERTY_TYPE)property++, propetyNode.text().as_string()));
}
else
Log::Instance().error("Properties parsing - fail");

return buff;
}

Loading