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

Make senators generate personal revenue #448

Merged
merged 3 commits into from
Mar 18, 2024
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
12 changes: 7 additions & 5 deletions backend/rorapp/functions/mortality_phase_helper.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os
import json
from typing import List
from typing import List, Tuple
from django.conf import settings
from rest_framework.response import Response
from rorapp.functions.forum_phase_starter import start_forum_phase
from rorapp.functions.mortality_chit_helper import draw_mortality_chits
from rorapp.functions.progress_helper import get_latest_step
from rorapp.functions.rank_helper import rank_senators_and_factions
from rorapp.functions.revenue_phase_helper import generate_personal_revenue
from rorapp.functions.websocket_message_helper import (
create_websocket_message,
destroy_websocket_message,
Expand All @@ -29,7 +30,7 @@

def face_mortality(
action_id: int, chit_codes: List[int] | None = None
) -> (Response, dict):
) -> Tuple[Response, dict]:
"""
Ready up for facing mortality.

Expand Down Expand Up @@ -85,9 +86,7 @@ def resolve_mortality(game_id: int, chit_codes: List[int] | None = None) -> dict
messages_to_send = []
killed_senator_count = 0
for code in drawn_codes:
senators = Senator.objects.filter(
game=game_id, alive=True, code=code
)
senators = Senator.objects.filter(game=game_id, alive=True, code=code)
if senators.exists():
senator = senators.first()
senators_former_faction = senator.faction
Expand Down Expand Up @@ -216,6 +215,9 @@ def resolve_mortality(game_id: int, chit_codes: List[int] | None = None) -> dict
# Update senator ranks
messages_to_send.extend(rank_senators_and_factions(game_id))

# Generate personal revenue
messages_to_send.extend(generate_personal_revenue(game_id))

# Proceed to the forum phase
messages_to_send.extend(start_forum_phase(game_id))

Expand Down
54 changes: 54 additions & 0 deletions backend/rorapp/functions/revenue_phase_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from rorapp.functions.progress_helper import get_latest_step
from rorapp.functions.websocket_message_helper import create_websocket_message
from rorapp.models import ActionLog, Game, Senator, Title
from rorapp.serializers import ActionLogSerializer, SenatorSerializer


def generate_personal_revenue(game_id: Game) -> dict:
"""
Generate personal revenue for all aligned senators.

Args:
game_id (int): The game ID.

