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

refactor initialize_world.py to be more concise #175

Merged
merged 2 commits into from
Apr 4, 2023
Merged
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
232 changes: 114 additions & 118 deletions dnd-bot/dnd_bot/logic/game/initialize_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dnd_bot.logic.character_creation.chosen_attributes import ChosenAttributes
from dnd_bot.database.database_entity import DatabaseEntity
from dnd_bot.database.database_player import DatabasePlayer
from dnd_bot.logic.prototype.creature import Creature
from dnd_bot.logic.prototype.entities.creatures.frost_mage import FrostMage
from dnd_bot.logic.prototype.entities.creatures.half_dragon_assassin import HalfDragonAssassin
from dnd_bot.logic.prototype.entities.creatures.half_dragon_warrior import HalfDragonWarrior
Expand Down Expand Up @@ -35,6 +36,27 @@


class InitializeWorld:
entity_classes_by_name: dict = {
'Rock': Rock,
'Hole': Hole,
'Mushrooms': Mushrooms,
'Frost mage': FrostMage,
'Half dragon assassin': HalfDragonAssassin,
'Half dragon warrior': HalfDragonWarrior,
'Lizardfolk archer': LizardfolkArcher,
'Nothic': Nothic,
'Skeleton morningstar': SkeletonMorningstar,
'Skeleton warrior': SkeletonWarrior,
'Dungeon connector': DungeonConnector,
'Dungeon corner in': DungeonCornerIn,
'Dungeon corner out': DungeonCornerOut,
'Dungeon crossroads': DungeonCrossroads,
'Dungeon pillar A': DungeonPillarA,
'Dungeon pillar B': DungeonPillarB,
'Dungeon pillar C': DungeonPillarC,
'Dungeon straight A': DungeonStraightA,
'Dungeon straight B': DungeonStraightB,
}

@staticmethod
def load_entities(game, map_path):
Expand All @@ -51,137 +73,98 @@ def load_entities(game, map_path):
for y, row in enumerate(entities_json):
entities_row = []
for x, entity in enumerate(row):
if str(entity) not in entity_types.keys():
if str(entity) not in entity_types:
entities_row.append(None)

elif entity_types[str(entity)] == 'Player':
player_spawning_points.append((x, y))
entities_row.append(None)
elif entity_types[str(entity)] == Rock.entity_name:
entities_row = InitializeWorld.add_entity(entities_row, Rock, x, y, game.token, game.id)
elif entity_types[str(entity)] == Hole.entity_name:
entities_row = InitializeWorld.add_entity(entities_row, Hole, x, y, game.token, game.id)
elif entity_types[str(entity)] == Mushrooms.entity_name:
entities_row = InitializeWorld.add_entity(entities_row, Mushrooms, x, y, game.token, game.id,)
elif entity_types[str(entity)] == FrostMage.creature_name:
entities_row = InitializeWorld.add_creature(entities_row, FrostMage, x, y, game.token, game.id,
entity_types[str(entity)])
elif entity_types[str(entity)] == HalfDragonAssassin.creature_name:
entities_row = InitializeWorld.add_creature(entities_row, HalfDragonAssassin, x, y, game.token, game.id,
entity_types[str(entity)])
elif entity_types[str(entity)] == HalfDragonWarrior.creature_name:
entities_row = InitializeWorld.add_creature(entities_row, HalfDragonWarrior, x, y, game.token, game.id,
entity_types[str(entity)])
elif entity_types[str(entity)] == LizardfolkArcher.creature_name:
entities_row = InitializeWorld.add_creature(entities_row, LizardfolkArcher, x, y, game.token, game.id,
entity_types[str(entity)])
elif entity_types[str(entity)] == Nothic.creature_name:
entities_row = InitializeWorld.add_creature(entities_row, Nothic, x, y, game.token, game.id,
entity_types[str(entity)])
elif entity_types[str(entity)] == SkeletonMorningstar.creature_name:
entities_row = InitializeWorld.add_creature(entities_row, SkeletonMorningstar, x, y, game.token, game.id,
entity_types[str(entity)])
elif entity_types[str(entity)] == SkeletonWarrior.creature_name:
entities_row = InitializeWorld.add_creature(entities_row, SkeletonWarrior, x, y, game.token, game.id,
entity_types[str(entity)])

