Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Senator history tab #267

Merged
merged 21 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backend/rorapp/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
from .completed_action import CompletedActionAdmin
from .faction import FactionAdmin
from .game import GameAdmin
from .notification import NotificationAdmin
from .action_log import ActionLogAdmin
from .player import PlayerAdmin
from .phase import PhaseAdmin
from .potential_action import PotentialActionAdmin
from .senator import SenatorAdmin
from .senator_action_log import SenatorActionLogAdmin
from .step import StepAdmin
from .title import TitleAdmin
from .turn import TurnAdmin
Expand Down
8 changes: 8 additions & 0 deletions backend/rorapp/admin/action_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.contrib import admin
from rorapp.models import ActionLog


# Admin configuration for action_logs
@admin.register(ActionLog)
class ActionLogAdmin(admin.ModelAdmin):
list_display = ('id', 'index', 'step', 'type', 'faction')
8 changes: 0 additions & 8 deletions backend/rorapp/admin/notification.py

This file was deleted.

2 changes: 1 addition & 1 deletion backend/rorapp/admin/senator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# Admin configuration for senators
@admin.register(Senator)
class SenatorAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'game', 'faction', 'alive', 'code', 'generation')
list_display = ('id', 'name', 'game', 'faction', 'alive', 'code', 'generation', 'rank')
8 changes: 8 additions & 0 deletions backend/rorapp/admin/senator_action_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.contrib import admin
from rorapp.models import SenatorActionLog


# Admin configuration for action_logs
@admin.register(SenatorActionLog)
class SenatorActionLogAdmin(admin.ModelAdmin):
list_display = ('id', 'senator', 'action_log')
43 changes: 32 additions & 11 deletions backend/rorapp/functions/face_mortality.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from asgiref.sync import async_to_sync
from rorapp.functions.draw_mortality_chits import draw_mortality_chits
from rorapp.functions.rank_senators_and_factions import rank_senators_and_factions
from rorapp.models import Faction, PotentialAction, CompletedAction, Step, Senator, Title, Phase, Turn, Notification
from rorapp.serializers import NotificationSerializer, PotentialActionSerializer, StepSerializer, TitleSerializer, PhaseSerializer, TurnSerializer, SenatorSerializer
from rorapp.models import Faction, PotentialAction, CompletedAction, Step, Senator, Title, Phase, Turn, ActionLog, SenatorActionLog
from rorapp.serializers import ActionLogSerializer, PotentialActionSerializer, StepSerializer, TitleSerializer, PhaseSerializer, TurnSerializer, SenatorSerializer, SenatorActionLogSerializer


def face_mortality(game, faction, potential_action, step):
Expand Down Expand Up @@ -67,7 +67,7 @@ def face_mortality(game, faction, potential_action, step):
# End associated titles
titles_to_end = Title.objects.filter(senator__id=senator.id, end_step__isnull=True)
ended_major_office = None
heir_id = None
heir = None
if titles_to_end.exists():
for title in titles_to_end:
title.end_step = step
Expand Down Expand Up @@ -121,25 +121,46 @@ def face_mortality(game, faction, potential_action, step):
"data": TitleSerializer(new_faction_leader).data
}
})

new_notification_index = Notification.objects.filter(step__phase__turn__game=game).order_by('-index')[0].index + 1
notification = Notification(
index=new_notification_index,

# Create a action_log and action_log relations
new_action_log_index = ActionLog.objects.filter(step__phase__turn__game=game).order_by('-index')[0].index + 1
action_log = ActionLog(
index=new_action_log_index,
step=step,
type="face_mortality",
faction=senators_former_faction,
data={"senator": senator.id, "major_office": ended_major_office, "heir": heir_id}
data={"senator": senator.id, "major_office": ended_major_office, "heir_senator": heir.id if heir else None}
)
notification.save()
action_log.save()
messages_to_send.append({
"operation": "create",
"instance": {
"class": "action_log",
"data": ActionLogSerializer(action_log).data
}
})

senator_action_log = SenatorActionLog(senator=senator, action_log=action_log)
senator_action_log.save()
messages_to_send.append({
"operation": "create",
"instance": {
"class": "notification",
"data": NotificationSerializer(notification).data
"class": "senator_action_log",
"data": SenatorActionLogSerializer(senator_action_log).data
}
})

if heir:
heir_senator_action_log = SenatorActionLog(senator=heir, action_log=action_log)
heir_senator_action_log.save()
messages_to_send.append({
"operation": "create",
"instance": {
"class": "senator_action_log",
"data": SenatorActionLogSerializer(heir_senator_action_log).data
}
})

# Update senator ranks
messages_to_send.extend(rank_senators_and_factions(game.id))

Expand Down
15 changes: 8 additions & 7 deletions backend/rorapp/functions/rank_senators_and_factions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,27 @@ def rank_senators_and_factions(game_id):
messages_to_send = []

# Assign rank values
rank = 0
rank_to_assign = 0
while True:

selected_senator = None

# Assign the rank to a major office holder
if rank <= len(ordered_major_offices) - 1:
selected_senator = ordered_major_offices[rank].senator
if rank_to_assign <= len(ordered_major_offices) - 1:
selected_senator = ordered_major_offices[rank_to_assign].senator

# Assign the rank to the first remaining senator
else:
selected_senator = senators.first()
if selected_senator is None:
break

senators = senators.exclude(id=selected_senator.id)

# Update senator's rank only if it's changed
if selected_senator.rank != rank:
selected_senator.rank = rank
if selected_senator.rank != rank_to_assign:
selected_senator.rank = rank_to_assign
selected_senator.save()
senators = senators.exclude(id=selected_senator.id)

messages_to_send.append({
"operation": "update",
Expand All @@ -64,7 +65,7 @@ def rank_senators_and_factions(game_id):
}
})

