Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(server).
- -compile(export_all).
- %%% Client API functions
- new_user(Data) ->
- whereis(tserver) ! {new_user, self(), Data},
- receive
- {user_added, Ref} ->
- io:format("Ref is: ~p~n", [Ref]), Ref
- end.
- %% async function
- give_state() ->
- whereis(tserver) ! state.
- %% async function
- add_calories(Reference, Day, Calories) ->
- whereis(tserver) ! {add, self(), Reference, Day, Calories}.
- get_feedback(Ref) ->
- whereis(tserver) ! {feedback, self(), Ref},
- receive
- {Note, FeedbackMap} -> {Note, FeedbackMap}
- after 5000 ->
- req_expired
- end.
- %% async function
- update_user(Ref, Data) ->
- whereis(tserver) ! {update, self(), Ref, Data}.
- %%% Server functions
- start() ->
- Pid = spawn(?MODULE, init, []),
- register(tserver, Pid).
- stop() ->
- whereis(tserver) ! stop.
- init() ->
- loop(#{}).
- loop(State) ->
- receive
- stop ->
- exit(shutdown);
- state ->
- io:format("Current state: ~n~p~n", [State]);
- {new_user, ClientPid, {Name, Weight, Height, Age, Sex, WeekWorkout}} ->
- Ref = erlang:make_ref(),
- MaxCalories = calc_max_cals(Weight, Height, Age, Sex, WeekWorkout),
- ClientPid ! {user_added, Ref},
- loop(State#{Ref => {Name, MaxCalories, #{}}});
- {add, ClientPid, Reference, Day, Calories} ->
- case maps:is_key(Reference, State) of
- false ->
- io:format("Unauthorised request"),
- loop(State);
- true ->
- {Name, MaxCalories, Diary} = maps:get(Reference, State),
- case maps:is_key(Day, Diary) of
- false ->
- UpdatedDiary = maps:put(Day, Calories, Diary),
- UpdatedState = maps:update(Reference, {Name, MaxCalories, UpdatedDiary}, State),
- loop(UpdatedState);
- true ->
- CurrCals = maps:get(Day, Diary),
- UpdatedCals = CurrCals + Calories,
- UpdatedDiary = maps:update(Day, UpdatedCals, Diary),
- UpdatedState = maps:update(Reference, {Name, MaxCalories, UpdatedDiary}, State),
- loop(UpdatedState)
- end
- end;
- {feedback, ClientPid, Ref} ->
- case maps:is_key(Ref, State) of
- true ->
- {_Name, MaxCalories, Diary} = maps:get(Ref, State),
- FeedbackMap = create_feedback_map(MaxCalories, Diary),
- io:format("Feedback map: ~p~n", [FeedbackMap]),
- ClientPid ! {"Consider you should eat " ++ float_to_list(MaxCalories), FeedbackMap},
- loop(State);
- false ->
- ClientPid ! "no data could be found",
- loop(State)
- end;
- {update, ClientPid, Ref, {Name, Weight, Height, Age, Sex, WeekWorkout}} ->
- case maps:is_key(Ref, State) of
- true ->
- {_, _, Diary} = maps:get(Ref, State),
- UpdatedMaxCals = calc_max_cals(Weight, Height, Age, Sex, WeekWorkout),
- UpdatedState = maps:update(Ref, {Name, UpdatedMaxCals, Diary}, State),
- loop(UpdatedState);
- false ->
- loop(State)
- end
- end.
- %%% Private server functions
- calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) when WeekWorkout =:= 0 ->
- bmr(Weight, Height, Age, Sex) * 1.2;
- calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) when WeekWorkout =:= 1 orelse WeekWorkout =:= 2 ->
- bmr(Weight, Height, Age, Sex) * 1.375;
- calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) when WeekWorkout >= 3 andalso WeekWorkout =< 5 ->
- bmr(Weight, Height, Age, Sex) * 1.55;
- calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) when WeekWorkout =:= 6 orelse WeekWorkout =:= 7 ->
- bmr(Weight, Height, Age, Sex) * 1.725;
- calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) -> 0.0.
- bmr(Weight,Height,Age,f)->
- ((10*Weight) + (6.25 * Height) - (5 * Age) - 161);
- bmr(Weight,Height,Age,m)->
- (10*Weight) + (6.25 * Height) - (5 * Age) + 5.
- create_feedback_map(MaxCals, Diary) ->
- create_feedback_map(MaxCals, maps:next(maps:iterator(Diary)), #{}).
- create_feedback_map(_, none, Acc) -> Acc;
- create_feedback_map(MaxCals, {Day, TotCals, NextIt}, Acc) when MaxCals < TotCals ->
- create_feedback_map(MaxCals, NextIt, Acc#{Day => {bad, TotCals}});
- create_feedback_map(MaxCals, {Day, TotCals, NextIt}, Acc) ->
- create_feedback_map(MaxCals, NextIt, Acc#{Day => {good, TotCals}}).
- -module(server).
- -compile(export_all).
- %%% Client API functions
- new_user(Data) ->
- whereis(tserver) ! {new_user, self(), Data},
- receive
- {user_added, Ref} ->
- io:format("Ref is: ~p~n", [Ref]), Ref
- end.
- %% async function
- give_state() ->
- whereis(tserver) ! state.
- %% async function
- add_calories(Reference, Day, Calories) ->
- whereis(tserver) ! {add, self(), Reference, Day, Calories}.
- get_feedback(Ref) ->
- whereis(tserver) ! {feedback, self(), Ref},
- receive
- {Note, FeedbackMap} -> {Note, FeedbackMap}
- after 5000 ->
- req_expired
- end.
- %% async function
- update_user(Ref, Data) ->
- whereis(tserver) ! {update, self(), Ref, Data}.
- %%% Server functions
- start() ->
- Pid = spawn(?MODULE, init, []),
- register(tserver, Pid).
- stop() ->
- whereis(tserver) ! stop.
- init() ->
- loop(#{}).
- loop(State) ->
- receive
- stop ->
- exit(shutdown);
- state ->
- io:format("Current state: ~n~p~n", [State]);
- {new_user, ClientPid, {Name, Weight, Height, Age, Sex, WeekWorkout}} ->
- Ref = erlang:make_ref(),
- MaxCalories = calc_max_cals(Weight, Height, Age, Sex, WeekWorkout),
- ClientPid ! {user_added, Ref},
- loop(State#{Ref => {Name, MaxCalories, #{}}});
- {add, ClientPid, Reference, Day, Calories} ->
- case maps:is_key(Reference, State) of
- false ->
- io:format("Unauthorised request"),
- loop(State);
- true ->
- {Name, MaxCalories, Diary} = maps:get(Reference, State),
- case maps:is_key(Day, Diary) of
- false ->
- UpdatedDiary = maps:put(Day, Calories, Diary),
- UpdatedState = maps:update(Reference, {Name, MaxCalories, UpdatedDiary}, State),
- loop(UpdatedState);
- true ->
- CurrCals = maps:get(Day, Diary),
- UpdatedCals = CurrCals + Calories,
- UpdatedDiary = maps:update(Day, UpdatedCals, Diary),
- UpdatedState = maps:update(Reference, {Name, MaxCalories, UpdatedDiary}, State),
- loop(UpdatedState)
- end
- end;
- {feedback, ClientPid, Ref} ->
- case maps:is_key(Ref, State) of
- true ->
- {_Name, MaxCalories, Diary} = maps:get(Ref, State),
- FeedbackMap = create_feedback_map(MaxCalories, Diary),
- io:format("Feedback map: ~p~n", [FeedbackMap]),
- ClientPid ! {"Consider you should eat " ++ float_to_list(MaxCalories), FeedbackMap},
- loop(State);
- false ->
- ClientPid ! "no data could be found",
- loop(State)
- end;
- {update, ClientPid, Ref, {Name, Weight, Height, Age, Sex, WeekWorkout}} ->
- case maps:is_key(Ref, State) of
- true ->
- {_, _, Diary} = maps:get(Ref, State),
- UpdatedMaxCals = calc_max_cals(Weight, Height, Age, Sex, WeekWorkout),
- UpdatedState = maps:update(Ref, {Name, UpdatedMaxCals, Diary}, State),
- loop(UpdatedState);
- false ->
- loop(State)
- end
- end.
- %%% Private server functions
- calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) when WeekWorkout =:= 0 ->
- bmr(Weight, Height, Age, Sex) * 1.2;
- calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) when WeekWorkout =:= 1 orelse WeekWorkout =:= 2 ->
- bmr(Weight, Height, Age, Sex) * 1.375;
- calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) when WeekWorkout >= 3 andalso WeekWorkout =< 5 ->
- bmr(Weight, Height, Age, Sex) * 1.55;
- calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) when WeekWorkout =:= 6 orelse WeekWorkout =:= 7 ->
- bmr(Weight, Height, Age, Sex) * 1.725;
- calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) -> 0.0.
- bmr(Weight,Height,Age,f)->
- ((10*Weight) + (6.25 * Height) - (5 * Age) - 161);
- bmr(Weight,Height,Age,m)->
- (10*Weight) + (6.25 * Height) - (5 * Age) + 5.
- create_feedback_map(MaxCals, Diary) ->
- create_feedback_map(MaxCals, maps:next(maps:iterator(Diary)), #{}).
- create_feedback_map(_, none, Acc) -> Acc;
- create_feedback_map(MaxCals, {Day, TotCals, NextIt}, Acc) when MaxCals < TotCals ->
- create_feedback_map(MaxCals, NextIt, Acc#{Day => {bad, TotCals}});
- create_feedback_map(MaxCals, {Day, TotCals, NextIt}, Acc) ->
- create_feedback_map(MaxCals, NextIt, Acc#{Day => {good, TotCals}}).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement