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)
@jesuslg123
Copy link
Author

jesuslg123 commented Apr 20, 2025

  "Heat": {
    "table": "heat",
    "attributes": [
      "id", // Primary key
      "name", // User-facing name
      "auto_name", // Auto-generated name
      "class_id", // Foreign key to race_class
      "results", // Internal use only
      "_cache_status", // Internal use only
      "order", // Not yet implemented
      "status", // Current status (PLANNED, PROJECTED, CONFIRMED)
      "auto_frequency", // Auto-assign pilot seats
      "group_id", // Group identifier
      "active", // Not yet implemented
      "coop_best_time", // Best time in co-op mode
      "coop_num_laps" // Best number of laps in co-op mode
    ]
  },
  "HeatNode": {
    "table": "heat_node",
    "attributes": [
      "id", // Primary key
      "heat_id", // Foreign key to heat
      "node_index", // Slot number
      "pilot_id", // Foreign key to pilot
      "color", // Hexadecimal color
      "method", // Assignment method (NONE, ASSIGN, HEAT_RESULT, CLASS_RESULT)
      "seed_rank", // Rank value for seeding
      "seed_id" // ID of heat or class used for seeding
    ]
  },
  "RaceClass": {
    "table": "race_class",
    "attributes": [
      "id", // Primary key
      "name", // User-facing name
      "description", // Long description
      "format_id", // Foreign key to race_format
      "win_condition", // Ranking algorithm
      "results", // Internal use only
      "_cache_status", // Internal use only
      "ranking", // Calculated ranking
      "rank_settings", // JSON-serialized ranking arguments
      "_rank_status", // Internal use only
      "rounds", // Planned rounds
      "heat_advance_type", // Method for advancing heats
      "round_type", // Method for round groupings
      "order", // Not yet implemented
      "active" // Not yet implemented
    ]
  },
  "SavedRaceMeta": {
    "table": "saved_race_meta",
    "attributes": [
      "id", // Primary key
      "round_id", // Round identifier
      "heat_id", // Foreign key to heat
      "class_id", // Foreign key to race_class
      "format_id", // Foreign key to race_format
      "start_time", // Start time
      "start_time_formatted", // Formatted start time
      "_cache_status" // Internal use only
    ]
  },
  "SavedPilotRace": {
    "table": "saved_pilot_race",
    "attributes": [
      "id", // Primary key
      "race_id", // Foreign key to saved_race_meta
      "node_index", // Slot number
      "pilot_id", // Foreign key to pilot
      "history_values", // JSON-serialized RSSI data
      "history_times", // JSON-serialized timestamps
      "penalty_time", // Penalty time
      "penalty_desc", // Penalty description
      "enter_at", // Gate enter calibration point
      "exit_at", // Gate exit calibration point
      "frequency" // Frequency used
    ]
  },
  "SavedRaceLap": {
    "table": "saved_race_lap",
    "attributes": [
      "id", // Primary key
      "race_id", // Foreign key to saved_race_meta
      "pilotrace_id", // Foreign key to saved_pilot_race
      "node_index", // Slot number
      "pilot_id", // Foreign key to pilot
      "lap_time_stamp", // Timestamp of lap
      "lap_time", // Lap time
      "lap_time_formatted", // Formatted lap time
      "source", // Source of lap
      "deleted" // Deletion status
    ]
  },
  "Pilot": {
    "table": "pilot",
    "attributes": [
      "id", // Primary key
      "name", // Pilot name
      "callsign", // Pilot callsign
      "team", // Team name
      "phonetic", // Phonetic name
      "color", // Hexadecimal color
      "used_frequencies" // JSON-serialized frequencies
    ]
  },
  "RaceFormat": {
    "table": "race_format",
    "attributes": [
      "id", // Primary key
      "name", // Format name
      "unlimited_time", // Unlimited time flag
      "race_time_sec", // Race time in seconds
      "lap_grace_sec", // Grace period for laps
      "staging_fixed_tones", // Fixed tones for staging
      "start_delay_min_ms", // Minimum start delay in milliseconds
      "start_delay_max_ms", // Maximum start delay in milliseconds
      "staging_delay_tones", // Delay tones for staging
      "number_laps_win", // Number of laps to win
      "win_condition", // Win condition
      "team_racing_mode", // Team racing mode
      "points_method" // Points calculation method
    ]
  },
  "GlobalSettings": {
    "table": "global_settings",
    "attributes": [
      "id", // Primary key
      "option_name", // Option name
      "option_value" // Option value
    ]
  },
  "Attributes": {
    "HeatAttribute": {
      "table": "heat_attribute",
      "attributes": [
        "id", // Foreign key to heat
        "name", // Attribute name
        "value" // Attribute value
      ]
    },
    "RaceClassAttribute": {
      "table": "race_class_attribute",
      "attributes": [
        "id", // Foreign key to race_class
        "name", // Attribute name
        "value" // Attribute value
      ]
    },
    "SavedRaceMetaAttribute": {
      "table": "saved_race_meta_attribute",
      "attributes": [
        "id", // Foreign key to saved_race_meta
        "name", // Attribute name
        "value" // Attribute value
      ]
    },
    "RaceFormatAttribute": {
      "table": "race_format_attribute",
      "attributes": [
        "id", // Foreign key to race_format
        "name", // Attribute name
        "value" // Attribute value
      ]
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment