Permalink
Browse files

Simple inventorymanager

  • Loading branch information...
MasseR committed May 5, 2013
1 parent 28c945c commit 51fe2207281450ddaa4f6ccda1cdbdd5c953c853
Showing with 170 additions and 2 deletions.
  1. +162 −0 src/inventorymanager_srv.erl
  2. +1 −1 src/itemstats_srv.erl
  3. +5 −1 src/itemstats_sup.erl
  4. +1 −0 src/minecraft.config
  5. +1 −0 start-dev.sh
@@ -0,0 +1,162 @@
+-module(inventorymanager_srv).
+
+-behaviour(gen_server).
+
+%% API
+-export([start_link/1, get_turtles/2, turtle_item/1, turtle_max/1, change_max/1]).
+
+%% gen_server callbacks
+-export([init/1,
+ handle_call/3,
+ handle_cast/2,
+ handle_info/2,
+ terminate/2,
+ code_change/3]).
+
+-record(state, {max}).
+
+%%%===================================================================
+%%% API
+%%%===================================================================
+
+%%---------
+%% @doc
+%% Return a list of turtles and item counts
+%%
+%% @end
+%%--------
+get_turtles(ItemId, Count) ->
+ gen_server:call(?MODULE, {get, ItemId, Count}).
+
+turtle_item(TurtleId) ->
+ gen_server:call(?MODULE, {turtle_item, TurtleId}).
+
+turtle_max(TurtleId) ->
+ gen_server:call(?MODULE, {turtle_max, TurtleId}).
+
+change_max(Max) ->
+ gen_server:cast(?MODULE, {new_max, Max}).
+
+%%--------------------------------------------------------------------
+%% @doc
+%% Starts the server
+%%
+%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
+%% @end
+%%--------------------------------------------------------------------
+start_link(Max) ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [Max], []).
+
+%%%===================================================================
+%%% gen_server callbacks
+%%%===================================================================
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Initializes the server
+%%
+%% @spec init(Args) -> {ok, State} |
+%% {ok, State, Timeout} |
+%% ignore |
+%% {stop, Reason}
+%% @end
+%%--------------------------------------------------------------------
+init([Max]) ->
+ {ok, #state{max=Max}}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Handling call messages
+%%
+%% @spec handle_call(Request, From, State) ->
+%% {reply, Reply, State} |
+%% {reply, Reply, State, Timeout} |
+%% {noreply, State} |
+%% {noreply, State, Timeout} |
+%% {stop, Reason, Reply, State} |
+%% {stop, Reason, State}
+%% @end
+%%--------------------------------------------------------------------
+handle_call({get, ItemId, Count}, _From, State) ->
+ Turtles = itemstats_srv:owners(ItemId),
+ Used = case itemstats_srv:value(ItemId) of
+ X when X < Count -> Turtles;
+ _ -> consume(Turtles, Count)
+ end,
+ lists:foreach(fun({TurtleId, UsedCount}) -> itemstats_srv:decrement(ItemId, TurtleId, UsedCount) end, Used),
+ {reply, Used, State};
+handle_call(_Request, _From, State) ->
+ Reply = ok,
+ {reply, Reply, State}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Handling cast messages
+%%
+%% @spec handle_cast(Msg, State) -> {noreply, State} |
+%% {noreply, State, Timeout} |
+%% {stop, Reason, State}
+%% @end
+%%--------------------------------------------------------------------
+
+handle_cast({new_max, Max}, State) ->
+ {noreply, State#state{max=Max}};
+handle_cast(_Msg, State) ->
+ {noreply, State}.
+
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Handling all non call/cast messages
+%%
+%% @spec handle_info(Info, State) -> {noreply, State} |
+%% {noreply, State, Timeout} |
+%% {stop, Reason, State}
+%% @end
+%%--------------------------------------------------------------------
+handle_info(_Info, State) ->
+ {noreply, State}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% This function is called by a gen_server when it is about to
+%% terminate. It should be the opposite of Module:init/1 and do any
+%% necessary cleaning up. When it returns, the gen_server terminates
+%% with Reason. The return value is ignored.
+%%
+%% @spec terminate(Reason, State) -> void()
+%% @end %%--------------------------------------------------------------------
+terminate(_Reason, _State) ->
+ ok.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Convert process state when code is changed
+%%
+%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
+%% @end
+%%--------------------------------------------------------------------
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.
+
+%%%===================================================================
+%%% Internal functions
+%%%===================================================================
+
+consume(Turtles, Count) ->
+ consume(Turtles, Count, []).
+
+consume(_Turtles, 0, Acc) -> Acc;
+consume([{TurtleId, Count}|Xs], Left, Acc) ->
+ case Left < Count of
+ true ->
+ consume(Xs, 0, [{TurtleId, Left} | Acc]);
+ false ->
+ consume(Xs, Left - Count, [{TurtleId, Count} | Acc])
+ end.
View
@@ -134,7 +134,7 @@ handle_cast({inc, Key, TurtleId, Value}, State) ->
X when X < 0 -> 0;
X -> X
end,
- dets:insert(?MODULE, {Key, NewValue}),
+ handle_cast({put, Key, TurtleId, NewValue}, State),
{noreply, State};
handle_cast(_Msg, State) ->
{noreply, State}.
View
@@ -43,7 +43,11 @@ start_link() ->
%% @end
%%--------------------------------------------------------------------
init([]) ->
- {ok, {{one_for_one, 5, 10}, [?CHILD('itemstats_srv', 'itemstats_srv', worker, [])]}}.
+ Max = case application:get_env(minecraft, max_items) of
+ {ok, M} -> M;
+ undefined -> 4096
+ end,
+ {ok, {{one_for_one, 5, 10}, [?CHILD('itemstats_srv', 'itemstats_srv', worker, []), ?CHILD('inventorymanager_srv', 'inventorymanager_srv', worker, [Max])]}}.
%%%===================================================================
%%% Internal functions
View
@@ -0,0 +1 @@
+[{minecraft, [{max_items, 4096}]}].
View
@@ -1,6 +1,7 @@
#!/bin/sh
# NOTE: mustache templates need \ because they are not awesome.
exec erl -pa ebin edit deps/*/ebin -boot start_sasl \
+ -config src/minecraft \
-sname minecraft_dev \
-s minecraft \
-s reloader

0 comments on commit 51fe220

Please sign in to comment.