Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-47: Fix API key frame label not covering full screen #59

Merged
merged 5 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions backend/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def get_alliance(self) -> Alliance | None:
route = Route("/alliance", "GET")
response = self.request(route)
data = response.json()
if data["success"] == False:
if not data["success"]:
return None

alliance = Alliance.from_data(data)
Expand Down Expand Up @@ -87,10 +87,7 @@ def recruit_troop(self, troop_type: str, amount: str, currency: str = "gold"):

def get_market_orders(self, item: str, order_type: str) -> list[MarketOrder | None]:
route = Route("/market", "GET")
query_params = {
"item_type": item,
"order_type": order_type
}
query_params = {"item_type": item, "order_type": order_type}
response = self.request(route, query_params=query_params)
orders = response.json()["orders"]
orders = [MarketOrder.from_data(order) for order in orders.values()]
Expand Down Expand Up @@ -122,4 +119,4 @@ def attack_npc(self, troops: dict[UnitType, int]) -> NPCResult | None:
json = {"troops": troops}
print(json)
response = self.request(route, json=json)
return NPCResult.from_api(response.json())
return NPCResult.from_api(response.json())
12 changes: 8 additions & 4 deletions gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
from gui.frames.empty_frame import EmptyFrame
from gui.frames.outposts import OutpostsFrame
from gui.frames.market import MarketFrame
from gui.components import labels


def resource_path(asset_path: str) -> str:
try:
base_path = sys._MEIPASS2 # type: ignore
base_path = sys._MEIPASS2 # type: ignore
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, asset_path)


class App(tkinter.Tk):
def __init__(self):
tkinter.Tk.__init__(self)
Expand Down Expand Up @@ -56,7 +57,7 @@ def build_app(self, destroy: bool = False) -> None:
self.construction_frame = ConstructionFrame(self)
self.alliance_frame = AllianceFrame(self)
self.settings_frame = SettingsFrame(self)
self.api_key_frame = APIKeyFrame(self)
self.api_key_frame = APIKeyFrame(self, sidebar=True)
self.outposts_frame = OutpostsFrame(self)
self.market_frame = MarketFrame(self)
self.current_frame = EmptyFrame(self)
Expand All @@ -75,7 +76,10 @@ def verify_api(self) -> str | bool:
api_key = f.read()
self.backend = API(api_key)
valid = self.backend.verify_key()
return api_key if valid else False
if valid:
return api_key
else:
return False
except FileNotFoundError:
return False

Expand Down
5 changes: 5 additions & 0 deletions gui/components/labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ def __init__(self, parent, text):
)


class FullWidthLabel(tkinter.Label):
def __init__(self, parent, text):
super().__init__(parent, text=text, width=50, height=2, font=("Helvetica", 20))


class FrameLabel(tkinter.Label):
def __init__(self, parent, text):
super().__init__(parent, text=text, width=42, height=2, font=("Helvetica", 20))
Expand Down
24 changes: 15 additions & 9 deletions gui/frames/alliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,24 @@ def __init__(self, parent):
def update_data(self):
self.allianceData = self.parent.backend.get_alliance()
self.allianceNameLabel.config(text=f"Alliance: {self.allianceData.name}")
self.allianceOwnerLabel.config(text=f"Owner (Discord ID): {self.allianceData.owner}")
self.allianceUserLimitLabel.config(text=f'User Limit: {"{:,}".format(self.allianceData.user_limit)}')
self.allianceBankBalanceLabel.config(text=f'Bank Balance: {"{:,}".format(self.allianceData.bank)}')
self.allianceCreationTimestamp.config(text=f"Alliance Creation Timestamp: {self.allianceData.created_at}")
self.allianceOwnerLabel.config(
text=f"Owner (Discord ID): {self.allianceData.owner}"
)
self.allianceUserLimitLabel.config(
text=f'User Limit: {"{:,}".format(self.allianceData.user_limit)}'
)
self.allianceBankBalanceLabel.config(
text=f'Bank Balance: {"{:,}".format(self.allianceData.bank)}'
)
self.allianceCreationTimestamp.config(
text=f"Alliance Creation Timestamp: {self.allianceData.created_at}"
)

def update_alliance_name(self):
name = self.allianceNameUpdateField.get()

response = self.parent.backend.update_alliance_name(name)
if response["success"] == False:
if not response["success"]:
self.show_feedback(response["detail"])

