11use anchor_lang:: prelude:: * ;
22
3- use crate :: error:: ChessError ;
3+ use crate :: { error:: ChessError , BOARD_SIZE , MAX_MOVE_COUNT } ;
44
55#[ derive( AnchorSerialize , AnchorDeserialize , Debug , InitSpace , Default , Clone , PartialEq , Eq ) ]
66pub enum GameStatus {
@@ -168,15 +168,6 @@ pub struct TimeControl {
168168 pub move_time_limit : Option < u32 > ,
169169}
170170
171- #[ derive( AnchorSerialize , AnchorDeserialize , Debug , InitSpace , Clone ) ]
172- pub struct RatingChange {
173- pub old_rating : u16 ,
174- pub new_rating : u16 ,
175- pub opponent_rating : u16 ,
176- pub timestamp : i64 ,
177- pub game_result : GameResult ,
178- }
179-
180171#[ account]
181172#[ derive( Debug , InitSpace ) ]
182173pub struct GameAccount {
@@ -194,7 +185,7 @@ pub struct GameAccount {
194185 // Game State
195186 pub game_status : GameStatus ,
196187 pub current_turn : PlayerColor ,
197- pub board_state : [ u8 ; 64 ] , // 8x8 board encoding
188+ pub board_state : [ u8 ; BOARD_SIZE ] , // 8x8 board encoding
198189 pub move_count : u16 ,
199190 pub halfmove_clock : u8 , // for 50-move rule
200191 pub fullmove_number : u16 ,
@@ -224,11 +215,11 @@ pub struct GameAccount {
224215 pub move_time_limit : Option < u32 > , // seconds per move
225216 pub total_time_limit_white : Option < u32 > ,
226217 pub total_time_limit_black : Option < u32 > ,
218+ pub time_increment : Option < u32 > , // seconds per move increment
227219 pub time_used_white : u32 ,
228220 pub time_used_black : u32 ,
229221
230222 // Game Settings
231- pub rated_game : bool ,
232223 pub allow_draw_offers : bool ,
233224
234225 // draw
@@ -246,6 +237,8 @@ pub struct GameAccount {
246237 pub white_claimed_amount : u64 ,
247238 pub black_claimed_amount : u64 ,
248239 pub distribution_complete : bool ,
240+ // state - prevent reentrancy
241+ pub is_claiming : bool ,
249242}
250243
251244impl GameAccount {
@@ -258,7 +251,6 @@ impl GameAccount {
258251 token_mint : Pubkey ,
259252 token_vault : Pubkey ,
260253 time_control : Option < TimeControl > ,
261- rated_game : bool ,
262254 ) -> Self {
263255 let initial_board = Self :: initialize_chess_board ( ) ;
264256
@@ -291,12 +283,12 @@ impl GameAccount {
291283 started_at : None ,
292284 last_move_at : None ,
293285 finished_at : None ,
294- move_time_limit : time_control. as_ref ( ) . map ( |tc| tc. move_time_limit ) . flatten ( ) ,
286+ move_time_limit : time_control. as_ref ( ) . and_then ( |tc| tc. move_time_limit ) ,
295287 total_time_limit_white : time_control. as_ref ( ) . map ( |tc| tc. initial_time ) ,
296288 total_time_limit_black : time_control. as_ref ( ) . map ( |tc| tc. initial_time ) ,
289+ time_increment : time_control. as_ref ( ) . map ( |tc| tc. increment ) ,
297290 time_used_white : 0 ,
298291 time_used_black : 0 ,
299- rated_game,
300292 allow_draw_offers : true ,
301293 draw_offer_pending : false ,
302294 draw_offered_by : None ,
@@ -311,11 +303,12 @@ impl GameAccount {
311303 white_claimed_amount : 0 ,
312304 black_claimed_amount : 0 ,
313305 distribution_complete : false ,
306+ is_claiming : false ,
314307 }
315308 }
316309
317- fn initialize_chess_board ( ) -> [ u8 ; 64 ] {
318- let mut board = [ 0u8 ; 64 ] ;
310+ fn initialize_chess_board ( ) -> [ u8 ; BOARD_SIZE ] {
311+ let mut board = [ 0u8 ; BOARD_SIZE ] ;
319312
320313 // White pieces (bottom ranks)
321314 board[ 0 ] = 0x14 ; // White Rook
@@ -356,15 +349,15 @@ impl GameAccount {
356349 }
357350
358351 pub fn get_piece_at ( & self , square : u8 ) -> u8 {
359- if square < 64 {
352+ if square < ( BOARD_SIZE as u8 ) {
360353 self . board_state [ square as usize ]
361354 } else {
362355 0
363356 }
364357 }
365358
366359 pub fn set_piece_at ( & mut self , square : u8 , piece : u8 ) {
367- if square < 64 {
360+ if square < ( BOARD_SIZE as u8 ) {
368361 self . board_state [ square as usize ] = piece;
369362 }
370363 }
@@ -461,8 +454,7 @@ impl GameAccount {
461454
462455 /// Get time increment for this game
463456 pub fn get_time_increment ( & self ) -> Option < u32 > {
464- // This would be stored in time control settings if implemented
465- None // Placeholder
457+ self . time_increment
466458 }
467459
468460 pub fn clear_draw_offer ( & mut self ) {
@@ -480,17 +472,18 @@ pub struct MoveHistory {
480472 pub _padding1 : [ u8 ; 3 ] ,
481473 pub move_count : u8 ,
482474 pub _padding2 : [ u8 ; 3 ] ,
483- pub moves : [ ChessMove ; 50 ] ,
475+ pub moves : [ ChessMove ; MAX_MOVE_COUNT as usize ] ,
484476}
485477
486478impl MoveHistory {
487479 pub fn add_move ( & mut self , chess_move : ChessMove ) -> Result < ( ) > {
488- if self . move_count >= 50 {
489- return err ! ( ChessError :: TooManyMoves ) ;
490- }
480+ require ! (
481+ ( self . move_count as usize ) < self . moves. len( ) ,
482+ ChessError :: TooManyMoves
483+ ) ;
491484
492485 self . moves [ self . move_count as usize ] = chess_move;
493- self . move_count += 1 ;
486+ self . move_count = self . move_count . saturating_add ( 1 ) ;
494487 Ok ( ( ) )
495488 }
496489}
0 commit comments