Skip to content

Commit

Permalink
Add basic Enemy Leader UI representation
Browse files Browse the repository at this point in the history
  • Loading branch information
iamlogand committed Feb 13, 2024
1 parent cb40b12 commit 3599ca3
Show file tree
Hide file tree
Showing 24 changed files with 249 additions and 43 deletions.
35 changes: 32 additions & 3 deletions backend/rorapp/functions/enemy_leader_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from typing import List, Tuple
from django.conf import settings
from rorapp.functions.websocket_message_helper import create_websocket_message
from rorapp.models import EnemyLeader, Faction, Game, War
from rorapp.serializers import EnemyLeaderSerializer
from rorapp.models import ActionLog, EnemyLeader, Faction, Game, Step, War
from rorapp.serializers import ActionLogSerializer, EnemyLeaderSerializer, WarSerializer


def create_new_enemy_leader(
Expand Down Expand Up @@ -61,7 +61,33 @@ def create_new_enemy_leader(
)
)

# TODO: Add action log for new enemy leader, which could also reference the matching/activated war
# Create action log for new leader
action_log_index = (
ActionLog.objects.filter(step__phase__turn__game=game.id)
.order_by("index")
.last()
.index
+ 1
)
latest_step = (
Step.objects.filter(phase__turn__game=game_id).order_by("-index").first()
)
action_log_data = {
"enemy_leader": enemy_leader.id,
"matching_war": matching_war.id,
"activated_the_war": len(activated_war_message) > 0,
"initiating_faction": faction.id,
}
action_log = ActionLog(
index=action_log_index,
step=latest_step,
type="new_enemy_leader",
data=action_log_data,
)
action_log.save()
messages_to_send.append(
create_websocket_message("action_log", ActionLogSerializer(action_log).data)
)

return messages_to_send

Expand All @@ -87,5 +113,8 @@ def get_and_activate_matching_war(game: Game, war_name: int) -> Tuple[War, List[
war = matching_wars.first()
war.status = "active"
war.save()
messages_to_send.append(
create_websocket_message("war", WarSerializer(war).data)
)

return war, messages_to_send
3 changes: 2 additions & 1 deletion backend/rorapp/models/enemy_leader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db import models
from rorapp.models.game import Game
from rorapp.models.war import War


# Model for representing enemy leaders
Expand All @@ -10,5 +11,5 @@ class EnemyLeader(models.Model):
disaster_number = models.IntegerField()
standoff_number = models.IntegerField()
war_name = models.CharField(max_length=10)
current_war = models.ForeignKey("War", on_delete=models.CASCADE)
current_war = models.ForeignKey(War, on_delete=models.CASCADE)
dead = models.BooleanField()
1 change: 1 addition & 0 deletions backend/rorapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
router.register('users', views.UserViewSet, basename='user')
router.register('waitlist-entries', views.WaitlistEntryViewSet, basename='waitlist-entry')
router.register('wars', views.WarViewSet, basename='war')
router.register('enemy-leaders', views.EnemyLeaderViewSet, basename='enemy-leader')

app_name = "rorapp"

Expand Down
7 changes: 4 additions & 3 deletions backend/rorapp/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Package used to group the view scripts
from .action import ActionViewSet # noqa: F401
from .action_log import ActionLogViewSet # noqa: F401
from .enemy_leader import EnemyLeaderViewSet # noqa: F401
from .faction import FactionViewSet # noqa: F401
from .game import GameViewSet # noqa: F401
from .index import index # noqa: F401
from .action_log import ActionLogViewSet # noqa: F401
from .player import PlayerViewSet # noqa: F401
from .phase import PhaseViewSet # noqa: F401
from .action import ActionViewSet # noqa: F401
from .player import PlayerViewSet # noqa: F401
from .secret import SecretPrivateViewSet, SecretPublicViewSet # noqa: F401
from .senator import SenatorViewSet # noqa: F401
from .senator_action_log import SenatorActionLogViewSet # noqa: F401
Expand Down
23 changes: 23 additions & 0 deletions backend/rorapp/views/enemy_leader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated
from rorapp.models import EnemyLeader
from rorapp.serializers import EnemyLeaderSerializer


class EnemyLeaderViewSet(viewsets.ReadOnlyModelViewSet):
"""
Read enemy leaders.
"""

permission_classes = [IsAuthenticated]
serializer_class = EnemyLeaderSerializer

def get_queryset(self):
queryset = EnemyLeader.objects.all()

# Filter against a `game` query parameter in the URL
game_id = self.request.query_params.get("game", None)
if game_id is not None:
queryset = queryset.filter(game__id=game_id)

return queryset
37 changes: 37 additions & 0 deletions frontend/classes/EnemyLeader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
interface IEnemyLeader {
id: number
name: string
game: number
strength: number
disaster_number: number
standoff_number: number
war_name: string
current_war: number
dead: boolean
}

class EnemyLeader {
id: number
name: string
game: number
strength: number
disasterNumber: number
standoffNumber: number
warName: string
currentWar: number
dead: boolean

constructor(data: IEnemyLeader) {
this.id = data.id
this.name = data.name
this.game = data.game
this.strength = data.strength
this.disasterNumber = data.disaster_number
this.standoffNumber = data.standoff_number
this.warName = data.war_name
this.currentWar = data.current_war
this.dead = data.dead
}
}

export default EnemyLeader
2 changes: 1 addition & 1 deletion frontend/components/DetailSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import FactionDetailSection from "@/components/entityDetails/EntityDetail_Factio
import { useGameContext } from "@/contexts/GameContext"
import HraoTerm from "@/components/terms/Term_Hrao"
import RomeConsulTerm from "@/components/terms/Term_RomeConsul"
import SelectedDetail from "@/types/selectedDetail"
import SelectedDetail from "@/types/SelectedDetail"
import PriorConsulTerm from "@/components/terms/Term_PriorConsul"
import FactionTerm from "@/components/terms/Term_Faction"
import SecretTerm from "@/components/terms/Term_Secret"
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/FactionIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Faction from "@/classes/Faction"
import SelectedDetail from "@/types/selectedDetail"
import SelectedDetail from "@/types/SelectedDetail"
import { useGameContext } from "@/contexts/GameContext"

interface FactionIconProps {
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/FactionLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Link } from "@mui/material"

import { useGameContext } from "@/contexts/GameContext"
import Faction from "@/classes/Faction"
import SelectedDetail from "@/types/selectedDetail"
import SelectedDetail from "@/types/SelectedDetail"
import FactionIcon from "@/components/FactionIcon"

interface FactionLinkProps {
Expand Down
21 changes: 11 additions & 10 deletions frontend/components/GamePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import SenatorActionLog from "@/classes/SenatorActionLog"
import Secret from "@/classes/Secret"
import War from "@/classes/War"
import WarfareTab from "@/components/WarfareTab"
import EnemyLeader from "@/classes/EnemyLeader"

const webSocketURL: string = process.env.NEXT_PUBLIC_WS_URL ?? ""

Expand Down Expand Up @@ -87,6 +88,7 @@ const GamePage = (props: GamePageProps) => {
setNotifications,
setAllSecrets,
setWars,
setEnemyLeaders,
} = useGameContext()
const [latestActions, setLatestActions] = useState<Collection<Action>>(
new Collection<Action>()
Expand Down Expand Up @@ -252,6 +254,11 @@ const GamePage = (props: GamePageProps) => {
fetchAndSetCollection(War, setWars, url)
}, [props.gameId, setWars, fetchAndSetCollection])

const fetchEnemyLeaders = useCallback(async () => {
const url = `enemy-leaders/?game=${props.gameId}`
fetchAndSetCollection(EnemyLeader, setEnemyLeaders, url)
}, [props.gameId, setEnemyLeaders, fetchAndSetCollection])

const fetchTitles = useCallback(async () => {
const url = `titles/?game=${props.gameId}&relevant`
fetchAndSetCollection(Title, setAllTitles, url)
Expand Down Expand Up @@ -403,6 +410,7 @@ const GamePage = (props: GamePageProps) => {
fetchNotifications(),
fetchSecrets(),
fetchWars(),
fetchEnemyLeaders(),
]
const results = await Promise.all(requestsBatch1)
const updatedLatestStep: Step | null = results[0] as Step | null
Expand Down Expand Up @@ -434,6 +442,7 @@ const GamePage = (props: GamePageProps) => {
fetchNotifications,
fetchSecrets,
fetchWars,
fetchEnemyLeaders,
])

// Function to handle instance updates
Expand Down Expand Up @@ -513,6 +522,7 @@ const GamePage = (props: GamePageProps) => {
handleCollectionUpdate,
],
war: [setWars, War, handleCollectionUpdate],
enemy_leader: [setEnemyLeaders, EnemyLeader, handleCollectionUpdate],
}),
[
handleCollectionUpdate,
Expand All @@ -527,6 +537,7 @@ const GamePage = (props: GamePageProps) => {
setNotifications,
setSenatorActionLogs,
setWars,
setEnemyLeaders,
]
)

Expand Down Expand Up @@ -555,16 +566,6 @@ const GamePage = (props: GamePageProps) => {
lastMessage,
game?.id,
classUpdateMap,
setLatestTurn,
setLatestPhase,
setLatestStep,
setLatestActions,
setAllFactions,
setAllTitles,
setAllSenators,
setNotifications,
setActionLogs,
setSenatorActionLogs,
])

// Remove old actions (i.e. actions from a step that is no longer the latest step)
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/MetaSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import VotesIcon from "@/images/icons/votes.svg"
import SecretsIcon from "@/images/icons/secrets.svg"
import AttributeFlex, { Attribute } from "@/components/AttributeFlex"
import Collection from "@/classes/Collection"
import SelectedDetail from "@/types/selectedDetail"
import SelectedDetail from "@/types/SelectedDetail"

// Section showing meta info about the game
const MetaSection = () => {
Expand Down
12 changes: 6 additions & 6 deletions frontend/components/ProgressSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import EastIcon from "@mui/icons-material/East"

import Collection from "@/classes/Collection"
import Action from "@/classes/Action"
import Actions from "@/data/actions.json"
import ActionDataCollection from "@/data/actions.json"
import FactionIcon from "@/components/FactionIcon"
import { useGameContext } from "@/contexts/GameContext"
import { useAuthContext } from "@/contexts/AuthContext"
import ActionDialog from "@/components/actionDialogs/ActionDialog"
import ActionsType from "@/types/actions"
import ActionDataCollectionType from "@/types/Action"
import Faction from "@/classes/Faction"
import FactionLink from "@/components/FactionLink"
import NotificationList from "@/components/NotificationList"

const typedActions: ActionsType = Actions
const typedActionDataCollection: ActionDataCollectionType = ActionDataCollection

const SEQUENTIAL_PHASES = ["Forum"]

Expand Down Expand Up @@ -64,7 +64,7 @@ const ProgressSection = ({ latestActions }: ProgressSectionProps) => {
waitingForDesc = (
<span>
Waiting for {pendingActions.length} factions to{" "}
{typedActions[firstPotentialAction.type]["sentence"]}
{typedActionDataCollection[firstPotentialAction.type]["sentence"]}
</span>
)
} else if (pendingActions.length === 1) {
Expand All @@ -80,7 +80,7 @@ const ProgressSection = ({ latestActions }: ProgressSectionProps) => {
) : (
<FactionLink faction={onlyPendingFaction} />
)}{" "}
to {typedActions[firstPotentialAction.type]["sentence"]}
to {typedActionDataCollection[firstPotentialAction.type]["sentence"]}
</span>
)
}
Expand Down Expand Up @@ -131,7 +131,7 @@ const ProgressSection = ({ latestActions }: ProgressSectionProps) => {
{thisFactionsPendingActions.allIds.length > 0 && requiredAction ? (
<div className="flex flex-col">
<Button variant="contained" onClick={() => setDialogOpen(true)}>
{typedActions[requiredAction.type]["title"]}
{typedActionDataCollection[requiredAction.type]["title"]}
</Button>
<ActionDialog
actions={thisFactionsPendingActions}
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/SenatorLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Link } from "@mui/material"

import { useGameContext } from "@/contexts/GameContext"
import Senator from "@/classes/Senator"
import SelectedDetail from "@/types/selectedDetail"
import SelectedDetail from "@/types/SelectedDetail"

interface SenatorLinkProps {
senator: Senator
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/SenatorPortrait.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Faction from "@/classes/Faction"
import styles from "./SenatorPortrait.module.css"
import Title from "@/classes/Title"
import TitleIcon from "@/components/TitleIcon"
import SelectedDetail from "@/types/selectedDetail"
import SelectedDetail from "@/types/SelectedDetail"
import factionColors from "@/data/factionColors.json"
import FactionLeaderPattern from "@/images/patterns/factionLeader.svg"
import DeadIcon from "@/images/icons/dead.svg"
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/TermLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import PriorConsulIcon from "@/images/icons/priorConsul.svg"
import SenatorIcon from "@/images/icons/senator.svg"
import styles from "./TermLink.module.css"
import { useGameContext } from "@/contexts/GameContext"
import SelectedDetail from "@/types/selectedDetail"
import SelectedDetail from "@/types/SelectedDetail"

// Map of term names to images
const termImages: { [key: string]: StaticImageData } = {
Expand Down
10 changes: 8 additions & 2 deletions frontend/components/WarfareTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import WarPortrait from "@/components/WarPortrait"
import { capitalize } from "@mui/material/utils"
import getDiceRollProbability from "@/functions/probability"
import War from "@/classes/War"
import { Popover } from "@mui/material"
import WarStrength from "./WarStrength"

const WarfareTab = () => {
const { wars } = useGameContext()
const { wars, enemyLeaders } = useGameContext()

const getWarStatus = (war: War) => {
switch (war.status) {
Expand Down Expand Up @@ -92,6 +91,13 @@ const WarfareTab = () => {
</li>
))}
</ul>
<ul>
{enemyLeaders.asArray.map((leader) => (
<li key={leader.id}>
{leader.name}
</li>
))}
</ul>
</div>
)
}
Expand Down
2 changes: 2 additions & 0 deletions frontend/components/actionLogs/ActionLog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import NewTurnNotification from "./ActionLog_NewTurn"
import NewFamilyNotification from "./ActionLog_NewFamily"
import NewWarNotification from "./ActionLog_NewWar"
import MatchedWarNotification from "./ActionLog_MatchedWar"
import NewEnemyLeaderNotification from "./ActionLog_NewEnemyLeader"

interface NotificationItemProps {
notification: ActionLog
Expand All @@ -20,6 +21,7 @@ const notifications: { [key: string]: React.ComponentType<any> } = {
new_war: NewWarNotification,
select_faction_leader: SelectFactionLeaderNotification,
temporary_rome_consul: TemporaryRomeConsulNotification,
new_enemy_leader: NewEnemyLeaderNotification,
}

// Container for a notification, which determines the type of notification to render
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/actionLogs/ActionLog_MatchedWar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface NotificationProps {

// Notification for when an existing war is matched during the forum phase
const MatchedWarNotification = ({ notification }: NotificationProps) => {
const { allFactions, wars } = useGameContext()
const { wars } = useGameContext()

// Get notification-specific data
const war: War | null = notification.data
Expand Down
Loading

0 comments on commit 3599ca3

Please sign in to comment.