from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
-from xonstat.util import strip_colors, html_colors
+from xonstat.util import strip_colors, html_colors, pretty_date
DBSession = scoped_session(sessionmaker())
Base = declarative_base()
return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt,
self.game_type_cd, self.server_id)
+ def fuzzy_date(self):
+ return pretty_date(self.start_dt)
+
class PlayerGameStat(object):
def __init__(self, create_dt=None):
% endif
-##### RECENT GAMES #####
+##### RECENT GAMES (v2) ####
% if recent_games:
<h2>Recent Games</h2>
-% for (gamestat, game, server, map) in recent_games:
- <a href="${request.route_url("game_info", id=game.game_id)}" name="Game info page for game #${game.game_id}">#${game.game_id}:</a> <a href="${request.route_url("map_info", id=map.map_id)}" name="Map info page for ${map.name}">${map.name}</a> on <a href="${request.route_url("server_info", id=server.server_id)}" name="Server info page for ${server.name}">${server.name}</a>
-<br />
-% endfor
-<a href="${request.route_url("player_game_index", player_id=player.player_id, page=1)}" title="Game index for ${player.nick}">More games</a> played by ${player.nick_html_colors()}...
-% endif
-
-##### RECENT GAMES (v2) ####
<table class="accuracy-table" border="1" cellpadding="3" align="center">
-<tr>
+<tr class='accuracy-table-header'>
<td>Game Type</td>
<td>Map</td>
<td>Result</td>
<td>Played</td>
+ <td>Permalink</td>
</tr>
% for (gamestat, game, server, map) in recent_games:
<tr>
Loss
% endif
</td>
- <td>${game.start_dt}</td>
+ <td>${game.fuzzy_date()}</td>
+ <td><a href="${request.route_url("game_info", id=game.game_id)}" name="Game info page for game #${game.game_id}">View</a></td>
</tr>
% endfor
+</table>
+<a href="${request.route_url("player_game_index", player_id=player.player_id, page=1)}" title="Game index for ${player.nick}">More games</a> played by ${player.nick_html_colors()}...
+% endif
import re
+from datetime import datetime
def strip_colors(str=None):
str = re.sub(r'\^x\w\w\w', '', str)
str = re.sub(r'\^\d', '', str)
return str
+
def html_colors(str=None):
orig = str
str = re.sub(r'\^x(\w)(\w)(\w)',
return str
+
def page_url(page):
return current_route_url(request, page=page, _query=request.GET)
+
+
+def pretty_date(time=False):
+ """
+ Get a datetime object or a int() Epoch timestamp and return a
+ pretty string like 'an hour ago', 'Yesterday', '3 months ago',
+ 'just now', etc
+ """
+ now = datetime.now()
+ if type(time) is int:
+ diff = now - datetime.fromtimestamp(time)
+ elif isinstance(time,datetime):
+ diff = now - time
+ elif not time:
+ diff = now - now
+ second_diff = diff.seconds
+ day_diff = diff.days
+
+ if day_diff < 0:
+ return ''
+
+ if day_diff == 0:
+ if second_diff < 10:
+ return "just now"
+ if second_diff < 60:
+ return str(second_diff) + " seconds ago"
+ if second_diff < 120:
+ return "a minute ago"
+ if second_diff < 3600:
+ return str( second_diff / 60 ) + " minutes ago"
+ if second_diff < 7200:
+ return "an hour ago"
+ if second_diff < 86400:
+ return str( second_diff / 3600 ) + " hours ago"
+ if day_diff == 1:
+ return "Yesterday"
+ if day_diff < 7:
+ return str(day_diff) + " days ago"
+ if day_diff < 31:
+ if day_diff/7 == 1:
+ return "a week ago"
+ else:
+ return str(day_diff/7) + " weeks ago"
+ if day_diff < 365:
+ if day_diff/30 == 1:
+ return "a month ago"
+ else:
+ return str(day_diff/30) + " months ago"
+ else:
+ if day_diff/365 == 1:
+ return "a year ago"
+ else:
+ return str(day_diff/365) + " years ago"