clean up game setup
This commit is contained in:
parent
5499a854b0
commit
afaf695f2d
1 changed files with 41 additions and 79 deletions
|
@ -54,7 +54,7 @@ impl GameHandler {
|
|||
.clone();
|
||||
|
||||
// Create player
|
||||
this_game.write().unwrap().create_player(this_user).unwrap();
|
||||
this_game.write().unwrap().create_player(this_user);
|
||||
|
||||
// Send updates for all players
|
||||
for player in this_game.read().unwrap().players.values() {
|
||||
|
@ -289,58 +289,47 @@ pub struct Game {
|
|||
|
||||
impl Game {
|
||||
fn new(state: Arc<AppState>, request: NewGameManifest) -> Result<Self> {
|
||||
let mut game = Game {
|
||||
uuid: Uuid::now_v7(),
|
||||
name: request.host.read().unwrap().name.clone(),
|
||||
host: request.host.clone(),
|
||||
white: vec![],
|
||||
black: vec![],
|
||||
players: HashMap::new(),
|
||||
current_black: Arc::new(CardBlack {
|
||||
text: "test".to_string(),
|
||||
pack: 0u8,
|
||||
pick: 0u8,
|
||||
}),
|
||||
packs: request.packs.clone(),
|
||||
};
|
||||
tracing::debug!(
|
||||
"Creating game {} with {} as host",
|
||||
&request.name,
|
||||
request.host.read().unwrap().name
|
||||
&request.host.read().unwrap().name
|
||||
);
|
||||
game.name = request.name;
|
||||
game.host = request.host.clone();
|
||||
|
||||
game.build_decks(state, request.packs)?;
|
||||
game.create_player(request.host)?;
|
||||
game.deal_black();
|
||||
|
||||
Ok(game)
|
||||
}
|
||||
|
||||
/// Build game decks from input data for game start.
|
||||
/// This should only run once and at startup.
|
||||
fn build_decks(&mut self, state: Arc<AppState>, selected_packs: Vec<u8>) -> Result<()> {
|
||||
// TODO: Make this right -- remove the clones, use references to single cards
|
||||
for pack_num in selected_packs {
|
||||
// Build the decks
|
||||
let mut white = vec![];
|
||||
let mut black = vec![];
|
||||
for pack_num in &request.packs {
|
||||
if let Some(pack) = state.packs.official.get(&pack_num) {
|
||||
if let Some(white) = &pack.white {
|
||||
self.white.extend(white.clone())
|
||||
if let Some(card) = &pack.white {
|
||||
white.extend(card.clone())
|
||||
}
|
||||
if let Some(black) = &pack.black {
|
||||
self.black.extend(black.clone())
|
||||
if let Some(card) = &pack.black {
|
||||
black.extend(card.clone())
|
||||
}
|
||||
} else if let Some(pack) = state.packs.unofficial.get(&pack_num) {
|
||||
if let Some(white) = &pack.white {
|
||||
self.white.extend(white.clone())
|
||||
if let Some(card) = &pack.white {
|
||||
white.extend(card.clone())
|
||||
}
|
||||
if let Some(black) = &pack.black {
|
||||
self.black.extend(black.clone())
|
||||
if let Some(card) = &pack.black {
|
||||
black.extend(card.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
let current_black = black.swap_remove((0..black.len()).choose(&mut thread_rng()).unwrap());
|
||||
white.shrink_to_fit();
|
||||
black.shrink_to_fit();
|
||||
|
||||
Ok(Game {
|
||||
uuid: Uuid::now_v7(),
|
||||
name: request.name,
|
||||
host: request.host.clone(),
|
||||
white,
|
||||
black,
|
||||
players: HashMap::new(),
|
||||
current_black,
|
||||
packs: request.packs.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Draw one white card at random from play deck.
|
||||
|
@ -355,53 +344,26 @@ impl Game {
|
|||
}
|
||||
}
|
||||
|
||||
/// Draw one black card at random from play deck.
|
||||
fn draw_one_black(&mut self) -> Option<Arc<CardBlack>> {
|
||||
let deck = &mut self.black;
|
||||
|
||||
// TODO: this feels sloppy
|
||||
if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) {
|
||||
Some(deck.swap_remove(index))
|
||||
} else {
|
||||
tracing::error!("Tried to draw black card that doesn't exist!");
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Deal a black card and use it for the current round
|
||||
fn deal_black(&mut self) {
|
||||
if let Some(black_card) = self.draw_one_black() {
|
||||
self.current_black = black_card;
|
||||
} else {
|
||||
tracing::error!("Tried to deal black card that doesn't exist!");
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new player and add them to the game.
|
||||
pub fn create_player(&mut self, user: Arc<RwLock<User>>) -> Result<()> {
|
||||
let mut new_player = Player {
|
||||
pub fn create_player(&mut self, user: Arc<RwLock<User>>) {
|
||||
// Build hand of 10 white cards
|
||||
let mut white = vec![];
|
||||
for _ in 0..10 {
|
||||
if let Some(card) = self.draw_one_white() {
|
||||
white.push(card);
|
||||
}
|
||||
}
|
||||
|
||||
// New player object
|
||||
let new_player = Player {
|
||||
user: user.clone(),
|
||||
white: vec![],
|
||||
white,
|
||||
black: vec![],
|
||||
};
|
||||
|
||||
let new_player_name = user.read().unwrap().name.clone();
|
||||
tracing::debug!("Creating player for {}", &new_player_name);
|
||||
|
||||
let mut hand_buf = vec![];
|
||||
for _ in 0..10 {
|
||||
if let Some(card) = self.draw_one_white() {
|
||||
hand_buf.push(card);
|
||||
}
|
||||
}
|
||||
tracing::debug!("Dealing hand to {}", &new_player_name);
|
||||
|
||||
new_player.white.extend(hand_buf);
|
||||
|
||||
// Add player to game
|
||||
self.players
|
||||
.insert(user.read().unwrap().uuid.clone(), new_player);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue