Analyzing the Detective game (homework in LIST)

For the homework with JSF technology, we are going to build an application that will allow pairs of users to play the notoriously known Black stories game for two players (see also detektivky, Ĩierne historky).

We will develop a web application which will connect pairs of players as they are connecting one after another to the application.
The first one will be the author of the story, and the second will be a detective. The author makes up a story in his mind, and let the detective ask questions. Author only answers "yes", "no", "no comment", or "that was it" - when the point of the story was discovered and the riddle solved.
To make the affairs as simple as possible, we will only create a single index.xhtml page, which will connect to its CDI session bean - either to post a question, or to submit an answer. Calling a method (answer(String s), postQuestion()) can be done by associating a method with action attribute of a command button:
         <h:commandButton action="#{sessionDetective.answer('yes')}" value="yes" /> <br/>
         <h:commandButton action="#{sessionDetective.answer('no')}" value="no" />
         <h:commandButton action="#{sessionDetective.postQuestion}" value="ask" />
In addition, a background asynchronous ajax request will be pending at the server and as soon as we will get a question or an answer from the other party, the pending request will return back to the client browser, and show it to the user. So we will use the same trick with the resend script and hidden button as in the previous exercise - the asynchronous JSF chat with ajax.

One CDI application-scoped bean will help the session bean determine whether it has the author or detective role. Thus a boolean property "isAuthor" in the session bean can be used inside of the xhtml JSF page to show the part that is relevant, with the help of <ui:fragment> element:
        <ui:fragment rendered="#{sessionDetective.isAutor}">
                ... elements shown for the author ...
        <ui:fragment rendered="#{!sessionDetective.isAutor}">
                ... elements shown for the detective ...
When the session bean asks the application bean for its role, the application bean may use @Inject of that session bean so that the application bean can give to session beans of both parties the references to each other, after both will have arrived. The application bean would thus remember the reference to the session bean of the author that arrived until the detective will have also arrived. And then the session beans could contact each other during the game in order to exchange the question/answer information to be sent to their clients directly by calling their public setters and notify the pending requests without involving the application bean in that exchange.

Another (more proper) solution would be to give each active game an object. This object representing a particular game would be inserted in some Map in the appliction bean. The session beans would always ask the application bean for contacting their peer. The reason why this is a more proper solution is that the session beans are passivation capable and thus their references to each other may become outdated if one of the instances is serialized and later deserialized. The @Inject annotation will ensure the valid references are always in use even after the serialization/deserialization event.

Good luck! If you run into any difficulties or have questions, do not hessitate to ask!