# walls
elif entity_types[str(entity)] == DungeonConnector.entity_name:
entities_row = InitializeWorld.add_entity(entities_row, DungeonConnector, x, y, game.token,
game.id)
elif entity_types[str(entity)] == DungeonCornerIn.entity_name:
entities_row = InitializeWorld.add_entity(entities_row, DungeonCornerIn, x, y, game.token,
game.id)
elif entity_types[str(entity)] == DungeonCornerOut.entity_name:
entities_row = InitializeWorld.add_entity(entities_row, DungeonCornerOut, x, y, game.token,
game.id)
elif entity_types[str(entity)] == DungeonCrossroads.entity_name:
entities_row = InitializeWorld.add_entity(entities_row, DungeonCrossroads, x, y, game.token,
game.id)
elif entity_types[str(entity)] == DungeonPillarA.entity_name:
entities_row = InitializeWorld.add_entity(entities_row, DungeonPillarA, x, y, game.token,
game.id)
elif entity_types[str(entity)] == DungeonPillarB.entity_name:
entities_row = InitializeWorld.add_entity(entities_row, DungeonPillarB, x, y, game.token,
game.id)
elif entity_types[str(entity)] == DungeonPillarC.entity_name:
entities_row = InitializeWorld.add_entity(entities_row, DungeonPillarC, x, y, game.token,
game.id)
elif entity_types[str(entity)] == DungeonStraightA.entity_name:
entities_row = InitializeWorld.add_entity(entities_row, DungeonStraightA, x, y, game.token,
game.id)
elif entity_types[str(entity)] == DungeonStraightB.entity_name:
entities_row = InitializeWorld.add_entity(entities_row, DungeonStraightB, x, y, game.token,
game.id)
else:
entities_row = InitializeWorld.add_entity(entities_row, InitializeWorld.entity_classes_by_name[
entity_types[str(entity)]], x, y, game.token, game.id)

entities.append(entities_row)

# handle entity rotations
game.entities = copy.deepcopy(entities)
InitializeWorld.load_entity_rotations(game, map_path)
InitializeWorld.spawn_players(game, player_spawning_points)

game.sprite = str(map_json['map']['img_file']) # path to raw map image
# generated image of map without fragile entities
game.sprite = cv.imread(get_game_view(game), cv.IMREAD_UNCHANGED)
game.world_width = map_json['map']['size']['x']
game.world_height = map_json['map']['size']['y']

@staticmethod
def load_entity_rotations(game, map_path):
"""
loads initial entity rotations from json
"""
with open(map_path) as file:
map_json = json.load(file)

for y, row in enumerate(map_json['map']['entities_rotation']):
for x, entity_rotation in enumerate(row):
if entities[y][x] is not None:
if game.entities[y][x] is not None:
if entity_rotation == 0:
entities[y][x].look_direction = 'down'
game.entities[y][x].look_direction = 'down'
elif entity_rotation == 1:
entities[y][x].look_direction = 'left'
game.entities[y][x].look_direction = 'left'
elif entity_rotation == 2:
entities[y][x].look_direction = 'up'
game.entities[y][x].look_direction = 'up'
elif entity_rotation == 3:
entities[y][x].look_direction = 'right'
game.entities[y][x].look_direction = 'right'
else:
entities[y][x].look_direction = 'down'

# handle random spawning points
players_positions = InitializeWorld.spawn_players_positions(player_spawning_points, len(game.user_list))
for i, player_pos in enumerate(players_positions):
entities[player_pos[1]].pop(player_pos[0])
if game.user_list[i].discord_id in ChosenAttributes.chosen_attributes:
character = ChosenAttributes.chosen_attributes[game.user_list[i].discord_id]
entities = InitializeWorld.add_player(x=player_pos[0], y=player_pos[1],
name=character['name'],
discord_identity=game.user_list[i].discord_id,
game_token=game.token,
entities=entities,
game_id=game.id,
backstory=character['backstory'],
alignment='-'.join(character['alignment']),
hp=character['hp'],
strength=character['strength'],
dexterity=character['dexterity'],
intelligence=character['intelligence'],
charisma=character['charisma'],
perception=character['perception'],
initiative=character['initiative'],
action_points=character['action points'],
character_race=character['race'],
character_class=character['class'])

