Skip to content

Commit

Permalink
Updated user data storage
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeWait committed Jun 20, 2024
1 parent e93104b commit 7e10e22
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 25 deletions.
32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ To install and run the application from source:
```sh
pip install -r requirements.txt
```
- **Linux**:
```sh
sudo apt-get install python3-gi
sudo apt-get install python3-gi-cairo gir1.2-gtk-3.0
sudo apt-get install libcairo2-dev
sudo apt install libgirepository1.0-dev
pip install pygobject==3.42.2
pip install -r requirements.txt
```
- **Linux**:
```sh
sudo apt-get install python3-gi
sudo apt-get install python3-gi-cairo gir1.2-gtk-3.0
sudo apt-get install libcairo2-dev
sudo apt install libgirepository1.0-dev
pip install pygobject==3.42.2
pip install -r requirements.txt
```

4. Run the application:
- **Windows**:
Expand All @@ -85,7 +85,15 @@ To install and run the application from source:
## Usage
After running the application, you can log in with your username and password or create a new account. Once connected to the network, you can challenge other users to a game of Rock Paper Scissors and chat with them using the built-in messaging system.

- **Add New User**: When playing for the first time you will need to create a profile. Click the `Add New User` button on the login page. Choose an avatar, username and password. Note: Password must contain at least 8 characters, 1 number, and 1 symbol. User information, including hashed passwords, is stored in a plain text file (user_data.txt) within the application directory.
- **Add New User**: When playing for the first time you will need to create a profile. Click the `Add New User` button on the login page. Choose an avatar, username and password. Note: Passwords must contain at least 8 characters, 1 number, and 1 symbol. User information, including hashed passwords, is stored in a plain text file (user_data.txt) with the system files:
- **Windows**:
```sh
C:\Users\<YourUsername>\AppData\Roaming\rps-app\user_data.txt
```
- **Linux**:
```sh
/home/<YourUsername>/.local/share/rps-app/user_data.txt
```

<p align="center">
<img src="https://github.com/LukeWait/rps-app/raw/main/assets/screenshots/rps-app-new-user.png" alt="New User Screenshot" width="600">
Expand Down Expand Up @@ -120,7 +128,7 @@ After running the application, you can log in with your username and password or
#### Windows
Run the following command from the project main directory:
```sh
pyinstaller --onefile --add-data "assets/images:assets/images" --add-data "assets/fonts:assets/fonts" --add-data "assets/audio:assets/audio" --add-data "data:data" --noconsole src/rps_app.py
pyinstaller --onefile --add-data "assets/images:assets/images" --add-data "assets/fonts:assets/fonts" --add-data "assets/audio:assets/audio" --noconsole src/rps_app.py
```
#### Linux
For Linux, you need to create a hook-PIL.py file to handle the PIL library correctly. Follow these steps:
Expand All @@ -133,7 +141,7 @@ For Linux, you need to create a hook-PIL.py file to handle the PIL library corre
```
2. Run the following command from the project main directory:
```sh
pyinstaller --onefile --add-data "assets/images:assets/images" --add-data "assets/fonts:assets/fonts" --add-data "assets/audio:assets/audio" --add-data "data:data" --additional-hooks-dir=. --noconsole src/rps_app.py
pyinstaller --onefile --add-data "assets/images:assets/images" --add-data "assets/fonts:assets/fonts" --add-data "assets/audio:assets/audio" --additional-hooks-dir=. --noconsole src/rps_app.py
```
This will generate the executable in the `dist` directory. It will also create a `build` directory and `.spec` file. These are used in the build process and can be safely removed.
Expand Down
2 changes: 0 additions & 2 deletions data/user_data.txt

This file was deleted.

44 changes: 33 additions & 11 deletions src/rps_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import os
import re
import sys
import platform
import customtkinter as ctk
from CTkMessagebox import *
from CTkListbox import *
Expand All @@ -37,12 +36,42 @@
from playsound import playsound

def resource_path(relative_path):
"""Finds the absolute path to a resource file, whether running as a PyInstaller bundle or in development.
Args:
relative_path (str): The relative path to the resource file.
Returns:
str: The absolute path to the resource file.
"""
try:
base_path = sys._MEIPASS
except AttributeError:
base_path = os.path.dirname(os.path.abspath(__file__))

return os.path.join(base_path, relative_path)

def user_data_path():
"""Creates a directory and file in system files to store user data if they do not exist.
Returns:
str: The full path to the user_data.txt file
"""
if sys.platform == "win32":
app_data_dir = os.path.join(os.getenv('APPDATA'), "rps-app")
else:
app_data_dir = os.path.join(os.path.expanduser("~"), ".local", "share", "rps-app")

if not os.path.exists(app_data_dir):
os.makedirs(app_data_dir)

user_data = os.path.join(app_data_dir, "user_data.txt")

if not os.path.exists(user_data):
open(user_data, 'w').close()

return user_data


class Gui(ctk.CTk):
"""Represents the graphical user interface.
Expand Down Expand Up @@ -146,7 +175,6 @@ def __init__(self):
self.create_main_frame()
self.show_login_screen()


def load_images(self):
"""Loads images used in the GUI from specified directories.
"""
Expand Down Expand Up @@ -281,7 +309,7 @@ def create_main_frame(self):
The status frame displays the current state of connectivity.
"""
# Determine the console text size based on the operating system
if platform.system() == "Windows":
if sys.platform == "win32":
text_size = 11
else:
text_size = 10
Expand Down Expand Up @@ -1042,14 +1070,8 @@ def __init__(self):
self.server = Server(self, self.gui, self.network)
self.client = Client(self, self.gui, self.network)

# Relative path to user save data
if hasattr(sys, '_MEIPASS'):
# In a PyInstaller bundle, use paths relative to the _MEIPASS directory
self.user_data_path = resource_path(os.path.join("data", "user_data.txt"))
else:
# In development, use paths relative to the script's directory
self.user_data_path = resource_path(os.path.join("..", "data", "user_data.txt"))

# Path to user save data in system file storage
self.user_data_path = user_data_path()

# User details and game variables
self.user_profile = None
Expand Down

0 comments on commit 7e10e22

Please sign in to comment.