views:

29

answers:

1

Hi all,

Over the past few weeks, I have been creating a sort of text-based adventure game revolving entirely around the actions of a player. The general idea is that there is a Simulation class that maintains the state of the world, and is the responsibility of the SimulationController (ie, the player) to keep actions going. Most of the time, it will be said Controller that is telling the Simulation what to do (ie, simulate 1 timestep forward), but occasionally the Simulation needs to ask something of the controller. Thus, I have created an interface like so:

/**
 * An interface to a GUI, command line, etc;
 * a way to interact with the Simulation class
 * @author dduckworth
 *
 */
public interface SimulationController {

    /**
     * Returns the index of a choice from a list
     * 
     * @param message: prompt for the player
     * @param choices: options, in order
     * @return: the index of the choice chosen
     */
    public int chooseItem(String message, List<String> choices);

    /**
     * Returns some text the player must type in manually.
     * 
     * @param message
     * @return
     */
    public String enterChoice(String message);

    /**
     * Give the user a message.  This could be notification
     * of a failed action, some response to some random event,
     * anything.
     * 
     * @param message
     */
    public void giveMessage(String message);

    /**
     * The simulation this controller is controlling
     * @return
     */
    public Simulation getSimulation();

    /**
     * The primary loop for this controller.  General flow
     * should be something like this:
     * 1)   Prompt the player to choose a tool and target
     *      from getAvailableTools() and getAvailableTargets()
     * 2)   Prompt the player to choose an action from
     *      getAvailableActions()
     * 3)   call Simuluation.simulate() with the tool, target,
     *      action chosen, the time taken to make that decision,
     *      and this
     * 4)   while Simulation.isFinished() == false, continue onward
     */
    public void run();
}

The main control loop in all this must be implemented in SimulationController.run(), but the simulation can also call the other methods to request some information from the player.

I am currently using Adobe Flex with BlazeDS to create a very simple user interface that will communicate with the Simulation by implementing or holding something that implements the SimulationController interface. There is the concept of 'long polling', but I do not admit to knowing quite how to use it with remote objects such as this.

My question is, what is a good design pattern to push information to the player such that all Simulation requests go straight to the Flash client and that all control loop logic can stay on the Java side?

Thanks!

A: 

Read on wiki about Push technology to understand the main concepts. After that read the messaging part from BlazeDS developer guide.

I assume that you have some experience with BlazeDS.. (at least you installed it in some application server). Take a look in the samples folder and you will find two interesting examples (a chat application, and another one called datapush). They are quite simple to understood.

Cornel Creanga
Hi Cornel,That helps, but it even with Push there's no simple way I see to ASK the player for a response and know when he's replying. For example, I may have a separate thread running, which pushes the question, "Where's the milk?" The client could answer by pushing an answer down another channel or calling a method of the original Remote Object, but none of that is seamless or intuitive.
duckworthd
Not sure if I agree but I'm working for some time with messaging and maybe I see the things in different light...for example in your case you just have to create a message with the question "where is the milk" to all the subscribers interested about your question. And at least theoretically the messaging framework should hide the various quirks done in order to simulate communication without a duplex socket.
Cornel Creanga