@@ -69,26 +69,34 @@ public Game(IMap map)
6969 else
7070 Thread . Sleep ( 1000 ) ;
7171 }
72- while ( _state != GameState . Done )
72+ try
7373 {
74- if ( _commands . IsEmpty || _state != GameState . Playing )
74+ while ( _state != GameState . Done )
7575 {
76- Thread . Sleep ( 100 ) ;
77- continue ;
78- }
79- else if ( _commands . TryDequeue ( out var cmd ) )
80- {
81- switch ( cmd . Action )
76+ if ( _commands . IsEmpty || _state != GameState . Playing )
8277 {
83- case Model . Action . Attack :
84- ProcessAttack ( cmd , _map ) ;
85- break ;
86- default :
87- break ;
78+ Thread . Sleep ( 100 ) ;
79+ continue ;
80+ }
81+ else if ( _commands . TryDequeue ( out var cmd ) )
82+ {
83+ switch ( cmd . Action )
84+ {
85+ case Model . Action . Attack :
86+ ProcessAttack ( cmd , _map ) ;
87+ break ;
88+ default :
89+ break ;
90+ }
8891 }
89- }
9092
91- ProcessVictory ( ) ;
93+ ProcessVictory ( ) ;
94+ }
95+ }
96+ catch ( Exception ex )
97+ {
98+ Console . WriteLine ( ex . Message ) ;
99+ Console . WriteLine ( ex . StackTrace ) ;
92100 }
93101 } ) . ContinueWith ( ( task ) => {
94102 //Recycling
@@ -100,33 +108,45 @@ public Game(IMap map)
100108
101109 private void ProcessAttack ( Command cmd , IMap map )
102110 {
103- var cell = map . Locate ( cmd . Target ) ;
104- var player = Core . Players . Get ( cmd . Sender ) ;
105- int time = ActionLogic . Calc ( cmd . Action , cell , player ) ;
106-
107- if ( cell . CanAttack ( player ) )
111+ int time = 0 ;
112+ try
108113 {
109- Task < bool > . Factory . StartNew ( ( ) =>
110- {
111- return cell . BeginChangeOwner ( player , time ) ;
112- } ) . ContinueWith ( ( task ) =>
114+ var cell = map . Locate ( cmd . Target ) ;
115+ var player = Core . Players . Get ( cmd . Sender ) ;
116+
117+ if ( cell != null && cell . CanAttack ( player ) )
113118 {
114- if ( task . Result )
115- return cell . EndChangeOwner ( player ) ;
116- else
117- return false ;
118- } ) . ContinueWith ( ( task ) => {
119- if ( task . Result )
120- ProcessAttackResult ( player , cell ) ;
119+ time = ActionLogic . Calc ( cmd . Action , cell , player ) ;
120+ Task < bool > . Factory . StartNew ( ( ) =>
121+ {
122+ return cell . BeginChangeOwner ( player , time ) ;
123+ } ) . ContinueWith ( ( task ) =>
124+ {
125+ if ( task . Result )
126+ return cell . EndChangeOwner ( player ) ;
127+ else
128+ return false ;
129+ } ) . ContinueWith ( ( task ) => {
130+ if ( task . Result )
131+ ProcessAttackResult ( player , cell ) ;
121132
122- ClearLostCells ( ) ;
123- } ) ;
133+ ClearLostCells ( ) ;
134+ } ) ;
135+ }
136+ }
137+ catch ( Exception ex )
138+ {
139+ Console . WriteLine ( "cell pos:" + cmd . Target . ToString ( ) ) ;
140+ Console . WriteLine ( "sender:" + cmd . Sender ) ;
141+ Console . WriteLine ( "calced time:" + time . ToString ( ) ) ;
142+ throw ex ;
124143 }
125144 }
126145
127146 private void ProcessVictory ( )
128147 {
129148 int iCount = 0 ;
149+
130150 foreach ( Player player in Players . Values )
131151 {
132152 if ( player . State == PlayerState . Playing )
0 commit comments