3

I am implementing a game engine. Most of my code currently resides in a StateT Game IO () monad. I use IO to get user input, and in fact all IO channels through one function,

getChoice :: Show a => [ a ] -> IO a

which prompts the user with all presented options, reads in a response, and returns it. For a particular flow of user responses (and initial state), the game proceeds deterministically, and I'd like to factor out these user responses to help with testing.

When I try to convert this to use Pipes, what feels most similar is to change all my code which is currently in StateT Game IO (), and make it run in Proxy p, Monad m => Client p [String] String m (). This way I can still 'request' choices from the user, which allows me to write the engine code in a blocking style, assuming that user choices will be provided as I need them.

However, this seems wrong - surely the user should be the Client, and the game engine the Server? What principles can I use to make this distinction correctly?

2 Answers 2

1

Like nushio said, both Client and Server are completely symmetric each other. The only thing that introduces asymmetry is the composition operator. (>->) begins at the Client end and (>~>) begins at the Server end.

Since you are using (>->), that means that the Client component has "the initiative" and gets things rolling. This means that the Server will not do anything unless the Client makes a request. Other than the fact that the Client begins first, there is no difference between the Client and Server.

The type of (>->) when you specialize it to a Client and Server reflects this asymmetry in initiative:

(>->) => ([String] -> Server [String] String IO r)
      -> (()       -> Client [String] String IO r)
      -> (()       -> Session                IO r)

The Server (i.e. your user) only reacts to the Client (i.e. the game engine), which is why the server must accept an argument of type [String] to get the ball rolling. This is reflected in the fact that if the game engine never prompts the user for a choice then the user "can never run" because the user has no set of choices from which to select. From the way you specified the problem the game engine is in control, not the user. If you were to specify it the other way around and have the user drive the game engine then the roles would be reversed.

1

In pipes, Servers and Clients are almost interchangeable, as are request and respond , upstream and downstream. One difference is that it is Clients job to start a Session by issuing the first request.

http://hackage.haskell.org/packages/archive/pipes/2.5.0/doc/html/Control-Proxy-Tutorial.html

I'm quite sure you can formulate the game player both as Client and Server: it's all about the senses. Why not consider him/her as a keyboard input server?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.