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

Dialogue view placeholder #199

Merged
merged 8 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
21 changes: 20 additions & 1 deletion dnd-bot/dnd_bot/assets/campaigns/campaign.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"description": "You're a hard, tough worker.",
"long-description": "A hardy and sturdy race, dwarves are known for their craftsmanship and mining skills. They are short but strong and resilient. They have the best ability to see in the dark. \n\n\n",
"stats": {
"perception": 3,
"perception": 2,
"hp": 18,
"strength": 7,
"action-points": 9
Expand Down Expand Up @@ -349,6 +349,25 @@
"ai": 0
}
},
"npc" : {
"Traveller" : {
"sprite_path": "dnd_bot/assets/gfx/entities/creatures/traveller.png",
"hp": 20,
"strength": 3,
"dexterity": 2,
"intelligence": 4,
"charisma": 6,
"perception": 3,
"initiative": 5,
"action_points": 1,
"level": 1,
"equipment": {},
"drop_money": [0, 0],
"drops": {},
"creature_class": "",
"ai": -1
}
},
"map_elements": {
"Rock": "dnd_bot/assets/gfx/entities/mushrooms.png",
"Mushrooms": "dnd_bot/assets/gfx/entities/rock.png",
Expand Down
5 changes: 3 additions & 2 deletions dnd-bot/dnd_bot/assets/maps/map.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 27, 27, 0 , 0 , 0 , 2 , 2 , 0 , 2 , 0 , 56],
[27, 27, 27, 27, 27, 27, 27, 22, 0 , 27, 27, 0 , 0 , 0 , 0 , 0 , 0 , 2 , 0 , 0 ],
[27, 27, 27, 27, 27, 27, 21, 27, 0 , 27, 27, 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 2 ],
[1 , 0 , 1 , 0 , 1 , 0 , 22, 22, 0 , 27, 21, 27, 27, 27, 22, 0 , 0 , 54, 0 , 0 ],
[1 , 0 , 1 , 0 , 1 , 90, 22, 22, 0 , 27, 21, 27, 27, 27, 22, 0 , 0 , 54, 0 , 0 ],
[0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 22, 27, 27, 27, 27, 22, 0 , 0 , 0 , 4 , 0 ],
[1 , 0 , 1 , 0 , 1 , 0 , 22, 22, 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 4 ],
[27, 27, 27, 27, 27, 27, 21, 21, 27, 27, 27, 27, 27, 27, 22, 0 , 0 , 4 , 0 , 4 ]
Expand Down Expand Up @@ -50,6 +50,7 @@
"53": "Lizardfolk archer",
"54": "Nothic",
"55": "Skeleton morningstar",
"56": "Skeleton warrior"
"56": "Skeleton warrior",
"90": "Traveller"
}
}
1 change: 1 addition & 0 deletions dnd-bot/dnd_bot/dc/ui/message_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ async def creature_turn_embed(token, user_id, recent_action=''):
text = f'{active_creature.name}\'s turn'
else:
text = f'{game.last_visible_creature.name}\'s turn'

embed.set_footer(text=text)

return embed
Expand Down
78 changes: 77 additions & 1 deletion dnd-bot/dnd_bot/dc/ui/views/view_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from dnd_bot.dc.utils.handler_views import HandlerViews
from dnd_bot.logic.game.handler_attack import HandlerAttack
from dnd_bot.logic.game.handler_loot_corpse import HandlerLootCorpse
from dnd_bot.logic.game.handler_dialogue import HandlerDialogue
from dnd_bot.logic.game.handler_manage_items import HandlerManageItems
from dnd_bot.logic.game.handler_movement import HandlerMovement
from dnd_bot.logic.game.handler_skills import HandlerSkills
Expand Down Expand Up @@ -246,6 +247,10 @@ def __init__(self, token, user_discord_id):
custom_id='more-actions-loot')
loot_corpse_button.callback = self.loot_corpse

dialogue_button = Button(label='Talk to...', style=nextcord.ButtonStyle.blurple, row=0,
custom_id='more-actions-dialogue')
dialogue_button.callback = self.talk

cancel_button = Button(label='Cancel', style=nextcord.ButtonStyle.red, row=1,
custom_id='more-actions-cancel')
cancel_button.callback = self.cancel
Expand All @@ -256,12 +261,29 @@ def __init__(self, token, user_discord_id):
if self.player.can_loot_corpse:
self.add_item(loot_corpse_button)

if self.player.can_talk:
self.add_item(dialogue_button)

self.add_item(cancel_button)

async def loot_corpse(self, interaction: nextcord.Interaction):
""" button callback for looting the corpse"""
try:
await HandlerLootCorpse.handle_loot_corpse(self.player)
await HandlerDialogue.handle_talk(self.player)
gitesthuman marked this conversation as resolved.
Show resolved Hide resolved
except DiscordDndBotException as e:
await Messager.send_dm_error_message(user_id=interaction.user.id,
token=self.token,
content=f"**{e}**")

