forked from ker0olos/fable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignParty.ts
170 lines (136 loc) · 4.02 KB
/
assignParty.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
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
/// <reference lib="deno.unstable" />
import { charactersByInstancePrefix, inventoriesByUser } from './indices.ts';
import db, { kv } from './mod.ts';
import { KvError } from '../src/errors.ts';
import type * as Schema from './schema.ts';
export async function assignCharacter(
user: Schema.User,
instance: Schema.Instance,
characterId: string,
spot?: number,
): Promise<Schema.Character> {
const { inventory, inventoryCheck } = await db.getInventory(instance, user);
const character = await db.getValue<Schema.Character>([
...charactersByInstancePrefix(instance._id),
characterId,
]);
if (!character) {
throw new Error('CHARACTER_NOT_FOUND');
}
if (character.inventory !== inventory._id) {
throw new Error('CHARACTER_NOT_OWNED');
}
inventory.party ??= {};
if (typeof spot === 'number' && spot >= 1 && spot <= 5) {
switch (spot) {
case 1:
inventory.party.member1 = character._id;
break;
case 2:
inventory.party.member2 = character._id;
break;
case 3:
inventory.party.member3 = character._id;
break;
case 4:
inventory.party.member4 = character._id;
break;
case 5:
inventory.party.member5 = character._id;
break;
}
} else if (!inventory.party.member1) {
inventory.party.member1 = character._id;
} else if (!inventory.party.member2) {
inventory.party.member2 = character._id;
} else if (!inventory.party.member3) {
inventory.party.member3 = character._id;
} else if (!inventory.party.member4) {
inventory.party.member4 = character._id;
} else {
inventory.party.member5 = character._id;
}
const update = await kv.atomic()
.check(inventoryCheck)
//
.set(['inventories', inventory._id], inventory)
.set(inventoriesByUser(instance._id, user._id), inventory)
//
.commit();
if (update.ok) {
return character;
}
throw new KvError('failed to update inventory');
}
export async function swapSpots(
user: Schema.User,
instance: Schema.Instance,
a: number,
b: number,
): Promise<Schema.Party> {
const { inventory, inventoryCheck } = await db.getInventory(instance, user);
inventory.party ??= {};
const party = [
inventory.party.member1,
inventory.party.member2,
inventory.party.member3,
inventory.party.member4,
inventory.party.member5,
];
const t = party[a - 1];
party[a - 1] = party[b - 1];
party[b - 1] = t;
inventory.party.member1 = party[0];
inventory.party.member2 = party[1];
inventory.party.member3 = party[2];
inventory.party.member4 = party[3];
inventory.party.member5 = party[4];
const update = await kv.atomic()
.check(inventoryCheck)
//
.set(['inventories', inventory._id], inventory)
.set(inventoriesByUser(instance._id, user._id), inventory)
//
.commit();
if (update.ok) {
return await db.getUserParty(inventory);
}
throw new KvError('failed to update inventory');
}
export async function unassignCharacter(
user: Schema.User,
instance: Schema.Instance,
spot: number,
): Promise<Schema.Character | undefined> {
const { inventory, inventoryCheck } = await db.getInventory(instance, user);
inventory.party ??= {};
const party = [
inventory.party.member1,
inventory.party.member2,
inventory.party.member3,
inventory.party.member4,
inventory.party.member5,
];
const t = party[spot - 1];
if (!t) {
return;
}
party[spot - 1] = undefined;
inventory.party.member1 = party[0];
inventory.party.member2 = party[1];
inventory.party.member3 = party[2];
inventory.party.member4 = party[3];
inventory.party.member5 = party[4];
const update = await kv.atomic()
.check(inventoryCheck)
//
.set(['inventories', inventory._id], inventory)
.set(inventoriesByUser(instance._id, user._id), inventory)
//
.commit();
if (update.ok) {
const character = await db.getValue<Schema.Character>(['characters', t]);
return character ?? undefined;
}
throw new KvError('failed to update inventory');
}