rank += 1
rank_to_assign += 1

# Get unaligned and dead senators
unaligned_senators = Senator.objects.filter(game=game_id, alive=True, faction__isnull=True)
Expand Down
47 changes: 35 additions & 12 deletions backend/rorapp/functions/select_faction_leader.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from rest_framework.response import Response
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
from rorapp.models import Faction, PotentialAction, CompletedAction, Step, Senator, Title, Phase, Turn, Notification
from rorapp.serializers import NotificationSerializer, PotentialActionSerializer, StepSerializer, TitleSerializer, PhaseSerializer
from rorapp.models import Faction, PotentialAction, CompletedAction, Step, Senator, Title, Phase, Turn, ActionLog, SenatorActionLog
from rorapp.serializers import ActionLogSerializer, PotentialActionSerializer, StepSerializer, TitleSerializer, PhaseSerializer, SenatorActionLogSerializer


def select_faction_leader(game, faction, potential_action, step, data):
Expand Down Expand Up @@ -61,27 +61,50 @@ def select_faction_leader(game, faction, potential_action, step, data):
}
})

all_notifications = Notification.objects.filter(step__phase__turn__game=game).order_by('-index')
new_notification_index = 0
if all_notifications.count() > 0:
latest_notification = all_notifications[0]
new_notification_index = latest_notification.index + 1
notification = Notification(
index=new_notification_index,
# Create a action_log and action_log relations
all_action_logs = ActionLog.objects.filter(step__phase__turn__game=game).order_by('-index')
new_action_log_index = 0
if all_action_logs.count() > 0:
latest_action_log = all_action_logs[0]
new_action_log_index = latest_action_log.index + 1
action_log = ActionLog(
index=new_action_log_index,
step=step,
type="select_faction_leader",
faction=faction,
data={"senator": senator.id, "previous_senator": previous_senator_id}
)
notification.save()
action_log.save()

messages_to_send.append({
"operation": "create",
"instance": {
"class": "notification",
"data": NotificationSerializer(notification).data
"class": "action_log",
"data": ActionLogSerializer(action_log).data
}
})

senator_action_log = SenatorActionLog(senator=senator, action_log=action_log)
senator_action_log.save()

messages_to_send.append({
"operation": "create",
"instance": {
"class": "senator_action_log",
"data": SenatorActionLogSerializer(senator_action_log).data
}
})

if previous_senator_id:
previous_senator_action_log = SenatorActionLog(senator=previous_senator_id, action_log=action_log)
previous_senator_action_log.save()
messages_to_send.append({
"operation": "create",
"instance": {
"class": "senator_action_log",
"data": SenatorActionLogSerializer(previous_senator_action_log).data
}
})

# Delete the potential action
potential_action_id = potential_action.id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This migration does not change models, but it does change data in the database.

from django.db import migrations


# Rename notification data "heir_senator" key to "heir_senator"
# In a future version, notifications might be used as a log of events, so it's better to keep the data consistent.
# Any data key that contains a senator ID should be suffixed with "senator".
def update_notification_data(apps, schema_editor):
Notification = apps.get_model('rorapp', 'Notification')

face_mortality_notifications = Notification.objects.filter(type='face_mortality')

for notification in face_mortality_notifications:

notification.data['heir_senator'] = notification.data['heir']
del notification.data['heir']
notification.save()


class Migration(migrations.Migration):

dependencies = [
('rorapp', '0031_senator_rank_and_faction_rank'),
]

operations = [
migrations.RunPython(update_notification_data, migrations.RunPython.noop),
]
47 changes: 47 additions & 0 deletions backend/rorapp/migrations/0033_senatornotification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Generated by Django 4.2.2 on 2023-09-24 09:24

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


# Create relationships between senators and notifications
def create_senator_notifications(apps, schema_editor):
Notification = apps.get_model('rorapp', 'Notification')
Senator = apps.get_model('rorapp', 'Senator')
SenatorNotification = apps.get_model('rorapp', 'SenatorNotification')

notifications = Notification.objects.all()

# For each notification create some relations
for notification in notifications:

# Look at notification data and create a list of keys that end in "senator"
senator_keys = [key for key in notification.data.keys() if key.endswith('senator')]

for senator_key in senator_keys:

# Create a SenatorNotification for each senator ID in the notification data
senator_id = notification.data[senator_key]

if senator_id:
senator = Senator.objects.get(id=senator_id)
SenatorNotification.objects.create(senator=senator, notification=notification)


class Migration(migrations.Migration):

dependencies = [
('rorapp', '0032_change_senator_notification_data'),
]

operations = [
migrations.CreateModel(
name='SenatorNotification',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('notification', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='rorapp.notification')),
('senator', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='rorapp.senator')),
],
),
migrations.RunPython(create_senator_notifications, migrations.RunPython.noop),
]
Loading