]> git.rm.cloudns.org Git - xonotic/xonstat.git/commitdiff
Basic support for batch processes.
authorAnt Zucaro <azucaro@gmail.com>
Sat, 7 Jul 2012 01:45:23 +0000 (21:45 -0400)
committerAnt Zucaro <azucaro@gmail.com>
Sat, 7 Jul 2012 01:45:23 +0000 (21:45 -0400)
Badges is the first batch process supported. It basically
sets up the pyramid environment, then iterates over the
player base and generates static HTML files showing their
high-level stats. That static HTML is then converted into
a PNG for use in forum signatures. A bash wrapper is
needed to drive the whole process via cron.

xonstat/batch/badges/css/style.css [new file with mode: 0644]
xonstat/batch/badges/gen_badges.py [new file with mode: 0644]
xonstat/batch/badges/img/dark_wall.png [new file with mode: 0644]
xonstat/batch/badges/templates/badge.html [new file with mode: 0644]
xonstat/batch/badges/templates/badge.mako [new file with mode: 0644]

diff --git a/xonstat/batch/badges/css/style.css b/xonstat/batch/badges/css/style.css
new file mode 100644 (file)
index 0000000..1d7627e
--- /dev/null
@@ -0,0 +1,39 @@
+body {
+  color: #ccc;
+  font-family: monospace;
+  font-size: 12px;
+}
+#badge {
+  background: url('../img/dark_wall.png');
+  height: 60px;
+  padding: 5px;
+  position: fixed;
+  width: 550px;
+}
+#nick {
+  font-size: 22px;
+  font-weight: bold;
+  height: 28px;
+  left: 10px;
+  overflow: hidden;
+  position: absolute;
+  width: 285px;
+}
+#games_played {
+  left: 10px;
+  position: absolute;
+  top: 45px;
+}
+#win_percentage, #kill_ratio, #elo {
+  left: 300px;
+  position: absolute;
+}
+#win_percentage {
+  top: 5px;
+}
+#kill_ratio {
+  top: 25px;
+}
+#elo {
+  top: 45px;
+}
diff --git a/xonstat/batch/badges/gen_badges.py b/xonstat/batch/badges/gen_badges.py
new file mode 100644 (file)
index 0000000..f6e02f4
--- /dev/null
@@ -0,0 +1,36 @@
+from mako.template import Template
+from os import system
+from pyramid.paster import bootstrap
+from xonstat.models import *
+from xonstat.views.player import player_info_data
+
+# converter params
+CONVERTER = "/home/ant/xonotic/xonstat/xonstat/batch/badges/wkhtmltoimage-amd64 --crop-x 10 --crop-y 10 --width 560 --height 70 -f png"
+
+# environment setup
+env = bootstrap('../../../development.ini.home')
+req = env['request']
+req.matchdict = {'id':3}
+
+# template setup
+t = Template(filename = 'templates/badge.mako')
+
+players = DBSession.query(Player).filter(Player.player_id < 50).\
+        filter(Player.player_id == PlayerElo.player_id).\
+        filter(Player.nick != None).\
+        filter(Player.player_id > 2).\
+        filter(Player.active_ind == True).all()
+
+for player in players:
+    req.matchdict['id'] = player.player_id
+    data = player_info_data(req)
+    rt = t.render(player=data['player'], elos_display=data['elos_display'],
+            total_stats=data['total_stats'], total_games=data['total_games'],
+            games_breakdown=data['games_breakdown'])
+    f = open("output/%s.html" % player.player_id, 'w')
+    f.write(rt)
+    f.close()
+
+    cmd = "%s %s %s" % (CONVERTER, "output/%s.html" % player.player_id,
+            "output/%s.png" % player.player_id)
+    system(cmd)
diff --git a/xonstat/batch/badges/img/dark_wall.png b/xonstat/batch/badges/img/dark_wall.png
new file mode 100644 (file)
index 0000000..50a2f64
Binary files /dev/null and b/xonstat/batch/badges/img/dark_wall.png differ
diff --git a/xonstat/batch/badges/templates/badge.html b/xonstat/batch/badges/templates/badge.html
new file mode 100644 (file)
index 0000000..9c6e11f
--- /dev/null
@@ -0,0 +1,31 @@
+<html>
+  <head>
+    <link href="../css/style.css" rel="stylesheet">
+  </head>
+  <body>
+    <div id="badge">
+      <div id="nick">
+        <b>
+        <span style="color:rgb(0,255,0)">print('<span style="color:rgb(255,0,0)">Anti<span style="color:rgb(127,127,127)">body<span style="color:rgb(0,255,0)">')<span style='color:rgb(255,255,255)'></span></span></span></span></span>
+      </b>
+      </div>
+
+      <div id="games_played">
+        218 games (144 duel, 41 ctf, 31 dm, 2 tdm)
+      </div>
+
+      <div id="win_percentage">
+        111 wins, 107 losses (50.92%)
+      </div>
+
+      <div id="kill_ratio">
+        3084 kills, 2519 deaths (1.224)
+      </div>
+
+      <div id="elo">
+        Elo: 375.091 (ctf), 358.604 (dm)
+      </div>
+    </div>
+
+  </body>
+</html>
diff --git a/xonstat/batch/badges/templates/badge.mako b/xonstat/batch/badges/templates/badge.mako
new file mode 100644 (file)
index 0000000..e43089e
--- /dev/null
@@ -0,0 +1,39 @@
+<html>
+  <head>
+    <link href="../css/style.css" rel="stylesheet">
+  </head>
+  <body>
+    <div id="badge">
+      <div id="nick">
+        <b>
+          ${player.nick_html_colors()|n}
+        </b>
+      </div>
+
+      <div id="games_played">
+        <% games_breakdown_str = ', '.join(["{0} {1}".format(ng, gt) for (gt, ng) in games_breakdown]) %>
+        ${total_games} (${games_breakdown_str})
+      </div>
+
+      <div id="win_percentage">
+        % if total_games > 0 and total_stats['wins'] is not None:
+          ${total_stats['wins']} wins, ${total_games - total_stats['wins']} losses (${round(float(total_stats['wins'])/total_games * 100, 2)}%)
+        % endif
+      </div>
+
+      <div id="kill_ratio">
+        % if total_stats['kills'] > 0 and total_stats['deaths'] > 0:
+          ${total_stats['kills']} kills, ${total_stats['deaths']} deaths (${round(float(total_stats['kills'])/total_stats['deaths'], 3)})
+        % endif
+      </div>
+
+      <div id="elo">
+        % if elos_display is not None and len(elos_display) > 0:
+         Elo: ${', '.join(elos_display[0:2])}
+       % endif
+      </div>
+
+    </div>
+
+  </body>
+</html>