Skip to content

Commit

Permalink
Added lineages
Browse files Browse the repository at this point in the history
  • Loading branch information
TCA166 committed Apr 30, 2024
1 parent fd3d94a commit b1d1681
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 5 deletions.
42 changes: 41 additions & 1 deletion src/save_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,14 @@ impl Section{
}
else{ // we pop the inner object and insert it into the outer object
let inner = stack.pop().unwrap();
stack.last_mut().unwrap().insert(inner.get_name().to_string(), SaveFileValue::Object(Rc::new(RefCell::new(inner))));
let name = inner.get_name().to_string();
let val = SaveFileValue::Object(Rc::new(RefCell::new(inner)));
if name.is_empty(){ //check if unnamed node, implies we are dealing with an array of unnamed objects
stack.last_mut().unwrap().push(val);
}
else{
stack.last_mut().unwrap().insert(name, val);
}
}
}
'"' => { // we have a quote, we toggle the quotes flag
Expand Down Expand Up @@ -372,6 +379,39 @@ mod tests {
assert_eq!(test2.get_array_iter().len(), 3);
}

#[test]
fn test_unnamed_obj(){
let mut file = NamedTempFile::new().unwrap();
file.write_all(b"
3623={
name=\"dynn_Sao\"
variables={
data={
{
flag=\"ai_random_harm_cooldown\"
tick=7818
data={
type=boolean
identity=1
}
}
{
something_else=\"test\"
}
}
}
}
}
").unwrap();
let mut save_file = super::SaveFile::new(file.path().to_str().unwrap());
let object = save_file.next().unwrap().to_object().unwrap();
let variables = object.get_object_ref("variables");
let data = variables.get_object_ref("data");
assert!(!data.is_empty());
assert_ne!(data.get_array_iter().len(), 0)

}

#[test]
fn test_example_1(){
let mut file = NamedTempFile::new().unwrap();
Expand Down
153 changes: 153 additions & 0 deletions src/structures/lineage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
use std::cell::Ref;

use serde::Serialize;
use serde::ser::SerializeStruct;

use crate::game_object::GameObject;
use crate::game_state::GameState;

use super::{Character, Dynasty, GameObjectDerived, Shared};

pub struct LineageNode{
pub character: Option<Shared<Character>>,
pub date: Shared<String>,
pub score: i32,
pub prestige: i32,
pub piety: i32,
pub dread:f32,
pub lifestyle: Shared<String>,
pub perks:Vec<Shared<String>>,
pub id: u32
}

fn get_perks(perks:&mut Vec<Shared<String>>, base:&Ref<'_, GameObject>){
let perks_node = base.get("perks");
if perks_node.is_some(){
perks.push(perks_node.unwrap().as_string());
}
}

fn get_dread(base:&Ref<'_, GameObject>) -> f32{
let dread;
let dread_node = base.get("dread");
if dread_node.is_some() {
dread = dread_node.unwrap().as_string_ref().unwrap().parse::<f32>().unwrap();
}
else{
dread = 0.0;
}
return dread;
}

fn get_score(base: &Ref<'_, GameObject>) -> i32 {
let score;
let score_node = base.get("score");
if score_node.is_some() {
score = score_node.unwrap().as_string_ref().unwrap().parse::<i32>().unwrap();
} else {
score = 0;
}
score
}

fn get_prestige(base: &Ref<'_, GameObject>) -> i32 {
let prestige;
let prestige_node = base.get("prestige");
if prestige_node.is_some() {
prestige = prestige_node.unwrap().as_string_ref().unwrap().parse::<i32>().unwrap();
} else {
prestige = 0;
}
prestige
}

fn get_piety(base: &Ref<'_, GameObject>) -> i32 {
let piety;
let piety_node = base.get("piety");
if piety_node.is_some() {
piety = piety_node.unwrap().as_string_ref().unwrap().parse::<i32>().unwrap();
}
else{
piety = 0;
}
piety
}

fn get_lifestyle(base: &Ref<'_, GameObject>) -> Shared<String>{
let lifestyle_node = base.get("lifestyle");
if lifestyle_node.is_some() {
lifestyle_node.unwrap().as_string()
} else {
Shared::new(String::new().into())
}
}

impl GameObjectDerived for LineageNode{
fn from_game_object(base:Ref<'_, GameObject>, game_state:&mut GameState) -> Self {
let id = base.get_string_ref("character");
let char = game_state.get_character(id.as_str());
let mut perks: Vec<Shared<String>> = Vec::new();
get_perks(&mut perks, &base);
println!("{:?}", base);
LineageNode {
character: Some(char),
date: base.get("date").unwrap().as_string(),
score: get_score(&base),
prestige: get_prestige(&base),
piety: get_piety(&base),
dread: get_dread(&base),
lifestyle: get_lifestyle(&base),
perks: perks,
id: id.parse::<u32>().unwrap()
}
}

fn dummy(id:u32) -> Self {
LineageNode{
character: None,
date: Shared::new(String::new().into()),
score: 0,
prestige: 0,
piety: 0,
dread: 0.0,
lifestyle: Shared::new(String::new().into()),
perks: Vec::new(),
id: id
}
}

fn init(&mut self, base:Ref<'_, GameObject>, game_state:&mut GameState) {
let character_id = base.get_string_ref("character");
self.character = Some(game_state.get_character(character_id.as_str()));
self.score = get_score(&base);
self.prestige = get_prestige(&base);
self.piety = get_piety(&base);
self.dread = get_dread(&base);
self.lifestyle = get_lifestyle(&base);
self.perks.clear();
get_perks(&mut self.perks, &base);
}

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

impl Serialize for LineageNode{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut state = serializer.serialize_struct("LineageNode", 11)?;
state.serialize_field("character", &self.character)?;
state.serialize_field("date", &self.date)?;
state.serialize_field("score", &self.score)?;
state.serialize_field("prestige", &self.prestige)?;
state.serialize_field("piety", &self.piety)?;
state.serialize_field("dread", &self.dread)?;
state.serialize_field("lifestyle", &self.lifestyle)?;
state.serialize_field("perks", &self.perks)?;
state.serialize_field("id", &self.id)?;
state.end()
}
}
3 changes: 3 additions & 0 deletions src/structures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub use memory::Memory;
mod title;
pub use title::Title;

mod lineage;
pub use lineage::LineageNode;

/// A type alias for shared objects.
/// Aliases: [std::rc::Rc]<[std::cell::RefCell]<>>
///
Expand Down
39 changes: 35 additions & 4 deletions src/structures/player.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,74 @@

use std::{cell::Ref, rc::Rc};

use serde::Serialize;
use serde::ser::SerializeStruct;

use crate::game_object::GameObject;

use crate::game_state::GameState;

use super::{Character, GameObjectDerived, Shared};
use super::{Character, GameObjectDerived, Shared, LineageNode};

pub struct Player {
pub name: Shared<String>,
pub id: u32,
pub character: Option<Shared<Character>>
pub character: Option<Shared<Character>>,
pub lineage: Vec<LineageNode>,
}

fn get_lineage(lineage: &mut Vec<LineageNode>, base: &Ref<'_, GameObject>, game_state: &mut GameState){
let lineage_node = base.get_object_ref("legacy");
for leg in lineage_node.get_array_iter(){
let o = leg.as_object_ref().unwrap();
lineage.push(LineageNode::from_game_object(o, game_state))
}
}

impl GameObjectDerived for Player {
fn from_game_object(base: Ref<'_, GameObject>, game_state: &mut GameState) -> Self {
let mut lineage: Vec<LineageNode> = Vec::new();
get_lineage(&mut lineage, &base, game_state);
let key = base.get("character").unwrap().as_string_ref().unwrap();
Player {
name: base.get("name").unwrap().as_string(),
id: base.get("player").unwrap().as_string_ref().unwrap().parse::<u32>().unwrap(),
character: Some(Rc::from(game_state.get_character(&key).clone()))
character: Some(Rc::from(game_state.get_character(&key).clone())),
lineage: lineage
}
}

fn dummy(id:u32) -> Self {
Player {
name: Rc::new("".to_owned().into()),
id: id,
character: None
character: None,
lineage: Vec::new()
}
}

fn init(&mut self, base: Ref<'_, GameObject>, game_state: &mut GameState) {
let key = base.get("character").unwrap().as_string_ref().unwrap();
self.character = Some(Rc::from(game_state.get_character(&key).clone()));
self.id = base.get("player").unwrap().as_string_ref().unwrap().parse::<u32>().unwrap();
get_lineage(&mut self.lineage, &base, game_state);
}

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

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

0 comments on commit b1d1681

Please sign in to comment.