BlueGit

timfa questward

Files

questward / server / src / friends / service.js

service.js

jewelerty, skulls
timfa Tim Falken · pushed by timfa · 2 months ago
2b6e44d0 History
service.js 7.0 KiB Download Raw
1import { layersFromBag } from "../../../shared/appearance/layers.js";
2
3/**
4 * Account-wide friends: requests and friendships key on account_id.
5 * Same-account characters are always treated as friends.
6 */
7export class FriendService
8{
9 constructor (stateDb)
10 {
11 this.stateDb = stateDb;
12 }
13
14 snapshot (characterId)
15 {
16 const accountId = this.#accountId(characterId);
17 return {
18 friends: this.listFriendAccounts(accountId),
19 incoming: this.#namedAccountRequests(
20 this.stateDb.listIncomingAccountRequests(accountId),
21 "from_account_id"
22 ),
23 outgoing: this.#namedAccountRequests(
24 this.stateDb.listOutgoingAccountRequests(accountId),
25 "to_account_id"
26 )
27 };
28 }
29
30 /** Flat character list for pigeon address book (all chars on friend accounts). */
31 listFriends (characterId)
32 {
33 const accounts = this.listFriendAccounts(this.#accountId(characterId));
34 const out = [];
35 for (const account of accounts)
36 {
37 for (const character of account.characters ?? [])
38 { out.push(character); }
39 }
40 return out;
41 }
42
43 listFriendAccounts (accountId)
44 {
45 const id = accountId | 0;
46 if (!id)
47 { return []; }
48 return this.stateDb.listAccountFriendRecords(id)
49 .map((row) => this.#publicAccount(row.account_id))
50 .filter(Boolean);
51 }
52
53 areFriends (characterId, otherCharacterId)
54 {
55 return this.stateDb.areFriends(characterId, otherCharacterId);
56 }
57
58 request (fromCharacterId, toCharacterId)
59 {
60 const fromId = Number(fromCharacterId);
61 const toId = Number(toCharacterId);
62 if (fromId === toId)
63 { throw userError("You cannot friend yourself."); }
64 const from = this.#requireCharacter(fromId);
65 const to = this.#requireCharacter(toId);
66 const fromAccount = from.account_id | 0;
67 const toAccount = to.account_id | 0;
68 if (fromAccount === toAccount)
69 { throw userError("You are already friends with your own characters."); }
70 if (this.stateDb.areAccountFriends(fromAccount, toAccount))
71 { throw userError("You are already friends."); }
72 if (this.stateDb.getAccountFriendRequest(fromAccount, toAccount))
73 { throw userError("Friend request already sent."); }
74 if (this.stateDb.getAccountFriendRequest(toAccount, fromAccount))
75 {
76 this.stateDb.deleteAccountFriendRequest(toAccount, fromAccount);
77 this.stateDb.insertAccountFriendship(fromAccount, toAccount);
78 return this.snapshot(fromId);
79 }
80 this.stateDb.insertAccountFriendRequest(fromAccount, toAccount);
81 return this.snapshot(fromId);
82 }
83
84 accept (characterId, fromCharacterId)
85 {
86 const toChar = this.#requireCharacter(characterId);
87 const fromChar = this.#requireCharacter(fromCharacterId);
88 const toAccount = toChar.account_id | 0;
89 const fromAccount = fromChar.account_id | 0;
90 if (!this.stateDb.getAccountFriendRequest(fromAccount, toAccount))
91 { throw userError("There is no friend request to accept."); }
92 this.stateDb.deleteAccountFriendRequest(fromAccount, toAccount);
93 this.stateDb.deleteAccountFriendRequest(toAccount, fromAccount);
94 if (!this.stateDb.areAccountFriends(fromAccount, toAccount))
95 { this.stateDb.insertAccountFriendship(fromAccount, toAccount); }
96 return this.snapshot(toChar.character_id);
97 }
98
99 /** Accept / decline / remove by target account id (friends panel). */
100 acceptAccount (characterId, fromAccountId)
101 {
102 const toChar = this.#requireCharacter(characterId);
103 const toAccount = toChar.account_id | 0;
104 const fromAccount = fromAccountId | 0;
105 if (!this.stateDb.getAccountFriendRequest(fromAccount, toAccount))
106 { throw userError("There is no friend request to accept."); }
107 this.stateDb.deleteAccountFriendRequest(fromAccount, toAccount);
108 this.stateDb.deleteAccountFriendRequest(toAccount, fromAccount);
109 if (!this.stateDb.areAccountFriends(fromAccount, toAccount))
110 { this.stateDb.insertAccountFriendship(fromAccount, toAccount); }
111 return this.snapshot(toChar.character_id);
112 }
113
114 decline (characterId, otherCharacterId)
115 {
116 const actor = this.#requireCharacter(characterId);
117 const other = this.stateDb.getCharacter(otherCharacterId);
118 if (!other)
119 { return this.snapshot(actor.character_id); }
120 this.stateDb.deleteAccountFriendRequest(actor.account_id, other.account_id);
121 this.stateDb.deleteAccountFriendRequest(other.account_id, actor.account_id);
122 return this.snapshot(actor.character_id);
123 }
124
125 declineAccount (characterId, otherAccountId)
126 {
127 const actor = this.#requireCharacter(characterId);
128 const otherAccount = otherAccountId | 0;
129 this.stateDb.deleteAccountFriendRequest(actor.account_id, otherAccount);
130 this.stateDb.deleteAccountFriendRequest(otherAccount, actor.account_id);
131 return this.snapshot(actor.character_id);
132 }
133
134 remove (characterId, otherCharacterId)
135 {
136 const actor = this.#requireCharacter(characterId);
137 const other = this.#requireCharacter(otherCharacterId);
138 if (!this.stateDb.areAccountFriends(actor.account_id, other.account_id))
139 { throw userError("You are not friends with that player."); }
140 if ((actor.account_id | 0) === (other.account_id | 0))
141 { throw userError("You cannot unfriend your own characters."); }
142 this.stateDb.deleteAccountFriendship(actor.account_id, other.account_id);
143 return this.snapshot(actor.character_id);
144 }
145
146 removeAccount (characterId, otherAccountId)
147 {
148 const actor = this.#requireCharacter(characterId);
149 const otherAccount = otherAccountId | 0;
150 if ((actor.account_id | 0) === otherAccount)
151 { throw userError("You cannot unfriend your own characters."); }
152 if (!this.stateDb.areAccountFriends(actor.account_id, otherAccount))
153 { throw userError("You are not friends with that player."); }
154 this.stateDb.deleteAccountFriendship(actor.account_id, otherAccount);
155 return this.snapshot(actor.character_id);
156 }
157
158 findByFullName (firstName, lastName)
159 {
160 const row = this.stateDb.getCharacterByName(firstName, lastName);
161 return row ? this.#publicCharacter(row.character_id) : null;
162 }
163
164 #namedAccountRequests (rows, idField)
165 {
166 return rows.map((row) => this.#publicAccount(row[idField])).filter(Boolean);
167 }
168
169 #publicAccount (accountId)
170 {
171 const account = this.stateDb.getAccount(accountId);
172 if (!account)
173 { return null; }
174 const characters = this.stateDb.listCharacters(account.account_id)
175 .filter((row) => !row.deletion_scheduled_at)
176 .map((row) => this.#publicCharacter(row.character_id))
177 .filter(Boolean);
178 return {
179 accountId: account.account_id,
180 username: this.stateDb.accountDisplayName(account.account_id),
181 characters
182 };
183 }
184
185 #publicCharacter (characterId)
186 {
187 const row = this.stateDb.getCharacter(characterId);
188 if (!row)
189 { return null; }
190 const bag = this.stateDb.getBag(characterId);
191 return {
192 characterId: row.character_id,
193 accountId: row.account_id,
194 firstName: row.first_name,
195 lastName: row.last_name,
196 sex: row.sex,
197 layers: layersFromBag(row.sex, bag)
198 };
199 }
200
201 #accountId (characterId)
202 {
203 return this.#requireCharacter(characterId).account_id | 0;
204 }
205
206 #requireCharacter (characterId)
207 {
208 const row = this.stateDb.getCharacter(characterId);
209 if (!row)
210 { throw userError("That player does not exist."); }
211 return row;
212 }
213}
214
215function userError (message)
216{
217 const error = new Error(message);
218 error.userFacing = true;
219 return error;
220}