johnameyer / cards-ts

A card game framework and various games in Typescript. The framework allows for bot development and playing card games in the terminal or in a browser.
MIT License
7 stars 2 forks source link

Reduce event handling boilerplate and centralize logic in one place #124

Open johnameyer opened 4 months ago

johnameyer commented 4 months ago

Today, users have to copy a number of different files in order to build their games. It would be ideal to define the game as nearly one nested function call using the tree built by #113 (i.e. we are already building the definition of the various states into the tree by this change). Going further, we can compose more of the data into the tree.

When we call handleSingle and company, we can also pass the event validators and merging logic. Upon an incoming event, it would then call that specific handler for that state.

handleSingle({
  handler: 'dealerDiscard',
  position: controllers => controllers.deck.dealer,
  process: { // new
    MessageType: {
      validate: (controllers, event) => {},
      merge: (controllers, event) => {},
    }
  }
})

There is the question of whether a handler type / processing will be duplicated multiple places.

This should also help verify that only certain events are valid at certain times.