Skip to content
net.ts 3.24 KiB
Newer Older
// socket.io

export interface ServerToClientEvents {
  canvas: (pixels: string[]) => void;
  user: (user: AuthSession) => void;
  config: (config: ClientConfig) => void;
  pixel: (pixel: Pixel) => void;
  online: (count: { count: number }) => void;
Grant's avatar
Grant committed
  availablePixels: (count: number) => void;
  pixelLastPlaced: (time: number) => void;
Grant's avatar
Grant committed
  undo: (
    data: { available: false } | { available: true; expireAt: number }
  ) => void;
}

export interface ClientToServerEvents {
Grant's avatar
Grant committed
  place: (
    pixel: Pixel,
    ack: (
      _: PacketAck<
        Pixel,
        | "no_user"
        | "invalid_pixel"
        | "pixel_cooldown"
        | "palette_color_invalid"
        | "you_already_placed_that"
Grant's avatar
Grant committed
      >
    ) => void
  ) => void;
Grant's avatar
Grant committed
  undo: (ack: (_: PacketAck<{}, "no_user" | "unavailable">) => void) => void;
Grant's avatar
Grant committed
// TODO: move to client/{...}/AppContext.tsx
export interface IAppContext {
  config?: ClientConfig;
  user?: AuthSession;
  canvasPosition?: ICanvasPosition;
  setCanvasPosition: (v: ICanvasPosition) => void;
  cursorPosition?: IPosition;
  setCursorPosition: (v?: IPosition) => void;
Grant's avatar
Grant committed
  pixels: { available: number };
Grant's avatar
Grant committed
  undo?: { available: true; expireAt: number };
Grant's avatar
Grant committed
  loadChat: boolean;
  setLoadChat: (v: boolean) => void;
  settingsSidebar: boolean;
  setSettingsSidebar: (v: boolean) => void;
  pixelWhois?: { x: number; y: number; surrounding: string[][] };
  setPixelWhois: (v: this["pixelWhois"]) => void;
  showKeybinds: boolean;
  setShowKeybinds: (v: boolean) => void;
Grant's avatar
Grant committed
  showVirginOverlay: boolean;
  setShowVirginOverlay: React.Dispatch<React.SetStateAction<boolean>>;

Grant's avatar
Grant committed
  hasAdmin: boolean;
}

export interface IPalleteContext {
  color?: number;
}

export interface ICanvasPosition {
Grant's avatar
Grant committed
  x: number;
  y: number;
  zoom: number;
}
Grant's avatar
Grant committed

export interface IPosition {
  x: number;
  y: number;
}

// other
Grant's avatar
Grant committed

export type Pixel = {
Grant's avatar
Grant committed
  x: number;
  y: number;
Grant's avatar
Grant committed
  /**
   * Palette color ID or -1 for nothing
   */
Grant's avatar
Grant committed
  color: number;
};

export type PalleteColor = {
  id: number;
  name: string;
  hex: string;
Grant's avatar
Grant committed
};

export type CanvasConfig = {
  size: [number, number];
  zoom: number;
Grant's avatar
Grant committed
  pixel: {
    maxStack: number;
    cooldown: number;
    multiplier: number;
  };
Grant's avatar
Grant committed
  undo: {
    /**
     * time in ms to allow undos
     */
    grace_period: number;
  };
Grant's avatar
Grant committed
};

export type ClientConfig = {
  /**
   * Monolith git hash, if it doesn't match, client will reload
   */
  version: string;
  pallete: {
    colors: PalleteColor[];
    pixel_cooldown: number;
  };
  canvas: CanvasConfig;
  chat: {
    enabled: boolean;
    /**
     * @example aftermath.gg
     */
    matrix_homeserver: string;
    /**
     * @example https://chat.fediverse.events
     */
    element_host: string;
  };
Grant's avatar
Grant committed
};

Grant's avatar
Grant committed
/**
 * @template T the packet data
 * @template E union type of errors possible
 */
export type PacketAck<T, E = string> =
Grant's avatar
Grant committed
  | {
      success: true;
      data: T;
    }
Grant's avatar
Grant committed
  | { success: false; error: E };
Grant's avatar
Grant committed

export type AuthSession = {
  service: {
    software: {
      name: string;
      version: string;
Grant's avatar
Grant committed
      logo_uri?: string;
      repository?: string;
      homepage?: string;
Grant's avatar
Grant committed
    };
    instance: {
      hostname: string;
Grant's avatar
Grant committed
      logo_uri?: string;
      banner_uri?: string;
      name?: string;
Grant's avatar
Grant committed
    };
  };
  user: {
    username: string;
Grant's avatar
Grant committed
    picture_url?: string;
Grant's avatar
Grant committed
  };
};