Added movement functionality to the frontend 💣
This commit is contained in:
12
src/game.rs
12
src/game.rs
@ -27,8 +27,8 @@ pub enum Player {
|
||||
struct InPlay {
|
||||
#[serde(with = "uuid::serde::braced")]
|
||||
id: Uuid,
|
||||
position_x: u16,
|
||||
position_y: u16,
|
||||
position_x: u8,
|
||||
position_y: u8,
|
||||
owner: Player,
|
||||
tapped: bool,
|
||||
#[serde(with = "uuid::serde::braced")]
|
||||
@ -260,6 +260,7 @@ pub fn bounce(game_state: &mut GameState, play_id: Uuid) {
|
||||
}
|
||||
|
||||
pub fn tap(game_state: &mut GameState, play_id: Uuid) {
|
||||
//let played_card = game_state.play.remove(&play_id).unwrap();
|
||||
game_state.events.push(CountedEvent {
|
||||
id: game_state.events.len(),
|
||||
event: Event::Tap(play_id.clone()),
|
||||
@ -267,3 +268,10 @@ pub fn tap(game_state: &mut GameState, play_id: Uuid) {
|
||||
let played_card = game_state.play.get_mut(&play_id).unwrap();
|
||||
played_card.tapped = !played_card.tapped;
|
||||
}
|
||||
|
||||
pub fn move_played_card(game_state: &mut GameState, play_id: Uuid, position_x: u8, position_y: u8) {
|
||||
if let Some(played_card) = game_state.play.get_mut(&play_id) {
|
||||
played_card.position_x = position_x;
|
||||
played_card.position_y = position_y;
|
||||
}
|
||||
}
|
||||
|
||||
24
src/main.rs
24
src/main.rs
@ -74,6 +74,25 @@ fn bounce(
|
||||
Ok(format!("{}", game::get_events(&game_state)))
|
||||
}
|
||||
|
||||
#[get("/<uuid>/move/<play_id>/<x>/<y>")]
|
||||
fn move_card(
|
||||
uuid: Uuid,
|
||||
play_id: Uuid,
|
||||
x: u8,
|
||||
y: u8,
|
||||
game_state_arc: &State<ArcMutexGameState>,
|
||||
player_uuids: &State<PlayerUuids>,
|
||||
) -> Result<String, BadRequest<String>> {
|
||||
match player_uuids.map.get(&uuid) {
|
||||
Some(_) => (),
|
||||
None => return Err(BadRequest(format!("Invalid player {}.", uuid))),
|
||||
};
|
||||
let game_state_mutex = Arc::clone(&game_state_arc.state);
|
||||
let mut game_state = game_state_mutex.lock().unwrap();
|
||||
game::move_played_card(&mut game_state, play_id, x, y);
|
||||
Ok(format!("{}", game::get_events(&game_state)))
|
||||
}
|
||||
|
||||
#[get("/<uuid>/tap/<play_id>")]
|
||||
fn tap(
|
||||
uuid: Uuid,
|
||||
@ -220,8 +239,8 @@ struct PlayerUuids {
|
||||
#[launch]
|
||||
fn rocket() -> _ {
|
||||
let mut game_state = game::new();
|
||||
game::draw(&mut game_state, 7, Player::A);
|
||||
game::draw(&mut game_state, 7, Player::B);
|
||||
//game::draw(&mut game_state, 7, Player::A);
|
||||
//game::draw(&mut game_state, 7, Player::B);
|
||||
let game_state_arc = Arc::new(Mutex::new(game_state));
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
@ -262,6 +281,7 @@ fn rocket() -> _ {
|
||||
play,
|
||||
bounce,
|
||||
tap,
|
||||
move_card,
|
||||
],
|
||||
)
|
||||
.manage(ArcMutexGameState {
|
||||
|
||||
Reference in New Issue
Block a user