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/processquest/apps/processquest-1.0.0/src/pq_events.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/processquest/apps/processquest-1.0.0/src/pq_events.erl')
-rw-r--r-- | learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_events.erl | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_events.erl b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_events.erl new file mode 100644 index 0000000..324c432 --- /dev/null +++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_events.erl @@ -0,0 +1,49 @@ +%%% Wrapper module for the event manager of ProgressQuest players. +%%% It adds a few functions to wrap the events and sleep on the right +%%% scale on behalf of the pq_player process. +-module(pq_events). +-export([killed/3, location/3, lvl_up/5, buy/4, sell/3]). +-export([start_link/1, stop/1, add_handler/3, delete_handler/3, notify/2]). + +start_link(Name) -> + {ok, Pid} = gen_event:start_link(), + ok = regis:register({events, Name}, Pid), + {ok, Pid}. + +stop(Name) -> + ManagerPid = regis:whereis({events, Name}), + gen_event:stop(ManagerPid). + +add_handler(Name, Handler, Args) -> + ManagerPid = regis:whereis({events, Name}), + gen_event:add_handler(ManagerPid, Handler, Args). + +delete_handler(Name, Handler, Args) -> + ManagerPid = regis:whereis({events, Name}), + gen_event:delete_handler(ManagerPid, Handler, Args). + +notify(Name, Msg) -> + ManagerPid = regis:whereis({events, Name}), + gen_event:notify(ManagerPid, Msg). + +killed(Name, Enemy = {_EnemyName, _Props}, Time) -> + notify(Name, {Name, killed, Time*2, Enemy}), + timer:sleep(Time*2). + +location(Name, Place, Time) -> + notify(Name, {Name, heading, Time, Place}), + timer:sleep(Time). + +lvl_up(Name, NewStats, NewLvl, NewExp, _Time) -> + notify(Name, {Name, lvl_up, 0, NewStats, NewLvl, NewExp}), + ok. + +buy(Name, Slot, Item, Time) -> + T = round(Time/2), + notify(Name, {Name, buy, T, Slot, Item}), + timer:sleep(T). + +sell(Name, Item, Time) -> + T = round(Time/5), + notify(Name, {Name, sell, T, Item}), + timer:sleep(Time). |