QuestReward.cpp 6.9 KB

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