Fix server-side promotion handling.

This commit is contained in:
yukirij 2024-08-17 19:08:02 -07:00
parent fd1d99dd77
commit d53e8e272e
4 changed files with 33 additions and 2 deletions

View File

@ -2,7 +2,7 @@ use crate::{
//consts::*, //consts::*,
board::Board, board::Board,
history::Play, history::Play,
piece::Piece, piece::Piece, util::Hex,
}; };
pub type Pool = [u8; 7]; pub type Pool = [u8; 7];
@ -91,6 +91,13 @@ impl Game {
piece.tile = play.to; piece.tile = play.to;
self.board.pieces[pid as usize] = Some(piece); self.board.pieces[pid as usize] = Some(piece);
// Check if piece is promoted.
let hex = Hex::from_tile(play.from);
let offset = (piece.player as i8 * 2) - 1;
if !piece.promoted && !Hex::is_valid(hex.x() as i8, hex.y() as i8 + offset) {
piece.promoted = true;
}
true true
} else { false } } else { false }
} else { false } } else { false }

View File

@ -41,6 +41,27 @@ impl Hex {
} }
} }
pub fn is_valid(x:i8, y:i8) -> bool
{
const COLUMNS :[(i8, i8); 9] = [
(0, 4),
(0, 5),
(0, 6),
(0, 7),
(0, 8),
(1, 8),
(2, 8),
(3, 8),
(4, 8),
];
let valid = x >= 0 && x < 9;
if valid {
let (min, max) = COLUMNS[x as usize];
y >= min && y <= max
} else { false }
}
pub fn x(&self) -> u8 { self.x } pub fn x(&self) -> u8 { self.x }
pub fn y(&self) -> u8 { self.y } pub fn y(&self) -> u8 { self.y }
pub fn tile(&self) -> u8 { self.tile } pub fn tile(&self) -> u8 { self.tile }

View File

@ -15,6 +15,9 @@ const SCENES = {
], []); ], []);
return true; return true;
}, },
reconnect() {
LOAD(CONTEXT.Scene);
}
}, },
Register:{ Register:{

View File

@ -35,7 +35,7 @@ function RESUME() {
CONTEXT.Auth.secret, CONTEXT.Auth.secret,
]); ]);
} else { } else {
LOAD(CONTEXT.Scene); if(SCENE.reconnect !== undefined) { SCENE.reconnect(); }
} }
} }