-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
112 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from asgiref.sync import async_to_sync | ||
from channels.layers import get_channel_layer | ||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
from helpers.signals import post_update | ||
from game.models import Patient | ||
from template.serializer.state_serialize import StateSerializer | ||
|
||
|
||
class ChannelEventTypes: | ||
ACTION_CONFIRMATION = "action-confirmation" | ||
ACTION_DECLINATION = "action-declination" | ||
ACTION_RESULT = "action-result" | ||
PHASE_UPDATE = "state" | ||
|
||
|
||
def _notify_group(group_channel_name, event): | ||
async_to_sync(get_channel_layer().group_send)(group_channel_name, event) | ||
|
||
|
||
def _notify_instance(instance_channel_name, event): | ||
async_to_sync(get_channel_layer().send)(instance_channel_name, event) | ||
|
||
|
||
@receiver(post_update, patient=Patient) | ||
def notify_patient_phase_change(patient): | ||
state = patient.stateID.get() | ||
channel = patient.channel_name | ||
serializer = StateSerializer(state) | ||
event = { | ||
"type": ChannelEventTypes.PHASE_UPDATE, | ||
**serializer.data, | ||
} | ||
_notify_group(channel, event) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# ToDo: implement |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# ToDo: Test update signal working |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# ToDo: Adapt online code, import in Channels | ||
from django.dispatch import Signal | ||
from django.db import models | ||
|
||
post_update = Signal() | ||
|
||
|
||
class SignalingQuerySet(models.query.QuerySet): | ||
def update(self, kwargs): | ||
super().update(kwargs) | ||
post_update.send(sender=self.model) | ||
|
||
|
||
class UpdateSignalManager(models.Manager): | ||
def getqueryset(self): | ||
return SignalingQuerySet(self.model, using=self._db) | ||
|
||
|
||
class UpdateSignals(models.Model): | ||
""" | ||
When using this class signals will be send when calling the Foo.objects.update() method. | ||
To decrease coupling we encourage to always use the update method when changing fields | ||
""" | ||
|
||
objects = UpdateSignalManager() | ||
|
||
class Meta: | ||
abstract = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Transitionable: | ||
""" | ||
This mixin provides table-based state transition logic. | ||
This class assumes that we are using the PatientState directly and not an InstanceClass | ||
""" | ||
|
||
def execute_state_change(self): | ||
if self.is_dead(): | ||
return False | ||
# ToDo: Add actual logic, remove stub | ||
next_state_id = self.determine_next_state(self.state.id) | ||
if not next_state_id: | ||
return False | ||
self.state = self.states.get(pk=next_state_id) | ||
return True | ||
|
||
def stub_determine_next_state(self, current_state): | ||
while not self.states.get(pk=current_state + 1): | ||
if current_state % 10: | ||
return None | ||
current_state += 1 | ||
return current_state + 1 | ||
|
||
|
||
# ToDo: Implement Stub for now | ||
# ToDo: Imoprt old componenent from old dps | ||
# ToDo: Update to amount based logic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters