Commits

Jordi Chacón committed 24ec494

Partially implemented the update of the scores of the winner and the looser after a game is played

Comments (0)

Files changed (3)

src/tt_data_resource.erl

     D = {obj,[{ranking, tt_couchdb:users()}]},
     tt_utils:json_return_object(<<"Ranking">>,D);
 data("match", ReqData) ->
-    ?dbg("match: ~p~n",
-         [ [wrq:get_qs_value(Key,ReqData) || Key <- ["winner","looser","figures"]]]),
-    register_scores(ReqData).
-
-register_scores(ReqData) ->
     Emsg = case validate_figures(wrq:get_qs_value("figures",ReqData)) of
                ok -> 
-                   store_scores(ReqData),
+                   store_match(ReqData),
                    <<"Registration succeeded!">>;
 
                {error, Msg} when is_binary(Msg) -> 
 validate_figures(_) ->
     ok.
 
-store_scores(ReqData) ->
+store_match(ReqData) ->
     L = [{Key,list_to_binary(wrq:get_qs_value(Key,ReqData))} 
          || Key <- ["winner","looser","figures"]],
+    Winner = wrq:get_qs_value("winner", ReqData),
+    Looser = wrq:get_qs_value("looser", ReqData),
+    update_score_wl(Winner, Looser, tt_couchdb:users()),
     tt_couchdb:store_doc([{"type",<<"match">>}|L]).
+
+update_score_wl(Winner, Looser, Users) ->
+    {WScore, LScore} = new_scores(Winner, Looser, Users, {0, 0}),
+    io:format("~p ~p~n", [WScore, LScore]).
+
+new_scores(Winner, Looser, [{obj, [{"nick", Winner}, {"score", WS}]}|T],
+	   {_, LS}) ->
+    new_scores(Winner, Looser, T, {WS, LS});
+new_scores(Winner, Looser, [{obj, [{"nick", Looser}, {"score", LS}]}|T],
+	   {WS, _}) ->
+    new_scores(Winner, Looser, T, {WS, LS});
+new_scores(Winner, Looser, [_|T], {WS, LS}) ->
+    new_scores(Winner, Looser, T, {WS, LS});
+new_scores(_Winner, _Looser, [], {WS, LS}) ->
+    tt_scoring:v1(WS, LS).

src/tt_resource.erl

     <div id=\"nav\">
       <a href=\"#\" id=\"nranking\" class=\"nav_link\">Ranking</a>
       <span class=\"nav_sep\">|</span>
-      <a href=\"#\" id=\"nmatch\" class=\"nav_link\">Match</a>
+      <a href=\"#\" id=\"nscores\" class=\"nav_link\">Matches</a>
       <span class=\"nav_sep\">|</span>
-      <a href=\"#\" id=\"nscores\" class=\"nav_link\">Scores</a>
+      <a href=\"#\" id=\"nmatch\" class=\"nav_link\">New match</a>
       <span class=\"nav_sep\">|</span>
       <a href=\"#\" id=\"nsign\" class=\"nav_link\">Sign up</a>
       <span class=\"nav_sep\">|</span>

src/tt_scoring.erl

 
 v1(W,L) when W>L -> {W+(1/(W-L)), L-(1/((W-L)*2))};
 v1(W,L) when W<L -> {W+log10((L-W)*3), L-log10(L-W)};
-v1(W,L)          -> {W, L}.
-
+v1(W,L)          -> {W+0.1, L-0.1}.