Commits

Johan Warlander committed 91599f9

Adding typespecs; em_rh_game, em_rh_login

Comments (0)

Files changed (3)

lib/erlymud/include/request.hrl

 % request.hrl
--record(req, {conn, user, living, queue=[], handlers=[]}).
+-record(req, {conn::em_conn:conn_pid(), user::em_user:user_pid(), 
+              living::em_living:living_pid(), 
+              queue=[]::[init | {input, string()}], 
+              handlers=[]::[req_handler()]}).
 
 -define(req_done, {done, Req}).
 -define(req_next(Fun), {ok, {?MODULE, Fun, []}, Req}).
 -define(req_next(Fun, Args, State), {ok, {?MODULE, Fun, Args}, State}).
 -define(req_next_and_link(Fun, Args, State), {link, {?MODULE, Fun, Args}, State}).
 -define(req_next_and_link(Module, Fun, Args, State), {link, {Module, Fun, Args}, State}).
+
+-type req_handler() :: {atom(), atom(), list()}.
+
+-type req()      :: #req{}.
+-type req_done() :: {done, req()}.
+-type req_ok()   :: {ok, req_handler(), req()}.
+-type req_link() :: {link, req_handler(), req()}.
+-type req_any()  :: req_done() | req_ok().
+

lib/erlymud/src/em_rh_game.erl

 %% Type Specifications
 -include("types.hrl").
 -type ob_list() :: [em_object:object()].
-
--type req()      :: #req{}.
--type req_done() :: {done, req()}.
--type req_ok()   :: {ok, mfargs(), req()}.
--type req_any()  :: req_done() | req_ok().
+-type liv_list() :: [em_living:living_pid()].
 
 -type cmd_ok()     :: {ok, req()}.
 -type cmd_stop()   :: {stop, req()}.
   try_drop(Id, Obs, Req),
   {ok, Req}.
 
+-spec try_drop(string(), ob_list(), req()) -> ok.
 try_drop(_Id, [], Req) ->
   print("You don't have anything like that.\n", Req);
 try_drop(Id, [Ob|Obs], Req) ->
       try_drop(Id, Obs, Req)
   end.
 