self.update_data()
Expand All @@ -77,15 +85,13 @@ def update_alliance_user_limit(self):
user_limit = self.allianceUserLimitUpdateField.get()

response = self.parent.backend.update_alliance_user_limit(int(user_limit))
if response["success"] == False:
if not response["success"]:
self.show_feedback(response["detail"])

self.update_data()

def show_feedback(self, feedback: str) -> None:
self.feedbackLabel = labels.InputLabel(
self, feedback
)
self.feedbackLabel = labels.InputLabel(self, feedback)
self.feedbackLabel.place(x=300, y=350)

def render(self):
Expand Down
14 changes: 11 additions & 3 deletions gui/frames/api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@
from ..components import labels, inputs, buttons
from backend.api import API


def resource_path(asset_path: str) -> str:
try:
base_path = sys._MEIPASS2 # type: ignore
base_path = sys._MEIPASS2 # type: ignore
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, asset_path)


class APIKeyFrame(tkinter.Frame):
def __init__(self, parent):
def __init__(self, parent, sidebar=False):
self.sidebar = sidebar

self.bg = "yellow"
super().__init__(parent, bg=self.bg)
self.parent = parent

self.label = labels.FrameLabel(self, "API Key")
if self.sidebar:
self.label = labels.FrameLabel(self, "API Key")
else:
self.label = labels.FullWidthLabel(self, "API Key")

self.apikeyentrylabel = labels.InputLabel(self, "Enter API Key:")
self.apikeyentry = inputs.TextInput(self)
self.apikeyentry.insert(0, self.parent.backend.api_key)
self.apikeysubmit = buttons.SubmitButton(
self, text="Submit", width=10, height=1, command=self.submit_api_key
)
Expand Down
38 changes: 26 additions & 12 deletions gui/frames/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,57 @@ def __init__(self, parent):
self, f'Farmhouse Level: {self.buildingLevels["farmhouse_level"]}'
)
self.farmhouseUpgradeButton = buttons.SubmitButton(
self, text="Upgrade", height=1, command=lambda: self.upgrade_building("farmhouse")
self,
text="Upgrade",
height=1,
command=lambda: self.upgrade_building("farmhouse"),
)

self.bakeryUpgradeLabel = labels.InputLabel(
self, f'Bakery Level: {self.buildingLevels["bakery_level"]}'
)
self.bakeryUpgradeButton = buttons.SubmitButton(
self, text="Upgrade", height=1, command=lambda: self.upgrade_building("bakery")
self,
text="Upgrade",
height=1,
command=lambda: self.upgrade_building("bakery"),
)

self.storehouseUpgradeLabel = labels.InputLabel(
self, f'Storehouse Level: {self.buildingLevels["storehouse_level"]}'
)
self.storehouseUpgradeButton = buttons.SubmitButton(
self, text="Upgrade", height=1, command=lambda: self.upgrade_building("storehouse")
self,
text="Upgrade",
height=1,
command=lambda: self.upgrade_building("storehouse"),
)

self.productionAmount = labels.InputLabel(
self, f'Food Production: {self.buildingLevels["production"]}'
)

def show_feedback(self, feedback: str) -> None:
self.feedbackLabel = labels.InputLabel(
self, feedback
)
self.feedbackLabel = labels.InputLabel(self, feedback)
self.feedbackLabel.place(x=300, y=350)

def update_data(self):
self.buildingLevels = self.parent.backend.get_buildings()
self.farmhouseUpgradeLabel.config(text=f'Farmhouse Level: {self.buildingLevels["farmhouse_level"]}')
self.bakeryUpgradeLabel.config(text=f'Bakery Level: {self.buildingLevels["bakery_level"]}')
self.storehouseUpgradeLabel.config(text=f'Storehouse Level: {self.buildingLevels["storehouse_level"]}')
self.productionAmount.config(text=f'Food Production: {self.buildingLevels["production"]}')

self.farmhouseUpgradeLabel.config(
text=f'Farmhouse Level: {self.buildingLevels["farmhouse_level"]}'
)
self.bakeryUpgradeLabel.config(
text=f'Bakery Level: {self.buildingLevels["bakery_level"]}'
)
self.storehouseUpgradeLabel.config(
text=f'Storehouse Level: {self.buildingLevels["storehouse_level"]}'
)
self.productionAmount.config(
text=f'Food Production: {self.buildingLevels["production"]}'
)

