export class FriendService { constructor (stateDb) { this.stateDb = stateDb; } snapshot (characterId) { return { friends: this.listFriends(characterId), incoming: this.#namedRequests(this.stateDb.listIncomingRequests(characterId), "from_character_id"), outgoing: this.#namedRequests(this.stateDb.listOutgoingRequests(characterId), "to_character_id") }; } listFriends (characterId) { return this.stateDb.listFriendRecords(characterId).map((row) => this.#publicCharacter(row.character_id)).filter(Boolean); } request (fromCharacterId, toCharacterId) { const fromId = Number(fromCharacterId); const toId = Number(toCharacterId); if (fromId === toId) { throw userError("You cannot friend yourself."); } this.#requireCharacter(fromId); this.#requireCharacter(toId); if (this.stateDb.areFriends(fromId, toId)) { throw userError("You are already friends."); } if (this.stateDb.getFriendRequest(fromId, toId)) { throw userError("Friend request already sent."); } if (this.stateDb.getFriendRequest(toId, fromId)) { this.stateDb.deleteFriendRequest(toId, fromId); this.stateDb.insertFriendship(fromId, toId); return this.snapshot(fromId); } this.stateDb.insertFriendRequest(fromId, toId); return this.snapshot(fromId); } accept (characterId, fromCharacterId) { const toId = Number(characterId); const fromId = Number(fromCharacterId); this.#requireCharacter(toId); this.#requireCharacter(fromId); if (!this.stateDb.getFriendRequest(fromId, toId)) { throw userError("There is no friend request to accept."); } this.stateDb.deleteFriendRequest(fromId, toId); this.stateDb.deleteFriendRequest(toId, fromId); if (!this.stateDb.areFriends(fromId, toId)) { this.stateDb.insertFriendship(fromId, toId); } return this.snapshot(toId); } decline (characterId, otherCharacterId) { const actorId = Number(characterId); const otherId = Number(otherCharacterId); this.stateDb.deleteFriendRequest(actorId, otherId); this.stateDb.deleteFriendRequest(otherId, actorId); return this.snapshot(actorId); } remove (characterId, otherCharacterId) { const actorId = Number(characterId); const otherId = Number(otherCharacterId); if (!this.stateDb.areFriends(actorId, otherId)) { throw userError("You are not friends with that player."); } this.stateDb.deleteFriendship(actorId, otherId); return this.snapshot(actorId); } findByFullName (firstName, lastName) { const row = this.stateDb.getCharacterByName(firstName, lastName); return row ? this.#publicCharacter(row.character_id) : null; } #namedRequests (rows, idField) { return rows.map((row) => this.#publicCharacter(row[idField])).filter(Boolean); } #publicCharacter (characterId) { const row = this.stateDb.getCharacter(characterId); if (!row) { return null; } return { characterId: row.character_id, firstName: row.first_name, lastName: row.last_name }; } #requireCharacter (characterId) { if (!this.stateDb.getCharacter(characterId)) { throw userError("That player does not exist."); } } } function userError (message) { const error = new Error(message); error.userFacing = true; return error; }