QuestDialog.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. { // disjunctive normal form
  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 += Framework::Text("quest_") + *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\" "
  337. "rewarded=\"false\" mainQuest=\""
  338. << zQuest->mainQuest << "\" requirements=\"" << requirements
  339. << "\" width=\"" << (zQuest->mainQuest ? "50" : "30")
  340. << "\" height=\"" << (zQuest->mainQuest ? "50" : "30") << "\"/>";
  341. return new Framework::XML::Element(result);
  342. }
  343. else
  344. {
  345. QuestStorage* zStorage = zParty->zQuestStorage(zQuest->questId);
  346. Framework::Text result = "<questGraphItem id=\"quest_";
  347. result.append() << zQuest->questId << "\" name=\""
  348. << zQuest->questName + "\" image=\""
  349. << zQuest->imagePath << "\" description=\""
  350. << zQuest->description << "\" finished=\""
  351. << zStorage->isQuestFinished() << "\" rewarded=\""
  352. << zStorage->isQuestRewarded() << "\" mainQuest=\""
  353. << zQuest->mainQuest << "\" requirements=\""
  354. << requirements << "\" width=\""
  355. << (zQuest->mainQuest ? "50" : "30") << "\" height=\""
  356. << (zQuest->mainQuest ? "50" : "30")
  357. << "\" onClick=\"quests;(char)1;" << zQuest->questId
  358. << "\"/>";
  359. return new Framework::XML::Element(result);
  360. }
  361. }
  362. void QuestDialog::showQuestDetails(
  363. Quest* zQuest, QuestStorage* zStorage, Framework::XML::Element* zParent)
  364. {
  365. zParent->setAttribute("title", zQuest->questName);
  366. zParent->addChild(new Framework::XML::Element(
  367. (((Framework::Text("<text id=\"quest_description\" width=\"50% - 15\" "
  368. "height=\"50% - 10\" "
  369. "align-left=\"start\" margin-left=\"10\" "
  370. "align-top=\"start\" margin-top=\"10\" border=\"1\" "
  371. "border-color=\"0xFF52525E\" vScroll=\"auto\" "
  372. "style=\"")
  373. += Framework::TextFeld::Style::Text
  374. | Framework::TextFeld::Style::Mehrzeilig
  375. | Framework::TextFeld::Style::AutoLineBreak)
  376. += "\">")
  377. += zQuest->description)
  378. += "</text>"));
  379. Framework::XML::Element* requirementsContainer
  380. = new Framework::XML::Element(
  381. "<frame id=\"quest_requirements\" align-left=\"quest_description\" "
  382. "margin-left=\"10\" "
  383. "align-top=\"start\" margin-top=\"10\" width=\"50% - 15\" "
  384. "height=\"100% - "
  385. "50\" vScroll=\"auto\" display=\"column\" gap=\"10\" border=\"1\" "
  386. "border-color=\"0xFF52525E\"/>");
  387. requirementsContainer->addChild(new Framework::XML::Element(
  388. "<text id=\"quest_requirements_title\" width=\"100%\" "
  389. "height=\"30\" text-align-horizontal=\"center\" "
  390. "text-align-vertical=\"center\" "
  391. "font-size=\"20\">Requirements</text>"));
  392. requirementsContainer->setAttribute("style",
  393. Framework::Text() += (Framework::Fenster::Style::Sichtbar
  394. | Framework::Fenster::Style::Erlaubt
  395. | Framework::Fenster::Style::Rahmen));
  396. for (QuestRequirement* requirement : zQuest->requirements)
  397. {
  398. requirement->addRequirementUIML(zStorage,
  399. requirementsContainer,
  400. Framework::Text("quests;(char)4;")
  401. += requirement->getRequirementId());
  402. }
  403. zParent->addChild(requirementsContainer);
  404. Framework::XML::Element* rewardsContainer = new Framework::XML::Element(
  405. "<frame id=\"quest_rewards\" align-left=\"start\" margin-left=\"10\" "
  406. "margin-top=\"10\" "
  407. "align-top=\"quest_description\" width=\"50% - 15\" height=\"50% - "
  408. "20\" vScroll=\"auto\" display=\"column\" gap=\"10\" border=\"1\" "
  409. "border-color=\"0xFF52525E\"/>");
  410. rewardsContainer->addChild(new Framework::XML::Element(
  411. "<text id=\"quest_rewards_title\" width=\"100%\" height=\"30\" "
  412. "text-align-horizontal=\"center\" text-align-vertical=\"center\" "
  413. "font-size=\"20\">Rewards</text>"));
  414. rewardsContainer->setAttribute("style",
  415. Framework::Text() += (Framework::Fenster::Style::Sichtbar
  416. | Framework::Fenster::Style::Erlaubt
  417. | Framework::Fenster::Style::Rahmen));
  418. for (QuestReward* reward : zQuest->rewards)
  419. {
  420. reward->addRewardUIML(rewardsContainer,
  421. zStorage,
  422. Framework::Text("quests;(char)5;") += reward->getRewardId());
  423. }
  424. zParent->addChild(rewardsContainer);
  425. zParent->addChild(new XML::Element(
  426. "<button id=\"back_to_graph\" width=\"100\" height=\"20\" "
  427. "align-right=\"end\" align-top=\"quest_requirements\" "
  428. "margin-right=\"10\" margin-top=\"10\" "
  429. "onClick=\"quests;(char)2\">Back</button>"));
  430. if (zStorage->isQuestFinished() && !zStorage->isQuestRewarded())
  431. {
  432. zParent->addChild(new XML::Element(
  433. "<button id=\"give_rewards\" width=\"100\" height=\"20\" "
  434. "align-left=\"quest_rewards\" align-top=\"quest_requirements\" "
  435. "margin-left=\"10\" margin-top=\"10\" "
  436. "onClick=\"quests;(char)3\">Give rewards</button>"));
  437. }
  438. update();
  439. }