Skip to content

Commit

Permalink
Added frontend refs
Browse files Browse the repository at this point in the history
  • Loading branch information
TCA166 committed May 2, 2024
1 parent f59d8ca commit fa3ba17
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/structures/character.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::cell::Ref;
use std::cell::{Ref, RefCell};
use std::rc::Rc;

use minijinja::{Environment, context};

Expand All @@ -10,7 +11,7 @@ use crate::game_state::GameState;

use super::renderer::Renderable;

use super::{Cullable, Culture, Dynasty, Faith, GameObjectDerived, Memory, Shared, Title};
use super::{Cullable, CultureRef, Dynasty, Faith, GameObjectDerived, Memory, Shared, Title};

pub struct Character {
pub id: u32,
Expand All @@ -20,8 +21,8 @@ pub struct Character {
pub dead: bool,
pub date: Option<Shared<String>>,
pub reason: Option<Shared<String>>,
pub faith: Shared<Faith>,
pub culture: Shared<Culture>,
pub faith: Option<Shared<Faith>>,
pub culture: Option<Shared<CultureRef>>,
pub house: Option<Shared<Dynasty>>,
pub skills: Vec<i8>,
pub traits: Vec<Shared<String>>,
Expand Down Expand Up @@ -56,7 +57,7 @@ fn get_faith(house:&Option<Shared<Dynasty>>, base:&GameObject, game_state:&mut G
if h.parent.is_some(){
let p = h.parent.as_ref().unwrap().borrow();
if p.leaders.len() != 0 {
return p.leaders[0].borrow().faith.clone();
return p.leaders[0].borrow().faith.as_ref().unwrap().clone();
}
else{
panic!("No faith found");
Expand All @@ -70,27 +71,28 @@ fn get_faith(house:&Option<Shared<Dynasty>>, base:&GameObject, game_state:&mut G
loop {
let l = h.leaders[i].try_borrow();
if l.is_ok(){
return l.unwrap().faith.clone();
return l.unwrap().faith.as_ref().unwrap().clone();
}
i += 1;
}
}
}
}

fn get_culture(house:&Option<Shared<Dynasty>>, base:&GameObject, game_state:&mut GameState) -> Shared<Culture>{
fn get_culture(house:&Option<Shared<Dynasty>>, base:&GameObject, game_state:&mut GameState) -> Shared<CultureRef>{
let culture_node = base.get("culture");
if culture_node.is_some(){
return game_state.get_culture(culture_node.unwrap().as_string_ref().unwrap().as_str()).clone();
let r = CultureRef::from_derived(game_state.get_culture(culture_node.unwrap().as_string_ref().unwrap().as_str()).clone());
return Rc::new(RefCell::new(r));
}
else{
let h = house.as_ref().unwrap().borrow();
if h.leaders.len() == 0 {
let p = h.parent.as_ref().unwrap().borrow();
return p.leaders[0].borrow().culture.clone();
return p.leaders[0].borrow().culture.as_ref().unwrap().clone();
}
else{
return h.leaders[0].borrow().culture.clone();
return h.leaders[0].borrow().culture.as_ref().unwrap().clone();
}
}
}
Expand Down Expand Up @@ -279,8 +281,8 @@ impl GameObjectDerived for Character {
false => None
},
house: house.clone(),
faith: get_faith(&house, &base, game_state),
culture: get_culture(&house, &base, game_state),
faith: Some(get_faith(&house, &base, game_state)),
culture: Some(get_culture(&house, &base, game_state)),
skills: skills,
traits: traits,
recessive:recessive,
Expand Down Expand Up @@ -311,8 +313,8 @@ impl GameObjectDerived for Character {
dead: false,
date: None,
reason: None,
faith: Shared::new(Faith::dummy(0).into()),
culture: Shared::new(Culture::dummy(0).into()),
faith: None,
culture: None,
house: None,
skills: Vec::new(),
traits: Vec::new(),
Expand Down Expand Up @@ -373,14 +375,18 @@ impl GameObjectDerived for Character {
false => None
};
self.house = house.clone();
self.faith.clone_from(&get_faith(&house, &base, game_state));
self.culture.clone_from(&get_culture(&house, &base, game_state));
self.faith = Some(get_faith(&house, &base, game_state));
self.culture = Some(get_culture(&house, &base, game_state));
self.dna = dna;
}

fn get_id(&self) -> u32 {
self.id
}

fn get_name(&self) -> Shared<String> {
self.name.clone()
}
}

// TODO possible astronomical cost of serializing? is it possible we serialize some things multiple times?
Expand Down Expand Up @@ -458,8 +464,8 @@ impl Cullable for Character {
for s in self.vassals.iter_mut(){
s.borrow_mut().set_depth(depth - 1);
}
self.culture.borrow_mut().set_depth(depth - 1);
self.faith.borrow_mut().set_depth(depth - 1);
self.culture.as_ref().unwrap().borrow_mut().set_depth(depth - 1);
self.faith.as_ref().unwrap().borrow_mut().set_depth(depth - 1);
for s in self.titles.iter_mut(){
s.borrow_mut().set_depth(depth - 1);
}
Expand Down
45 changes: 45 additions & 0 deletions src/structures/culture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ impl GameObjectDerived for Culture {
fn get_id(&self) -> u32 {
self.id
}

fn get_name(&self) -> Shared<String> {
self.name.clone()
}
}

impl Serialize for Culture {
Expand Down Expand Up @@ -131,3 +135,44 @@ impl Cullable for Culture {
}
}
}

/// A struct representing a shallow reference to a culture
/// This is used to avoid infinite recursion when serializing cultures
pub struct CultureRef{
pub id: u32,
pub name: Shared<String>,
culture: Shared<Culture>
}

impl Serialize for CultureRef{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut state = serializer.serialize_struct("CultureRef", 2)?;
state.serialize_field("id", &self.id)?;
state.serialize_field("name", &self.name)?;
state.end()
}
}

impl Cullable for CultureRef{
fn get_depth(&self) -> usize {
self.culture.borrow().get_depth()
}

fn set_depth(&mut self, depth:usize) {
self.culture.borrow_mut().set_depth(depth);
}
}

impl CultureRef{
pub fn from_derived(culture:Shared<Culture>) -> Self{
let c = culture.borrow();
CultureRef{
id: c.id,
name: c.name.clone(),
culture: culture.clone()
}
}
}
47 changes: 47 additions & 0 deletions src/structures/derived_ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use serde::Serialize;
use serde::ser::SerializeStruct;

use super::{Cullable, GameObjectDerived, Shared};

struct DerivedRef<T> where T:GameObjectDerived + Cullable{
pub id: u32,
pub name: Shared<String>,
obj: T
}

impl<T> DerivedRef<T> where T:GameObjectDerived + Cullable{
pub fn new(id:u32, name:Shared<String>, obj:T) -> Self{
DerivedRef{
id,
name,
obj
}
}

pub fn from_derived(obj:T) -> Self{
DerivedRef{
id: obj.get_id(),
name: obj.get_name(),
obj
}
}
}

impl<T> Serialize for DerivedRef<T> where T:GameObjectDerived + Cullable{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
let mut state = serializer.serialize_struct("DerivedRef", 3)?;
state.serialize_field("id", &self.id)?;
state.serialize_field("name", &self.name)?;
state.end()
}
}

impl<T> Cullable for DerivedRef<T> where T:GameObjectDerived + Cullable{
fn get_depth(&self) -> usize {
self.obj.get_depth()
}

fn set_depth(&mut self, depth:usize) {
self.obj.set_depth(depth);
}
}
4 changes: 4 additions & 0 deletions src/structures/dynasty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ impl GameObjectDerived for Dynasty {
fn get_id(&self) -> u32 {
self.id
}

fn get_name(&self) -> Shared<String> {
self.name.as_ref().unwrap().clone()
}
}

impl Serialize for Dynasty {
Expand Down
4 changes: 4 additions & 0 deletions src/structures/faith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ impl GameObjectDerived for Faith {
fn get_id(&self) -> u32 {
self.id
}

fn get_name(&self) -> Shared<String> {
self.name.clone()
}
}

impl Serialize for Faith {
Expand Down
4 changes: 4 additions & 0 deletions src/structures/lineage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ impl GameObjectDerived for LineageNode{
fn get_id(&self) -> u32 {
self.id
}

fn get_name(&self) -> Shared<String> {
self.character.as_ref().unwrap().borrow().get_name()
}
}

impl Serialize for LineageNode{
Expand Down
4 changes: 4 additions & 0 deletions src/structures/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ impl GameObjectDerived for Memory {
fn get_id(&self) -> u32 {
self.id
}

fn get_name(&self) -> Shared<String> {
self.r#type.clone()
}
}

impl Serialize for Memory {
Expand Down
6 changes: 6 additions & 0 deletions src/structures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub use faith::Faith;

mod culture;
pub use culture::Culture;
pub use culture::CultureRef;

mod dynasty;
pub use dynasty::Dynasty;
Expand All @@ -31,6 +32,8 @@ pub use title::Title;
mod lineage;
pub use lineage::LineageNode;

mod derived_ref;

/// A type alias for shared objects.
/// Aliases: [std::rc::Rc]<[std::cell::RefCell]<>>
///
Expand Down Expand Up @@ -63,4 +66,7 @@ pub trait GameObjectDerived{
/// Within a given section that number is unique.
/// For example, all characters have a unique id, but a title and a character can have the same id.
fn get_id(&self) -> u32;

/// Get the name of the object.
fn get_name(&self) -> Shared<String>;
}
4 changes: 4 additions & 0 deletions src/structures/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ impl GameObjectDerived for Player {
fn get_id(&self) -> u32 {
self.id
}

fn get_name(&self) -> Shared<String> {
self.name.clone()
}
}

impl Serialize for Player{
Expand Down
5 changes: 5 additions & 0 deletions src/structures/title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ impl GameObjectDerived for Title{
self.history = history;

}

fn get_name(&self) -> Shared<String> {
self.name.clone()

}
}

impl Serialize for Title {
Expand Down

0 comments on commit fa3ba17

Please sign in to comment.