import re
from datetime import datetime
-# Map of special chars to ascii from Quake's console.c.
-qfont_table = [
+# Map of special chars to ascii from Darkplace's console.c.
+_qfont_table = [
'\0', '#', '#', '#', '#', '.', '#', '#',
'#', '\t', '\n', '#', ' ', '\r', '.', '.',
'[', ']', '0', '1', '2', '3', '4', '5',
'x', 'y', 'z', '{', '|', '}', '~', '<'
]
+# 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'>"
+]
+
+# Color code patterns
+_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])')
+
def qfont_decode(qstr=''):
+ """ Convert the qfont characters in a string to ascii.
"""
- Convert the qfont characters in a string to ascii.
- """
- if qstr is None:
+ if qstr == None:
qstr = ''
-
chars = []
for c in qstr:
if c >= u'\ue000' and c <= u'\ue0ff':
- c = qfont_table[ord(c) - 0xe000]
+ c = _qfont_table[ord(c) - 0xe000]
chars.append(c)
return ''.join(chars)
-def strip_colors(str=''):
- if str is None:
- str = ''
-
- str = re.sub(r'\^x\w\w\w', '', str)
- str = re.sub(r'\^\d', '', str)
- return str
-
-
-def html_colors(str=''):
- if str is None:
- str = ''
-
- orig = str
-
- # "downsample" the given UTF-8 characters to ASCII
- str = qfont_decode(str)
-
- str = re.sub(r'\^x(\w)(\w)(\w)',
- "<span style='color:#\g<1>\g<1>\g<2>\g<2>\g<3>\g<3>'>", str)
- str = re.sub(r'\^1', "<span style='color:#FF9900'>", str)
- str = re.sub(r'\^2', "<span style='color:#33FF00'>", str)
- str = re.sub(r'\^3', "<span style='color:#FFFF00'>", str)
- str = re.sub(r'\^4', "<span style='color:#3366FF'>", str)
- str = re.sub(r'\^5', "<span style='color:#33FFFF'>", str)
- str = re.sub(r'\^6', "<span style='color:#FF3366'>", str)
- str = re.sub(r'\^7', "<span style='color:#FFFFFF'>", str)
- str = re.sub(r'\^8', "<span style='color:#999999'>", str)
- str = re.sub(r'\^9', "<span style='color:#666666'>", str)
- str = re.sub(r'\^0', "<span style='color:#333333'>", str)
+def strip_colors(qstr=''):
+ return _all_colors.sub('', qstr)
- for span in range(len(re.findall(r'\^x\w\w\w|\^\d', orig))):
- str += "</span>"
- return str
+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)
+ return html + "</span>" * len(_all_colors.findall(qstr))
def page_url(page):
# all games have a score\r
pgstat.score = 0\r
\r
- if game.game_type_cd == 'dm':\r
+ if game.game_type_cd == 'dm' or game.game_type_cd == 'tdm' or game.game_type_cd == 'duel':\r
pgstat.kills = 0\r
pgstat.deaths = 0\r
pgstat.suicides = 0\r
if pgstat.nick == None:\r
pgstat.nick = player.nick\r
\r
+ # whichever nick we ended up with, strip it and store as the stripped_nick\r
+ pgstat.stripped_nick = qfont_decode(pgstat.nick)\r
+\r
# if the nick we end up with is different from the one in the\r
# player record, change the nick to reflect the new value\r
if pgstat.nick != player.nick and player.player_id > 2:\r
log.debug("ERROR: Not enough real players")\r
raise pyramid.httpexceptions.HTTPOk("OK")\r
\r
+ # FIXME: if we have two players and game type is 'dm',\r
+ # change this into a 'duel' gametype. This should be\r
+ # removed when the stats actually send 'duel' instead of 'dm'\r
+ if len(players) == 2 and game_meta['G'] == 'dm':\r
+ game_meta['G'] = 'duel'\r
+\r
server = get_or_create_server(session=session, hashkey=idfp, \r
name=game_meta['S'], revision=game_meta['R'],\r
ip_addr=get_remote_addr(request))\r