Skip to content

Commit

Permalink
fix: account json file compatibility (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
iholston committed Nov 24, 2023
1 parent 3cd0891 commit b5809b4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build_and_release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ jobs:
- name: Build artifact
run: |
pip install -r requirements.txt pillow pyinstaller
pyinstaller --onefile --noconsole --name "LoL Bot" --icon=lolbot/resources/images/a.ico --add-data "lolbot/resources:lolbot/resources" main.pyw
pyinstaller --onefile --noconsole --name "LoLBot" --icon=lolbot/resources/images/a.ico --add-data "lolbot/resources:lolbot/resources" main.pyw
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: lolbot
name: LoLBot
path: dist/*

release:
Expand All @@ -34,10 +34,10 @@ jobs:
- name: Download artifact
uses: actions/download-artifact@v3
with:
name: lolbot
name: LoLBot
- name: Push to release
uses: ncipollo/release-action@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
replacesArtifacts: false
artifacts: LoL Bot.exe
artifacts: LoLBot.exe
30 changes: 18 additions & 12 deletions lolbot/common/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,32 @@ class AccountManager(AccountGenerator):
"""Class that handles account persistence"""

def __init__(self):
data = {'accounts': []}
if not os.path.exists(Constants.ACCOUNT_PATH):
data = {'Accounts': []}
with open(Constants.ACCOUNT_PATH, 'w+') as f:
json.dump(data, f, indent=4)
else:
with open(Constants.ACCOUNT_PATH, 'w+') as f:
try:
json.load(f)
except:
json.dump(data, f, indent=4)

def get_account(self, max_level: int) -> Account:
"""Gets an account username from JSON file where level is < max_level"""
with open(Constants.ACCOUNT_PATH, "r") as f:
data = json.load(f)
for account in data['Accounts']:
for account in data['accounts']:
if account['level'] < max_level:
return Account(account['username'], account['password'], account['level'])

def add_account(self, account: Account):
"""Writes account to JSON, will not write duplicates"""
with open(Constants.ACCOUNT_PATH, 'r+') as f:
data = json.load(f)
if asdict(account) in data['Accounts']:
if asdict(account) in data['accounts']:
return
data['Accounts'].append(asdict(account))
data['accounts'].append(asdict(account))
with open(Constants.ACCOUNT_PATH, 'r+') as outfile:
outfile.write(json.dumps(data, indent=4))

Expand All @@ -76,35 +82,35 @@ def edit_account(self, og_uname: str, account: Account):
with open(Constants.ACCOUNT_PATH, 'r') as f:
data = json.load(f)
index = -1
for i in range(len(data['Accounts'])):
if data['Accounts'][i]['username'] == og_uname:
for i in range(len(data['accounts'])):
if data['accounts'][i]['username'] == og_uname:
index = i
break
data['Accounts'][index]['username'] = account.username
data['Accounts'][index]['password'] = account.password
data['Accounts'][index]['level'] = account.level
data['accounts'][index]['username'] = account.username
data['accounts'][index]['password'] = account.password
data['accounts'][index]['level'] = account.level
with open(Constants.ACCOUNT_PATH, 'w') as outfile:
outfile.write(json.dumps(data, indent=4))

def delete_account(self, account: Account):
"""Deletes account"""
with open(Constants.ACCOUNT_PATH, 'r') as f:
data = json.load(f)
data['Accounts'].remove(asdict(account))
data['accounts'].remove(asdict(account))
with open(Constants.ACCOUNT_PATH, 'w') as outfile:
outfile.write(json.dumps(data, indent=4))

def get_all_accounts(self) -> dict:
"""Returns all accounts as dictionary"""
with open(Constants.ACCOUNT_PATH, 'r') as f:
data = json.load(f)
return data['Accounts']
return data['accounts']

def set_account_as_leveled(self, account: Account, max_level: int):
"""Sets account level to user configured max level in the JSON file"""
with open(Constants.ACCOUNT_PATH, 'r') as f:
data = json.load(f)
for account in data['Accounts']:
for account in data['accounts']:
if account['username'] == account.username:
account['level'] = max_level
with open(Constants.ACCOUNT_PATH, 'w') as json_file:
Expand Down
7 changes: 3 additions & 4 deletions lolbot/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Constants:
"""Constant settings"""
# Constant paths
RIOT_LOCKFILE = os.path.join(os.getenv('LOCALAPPDATA'), 'Riot Games/Riot Client/Config/lockfile')
CONFIG_DIR = os.path.join(os.getenv('LOCALAPPDATA'), 'LoL Bot')
CONFIG_DIR = os.path.join(os.getenv('LOCALAPPDATA'), 'LoLBot')
LOG_DIR = os.path.join(CONFIG_DIR, 'logs')
CONFIG_PATH = os.path.join(CONFIG_DIR, 'configs.json')
ACCOUNT_PATH = os.path.join(CONFIG_DIR, 'accounts.json')
Expand All @@ -21,7 +21,7 @@ class Constants:
ICON_PATH = 'lolbot/resources/images/a.ico'

# Other
VERSION = '2.1.0'
VERSION = '2.1.1'

@staticmethod
def create_dirs():
Expand All @@ -32,8 +32,7 @@ def create_dirs():
if not os.path.exists(Constants.CONFIG_PATH):
open(Constants.CONFIG_PATH, 'w+')
if not os.path.exists(Constants.ACCOUNT_PATH):
with open(Constants.ACCOUNT_PATH, 'w+') as outfile:
json.dump({'Accounts': []}, outfile, indent=4)
open(Constants.ACCOUNT_PATH, 'w+')


class DefaultSettings:
Expand Down

0 comments on commit b5809b4

Please sign in to comment.