Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Integrating It All


[ 316 ]

This particular game engine also has the concept of automated player bots built in.
The code in the event block can be any valid Groovy code. We can call the built-in
functions to get a list of players and any assignment to a built-in variable page is
assumed to represent a desired page transition in the game. The event block has an
implied event parameter, which will contain any parameters passed to it by the UI.
Before we look at the AST transformation that implements this DSL, let's first look at
a Groovy class pattern that implements the same logic. This pattern of classes is what
the AST transform will generate at compile time:


class GameEngineClient implements GameEngineTraits {
def GameEngineClient() {
}
def GameEngineClient(session) {
this.session = session
}
Map game_start(Object event = null, Object saved = null) {
restoreSession('TicTacToeEngine', saved)
session.page.game_start(event)
session.asMap()
}
Map select_players(Object event = null, Object saved = null) {
restoreSession('TicTacToeEngine', saved)
session.page.select_players(event)
session.asMap()
}
Map play_round(Object event = null, Object saved = null) {
restoreSession('TicTacToeEngine', saved)
session.page.play_round(event)
session.asMap()
}
Map game_end(Object event = null, Object saved = null) {
restoreSession('TicTacToeEngine', saved)
session.page.game_end(event)
session.asMap()
}
}

This is the class that the game server will interact with. It is quite similar to the state
machine client class we encountered in Chapter 8, AST Transformations, LEDToggle.
The session serves the same role in the pattern as the StateContext class. However,
we are going to be persisting the context between events so the term session better
fits our understanding of that mechanism.


http://www.ebook3000.com
Free download pdf