Skip to content

Commit

Permalink
Merge pull request #1851 from gereon77/fix-place-orders-for-vassals
Browse files Browse the repository at this point in the history
Fix place orders for vassals and some other things
  • Loading branch information
gereon77 authored Jul 5, 2024
2 parents 8e9eb2f + fc90c4a commit 6f22d07
Show file tree
Hide file tree
Showing 18 changed files with 812 additions and 553 deletions.
2 changes: 1 addition & 1 deletion agot-bg-game-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"classnames": "^2.3.1",
"css-loader": "^6.7.3",
"dotenv": "^14.2.0",
"emoji-picker-react": "^4.0.6",
"emoji-picker-react": "^4.10.0",
"eslint": "^8.7.0",
"eslint-plugin-react": "^7.28.0",
"express": "^4.17.2",
Expand Down
87 changes: 57 additions & 30 deletions agot-bg-game-server/src/client/GameClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,107 +42,120 @@ export default class GameClient {
chatClient: ChatClient = new ChatClient(this);
sfxManager: SfxManager = new SfxManager(this);

get currentVolumeSettings(): { notifications: number, music: number, sfx: number} {
if (!this.authenticatedUser) {
throw new Error("Authenticated user required");
}
return {
notifications: this.authenticatedUser.settings.notificationsVolume,
music: this.authenticatedUser.settings.musicVolume,
sfx: this.authenticatedUser.settings.sfxVolume
};
}

private set currentVolumeSettings(value: { notifications: number, music: number, sfx: number}) {
if (!this.authenticatedUser) {
throw new Error("Authenticated user required");
}
this.authenticatedUser.settings.notificationsVolume = value.notifications ?? 1;
this.authenticatedUser.settings.musicVolume = value.music ?? 1;
this.authenticatedUser.settings.sfxVolume = value.sfx ?? 1;
}

get muted(): boolean {
if (!this.authenticatedUser) {
throw new Error("Game client must have an authenticated user");
throw new Error("Authenticated user required");
}

return this.authenticatedUser.settings.muted;
}

set muted(value: boolean) {
if (!this.authenticatedUser) {
throw new Error("Game client must have an authenticated user");
throw new Error("Authenticated user required");
}

this.authenticatedUser.settings.muted = value;
if (value == true) {
const oldVolumes = {
notificationsVolume: this.authenticatedUser.settings.notificationsVolume,
musicVolume: this.authenticatedUser.settings.musicVolume,
sfxVolume: this.authenticatedUser.settings.sfxVolume
};
localStorage.setItem('oldVolumes', JSON.stringify(oldVolumes));
sessionStorage.setItem('oldVolumes', JSON.stringify(this.currentVolumeSettings));

this.authenticatedUser.settings.notificationsVolume = 0;
this.authenticatedUser.settings.musicVolume = 0;
this.authenticatedUser.settings.sfxVolume = 0;
this.currentVolumeSettings = {notifications: 0, music: 0, sfx: 0};

this.sfxManager.muteAll();
} else {
const oldVolumesFromStorage = JSON.parse(localStorage.getItem('oldVolumes') || '{}');
this.authenticatedUser.settings.notificationsVolume = oldVolumesFromStorage.notificationsVolume ?? 1;
this.authenticatedUser.settings.musicVolume = oldVolumesFromStorage.musicVolume ?? 1;
this.authenticatedUser.settings.sfxVolume = oldVolumesFromStorage.sfxVolume ?? 1;
const oldVolumesFromStorage = JSON.parse(sessionStorage.getItem('oldVolumes') || '{}');
this.currentVolumeSettings = oldVolumesFromStorage;

localStorage.removeItem('oldVolumes');
localStorage.removeItem('oldVolumes'); // Todo: Remove this some day
sessionStorage.removeItem('oldVolumes');

this.sfxManager.unmuteAll();
}

localStorage.setItem('volumeSettings', JSON.stringify(this.currentVolumeSettings));
this.authenticatedUser.syncSettings();
}

get notificationsVolume(): number {
if (!this.authenticatedUser) {
throw new Error("Game client must have an authenticated user");
throw new Error("Authenticated user required");
}

return this.authenticatedUser.settings.notificationsVolume;
}

set notificationsVolume(value: number) {
if (!this.authenticatedUser) {
throw new Error("Game client must have an authenticated user");
throw new Error("Authenticated user required");
}

this.authenticatedUser.settings.notificationsVolume = value;
this.setCurrentMutedState(value);
this.setCurrentMutedStateAndSaveVolumeSettingsToLocalStorage();

this.authenticatedUser.syncSettings();
}

get musicVolume(): number {
if (!this.authenticatedUser) {
throw new Error("Game client must have an authenticated user");
throw new Error("Authenticated user required");
}

return this.authenticatedUser.settings.musicVolume;
}

set musicVolume(value: number) {
if (!this.authenticatedUser) {
throw new Error("Game client must have an authenticated user");
throw new Error("Authenticated user required");
}

this.authenticatedUser.settings.musicVolume = value;
this.setCurrentMutedState(value);
this.setCurrentMutedStateAndSaveVolumeSettingsToLocalStorage();

this.authenticatedUser.syncSettings();
}

get sfxVolume(): number {
if (!this.authenticatedUser) {
throw new Error("Game client must have an authenticated user");
throw new Error("Authenticated user required");
}

return this.authenticatedUser.settings.sfxVolume;
}

set sfxVolume(value: number) {
if (!this.authenticatedUser) {
throw new Error("Game client must have an authenticated user");
throw new Error("Authenticated user required");
}

this.authenticatedUser.settings.sfxVolume = value;
this.setCurrentMutedState(value);
this.setCurrentMutedStateAndSaveVolumeSettingsToLocalStorage();

this.authenticatedUser.syncSettings();
}

get authenticatedPlayer(): Player | null {
if (!this.authenticatedUser) {
throw new Error("Game client must have an authenticated user");
throw new Error("Authenticated user required");
}

if (!this.entireGame || !(this.entireGame.childGameState instanceof IngameGameState)) {
Expand All @@ -156,16 +169,18 @@ export default class GameClient {
}
}

private setCurrentMutedState(value: number): void {
private setCurrentMutedStateAndSaveVolumeSettingsToLocalStorage(): void {
if (!this.authenticatedUser) {
return;
}

if (value > 0) {
this.authenticatedUser.settings.muted = false;
} else if (this.musicVolume == 0 && this.notificationsVolume == 0 && this.sfxVolume == 0) {
if (this.musicVolume == 0 && this.notificationsVolume == 0 && this.sfxVolume == 0) {
this.authenticatedUser.settings.muted = true;
} else {
this.authenticatedUser.settings.muted = false;
}

localStorage.setItem('volumeSettings', JSON.stringify(this.currentVolumeSettings));
}

constructor(authData: AuthData) {
Expand Down Expand Up @@ -307,6 +322,7 @@ export default class GameClient {

this.connectionState = ConnectionState.SYNCED;
this.isReconnecting = false;
this.loadVolumeSettingsFromLocalStorage();
} else if (message.type == "new-private-chat-room") {
if (this.entireGame == null) {
return;
Expand Down Expand Up @@ -377,4 +393,15 @@ export default class GameClient {
this.authenticatedUser = null;
this.isReconnecting = false;
}

private loadVolumeSettingsFromLocalStorage(): void {
const item = localStorage.getItem('volumeSettings');
if (!item) {
return;
}

const volumeSettings = JSON.parse(item);
this.currentVolumeSettings = volumeSettings;
this.setCurrentMutedStateAndSaveVolumeSettingsToLocalStorage();
}
}
8 changes: 4 additions & 4 deletions agot-bg-game-server/src/client/MapComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,10 @@ export default class MapComponent extends Component<MapComponentProps> {

let order: Order | null = null;
let orderPresent = false;
if (this.ingame.childGameState instanceof PlanningGameState && this.ingame.childGameState.childGameState instanceof PlaceOrdersGameState) {
const placeOrders = this.ingame.childGameState.childGameState;
orderPresent = placeOrders.placedOrders.has(region);
order = orderPresent ? placeOrders.placedOrders.get(region) : null;
if (this.ingame.childGameState instanceof PlanningGameState) {
const planning = this.ingame.childGameState;
orderPresent = planning.placedOrders.has(region);
order = orderPresent ? planning.placedOrders.get(region) : null;
} else {
orderPresent = this.ingame.ordersOnBoard.has(region);
order = orderPresent ? this.ingame.ordersOnBoard.get(region) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export default class ChatComponent extends Component<ChatComponentProps> {
theme={Theme.DARK}
autoFocusSearch={!isMobile}
emojiStyle={isMobile ? EmojiStyle.NATIVE : EmojiStyle.APPLE}
suggestedEmojisMode={SuggestionMode.FREQUENT}
suggestedEmojisMode={SuggestionMode.RECENT}
lazyLoadEmojis={true}
onEmojiClick={(emoji) => {
const input = document.getElementById(`chat-client-input-${this.channel.id}`) as HTMLInputElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ export default class PlaceOrdersComponent extends Component<GameStateComponentPr
return this.placeOrders.getHousesToPutOrdersForPlayer(this.player);
}

get forVassals(): boolean {
return this.placeOrders.forVassals;
}

get placeOrders(): PlaceOrdersGameState {
return this.props.gameState;
}
Expand All @@ -63,11 +59,7 @@ export default class PlaceOrdersComponent extends Component<GameStateComponentPr
<>
<Row className="justify-content-center">
<Col xs={12} className="text-center">
{!this.forVassals ? (
<>Players must now place orders in every area where they have at least one unit.</>
) : (
<>Players must now place orders for their vassals.</>
)}
Players must now place orders in every area where they have at least one unit.
</Col>
{this.placeOrders.parentGameState.planningRestrictions.map(pr => this.restrictionToWesterosCardTypeMap.tryGet(pr, null))
.map(prWc => prWc != null ?
Expand All @@ -76,7 +68,7 @@ export default class PlaceOrdersComponent extends Component<GameStateComponentPr
</Col> : <></>)
}
<Col xs={12}>
{this.props.gameClient.authenticatedPlayer && this.showButtons() && (
{this.props.gameClient.authenticatedPlayer && (
<Row className="justify-content-center">
<Col xs="auto">
<Button type="button"
Expand Down Expand Up @@ -109,10 +101,6 @@ export default class PlaceOrdersComponent extends Component<GameStateComponentPr
);
}

showButtons(): boolean {
return !this.placeOrders.forVassals || this.placeOrders.ingame.getVassalsControlledByPlayer(this.player).length > 0;
}

componentDidMount(): void {
this.props.mapControls.modifyRegionsOnMap.push(this.modifyRegionsOnMapCallback = () => this.modifyRegionsOnMap());
this.props.mapControls.modifyOrdersOnMap.push(this.modifyOrdersOnMapCallback = () => this.modifyOrdersOnMap());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Component, ReactNode } from "react";
import { observer } from "mobx-react";
import React from "react";
import GameStateComponentProps from "./GameStateComponentProps";
import renderChildGameState from "../utils/renderChildGameState";
import { Row } from "react-bootstrap";
import PlaceOrdersForVassalsGameState from "../../common/ingame-game-state/planning-game-state/place-orders-for-vassals-game-state/PlaceOrdersForVassalsGameState";
import ResolveSinglePlaceOrdersForVassalsGameState from "../../common/ingame-game-state/planning-game-state/place-orders-for-vassals-game-state/resolve-single-place-orders-for-vassals-game-state/ResolveSinglePlaceOrdersForVassalsGameState";
import ResolveSinglePlaceOrdersForVassalsComponent from "./ResolveSinglePlaceOrdersForVassalsComponent";

@observer
export default class PlaceOrdersForVassalsComponent extends Component<GameStateComponentProps<PlaceOrdersForVassalsGameState>> {
render(): ReactNode {
return <Row className="justify-content-center">
{renderChildGameState(this.props, [
[ResolveSinglePlaceOrdersForVassalsGameState, ResolveSinglePlaceOrdersForVassalsComponent]
])}
</Row>;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import MusteringGameState from "../../common/ingame-game-state/westeros-game-sta
import MusteringComponent from "./MusteringComponent";
import { Row, Col } from "react-bootstrap";
import WesterosCardComponent from "./utils/WesterosCardComponent";
import PlaceOrdersForVassalsGameState from "../../common/ingame-game-state/planning-game-state/place-orders-for-vassals-game-state/PlaceOrdersForVassalsGameState";
import PlaceOrdersForVassalsComponent from "./PlaceOrdersForVassalsComponent";

@observer
export default class PlanningComponent extends Component<GameStateComponentProps<PlanningGameState>> {
modifyRegionsOnMapCallback: any;
modifyOrdersOnMapCallback: any;

render(): ReactNode {
return (
<>
Expand All @@ -38,6 +37,7 @@ export default class PlanningComponent extends Component<GameStateComponentProps
<Row className="justify-content-center">
{renderChildGameState(this.props, [
[PlaceOrdersGameState, PlaceOrdersComponent],
[PlaceOrdersForVassalsGameState, PlaceOrdersForVassalsComponent],
[ClaimVassalsGameState, ClaimVassalsComponent],
[MusteringGameState, MusteringComponent]
])}
Expand Down
Loading

0 comments on commit 6f22d07

Please sign in to comment.