Why doesn't play serialize??? 😢

This commit is contained in:
Seoxi Ryouko
2025-02-08 09:32:03 -06:00
parent c188cfc5e8
commit f69eb3262a
6 changed files with 533 additions and 74 deletions

View File

@ -1,6 +1,6 @@
[
{
"scryfall_id": "{3df927c0-9aa9-450b-ab2a-ae12c5489d57}",
"id": "{3df927c0-9aa9-450b-ab2a-ae12c5489d57}",
"name": "Supplant Form",
"png": "https://cards.scryfall.io/png/front/3/d/3df927c0-9aa9-450b-ab2a-ae12c5489d57.png?1562824437",
"jpg": "https://cards.scryfall.io/small/front/3/d/3df927c0-9aa9-450b-ab2a-ae12c5489d57.jpg?1562824437",
@ -8,7 +8,7 @@
"count": 2
},
{
"scryfall_id": "{30202613-d05f-4f47-af97-d0b75ccac293}",
"id": "{30202613-d05f-4f47-af97-d0b75ccac293}",
"name": "Memory Lapse",
"png": "https://cards.scryfall.io/png/front/3/0/30202613-d05f-4f47-af97-d0b75ccac293.png?1634131658",
"jpg": "https://cards.scryfall.io/small/front/3/0/30202613-d05f-4f47-af97-d0b75ccac293.jpg?1634131658",
@ -16,7 +16,7 @@
"count": 8
},
{
"scryfall_id": "{ad88e5ee-0eee-47af-a7b4-9bac044e1c8c}",
"id": "{ad88e5ee-0eee-47af-a7b4-9bac044e1c8c}",
"name": "Accumulated Knowledge",
"png": "https://cards.scryfall.io/png/front/a/d/ad88e5ee-0eee-47af-a7b4-9bac044e1c8c.png?1562439718",
"jpg": "https://cards.scryfall.io/small/front/a/d/ad88e5ee-0eee-47af-a7b4-9bac044e1c8c.jpg?1562439718",
@ -24,7 +24,7 @@
"count": 4
},
{
"scryfall_id": "{806e5536-103b-4d6c-83b4-8659a55b25b5}",
"id": "{806e5536-103b-4d6c-83b4-8659a55b25b5}",
"name": "Mystic Retrieval",
"png": "https://cards.scryfall.io/png/front/8/0/806e5536-103b-4d6c-83b4-8659a55b25b5.png?1736467785",
"jpg": "https://cards.scryfall.io/small/front/8/0/806e5536-103b-4d6c-83b4-8659a55b25b5.jpg?1736467785",
@ -32,7 +32,7 @@
"count": 2
},
{
"scryfall_id": "{36fa9a0b-b0c9-43ea-ba11-99d7982f974e}",
"id": "{36fa9a0b-b0c9-43ea-ba11-99d7982f974e}",
"name": "Mystical Tutor",
"png": "https://cards.scryfall.io/png/front/3/6/36fa9a0b-b0c9-43ea-ba11-99d7982f974e.png?1675199375",
"jpg": "https://cards.scryfall.io/small/front/3/6/36fa9a0b-b0c9-43ea-ba11-99d7982f974e.jpg?1675199375",
@ -40,7 +40,7 @@
"count": 2
},
{
"scryfall_id": "{21fe2f1b-bfd1-4682-ae23-069300de3791}",
"id": "{21fe2f1b-bfd1-4682-ae23-069300de3791}",
"name": "Svyelunite Temple",
"png": "https://cards.scryfall.io/png/front/2/1/21fe2f1b-bfd1-4682-ae23-069300de3791.png?1562867851",
"jpg": "https://cards.scryfall.io/small/front/2/1/21fe2f1b-bfd1-4682-ae23-069300de3791.jpg?1562867851",
@ -48,7 +48,7 @@
"count": 2
},
{
"scryfall_id": "{8beb987c-1b67-4a4e-ae71-58547afad2a0}",
"id": "{8beb987c-1b67-4a4e-ae71-58547afad2a0}",
"name": "Brainstorm",
"png": "https://cards.scryfall.io/png/front/8/b/8beb987c-1b67-4a4e-ae71-58547afad2a0.png?1726284649",
"jpg": "https://cards.scryfall.io/small/front/8/b/8beb987c-1b67-4a4e-ae71-58547afad2a0.jpg?1726284649",
@ -56,7 +56,7 @@
"count": 2
},
{
"scryfall_id": "{c548d140-3d81-4b33-9985-87703d316a83}",
"id": "{c548d140-3d81-4b33-9985-87703d316a83}",
"name": "Dance of the Skywise",
"png": "https://cards.scryfall.io/png/front/c/5/c548d140-3d81-4b33-9985-87703d316a83.png?1562792779",
"jpg": "https://cards.scryfall.io/small/front/c/5/c548d140-3d81-4b33-9985-87703d316a83.jpg?1562792779",
@ -64,7 +64,7 @@
"count": 2
},
{
"scryfall_id": "{7132999f-6e2d-4689-8131-7b12076a348d}",
"id": "{7132999f-6e2d-4689-8131-7b12076a348d}",
"name": "Island",
"png": "https://cards.scryfall.io/png/front/7/1/7132999f-6e2d-4689-8131-7b12076a348d.png?1655495395",
"jpg": "https://cards.scryfall.io/small/front/7/1/7132999f-6e2d-4689-8131-7b12076a348d.jpg?1655495395",
@ -72,7 +72,7 @@
"count": 18
},
{
"scryfall_id": "{7ca8a427-5c20-4650-bbb5-9895e4f2c009}",
"id": "{7ca8a427-5c20-4650-bbb5-9895e4f2c009}",
"name": "Ray of Command",
"png": "https://cards.scryfall.io/png/front/7/c/7ca8a427-5c20-4650-bbb5-9895e4f2c009.png?1592754536",
"jpg": "https://cards.scryfall.io/small/front/7/c/7ca8a427-5c20-4650-bbb5-9895e4f2c009.jpg?1592754536",
@ -80,7 +80,7 @@
"count": 2
},
{
"scryfall_id": "{2dc0bafd-debc-4b62-9fe0-56b4aad02484}",
"id": "{2dc0bafd-debc-4b62-9fe0-56b4aad02484}",
"name": "Unsubstantiate",
"png": "https://cards.scryfall.io/png/front/2/d/2dc0bafd-debc-4b62-9fe0-56b4aad02484.png?1594735888",
"jpg": "https://cards.scryfall.io/small/front/2/d/2dc0bafd-debc-4b62-9fe0-56b4aad02484.jpg?1594735888",
@ -88,7 +88,7 @@
"count": 2
},
{
"scryfall_id": "{ac2e32d0-f172-4934-9d73-1bc2ab86586e}",
"id": "{ac2e32d0-f172-4934-9d73-1bc2ab86586e}",
"name": "Dandân",
"png": "https://cards.scryfall.io/png/front/a/c/ac2e32d0-f172-4934-9d73-1bc2ab86586e.png?1562781784",
"jpg": "https://cards.scryfall.io/small/front/a/c/ac2e32d0-f172-4934-9d73-1bc2ab86586e.jpg?1562781784",
@ -96,7 +96,7 @@
"count": 10
},
{
"scryfall_id": "{692a5bfa-9a50-4610-acd7-19fc0a53f798}",
"id": "{692a5bfa-9a50-4610-acd7-19fc0a53f798}",
"name": "Mind Bend",
"png": "https://cards.scryfall.io/png/front/6/9/692a5bfa-9a50-4610-acd7-19fc0a53f798.png?1562549129",
"jpg": "https://cards.scryfall.io/small/front/6/9/692a5bfa-9a50-4610-acd7-19fc0a53f798.jpg?1562549129",
@ -104,7 +104,7 @@
"count": 2
},
{
"scryfall_id": "{a340af37-c0a7-4f26-974b-95397b5c32f7}",
"id": "{a340af37-c0a7-4f26-974b-95397b5c32f7}",
"name": "Remote Isle",
"png": "https://cards.scryfall.io/png/front/a/3/a340af37-c0a7-4f26-974b-95397b5c32f7.png?1709139228",
"jpg": "https://cards.scryfall.io/small/front/a/3/a340af37-c0a7-4f26-974b-95397b5c32f7.jpg?1709139228",
@ -112,7 +112,7 @@
"count": 2
},
{
"scryfall_id": "{8eafb2bb-58bf-4c6b-ae8f-91bcea12c7d2}",
"id": "{8eafb2bb-58bf-4c6b-ae8f-91bcea12c7d2}",
"name": "Insidious Will",
"png": "https://cards.scryfall.io/png/front/8/e/8eafb2bb-58bf-4c6b-ae8f-91bcea12c7d2.png?1576381260",
"jpg": "https://cards.scryfall.io/small/front/8/e/8eafb2bb-58bf-4c6b-ae8f-91bcea12c7d2.jpg?1576381260",
@ -120,7 +120,7 @@
"count": 2
},
{
"scryfall_id": "{bfac9395-7ca5-48dd-ab83-7c26ada12f61}",
"id": "{bfac9395-7ca5-48dd-ab83-7c26ada12f61}",
"name": "Izzet Boilerworks",
"png": "https://cards.scryfall.io/png/front/b/f/bfac9395-7ca5-48dd-ab83-7c26ada12f61.png?1712355049",
"jpg": "https://cards.scryfall.io/small/front/b/f/bfac9395-7ca5-48dd-ab83-7c26ada12f61.jpg?1712355049",
@ -128,7 +128,7 @@
"count": 2
},
{
"scryfall_id": "{349ac7e6-af38-4dc3-abfe-369564c75630}",
"id": "{349ac7e6-af38-4dc3-abfe-369564c75630}",
"name": "Predict",
"png": "https://cards.scryfall.io/png/front/3/4/349ac7e6-af38-4dc3-abfe-369564c75630.png?1592710621",
"jpg": "https://cards.scryfall.io/small/front/3/4/349ac7e6-af38-4dc3-abfe-369564c75630.jpg?1592710621",
@ -136,7 +136,7 @@
"count": 2
},
{
"scryfall_id": "{f9ed455f-95cd-48ca-8b0d-4db259347bb6}",
"id": "{f9ed455f-95cd-48ca-8b0d-4db259347bb6}",
"name": "Diminishing Returns",
"png": "https://cards.scryfall.io/png/front/f/9/f9ed455f-95cd-48ca-8b0d-4db259347bb6.png?1580013930",
"jpg": "https://cards.scryfall.io/small/front/f/9/f9ed455f-95cd-48ca-8b0d-4db259347bb6.jpg?1580013930",
@ -144,7 +144,7 @@
"count": 2
},
{
"scryfall_id": "{78b384d3-3adf-493a-8b89-bfe68fd1c3e2}",
"id": "{78b384d3-3adf-493a-8b89-bfe68fd1c3e2}",
"name": "Vision Charm",
"png": "https://cards.scryfall.io/png/front/7/8/78b384d3-3adf-493a-8b89-bfe68fd1c3e2.png?1562277700",
"jpg": "https://cards.scryfall.io/small/front/7/8/78b384d3-3adf-493a-8b89-bfe68fd1c3e2.jpg?1562277700",
@ -152,7 +152,7 @@
"count": 2
},
{
"scryfall_id": "{8798a4f1-34bb-449d-a8cc-faf8bda8e0ab}",
"id": "{8798a4f1-34bb-449d-a8cc-faf8bda8e0ab}",
"name": "Crystal Spray",
"png": "https://cards.scryfall.io/png/front/8/7/8798a4f1-34bb-449d-a8cc-faf8bda8e0ab.png?1562922403",
"jpg": "https://cards.scryfall.io/small/front/8/7/8798a4f1-34bb-449d-a8cc-faf8bda8e0ab.jpg?1562922403",
@ -160,7 +160,7 @@
"count": 2
},
{
"scryfall_id": "{63eb18e0-c723-4de4-8498-c5362c75b2b4}",
"id": "{63eb18e0-c723-4de4-8498-c5362c75b2b4}",
"name": "Halimar Depths",
"png": "https://cards.scryfall.io/png/front/6/3/63eb18e0-c723-4de4-8498-c5362c75b2b4.png?1726285434",
"jpg": "https://cards.scryfall.io/small/front/6/3/63eb18e0-c723-4de4-8498-c5362c75b2b4.jpg?1726285434",
@ -168,7 +168,7 @@
"count": 2
},
{
"scryfall_id": "{a0f0c20c-184e-4d27-ae8b-933abb6fee0c}",
"id": "{a0f0c20c-184e-4d27-ae8b-933abb6fee0c}",
"name": "Metamorphose",
"png": "https://cards.scryfall.io/png/front/a/0/a0f0c20c-184e-4d27-ae8b-933abb6fee0c.png?1562533013",
"jpg": "https://cards.scryfall.io/small/front/a/0/a0f0c20c-184e-4d27-ae8b-933abb6fee0c.jpg?1562533013",
@ -176,7 +176,7 @@
"count": 2
},
{
"scryfall_id": "{67652446-6d12-4e2a-bb51-ba685f2e79d1}",
"id": "{67652446-6d12-4e2a-bb51-ba685f2e79d1}",
"name": "Mystic Sanctuary",
"png": "https://cards.scryfall.io/png/front/6/7/67652446-6d12-4e2a-bb51-ba685f2e79d1.png?1706241201",
"jpg": "https://cards.scryfall.io/small/front/6/7/67652446-6d12-4e2a-bb51-ba685f2e79d1.jpg?1706241201",
@ -184,7 +184,7 @@
"count": 2
},
{
"scryfall_id": "{5833825f-aeb0-41bb-92a4-13e869765295}",
"id": "{5833825f-aeb0-41bb-92a4-13e869765295}",
"name": "Lonely Sandbar",
"png": "https://cards.scryfall.io/png/front/5/8/5833825f-aeb0-41bb-92a4-13e869765295.png?1708054955",
"jpg": "https://cards.scryfall.io/small/front/5/8/5833825f-aeb0-41bb-92a4-13e869765295.jpg?1708054955",
@ -192,7 +192,7 @@
"count": 2
},
{
"scryfall_id": "{224e8255-7bc6-44a2-86af-14f8446f4f77}",
"id": "{224e8255-7bc6-44a2-86af-14f8446f4f77}",
"name": "Temple of Epiphany",
"png": "https://cards.scryfall.io/png/front/2/2/224e8255-7bc6-44a2-86af-14f8446f4f77.png?1730491248",
"jpg": "https://cards.scryfall.io/small/front/2/2/224e8255-7bc6-44a2-86af-14f8446f4f77.jpg?1730491248",

View File

@ -1,14 +1,16 @@
use rand::seq::SliceRandom;
use rand::{random_bool, rng};
use rocket::serde::{Deserialize, Serialize};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use std::collections::{HashMap, VecDeque};
use url::Url;
use uuid::Uuid;
#[derive(Deserialize, Serialize, Clone)]
#[serde(crate = "rocket::serde")]
#[derive(Deserialize, Serialize, Clone, Debug)]
//#[serde(crate = "rocket::serde")]
struct CardInfo {
#[serde(with = "uuid::serde::braced")]
scryfall_id: Uuid,
id: Uuid,
name: String,
png: Url,
jpg: Url,
@ -16,65 +18,79 @@ struct CardInfo {
count: usize,
}
#[derive(Deserialize, Serialize, Clone, Copy)]
#[serde(crate = "rocket::serde")]
#[derive(Deserialize, Serialize, Clone, Copy, Debug)]
//#[serde(crate = "rocket::serde")]
pub enum Player {
A,
B,
}
#[derive(Deserialize, Serialize, Clone)]
#[serde(crate = "rocket::serde")]
#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
//#[serde(crate = "rocket::serde")]
struct InPlay {
#[serde(with = "uuid::serde::braced")]
id: Uuid,
position_x: u16,
position_y: u16,
owner: Player,
}
#[derive(Serialize, Clone)]
#[serde(crate = "rocket::serde")]
struct Hand {
cards: Vec<Uuid>,
owner: Player,
tapped: bool,
#[serde(with = "uuid::serde::braced")]
play_id: Uuid,
//locked: bool, // prevents untapping
}
#[derive(Serialize, Clone)]
#[serde(crate = "rocket::serde")]
#[derive(Serialize, Clone, Debug)]
//#[serde(crate = "rocket::serde")]
struct Hand {
cards: Vec<Uuid>,
owner: Player,
}
#[derive(Serialize, Clone, Debug)]
//#[serde(crate = "rocket::serde")]
enum Event {
Shuffle(Player),
Draw(Player, usize),
Pass(Player, Player),
ChangeLifeTotal(Player, i32, i32),
FadeFromHand(Player),
PlayFromHand(Player),
Bounce(Player),
Tap(Uuid),
}
#[derive(Serialize, Clone)]
#[serde(crate = "rocket::serde")]
#[derive(Serialize, Clone, Debug)]
//#[serde(crate = "rocket::serde")]
struct CountedEvent {
id: usize,
event: Event,
}
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct GameState {
deck: Vec<Uuid>,
deck: VecDeque<Uuid>,
discard_pile: Vec<Uuid>,
shadow_realm: Vec<Uuid>,
play: Vec<InPlay>,
play: HashMap<Uuid, InPlay>,
hands: Vec<Hand>,
life_totals: Vec<i32>,
turn_player: Player,
//card_map: HashMap<Uuid, CardInfo>,
events: Vec<CountedEvent>,
}
#[serde_as]
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
#[serde(deny_unknown_fields)]
//#[serde(crate = "rocket::serde")]
pub struct OnePlayerGameState {
discard_pile: Vec<Uuid>,
shadow_realm: Vec<Uuid>,
play: Vec<InPlay>,
// I hate my life. Uuid seems to be causing issues and for some
// reason serde just keeps giving an empty object instead of
// serializing properly.
#[serde_as(as = "Vec<(_, _)>")]
play: HashMap<String, InPlay>,
hand: Vec<Uuid>,
life_totals: Vec<i32>,
turn_player: Player,
@ -87,7 +103,12 @@ impl GameState {
OnePlayerGameState {
discard_pile: self.discard_pile.clone(),
shadow_realm: self.shadow_realm.clone(),
play: self.play.clone(),
play: self
.play
.clone()
.into_iter()
.map(|(key, value)| (format!("{}", key.hyphenated()), value))
.collect(),
hand: match player {
Player::A => self.hands[0].cards.clone(),
_ => self.hands[1].cards.clone(),
@ -109,22 +130,22 @@ pub fn new() -> GameState {
let parsed_cards: Vec<CardInfo> = serde_json::from_str(CARD_DATA_JSON).unwrap();
/*let card_map = parsed_cards
.into_iter()
.map(|card| (card.scryfall_id, card))
.map(|card| (card.id, card))
.collect::<HashMap<_, _>>();*/
let starting_player = match random_bool(0.5) {
true => Player::A,
_ => Player::B,
};
let mut deck: Vec<Uuid> = parsed_cards
let mut deck: VecDeque<Uuid> = parsed_cards
.iter()
.flat_map(move |card| vec![card.scryfall_id.clone(); card.count])
.collect();
deck.shuffle(&mut rng());
.flat_map(move |card| vec![card.id.clone(); card.count])
.collect::<VecDeque<Uuid>>();
deck.make_contiguous().shuffle(&mut rng());
GameState {
deck,
discard_pile: vec![],
shadow_realm: vec![],
play: vec![],
play: HashMap::new(),
hands: vec![
Hand {
cards: vec![],
@ -137,7 +158,6 @@ pub fn new() -> GameState {
],
life_totals: vec![20; 2],
turn_player: starting_player,
//card_map,
events: vec![],
}
}
@ -155,7 +175,7 @@ pub fn draw(game_state: &mut GameState, count: usize, player: Player) {
game_state
.deck
.split_off(game_state.deck.len() - count)
.as_mut_slice(),
.make_contiguous(),
);
}
@ -164,7 +184,7 @@ pub fn shuffle(game_state: &mut GameState, player: Player) {
id: game_state.events.len(),
event: Event::Shuffle(player),
});
game_state.deck.shuffle(&mut rng());
game_state.deck.make_contiguous().shuffle(&mut rng());
}
pub fn get_game_one_player(game_state: &GameState, player: Player) -> String {
@ -179,7 +199,7 @@ pub fn pass(game_state: &mut GameState) {
let current_player = &game_state.turn_player;
let new_player = match current_player {
Player::A => Player::B,
_ => Player::A,
Player::B => Player::A,
};
game_state.events.push(CountedEvent {
id: game_state.events.len(),
@ -191,9 +211,8 @@ pub fn pass(game_state: &mut GameState) {
pub fn change_life(game_state: &mut GameState, diff: i32, player: Player) {
let mut player_life = match player {
Player::A => game_state.life_totals[0].clone(),
_ => game_state.life_totals[1].clone(),
Player::B => game_state.life_totals[1].clone(),
};
//let diffed_player = player_life.clone() + diff;
game_state.events.push(CountedEvent {
id: game_state.events.len(),
event: Event::ChangeLifeTotal(player, player_life.clone(), player_life.clone() + diff),
@ -201,6 +220,69 @@ pub fn change_life(game_state: &mut GameState, diff: i32, player: Player) {
player_life += diff;
match player {
Player::A => game_state.life_totals[0] = player_life,
_ => game_state.life_totals[1] = player_life,
Player::B => game_state.life_totals[1] = player_life,
};
}
pub fn fade_from_hand(game_state: &mut GameState, hand_index: usize, player: Player, bottom: bool) {
let player_hand = match player {
Player::A => &mut game_state.hands[0],
Player::B => &mut game_state.hands[1],
};
game_state.events.push(CountedEvent {
id: game_state.events.len(),
event: Event::FadeFromHand(player.clone()),
});
let card = player_hand.cards.remove(hand_index);
if bottom {
game_state.deck.push_front(card);
} else {
game_state.deck.push_back(card);
}
}
pub fn play_from_hand(game_state: &mut GameState, hand_index: usize, player: Player) {
let player_hand = match player {
Player::A => &mut game_state.hands[0],
Player::B => &mut game_state.hands[1],
};
game_state.events.push(CountedEvent {
id: game_state.events.len(),
event: Event::PlayFromHand(player.clone()),
});
let card = player_hand.cards.remove(hand_index);
let play_uuid = Uuid::new_v4();
game_state.play.insert(
play_uuid,
InPlay {
id: card,
position_x: 5,
position_y: 50,
owner: player,
tapped: false,
play_id: play_uuid,
},
);
}
pub fn bounce(game_state: &mut GameState, play_id: Uuid) {
let played_card = game_state.play.remove(&play_id).unwrap();
let player_hand = match played_card.owner {
Player::A => &mut game_state.hands[0],
Player::B => &mut game_state.hands[1],
};
game_state.events.push(CountedEvent {
id: game_state.events.len(),
event: Event::Bounce(played_card.owner),
});
player_hand.cards.push(played_card.id);
}
pub fn tap(game_state: &mut GameState, play_id: Uuid) {
game_state.events.push(CountedEvent {
id: game_state.events.len(),
event: Event::Tap(play_id.clone()),
});
let played_card = game_state.play.get_mut(&play_id).unwrap();
played_card.tapped = !played_card.tapped;
}

View File

@ -8,12 +8,12 @@ use rocket::response::status::BadRequest;
use rocket::State;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use uuid::Uuid;
use uuid::{uuid, Uuid};
#[get("/")]
fn index(player_uuids: &State<PlayerUuids>) -> String {
format!(
"a: localhost:8000/{}/\nb: localhost:8000/{}/",
"localhost:8000/{}/\nlocalhost:8000/{}/",
player_uuids.a, player_uuids.b
)
}
@ -57,6 +57,58 @@ fn pass(
Ok(format!("{}", game::get_events(&game_state)))
}
#[get("/<uuid>/bounce/<play_id>")]
fn bounce(
uuid: Uuid,
play_id: Uuid,
game_state_arc: &State<ArcMutexGameState>,
player_uuids: &State<PlayerUuids>,
) -> Result<String, BadRequest<String>> {
match player_uuids.map.get(&uuid) {
Some(_) => (),
None => return Err(BadRequest(format!("Invalid player {}.", uuid))),
};
let game_state_mutex = Arc::clone(&game_state_arc.state);
let mut game_state = game_state_mutex.lock().unwrap();
game::bounce(&mut game_state, play_id);
Ok(format!("{}", game::get_events(&game_state)))
}
#[get("/<uuid>/tap/<play_id>")]
fn tap(
uuid: Uuid,
play_id: Uuid,
game_state_arc: &State<ArcMutexGameState>,
player_uuids: &State<PlayerUuids>,
) -> Result<String, BadRequest<String>> {
match player_uuids.map.get(&uuid) {
Some(_) => (),
None => return Err(BadRequest(format!("Invalid player {}.", uuid))),
};
let game_state_mutex = Arc::clone(&game_state_arc.state);
let mut game_state = game_state_mutex.lock().unwrap();
game::tap(&mut game_state, play_id);
Ok(format!("{}", game::get_events(&game_state)))
}
#[get("/<uuid>/play/<index>")]
fn play(
uuid: Uuid,
index: usize,
game_state_arc: &State<ArcMutexGameState>,
player_uuids: &State<PlayerUuids>,
) -> Result<String, BadRequest<String>> {
let player = match player_uuids.map.get(&uuid) {
Some(player) => player,
None => return Err(BadRequest(format!("Invalid player {}.", uuid))),
};
let game_state_mutex = Arc::clone(&game_state_arc.state);
let mut game_state = game_state_mutex.lock().unwrap();
game::play_from_hand(&mut game_state, index, player.clone());
//println!("{:#?}", game_state);
Ok(format!("{}", game::get_events(&game_state)))
}
#[get("/<uuid>/get_state")]
fn get_state(
uuid: Uuid,
@ -95,6 +147,46 @@ fn draw(
))
}
#[get("/<uuid>/fade/<index>")]
fn fade(
uuid: Uuid,
index: usize,
game_state_arc: &State<ArcMutexGameState>,
player_uuids: &State<PlayerUuids>,
) -> Result<String, BadRequest<String>> {
let game_state_mutex = Arc::clone(&game_state_arc.state);
let mut game_state = game_state_mutex.lock().unwrap();
let player = match player_uuids.map.get(&uuid) {
Some(player) => player,
None => return Err(BadRequest(format!("Invalid player {}.", uuid))),
};
game::fade_from_hand(&mut game_state, index, player.clone(), false);
Ok(format!(
"{}",
game::get_game_one_player(&game_state, player.clone())
))
}
#[get("/<uuid>/fade-bottom/<index>")]
fn fade_bottom(
uuid: Uuid,
index: usize,
game_state_arc: &State<ArcMutexGameState>,
player_uuids: &State<PlayerUuids>,
) -> Result<String, BadRequest<String>> {
let game_state_mutex = Arc::clone(&game_state_arc.state);
let mut game_state = game_state_mutex.lock().unwrap();
let player = match player_uuids.map.get(&uuid) {
Some(player) => player,
None => return Err(BadRequest(format!("Invalid player {}.", uuid))),
};
game::fade_from_hand(&mut game_state, index, player.clone(), true);
Ok(format!(
"{}",
game::get_game_one_player(&game_state, player.clone())
))
}
#[get("/<uuid>/life/<count>")]
fn life(
uuid: Uuid,
@ -127,15 +219,26 @@ struct PlayerUuids {
#[launch]
fn rocket() -> _ {
let game_state = game::new();
let mut game_state = game::new();
game::draw(&mut game_state, 7, Player::A);
game::draw(&mut game_state, 7, Player::B);
let game_state_arc = Arc::new(Mutex::new(game_state));
#[cfg(debug_assertions)]
let a_uuid: Uuid = uuid!("9b0a2a95-72a9-4e03-930e-a9583d2a2a5a");
#[cfg(not(debug_assertions))]
let a_uuid: Uuid = Uuid::new_v4();
#[cfg(debug_assertions)]
let b_uuid: Uuid = uuid!("2efc0332-975d-4f71-9dd0-ffc9213098a5");
#[cfg(not(debug_assertions))]
let b_uuid: Uuid = Uuid::new_v4();
let uuid_map = HashMap::from([(a_uuid, Player::A), (b_uuid, Player::B)]);
println!("A: {}", a_uuid);
println!("B: {}", b_uuid);
rocket::build()
.mount("/", FileServer::from(relative!("static/assets")).rank(0))
.mount(
format!("/{}", a_uuid.hyphenated()),
FileServer::from(relative!("static")),
@ -146,7 +249,20 @@ fn rocket() -> _ {
)
.mount(
"/",
routes![index, get_events, get_state, draw, shuffle, pass, life,],
routes![
index,
get_events,
get_state,
draw,
shuffle,
pass,
life,
fade,
fade_bottom,
play,
bounce,
tap,
],
)
.manage(ArcMutexGameState {
state: game_state_arc,