bloxd-types 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ declare function debugLog(message: string): void;
2
+
3
+ export { debugLog };
@@ -0,0 +1,67 @@
1
+ import { PlayerId } from '@bloxd';
2
+
3
+ declare const __brand: unique symbol;
4
+ type Branded<T, B> = T & {
5
+ [__brand]: B;
6
+ };
7
+ type TimeoutId = Branded<number, "TimeoutId">;
8
+ type IntervalId = Branded<number, "IntervalId">;
9
+ /**
10
+ * Registers a function to be called after a delay.
11
+ * @param callback - The callback to call when the timeout expires
12
+ * @param delay - The delay in milliseconds before the callback is called
13
+ * @param playerId - Optional playerId to tie the timeout to. If the player leaves, this timeout will be cleared
14
+ * @returns The timeout id
15
+ */
16
+ declare function setTimeout(callback: () => void, delay: number, playerId?: PlayerId): TimeoutId;
17
+ /**
18
+ * Registers a function to be called repeatedly every interval.
19
+ * @param callback - The callback to call when the interval expires
20
+ * @param interval - The interval in milliseconds before the callback is called
21
+ * @param playerId - Optional playerId to tie the interval to. If the player leaves, this interval will be stopped
22
+ * @returns The interval id
23
+ */
24
+ declare function setInterval(callback: () => void, interval: number, playerId?: PlayerId): IntervalId;
25
+ /**
26
+ * Clears a timeout.
27
+ * @param id - The id of the timeout to clear
28
+ */
29
+ declare function clearTimeout(id: TimeoutId): void;
30
+ /**
31
+ * Clears an interval.
32
+ * @param id - The id of the interval to clear
33
+ */
34
+ declare function clearInterval(id: IntervalId): void;
35
+
36
+ /**
37
+ * Forces a chunk to be loaded.
38
+ * @param x - The x coordinate of the chunk
39
+ * @param y - The y coordinate of the chunk
40
+ * @param z - The z coordinate of the chunk
41
+ * @param onLoaded - The function to call when the chunk is loaded
42
+ */
43
+ declare function forceLoadChunk(x: number, y: number, z: number, onLoaded: () => void): void;
44
+
45
+ /**
46
+ * Generates a random integer between min and max, inclusive.
47
+ * @param min - The minimum value.
48
+ * @param max - The maximum value.
49
+ * @param includeMax - Whether to include the max value in the range. Defaults to false.
50
+ * @returns A random integer between min and max, inclusive.
51
+ */
52
+ declare function randomInt(min: number, max: number, includeMax?: boolean): number;
53
+ /**
54
+ * Shuffles an array in place.
55
+ * @param array - The array to shuffle.
56
+ * @returns The shuffled array.
57
+ */
58
+ declare function shuffleArray<T>(array: T[]): T[];
59
+ /**
60
+ * Gets a random item from an array.
61
+ * @param array - The array to get a random item from.
62
+ * @returns A random item from the array.
63
+ */
64
+ declare function getRandomItem<T extends readonly any[]>(array: T): T[number];
65
+
66
+ export { clearInterval, clearTimeout, forceLoadChunk, getRandomItem, randomInt, setInterval, setTimeout, shuffleArray };
67
+ export type { IntervalId, TimeoutId };
@@ -0,0 +1,185 @@
1
+ import * as _bloxd from '@bloxd';
2
+ import { PNull, PlayerId, CustomTextStyling, BlockName, ChatTags, Pos, RecursiveReadonly } from '@bloxd';
3
+
4
+ declare class SessionBasedGame<TeamInfo extends BaseTeamInfo, MapInfo extends BaseMapInfo<TeamInfo>, Player extends SessionBasedPlayer<TeamInfo, MapInfo>> {
5
+ readonly gameOptions: Required<SessionBasedGameSetupOptions<TeamInfo, MapInfo, Player>>;
6
+ gamePhase: "beforeGame" | "inGame" | "gameFinished";
7
+ private startingRound;
8
+ private players;
9
+ map: PNull<MapInfo>;
10
+ private gameStartTime;
11
+ private startGameAt;
12
+ private endGameAt;
13
+ private mapVoter;
14
+ messageColours: {
15
+ error: string;
16
+ success: string;
17
+ info: string;
18
+ };
19
+ private get playersForFullLobby();
20
+ private get playerCount();
21
+ constructor(gameOptions: Required<SessionBasedGameSetupOptions<TeamInfo, MapInfo, Player>>);
22
+ /**
23
+ * Gets the player object for a given player ID.
24
+ * @param playerId The player ID to get the player for
25
+ */
26
+ getPlayer(playerId: PlayerId): Player;
27
+ /**
28
+ * Gets all the players in the game, including spectators.
29
+ * @returns An iterator of all the players in the game.
30
+ */
31
+ getPlayers(): Generator<Player>;
32
+ getStartingInText(): string | CustomTextStyling;
33
+ /**
34
+ * Gives all players coloured armour based on their team name. Spectators will get black armour.
35
+ * If their team name is not a valid colour, they will be skipped.
36
+ */
37
+ giveColouredArmourToEveryone(): void;
38
+ onPlayerJoin(playerId: PlayerId): void;
39
+ onPlayerLeave(playerId: PlayerId): void;
40
+ onPlayerDie(playerId: PlayerId): void;
41
+ checkWin(): void;
42
+ onPlayerChat(playerId: PlayerId, message: string): _bloxd.ChatTags;
43
+ onPlayerBoughtShopItem(playerId: PlayerId, categoryKey: string, itemKey: string): void;
44
+ tick(): void;
45
+ private getTeamsAlive;
46
+ private updateTimeTillStart;
47
+ private setStartGame;
48
+ private checkGameTransition;
49
+ private sendRightInfoText;
50
+ private loadMapFromRect;
51
+ private startGame;
52
+ /**
53
+ * Whether spectators should render faded. gamePhase can't be read directly: it only flips to
54
+ * "inGame" once startGame() has started every player, and the sit-out spectators it makes
55
+ * along the way need fading too.
56
+ */
57
+ spectatorsAreFaded(): boolean;
58
+ private assignTeams;
59
+ /**
60
+ * Manually sets a team to win the game. Will begin the game reset timer to restart the game.#
61
+ * Automatically called when there is only one team left.
62
+ * @param winningTeam - The team object that won the game.
63
+ */
64
+ makeTeamWin(winningTeam: PNull<TeamInfo>): void;
65
+ private resetGame;
66
+ onBlockStandStart(playerId: PlayerId, x: number, y: number, z: number, blockName: BlockName): void;
67
+ private placeTeamPad;
68
+ private placeTeamPads;
69
+ }
70
+
71
+ declare class SessionBasedPlayer<TeamInfo extends BaseTeamInfo, MapInfo extends BaseMapInfo<TeamInfo>> {
72
+ readonly playerId: PlayerId;
73
+ readonly game: SessionBasedGame<TeamInfo, MapInfo, SessionBasedPlayer<TeamInfo, MapInfo>>;
74
+ isSpectating: boolean;
75
+ team: PNull<TeamInfo>;
76
+ requestedTeam: PNull<TeamInfo>;
77
+ qTextId: PNull<string>;
78
+ constructor(playerId: PlayerId, game: SessionBasedGame<TeamInfo, MapInfo, SessionBasedPlayer<TeamInfo, MapInfo>>);
79
+ private spawn;
80
+ /**
81
+ * Sets the player as playing the game, setting their health, shields, and other settings.
82
+ * Automatically called at the start of a game for players assigned to a team.
83
+ */
84
+ startPlaying(): void;
85
+ /**
86
+ * Sets the player as spectating the game, resetting their health, shields, and other settings.
87
+ * Automatically called at the start of a game for players not assigned to a team.
88
+ * Call this function to turn a player into a spectator (for example, once they die).
89
+ */
90
+ startSpectating(): void;
91
+ private updateOtherEntitySettings;
92
+ onMessage(): ChatTags;
93
+ onRespawnRequest(): Pos | undefined;
94
+ /**
95
+ * Gets the position of the player relative to the play area.
96
+ * @returns The position of the player relative to the play area.
97
+ */
98
+ getPositionInPlayArea(): Pos;
99
+ }
100
+
101
+ type BaseTeamInfo = RecursiveReadonly<{
102
+ name: string;
103
+ maxPlayers: number;
104
+ colour: string;
105
+ }>;
106
+ type BaseMapInfo<TeamInfo extends BaseTeamInfo> = RecursiveReadonly<{
107
+ /** The name of the map */
108
+ name: string;
109
+ /** The icon of the map - used in the Map Voting Shop UI */
110
+ icon: string;
111
+ /** Block coordinates representing the start and the end of the map. These will be rounded outwards to the nearest chunk boundary */
112
+ source: {
113
+ rect: [Pos, Pos];
114
+ };
115
+ }> & {
116
+ readonly teams: Record<TeamInfo["name"], RecursiveReadonly<{
117
+ spawn: Pos;
118
+ spawnFacing: Pos;
119
+ }>>;
120
+ };
121
+ type Constructor<T> = new (...args: any[]) => T;
122
+ type TeamPadInfo = RecursiveReadonly<{
123
+ rect: [Pos, Pos];
124
+ emptyBlock: BlockName;
125
+ fullBlock: BlockName;
126
+ }>;
127
+ type SessionBasedGameSetupOptions<TeamInfo extends BaseTeamInfo, MapInfo extends BaseMapInfo<TeamInfo>, Player extends SessionBasedPlayer<TeamInfo, MapInfo>> = {
128
+ teams: readonly TeamInfo[];
129
+ maps: readonly MapInfo[];
130
+ /** Lobby area information */
131
+ lobby: RecursiveReadonly<{
132
+ /** Block coordinates representing the start and the end of the lobby area. */
133
+ source: {
134
+ rect: [Pos, Pos];
135
+ };
136
+ spawn: Pos;
137
+ spawnFacing: Pos;
138
+ }> & {
139
+ /** Block coordinates representing the start and the end of the team pads. Offset from the lowest corner of the map (after rounding outwards). If not provided, team choosing will be disabled. */
140
+ teamPads?: Record<TeamInfo["name"], TeamPadInfo>;
141
+ };
142
+ /** Block coordinates representing the start and the end of the play area. These will be rounded outwards to the nearest chunk boundary */
143
+ playAreaRect: readonly [Pos, Pos];
144
+ /** Maximum number of spectators allowed in the game */
145
+ maxSpectators?: number;
146
+ /** The number of seconds to wait before resetting the game after the game finishes */
147
+ emptyLobbyStartInSecs?: number;
148
+ /** The number of seconds to wait before starting the game when the lobby is full */
149
+ fullLobbyStartInSecs?: number;
150
+ /** The number of seconds to wait before resetting the game after the game finishes */
151
+ gameResetInSecs?: number;
152
+ /** Enable Map Voting - If enabled, players will be able to vote for the next map to play. */
153
+ mapVotingEnabled?: boolean;
154
+ /** Function to call when the game starts */
155
+ onStart?: () => void;
156
+ /** Function to call when the game finishes */
157
+ onFinish?: (winningTeam: TeamInfo) => void;
158
+ /** Function to get the RightInfoText for a player */
159
+ getRightInfoText?: (player: SessionBasedPlayer<TeamInfo, MapInfo>) => PNull<string | CustomTextStyling>;
160
+ /** The constructor for the player class. This should be a class that extends SessionBasedPlayer */
161
+ playerConstructor?: Constructor<Player>;
162
+ };
163
+
164
+ /**
165
+ * Create a new SessionBasedGame.
166
+ * @param opts - The options for the game.
167
+ * @param opts.teams - The teams for the game. This should include the team name, max players for this team, and hex colour.
168
+ * @param opts.maps - The maps for the game. This should include the map name, source rectangle, and the spawn positions for each team.
169
+ * @param opts.lobby - The lobby area. This should include the source rectangle, spawn info, and team pads for team choosing.
170
+ * @param opts.playAreaRect - The play area rectangle. These will be rounded outwards to the nearest chunk boundary
171
+ * @param opts.maxSpectators - The maximum number of spectators allowed in the game.
172
+ * @param opts.emptyLobbyStartInSecs - The number of seconds to wait for the game to start when the lobby isn't full yet.
173
+ * @param opts.fullLobbyStartInSecs - The number of seconds to wait for the game to start when the lobby is full.
174
+ * @param opts.gameResetInSecs - The number of seconds to wait before resetting the game after the game finishes.
175
+ * @param opts.mapVotingEnabled - Whether to enable map voting.
176
+ * @param opts.onStart - The function to call when the game starts.
177
+ * @param opts.onFinish - The function to call when the game finishes.
178
+ * @param opts.getRightInfoText - The function to call to get the RightInfoText for the game.
179
+ * @param opts.playerConstructor - The constructor for the player class. This should be a class that extends SessionBasedPlayer
180
+ * @returns The game instance
181
+ */
182
+ declare function createGame<TeamInfo extends BaseTeamInfo, MapInfo extends BaseMapInfo<TeamInfo>, Player extends SessionBasedPlayer<TeamInfo, MapInfo>>(opts: SessionBasedGameSetupOptions<TeamInfo, MapInfo, Player>): SessionBasedGame<TeamInfo, MapInfo, Player>;
183
+
184
+ export { SessionBasedGame, SessionBasedPlayer, createGame };
185
+ export type { BaseMapInfo, BaseTeamInfo, SessionBasedGameSetupOptions };