From d7d69b808e0cb5357f0fb75fa1ca7fceb1365b64 Mon Sep 17 00:00:00 2001 From: oldnapalm <38410858+oldnapalm@users.noreply.github.com> Date: Thu, 12 Nov 2020 14:42:09 -0300 Subject: [PATCH] Add preliminar pace partner, fix save ghost --- standalone.py | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/standalone.py b/standalone.py index 33f6ee5..0b1e586 100755 --- a/standalone.py +++ b/standalone.py @@ -44,6 +44,7 @@ globalGhosts = {} ghostsEnabled = {} online = {} playerUpdateQueue = {} +globalPacePartners = {} def roadID(state): return (state.f20 & 0xff00) >> 8 @@ -195,13 +196,10 @@ class CDNHandler(SimpleHTTPRequestHandler): # This can only work if we're running on a different machine than the Zwift client. sent = False try: - url = 'http://{}{}'.format(hostname, self.path) req_header = self.parse_headers() - resp = requests.get(url, headers=merge_two_dicts(req_header, set_header()), verify=False) sent = True - self.send_response(resp.status_code) self.send_resp_headers(resp) self.wfile.write(resp.content) @@ -345,6 +343,37 @@ class GhostsVariables: start_road = 0 start_rt = 0 +class PacePartnerVariables: + route = None + position = 0 + +def loadPacePartners(): + folder = '%s/pace_partners' % STORAGE_DIR + if not os.path.isdir(folder): return + for (root, dirs, files) in os.walk(folder): + for pp_id in dirs: + route = '%s/%s/route.bin' % (folder, pp_id) + if os.path.isfile(route): + with open(route, 'rb') as fd: + globalPacePartners[pp_id] = PacePartnerVariables() + pp = globalPacePartners[pp_id] + pp.route = udp_node_msgs_pb2.Ghost() + pp.route.ParseFromString(fd.read()) + pp.position = 0 + +def playPacePartners(): + while True: + keys = globalPacePartners.keys() + for pp_id in keys: + pp = globalPacePartners[pp_id] + state = pp.route.states[pp.position] + state.id = int(pp_id) + state.worldTime = zwift_offline.world_time() + online[pp_id] = state + if pp.position < len(pp.route.states): pp.position += 1 + else: pp.position = 0 + ppthreadevent.wait(timeout=3) + class UDPHandler(socketserver.BaseRequestHandler): def handle(self): data = self.request[0] @@ -383,13 +412,13 @@ class UDPHandler(socketserver.BaseRequestHandler): ghosts.lastPackageTime = t if player_id in ghostsEnabled and ghostsEnabled[player_id]: - if not ghosts.loaded and state.roadTime > 0: + if not ghosts.loaded and getCourse(state): ghosts.loaded = True loadGhosts(player_id, state, ghosts) if state.roadTime and ghosts.last_rt and state.roadTime != ghosts.last_rt: if t >= ghosts.last_rec + update_freq: - state = ghosts.rec.states.add() - state.CopyFrom(state) + s = ghosts.rec.states.add() + s.CopyFrom(state) ghosts.last_rec = t if not ghosts.started and ghosts.play.ghosts and roadID(state) == ghosts.start_road: if isForward(state): @@ -513,4 +542,9 @@ udpserver_thread = threading.Thread(target=udpserver.serve_forever) udpserver_thread.daemon = True udpserver_thread.start() +loadPacePartners() +ppthreadevent = threading.Event() +pp = threading.Thread(target=playPacePartners) +pp.start() + zwift_offline.run_standalone(online, ghostsEnabled, saveGhost, playerUpdateQueue)