Merge branch 'master' of github.com:antzucaro/XonStat
authorAnt Zucaro <azucaro@gmail.com>
Sat, 16 Jun 2012 13:53:08 +0000 (09:53 -0400)
committerAnt Zucaro <azucaro@gmail.com>
Sat, 16 Jun 2012 13:53:08 +0000 (09:53 -0400)
Conflicts:
xonstat/views/__init__.py
xonstat/views/player.py

xonstat/__init__.py
xonstat/templates/player_info.mako
xonstat/views/__init__.py
xonstat/views/player.py

index 2e45a0853d4d47e1815a28f8c1e3a6a37ed44476..3160b7583c2cba36fd2ab88e5b4168ddd952d36d 100644 (file)
@@ -41,6 +41,10 @@ def main(global_config, **settings):
     config.add_view(player_accuracy_json, route_name="player_accuracy",      renderer="json")
     config.add_view(player_accuracy_json, route_name="player_accuracy_json", renderer="json")
 
+    config.add_route("player_damage", "/player/{id:\d+}/damage")
+    config.add_view(player_damage_json, route_name="player_damage",
+        renderer="json")
+
     # GAME ROUTES
     config.add_route("game_index", "/games")
     config.add_view(game_index, route_name="game_index", renderer="game_index.mako")
index 8216f7554ecd000e0bee6600f80233628bbacf0b..24ed9056b4fbc77d9bfb97c88e598bf2338b37d2 100644 (file)
@@ -13,6 +13,7 @@ ${nav.nav('players')}
       <script type="text/javascript">
       $(function () {
 
+          // plot the accuracy graph
           function plot_acc_graph(data) {
               var games = new Array();
               var avgs = new Array();
@@ -23,7 +24,7 @@ ${nav.nav('players')}
                   avgs[i] = [i, data.avg];
                   accs[i] = [i, data.accs[i][1]];
                   game_link = '/game/' + data.accs[i][0];
-                  j = 20 - i;
+                  j = data.games - i;
                   games[i] = [i, '<a href="' + game_link + '">' + j + '</a>'];
               }
 
@@ -37,6 +38,31 @@ ${nav.nav('players')}
               });
           }
 
+          // plot the damage graph
+          function plot_dmg_graph(data) {
+              var games = new Array();
+              var avgs = new Array();
+              var dmgs = new Array();
+
+              var i=0;
+              for(i=0; i < data.games; i++) {
+                  avgs[i] = [i, data.avg];
+                  dmgs[i] = [i, data.dmgs[i][1]];
+                  game_link = '/game/' + data.dmgs[i][0];
+                  j = data.games - i;
+                  games[i] = [i, '<a href="' + game_link + '">' + j + '</a>'];
+              }
+
+              $.plot(
+                  $("#dmg-graph"), 
+                  [ { label: 'average', data: avgs, hoverable: true, clickable: false }, 
+                    { label: 'efficiency', data: dmgs, lines: {show:true}, points: {show:false}, hoverable: true, clickable: true }, ],
+                  { yaxis: {ticks: 10, min: 0 },
+                    xaxis: {ticks: games},
+                    grid: { hoverable: true, clickable: true },
+              });
+          }
+
           function showTooltip(x, y, contents) {
             $('<div id="tooltip">' + contents + '</div>').css( {
                 position: 'absolute',
@@ -55,11 +81,11 @@ ${nav.nav('players')}
               if (item) {
                   if (previousPoint != item.dataIndex) {
                     previousPoint = item.dataIndex;
-                    
+
                     $("#tooltip").remove();
                     var x = item.datapoint[0].toFixed(2),
                         y = item.datapoint[1].toFixed(2);
-                    
+
                     showTooltip(item.pageX, item.pageY, y + "%");
                   }
               }
@@ -69,17 +95,29 @@ ${nav.nav('players')}
               }
           });
 
