Commits

Jordi Chacón committed 3cf0446

Added some statistics to each user: amount of matches, wins and defeats. These statistics are updated after each match along with the score and they are shown in the ranking table

  • Participants
  • Parent commits de9d92d

Comments (0)

Files changed (5)

priv/docroot/js/tt.js

   var directive = {
     'tr.scoreentry' : {
       'r<-ranking' : {
-	'span.name'  : 'r.nick',
-        'span.score' : 'r.score'
+	'span.name'   : 'r.nick',
+        'span.score'  : 'r.score',
+	'span.matches': 'r.matches',
+	'span.wins'   : 'r.wins',
+	'span.losses' : 'r.losses'
       }
     }
   };
   var template = '<tr class="scoreentry">\
 	    <td><span class="name"></span></td>\
 	    <td><span class="score"></span></td>\
+	    <td><span class="matches"></span></td>\
+  	    <td><span class="wins"></span></td>\
+  	    <td><span class="losses"></span></td>\
   </tr>';
   $('#scoredata').html(template);
   $('#ranking').render(x.data, directive);
 		    <tr>\
 		      <th><span class="name">Name</span></td>\
 		      <th><span class="score">Score</span></td>\
+		      <th><span class="matches">Matches</span></td>\
+	      	      <th><span class="wins">Wins</span></td>\
+	      	      <th><span class="losses">Defeats</span></td>\
 		    </tr>\
 		  </thead>\
 		  <tbody id="scoredata">\

src/tt_couchdb.erl

 ranking_map() ->
     "function(doc) {"
 	"if(doc.type == 'user')"
-	"emit(doc.score, {nick:doc.nick, score:doc.score});}".
+	"emit(doc.score, {nick:doc.nick, score:doc.score, matches:doc.matches,"
+	"wins:doc.wins, losses:doc.losses});}".
 
 
 %%

src/tt_matches_resource.erl

     {WScore, LScore} = tt_scoring:gen_new_scores(
 			 proplists:get_value("score",Winner),
 			 proplists:get_value("score",Looser)),
-    update_user_score(Winner, WScore),
-    update_user_score(Looser, LScore).
+    update_user_score(Winner, WScore, winner),
+    update_user_score(Looser, LScore, looser).
 
-update_user_score(User, NewScore) ->
-    User2 = [{"score", NewScore} | proplists:delete("score", User)],
-    tt_couchdb:update_user(User2).
+update_user_score(User, NewScore, Result) ->
+    NewMatches = proplists:get_value("matches", User) + 1,
+    User2 = proplists:delete("score", User),
+    User3 = proplists:delete("matches", User2),
+    {Tuple, User4} = 
+	case Result of
+	    winner ->
+		NewWins = proplists:get_value("wins", User3) + 1,
+		{{"wins", NewWins}, proplists:delete("wins", User3)};
+	    looser ->
+		NewLosses = proplists:get_value("losses", User3) + 1,
+		{{"losses", NewLosses}, proplists:delete("losses", User3)}
+	end,
+    User5 = [{"score", NewScore}, {"matches", NewMatches}, Tuple | User4],
+    tt_couchdb:update_user(User5).

src/tt_scoring.erl

 
 gen_new_scores(W, L) ->
     {NW, NL} = v1(W, L),
-    [NW2] = io_lib:format("~.3f", [NW]),
-    [NL2] = io_lib:format("~.3f", [NL]),
-    {list_to_float(NW2), list_to_float(NL2)}.
+    {format(NW), format(NL)}.
 
 v1(W, L) when W > L -> 
     {W + min(1, (1 / (W - L))), L - min(0.7, (1 / ((W - L) * 2)))};
 	false -> B
     end.
 	    
+format(Score) ->
+    case is_integer(Score) of
+	true -> 
+	    Score;
+	false -> 
+	    [Score2] = io_lib:format("~.3f", [Score]),
+	    list_to_float(Score2)
+    end.

src/tt_users_resource.erl

         no_exists ->
 	    tt_couchdb:store_doc([{"type",<<"user">>},
 				  {"nick",list_to_binary(Nick)},
-				  {"score",0}]),
+				  {"score",0}, {"matches", 0},
+				  {"wins", 0}, {"losses", 0}]),
             {true, ReqData, Context};
         _ ->
 	    {{error, "already_exists"}, ReqData, Context}