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,
|
EndTurn,
|
||||||
PlayCard { name: String, index: usize },
|
PlayCard { name: String, index: usize },
|
||||||
GainCard { name: String, index: usize },
|
GainCard { name: String, index: usize },
|
||||||
|
BuyCard { index: usize },
|
||||||
DrawCard,
|
DrawCard,
|
||||||
Discard { index: usize },
|
Discard { index: usize },
|
||||||
TrashHand { index: usize },
|
TrashHand { index: usize },
|
||||||
@ -362,8 +363,38 @@ impl Game {
|
|||||||
self.turn_state.coin += coin;
|
self.turn_state.coin += coin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(_) = card.action() {
|
||||||
|
self.turn_state.actions -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
player.played_cards.push(card);
|
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)]
|
#[derive(Clone, Default)]
|
||||||
@ -646,6 +677,27 @@ async fn main() -> Result<(), std::io::Error> {
|
|||||||
broadcast_state(&game).await;
|
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 } => {
|
ClientMessage::GainCard { name: _, index } => {
|
||||||
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();
|
||||||
|
@ -481,7 +481,6 @@ img.card:hover {
|
|||||||
|
|
||||||
function SupplyPile(initialVnode) {
|
function SupplyPile(initialVnode) {
|
||||||
var dragStart = function(ev) {
|
var dragStart = function(ev) {
|
||||||
console.log(ev);
|
|
||||||
let data = {
|
let data = {
|
||||||
source: "Supply",
|
source: "Supply",
|
||||||
name: ev.target.dataset.name,
|
name: ev.target.dataset.name,
|
||||||
@ -490,6 +489,15 @@ img.card:hover {
|
|||||||
ev.dataTransfer.setData("text", JSON.stringify(data));
|
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 {
|
return {
|
||||||
view: function(vnode) {
|
view: function(vnode) {
|
||||||
return m(".supply-pile",
|
return m(".supply-pile",
|
||||||
@ -499,6 +507,7 @@ img.card:hover {
|
|||||||
"data-index": vnode.attrs.index,
|
"data-index": vnode.attrs.index,
|
||||||
ondragstart: dragStart,
|
ondragstart: dragStart,
|
||||||
draggable: true,
|
draggable: true,
|
||||||
|
ondblclick: doubleclick,
|
||||||
},
|
},
|
||||||
m("img", {
|
m("img", {
|
||||||
class: "card",
|
class: "card",
|
||||||
|
Loading…
Reference in New Issue
Block a user