Skip to content
net.ts 1.48 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;
}

export interface ClientToServerEvents {
  place: (pixel: Pixel, ack: (_: PacketAck<Pixel>) => void) => void;
}

// app context

export interface IAppContext {
  config: ClientConfig;
  user?: AuthSession;
  canvasPosition?: ICanvasPosition;
  setCanvasPosition: (v: ICanvasPosition) => void;
  cursorPosition?: IPosition;
  setCursorPosition: (v?: IPosition) => void;
}

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;
  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
};

export type ClientConfig = {
  pallete: {
    colors: PalleteColor[];
    pixel_cooldown: number;
  };
  canvas: CanvasConfig;
Grant's avatar
Grant committed
};

export type PacketAck<T> =
Grant's avatar
Grant committed
  | {
      success: true;
      data: T;
    }
  | { success: false; error: string };

export type AuthSession = {
  service: {
    software: {
      name: string;
      version: string;
    };
    instance: {
      hostname: string;
    };
  };
  user: {
    username: string;
    profile: string;
  };
};