123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- #include "QuestDialog.h"
- #include <Fenster.h>
- #include <TextFeld.h>
- #include "Game.h"
- QuestDialog::QuestDialog(int playerId)
- : UIDialog("quests", playerId, 0)
- {
- QuestManager* zManager = Game::INSTANCE->zQuestManager();
- Framework::Text uiml = "<dialog id=\"quests\" title=\"Quests\" "
- "width=\"1200\" height=\"800\">";
- uiml.append()
- << "<listView id=\"collectionList\" width=\"200\" height=\"100%\" "
- "member-height=\"40\" align-top=\"start\" "
- "align-left=\"start\" onSelectMessage=\"quests;0\">";
- QuestParty* party = zManager->zParty(playerId);
- int index = 0;
- for (QuestCollection* questCollection : zManager->questCollections)
- {
- if (questCollection->isVisible(party, zManager))
- {
- uiml.append() << "<listItem id=\"quest_collection_" << index++
- << "\" collectionName=\""
- << questCollection->getName() << "\">"
- << questCollection->getName() << "</listItem>";
- }
- }
- uiml.append()
- << "</listView><questGraph id=\"visible_quest_graph\" width=\"1000\" "
- "align-left=\"collectionList\" "
- "height=\"100%\"/><frame id=\"questDetailView\" width=\"100%\" "
- "height=\"0%\" style=\""
- << (Framework::Fenster::Style::Titel
- | Framework::Fenster::Style::Erlaubt
- | Framework::Fenster::Style::Sichtbar)
- << "\" title-height=\"30\" title-font-size=\"20\"/></dialog>";
- this->uiml = new Framework::XML::Element(uiml);
- }
- void QuestDialog::api(
- Framework::StreamReader* zRequest, NetworkMessage* zResponse)
- {
- char typ;
- zRequest->lese(&typ, 1);
- switch (typ)
- {
- case 0: // onSelect Message of listView
- { // open quest graph
- int selection;
- zRequest->lese((char*)&selection, 4);
- auto questGraph = this->uiml->selectChildsByName("questGraph");
- questGraph.selectChildren().remove();
- auto listItems
- = uiml->selectChildsByName("listView").selectChildren();
- listItems.removeAttribute("selected");
- listItems
- .whereAttributeEquals(
- "id", Framework::Text("quest_collection_") + selection)
- .forEach([this, &questGraph](
- Framework::XML::Element* zElement) {
- zElement->setAttribute("selected", "");
- auto collectionName
- = zElement->getAttributeValue("collectionName");
- QuestManager* zManager = Game::INSTANCE->zQuestManager();
- auto questCollection
- = zManager->zCollection(collectionName);
- QuestParty* zParty = zManager->zParty(getPlayerId());
- for (Quest* quest : questCollection->quests)
- {
- if (quest->isVisible(zParty, zManager))
- {
- questGraph.addChild(
- getQuestGraphItem(quest, zParty));
- }
- }
- });
- update();
- break;
- }
- case 1: // onClick Message of questGraphItem
- { // open quest detail view
- unsigned char len;
- zRequest->lese((char*)&len, 1);
- char* id = new char[len + 1];
- zRequest->lese(id, len);
- id[len] = 0;
- Framework::Text collectionName
- = uiml->selectChildsByName("listView")
- .selectChildren()
- .whereAttributeExists("selected")
- .getFirstElement()
- .map<Framework::Text>([](auto element) {
- return element->getAttributeValue("collectionName");
- })
- .orElse("");
- QuestManager* zManager = Game::INSTANCE->zQuestManager();
- QuestCollection* questCollection
- = zManager->zCollection(collectionName);
- QuestParty* zParty = zManager->zParty(getPlayerId());
- for (Quest* quest : questCollection->quests)
- {
- if (quest->isActive(zParty)
- && quest->getQuestId().istGleich(id))
- {
- QuestStorage* zStorage
- = zParty->zQuestStorage(quest->questId);
- uiml->selectChildsByAttribute("id", "collectionList")
- .setAttribute("height", "0");
- uiml->selectChildsByAttribute("id", "visible_quest_graph")
- .setAttribute("height", "0");
- auto questView
- = uiml->selectChildsByAttribute("id", "questDetailView")
- .getFirstElement();
- questView->setAttribute("height", "100%");
- questView->setAttribute("questId", quest->questId);
- questView->removeAllChilds();
- showQuestDetails(quest,
- zStorage,
- (Framework::RCPointer<Framework::XML::Element>)
- questView);
- break;
- }
- }
- delete[] id;
- break;
- }
- case 2: // onClick Message of back_to_graph
- {
- auto detailView
- = uiml->selectChildsByAttribute("id", "questDetailView");
- detailView.removeAllChilds();
- detailView.setAttribute("height", "0");
- uiml->selectChildsByAttribute("id", "collectionList")
- .setAttribute("height", "100%");
- uiml->selectChildsByAttribute("id", "visible_quest_graph")
- .setAttribute("height", "100%");
- update();
- break;
- }
- case 3: // onClick Message of give_rewards
- {
- Framework::Text collectionName
- = uiml->selectChildsByName("listView")
- .selectChildren()
- .whereAttributeExists("selected")
- .getFirstElement()
- .map<Framework::Text>([](auto element) {
- return element->getAttributeValue("collectionName");
- })
- .orElse("");
- QuestManager* zManager = Game::INSTANCE->zQuestManager();
- QuestCollection* questCollection
- = zManager->zCollection(collectionName);
- QuestParty* zParty = zManager->zParty(getPlayerId());
- Framework::Text questId
- = uiml->selectChildsByAttribute("id", "questDetailView")
- .getFirstElement()
- ->getAttributeValue("questId");
- for (Quest* quest : questCollection->quests)
- {
- if (quest->getQuestId().istGleich(questId))
- {
- QuestStorage* zStorage
- = zParty->zQuestStorage(quest->questId);
- if (zStorage->isQuestFinished()
- && !zStorage->isQuestRewarded())
- {
- Framework::XML::Element* rewardContainer
- = (Framework::RCPointer<Framework::XML::Element>)
- uiml->selectChildren()
- .selectAllElements()
- .whereAttributeEquals(
- "id", "quest_rewards")
- .getFirstElement();
- bool valid = 1;
- for (auto reward : quest->rewards)
- {
- valid &= reward->validateSettings(
- rewardContainer, zStorage);
- }
- if (valid)
- {
- zStorage->setQuestRewarded(1);
- for (auto reward : quest->rewards)
- {
- reward->giveReward(rewardContainer,
- zStorage,
- Game::INSTANCE->zEntity(getPlayerId()));
- }
- auto questView = uiml->selectChildsByAttribute(
- "id", "questDetailView")
- .getFirstElement();
- questView->removeAllChilds();
- showQuestDetails(quest,
- zStorage,
- (Framework::RCPointer<Framework::XML::Element>)
- questView);
- }
- break;
- }
- }
- }
- break;
- }
- case 4: // requirement message
- {
- Framework::Text collectionName
- = uiml->selectChildsByName("listView")
- .selectChildren()
- .whereAttributeExists("selected")
- .getFirstElement()
- .map<Framework::Text>([](auto element) {
- return element->getAttributeValue("collectionName");
- })
- .orElse("");
- QuestManager* zManager = Game::INSTANCE->zQuestManager();
- QuestCollection* questCollection
- = zManager->zCollection(collectionName);
- QuestParty* zParty = zManager->zParty(getPlayerId());
- Framework::Text questId
- = uiml->selectChildsByAttribute("id", "questDetailView")
- .getFirstElement()
- ->getAttributeValue("questId");
- unsigned char len;
- zRequest->lese((char*)&len, 1);
- char* requirementId = new char[len + 1];
- requirementId[len] = 0;
- auto requirementsContainer
- = uiml->selectChildsByAttribute("id", "quest_requirements");
- if (requirementsContainer.exists())
- {
- for (Quest* quest : questCollection->quests)
- {
- if (quest->getQuestId().istGleich(questId))
- {
- for (QuestRequirement* requirement :
- quest->requirements)
- {
- if (requirement->getRequirementId().istGleich(
- requirementId))
- {
- requirement->api(zRequest,
- (RCPointer<Framework::XML::Element>)
- requirementsContainer.getFirstElement(),
- zParty->zQuestStorage(questId));
- }
- }
- }
- }
- }
- delete[] requirementId;
- break;
- }
- case 5: // reward message
- {
- Framework::Text collectionName
- = uiml->selectChildsByName("listView")
- .selectChildren()
- .whereAttributeExists("selected")
- .getFirstElement()
- .map<Framework::Text>([](auto element) {
- return element->getAttributeValue("collectionName");
- })
- .orElse("");
- QuestManager* zManager = Game::INSTANCE->zQuestManager();
- QuestCollection* questCollection
- = zManager->zCollection(collectionName);
- QuestParty* zParty = zManager->zParty(getPlayerId());
- Framework::Text questId
- = uiml->selectChildsByAttribute("id", "questDetailView")
- .getFirstElement()
- ->getAttributeValue("questId");
- unsigned char len;
- zRequest->lese((char*)&len, 1);
- char* reqardId = new char[len + 1];
- reqardId[len] = 0;
- auto requirementsContainer
- = uiml->selectChildsByAttribute("id", "quest_rewards");
- if (requirementsContainer.exists())
- {
- for (Quest* quest : questCollection->quests)
- {
- if (quest->getQuestId().istGleich(questId))
- {
- for (QuestReward* reward : quest->rewards)
- {
- if (reward->getRewardId().istGleich(reqardId))
- {
- reward->api(zRequest,
- (RCPointer<Framework::XML::Element>)
- requirementsContainer.getFirstElement(),
- zParty->zQuestStorage(questId));
- }
- }
- }
- }
- }
- delete[] reqardId;
- break;
- }
- }
- }
- Framework::XML::Element* QuestDialog::getQuestGraphItem(
- Quest* zQuest, QuestParty* zParty)
- {
- Framework::Text requirements = "";
- int index = 0;
- int currentGroup = 0;
- for (Framework::Text* requiredQuest : zQuest->requiredQuestsIds)
- {
- int group = zQuest->requiredQuestsGroups.get(index);
- if (index != 0)
- {
- if (group != currentGroup)
- {
- requirements += "||";
- currentGroup = group;
- }
- else
- {
- requirements += "&&";
- }
- }
- else
- {
- currentGroup = group;
- }
- requirements += *requiredQuest;
- index++;
- }
- if (!zQuest->isActive(zParty))
- {
- Framework::Text result = "<questGraphItem id=\"quest_";
- result.append()
- << zQuest->questId << "\" name=\"Unknown Quest\" image=\""
- << zQuest->imagePath
- << "\" description=\"You have to finish other quests befor this "
- "quest will be available.\" finished=\"false\" mainQuest=\""
- << zQuest->mainQuest << "\" requirements=\"" << requirements
- << "\" width=\"" << (zQuest->mainQuest ? "50" : "30")
- << "\" height=\"" << (zQuest->mainQuest ? "50" : "30") << "\"/>";
- return new Framework::XML::Element(result);
- }
- else
- {
- QuestStorage* zStorage = zParty->zQuestStorage(zQuest->questId);
- Framework::Text result = "<questGraphItem id=\"quest_";
- result.append() << zQuest->questId << "\" name=\""
- << zQuest->questName + "\" image=\""
- << zQuest->imagePath << "\" description=\""
- << zQuest->description << "\" finished=\""
- << zStorage->isQuestFinished() << "\" mainQuest=\""
- << zQuest->mainQuest << "\" requirements=\""
- << requirements << "\" width=\""
- << (zQuest->mainQuest ? "50" : "30") << "\" height=\""
- << (zQuest->mainQuest ? "50" : "30")
- << "\" onClick=\"quests;(char)1;" << zQuest->questId
- << "\"/>";
- return new Framework::XML::Element(result);
- }
- }
- void QuestDialog::showQuestDetails(
- Quest* zQuest, QuestStorage* zStorage, Framework::XML::Element* zParent)
- {
- zParent->setAttribute("title", zQuest->questName);
- zParent->addChild(new Framework::XML::Element(
- (((Framework::Text("<text id=\"quest_description\" width=\"50% - 15\" "
- "height=\"50% - 10\" "
- "align-left=\"start\" margin-left=\"10\" "
- "align-top=\"start\" margin-top=\"10\" border=\"1\" "
- "border-color=\"0xFF52525E\" vScroll=\"auto\" "
- "style=\"")
- += Framework::TextFeld::Style::Text
- | Framework::TextFeld::Style::Mehrzeilig
- | Framework::TextFeld::Style::AutoLineBreak)
- += "\">")
- += zQuest->description)
- += "</text>"));
- Framework::XML::Element* requirementsContainer
- = new Framework::XML::Element(
- "<frame id=\"quest_requirements\" align-left=\"quest_description\" "
- "margin-left=\"10\" "
- "align-top=\"start\" margin-top=\"10\" width=\"50% - 15\" "
- "height=\"100% - "
- "50\" vScroll=\"auto\" display=\"column\" gap=\"10\" border=\"1\" "
- "border-color=\"0xFF52525E\"/>");
- requirementsContainer->addChild(new Framework::XML::Element(
- "<text id=\"quest_requirements_title\" width=\"100%\" "
- "height=\"30\" text-align-horizontal=\"center\" "
- "text-align-vertical=\"center\" "
- "font-size=\"20\">Requirements</text>"));
- requirementsContainer->setAttribute("style",
- Framework::Text() += (Framework::Fenster::Style::Sichtbar
- | Framework::Fenster::Style::Erlaubt
- | Framework::Fenster::Style::Rahmen));
- for (QuestRequirement* requirement : zQuest->requirements)
- {
- requirement->addRequirementUIML(zStorage,
- requirementsContainer,
- Framework::Text("quests;(char)4;")
- += requirement->getRequirementId());
- }
- zParent->addChild(requirementsContainer);
- Framework::XML::Element* rewardsContainer = new Framework::XML::Element(
- "<frame id=\"quest_rewards\" align-left=\"start\" margin-left=\"10\" "
- "margin-top=\"10\" "
- "align-top=\"quest_description\" width=\"50% - 15\" height=\"50% - "
- "20\" vScroll=\"auto\" display=\"column\" gap=\"10\" border=\"1\" "
- "border-color=\"0xFF52525E\"/>");
- rewardsContainer->addChild(new Framework::XML::Element(
- "<text id=\"quest_rewards_title\" width=\"100%\" height=\"30\" "
- "text-align-horizontal=\"center\" text-align-vertical=\"center\" "
- "font-size=\"20\">Rewards</text>"));
- rewardsContainer->setAttribute("style",
- Framework::Text() += (Framework::Fenster::Style::Sichtbar
- | Framework::Fenster::Style::Erlaubt
- | Framework::Fenster::Style::Rahmen));
- for (QuestReward* reward : zQuest->rewards)
- {
- reward->addRewardUIML(rewardsContainer,
- zStorage,
- Framework::Text("quests;(char)5;") += reward->getRewardId());
- }
- zParent->addChild(rewardsContainer);
- zParent->addChild(new XML::Element(
- "<button id=\"back_to_graph\" width=\"100\" height=\"20\" "
- "align-right=\"end\" align-top=\"quest_requirements\" "
- "margin-right=\"10\" margin-top=\"10\" "
- "onClick=\"quests;(char)2\">Back</button>"));
- if (zStorage->isQuestFinished() && !zStorage->isQuestRewarded())
- {
- zParent->addChild(new XML::Element(
- "<button id=\"give_rewards\" width=\"100\" height=\"20\" "
- "align-left=\"quest_rewards\" align-top=\"quest_requirements\" "
- "margin-left=\"10\" margin-top=\"10\" "
- "onClick=\"quests;(char)3\">Give rewards</button>"));
- }
- update();
- }
|