-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathstarWarsData.ts
76 lines (62 loc) · 1.73 KB
/
starWarsData.ts
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
/**
* This defines a basic set of data for our Star Wars Schema.
*
* This data is hard coded for the sake of the demo, but you could imagine
* fetching this data from a backend service rather than from hardcoded
* JSON objects in a more complex demo.
*/
interface Ship {
id: string;
name: string;
}
const allShips: Array<Ship> = [
{ id: '1', name: 'X-Wing' },
{ id: '2', name: 'Y-Wing' },
{ id: '3', name: 'A-Wing' },
// Yeah, technically it's Corellian. But it flew in the service of the rebels,
// so for the purposes of this demo it's a rebel ship.
{ id: '4', name: 'Millennium Falcon' },
{ id: '5', name: 'Home One' },
{ id: '6', name: 'TIE Fighter' },
{ id: '7', name: 'TIE Interceptor' },
{ id: '8', name: 'Executor' },
];
interface Faction {
id: string;
name: string;
ships: Array<string>;
}
const rebels: Faction = {
id: '1',
name: 'Alliance to Restore the Republic',
ships: ['1', '2', '3', '4', '5'],
};
const empire: Faction = {
id: '2',
name: 'Galactic Empire',
ships: ['6', '7', '8'],
};
const allFactions: Array<Faction> = [rebels, empire];
let nextShip = 9;
export function createShip(shipName: string, factionId: string): Ship {
const newShip = {
id: String(nextShip++),
name: shipName,
};
allShips.push(newShip);
const faction = allFactions.find((obj) => obj.id === factionId);
faction?.ships.push(newShip.id);
return newShip;
}
export function getShip(id: string): Ship | undefined {
return allShips.find((ship) => ship.id === id);
}
export function getFaction(id: string): Faction | undefined {
return allFactions.find((faction) => faction.id === id);
}
export function getRebels(): Faction {
return rebels;
}
export function getEmpire(): Faction {
return empire;
}