-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
character.js
178 lines (152 loc) · 4.86 KB
/
character.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const fetch = require("node-fetch");
const CONFIG = require("./config.js");
const authentication = require("./auth.js");
const isValidData = data => {
return data.success === true;
};
const extractClassOptions = (cobaltId, optionIds=[], campaignId=null) => {
console.log(optionIds);
return new Promise((resolve, reject) => {
console.log(`Retrieving class options for ${cobaltId}`);
const url = CONFIG.urls.classOptionsAPI();
const body = JSON.stringify({
"campaignId": campaignId,
"sharingSetting": 2,
"ids": optionIds,
});
const auth = authentication.CACHE_AUTH.exists(cobaltId);
const headers = (auth && auth.data) ? {
"Authorization": `Bearer ${auth.data}`,
"Content-Type": "application/json",
"Content-Length": body.length,
} : {};
const options = {
method: "POST",
headers: headers,
body: body
};
fetch(url, options)
.then(res => res.json())
.then(json => {
if (isValidData(json)) {
const filteredItems = json.data.definitionData.filter(option =>
option.sources && (option.sources.length === 0 || option.sources.some((source) => source.sourceId != 39))
);
resolve(filteredItems);
} else {
console.log("Received no valid class option data, instead:" + json.message);
reject(json.message);
}
})
.catch(error => {
console.log("Error retrieving class options");
console.log(error);
reject(error);
});
});
};
const extractRacialTraitsOptions = (cobaltId, optionIds=[], campaignId=null) => {
console.log(optionIds);
return new Promise((resolve, reject) => {
console.log(`Retrieving origin options for ${cobaltId}`);
const url = CONFIG.urls.racialTraitOptionsAPI();
const body = JSON.stringify({
"campaignId": campaignId,
"sharingSetting": 2,
"ids": optionIds,
});
const auth = authentication.CACHE_AUTH.exists(cobaltId);
const headers = (auth && auth.data) ? {
"Authorization": `Bearer ${auth.data}`,
"Content-Type": "application/json",
"Content-Length": body.length,
} : {};
const options = {
method: "POST",
headers: headers,
body: body
};
fetch(url, options)
.then(res => res.json())
.then(json => {
if (isValidData(json)) {
const filteredItems = json.data.definitionData.filter(option =>
option.sources && (option.sources.length === 0 || option.sources.some((source) => source.sourceId != 39))
);
resolve(filteredItems);
} else {
console.log("Received no valid origin option data, instead:" + json.message);
reject(json.message);
}
})
.catch(error => {
console.log("Error retrieving origin options");
console.log(error);
reject(error);
});
});
};
const checkStatus = res => {
if (res.ok) {
// res.status >= 200 && res.status < 300
return res;
} else {
throw res.statusText;
}
};
const extractCharacterData = (cobaltId, characterId) => {
return new Promise((resolve, reject) => {
console.log(`Retrieving character id ${characterId}`);
const auth = authentication.CACHE_AUTH.exists(cobaltId);
const headers = (auth) ? {headers: {"Authorization": `Bearer ${auth.data}`}} : {};
const characterUrl = CONFIG.urls.characterUrl(characterId);
fetch(characterUrl, headers)
.then(checkStatus)
.then(res => res.json())
.then(json => {
if (isValidData(json)) {
resolve(json.data);
} else {
reject(json.message);
}
})
.catch(error => {
console.log(`loadCharacterData(${characterId}): ${error}`);
reject(error);
});
});
};
const getOptionalClassFeatures = (data, optionIds, campaignId, cobaltId) => {
const cacheId = authentication.CACHE_AUTH.exists(cobaltId);
return new Promise((resolve) => {
if (cacheId) {
console.log("CLASS Optional Features:");
extractClassOptions(cobaltId, optionIds, campaignId)
.then(options => {
data.classOptions = options;
resolve(data);
});
} else {
resolve(data);
}
});
};
const getOptionalOrigins = (data, optionIds, campaignId, cobaltId) => {
const cacheId = authentication.CACHE_AUTH.exists(cobaltId);
return new Promise((resolve) => {
if (cacheId) {
console.log("ORIGIN Optional Features:");
extractRacialTraitsOptions(cobaltId, optionIds, campaignId)
.then(options => {
data.originOptions = options;
resolve(data);
});
} else {
resolve(data);
}
});
};
exports.extractClassOptions = extractClassOptions;
exports.extractCharacterData = extractCharacterData;
exports.getOptionalClassFeatures = getOptionalClassFeatures;
exports.getOptionalOrigins = getOptionalOrigins;