PaimaStudios / paima-engine

Novel trustless web3/blockchain gaming engine.
MIT License
55 stars 20 forks source link

Proposed changed to event export interface #425

Closed SebastienGllmt closed 2 months ago

SebastienGllmt commented 2 months ago

The new code for the events package is as follows

import type { EventQueue } from '@paima/events';
import { registerEvents } from '@paima/events';
import { genEvent } from '@paima/events';
import { Type } from '@sinclair/typebox';

export const ClickEvent = genEvent({
  name: 'MyClickEvent',
  fields: [
    {
      indexed: true,
      name: 'user',
      type: Type.String(),
    },
    {
      name: 'time',
      type: Type.Number(),
    },
  ],
});

export const eventDefinitions = {
  ClickEvent,
} as const;

export const events = registerEvents(eventDefinitions);
export type Events = EventQueue<typeof eventDefinitions>;

You can then use it inside a subscribe call like this

import { events } from '@game/events';

await PaimaEventManager.Instance.subscribe(
  {
    topic: events.ClickEvent,
    filter: { user: undefined }, // all users
  },
  event => {
    console.log(`EVENT: ${event.time}`);
    callback(`Click time: ${event.time}`);
  }
);