/// import Websocket from "isomorphic-ws"; import { Endpoint } from "./endpoints"; import type { ZJoinRequest } from "../ws-api"; import { ZTileLayer, ZBlockPosition, ZBlock, ZUserId, ZPlayerPosition, ZVelocity, ZInputs, ZAngle, ZHeap, ZKeyKind } from "../types"; import { zBlockPosition } from "../types"; import { ZSPacket, ZPacket, ZSInit, ZMovementQueue } from "../packets"; import { zPacket, zsPacket } from "../packets"; import AsyncQueue from "../AsyncQueue"; import TileRegistration from "../tiles/TileRegistration"; export default class Connection { /** * Makes a connection to Smiley Face Game. * @param endpoint The websocket endpoint to establish a connection with. * @param token The token to use when connecting. * @param joinRequest The join request to supply when making the connection. */ static establish(endpoint: Endpoint, token: string, joinRequest: ZJoinRequest, extraChecks?: boolean): Promise; /** * The validator for a `BlockPosition`. */ readonly zBlockPosition: ReturnType; /** * The validator for packets sent from the server. */ readonly zsPacket: ReturnType; /** * The validator for packets sent from this client. This is so that friendly error messages are given to the client * incase an invalid packet is sent. */ readonly zPacket: ReturnType; /** * The websocket that the underlying connection is using. It is not recommended to use this, but you can if you must. */ readonly websocket: Websocket; /** * The `init` packet sent to the server. `Connection` guarantees that you will have received `init` by the time */ readonly init: ZSInit; /** * A data structure used to convert the callback nature of a websocket into an easy `for await` loop for the user. */ readonly messages: AsyncQueue; /** * The state for registered tiles. */ readonly tileJson: TileRegistration; private _connected; /** * Gets whether or not this connection is connected. */ get connected(): boolean; private readonly extraChecks; /** * Constructs a new `Connection`, given a `websocket` and an `init` message. * @param websocket The websocket that the connection is on. * @param init The payload of the `init` message. * @param extraChecks Whether or not to perform extra checks to make sure incoming packets are validated. */ constructor(websocket: Websocket, init: ZSInit, extraChecks: boolean); /** * Allows asynchronous iteration over this `Connection`. This allows the user to use a simple for loop to iterate * over every message, and makes the control flow and life of their application inherently more vivid. */ [Symbol.asyncIterator](): { next: () => Promise<{ done: false; value: ZSPacket; } | { done: true; }>; }; /** * Kills the connection. */ close(): void; /** * Sends a raw packet to the server. This method may be a bit more verbose than one would like, so consider any of * the overload helper methods to make your code more clear and less dependent on the exact implementation of the * packet structure. * @param packet The packet to send to the server. */ send(packet: ZPacket): void; /** * @package * Sends a raw packet on the websocket, without checking if it is valid. This is NOT meant to be used by consumers. * This is just so that helper methods (such as `place`) don't have to go through `send`'s validation, for a marginal * (but albeit un-necessary) speed boost. This is generally recommended for any paths that have already been type * checked. * @param packet The packet to send on the wire. */ private _send; /** * Sends a movement packet. * @param position The position the player is at. * @param velocity The current velocity of the player. * @param inputs The inputs the player is pressing. */ move(position: ZPlayerPosition, velocity: ZVelocity, inputs: ZInputs, queue: ZMovementQueue): void; /** * Sends a packet to pick up a gun. Picking up a gun occurs when the player walks over a gun block. From there, the * gun is automatically equipped so the player can start shooting. If the player presses `E`, the gun gets un-equipped * and can go back to editing, or can re-equip it by pressing `E` again. * @param position The position of the gun block to pickup. */ pickupGun(position: ZBlockPosition): void; /** * Equips a gun. Equipping a gun is when the player is holding a gun, and on pressing on the screen they will start * firing their gun. Picking up a gun happens when the player walks over a gun block to receive a gun. * @param equipped Whether the gun should be equipped or not. */ equipGun(equipped: boolean): void; /** * Fires a bullet. If the player doesn't have a gun equipped, you may get disconnected. * @param angle The angle to fire the bullet at. */ fireBullet(angle: ZAngle): void; /** * Places a block in the world. * @param block The block to place. * @param position The position to place the block at. * @param layer The layer to place the block on. * @param heap The heap associated data with the block, if any. */ place(block: ZBlock, position: ZBlockPosition, heap?: ZHeap, layer?: ZTileLayer): void; /** * Places a line of blocks in the world. * @param block The block to place. * @param start The starting position. * @param end The ending position. * @param layer The layer to place the block on. * @param heap The heap associated data with the block, if any. */ placeLine(block: ZBlock, start: ZBlockPosition, end: ZBlockPosition, heap?: ZHeap, layer?: ZTileLayer): void; /** * Sends a chat message. * @param message The message to send to chat. */ chat(message: string): void; /** * Gives edit to a player. This may cause you to disconnect if you are not the owner. * @param playerId The ID of the player to give edit to. */ giveEdit(playerId: ZUserId): void; /** * Takes edit from a player. This may cause you to disconnect if you are not the owner. * @param playerId The ID of the player to take edit from. */ takeEdit(playerId: ZUserId): void; /** * Gives edit to a player. This may cause you to disconnect if you are not the owner. * @param playerId The ID of the player to give edit to. */ giveGod(playerId: ZUserId): void; /** * Takes edit from a player. This may cause you to disconnect if you are not the owner. * @param playerId The ID of the player to take edit from. */ takeGod(playerId: ZUserId): void; /** * Kicks a player from your world. This may cause you to disconnect if you are not the owner. * @param playerId The ID of the player to kick. */ kick(playerId: ZUserId): void; /** * Sends a packet to the server to enable or disable god mode. It is assumed you already * know whether or not god mode is possible, so only send packets when you do. If you send * a god mode packet "correctly", you will not hear anything back from the server (but * other players will). If you send it incorrectly, you will hear a god mode packet back * from the server. * @param godMode If god mode should be enabled or not */ toggleGod(godMode: boolean): void; /** * Clears the world. This may cause you to disconnect if you are not the owner. */ clear(): void; /** * Saves the world. This may cause you to disconnect if you are not the owner. */ save(): void; /** * Loads the world. This may cause you to disconnect if you are not the owner. */ load(): void; /** * Changes the title of the room. * @param title The new title */ changeWorldTitle(title: string): void; /** * Touches a red key. Players receive the ID of the user triggering a key touch, and the time at which the key effect is to wear off. */ touchKey(kind: ZKeyKind): void; /** * Teleports a player to a given position, perhaps with velocity. * @param player The player to teleport * @param to The position to teleport to, in 16x16 pixels * @param velocity The velocity of the player */ teleport(player: ZUserId, to: ZPlayerPosition, velocity?: ZVelocity): void; }