api.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Typed API exposed to the renderer by the preload script. Type-only: shared by preload and renderer.
  2. import type { RgbaImage } from '../shared/maps/loader';
  3. import type { Phase } from '../shared/protocol';
  4. export interface MapFiles {
  5. /** Raw map.json, validated by the renderer with shared/maps/loader. */
  6. definition: unknown;
  7. terrain: RgbaImage;
  8. /** background.png bytes. */
  9. background: Uint8Array;
  10. }
  11. export interface HostResult {
  12. port: number;
  13. }
  14. /** A game seen on the LAN. */
  15. export interface LanGame {
  16. serverId: string;
  17. name: string;
  18. mapId: string;
  19. players: number;
  20. maxPlayers: number;
  21. phase: Phase;
  22. version: number;
  23. address: string;
  24. port: number;
  25. }
  26. export interface LanApi {
  27. /** Starts the local authoritative server (once) and resolves once it listens. */
  28. host(name: string): Promise<HostResult>;
  29. /** Starts listening for LAN games; `onGames` receives the full list about once per second. Returns a stop function. */
  30. discover(onGames: (games: LanGame[]) => void): () => void;
  31. }
  32. export interface AssetsApi {
  33. materials(): Promise<unknown>;
  34. /** Raw character.json of every playable character. */
  35. characters(): Promise<unknown[]>;
  36. map(mapId: string): Promise<MapFiles>;
  37. }
  38. /** Where the self-update stands (src/main/updater.ts). */
  39. export type UpdateStatus =
  40. | { state: 'idle' }
  41. | { state: 'downloading'; version: string; progress: number }
  42. /** Downloaded: runs on the next launch. */
  43. | { state: 'ready'; version: string }
  44. /** Needs a new installer (different Electron runtime). */
  45. | { state: 'manual'; version: string }
  46. | { state: 'error'; message: string };
  47. export interface UpdatesApi {
  48. /** Version of the running game. */
  49. version(): Promise<string>;
  50. /** `onStatus` receives the current status at once, then every change. Returns an unsubscribe function. */
  51. watch(onStatus: (status: UpdateStatus) => void): () => void;
  52. /** Restarts the app on the downloaded version. */
  53. restart(): void;
  54. }
  55. declare global {
  56. interface Window {
  57. lan: LanApi;
  58. assets: AssetsApi;
  59. updates: UpdatesApi;
  60. }
  61. }