QuestReward.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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->setAttribute(
  129. "id", (Framework::Text("reward_") += rewardId) += "_item_description");
  130. text->setText((Framework::Text(info->getCount()) += " ")
  131. += info->zItem()->getName());
  132. text->setAttribute("align-left", stack->getAttributeValue("id"));
  133. text->setAttribute("align-y", stack->getAttributeValue("id"));
  134. container->addChild(stack);
  135. container->addChild(text);
  136. if (zStorage->containsKey( (Framework::Text("reward_") += rewardId) += "_given_to"))
  137. {
  138. auto givenTo = new Framework::XML::Element(
  139. "<text width=\"auto\" height=\"auto\" margin-top=\"10\" text-color=\"0xFF00FF00\"/>");
  140. auto name = zStorage->zValue(
  141. (Framework::Text("reward_") += rewardId) += "_given_to")->asString();
  142. givenTo->setText((Framework::Text("Given to: ") += name->getString()));
  143. givenTo->setAttribute("align-top", text->getAttributeValue("id"));
  144. givenTo->setAttribute("align-x", text->getAttributeValue("id"));
  145. container->addChild(givenTo);
  146. }
  147. }
  148. zParent->addChild(container);
  149. }
  150. QuestRewardGiveItemsType::QuestRewardGiveItemsType()
  151. : SubTypeFactory<QuestReward, QuestRewardGiveItems>()
  152. {}
  153. QuestRewardGiveItems* QuestRewardGiveItemsType::fromJson(
  154. Framework::JSON::JSONObject* zJson) const
  155. {
  156. QuestRewardGiveItems* result = new QuestRewardGiveItems(
  157. zJson->zValue("rewardId")->asString()->getString());
  158. Framework::JSON::JSONArray* itemsJson = zJson->zValue("items")->asArray();
  159. for (Framework::JSON::JSONValue* itemJson : *itemsJson)
  160. {
  161. result->items.add(
  162. Game::INSTANCE->zTypeRegistry()->fromJson<ItemStackInfo>(itemJson));
  163. }
  164. return result;
  165. }
  166. Framework::JSON::JSONObject* QuestRewardGiveItemsType::toJson(
  167. QuestRewardGiveItems* zObject) const
  168. {
  169. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  170. result->addValue(
  171. "rewardId", new Framework::JSON::JSONString(zObject->getRewardId()));
  172. Framework::JSON::JSONArray* itemsJson = new Framework::JSON::JSONArray();
  173. for (ItemStackInfo* item : zObject->items)
  174. {
  175. itemsJson->addValue(Game::INSTANCE->zTypeRegistry()->toJson(item));
  176. }
  177. result->addValue("items", itemsJson);
  178. return result;
  179. }
  180. Framework::JSON::Validator::JSONValidator*
  181. QuestRewardGiveItemsType::getValidator(
  182. Framework::JSON::Validator::ObjectValidationBuilder<
  183. Framework::JSON::Validator::JSONValidator>* builder) const
  184. {
  185. return builder->withRequiredString("rewardId")
  186. ->finishString()
  187. ->withRequiredArray("items")
  188. ->addAcceptedTypeInArray(
  189. Game::INSTANCE->zTypeRegistry()->getValidator<ItemStackInfo>())
  190. ->finishArray()
  191. ->finishObject();
  192. }
  193. Framework::Text QuestRewardGiveItemsType::getTypeToken() const
  194. {
  195. return "give_items";
  196. }