Add command to buy cards
This commit is contained in:
parent
9ab7d7fcfb
commit
0f2f085efe
52
src/main.rs
52
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();
|
||||
|
@ -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,6 +489,15 @@ img.card:hover {
|
||||
ev.dataTransfer.setData("text", JSON.stringify(data));
|
||||
}
|
||||
|
||||
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",
|
||||
|
Loading…
Reference in New Issue
Block a user