Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijan committed Feb 7, 2025
1 parent 33477fb commit d7eec27
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
24 changes: 21 additions & 3 deletions entities/src/dao/char_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::entities::{character, item};
use chrono::Utc;
use sea_orm::DbErr;
use serde_json::Value;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -128,7 +127,7 @@ pub struct CharacterInfo {

#[allow(clippy::missing_errors_doc)]
impl CharacterInfo {
pub fn new(char_model: character::Model, items: Vec<item::Model>) -> Result<Self, DbErr> {
pub fn new(char_model: character::Model, items: Vec<item::Model>) -> anyhow::Result<Self> {
let paperdoll = char_model.restore_visible_inventory(&items)?;
Ok(Self {
char_model,
Expand Down Expand Up @@ -228,6 +227,11 @@ impl CharacterInfo {
pub fn get_max_mp(&self) -> f64 {
self.char_model.max_mp
}

#[must_use]
pub fn get_max_cp(&self) -> f64 {
self.char_model.max_cp
}
#[must_use]
#[allow(clippy::cast_possible_truncation)]
pub fn get_delete_timer(&self) -> i32 {
Expand Down Expand Up @@ -353,7 +357,7 @@ impl TryFrom<u8> for Race {
#[cfg(test)]
mod tests {
use super::*;
use crate::dao::item::{ItemVariations, LocType};
use crate::dao::item::{ItemVariables, ItemVariations, LocType};
use crate::test_factories::factories::{char_factory, item_factory, user_factory};
use serde_json::json;
use test_utils::utils::get_test_db;
Expand Down Expand Up @@ -468,8 +472,12 @@ mod tests {
let now = Utc::now();
let char = char_factory(&db_pool, |mut ch| {
ch.user_id = user.id;
ch.max_hp = 300f64;
ch.max_mp = 400f64;
ch.max_cp = 500f64;
ch.variables = json!({
CharVariables::VitalityItemsUsed.as_key(): 10,
CharVariables::VisualFaceId.as_key(): 3,
CharVariables::HairAccessoryEnabled.as_key(): false,
CharVariables::VisualHairColorId.as_key(): 4,
CharVariables::VisualHairStyleId.as_key(): 3,
Expand All @@ -493,6 +501,9 @@ mod tests {
it.item_id = 2;
it.count = 1;
it.enchant_level = 0;
it.variables = json!({
ItemVariables::VisualId.as_key(): 3
});
it.time_of_use = 0;
it.loc = LocType::Paperdoll;
it.loc_data = PaperDoll::Hair as i32;
Expand All @@ -507,6 +518,13 @@ mod tests {
assert_eq!(weapon.id, 1);
assert!(!char_info.is_hair_accessory_enabled());
assert_eq!(char_info.get_hair_color(), 4);
assert_eq!(char_info.get_face(), 3);
assert_eq!(char_info.get_max_hp().to_bits(), 300f64.to_bits());
assert_eq!(char_info.get_max_mp().to_bits(), 400f64.to_bits());
assert_eq!(char_info.get_max_cp().to_bits(), 500f64.to_bits());
assert_eq!(char_info.try_get_paper_doll_visual_id(PaperDoll::Hair).unwrap(), 3);
assert_eq!(char_info.try_get_paper_doll_item_id(PaperDoll::RHand).unwrap(), 2);
assert_eq!(char_info.get_enchant_effect(PaperDoll::RHand), 0);
assert_eq!(char_info.get_hair_style(), 3);
assert_eq!(char_info.get_transform_id(), 2);
assert_eq!(char_info.get_delete_timer(), 0);
Expand Down
16 changes: 8 additions & 8 deletions entities/src/dao/character.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::dao::char_info::CharacterInfo;
use crate::dao::item::LocType;
use crate::dao::item::{ItemVariables, LocType};
use crate::entities::{character, item, user};
use crate::DBPool;
use chrono::{Duration, Utc};
Expand Down Expand Up @@ -60,7 +60,7 @@ impl character::Model {
db_pool: &DBPool,
username: &str,
loc_type: LocType,
) -> Result<Vec<CharacterInfo>, DbErr> {
) -> anyhow::Result<Vec<CharacterInfo>> {
let characters = character::Entity::find()
.order_by(character::Column::DeleteAt, Order::Asc)
.order_by(character::Column::CreatedAt, Order::Asc)
Expand Down Expand Up @@ -92,19 +92,19 @@ impl character::Model {
pub fn restore_visible_inventory(
&self,
items: &Vec<item::Model>,
) -> Result<[[i32; 4]; 33], DbErr> {
) -> anyhow::Result<[[i32; 4]; 33]> {
let mut result = [[0; 4]; 33];
for item in items {
if item.loc == LocType::Paperdoll {
let slot = item.loc_data;
result[slot as usize][0] = item.id;
result[slot as usize][1] = item.item_id;
result[slot as usize][2] = item.enchant_level;
// todo: result[slot as usize][3] = vars
// .get(item_variable::Model::VISUAL_ID)
// .unwrap_or(&"0".to_string())
// .parse()
// .unwrap_or(0);
result[slot as usize][3] = i32::try_from(item
.variables
.get(ItemVariables::VisualId.as_key())
.and_then(serde_json::Value::as_i64)
.unwrap_or(0))?;
if result[slot as usize][3] > 0 {
// fix for hair appearance conflicting with original model
result[slot as usize][1] = result[slot as usize][3];
Expand Down
22 changes: 17 additions & 5 deletions entities/src/dao/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,30 @@ impl ItemVariations {
#[must_use]
pub fn as_key(&self) -> &'static str {
match self {
ItemVariations::MineralId => "mineralId",
ItemVariations::MineralId => "mineral_id",
ItemVariations::Option1 => "option1",
ItemVariations::Option2 => "option2",
}
}
}

#[derive(Debug, Clone)]
pub enum ItemVariables{
VisualId,
VisualAppearanceStoneId,
VisualAppearanceLifeTime
}
impl ItemVariables {
#[must_use]
pub fn as_key(&self) -> &'static str {
match self {
ItemVariables::VisualId => "visual_id",
ItemVariables::VisualAppearanceStoneId => "visual_appearance_stone_id",
ItemVariables::VisualAppearanceLifeTime => "visual_appearance_lifetime",
}
}
}
#[allow(clippy::missing_errors_doc)]
impl item::Model {
pub const VISUAL_ID: &'static str = "visualId";
pub const VISUAL_APPEARANCE_STONE_ID: &'static str = "visualAppearanceStoneId";
pub const VISUAL_APPEARANCE_LIFE_TIME: &'static str = "visualAppearanceLifetime";
///
/// # returns
/// tuple where:
Expand Down

0 comments on commit d7eec27

Please sign in to comment.