aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tictactoe-2/apps/ttt_server/src/game.erl26
-rw-r--r--tictactoe-2/apps/ttt_server/src/ttt_server.erl16
2 files changed, 25 insertions, 17 deletions
diff --git a/tictactoe-2/apps/ttt_server/src/game.erl b/tictactoe-2/apps/ttt_server/src/game.erl
index 608b5a8..36d3348 100644
--- a/tictactoe-2/apps/ttt_server/src/game.erl
+++ b/tictactoe-2/apps/ttt_server/src/game.erl
@@ -13,9 +13,12 @@
player1,
player2}).
-game_loop(Id, Player1) -> loop(#state{
- id = Id,
- player1 = Player1}).
+game_loop(Id, Player1) ->
+ State = #state{
+ id = Id,
+ player1 = Player1},
+ io:format("new game state: ~p~n", [State]),
+ loop(State).
loop(State) ->
Id = State#state.id,
@@ -23,16 +26,27 @@ loop(State) ->
{From, join} when not is_pid(State#state.player2) ->
io:format("game ~p: player joined: ~p~n", [Id, From]),
From ! {joined, State#state.id},
- loop(#state{player2 = From});
+ loop(State#state{player2 = From});
{From, join} ->
io:format("game ~p: busy: ~p~n", [Id, From]),
From ! {busy},
loop(State);
- {From, show} ->
+ {_From, show} ->
Str = ttt:format(State#state.board),
io:format("game ~p: current state:~n~s~n", [Id, Str]),
- From ! Str,
loop(State);
+ {From, status} ->
+ From ! {status, [
+ {id, State#state.id},
+ {board, State#state.board},
+ {player1, State#state.player1},
+ {player2, State#state.player2}]},
+ loop(State);
+ {_From, stop} ->
+ io:format("game ~p: Stopping~n", [Id]),
+ State#state.player1 ! {game_stopped, Id},
+ State#state.player2 ! {game_stopped, Id},
+ ok;
code_changed ->
io:format("game ~p: code changed~n", [Id]),
?MODULE:loop(State);
diff --git a/tictactoe-2/apps/ttt_server/src/ttt_server.erl b/tictactoe-2/apps/ttt_server/src/ttt_server.erl
index 52a7957..ce59968 100644
--- a/tictactoe-2/apps/ttt_server/src/ttt_server.erl
+++ b/tictactoe-2/apps/ttt_server/src/ttt_server.erl
@@ -36,21 +36,15 @@ loop(State) ->
loop(#ttt_state{
seq = Id + 1,
games = orddict:append(Id, Game, State#ttt_state.games)});
- {Player, show, Id} ->
+ {Player, Cmd, Id} when (Cmd == show) or
+ (Cmd == join) or
+ (Cmd == status) or
+ (Cmd == stop) ->
case orddict:find(Id, State#ttt_state.games) of
error ->
Player ! no_such_game;
{ok, [Game]} ->
- io:format("show: ~p~n", [Game]),
- Game#ttt_game.pid ! {Player, show}
- end,
- loop(State);
- {Player, join, Id} ->
- case orddict:find(Id, State#ttt_state.games) of
- error ->
- Player ! no_such_game;
- {ok, [Game]} ->
- Game#ttt_game.pid ! {Player, join}
+ Game#ttt_game.pid ! {Player, Cmd}
end,
loop(State);
dump ->