-module(ai_supervisor).
|
|
-behaviour(supervisor).
|
|
|
|
-export([start_link/0, init/1]).
|
|
-export([start_ai/1, stop_ai/1]).
|
|
|
|
%% API
|
|
start_link() ->
|
|
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
|
|
|
%% 启动一个AI玩家
|
|
start_ai(Difficulty) ->
|
|
supervisor:start_child(?MODULE, [Difficulty]).
|
|
|
|
%% 停止一个AI玩家
|
|
stop_ai(AiPid) ->
|
|
supervisor:terminate_child(?MODULE, AiPid).
|
|
|
|
%% Callbacks
|
|
init([]) ->
|
|
SupFlags = #{strategy => simple_one_for_one,
|
|
intensity => 10,
|
|
period => 60},
|
|
|
|
ChildSpecs = [
|
|
#{id => ai_player,
|
|
start => {ai_player, create_ai_player, []},
|
|
restart => temporary,
|
|
shutdown => 5000,
|
|
type => worker,
|
|
modules => [ai_player]}
|
|
],
|
|
|
|
{ok, {SupFlags, ChildSpecs}}.
|