]> de.git.xonotic.org Git - xonotic/xonstat.git/blobdiff - xonstat/util.py
Implemented some (preliminary) JSON API to retrieve player data
[xonotic/xonstat.git] / xonstat / util.py
old mode 100755 (executable)
new mode 100644 (file)
index 98c4bea..7c2692d
@@ -1,5 +1,7 @@
 import re
-from datetime import datetime
+from colorsys import rgb_to_hls, hls_to_rgb
+from cgi import escape as html_escape
+from datetime import datetime, timedelta
 
 # Map of special chars to ascii from Darkplace's console.c.
 _qfont_table = [
@@ -40,16 +42,16 @@ _qfont_table = [
 
 # Hex-colored spans for decimal color codes ^0 - ^9
 _dec_spans = [
- "<span style='color:#333333'>",
- "<span style='color:#FF9900'>",
- "<span style='color:#33FF00'>",
- "<span style='color:#FFFF00'>",
- "<span style='color:#3366FF'>",
- "<span style='color:#33FFFF'>",
- "<span style='color:#FF3366'>",
- "<span style='color:#FFFFFF'>",
- "<span style='color:#999999'>",
- "<span style='color:#666666'>"
+ "<span style='color:rgb(128,128,128)'>",
+ "<span style='color:rgb(255,0,0)'>",
+ "<span style='color:rgb(51,255,0)'>",
+ "<span style='color:rgb(255,255,0)'>",
+ "<span style='color:rgb(51,102,255)'>",
+ "<span style='color:rgb(51,255,255)'>",
+ "<span style='color:rgb(255,51,102)'>",
+ "<span style='color:rgb(255,255,255)'>",
+ "<span style='color:rgb(153,153,153)'>",
+ "<span style='color:rgb(128,128,128)'>"
 ]
 
 # Color code patterns
@@ -57,6 +59,9 @@ _all_colors = re.compile(r'\^(\d|x[\dA-Fa-f]{3})')
 _dec_colors = re.compile(r'\^(\d)')
 _hex_colors = re.compile(r'\^x([\dA-Fa-f])([\dA-Fa-f])([\dA-Fa-f])')
 
+# On a light scale of 0 (black) to 1.0 (white)
+_contrast_threshold = 0.5
+
 
 def qfont_decode(qstr=''):
     """ Convert the qfont characters in a string to ascii.
@@ -65,22 +70,41 @@ def qfont_decode(qstr=''):
         qstr = ''
     chars = []
     for c in qstr:
-        if c >= u'\ue000' and c <= u'\ue0ff':
+        if u'\ue000' <= c <= u'\ue0ff':
             c = _qfont_table[ord(c) - 0xe000]
         chars.append(c)
     return ''.join(chars)
 
 
 def strip_colors(qstr=''):
+    if qstr == None:
+        qstr = ''
     return _all_colors.sub('', qstr)
 
 
+def hex_repl(match):
+    """Convert Darkplaces hex color codes to CSS rgb.
+    Brighten colors with HSL light value less than 50%"""
+
+    # Extend hex char to 8 bits and to 0.0-1.0 scale
+    r = int(match.group(1) * 2, 16) / 255.0
+    g = int(match.group(2) * 2, 16) / 255.0
+    b = int(match.group(3) * 2, 16) / 255.0
+
+    # Check if color is too dark
+    hue, light, satur = rgb_to_hls(r, g, b)
+    if light < _contrast_threshold:
+        light = _contrast_threshold
+        r, g, b = hls_to_rgb(hue, light, satur)
+
+    # Convert back to 0-255 scale for css
+    return '<span style="color:rgb(%d,%d,%d)">' % (255 * r, 255 * g, 255 * b)
+
+
 def html_colors(qstr=''):
-    def dec_repl(match):
-        return _dec_spans[int(match.group(1))]
-    qstr = qstr.replace('^^', '^')
-    html = _dec_colors.sub(dec_repl, qstr)
-    html = _hex_colors.sub(r"<span style='color:#\1\1\2\2\3\3'>", html)
+    qstr = html_escape(qfont_decode(qstr).replace('^^', '^'))
+    html = _dec_colors.sub(lambda match: _dec_spans[int(match.group(1))], qstr)
+    html = _hex_colors.sub(hex_repl, html)
     return html + "</span>" * len(_all_colors.findall(qstr))
 
 
@@ -89,53 +113,45 @@ def page_url(page):
 
 
 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()
+    '''Returns a human-readable relative date.'''
+    now = datetime.utcnow()
     if type(time) is int:
         diff = now - datetime.fromtimestamp(time)
     elif isinstance(time,datetime):
-        diff = now - time 
+        diff = now - time
     elif not time:
+        print "not a time value"
         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"
+
+    dim = round(diff.seconds/60.0 + diff.days*1440.0)
+
+    if dim == 0:
+        return "less than a minute ago"
+    elif dim == 1:
+        return "1 minute ago"
+    elif dim >= 2 and dim <= 44:
+        return "{0} minutes ago".format(int(dim))
+    elif dim >= 45 and dim <= 89:
+        return "about 1 hour ago"
+    elif dim >= 90 and dim <= 1439:
+        return "about {0} hours ago".format(int(round(dim/60.0)))
+    elif dim >= 1440 and dim <= 2519:
+        return "1 day ago"
+    elif dim >= 2520 and dim <= 43199:
+        return "{0} days ago".format(int(round(dim/1440.0)))
+    elif dim >= 43200 and dim <= 86399:
+        return "about 1 month ago"
+    elif dim >= 86400 and dim <= 525599:
+        return "{0} months ago".format(int(round(dim/43200.0)))
+    elif dim >= 525600 and dim <= 655199:
+        return "about 1 year ago"
+    elif dim >= 655200 and dim <= 914399:
+        return "over 1 year ago"
+    elif dim >= 914400 and dim <= 1051199:
+        return "almost 2 years ago"
     else:
-        if day_diff/365 == 1:
-            return "a year ago"
-        else:
-            return str(day_diff/365) + " years ago"
+        return "about {0} years ago".format(int(round(dim/525600.0)))
+
+def datetime_seconds(td):
+    return float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
+