QuestReward.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include "QuestReward.h"
  2. #include <Fenster.h>
  3. #include "Entity.h"
  4. #include "Game.h"
  5. #include "Player.h"
  6. #include "Quest.h"
  7. QuestReward::QuestReward()
  8. : ReferenceCounter()
  9. {}
  10. bool QuestReward::validateSettings(
  11. Framework::XML::Element* zParent, QuestStorage* zStorage)
  12. {
  13. return true;
  14. }
  15. void QuestReward::api(Framework::StreamReader* message,
  16. Framework::XML::Element* zParent,
  17. QuestStorage* zStorage)
  18. {}
  19. void QuestReward::setRewardId(Framework::Text rewardId)
  20. {
  21. this->rewardId = rewardId;
  22. }
  23. const Framework::Text& QuestReward::getRewardId() const
  24. {
  25. return rewardId;
  26. }
  27. ItemStackInfo::ItemStackInfo()
  28. : ReferenceCounter(),
  29. item(0),
  30. count(0)
  31. {}
  32. ItemStackInfo::~ItemStackInfo()
  33. {
  34. if (item) item->release();
  35. }
  36. void ItemStackInfo::setItem(Item* item)
  37. {
  38. if (this->item) this->item->release();
  39. this->item = item;
  40. }
  41. Item* ItemStackInfo::zItem() const
  42. {
  43. return item;
  44. }
  45. void ItemStackInfo::setCount(int count)
  46. {
  47. this->count = count;
  48. }
  49. int ItemStackInfo::getCount() const
  50. {
  51. return count;
  52. }
  53. ItemStackInfoType::ItemStackInfoType()
  54. : TypeFactory<ItemStackInfo>()
  55. {}
  56. ItemStackInfo* ItemStackInfoType::createValue(
  57. Framework::JSON::JSONObject* zJson) const
  58. {
  59. return new ItemStackInfo();
  60. }
  61. void ItemStackInfoType::fromJson(
  62. ItemStackInfo* zObject, Framework::JSON::JSONObject* zJson) const
  63. {
  64. zObject->setItem(Game::INSTANCE->zTypeRegistry()->fromJson<Item>(
  65. zJson->asObject()->zValue("item")));
  66. zObject->setCount(
  67. (int)zJson->asObject()->zValue("count")->asNumber()->getNumber());
  68. }
  69. void ItemStackInfoType::toJson(
  70. ItemStackInfo* zObject, Framework::JSON::JSONObject* zResult) const
  71. {
  72. zResult->addValue(
  73. "item", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zItem()));
  74. zResult->addValue(
  75. "count", new Framework::JSON::JSONNumber((double)zObject->getCount()));
  76. }
  77. JSONObjectValidationBuilder* ItemStackInfoType::addToValidator(
  78. JSONObjectValidationBuilder* builder) const
  79. {
  80. return builder
  81. ->withRequiredAttribute(
  82. "item", Game::INSTANCE->zTypeRegistry()->getValidator<Item>())
  83. ->withRequiredNumber("count")
  84. ->withDefault(1.0)
  85. ->whichIsGreaterOrEqual(1.0)
  86. ->finishNumber();
  87. }
  88. QuestRewardGiveItems::QuestRewardGiveItems()
  89. : QuestReward()
  90. {}
  91. void QuestRewardGiveItems::giveReward(Framework::XML::Element* zParent,
  92. QuestStorage* zStorage,
  93. Entity* zTargetEntity)
  94. {
  95. Player* p = dynamic_cast<Player*>(zTargetEntity);
  96. if (p)
  97. {
  98. zStorage->putValue(
  99. (Framework::Text("reward_") += rewardId) += "_given_to",
  100. new Framework::JSON::JSONString(p->getName()));
  101. }
  102. for (ItemStackInfo* info : items)
  103. {
  104. ItemStack* stack = new ItemStack(
  105. dynamic_cast<Item*>(info->zItem()->getThis()), info->getCount());
  106. zTargetEntity->unsaveAddItem(stack, Direction::NO_DIRECTION, 0);
  107. if (stack->getSize() > 0)
  108. {
  109. Game::INSTANCE->spawnItem(zTargetEntity->getLocation(),
  110. zTargetEntity->getDimensionId(),
  111. stack);
  112. }
  113. else
  114. {
  115. stack->release();
  116. }
  117. }
  118. }
  119. void QuestRewardGiveItems::addRewardUIML(Framework::XML::Element* zParent,
  120. QuestStorage* zStorage,
  121. Framework::Text onClickPrefix)
  122. {
  123. Framework::XML::Element* container
  124. = new Framework::XML::Element("<frame width=\"100%\" height=\"auto\" "
  125. "display=\"column\" gap=\"10\"/>");
  126. container->setAttribute("style",
  127. Framework::Text() += (Framework::Fenster::Style::Sichtbar
  128. | Framework::Fenster::Style::Erlaubt));
  129. container->setAttribute("id", Framework::Text("reward_") += rewardId);
  130. container->addChild(new Framework::XML::Element(
  131. "<text width=\"auto\" height=\"auto\">Item Reward:</text>"));
  132. for (ItemStackInfo* info : items)
  133. {
  134. auto stack = new Framework::XML::Element(
  135. "<itemStack width=\"50\" height=\"50\"/>");
  136. stack->setAttribute(
  137. "id", (Framework::Text("reward_") += rewardId) += "_item_stack");
  138. stack->setAttribute("count", info->getCount());
  139. stack->setAttribute("type", info->zItem()->getTypeId());
  140. stack->addChild(
  141. new Framework::XML::Element(info->zItem()->getTooltipUIML()));
  142. auto text = new Framework::XML::Element(
  143. "<text margin-left=\"10\" width=\"auto\" height=\"auto\"/>");
  144. text->setAttribute("id",
  145. (Framework::Text("reward_") += rewardId) += "_item_description");
  146. text->setText((Framework::Text(info->getCount()) += " ")
  147. += info->zItem()->getName());
  148. text->setAttribute("align-left", stack->getAttributeValue("id"));
  149. text->setAttribute("align-y", stack->getAttributeValue("id"));
  150. container->addChild(stack);
  151. container->addChild(text);
  152. if (zStorage->containsKey(
  153. (Framework::Text("reward_") += rewardId) += "_given_to"))
  154. {
  155. auto givenTo = new Framework::XML::Element(
  156. "<text width=\"auto\" height=\"auto\" margin-top=\"10\" "
  157. "text-color=\"0xFF00FF00\"/>");
  158. auto name = zStorage
  159. ->zValue((Framework::Text("reward_") += rewardId)
  160. += "_given_to")
  161. ->asString();
  162. givenTo->setText(
  163. (Framework::Text("Given to: ") += name->getString()));
  164. givenTo->setAttribute("align-top", text->getAttributeValue("id"));
  165. givenTo->setAttribute("align-x", text->getAttributeValue("id"));
  166. container->addChild(givenTo);
  167. }
  168. }
  169. zParent->addChild(container);
  170. }
  171. QuestRewardGiveItemsType::QuestRewardGiveItemsType()
  172. : QuestRewardFactoryBase()
  173. {}
  174. QuestRewardGiveItems* QuestRewardGiveItemsType::createValue(
  175. Framework::JSON::JSONObject* zJson) const
  176. {
  177. return new QuestRewardGiveItems();
  178. }
  179. void QuestRewardGiveItemsType::fromJson(
  180. QuestRewardGiveItems* zResult, Framework::JSON::JSONObject* zJson) const
  181. {
  182. Framework::JSON::JSONArray* itemsJson = zJson->zValue("items")->asArray();
  183. for (Framework::JSON::JSONValue* itemJson : *itemsJson)
  184. {
  185. zResult->items.add(
  186. Game::INSTANCE->zTypeRegistry()->fromJson<ItemStackInfo>(itemJson));
  187. }
  188. QuestRewardFactoryBase::fromJson(zResult, zJson);
  189. }
  190. void QuestRewardGiveItemsType::toJson(
  191. QuestRewardGiveItems* zObject, Framework::JSON::JSONObject* zResult) const
  192. {
  193. zResult->addValue(
  194. "rewardId", new Framework::JSON::JSONString(zObject->getRewardId()));
  195. Framework::JSON::JSONArray* itemsJson = new Framework::JSON::JSONArray();
  196. for (ItemStackInfo* item : zObject->items)
  197. {
  198. itemsJson->addValue(Game::INSTANCE->zTypeRegistry()->toJson(item));
  199. }
  200. zResult->addValue("items", itemsJson);
  201. QuestRewardFactoryBase::toJson(zObject, zResult);
  202. }
  203. JSONObjectValidationBuilder* QuestRewardGiveItemsType::addToValidator(
  204. JSONObjectValidationBuilder* builder) const
  205. {
  206. return QuestRewardFactoryBase::addToValidator(
  207. builder->withRequiredArray("items")
  208. ->addAcceptedTypeInArray(
  209. Game::INSTANCE->zTypeRegistry()->getValidator<ItemStackInfo>())
  210. ->finishArray());
  211. }
  212. Framework::Text QuestRewardGiveItemsType::getTypeToken() const
  213. {
  214. return "give_items";
  215. }