+-spec do_drop(em_object:object(), req()) -> ok.
 do_drop(Ob, #req{living=Liv}=Req) ->
   Name = em_living:get_name(Liv),
   Room = em_living:get_room(Liv),
   do_get(Id, Obs, Req),
   {ok, Req}.
 
+-spec do_get(string(), ob_list(), req()) -> ok.
 do_get(_Id, [], Req) ->
   print("There's no such thing here.\n", Req);
 do_get(Id, [Ob|Obs], #req{living=Liv}=Req) ->
       end
   end.
 
+-spec do_look_ob(string(), ob_list(), req()) -> ok | {error, not_found}.
 do_look_ob(_Id, [], _Req) ->
   {error, not_found};
 do_look_ob(Id, [Ob|Obs], Req) ->
       do_look_ob(Id, Obs, Req)
   end.
 
+-spec do_look_liv(string(), liv_list(), req()) -> ok | {error, not_found}.
 do_look_liv(_Id, [], _Req) ->
   {error, not_found};
 do_look_liv(Id, [Liv|People], Req) ->
   end.
 
 % Go / North / East / South / West 
+-spec cmd_north([string()], req()) -> cmd_ok().
 cmd_north(_Args, Req) ->
   cmd_go(["north"], Req).
+-spec cmd_east([string()], req()) -> cmd_ok().
 cmd_east(_Args, Req) ->
   cmd_go(["east"], Req).
+-spec cmd_south([string()], req()) -> cmd_ok().
 cmd_south(_Args, Req) ->
   cmd_go(["south"], Req).
+-spec cmd_west([string()], req()) -> cmd_ok().
 cmd_west(_Args, Req) ->
   cmd_go(["west"], Req).
 
+-spec cmd_go([string()], req()) -> cmd_ok().
 cmd_go([Dir|_Args], #req{living=Liv}=Req) ->
   Room = em_living:get_room(Liv),
   do_go(em_room:get_exit(Room, Dir), Req), 
   {ok, Req}.
 
+-spec do_go({error, not_found}, req()) -> ok
+         ; ({ok, {string(), string()}}, req()) -> cmd_ok().
 do_go({error, not_found}, Req) ->
   print("You can't go in that direction.\n", Req);
 do_go({ok, {Dir, Dest}}, #req{living=Liv}=Req) ->
   cmd_glance([], Req).
 
 %% Emote
+-spec cmd_emote([string()], req()) -> cmd_ok().
 cmd_emote(Args, #req{living=Liv}=Req) ->
   Text = em_grammar:punctuate(string:join(Args, " ")),
   Name = em_living:get_name(Liv),
   {ok, Req}.
 
 %% Say
+-spec cmd_say([string()], req()) -> cmd_ok().
 cmd_say([FirstWord|Rest], #req{living=Liv}=Req) ->
   Text = string:join([em_text:capitalize(FirstWord)|Rest], " "),
   Name = em_living:get_name(Liv),
   {ok, Req}.
 
 %% Tell
+-spec cmd_tell([string()], req()) -> cmd_ok().
 cmd_tell([Who,FirstWord|Rest], #req{user=User}=Req) ->
   Name = em_user:get_name(User),
   case em_game:lookup_user(Who) of
   {ok, Req}.
 
 %% Who
+-spec cmd_who([string()], req()) -> cmd_ok().
 cmd_who(_Args, Req) ->
   print(["Users:\n",
     [[" ", Name, "\n"] || {Name, _Pid} <- em_game:get_users()]], Req),
   {ok, Req}.
 
 %% Save
+-spec cmd_save([string()], req()) -> cmd_ok().
 cmd_save(_Args, #req{living=Liv}=Req) ->
   print("Saving..\n", Req),
   case em_living:save(Liv) of
   end.
 
 %% Setlong
+-spec cmd_setlong([string()], req()) -> cmd_ok().
 cmd_setlong(Args, #req{living=Liv}=Req) ->
   em_living:set_long(Liv, string:join(Args, " ")),
   {ok, Req}.
 
 %% addexit
+-spec cmd_addexit([string()], req()) -> cmd_ok().
 cmd_addexit([Dir, ToName|_Rest], #req{living=Liv}=Req) ->
   ok = verify_privilege(admin, Req),
   case em_room_mgr:get_room(ToName) of
   {ok, Req}.
 
 %% REdit
+-spec cmd_redit([string()], req()) -> cmd_ok().
 cmd_redit(["dig", Dir, ToName|_Rest], #req{living=Liv}=Req) ->
   ok = verify_privilege(admin, Req),
   case em_room_mgr:new_room(ToName) of
   Req),
   {ok, Req}.
 
+-spec reverse_dir(string()) -> string().
 reverse_dir("north") -> "south";
 reverse_dir("east") -> "west";
 reverse_dir("south") -> "north";
 reverse_dir("west") -> "east".
 
+-spec verify_privilege(atom(), req()) -> ok.
 verify_privilege(Priv, #req{user=User}) ->
   case em_user:has_privilege(User, Priv) of
     true -> ok;
   end.
 
 %% Help
+-spec cmd_help([string()], req()) -> cmd_ok().
 cmd_help(["privileges"], Req) ->
   print(
   "Privileges are used to control what commands users have access to.\n"
 
 %% Utility functions
 
+-spec print(iolist(), req()) -> ok.
 print(Format, Req) ->
   print(Format, [], Req).
+
+-spec print(iolist(), list(), req()) -> ok.
 print(Format, Args, #req{conn=Conn}) ->
   em_conn:print(Conn, Format, Args).
     

lib/erlymud/src/em_rh_login.erl

 %%% =========================================================================
 -module(em_rh_login).
 
+%% API
 -export([welcome/1, login/3]).
 
 -include("request.hrl").
 
-%% API
+%% Type Specifications
+-include("types.hrl").
 
+
+%% ==========================================================================
+%% API Functions
+%% ==========================================================================
+
+-spec welcome(em_conn:conn_pid()) -> ok.
 welcome(Conn) ->
   em_conn:print(Conn, "\nWelcome to ErlyMUD 0.3.4\n\n"),
   em_conn:print(Conn, "Login: ").
 
 %% Got a username, do something with it
+-spec login(any(), string(), req()) -> req_ok() | req_link().
 login(got_user, "", #req{conn=Conn}=Req) ->
   em_conn:print(Conn, "Invalid username.\n\n"),
   em_conn:print(Conn, "Login: "),
       ?req_next(login, [got_user])
   end.
 
+-spec do_login(string(), req()) -> req_ok() | req_link().
 do_login(Name, #req{conn=Conn}=Req) ->
   {ok, User} = em_user_sup:start_child(Name, Conn),
   link(User),
       ?req_next(login, [got_user])
   end.
 
+-spec do_incarnate(req()) -> {ok, req()}.
 do_incarnate(#req{conn=Conn, living=Living}=Req) ->
   ok = em_game:incarnate(Living),
   em_conn:print(Conn, "\nType \"help\" to get some basic information about\n"
   em_conn:print(Conn, "\n> "),
   {ok, Req}.
 
--ifdef(false).
-parse(User, Line, #req{conn=Conn}=Req) ->
-  case Line of
-    "" ->
-      em_conn:print(Conn, "> "),
-      ?req_next(parse, [User]);
-    "quit" ->
-      em_living:cmd(User, "quit"),
-      ?req_done;
-    Cmd ->
-      em_living:cmd(User, Cmd),
-      em_conn:print(Conn, "\n> "),
-      ?req_next(parse, [User])
-  end.
--endif.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.