Skip to content

Commit

Permalink
Clean up action class
Browse files Browse the repository at this point in the history
  • Loading branch information
Pbatch committed Jun 19, 2024
1 parent b33ea27 commit a2ecaf3
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 234 deletions.
2 changes: 0 additions & 2 deletions clashroyalebuildabot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from . import constants
from .bot import Action
from .bot import Bot
from .bot import PeteBot
from .bot import RandomBot
from .bot import TwoSixHogCycle
from .namespaces import Cards
Expand All @@ -17,7 +16,6 @@

__all__ = [
"RandomBot",
"PeteBot",
"TwoSixHogCycle",
"constants",
"Cards",
Expand Down
2 changes: 0 additions & 2 deletions clashroyalebuildabot/bot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# Exports for bot submodule
from .action import Action
from .bot import Bot
from .pete import PeteBot
from .random import RandomBot
from .two_six_hog_cycle import TwoSixHogCycle

__all__ = [
"TwoSixHogCycle",
"PeteBot",
"RandomBot",
"Action",
"Bot",
Expand Down
12 changes: 3 additions & 9 deletions clashroyalebuildabot/bot/action.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
class Action:
def __init__(
self, index, tile_x, tile_y, name, cost, type_, target, ready
):
def __init__(self, index, tile_x, tile_y, card):
self.index = index
self.tile_x = tile_x
self.tile_y = tile_y
self.name = name
self.cost = cost
self.type = type_
self.target = target
self.ready = ready
self.card = card

def __repr__(self):
return f"{self.name} at ({self.tile_x}, {self.tile_y})"
return f"{self.card.name} at ({self.tile_x}, {self.tile_y})"
21 changes: 8 additions & 13 deletions clashroyalebuildabot/bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,15 @@ def get_actions(self):
all_tiles = ALLY_TILES + LEFT_PRINCESS_TILES + RIGHT_PRINCESS_TILES
valid_tiles = self._get_valid_tiles()
actions = []
for i in range(4):
for i in self.state["ready"]:
card = self.state["cards"][i + 1]
if (
int(self.state["numbers"]["elixir"]["number"]) >= card["cost"]
and card["ready"]
and card["name"] != "blank"
):
tiles = all_tiles if card["target_anywhere"] else valid_tiles
actions.extend(
[
self.action_class(i, x, y, *card.values())
for (x, y) in tiles
]
)
if int(self.state["numbers"]["elixir"]["number"]) < card.cost:
continue

tiles = all_tiles if card.target_anywhere else valid_tiles
actions.extend(
[self.action_class(i, x, y, card) for (x, y) in tiles]
)

return actions

Expand Down
20 changes: 10 additions & 10 deletions clashroyalebuildabot/bot/example/custom_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,16 @@ def _calculate_musketeer_score(self, state):
return [0]

def calculate_score(self, state):
name_to_score = {
Cards.KNIGHT.name: self._calculate_knight_score,
Cards.MINIONS.name: self._calculate_minions_score,
Cards.FIREBALL.name: self._calculate_fireball_score,
Cards.GIANT.name: self._calculate_giant_score,
Cards.MINIPEKKA.name: self._calculate_minipekka_score,
Cards.MUSKETEER.name: self._calculate_musketeer_score,
Cards.ARROWS.name: self._calculate_arrows_score,
Cards.ARCHERS.name: self._calculate_archers_score,
card_to_score = {
Cards.KNIGHT: self._calculate_knight_score,
Cards.MINIONS: self._calculate_minions_score,
Cards.FIREBALL: self._calculate_fireball_score,
Cards.GIANT: self._calculate_giant_score,
Cards.MINIPEKKA: self._calculate_minipekka_score,
Cards.MUSKETEER: self._calculate_musketeer_score,
Cards.ARROWS: self._calculate_arrows_score,
Cards.ARCHERS: self._calculate_archers_score,
}
score_function = name_to_score.get(self.name)
score_function = card_to_score.get(self.card)
self.score = score_function(state) if score_function else [0]
return self.score
4 changes: 0 additions & 4 deletions clashroyalebuildabot/bot/pete/__init__.py

This file was deleted.

116 changes: 0 additions & 116 deletions clashroyalebuildabot/bot/pete/pete_action.py

This file was deleted.

60 changes: 0 additions & 60 deletions clashroyalebuildabot/bot/pete/pete_bot.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,18 @@ def _calculate_fireball_score(self, state):
)

def calculate_score(self, state):
name_to_score = {
Cards.HOG_RIDER.name: self._calculate_hog_rider_score,
Cards.ICE_GOLEM.name: self._calculate_ice_golem_score,
Cards.FIREBALL.name: self._calculate_fireball_score,
Cards.ICE_SPIRIT.name: self._calculate_ice_spirit_score,
Cards.THE_LOG.name: self._calculate_log_score,
Cards.MUSKETEER.name: self._calculate_musketeer_score,
Cards.CANNON.name: self._calculate_cannon_score,
Cards.SKELETONS.name: self._calculate_ice_spirit_score,
card_to_score = {
Cards.HOG_RIDER: self._calculate_hog_rider_score,
Cards.ICE_GOLEM: self._calculate_ice_golem_score,
Cards.FIREBALL: self._calculate_fireball_score,
Cards.ICE_SPIRIT: self._calculate_ice_spirit_score,
Cards.THE_LOG: self._calculate_log_score,
Cards.MUSKETEER: self._calculate_musketeer_score,
Cards.CANNON: self._calculate_cannon_score,
Cards.SKELETONS: self._calculate_ice_spirit_score,
}

score_function = name_to_score[self.name]
score_function = card_to_score[self.card]
score = score_function(state)
self.score = score
return score
17 changes: 10 additions & 7 deletions clashroyalebuildabot/state/card_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,19 @@ def _detect_cards(self, image):
np.amin(np.abs(crop_hashes - self.card_hashes), axis=1), axis=1
).T
_, idx = linear_sum_assignment(hash_diffs)
cards = [self.cards[i].__dict__ for i in idx]
cards = [self.cards[i] for i in idx]

return cards, crops

def _detect_if_ready(self, cards, crops):
for card, crop in zip(cards, crops):
def _detect_if_ready(self, crops):
ready = []
for i, crop in enumerate(crops[1:]):
std = np.mean(np.std(np.array(crop), axis=2))
card["ready"] = std > self.grey_std_threshold
return cards
if std > self.grey_std_threshold:
ready.append(i)
return ready

def run(self, image):
cards, crops = self._detect_cards(image)
cards = self._detect_if_ready(cards, crops)
return cards
ready = self._detect_if_ready(crops)
return cards, ready
4 changes: 3 additions & 1 deletion clashroyalebuildabot/state/detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ def __init__(self, cards, debug=False):
self.debugger = Debugger()

def run(self, image):
cards, ready = self.card_detector.run(image)
state = {
"units": self.unit_detector.run(image),
"numbers": self.number_detector.run(image),
"cards": self.card_detector.run(image),
"cards": cards,
"ready": ready,
"screen": self.screen_detector.run(image),
}

Expand Down

0 comments on commit a2ecaf3

Please sign in to comment.