Added piles, cleaned up dragging positioning, various improvements 📈
This commit is contained in:
61
src/game.rs
61
src/game.rs
@ -52,6 +52,9 @@ enum Event {
|
||||
PlayFromHand(Player),
|
||||
Bounce(Player),
|
||||
Tap(Uuid),
|
||||
Discard(Player),
|
||||
Kill(Uuid, bool),
|
||||
Unkill(Uuid, bool),
|
||||
}
|
||||
|
||||
#[derive(Serialize, Clone, Debug)]
|
||||
@ -82,6 +85,7 @@ pub struct OnePlayerGameState {
|
||||
turn_player: Player,
|
||||
opponent_cards_in_hand: usize,
|
||||
you: Player,
|
||||
deck_size: usize,
|
||||
}
|
||||
|
||||
impl GameState {
|
||||
@ -101,6 +105,7 @@ impl GameState {
|
||||
_ => self.hands[0].cards.len(),
|
||||
},
|
||||
you: player,
|
||||
deck_size: self.deck.len(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -237,7 +242,7 @@ pub fn play_from_hand(game_state: &mut GameState, hand_index: usize, player: Pla
|
||||
play_uuid,
|
||||
InPlay {
|
||||
id: card,
|
||||
position_x: 5,
|
||||
position_x: 50,
|
||||
position_y: 50,
|
||||
owner: player,
|
||||
tapped: false,
|
||||
@ -275,3 +280,57 @@ pub fn move_played_card(game_state: &mut GameState, play_id: Uuid, position_x: u
|
||||
played_card.position_y = position_y;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn discard(game_state: &mut GameState, hand_index: usize, player: Player, shadow: bool) {
|
||||
let player_hand = match player {
|
||||
Player::A => &mut game_state.hands[0],
|
||||
Player::B => &mut game_state.hands[1],
|
||||
};
|
||||
game_state.events.push(CountedEvent {
|
||||
id: game_state.events.len(),
|
||||
event: Event::Discard(player.clone()),
|
||||
});
|
||||
let card = player_hand.cards.remove(hand_index);
|
||||
if shadow {
|
||||
game_state.shadow_realm.push(card);
|
||||
} else {
|
||||
game_state.discard_pile.push(card);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn kill(game_state: &mut GameState, play_id: Uuid, shadow: bool) {
|
||||
let played_card = game_state.play.remove(&play_id).unwrap();
|
||||
game_state.events.push(CountedEvent {
|
||||
id: game_state.events.len(),
|
||||
event: Event::Kill(played_card.id.clone(), shadow),
|
||||
});
|
||||
if shadow {
|
||||
game_state.shadow_realm.push(played_card.id);
|
||||
} else {
|
||||
game_state.discard_pile.push(played_card.id);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unkill(game_state: &mut GameState, player: Player, pile_index: usize, shadow: bool) {
|
||||
let card = if shadow {
|
||||
game_state.shadow_realm.remove(pile_index)
|
||||
} else {
|
||||
game_state.discard_pile.remove(pile_index)
|
||||
};
|
||||
let play_uuid = Uuid::new_v4();
|
||||
game_state.events.push(CountedEvent {
|
||||
id: game_state.events.len(),
|
||||
event: Event::Unkill(card, shadow),
|
||||
});
|
||||
game_state.play.insert(
|
||||
play_uuid,
|
||||
InPlay {
|
||||
id: card,
|
||||
position_x: 50,
|
||||
position_y: 50,
|
||||
owner: player,
|
||||
tapped: false,
|
||||
play_id: play_uuid,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
120
src/main.rs
120
src/main.rs
@ -4,6 +4,7 @@ mod game;
|
||||
use game::GameState;
|
||||
use game::Player;
|
||||
use rocket::fs::{relative, FileServer};
|
||||
use rocket::response::content;
|
||||
use rocket::response::status::BadRequest;
|
||||
use rocket::State;
|
||||
use std::collections::HashMap;
|
||||
@ -11,11 +12,11 @@ use std::sync::{Arc, Mutex};
|
||||
use uuid::{uuid, Uuid};
|
||||
|
||||
#[get("/")]
|
||||
fn index(player_uuids: &State<PlayerUuids>) -> String {
|
||||
format!(
|
||||
"localhost:8000/{}/\nlocalhost:8000/{}/",
|
||||
player_uuids.a, player_uuids.b
|
||||
)
|
||||
fn index(player_uuids: &State<PlayerUuids>) -> content::RawHtml<String> {
|
||||
content::RawHtml(format!(
|
||||
"<a href=\"localhost:8000/{}/\">localhost:8000/{}</a><br /><a href=\"localhost:8000/{}/\">localhost:8000/{}</a>",
|
||||
player_uuids.a, player_uuids.a, player_uuids.b, player_uuids.b
|
||||
))
|
||||
}
|
||||
|
||||
#[get("/get_events")]
|
||||
@ -124,7 +125,108 @@ fn play(
|
||||
let game_state_mutex = Arc::clone(&game_state_arc.state);
|
||||
let mut game_state = game_state_mutex.lock().unwrap();
|
||||
game::play_from_hand(&mut game_state, index, player.clone());
|
||||
//println!("{:#?}", game_state);
|
||||
Ok(format!("{}", game::get_events(&game_state)))
|
||||
}
|
||||
|
||||
#[get("/<uuid>/unkill/<index>")]
|
||||
fn unkill(
|
||||
uuid: Uuid,
|
||||
index: usize,
|
||||
game_state_arc: &State<ArcMutexGameState>,
|
||||
player_uuids: &State<PlayerUuids>,
|
||||
) -> Result<String, BadRequest<String>> {
|
||||
let player = match player_uuids.map.get(&uuid) {
|
||||
Some(player) => player,
|
||||
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::unkill(&mut game_state, player.clone(), index, false);
|
||||
Ok(format!("{}", game::get_events(&game_state)))
|
||||
}
|
||||
|
||||
#[get("/<uuid>/unbanish/<index>")]
|
||||
fn unbanish(
|
||||
uuid: Uuid,
|
||||
index: usize,
|
||||
game_state_arc: &State<ArcMutexGameState>,
|
||||
player_uuids: &State<PlayerUuids>,
|
||||
) -> Result<String, BadRequest<String>> {
|
||||
let player = match player_uuids.map.get(&uuid) {
|
||||
Some(player) => player,
|
||||
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::unkill(&mut game_state, player.clone(), index, true);
|
||||
Ok(format!("{}", game::get_events(&game_state)))
|
||||
}
|
||||
|
||||
#[get("/<uuid>/discard/<index>")]
|
||||
fn discard(
|
||||
uuid: Uuid,
|
||||
index: usize,
|
||||
game_state_arc: &State<ArcMutexGameState>,
|
||||
player_uuids: &State<PlayerUuids>,
|
||||
) -> Result<String, BadRequest<String>> {
|
||||
let player = match player_uuids.map.get(&uuid) {
|
||||
Some(player) => player,
|
||||
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::discard(&mut game_state, index, player.clone(), false);
|
||||
Ok(format!("{}", game::get_events(&game_state)))
|
||||
}
|
||||
|
||||
#[get("/<uuid>/forget/<index>")]
|
||||
fn forget(
|
||||
uuid: Uuid,
|
||||
index: usize,
|
||||
game_state_arc: &State<ArcMutexGameState>,
|
||||
player_uuids: &State<PlayerUuids>,
|
||||
) -> Result<String, BadRequest<String>> {
|
||||
let player = match player_uuids.map.get(&uuid) {
|
||||
Some(player) => player,
|
||||
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::discard(&mut game_state, index, player.clone(), true);
|
||||
Ok(format!("{}", game::get_events(&game_state)))
|
||||
}
|
||||
|
||||
#[get("/<uuid>/kill/<play_id>")]
|
||||
fn kill(
|
||||
uuid: Uuid,
|
||||
play_id: Uuid,
|
||||
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::kill(&mut game_state, play_id, false);
|
||||
Ok(format!("{}", game::get_events(&game_state)))
|
||||
}
|
||||
|
||||
#[get("/<uuid>/banish/<play_id>")]
|
||||
fn banish(
|
||||
uuid: Uuid,
|
||||
play_id: Uuid,
|
||||
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::kill(&mut game_state, play_id, true);
|
||||
Ok(format!("{}", game::get_events(&game_state)))
|
||||
}
|
||||
|
||||
@ -282,6 +384,12 @@ fn rocket() -> _ {
|
||||
bounce,
|
||||
tap,
|
||||
move_card,
|
||||
discard,
|
||||
kill,
|
||||
forget,
|
||||
banish,
|
||||
unkill,
|
||||
unbanish
|
||||
],
|
||||
)
|
||||
.manage(ArcMutexGameState {
|
||||
|
||||
Reference in New Issue
Block a user