QuestReward.cpp 7.3 KB

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