ChosenAttributes.delete_user(game.user_list[i].discord_id)
else:
entities = InitializeWorld.add_player(x=player_pos[0], y=player_pos[1],
name=game.user_list[i].username,
discord_identity=game.user_list[i].discord_id,
game_token=game.token,
entities=entities,
game_id=game.id,
backstory='fascinating backstory',
alignment='chaotic-evil',
hp=random.randint(15, 30),
strength=random.randint(1, 5),
dexterity=random.randint(1, 5),
intelligence=random.randint(1, 5),
charisma=random.randint(1, 5),
perception=random.randint(2, 4),
initiative=random.randint(1, 5),
action_points=100,
character_race=random.choice(['Human', 'Elf', 'Dwarf']),
character_class=random.choice(['Warrior', 'Mage', 'Ranger']))

game.entities = copy.deepcopy(entities)
game.sprite = str(map_json['map']['img_file']) # path to raw map image
# generated image of map with not fragile entities
game.sprite = cv.imread(get_game_view(game), cv.IMREAD_UNCHANGED)
game.world_width = map_json['map']['size']['x']
game.world_height = map_json['map']['size']['y']
game.entities[y][x].look_direction = 'down'

@staticmethod
def spawn_players(game, player_spawning_spots):
"""
function spawns players in some spawning spots randomly
"""
players_positions = InitializeWorld.spawn_players_positions(player_spawning_spots, len(game.user_list))
for i, player_pos in enumerate(players_positions):
game.entities[player_pos[1]].pop(player_pos[0])
if game.user_list[i].discord_id in ChosenAttributes.chosen_attributes:
character = ChosenAttributes.chosen_attributes[game.user_list[i].discord_id]
entities = InitializeWorld.add_player(x=player_pos[0], y=player_pos[1],
name=character['name'],
discord_identity=game.user_list[i].discord_id,
game_token=game.token,
entities=game.entities,
game_id=game.id,
backstory=character['backstory'],
alignment='-'.join(character['alignment']),
hp=character['hp'],
strength=character['strength'],
dexterity=character['dexterity'],
intelligence=character['intelligence'],
charisma=character['charisma'],
perception=character['perception'],
initiative=character['initiative'],
action_points=character['action points'],
character_race=character['race'],
character_class=character['class'])
ChosenAttributes.delete_user(game.user_list[i].discord_id)
else:
entities = InitializeWorld.add_player(x=player_pos[0], y=player_pos[1],
name=game.user_list[i].username,
discord_identity=game.user_list[i].discord_id,
game_token=game.token,
entities=game.entities,
game_id=game.id,
backstory='fascinating backstory',
alignment='chaotic-evil',
hp=random.randint(15, 30),
strength=random.randint(1, 5),
dexterity=random.randint(1, 5),
intelligence=random.randint(1, 5),
charisma=random.randint(1, 5),
perception=random.randint(2, 4),
initiative=random.randint(1, 5),
action_points=100,
character_race=random.choice(['Human', 'Elf', 'Dwarf']),
character_class=random.choice(['Warrior', 'Mage', 'Ranger']))

@staticmethod
def spawn_players_positions(spawning_points, num_players):
Expand All @@ -195,7 +178,20 @@ def spawn_players_positions(spawning_points, num_players):

@staticmethod
def add_entity(entity_row, entity_class, x, y, game_token, game_id):
entity = entity_class(x=x, y=y, game_token=game_token, name=entity_class.entity_name, sprite=entity_class.sprite_path)
"""adds entity of class to entity matrix in game
:param entity_row: representing row of entity matrix
:param entity_class: class of entity to be added
:param x: entity x position
:param x: entity y position
"""

if issubclass(entity_class, Creature): # add a creature
entity_row = InitializeWorld.add_creature(entity_row, entity_class, x, y, game_token, game_id,
entity_class.creature_name)
return entity_row

entity = entity_class(x=x, y=y, game_token=game_token, name=entity_class.entity_name,
sprite=entity_class.sprite_path)
id_entity = DatabaseEntity.add_entity(name=entity_class.entity_name, x=x, y=y, id_game=game_id)
entity.id = id_entity
entity_row.append(entity)
Expand Down