Skip to content

Commit

Permalink
fix setting player in create_transition_from_player_history
Browse files Browse the repository at this point in the history
  • Loading branch information
AmooHashem committed May 15, 2024
1 parent e056810 commit 5bd3971
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 40 deletions.
4 changes: 2 additions & 2 deletions apps/fsm/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ def delta_time(self, obj):
class PlayerTransitionAdmin(admin.ModelAdmin):
model = PlayerTransition
readonly_fields = ('time',)
list_display = ['source_state', 'target_state', 'time', 'transited_edge']
list_display = ['player', 'source_state', 'target_state', 'time', 'transited_edge']
list_filter = []
raw_id_fields = ('source_state', 'target_state', 'transited_edge')
raw_id_fields = ('player', 'source_state', 'target_state', 'transited_edge')


class TextWidgetAdmin(admin.ModelAdmin):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,61 @@ def long_task():

@transaction.atomic
def do():
if playerStateHistory.is_processed:
return

playerStateHistory.is_processed = True
playerStateHistory.save()

if not playerStateHistory.transited_edge or playerStateHistory.is_edge_transited_in_reverse is None:
return

edge = playerStateHistory.transited_edge
player = playerStateHistory.player
is_reverse = playerStateHistory.is_edge_transited_in_reverse
source_state = edge.head if is_reverse else edge.tail
target_state = edge.tail if is_reverse else edge.head
transition_time = playerStateHistory.arrival_time
departure_time = playerStateHistory.departure_time

# last player transition
last_player_transition = PlayerTransition.objects.create(
source_state=source_state,
target_state=target_state,
time=transition_time,
transited_edge=edge
)

# previous player state history
try:
previous_state_history = PlayerStateHistory.objects.filter(
player=player, state=source_state, departure_time__lte=transition_time).last()
# previous_state_history.departure_time = transition_time
previous_state_history.departure = last_player_transition
previous_state_history.save()
except:
pass

# current player state history
playerStateHistory.arrival = last_player_transition
playerStateHistory.save()
if not playerStateHistory.is_processed:

playerStateHistory.is_processed = True

if playerStateHistory.transited_edge or playerStateHistory.is_edge_transited_in_reverse is None:
edge = playerStateHistory.transited_edge
player = playerStateHistory.player
is_reverse = playerStateHistory.is_edge_transited_in_reverse
source_state = edge.head if is_reverse else edge.tail
target_state = edge.tail if is_reverse else edge.head
transition_time = playerStateHistory.arrival_time
departure_time = playerStateHistory.departure_time

# last player transition
last_player_transition = PlayerTransition.objects.create(
player=player,
source_state=source_state,
target_state=target_state,
time=transition_time,
transited_edge=edge
)

# previous player state history
try:
previous_state_history = PlayerStateHistory.objects.filter(
player=player, state=source_state, departure_time__lte=transition_time).last()
# previous_state_history.departure_time = transition_time
previous_state_history.departure = last_player_transition
previous_state_history.save()
except:
pass

# current player state history
playerStateHistory.arrival = last_player_transition

playerStateHistory.save()

if not playerStateHistory.is_processed2:
playerStateHistory.is_processed2 = True
playerStateHistory.save()

player = playerStateHistory.player

arrival_transtion = playerStateHistory.arrival
if arrival_transtion:
arrival_transtion.player = player
arrival_transtion.save()

departure_transition = playerStateHistory.departure
if departure_transition:
departure_transition.player = player
departure_transition.save()

do()

thread = Thread(target=long_task)
thread.start()
return

0 comments on commit 5bd3971

Please sign in to comment.