diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2024-03-07 22:26:16 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2024-03-07 22:26:16 +0100 |
commit | f61604a8946ea0f6f003f43c3628bd84381d057f (patch) | |
tree | cdfe4ab5956e6575c84bcc8cc1fe2267ccb83f91 /tictactoe-2/apps/ttt_server/src/game.erl | |
parent | 911547d0e4e3838fa71cc3f71e5f472114e01431 (diff) | |
download | erlang-workshop-main.tar.gz erlang-workshop-main.tar.bz2 erlang-workshop-main.tar.xz erlang-workshop-main.zip |
Diffstat (limited to 'tictactoe-2/apps/ttt_server/src/game.erl')
-rw-r--r-- | tictactoe-2/apps/ttt_server/src/game.erl | 26 |
1 files changed, 20 insertions, 6 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); |