aboutsummaryrefslogtreecommitdiff
path: root/learn-you-some-erlang/processquest/apps/processquest-1.0.0
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2024-02-23 07:08:18 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2024-02-23 07:08:18 +0100
commit5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e (patch)
tree982ca2e7f9ac4e8c350dfb5c4f60bcfdfff5afaf /learn-you-some-erlang/processquest/apps/processquest-1.0.0
parent05ae56e5e89abf2993f84e6d52b250131f247c35 (diff)
downloaderlang-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')
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/Emakefile2
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/ebin/.this-file-intentionally-left-blank0
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/ebin/processquest.app7
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/include/.this-file-intentionally-left-blank0
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/priv/.this-file-intentionally-left-blank0
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_enemy.erl17
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_events.erl49
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_market.erl77
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_player.erl175
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_stats.erl19
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_sup.erl31
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_supersup.erl28
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/processquest.erl39
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_enemy_tests.erl33
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_events_handler.erl24
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_events_tests.erl55
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_market_tests.erl15
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_player_tests.erl303
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_stats_tests.erl28
-rw-r--r--learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/processquest_tests.erl49
20 files changed, 951 insertions, 0 deletions
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/Emakefile b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/Emakefile
new file mode 100644
index 0000000..b203ea3
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/Emakefile
@@ -0,0 +1,2 @@
+{["src/*", "test/*"],
+ [{i,"include"}, {outdir, "ebin"}]}.
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/ebin/.this-file-intentionally-left-blank b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/ebin/.this-file-intentionally-left-blank
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/ebin/.this-file-intentionally-left-blank
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/ebin/processquest.app b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/ebin/processquest.app
new file mode 100644
index 0000000..abda4b2
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/ebin/processquest.app
@@ -0,0 +1,7 @@
+{application, processquest,
+ [{description, "Game inspired by the Progress Quest game (http://progressquest.com)"},
+ {vsn, "1.0.0"},
+ {mod, {processquest, []}},
+ {registered, [pq_supersup]},
+ {modules, [processquest, pq_stats, pq_enemy, pq_events, pq_player]},
+ {applications, [stdlib, kernel, regis, crypto]}]}.
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/include/.this-file-intentionally-left-blank b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/include/.this-file-intentionally-left-blank
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/include/.this-file-intentionally-left-blank
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/priv/.this-file-intentionally-left-blank b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/priv/.this-file-intentionally-left-blank
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/priv/.this-file-intentionally-left-blank
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_enemy.erl b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_enemy.erl
new file mode 100644
index 0000000..6f8e9b3
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_enemy.erl
@@ -0,0 +1,17 @@
+%% Gives random enemies
+-module(pq_enemy).
+-export([fetch/0]).
+
+fetch() ->
+ L = enemies(),
+ lists:nth(random:uniform(length(L)), L).
+
+enemies() ->
+ [{<<"Ant">>, [{drop, {<<"Ant Egg">>, 1}}, {experience, 1}]},
+ {<<"Wildcat">>, [{drop, {<<"Pelt">>, 1}}, {experience, 1}]},
+ {<<"Pig">>, [{drop, {<<"Bacon">>, 1}}, {experience, 1}]},
+ {<<"Wild Pig">>, [{drop, {<<"Tasty Ribs">>, 2}}, {experience, 1}]},
+ {<<"Goblin">>, [{drop, {<<"Goblin hair">>, 1}}, {experience, 2}]},
+ {<<"Robot">>, [{drop, {<<"Chunks of Metal">>, 3}}, {experience, 2}]}].
+
+
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).
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_market.erl b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_market.erl
new file mode 100644
index 0000000..241e9cd
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_market.erl
@@ -0,0 +1,77 @@
+%%% Can be used to obtain weapons and pieces of equipment of various types
+%%% to be equipped by the hero. The standard format is:
+%%% {Name, LevelModifier, Level, Price}.
+-module(pq_market).
+-export([helmet/2, weapon/2, shield/2, armor/2]).
+
+weapon(CombinedLvl, Money) ->
+ L = [
+ {<<"plastic knife">>, -1, 1, 2},
+ {<<"plastic knife">>, 0, 1, 3},
+ {<<"plastic knife">>, 1, 1, 5},
+ {<<"metal spoon">>, -1, 4, 3},
+ {<<"metal spoon">>, 0, 4, 4},
+ {<<"butter knife">>, -1, 6, 5},
+ {<<"butter knife">>, 0, 6, 7},
+ {<<"butter knife">>, 1, 6, 9},
+ {<<"machete">>, -1, 9, 15},
+ {<<"machete">>, 0, 9, 20},
+ {<<"machete">>, 1, 9, 25},
+ {<<"broad sword">>, -1, 12, 23},
+ {<<"broad sword">>, 0, 12, 30},
+ {<<"broad sword">>, 1, 12, 38},
+ {<<"lance">>, -1, 15, 32},
+ {<<"lance">>, 0, 15, 44},
+ {<<"lance">>, 1, 15, 57},
+ {<<"pistol">>, -1, 25, 95},
+ {<<"pistol">>, 0, 25, 105},
+ {<<"pistol">>, 1, 25, 155},
+ {<<"submachine gun">>, -1, 40, 200},
+ {<<"submachine gun">>, 0, 40, 245},
+ {<<"submachine gun">>, 1, 40, 365}
+ ],
+ first_match(fun(W = {_, Modifier, Lvl, Price}) ->
+ if Modifier+Lvl > CombinedLvl, Price =< Money -> W;
+ true -> continue
+ end
+ end, L).
+
+helmet(CombinedLvl, Money) -> pick_material(CombinedLvl, Money).
+shield(CombinedLvl, Money) -> pick_material(CombinedLvl, Money).
+armor(CombinedLvl, Money) -> pick_material(CombinedLvl, Money).
+
+pick_material(CombinedLvl, Money) ->
+ L = materials(),
+ first_match(fun(W = {_, Modifier, Lvl, Price}) ->
+ if Modifier+Lvl > CombinedLvl, Price =< Money -> W;
+ true -> continue
+ end
+ end, L).
+
+
+first_match(_, []) -> undefined;
+first_match(F, [H|T]) ->
+ case F(H) of
+ continue -> first_match(F,T);
+ Val -> Val
+ end.
+
+materials() ->
+ [{<<"wool">>, 0, 1, 25},
+ {<<"pleather">>, 0, 2, 45},
+ {<<"pleather">>, 1, 2, 50},
+ {<<"pleather">>, 2, 2, 65},
+ {<<"leather">>, -2, 7, 30},
+ {<<"leather">>, -1, 7, 35},
+ {<<"leather">>, 0, 7, 45},
+ {<<"leather">>, 2, 7, 65},
+ {<<"chain mail">>, -2, 12, 70},
+ {<<"chain mail">>, 0, 12, 85},
+ {<<"chain mail">>, 1, 12, 95},
+ {<<"chain mail">>, 2, 12, 105},
+ {<<"plate mail">>, -2, 17, 90},
+ {<<"plate mail">>, -1, 17, 95},
+ {<<"plate mail">>, 0, 17, 105},
+ {<<"plate mail">>, 1, 17, 115},
+ {<<"plate mail">>, 2, 17, 135}].
+
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_player.erl b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_player.erl
new file mode 100644
index 0000000..b304f69
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_player.erl
@@ -0,0 +1,175 @@
+%%% The core of ProcessQuest -- the player FSM,
+%%% acting for each of the players in the game.
+%%%
+%%% The FSM actually depends on no external events and only sends messages
+%%% to itself to prompt state changes. This is somewhat unusual as far as
+%%% gen_fsm usages go, but it's pretty useful when needing to work with
+%%% a standalone process.
+-module(pq_player).
+-behaviour(gen_fsm).
+-export([start_link/2]).
+-export([init/1, market/2, killing/2, handle_event/3, handle_sync_event/4,
+ handle_info/3, terminate/3, code_change/4]).
+
+-record(state, {name, stats, exp=0, lvlexp=1000, lvl=1,
+ equip=[], money=0, loot=[], bought=[], time=0}).
+
+
+%%% Possible states & events
+%%
+% sell buy
+% / | | \
+% \ ^ ^ /
+% [market]<--,
+% | |
+% done buying |
+% | bag full
+% v /
+% [killing fields]
+% / V V |
+% \ / | |
+% kill lvl up
+
+start_link(Name, Opts) ->
+ gen_fsm:start_link(?MODULE, {Name, Opts}, []).
+
+init({Name, Opts}) ->
+ %% Properly seeding stuff. If not doing this, the random module will
+ %% seed it based on a slightly unique time value. However, when starting
+ %% many processes at about the same time, the seeds can be very close
+ %% and give barely random results. The crypto:rand_bytes/1 function
+ %% allows for much better seeding.
+ <<A:32, B:32, C:32>> = crypto:rand_bytes(12),
+ random:seed({A,B,C}),
+ %% The first event, to start the FSM
+ gen_fsm:send_event(self(), kill),
+ case regis:register(Name, self()) of
+ {error, _} ->
+ {stop, name_taken};
+ ok ->
+ %% Use proplists with default values to let the user configure
+ %% all parts of the FSM's state by using the Opts proplist.
+ S = #state{
+ name=Name,
+ stats=proplists:get_value(stats, Opts, pq_stats:initial_roll()),
+ exp=proplists:get_value(exp, Opts, 0),
+ lvlexp=proplists:get_value(lvlexp, Opts, 1000),
+ lvl=proplists:get_value(lvl, Opts, 1),
+ equip=proplists:get_value(equip, Opts, []),
+ money=proplists:get_value(money, Opts, 0),
+ loot=proplists:get_value(loot, Opts, []),
+ bought=proplists:get_value(bought, Opts, []),
+ time=proplists:get_value(time, Opts, 0)
+ },
+ {ok, market, S}
+ end.
+
+%% Done selling. Switch to the event where we head to the killing fields
+market(sell, S = #state{loot=[]}) ->
+ gen_fsm:send_event(self(), buy),
+ {next_state, market, S};
+%% Selling an Item we have looted to the market, for whatever value it has
+market(sell, S = #state{loot=[H={_X,Val}|T], money=M}) ->
+ pq_events:sell(S#state.name, H, S#state.time),
+ gen_fsm:send_event(self(), sell),
+ {next_state, market, S#state{loot=T, money=M+Val}};
+%% When done selling, buy items with your money
+market(buy, S = #state{equip=Equip, money=Money, bought=Bought}) ->
+ %% we have slots of equipment. It's useless to buy the same
+ %% kind of item time after time, so we must select one we haven't observed yet
+ case next_slot(Equip, Bought) of
+ undefined ->
+ %% when no slot is found, go to the killing field
+ gen_fsm:send_event(self(), kill),
+ {next_state, market, S#state{bought=[]}};
+ OldItem = {Slot, {_Name, Modifier, Lvl, _Price}} ->
+ %% Replace the item by a slightly better one if possible.
+ case pq_market:Slot(Modifier+Lvl, Money) of
+ undefined ->
+ market(buy, S#state{bought=[Slot|Bought]});
+ NewItem = {_, _, _, Price} ->
+ pq_events:buy(S#state.name, Slot, NewItem, S#state.time),
+ gen_fsm:send_event(self(), buy),
+ NewEquip = [{Slot, NewItem} | Equip -- [OldItem]],
+ {next_state, market, S#state{equip=NewEquip,
+ money=Money-Price,
+ bought=[Slot|Bought]}}
+ end
+ end;
+%% Heading to the killing field. State only useful as a state transition.
+market(kill, S) ->
+ pq_events:location(S#state.name, killing, S#state.time),
+ gen_fsm:send_event(self(), kill),
+ {next_state, killing, S}.
+
+%% Killing an enemy on the killing field. Taking its drop and keeping it
+%% in our loot.
+killing(kill, S = #state{loot=Loot, stats=Stats, exp=Exp, lvlexp=LvlExp}) ->
+ MaxSize = proplists:get_value(strength, Stats)*2,
+ {EnemyName, Props} = pq_enemy:fetch(),
+ pq_events:killed(S#state.name, {EnemyName, Props}, S#state.time),
+ Drop = {_N, _V} = proplists:get_value(drop, Props),
+ KillExp = proplists:get_value(experience, Props),
+ NewLoot = [Drop|Loot],
+ if length(NewLoot) =:= MaxSize ->
+ gen_fsm:send_event(self(), market);
+ Exp+KillExp >= LvlExp ->
+ gen_fsm:send_event(self(), lvl_up);
+ true ->
+ gen_fsm:send_event(self(), kill)
+ end,
+ {next_state, killing, S#state{loot=NewLoot, exp=Exp+KillExp}};
+%% If we just leveled up, the stats get updated before we keep killing.
+killing(lvl_up, S = #state{stats=Stats, lvl=Lvl, lvlexp=LvlExp}) ->
+ NewStats = [{charisma, proplists:get_value(charisma, Stats)+pq_stats:roll()},
+ {constitution, proplists:get_value(constitution, Stats)+pq_stats:roll()},
+ {dexterity, proplists:get_value(dexterity, Stats)+pq_stats:roll()},
+ {intelligence, proplists:get_value(intelligence, Stats)+pq_stats:roll()},
+ {strength, proplists:get_value(strength, Stats)+pq_stats:roll()},
+ {wisdom, proplists:get_value(wisdom, Stats)+pq_stats:roll()}],
+ gen_fsm:send_event(self(), kill),
+ pq_events:lvl_up(S#state.name, NewStats, Lvl+1, LvlExp*2, S#state.time),
+ {next_state, killing, S#state{stats=NewStats, lvl=Lvl+1, lvlexp=LvlExp*2}};
+%% Heading to the market state transition
+killing(market, S) ->
+ pq_events:location(S#state.name, market, S#state.time),
+ gen_fsm:send_event(self(), sell),
+ {next_state, market, S}.
+
+handle_event(_Event, StateName, State) ->
+ {next_state, StateName, State}.
+
+handle_sync_event(_Event, _From, StateName, State) ->
+ {next_state, StateName, State}.
+
+handle_info(_Event, StateName, State) ->
+ {next_state, StateName, State}.
+
+terminate(_Reason, _StateName, _State) ->
+ ok.
+
+code_change(_OldVsn, StateName, State, _Extra) ->
+ {next_state, StateName, State}.
+
+%%%%%%%%%%%%%%%
+%%% PRIVATE %%%
+%%%%%%%%%%%%%%%
+
+%% Picks a slot based on what has been seen so far, combined with the
+%% current weakest item.
+next_slot(Equip, Bought) ->
+ L = expand(Equip),
+ case lists:sort([{Mod+Lvl, Entry} || Entry = {Slot, {_, Mod, Lvl, _}} <- L,
+ not lists:member(Slot, Bought)]) of
+ [] -> undefined;
+ [{_, Entry}|_] -> Entry
+ end.
+
+expand(L) ->
+ [expand_field(armor, L),
+ expand_field(helmet, L),
+ expand_field(shield, L),
+ expand_field(weapon, L)].
+
+expand_field(F, L) ->
+ {F, proplists:get_value(F, L, {undefined,0,0,0})}.
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_stats.erl b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_stats.erl
new file mode 100644
index 0000000..379f5e9
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_stats.erl
@@ -0,0 +1,19 @@
+%%% Rolls dice to generate statistics or level increases for a character
+-module(pq_stats).
+-export([initial_roll/0, roll/0]).
+
+%% First roll, when setting the stats up for the first time
+initial_roll() ->
+ [{charisma, roll(3)},
+ {constitution, roll(3)},
+ {dexterity, roll(3)},
+ {intelligence, roll(3)},
+ {strength, roll(3)},
+ {wisdom, roll(3)}].
+
+%% Rolls a single die. Used when leveling up
+roll() -> roll(1).
+
+%% Rolls Num 6-faced dice
+roll(Num) ->
+ lists:sum([random:uniform(6) || _ <- lists:seq(1,Num)]).
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_sup.erl b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_sup.erl
new file mode 100644
index 0000000..de89a9a
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_sup.erl
@@ -0,0 +1,31 @@
+%%% Supervisor for each player. Goes over a pair of a
+%%% gen_fsm (pq_player) and gen_event (pq_events).
+-module(pq_sup).
+-behaviour(supervisor).
+-export([start_link/2]).
+-export([init/1]).
+
+
+start_link(Name, Info) ->
+ supervisor:start_link(?MODULE, {Name,Info}).
+
+%% The name is passed to the events so that
+%% it can register itself as {events, Name} into the
+%% 'regis' regsitry app.
+%% Same for pq_player, which also gets the info.
+%%
+%% It is important that pq_events is started before
+%% pq_player, otherwise we might create race conditions
+%% when starting a player and then quickly generating events to
+%% an event manager that doesn't exist.
+init({Name, Info}) ->
+ {ok,
+ {{one_for_all, 2, 3600},
+ [{events,
+ {pq_events, start_link, [Name]},
+ permanent, 5000, worker, [dynamic]},
+ {player,
+ {pq_player, start_link, [Name, Info]},
+ permanent, 2000, worker, [pq_player]}]}}.
+
+
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_supersup.erl b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_supersup.erl
new file mode 100644
index 0000000..577bbed
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/pq_supersup.erl
@@ -0,0 +1,28 @@
+%%% pq_supersup is the ProcessQuest top-level supervisor.
+%%% It sits over many pq_sup instances, allowing to have
+%%% a truckload of different players running at once.
+-module(pq_supersup).
+-behaviour(supervisor).
+-export([start_link/0, start_player/2, stop_player/1]).
+-export([init/1]).
+
+%% We register it so that it's guaranteed to be unique
+start_link() ->
+ supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+%% Using a SOFO strategy because we get to have many
+%% supervisees of the same type.
+init([]) ->
+ {ok,
+ {{simple_one_for_one, 1, 60000},
+ [{sup,
+ {pq_sup, start_link, []},
+ permanent, infinity, supervisor, [pq_sup]}]}}.
+
+%% Starts an individual player
+start_player(Name, Info) ->
+ supervisor:start_child(?MODULE, [Name, Info]).
+
+%% Stops a player.
+stop_player(Name) ->
+ supervisor:terminate_child(?MODULE, regis:whereis(Name)).
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/processquest.erl b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/processquest.erl
new file mode 100644
index 0000000..469dcb0
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/src/processquest.erl
@@ -0,0 +1,39 @@
+%%% ProcessQuest's main wrapping module.
+%%% Start ProcessQuest by calling application:start(processquest).
+%%% Create a player by calling processquest:start_player(Name, Info).
+%%% - Name is any term to identify the player
+%%% - Info is additional information to configure the player. Consult
+%%% the pq_player module for more info.
+%%%
+%%% You can subscribe to the player events by calling
+%%% processquest:subscribe(Name, Handler, Args).
+%%% The handler is a regular event handler. See test/pq_events_handler.erl.
+-module(processquest).
+-behaviour(application).
+-export([start/2, stop/1]).
+-export([start_player/2, stop_player/1, subscribe/3]).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%% APPLICATION CALLBACKS %%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+start(normal, []) ->
+ pq_supersup:start_link().
+
+stop(_) -> ok.
+
+%%%%%%%%%%%%%%%%%%%%%%
+%%% USER INTERFACE %%%
+%%%%%%%%%%%%%%%%%%%%%%
+
+%% Starts a player
+start_player(Name, Info) ->
+ pq_supersup:start_player(Name, Info).
+
+%% Stops a player
+stop_player(Name) ->
+ pq_supersup:stop_player(Name).
+
+%% Subscribe to user events
+subscribe(Name, Handler, Args) ->
+ pq_events:add_handler(Name, Handler, Args).
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_enemy_tests.erl b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_enemy_tests.erl
new file mode 100644
index 0000000..14742fa
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_enemy_tests.erl
@@ -0,0 +1,33 @@
+-module(pq_enemy_tests).
+-include_lib("eunit/include/eunit.hrl").
+
+is_random_test_() ->
+ F = fun(Parent, Ref) -> fun() ->
+ <<A:32,B:32,C:32>> = crypto:rand_bytes(12),
+ random:seed({A,B,C}),
+ Entries = [pq_enemy:fetch() || _ <- lists:seq(1,100)],
+ Parent ! {Ref, Entries}
+ end end,
+ Refs = [begin
+ Ref = make_ref(),
+ spawn_link(F(self(), Ref)),
+ Ref
+ end || _ <- lists:seq(1,3)],
+ [A,B,C] = [receive
+ {Ref, X} -> X
+ end || Ref <- Refs],
+ [?_assert(A =/= B),
+ ?_assert(A =/= C),
+ ?_assert(B =/= C)].
+
+format_test_() ->
+ [[?_assertMatch({_Name, [{drop, {_DropName, _DropVal}},
+ {experience, _Exp}]}, pq_enemy:fetch())
+ || _ <- lists:seq(1,10)],
+ begin
+ {Name, [{drop, {Drop, Val}}, {experience, Exp}]} = pq_enemy:fetch(),
+ [?_assert(is_binary(Name)),
+ ?_assert(is_binary(Drop)),
+ ?_assert(is_integer(Val)),
+ ?_assert(is_integer(Exp))]
+ end].
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_events_handler.erl b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_events_handler.erl
new file mode 100644
index 0000000..a02df4c
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_events_handler.erl
@@ -0,0 +1,24 @@
+%% A fake event handler used for tests
+-module(pq_events_handler).
+-behaviour(gen_event).
+-export([init/1, handle_event/2, handle_call/2, handle_info/2,
+ terminate/2, code_change/3]).
+
+init(Parent) -> {ok, Parent}.
+
+handle_event(E, Pid) ->
+ Pid ! E,
+ {ok, Pid}.
+
+handle_call(Req, Pid) ->
+ Pid ! Req,
+ {ok, ok, Pid}.
+
+handle_info(E, Pid) ->
+ Pid ! E,
+ {ok, Pid}.
+
+terminate(_, _) -> ok.
+
+code_change(_OldVsn, Pid, _Extra) ->
+ {ok, Pid}.
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_events_tests.erl b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_events_tests.erl
new file mode 100644
index 0000000..3a327b7
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_events_tests.erl
@@ -0,0 +1,55 @@
+-module(pq_events_tests).
+-include_lib("eunit/include/eunit.hrl").
+
+-define(setup(Name, T), {setup, fun() -> start(Name) end, fun stop/1, fun T/1}).
+-define(setup(T), ?setup(make_ref(), T)).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%% TESTS DESCRIPTIONS %%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%
+events_start_stop_reg_test_() ->
+ {"The event handler can be reached, started and stopped by using the "
+ "player's name",
+ ?setup(can_contact)}.
+
+%%%%%%%%%%%%%%%%%%%%%%%
+%%% SETUP FUNCTIONS %%%
+%%%%%%%%%%%%%%%%%%%%%%%
+start(Name) ->
+ application:start(regis),
+ {ok, Pid} = pq_events:start_link(Name),
+ unlink(Pid),
+ Name.
+
+stop(Name) ->
+ pq_events:stop(Name).
+
+%%%%%%%%%%%%%%%%%%%%
+%%% ACTUAL TESTS %%%
+%%%%%%%%%%%%%%%%%%%%
+can_contact(Name) ->
+ ok = pq_events:add_handler(Name, pq_events_handler, self()),
+ pq_events:notify(Name, hello),
+ L1 = flush(),
+ pq_events:delete_handler(Name, pq_events_handler, []),
+ pq_events:notify(Name, hello),
+ L2 = flush(),
+ [?_assertEqual([hello], L1),
+ ?_assertEqual([], L2)].
+
+%%%%%%%%%%%%%%%%%%%%%%%%
+%%% HELPER FUNCTIONS %%%
+%%%%%%%%%%%%%%%%%%%%%%%%
+flush() ->
+ receive
+ X -> [X | flush1()]
+ after 300 ->
+ []
+ end.
+
+flush1() ->
+ receive
+ X -> [X | flush1()]
+ after 0 ->
+ []
+ end.
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_market_tests.erl b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_market_tests.erl
new file mode 100644
index 0000000..d689f67
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_market_tests.erl
@@ -0,0 +1,15 @@
+-module(pq_market_tests).
+-include_lib("eunit/include/eunit.hrl").
+
+best_smallest_weapon_test_() ->
+ [?_assertMatch({<<"plastic knife">>, 0, 1, 3}, pq_market:weapon(0, 5)),
+ ?_assertMatch({<<"plastic knife">>, 1, 1, 5}, pq_market:weapon(1, 100)),
+ ?_assertMatch(undefined, pq_market:weapon(0,0)),
+ ?_assertMatch(undefined, pq_market:weapon(50000,100000000000000000000))].
+
+best_smallest_gear_test_() ->
+ [[?_assertMatch({<<"wool">>, 0, 1, 25}, pq_market:F(0, 35)),
+ ?_assertMatch({<<"pleather">>, 0, 2, 45}, pq_market:F(1, 100)),
+ ?_assertMatch(undefined, pq_market:F(0,0)),
+ ?_assertMatch(undefined, pq_market:F(50000,100000000000000000000))]
+ || F <- [helmet, shield, armor]].
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_player_tests.erl b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_player_tests.erl
new file mode 100644
index 0000000..bbdbf3d
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_player_tests.erl
@@ -0,0 +1,303 @@
+-module(pq_player_tests).
+-include_lib("eunit/include/eunit.hrl").
+-record(state, {name, stats, exp=0, lvlexp=1000, lvl=1, % copied from pq_player.erl
+ equip=[], money=0, loot=[], bought=[], time=0}).
+
+-define(setup(Name, T), {setup, fun() -> start(Name) end, fun stop/1, fun T/1}).
+-define(setup(T), ?setup(make_ref(), T)).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%% TESTS DESCRIPTIONS %%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%
+new_player_test_() ->
+ [{"A player holds its own name in its state",
+ ?setup(initial_name)},
+ {"A new player has stats randomly rolled",
+ ?setup(initial_roll)},
+ {"A new player becomes a registered process",
+ ?setup(initial_register)},
+ {"A player has a counter for experience and a counter for "
+ "the next level (which is higher than the current exp)",
+ ?setup(initial_lvl)},
+ {"A player has a basic equipment (empty) when first going",
+ ?setup(initial_equipment)},
+ {"A new player has no money",
+ ?setup(initial_money)},
+ {"A new player has no loot",
+ ?setup(initial_loot)},
+ {"The state of a player can be overriden using the info "
+ "arguments to the init function",
+ ?setup(override_init)}].
+
+market_test_() ->
+ [{"A player with N items will sell all of them to the market "
+ "and end with equivalent money, before switching to the "
+ "buying state",
+ ?setup(sell_all)},
+ {"A player with nearly infinite money will buy items available "
+ "for his money, higher than his level",
+ ?setup(buy_items)},
+ {"A player with no money or no items available for the price "
+ "range leaves for the killing fields.",
+ ?setup(buy_none)},
+ {"Receiving the kill message just forwards to the killing state",
+ ?setup(to_killing)}].
+
+killing_fields_test_() ->
+ [{"Kill enemies until the loot limit is hit. Loot is 2x Strength",
+ ?setup(loot_limit)},
+ {"Killing enemies raises XP until someone levels up",
+ ?setup(kill_xp)},
+ {"Leveling up boosts stats. The sum is higher by at least as "
+ "many points as there are fields, but not more than 6 times. "
+ "Moreover, the rolling is random.",
+ ?setup(lvl_stats)},
+ {"receiving the market message just forwards to the market state",
+ ?setup(to_market)}].
+
+%%%%%%%%%%%%%%%%%%%%%%%
+%%% SETUP FUNCTIONS %%%
+%%%%%%%%%%%%%%%%%%%%%%%
+start(Name) ->
+ application:start(crypto),
+ application:start(regis),
+ Pid = spawn(fun() -> timer:sleep(infinity) end),
+ regis:register({events, Name}, Pid),
+ Name.
+
+stop(Name) ->
+ exit(regis:whereis({events, Name}), kill),
+ application:stop(regis),
+ application:stop(crypto).
+
+%%%%%%%%%%%%%%%%%%%%
+%%% ACTUAL TESTS %%%
+%%%%%%%%%%%%%%%%%%%%
+
+%% Initial State
+initial_name(Name) ->
+ {ok, market, S} = pq_player:init({Name, []}),
+ M = read_event(),
+ [?_assertEqual(Name, S#state.name),
+ ?_assertEqual(M, kill)].
+
+initial_roll(Name) ->
+ {ok, _, S} = pq_player:init({Name, []}),
+ _ = read_event(),
+ ?_assertMatch([{charisma,_}, {constitution, _}, {dexterity, _},
+ {intelligence, _}, {strength, _}, {wisdom, _}],
+ lists:sort(S#state.stats)).
+
+initial_register(Ref) ->
+ {ok, _, _} = pq_player:init({Ref, []}),
+ _ = read_event(),
+ Pid = regis:whereis(Ref),
+ Ret = pq_player:init({Ref, []}),
+ _ = read_event(),
+ [?_assert(undefined =/= Pid),
+ ?_assert(is_pid(Pid)),
+ ?_assertEqual({stop, name_taken}, Ret)].
+
+initial_lvl(Name) ->
+ {ok, _, S} = pq_player:init({Name, []}),
+ _ = read_event(),
+ [?_assert(is_integer(S#state.lvlexp)),
+ ?_assert(S#state.lvlexp > 0),
+ ?_assert(S#state.exp =:= 0)]. % start at 0 exp
+
+initial_equipment(Name) ->
+ {ok, _, S} = pq_player:init({Name, []}),
+ _ = read_event(),
+ [?_assert(is_list(S#state.equip)),
+ ?_assertEqual(undefined, proplists:get_value(armor, S#state.equip)),
+ ?_assertEqual(undefined, proplists:get_value(helmet, S#state.equip)),
+ ?_assertEqual(undefined, proplists:get_value(weapon, S#state.equip)),
+ ?_assertEqual(undefined, proplists:get_value(shield, S#state.equip))].
+
+initial_money(Name) ->
+ {ok, _, S} = pq_player:init({Name, []}),
+ _ = read_event(),
+ ?_assertEqual(0, S#state.money).
+
+initial_loot(Name) ->
+ {ok, _, S} = pq_player:init({Name, []}),
+ _ = read_event(),
+ ?_assertEqual([], S#state.loot).
+
+override_init(Name) ->
+ {ok, _, Partial} = pq_player:init({Name, [
+ {stats, [{charisma,1}, {constitution,1}, {dexterity,1},
+ {intelligence,1}, {strength,1}, {wisdom,1}]},
+ {lvlexp,1}]}),
+ regis:unregister(Name),
+ _ = read_event(),
+ {ok, _, Complete} = pq_player:init({Name, [
+ {stats, [{charisma,1}, {constitution,1}, {dexterity,1},
+ {intelligence,1}, {strength,1}, {wisdom,1}]},
+ {exp, 1}, {lvlexp,1}, {lvl,9},
+ {equip, [{weapon,{<<"plastic knife">>, -1, 1, 2}}]},
+ {money,1}, {loot, [{<<"Bacon">>, 1}]}, {bought, [helmet]}
+ ]}),
+ _ = read_event(),
+ [?_assertMatch(#state{stats=[{_,1},{_,1},{_,1},{_,1},{_,1},{_,1}],
+ lvlexp=1, lvl=1, exp=0},
+ Partial),
+ ?_assertMatch(#state{stats=[{_,1},{_,1},{_,1},{_,1},{_,1},{_,1}],
+ exp=1, lvlexp=1, lvl=9, equip=[{weapon,_}],
+ money=1, loot=[{_,_}], bought=[_]},
+ Complete)].
+
+%% Market
+sell_all(Name) ->
+ undefined = read_event(),
+ Loot = [proplists:get_value(drop, element(2, pq_enemy:fetch()))
+ || _ <- lists:seq(1,5)],
+ {[Sum1, Sum2, Sum3, Sum4, Sum5], _} = lists:mapfoldl(
+ fun(X, Sum) -> {X+Sum, X+Sum} end,
+ 0,
+ [Val || {_, Val} <- Loot]
+ ),
+ %undefined = read_event(),
+ S0 = #state{name=Name, loot=Loot, money=0, lvl=1},
+ {next_state, market, S1} = pq_player:market(sell, S0),
+ M1 = read_event(),
+ {next_state, market, S2} = pq_player:market(sell, S1),
+ M2 = read_event(),
+ {next_state, market, S3} = pq_player:market(sell, S2),
+ M3 = read_event(),
+ {next_state, market, S4} = pq_player:market(sell, S3),
+ M4 = read_event(),
+ {next_state, market, S5} = pq_player:market(sell, S4),
+ M5 = read_event(),
+ {next_state, market, S6} = pq_player:market(sell, S5),
+ M6 = read_event(),
+ [?_assertMatch(#state{money=Sum1, loot=[_,_,_,_]}, S1),
+ ?_assertMatch(#state{money=Sum2, loot=[_,_,_]}, S2),
+ ?_assertMatch(#state{money=Sum3, loot=[_,_]}, S3),
+ ?_assertMatch(#state{money=Sum4, loot=[_]}, S4),
+ ?_assertMatch(#state{money=Sum5, loot=[]}, S5),
+ ?_assertMatch(#state{money=Sum5, loot=[]}, S6),
+ ?_assertEqual([sell, sell, sell, sell, sell, buy],
+ [M1,M2,M3,M4,M5,M6])].
+
+buy_items(Name) ->
+ %% 4 different pieces of equipment to buy
+ S0 = #state{name=Name, equip=[], money=999999999999},
+ {next_state, market, S1} = pq_player:market(buy, S0),
+ M1 = read_event(),
+ {next_state, market, S2} = pq_player:market(buy, S1),
+ M2 = read_event(),
+ {next_state, market, S3} = pq_player:market(buy, S2),
+ M3 = read_event(),
+ {next_state, market, S4} = pq_player:market(buy, S3),
+ M4 = read_event(),
+ %% All slots bought. Implicit requirement: not buying for the
+ %% same slot twice.
+ {next_state, market, S5} = pq_player:market(buy, S4),
+ M5 = read_event(),
+ [?_assertEqual([S5#state.money, S4#state.money, S3#state.money,
+ S2#state.money, S1#state.money, S0#state.money],
+ lists:sort([S5#state.money, S4#state.money, S3#state.money,
+ S2#state.money, S1#state.money, S0#state.money])),
+ ?_assertEqual([1,2,3,4,4],
+ [length(L) || L <- [S1#state.equip, S2#state.equip,
+ S3#state.equip, S4#state.equip,
+ S5#state.equip]]),
+ ?_assertEqual([buy, buy, buy, buy, kill],
+ [M1, M2, M3, M4, M5])].
+
+buy_none(Name) ->
+ S0 = #state{name=Name, equip=[], money=0},
+ %% one try per part of the equipment
+ {next_state, market, S1} = pq_player:market(buy, S0),
+ _ = read_event(),
+ {next_state, market, S2} = pq_player:market(buy, S1),
+ _ = read_event(),
+ {next_state, market, S3} = pq_player:market(buy, S2),
+ _ = read_event(),
+ {next_state, market, S4} = pq_player:market(buy, S3),
+ M = read_event(),
+ [?_assertEqual(S0, S4),
+ ?_assertEqual(kill, M)].
+
+to_killing(Name) ->
+ S = #state{name=Name},
+ Res = pq_player:market(kill, S),
+ M = read_event(),
+ [?_assertMatch({next_state, killing, S}, Res),
+ ?_assertEqual(kill, M)].
+
+%% Killing fields tests
+loot_limit(Name) ->
+ S0 = #state{name=Name, stats=[{strength, 2}], loot=[]},
+ {next_state, killing, S1 = #state{loot=L1}} = pq_player:killing(kill, S0),
+ M1 = read_event(),
+ {next_state, killing, S2 = #state{loot=L2}} = pq_player:killing(kill, S1),
+ M2 = read_event(),
+ {next_state, killing, S3 = #state{loot=L3}} = pq_player:killing(kill, S2),
+ M3 = read_event(),
+ {next_state, killing, #state{loot=L4}} = pq_player:killing(kill, S3),
+ M4 = read_event(),
+ %% Group identical drops with a counter?
+ [?_assertEqual([1,2,3,4], [length(L) || L <- [L1, L2, L3, L4]]),
+ ?_assertEqual([kill, kill, kill, market], [M1, M2, M3, M4])].
+
+kill_xp(Name) ->
+ S0 = #state{name=Name, stats=[{strength, 999}|pq_stats:initial_roll()],
+ lvl=1, exp=0, lvlexp=5},
+ %% between 1 and 5 kills required to lvl up.
+ {next_state, NS1, S1} = pq_player:killing(kill, S0),
+ M1 = read_event(),
+ {next_state, NS2, S2} = pq_player:NS1(M1, S1),
+ M2 = read_event(),
+ {next_state, NS3, S3} = pq_player:NS2(M2, S2),
+ M3 = read_event(),
+ {next_state, NS4, S4} = pq_player:NS3(M3, S3),
+ M4 = read_event(),
+ {next_state, NS5, S5} = pq_player:NS4(M4, S4),
+ M5 = read_event(),
+ {next_state, NS6, S6} = pq_player:NS5(M5, S5),
+ M6 = read_event(),
+ [?_assert(lists:any(fun(#state{lvl=L}) -> L > 1 end, [S1,S2,S3,S4,S5,S6])),
+ ?_assert(lists:any(fun(#state{lvlexp=L}) -> L >= 10 end, [S1,S2,S3,S4,S5,S6])),
+ ?_assert(lists:any(fun(#state{exp=E}) -> E >= 5 end, [S1,S2,S3,S4,S5,S6])),
+ ?_assert(lists:any(fun(FSMState) -> FSMState =:= killing end,
+ [NS1, NS2, NS3, NS4, NS5, NS6])),
+ ?_assert(lists:any(fun(Msg) -> Msg =:= kill end,
+ [M1, M2, M3, M4, M5, M6])),
+ ?_assert(lists:any(fun(Msg) -> Msg =:= lvl_up end,
+ [M1, M2, M3, M4, M5, M6]))].
+
+lvl_stats(Name) ->
+ {ok, _, S0} = pq_player:init({Name, []}),
+ _ = read_event(),
+ TotalStats = length(S0#state.stats),
+ {next_state, killing, S1} = pq_player:killing(lvl_up, S0),
+ _ = read_event(),
+ {next_state, killing, S2} = pq_player:killing(lvl_up, S0),
+ _ = read_event(),
+ SumInit = lists:sum([Pts || {_,Pts} <- S0#state.stats]),
+ SumS1 = lists:sum([Pts || {_,Pts} <- S1#state.stats]),
+ SumS2 = lists:sum([Pts || {_,Pts} <- S2#state.stats]),
+ [?_assert(SumS1 >= TotalStats+SumInit),
+ ?_assert(SumS2 >= TotalStats+SumInit),
+ ?_assert(SumS1 =< TotalStats*6 + SumInit),
+ ?_assert(SumS2 =< TotalStats*6 + SumInit),
+ ?_assert(S1#state.stats =/= S2#state.stats)].
+
+to_market(Name) ->
+ S = #state{name=Name},
+ Res = pq_player:killing(market, S),
+ M = read_event(),
+ [?_assertMatch({next_state, market, S}, Res),
+ ?_assertEqual(sell, M)].
+
+%%%%%%%%%%%%%%%%%%%%%%%%
+%%% HELPER FUNCTIONS %%%
+%%%%%%%%%%%%%%%%%%%%%%%%
+read_event() ->
+ receive
+ {'$gen_event', Msg} -> Msg
+ after 0 ->
+ undefined
+ end.
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_stats_tests.erl b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_stats_tests.erl
new file mode 100644
index 0000000..da9daa5
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/pq_stats_tests.erl
@@ -0,0 +1,28 @@
+-module(pq_stats_tests).
+-include_lib("eunit/include/eunit.hrl").
+
+all_stats_test_() ->
+ Stats = pq_stats:initial_roll(),
+ {"Checks whether all stats are returned",
+ [?_assertEqual([charisma, constitution, dexterity,
+ intelligence, strength, wisdom],
+ lists:sort(proplists:get_keys(Stats)))]}.
+
+initial_roll_test_() ->
+ Rolls = [pq_stats:initial_roll() || _ <- lists:seq(1,100)],
+ {"All die rolls are made out of 3 d6 dice",
+ %% 6 == number of stats
+ [?_assertEqual(6, length([S || {_,S} <- Stats, S >= 3, S =< 18]))
+ || Stats <- Rolls]}.
+
+initial_random_roll_test_() ->
+ Stats = [pq_stats:initial_roll() || _ <- lists:seq(1,100)],
+ {"All die rolls are random",
+ ?_assertEqual(lists:sort(Stats),
+ lists:sort(sets:to_list(sets:from_list(Stats))))}.
+
+single_die_roll_test_() ->
+ Rolls = [pq_stats:roll() || _ <- lists:seq(1,100)],
+ [?_assertEqual(100, length([N || N <- Rolls, N >= 1, N =< 6])),
+ ?_assert(1 =/= length(sets:to_list(sets:from_list(Rolls))))].
+
diff --git a/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/processquest_tests.erl b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/processquest_tests.erl
new file mode 100644
index 0000000..fa118b1
--- /dev/null
+++ b/learn-you-some-erlang/processquest/apps/processquest-1.0.0/test/processquest_tests.erl
@@ -0,0 +1,49 @@
+-module(processquest_tests).
+-include_lib("eunit/include/eunit.hrl").
+
+%%% Integration tests verifying the whole app.
+-define(setup(Name, T), {setup, fun() -> start(Name) end, fun stop/1, fun T/1}).
+-define(setup(T), ?setup(make_ref(), T)).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%% TESTS DESCRIPTIONS %%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%
+integration_test_() ->
+ [{"A player can be started from the processquest module and monitored",
+ ?setup(subscribe)}].
+
+%%%%%%%%%%%%%%%%%%%%%%%
+%%% SETUP FUNCTIONS %%%
+%%%%%%%%%%%%%%%%%%%%%%%
+start(Name) ->
+ application:start(crypto),
+ application:start(regis),
+ application:start(processquest),
+ processquest:start_player(Name, [{time,1100}]),
+ Name.
+
+stop(Name) ->
+ processquest:stop_player(Name),
+ application:stop(processquest),
+ application:stop(regis).
+
+%%%%%%%%%%%%%%%%%%%%
+%%% ACTUAL TESTS %%%
+%%%%%%%%%%%%%%%%%%%%
+subscribe(Name) ->
+ ok = processquest:subscribe(Name, pq_events_handler, self()),
+ timer:sleep(4000),
+ Msgs = flush(),
+ [?_assertMatch([{Name, killed, _Time1, {_EnemyName1, _Props1}},
+ {Name, killed, _Time2, {_EnemyName2, _Props2}},
+ {Name, killed, _Time3, {_EnemyName3, _Props3}}],
+ Msgs)].
+
+%%%%%%%%%%%%%%%%%%%%%%%%
+%%% HELPER FUNCTIONS %%%
+%%%%%%%%%%%%%%%%%%%%%%%%
+flush() ->
+ receive
+ X -> [X | flush()]
+ after 0 -> []
+ end.