-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
815 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,4 +51,4 @@ l10n/ | |
launch.json | ||
venv | ||
*.db | ||
profiles.json | ||
Client/Data/profiles.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,60 @@ | ||
import json | ||
from os.path import exists | ||
import os | ||
from Server.player import Player | ||
|
||
# Path to the profiles.json file | ||
path = ('../json_schema/profiles.json') | ||
path=os.path.abspath('Client/Data/profiles.json') | ||
|
||
class Profile: | ||
""" | ||
This class is used to handle the profiles.json file. It is used to get, set, and add profiles to the file. | ||
""" | ||
def check_file(self): | ||
""" | ||
This method checks if the file exists | ||
:return: True if the file exists, False if it does not | ||
""" | ||
if exists(path): | ||
print("found") | ||
return True | ||
else: | ||
print("not found") | ||
return False | ||
|
||
def get_profiles(self): | ||
@staticmethod | ||
def get_profiles(): | ||
""" | ||
This method returns all the profiles from the file | ||
:return: An array of all profiles | ||
""" | ||
if self.check_file(): | ||
Profile._check_folder() | ||
if exists(path): | ||
with open(path, 'r') as file: | ||
data = json.load(file) | ||
return data | ||
if not data: | ||
return [], 0 | ||
output = [] | ||
profile_data = data[0] | ||
selected = data[1] | ||
for profile in profile_data: | ||
output.append(Player.from_dict(profile)) | ||
return output, selected | ||
else: | ||
return None | ||
def get_profile(self,profile_uuid): | ||
""" | ||
This method returns a profile by its uuid | ||
:param profile_uuid: | ||
:return: profile matching given uuid | ||
""" | ||
if self.check_file(): | ||
try: | ||
with open(path, 'r') as file: | ||
data = json.load(file) | ||
for profile in data: | ||
if profile["profile_uuid"] == profile_uuid: | ||
return profile | ||
except: | ||
print("json error: Make sure profiles.json is formatted correctly") | ||
return None | ||
def set_profile(self, profile_uuid , profile_name = None, profile_color = None): | ||
""" | ||
This method sets the profile name and/or color by the uuid | ||
:param profile_uuid: | ||
:param profile_name: *optional* | ||
:param profile_color: *optional* | ||
""" | ||
if self.check_file(): | ||
try: | ||
with open(path, 'r+') as file: | ||
data = json.load(file) | ||
for profile in data: | ||
if profile["profile_uuid"] == profile_uuid: | ||
if profile_name != None: | ||
profile["profile_name"] = profile_name | ||
if profile_color != None: | ||
profile["profile_color"] = profile_color | ||
break | ||
with open(path, 'w') as file: | ||
json.dump(data, file) | ||
except: | ||
print("json error: Make sure profiles.json is formatted correctly") | ||
return None | ||
def get_profile_by_name(self, profile_name): | ||
if self.check_file(): | ||
""" | ||
This method returns a profile by its name | ||
:param profile_name: | ||
:return: profile matching given name | ||
""" | ||
try: | ||
with open(path, 'r') as file: | ||
data = json.load(file) | ||
for profile in data: | ||
if profile["profile_name"] == profile_name: | ||
return profile | ||
except: | ||
print("json error: Make sure profiles.json is formatted correctly") | ||
return None | ||
return [], None | ||
|
||
def add_new_profile(self, profile_name, profile_uuid, profile_color): | ||
@staticmethod | ||
def set_profiles(players: list, selected: int): | ||
Profile._check_folder() | ||
with open(path, 'w+') as file: | ||
entry = [] | ||
for player in players: | ||
entry.append(player.as_dict()) | ||
json.dump([entry, selected], file) | ||
|
||
@staticmethod | ||
def delete_all_profiles(): | ||
""" | ||
This method adds a new profile to the file | ||
:param profile_name: | ||
:param profile_uuid: | ||
:param profile_color: | ||
This method deletes all profiles | ||
""" | ||
if self.check_file(): | ||
entry = {"profile_name": profile_name, "profile_uuid": profile_uuid, "profile_color": profile_color} | ||
try: | ||
with open(path, 'r+') as file: | ||
data = json.load(file) | ||
file.seek(0) | ||
data.append(entry) | ||
json.dump(data, file) | ||
file.truncate() | ||
except: | ||
print("json error: Make sure profiles.json is formatted correctly") | ||
|
||
else: | ||
Profile._check_folder() | ||
if exists(path): | ||
with open(path, 'w') as file: | ||
entry = [{"profile_name": profile_name, "profile_uuid": profile_uuid, "profile_color": profile_color}] | ||
json.dump(entry, file) | ||
file.write("[]") | ||
|
||
#Testing | ||
#profile = Profile() | ||
#profile.add_new_profile("test", "test", "test") | ||
#print(profile.get_profiles()) | ||
#print(profile.get_profile("test")) | ||
#profile.set_profile("test", "test2", "test3") | ||
#print(profile.get_profiles()) | ||
@staticmethod | ||
def _check_folder(): | ||
dir = os.path.abspath('Client/Data/') | ||
if not os.path.exists(os.path.abspath(dir)): | ||
try: | ||
os.makedirs(os.path.abspath(dir)) | ||
except OSError: | ||
raise OSError(f"Creation of the directory {dir} failed") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import unittest | ||
from Client.profile_save import Profile | ||
from Server.player import Player | ||
from uuid import uuid4 | ||
|
||
class TestProfileSave(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.player1 = Player("test", 0, uuid4(), False) | ||
self.player2 = Player("test2", 0, uuid4(), False) | ||
Profile.delete_all_profiles() | ||
|
||
def test_all(self): | ||
data = ([self.player1, self.player2], 0) | ||
Profile.set_profiles(data[0], data[1]) | ||
self.assertEqual(Profile.get_profiles(), data) | ||
Profile.delete_all_profiles() | ||
self.assertEqual(Profile.get_profiles(), ([], 0)) |
Oops, something went wrong.