-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
emit mqtt events + refactor the api slightly
- Loading branch information
1 parent
e11a13f
commit 9f913ac
Showing
9 changed files
with
204 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,25 @@ | ||
import type { GameStateMachine } from '@paima/sm'; | ||
import PaimaSM from '@paima/sm'; | ||
import type { PreCompilesImport } from './utils/import.js'; | ||
import { importGameStateTransitionRouter, importEvents } from './utils/import.js'; | ||
import type { AppEventsImport, PreCompilesImport } from './utils/import.js'; | ||
import { importGameStateTransitionRouter } from './utils/import.js'; | ||
import { poolConfig } from './utils/index.js'; | ||
import { ENV } from '@paima/utils'; | ||
|
||
export const gameSM = (precompiles: PreCompilesImport): GameStateMachine => { | ||
export const gameSM = ( | ||
precompiles: PreCompilesImport, | ||
gameEvents: AppEventsImport | ||
): GameStateMachine => { | ||
const gameStateTransitionRouter = importGameStateTransitionRouter(); | ||
const events = importEvents(); | ||
|
||
return PaimaSM.initialize( | ||
poolConfig, | ||
4, // https://xkcd.com/221/ | ||
// there is no way of statically generating the event type here, since it's | ||
// imported at runtime. | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
gameStateTransitionRouter, | ||
ENV.START_BLOCKHEIGHT, | ||
precompiles, | ||
events.GameEvents | ||
gameEvents.events | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { LogEvent, LogEventFields } from './types.js'; | ||
import { toPath, TopicPrefix } from './types.js'; | ||
import type { TSchema } from '@sinclair/typebox'; | ||
import { keccak_256 } from 'js-sha3'; | ||
|
||
export function toSignature<T extends LogEvent<LogEventFields<TSchema>[]>>(event: T): string { | ||
return event.name + '(' + event.fields.map(f => f.type.type).join(',') + ')'; | ||
} | ||
|
||
export function toTopicHash<T extends LogEvent<LogEventFields<TSchema>[]>>(event: T): string { | ||
return keccak_256(toSignature(event)); | ||
} | ||
|
||
export const generateAppEvents = < | ||
const T extends ReadonlyArray<LogEvent<LogEventFields<TSchema>[]>>, | ||
>( | ||
entries: T | ||
): { | ||
[K in T[number] as K['name']]: ReturnType<typeof toPath<K, typeof TopicPrefix.App>> & | ||
{ definition: T[0]; topicHash: string }[]; | ||
} => { | ||
let result: Record<string, any> = {}; // we can't know the type here | ||
for (const event of entries) { | ||
if (!result[event.name]) { | ||
result[event.name] = []; | ||
} | ||
|
||
result[event.name].push({ | ||
...toPath(TopicPrefix.App, event), | ||
// keep the original definition around since it's nicer to work with, it | ||
// also has the advantage that it allows recovering the initial order in | ||
// case the signature/topicHash needs to be computed again, which can't be | ||
// done from the path (since you don't know which non indexed fields go in | ||
// between each indexed field). | ||
definition: event, | ||
// we add this to avoid having to re-compute it all the time. | ||
topicHash: toTopicHash(event), | ||
}); | ||
} | ||
return result as any; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.