player is now serializable and deserializable

This commit is contained in:
AlphaCodingPilot 2023-01-02 15:13:57 +01:00
commit bae0ff6301
11 changed files with 320 additions and 28 deletions

View file

@ -1,9 +1,13 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub enum Unit {
Ship,
ArmyUnit(ArmyUnit),
Settler,
}
#[derive(Serialize, Deserialize)]
pub enum ArmyUnit {
Infantry,
Cavalry,

View file

@ -1,9 +1,11 @@
use crate::{
content::wonders,
player::Player,
resource_pile::ResourcePile,
wonder::{Wonder, WONDER_VICTORY_POINTS},
};
use serde::{Deserialize, Serialize};
use Building::*;
use MoodState::*;
@ -27,12 +29,30 @@ pub struct City {
}
impl City {
pub fn from_data(data: CityData) -> Self {
Self {
buildings: Buildings::from_data(data.buildings),
mood_state: data.mood_state,
is_activated: data.is_activated,
player: data.player,
}
}
pub fn to_data(self) -> CityData {
CityData::new(
self.buildings.to_data(),
self.mood_state,
self.is_activated,
self.player,
)
}
pub fn new(player: String) -> Self {
Self {
buildings: Buildings::default(),
mood_state: Neutral,
is_activated: false,
player
player,
}
}
@ -123,6 +143,30 @@ impl City {
}
}
#[derive(Serialize, Deserialize)]
pub struct CityData {
buildings: BuildingsData,
mood_state: MoodState,
is_activated: bool,
player: String,
}
impl CityData {
pub fn new(
buildings: BuildingsData,
mood_state: MoodState,
is_activated: bool,
player: String,
) -> Self {
Self {
buildings,
mood_state,
is_activated,
player,
}
}
}
pub enum Building {
Academy,
Market,
@ -136,17 +180,65 @@ pub enum Building {
#[derive(Default)]
pub struct Buildings {
academy: Option<String>,
market: Option<String>,
obelisk: Option<String>,
apothecary: Option<String>,
fortress: Option<String>,
port: Option<String>,
temple: Option<String>,
wonder: Option<Wonder>,
pub academy: Option<String>,
pub market: Option<String>,
pub obelisk: Option<String>,
pub apothecary: Option<String>,
pub fortress: Option<String>,
pub port: Option<String>,
pub temple: Option<String>,
pub wonder: Option<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 {
Self {
academy,
market,
obelisk,
apothecary,
fortress,
port,
temple,
wonder,
}
}
fn can_add_building(&self, building: &Building) -> bool {
match building {
Academy => self.academy.is_none(),
@ -224,6 +316,43 @@ impl Buildings {
}
}
#[derive(Serialize, Deserialize)]
pub struct BuildingsData {
academy: Option<String>,
market: Option<String>,
obelisk: Option<String>,
apothecary: 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,
}
}
}
#[derive(Serialize, Deserialize)]
pub enum MoodState {
Happy,
Neutral,

3
server/src/content.rs Normal file
View file

@ -0,0 +1,3 @@
pub mod civilizations;
pub mod technologies;
pub mod wonders;

View file

@ -0,0 +1,13 @@
use crate::{civilization::Civilization, leader::Leader};
pub fn get_civilizations() -> Vec<Civilization> {
todo!()
}
pub fn get_civilization_by_name(name: String) -> Civilization {
todo!()
}
pub fn get_leader_by_name(name: String, civilization: String) -> Leader {
todo!()
}

View file

@ -0,0 +1,9 @@
use crate::technology::Technology;
pub fn get_technologies() -> Vec<Technology> {
todo!()
}
pub fn get_technology_by_name(name: String) -> Technology {
todo!()
}

View file

@ -0,0 +1,9 @@
use crate::wonder::Wonder;
pub fn get_wonders() -> Vec<Wonder> {
todo!()
}
pub fn get_wonder_by_name(name: String) -> Wonder {
todo!()
}

View file

@ -27,60 +27,60 @@ pub async extern "C" fn init(
seed: String,
creator: Option<usize>,
) -> GameJson {
toJson(GameData {});
to_json(GameData {});
todo!()
}
#[export_name = "move"]
pub extern "C" fn execute_move(json: GameJson, move_data: Move, player: usize) -> GameJson {
let data = fromJson(json);
toJson(data);
let data = from_json(json);
to_json(data);
todo!()
}
#[export_name = "ended"]
pub extern "C" fn ended(json: GameJson) -> bool {
let data = fromJson(json);
let data = from_json(json);
todo!()
}
#[export_name = "scores"]
pub extern "C" fn scores(json: GameJson) -> Vec<u32> {
let data = fromJson(json);
let data = from_json(json);
todo!()
}
#[export_name = "dropPlayer"]
pub async extern "C" fn drop_player(json: GameJson, player: usize) -> GameJson {
let data = fromJson(json);
toJson(data);
let data = from_json(json);
to_json(data);
todo!()
}
#[export_name = "currentPlayer"]
pub async extern "C" fn current_player(json: GameJson) -> Option<usize> {
let data = fromJson(json);
let data = from_json(json);
todo!()
}
#[export_name = "logLength"]
pub async extern "C" fn log_length(json: GameJson) -> u32 {
let data = fromJson(json);
let data = from_json(json);
todo!()
}
#[export_name = "logSlice"]
pub async extern "C" fn log_slice(json: GameJson, options: LogSliceOptions) -> LogData {
let data = fromJson(json);
let data = from_json(json);
todo!()
}
fn fromJson(data: GameJson) -> GameData {
let game: GameData = serde_json::from_str(&data).unwrap();
return game;
fn from_json(data: GameJson) -> GameData {
let game: GameData =
serde_json::from_str(&data).expect("API call should receive valid game data json");
game
}
fn toJson(game: GameData) -> GameJson {
return serde_json::to_string(&game).unwrap();
fn to_json(game: GameData) -> GameJson {
serde_json::to_string(&game).expect("game data should be valid json")
}

View file

@ -1,3 +1,5 @@
use serde::{Deserialize, Serialize};
use crate::landmark::Landmark;
pub struct Hexagon {
@ -16,6 +18,7 @@ impl Hexagon {
}
}
#[derive(Serialize, Deserialize)]
pub struct HexagonPosition {
pub x: i32,
pub y: i32,

View file

@ -1,6 +1,7 @@
mod army;
mod city;
mod civilization;
mod content;
mod events;
mod game_api;
mod hexagon;

View file

@ -1,3 +1,4 @@
use serde::{Deserialize, Serialize};
use std::{
collections::{HashMap, VecDeque},
fmt::Display,
@ -5,8 +6,9 @@ use std::{
use crate::{
army::Unit,
city::{Building, City},
city::{Building, City, CityData},
civilization::Civilization,
content::{civilizations, technologies},
events::EventMut,
hexagon::HexagonPosition,
leader::Leader,
@ -35,6 +37,73 @@ pub struct Player {
}
impl Player {
pub fn from_data(data: PlayerData) -> Self {
let mut player = Self {
name: data.name,
resources: data.resources,
resource_limit: data.resource_limit,
events: Some(PlayerEvents::default()),
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()),
active_leader: data
.active_leader
.map(|leader| civilizations::get_leader_by_name(leader, data.civilization.clone())),
available_leaders: data
.available_leaders
.into_iter()
.map(|leader| civilizations::get_leader_by_name(leader, data.civilization.clone()))
.collect(),
researched_technologies: data.researched_technologies,
leader_position: data.leader_position,
event_tokens: data.event_tokens,
victory_points: data.victory_points,
};
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);
}
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);
for city in cities.iter_mut() {
if let Some(wonder) = city.buildings.wonder.take() {
(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
.into_iter()
.map(|leader| leader.name)
.collect(),
self.researched_technologies,
self.leader_position,
self.event_tokens,
self.victory_points,
)
}
pub fn new(name: &str, mut civilization: Civilization) -> Self {
let mut leaders = Vec::new();
leaders.append(&mut civilization.leaders);
@ -116,7 +185,9 @@ impl Player {
if self.civilization.special_technologies[i].required_technology == technology.name {
let special_technology = self.civilization.special_technologies.remove(i);
self.unlock_special_technology(&special_technology);
self.civilization.special_technologies.insert(i, special_technology);
self.civilization
.special_technologies
.insert(i, special_technology);
break;
}
}
@ -179,6 +250,54 @@ impl Player {
}
}
#[derive(Serialize, Deserialize)]
pub struct PlayerData {
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,
}
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,
}
}
}
#[derive(Default)]
pub struct PlayerEvents {
pub some_event: EventMut<i32, String>,

View file

@ -3,7 +3,9 @@ use std::{
ops::{Add, AddAssign, SubAssign},
};
#[derive(Default, Clone)]
use serde::{Deserialize, Serialize};
#[derive(Default, Clone, Serialize, Deserialize)]
pub struct ResourcePile {
pub wood: u32,
pub stone: u32,