Compare commits

..

No commits in common. "d78b8b1416533e8f7fe179e6020e49959686cbbf" and "c48c63debb41b404f4261d2ff86ba8171c101d0d" have entirely different histories.

3 changed files with 66 additions and 175 deletions

View File

@ -1,7 +1,4 @@
mod cards;
use async_std::{prelude::*, sync::RwLock}; use async_std::{prelude::*, sync::RwLock};
use cards::*;
use itertools::Itertools; use itertools::Itertools;
use rand::{seq::SliceRandom, thread_rng}; use rand::{seq::SliceRandom, thread_rng};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -10,6 +7,8 @@ use tide::{Body, Redirect, Request, Response};
use tide_websockets::{Message, WebSocket, WebSocketConnection}; use tide_websockets::{Message, WebSocket, WebSocketConnection};
use uuid::Uuid; use uuid::Uuid;
type Card = String;
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(tag = "type")] #[serde(tag = "type")]
enum ClientMessage { enum ClientMessage {
@ -71,33 +70,23 @@ struct PlayerState {
name: String, name: String,
draw_pile_count: usize, draw_pile_count: usize,
hand_count: usize, hand_count: usize,
discard_pile: Option<String>, discard_pile: Option<Card>,
played_cards: Vec<String>, played_cards: Vec<Card>,
} }
#[derive(Serialize)] #[derive(Serialize)]
struct PileState { struct PileState {
name: Card, name: String,
count: usize, count: usize,
} }
#[derive(Clone, Serialize)] #[derive(Serialize)]
struct TurnState { struct TurnState {
actions: u32, actions: u32,
buys: u32, buys: u32,
coin: u32, coin: u32,
} }
impl Default for TurnState {
fn default() -> Self {
TurnState {
actions: 1,
buys: 1,
coin: 0,
}
}
}
struct Player { struct Player {
id: String, id: String,
name: String, name: String,
@ -132,6 +121,10 @@ impl Player {
} }
} }
pub fn play_card(&mut self, index: usize) {
self.played_cards.push(self.hand.remove(index));
}
pub fn discard(&mut self, index: usize) { pub fn discard(&mut self, index: usize) {
self.discard_pile.push(self.hand.remove(index)); self.discard_pile.push(self.hand.remove(index));
} }
@ -148,16 +141,16 @@ impl Default for GameSetup {
fn default() -> GameSetup { fn default() -> GameSetup {
GameSetup { GameSetup {
deck: vec![ deck: vec![
copper(), "Copper".into(),
copper(), "Copper".into(),
copper(), "Copper".into(),
copper(), "Copper".into(),
copper(), "Copper".into(),
copper(), "Copper".into(),
copper(), "Copper".into(),
estate(), "Estate".into(),
estate(), "Estate".into(),
estate(), "Estate".into(),
], ],
} }
} }
@ -169,9 +162,8 @@ struct Game {
setup: GameSetup, setup: GameSetup,
connections: HashMap<Uuid, (String, WebSocketConnection)>, connections: HashMap<Uuid, (String, WebSocketConnection)>,
active_player: usize, active_player: usize,
supply: Vec<(Card, usize)>, supply: Vec<(String, usize)>,
trash: Vec<Card>, trash: Vec<Card>,
turn_state: TurnState,
} }
impl Game { impl Game {
@ -184,7 +176,6 @@ impl Game {
active_player: 0, active_player: 0,
supply: vec![], supply: vec![],
trash: vec![], trash: vec![],
turn_state: TurnState::default(),
} }
} }
@ -199,119 +190,28 @@ impl Game {
} }
let victory_qty = match self.players.len() { let victory_qty = match self.players.len() {
x if x <= 2 => 8, x if x <=2 => 8,
_ => 12, _ => 12,
}; };
self.supply = vec![ self.supply = vec![
(copper(), 60 - self.players.len() * 7), ("Copper".into(), 60 - self.players.len() * 7),
(silver(), 40), ("Silver".into(), 40),
(gold(), 30), ("Gold".into(), 30),
(estate(), victory_qty), ("Estate".into(), victory_qty),
( ("Duchery".into(), victory_qty),
Card { ("Province".into(), victory_qty),
name: "Duchy".into(), ("Curse".into(), 10),
cost: 5, ("Cellar".into(), 10),
types: vec![], ("Moat".into(), 10),
}, ("Village".into(), 10),
victory_qty, ("Merchant".into(), 10),
), ("Workshop".into(), 10),
( ("Smithy".into(), 10),
Card { ("Remodel".into(), 10),
name: "Province".into(), ("Militia".into(), 10),
cost: 8, ("Market".into(), 10),
types: vec![], ("Mine".into(), 10),
},
victory_qty,
),
(
Card {
name: "Curse".into(),
cost: 0,
types: vec![],
},
10,
),
(
Card {
name: "Cellar".into(),
cost: 2,
types: vec![],
},
10,
),
(
Card {
name: "Moat".into(),
cost: 2,
types: vec![],
},
10,
),
(
Card {
name: "Village".into(),
cost: 3,
types: vec![],
},
10,
),
(
Card {
name: "Merchant".into(),
cost: 3,
types: vec![],
},
10,
),
(
Card {
name: "Workshop".into(),
cost: 3,
types: vec![],
},
10,
),
(
Card {
name: "Smithy".into(),
cost: 4,
types: vec![],
},
10,
),
(
Card {
name: "Remodel".into(),
cost: 4,
types: vec![],
},
10,
),
(
Card {
name: "Militia".into(),
cost: 4,
types: vec![],
},
10,
),
(
Card {
name: "Market".into(),
cost: 5,
types: vec![],
},
10,
),
(
Card {
name: "Mine".into(),
cost: 5,
types: vec![],
},
10,
),
]; ];
} }
@ -333,16 +233,12 @@ impl Game {
self.active_player += 1; self.active_player += 1;
self.active_player %= self.players.len(); self.active_player %= self.players.len();
self.turn_state = TurnState::default();
} }
// Check if the end game condition is reached and finish the game if so, // Check if the end game condition is reached and finish the game if so,
// sending a message to all clients. // sending a message to all clients.
fn end_game(&mut self) { fn end_game(&mut self) {
let provinces = self let provinces = self.supply.iter().any(|(c, n)| *c == "Province" && *n == 0);
.supply
.iter()
.any(|(c, n)| (*c).name == "Province" && *n == 0);
let supply = self.supply.iter().filter(|(_, n)| *n == 0).count() > 2; let supply = self.supply.iter().filter(|(_, n)| *n == 0).count() > 2;
if supply || provinces { if supply || provinces {
@ -367,17 +263,6 @@ impl Game {
self.trash self.trash
.push(self.players[player_number].hand.remove(index)); .push(self.players[player_number].hand.remove(index));
} }
pub fn play_card(&mut self, player_number: usize, index: usize) {
let player = self.players.get_mut(player_number).unwrap();
let card = player.hand.remove(index);
if let Some(coin) = card.treasure() {
self.turn_state.coin += coin;
}
player.played_cards.push(card);
}
} }
#[derive(Clone, Default)] #[derive(Clone, Default)]
@ -408,20 +293,24 @@ async fn broadcast_state(game: &Game) {
name: html_escape::encode_text(&p.name).into(), name: html_escape::encode_text(&p.name).into(),
draw_pile_count: p.draw_pile.len(), draw_pile_count: p.draw_pile.len(),
hand_count: p.hand.len(), hand_count: p.hand.len(),
discard_pile: p.discard_pile.last().map(|c| c.name.clone()), discard_pile: p.discard_pile.last().map(|c| c.clone()),
played_cards: p.played_cards.iter().map(|c| c.name.clone()).collect(), played_cards: p.played_cards.clone(),
}) })
.collect(), .collect(),
active_player: game.active_player, active_player: game.active_player,
supply: game supply: game
.supply .supply
.iter() .iter()
.map(|(card, count)| PileState { .map(|(name, count)| PileState {
name: card.clone(), name: name.into(),
count: count.clone(), count: count.clone(),
}) })
.collect(), .collect(),
turn_state: game.turn_state.clone(), turn_state: TurnState {
actions: 1,
buys: 1,
coin: 0,
},
trash: game.trash.clone(), trash: game.trash.clone(),
}; };
@ -442,15 +331,12 @@ async fn broadcast_state(game: &Game) {
.iter() .iter()
.enumerate() .enumerate()
.map(|(i, p)| { .map(|(i, p)| {
let score = p let score = p.draw_pile.iter().fold(0, |acc, card| match card.as_str() {
.draw_pile "Province" => acc + 6,
.iter() "Duchery" => acc + 3,
.fold(0, |acc, card| match card.name.as_str() { "Estate" => acc + 1,
"Province" => acc + 6, _ => acc,
"Duchery" => acc + 3, });
"Estate" => acc + 1,
_ => acc,
});
(i, score) (i, score)
}) })
.sorted_by(|a, b| Ord::cmp(&b.1, &a.1)) .sorted_by(|a, b| Ord::cmp(&b.1, &a.1))
@ -651,8 +537,8 @@ async fn main() -> Result<(), std::io::Error> {
let mut games = req.state().games.write().await; let mut games = req.state().games.write().await;
let game = games.get_mut(&game_id).unwrap(); let game = games.get_mut(&game_id).unwrap();
let card_name = game.players[player_number].hand[index].name.clone(); let card_name = game.players[player_number].hand[index].clone();
game.play_card(player_number, index); game.players[player_number].play_card(index);
notify_players( notify_players(
&game, &game,
@ -668,13 +554,15 @@ async fn main() -> Result<(), std::io::Error> {
game.supply.get_mut(index).unwrap().1 = game.supply.get_mut(index).unwrap().1 =
game.supply.get(index).unwrap().1 - 1; game.supply.get(index).unwrap().1 - 1;
let card = game.supply[index].0.clone(); let card_name = game.supply[index].0.clone();
game.players[player_number].discard_pile.push(card.clone()); game.players[player_number]
.discard_pile
.push(card_name.clone());
notify_players( notify_players(
&game, &game,
format!("{} nimmt {}", game.players[player_number].name, card.name), format!("{} nimmt {}", game.players[player_number].name, card_name),
) )
.await; .await;
broadcast_state(&game).await; broadcast_state(&game).await;

View File

@ -883,7 +883,7 @@ img.card:hover {
setup_state = { setup_state = {
starting_deck: [], starting_deck: [],
basic_cards: ["Copper", "Silver", "Gold", "Estate", "Duchy", "Province", "Curse"], basic_cards: ["Copper", "Silver", "Gold", "Estate", "Duchery", "Province", "Curse"],
kingdom_cards: ["Cellar", "Moat", "Village", "Merchant", "Workshop", "Smithy", "Remodel", "Militia", "Market", "Mine"], kingdom_cards: ["Cellar", "Moat", "Village", "Merchant", "Workshop", "Smithy", "Remodel", "Militia", "Market", "Mine"],
socket: webSocket socket: webSocket
} }

BIN
static/images/cards/duchery.jpg (Stored with Git LFS) Normal file

Binary file not shown.