PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/servers/src/judgment_mapserver.erl

http://erl-chess.googlecode.com/
Erlang | 121 lines | 43 code | 17 blank | 61 comment | 1 complexity | 41ca15ba08d24b426b432e9100b7fc9d MD5 | raw file
  1. %%%-------------------------------------------------------------------
  2. %%% File : gen_server_template.full
  3. %%% Author : my name <yourname@localhost.localdomain>
  4. %%% Description :
  5. %%%
  6. %%% Created : 2 Mar 2007 by my name <yourname@localhost.localdomain>
  7. %%%-------------------------------------------------------------------
  8. -module(judgment_mapserver).
  9. -behaviour(gen_server).
  10. -define(MAP, {
  11. -1, -2, -4, -5, -6, -5, -4, -2, -1,
  12. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  13. 0, -3, 0, 0, 0, 0, 0, -3, 0,
  14. -7, 0, -7, 0, -7, 0, -7, 0, -7,
  15. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  16. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  17. 7, 0, 7, 0, 7, 0, 7, 0, 7,
  18. 0, 3, 0, 0, 0, 0, 0, 3, 0,
  19. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  20. 1, 2, 4, 5, 6, 5, 4, 2, 1
  21. }).
  22. %% API
  23. -export([start_link/1]).
  24. %% gen_server callbacks
  25. -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
  26. terminate/2, code_change/3]).
  27. %%====================================================================
  28. %% API
  29. %%====================================================================
  30. %%--------------------------------------------------------------------
  31. %% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
  32. %% Description: Starts the server
  33. %%--------------------------------------------------------------------
  34. start_link(Name) ->
  35. gen_server:start_link({local, Name}, ?MODULE, [?MAP], []).
  36. %%====================================================================
  37. %% gen_server callbacks
  38. %%====================================================================
  39. %%--------------------------------------------------------------------
  40. %% Function: init(Args) -> {ok, State} |
  41. %% {ok, State, Timeout} |
  42. %% ignore |
  43. %% {stop, Reason}
  44. %% Description: Initiates the server
  45. %%--------------------------------------------------------------------
  46. init([Map]) ->
  47. {ok, Map}.
  48. %%--------------------------------------------------------------------
  49. %% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
  50. %% {reply, Reply, State, Timeout} |
  51. %% {noreply, State} |
  52. %% {noreply, State, Timeout} |
  53. %% {stop, Reason, Reply, State} |
  54. %% {stop, Reason, State}
  55. %% Description: Handling call messages
  56. %%--------------------------------------------------------------------
  57. handle_call({get_map}, _From, Map) ->
  58. {reply, Map, Map};
  59. handle_call({try_move, {{XFrom, YFrom}, {XTo, YTo}}}, _From, Map) ->
  60. L = rules:get_list(Map, {XFrom, YFrom}),
  61. case [P || P <- L, P =:= {{XFrom, YFrom}, {XTo, YTo}}] of
  62. [] -> {reply, false, Map};
  63. [_] -> {reply, true, chessmap:apply(Map, {{XFrom, YFrom}, {XTo, YTo}})}
  64. end;
  65. handle_call(stop, _From, Map) ->
  66. {stop, normal, stopped, Map};
  67. handle_call(Request, _From, State) ->
  68. io:format("~nbad args : ~p~n", [Request]),
  69. Reply = ok,
  70. {reply, Reply, State}.
  71. %%--------------------------------------------------------------------
  72. %% Function: handle_cast(Msg, State) -> {noreply, State} |
  73. %% {noreply, State, Timeout} |
  74. %% {stop, Reason, State}
  75. %% Description: Handling cast messages
  76. %%--------------------------------------------------------------------
  77. handle_cast(_Msg, State) ->
  78. {noreply, State}.
  79. %%--------------------------------------------------------------------
  80. %% Function: handle_info(Info, State) -> {noreply, State} |
  81. %% {noreply, State, Timeout} |
  82. %% {stop, Reason, State}
  83. %% Description: Handling all non call/cast messages
  84. %%--------------------------------------------------------------------
  85. handle_info(_Info, State) ->
  86. {noreply, State}.
  87. %%--------------------------------------------------------------------
  88. %% Function: terminate(Reason, State) -> void()
  89. %% Description: This function is called by a gen_server when it is about to
  90. %% terminate. It should be the opposite of Module:init/1 and do any necessary
  91. %% cleaning up. When it returns, the gen_server terminates with Reason.
  92. %% The return value is ignored.
  93. %%--------------------------------------------------------------------
  94. terminate(_Reason, _State) ->
  95. ok.
  96. %%--------------------------------------------------------------------
  97. %% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
  98. %% Description: Convert process state when code is changed
  99. %%--------------------------------------------------------------------
  100. code_change(_OldVsn, State, _Extra) ->
  101. {ok, State}.
  102. %%--------------------------------------------------------------------
  103. %%% Internal functions
  104. %%--------------------------------------------------------------------