Skip to content

Commit

Permalink
refactor: CreateRandomObjectAction add replace attribute
Browse files Browse the repository at this point in the history
resolves #104
  • Loading branch information
zyr17 committed Mar 24, 2024
1 parent 371e5eb commit 7bc89f5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 28 deletions.
31 changes: 22 additions & 9 deletions src/lpsim/server/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,8 @@ class CreateRandomObjectAction(ActionBase):
"""
Action for creating random objects. A list of names are provided, and
`number` of them will be created by uniformly selecting from the list and doing
`CreateObjectAction` for each of them.
`CreateObjectAction` for each of them. Replace means whether the sample is with or
without replacement. True meaning that a value of a can be selected multiple times.
NOTE: some actions will not generate existing objects, e.g. elemental skill of
Rhodeia will not summoning existing objects unless full. This action will not deal
with such situation, you need to write logic to filter out existing objects before
Expand All @@ -324,15 +325,16 @@ class CreateRandomObjectAction(ActionBase):
object_names: List[str]
object_arguments: dict
number: int
replace: bool

def select_by_idx(
self, idx: int
) -> Tuple[CreateObjectAction, "CreateRandomObjectAction"]:
"""
Select an object name by index and create a `CreateObjectAction` for it.
It will also return a new `CreateRandomObjectAction` with the same
arguments but with the `number` reduced by 1 and the selected object
name removed from the list.
It will also return a new `CreateRandomObjectAction`. If replace is False, with
the same arguments but with the `number` reduced by 1 and the selected object
name removed from the list. If replace is True, only number is decreased by 1.
Returns:
CreateObjectAction: The action for creating the object.
Expand All @@ -341,18 +343,29 @@ def select_by_idx(
"""
selected = self.object_names[idx]
others = self.object_names[:idx] + self.object_names[idx + 1 :]
return (
CreateObjectAction(
if self.replace:
ret_action = CreateRandomObjectAction(
object_position=self.object_position,
object_name=selected,
object_names=self.object_names,
object_arguments=self.object_arguments,
),
CreateRandomObjectAction(
number=self.number - 1,
replace=True,
)
else:
ret_action = CreateRandomObjectAction(
object_position=self.object_position,
object_names=others,
object_arguments=self.object_arguments,
number=self.number - 1,
replace=False,
)
return (
CreateObjectAction(
object_position=self.object_position,
object_name=selected,
object_arguments=self.object_arguments,
),
ret_action,
)


Expand Down
2 changes: 2 additions & 0 deletions src/lpsim/server/card/event/resonance.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ def get_actions(
),
object_arguments={},
number=1,
replace=False,
)
]

Expand Down Expand Up @@ -609,6 +610,7 @@ def get_actions(
),
object_arguments={},
number=1,
replace=False,
)
]

Expand Down
4 changes: 4 additions & 0 deletions src/lpsim/server/character/hydro/rhodeia_3_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def get_actions(self, match: Any) -> List[ChargeAction | CreateRandomObjectActio
object_position=target_position,
object_arguments={"version": self.version},
number=self._summon_number,
replace=False,
)
)
elif len(unexist_names) >= self._summon_number:
Expand All @@ -138,6 +139,7 @@ def get_actions(self, match: Any) -> List[ChargeAction | CreateRandomObjectActio
object_position=target_position,
object_arguments={"version": self.version},
number=self._summon_number,
replace=False,
)
)
else:
Expand All @@ -148,6 +150,7 @@ def get_actions(self, match: Any) -> List[ChargeAction | CreateRandomObjectActio
object_position=target_position,
object_arguments={"version": self.version},
number=len(unexist_names),
replace=False,
)
)
ret.append(
Expand All @@ -156,6 +159,7 @@ def get_actions(self, match: Any) -> List[ChargeAction | CreateRandomObjectActio
object_position=target_position,
object_arguments={"version": self.version},
number=self._summon_number - len(unexist_names),
replace=False,
)
)
ret.append(self.charge_self(1))
Expand Down
4 changes: 4 additions & 0 deletions src/lpsim/server/patch/v43/balance/rhodeia_of_loch_4_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def get_actions(
object_position=target_position,
object_arguments={"version": self.version},
number=self._summon_number,
replace=False,
)
)
elif (
Expand All @@ -85,6 +86,7 @@ def get_actions(
object_position=target_position,
object_arguments={"version": self.version},
number=self._summon_number,
replace=False,
)
)
else:
Expand All @@ -95,6 +97,7 @@ def get_actions(
object_position=target_position,
object_arguments={"version": self.version},
number=len(unexist_names) - 1,
replace=False,
)
)
ret.append(
Expand All @@ -103,6 +106,7 @@ def get_actions(
object_position=target_position,
object_arguments={"version": self.version},
number=self._summon_number - len(unexist_names) + 1,
replace=False,
)
)
ret.append(self.charge_self(1))
Expand Down
1 change: 1 addition & 0 deletions src/lpsim/server/patch/v43/cards/mamere_4_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def event_handler_USE_CARD(
object_position=position,
object_arguments=args,
number=1,
replace=True,
)
)
res += self.check_should_remove()
Expand Down
44 changes: 25 additions & 19 deletions src/lpsim/server/patch/v44/cards/sunyata_flower_4_4.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from typing import Any, Dict, List, Literal, Type
from pydantic import PrivateAttr

from ....action import ActionTypes, Actions, CreateObjectAction, RemoveObjectAction
from ....action import (
ActionTypes,
Actions,
CreateObjectAction,
CreateRandomObjectAction,
RemoveObjectAction,
)
from .....utils.class_registry import get_instance, register_class
from ....event import GameStartEventArguments, MoveObjectEventArguments
from ....modifiable_values import CostValue
Expand Down Expand Up @@ -109,30 +115,30 @@ def get_targets(self, match: Match) -> List[ObjectPosition]:

def get_actions(
self, target: ObjectPosition | None, match: Match
) -> List[RemoveObjectAction | CreateObjectAction]:
) -> List[RemoveObjectAction | CreateObjectAction | CreateRandomObjectAction]:
"""
remove target, and generate 2 random support cards, and create status
"""
assert target is not None
ret: List[RemoveObjectAction | CreateObjectAction] = [
RemoveObjectAction(object_position=target)
]
ret: List[
RemoveObjectAction | CreateObjectAction | CreateRandomObjectAction
] = [RemoveObjectAction(object_position=target)]
candidate_list = self._get_candidate_list(match)
for i in range(2):
random_target = candidate_list[int(match._random() * len(candidate_list))]
position = ObjectPosition(
player_idx=self.position.player_idx, area=ObjectPositionType.HAND, id=-1
)
args = {}
if self._accept_version is not None:
args["version"] = self._accept_version
ret.append(
CreateObjectAction(
object_name=random_target,
object_position=position,
object_arguments=args,
)
position = ObjectPosition(
player_idx=self.position.player_idx, area=ObjectPositionType.HAND, id=-1
)
args = {}
if self._accept_version is not None:
args["version"] = self._accept_version
ret.append(
CreateRandomObjectAction(
object_names=candidate_list,
object_position=position,
object_arguments=args,
number=2,
replace=True,
)
)
ret.append(
CreateObjectAction(
object_name=self.name,
Expand Down

0 comments on commit 7bc89f5

Please sign in to comment.