Skip to content
This repository has been archived by the owner on Feb 18, 2018. It is now read-only.

Commit

Permalink
Cleaning up the code a bit more
Browse files Browse the repository at this point in the history
  • Loading branch information
mads-hartmann committed Aug 23, 2011
1 parent 4de6ede commit 06c705e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 66 deletions.
59 changes: 59 additions & 0 deletions src/game.opa
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ type Game.status = {

Game = {{

/*
Data related
*/

user_state = UserContext.make({none}: option(Game.status))

get_state() = UserContext.execute((a -> a), user_state)
Expand Down Expand Up @@ -82,4 +86,59 @@ Game = {{
{ success = game }
)
)

/*
View related
*/

// Message received about the state of the game.
@client message_recieved(msg: message) =
match msg with
| { joining = user } -> Dom.hide(#waiting)
| { state = board } ->
do Dom.transform([#color_of_current_player <- colorc_to_string(board.current_color)])
do if Option.get(Game.get_state()).color == board.current_color then
Dom.hide(#waiting)
else
do Dom.select_raw("#waiting h1") |> Dom.set_text(_, "Waiting for " ^ colorc_to_string(board.current_color))
Dom.show(#waiting)
Board.update(board)

// invoked when the game_view is ready to initialize the dom with the appropriate data.
@client when_ready(name,color): void = (
channel = Option.get(Game.get_state()).channel
do Dom.set_text(#color_of_player,colorc_to_string(color))
do Dom.set_text(#name_of_game, name)
do Dom.set_text(#color_of_current_player, colorc_to_string({white}))
do Network.observe(message_recieved, channel)
do Option.iter( state -> (
if state.color == {white} then
Dom.select_raw("#waiting h1") |> Dom.set_text(_, "Waiting for black player")
else
Dom.select_raw("#waiting h1") |> Dom.set_text(_, "Waiting for white player")
),Game.get_state())
Board.prepare(Board.create())
)

game_view(name: string) = ( User.withUser( user ->
match Game.get(name) with
| { some = game } -> (

xml = color ->
<div onready={_ -> Network.add_callback(game_finished_recieved, game_observer)}>
<div onready={_ -> when_ready(name,color) } class="game">
{Chat.create_with_channel(user.name, NetworkWrapperChat.memo(game.name ^ "_chat"))}
{Template.parse(Template.default, @static_content("resources/board.xmlt")()) |> Template.to_xhtml(Template.default, _)}
</div>
</div>

if (Option.get(game.white) == user) then
Resource.styled_page("Chess", Page.style, xml({white}))
else
Resource.styled_page("Chess", Page.style, xml({black}))
)
| {none} -> Page.fourOfour()
,User.login_view()) // 404 shouldn't happen


}}
70 changes: 4 additions & 66 deletions src/main.opa
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,7 @@

package chess


/* Message received about the state of the game. */
@client message_recieved(msg: message) =
match msg with
| { joining = user } -> Dom.hide(#waiting)
| { state = board } ->
do Dom.transform([#color_of_current_player <- colorc_to_string(board.current_color)])
do if Option.get(Game.get_state()).color == board.current_color then
Dom.hide(#waiting)
else
do Dom.select_raw("#waiting h1") |> Dom.set_text(_, "Waiting for " ^ colorc_to_string(board.current_color))
Dom.show(#waiting)
Board.update(board)


@client when_ready(name,color): void = (
channel = Option.get(Game.get_state()).channel
do Dom.set_text(#color_of_player,colorc_to_string(color))
do Dom.set_text(#name_of_game, name)
do Dom.set_text(#color_of_current_player, colorc_to_string({white}))
do Network.observe(message_recieved, channel)
do Option.iter( state -> (
if state.color == {white} then
Dom.select_raw("#waiting h1") |> Dom.set_text(_, "Waiting for black player")
else
Dom.select_raw("#waiting h1") |> Dom.set_text(_, "Waiting for white player")
),Game.get_state())

Board.prepare(Board.create())
)

boardgame(name: string) = (
// this page will only get rendered if the user is logged in so it's safe to 'get'.
match User.get_status() with
| {user = user} -> (
match Game.get(name) with
| { some = game } -> (

xml = color ->
<div onready={_ -> Network.add_callback(game_finished_recieved, game_observer)}>
<div onready={_ -> when_ready(name,color) } class="game">
{Chat.create_with_channel(user.name, NetworkWrapperChat.memo(game.name ^ "_chat"))}
{Template.parse(Template.default, @static_content("resources/board.xmlt")()) |> Template.to_xhtml(Template.default, _)}
</div>
</div>

if (Option.get(game.white) == user) then
Resource.styled_page("Chess", Page.style, xml({white}))
else
Resource.styled_page("Chess", Page.style, xml({black}))
)
| {none} -> Page.fourOfour()
)
| {unlogged} -> Page.fourOfour() //Shouldn't be able to happen here as we checked in the routing.
)

/*
{Routing logic}
*/
/* {Routing logic} */

login_required( page: -> resource ) =
if User.is_logged_in() then page() else User.login_view()
Expand All @@ -76,17 +18,13 @@ start(uri) =
| { path = [] ... } -> login_required( -> Page.main() )
| { path = ["login"] ... } -> User.login_view()
| { path = ["signup"] ...} -> User.signup_view()
| { path = ["game",x|xs] ...} -> login_required( -> boardgame(x) )
| { path = ["game",x|xs] ...} -> login_required( -> Game.game_view(x) )
| { path = ["user", x|xs] ...} -> User.withUserNamed(x, User.page_view(_), Page.fourOfour)
| { path = x ...} -> Page.fourOfour()


/**
* Statically embed a bundle of resources
*/
/* Statically embed a bundle of resources */
server = Server.of_bundle([@static_include_directory("resources")])

/**
* Launch the [start] dispatcher
*/
/* Launch the [start] dispatcher */
server = Server.simple_dispatch(start)

0 comments on commit 06c705e

Please sign in to comment.