actions
This commit is contained in:
parent
2f096e0bae
commit
5f1cba363b
22 changed files with 1290 additions and 388 deletions
66
server/Cargo.lock
generated
66
server/Cargo.lock
generated
|
|
@ -2,20 +2,50 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "game"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rand",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.139"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.49"
|
||||
|
|
@ -34,6 +64,36 @@ dependencies = [
|
|||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.12"
|
||||
|
|
@ -87,3 +147,9 @@ name = "unicode-ident"
|
|||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||
|
|
|
|||
|
|
@ -6,5 +6,6 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.5"
|
||||
serde = { version = "1.0.152", features = ["derive"] }
|
||||
serde_json = "1.0.91"
|
||||
|
|
|
|||
182
server/src/action.rs
Normal file
182
server/src/action.rs
Normal file
|
|
@ -0,0 +1,182 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
city::{
|
||||
City, CityData,
|
||||
CityPiece::{self, *},
|
||||
CityPieceData, BUILDING_COST,
|
||||
},
|
||||
content::custom_actions,
|
||||
player::Player,
|
||||
resource_pile::ResourcePile,
|
||||
};
|
||||
|
||||
use PlayingAction::*;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub enum PlayingAction {
|
||||
Advance {
|
||||
technology: String,
|
||||
payment: ResourcePile,
|
||||
},
|
||||
Build {
|
||||
city: CityData,
|
||||
city_piece: CityPieceData,
|
||||
payment: ResourcePile,
|
||||
},
|
||||
IncreaseHappiness {
|
||||
cities: Vec<(CityData, u32)>,
|
||||
},
|
||||
InfluenceCulture {
|
||||
success: bool,
|
||||
starting_city: Box<CityData>,
|
||||
target_city: CityData,
|
||||
city_piece: CityPieceData,
|
||||
range_boost: u32,
|
||||
result_boost: u32,
|
||||
},
|
||||
Custom {
|
||||
name: String,
|
||||
contents: String,
|
||||
},
|
||||
EndTurn,
|
||||
}
|
||||
|
||||
impl PlayingAction {
|
||||
pub fn execute(self, player: &mut Player, user_specification: Option<String>) {
|
||||
let player_name = player.name();
|
||||
match self {
|
||||
Advance {
|
||||
technology,
|
||||
payment,
|
||||
} => {
|
||||
if !player.can_research_technology(&technology)
|
||||
|| payment.food + payment.ideas + payment.gold as u32 != 2
|
||||
{
|
||||
panic!("Illegal action");
|
||||
}
|
||||
player.loose_resources(payment);
|
||||
player.research_technology(&technology);
|
||||
}
|
||||
Build {
|
||||
city,
|
||||
city_piece,
|
||||
payment,
|
||||
} => {
|
||||
let city = City::from_data(city);
|
||||
let city_piece = CityPiece::from_data(&city_piece);
|
||||
let mut cost = BUILDING_COST;
|
||||
player
|
||||
.events()
|
||||
.city_size_increase_cost
|
||||
.trigger(&mut cost, &city, &city_piece);
|
||||
if city.player != player_name
|
||||
|| !city.can_increase_size(&city_piece, player)
|
||||
|| !payment.can_afford(&cost)
|
||||
{
|
||||
panic!("Illegal action");
|
||||
}
|
||||
if matches!(city_piece, Temple) {
|
||||
let building_bonus = serde_json::from_str(
|
||||
&user_specification.expect("user should have specified the building bonus"),
|
||||
)
|
||||
.expect("use should have specified the building bonus");
|
||||
if building_bonus != ResourcePile::mood_tokens(1)
|
||||
&& building_bonus != ResourcePile::culture_tokens(1)
|
||||
{
|
||||
panic!("Illegal action");
|
||||
}
|
||||
player.gain_resources(building_bonus);
|
||||
}
|
||||
player.loose_resources(payment);
|
||||
let mut city = player.cities.remove(
|
||||
player
|
||||
.cities
|
||||
.iter()
|
||||
.position(|player_city| player_city.position == city.position)
|
||||
.expect("city should exist"),
|
||||
);
|
||||
city.increase_size(city_piece, player);
|
||||
player.cities.push(city);
|
||||
}
|
||||
IncreaseHappiness { cities } => {
|
||||
for (city, steps) in cities {
|
||||
let city = City::from_data(city);
|
||||
let cost = ResourcePile::mood_tokens(city.size()) * steps;
|
||||
if city.player != player_name || !player.resources().can_afford(&cost) {
|
||||
panic!("Illegal action");
|
||||
}
|
||||
player.loose_resources(cost);
|
||||
for player_city in player.cities.iter_mut() {
|
||||
if player_city.position == city.position {
|
||||
for _ in 0..steps {
|
||||
player_city.increase_mood_state();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
InfluenceCulture {
|
||||
success,
|
||||
starting_city,
|
||||
target_city,
|
||||
city_piece,
|
||||
range_boost,
|
||||
result_boost,
|
||||
} => {
|
||||
let starting_city = City::from_data(*starting_city);
|
||||
let target_city = City::from_data(target_city);
|
||||
let city_piece = CityPiece::from_data(&city_piece);
|
||||
let cost = ResourcePile::culture_tokens(range_boost + result_boost);
|
||||
if matches!(city_piece, Obelisk)
|
||||
|| matches!(city_piece, Wonder(_))
|
||||
|| starting_city.position.distance(&target_city.position)
|
||||
> starting_city.size() + range_boost
|
||||
|| starting_city.player != player_name
|
||||
|| !player.resources().can_afford(&cost)
|
||||
{
|
||||
panic!("Illegal action");
|
||||
}
|
||||
if !success {
|
||||
return;
|
||||
}
|
||||
player.loose_resources(cost);
|
||||
todo!()
|
||||
}
|
||||
Custom { name, contents } => {
|
||||
custom_actions::get_custom_action(&name, &contents).execute(player)
|
||||
}
|
||||
EndTurn => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn action_type(&self) -> ActionType {
|
||||
match self {
|
||||
Custom { name, contents } => {
|
||||
custom_actions::get_custom_action(name, contents).action_type()
|
||||
}
|
||||
_ => ActionType::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait CustomAction {
|
||||
fn execute(&self, player: &mut Player);
|
||||
fn action_type(&self) -> ActionType;
|
||||
fn name(&self) -> String;
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ActionType {
|
||||
pub free: bool,
|
||||
pub once_per_turn: bool,
|
||||
}
|
||||
|
||||
impl ActionType {
|
||||
pub fn new(free: bool, once_per_turn: bool) -> Self {
|
||||
Self {
|
||||
free,
|
||||
once_per_turn,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,14 +1,24 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::hexagon::HexagonPosition;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub enum Unit {
|
||||
pub struct Unit {
|
||||
pub player: String,
|
||||
pub unit_type: UnitType,
|
||||
pub transporter: Option<HexagonPosition>,
|
||||
pub can_attack: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub enum UnitType {
|
||||
Ship,
|
||||
ArmyUnit(ArmyUnit),
|
||||
ArmyUnit(ArmyUnitType),
|
||||
Settler,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub enum ArmyUnit {
|
||||
pub enum ArmyUnitType {
|
||||
Infantry,
|
||||
Cavalry,
|
||||
Elephant,
|
||||
|
|
|
|||
|
|
@ -1,58 +1,60 @@
|
|||
use std::fmt::Display;
|
||||
|
||||
use crate::{
|
||||
content::wonders,
|
||||
player::Player,
|
||||
resource_pile::ResourcePile,
|
||||
wonder::{Wonder, WONDER_VICTORY_POINTS},
|
||||
content::wonders, hexagon::HexagonPosition, player::Player, resource_pile::ResourcePile,
|
||||
wonder::Wonder,
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use Building::*;
|
||||
use MoodState::*;
|
||||
|
||||
const MAX_CITY_SIZE: usize = 4;
|
||||
const CITY_PIECE_VICTORY_POINTS: f32 = 1.0;
|
||||
const BUILDING_COST: ResourcePile = ResourcePile {
|
||||
wood: 1,
|
||||
stone: 1,
|
||||
gold: 0,
|
||||
const MAX_CITY_SIZE: u32 = 4;
|
||||
pub const BUILDING_COST: ResourcePile = ResourcePile {
|
||||
food: 1,
|
||||
wood: 1,
|
||||
ore: 1,
|
||||
ideas: 0,
|
||||
gold: 0,
|
||||
mood_tokens: 0,
|
||||
culture_tokens: 0,
|
||||
};
|
||||
|
||||
pub struct City {
|
||||
pub buildings: Buildings,
|
||||
pub city_pieces: CityPieces,
|
||||
pub mood_state: MoodState,
|
||||
pub is_activated: bool,
|
||||
pub player: String,
|
||||
pub position: HexagonPosition,
|
||||
}
|
||||
|
||||
impl City {
|
||||
pub fn from_data(data: CityData) -> Self {
|
||||
Self {
|
||||
buildings: Buildings::from_data(data.buildings),
|
||||
city_pieces: CityPieces::from_data(data.city_pieces),
|
||||
mood_state: data.mood_state,
|
||||
is_activated: data.is_activated,
|
||||
player: data.player,
|
||||
position: data.position,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_data(self) -> CityData {
|
||||
pub fn data(self) -> CityData {
|
||||
CityData::new(
|
||||
self.buildings.to_data(),
|
||||
self.city_pieces.data(),
|
||||
self.mood_state,
|
||||
self.is_activated,
|
||||
self.player,
|
||||
self.position,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new(player: String) -> Self {
|
||||
pub fn new(player: String, position: HexagonPosition) -> Self {
|
||||
Self {
|
||||
buildings: Buildings::default(),
|
||||
city_pieces: CityPieces::default(),
|
||||
mood_state: Neutral,
|
||||
is_activated: false,
|
||||
player,
|
||||
position,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -63,64 +65,97 @@ impl City {
|
|||
self.is_activated = true;
|
||||
}
|
||||
|
||||
pub fn can_increase_size(&self, building: Building, player: &mut Player) -> bool {
|
||||
if self.buildings.amount() == MAX_CITY_SIZE {
|
||||
pub fn can_increase_size(&self, city_piece: &CityPiece, player: &mut Player) -> bool {
|
||||
if self.city_pieces.amount() == MAX_CITY_SIZE {
|
||||
return false;
|
||||
}
|
||||
if self.buildings.can_add_building(&building) {
|
||||
if matches!(self.mood_state, Angry) {
|
||||
return false;
|
||||
}
|
||||
let mut cost = match &building {
|
||||
Wonder(wonder) => wonder.cost.clone(),
|
||||
if self.city_pieces.can_add_city_piece(city_piece) {
|
||||
return false;
|
||||
}
|
||||
let mut cost = match &city_piece {
|
||||
CityPiece::Wonder(wonder) => wonder.cost.clone(),
|
||||
_ => BUILDING_COST,
|
||||
};
|
||||
player
|
||||
.events()
|
||||
.city_size_increase_cost
|
||||
.trigger(&mut cost, self, &building);
|
||||
.trigger(&mut cost, self, city_piece);
|
||||
player.resources().can_afford(&cost)
|
||||
}
|
||||
|
||||
pub fn increase_size(&mut self, building: Building, player: &mut Player) {
|
||||
pub fn increase_size(&mut self, city_piece: CityPiece, player: &mut Player) {
|
||||
self.activate();
|
||||
let mut events = player.take_events();
|
||||
events.city_size_increase.trigger(player, self, &building);
|
||||
events.city_size_increase.trigger(player, self, &city_piece);
|
||||
player.set_events(events);
|
||||
let victory_points = match &building {
|
||||
Wonder(_) => WONDER_VICTORY_POINTS,
|
||||
_ => CITY_PIECE_VICTORY_POINTS,
|
||||
};
|
||||
player.gain_victory_points(victory_points);
|
||||
match &building {
|
||||
Academy => player.gain_resources(ResourcePile::ideas(2)),
|
||||
Wonder(wonder) => (wonder.player_initializer)(player),
|
||||
match &city_piece {
|
||||
CityPiece::Academy => player.gain_resources(ResourcePile::ideas(2)),
|
||||
CityPiece::Wonder(wonder) => {
|
||||
(wonder.player_initializer)(player);
|
||||
player.wonders.push(wonder.name.clone());
|
||||
player.wonders_build += 1;
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
self.buildings.add_building(building, self.player.clone());
|
||||
self.city_pieces
|
||||
.add_city_piece(city_piece, self.player.clone());
|
||||
}
|
||||
|
||||
pub fn conquer(&mut self, new_player: &mut Player, old_player: &mut Player) {
|
||||
self.player = new_player.name.clone();
|
||||
let new_player_name = new_player.name();
|
||||
self.player = new_player_name.clone();
|
||||
self.mood_state = Angry;
|
||||
if let Some(wonder) = &self.buildings.wonder {
|
||||
for wonder in self.city_pieces.wonders.iter() {
|
||||
(wonder.player_deinitializer)(old_player);
|
||||
(wonder.player_initializer)(new_player);
|
||||
new_player.gain_victory_points(WONDER_VICTORY_POINTS / 2.0 - 1.0);
|
||||
old_player.loose_victory_points(WONDER_VICTORY_POINTS / 2.0 - 1.0);
|
||||
let wonder = old_player.wonders.remove(
|
||||
old_player
|
||||
.wonders
|
||||
.iter()
|
||||
.position(|player_wonder| player_wonder == &wonder.name)
|
||||
.expect("player should have conquered wonder"),
|
||||
);
|
||||
new_player.wonders.push(wonder);
|
||||
}
|
||||
new_player.gain_victory_points(self.size() as f32);
|
||||
old_player.loose_victory_points(self.size() as f32);
|
||||
if self.buildings.obelisk.is_some() {
|
||||
new_player.loose_victory_points(1.0);
|
||||
old_player.gain_victory_points(1.0);
|
||||
if let Some(player) = &self.city_pieces.obelisk {
|
||||
if player == &old_player.name() {
|
||||
old_player.influenced_buildings += 1;
|
||||
}
|
||||
}
|
||||
self.buildings.change_player(new_player.name.clone());
|
||||
let mut previously_influenced_building = 0;
|
||||
if self.city_pieces.academy == Some(new_player_name.clone()) {
|
||||
previously_influenced_building += 1;
|
||||
}
|
||||
if self.city_pieces.market == Some(new_player_name.clone()) {
|
||||
previously_influenced_building += 1;
|
||||
}
|
||||
if self.city_pieces.obelisk == Some(new_player_name.clone()) {
|
||||
previously_influenced_building += 1;
|
||||
}
|
||||
if self.city_pieces.observatory == Some(new_player_name.clone()) {
|
||||
previously_influenced_building += 1;
|
||||
}
|
||||
if self.city_pieces.fortress == Some(new_player_name.clone()) {
|
||||
previously_influenced_building += 1;
|
||||
}
|
||||
if self.city_pieces.port == Some(new_player_name.clone()) {
|
||||
previously_influenced_building += 1;
|
||||
}
|
||||
if self.city_pieces.temple == Some(new_player_name.clone()) {
|
||||
previously_influenced_building += 1;
|
||||
}
|
||||
new_player.influenced_buildings -= previously_influenced_building;
|
||||
self.city_pieces.change_player(new_player_name);
|
||||
}
|
||||
|
||||
pub fn size(&self) -> usize {
|
||||
self.buildings.amount() + 1
|
||||
pub fn size(&self) -> u32 {
|
||||
self.city_pieces.amount() + 1
|
||||
}
|
||||
|
||||
pub fn mood_modified_size(&self) -> usize {
|
||||
pub fn mood_modified_size(&self) -> u32 {
|
||||
match self.mood_state {
|
||||
Happy => self.size() + 1,
|
||||
Neutral => self.size(),
|
||||
|
|
@ -141,131 +176,223 @@ impl City {
|
|||
Neutral | Angry => Angry,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn buildings(&self) -> u32 {
|
||||
let mut value = 0;
|
||||
if self.city_pieces.academy == Some(self.player.clone()) {
|
||||
value += 1;
|
||||
}
|
||||
if self.city_pieces.market == Some(self.player.clone()) {
|
||||
value += 1;
|
||||
}
|
||||
if self.city_pieces.obelisk == Some(self.player.clone()) {
|
||||
value += 1;
|
||||
}
|
||||
if self.city_pieces.observatory == Some(self.player.clone()) {
|
||||
value += 1;
|
||||
}
|
||||
if self.city_pieces.fortress == Some(self.player.clone()) {
|
||||
value += 1;
|
||||
}
|
||||
if self.city_pieces.port == Some(self.player.clone()) {
|
||||
value += 1;
|
||||
}
|
||||
if self.city_pieces.temple == Some(self.player.clone()) {
|
||||
value += 1;
|
||||
}
|
||||
value
|
||||
}
|
||||
|
||||
pub fn influence_culture(&mut self, player: &mut Player, building: CityPiece) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CityData {
|
||||
buildings: BuildingsData,
|
||||
city_pieces: CityPiecesData,
|
||||
mood_state: MoodState,
|
||||
is_activated: bool,
|
||||
player: String,
|
||||
position: HexagonPosition,
|
||||
}
|
||||
|
||||
impl CityData {
|
||||
pub fn new(
|
||||
buildings: BuildingsData,
|
||||
city_pieces: CityPiecesData,
|
||||
mood_state: MoodState,
|
||||
is_activated: bool,
|
||||
player: String,
|
||||
position: HexagonPosition,
|
||||
) -> Self {
|
||||
Self {
|
||||
buildings,
|
||||
city_pieces,
|
||||
mood_state,
|
||||
is_activated,
|
||||
player,
|
||||
position,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Building {
|
||||
pub enum CityPiece {
|
||||
Academy,
|
||||
Market,
|
||||
Obelisk,
|
||||
Apothecary,
|
||||
Observatory,
|
||||
Fortress,
|
||||
Port,
|
||||
Temple,
|
||||
Wonder(Wonder),
|
||||
}
|
||||
|
||||
impl CityPiece {
|
||||
pub fn json(&self) -> String {
|
||||
serde_json::to_string(&self.to_data()).expect("city piece data should be valid json")
|
||||
}
|
||||
|
||||
pub fn from_json(json: &str) -> Self {
|
||||
Self::from_data(
|
||||
serde_json::from_str(json)
|
||||
.as_ref()
|
||||
.expect("API call should receive valid city piece data json"),
|
||||
)
|
||||
}
|
||||
|
||||
fn to_data(&self) -> CityPieceData {
|
||||
match self {
|
||||
Self::Academy => CityPieceData::Academy,
|
||||
Self::Market => CityPieceData::Market,
|
||||
Self::Obelisk => CityPieceData::Obelisk,
|
||||
Self::Observatory => CityPieceData::Observatory,
|
||||
Self::Fortress => CityPieceData::Fortress,
|
||||
Self::Port => CityPieceData::Port,
|
||||
Self::Temple => CityPieceData::Temple,
|
||||
Self::Wonder(wonder) => CityPieceData::Wonder(wonder.name.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_data(data: &CityPieceData) -> Self {
|
||||
match data {
|
||||
CityPieceData::Academy => Self::Academy,
|
||||
CityPieceData::Market => Self::Market,
|
||||
CityPieceData::Obelisk => Self::Obelisk,
|
||||
CityPieceData::Observatory => Self::Observatory,
|
||||
CityPieceData::Fortress => Self::Fortress,
|
||||
CityPieceData::Port => Self::Port,
|
||||
CityPieceData::Temple => Self::Temple,
|
||||
CityPieceData::Wonder(name) => Self::Wonder(
|
||||
wonders::get_wonder_by_name(name)
|
||||
.expect("city piece data should have a valid wonder name"),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub enum CityPieceData {
|
||||
Academy,
|
||||
Market,
|
||||
Obelisk,
|
||||
Observatory,
|
||||
Fortress,
|
||||
Port,
|
||||
Temple,
|
||||
Wonder(String),
|
||||
}
|
||||
|
||||
impl Display for CityPieceData {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
match self {
|
||||
Self::Academy => "a academy",
|
||||
Self::Market => "a market",
|
||||
Self::Obelisk => "an obelisk",
|
||||
Self::Observatory => "an observatory",
|
||||
Self::Fortress => "a fortress",
|
||||
Self::Port => "a port",
|
||||
Self::Temple => "a temple",
|
||||
Self::Wonder(name) => return write!(f, "the wonder \"{name}\""),
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Buildings {
|
||||
pub struct CityPieces {
|
||||
pub academy: Option<String>,
|
||||
pub market: Option<String>,
|
||||
pub obelisk: Option<String>,
|
||||
pub apothecary: Option<String>,
|
||||
pub observatory: Option<String>,
|
||||
pub fortress: Option<String>,
|
||||
pub port: Option<String>,
|
||||
pub temple: Option<String>,
|
||||
pub wonder: Option<Wonder>,
|
||||
pub wonders: Vec<Wonder>,
|
||||
}
|
||||
|
||||
impl Buildings {
|
||||
fn from_data(data: BuildingsData) -> Self {
|
||||
Self::new(
|
||||
data.academy,
|
||||
data.market,
|
||||
data.obelisk,
|
||||
data.apothecary,
|
||||
data.fortress,
|
||||
data.port,
|
||||
data.temple,
|
||||
data.wonder.map(wonders::get_wonder_by_name),
|
||||
)
|
||||
}
|
||||
|
||||
fn to_data(self) -> BuildingsData {
|
||||
BuildingsData::new(
|
||||
self.academy,
|
||||
self.market,
|
||||
self.obelisk,
|
||||
self.apothecary,
|
||||
self.fortress,
|
||||
self.port,
|
||||
self.temple,
|
||||
self.wonder.map(|wonder| wonder.name),
|
||||
)
|
||||
}
|
||||
|
||||
fn new(
|
||||
academy: Option<String>,
|
||||
market: Option<String>,
|
||||
obelisk: Option<String>,
|
||||
apothecary: Option<String>,
|
||||
fortress: Option<String>,
|
||||
port: Option<String>,
|
||||
temple: Option<String>,
|
||||
wonder: Option<Wonder>,
|
||||
) -> Self {
|
||||
impl CityPieces {
|
||||
fn from_data(data: CityPiecesData) -> Self {
|
||||
Self {
|
||||
academy,
|
||||
market,
|
||||
obelisk,
|
||||
apothecary,
|
||||
fortress,
|
||||
port,
|
||||
temple,
|
||||
wonder,
|
||||
academy: data.academy,
|
||||
market: data.market,
|
||||
obelisk: data.obelisk,
|
||||
observatory: data.observatory,
|
||||
fortress: data.fortress,
|
||||
port: data.port,
|
||||
temple: data.temple,
|
||||
wonders: data
|
||||
.wonders
|
||||
.iter()
|
||||
.map(|wonder| {
|
||||
wonders::get_wonder_by_name(&wonder)
|
||||
.expect("city piece data should contain a valid wonder")
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn can_add_building(&self, building: &Building) -> bool {
|
||||
match building {
|
||||
Academy => self.academy.is_none(),
|
||||
Market => self.market.is_none(),
|
||||
Obelisk => self.obelisk.is_none(),
|
||||
Apothecary => self.apothecary.is_none(),
|
||||
Fortress => self.fortress.is_none(),
|
||||
Port => self.port.is_none(),
|
||||
Temple => self.temple.is_none(),
|
||||
Wonder(_) => self.wonder.is_none(),
|
||||
fn data(self) -> CityPiecesData {
|
||||
CityPiecesData {
|
||||
academy: self.academy,
|
||||
market: self.market,
|
||||
obelisk: self.obelisk,
|
||||
observatory: self.observatory,
|
||||
fortress: self.fortress,
|
||||
port: self.port,
|
||||
temple: self.temple,
|
||||
wonders: self.wonders.into_iter().map(|wonder| wonder.name).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_building(&mut self, building: Building, player: String) {
|
||||
match building {
|
||||
Academy => self.academy = Some(player),
|
||||
Market => self.market = Some(player),
|
||||
Obelisk => self.obelisk = Some(player),
|
||||
Apothecary => self.apothecary = Some(player),
|
||||
Fortress => self.fortress = Some(player),
|
||||
Port => self.port = Some(player),
|
||||
Temple => self.temple = Some(player),
|
||||
Wonder(wonder) => self.wonder = Some(wonder),
|
||||
fn can_add_city_piece(&self, city_piece: &CityPiece) -> bool {
|
||||
match city_piece {
|
||||
CityPiece::Academy => self.academy.is_none(),
|
||||
CityPiece::Market => self.market.is_none(),
|
||||
CityPiece::Obelisk => self.obelisk.is_none(),
|
||||
CityPiece::Observatory => self.observatory.is_none(),
|
||||
CityPiece::Fortress => self.fortress.is_none(),
|
||||
CityPiece::Port => self.port.is_none(),
|
||||
CityPiece::Temple => self.temple.is_none(),
|
||||
CityPiece::Wonder(_) => true,
|
||||
}
|
||||
}
|
||||
|
||||
fn amount(&self) -> usize {
|
||||
fn add_city_piece(&mut self, city_piece: CityPiece, player: String) {
|
||||
match city_piece {
|
||||
CityPiece::Academy => self.academy = Some(player),
|
||||
CityPiece::Market => self.market = Some(player),
|
||||
CityPiece::Obelisk => self.obelisk = Some(player),
|
||||
CityPiece::Observatory => self.observatory = Some(player),
|
||||
CityPiece::Fortress => self.fortress = Some(player),
|
||||
CityPiece::Port => self.port = Some(player),
|
||||
CityPiece::Temple => self.temple = Some(player),
|
||||
CityPiece::Wonder(wonder) => self.wonders.push(wonder),
|
||||
}
|
||||
}
|
||||
|
||||
fn amount(&self) -> u32 {
|
||||
let mut amount = 0;
|
||||
if self.academy.is_some() {
|
||||
amount += 1;
|
||||
|
|
@ -276,7 +403,7 @@ impl Buildings {
|
|||
if self.obelisk.is_some() {
|
||||
amount += 1;
|
||||
}
|
||||
if self.apothecary.is_some() {
|
||||
if self.observatory.is_some() {
|
||||
amount += 1;
|
||||
}
|
||||
if self.fortress.is_some() {
|
||||
|
|
@ -288,9 +415,7 @@ impl Buildings {
|
|||
if self.temple.is_some() {
|
||||
amount += 1;
|
||||
}
|
||||
if self.wonder.is_some() {
|
||||
amount += 1;
|
||||
}
|
||||
amount += self.wonders.len() as u32;
|
||||
amount
|
||||
}
|
||||
|
||||
|
|
@ -301,8 +426,8 @@ impl Buildings {
|
|||
if let Some(market) = self.market.as_mut() {
|
||||
*market = new_player.clone();
|
||||
}
|
||||
if let Some(apothecary) = self.apothecary.as_mut() {
|
||||
*apothecary = new_player.clone();
|
||||
if let Some(observatory) = self.observatory.as_mut() {
|
||||
*observatory = new_player.clone();
|
||||
}
|
||||
if let Some(fortress) = self.fortress.as_mut() {
|
||||
*fortress = new_player.clone();
|
||||
|
|
@ -317,39 +442,15 @@ impl Buildings {
|
|||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct BuildingsData {
|
||||
pub struct CityPiecesData {
|
||||
academy: Option<String>,
|
||||
market: Option<String>,
|
||||
obelisk: Option<String>,
|
||||
apothecary: Option<String>,
|
||||
observatory: Option<String>,
|
||||
fortress: Option<String>,
|
||||
port: Option<String>,
|
||||
temple: Option<String>,
|
||||
wonder: Option<String>,
|
||||
}
|
||||
|
||||
impl BuildingsData {
|
||||
pub fn new(
|
||||
academy: Option<String>,
|
||||
market: Option<String>,
|
||||
obelisk: Option<String>,
|
||||
apothecary: Option<String>,
|
||||
fortress: Option<String>,
|
||||
port: Option<String>,
|
||||
temple: Option<String>,
|
||||
wonder: Option<String>,
|
||||
) -> Self {
|
||||
Self {
|
||||
academy,
|
||||
market,
|
||||
obelisk,
|
||||
apothecary,
|
||||
fortress,
|
||||
port,
|
||||
temple,
|
||||
wonder,
|
||||
}
|
||||
}
|
||||
wonders: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
use crate::{hexagon::Tile, leader::Leader, special_technology::SpecialTechnology};
|
||||
use crate::{leader::Leader, special_technology::SpecialTechnology};
|
||||
|
||||
pub struct Civilization {
|
||||
pub name: String,
|
||||
pub special_technologies: Vec<SpecialTechnology>,
|
||||
pub leaders: Vec<Leader>,
|
||||
pub special_tile: Option<Tile>,
|
||||
}
|
||||
|
||||
impl Civilization {
|
||||
|
|
@ -12,13 +11,11 @@ impl Civilization {
|
|||
name: &str,
|
||||
special_technologies: Vec<SpecialTechnology>,
|
||||
leaders: Vec<Leader>,
|
||||
special_tile: Option<Tile>,
|
||||
) -> Self {
|
||||
Self {
|
||||
name: name.to_string(),
|
||||
special_technologies,
|
||||
leaders,
|
||||
special_tile,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
pub mod civilizations;
|
||||
pub mod custom_actions;
|
||||
pub mod technologies;
|
||||
pub mod wonders;
|
||||
|
|
|
|||
|
|
@ -1,13 +1,18 @@
|
|||
use crate::{civilization::Civilization, leader::Leader};
|
||||
|
||||
pub fn get_civilizations() -> Vec<Civilization> {
|
||||
todo!()
|
||||
vec![]
|
||||
}
|
||||
|
||||
pub fn get_civilization_by_name(name: String) -> Civilization {
|
||||
todo!()
|
||||
pub fn get_civilization_by_name(name: &str) -> Option<Civilization> {
|
||||
get_civilizations()
|
||||
.into_iter()
|
||||
.find(|civilization| civilization.name == name)
|
||||
}
|
||||
|
||||
pub fn get_leader_by_name(name: String, civilization: String) -> Leader {
|
||||
todo!()
|
||||
pub fn get_leader_by_name(name: &str, civilization: &str) -> Option<Leader> {
|
||||
get_civilization_by_name(civilization)?
|
||||
.leaders
|
||||
.into_iter()
|
||||
.find(|leader| leader.name == name)
|
||||
}
|
||||
|
|
|
|||
5
server/src/content/custom_actions.rs
Normal file
5
server/src/content/custom_actions.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
use crate::action::CustomAction;
|
||||
|
||||
pub fn get_custom_action(name: &str, contents: &str) -> Box<dyn CustomAction> {
|
||||
panic!("Invalid name");
|
||||
}
|
||||
|
|
@ -1,9 +1,11 @@
|
|||
use crate::technology::Technology;
|
||||
|
||||
pub fn get_technologies() -> Vec<Technology> {
|
||||
todo!()
|
||||
vec![]
|
||||
}
|
||||
|
||||
pub fn get_technology_by_name(name: String) -> Technology {
|
||||
todo!()
|
||||
pub fn get_technology_by_name(name: &str) -> Option<Technology> {
|
||||
get_technologies()
|
||||
.into_iter()
|
||||
.find(|technology| technology.name == name)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
use crate::wonder::Wonder;
|
||||
|
||||
pub fn get_wonders() -> Vec<Wonder> {
|
||||
todo!()
|
||||
vec![]
|
||||
}
|
||||
|
||||
pub fn get_wonder_by_name(name: String) -> Wonder {
|
||||
todo!()
|
||||
pub fn get_wonder_by_name(name: &str) -> Option<Wonder> {
|
||||
get_wonders().into_iter().find(|wonder| wonder.name == name)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,208 @@
|
|||
use rand::{rngs::StdRng, Rng, SeedableRng};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
action::PlayingAction::*,
|
||||
content::civilizations,
|
||||
game_api::UserAction,
|
||||
player::{Player, PlayerData},
|
||||
};
|
||||
|
||||
use GameState::*;
|
||||
|
||||
const DICE_ROLL_BUFFER: u32 = 200;
|
||||
const AGES: u32 = 6;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub enum GameState {
|
||||
Playing,
|
||||
StatusPhase,
|
||||
Finished,
|
||||
}
|
||||
|
||||
pub struct Game {
|
||||
pub state: GameState,
|
||||
pub players: Vec<Player>,
|
||||
seed: String,
|
||||
pub starting_player: usize,
|
||||
pub current_player: usize,
|
||||
pub log: Vec<String>,
|
||||
pub played_limited_actions: Vec<String>,
|
||||
pub actions_left: u32,
|
||||
pub round: u32,
|
||||
pub age: u32,
|
||||
pub messages: Vec<String>,
|
||||
pub dice_roll_outcomes: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Game {
|
||||
pub fn new(players: usize, seed: String) -> Self {
|
||||
Self {
|
||||
players: vec![Player::new(); players],
|
||||
seed,
|
||||
pub fn new(player_amount: usize, seed: String) -> Self {
|
||||
let seed_length = seed.len();
|
||||
let seed = if seed_length < 32 {
|
||||
seed + &" ".repeat(32 - seed_length)
|
||||
} else {
|
||||
String::from(&seed[..32])
|
||||
};
|
||||
let seed = seed
|
||||
.as_bytes()
|
||||
.try_into()
|
||||
.expect("seed should be of length 32");
|
||||
let mut random_number_generator = StdRng::from_seed(seed);
|
||||
|
||||
let mut players = Vec::new();
|
||||
let mut civilizations = civilizations::get_civilizations();
|
||||
for _ in 0..player_amount {
|
||||
let civilization = random_number_generator.gen_range(0..civilizations.len());
|
||||
players.push(Player::new(civilizations.remove(civilization)));
|
||||
}
|
||||
|
||||
let starting_player = random_number_generator.gen_range(0..players.len());
|
||||
let mut dice_roll_outcomes = Vec::new();
|
||||
for _ in 0..DICE_ROLL_BUFFER {
|
||||
dice_roll_outcomes.push(random_number_generator.gen_range(1..=12));
|
||||
}
|
||||
|
||||
Self {
|
||||
state: Playing,
|
||||
players,
|
||||
starting_player,
|
||||
current_player: starting_player,
|
||||
log: Vec::new(),
|
||||
played_limited_actions: Vec::new(),
|
||||
actions_left: 3,
|
||||
round: 1,
|
||||
age: 1,
|
||||
messages: Vec::new(),
|
||||
dice_roll_outcomes,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_json(json: &str) -> Self {
|
||||
Self::from_data(
|
||||
serde_json::from_str(json).expect("API call should receive valid game data json"),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn json(self) -> String {
|
||||
serde_json::to_string(&self.data()).expect("game data should be valid json")
|
||||
}
|
||||
|
||||
fn from_data(data: GameData) -> Self {
|
||||
Self {
|
||||
state: data.state,
|
||||
players: data.players.into_iter().map(Player::from_data).collect(),
|
||||
starting_player: data.current_player,
|
||||
current_player: data.current_player,
|
||||
actions_left: data.actions_left,
|
||||
log: data.log,
|
||||
played_limited_actions: data.played_limited_actions,
|
||||
round: data.round,
|
||||
age: data.age,
|
||||
messages: data.messages,
|
||||
dice_roll_outcomes: data.dice_roll_outcomes,
|
||||
}
|
||||
}
|
||||
|
||||
fn data(self) -> GameData {
|
||||
GameData {
|
||||
state: self.state,
|
||||
players: self
|
||||
.players
|
||||
.into_iter()
|
||||
.map(|player| player.data())
|
||||
.collect(),
|
||||
starting_player: self.starting_player,
|
||||
current_player: self.current_player,
|
||||
log: self.log,
|
||||
played_limited_actions: self.played_limited_actions,
|
||||
actions_left: self.actions_left,
|
||||
round: self.round,
|
||||
age: self.age,
|
||||
messages: self.messages,
|
||||
dice_roll_outcomes: self.dice_roll_outcomes,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_next_dice_roll(&mut self) -> u8 {
|
||||
if self.dice_roll_outcomes.is_empty() {
|
||||
println!("ran out of predetermined dice roll outcomes, unseeded rng is no being used");
|
||||
return rand::thread_rng().gen_range(1..=12);
|
||||
}
|
||||
self.dice_roll_outcomes
|
||||
.pop()
|
||||
.expect("dice roll outcomes should not be empty")
|
||||
}
|
||||
|
||||
pub fn execute_playing_action(&mut self, user_action: UserAction, player_index: usize) {
|
||||
let action = user_action.action;
|
||||
let user_specification = user_action.specification;
|
||||
if matches!(action, EndTurn) {
|
||||
self.next_turn();
|
||||
return;
|
||||
}
|
||||
let free_action = action.action_type().free;
|
||||
if !free_action && self.actions_left == 0 {
|
||||
panic!("Illegal action");
|
||||
}
|
||||
if let Custom { name, .. } = &action {
|
||||
if self.played_limited_actions.contains(name) {
|
||||
panic!("Illegal action");
|
||||
}
|
||||
let action_type = action.action_type();
|
||||
if action_type.once_per_turn {
|
||||
self.played_limited_actions.push(name.clone());
|
||||
}
|
||||
}
|
||||
action.execute(&mut self.players[player_index], user_specification);
|
||||
if !free_action {
|
||||
self.actions_left -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn next_turn(&mut self) {
|
||||
self.actions_left = 3;
|
||||
self.played_limited_actions = Vec::new();
|
||||
self.current_player += 1;
|
||||
self.current_player %= self.players.len();
|
||||
if self.current_player == self.starting_player {
|
||||
self.next_round();
|
||||
}
|
||||
}
|
||||
|
||||
fn next_round(&mut self) {
|
||||
self.round += 1;
|
||||
if self.round > 3 {
|
||||
self.round = 1;
|
||||
self.next_age();
|
||||
}
|
||||
}
|
||||
|
||||
fn next_age(&mut self) {
|
||||
self.age += 1;
|
||||
if self.age > AGES {
|
||||
self.add_message("Game has ended");
|
||||
self.state = Finished;
|
||||
return;
|
||||
}
|
||||
self.messages.push(format!("Age {} has started", self.age));
|
||||
self.state = StatusPhase;
|
||||
}
|
||||
|
||||
fn add_message(&mut self, message: &str) {
|
||||
self.messages.push(message.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct GameData {
|
||||
state: GameState,
|
||||
players: Vec<PlayerData>,
|
||||
seed: String,
|
||||
starting_player: usize,
|
||||
current_player: usize,
|
||||
log: Vec<String>,
|
||||
played_limited_actions: Vec<String>,
|
||||
actions_left: u32,
|
||||
round: u32,
|
||||
age: u32,
|
||||
messages: Vec<String>,
|
||||
dice_roll_outcomes: Vec<u8>,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,86 +1,181 @@
|
|||
use std::{cmp::Ordering::*, mem};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::action::PlayingAction;
|
||||
use crate::game::Game;
|
||||
use crate::game::GameState::*;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct GameData;
|
||||
|
||||
pub type GameJson = String;
|
||||
|
||||
pub struct Move;
|
||||
|
||||
pub struct GameOptions;
|
||||
|
||||
pub struct LogSliceOptions {
|
||||
player: Option<usize>,
|
||||
start: u32,
|
||||
end: Option<u32>,
|
||||
pub struct UserAction {
|
||||
pub action: PlayingAction,
|
||||
pub specification: Option<String>,
|
||||
}
|
||||
|
||||
pub struct LogData;
|
||||
impl UserAction {
|
||||
pub fn new(action: PlayingAction, specification: Option<String>) -> Self {
|
||||
Self {
|
||||
action,
|
||||
specification,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct LogSliceOptions {
|
||||
player: Option<usize>,
|
||||
start: usize,
|
||||
end: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Log {
|
||||
items: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct PlayerMetaData {
|
||||
name: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Messages(Vec<String>, String);
|
||||
|
||||
// Game API methods, see https://docs.boardgamers.space/guide/engine-api.html#required-methods
|
||||
|
||||
#[export_name = "init"]
|
||||
pub async extern "C" fn init(
|
||||
players: u32,
|
||||
expansions: Vec<String>,
|
||||
options: GameOptions,
|
||||
players: usize,
|
||||
_expansions: String,
|
||||
_options: String,
|
||||
seed: String,
|
||||
creator: Option<usize>,
|
||||
) -> GameJson {
|
||||
to_json(GameData {});
|
||||
todo!()
|
||||
_creator: String,
|
||||
) -> String {
|
||||
Game::new(players, seed).json()
|
||||
}
|
||||
|
||||
#[export_name = "move"]
|
||||
pub extern "C" fn execute_move(json: GameJson, move_data: Move, player: usize) -> GameJson {
|
||||
let data = from_json(json);
|
||||
to_json(data);
|
||||
todo!()
|
||||
pub extern "C" fn execute_action(game: String, r#move: String, player: usize) -> String {
|
||||
let mut game = Game::from_json(&game);
|
||||
let user_action =
|
||||
serde_json::from_str(&r#move).expect("API call should receive valid move json");
|
||||
game.log.push(r#move);
|
||||
game.execute_playing_action(user_action, player);
|
||||
game.json()
|
||||
}
|
||||
|
||||
#[export_name = "ended"]
|
||||
pub extern "C" fn ended(json: GameJson) -> bool {
|
||||
let data = from_json(json);
|
||||
todo!()
|
||||
pub extern "C" fn ended(game: String) -> bool {
|
||||
let game = Game::from_json(&game);
|
||||
matches!(game.state, Finished)
|
||||
}
|
||||
|
||||
#[export_name = "scores"]
|
||||
pub extern "C" fn scores(json: GameJson) -> Vec<u32> {
|
||||
let data = from_json(json);
|
||||
todo!()
|
||||
pub extern "C" fn scores(game: String) -> Vec<f32> {
|
||||
let game = Game::from_json(&game);
|
||||
let mut scores: Vec<f32> = Vec::new();
|
||||
for player in game.players.iter() {
|
||||
scores.push(player.victory_points());
|
||||
}
|
||||
scores
|
||||
}
|
||||
|
||||
#[export_name = "dropPlayer"]
|
||||
pub async extern "C" fn drop_player(json: GameJson, player: usize) -> GameJson {
|
||||
let data = from_json(json);
|
||||
to_json(data);
|
||||
todo!()
|
||||
pub async extern "C" fn drop_player(game: String, player: usize) -> String {
|
||||
let mut game = Game::from_json(&game);
|
||||
game.players.remove(player);
|
||||
game.json()
|
||||
}
|
||||
|
||||
#[export_name = "currentPlayer"]
|
||||
pub async extern "C" fn current_player(json: GameJson) -> Option<usize> {
|
||||
let data = from_json(json);
|
||||
todo!()
|
||||
pub async extern "C" fn current_player(game: String) -> Option<usize> {
|
||||
let game = Game::from_json(&game);
|
||||
match game.state {
|
||||
Finished => None,
|
||||
_ => Some(game.current_player),
|
||||
}
|
||||
}
|
||||
|
||||
#[export_name = "logLength"]
|
||||
pub async extern "C" fn log_length(json: GameJson) -> u32 {
|
||||
let data = from_json(json);
|
||||
todo!()
|
||||
pub async extern "C" fn log_length(game: String) -> usize {
|
||||
let game = Game::from_json(&game);
|
||||
game.log.len()
|
||||
}
|
||||
|
||||
#[export_name = "logSlice"]
|
||||
pub async extern "C" fn log_slice(json: GameJson, options: LogSliceOptions) -> LogData {
|
||||
let data = from_json(json);
|
||||
todo!()
|
||||
pub async extern "C" fn log_slice(game: String, options: String) -> String {
|
||||
let game = Game::from_json(&game);
|
||||
let options =
|
||||
serde_json::from_str::<LogSliceOptions>(&options).expect("options should be serializable");
|
||||
let log_slice = match options.end {
|
||||
Some(end) => &game.log[options.start..=end],
|
||||
None => &game.log[options.start..],
|
||||
}
|
||||
.to_vec();
|
||||
serde_json::to_string(&log_slice).expect("log slice should be serializable")
|
||||
}
|
||||
|
||||
fn from_json(data: GameJson) -> GameData {
|
||||
let game: GameData =
|
||||
serde_json::from_str(&data).expect("API call should receive valid game data json");
|
||||
game
|
||||
#[export_name = "setPlayerMetaData"]
|
||||
pub extern "C" fn set_player_meta_data(game: String, player: usize, meta_data: String) -> String {
|
||||
let name = serde_json::from_str::<PlayerMetaData>(&meta_data)
|
||||
.expect("")
|
||||
.name;
|
||||
let mut game = Game::from_json(&game);
|
||||
game.players[player].set_name(name);
|
||||
game.json()
|
||||
}
|
||||
|
||||
fn to_json(game: GameData) -> GameJson {
|
||||
serde_json::to_string(&game).expect("game data should be valid json")
|
||||
#[export_name = "rankings"]
|
||||
pub extern "C" fn rankings(game: String) -> Vec<u32> {
|
||||
let game = Game::from_json(&game);
|
||||
let mut rankings = Vec::new();
|
||||
for player in game.players.iter() {
|
||||
let mut rank = 1;
|
||||
for other in game.players.iter() {
|
||||
if other.compare_score(player) == Greater {
|
||||
rank += 1;
|
||||
}
|
||||
}
|
||||
rankings.push(rank);
|
||||
}
|
||||
rankings
|
||||
}
|
||||
|
||||
#[export_name = "round"]
|
||||
pub extern "C" fn round(game: String) -> Option<u32> {
|
||||
let game = Game::from_json(&game);
|
||||
match game.state {
|
||||
Playing => Some((game.age - 1) * 3 + game.round),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[export_name = "factions"]
|
||||
pub extern "C" fn civilizations(game: String) -> Vec<String> {
|
||||
let game = Game::from_json(&game);
|
||||
game.players
|
||||
.into_iter()
|
||||
.map(|player| player.civilization.name)
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[export_name = "stripSecret"]
|
||||
pub extern "C" fn strip_secret(game: String, player: Option<usize>) -> String {
|
||||
let player_index = player;
|
||||
let mut game = Game::from_json(&game);
|
||||
game.dice_roll_outcomes = Vec::new();
|
||||
for (i, player) in game.players.iter_mut().enumerate() {
|
||||
if player_index != Some(i) {
|
||||
player.strip_secret()
|
||||
}
|
||||
}
|
||||
game.json()
|
||||
}
|
||||
|
||||
#[export_name = "messages"]
|
||||
pub extern "C" fn messages(game: String) -> String {
|
||||
let mut game = Game::from_json(&game);
|
||||
let messages = mem::take(&mut game.messages);
|
||||
serde_json::to_string(&Messages(messages, game.json()))
|
||||
.expect("Messages should be serializable")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,24 +1,6 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::landmark::Landmark;
|
||||
|
||||
pub struct Hexagon {
|
||||
pub position: HexagonPosition,
|
||||
pub landmark: Landmark,
|
||||
pub discovered: bool,
|
||||
}
|
||||
|
||||
impl Hexagon {
|
||||
pub fn new(position: HexagonPosition, landmark: Landmark) -> Hexagon {
|
||||
Hexagon {
|
||||
position,
|
||||
landmark,
|
||||
discovered: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct HexagonPosition {
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
|
|
@ -38,6 +20,11 @@ impl HexagonPosition {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct Tile {
|
||||
hexagons: Vec<Hexagon>,
|
||||
pub enum Landmark {
|
||||
Barren,
|
||||
Mountain,
|
||||
Fertile,
|
||||
Forest,
|
||||
Unusable,
|
||||
Water,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +0,0 @@
|
|||
pub enum Landmark {
|
||||
Barren,
|
||||
Mountain,
|
||||
Fertile,
|
||||
Forest,
|
||||
Unusable,
|
||||
Water,
|
||||
}
|
||||
|
|
@ -4,22 +4,46 @@ use crate::player::{self, PlayerInitializer, PlayerSetup};
|
|||
|
||||
pub struct Leader {
|
||||
pub name: String,
|
||||
pub first_ability: String,
|
||||
pub first_ability_description: String,
|
||||
pub second_ability: String,
|
||||
pub second_ability_description: String,
|
||||
pub player_initializer: PlayerInitializer,
|
||||
pub player_deinitializer: PlayerInitializer,
|
||||
}
|
||||
|
||||
impl Leader {
|
||||
pub fn builder(name: &str) -> LeaderBuilder {
|
||||
LeaderBuilder::new(name.to_string())
|
||||
pub fn builder(
|
||||
name: &str,
|
||||
first_ability: &str,
|
||||
first_ability_description: &str,
|
||||
second_ability: &str,
|
||||
second_ability_description: &str,
|
||||
) -> LeaderBuilder {
|
||||
LeaderBuilder::new(
|
||||
name.to_string(),
|
||||
first_ability.to_string(),
|
||||
first_ability_description.to_string(),
|
||||
second_ability.to_string(),
|
||||
second_ability_description.to_string(),
|
||||
)
|
||||
}
|
||||
|
||||
fn new(
|
||||
name: String,
|
||||
first_ability: String,
|
||||
first_ability_description: String,
|
||||
second_ability: String,
|
||||
second_ability_description: String,
|
||||
player_initializer: PlayerInitializer,
|
||||
player_deinitializer: PlayerInitializer,
|
||||
) -> Self {
|
||||
Self {
|
||||
name,
|
||||
first_ability,
|
||||
first_ability_description,
|
||||
second_ability,
|
||||
second_ability_description,
|
||||
player_initializer,
|
||||
player_deinitializer,
|
||||
}
|
||||
|
|
@ -28,14 +52,28 @@ impl Leader {
|
|||
|
||||
pub struct LeaderBuilder {
|
||||
name: String,
|
||||
first_ability: String,
|
||||
first_ability_description: String,
|
||||
second_ability: String,
|
||||
second_ability_description: String,
|
||||
player_initializers: Vec<PlayerInitializer>,
|
||||
player_deinitializers: Vec<PlayerInitializer>,
|
||||
}
|
||||
|
||||
impl LeaderBuilder {
|
||||
fn new(name: String) -> Self {
|
||||
fn new(
|
||||
name: String,
|
||||
first_ability: String,
|
||||
first_ability_description: String,
|
||||
second_ability: String,
|
||||
second_ability_description: String,
|
||||
) -> Self {
|
||||
Self {
|
||||
name,
|
||||
first_ability,
|
||||
first_ability_description,
|
||||
second_ability,
|
||||
second_ability_description,
|
||||
player_initializers: Vec::new(),
|
||||
player_deinitializers: Vec::new(),
|
||||
}
|
||||
|
|
@ -44,7 +82,15 @@ impl LeaderBuilder {
|
|||
pub fn build(self) -> Leader {
|
||||
let player_initializer = player::join_player_initializers(self.player_initializers);
|
||||
let player_deinitializer = player::join_player_initializers(self.player_deinitializers);
|
||||
Leader::new(self.name, player_initializer, player_deinitializer)
|
||||
Leader::new(
|
||||
self.name,
|
||||
self.first_ability,
|
||||
self.first_ability_description,
|
||||
self.second_ability,
|
||||
self.second_ability_description,
|
||||
player_initializer,
|
||||
player_deinitializer,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
mod action;
|
||||
mod army;
|
||||
mod city;
|
||||
mod civilization;
|
||||
mod content;
|
||||
mod events;
|
||||
mod game;
|
||||
mod game_api;
|
||||
mod hexagon;
|
||||
mod landmark;
|
||||
mod leader;
|
||||
mod player;
|
||||
mod resource_pile;
|
||||
|
|
|
|||
|
|
@ -1,12 +1,14 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
cmp::Ordering::{self, *},
|
||||
collections::{HashMap, VecDeque},
|
||||
fmt::Display,
|
||||
mem,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
army::Unit,
|
||||
city::{Building, City, CityData},
|
||||
city::{City, CityData, CityPiece},
|
||||
civilization::Civilization,
|
||||
content::{civilizations, technologies},
|
||||
events::EventMut,
|
||||
|
|
@ -19,8 +21,14 @@ use crate::{
|
|||
|
||||
const TECHNOLOGY_COST: u32 = 2;
|
||||
|
||||
const BUILDING_VICTORY_POINTS: f32 = 1.0;
|
||||
const TECHNOLOGY_VICTORY_POINTS: f32 = 0.5;
|
||||
const OBJECTIVE_VICTORY_POINTS: f32 = 2.0;
|
||||
const WONDER_VICTORY_POINTS: f32 = 4.0;
|
||||
const DEFEATED_LEADER_VICTORY_POINTS: f32 = 2.0;
|
||||
|
||||
pub struct Player {
|
||||
pub name: String,
|
||||
name: Option<String>,
|
||||
resources: ResourcePile,
|
||||
pub resource_limit: ResourcePile,
|
||||
events: Option<PlayerEvents>,
|
||||
|
|
@ -31,9 +39,16 @@ pub struct Player {
|
|||
pub active_leader: Option<Leader>,
|
||||
pub available_leaders: Vec<Leader>,
|
||||
pub researched_technologies: Vec<String>,
|
||||
pub unlocked_special_technologies: Vec<String>,
|
||||
pub wonders: Vec<String>,
|
||||
pub wonders_build: usize,
|
||||
pub leader_position: Option<HexagonPosition>,
|
||||
event_tokens: u8,
|
||||
victory_points: u32,
|
||||
pub influenced_buildings: u32,
|
||||
pub event_tokens: u8,
|
||||
pub completed_objectives: Vec<String>,
|
||||
pub defeated_leaders: Vec<String>,
|
||||
pub event_victory_points: f32,
|
||||
pub custom_actions: Vec<String>,
|
||||
}
|
||||
|
||||
impl Player {
|
||||
|
|
@ -46,69 +61,82 @@ impl Player {
|
|||
event_listener_indices: HashMap::new(),
|
||||
cities: data.cities.into_iter().map(City::from_data).collect(),
|
||||
units: data.units,
|
||||
civilization: civilizations::get_civilization_by_name(data.civilization.clone()),
|
||||
civilization: civilizations::get_civilization_by_name(&data.civilization)
|
||||
.expect("player data should have a valid civilization"),
|
||||
active_leader: data
|
||||
.active_leader
|
||||
.map(|leader| civilizations::get_leader_by_name(leader, data.civilization.clone())),
|
||||
.map(|leader| civilizations::get_leader_by_name(&leader, &data.civilization))
|
||||
.expect("player data should contain a valid leader"),
|
||||
available_leaders: data
|
||||
.available_leaders
|
||||
.into_iter()
|
||||
.map(|leader| civilizations::get_leader_by_name(leader, data.civilization.clone()))
|
||||
.map(|leader| {
|
||||
civilizations::get_leader_by_name(&leader, &data.civilization)
|
||||
.expect("player data should contain valid leaders")
|
||||
})
|
||||
.collect(),
|
||||
researched_technologies: data.researched_technologies,
|
||||
unlocked_special_technologies: data.unlocked_special_technologies,
|
||||
wonders: data.wonders,
|
||||
wonders_build: data.wonders_build,
|
||||
leader_position: data.leader_position,
|
||||
event_tokens: data.event_tokens,
|
||||
victory_points: data.victory_points,
|
||||
influenced_buildings: data.influenced_buildings,
|
||||
completed_objectives: data.completed_objectives,
|
||||
defeated_leaders: data.defeated_leaders,
|
||||
event_victory_points: data.event_victory_points,
|
||||
custom_actions: data.custom_actions,
|
||||
};
|
||||
let technologies = player
|
||||
.researched_technologies
|
||||
.iter()
|
||||
.map(|technology| technologies::get_technology_by_name(technology.clone()))
|
||||
.collect::<Vec<Technology>>();
|
||||
for technology in technologies.into_iter() {
|
||||
player.research_technology(&technology);
|
||||
let technologies = mem::take(&mut player.researched_technologies);
|
||||
for technology in technologies.iter() {
|
||||
player.research_technology(technology);
|
||||
}
|
||||
player.researched_technologies = technologies;
|
||||
if let Some(leader) = player.active_leader.take() {
|
||||
(leader.player_initializer)(&mut player);
|
||||
player.active_leader = Some(leader);
|
||||
}
|
||||
let mut cities = Vec::new();
|
||||
cities.append(&mut player.cities);
|
||||
let mut cities = mem::take(&mut player.cities);
|
||||
for city in cities.iter_mut() {
|
||||
if let Some(wonder) = city.buildings.wonder.take() {
|
||||
for wonder in city.city_pieces.wonders.iter() {
|
||||
(wonder.player_initializer)(&mut player);
|
||||
city.buildings.wonder = Some(wonder);
|
||||
}
|
||||
}
|
||||
player.cities.append(&mut cities);
|
||||
player
|
||||
}
|
||||
|
||||
pub fn to_data(self) -> PlayerData {
|
||||
PlayerData::new(
|
||||
self.name,
|
||||
self.resources,
|
||||
self.resource_limit,
|
||||
self.cities.into_iter().map(|city| city.to_data()).collect(),
|
||||
self.units,
|
||||
self.civilization.name,
|
||||
self.active_leader.map(|leader| leader.name),
|
||||
self.available_leaders
|
||||
pub fn data(self) -> PlayerData {
|
||||
PlayerData {
|
||||
name: self.name,
|
||||
resources: self.resources,
|
||||
resource_limit: self.resource_limit,
|
||||
cities: self.cities.into_iter().map(|city| city.data()).collect(),
|
||||
units: self.units,
|
||||
civilization: self.civilization.name,
|
||||
active_leader: self.active_leader.map(|leader| leader.name),
|
||||
available_leaders: self
|
||||
.available_leaders
|
||||
.into_iter()
|
||||
.map(|leader| leader.name)
|
||||
.collect(),
|
||||
self.researched_technologies,
|
||||
self.leader_position,
|
||||
self.event_tokens,
|
||||
self.victory_points,
|
||||
)
|
||||
researched_technologies: self.researched_technologies,
|
||||
unlocked_special_technologies: self.unlocked_special_technologies,
|
||||
wonders: self.wonders,
|
||||
wonders_build: self.wonders_build,
|
||||
leader_position: self.leader_position,
|
||||
event_tokens: self.event_tokens,
|
||||
influenced_buildings: self.influenced_buildings,
|
||||
completed_objectives: self.completed_objectives,
|
||||
defeated_leaders: self.defeated_leaders,
|
||||
event_victory_points: self.event_victory_points,
|
||||
custom_actions: self.custom_actions,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(name: &str, mut civilization: Civilization) -> Self {
|
||||
let mut leaders = Vec::new();
|
||||
leaders.append(&mut civilization.leaders);
|
||||
pub fn new(civilization: Civilization) -> Self {
|
||||
Self {
|
||||
name: name.to_string(),
|
||||
name: None,
|
||||
resources: ResourcePile::food(2),
|
||||
resource_limit: ResourcePile::new(2, 7, 7, 7, 7, 7, 7),
|
||||
events: Some(PlayerEvents::default()),
|
||||
|
|
@ -117,14 +145,32 @@ impl Player {
|
|||
units: Vec::new(),
|
||||
civilization,
|
||||
active_leader: None,
|
||||
available_leaders: leaders,
|
||||
available_leaders: Vec::new(),
|
||||
researched_technologies: Vec::new(),
|
||||
unlocked_special_technologies: Vec::new(),
|
||||
wonders: Vec::new(),
|
||||
wonders_build: 0,
|
||||
leader_position: None,
|
||||
event_tokens: 3,
|
||||
victory_points: 0,
|
||||
influenced_buildings: 0,
|
||||
completed_objectives: Vec::new(),
|
||||
defeated_leaders: Vec::new(),
|
||||
event_victory_points: 0.0,
|
||||
custom_actions: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(&self) -> String {
|
||||
self.name
|
||||
.as_ref()
|
||||
.expect("name should be set at this point")
|
||||
.clone()
|
||||
}
|
||||
|
||||
pub fn set_name(&mut self, name: String) {
|
||||
self.name = Some(name);
|
||||
}
|
||||
|
||||
pub fn gain_resources(&mut self, resources: ResourcePile) {
|
||||
self.resources += resources;
|
||||
self.resources.apply_resource_limit(&self.resource_limit);
|
||||
|
|
@ -151,8 +197,12 @@ impl Player {
|
|||
self.active_leader = Some(new_leader);
|
||||
}
|
||||
|
||||
pub fn can_research_technology(&self, technology: &Technology) -> bool {
|
||||
if self.resources.food + self.resources.ideas + self.resources.gold < TECHNOLOGY_COST {
|
||||
pub fn can_research_technology(&self, technology: &str) -> bool {
|
||||
let technology =
|
||||
technologies::get_technology_by_name(technology).expect("technology should exist");
|
||||
if self.resources.food + self.resources.ideas + (self.resources.gold as u32)
|
||||
< TECHNOLOGY_COST
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if self.has_technology(&technology.name) {
|
||||
|
|
@ -177,7 +227,9 @@ impl Player {
|
|||
.any(|research_technology| research_technology == technology)
|
||||
}
|
||||
|
||||
pub fn research_technology(&mut self, technology: &Technology) {
|
||||
pub fn research_technology(&mut self, technology: &String) {
|
||||
let technology =
|
||||
technologies::get_technology_by_name(technology).expect("technology should exist");
|
||||
if let Some(advance_bonus) = &technology.advance_bonus {
|
||||
self.gain_resources(advance_bonus.resources());
|
||||
}
|
||||
|
|
@ -192,7 +244,7 @@ impl Player {
|
|||
}
|
||||
}
|
||||
(technology.player_initializer)(self);
|
||||
self.researched_technologies.push(technology.name.clone());
|
||||
self.researched_technologies.push(technology.name);
|
||||
self.event_tokens -= 1;
|
||||
if self.event_tokens == 0 {
|
||||
self.event_tokens = 3;
|
||||
|
|
@ -213,18 +265,25 @@ impl Player {
|
|||
|
||||
fn unlock_special_technology(&mut self, special_technology: &SpecialTechnology) {
|
||||
(special_technology.player_initializer)(self);
|
||||
self.unlocked_special_technologies
|
||||
.push(special_technology.name.clone());
|
||||
}
|
||||
|
||||
pub fn victory_points(&self) -> u32 {
|
||||
self.victory_points / 2
|
||||
}
|
||||
|
||||
pub fn gain_victory_points(&mut self, victory_points: f32) {
|
||||
self.victory_points += (victory_points * 2.0) as u32;
|
||||
}
|
||||
|
||||
pub fn loose_victory_points(&mut self, victory_points: f32) {
|
||||
self.victory_points -= (victory_points * 2.0) as u32;
|
||||
pub fn victory_points(&self) -> f32 {
|
||||
let mut victory_points = 0.0;
|
||||
for city in self.cities.iter() {
|
||||
victory_points += city.buildings() as f32 * BUILDING_VICTORY_POINTS;
|
||||
}
|
||||
victory_points += self.influenced_buildings as f32 * BUILDING_VICTORY_POINTS;
|
||||
victory_points += (self.researched_technologies.len()
|
||||
+ self.unlocked_special_technologies.len()) as f32
|
||||
* TECHNOLOGY_VICTORY_POINTS;
|
||||
victory_points += self.completed_objectives.len() as f32 * OBJECTIVE_VICTORY_POINTS;
|
||||
victory_points += self.wonders.len() as f32 * WONDER_VICTORY_POINTS / 2.0;
|
||||
victory_points += self.wonders_build as f32 * WONDER_VICTORY_POINTS / 2.0;
|
||||
victory_points += self.event_victory_points;
|
||||
victory_points += self.defeated_leaders.len() as f32 * DEFEATED_LEADER_VICTORY_POINTS;
|
||||
victory_points
|
||||
}
|
||||
|
||||
pub fn events(&mut self) -> &mut PlayerEvents {
|
||||
|
|
@ -248,11 +307,58 @@ impl Player {
|
|||
fn trigger_game_event(&mut self) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn strip_secret(&mut self) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn compare_score(&self, other: &Self) -> Ordering {
|
||||
let mut building_score = 0;
|
||||
for city in self.cities.iter() {
|
||||
building_score += city.buildings();
|
||||
}
|
||||
building_score += self.influenced_buildings;
|
||||
let mut other_building_score = 0;
|
||||
for city in self.cities.iter() {
|
||||
other_building_score += city.buildings();
|
||||
}
|
||||
other_building_score += self.influenced_buildings;
|
||||
match building_score.cmp(&other_building_score) {
|
||||
Less => return Less,
|
||||
Equal => (),
|
||||
Greater => return Greater,
|
||||
}
|
||||
match (self.researched_technologies.len() + self.unlocked_special_technologies.len())
|
||||
.cmp(&(other.researched_technologies.len() + other.unlocked_special_technologies.len()))
|
||||
{
|
||||
Less => return Less,
|
||||
Equal => (),
|
||||
Greater => return Greater,
|
||||
}
|
||||
match self
|
||||
.completed_objectives
|
||||
.len()
|
||||
.cmp(&other.completed_objectives.len())
|
||||
{
|
||||
Less => return Less,
|
||||
Equal => (),
|
||||
Greater => return Greater,
|
||||
}
|
||||
match (self.wonders.len() + self.wonders_build)
|
||||
.cmp(&(other.wonders.len() + other.wonders_build))
|
||||
{
|
||||
Less => return Less,
|
||||
Equal => (),
|
||||
Greater => return Greater,
|
||||
}
|
||||
self.event_victory_points
|
||||
.total_cmp(&other.event_victory_points)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct PlayerData {
|
||||
name: String,
|
||||
name: Option<String>,
|
||||
resources: ResourcePile,
|
||||
resource_limit: ResourcePile,
|
||||
cities: Vec<CityData>,
|
||||
|
|
@ -261,48 +367,22 @@ pub struct PlayerData {
|
|||
active_leader: Option<String>,
|
||||
available_leaders: Vec<String>,
|
||||
researched_technologies: Vec<String>,
|
||||
unlocked_special_technologies: Vec<String>,
|
||||
wonders: Vec<String>,
|
||||
wonders_build: usize,
|
||||
leader_position: Option<HexagonPosition>,
|
||||
event_tokens: u8,
|
||||
victory_points: u32,
|
||||
}
|
||||
|
||||
impl PlayerData {
|
||||
pub fn new(
|
||||
name: String,
|
||||
resources: ResourcePile,
|
||||
resource_limit: ResourcePile,
|
||||
cities: Vec<CityData>,
|
||||
units: Vec<Unit>,
|
||||
civilization: String,
|
||||
active_leader: Option<String>,
|
||||
available_leaders: Vec<String>,
|
||||
researched_technologies: Vec<String>,
|
||||
leader_position: Option<HexagonPosition>,
|
||||
event_tokens: u8,
|
||||
victory_points: u32,
|
||||
) -> Self {
|
||||
Self {
|
||||
name,
|
||||
resources,
|
||||
resource_limit,
|
||||
cities,
|
||||
units,
|
||||
civilization,
|
||||
active_leader,
|
||||
available_leaders,
|
||||
researched_technologies,
|
||||
leader_position,
|
||||
event_tokens,
|
||||
victory_points,
|
||||
}
|
||||
}
|
||||
influenced_buildings: u32,
|
||||
completed_objectives: Vec<String>,
|
||||
defeated_leaders: Vec<String>,
|
||||
event_victory_points: f32,
|
||||
custom_actions: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct PlayerEvents {
|
||||
pub some_event: EventMut<i32, String>,
|
||||
pub city_size_increase: EventMut<Player, City, Building>,
|
||||
pub city_size_increase_cost: EventMut<ResourcePile, City, Building>,
|
||||
pub city_size_increase: EventMut<Player, City, CityPiece>,
|
||||
pub city_size_increase_cost: EventMut<ResourcePile, City, CityPiece>,
|
||||
}
|
||||
|
||||
pub type PlayerInitializer = Box<dyn Fn(&mut Player)>;
|
||||
|
|
|
|||
|
|
@ -1,17 +1,18 @@
|
|||
use std::{
|
||||
cmp,
|
||||
ops::{Add, AddAssign, SubAssign},
|
||||
fmt::Display,
|
||||
ops::{Add, AddAssign, Mul, SubAssign},
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Default, Clone, Serialize, Deserialize)]
|
||||
#[derive(Default, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct ResourcePile {
|
||||
pub wood: u32,
|
||||
pub stone: u32,
|
||||
pub gold: u32,
|
||||
pub food: u32,
|
||||
pub wood: u32,
|
||||
pub ore: u32,
|
||||
pub ideas: u32,
|
||||
pub gold: i32,
|
||||
pub mood_tokens: u32,
|
||||
pub culture_tokens: u32,
|
||||
}
|
||||
|
|
@ -20,40 +21,40 @@ impl ResourcePile {
|
|||
pub fn new(
|
||||
food: u32,
|
||||
wood: u32,
|
||||
stone: u32,
|
||||
ore: u32,
|
||||
ideas: u32,
|
||||
gold: u32,
|
||||
gold: i32,
|
||||
mood_tokens: u32,
|
||||
culture_tokens: u32,
|
||||
) -> Self {
|
||||
Self {
|
||||
wood,
|
||||
stone,
|
||||
gold,
|
||||
food,
|
||||
wood,
|
||||
ore,
|
||||
ideas,
|
||||
gold,
|
||||
mood_tokens,
|
||||
culture_tokens,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wood(amount: u32) -> Self {
|
||||
pub fn food(amount: u32) -> Self {
|
||||
Self::new(amount, 0, 0, 0, 0, 0, 0)
|
||||
}
|
||||
|
||||
pub fn stone(amount: u32) -> Self {
|
||||
pub fn wood(amount: u32) -> Self {
|
||||
Self::new(0, amount, 0, 0, 0, 0, 0)
|
||||
}
|
||||
|
||||
pub fn gold(amount: u32) -> Self {
|
||||
pub fn ore(amount: u32) -> Self {
|
||||
Self::new(0, 0, amount, 0, 0, 0, 0)
|
||||
}
|
||||
|
||||
pub fn food(amount: u32) -> Self {
|
||||
pub fn ideas(amount: u32) -> Self {
|
||||
Self::new(0, 0, 0, amount, 0, 0, 0)
|
||||
}
|
||||
|
||||
pub fn ideas(amount: u32) -> Self {
|
||||
pub fn gold(amount: i32) -> Self {
|
||||
Self::new(0, 0, 0, 0, amount, 0, 0)
|
||||
}
|
||||
|
||||
|
|
@ -67,36 +68,39 @@ impl ResourcePile {
|
|||
|
||||
pub fn can_afford(&self, other: &Self) -> bool {
|
||||
let mut resource_deficit = 0;
|
||||
if other.food > self.food {
|
||||
resource_deficit += other.food - self.food;
|
||||
}
|
||||
if other.wood > self.wood {
|
||||
resource_deficit += other.wood - self.wood;
|
||||
}
|
||||
if other.stone > self.stone {
|
||||
resource_deficit += other.stone - self.stone;
|
||||
}
|
||||
if other.food > self.food {
|
||||
resource_deficit += other.food - self.food;
|
||||
if other.ore > self.ore {
|
||||
resource_deficit += other.ore - self.ore;
|
||||
}
|
||||
if other.ideas > self.ideas {
|
||||
resource_deficit += other.ideas - self.ideas;
|
||||
}
|
||||
self.gold >= other.gold + resource_deficit
|
||||
self.gold >= other.gold + resource_deficit as i32
|
||||
&& self.mood_tokens >= other.mood_tokens
|
||||
&& self.culture_tokens >= other.culture_tokens
|
||||
}
|
||||
|
||||
pub fn apply_resource_limit(&mut self, limit: &ResourcePile) {
|
||||
if self.wood > limit.wood {
|
||||
self.wood = limit.wood;
|
||||
}
|
||||
if self.stone > limit.stone {
|
||||
self.stone = limit.stone;
|
||||
}
|
||||
if self.food > limit.food {
|
||||
self.food = limit.food;
|
||||
}
|
||||
if self.wood > limit.wood {
|
||||
self.wood = limit.wood;
|
||||
}
|
||||
if self.ore > limit.ore {
|
||||
self.ore = limit.ore;
|
||||
}
|
||||
if self.ideas > limit.ideas {
|
||||
self.ideas = limit.ideas;
|
||||
}
|
||||
if self.gold > limit.gold {
|
||||
self.gold = limit.gold;
|
||||
}
|
||||
if self.mood_tokens > limit.mood_tokens {
|
||||
self.mood_tokens = limit.mood_tokens;
|
||||
}
|
||||
|
|
@ -104,15 +108,46 @@ impl ResourcePile {
|
|||
self.culture_tokens = limit.culture_tokens;
|
||||
}
|
||||
}
|
||||
|
||||
//this function assumes that `budget` can afford `self`
|
||||
pub fn get_payment_options(&self, budged: &Self) -> PaymentOptions {
|
||||
let mut gold_left = budged.gold as u32;
|
||||
let mut gold_cost = self.gold;
|
||||
let mut jokers_left = 0;
|
||||
if gold_cost >= 0 {
|
||||
gold_left -= gold_cost as u32;
|
||||
} else {
|
||||
jokers_left = (-gold_cost) as u32;
|
||||
gold_cost = 0;
|
||||
}
|
||||
if self.wood > budged.wood {
|
||||
let joker_cost = self.wood - budged.wood;
|
||||
if joker_cost > jokers_left {
|
||||
gold_left -= joker_cost - jokers_left;
|
||||
gold_cost += (joker_cost - jokers_left) as i32;
|
||||
}
|
||||
jokers_left = cmp::max(jokers_left - joker_cost, 0);
|
||||
}
|
||||
let default = Self::new(
|
||||
cmp::min(self.food, budged.food),
|
||||
cmp::min(self.wood, budged.wood),
|
||||
cmp::min(self.ore, budged.ore),
|
||||
cmp::min(self.ideas, budged.ideas),
|
||||
gold_cost,
|
||||
self.mood_tokens,
|
||||
self.culture_tokens,
|
||||
);
|
||||
PaymentOptions::new(default, gold_left, jokers_left)
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign for ResourcePile {
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
self.wood += rhs.wood;
|
||||
self.stone += rhs.stone;
|
||||
self.gold += rhs.gold;
|
||||
self.food += rhs.food;
|
||||
self.wood += rhs.wood;
|
||||
self.ore += rhs.ore;
|
||||
self.ideas += rhs.ideas;
|
||||
self.gold += rhs.gold;
|
||||
self.mood_tokens += rhs.mood_tokens;
|
||||
self.culture_tokens += rhs.culture_tokens;
|
||||
}
|
||||
|
|
@ -123,11 +158,11 @@ impl Add for ResourcePile {
|
|||
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
Self::new(
|
||||
self.wood + rhs.wood,
|
||||
self.stone + rhs.stone,
|
||||
self.gold + rhs.gold,
|
||||
self.food + rhs.food,
|
||||
self.wood + rhs.wood,
|
||||
self.ore + rhs.ore,
|
||||
self.ideas + rhs.ideas,
|
||||
self.gold + rhs.gold,
|
||||
self.mood_tokens + rhs.mood_tokens,
|
||||
self.culture_tokens + rhs.culture_tokens,
|
||||
)
|
||||
|
|
@ -136,12 +171,84 @@ impl Add for ResourcePile {
|
|||
|
||||
impl SubAssign for ResourcePile {
|
||||
fn sub_assign(&mut self, rhs: Self) {
|
||||
self.wood = cmp::max(self.wood - rhs.wood, 0);
|
||||
self.stone = cmp::max(self.stone - rhs.stone, 0);
|
||||
self.gold = cmp::max(self.gold - rhs.gold, 0);
|
||||
self.food = cmp::max(self.food - rhs.food, 0);
|
||||
self.wood = cmp::max(self.wood - rhs.wood, 0);
|
||||
self.ore = cmp::max(self.ore - rhs.ore, 0);
|
||||
self.ideas = cmp::max(self.ideas - rhs.ideas, 0);
|
||||
self.gold = cmp::max(self.gold - rhs.gold, 0);
|
||||
self.mood_tokens = cmp::max(self.mood_tokens - rhs.mood_tokens, 0);
|
||||
self.culture_tokens = cmp::max(self.culture_tokens - rhs.culture_tokens, 0);
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<u32> for ResourcePile {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, rhs: u32) -> Self::Output {
|
||||
Self::new(
|
||||
self.food * rhs,
|
||||
self.wood * rhs,
|
||||
self.ore * rhs,
|
||||
self.ideas * rhs,
|
||||
self.gold * rhs as i32,
|
||||
self.mood_tokens * rhs,
|
||||
self.culture_tokens * rhs,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for ResourcePile {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let mut resources = Vec::new();
|
||||
if self.food > 0 {
|
||||
resources.push(format!("{} food", self.food));
|
||||
}
|
||||
if self.wood > 0 {
|
||||
resources.push(format!("{} wood", self.wood));
|
||||
}
|
||||
if self.ore > 0 {
|
||||
resources.push(format!("{} ore", self.ore));
|
||||
}
|
||||
if self.ideas > 0 {
|
||||
resources.push(format!("{} ideas", self.ideas));
|
||||
}
|
||||
if self.gold > 0 {
|
||||
resources.push(format!("{} gold", self.gold));
|
||||
}
|
||||
if self.mood_tokens > 0 {
|
||||
resources.push(format!("{} mood tokens", self.mood_tokens));
|
||||
}
|
||||
if self.culture_tokens > 0 {
|
||||
resources.push(format!("{} culture tokens", self.culture_tokens));
|
||||
}
|
||||
match &resources[..] {
|
||||
[] => write!(f, "nothing"),
|
||||
[resource] => write!(f, "{resource}"),
|
||||
_ => write!(
|
||||
f,
|
||||
"{} and {}",
|
||||
&resources[..resources.len() - 1].join(", "),
|
||||
resources
|
||||
.last()
|
||||
.as_ref()
|
||||
.expect("resources should have a length greater or equal to 2")
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PaymentOptions {
|
||||
default: ResourcePile,
|
||||
gold_left: u32,
|
||||
jokers_left: u32,
|
||||
}
|
||||
|
||||
impl PaymentOptions {
|
||||
fn new(default: ResourcePile, gold_left: u32, jokers_left: u32) -> Self {
|
||||
Self {
|
||||
default,
|
||||
gold_left,
|
||||
jokers_left,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ use crate::player::{self, PlayerInitializer, PlayerSetup};
|
|||
|
||||
pub struct SpecialTechnology {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub required_technology: String,
|
||||
pub player_initializer: PlayerInitializer,
|
||||
pub player_deinitializer: PlayerInitializer,
|
||||
|
|
@ -16,12 +17,14 @@ impl SpecialTechnology {
|
|||
|
||||
fn new(
|
||||
name: String,
|
||||
description: String,
|
||||
required_technology: String,
|
||||
player_initializer: PlayerInitializer,
|
||||
player_deinitializer: PlayerInitializer,
|
||||
) -> Self {
|
||||
Self {
|
||||
name,
|
||||
description,
|
||||
required_technology,
|
||||
player_initializer,
|
||||
player_deinitializer,
|
||||
|
|
@ -31,6 +34,7 @@ impl SpecialTechnology {
|
|||
|
||||
pub struct SpecialTechnologyBuilder {
|
||||
name: String,
|
||||
descriptions: Vec<String>,
|
||||
required_technology: String,
|
||||
initializers: Vec<PlayerInitializer>,
|
||||
deinitializers: Vec<PlayerInitializer>,
|
||||
|
|
@ -40,17 +44,24 @@ impl SpecialTechnologyBuilder {
|
|||
fn new(name: String, required_technology: String) -> Self {
|
||||
Self {
|
||||
name,
|
||||
descriptions: Vec::new(),
|
||||
required_technology,
|
||||
initializers: Vec::new(),
|
||||
deinitializers: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_description(mut self, description: String) -> Self {
|
||||
self.descriptions.push(description);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> SpecialTechnology {
|
||||
let initializer = player::join_player_initializers(self.initializers);
|
||||
let deinitializer = player::join_player_initializers(self.deinitializers);
|
||||
SpecialTechnology::new(
|
||||
self.name,
|
||||
String::from("● ") + &self.descriptions.join("\n● "),
|
||||
self.required_technology,
|
||||
initializer,
|
||||
deinitializer,
|
||||
|
|
|
|||
|
|
@ -9,31 +9,41 @@ use AdvanceBonus::*;
|
|||
|
||||
pub struct Technology {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub advance_bonus: Option<AdvanceBonus>,
|
||||
pub required_technology: Option<String>,
|
||||
pub contradicting_technology: Option<String>,
|
||||
pub unlocked_building: Option<String>,
|
||||
pub player_initializer: PlayerInitializer,
|
||||
pub player_deinitializer: PlayerInitializer,
|
||||
}
|
||||
|
||||
impl Technology {
|
||||
pub fn builder(name: &str, advance_bonus: Option<AdvanceBonus>) -> TechnologyBuilder {
|
||||
TechnologyBuilder::new(name.to_string(), advance_bonus)
|
||||
pub fn builder(
|
||||
name: &str,
|
||||
description: &str,
|
||||
advance_bonus: Option<AdvanceBonus>,
|
||||
) -> TechnologyBuilder {
|
||||
TechnologyBuilder::new(name.to_string(), description.to_string(), advance_bonus)
|
||||
}
|
||||
|
||||
fn new(
|
||||
name: String,
|
||||
description: String,
|
||||
advance_bonus: Option<AdvanceBonus>,
|
||||
required_technology: Option<String>,
|
||||
contradicting_technology: Option<String>,
|
||||
unlocked_building: Option<String>,
|
||||
player_initializer: PlayerInitializer,
|
||||
player_deinitializer: PlayerInitializer,
|
||||
) -> Self {
|
||||
Self {
|
||||
name,
|
||||
description,
|
||||
advance_bonus,
|
||||
required_technology,
|
||||
contradicting_technology,
|
||||
unlocked_building,
|
||||
player_initializer,
|
||||
player_deinitializer,
|
||||
}
|
||||
|
|
@ -48,20 +58,24 @@ impl PartialEq for Technology {
|
|||
|
||||
pub struct TechnologyBuilder {
|
||||
name: String,
|
||||
description: String,
|
||||
advance_bonus: Option<AdvanceBonus>,
|
||||
required_technology: Option<String>,
|
||||
contradicting_technology: Option<String>,
|
||||
unlocked_building: Option<String>,
|
||||
initializers: Vec<PlayerInitializer>,
|
||||
deinitializers: Vec<PlayerInitializer>,
|
||||
}
|
||||
|
||||
impl TechnologyBuilder {
|
||||
fn new(name: String, advance_bonus: Option<AdvanceBonus>) -> Self {
|
||||
fn new(name: String, description: String, advance_bonus: Option<AdvanceBonus>) -> Self {
|
||||
Self {
|
||||
name,
|
||||
description,
|
||||
advance_bonus,
|
||||
required_technology: None,
|
||||
contradicting_technology: None,
|
||||
unlocked_building: None,
|
||||
initializers: Vec::new(),
|
||||
deinitializers: Vec::new(),
|
||||
}
|
||||
|
|
@ -82,9 +96,11 @@ impl TechnologyBuilder {
|
|||
let deinitializer = player::join_player_initializers(self.deinitializers);
|
||||
Technology::new(
|
||||
self.name,
|
||||
self.description,
|
||||
self.advance_bonus,
|
||||
self.required_technology,
|
||||
self.contradicting_technology,
|
||||
self.unlocked_building,
|
||||
initializer,
|
||||
deinitializer,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,17 +1,17 @@
|
|||
pub const WONDER_VICTORY_POINTS: f32 = 4.0;
|
||||
|
||||
use std::fmt::Display;
|
||||
|
||||
use crate::{
|
||||
hexagon::Hexagon,
|
||||
hexagon::HexagonPosition,
|
||||
player::{self, PlayerInitializer, PlayerSetup},
|
||||
resource_pile::ResourcePile,
|
||||
};
|
||||
|
||||
type PlacementChecker = Box<dyn Fn(&Hexagon) -> bool>;
|
||||
//todo! provide more info for position
|
||||
type PlacementChecker = Box<dyn Fn(&HexagonPosition) -> bool>;
|
||||
|
||||
pub struct Wonder {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub cost: ResourcePile,
|
||||
pub required_technologies: Vec<String>,
|
||||
pub placement_requirement: Option<PlacementChecker>,
|
||||
|
|
@ -38,6 +38,7 @@ impl Wonder {
|
|||
|
||||
pub struct WonderBuilder {
|
||||
name: String,
|
||||
descriptions: Vec<String>,
|
||||
cost: ResourcePile,
|
||||
required_technologies: Vec<String>,
|
||||
placement_requirement: Option<PlacementChecker>,
|
||||
|
|
@ -49,6 +50,7 @@ impl WonderBuilder {
|
|||
fn new(name: String, cost: ResourcePile, required_technologies: Vec<String>) -> Self {
|
||||
Self {
|
||||
name,
|
||||
descriptions: Vec::new(),
|
||||
cost,
|
||||
required_technologies,
|
||||
placement_requirement: None,
|
||||
|
|
@ -57,6 +59,11 @@ impl WonderBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn add_description(mut self, description: String) -> Self {
|
||||
self.descriptions.push(description);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn placement_requirement(&mut self, placement_requirement: PlacementChecker) -> &mut Self {
|
||||
self.placement_requirement = Some(placement_requirement);
|
||||
self
|
||||
|
|
@ -67,6 +74,7 @@ impl WonderBuilder {
|
|||
let player_deinitializer = player::join_player_initializers(self.player_deinitializers);
|
||||
Wonder {
|
||||
name: self.name,
|
||||
description: String::from("● ") + &self.descriptions.join("\n● "),
|
||||
cost: self.cost,
|
||||
required_technologies: self.required_technologies,
|
||||
placement_requirement: self.placement_requirement,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue