Skip to content

Commit

Permalink
Show bookmarks on home screen
Browse files Browse the repository at this point in the history
  • Loading branch information
oldnapalm committed Apr 23, 2024
1 parent a20048e commit 75b508f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 42 deletions.
30 changes: 4 additions & 26 deletions standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def change_presence(self, n):
online = {}
global_pace_partners = {}
global_bots = {}
global_bookmarks = {}
global_news = {} #player id to dictionary of peer_player_id->worldTime
global_relay = {}
global_clients = {}
Expand Down Expand Up @@ -567,26 +566,6 @@ def play_bots():
pause = bot_update_freq - (time.perf_counter() - start)
if pause > 0: time.sleep(pause)

class Bookmark:
name = ''
state = None

def load_bookmarks(player_id, course, bookmarks):
bookmarks.clear()
bookmarks_dir = os.path.join(STORAGE_DIR, str(player_id), 'bookmarks', str(course))
if os.path.isdir(bookmarks_dir):
for i, name in enumerate(os.listdir(bookmarks_dir)):
if name.endswith('.bin'):
state = udp_node_msgs_pb2.PlayerState()
with open(os.path.join(bookmarks_dir, name), 'rb') as f:
state.ParseFromString(f.read())
if zo.get_course(state) == course:
state.id = i + 9000000 + player_id * 1000
bookmark = Bookmark()
bookmark.name = name[:-4]
bookmark.state = state
bookmarks[state.id] = bookmark

def remove_inactive():
while True:
for p_id in list(online.keys()):
Expand Down Expand Up @@ -679,17 +658,16 @@ def handle(self):
last_bookmark_updates[player_id] = 0

#Add bookmarks for player if missing
if not player_id in global_bookmarks.keys():
global_bookmarks[player_id] = {}
bookmarks = global_bookmarks[player_id]
if not player_id in zo.global_bookmarks.keys():
zo.global_bookmarks[player_id] = {}
bookmarks = zo.global_bookmarks[player_id]

#Update player online state
if state.roadTime:
if player_id in online.keys():
if online[player_id].worldTime > state.worldTime:
return #udp is unordered -> drop old state
else:
load_bookmarks(player_id, zo.get_course(state), bookmarks)
discord.change_presence(len(online) + 1)
online[player_id] = state

Expand Down Expand Up @@ -859,4 +837,4 @@ def handle(self):
dns = threading.Thread(target=fake_dns, args=(zo.server_ip,))
dns.start()

zo.run_standalone(online, global_relay, global_pace_partners, global_bots, global_ghosts, global_bookmarks, regroup_ghosts, discord)
zo.run_standalone(online, global_relay, global_pace_partners, global_bots, global_ghosts, regroup_ghosts, discord)
69 changes: 53 additions & 16 deletions zwift_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def make_dir(name):
player_partial_profiles = {}
map_override = {}
climb_override = {}
global_bookmarks = {}
restarting = False
restarting_in_minutes = 0
reload_pacer_bots = False
Expand Down Expand Up @@ -387,6 +388,10 @@ def to_json(self):
"male": self.male,
"playerType": self.player_type }

class Bookmark:
name = ''
state = None

class Online:
total = 0
richmond = 0
Expand Down Expand Up @@ -1119,7 +1124,7 @@ def api_eventfeed():

@app.route('/api/recommendations/recommendation', methods=['GET'])
def api_recommendations_recommendation():
return jsonify([{"type": "EVENT"}])
return jsonify([{"type": "EVENT"}, {"type": "RIDE_WITH"}])

@app.route('/api/campaign/profile/campaigns', methods=['GET'])
@app.route('/api/announcements/active', methods=['GET'])
Expand Down Expand Up @@ -2934,7 +2939,7 @@ def api_tcp_config():
return infos.SerializeToString(), 200


