-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayer-service.ts
275 lines (233 loc) · 8.42 KB
/
player-service.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import type { OnStart } from "@flamework/core";
import { Service } from "@flamework/core";
import { Janitor } from "@rbxts/janitor";
import type { Document } from "@rbxts/lapis";
import type { Logger } from "@rbxts/log";
import { Object } from "@rbxts/luau-polyfill";
import Signal from "@rbxts/rbx-better-signal";
import { Players } from "@rbxts/services";
import { $NODE_ENV } from "rbxts-transform-env";
import PlayerEntity from "server/player/player-entity";
import type { PlayerData } from "shared/store/persistent";
import type { ListenerData } from "shared/util/flamework-util";
import { setupLifecycle } from "shared/util/flamework-util";
import { onPlayerAdded, promisePlayerDisconnected } from "shared/util/player-util";
import KickCode from "types/enum/kick-reason";
import type PlayerDataService from "./data/player-data-service";
import type PlayerRemovalService from "./player-removal-service";
export interface OnPlayerJoin {
/**
* Fires when a player joins the game and is fully initialized. The player
* is considered "fully initialized" once all of their data is loaded and
* their PlayerEntity class is fully setup. At which point, anything that
* implements this lifecycle event will be fired with the class.
*/
onPlayerJoin(player: PlayerEntity): void;
}
export interface OnPlayerLeave {
/**
* Fires when a player leaves the game. This is called before the player is
* removed from the game and their PlayerEntity class is cleaned up. This
* means it is still safe to access player data before it is removed.
*/
onPlayerLeave(player: PlayerEntity): Promise<void> | void;
}
/** A service that manages player entities in the game. */
@Service({})
export default class PlayerService implements OnStart {
private readonly onEntityJoined = new Signal<(playerEntity: PlayerEntity) => void>();
private readonly onEntityRemoving = new Signal();
private readonly playerEntities = new Map<Player, PlayerEntity>();
private readonly playerJoinEvents = new Array<ListenerData<OnPlayerJoin>>();
private readonly playerLeaveEvents = new Array<ListenerData<OnPlayerLeave>>();
constructor(
private readonly logger: Logger,
private readonly playerDataService: PlayerDataService,
private readonly playerRemovalService: PlayerRemovalService,
) {}
/** @ignore */
public onStart(): void {
setupLifecycle<OnPlayerJoin>(this.playerJoinEvents);
setupLifecycle<OnPlayerLeave>(this.playerLeaveEvents);
onPlayerAdded(player => {
this.onPlayerAdded(player).catch(err => {
this.logger.Error(`Failed to load player ${player.UserId}: ${err}`);
});
});
Players.PlayerRemoving.Connect(
this.withPlayerEntity(playerEntity => {
this.onPlayerRemoving(playerEntity).catch(err => {
this.logger.Error(`Failed to close player ${playerEntity.userId}: ${err}`);
});
}),
);
this.bindHoldServerOpen();
}
/**
* Returns an array of all `PlayerEntity` instances associated with players
* that have joined the game.
*
* @returns An array of `PlayerEntity` instances.
*/
public getPlayerEntities(): Array<PlayerEntity> {
return Object.values(this.playerEntities);
}
/**
* Gets the `PlayerEntity` object for a given player.
*
* @param player - The `Player` instance to get the `PlayerEntity` for.
* @returns The `PlayerEntity` instance associated with the given `Player`,
* if any.
*/
public getPlayerEntity(player: Player): PlayerEntity | undefined {
return this.playerEntities.get(player);
}
/**
* Retrieves the player entity asynchronously. This method will reject if
* the player disconnects before the entity is created.
*
* @param player - The player object.
* @returns A promise that resolves to the player entity, or undefined if
* not found.
*/
public async getPlayerEntityAsync(player: Player): Promise<PlayerEntity | undefined> {
const potentialEntity = this.getPlayerEntity(player);
if (potentialEntity) {
return potentialEntity;
}
const promise = Promise.fromEvent(
this.onEntityJoined,
(playerEntity: PlayerEntity) => playerEntity.player === player,
);
// eslint-disable-next-line promise/always-return -- This is the last callback
const disconnect = promisePlayerDisconnected(player).then(() => {
promise.cancel();
});
const [success, playerEntity] = promise.await();
if (!success) {
throw `Player ${player.UserId} disconnected before entity was created`;
}
disconnect.cancel();
return playerEntity;
}
/**
* This method wraps a callback and replaces the first argument (that must
* be of type `Player`) with that players `PlayerEntity` class.
*
* @param func - The callback to wrap.
* @returns A new callback that replaces the first argument with the
* player's `PlayerEntity` class.
*/
public withPlayerEntity<T extends Array<unknown>, R = void>(
func: (playerEntity: PlayerEntity, ...args: T) => R,
) {
return (player: Player, ...args: T): R | undefined => {
const playerEntity = this.getPlayerEntity(player);
if (!playerEntity) {
this.logger.Error(
`No entity for player ${player.UserId}, cannot continue to callback`,
);
return;
}
return func(playerEntity, ...args);
};
}
/**
* Called internally when a player joins the game.
*
* @param player - The player that joined the game.
*/
private async onPlayerAdded(player: Player): Promise<void> {
const playerDocument = await this.playerDataService.loadPlayerData(player);
if (!playerDocument) {
this.playerRemovalService.removeForBug(player, KickCode.PlayerInstantiationError);
return;
}
const janitor = this.setupPlayerJanitor(player, playerDocument);
const playerEntity = new PlayerEntity(player, janitor, playerDocument);
this.playerEntities.set(player, playerEntity);
// Call all connected lifecycle events
debug.profilebegin("Lifecycle_Player_Join");
for (const { id, event } of this.playerJoinEvents) {
janitor
.AddPromise(
Promise.defer(() => {
debug.profilebegin(id);
event.onPlayerJoin(playerEntity);
}),
)
.catch(err => {
this.logger.Error(`Error in player lifecycle ${id}: ${err}`);
});
}
debug.profileend();
this.logger.Info(`Player ${player.UserId} joined the game.`);
this.onEntityJoined.Fire(playerEntity);
}
private setupPlayerJanitor(player: Player, playerDocument: Document<PlayerData>): Janitor {
const janitor = new Janitor();
janitor.Add(async () => {
this.logger.Info(`Player ${player.UserId} leaving game, cleaning up Janitor`);
try {
await playerDocument.close();
} catch (err) {
this.logger.Error(`Failed to close player document for ${player.UserId}: ${err}`);
}
this.playerEntities.delete(player);
this.onEntityRemoving.Fire();
});
return janitor;
}
/**
* Called internally when a player is removed from the game. We hold the
* PlayerEntity until all lifecycle events have been called, so that we can
* access player data on player leaving if required.
*
* @param playerEntity - The player entity associated with the player.
*/
private async onPlayerRemoving(playerEntity: PlayerEntity): Promise<void> {
// Call all connected lifecycle events
const promises = new Array<Promise<void>>();
debug.profilebegin("Lifecycle_Player_Leave");
for (const { id, event } of this.playerLeaveEvents) {
const promiseEvent = Promise.defer<void>((resolve, reject) => {
debug.profilebegin(id);
try {
const leaveEvent = async (): Promise<void> => {
await event.onPlayerLeave(playerEntity);
};
const [success, err] = leaveEvent().await();
if (!success) {
reject(err);
return;
}
resolve();
} catch (err) {
this.logger.Error(`Error in player lifecycle ${id}: ${err}`);
}
});
promises.push(promiseEvent);
}
debug.profileend();
// We ensure that all lifecycle events are called before we continue
// since we may need to access player data in these events.
await Promise.all(promises).finally(() => {
playerEntity.janitor.Destroy();
});
}
private bindHoldServerOpen(): void {
// We want to hold the server open until all PlayerEntities are cleaned
// up and removed.
game.BindToClose(() => {
// We don't want to hold the server open in development
if ($NODE_ENV !== "production") {
return;
}
this.logger.Debug("Game closing, holding open until all player entities are removed.");
while (!this.playerEntities.isEmpty()) {
this.onEntityRemoving.Wait();
}
this.logger.Debug("All player entities removed, closing game.");
});
}
}