#include "QuestReward.h" #include "Entity.h" #include "Game.h" QuestReward::QuestReward(Framework::Text rewardId) : ReferenceCounter(), rewardId(rewardId) {} const Framework::Text& QuestReward::getRewardId() const { return rewardId; } ItemStackInfo::ItemStackInfo(Item* item, int count) : ReferenceCounter(), item(item), count(count) {} ItemStackInfo::~ItemStackInfo() { item->release(); } Item* ItemStackInfo::zItem() const { return item; } int ItemStackInfo::getCount() const { return count; } ItemStackInfoType::ItemStackInfoType() : TypeFactory() {} ItemStackInfo* ItemStackInfoType::fromJson( Framework::JSON::JSONValue* zJson) const { return new ItemStackInfo(Game::INSTANCE->zTypeRegistry()->fromJson( zJson->asObject()->zValue("item")), (int)zJson->asObject()->zValue("count")->asNumber()->getNumber()); } Framework::JSON::JSONValue* ItemStackInfoType::toJson( ItemStackInfo* zObject) const { Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); result->addValue( "item", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zItem())); result->addValue( "count", new Framework::JSON::JSONNumber((double)zObject->getCount())); return result; } Framework::JSON::Validator::JSONValidator* ItemStackInfoType::getValidator() const { return Framework::JSON::Validator::JSONValidator::buildForObject() ->withRequiredAttribute( "item", Game::INSTANCE->zTypeRegistry()->getValidator()) ->withRequiredNumber("count") ->withDefault(1.0) ->whichIsGreaterOrEqual(1.0) ->finishNumber() ->finishObject(); } QuestRewardGiveItems::QuestRewardGiveItems(Framework::Text rewardId) : QuestReward(rewardId) {} void QuestRewardGiveItems::giveReward(Entity* zTargetEntity) { for (ItemStackInfo* info : items) { ItemStack* stack = new ItemStack( dynamic_cast(info->zItem()->getThis()), info->getCount()); zTargetEntity->unsaveAddItem(stack, Direction::NO_DIRECTION, 0); if (stack->getSize() > 0) { Game::INSTANCE->spawnItem(zTargetEntity->getLocation(), zTargetEntity->getDimensionId(), stack); } else { stack->release(); } } } QuestRewardGiveItemsType::QuestRewardGiveItemsType() : SubTypeFactory() {} QuestRewardGiveItems* QuestRewardGiveItemsType::fromJson( Framework::JSON::JSONObject* zJson) const { QuestRewardGiveItems* result = new QuestRewardGiveItems( zJson->zValue("rewardId")->asString()->getString()); Framework::JSON::JSONArray* itemsJson = zJson->zValue("items")->asArray(); for (Framework::JSON::JSONValue* itemJson : *itemsJson) { result->items.add( Game::INSTANCE->zTypeRegistry()->fromJson(itemJson)); } return result; } Framework::JSON::JSONObject* QuestRewardGiveItemsType::toJson( QuestRewardGiveItems* zObject) const { Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); result->addValue( "rewardId", new Framework::JSON::JSONString(zObject->getRewardId())); Framework::JSON::JSONArray* itemsJson = new Framework::JSON::JSONArray(); for (ItemStackInfo* item : zObject->items) { itemsJson->addValue(Game::INSTANCE->zTypeRegistry()->toJson(item)); } result->addValue("items", itemsJson); return result; } Framework::JSON::Validator::JSONValidator* QuestRewardGiveItemsType::getValidator( Framework::JSON::Validator::ObjectValidationBuilder< Framework::JSON::Validator::JSONValidator>* builder) const { return builder->withRequiredString("rewardId") ->finishString() ->withRequiredArray("items") ->addAcceptedTypeInArray( Game::INSTANCE->zTypeRegistry()->getValidator()) ->finishArray() ->finishObject(); } Framework::Text QuestRewardGiveItemsType::getTypeToken() const { return "give_items"; }