Skip to content

Commit

Permalink
Fix bugs (sacrificial weapons generate OMNI element)
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuai-gao committed Feb 20, 2023
1 parent 136eb48 commit ca3955d
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 24 deletions.
36 changes: 25 additions & 11 deletions gisim/agent.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Player & agent APIs
"""
import enum
from abc import ABC, abstractmethod
from collections import Counter
import enum
from typing import OrderedDict

from gisim.cards.characters import get_character_card
Expand Down Expand Up @@ -113,12 +113,16 @@ def get_dice_idx_greedy(
if remaining > 0:
return []
elif key == ElementType.SAME:
dice_counter = Counter([dice[idx] for idx, die_used in enumerate(used) if not die_used])
dice_counter = Counter(
[dice[idx] for idx, die_used in enumerate(used) if not die_used]
)
if ElementType.OMNI not in dice_counter.keys():
dice_counter[ElementType.OMNI] = 0
if char_element not in dice_counter.keys():
dice_counter[char_element] = 0
sorted_counter = dict(sorted(dice_counter.items(), key=lambda item: item[1]))
sorted_counter = dict(
sorted(dice_counter.items(), key=lambda item: item[1])
)
satisfied = False
for element, cnt in sorted_counter.items():
if element in [ElementType.OMNI, char_element]:
Expand All @@ -138,7 +142,9 @@ def get_dice_idx_greedy(
if satisfied:
continue
omni_count = sorted_counter[ElementType.OMNI]
sorted_counter = dict(sorted(dice_counter.items(), key=lambda item: item[1], reverse=True))
sorted_counter = dict(
sorted(dice_counter.items(), key=lambda item: item[1], reverse=True)
)
for element, cnt in sorted_counter.items():
if element in [ElementType.OMNI, char_element]:
continue
Expand Down Expand Up @@ -182,7 +188,7 @@ def get_dice_idx_greedy(
break
if not satisfied:
return []

return dice_idx

def take_action(self, game_info: GameInfo) -> Action:
Expand Down Expand Up @@ -237,7 +243,9 @@ def take_action(self, game_info: GameInfo) -> Action:
current_dice, {ElementType.CRYO: 2}, character_card.element_type
)
if len(dice_idx) > 0:
card_idx = player_info.hand_cards.index("Kanten Senmyou Blessing")
card_idx = player_info.hand_cards.index(
"Kanten Senmyou Blessing"
)
return UseCardAction(
card_idx=card_idx,
card_target=[
Expand All @@ -247,14 +255,20 @@ def take_action(self, game_info: GameInfo) -> Action:
card_user_pos=active_pos,
)
if "Traveler's Handy Sword" in player_info.hand_cards:
dice_idx = self.get_dice_idx_greedy(current_dice, {ElementType.SAME: 2}, character_card.element_type)
dice_idx = self.get_dice_idx_greedy(
current_dice, {ElementType.SAME: 2}, character_card.element_type
)
if len(dice_idx) > 0:
card_idx = player_info.hand_cards.index("Traveler's Handy Sword")
card_idx = player_info.hand_cards.index(
"Traveler's Handy Sword"
)
return UseCardAction(
card_idx=card_idx,
card_target=[(self.player_id, EntityType.WEAPON, active_pos.value)],
dice_idx = dice_idx,
card_user_pos = active_pos
card_target=[
(self.player_id, EntityType.WEAPON, active_pos.value)
],
dice_idx=dice_idx,
card_user_pos=active_pos,
)
if (
character_info.character.power
Expand Down
1 change: 0 additions & 1 deletion gisim/cards/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,3 @@ def use_card(self, msg_queue: PriorityQueue[Message], game_info: "GameInfo"):
equipment_type=EquipmentType.WEAPON,
)
msg_queue.put(new_msg)

7 changes: 4 additions & 3 deletions gisim/cards/characters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ def get_character_card(character_name: str):
character_name = character_name.replace(" ", "").replace("'", "")
# character_card: CharacterCard = eval(f"{character_name}()")
character_card_class = globals()[character_name]
character_card: CharacterCard = character_card_class()
# Remove the spaces for class names
return character_card_class()
return character_card


def get_skill_type(skill_name: str):
skill_name = skill_name.replace(" ", "").replace("'", "")
skill_class = globals()[skill_name]
skill:CharacterSkill = skill_class()
return skill.type
skill: CharacterSkill = skill_class()
return skill.type
5 changes: 2 additions & 3 deletions gisim/cards/characters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def use_skill(self, msg_queue: PriorityQueue[Message], parent: "CharacterEntity"


class GenericSkill(CharacterSkill):
skill_type: SkillType
damage_element: ElementType = ElementType.NONE
damage_value: int = 0
summon_name: str = ""
Expand Down Expand Up @@ -75,7 +74,7 @@ def use_skill(self, msg_queue: PriorityQueue[Message], parent: "CharacterEntity"
target_player_id, target_char_pos = msg.skill_targets[0]
if self.damage_value > 0:
new_msg = DealDamageMsg(
attack_type=AttackType(self.skill_type.value),
attack_type=AttackType(self.type.value),
attacker=(parent.player_id, parent.position),
sender_id=parent.player_id,
targets=[
Expand All @@ -91,7 +90,7 @@ def use_skill(self, msg_queue: PriorityQueue[Message], parent: "CharacterEntity"

if self.piercing_damage_value > 0:
new_msg = DealDamageMsg(
attack_type=AttackType(self.skill_type.value),
attack_type=AttackType(self.type.value),
attacker=(parent.player_id, parent.position),
sender_id=parent.player_id,
targets=[
Expand Down
2 changes: 1 addition & 1 deletion gisim/cards/equipments/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .artifacts import *
from .talents import *
from .weapons import *
from .weapons import *
24 changes: 20 additions & 4 deletions gisim/cards/equipments/weapons.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from queue import PriorityQueue
from typing import cast

from cards.base import WeaponCard
from cards.characters import get_skill_type
from classes.enums import ElementType, EquipmentType, WeaponType
from classes.equipment import WeaponEntity
from classes.message import AfterUsingSkillMsg, DealDamageMsg, Message

from gisim.classes.enums import SkillType
from gisim.classes.message import ChangeDiceMsg


class MagicGuideCard(WeaponCard):
id: int = 311101
Expand Down Expand Up @@ -187,9 +191,9 @@ class SacrificialSwordCard(WeaponCard):
class SacrificialSword(WeaponEntity):
name: str = "Sacrificial Sword"
weapon_type: WeaponType = WeaponType.SWORD

def msg_handler(self, msg_queue: PriorityQueue[Message]):
# Increase 1 dmg by default without any advanced effects
# Increase 1 dmg by default without any advanced effects
top_msg = msg_queue.queue[0]
updated = False
if self._uuid in top_msg.responded_entities:
Expand All @@ -210,8 +214,20 @@ def msg_handler(self, msg_queue: PriorityQueue[Message]):
)
updated = True
if isinstance(top_msg, AfterUsingSkillMsg):
top_msg = cast(AfterUsingSkillMsg, top_msg)
if get_skill_type(top_msg.skill_name)
if self.active == True and self.triggered_in_a_round == 0:
top_msg = cast(AfterUsingSkillMsg, top_msg)
if get_skill_type(top_msg.skill_name) == SkillType.ELEMENTAL_SKILL:
# TODO: Get the element type of the current character (Essentially one need to get all the game information visible for all entities)
new_msg = ChangeDiceMsg(
sender_id=self.player_id,
remove_dice_idx=[],
new_target_element=[ElementType.OMNI],
)
msg_queue.put(new_msg)
updated = True
self.triggered_in_a_round += 1
self.active = False

if updated:
top_msg.responded_entities.append(self._uuid)
return updated
Expand Down
2 changes: 1 addition & 1 deletion gisim/classes/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class AttackType(Enum):
COMBAT_STATUS = auto()
SUMMON = auto()


class CharPos(Enum):
"""Character position"""

Expand Down Expand Up @@ -260,4 +261,3 @@ class EntityType(Enum):
"""For the cards in hand only"""
SUMMON = auto()
SUPPORT = auto()

0 comments on commit ca3955d

Please sign in to comment.