-          $.ajax({
-              url: '${request.route_url("player_accuracy", id=player.player_id)}',
-              method: 'GET',
-              dataType: 'json',
-              success: plot_acc_graph
+          $('#dmg-graph').bind("plothover", function (event, pos, item) {
+              if (item) {
+                  if (previousPoint != item.dataIndex) {
+                    previousPoint = item.dataIndex;
+
+                    $("#tooltip").remove();
+                    var x = item.datapoint[0].toFixed(2),
+                        y = item.datapoint[1].toFixed(2);
+
+                    showTooltip(item.pageX, item.pageY, y);
+                  }
+              }
+              else {
+                  $("#tooltip").remove();
+                  previousPoint = null;
+              }
           });
 
+          // bind click events to the weapon images
           $(".acc-weap").click(function () {
               var dataurl = $(this).find('a').attr('href');
 
-              $('.weapon-active').removeClass('weapon-active');
+              $('.accuracy-nav').find('.weapon-active').removeClass('weapon-active');
               $(this).addClass('weapon-active');
 
               $.ajax({
@@ -89,6 +127,37 @@ ${nav.nav('players')}
                   success: plot_acc_graph
               });
           });
+
+          $(".dmg-weap").click(function () {
+              var dataurl = $(this).find('a').attr('href');
+
+              $('.damage-nav').find('.weapon-active').removeClass('weapon-active');
+              $(this).addClass('weapon-active');
+
+              $.ajax({
+                  url: dataurl,
+                  method: 'GET',
+                  dataType: 'json',
+                  success: plot_dmg_graph
+              });
+          });
+
+          // populate the graphs with the default weapons
+          $.ajax({
+              url: '${request.route_url("player_accuracy", id=player.player_id)}',
+              method: 'GET',
+              dataType: 'json',
+              success: plot_acc_graph
+          });
+
+          $.ajax({
+              url: '${request.route_url("player_damage", id=player.player_id)}',
+              method: 'GET',
+              dataType: 'json',
+              success: plot_dmg_graph
+          });
+
+
       })
       </script>
     % endif
@@ -132,14 +201,14 @@ Player Information
 </div>
 
 
-% if accs is not None:
+% if 'nex' in recent_weapons or 'rifle' in recent_weapons or 'minstanex' in recent_weapons or 'uzi' in recent_weapons or 'shotgun' in recent_weapons:
 <div class="row">
   <div class="span10">
     <h3>Accuracy</h3>
     <div id="acc-graph" class="flot" style="width:800px; height:200px;">
     </div>
 
-    <div class="weapon-nav">
+    <div class="weapon-nav accuracy-nav">
       <ul>
         % if 'nex' in recent_weapons:
         <li>
@@ -198,6 +267,83 @@ Player Information
 % endif
 
 
+% if 'rocketlauncher' in recent_weapons or 'grenadelauncher' in recent_weapons or 'electro' in recent_weapons or 'crylink' in recent_weapons or 'laser' in recent_weapons:
+<div class="row">
+  <div class="span10">
+    <h3>Damage Efficiency</h3>
+    <div id="dmg-graph" class="flot" style="width:800px; height:200px;">
+    </div>
+
+    <div class="weapon-nav damage-nav">
+      <ul>
+        % if 'rocketlauncher' in recent_weapons:
+        <li>
+          <div class="dmg-weap weapon-active">
+            <img src="${request.static_url("xonstat:static/images/rocketlauncher.png")}" />
+            <p><small>Rocket</small></p>
+            <a href="${request.route_url('player_damage', id=player.player_id, _query={'weapon':'rocketlauncher'})}" title="Show rocket launcher efficiency"></a>
+          </div>
+        </li>
+        % endif
+
+        % if 'grenadelauncher' in recent_weapons:
+        <li>
+          <div class="dmg-weap">
+            <img src="${request.static_url("xonstat:static/images/grenadelauncher.png")}" />
+            <p><small>Mortar</small></p>
+            <a href="${request.route_url('player_damage', id=player.player_id, _query={'weapon':'grenadelauncher'})}" title="Show mortar damage efficiency"></a>
+          </div>
+        </li>
+        % endif
+
+        % if 'electro' in recent_weapons:
+        <li>
+          <div class="dmg-weap">
+            <img src="${request.static_url("xonstat:static/images/electro.png")}" />
+            <p><small>Electro</small></p>
+            <a href="${request.route_url('player_damage', id=player.player_id, _query={'weapon':'electro'})}" title="Show electro damage efficiency"></a>
+          </div>
+        </li>
+        % endif
+
+        % if 'crylink' in recent_weapons:
+        <li>
+          <div class="dmg-weap">
+            <img src="${request.static_url("xonstat:static/images/crylink.png")}" />
+            <p><small>Crylink</small></p>
+            <a href="${request.route_url('player_damage', id=player.player_id, _query={'weapon':'crylink'})}" title="Show crylink damage efficiency"></a>
+          </div>
+        </li>
+        % endif
+
+        % if 'hagar' in recent_weapons:
+        <li>
+          <div class="dmg-weap">
+            <img src="${request.static_url("xonstat:static/images/hagar.png")}" />
+            <p><small>Hagar</small></p>
+            <a href="${request.route_url('player_damage', id=player.player_id, _query={'weapon':'hagar'})}" title="Show hagar damage efficiency"></a>
+          </div>
+        </li>
+        % endif
+
+        % if 'laser' in recent_weapons:
+        <li>
+          <div class="dmg-weap">
+            <img src="${request.static_url("xonstat:static/images/laser.png")}" />
+            <p><small>Laser</small></p>
+            <a href="${request.route_url('player_damage', id=player.player_id, _query={'weapon':'laser'})}" title="Show laser damage efficiency"></a>
+          </div>
+        </li>
+        % endif
+
+      </ul>
+    </div>
+
+  </div>
+</div>
+% endif
+
+
 ##### RECENT GAMES (v2) ####
 % if recent_games:
 <div class="row">
