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