Current section

Files

Jump to
gchess src game_server.erl
Raw

src/game_server.erl

-module(game_server).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([print_board/1, apply_move/2, apply_move_uci_string/2, apply_move_san_string/2, apply_move_raw/2, undo_move/1, all_legal_moves/1, get_fen/1, get_status/1, disable_status/1, new_game/1, new_game_from_fen/2, new_server/0, load_pgn/1]).
-export_type([message/0]).
-type message() :: {all_legal_moves,
gleam@erlang@process:subject(list(move:move()))} |
{apply_move, gleam@erlang@process:subject(game:game()), move:move()} |
{apply_move_uci_string, gleam@erlang@process:subject(game:game()), binary()} |
{apply_move_san_string, gleam@erlang@process:subject(game:game()), binary()} |
{apply_move_raw, gleam@erlang@process:subject(game:game()), move:move()} |
{undo_move, gleam@erlang@process:subject(game:game())} |
{get_state, gleam@erlang@process:subject(game:game())} |
{get_side_to_move, gleam@erlang@process:subject(color:color())} |
{get_fen, gleam@erlang@process:subject(binary())} |
{get_status,
gleam@erlang@process:subject(gleam@option:option(status:status()))} |
{new_game, gleam@erlang@process:subject(game:game())} |
{new_game_from_fen, gleam@erlang@process:subject(game:game()), binary()} |
{disable_status, gleam@erlang@process:subject(game:game())} |
shutdown |
{print_board, gleam@erlang@process:subject(nil)}.
-spec print_board(gleam@erlang@process:subject(message())) -> nil.
print_board(Game_actor) ->
gleam@erlang@process:call(
Game_actor,
fun(Field@0) -> {print_board, Field@0} end,
1000
).
-spec apply_move(gleam@erlang@process:subject(message()), move:move()) -> game:game().
apply_move(Game_actor, Move) ->
gleam@erlang@process:call(
Game_actor,
fun(_capture) -> {apply_move, _capture, Move} end,
1000
).
-spec apply_move_uci_string(gleam@erlang@process:subject(message()), binary()) -> game:game().
apply_move_uci_string(Game_actor, Move_uci) ->
gleam@erlang@process:call(
Game_actor,
fun(_capture) -> {apply_move_uci_string, _capture, Move_uci} end,
1000
).
-spec apply_move_san_string(gleam@erlang@process:subject(message()), binary()) -> game:game().
apply_move_san_string(Game_actor, Move_san) ->
gleam@erlang@process:call(
Game_actor,
fun(_capture) -> {apply_move_san_string, _capture, Move_san} end,
1000
).
-spec apply_move_raw(gleam@erlang@process:subject(message()), move:move()) -> game:game().
apply_move_raw(Game_actor, Move) ->
gleam@erlang@process:call(
Game_actor,
fun(_capture) -> {apply_move_raw, _capture, Move} end,
1000
).
-spec undo_move(gleam@erlang@process:subject(message())) -> game:game().
undo_move(Game_actor) ->
gleam@erlang@process:call(
Game_actor,
fun(Field@0) -> {undo_move, Field@0} end,
1000
).
-spec all_legal_moves(gleam@erlang@process:subject(message())) -> list(move:move()).
all_legal_moves(Game_actor) ->
gleam@erlang@process:call(
Game_actor,
fun(Field@0) -> {all_legal_moves, Field@0} end,
1000
).
-spec get_fen(gleam@erlang@process:subject(message())) -> binary().
get_fen(Game_actor) ->
gleam@erlang@process:call(
Game_actor,
fun(Field@0) -> {get_fen, Field@0} end,
1000
).
-spec get_status(gleam@erlang@process:subject(message())) -> gleam@option:option(status:status()).
get_status(Game_actor) ->
gleam@erlang@process:call(
Game_actor,
fun(Field@0) -> {get_status, Field@0} end,
1000
).
-spec disable_status(gleam@erlang@process:subject(message())) -> game:game().
disable_status(Game_actor) ->
gleam@erlang@process:call(
Game_actor,
fun(Field@0) -> {disable_status, Field@0} end,
1000
).
-spec new_game(gleam@erlang@process:subject(message())) -> game:game().
new_game(Game_actor) ->
gleam@erlang@process:call(
Game_actor,
fun(Field@0) -> {new_game, Field@0} end,
1000
).
-spec new_game_from_fen(gleam@erlang@process:subject(message()), binary()) -> game:game().
new_game_from_fen(Game_actor, Fen) ->
gleam@erlang@process:call(
Game_actor,
fun(_capture) -> {new_game_from_fen, _capture, Fen} end,
1000
).
-spec handle_all_legal_moves(
game:game(),
gleam@erlang@process:subject(list(move:move()))
) -> gleam@otp@actor:next(message(), game:game()).
handle_all_legal_moves(Game, Client) ->
gleam@erlang@process:send(Client, game:all_legal_moves(Game)),
gleam@otp@actor:continue(Game).
-spec handle_undo_move(game:game(), gleam@erlang@process:subject(game:game())) -> gleam@otp@actor:next(any(), game:game()).
handle_undo_move(Game, Client) ->
New_game_state = game:undo_move(Game),
gleam@erlang@process:send(Client, New_game_state),
gleam@otp@actor:continue(New_game_state).
-spec handle_apply_move_san_string(
game:game(),
gleam@erlang@process:subject(game:game()),
binary()
) -> gleam@otp@actor:next(any(), game:game()).
handle_apply_move_san_string(Game, Client, Move) ->
_assert_subject = game:apply_move_san_string(Game, Move),
{ok, New_game_state} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"game_server"/utf8>>,
function => <<"handle_apply_move_san_string"/utf8>>,
line => 134})
end,
gleam@erlang@process:send(Client, New_game_state),
gleam@otp@actor:continue(New_game_state).
-spec handle_apply_move_uci(
game:game(),
gleam@erlang@process:subject(game:game()),
binary()
) -> gleam@otp@actor:next(any(), game:game()).
handle_apply_move_uci(Game, Client, Move) ->
New_game_state = game:apply_move_uci(Game, Move),
gleam@erlang@process:send(Client, New_game_state),
gleam@otp@actor:continue(New_game_state).
-spec handle_apply_move(
game:game(),
gleam@erlang@process:subject(game:game()),
move:move()
) -> gleam@otp@actor:next(any(), game:game()).
handle_apply_move(Game, Client, Move) ->
New_game_state = game:apply_move(Game, Move),
gleam@erlang@process:send(Client, New_game_state),
gleam@otp@actor:continue(New_game_state).
-spec handle_apply_move_raw(
game:game(),
gleam@erlang@process:subject(game:game()),
move:move()
) -> gleam@otp@actor:next(any(), game:game()).
handle_apply_move_raw(Game, Client, Move) ->
New_game_state = game:apply_move_raw(Game, Move),
gleam@erlang@process:send(Client, New_game_state),
gleam@otp@actor:continue(New_game_state).
-spec handle_get_fen(game:game(), gleam@erlang@process:subject(binary())) -> gleam@otp@actor:next(any(), game:game()).
handle_get_fen(Game, Client) ->
gleam@erlang@process:send(Client, game:to_fen(Game)),
gleam@otp@actor:continue(Game).
-spec handle_print_board(game:game(), gleam@erlang@process:subject(nil)) -> gleam@otp@actor:next(message(), game:game()).
handle_print_board(Game, Client) ->
game:print_board(Game),
gleam@erlang@process:send(Client, nil),
gleam@otp@actor:continue(Game).
-spec handle_message(message(), game:game()) -> gleam@otp@actor:next(message(), game:game()).
handle_message(Message, Game) ->
case Message of
{all_legal_moves, Client} ->
handle_all_legal_moves(Game, Client);
{apply_move, Client@1, Move} ->
handle_apply_move(Game, Client@1, Move);
{apply_move_uci_string, Client@2, Move@1} ->
handle_apply_move_uci(Game, Client@2, Move@1);
{apply_move_san_string, Client@3, Move@2} ->
handle_apply_move_san_string(Game, Client@3, Move@2);
{apply_move_raw, Client@4, Move@3} ->
handle_apply_move_raw(Game, Client@4, Move@3);
{undo_move, Client@5} ->
handle_undo_move(Game, Client@5);
{get_state, Client@6} ->
gleam@erlang@process:send(Client@6, Game),
gleam@otp@actor:continue(Game);
{get_fen, Client@7} ->
handle_get_fen(Game, Client@7);
{get_status, Client@8} ->
gleam@erlang@process:send(Client@8, erlang:element(5, Game)),
gleam@otp@actor:continue(Game);
{get_side_to_move, Client@9} ->
gleam@erlang@process:send(Client@9, erlang:element(3, Game)),
gleam@otp@actor:continue(Game);
{new_game, Client@10} ->
New_game = game:new_game(),
gleam@erlang@process:send(Client@10, New_game),
gleam@otp@actor:continue(New_game);
{new_game_from_fen, Client@11, Fen} ->
New_game@1 = game:from_fen_string(Fen),
gleam@erlang@process:send(Client@11, New_game@1),
gleam@otp@actor:continue(New_game@1);
{disable_status, Client@12} ->
New_game@2 = game:disable_status(Game),
gleam@erlang@process:send(Client@12, New_game@2),
gleam@otp@actor:continue(New_game@2);
shutdown ->
{stop, normal};
{print_board, Client@13} ->
handle_print_board(Game, Client@13)
end.
-spec new_server() -> gleam@erlang@process:subject(message()).
new_server() ->
_assert_subject = gleam@otp@actor:start(
game:new_game(),
fun handle_message/2
),
{ok, Actor} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"game_server"/utf8>>,
function => <<"new_server"/utf8>>,
line => 173})
end,
Actor.
-spec load_pgn(binary()) -> {ok, gleam@erlang@process:subject(message())} |
{error, binary()}.
load_pgn(Pgn_string) ->
case game:load_pgn(Pgn_string) of
{ok, Game} ->
_assert_subject = gleam@otp@actor:start(Game, fun handle_message/2),
{ok, Actor} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"game_server"/utf8>>,
function => <<"load_pgn"/utf8>>,
line => 180})
end,
{ok, Actor};
{error, Error} ->
{error, Error}
end.