-
Notifications
You must be signed in to change notification settings - Fork 0
/
factions.js
78 lines (66 loc) · 1.75 KB
/
factions.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
import {zipObject, map} from 'lodash'
const parentLookup = {
'Rebel Alliance' : 'REBEL',
'Resistance' : 'REBEL',
'Galactic Empire' : 'EMPIRE',
'First Order' : 'EMPIRE',
'Scum and Villainy': 'SCUM',
}
// create factions
const createFaction = async (faction: Faction, client: Client) => {
const {name, xws} = faction
const parent = parentLookup[name] || '';
const result = await client.mutate(`{
faction: createFaction(
name: "${name}",
parent: ${parent},
xws: "${xws}"
){
id
}
}`)
if(!result) {
console.log(`Error adding ${faction}:`)
console.log(faction)
}
// else {
// console.log(`Added ${name} faction`)
// }
return result.faction.id
}
const findFaction = async (faction: Faction, client: Client) => {
const {name} = faction
const result = await client.query(`{
allFactions(
filter: {
name: "${name}"
}
) {
id
}
}`)
return result
}
export const factionListToObj = (allFactions: string[]) => {
const toObj = (item) => {
const xws = item.replace(/\W/g,'').toLowerCase()
return {
'name': item,
'xws': xws
}
}
return map(allFactions, toObj)
}
export const createFactions = async (rawFactions: Faction[], client: Client): Promise<IdMap> => {
const factionIds = await Promise.all(rawFactions.map( async(faction) => {
// check if faction already exists
const existingFaction = await findFaction(faction, client)
// if faction doesn't exist, create it
if (existingFaction.allFactions.length === 0) {
return await createFaction(faction, client)
} else {
return existingFaction.allFactions[0].id
}
}))
return zipObject(rawFactions.map(faction => faction.name), factionIds)
}