Quest.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. #include "Quest.h"
  2. #include <Datei.h>
  3. #include <Fenster.h>
  4. #include <Logging.h>
  5. #include <TextFeld.h>
  6. #include "Game.h"
  7. QuestRequirementStorage::QuestRequirementStorage()
  8. : ReferenceCounter(),
  9. fulfilled(0)
  10. {}
  11. void QuestRequirementStorage::setFullfilled(bool fullfilled)
  12. {
  13. this->fulfilled = fullfilled;
  14. }
  15. bool QuestRequirementStorage::isFullfilled() const
  16. {
  17. return fulfilled;
  18. }
  19. void QuestRequirementStorage::setRequirementId(Framework::Text requirementId)
  20. {
  21. this->requirementId = requirementId;
  22. }
  23. const Framework::Text& QuestRequirementStorage::getRequirementId() const
  24. {
  25. return requirementId;
  26. }
  27. QuestRequirementStorageType::QuestRequirementStorageType()
  28. : TypeFactory<QuestRequirementStorage>()
  29. {}
  30. QuestRequirementStorage* QuestRequirementStorageType::createValue(
  31. Framework::JSON::JSONObject* zJson) const
  32. {
  33. return new QuestRequirementStorage();
  34. }
  35. void QuestRequirementStorageType::fromJson(
  36. QuestRequirementStorage* zResult, Framework::JSON::JSONObject* zJson) const
  37. {
  38. zResult->setRequirementId(
  39. zJson->asObject()->zValue("requirementId")->asString()->getString());
  40. zResult->setFullfilled(
  41. zJson->asObject()->zValue("fulfilled")->asBool()->getBool());
  42. }
  43. void QuestRequirementStorageType::toJson(QuestRequirementStorage* zObject,
  44. Framework::JSON::JSONObject* zResult) const
  45. {
  46. zResult->addValue("requirementId",
  47. new Framework::JSON::JSONString(zObject->getRequirementId()));
  48. zResult->addValue(
  49. "fulfilled", new Framework::JSON::JSONBool(zObject->isFullfilled()));
  50. }
  51. JSONObjectValidationBuilder* QuestRequirementStorageType::addToValidator(
  52. JSONObjectValidationBuilder* builder) const
  53. {
  54. return builder->withRequiredString("requirementId")
  55. ->finishString()
  56. ->withRequiredBool("fulfilled")
  57. ->withDefault(false)
  58. ->finishBool();
  59. }
  60. QuestStorage::QuestStorage()
  61. : finished(0),
  62. rewarded(0),
  63. data(new Framework::JSON::JSONObject())
  64. {}
  65. QuestStorage::~QuestStorage()
  66. {
  67. data->release();
  68. }
  69. void QuestStorage::setQuestFinished(bool finished)
  70. {
  71. this->finished = finished;
  72. }
  73. void QuestStorage::setQuestRewarded(bool rewarded)
  74. {
  75. this->rewarded = rewarded;
  76. }
  77. bool QuestStorage::isQuestFinished() const
  78. {
  79. return finished;
  80. }
  81. bool QuestStorage::isQuestRewarded() const
  82. {
  83. return rewarded;
  84. }
  85. QuestRequirementStorage* QuestStorage::zStorage(Framework::Text requirementId)
  86. {
  87. for (QuestRequirementStorage* storage : requirements)
  88. {
  89. if (storage->getRequirementId().istGleich(requirementId))
  90. {
  91. return storage;
  92. }
  93. }
  94. QuestRequirementStorage* result = new QuestRequirementStorage();
  95. result->setRequirementId(requirementId);
  96. requirements.add(result);
  97. return result;
  98. }
  99. void QuestStorage::setQuestId(Framework::Text questId)
  100. {
  101. this->questId = questId;
  102. }
  103. const Framework::Text& QuestStorage::getQuestId() const
  104. {
  105. return questId;
  106. }
  107. bool QuestStorage::isVisible() const
  108. {
  109. return visible;
  110. }
  111. void QuestStorage::setVisible(bool visible)
  112. {
  113. this->visible = visible;
  114. }
  115. void QuestStorage::putValue(
  116. Framework::Text key, Framework::JSON::JSONValue* value)
  117. {
  118. data->removeValue(key);
  119. data->addValue(key, value);
  120. }
  121. Framework::JSON::JSONValue* QuestStorage::getValue(Framework::Text key) const
  122. {
  123. return data->getValue(key);
  124. }
  125. Framework::JSON::JSONValue* QuestStorage::zValue(Framework::Text key) const
  126. {
  127. return data->zValue(key);
  128. }
  129. bool QuestStorage::containsKey(Framework::Text key) const
  130. {
  131. return data->hasValue(key);
  132. }
  133. QuestStorageType::QuestStorageType()
  134. : TypeFactory<QuestStorage>()
  135. {}
  136. QuestStorage* QuestStorageType::createValue(
  137. Framework::JSON::JSONObject* zJson) const
  138. {
  139. return new QuestStorage();
  140. }
  141. void QuestStorageType::fromJson(
  142. QuestStorage* zResult, Framework::JSON::JSONObject* zJson) const
  143. {
  144. zResult->setQuestId(
  145. zJson->asObject()->zValue("questId")->asString()->getString());
  146. zResult->setQuestFinished(
  147. zJson->asObject()->zValue("finished")->asBool()->getBool());
  148. zResult->setQuestRewarded(
  149. zJson->asObject()->zValue("rewarded")->asBool()->getBool());
  150. Framework::JSON::JSONArray* rewardsArray
  151. = zJson->asObject()->zValue("requirements")->asArray();
  152. for (int i = 0; i < rewardsArray->getLength(); i++)
  153. {
  154. zResult->requirements.add(
  155. Game::INSTANCE->zTypeRegistry()->fromJson<QuestRequirementStorage>(
  156. rewardsArray->zValue(i)->asObject()));
  157. }
  158. zResult->data->release();
  159. zResult->data = zJson->asObject()->getValue("data")->asObject();
  160. }
  161. void QuestStorageType::toJson(
  162. QuestStorage* zObject, Framework::JSON::JSONObject* zResult) const
  163. {
  164. zResult->addValue(
  165. "questId", new Framework::JSON::JSONString(zObject->questId));
  166. zResult->addValue(
  167. "finished", new Framework::JSON::JSONBool(zObject->finished));
  168. zResult->addValue(
  169. "rewarded", new Framework::JSON::JSONBool(zObject->rewarded));
  170. Framework::JSON::JSONArray* rewardsArray = new Framework::JSON::JSONArray();
  171. for (QuestRequirementStorage* storage : zObject->requirements)
  172. {
  173. rewardsArray->addValue(
  174. Game::INSTANCE->zTypeRegistry()->toJson(storage));
  175. }
  176. zResult->addValue("requirements", rewardsArray);
  177. Framework::Text dataString = zObject->data->toString();
  178. zResult->addValue("data", Framework::JSON::Parser::getValue(dataString));
  179. }
  180. JSONObjectValidationBuilder* QuestStorageType::addToValidator(
  181. JSONObjectValidationBuilder* builder) const
  182. {
  183. return builder->withRequiredString("questId")
  184. ->finishString()
  185. ->withRequiredBool("finished")
  186. ->withDefault(false)
  187. ->finishBool()
  188. ->withRequiredBool("rewarded")
  189. ->withDefault(false)
  190. ->finishBool()
  191. ->withRequiredArray("requirements")
  192. ->addAcceptedTypeInArray(Game::INSTANCE->zTypeRegistry()
  193. ->getValidator<QuestRequirementStorage>())
  194. ->finishArray()
  195. ->withRequiredObject("data")
  196. ->allowAdditionalAttriutes()
  197. ->finishObject();
  198. }
  199. QuestParty::QuestParty()
  200. : ReferenceCounter()
  201. {}
  202. void QuestParty::addMember(int memberEntityId)
  203. {
  204. memberEntityIds.add(memberEntityId);
  205. }
  206. void QuestParty::removeMember(int memberEntityId)
  207. {
  208. memberEntityIds.removeValue(memberEntityId);
  209. }
  210. bool QuestParty::isMember(int memberEntityId) const
  211. {
  212. return memberEntityIds.hat(memberEntityId);
  213. }
  214. bool QuestParty::isEmpty() const
  215. {
  216. return memberEntityIds.getEintragAnzahl() == 0;
  217. }
  218. QuestStorage* QuestParty::zQuestStorage(Framework::Text questId)
  219. {
  220. for (QuestStorage* storage : questStorage)
  221. {
  222. if (storage->getQuestId().istGleich(questId))
  223. {
  224. return storage;
  225. }
  226. }
  227. QuestStorage* result = new QuestStorage();
  228. result->setQuestId(questId);
  229. questStorage.add(result);
  230. return result;
  231. }
  232. QuestParty* QuestParty::clone() const
  233. {
  234. QuestParty* result = new QuestParty();
  235. for (QuestStorage* storage : questStorage)
  236. {
  237. Framework::JSON::JSONValue* value
  238. = Game::INSTANCE->zTypeRegistry()->toJson(storage);
  239. result->questStorage.add(
  240. Game::INSTANCE->zTypeRegistry()->fromJson<QuestStorage>(value));
  241. value->release();
  242. }
  243. return result;
  244. }
  245. QuestPartyType::QuestPartyType()
  246. : TypeFactory<QuestParty>()
  247. {}
  248. QuestParty* QuestPartyType::createValue(
  249. Framework::JSON::JSONObject* zJson) const
  250. {
  251. return new QuestParty();
  252. }
  253. void QuestPartyType::fromJson(
  254. QuestParty* zResult, Framework::JSON::JSONObject* zJson) const
  255. {
  256. Framework::JSON::JSONArray* membersArray
  257. = zJson->asObject()->zValue("members")->asArray();
  258. for (int i = 0; i < membersArray->getLength(); i++)
  259. {
  260. zResult->memberEntityIds.add(
  261. (int)membersArray->zValue(i)->asNumber()->getNumber());
  262. }
  263. Framework::JSON::JSONArray* storagesArray
  264. = zJson->asObject()->zValue("quests")->asArray();
  265. for (int i = 0; i < storagesArray->getLength(); i++)
  266. {
  267. zResult->questStorage.add(
  268. Game::INSTANCE->zTypeRegistry()->fromJson<QuestStorage>(
  269. storagesArray->zValue(i)->asObject()));
  270. }
  271. }
  272. void QuestPartyType::toJson(
  273. QuestParty* zObject, Framework::JSON::JSONObject* zResult) const
  274. {
  275. Framework::JSON::JSONArray* membersArray = new Framework::JSON::JSONArray();
  276. for (int memberEntityId : zObject->memberEntityIds)
  277. {
  278. membersArray->addValue(new Framework::JSON::JSONNumber(memberEntityId));
  279. }
  280. zResult->addValue("members", membersArray);
  281. Framework::JSON::JSONArray* storagesArray
  282. = new Framework::JSON::JSONArray();
  283. for (QuestStorage* storage : zObject->questStorage)
  284. {
  285. storagesArray->addValue(
  286. Game::INSTANCE->zTypeRegistry()->toJson(storage));
  287. }
  288. zResult->addValue("quests", storagesArray);
  289. }
  290. JSONObjectValidationBuilder* QuestPartyType::addToValidator(
  291. JSONObjectValidationBuilder* builder) const
  292. {
  293. return builder->withRequiredArray("members")
  294. ->addAcceptedNumberInArray()
  295. ->finishNumber()
  296. ->removeInvalidEntries()
  297. ->finishArray()
  298. ->withRequiredArray("quests")
  299. ->addAcceptedTypeInArray(
  300. Game::INSTANCE->zTypeRegistry()->getValidator<QuestStorage>())
  301. ->removeInvalidEntries()
  302. ->finishArray();
  303. }
  304. Quest::Quest()
  305. : ReferenceCounter(),
  306. mainQuest(0)
  307. {}
  308. void Quest::processEvent(QuestEvent* zEvent, QuestStorage* zStorage)
  309. {
  310. bool finished = 1;
  311. for (QuestRequirement* requirement : requirements)
  312. {
  313. QuestRequirementStorage* zRequirementStorage
  314. = zStorage->zStorage(requirement->getRequirementId());
  315. if (!zRequirementStorage->isFullfilled())
  316. {
  317. requirement->processEvent(zEvent, zStorage);
  318. finished &= zRequirementStorage->isFullfilled();
  319. }
  320. }
  321. if (finished)
  322. {
  323. zStorage->setQuestFinished(1);
  324. }
  325. }
  326. bool Quest::isVisible(QuestParty* zParty, QuestManager* zManager)
  327. {
  328. if (zParty->zQuestStorage(questId)->isVisible())
  329. {
  330. return 1;
  331. }
  332. bool visible = 0;
  333. for (Framework::Text* requiredQuestId : requiredQuestsIds)
  334. {
  335. visible |= zParty->zQuestStorage(*requiredQuestId)->isQuestFinished();
  336. }
  337. if (visible)
  338. {
  339. zParty->zQuestStorage(questId)->setVisible(visible);
  340. }
  341. for (Framework::Text* requiredQuestId : requiredQuestsIds)
  342. {
  343. zManager->setQuestVisible(true, *requiredQuestId, zParty);
  344. }
  345. return visible;
  346. }
  347. bool Quest::isActive(QuestParty* zParty)
  348. {
  349. bool active = 1;
  350. for (Framework::Text* requiredQuestId : requiredQuestsIds)
  351. {
  352. active &= zParty->zQuestStorage(*requiredQuestId)->isQuestFinished();
  353. }
  354. return active;
  355. }
  356. void Quest::setQuestId(Framework::Text questId)
  357. {
  358. this->questId = questId;
  359. }
  360. const Framework::Text& Quest::getQuestId() const
  361. {
  362. return questId;
  363. }
  364. void Quest::setVisible(bool visible, QuestParty* zParty, QuestManager* zManager)
  365. {
  366. if (zParty->zQuestStorage(questId)->isVisible() == visible)
  367. {
  368. return;
  369. }
  370. zParty->zQuestStorage(questId)->setVisible(visible);
  371. if (visible)
  372. {
  373. for (Framework::Text* requiredQuestId : requiredQuestsIds)
  374. {
  375. zManager->setQuestVisible(visible, *requiredQuestId, zParty);
  376. }
  377. }
  378. }
  379. QuestType::QuestType()
  380. : TypeFactory<Quest>()
  381. {}
  382. Quest* QuestType::createValue(Framework::JSON::JSONObject* zJson) const
  383. {
  384. return new Quest();
  385. }
  386. void QuestType::fromJson(
  387. Quest* zResult, Framework::JSON::JSONObject* zJson) const
  388. {
  389. zResult->setQuestId(
  390. zJson->asObject()->zValue("questId")->asString()->getString());
  391. zResult->questName
  392. = zJson->asObject()->zValue("questName")->asString()->getString();
  393. zResult->description
  394. = zJson->asObject()->zValue("description")->asString()->getString();
  395. Framework::JSON::JSONArray* requiredQuestGroups
  396. = zJson->asObject()->zValue("requiredQuestIds")->asArray();
  397. for (int i = 0; i < requiredQuestGroups->getLength(); i++)
  398. {
  399. Framework::JSON::JSONArray* requiredQuestIdsArray
  400. = requiredQuestGroups->zValue(i)->asArray();
  401. for (int j = 0; j < requiredQuestIdsArray->getLength(); j++)
  402. {
  403. zResult->requiredQuestsIds.add(new Framework::Text(
  404. requiredQuestIdsArray->zValue(j)->asString()->getString()));
  405. zResult->requiredQuestsGroups.add(i);
  406. }
  407. }
  408. Framework::JSON::JSONArray* requirementsArray
  409. = zJson->asObject()->zValue("requirements")->asArray();
  410. for (int i = 0; i < requirementsArray->getLength(); i++)
  411. {
  412. zResult->requirements.add(
  413. Game::INSTANCE->zTypeRegistry()->fromJson<QuestRequirement>(
  414. requirementsArray->zValue(i)));
  415. }
  416. Framework::JSON::JSONArray* rewardsArray
  417. = zJson->asObject()->zValue("rewards")->asArray();
  418. for (int i = 0; i < rewardsArray->getLength(); i++)
  419. {
  420. zResult->rewards.add(
  421. Game::INSTANCE->zTypeRegistry()->fromJson<QuestReward>(
  422. rewardsArray->zValue(i)));
  423. }
  424. zResult->imagePath
  425. = zJson->asObject()->zValue("imagePath")->asString()->getString();
  426. }
  427. void QuestType::toJson(
  428. Quest* zObject, Framework::JSON::JSONObject* zResult) const
  429. {
  430. zResult->addValue(
  431. "questId", new Framework::JSON::JSONString(zObject->questId));
  432. zResult->addValue(
  433. "questName", new Framework::JSON::JSONString(zObject->questName));
  434. zResult->addValue(
  435. "description", new Framework::JSON::JSONString(zObject->description));
  436. Framework::JSON::JSONArray* requiredQuestGroupArray
  437. = new Framework::JSON::JSONArray();
  438. Framework::JSON::JSONArray* requiredQuestIdsArray
  439. = new Framework::JSON::JSONArray();
  440. int index = 0;
  441. int lastGroup = 0;
  442. for (Framework::Text* requiredQuestId : zObject->requiredQuestsIds)
  443. {
  444. int group = zObject->requiredQuestsGroups.get(index);
  445. if (lastGroup != group)
  446. {
  447. if (requiredQuestIdsArray->getLength())
  448. {
  449. requiredQuestGroupArray->addValue(requiredQuestIdsArray);
  450. requiredQuestIdsArray = new Framework::JSON::JSONArray();
  451. }
  452. lastGroup = group;
  453. }
  454. requiredQuestIdsArray->addValue(
  455. new Framework::JSON::JSONString(*requiredQuestId));
  456. index++;
  457. }
  458. if (requiredQuestIdsArray->getLength())
  459. {
  460. requiredQuestGroupArray->addValue(requiredQuestIdsArray);
  461. requiredQuestIdsArray = new Framework::JSON::JSONArray();
  462. }
  463. zResult->addValue("requiredQuestIds", requiredQuestGroupArray);
  464. Framework::JSON::JSONArray* requirementsArray
  465. = new Framework::JSON::JSONArray();
  466. for (QuestRequirement* requirement : zObject->requirements)
  467. {
  468. requirementsArray->addValue(
  469. Game::INSTANCE->zTypeRegistry()->toJson(requirement));
  470. }
  471. zResult->addValue("requirements", requirementsArray);
  472. Framework::JSON::JSONArray* rewardsArray = new Framework::JSON::JSONArray();
  473. for (QuestReward* reward : zObject->rewards)
  474. {
  475. rewardsArray->addValue(Game::INSTANCE->zTypeRegistry()->toJson(reward));
  476. }
  477. zResult->addValue("rewards", rewardsArray);
  478. zResult->addValue(
  479. "imagePath", new Framework::JSON::JSONString(zObject->imagePath));
  480. }
  481. JSONObjectValidationBuilder* QuestType::addToValidator(
  482. JSONObjectValidationBuilder* builder) const
  483. {
  484. return builder->withRequiredString("questId")
  485. ->finishString()
  486. ->withRequiredString("questName")
  487. ->finishString()
  488. ->withRequiredString("description")
  489. ->finishString()
  490. ->withRequiredArray("requiredQuestIds")
  491. ->addAcceptedArrayInArray()
  492. ->addAcceptedStringInArray()
  493. ->finishString()
  494. ->finishArray()
  495. ->withDefault(new Framework::JSON::JSONArray())
  496. ->finishArray()
  497. ->withRequiredArray("requirements")
  498. ->addAcceptedTypeInArray(
  499. Game::INSTANCE->zTypeRegistry()->getValidator<QuestRequirement>())
  500. ->finishArray()
  501. ->withRequiredArray("rewards")
  502. ->addAcceptedTypeInArray(
  503. Game::INSTANCE->zTypeRegistry()->getValidator<QuestReward>())
  504. ->finishArray()
  505. ->withRequiredString("imagePath")
  506. ->finishString();
  507. }
  508. QuestCollection::QuestCollection()
  509. : ReferenceCounter()
  510. {}
  511. void QuestCollection::processEvent(QuestEvent* zEvent, QuestParty* zParty)
  512. {
  513. for (Quest* quest : quests)
  514. {
  515. QuestStorage* zStorage = zParty->zQuestStorage(quest->getQuestId());
  516. if (quest->isActive(zParty) && !zStorage->isQuestFinished())
  517. {
  518. quest->processEvent(zEvent, zStorage);
  519. }
  520. }
  521. }
  522. bool QuestCollection::isVisible(QuestParty* zParty, QuestManager* zManager)
  523. {
  524. bool visible = 1;
  525. for (Quest* quest : quests)
  526. {
  527. visible &= quest->isVisible(zParty, zManager);
  528. }
  529. return visible;
  530. }
  531. void QuestCollection::addQuest(Quest* zQuest)
  532. {
  533. quests.add(zQuest);
  534. }
  535. void QuestCollection::setName(Framework::Text name)
  536. {
  537. this->name = name;
  538. }
  539. const Framework::Text& QuestCollection::getName() const
  540. {
  541. return name;
  542. }
  543. void QuestCollection::setQuestVisible(bool visible,
  544. Framework::Text questId,
  545. QuestParty* zParty,
  546. QuestManager* zManager)
  547. {
  548. for (Quest* quest : quests)
  549. {
  550. if (quest->getQuestId().istGleich(questId))
  551. {
  552. return quest->setVisible(visible, zParty, zManager);
  553. }
  554. }
  555. }
  556. QuestCollectionType::QuestCollectionType()
  557. : TypeFactory<QuestCollection>()
  558. {}
  559. QuestCollection* QuestCollectionType::createValue(
  560. Framework::JSON::JSONObject* zJson) const
  561. {
  562. return new QuestCollection();
  563. }
  564. void QuestCollectionType::fromJson(
  565. QuestCollection* zResult, Framework::JSON::JSONObject* zJson) const
  566. {
  567. zResult->setName(
  568. zJson->asObject()->zValue("name")->asString()->getString());
  569. Framework::JSON::JSONArray* questsArray
  570. = zJson->asObject()->zValue("quests")->asArray();
  571. for (int i = 0; i < questsArray->getLength(); i++)
  572. {
  573. zResult->quests.add(Game::INSTANCE->zTypeRegistry()->fromJson<Quest>(
  574. questsArray->zValue(i)->asObject()));
  575. }
  576. }
  577. void QuestCollectionType::toJson(
  578. QuestCollection* zObject, Framework::JSON::JSONObject* zResult) const
  579. {
  580. zResult->addValue("name", new Framework::JSON::JSONString(zObject->name));
  581. Framework::JSON::JSONArray* questsArray = new Framework::JSON::JSONArray();
  582. for (Quest* quest : zObject->quests)
  583. {
  584. questsArray->addValue(Game::INSTANCE->zTypeRegistry()->toJson(quest));
  585. }
  586. zResult->addValue("quests", questsArray);
  587. }
  588. JSONObjectValidationBuilder* QuestCollectionType::addToValidator(
  589. JSONObjectValidationBuilder* builder) const
  590. {
  591. return builder->withRequiredString("name")
  592. ->finishString()
  593. ->withRequiredArray("quests")
  594. ->addAcceptedTypeInArray(
  595. Game::INSTANCE->zTypeRegistry()->getValidator<Quest>())
  596. ->removeInvalidEntries()
  597. ->finishArray();
  598. }
  599. QuestManager::QuestManager()
  600. : ReferenceCounter()
  601. {}
  602. QuestParty* QuestManager::zParty(int entityId)
  603. {
  604. for (QuestParty* party : parties)
  605. {
  606. if (party->isMember(entityId))
  607. {
  608. return party;
  609. }
  610. }
  611. QuestParty* result = new QuestParty();
  612. result->addMember(entityId);
  613. parties.add(result);
  614. return result;
  615. }
  616. QuestCollection* QuestManager::zCollection(Framework::Text collectionName)
  617. {
  618. for (QuestCollection* collection : questCollections)
  619. {
  620. if (collection->getName().istGleich(collectionName))
  621. {
  622. return collection;
  623. }
  624. }
  625. return 0;
  626. }
  627. void QuestManager::loadQuests()
  628. {
  629. Framework::Text worldDir = Game::INSTANCE->getWorldDirectory();
  630. Framework::Text questDir = worldDir + "/quests";
  631. if (!Framework::DateiExistiert(questDir))
  632. {
  633. Framework::Logging::info()
  634. << "no quest data found in world save, using default data";
  635. questDir = "data/quests";
  636. }
  637. Framework::JSON::JSONValue* value
  638. = Framework::JSON::loadJSONFromFile(questDir + "/quests.json");
  639. if (!value || value->getType() == Framework::JSON::JSONType::NULL_)
  640. {
  641. Framework::Logging::warning()
  642. << "No valid quests.json found in " << questDir;
  643. if (value)
  644. {
  645. value->release();
  646. }
  647. }
  648. else
  649. {
  650. Framework::Logging::info() << "loading quests ...";
  651. Framework::JSON::Validator::JSONValidator* validator
  652. = Framework::JSON::Validator::JSONValidator::buildForArray()
  653. ->addAcceptedTypeInArray(
  654. Game::INSTANCE->zTypeRegistry()
  655. ->getValidator<QuestCollection>())
  656. ->removeInvalidEntries()
  657. ->finishArray();
  658. Framework::RCArray<Framework::JSON::Validator::JSONValidationResult>
  659. validationResult;
  660. Framework::JSON::JSONArray* valid
  661. = validator->getValidParts(value, &validationResult)->asArray();
  662. for (Framework::JSON::Validator::JSONValidationResult* invalidPart :
  663. validationResult)
  664. {
  665. Framework::Logging::error() << invalidPart->getInvalidInfo();
  666. }
  667. value->release();
  668. for (Framework::JSON::JSONValue* validPart : *valid)
  669. {
  670. questCollections.add(
  671. Game::INSTANCE->zTypeRegistry()->fromJson<QuestCollection>(
  672. validPart->asObject()));
  673. }
  674. valid->release();
  675. validator->release();
  676. }
  677. value = Framework::JSON::loadJSONFromFile(questDir + "/parties.json");
  678. if (!value || value->getType() == Framework::JSON::JSONType::NULL_)
  679. {
  680. if (value)
  681. {
  682. value->release();
  683. }
  684. }
  685. else
  686. {
  687. Framework::Logging::info() << "loading quest parties ...";
  688. Framework::JSON::Validator::JSONValidator* validator
  689. = Framework::JSON::Validator::JSONValidator::buildForArray()
  690. ->addAcceptedTypeInArray(Game::INSTANCE->zTypeRegistry()
  691. ->getValidator<QuestParty>())
  692. ->removeInvalidEntries()
  693. ->finishArray();
  694. Framework::RCArray<Framework::JSON::Validator::JSONValidationResult>
  695. validationResult;
  696. Framework::JSON::JSONArray* valid
  697. = validator->getValidParts(value, &validationResult)->asArray();
  698. for (Framework::JSON::Validator::JSONValidationResult* invalidPart :
  699. validationResult)
  700. {
  701. Framework::Logging::error() << invalidPart->getInvalidInfo();
  702. }
  703. value->release();
  704. for (Framework::JSON::JSONValue* validPart : *valid)
  705. {
  706. parties.add(Game::INSTANCE->zTypeRegistry()->fromJson<QuestParty>(
  707. validPart->asObject()));
  708. }
  709. valid->release();
  710. validator->release();
  711. }
  712. Framework::Logging::info()
  713. << "Loaded " << questCollections.getEintragAnzahl()
  714. << " Quest Collections and " << parties.getEintragAnzahl()
  715. << " Quest Parties";
  716. }
  717. void QuestManager::saveQuests()
  718. {
  719. Framework::Text worldDir = Game::INSTANCE->getWorldDirectory();
  720. Framework::Text questDir = worldDir + "/quests";
  721. if (!Framework::DateiExistiert(questDir))
  722. {
  723. Framework::DateiPfadErstellen(questDir + "/");
  724. }
  725. Framework::JSON::JSONArray* questCollectionsArray
  726. = new Framework::JSON::JSONArray();
  727. for (QuestCollection* questCollection : questCollections)
  728. {
  729. questCollectionsArray->addValue(
  730. Game::INSTANCE->zTypeRegistry()->toJson(questCollection));
  731. }
  732. Framework::Datei datei(questDir + "/quests.json");
  733. if (!datei.existiert()) datei.erstellen();
  734. datei.open(Framework::Datei::Style::schreiben);
  735. Framework::Text jsonStr = questCollectionsArray->toString();
  736. datei.schreibe(jsonStr.getText(), jsonStr.getLength());
  737. datei.close();
  738. questCollectionsArray->release();
  739. Framework::JSON::JSONArray* partiesArray = new Framework::JSON::JSONArray();
  740. for (QuestParty* party : parties)
  741. {
  742. partiesArray->addValue(Game::INSTANCE->zTypeRegistry()->toJson(party));
  743. }
  744. datei.setDatei(questDir + "/parties.json");
  745. if (!datei.existiert()) datei.erstellen();
  746. datei.open(Framework::Datei::Style::schreiben);
  747. jsonStr = partiesArray->toString();
  748. datei.schreibe(jsonStr.getText(), jsonStr.getLength());
  749. datei.close();
  750. partiesArray->release();
  751. }
  752. void QuestManager::processEvent(QuestEvent* event)
  753. {
  754. QuestParty* party = zParty(event->zActingEntity()->getId());
  755. if (party)
  756. {
  757. for (QuestCollection* questCollection : questCollections)
  758. {
  759. questCollection->processEvent(event, party);
  760. }
  761. }
  762. event->release();
  763. }
  764. void QuestManager::setQuestVisible(
  765. bool visible, Framework::Text questId, QuestParty* zParty)
  766. {
  767. for (QuestCollection* questCollection : questCollections)
  768. {
  769. questCollection->setQuestVisible(visible, questId, zParty, this);
  770. }
  771. }