def upgrade_building(self, building_type: str) -> None:
self.upgradeResponse = self.parent.backend.upgrade_building(building_type, '1')
self.upgradeResponse = self.parent.backend.upgrade_building(building_type, "1")

self.show_feedback(self.upgradeResponse["detail"])
self.update_data()
Expand Down
1 change: 1 addition & 0 deletions gui/frames/empty_frame.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import tkinter
from ..components import labels


class EmptyFrame(tkinter.Frame):
def __init__(self, parent):
super().__init__(parent, width=650, height=600)
Expand Down
41 changes: 30 additions & 11 deletions gui/frames/market.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,43 @@
import tkinter
from ..components import labels, dropdowns, inputs

VALID_ITEM_TYPES = ["IRON_FRAME", "BAKERY_TOKEN", "FARMHOUSE_TOKEN", "STOREHOUSE_TOKEN", "MORALE_TOKEN", "HEALING_TOKEN", "LUCKYCHARM_TOKEN", "LOOT_TOKEN", "BAKERY_FRAME", "FARMHOUSE_FRAME", "STOREHOUSE_FRAME", "MORALE_FRAME", "HEALING_FRAME", "LUCKYCHARM_FRAME", "LOOT_FRAME"]
VALID_ITEM_TYPES = [
"IRON_FRAME",
"BAKERY_TOKEN",
"FARMHOUSE_TOKEN",
"STOREHOUSE_TOKEN",
"MORALE_TOKEN",
"HEALING_TOKEN",
"LUCKYCHARM_TOKEN",
"LOOT_TOKEN",
"BAKERY_FRAME",
"FARMHOUSE_FRAME",
"STOREHOUSE_FRAME",
"MORALE_FRAME",
"HEALING_FRAME",
"LUCKYCHARM_FRAME",
"LOOT_FRAME",
]


class MarketFrame(tkinter.Frame):
def __init__(self, parent, bg="gray"):
super().__init__(parent, width=650, height=600, bg=bg)
self.parent = parent
self.bg = bg

self.item_selector = dropdowns.Dropdown(
self, VALID_ITEM_TYPES
)
self.item_selector = dropdowns.Dropdown(self, VALID_ITEM_TYPES)
self.item_selector_label = labels.InputLabel(self, "Item", bg="white")

self.buy_offers = tkinter.Listbox(self, bg=self.bg)
self.buy_offers_label = labels.InputLabel(self, "Buy Offers", bg="white")

self.sell_offers = tkinter.Listbox(self, bg=self.bg)
self.sell_offers_label = labels.InputLabel(self, "Sell Offers", bg="white")

self.refresh_button = tkinter.Button(self, bg="green", text="Refresh", command=self.update_offers)
self.refresh_button = tkinter.Button(
self, bg="green", text="Refresh", command=self.update_offers
)

self.amount = inputs.IntergerOnlyEntry(self, 0, 100)
self.amount_label = labels.InputLabel(self, "Amount", bg=self.bg)
Expand All @@ -31,7 +48,9 @@ def __init__(self, parent, bg="gray"):
self.instant_buy_button = tkinter.Button(self, bg="green", text="Buy Instantly")
self.instant_sell_button = tkinter.Button(self, bg="red", text="Sell Instantly")

self.accept_offer_button = tkinter.Button(self, bg="yellow", text="Accept Offer")
self.accept_offer_button = tkinter.Button(
self, bg="yellow", text="Accept Offer"
)

self.create_offer_button = tkinter.Button(self, bg="green", text="Create Offer")

Expand All @@ -40,14 +59,14 @@ def __init__(self, parent, bg="gray"):
def render(self):
self.item_selector.place(x=100, y=50)
self.item_selector_label.place(x=100, y=20)

self.buy_offers.place(x=100, y=190)
self.buy_offers_label.place(x=100, y=160)
self.sell_offers.place(x=275, y=190)
self.sell_offers_label.place(x=275, y=160)

self.refresh_button.place(x=500, y=30)

self.amount.place(x=500, y=100)
self.amount_label.place(x=500, y=80)

Expand All @@ -65,7 +84,7 @@ def update_offers(self) -> None:
item = self.item_selector.get_selection().get()
buy_orders = self.parent.backend.get_market_orders(item, "buy")
sell_orders = self.parent.backend.get_market_orders(item, "sell")

self.buy_offers.delete(0, "end")
self.sell_offers.delete(0, "end")
for order in buy_orders:
Expand Down
Loading
Loading