You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
800 B

1 month ago
  1. -module(ai_supervisor).
  2. -behaviour(supervisor).
  3. -export([start_link/0, init/1]).
  4. -export([start_ai/1, stop_ai/1]).
  5. %% API
  6. start_link() ->
  7. supervisor:start_link({local, ?MODULE}, ?MODULE, []).
  8. %% 启动一个AI玩家
  9. start_ai(Difficulty) ->
  10. supervisor:start_child(?MODULE, [Difficulty]).
  11. %% 停止一个AI玩家
  12. stop_ai(AiPid) ->
  13. supervisor:terminate_child(?MODULE, AiPid).
  14. %% Callbacks
  15. init([]) ->
  16. SupFlags = #{strategy => simple_one_for_one,
  17. intensity => 10,
  18. period => 60},
  19. ChildSpecs = [
  20. #{id => ai_player,
  21. start => {ai_player, create_ai_player, []},
  22. restart => temporary,
  23. shutdown => 5000,
  24. type => worker,
  25. modules => [ai_player]}
  26. ],
  27. {ok, {SupFlags, ChildSpecs}}.