From 0f2f085efe39677a240bd38727941fcb6e2b3a6f Mon Sep 17 00:00:00 2001 From: Markus Wagner Date: Sat, 9 Jan 2021 21:23:52 +0100 Subject: [PATCH] Add command to buy cards --- src/main.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ static/game.html | 13 ++++++++++-- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 37fc4ba..9e7b11d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,7 @@ enum ClientMessage { EndTurn, PlayCard { name: String, index: usize }, GainCard { name: String, index: usize }, + BuyCard { index: usize }, DrawCard, Discard { index: usize }, TrashHand { index: usize }, @@ -362,8 +363,38 @@ impl Game { self.turn_state.coin += coin; } + if let Some(_) = card.action() { + self.turn_state.actions -= 1; + } + player.played_cards.push(card); } + + pub fn buy_card(&mut self, player_number: usize, index: usize) -> bool /*-> Result<(), &'static str>*/ + { + if player_number != self.active_player { + return false; + } + + if self.turn_state.buys < 1 { + return false; + } + + if let Some(card) = self.supply.get(index).as_deref() { + if card.0.cost <= self.turn_state.coin { + let card = card.0.clone(); + + self.supply.get_mut(index).unwrap().1 = self.supply.get(index).unwrap().1 - 1; + self.turn_state.coin -= card.cost; + self.turn_state.buys -= 1; + self.players[player_number].discard_pile.push(card); + return true; + //return Ok(()); + } + } + //Err("Not enough coin"); + false + } } #[derive(Clone, Default)] @@ -646,6 +677,27 @@ async fn main() -> Result<(), std::io::Error> { broadcast_state(&game).await; } + ClientMessage::BuyCard { index } => { + let mut games = req.state().games.write().await; + let game = games.get_mut(&game_id).unwrap(); + + if game.buy_card(player_number, index) { + if let Some(card) = + game.players[player_number].discard_pile.last().as_deref() + { + notify_players( + &game, + format!( + "{} kauft {}", + game.players[player_number].name, card.name + ), + ) + .await; + broadcast_state(&game).await; + } + } + } + ClientMessage::GainCard { name: _, index } => { let mut games = req.state().games.write().await; let game = games.get_mut(&game_id).unwrap(); diff --git a/static/game.html b/static/game.html index c98d3ce..2102d95 100644 --- a/static/game.html +++ b/static/game.html @@ -481,7 +481,6 @@ img.card:hover { function SupplyPile(initialVnode) { var dragStart = function(ev) { - console.log(ev); let data = { source: "Supply", name: ev.target.dataset.name, @@ -490,7 +489,16 @@ img.card:hover { ev.dataTransfer.setData("text", JSON.stringify(data)); } - return { + var doubleclick = function(ev) { + let msg = { + type: "BuyCard", + index: parseInt(ev.srcElement.parentElement.dataset.index), + } + + webSocket.send(JSON.stringify(msg)); + } + + return { view: function(vnode) { return m(".supply-pile", { @@ -499,6 +507,7 @@ img.card:hover { "data-index": vnode.attrs.index, ondragstart: dragStart, draggable: true, + ondblclick: doubleclick, }, m("img", { class: "card",