|
|
|
@ -5,15 +5,18 @@ import re
|
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
from fpdf import FPDF
|
|
|
|
|
|
|
|
|
|
class event:
|
|
|
|
|
|
|
|
|
|
# The event object stores all the event data.
|
|
|
|
|
# A .otime file is just a pickle dump of an event object.
|
|
|
|
|
class Event:
|
|
|
|
|
def __init__(self, eventid, name, **kwargs):
|
|
|
|
|
self.id = eventid
|
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
|
|
self.courses = []
|
|
|
|
|
self.o_classes = []
|
|
|
|
|
self.runners = []
|
|
|
|
|
self.card_punches= []
|
|
|
|
|
self.card_dumps = []
|
|
|
|
|
|
|
|
|
|
def add_course(self, *args):
|
|
|
|
|
for n in args:
|
|
|
|
|
self.courses.append(n)
|
|
|
|
@ -25,6 +28,7 @@ class event:
|
|
|
|
|
def add_runners(self, *args):
|
|
|
|
|
for n in args:
|
|
|
|
|
self.runners.append(n)
|
|
|
|
|
|
|
|
|
|
def import_ttime_cnf(self, ttime_file):
|
|
|
|
|
self.add_course(*courses_from_ttime_conf(ttime_file))
|
|
|
|
|
self.add_o_class(*classes_from_ttime_conf(ttime_file, self.courses))
|
|
|
|
@ -35,13 +39,15 @@ class event:
|
|
|
|
|
for row in csvreader:
|
|
|
|
|
if len(row) == 0 or row[1] == '':
|
|
|
|
|
continue
|
|
|
|
|
runnerarray.append(runner.from_string(row, self.o_classes))
|
|
|
|
|
runnerarray.append(Runner.from_string(row, self.o_classes))
|
|
|
|
|
self.runners = runnerarray
|
|
|
|
|
|
|
|
|
|
def import_mtr_file(self, mtr_file):
|
|
|
|
|
self.card_punches = card_punch.list_from_mtr_f(mtr_file)
|
|
|
|
|
self.card_dumps = card_punch.list_from_mtr_f(mtr_file)
|
|
|
|
|
|
|
|
|
|
def match_runners_cards(self):
|
|
|
|
|
for n in self.runners:
|
|
|
|
|
for i in self.card_punches:
|
|
|
|
|
for i in self.card_dumps:
|
|
|
|
|
if n.card == i.card:
|
|
|
|
|
n.card_r = i
|
|
|
|
|
|
|
|
|
@ -58,12 +64,12 @@ class event:
|
|
|
|
|
xml_child(event, 'Id', self.id)
|
|
|
|
|
xml_child(event, 'Name', self.name)
|
|
|
|
|
for i in self.o_classes:
|
|
|
|
|
#<ClassResult>
|
|
|
|
|
# <ClassResult>
|
|
|
|
|
class_result = ET.SubElement(root, 'ClassResult')
|
|
|
|
|
#<Class>
|
|
|
|
|
# <Class>
|
|
|
|
|
t = ET.SubElement(class_result, 'Class')
|
|
|
|
|
xml_child(t, 'Name', i.name)
|
|
|
|
|
#<PersonResult>
|
|
|
|
|
# <PersonResult>
|
|
|
|
|
runners_same_c = get_runners_in_class(self.runners, i)
|
|
|
|
|
runners_ranked = rank_runners(runners_same_c, i)
|
|
|
|
|
# Put the OK runners first and Active last
|
|
|
|
@ -72,16 +78,16 @@ class event:
|
|
|
|
|
|
|
|
|
|
for n in runners_ranked:
|
|
|
|
|
person_result = ET.SubElement(class_result, 'PersonResult')
|
|
|
|
|
#<Person>
|
|
|
|
|
# <Person>
|
|
|
|
|
person = ET.SubElement(person_result, 'Person')
|
|
|
|
|
xml_child(person, 'Id', n.id)
|
|
|
|
|
#<Name>
|
|
|
|
|
# <Name>
|
|
|
|
|
name = ET.SubElement(person, 'Name')
|
|
|
|
|
xml_child(name, 'Family', n.last)
|
|
|
|
|
xml_child(name, 'Given', n.first)
|
|
|
|
|
#</Name>
|
|
|
|
|
#</Person>
|
|
|
|
|
#<Organisation>
|
|
|
|
|
# </Name>
|
|
|
|
|
# </Person>
|
|
|
|
|
# <Organisation>
|
|
|
|
|
org = ET.SubElement(person_result, 'Organisation')
|
|
|
|
|
xml_child(org, 'Id', '0')
|
|
|
|
|
xml_child(org, 'Name', n.club)
|
|
|
|
@ -89,43 +95,43 @@ class event:
|
|
|
|
|
# TODO: hent land fra løperobjektet
|
|
|
|
|
country.text = 'Norway'
|
|
|
|
|
country.set('code', 'NOR')
|
|
|
|
|
#</Organisation>
|
|
|
|
|
#<Result>
|
|
|
|
|
# </Organisation>
|
|
|
|
|
# <Result>
|
|
|
|
|
result = ET.SubElement(person_result, 'Result')
|
|
|
|
|
if hasattr(n,'card_r') and len(n.card_r.splits) > 2:
|
|
|
|
|
if hasattr(n, 'card_r') and len(n.card_r.splits) > 2:
|
|
|
|
|
xml_child(result, 'StartTime', n.card_r.s_time.isoformat())
|
|
|
|
|
xml_child(result, 'FinishTime', n.card_r.f_time.isoformat())
|
|
|
|
|
xml_child(result, 'Time', n.totaltime())
|
|
|
|
|
if n.status() == 'OK':
|
|
|
|
|
#<TimeBehind>
|
|
|
|
|
# <TimeBehind>
|
|
|
|
|
xml_child(result, 'TimeBehind', n.totaltime() - runners_ranked[0].totaltime())
|
|
|
|
|
#</TimeBehind>
|
|
|
|
|
# </TimeBehind>
|
|
|
|
|
xml_child(result, 'Position', n.rank(self.runners))
|
|
|
|
|
xml_child(result, 'Status', n.status())
|
|
|
|
|
#<SplitTime>
|
|
|
|
|
#TODO: ta utgangspunkt i løypa, ikke det brikka har stempla
|
|
|
|
|
for code,split in zip(n.card_r.controls,n.card_r.splits):
|
|
|
|
|
# <SplitTime>
|
|
|
|
|
# TODO: ta utgangspunkt i løypa, ikke det brikka har stempla
|
|
|
|
|
for code, split in zip(n.card_r.controls, n.card_r.splits):
|
|
|
|
|
st = ET.SubElement(result, 'SplitTime')
|
|
|
|
|
xml_child(st, 'ControlCode', code)
|
|
|
|
|
xml_child(st, 'Time', split)
|
|
|
|
|
if code == n.o_class.course.codes[-1]:
|
|
|
|
|
break
|
|
|
|
|
#</SplitTime>
|
|
|
|
|
# </SplitTime>
|
|
|
|
|
elif n.status() == 'Disqualified':
|
|
|
|
|
xml_child(result, 'Status', n.status())
|
|
|
|
|
for code in n.o_class.course.codes:
|
|
|
|
|
st = ET.SubElement(result, 'SplitTime')
|
|
|
|
|
xml_child(st, 'ControlCode', code)
|
|
|
|
|
for control,split in zip(n.card_r.controls,n.card_r.splits):
|
|
|
|
|
if code == control:
|
|
|
|
|
xml_child(st, 'Time', split)
|
|
|
|
|
for control, split in zip(n.card_r.controls, n.card_r.splits):
|
|
|
|
|
if code == control:
|
|
|
|
|
xml_child(st, 'Time', split)
|
|
|
|
|
else:
|
|
|
|
|
xml_child(result, 'Status', n.status())
|
|
|
|
|
else:
|
|
|
|
|
xml_child(result, 'Status', n.status())
|
|
|
|
|
#</Result>
|
|
|
|
|
#</PersonResult>
|
|
|
|
|
#</Class>
|
|
|
|
|
# </Result>
|
|
|
|
|
# </PersonResult>
|
|
|
|
|
# </Class>
|
|
|
|
|
ET.indent(root, space=' ', level=0)
|
|
|
|
|
return tree
|
|
|
|
|
|
|
|
|
@ -147,61 +153,66 @@ class event:
|
|
|
|
|
pdf.ln(line_height)
|
|
|
|
|
pdf.output(file_name)
|
|
|
|
|
|
|
|
|
|
class runner:
|
|
|
|
|
|
|
|
|
|
# The runner object stores all the data specific to a runner.
|
|
|
|
|
class Runner:
|
|
|
|
|
def __init__(self, runner_id, first, last, club, country, card, o_class, start_time):
|
|
|
|
|
self.id = runner_id
|
|
|
|
|
self.first = first
|
|
|
|
|
self.last = last
|
|
|
|
|
self.club = club
|
|
|
|
|
self.country = country
|
|
|
|
|
self.card = card
|
|
|
|
|
self.o_class = o_class
|
|
|
|
|
self.start_time = start_time
|
|
|
|
|
self.id = runner_id
|
|
|
|
|
self.first = first
|
|
|
|
|
self.last = last
|
|
|
|
|
self.club = club
|
|
|
|
|
self.country = country
|
|
|
|
|
self.card = card
|
|
|
|
|
self.o_class = o_class
|
|
|
|
|
self.start_time = start_time
|
|
|
|
|
|
|
|
|
|
def from_string(tt_line, o_classes):
|
|
|
|
|
#https://web.archive.org/web/20191229124046/http://wiki.ttime.no/index.php/Developer
|
|
|
|
|
eventorid = tt_line[0]
|
|
|
|
|
country = ''
|
|
|
|
|
name = tt_line[2].split(',')
|
|
|
|
|
try:
|
|
|
|
|
first = name[1].strip()
|
|
|
|
|
except:
|
|
|
|
|
first = ''
|
|
|
|
|
last = name[0]
|
|
|
|
|
try:
|
|
|
|
|
club = tt_line[4]
|
|
|
|
|
except:
|
|
|
|
|
club = "None"
|
|
|
|
|
try:
|
|
|
|
|
card = int(tt_line[6])
|
|
|
|
|
except:
|
|
|
|
|
card = 0
|
|
|
|
|
#https://web.archive.org/web/20191229124046/http://wiki.ttime.no/index.php/Developer
|
|
|
|
|
eventorid = tt_line[0]
|
|
|
|
|
country = ''
|
|
|
|
|
name = tt_line[2].split(',')
|
|
|
|
|
try:
|
|
|
|
|
first = name[1].strip()
|
|
|
|
|
except:
|
|
|
|
|
first = ''
|
|
|
|
|
last = name[0]
|
|
|
|
|
try:
|
|
|
|
|
club = tt_line[4]
|
|
|
|
|
except:
|
|
|
|
|
club = "None"
|
|
|
|
|
try:
|
|
|
|
|
card = int(tt_line[6])
|
|
|
|
|
except:
|
|
|
|
|
card = 0
|
|
|
|
|
runner_o_class = None
|
|
|
|
|
try:
|
|
|
|
|
raw_class_str = tt_line[3]
|
|
|
|
|
except:
|
|
|
|
|
# VELDIG MIDLERTIDIG
|
|
|
|
|
runner_o_class = None
|
|
|
|
|
try:
|
|
|
|
|
raw_class_str = tt_line[3]
|
|
|
|
|
except:
|
|
|
|
|
# VELDIG MIDLERTIDIG
|
|
|
|
|
runner_o_class = None
|
|
|
|
|
else:
|
|
|
|
|
if raw_class_str != '':
|
|
|
|
|
for i in o_classes:
|
|
|
|
|
if i.name == raw_class_str:
|
|
|
|
|
runner_o_class = i
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
if raw_class_str != '':
|
|
|
|
|
for i in o_classes:
|
|
|
|
|
if i.name == raw_class_str:
|
|
|
|
|
runner_o_class = i
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
runner_o_class = None
|
|
|
|
|
# TODO: Gjør sånn at den lager nye o klasser om den ikke finnes fra før
|
|
|
|
|
try:
|
|
|
|
|
options = tt_line[5].split(',')
|
|
|
|
|
start_time = options[options.index('U')+1]
|
|
|
|
|
except:
|
|
|
|
|
start_time = None
|
|
|
|
|
return runner(eventorid, first, last, club, country, card, runner_o_class, start_time)
|
|
|
|
|
runner_o_class = None
|
|
|
|
|
# TODO: Gjør sånn at den lager nye o klasser om den ikke finnes fra før
|
|
|
|
|
try:
|
|
|
|
|
options = tt_line[5].split(',')
|
|
|
|
|
start_time = options[options.index('U')+1]
|
|
|
|
|
except:
|
|
|
|
|
start_time = None
|
|
|
|
|
return Runner(eventorid, first, last, club, country, card, runner_o_class, start_time)
|
|
|
|
|
|
|
|
|
|
def fullname(self):
|
|
|
|
|
return '{} {}'.format(self.first, self.last)
|
|
|
|
|
|
|
|
|
|
def check_codes(self):
|
|
|
|
|
# Returns False if not ok and touple if ok
|
|
|
|
|
return contains(self.o_class.course.codes, list(self.card_r.controls))
|
|
|
|
|
|
|
|
|
|
def totaltime(self):
|
|
|
|
|
f_control = self.o_class.course.codes[-1]
|
|
|
|
|
try:
|
|
|
|
@ -217,9 +228,11 @@ class runner:
|
|
|
|
|
return 'OK'
|
|
|
|
|
elif self.check_codes() == False:
|
|
|
|
|
return 'Disqualified'
|
|
|
|
|
|
|
|
|
|
def rank(self, allrunners):
|
|
|
|
|
c_ranked = rank_runners(allrunners, self.o_class)
|
|
|
|
|
return c_ranked.index(self) + 1
|
|
|
|
|
|
|
|
|
|
def res_splits(self):
|
|
|
|
|
splits_cpy = self.card_r.splits.copy()
|
|
|
|
|
for control in self.card_r.controls:
|
|
|
|
@ -229,7 +242,7 @@ class runner:
|
|
|
|
|
splits_cpy.remove(split)
|
|
|
|
|
return splits_cpy
|
|
|
|
|
|
|
|
|
|
class card_punch:
|
|
|
|
|
class CardDump:
|
|
|
|
|
def __init__(self, card, controls, splits, read_time, s_time, f_time):
|
|
|
|
|
self.card = card
|
|
|
|
|
self.controls = controls
|
|
|
|
@ -237,6 +250,7 @@ class card_punch:
|
|
|
|
|
self.read_time = read_time
|
|
|
|
|
self.s_time = s_time
|
|
|
|
|
self.f_time = f_time
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return f'card({self.card}) controls({self.controls}) splits({self.splits})'
|
|
|
|
|
|
|
|
|
@ -271,12 +285,12 @@ class card_punch:
|
|
|
|
|
read_time = datetime.datetime(year, month, day, hours, minutes, seconds, milliseconds)
|
|
|
|
|
if len(controls) > 2:
|
|
|
|
|
s_time = read_time - datetime.timedelta(seconds = splits[-1])
|
|
|
|
|
f_time = read_time - (datetime.timedelta(seconds = splits[-1]) + datetime.timedelta(seconds = splits[-2]))
|
|
|
|
|
f_time = read_time - (datetime.timedelta(seconds=splits[-1]) + datetime.timedelta(seconds=splits[-2]))
|
|
|
|
|
else:
|
|
|
|
|
s_time = read_time
|
|
|
|
|
f_time = read_time
|
|
|
|
|
|
|
|
|
|
return(card_punch(card, controls, splits, read_time, s_time, f_time))
|
|
|
|
|
return(CardDump(card, controls, splits, read_time, s_time, f_time))
|
|
|
|
|
|
|
|
|
|
def list_from_mtr_f(mtr_f):
|
|
|
|
|
csvreader = csv.reader(open(mtr_f))
|
|
|
|
@ -321,23 +335,27 @@ class card_punch:
|
|
|
|
|
else:
|
|
|
|
|
s_time = read_time
|
|
|
|
|
f_time = read_time
|
|
|
|
|
cards.append(card_punch(int(row[6]), controls, splits, read_time, s_time, f_time))
|
|
|
|
|
cards.append(CardDump(int(row[6]), controls, splits, read_time, s_time, f_time))
|
|
|
|
|
return cards
|
|
|
|
|
class course:
|
|
|
|
|
|
|
|
|
|
# Stored in Event.courses
|
|
|
|
|
class Course:
|
|
|
|
|
def __init__(self, name, codes):
|
|
|
|
|
self.name = name
|
|
|
|
|
self.codes = codes
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return f'name({self.name})'
|
|
|
|
|
|
|
|
|
|
class o_class:
|
|
|
|
|
# Stored in Event.o_classes
|
|
|
|
|
class OClass:
|
|
|
|
|
def __init__(self, name, course):
|
|
|
|
|
self.name = name
|
|
|
|
|
self.course = course
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return f'name({self.name})'
|
|
|
|
|
|
|
|
|
|
def courses_from_ttime_conf(ttime_file = 'sc_2021_ttime/ttime.cnf.txt'):
|
|
|
|
|
# TODO: Take string instead of file.
|
|
|
|
|
def courses_from_ttime_conf(ttime_file):
|
|
|
|
|
courses = []
|
|
|
|
|
conf = open(ttime_file, 'r', encoding='latin_1').readlines()
|
|
|
|
|
for line in conf:
|
|
|
|
@ -348,8 +366,9 @@ def courses_from_ttime_conf(ttime_file = 'sc_2021_ttime/ttime.cnf.txt'):
|
|
|
|
|
n = n.split(',')
|
|
|
|
|
loops += 1
|
|
|
|
|
n = list(map(int, n))
|
|
|
|
|
courses.append(course('course_'+str(loops), n))
|
|
|
|
|
courses.append(Course('course_'+str(loops), n))
|
|
|
|
|
return courses
|
|
|
|
|
|
|
|
|
|
def classes_from_ttime_conf(ttime_file, courses):
|
|
|
|
|
o_classes = []
|
|
|
|
|
conf = open(ttime_file, 'r', encoding='latin_1').readlines()
|
|
|
|
@ -360,16 +379,12 @@ def classes_from_ttime_conf(ttime_file, courses):
|
|
|
|
|
for n in raw_courselist:
|
|
|
|
|
split = n.split(',')
|
|
|
|
|
for i in split:
|
|
|
|
|
o_classes.append(o_class(i,courses[loops]))
|
|
|
|
|
o_classes.append(OClass(i,courses[loops]))
|
|
|
|
|
loops += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return o_classes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Checks if small list is in big list
|
|
|
|
|
def contains(small, big):
|
|
|
|
|
# Checks if small list is in big list
|
|
|
|
|
valid = True
|
|
|
|
|
mark = 0
|
|
|
|
|
map_bl = []
|
|
|
|
@ -404,7 +419,7 @@ def rank_runners(allrunners, o_class):
|
|
|
|
|
runners_ranked.sort(key=lambda x: x.totaltime())
|
|
|
|
|
return runners_ranked
|
|
|
|
|
|
|
|
|
|
# Used to make creating xml files easier
|
|
|
|
|
def xml_child(parent, tag, content):
|
|
|
|
|
# Used to make creating xml files easier
|
|
|
|
|
e = ET.SubElement(parent, tag)
|
|
|
|
|
e.text = str(content)
|
|
|
|
|