Skip to content

Commit

Permalink
feat: add start attribute (for transferring reward on entering fsm)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmooHashem committed Nov 6, 2024
1 parent 7ab0e5c commit 93963ac
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 34 deletions.
17 changes: 11 additions & 6 deletions apps/attributes/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from apps.attributes.models.base import Attribute, IntrinsicAttribute, PerformableAction
from apps.attributes.models.intrinsic_attributes import Enabled, Condition, Cost, Reward
from apps.attributes.models.performable_actions import Answer, Buy, Submission, Transition
from apps.attributes.models.performable_actions import Answer, Buy, Start, Submission, Transition


class AttributeCustomAdmin(admin.ModelAdmin):
Expand Down Expand Up @@ -45,6 +45,16 @@ class RewardCustomAdmin(IntrinsicAttributeCustomAdmin):
################ PERFORMABLE ACTIONS ################


@admin.register(Submission)
class SubmissionCustomAdmin(PerformableActionCustomAdmin):
list_display = PerformableActionCustomAdmin.list_display + []


@admin.register(Start)
class StartCustomAdmin(PerformableActionCustomAdmin):
list_display = PerformableActionCustomAdmin.list_display + []


@admin.register(Transition)
class TransitionCustomAdmin(PerformableActionCustomAdmin):
list_display = PerformableActionCustomAdmin.list_display + \
Expand All @@ -56,11 +66,6 @@ class BuyCustomAdmin(PerformableActionCustomAdmin):
list_display = PerformableActionCustomAdmin.list_display + []


@admin.register(Submission)
class SubmissionCustomAdmin(PerformableActionCustomAdmin):
list_display = PerformableActionCustomAdmin.list_display + []


@admin.register(Answer)
class AnswerCustomAdmin(PerformableActionCustomAdmin):
list_display = PerformableActionCustomAdmin.list_display + \
Expand Down
25 changes: 25 additions & 0 deletions apps/attributes/migrations/0013_start.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.1.3 on 2024-11-06 00:40

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('attributes', '0012_answer'),
]

operations = [
migrations.CreateModel(
name='Start',
fields=[
('performableaction_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='attributes.performableaction')),
],
options={
'abstract': False,
'base_manager_name': 'objects',
},
bases=('attributes.performableaction',),
),
]
50 changes: 31 additions & 19 deletions apps/attributes/models/performable_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,37 @@
from django.db import models


class Start(PerformableAction):

def perform(self, *args, **kwargs):
if not super().perform(*args, **kwargs):
return False

# perform main action:
self.give_reward(*args, **kwargs)

return True


class Submission(PerformableAction):

def perform(self, *args, **kwargs):
if not super().perform(*args, **kwargs):
return False

# perform main action:
self.give_reward(*args, **kwargs)

from apps.attributes.utils import perform_posterior_actions
perform_posterior_actions(
attributes=self.attributes,
*args,
**kwargs,
)

return True


class Answer(PerformableAction):
class AnswerTypes(models.TextChoices):
SmallAnswer = 'SmallAnswer'
Expand Down Expand Up @@ -64,25 +95,6 @@ def perform(self, *args, **kwargs) -> bool:
return True


class Submission(PerformableAction):

def perform(self, *args, **kwargs):
if not super().perform(*args, **kwargs):
return False

# perform main action:
self.give_reward(*args, **kwargs)

from apps.attributes.utils import perform_posterior_actions
perform_posterior_actions(
attributes=self.attributes,
*args,
**kwargs,
)

return True


class Buy(PerformableAction):

def perform(self, *args, **kwargs):
Expand Down
25 changes: 17 additions & 8 deletions apps/fsm/serializers/player_serializer.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
from rest_framework import serializers

from apps.fsm.models import Player, PlayerStateHistory


class PlayerHistorySerializer(serializers.ModelSerializer):
class Meta:
model = PlayerStateHistory
fields = '__all__'
read_only_fields = ['id']
from apps.fsm.models import Player


class PlayerMinimalSerializer(serializers.ModelSerializer):
Expand All @@ -20,6 +13,22 @@ class Meta:

class PlayerSerializer(serializers.ModelSerializer):

def create(self, validated_data):
context = self.context
request = context.get('request')
fsm = validated_data.get('fsm')

# perform fsm start actions
from apps.attributes.models import Start
start_attributes = fsm.attributes.instance_of(Start)
for start_attribute in start_attributes:
start_attribute.perform(
request=request,
user=request.user,
)

return super().create(validated_data)

class Meta:
model = Player
fields = '__all__'
Expand Down
3 changes: 2 additions & 1 deletion apps/fsm/views/fsm_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ def enter_fsm(self, request, pk=None):
'receipt': receipt,
'current_state': fsm.first_state.id,
'last_visit': timezone.now(),
}
},
context=self.get_serializer_context(),
)
serializer.is_valid(raise_exception=True)
player = serializer.save()
Expand Down

0 comments on commit 93963ac

Please sign in to comment.