📦State
An explanation of the three types of state in an extension, and when to use each.
Initialization State
Message State
Chat State
Example: Maze Extension
type InitStateType = {
// The maze is generated algorithmically
// at the beginning of a chat,
// with a configurable size,
// and is never the same maze twice.
// This grid stores where the walls are.
maze: MazeGrid
};
// The maze's message state is just the player's location,
// and a generated image being displayed currently, if any.
type MessageStateType = {userLocation: { posX: number, posY: number }, image: string | null };
// The maze's chat state keeps track of what tiles
// (plus a small radius around them) the user has visited,
// in order to keep the fog of war on those tiles cleared,
// even if the user backtracks and goes in a different direction.
type ChatStateType = {
visited: {[key: number]: Set<number>}
}
// Since the maze is square, we don't have to do anything fancy with nodes,
// just an array of arrays. Clever no, readable yes.
type MazeGrid = MazeCell[][];
interface MazeCell {
walls: { [key in MazeWall]: boolean };
colNum: number;
rowNum: number;
// This is used for something else internally,
// unrelated to the chat state 'visited'.
visited: boolean;
}
enum MazeWall {
down = 'down',
right = 'right',
up = 'up',
left = 'left',
}Last updated