Skip to content

Instantly share code, notes, and snippets.

@jesuslg123
Created July 2, 2024 20:47
Show Gist options
  • Save jesuslg123/dd1f4a168d8d400358705f6f9d7603c3 to your computer and use it in GitHub Desktop.
Save jesuslg123/dd1f4a168d8d400358705f6f9d7603c3 to your computer and use it in GitHub Desktop.
RotorHazard class ranking plugin for order heat + fast lap for last of the heat and first of the next one
''' Class ranking method: By ..... '''
import logging
import RHUtils
from eventmanager import Evt
from RHRace import StartBehavior
from Results import RaceClassRankMethod
logger = logging.getLogger(__name__)
def rank_heat_pos_and_time(rhapi, race_class, _args):
heats = rhapi.db.heats_by_class(race_class.id)
logger.info(race_class.name)
leaderboard = []
ranked_pilots = []
rank_pos = 1
for heat in reversed(heats):
heat_result = rhapi.db.heat_results(heat)
logger.info(heat_result)
if heat_result:
heat_leaderboard = heat_result[heat_result['meta']['primary_leaderboard']]
for line in heat_leaderboard:
if line['pilot_id'] not in ranked_pilots:
leaderboard.append({
'pilot_id': line['pilot_id'],
'callsign': line['callsign'],
'team_name': line['team_name'],
'heat': heat.display_name,
'heat_rank': line['position'],
'position': rank_pos,
'fastest_lap': line['fastest_lap'] # Assuming this field exists
})
ranked_pilots.append(line['pilot_id'])
rank_pos += 1
# Swap 3rd and 4th, 6th and 7th, etc., if necessary
i = 2
while i < len(leaderboard) - 1:
if leaderboard[i + 1]['fastest_lap'] < leaderboard[i]['fastest_lap']:
# Swap the 3rd and 4th pilot
leaderboard[i], leaderboard[i + 1] = leaderboard[i + 1], leaderboard[i]
i += 3
# Update the position based on the new order
for idx, pilot in enumerate(leaderboard):
pilot['position'] = idx + 1
meta = {
'rank_fields': [{
'name': 'heat',
'label': "Heat"
},{
'name': 'heat_rank',
'label': "Position"
}]
}
return leaderboard, meta
def register_handlers(args):
args['register_fn'](
RaceClassRankMethod(
"Last Heat Position and lap time",
rank_heat_pos_and_time,
None,
None
)
)
def initialize(rhapi):
rhapi.events.on(Evt.CLASS_RANK_INITIALIZE, register_handlers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment