Skip to content

Commit

Permalink
Add user storage
Browse files Browse the repository at this point in the history
  • Loading branch information
oldnapalm committed Sep 7, 2024
1 parent 100bf5b commit 46e4494
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions protobuf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ all:
protoc --python_out=. variants.proto
protoc --python_out=. playback.proto
protoc --python_out=. route-result.proto
protoc --python_out=. user_storage.proto

clean:
rm -f *_pb2.py *_pb2.pyc
1 change: 1 addition & 0 deletions protobuf/make.bat
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ protoc --python_out=. events.proto
protoc --python_out=. variants.proto
protoc --python_out=. playback.proto
protoc --python_out=. route-result.proto
protoc --python_out=. user_storage.proto

pause
18 changes: 18 additions & 0 deletions protobuf/user_storage.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto2";

message UserStorage {
optional GameSettings game_settings = 2;
}

message GameSettings {
optional Attributes f22 = 22;
}

message Attributes {
optional float f2 = 2;
optional int32 f3 = 3;
optional int32 f4 = 4;
optional int32 f5 = 5;
optional int32 f6 = 6;
optional int32 f7 = 7;
}
29 changes: 29 additions & 0 deletions protobuf/user_storage_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 26 additions & 1 deletion zwift_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import events_pb2
import variants_pb2
import playback_pb2
import user_storage_pb2
import online_sync

logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
Expand Down Expand Up @@ -1313,7 +1314,6 @@ def api_clubs_club_my_clubs_summary():
@app.route('/api/game-asset-patching-service/manifest', methods=['GET'])
@app.route('/api/race-results', methods=['POST'])
@app.route('/api/workout/progress', methods=['POST'])
@app.route('/api/player-profile/user-game-storage/attributes', methods=['GET', 'POST']) # will load from prefs.xml
def api_proto_empty():
return '', 200

Expand Down Expand Up @@ -3656,6 +3656,31 @@ def api_power_curve_best(option):
return power_curves.SerializeToString(), 200


@app.route('/api/player-profile/user-game-storage/attributes', methods=['GET', 'POST'])
@jwt_to_session_cookie
@login_required
def api_player_profile_user_game_storage_attributes():
user_storage = user_storage_pb2.UserStorage()
user_storage_file = os.path.join(STORAGE_DIR, str(current_user.player_id), 'user_storage.bin')
if os.path.isfile(user_storage_file):
with open(user_storage_file, 'rb') as f:
user_storage.ParseFromString(f.read())
if request.method == 'POST':
new = user_storage_pb2.UserStorage()
new.ParseFromString(request.stream.read())
user_storage.MergeFrom(new)
with open(user_storage_file, 'wb') as f:
f.write(user_storage.SerializeToString())
return '', 202
ret = user_storage_pb2.UserStorage()
n = int(request.args.get('n'))
if n in user_storage.game_settings.DESCRIPTOR.fields_by_number:
field = user_storage.game_settings.DESCRIPTOR.fields_by_number[n].name
if user_storage.game_settings.HasField(field):
getattr(ret.game_settings, field).CopyFrom(getattr(user_storage.game_settings, field))
return ret.SerializeToString(), 200


@app.teardown_request
def teardown_request(exception):
db.session.close()
Expand Down

0 comments on commit 46e4494

Please sign in to comment.