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

[Code Cleanup]: Very Unfinished: Reducing String Data Checks and Code with For Loops and Enums #2615

Draft
wants to merge 6 commits into
base: development
Choose a base branch
from
Draft
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
5 changes: 3 additions & 2 deletions scripts/clan.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from scripts.cat.history import History
from scripts.cat.names import names
from scripts.cat.sprites import sprites
from scripts.enums import BIOME, SEASON
from scripts.clan_resources.freshkill import FreshkillPile, Nutrition
from scripts.events_module.generate_events import OngoingEvent
from scripts.game_structure.game_essentials import game
Expand All @@ -39,7 +40,7 @@ class Clan:

"""

BIOME_TYPES = ["Forest", "Plains", "Mountainous", "Beach"]
BIOME_TYPES = list(BIOME)

CAT_TYPES = [
"newborn",
Expand Down Expand Up @@ -93,7 +94,7 @@ def __init__(
leader=None,
deputy=None,
medicine_cat=None,
biome="Forest",
biome=BIOME.FOREST,
camp_bg=None,
symbol=None,
game_mode="classic",
Expand Down
13 changes: 13 additions & 0 deletions scripts/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from enum import Enum

class BIOME(Enum):
FOREST = 0
MOUNTAINOUS = 1
PLAINS = 2
BEACH = 3

class SEASON(Enum):
NEWLEAF = 0
GREENLEAF = 1
LEAFFALL = 2
LEAFBARE = 3
136 changes: 51 additions & 85 deletions scripts/screens/MakeClanScreen.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from random import choice, randrange
from enum import Enum
from re import sub

import pygame
import pygame_gui

from scripts.enums import BIOME, SEASON
from scripts.utility import get_text_box_theme, scale
from scripts.clan import Clan
from scripts.cat.cats import create_example_cats, create_cat, Cat
Expand All @@ -24,7 +26,6 @@
from ..cat.sprites import sprites
from ..game_structure.windows import SymbolFilterWindow


class MakeClanScreen(Screens):
# UI images
clan_frame_img = pygame.transform.scale(
Expand Down Expand Up @@ -436,33 +437,21 @@ def handle_choose_background_event(self, event):
if event.ui_element == self.elements["previous_step"]:
self.open_choose_members()
elif event.ui_element == self.elements["forest_biome"]:
self.biome_selected = "Forest"
self.biome_selected = BIOME.FOREST
self.selected_camp_tab = 1
self.refresh_text_and_buttons()
elif event.ui_element == self.elements["mountain_biome"]:
self.biome_selected = "Mountainous"
self.biome_selected = BIOME.MOUNTAINOUS
self.selected_camp_tab = 1
self.refresh_text_and_buttons()
elif event.ui_element == self.elements["plains_biome"]:
self.biome_selected = "Plains"
self.biome_selected = BIOME.PLAINS
self.selected_camp_tab = 1
self.refresh_text_and_buttons()
elif event.ui_element == self.elements["beach_biome"]:
self.biome_selected = "Beach"
self.biome_selected = BIOME.BEACH
self.selected_camp_tab = 1
self.refresh_text_and_buttons()
elif event.ui_element == self.tabs["tab1"]:
self.selected_camp_tab = 1
self.refresh_selected_camp()
elif event.ui_element == self.tabs["tab2"]:
self.selected_camp_tab = 2
self.refresh_selected_camp()
elif event.ui_element == self.tabs["tab3"]:
self.selected_camp_tab = 3
self.refresh_selected_camp()
elif event.ui_element == self.tabs["tab4"]:
self.selected_camp_tab = 4
self.refresh_selected_camp()
elif event.ui_element == self.tabs["newleaf_tab"]:
self.selected_season = "Newleaf"
self.refresh_text_and_buttons()
Expand All @@ -478,36 +467,47 @@ def handle_choose_background_event(self, event):
elif event.ui_element == self.elements["random_background"]:
# Select a random biome and background
self.biome_selected = self.random_biome_selection()
if self.biome_selected in ['Forest', "Mountainous"]:
if self.biome_selected in [BIOME.FOREST, BIOME.MOUNTAINOUS]:
self.selected_camp_tab = randrange(1, 5)
else:
self.selected_camp_tab = randrange(1, 4)
self.refresh_selected_camp()
self.refresh_text_and_buttons()
elif event.ui_element == self.elements["next_step"]:
self.open_choose_symbol()
else:
# Runs through all of the tabs to select the one that matches.
for i in range(1, 5):
if event.ui_element == self.tabs["tab"+str(i)]:
self.selected_camp_tab = i
self.refresh_selected_camp()
break

def handle_choose_background_key(self, event):
if event.key == pygame.K_RIGHT:
if self.biome_selected is None:
self.biome_selected = "Forest"
elif self.biome_selected == "Forest":
self.biome_selected = "Mountainous"
elif self.biome_selected == "Mountainous":
self.biome_selected = "Plains"
elif self.biome_selected == "Plains":
self.biome_selected = "Beach"
self.biome_selected = list(BIOME)[0]
else:
for i in range(len(list(BIOME))):
if self.biome_selected is list(BIOME)[i]:
if i == len(list(BIOME)):
self.biome_selected = list(BIOME)[0]
break
self.biome_selected = list(BIOME)[i+1]
break
self.selected_camp_tab = 1
self.refresh_text_and_buttons()
elif event.key == pygame.K_LEFT:
if self.biome_selected is None:
self.biome_selected = "Beach"
elif self.biome_selected == "Beach":
self.biome_selected = "Plains"
elif self.biome_selected == "Plains":
self.biome_selected = "Mountainous"
elif self.biome_selected == "Mountainous":
self.biome_selected = "Forest"
self.biome_selected = list(BIOME)[len(list(BIOME))]
else:
for i in range(len(list(BIOME))):
if self.biome_selected is list(BIOME)[i]:
if i == 0:
self.biome_selected = list(BIOME)[len(list(BIOME))]
break
self.biome_selected = list(BIOME)[i-1]
break
self.selected_camp_tab = 1
self.refresh_text_and_buttons()
elif event.key == pygame.K_UP and self.biome_selected is not None:
Expand Down Expand Up @@ -734,27 +734,13 @@ def refresh_text_and_buttons(self):

elif self.sub_screen == "choose camp":
# Enable/disable biome buttons
if self.biome_selected == "Forest":
self.elements["forest_biome"].disable()
self.elements["mountain_biome"].enable()
self.elements["plains_biome"].enable()
self.elements["beach_biome"].enable()
elif self.biome_selected == "Mountainous":
self.elements["forest_biome"].enable()
self.elements["mountain_biome"].disable()
self.elements["plains_biome"].enable()
self.elements["beach_biome"].enable()
elif self.biome_selected == "Plains":
self.elements["forest_biome"].enable()
self.elements["mountain_biome"].enable()
self.elements["plains_biome"].disable()
self.elements["beach_biome"].enable()
elif self.biome_selected == "Beach":
self.elements["forest_biome"].enable()
self.elements["mountain_biome"].enable()
self.elements["plains_biome"].enable()
self.elements["beach_biome"].disable()

biomes = ["forest_biome", "mountain_biome", "plains_biome", "beach_biome"] # The possible biomes
for i in range(len(list(BIOME))):
if self.biome_selected is list(BIOME)[i]:
self.elements[biomes[i]].disable()
else:
self.elements[biomes[i]].enable()

if self.selected_season == "Newleaf":
self.tabs["newleaf_tab"].disable()
self.tabs["greenleaf_tab"].enable()
Expand Down Expand Up @@ -803,7 +789,7 @@ def refresh_selected_camp(self):
self.tabs["tab3"].kill()
self.tabs["tab4"].kill()

if self.biome_selected == "Forest":
if self.biome_selected is BIOME.FOREST:
self.tabs["tab1"] = UIImageButton(
scale(pygame.Rect((190, 360), (308, 60))),
"",
Expand All @@ -828,7 +814,7 @@ def refresh_selected_camp(self):
object_id="#lakeside_tab",
manager=MANAGER,
)
elif self.biome_selected == "Mountainous":
elif self.biome_selected is BIOME.MOUNTAINOUS:
self.tabs["tab1"] = UIImageButton(
scale(pygame.Rect((222, 360), (308, 60))),
"",
Expand All @@ -853,7 +839,7 @@ def refresh_selected_camp(self):
object_id="#ruins_tab",
manager=MANAGER
)
elif self.biome_selected == "Plains":
elif self.biome_selected is BIOME.PLAINS:
self.tabs["tab1"] = UIImageButton(
scale(pygame.Rect((128, 360), (308, 60))),
"",
Expand All @@ -872,7 +858,7 @@ def refresh_selected_camp(self):
object_id="#wasteland_tab",
manager=MANAGER,
)
elif self.biome_selected == "Beach":
elif self.biome_selected is BIOME.BEACH:
self.tabs["tab1"] = UIImageButton(
scale(pygame.Rect((152, 360), (308, 60))),
"",
Expand All @@ -892,32 +878,12 @@ def refresh_selected_camp(self):
manager=MANAGER,
)


if self.selected_camp_tab == 1:
self.tabs["tab1"].disable()
self.tabs["tab2"].enable()
self.tabs["tab3"].enable()
self.tabs["tab4"].enable()
elif self.selected_camp_tab == 2:
self.tabs["tab1"].enable()
self.tabs["tab2"].disable()
self.tabs["tab3"].enable()
self.tabs["tab4"].enable()
elif self.selected_camp_tab == 3:
self.tabs["tab1"].enable()
self.tabs["tab2"].enable()
self.tabs["tab3"].disable()
self.tabs["tab4"].enable()
elif self.selected_camp_tab == 4:
self.tabs["tab1"].enable()
self.tabs["tab2"].enable()
self.tabs["tab3"].enable()
self.tabs["tab4"].disable()
else:
self.tabs["tab1"].enable()
self.tabs["tab2"].enable()
self.tabs["tab3"].enable()
self.tabs["tab4"].enable()
# Disables the selected camp tab, enables others.
for i in range(1, 5):
if i == self.selected_camp_tab:
self.tabs["tab"+str(i)].disable()
else:
self.tabs["tab"+str(i)].enable()

# I have to do this for proper layering.
if "camp_art" in self.elements:
Expand Down Expand Up @@ -1144,7 +1110,7 @@ def random_clan_name(self):
def random_biome_selection(self):
# Select a random biome and background
old_biome = self.biome_selected
possible_biomes = ['Forest', 'Mountainous', 'Plains', 'Beach']
possible_biomes = list(BIOME)
# ensuring that the new random camp will not be the same one
if old_biome is not None:
possible_biomes.remove(old_biome)
Expand Down Expand Up @@ -1962,7 +1928,7 @@ def get_camp_art_path(self, campnum):
if game.settings["dark mode"]:
light_dark = "dark"

biome = self.biome_selected.lower()
biome = self.biome_selected.name.lower()

if campnum:
return f"{camp_bg_base_dir}/{biome}/{start_leave}_camp{campnum}_{light_dark}.png"
Expand Down
11 changes: 6 additions & 5 deletions tests/test_thoughts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from scripts.cat.cats import Cat
from scripts.cat.thoughts import Thoughts
from scripts.enums import BIOME, SEASON

os.environ["SDL_VIDEODRIVER"] = "dummy"
os.environ["SDL_AUDIODRIVER"] = "dummy"
Expand All @@ -12,7 +13,7 @@ class TestNotWorkingThoughts(unittest.TestCase):
def setUp(self):
self.main = Cat(status="warrior")
self.other = Cat(status="warrior")
self.biome = "Forest"
self.biome = BIOME.FOREST
self.season = "Newleaf"
self.camp = "camp2"

Expand Down Expand Up @@ -77,7 +78,7 @@ def test_medicine_thought(self):
medicine.status = "medicine cat"
warrior.status = "warrior"
medicine.trait = "bold"
biome = "Forest"
biome = BIOME.FOREST
season = "Newleaf"
camp = "camp2"

Expand All @@ -92,7 +93,7 @@ def test_exiled_thoughts(self):
cat = Cat(status="exiled", moons=40)
cat.exiled = True
cat.outside = True
biome = "Forest"
biome = BIOME.FOREST
season = "Newleaf"
camp = "camp2"

Expand All @@ -103,7 +104,7 @@ def test_lost_thoughts(self):
# given
cat = Cat(status="warrior", moons=40)
cat.outside = True
biome = "Forest"
biome = BIOME.FOREST
season = "Newleaf"
camp = "camp2"

Expand All @@ -117,7 +118,7 @@ def test_family_thought_young_children(self):
# given
parent = Cat(moons=40)
kit = Cat(parent1=parent.ID, moons=4)
biome = "Forest"
biome = BIOME.FOREST
season = "Newleaf"
camp = "camp2"

Expand Down
Loading