-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcharacter_modeling.js
275 lines (240 loc) · 8.04 KB
/
character_modeling.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import "./setup.js"
const NOT_DISPLAYED_SLOTS = [
2, // neck
11, // finger1
12, // finger1
13, // trinket1
14, // trinket2
]
const modelingType = {
ARMOR: 128,
CHARACTER: 16,
COLLECTION: 1024,
HELM: 2,
HUMANOIDNPC: 32,
ITEM: 1,
ITEMVISUAL: 512,
NPC: 8,
OBJECT: 64,
PATH: 256,
SHOULDER: 4
}
const characterPart = () => {
const ret = {
Face: `face`,
"Skin Color": `skin`,
"Hair Style": `hairStyle`,
"Hair Color": `hairColor`,
"Facial Hair": `facialStyle`,
Mustache: `facialStyle`,
Beard: `facialStyle`,
Sideburns: `facialStyle`,
"Face Shape": `facialStyle`,
Eyebrow: `facialStyle`,
"Jaw Features": undefined,
"Face Features": undefined,
"Skin Type": undefined,
Ears: (window.WOTLK_TO_RETAIL_DISPLAY_ID_API) ? undefined : `ears`,
"Fur Color": (window.WOTLK_TO_RETAIL_DISPLAY_ID_API) ? undefined : `furColor`,
Snout: `snout`,
Blindfold: undefined,
Tattoo: undefined,
"Eye Color": undefined,
"Tattoo Color": undefined,
Armbands: undefined,
"Jewelry Color": undefined,
Bracelets: undefined,
Necklace: undefined,
Earring: undefined,
"Primary Color": (window.WOTLK_TO_RETAIL_DISPLAY_ID_API) ? undefined : `primaryColor`,
"Secondary Color Strength": (window.WOTLK_TO_RETAIL_DISPLAY_ID_API) ? undefined : `secondaryColorStrength`,
"Secondary Color": (window.WOTLK_TO_RETAIL_DISPLAY_ID_API) ? undefined : `secondaryColor`,
"Horn Color": (window.WOTLK_TO_RETAIL_DISPLAY_ID_API) ? undefined : `hornColor`,
Horns: (window.WOTLK_TO_RETAIL_DISPLAY_ID_API) ? undefined : `horns`,
"Body Size": (window.WOTLK_TO_RETAIL_DISPLAY_ID_API) ? undefined : `bodySize`
}
console.log(ret)
return ret
}
function optionalChaining(choice) {
//todo replace by `part.Choices[character[CHARACTER_PART[prop]]]?.Id` when it works on almost all frameworks
return choice ? choice.Id : undefined
}
/**
*
* @param {Object} character - The character object.
* @param {number} character.face - Description for face.
* @param {number} character.facialStyle - Description for facialStyle.
* @param {number} character.gender - Description for gender.
* @param {number} character.hairColor - Description for hairColor.
* @param {number} character.hairStyle - Description for hairStyle.
* @param {Array<Array<number>>} character.items - Description for items. (Optional)
* @param {number} character.race - Description for race.
* @param {number} character.skin - Description for skin.
* @param {Object} fullOptions - Zaming API character options payload.
* @return {[]}
*/
function getCharacterOptions(character, fullOptions) {
const options = fullOptions.Options
const missingChoice = []
const ret = []
for (const prop in characterPart()) {
const part = options.find(e => e.Name === prop)
if (!part) {
continue
}
const newOption = {
optionId: part.Id,
choiceId: (characterPart()[prop]) ? optionalChaining(part.Choices[character[characterPart()[prop]]]) : part.Choices[0].Id
}
if(newOption.choiceId === undefined) {
missingChoice.push(characterPart()[prop])
}
ret.push(newOption)
}
console.warn(`In character: `, character, `the following options are missing`, missingChoice)
return ret
}
/**
* This function return the design choices for a character this does not work for NPC / Creature / Items
* @param {Object} model - The model object to generate options from.
* @param {{}} fullOptions - The type of the model.
* @returns {{models: {id: string, type: number}, charCustomization: {options: []}, items: (*|*[])}|{models: {id, type}}
*/
function optionsFromModel(model, fullOptions) {
const {race, gender} = model
// slot ids on model viewer
const characterItems = (model.items) ? model.items.filter(e => !NOT_DISPLAYED_SLOTS.includes(e[0])) : []
const options = getCharacterOptions(model, fullOptions)
let charCustomization = {
options: options
}
const ret = {
items: characterItems,
models: {
id: race*2-1+gender,
type: modelingType.CHARACTER
},
}
if(!model.noCharCustomization) {
ret.charCustomization = charCustomization
}
return ret
}
/**
*
* @param item{number}: Item id
* @param slot{number}: Item slot number
* @param displayId{number}: DisplayId of the item
* @param env {('classic'|'live')}: select game env
* @return {Promise<boolean|*>}
*/
async function getDisplaySlot(item, slot, displayId, env=`live`) {
if (typeof item !== `number`) {
throw new Error(`item must be a number`)
}
if (typeof slot !== `number`) {
throw new Error(`slot must be a number`)
}
if (typeof displayId !== `number`) {
throw new Error(`displayId must be a number`)
}
try {
const jsonPath = (env === `classic` && [21, 22].includes(slot)) ?
`${window.CONTENT_PATH}meta/item/${displayId}.json` :
`${window.CONTENT_PATH}meta/armor/${slot}/${displayId}.json`
await fetch(jsonPath)
.then(response => response.json())
return {
displaySlot: slot,
displayId: displayId
}
} catch (e) {
if(!window.WOTLK_TO_RETAIL_DISPLAY_ID_API){
throw Error(`Item not found and window.WOTLK_TO_RETAIL_DISPLAY_ID_API not set`)
}
const resp = await fetch(`${window.WOTLK_TO_RETAIL_DISPLAY_ID_API}/${item}/${displayId}`)
.then((response) => response.json())
const res = resp.data || resp
if (res.newDisplayId !== displayId) {
return {
displaySlot: slot,
displayId: res.newDisplayId
}
}
}
// old slots to new slots
const retSlot = {
5: 20, // chest
16: 21, // main hand
18: 22 // off hand
}[slot]
if (!retSlot) {
console.warn(`Item: ${item} display: ${displayId} or slot: ${slot} not found for `)
return {
displaySlot: slot,
displayId: displayId
}
}
return {
displaySlot: retSlot,
displayId: displayId
}
}
/**
* Returns a 2-dimensional list the inner list contains on first position the item slot, the second the item
* display-id ex: [[1,1170],[3,4925]]
* @param {*[{item: {entry: number, displayid: number}, transmog: {entry: number, displayid: number}, slot: number}]} equipments
* @param env {('classic'|'live')}: select game enve
* @returns {Promise<number[]>}
*/
async function findItemsInEquipments(equipments, env=`live`) {
for (const equipment of equipments) {
if (NOT_DISPLAYED_SLOTS.includes(equipment.slot)) {
continue
}
const displayedItem = (Object.keys(equipment.transmog).length !== 0) ? equipment.transmog : equipment.item
const displaySlot = await getDisplaySlot(
displayedItem.entry,
equipment.slot,
displayedItem.displayid,
env
)
equipment.displaySlot = displaySlot.displaySlot
equipment.displayId = displaySlot.displayId
Object.assign(displaySlot, equipment)
}
return equipments
.filter(e => e.displaySlot)
.map(e => [
e.displaySlot,
e.displayId
]
)
}
/**
*
* @param {number} race
* @param {number} gender
* @returns {Promise<Object>}
*/
async function findRaceGenderOptions(race, gender) {
const raceGender = race * 2 - 1 + gender
const options = await fetch(`${window.CONTENT_PATH}meta/charactercustomization/${raceGender}.json`)
.then(
(response) => response.json()
)
if (options.data) {
return options.data
}
return options
}
export {
optionsFromModel,
findRaceGenderOptions,
findItemsInEquipments,
getDisplaySlot,
getCharacterOptions,
characterPart,
modelingType
}