@@ -3,22 +3,24 @@ import {
33 Anim ,
44 animationMap ,
55 ChanceInput ,
6- GamePlayer ,
76 MoneyExchangeDirection ,
87} from '@prisel/monopoly-common' ;
98import { monopolypb } from '@prisel/protos' ;
10- import { Packet } from '@prisel/server' ;
9+ import { broadcast , Packet , TurnOrder } from '@prisel/server' ;
1110import { endState , newState , StateConfig } from '@prisel/state' ;
1211import { log } from '../log' ;
13- import { AnimatingAllPlayers , getCurrentPlayer , getGame } from '../stateMachine/utils' ;
12+ import { getGamePlayer , PlayingAnimation } from '../stateMachine/utils' ;
1413
1514export function * moneyExchangeHandler ( props : {
1615 input : ChanceInput < 'money_exchange' > ;
1716 setNextState : ( nextState : StateConfig < any > ) => void ;
17+ turnOrder : TurnOrder ;
1818} ) {
19- const game = getGame ( ) ;
20- const currentPlayer = getCurrentPlayer ( ) ;
21- const { input, setNextState } = props ;
19+ const { input, turnOrder } = props ;
20+ const currentPlayer = getGamePlayer ( turnOrder . getCurrentPlayer ( ) ) ;
21+ if ( ! currentPlayer ) {
22+ return endState ( ) ;
23+ }
2224 const inputArgs = input . inputArgs ;
2325 const exchanges : {
2426 [ playerId : string ] : number ;
@@ -39,22 +41,28 @@ export function* moneyExchangeHandler(props: {
3941 case MoneyExchangeDirection . FROM_ALL_OTHER_PLAYERS :
4042 // TODO: take account of lost players if we allow them to stay in
4143 // game
42- const totalGainedMoney = inputArgs . amount * Array . from ( game . players . keys ( ) ) . length ;
44+ const totalGainedMoney = inputArgs . amount * turnOrder . size ;
4345 currentPlayer . gainMoney ( totalGainedMoney ) ;
4446 exchanges [ currentPlayer . id ] = totalGainedMoney ;
45- game . players . forEach ( ( gamePlayer ) => {
46- gamePlayer . gainMoney ( - inputArgs . amount ) ;
47- exchanges [ gamePlayer . id ] = - inputArgs . amount ;
47+ turnOrder . getAllPlayers ( ) . forEach ( ( player ) => {
48+ const gamePlayer = getGamePlayer ( player ) ;
49+ if ( gamePlayer ) {
50+ gamePlayer . gainMoney ( - inputArgs . amount ) ;
51+ exchanges [ gamePlayer . id ] = - inputArgs . amount ;
52+ }
4853 } ) ;
4954 playerEmotion = monopolypb . PlayerEmotionExtra_EmotionType . CHEER ;
5055 break ;
5156 case MoneyExchangeDirection . TO_ALL_OTHER_PLAYERS :
52- const totalLostMoney = inputArgs . amount * Array . from ( game . players . keys ( ) ) . length ;
57+ const totalLostMoney = inputArgs . amount * turnOrder . size ;
5358 currentPlayer . gainMoney ( - totalLostMoney ) ;
5459 exchanges [ currentPlayer . id ] = - totalLostMoney ;
55- game . players . forEach ( ( gamePlayer ) => {
56- gamePlayer . gainMoney ( inputArgs . amount ) ;
57- exchanges [ gamePlayer . id ] = inputArgs . amount ;
60+ turnOrder . getAllPlayers ( ) . forEach ( ( player ) => {
61+ const gamePlayer = getGamePlayer ( player ) ;
62+ if ( gamePlayer ) {
63+ gamePlayer . gainMoney ( inputArgs . amount ) ;
64+ exchanges [ gamePlayer . id ] = inputArgs . amount ;
65+ }
5866 } ) ;
5967 playerEmotion = monopolypb . PlayerEmotionExtra_EmotionType . ANGRY ;
6068 break ;
@@ -63,8 +71,12 @@ export function* moneyExchangeHandler(props: {
6371 log . error ( 'CashExchangeHandler cannot handle cash_exchange Chance with no direction' ) ;
6472 return endState ( ) ;
6573 }
66- game . broadcast ( ( player : GamePlayer ) =>
67- Packet . forAction ( Action . ANNOUNCE_CHANCE )
74+ broadcast ( turnOrder . getAllPlayers ( ) , ( player ) => {
75+ const gamePlayer = getGamePlayer ( player ) ;
76+ if ( ! gamePlayer ) {
77+ return ;
78+ }
79+ return Packet . forAction ( Action . ANNOUNCE_CHANCE )
6880 . setPayload ( monopolypb . AnnounceRecievedChancePayload , {
6981 player : currentPlayer . id ,
7082 chance : {
@@ -73,23 +85,23 @@ export function* moneyExchangeHandler(props: {
7385 oneofKind : 'moneyExchange' ,
7486 moneyExchange : {
7587 exchanges,
76- myCurrentMoney : player . money ,
88+ myCurrentMoney : gamePlayer . money ,
7789 } ,
7890 } ,
7991 } ,
8092 } )
81- . build ( ) ,
82- ) ;
93+ . build ( ) ;
94+ } ) ;
8395
84- yield newState (
85- AnimatingAllPlayers ,
86- Anim . create ( 'player_emotion' , monopolypb . PlayerEmotionExtra )
96+ yield newState ( PlayingAnimation , {
97+ animation : Anim . create ( 'player_emotion' , monopolypb . PlayerEmotionExtra )
8798 . setExtra ( {
8899 player : currentPlayer . getGamePlayerInfo ( ) ,
89100 emotion : playerEmotion ,
90101 } )
91102 . setLength ( animationMap . player_emotion ) ,
92- ) ;
103+ turnOrder,
104+ } ) ;
93105
94106 return endState ( ) ;
95107}
0 commit comments