async def talk(self, interaction: nextcord.Interaction):
""" button callback for looting the corpse"""
gitesthuman marked this conversation as resolved.
Show resolved Hide resolved
try:
turn_view_embed = await MessageTemplates.creature_turn_embed(self.token, interaction.user.id)
self.game.players_views[self.user_discord_id] = (ViewDialog, [])
await Messager.edit_last_user_message(user_id=interaction.user.id,
token=self.token,
embeds=[turn_view_embed],
view=ViewDialog(self.token, self.user_discord_id))
except DiscordDndBotException as e:
await Messager.send_dm_error_message(user_id=interaction.user.id,
token=self.token,
Expand Down Expand Up @@ -556,3 +578,57 @@ async def use_skill(skill, id_user, token, interaction: nextcord.Interaction):
await HandlerViews.display_views_for_users(token, message)
except DiscordDndBotException as e:
await Messager.send_dm_error_message(id_user, token, f"**{e}**")


class ViewDialog(ViewGame):
def __init__(self, token, user_discord_id):
super().__init__(token, user_discord_id)
from dnd_bot.logic.prototype.entities.creatures.npc import NPC
game = Multiverse.get_game(token)
self.interlocutors = [e for e in game.get_player_by_id_user(user_discord_id).get_entities_around()
if isinstance(e, NPC)]
interlocutors_select_options = []

for s in self.interlocutors:
interlocutors_select_options.append(nextcord.SelectOption(
label=f"{s.name} at ({s.x}, {s.y})", value=s.id
))
self.select_interlocutor_list = nextcord.ui.Select(
placeholder="Choose the interlocutor",
options=interlocutors_select_options,
row=0
)

self.add_item(self.select_interlocutor_list)

dialogue_button = Button(label='Talk', style=nextcord.ButtonStyle.blurple, row=1,
custom_id='dialogue-action-button')
dialogue_button.callback = self.dialogue_button
self.add_item(dialogue_button)

cancel_button = Button(label='Cancel', style=nextcord.ButtonStyle.red, row=1,
custom_id='dialogue-cancel-button')
cancel_button.callback = self.cancel
self.add_item(cancel_button)

async def dialogue_button(self, interaction: nextcord.Interaction):
await ViewDialog.talk(self.select_interlocutor_list.values[0],
interaction.user.id, self.token, interaction)

async def cancel(self, interaction: nextcord.Interaction):
player = self.game.get_player_by_id_user(interaction.user.id)
await super().cancel(interaction, [get_player_view(self.game, player)])

@staticmethod
async def talk(target_id, id_user, token, interaction: nextcord.Interaction):
"""talk to the target from the available interlocutors list"""
try:
game = Multiverse.get_game(token)
player = game.get_player_by_id_user(id_user)
target = game.get_entity_by_id(target_id)

message = await HandlerDialogue.handle_talk(player, target)

await HandlerViews.display_views_for_users(token, message, False)
except DiscordDndBotException as e:
await Messager.send_dm_error_message(id_user, token, f"**{e}**")
1 change: 0 additions & 1 deletion dnd-bot/dnd_bot/dc/utils/handler_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ async def send_view(user):
game.user_list.remove(user)
del game.players_views[user.discord_id]
return

if update_pov: # if to generate new player's pov
player_view = get_player_view(Multiverse.get_game(game_token), player, player.attack_mode)
turn_view_embed = await MessageTemplates.creature_turn_embed(game_token, user.discord_id,
Expand Down
5 changes: 4 additions & 1 deletion dnd-bot/dnd_bot/logic/game/game_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ async def start_loop(game_token):

first_creature = game.creatures_queue.popleft()
game.active_creature = first_creature
game.last_visible_creature = first_creature
for c in game.creatures_queue:
if c.visible_for_players() or isinstance(c, Player):
game.last_visible_creature = c
break

for user in game.user_list:
game.players_views[user.discord_id] = (ViewCharacterNonActive, [])
Expand Down
7 changes: 7 additions & 0 deletions dnd-bot/dnd_bot/logic/game/handler_dialogue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class HandlerDialogue:

@staticmethod
async def handle_talk(player, target):
""" handles giving loot for the player and removing the corpse from the world """
gitesthuman marked this conversation as resolved.
Show resolved Hide resolved

return f"{target.name}: \"Hello, {player.name}!\""
4 changes: 3 additions & 1 deletion dnd-bot/dnd_bot/logic/game/handler_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ async def end_turn(game_token, visible: bool = True):
# delete any error messages that were left out
await Messager.delete_last_user_error_message(game.active_creature.discord_identity, game_token)

recent_action_message = MessageTemplates.end_turn_recent_action_message(game.last_visible_creature)
recent_action_message = MessageTemplates.end_turn_recent_action_message(game.last_visible_creature) \
if game.last_visible_creature else ""

# reset creature's action points to the initial value
game.active_creature.action_points = game.active_creature.initial_action_points
Expand All @@ -52,6 +53,7 @@ async def turn(game_token, active_creature):
await asyncio.sleep(1)
print(f"{active_creature.name}<{active_creature.id}>", recent_action_message)
if active_creature.visible_for_players():
game.last_visible_creature = active_creature
await HandlerViews.display_views_for_users(game_token, recent_action_message)

await HandlerGame.end_turn(game_token, active_creature.visible_for_players())
Expand Down
34 changes: 21 additions & 13 deletions dnd-bot/dnd_bot/logic/game/initialize_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from dnd_bot.database.database_entity import DatabaseEntity
from dnd_bot.database.database_player import DatabasePlayer
from dnd_bot.logic.prototype.creature import Creature
gitesthuman marked this conversation as resolved.
Show resolved Hide resolved
from dnd_bot.logic.prototype.entities.creatures.enemy import Enemy
from dnd_bot.logic.prototype.entities.creatures.npc import NPC
from dnd_bot.logic.prototype.entity import Entity
from dnd_bot.logic.prototype.equipment import Equipment
from dnd_bot.logic.prototype.items.item import Item
Expand All @@ -30,6 +32,7 @@ def load_entities(game, map_path, campaign_path):
with open(campaign_path) as file:
campaign_json = json.load(file)
enemies_json = campaign_json['entities']['enemies']
npc_json = campaign_json['entities']['npc']
map_elements_json = campaign_json['entities']['map_elements']

entities = []
Expand All @@ -45,7 +48,7 @@ def load_entities(game, map_path, campaign_path):
entities_row.append(None)
else:
entities_row = InitializeWorld.add_entity(entities_row, entity_types[str(entity)], x, y,
game.token, game.id, enemies_json,
game.token, game.id, enemies_json, npc_json,
map_elements_json)

entities.append(entities_row)
Expand Down Expand Up @@ -142,21 +145,26 @@ def spawn_players_positions(spawning_points, num_players):
return players_positions

@staticmethod
def add_entity(entity_row, entity_name, x, y, game_token, game_id, enemies_json, map_elements_json):
def add_entity(entity_row, entity_name, x, y, game_token, game_id, enemies_json, npc_json, map_elements_json):
"""adds entity of class to entity matrix in game
:param entity_row: representing row of entity matrix
:param entity_name: name of entity to be added
:param x: entity x position
:param y: entity y position
:param map_elements_json: data of map elements
:param enemies_json: data of enemies
:param npc_json: data of npc
:param game_id: id from database
:param game_token: game token
"""

if entity_name in enemies_json:
entity_row = InitializeWorld.add_creature(entity_row, x, y, entity_name, game_token, game_id,
enemies_json[entity_name])
enemies_json[entity_name], "Enemy")
return entity_row
if entity_name in npc_json:
entity_row = InitializeWorld.add_creature(entity_row, x, y, entity_name, game_token, game_id,
npc_json[entity_name], "NPC")
return entity_row

entity = Entity(x=x, y=y, sprite=map_elements_json[entity_name], name=entity_name, game_token=game_token)
Expand All @@ -166,16 +174,16 @@ def add_entity(entity_row, entity_name, x, y, game_token, game_id, enemies_json,
return entity_row

@staticmethod
def add_creature(entity_row, x, y, name, game_token, game_id, entity_data):
entity = Creature(game_token=game_token, x=x, y=y, sprite=entity_data['sprite_path'], name=name,
hp=entity_data['hp'],
strength=entity_data['strength'], dexterity=entity_data['dexterity'],
intelligence=entity_data['intelligence'],
charisma=entity_data['charisma'], perception=entity_data['perception'],
initiative=entity_data['initiative'],
action_points=entity_data['action_points'], level=entity_data['level'],
drop_money=entity_data['drop_money'], drops=entity_data['drops'],
creature_class=entity_data['creature_class'], ai=entity_data['ai'])
def add_creature(entity_row, x, y, name, game_token, game_id, entity_data, creature_type="Creature"):
entity = eval(creature_type)(game_token=game_token, x=x, y=y, sprite=entity_data['sprite_path'], name=name,
hp=entity_data['hp'],
strength=entity_data['strength'], dexterity=entity_data['dexterity'],
intelligence=entity_data['intelligence'],
charisma=entity_data['charisma'], perception=entity_data['perception'],
initiative=entity_data['initiative'],
action_points=entity_data['action_points'], level=entity_data['level'],
drop_money=entity_data['drop_money'], drops=entity_data['drops'],
creature_class=entity_data['creature_class'], ai=entity_data['ai'])

id_entity = DatabaseEntity.add_entity(name=name, x=x, y=y, id_game=game_id)
entity.id = id_entity
Expand Down
Loading