aboutsummaryrefslogtreecommitdiffstats
path: root/lib/questsLoader.ts
blob: a4ea64f7621d93c3648696fe90a4342618181f41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import type { EditorQuest, EditorTask, EditorCategory } from '../stores/session';

export function loadQuestsFromJson(config: any): EditorQuest[] {
  return Object.keys(config).map((questid: any) => {
    const quest = config[questid];

    return {
      id: questid,
      display: {
        name: quest.display.name,
        lore: {
          normal: quest.display['lore-normal'],
          started: quest.display['lore-started'],
        },
        type: quest.display.type,
      },
      tasks: Object.fromEntries(Object.keys(quest.tasks).map((taskId: string) => {
        return [taskId, {
          id: taskId,
          config: quest.tasks[taskId],
        } as EditorTask];
      })),
      rewards: quest.rewards,
      ...(quest.startcommands && { startCommands: quest.startcommands }),
      ...(quest.startstring && { startString: quest.startstring }),
      ...(quest.rewardstring && { rewardString: quest.rewardstring }),
      ...(quest.placeholders && { placeholders: quest.placeholders }),
      options: {
        category: quest.options.category,
        requirements: quest.options.requires || [],
        permissionRequired: quest.options['permission-required'] || false,
        cancellable: quest.options.cancellable || false,
        countsTowardsLimit: quest.options['counts-towards-limit'] || true,
        repeatable: quest.options.repeatable || false,
        cooldown: {
          enabled: quest.options.cooldown?.enabled || false,
          time: quest.options.cooldown?.time || 0,
        },
        timeLimit: {
          enabled: quest.options['time-limit']?.enabled || false,
          time: quest.options['time-limit']?.time || 0,
        },
        sortOrder: quest.options['sort-order'] || 0,
        autostart: quest.options.autostart || false,
        ...(quest.options['completed-display'] && { completedDisplay: quest.options['completed-display'] }),
        ...(quest.options['cooldown-display'] && { cooldownDisplay: quest.options['cooldown-display'] }),
        ...(quest.options['permission-display'] && { permissionDisplay: quest.options['permission-display'] }),
        ...(quest.options['locked-display'] && { lockedDisplay: quest.options['locked-display'] }),
      },
    } as EditorQuest;
  });
}

export function loadCategoriesFromJson(config: any): EditorCategory[] {
  return Object.keys(config).map((categoryid: any) => {
    const category = config[categoryid];

    return {
      id: categoryid,
      display: {
        name: category.display.name,
        lore: category.display.lore,
        type: category.display.type,
      },
      permissionRequired: category['permission-required'],
    };
  });
}

//TODO don't write fields if they're unchanged
export function mapJsonQuestToYamlObject(quest: EditorQuest): any {
  return {
    tasks: Object.fromEntries(Object.keys(quest.tasks).map((taskId: string) => {
      return [taskId, quest.tasks[taskId].config]
    })),
    display: {
      name: quest.display.name,
      "lore-normal": quest.display.lore.normal,
      "lore-started": quest.display.lore.started,
      type: quest.display.type
    },
    rewards: quest.rewards,
    ...(quest.startCommands && { startcommands: quest.startCommands }),
    ...(quest.startString && { startstring: quest.startString }),
    ...(quest.rewardString && { rewardstring: quest.rewardString }),
    ...(quest.placeholders && { placeholders: quest.placeholders }),
    options: {
      category: quest.options.category,
      requires: quest.options.requirements,
      "permission-required": quest.options.permissionRequired,
      cancellable: quest.options.cancellable,
      "counts-towards-limit": quest.options.countsTowardsLimit,
      repeatable: quest.options.repeatable,
      cooldown: {
        enabled: quest.options.cooldown.enabled,
        time: quest.options.cooldown.time,
      },
      "time-limit": {
        enabled: quest.options.timeLimit.enabled,
        time: quest.options.timeLimit.time,
      },
      "sort-order": quest.options.sortOrder,
      autostart: quest.options.autostart || false,
      ...(quest.options.completedDisplay && { completedDisplay: quest.options.completedDisplay }),
      ...(quest.options.cooldownDisplay && { cooldownDisplay: quest.options.cooldownDisplay }),
      ...(quest.options.permissionDisplay && { permissionDisplay: quest.options.permissionDisplay }),
      ...(quest.options.lockedDisplay && { lockedDisplay: quest.options.lockedDisplay }),
    },
  }
}