-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrunsheet_laps.py
More file actions
83 lines (62 loc) · 3.02 KB
/
Copy pathrunsheet_laps.py
File metadata and controls
83 lines (62 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import asyncio
import hhdm_apiclient_wrapper as hh
async def main():
api_key = 'YOUR_API_KEY'
auth_settings = hh.AuthenticationSettings(
api_key=api_key,
authentication_mode=hh.AuthenticationMode.API_KEY,
)
client = hh.ApiClient(authentication_manager=hh.AuthenticationManager(auth_settings))
print(f'Api client wrapper v{client.get_version()}. Running lap average computation demo.')
result = await client.get_all_accounts()
if not result.success:
print(f'Failed to get account information: {result.message}')
return
account = get_named_input(result.return_value, 'Enter the number of the account to run the demo on: ', lambda x: x['Name'])
account_id = account['Id']
championships_result = await client.get_all_championships(account_id, hh.ApiGetOptions([
'*',
'Events.*',
'Events.Sessions.*',
'Events.Cars.*',
'Events.Cars.Car.*'
]))
if not championships_result.success:
print(f'Failed to get championship information: {championships_result.message}')
return
championship = get_named_input(championships_result.return_value, 'Select a championship: ')
event = get_named_input(championship['Events'], 'Select an event: ')
session = get_named_input(event['Sessions'], 'Select a session: ')
session_id = session['Id']
car = get_named_input(event['Cars'], 'Select a car: ', lambda x: x['Parameters']['Car']['Parameters']['Number'])
car_id = car['Parameters']['CarId']
print(f'Account: {account_id}')
print(f'Session: {session_id}')
print(f'Car: {car_id}')
runsheets_result = await client.get_all_run_sheets_for_session_car(account_id, session_id, car_id, hh.ApiGetOptions(['RunName','Laps.LapTime']))
if not runsheets_result.success:
print(f'Failed to get runsheets: {result.message}')
return
runsheet = get_named_input(runsheets_result.return_value, 'Select a runsheet: ', lambda x: x['Parameters']['RunName'])
laptimes = [l['Parameters']['LapTime'] for l in runsheet['Laps'] if l['Parameters']['LapTime']]
avg_lap = sum(laptimes) / len(laptimes) if len(laptimes) > 0 else 0
print(f"Average laptime for \"{runsheet['Parameters']['RunName']}\" with {len(laptimes)} laps: {avg_lap} s")
print('\nUpdating runsheet AverageLapTimeCalculated...')
update_result = await client.update_run_sheet(account_id, runsheet['Id'], hh.UpdateModel(None,[
hh.ParameterUpdateModel('AverageLapTimeCalculated', str(avg_lap))
]))
if update_result:
print('Success.')
else:
print('Failed.')
await client.close()
def get_named_input(items, prompt, name_accessor=lambda x: x['Parameters']['Name']):
if len(items) == 0:
print('No entities were found.')
return None
print('\n'.join([f"{i+1}) {name_accessor(item)}" for (i, item) in enumerate(items)]))
idx = int(input(prompt)) - 1
print(f'You selected "{name_accessor(items[idx])}"\n')
return items[idx]
if __name__ == '__main__':
asyncio.run(main())