QuestDialog.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #include "QuestDialog.h"
  2. #include <Fenster.h>
  3. #include <TextFeld.h>
  4. #include "Game.h"
  5. QuestDialog::QuestDialog(int playerId)
  6. : UIDialog("quests", playerId, 0)
  7. {
  8. QuestManager* zManager = Game::INSTANCE->zQuestManager();
  9. Framework::Text uiml = "<dialog id=\"quests\" title=\"Quests\" "
  10. "width=\"1200\" height=\"800\">";
  11. uiml.append()
  12. << "<listView id=\"collectionList\" width=\"200\" height=\"100%\" "
  13. "member-height=\"40\" align-top=\"start\" "
  14. "align-left=\"start\" onSelectMessage=\"quests;0\">";
  15. QuestParty* party = zManager->zParty(playerId);
  16. int index = 0;
  17. for (QuestCollection* questCollection : zManager->questCollections)
  18. {
  19. if (questCollection->isVisible(party, zManager))
  20. {
  21. uiml.append() << "<listItem id=\"quest_collection_" << index++
  22. << "\" collectionName=\""
  23. << questCollection->getName() << "\">"
  24. << questCollection->getName() << "</listItem>";
  25. }
  26. }
  27. uiml.append()
  28. << "</listView><questGraph id=\"visible_quest_graph\" width=\"1000\" "
  29. "align-left=\"collectionList\" "
  30. "height=\"100%\"/><frame id=\"questDetailView\" width=\"100%\" "
  31. "height=\"0%\" style=\""
  32. << (Framework::Fenster::Style::Titel
  33. | Framework::Fenster::Style::Erlaubt
  34. | Framework::Fenster::Style::Sichtbar)
  35. << "\" title-height=\"30\" title-font-size=\"20\"/></dialog>";
  36. this->uiml = new Framework::XML::Element(uiml);
  37. }
  38. void QuestDialog::api(
  39. Framework::StreamReader* zRequest, NetworkMessage* zResponse)
  40. {
  41. char typ;
  42. zRequest->lese(&typ, 1);
  43. switch (typ)
  44. {
  45. case 0: // onSelect Message of listView
  46. { // open quest graph
  47. int selection;
  48. zRequest->lese((char*)&selection, 4);
  49. auto questGraph = this->uiml->selectChildsByName("questGraph");
  50. questGraph.selectChildren().remove();
  51. auto listItems
  52. = uiml->selectChildsByName("listView").selectChildren();
  53. listItems.removeAttribute("selected");
  54. listItems
  55. .whereAttributeEquals(
  56. "id", Framework::Text("quest_collection_") + selection)
  57. .forEach([this, &questGraph](
  58. Framework::XML::Element* zElement) {
  59. zElement->setAttribute("selected", "");
  60. auto collectionName
  61. = zElement->getAttributeValue("collectionName");
  62. QuestManager* zManager = Game::INSTANCE->zQuestManager();
  63. auto questCollection
  64. = zManager->zCollection(collectionName);
  65. QuestParty* zParty = zManager->zParty(getPlayerId());
  66. for (Quest* quest : questCollection->quests)
  67. {
  68. if (quest->isVisible(zParty, zManager))
  69. {
  70. questGraph.addChild(
  71. getQuestGraphItem(quest, zParty));
  72. }
  73. }
  74. });
  75. update();
  76. break;
  77. }
  78. case 1: // onClick Message of questGraphItem
  79. { // open quest detail view
  80. unsigned char len;
  81. zRequest->lese((char*)&len, 1);
  82. char* id = new char[len + 1];
  83. zRequest->lese(id, len);
  84. id[len] = 0;
  85. Framework::Text collectionName
  86. = uiml->selectChildsByName("listView")
  87. .selectChildren()
  88. .whereAttributeExists("selected")
  89. .getFirstElement()
  90. .map<Framework::Text>([](auto element) {
  91. return element->getAttributeValue("collectionName");
  92. })
  93. .orElse("");
  94. QuestManager* zManager = Game::INSTANCE->zQuestManager();
  95. QuestCollection* questCollection
  96. = zManager->zCollection(collectionName);
  97. QuestParty* zParty = zManager->zParty(getPlayerId());
  98. for (Quest* quest : questCollection->quests)
  99. {
  100. if (quest->isActive(zParty)
  101. && quest->getQuestId().istGleich(id))
  102. {
  103. QuestStorage* zStorage
  104. = zParty->zQuestStorage(quest->questId);
  105. uiml->selectChildsByAttribute("id", "collectionList")
  106. .setAttribute("height", "0");
  107. uiml->selectChildsByAttribute("id", "visible_quest_graph")
  108. .setAttribute("height", "0");
  109. auto questView
  110. = uiml->selectChildsByAttribute("id", "questDetailView")
  111. .getFirstElement();
  112. questView->setAttribute("height", "100%");
  113. questView->setAttribute("questId", quest->questId);
  114. questView->removeAllChilds();
  115. showQuestDetails(quest,
  116. zStorage,
  117. (Framework::RCPointer<Framework::XML::Element>)
  118. questView);
  119. break;
  120. }
  121. }
  122. delete[] id;
  123. break;
  124. }
  125. case 2: // onClick Message of back_to_graph
  126. {
  127. auto detailView
  128. = uiml->selectChildsByAttribute("id", "questDetailView");
  129. detailView.removeAllChilds();
  130. detailView.setAttribute("height", "0");
  131. uiml->selectChildsByAttribute("id", "collectionList")
  132. .setAttribute("height", "100%");
  133. uiml->selectChildsByAttribute("id", "visible_quest_graph")
  134. .setAttribute("height", "100%");
  135. update();
  136. break;
  137. }
  138. case 3: // onClick Message of give_rewards
  139. {
  140. Framework::Text collectionName
  141. = uiml->selectChildsByName("listView")
  142. .selectChildren()
  143. .whereAttributeExists("selected")
  144. .getFirstElement()
  145. .map<Framework::Text>([](auto element) {
  146. return element->getAttributeValue("collectionName");
  147. })
  148. .orElse("");
  149. QuestManager* zManager = Game::INSTANCE->zQuestManager();
  150. QuestCollection* questCollection
  151. = zManager->zCollection(collectionName);
  152. QuestParty* zParty = zManager->zParty(getPlayerId());
  153. Framework::Text questId
  154. = uiml->selectChildsByAttribute("id", "questDetailView")
  155. .getFirstElement()
  156. ->getAttributeValue("questId");
  157. for (Quest* quest : questCollection->quests)
  158. {
  159. if (quest->getQuestId().istGleich(questId))
  160. {
  161. QuestStorage* zStorage
  162. = zParty->zQuestStorage(quest->questId);
  163. if (zStorage->isQuestFinished()
  164. && !zStorage->isQuestRewarded())
  165. {
  166. Framework::XML::Element* rewardContainer
  167. = (Framework::RCPointer<Framework::XML::Element>)
  168. uiml->selectChildren()
  169. .selectAllElements()
  170. .whereAttributeEquals(
  171. "id", "quest_rewards")
  172. .getFirstElement();
  173. bool valid = 1;
  174. for (auto reward : quest->rewards)
  175. {
  176. valid &= reward->validateSettings(
  177. rewardContainer, zStorage);
  178. }
  179. if (valid)
  180. {
  181. zStorage->setQuestRewarded(1);
  182. for (auto reward : quest->rewards)
  183. {
  184. reward->giveReward(rewardContainer,
  185. zStorage,
  186. Game::INSTANCE->zEntity(getPlayerId()));
  187. }
  188. auto questView = uiml->selectChildsByAttribute(
  189. "id", "questDetailView")
  190. .getFirstElement();
  191. questView->removeAllChilds();
  192. showQuestDetails(quest,
  193. zStorage,
  194. (Framework::RCPointer<Framework::XML::Element>)
  195. questView);
  196. }
  197. break;
  198. }
  199. }
  200. }
  201. break;
  202. }
  203. case 4: // requirement message
  204. {
  205. Framework::Text collectionName
  206. = uiml->selectChildsByName("listView")
  207. .selectChildren()
  208. .whereAttributeExists("selected")
  209. .getFirstElement()
  210. .map<Framework::Text>([](auto element) {
  211. return element->getAttributeValue("collectionName");
  212. })
  213. .orElse("");
  214. QuestManager* zManager = Game::INSTANCE->zQuestManager();
  215. QuestCollection* questCollection
  216. = zManager->zCollection(collectionName);
  217. QuestParty* zParty = zManager->zParty(getPlayerId());
  218. Framework::Text questId
  219. = uiml->selectChildsByAttribute("id", "questDetailView")
  220. .getFirstElement()
  221. ->getAttributeValue("questId");
  222. unsigned char len;
  223. zRequest->lese((char*)&len, 1);
  224. char* requirementId = new char[len + 1];
  225. requirementId[len] = 0;
  226. auto requirementsContainer
  227. = uiml->selectChildsByAttribute("id", "quest_requirements");
  228. if (requirementsContainer.exists())
  229. {
  230. for (Quest* quest : questCollection->quests)
  231. {
  232. if (quest->getQuestId().istGleich(questId))
  233. {
  234. for (QuestRequirement* requirement :
  235. quest->requirements)
  236. {
  237. if (requirement->getRequirementId().istGleich(
  238. requirementId))
  239. {
  240. requirement->api(zRequest,
  241. (RCPointer<Framework::XML::Element>)
  242. requirementsContainer.getFirstElement(),
  243. zParty->zQuestStorage(questId));
  244. }
  245. }
  246. }
  247. }
  248. }
  249. delete[] requirementId;
  250. break;
  251. }
  252. case 5: // reward message
  253. {
  254. Framework::Text collectionName
  255. = uiml->selectChildsByName("listView")
  256. .selectChildren()
  257. .whereAttributeExists("selected")
  258. .getFirstElement()
  259. .map<Framework::Text>([](auto element) {
  260. return element->getAttributeValue("collectionName");
  261. })
  262. .orElse("");
  263. QuestManager* zManager = Game::INSTANCE->zQuestManager();
  264. QuestCollection* questCollection
  265. = zManager->zCollection(collectionName);
  266. QuestParty* zParty = zManager->zParty(getPlayerId());
  267. Framework::Text questId
  268. = uiml->selectChildsByAttribute("id", "questDetailView")
  269. .getFirstElement()
  270. ->getAttributeValue("questId");
  271. unsigned char len;
  272. zRequest->lese((char*)&len, 1);
  273. char* reqardId = new char[len + 1];
  274. reqardId[len] = 0;
  275. auto requirementsContainer
  276. = uiml->selectChildsByAttribute("id", "quest_rewards");
  277. if (requirementsContainer.exists())
  278. {
  279. for (Quest* quest : questCollection->quests)
  280. {
  281. if (quest->getQuestId().istGleich(questId))
  282. {
  283. for (QuestReward* reward : quest->rewards)
  284. {
  285. if (reward->getRewardId().istGleich(reqardId))
  286. {
  287. reward->api(zRequest,
  288. (RCPointer<Framework::XML::Element>)
  289. requirementsContainer.getFirstElement(),
  290. zParty->zQuestStorage(questId));
  291. }
  292. }
  293. }
  294. }
  295. }
  296. delete[] reqardId;
  297. break;
  298. }
  299. }
  300. }
  301. Framework::XML::Element* QuestDialog::getQuestGraphItem(
  302. Quest* zQuest, QuestParty* zParty)
  303. {
  304. Framework::Text requirements = "";
  305. int index = 0;
  306. int currentGroup = 0;
  307. for (Framework::Text* requiredQuest : zQuest->requiredQuestsIds)
  308. {
  309. int group = zQuest->requiredQuestsGroups.get(index);
  310. if (index != 0)
  311. {
  312. if (group != currentGroup)
  313. {
  314. requirements += "||";
  315. currentGroup = group;
  316. }
  317. else
  318. {
  319. requirements += "&&";
  320. }
  321. }
  322. else
  323. {
  324. currentGroup = group;
  325. }
  326. requirements += *requiredQuest;
  327. index++;
  328. }
  329. if (!zQuest->isActive(zParty))
  330. {
  331. Framework::Text result = "<questGraphItem id=\"quest_";
  332. result.append()
  333. << zQuest->questId << "\" name=\"Unknown Quest\" image=\""
  334. << zQuest->imagePath
  335. << "\" description=\"You have to finish other quests befor this "
  336. "quest will be available.\" finished=\"false\" mainQuest=\""
  337. << zQuest->mainQuest << "\" requirements=\"" << requirements
  338. << "\" width=\"" << (zQuest->mainQuest ? "50" : "30")
  339. << "\" height=\"" << (zQuest->mainQuest ? "50" : "30") << "\"/>";
  340. return new Framework::XML::Element(result);
  341. }
  342. else
  343. {
  344. QuestStorage* zStorage = zParty->zQuestStorage(zQuest->questId);
  345. Framework::Text result = "<questGraphItem id=\"quest_";
  346. result.append() << zQuest->questId << "\" name=\""
  347. << zQuest->questName + "\" image=\""
  348. << zQuest->imagePath << "\" description=\""
  349. << zQuest->description << "\" finished=\""
  350. << zStorage->isQuestFinished() << "\" mainQuest=\""
  351. << zQuest->mainQuest << "\" requirements=\""
  352. << requirements << "\" width=\""
  353. << (zQuest->mainQuest ? "50" : "30") << "\" height=\""
  354. << (zQuest->mainQuest ? "50" : "30")
  355. << "\" onClick=\"quests;(char)1;" << zQuest->questId
  356. << "\"/>";
  357. return new Framework::XML::Element(result);
  358. }
  359. }
  360. void QuestDialog::showQuestDetails(
  361. Quest* zQuest, QuestStorage* zStorage, Framework::XML::Element* zParent)
  362. {
  363. zParent->setAttribute("title", zQuest->questName);
  364. zParent->addChild(new Framework::XML::Element(
  365. (((Framework::Text("<text id=\"quest_description\" width=\"50% - 15\" "
  366. "height=\"50% - 10\" "
  367. "align-left=\"start\" margin-left=\"10\" "
  368. "align-top=\"start\" margin-top=\"10\" border=\"1\" "
  369. "border-color=\"0xFF52525E\" vScroll=\"auto\" "
  370. "style=\"")
  371. += Framework::TextFeld::Style::Text
  372. | Framework::TextFeld::Style::Mehrzeilig
  373. | Framework::TextFeld::Style::AutoLineBreak)
  374. += "\">")
  375. += zQuest->description)
  376. += "</text>"));
  377. Framework::XML::Element* requirementsContainer
  378. = new Framework::XML::Element(
  379. "<frame id=\"quest_requirements\" align-left=\"quest_description\" "
  380. "margin-left=\"10\" "
  381. "align-top=\"start\" margin-top=\"10\" width=\"50% - 15\" "
  382. "height=\"100% - "
  383. "50\" vScroll=\"auto\" display=\"column\" gap=\"10\" border=\"1\" "
  384. "border-color=\"0xFF52525E\"/>");
  385. requirementsContainer->addChild(new Framework::XML::Element(
  386. "<text id=\"quest_requirements_title\" width=\"100%\" "
  387. "height=\"30\" text-align-horizontal=\"center\" "
  388. "text-align-vertical=\"center\" "
  389. "font-size=\"20\">Requirements</text>"));
  390. requirementsContainer->setAttribute("style",
  391. Framework::Text() += (Framework::Fenster::Style::Sichtbar
  392. | Framework::Fenster::Style::Erlaubt
  393. | Framework::Fenster::Style::Rahmen));
  394. for (QuestRequirement* requirement : zQuest->requirements)
  395. {
  396. requirement->addRequirementUIML(zStorage,
  397. requirementsContainer,
  398. Framework::Text("quests;(char)4;")
  399. += requirement->getRequirementId());
  400. }
  401. zParent->addChild(requirementsContainer);
  402. Framework::XML::Element* rewardsContainer = new Framework::XML::Element(
  403. "<frame id=\"quest_rewards\" align-left=\"start\" margin-left=\"10\" "
  404. "margin-top=\"10\" "
  405. "align-top=\"quest_description\" width=\"50% - 15\" height=\"50% - "
  406. "20\" vScroll=\"auto\" display=\"column\" gap=\"10\" border=\"1\" "
  407. "border-color=\"0xFF52525E\"/>");
  408. rewardsContainer->addChild(new Framework::XML::Element(
  409. "<text id=\"quest_rewards_title\" width=\"100%\" height=\"30\" "
  410. "text-align-horizontal=\"center\" text-align-vertical=\"center\" "
  411. "font-size=\"20\">Rewards</text>"));
  412. rewardsContainer->setAttribute("style",
  413. Framework::Text() += (Framework::Fenster::Style::Sichtbar
  414. | Framework::Fenster::Style::Erlaubt
  415. | Framework::Fenster::Style::Rahmen));
  416. for (QuestReward* reward : zQuest->rewards)
  417. {
  418. reward->addRewardUIML(rewardsContainer,
  419. zStorage,
  420. Framework::Text("quests;(char)5;") += reward->getRewardId());
  421. }
  422. zParent->addChild(rewardsContainer);
  423. zParent->addChild(new XML::Element(
  424. "<button id=\"back_to_graph\" width=\"100\" height=\"20\" "
  425. "align-right=\"end\" align-top=\"quest_requirements\" "
  426. "margin-right=\"10\" margin-top=\"10\" "
  427. "onClick=\"quests;(char)2\">Back</button>"));
  428. if (zStorage->isQuestFinished() && !zStorage->isQuestRewarded())
  429. {
  430. zParent->addChild(new XML::Element(
  431. "<button id=\"give_rewards\" width=\"100\" height=\"20\" "
  432. "align-left=\"quest_rewards\" align-top=\"quest_requirements\" "
  433. "margin-left=\"10\" margin-top=\"10\" "
  434. "onClick=\"quests;(char)3\">Give rewards</button>"));
  435. }
  436. update();
  437. }