2022-02-06 00:12:32 +00:00
|
|
|
import datetime
|
|
|
|
import csv
|
|
|
|
import re
|
|
|
|
from rich.console import Console
|
|
|
|
from rich.table import Table
|
|
|
|
from rich.traceback import install
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
install(show_locals=True)
|
|
|
|
class runner:
|
|
|
|
def __init__(self, first, last, club, country, card, o_class, controls, splits):
|
|
|
|
self.first = first
|
|
|
|
self.last = last
|
|
|
|
self.club = club
|
|
|
|
self.country = country
|
|
|
|
self.card = card
|
|
|
|
self.o_class = o_class
|
|
|
|
self.controls = controls
|
2022-02-14 06:41:31 +00:00
|
|
|
self.splits = splits
|
|
|
|
self.id = 0
|
|
|
|
self.s_time = 0
|
|
|
|
self.f_time = 0
|
|
|
|
|
2022-02-06 00:12:32 +00:00
|
|
|
def fullname(self):
|
|
|
|
return '{} {}'.format(self.first, self.last)
|
2022-02-08 20:56:44 +00:00
|
|
|
def check_codes(self):
|
2022-02-10 19:42:01 +00:00
|
|
|
# Returns False if not ok and touple if ok
|
2022-02-08 20:56:44 +00:00
|
|
|
return contains(self.o_class.course.codes, self.controls)
|
|
|
|
def totaltime(self):
|
|
|
|
return self.splits[-2]
|
2022-02-10 19:42:01 +00:00
|
|
|
|
|
|
|
def status(self):
|
|
|
|
if self.controls == []:
|
|
|
|
return 'Active'
|
|
|
|
elif self.check_codes():
|
|
|
|
return 'OK'
|
|
|
|
elif self.check_codes() == False:
|
|
|
|
return 'Disqualified'
|
2022-02-14 06:41:31 +00:00
|
|
|
def rank(self, allrunners):
|
|
|
|
c_ranked = rank_runners(allrunners, self.o_class)
|
|
|
|
return c_ranked.index(self) + 1
|
2022-02-14 18:03:13 +00:00
|
|
|
def res_splits(self):
|
2022-02-17 20:17:14 +00:00
|
|
|
control_map = self.check_codes()
|
|
|
|
splits = []
|
|
|
|
lastn = 0
|
|
|
|
for n in control_map:
|
|
|
|
if n != lastn+1:
|
|
|
|
splits.append(self.splits[n])
|
|
|
|
else:
|
|
|
|
# Dette må testes ordentlig seinere!
|
|
|
|
gap = n - lastn
|
|
|
|
split = 0
|
|
|
|
for i in range(gap):
|
|
|
|
split += self.splits[n-gap]
|
|
|
|
splits.append(split)
|
|
|
|
return splits
|
2022-02-06 00:12:32 +00:00
|
|
|
class course:
|
|
|
|
def __init__(self, name, codes):
|
|
|
|
self.name = name
|
|
|
|
self.codes = codes
|
|
|
|
|
|
|
|
class o_class:
|
|
|
|
def __init__(self, name, course):
|
|
|
|
self.name = name
|
|
|
|
self.course = course
|
|
|
|
|
|
|
|
def courses_from_ttime_conf(ttime_file = 'sc_2021_ttime/ttime.cnf.txt'):
|
|
|
|
courses = []
|
|
|
|
conf = open(ttime_file).readlines()
|
|
|
|
for line in conf:
|
|
|
|
if '-codes' in line:
|
|
|
|
code_list = re.search(r'(?<=\")(.*?)(?=\")', line).group().split(';')
|
|
|
|
loops = 0
|
|
|
|
for n in code_list:
|
|
|
|
n = n.split(',')
|
|
|
|
loops += 1
|
2022-02-08 17:05:11 +00:00
|
|
|
n = list(map(int, n))
|
2022-02-06 00:12:32 +00:00
|
|
|
courses.append(course('course_'+str(loops), n))
|
|
|
|
return courses
|
|
|
|
def classes_from_ttime_conf(ttime_file, courses):
|
|
|
|
o_classes = []
|
|
|
|
conf = open(ttime_file).readlines()
|
|
|
|
for line in conf:
|
|
|
|
if '-courses' in line:
|
|
|
|
raw_courselist = re.search(r'(?<=\")(.*?)(?=\")', line).group().split(';')
|
|
|
|
loops = 0
|
|
|
|
for n in raw_courselist:
|
|
|
|
n = n.split(',')
|
|
|
|
for i in n:
|
|
|
|
o_classes.append(o_class(i,courses[loops]))
|
|
|
|
loops += 1
|
|
|
|
|
|
|
|
|
|
|
|
return o_classes
|
|
|
|
|
2022-02-08 17:05:11 +00:00
|
|
|
def xml_to_class(xml_file, o_class_list=[]):
|
2022-02-06 00:12:32 +00:00
|
|
|
tree = ET.parse(xml_file)
|
|
|
|
root = tree.getroot()
|
|
|
|
runnerarray = []
|
|
|
|
|
|
|
|
for person_root in root[1:]:
|
|
|
|
first = person_root[1][1][1].text
|
|
|
|
last = person_root[1][1][0].text
|
|
|
|
try:
|
|
|
|
club = person_root[2][1].text
|
|
|
|
except:
|
|
|
|
club = "None"
|
|
|
|
country = person_root[1][3].text
|
|
|
|
try:
|
|
|
|
card = int(person_root[3].text)
|
|
|
|
except:
|
|
|
|
card = 0
|
|
|
|
try:
|
2022-02-08 17:05:11 +00:00
|
|
|
xml_class_str = person_root[4][1].text
|
2022-02-06 00:12:32 +00:00
|
|
|
except:
|
2022-02-08 17:05:11 +00:00
|
|
|
# VELDIG MIDLERTIDIG
|
|
|
|
runner_o_class = o_class_list[0]
|
|
|
|
else:
|
|
|
|
for i in o_class_list:
|
|
|
|
if i.name == xml_class_str:
|
|
|
|
runner_o_class = i
|
|
|
|
break
|
|
|
|
# Gjør sånn at den lager nye o klasser om den ikke finnes fra før
|
|
|
|
runnerarray.append(runner(first, last, club, country, card, runner_o_class, [], []))
|
2022-02-06 00:12:32 +00:00
|
|
|
return runnerarray
|
2022-02-09 20:44:13 +00:00
|
|
|
def ttime_db_to_class(ttime_file, o_class_list=[]):
|
|
|
|
csvreader = csv.reader(open(ttime_file, 'r', encoding='latin_1'), delimiter=';',)
|
|
|
|
runnerarray = []
|
|
|
|
for row in csvreader:
|
|
|
|
country = ''
|
|
|
|
name = row[2].split(',')
|
|
|
|
first = name[1]
|
|
|
|
last = name[0]
|
|
|
|
try:
|
|
|
|
club = row[4]
|
|
|
|
except:
|
|
|
|
club = "None"
|
|
|
|
try:
|
|
|
|
card = int(row[6])
|
|
|
|
except:
|
|
|
|
card = 0
|
|
|
|
try:
|
|
|
|
raw_class_str = row[3]
|
|
|
|
except:
|
|
|
|
# VELDIG MIDLERTIDIG
|
|
|
|
runner_o_class = o_class_list[0]
|
|
|
|
else:
|
|
|
|
for i in o_class_list:
|
|
|
|
if i.name == raw_class_str:
|
|
|
|
runner_o_class = i
|
|
|
|
break
|
|
|
|
# TODO: Gjør sånn at den lager nye o klasser om den ikke finnes fra før
|
|
|
|
runnerarray.append(runner(first, last, club, country, card, runner_o_class, [], []))
|
|
|
|
return runnerarray
|
|
|
|
|
2022-02-06 00:12:32 +00:00
|
|
|
def ttime_mtr_to_class(csv_file, runnerarray):
|
|
|
|
csvreader = csv.reader(open(csv_file))
|
|
|
|
fields = next(csvreader)
|
|
|
|
|
|
|
|
fields = []
|
|
|
|
rows = []
|
|
|
|
# hver rad er brikkenummer med tilhørende info
|
|
|
|
for row in csvreader:
|
|
|
|
rows.append(row)
|
|
|
|
for row in rows:
|
|
|
|
controls = []
|
|
|
|
splits = []
|
|
|
|
# postkodene kommer på oddetall fra og med den 11. De blir hevet inn i controls
|
|
|
|
for item in row[11::2]:
|
|
|
|
if item != '000' and item != '0000' and item != '00000':
|
|
|
|
controls.append(int(item))
|
|
|
|
# strekktidene kommer på partall fra og med den 12. De blir hevet i splits.
|
|
|
|
for item in row[12::2]:
|
|
|
|
if item != '000' and item != '0000' and item != '00000':
|
|
|
|
splits.append(int(item))
|
2022-02-14 06:41:31 +00:00
|
|
|
# looper gjonnom løperobjektene og legger til poster og strekktider + start og sluttid
|
2022-02-06 00:12:32 +00:00
|
|
|
for runner in runnerarray:
|
|
|
|
if runner.card == int(row[6]):
|
|
|
|
runner.controls = controls
|
|
|
|
runner.splits = splits
|
2022-02-08 20:56:44 +00:00
|
|
|
|
2022-02-14 06:41:31 +00:00
|
|
|
# usikker på om dette er riktig klokeslett
|
|
|
|
if len(runner.splits) > 2:
|
|
|
|
tl = row[5].split(' ')
|
|
|
|
tl[0] = tl[0].split('.')
|
|
|
|
tl[0][2] = '20' + tl[0][2]
|
|
|
|
tl[0] = list(map(int, tl[0]))
|
|
|
|
tl[1] = tl[1].split(':')
|
|
|
|
tl[1][2] = float(tl[1][2])
|
|
|
|
tl[1] = list(map(int, tl[1]))
|
|
|
|
f_time = datetime.datetime(tl[0][2], tl[0][1], tl[0][0], tl[1][0], tl[1][1], tl[1][2])
|
|
|
|
s_time = f_time - datetime.timedelta(seconds = runner.totaltime())
|
|
|
|
runner.f_time = f_time
|
|
|
|
runner.s_time = s_time
|
|
|
|
|
2022-02-08 20:56:44 +00:00
|
|
|
def contains(small, big):
|
2022-02-17 20:17:14 +00:00
|
|
|
valid = True
|
|
|
|
mark = 0
|
|
|
|
map_bl = []
|
|
|
|
for i in small:
|
|
|
|
for n,control in enumerate(big[mark:]):
|
|
|
|
if i == control:
|
|
|
|
mark += n
|
|
|
|
map_bl.append(mark)
|
2022-02-08 20:56:44 +00:00
|
|
|
break
|
|
|
|
else:
|
2022-02-17 20:17:14 +00:00
|
|
|
valid = False
|
|
|
|
if valid:
|
|
|
|
return map_bl
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2022-02-14 06:41:31 +00:00
|
|
|
def get_runners_in_class(runners, o_class):
|
2022-02-10 19:42:01 +00:00
|
|
|
list_filtrd = []
|
|
|
|
for i in runners:
|
2022-02-17 20:17:14 +00:00
|
|
|
marker = 0
|
2022-02-10 19:42:01 +00:00
|
|
|
if i.o_class == o_class:
|
|
|
|
list_filtrd.append(i)
|
|
|
|
|
|
|
|
return list_filtrd
|
|
|
|
|
2022-02-14 06:41:31 +00:00
|
|
|
def rank_runners(allrunners, o_class):
|
|
|
|
runners = get_runners_in_class(allrunners, o_class)
|
|
|
|
runners_ranked = []
|
|
|
|
for i in runners:
|
|
|
|
if i.status() == 'OK':
|
|
|
|
runners_ranked.append(i)
|
|
|
|
return runners_ranked
|
|
|
|
|
2022-02-18 10:01:55 +00:00
|
|
|
def xml_child(parent, tag, content):
|
|
|
|
e = ET.SubElement(parent, tag)
|
|
|
|
e.text = str(content)
|
|
|
|
|
2022-02-10 19:42:01 +00:00
|
|
|
def gen_xml_result(runners, o_classes):
|
|
|
|
root = ET.Element('ResultList')
|
|
|
|
tree = ET.ElementTree(root)
|
|
|
|
event = ET.SubElement(root, 'Event')
|
2022-02-18 10:01:55 +00:00
|
|
|
xml_child(event, 'Name', 'PLACEHOLDER')
|
2022-02-10 19:42:01 +00:00
|
|
|
for i in o_classes:
|
|
|
|
#<ClassResult>
|
2022-02-14 06:41:31 +00:00
|
|
|
class_result = ET.SubElement(root, 'ClassResult')
|
2022-02-10 19:42:01 +00:00
|
|
|
#<Class>
|
|
|
|
t = ET.SubElement(class_result, 'Class')
|
2022-02-18 10:01:55 +00:00
|
|
|
xml_child(t, 'Name', i.name)
|
2022-02-10 19:42:01 +00:00
|
|
|
#<PersonResult>
|
2022-02-14 18:03:13 +00:00
|
|
|
runners_same_c = get_runners_in_class(runners, i)
|
|
|
|
runners_ranked = rank_runners(runners_same_c, i)
|
|
|
|
for n in runners_same_c:
|
2022-02-10 19:42:01 +00:00
|
|
|
person_result = ET.SubElement(class_result, 'PersonResult')
|
|
|
|
#<Person>
|
|
|
|
person = ET.SubElement(person_result, 'Person')
|
2022-02-18 10:01:55 +00:00
|
|
|
xml_child(person, 'Id', '0')
|
2022-02-10 19:42:01 +00:00
|
|
|
#<Name>
|
|
|
|
name = ET.SubElement(person, 'Name')
|
2022-02-18 10:01:55 +00:00
|
|
|
xml_child(name, 'Family', n.last)
|
|
|
|
xml_child(name, 'Given', n.first)
|
2022-02-10 19:42:01 +00:00
|
|
|
#</Name>
|
|
|
|
#</Person>
|
|
|
|
#<Organisation>
|
|
|
|
org = ET.SubElement(person_result, 'Organisation')
|
2022-02-18 10:01:55 +00:00
|
|
|
xml_child(org, 'Id', '0')
|
|
|
|
xml_child(org, 'Name', n.club)
|
2022-02-10 19:42:01 +00:00
|
|
|
country = ET.SubElement(org, 'Country')
|
|
|
|
# TODO: hent land fra løperobjektet
|
|
|
|
country.text = 'Norway'
|
|
|
|
country.set('code', 'NOR')
|
|
|
|
#</Organisation>
|
|
|
|
#<Result>
|
2022-02-17 20:17:14 +00:00
|
|
|
result = ET.SubElement(person_result, 'Result')
|
2022-02-14 06:41:31 +00:00
|
|
|
if len(n.splits) > 2:
|
2022-02-18 10:01:55 +00:00
|
|
|
xml_child(result, 'StartTime', n.s_time.isoformat())
|
|
|
|
xml_child(result, 'FinishTime', n.f_time.isoformat())
|
|
|
|
xml_child(result, 'Time', n.totaltime())
|
2022-02-14 06:41:31 +00:00
|
|
|
if n.status() == 'OK':
|
2022-02-14 18:03:13 +00:00
|
|
|
#<TimeBehind>
|
2022-02-18 10:01:55 +00:00
|
|
|
xml_child(result, 'TimeBehind', n.totaltime() - runners_ranked[0].totaltime())
|
2022-02-14 18:03:13 +00:00
|
|
|
#</TimeBehind>
|
2022-02-18 10:01:55 +00:00
|
|
|
xml_child(result, 'Position', n.rank(runners))
|
|
|
|
xml_child(result, 'Status', n.status())
|
2022-02-17 20:17:14 +00:00
|
|
|
#<SplitTime>
|
|
|
|
for code,split in zip(n.o_class.course.codes, n.res_splits()):
|
|
|
|
st = ET.SubElement(result, 'SplitTime')
|
2022-02-18 10:01:55 +00:00
|
|
|
xml_child(st, 'ControlCode', code)
|
|
|
|
xml_child(st, 'Time', split)
|
2022-02-17 20:17:14 +00:00
|
|
|
#</SplitTime>
|
|
|
|
else:
|
2022-02-18 10:01:55 +00:00
|
|
|
xml_child(result, 'Status', n.status())
|
2022-02-17 20:17:14 +00:00
|
|
|
else:
|
2022-02-18 10:01:55 +00:00
|
|
|
xml_child(result, 'Status', n.status())
|
2022-02-10 19:42:01 +00:00
|
|
|
#</Result>
|
|
|
|
#</PersonResult>
|
|
|
|
#</Class>
|
2022-02-08 20:56:44 +00:00
|
|
|
|
2022-02-17 20:17:14 +00:00
|
|
|
tree.write('Resultater.xml')
|
2022-02-10 19:42:01 +00:00
|
|
|
return ET.dump(root)
|