index e7f6e389dfa1c32cf43bba4da96e9437b129a815..525d2f9d353357a1edf22b543962e4aa33866c07 100644 (file)
@@ -1,9 +1,8 @@
 from xonstat.views.submission import stats_submit
 from xonstat.views.player import player_index, player_info, player_game_index
-from xonstat.views.player import player_accuracy_json
-from xonstat.views.game   import game_index, game_info, rank_index
-from xonstat.views.map    import map_info, map_index
-from xonstat.views.map    import map_index_json
+from xonstat.views.player import player_accuracy_json, player_damage_json
+from xonstat.views.game import game_index, game_info, rank_index
+from xonstat.views.map import map_info, map_index, map_index_json
 from xonstat.views.server import server_info, server_game_index, server_index
 from xonstat.views.search import search_q, search
-from xonstat.views.main   import main_index
+from xonstat.views.main import main_index
index 02e3cd3360a9b5437f29c470e0f03d615ae0b2c5..fe322a78f8767c220009370d5be14a7f4834c5d7 100644 (file)
@@ -147,6 +147,48 @@ def get_accuracy_stats(player_id, weapon_cd, games):
     return (avg, accs)
 
 
+def get_damage_stats(player_id, weapon_cd, games):
+    """
+    Provides damage info for weapon_cd by player_id for the past N games.
+    """
+    try:
+        raw_avg = DBSession.query(func.sum(PlayerWeaponStat.actual),
+                func.sum(PlayerWeaponStat.hit)).\
+                filter(PlayerWeaponStat.player_id == player_id).\
+                filter(PlayerWeaponStat.weapon_cd == weapon_cd).\
+                one()
+
+        avg = round(float(raw_avg[0])/raw_avg[1], 2)
+
+        # Determine the damage efficiency (hit, fired) numbers for $games games
+        # This is then enumerated to create parameters for a flot graph
+        raw_dmgs = DBSession.query(PlayerWeaponStat.game_id, 
+            PlayerWeaponStat.actual, PlayerWeaponStat.hit).\
+                filter(PlayerWeaponStat.player_id == player_id).\
+                filter(PlayerWeaponStat.weapon_cd == weapon_cd).\
+                order_by(PlayerWeaponStat.game_id.desc()).\
+                limit(games).\
+                all()
+
+        # they come out in opposite order, so flip them in the right direction
+        raw_dmgs.reverse()
+
+        dmgs = []
+        for i in range(len(raw_dmgs)):
+            # try to derive, unless we've hit nothing then set to 0!
+            try:
+                dmg = round(float(raw_dmgs[i][1])/raw_dmgs[i][2], 2)
+            except:
+                dmg = 0.0
+
+            dmgs.append((raw_dmgs[i][0], dmg))
+    except Exception as e:
+        dmgs = []
+        avg = 0.0
+
+    return (avg, dmgs)
+
+
 def _player_info_data(request):
     player_id = int(request.matchdict['id'])
     if player_id <= 2:
@@ -314,3 +356,54 @@ def player_accuracy_json(request):
        games = over how many games to display accuracy. Can be up to 50.
     """
     return _player_accuracy_data(request)
+
+
+def _player_damage_data(request):
+    player_id = request.matchdict['id']
+    allowed_weapons = ['grenadelauncher', 'electro', 'crylink', 'hagar',
+            'rocketlauncher', 'laser']
+    weapon_cd = 'laser'
+    games = 20
+
+    if request.params.has_key('weapon'):
+        if request.params['weapon'] in allowed_weapons:
+            weapon_cd = request.params['weapon']
+
+    if request.params.has_key('games'):
+        try:
+            games = request.params['games']
+
+            if games < 0:
+                games = 20
+            if games > 50:
+                games = 50
+        except:
+            games = 20
+
+    (avg, dmgs) = get_damage_stats(player_id, weapon_cd, games)
+
+    # if we don't have enough data for the given weapon
+    if len(dmgs) < games:
+        games = len(dmgs)
+
+    return {
+            'player_id':player_id, 
+            'player_url':request.route_url('player_info', id=player_id), 
+            'weapon':weapon_cd, 
+            'games':games, 
+            'avg':avg, 
+            'dmgs':dmgs
+            }
+
+
+def player_damage_json(request):
+    """
+    Provides a JSON response representing the damage for the given weapon.
+
+    Parameters:
+       weapon = which weapon to display damage for. Valid values are
+         'grenadelauncher', 'electro', 'crylink', 'hagar', 'rocketlauncher',
+         'laser'.
+       games = over how many games to display damage. Can be up to 50.
+    """
+    return _player_damage_data(request)