Skip to content

Commit

Permalink
feat: add "not" condition
Browse files Browse the repository at this point in the history
  • Loading branch information
AmooHashem committed Oct 28, 2024
1 parent 3da1036 commit 106ff06
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions apps/attributes/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Attribute(PolymorphicModel):
related_name='related_to',
)

def is_permitted(self, *args, **kwargs):
def is_permitted(self, *args, **kwargs) -> bool:
is_permitted = True

from .intrinsic_attributes import Condition
Expand Down Expand Up @@ -69,7 +69,7 @@ def give_reward(self, *args, **kwargs):
)

@abstractmethod
def perform(self, *args, **kwargs):
def perform(self, *args, **kwargs) -> bool:
pass

class Meta:
Expand Down
18 changes: 11 additions & 7 deletions apps/attributes/models/intrinsic_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Condition(IntrinsicAttribute):
def is_true(self, *args, **kwargs):
player = kwargs.get('player')
value = self.value
total_condition_result = True

if 'expected_correct_choices_in_last_answer_count' in value:
expected_count = value.get(
Expand All @@ -33,10 +34,10 @@ def is_true(self, *args, **kwargs):
if choice.is_correct
)

return correct_choices_count == expected_count
total_condition_result = correct_choices_count == expected_count

except MultiChoiceAnswer.DoesNotExist:
return False
total_condition_result = False

if 'completed_fsms' in value:
completed_fsm_ids = value.get('completed_fsms')
Expand All @@ -49,7 +50,8 @@ def is_true(self, *args, **kwargs):
).values('fsm_id').distinct().count()

# Compare with the total number of required unique FSMs
return completed_fsm_count == len(set(completed_fsm_ids))
total_condition_result = \
completed_fsm_count == len(set(completed_fsm_ids))

if 'expected_choices' in value:
expected_choice_ids = value.get('expected_choices')
Expand All @@ -70,7 +72,8 @@ def is_true(self, *args, **kwargs):
}

# Check if all expected choices exist in the set
return all(choice_id in all_choice_ids for choice_id in expected_choice_ids)
total_condition_result = \
all(choice_id in all_choice_ids for choice_id in expected_choice_ids)

if 'expected_choices_in_last_answer' in value:
expected_choice_ids = value.get('expected_choices_in_last_answer')
Expand All @@ -91,12 +94,13 @@ def is_true(self, *args, **kwargs):

expected_choice_ids = set(expected_choice_ids)

return submitted_choice_ids == expected_choice_ids
total_condition_result = submitted_choice_ids == expected_choice_ids

except MultiChoiceAnswer.DoesNotExist:
return False
total_condition_result = False

return True
is_not = value.get('not', False)
return total_condition_result ^ is_not


class Cost(IntrinsicAttribute):
Expand Down
7 changes: 5 additions & 2 deletions apps/attributes/models/performable_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Transition(PerformableAction):
destination_state_id = models.IntegerField()

def perform(self, *arg, **kwargs):
def perform(self, *arg, **kwargs) -> bool:

if not self.is_permitted(*arg, **kwargs):
return False
Expand All @@ -21,7 +21,10 @@ def perform(self, *arg, **kwargs):

player = kwargs.get('player')
transit_player_in_fsm(
player, player.current_state, destination_state)
player,
player.current_state,
destination_state,
)

return True

Expand Down

0 comments on commit 106ff06

Please sign in to comment.