feat: Basic structs

- Added Account struct
- Added Config struct
- Added empty Map struct
- Added Quest struct
This commit is contained in:
Alexey 2025-11-28 14:06:12 +03:00
commit 4e3c137f8b
8 changed files with 441 additions and 0 deletions

24
src/account/mod.rs Normal file
View file

@ -0,0 +1,24 @@
//! Module for handling user accounts
use serde::{ Serialize, Deserialize };
fn default_id() -> String {
"none".to_string()
}
/// User account struct, which can be (de-)serialized from/into TOML
#[derive(Serialize, Deserialize)]
pub struct Account {
/// User id, specific to used service
#[serde(default = "default_id")]
pub id: String,
/// User balance,
#[serde(default)]
pub balance: u32,
/// Id of room node where user is located
#[serde(default)]
pub location: u16
}

55
src/config/mod.rs Normal file
View file

@ -0,0 +1,55 @@
//! Module for handling configuration
use std::path::PathBuf;
use serde::Deserialize;
/// Struct for containing paths to other (de-)serializable things
#[derive(Deserialize)]
pub struct Config {
/// Path to serialized [quests][`crate::quest::Quest`] folder
#[serde(default)]
pub quests_path: PathBuf,
/// Path to serialized [accounts][`crate::account::Account`] folder
#[serde(default)]
pub accounts_path: PathBuf,
/// Path to serialized [map][`crate::map::Map`] file
#[serde(default)]
pub map: PathBuf
}
impl Default for Config {
fn default() -> Self {
Config {
quests_path: "quests".into(),
accounts_path: "accounts".into(),
map: "map.toml".into()
}
}
}
impl Config {
/// Deserialize config from TOML
pub fn load(path: PathBuf) -> Self {
match std::fs::read_to_string(path) {
Ok(string) => {
match toml::from_str::<Config>(&string) {
Ok(conf) => {
println!("Successfully loaded config");
conf
},
Err(error) => {
eprintln!("Error on parsing config: {error}");
Config::default()
}
}
},
Err(error) => {
eprintln!("Error on reading config path: {error}");
Config::default()
}
}
}
}

View file

@ -0,0 +1,8 @@
//! This crate allows to build primitive RPG-like system for community events.
#![warn(missing_docs)]
pub mod account;
pub mod config;
pub mod map;
pub mod quest;

4
src/map/mod.rs Normal file
View file

@ -0,0 +1,4 @@
//! Module for handling map, which is a graph of rooms
#![allow(dead_code)]
struct Map;

62
src/quest/mod.rs Normal file
View file

@ -0,0 +1,62 @@
//! Module for handling text-based quests and user answers
use serde::{ Serialize, Deserialize };
/// Difficulty of the quest
#[derive(Serialize, Deserialize)]
pub enum QuestDifficulty {
/// Easy quest
Easy,
/// Normal quest
Normal,
/// Hard quest
Hard,
/// Special case of hard quests. Also is a default value for enum
Secret
}
impl Default for QuestDifficulty {
fn default() -> Self {
QuestDifficulty::Secret
}
}
fn default_name() -> String {
"Slay the dragon".to_string()
}
fn default_description() -> String {
"Just do it in any way".to_string()
}
fn default_answer() -> String {
"Attachment should show that the dragon was slain".to_string()
}
/// Quest struct
#[derive(Serialize, Deserialize)]
pub struct Quest {
/// Quest identifier
#[serde(default)]
pub id: u16,
/// Difficulty of this quest
#[serde(default)]
pub difficulty: QuestDifficulty,
/// Reward for the quest
#[serde(default)]
pub reward: u32,
/// Visible quest name
#[serde(default = "default_name")]
pub name: String,
/// Visible quest description
#[serde(default = "default_description")]
pub description: String,
/// Quest answer, available for admins
#[serde(default = "default_answer")]
pub answer: String,
}