GeneralDuke

Hellmann Update

Dec 11th, 2020
140
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from sys import exit, stdin
  2. from typing import List
  3.  
  4.  
  5. class Update:
  6.     station: str
  7.     value: int
  8.  
  9.     def __init__(self, _station: str, _value: int):
  10.         self.station = _station
  11.         self.value = _value
  12.  
  13.  
  14. class Station:
  15.     name: str
  16.     rank: int
  17.     new_rank: int
  18.     score: int
  19.     gain: int
  20.  
  21.     def __init__(self, _name: str, _rank: int, _score: int):
  22.         self.name = _name
  23.         self.rank = _rank
  24.         self.score = _score
  25.         self.gain = 0
  26.  
  27.     def __lt__(self, other):
  28.         if self.score == other.score:
  29.             return self.name > other.name
  30.         else:
  31.             return self.score < other.score
  32.  
  33.  
  34. class Ranking:
  35.     stations: List[Station] = []
  36.  
  37.     def update_values(self, new_values: List[Update]):
  38.         names = [station.name for station in self.stations]
  39.         for update in new_values:
  40.             if update.station in names:
  41.                 idx = names.index(update.station)
  42.                 station = self.stations[idx]
  43.                 station.gain = update.value
  44.                 station.score += station.gain
  45.             else:
  46.                 self.stations.append(Station(update.station, 0, update.value))
  47.  
  48.     def update_ranks(self):
  49.         self.stations.sort(reverse=True)
  50.         for station in self.stations:
  51.             station.new_rank = self.stations.index(station) + 1
  52.             if station.new_rank > 1:
  53.                 higher_station = self.stations[self.stations.index(station) - 1]
  54.                 if station.score == higher_station.score:
  55.                     station.new_rank = higher_station.new_rank
  56.  
  57.  
  58. def read_stations() -> Ranking:
  59.     ranks = Ranking()
  60.     try:
  61.         with open("ranking.txt", "r") as datafile:
  62.             for line in datafile:
  63.                 (rank, name, score) = line.split(",")
  64.                 ranks.stations.append(Station(name, int(rank), int(score)))
  65.     except FileNotFoundError:
  66.         pass
  67.  
  68.     return ranks
  69.  
  70.  
  71. def prompt_update() -> List[Update]:
  72.     update: List[Update] = []
  73.     print("Enter update as name,value")
  74.     print("End with Ctrl+D")
  75.     for line in stdin:
  76.         (name, value) = line.split(",")
  77.         # Todo: verify input
  78.         update.append(Update(name, int(10 * float(value))))
  79.  
  80.     return update
  81.  
  82.  
  83. def write_file(ranking: Ranking):
  84.     with open("ranking.txt", "w") as datafile:
  85.         for station in ranking.stations:
  86.             datafile.write(f"{station.new_rank},{station.name},{station.score}\n")
  87.  
  88.  
  89. def get_board_update(ranking: Ranking):
  90.     with open("board_update.txt", "w") as datafile:
  91.         for station in ranking.stations:
  92.             orange = ""
  93.             orange_end = ""
  94.             gain = ""
  95.             rank_change = station.rank - station.new_rank
  96.             rank = ""
  97.             score = str(float(station.score) / 10.0).replace(".", ",")
  98.             if station.rank == 0:
  99.                 orange = "[color=orange]"
  100.                 orange_end = "[/color]"
  101.             else:
  102.                 if station.gain != 0:
  103.                     gaincomma = str(float(station.gain) / 10.0).replace(".", ",")
  104.                     gain = f" [color=grey][i]+{gaincomma}[/i][/color]"
  105.                 if rank_change > 0:
  106.                     if station.rank != 8:
  107.                         rank = f" [color=green]({station.rank})[/color]"
  108.                     else:
  109.                         rank = f" [color=green]({station.rank} )[/color]"
  110.                 elif rank_change < 0:
  111.                     if station.rank != 8:
  112.                         rank = f" [color=red]({station.rank})[/color]"
  113.                     else:
  114.                         rank = f" [color=red]({station.rank} )[/color]"
  115.             datafile.write(f"{orange}{station.new_rank}. {station.name} {score}{gain}{rank}{orange_end}\n")
  116.  
  117.  
  118. def main():
  119.     ranking: Ranking
  120.     new_values: List[Update]
  121.  
  122.     ranking = read_stations()
  123.     new_values = prompt_update()
  124.     if new_values == []:
  125.         print("No update received, exit")
  126.         exit(1)
  127.     ranking.update_values(new_values)
  128.     ranking.update_ranks()
  129.     write_file(ranking)
  130.     get_board_update(ranking)
  131.  
  132.  
  133. if __name__ == "__main__":
  134.     main()
  135.  
RAW Paste Data