trapcodien

auto accept quests

Jul 1st, 2022 (edited)
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import type { Quest } from "@spt-aki/models/eft/common/IPmcData";
  2. import type { IAkiProfile } from "@spt-aki/models/eft/profile/IAkiProfile";
  3.  
  4. /**
  5.  * Some generic utils
  6.  */
  7. const indexBy =
  8.   <T>(getId: (q: T) => string) =>
  9.   (quests: T[]) => {
  10.     const indexedQuests: Record<string, T> = {};
  11.  
  12.     quests.forEach((q) => {
  13.       indexedQuests[getId(q)] = q;
  14.     });
  15.  
  16.     return indexedQuests;
  17.   };
  18.  
  19. const indexQuestsById = indexBy<Quest>((q) => q.qid);
  20. const indexIds = indexBy<string>((id) => id);
  21.  
  22. /**
  23.  * A helper (curried) function that create a started quest status
  24.  */
  25. const createStartedQuestStatus = (startTime: number) => (qid: string) => {
  26.   return {
  27.     qid,
  28.     startTime,
  29.     status: "Started",
  30.     completeConditions: [],
  31.   };
  32. };
  33.  
  34. /**
  35.  * This should work fine but I suggest to avoid applying this systematically on all profiles (on the delayedLoad).
  36.  * Prefer hack the gameStart method on GameCallbacks instance to only apply this on the concerned profile.
  37.  */
  38. const autoAcceptQuests = (
  39.   profile: IAkiProfile,
  40.   questIdsToAccept: string[]
  41. ): void => {
  42.   const pmc = profile.characters.pmc;
  43.  
  44.   if (pmc && pmc.Quests) {
  45.     const indexedProfileQuests = indexQuestsById(pmc.Quests);
  46.     const indexedIdsToAccept = indexIds(questIdsToAccept);
  47.     const timeInSeconds = Math.floor(Date.now() / 1000);
  48.  
  49.     // when not found: create new quests status
  50.     const newQuestsStatus = questIdsToAccept
  51.       .filter((qid) => !indexedProfileQuests[qid])
  52.       .map(createStartedQuestStatus(timeInSeconds));
  53.  
  54.     // all quests we want to auto-accept may have the `AvailableForStart` status, so we replace it by a new `Started` quest status
  55.     const updatedQuestsStatus = pmc.Quests.map((q) => {
  56.       if (indexedIdsToAccept[q.qid] && q.status === "AvailableForStart") {
  57.         return createStartedQuestStatus(timeInSeconds)(q.qid);
  58.       }
  59.  
  60.       return q;
  61.     });
  62.  
  63.     pmc.Quests = [...updatedQuestsStatus, ...newQuestsStatus];
  64.   }
  65. };
Add Comment
Please, Sign In to add comment