Returns:
dict: The WebSocket messages to send.
"""

messages_to_send = []

aligned_senators = Senator.objects.filter(
game=game_id, alive=True, faction__isnull=False
)
faction_leader_titles = Title.objects.filter(
senator__game=game_id, name="Faction Leader", end_step__isnull=True
)
faction_leader_senator_ids = [title.senator.id for title in faction_leader_titles]

# Generate personal revenue for all aligned senators
for senator in aligned_senators:
if senator.id in faction_leader_senator_ids:
senator.talents += 3
else:
senator.talents += 1
senator.save()
messages_to_send.append(
create_websocket_message("senator", SenatorSerializer(senator).data)
)

# Create action log
latest_step = get_latest_step(game_id)
latest_action_log = (
ActionLog.objects.filter(step=latest_step).order_by("index").last()
)
action_log = ActionLog(
index=latest_action_log.index,
step=latest_step,
type="personal_revenue",
)
action_log.save()
messages_to_send.append(
create_websocket_message("action_log", ActionLogSerializer(action_log).data)
)

return messages_to_send
12 changes: 6 additions & 6 deletions backend/rorapp/tests/mortality_phase_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ def select_faction_leaders(self, game_id: int) -> None:
def check_action_log(self, game_id: int) -> None:
previous_step = get_latest_step(game_id, 1)
action_log = ActionLog.objects.filter(step=previous_step)
# It's possible to have more than 1 action log in the event if more than one senator dies,
# It's possible to have more than 2 action logs in the event that more than one senator dies,
# but in this deterministic test no more than one senator should die
self.assertEqual(action_log.count(), 1)
self.assertEqual(action_log.count(), 2)
self.assertEqual(action_log[0].type, "face_mortality")

def kill_hrao(self, game_id: int) -> None:
Expand All @@ -97,7 +97,7 @@ def kill_hrao(self, game_id: int) -> None:
action_logs, messages = self.kill_senators(
game_id, [highest_ranking_senator.id]
)
self.assertEqual(len(messages), 18)
self.assertEqual(len(messages), 27)
latest_action_log = action_logs[0]

self.assertIsNone(latest_action_log.data["heir_senator"])
Expand All @@ -124,7 +124,7 @@ def kill_faction_leader(self, game_id: int) -> None:
faction_leader_title = Title.objects.get(senator=faction_leader)

action_logs, messages = self.kill_senators(game_id, [faction_leader.id])
self.assertEqual(len(messages), 11)
self.assertEqual(len(messages), 20)
latest_action_log = action_logs[0]

heir_id = latest_action_log.data["heir_senator"]
Expand All @@ -150,7 +150,7 @@ def kill_regular_senator(self, game_id: int) -> None:
regular_senator = self.get_senators_with_title(game_id, None)[0]

action_logs, messages = self.kill_senators(game_id, [regular_senator.id])
self.assertEqual(len(messages), 8)
self.assertEqual(len(messages), 16)
latest_action_log = action_logs[0]

self.assertIsNone(latest_action_log.data["heir_senator"])
Expand All @@ -167,7 +167,7 @@ def kill_two_senators(self, game_id: int) -> None:
two_regular_senators = self.get_senators_with_title(game_id, None)[0:2]
senator_ids = [senator.id for senator in two_regular_senators]
_, messages = self.kill_senators(game_id, senator_ids)
self.assertEqual(len(messages), 14)
self.assertEqual(len(messages), 20)
post_death_living_senator_count = Senator.objects.filter(
game=game_id, alive=True
).count()
Expand Down
22 changes: 12 additions & 10 deletions frontend/components/ActionLog.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import ActionLog from "@/classes/ActionLog"
import SelectFactionLeaderActionLog from "./actionLogs/ActionLog_SelectFactionLeader"
import FaceMortalityActionLog from "./actionLogs/ActionLog_FaceMortality"
import TemporaryRomeConsulActionLog from "./actionLogs/ActionLog_TemporaryRomeConsul"
import NewTurnActionLog from "./actionLogs/ActionLog_NewTurn"
import NewFamilyActionLog from "./actionLogs/ActionLog_NewFamily"
import NewWarActionLog from "./actionLogs/ActionLog_NewWar"
import MatchedWarActionLog from "./actionLogs/ActionLog_MatchedWar"
import NewEnemyLeaderActionLog from "./actionLogs/ActionLog_NewEnemyLeader"
import MatchedEnemyLeaderActionLog from "./actionLogs/ActionLog_MatchedEnemyLeader"
import NewSecretActionLog from "./actionLogs/ActionLog_NewSecret"
import SelectFactionLeaderActionLog from "@/components/actionLogs/ActionLog_SelectFactionLeader"
import FaceMortalityActionLog from "@/components/actionLogs/ActionLog_FaceMortality"
import TemporaryRomeConsulActionLog from "@/components/actionLogs/ActionLog_TemporaryRomeConsul"
import NewTurnActionLog from "@/components/actionLogs/ActionLog_NewTurn"
import NewFamilyActionLog from "@/components/actionLogs/ActionLog_NewFamily"
import NewWarActionLog from "@/components/actionLogs/ActionLog_NewWar"
import MatchedWarActionLog from "@/components/actionLogs/ActionLog_MatchedWar"
import NewEnemyLeaderActionLog from "@/components/actionLogs/ActionLog_NewEnemyLeader"
import MatchedEnemyLeaderActionLog from "@/components/actionLogs/ActionLog_MatchedEnemyLeader"
import NewSecretActionLog from "@/components/actionLogs/ActionLog_NewSecret"
import PersonalRevenueActionLog from "@/components/actionLogs/ActionLog_PersonalRevenue"

interface ActionLogItemProps {
notification: ActionLog
Expand All @@ -24,6 +25,7 @@ const notifications: { [key: string]: React.ComponentType<any> } = {
new_secret: NewSecretActionLog,
new_turn: NewTurnActionLog,
new_war: NewWarActionLog,
personal_revenue: PersonalRevenueActionLog,
select_faction_leader: SelectFactionLeaderActionLog,
temporary_rome_consul: TemporaryRomeConsulActionLog,
}
Expand Down
8 changes: 4 additions & 4 deletions frontend/components/FactionListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ const FactionListItem = (props: FactionListItemProps) => {
// Attribute data
const attributeItems: Attribute[] = [
{
name: "influence",
name: "Influence",
value: totalInfluence,
icon: InfluenceIcon,
},
{ name: "talents", value: totalTalents, icon: TalentsIcon },
{ name: "votes", value: totalVotes, icon: VotesIcon },
{ name: "secrets", value: secrets.length, icon: SecretsIcon },
{ name: "Personal Talents", value: totalTalents, icon: TalentsIcon },
{ name: "Votes", value: totalVotes, icon: VotesIcon },
{ name: "Secrets", value: secrets.length, icon: SecretsIcon },
]

if (!player?.user || senators.allIds.length === 0) return null
Expand Down
1 change: 1 addition & 0 deletions frontend/components/SenatorListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const SenatorListItem = ({ senator, ...props }: SenatorListItemProps) => {
size={80}
selectable={props.selectable}
blurryPlaceholder
summary
/>
</div>
<div className="w-full flex flex-col justify-between">
Expand Down
29 changes: 29 additions & 0 deletions frontend/components/actionLogs/ActionLog_PersonalRevenue.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Image from "next/image"
import TalentsIcon from "@/images/icons/talents.svg"
import ActionLog from "@/classes/ActionLog"
import ActionLogLayout from "@/components/ActionLogLayout"

interface ActionLogProps {
notification: ActionLog
}

// ActionLog for when a senator dies during the mortality phase
const PersonalRevenueActionLog = ({ notification }: ActionLogProps) => {
const getIcon = () => (
<div className="h-[18px] w-[24px] flex justify-center">
<Image src={TalentsIcon} alt="Talents icon" width={30} height={30} />
</div>
)

return (
<ActionLogLayout
actionLog={notification}
icon={getIcon()}
title="Personal Revenue"
>
<p>Aligned Senators have earned Personal Revenue.</p>
</ActionLogLayout>
)
}

export default PersonalRevenueActionLog
Loading