diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2024-02-23 07:08:18 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2024-02-23 07:08:18 +0100 |
commit | 5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e (patch) | |
tree | 982ca2e7f9ac4e8c350dfb5c4f60bcfdfff5afaf /learn-you-some-erlang/curling.erl | |
parent | 05ae56e5e89abf2993f84e6d52b250131f247c35 (diff) | |
download | erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.gz erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.bz2 erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.xz erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.zip |
wip
Diffstat (limited to 'learn-you-some-erlang/curling.erl')
-rw-r--r-- | learn-you-some-erlang/curling.erl | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/learn-you-some-erlang/curling.erl b/learn-you-some-erlang/curling.erl new file mode 100644 index 0000000..2c50074 --- /dev/null +++ b/learn-you-some-erlang/curling.erl @@ -0,0 +1,37 @@ +-module(curling). +-export([start_link/2, set_teams/3, add_points/3, next_round/1]). +-export([join_feed/2, leave_feed/2]). +-export([game_info/1]). + +start_link(TeamA, TeamB) -> + {ok, Pid} = gen_event:start_link(), + %% The scoreboard will always be there + gen_event:add_handler(Pid, curling_scoreboard, []), + %% Start the stats accumulator + gen_event:add_handler(Pid, curling_accumulator, []), + set_teams(Pid, TeamA, TeamB), + {ok, Pid}. + +set_teams(Pid, TeamA, TeamB) -> + gen_event:notify(Pid, {set_teams, TeamA, TeamB}). + +add_points(Pid, Team, N) -> + gen_event:notify(Pid, {add_points, Team, N}). + +next_round(Pid) -> + gen_event:notify(Pid, next_round). + +%% Subscribes the pid ToPid to the event feed. +%% The specific event handler for the newsfeed is +%% returned in case someone wants to leave +join_feed(Pid, ToPid) -> + HandlerId = {curling_feed, make_ref()}, + gen_event:add_sup_handler(Pid, HandlerId, [ToPid]), + HandlerId. + +leave_feed(Pid, HandlerId) -> + gen_event:delete_handler(Pid, HandlerId, leave_feed). + +%% Returns the current game state. +game_info(Pid) -> + gen_event:call(Pid, curling_accumulator, game_data). |