def add_player_to_world(player, course_world, is_pace_partner=False, is_bot=False):
def add_player_to_world(player, course_world, is_pace_partner=False, is_bot=False, is_bookmark=False, name=None):
course_id = get_course(player)
if course_id in course_world.keys():
partial_profile = get_partial_profile(player.id)
Expand All @@ -2948,11 +2953,17 @@ def add_player_to_world(player, course_world, is_pace_partner=False, is_bot=Fals
online_player.speed = player.speed
elif is_bot:
online_player = course_world[course_id].others.add()
elif is_bookmark:
online_player = course_world[course_id].pro_players.add()
else: # to be able to join zwifter using new home screen
online_player = course_world[course_id].followees.add()
online_player.id = player.id
online_player.firstName = partial_profile.first_name
online_player.lastName = partial_profile.last_name
if name:
online_player.firstName = courses_lookup[course_id]
online_player.lastName = name
else:
online_player.firstName = partial_profile.first_name
online_player.lastName = partial_profile.last_name
online_player.distance = player.distance
online_player.time = player.time
online_player.country_code = partial_profile.country_code
Expand All @@ -2964,15 +2975,15 @@ def add_player_to_world(player, course_world, is_pace_partner=False, is_bot=Fals
course_world[course_id].zwifters += 1


def relay_worlds_generic(server_realm=None):
def relay_worlds_generic(server_realm=None, player_id=None):
# Android client also requests a JSON version
if request.headers['Accept'] == 'application/json':
friends = []
for player_id in online:
profile = get_partial_profile(player_id)
friend = {"playerId": player_id, "firstName": profile.first_name, "lastName": profile.last_name, "male": profile.male, "countryISOCode": profile.country_code,
"totalDistanceInMeters": jsv0(online[player_id], 'distance'), "rideDurationInSeconds": jsv0(online[player_id], 'time'), "playerType": profile.player_type,
"followerStatusOfLoggedInPlayer": "NO_RELATIONSHIP", "rideOnGiven": False, "currentSport": profile_pb2.Sport.Name(jsv0(online[player_id], 'sport')),
for p_id in online:
profile = get_partial_profile(p_id)
friend = {"playerId": p_id, "firstName": profile.first_name, "lastName": profile.last_name, "male": profile.male, "countryISOCode": profile.country_code,
"totalDistanceInMeters": jsv0(online[p_id], 'distance'), "rideDurationInSeconds": jsv0(online[p_id], 'time'), "playerType": profile.player_type,
"followerStatusOfLoggedInPlayer": "NO_RELATIONSHIP", "rideOnGiven": False, "currentSport": profile_pb2.Sport.Name(jsv0(online[p_id], 'sport')),
"enrolledZwiftAcademy": False, "mapId": 1, "ftp": 100, "runTime10kmInSeconds": 3600}
friends.append(friend)
world = { 'currentDateTime': int(time.time()),
Expand Down Expand Up @@ -3012,17 +3023,44 @@ def relay_worlds_generic(server_realm=None):
bot_variables = global_bots[p_id]
bot = bot_variables.route.states[bot_variables.position]
add_player_to_world(bot, course_world, is_bot=True)
if player_id in global_bookmarks.keys():
for bookmark in global_bookmarks[player_id].values():
add_player_to_world(bookmark.state, course_world, is_bookmark=True, name=bookmark.name)
if server_realm:
world.id = server_realm
return world.SerializeToString()
else:
return worlds.SerializeToString()


def load_bookmarks(player_id):
if not player_id in global_bookmarks.keys():
global_bookmarks[player_id] = {}
bookmarks = global_bookmarks[player_id]
bookmarks.clear()
bookmarks_dir = os.path.join(STORAGE_DIR, str(player_id), 'bookmarks')
if os.path.isdir(bookmarks_dir):
i = 1
for (root, dirs, files) in os.walk(bookmarks_dir):
for file in files:
if file.endswith('.bin'):
state = udp_node_msgs_pb2.PlayerState()
with open(os.path.join(root, file), 'rb') as f:
state.ParseFromString(f.read())
state.id = i + 9000000 + player_id * 1000
bookmark = Bookmark()
bookmark.name = file[:-4]
bookmark.state = state
bookmarks[state.id] = bookmark
i += 1

@app.route('/relay/worlds', methods=['GET'])
@app.route('/relay/dropin', methods=['GET']) #zwift::protobuf::DropInWorldList
@jwt_to_session_cookie
@login_required
def relay_worlds():
return relay_worlds_generic()
load_bookmarks(current_user.player_id)
return relay_worlds_generic(player_id=current_user.player_id)


def add_teleport_target(player, targets, is_pace_partner=True, name=None):
Expand Down Expand Up @@ -3067,8 +3105,9 @@ def relay_teleport_targets():
if get_course(player) == course:
add_teleport_target(player, targets, False)
if current_user.player_id in global_bookmarks.keys():
for bookmark in list(global_bookmarks[current_user.player_id].values()):
add_teleport_target(bookmark.state, targets, False, bookmark.name)
for bookmark in global_bookmarks[current_user.player_id].values():
if get_course(bookmark.state) == course:
add_teleport_target(bookmark.state, targets, False, bookmark.name)
return targets.SerializeToString()


Expand Down Expand Up @@ -3880,13 +3919,12 @@ def start_zwift():
return redirect("/ride", 302)


def run_standalone(passed_online, passed_global_relay, passed_global_pace_partners, passed_global_bots, passed_global_ghosts, passed_global_bookmarks, passed_regroup_ghosts, passed_discord):
def run_standalone(passed_online, passed_global_relay, passed_global_pace_partners, passed_global_bots, passed_global_ghosts, passed_regroup_ghosts, passed_discord):
global online
global global_relay
global global_pace_partners
global global_bots
global global_ghosts
global global_bookmarks
global regroup_ghosts
global discord
global login_manager
Expand All @@ -3895,7 +3933,6 @@ def run_standalone(passed_online, passed_global_relay, passed_global_pace_partne
global_pace_partners = passed_global_pace_partners
global_bots = passed_global_bots
global_ghosts = passed_global_ghosts
global_bookmarks = passed_global_bookmarks
regroup_ghosts = passed_regroup_ghosts
discord = passed_discord
login_manager = LoginManager()
Expand Down

5 comments on commit 75b508f

@jricoclq
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to turn off the bookmark function on the home screen?
Or enable the checkbox function on the launcher interface.

@oldnapalm
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to turn off the bookmark function on the home screen?

Revert this change 75b508f#diff-f4053a7fcb3c73ca8b58c6fb646b8f0e2c467edfad4759aa3a241f09b3fe2ca2R1127

@jricoclq
Copy link

@jricoclq jricoclq commented on 75b508f Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask if it is possible to retain the online users above and remove the bookmarks below?

Image

@oldnapalm
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask if it is possible to retain the online users above and remove the bookmarks below?

Delete lines 3026-3028 above.

@jricoclq
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much!

Please sign in to comment.