SHARE
TWEET

Untitled

a guest Dec 17th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -module(server).
  2. -compile(export_all).
  3.  
  4. %%% Client API functions
  5. new_user(Data) ->
  6.     whereis(tserver) ! {new_user, self(), Data},
  7.     receive
  8.         {user_added, Ref} ->
  9.             io:format("Ref is: ~p~n", [Ref]), Ref
  10.     end.
  11.  
  12. %% async function
  13. give_state() ->
  14.     whereis(tserver) ! state.
  15.  
  16. %% async function
  17. add_calories(Reference, Day, Calories) ->
  18.     whereis(tserver) ! {add, self(), Reference, Day, Calories}.
  19.    
  20. get_feedback(Ref) ->
  21.     whereis(tserver) ! {feedback, self(), Ref},
  22.     receive
  23.         {Note, FeedbackMap} -> {Note, FeedbackMap}
  24.     after 5000 ->
  25.         req_expired
  26.     end.
  27.  
  28. %% async function
  29. update_user(Ref, Data) ->
  30.     whereis(tserver) ! {update, self(), Ref, Data}.
  31.  
  32.  
  33.  
  34.  
  35.  
  36. %%% Server functions
  37. start() ->
  38.     Pid = spawn(?MODULE, init, []),
  39.     register(tserver, Pid).
  40.    
  41. stop() ->
  42.     whereis(tserver) ! stop.
  43.    
  44. init() ->
  45.     loop(#{}).
  46.  
  47. loop(State) ->
  48.     receive
  49.         stop ->
  50.             exit(shutdown);
  51.            
  52.         state ->
  53.             io:format("Current state: ~n~p~n", [State]);
  54.            
  55.         {new_user, ClientPid, {Name, Weight, Height, Age, Sex, WeekWorkout}} ->
  56.             Ref = erlang:make_ref(),
  57.             MaxCalories = calc_max_cals(Weight, Height, Age, Sex, WeekWorkout),
  58.             ClientPid ! {user_added, Ref},
  59.             loop(State#{Ref => {Name, MaxCalories, #{}}});
  60.            
  61.         {add, ClientPid, Reference, Day, Calories} ->
  62.             case maps:is_key(Reference, State) of
  63.                 false ->
  64.                     io:format("Unauthorised request"),
  65.                     loop(State);
  66.                 true ->
  67.                     {Name, MaxCalories, Diary} = maps:get(Reference, State),
  68.                     case maps:is_key(Day, Diary) of
  69.                         false ->
  70.                             UpdatedDiary = maps:put(Day, Calories, Diary),
  71.                             UpdatedState = maps:update(Reference, {Name, MaxCalories, UpdatedDiary}, State),
  72.                             loop(UpdatedState);
  73.                         true ->
  74.                             CurrCals = maps:get(Day, Diary),
  75.                             UpdatedCals = CurrCals + Calories,
  76.                             UpdatedDiary = maps:update(Day, UpdatedCals, Diary),
  77.                             UpdatedState = maps:update(Reference, {Name, MaxCalories, UpdatedDiary}, State),
  78.                             loop(UpdatedState)
  79.                     end
  80.             end;
  81.            
  82.         {feedback, ClientPid, Ref} ->
  83.             case maps:is_key(Ref, State) of
  84.                 true ->
  85.                     {_Name, MaxCalories, Diary} = maps:get(Ref, State),
  86.                     FeedbackMap = create_feedback_map(MaxCalories, Diary),
  87.                     io:format("Feedback map: ~p~n", [FeedbackMap]),
  88.                     ClientPid ! {"Consider you should eat " ++ float_to_list(MaxCalories), FeedbackMap},
  89.                     loop(State);
  90.                 false ->
  91.                     ClientPid ! "no data could be found",
  92.                     loop(State)
  93.             end;
  94.            
  95.         {update, ClientPid, Ref, {Name, Weight, Height, Age, Sex, WeekWorkout}} ->
  96.             case maps:is_key(Ref, State) of
  97.                 true ->
  98.                     {_, _, Diary} = maps:get(Ref, State),
  99.                     UpdatedMaxCals = calc_max_cals(Weight, Height, Age, Sex, WeekWorkout),
  100.                     UpdatedState = maps:update(Ref, {Name, UpdatedMaxCals, Diary}, State),
  101.                     loop(UpdatedState);
  102.                 false ->
  103.                     loop(State)
  104.             end    
  105.     end.
  106.    
  107.    
  108.    
  109. %%% Private server functions
  110. calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) when WeekWorkout =:= 0 ->
  111.     bmr(Weight, Height, Age, Sex) * 1.2;
  112. calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) when WeekWorkout =:= 1 orelse WeekWorkout =:= 2 ->
  113.     bmr(Weight, Height, Age, Sex) * 1.375;
  114. calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) when WeekWorkout >= 3 andalso WeekWorkout =< 5 ->
  115.     bmr(Weight, Height, Age, Sex) * 1.55;
  116. calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) when WeekWorkout =:= 6 orelse WeekWorkout =:= 7 ->
  117.     bmr(Weight, Height, Age, Sex) * 1.725;
  118. calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) -> 0.0.   
  119.    
  120. bmr(Weight,Height,Age,f)->
  121.     ((10*Weight) + (6.25 * Height) - (5 * Age) - 161);
  122. bmr(Weight,Height,Age,m)->
  123.     (10*Weight) + (6.25 * Height) - (5 * Age) + 5.
  124.    
  125.    
  126. create_feedback_map(MaxCals, Diary) ->
  127.     create_feedback_map(MaxCals, maps:next(maps:iterator(Diary)), #{}).
  128.  
  129. create_feedback_map(_, none, Acc) -> Acc;
  130. create_feedback_map(MaxCals, {Day, TotCals, NextIt}, Acc) when MaxCals < TotCals ->
  131.     create_feedback_map(MaxCals, NextIt, Acc#{Day => {bad, TotCals}});
  132. create_feedback_map(MaxCals, {Day, TotCals, NextIt}, Acc) ->
  133.     create_feedback_map(MaxCals, NextIt, Acc#{Day => {good, TotCals}}).
  134.    
  135.    
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.    
  149.     -module(server).
  150. -compile(export_all).
  151.  
  152. %%% Client API functions
  153. new_user(Data) ->
  154.     whereis(tserver) ! {new_user, self(), Data},
  155.     receive
  156.         {user_added, Ref} ->
  157.             io:format("Ref is: ~p~n", [Ref]), Ref
  158.     end.
  159.  
  160. %% async function
  161. give_state() ->
  162.     whereis(tserver) ! state.
  163.  
  164. %% async function
  165. add_calories(Reference, Day, Calories) ->
  166.     whereis(tserver) ! {add, self(), Reference, Day, Calories}.
  167.    
  168. get_feedback(Ref) ->
  169.     whereis(tserver) ! {feedback, self(), Ref},
  170.     receive
  171.         {Note, FeedbackMap} -> {Note, FeedbackMap}
  172.     after 5000 ->
  173.         req_expired
  174.     end.
  175.  
  176. %% async function
  177. update_user(Ref, Data) ->
  178.     whereis(tserver) ! {update, self(), Ref, Data}.
  179.  
  180.  
  181.  
  182.  
  183.  
  184. %%% Server functions
  185. start() ->
  186.     Pid = spawn(?MODULE, init, []),
  187.     register(tserver, Pid).
  188.    
  189. stop() ->
  190.     whereis(tserver) ! stop.
  191.    
  192. init() ->
  193.     loop(#{}).
  194.  
  195. loop(State) ->
  196.     receive
  197.         stop ->
  198.             exit(shutdown);
  199.            
  200.         state ->
  201.             io:format("Current state: ~n~p~n", [State]);
  202.            
  203.         {new_user, ClientPid, {Name, Weight, Height, Age, Sex, WeekWorkout}} ->
  204.             Ref = erlang:make_ref(),
  205.             MaxCalories = calc_max_cals(Weight, Height, Age, Sex, WeekWorkout),
  206.             ClientPid ! {user_added, Ref},
  207.             loop(State#{Ref => {Name, MaxCalories, #{}}});
  208.            
  209.         {add, ClientPid, Reference, Day, Calories} ->
  210.             case maps:is_key(Reference, State) of
  211.                 false ->
  212.                     io:format("Unauthorised request"),
  213.                     loop(State);
  214.                 true ->
  215.                     {Name, MaxCalories, Diary} = maps:get(Reference, State),
  216.                     case maps:is_key(Day, Diary) of
  217.                         false ->
  218.                             UpdatedDiary = maps:put(Day, Calories, Diary),
  219.                             UpdatedState = maps:update(Reference, {Name, MaxCalories, UpdatedDiary}, State),
  220.                             loop(UpdatedState);
  221.                         true ->
  222.                             CurrCals = maps:get(Day, Diary),
  223.                             UpdatedCals = CurrCals + Calories,
  224.                             UpdatedDiary = maps:update(Day, UpdatedCals, Diary),
  225.                             UpdatedState = maps:update(Reference, {Name, MaxCalories, UpdatedDiary}, State),
  226.                             loop(UpdatedState)
  227.                     end
  228.             end;
  229.            
  230.         {feedback, ClientPid, Ref} ->
  231.             case maps:is_key(Ref, State) of
  232.                 true ->
  233.                     {_Name, MaxCalories, Diary} = maps:get(Ref, State),
  234.                     FeedbackMap = create_feedback_map(MaxCalories, Diary),
  235.                     io:format("Feedback map: ~p~n", [FeedbackMap]),
  236.                     ClientPid ! {"Consider you should eat " ++ float_to_list(MaxCalories), FeedbackMap},
  237.                     loop(State);
  238.                 false ->
  239.                     ClientPid ! "no data could be found",
  240.                     loop(State)
  241.             end;
  242.            
  243.         {update, ClientPid, Ref, {Name, Weight, Height, Age, Sex, WeekWorkout}} ->
  244.             case maps:is_key(Ref, State) of
  245.                 true ->
  246.                     {_, _, Diary} = maps:get(Ref, State),
  247.                     UpdatedMaxCals = calc_max_cals(Weight, Height, Age, Sex, WeekWorkout),
  248.                     UpdatedState = maps:update(Ref, {Name, UpdatedMaxCals, Diary}, State),
  249.                     loop(UpdatedState);
  250.                 false ->
  251.                     loop(State)
  252.             end    
  253.     end.
  254.    
  255.    
  256.    
  257. %%% Private server functions
  258. calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) when WeekWorkout =:= 0 ->
  259.     bmr(Weight, Height, Age, Sex) * 1.2;
  260. calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) when WeekWorkout =:= 1 orelse WeekWorkout =:= 2 ->
  261.     bmr(Weight, Height, Age, Sex) * 1.375;
  262. calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) when WeekWorkout >= 3 andalso WeekWorkout =< 5 ->
  263.     bmr(Weight, Height, Age, Sex) * 1.55;
  264. calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) when WeekWorkout =:= 6 orelse WeekWorkout =:= 7 ->
  265.     bmr(Weight, Height, Age, Sex) * 1.725;
  266. calc_max_cals(Weight, Height, Age, Sex, WeekWorkout) -> 0.0.   
  267.    
  268. bmr(Weight,Height,Age,f)->
  269.     ((10*Weight) + (6.25 * Height) - (5 * Age) - 161);
  270. bmr(Weight,Height,Age,m)->
  271.     (10*Weight) + (6.25 * Height) - (5 * Age) + 5.
  272.    
  273.    
  274. create_feedback_map(MaxCals, Diary) ->
  275.     create_feedback_map(MaxCals, maps:next(maps:iterator(Diary)), #{}).
  276.  
  277. create_feedback_map(_, none, Acc) -> Acc;
  278. create_feedback_map(MaxCals, {Day, TotCals, NextIt}, Acc) when MaxCals < TotCals ->
  279.     create_feedback_map(MaxCals, NextIt, Acc#{Day => {bad, TotCals}});
  280. create_feedback_map(MaxCals, {Day, TotCals, NextIt}, Acc) ->
  281.     create_feedback_map(MaxCals, NextIt, Acc#{Day => {good, TotCals}}).
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top