sb-core: DomainError + signature conversions (WI-2, issue #2)

This commit is contained in:
Jan-Peter von Hunnius 2026-10-03 12:00:11 +02:00
parent 093012af20
commit 423997f2d7
5 changed files with 76 additions and 38 deletions

View file

@ -1,4 +1,5 @@
use crate::constants;
use crate::error::DomainError;
use crate::spells;
use sb_shared::{Character, CharacterStats, Class, Race};
@ -351,17 +352,17 @@ pub fn level_up_cost(level: i32) -> i32 {
constants::LEVEL_UP_GOLD_PER_LEVEL * level
}
pub fn level_up(char: &mut Character) -> Result<i32, &'static str> {
pub fn level_up(char: &mut Character) -> Result<i32, DomainError> {
if char.level >= MAX_LEVEL {
return Err("already at max level");
return Err(DomainError::AlreadyAtMaxLevel);
}
let next_level = char.level + 1;
if char.xp < xp_for_level(next_level) {
return Err("not enough XP");
return Err(DomainError::NotEnoughXp);
}
let cost = level_up_cost(next_level);
if char.gold < cost {
return Err("not enough gold");
return Err(DomainError::NotEnoughGold);
}
char.gold -= cost;
char.level = next_level;
@ -483,7 +484,7 @@ mod tests {
c.level = MAX_LEVEL;
c.xp = i64::MAX;
c.gold = i32::MAX;
assert_eq!(level_up(&mut c), Err("already at max level"));
assert_eq!(level_up(&mut c), Err(DomainError::AlreadyAtMaxLevel));
}
#[test]
@ -512,7 +513,7 @@ mod tests {
let mut c = base_char();
c.xp = 100;
c.gold = 10000;
assert_eq!(level_up(&mut c), Err("not enough XP"));
assert_eq!(level_up(&mut c), Err(DomainError::NotEnoughXp));
assert_eq!(c.level, 1);
assert_eq!(c.gold, 10000);
}
@ -522,7 +523,7 @@ mod tests {
let mut c = base_char();
c.xp = 1800;
c.gold = 0;
assert_eq!(level_up(&mut c), Err("not enough gold"));
assert_eq!(level_up(&mut c), Err(DomainError::NotEnoughGold));
assert_eq!(c.level, 1);
}