Add SQLite persistence, per-player NPC attitude system, character creation, and combat
- Add trait-based DB layer (db.rs) with SQLite backend for easy future swapping - Player state persisted to SQLite: stats, inventory, equipment, room position - Returning players skip chargen and resume where they left off - Replace boolean hostile flag with 5-tier attitude system (friendly/neutral/wary/aggressive/hostile) - Per-player NPC attitudes stored in DB, shift on kills with faction propagation - Add character creation flow (chargen.rs) with data-driven races and classes from TOML - Add turn-based combat system (combat.rs) with XP, leveling, and NPC respawn - Add commands: take, drop, inventory, equip, use, examine, talk, attack, flee, stats - Add world data: 5 races, 4 classes, hostile NPCs (rat, thief), new items Made-with: Cursor
This commit is contained in:
325
src/ssh.rs
325
src/ssh.rs
@@ -4,6 +4,7 @@ use russh::server::{Auth, Handle, Msg, Server, Session};
|
||||
use russh::{Channel, ChannelId, CryptoVec, Pty};
|
||||
|
||||
use crate::ansi;
|
||||
use crate::chargen::ChargenState;
|
||||
use crate::commands;
|
||||
use crate::game::SharedState;
|
||||
|
||||
@@ -14,10 +15,7 @@ pub struct MudServer {
|
||||
|
||||
impl MudServer {
|
||||
pub fn new(state: SharedState) -> Self {
|
||||
MudServer {
|
||||
state,
|
||||
next_id: AtomicUsize::new(1),
|
||||
}
|
||||
MudServer { state, next_id: AtomicUsize::new(1) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,12 +26,8 @@ impl Server for MudServer {
|
||||
let id = self.next_id.fetch_add(1, Ordering::SeqCst);
|
||||
log::info!("New connection (id={id}) from {addr:?}");
|
||||
MudHandler {
|
||||
id,
|
||||
username: String::new(),
|
||||
channel: None,
|
||||
handle: None,
|
||||
line_buffer: String::new(),
|
||||
state: self.state.clone(),
|
||||
id, username: String::new(), channel: None, handle: None,
|
||||
line_buffer: String::new(), chargen: None, state: self.state.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,129 +42,150 @@ pub struct MudHandler {
|
||||
channel: Option<ChannelId>,
|
||||
handle: Option<Handle>,
|
||||
line_buffer: String,
|
||||
// None = not yet determined, Some(None) = returning player, Some(Some(cg)) = in chargen
|
||||
chargen: Option<Option<ChargenState>>,
|
||||
state: SharedState,
|
||||
}
|
||||
|
||||
impl MudHandler {
|
||||
async fn register_player(&self, session: &mut Session, channel: ChannelId) {
|
||||
let handle = session.handle();
|
||||
let mut state = self.state.lock().await;
|
||||
state.add_player(self.id, self.username.clone(), channel, handle);
|
||||
fn send_text(&self, session: &mut Session, channel: ChannelId, text: &str) {
|
||||
let _ = session.data(channel, CryptoVec::from(text.as_bytes()));
|
||||
}
|
||||
|
||||
let spawn_room = state.spawn_room().to_string();
|
||||
let arrival = CryptoVec::from(
|
||||
format!(
|
||||
"\r\n{}\r\n{}",
|
||||
ansi::system_msg(&format!("{} has entered the world.", self.username)),
|
||||
ansi::prompt()
|
||||
)
|
||||
.as_bytes(),
|
||||
);
|
||||
let others: Vec<_> = state
|
||||
.players_in_room(&spawn_room, self.id)
|
||||
.iter()
|
||||
.map(|c| (c.channel, c.handle.clone()))
|
||||
.collect();
|
||||
async fn start_session(&mut self, session: &mut Session, channel: ChannelId) {
|
||||
let state = self.state.lock().await;
|
||||
let world_name = state.world.name.clone();
|
||||
|
||||
// Check if returning player
|
||||
let saved = state.db.load_player(&self.username);
|
||||
drop(state);
|
||||
|
||||
let welcome = format!(
|
||||
"{}\r\n{}Welcome to {}, {}!\r\n",
|
||||
ansi::CLEAR_SCREEN, ansi::welcome_banner(),
|
||||
ansi::bold(&world_name), ansi::player_name(&self.username),
|
||||
);
|
||||
self.send_text(session, channel, &welcome);
|
||||
|
||||
if let Some(saved) = saved {
|
||||
// Returning player — load from DB
|
||||
let handle = session.handle();
|
||||
let mut state = self.state.lock().await;
|
||||
state.load_existing_player(self.id, saved, channel, handle);
|
||||
|
||||
let msg = format!("{}\r\n", ansi::system_msg("Welcome back! Your character has been restored."));
|
||||
drop(state);
|
||||
self.send_text(session, channel, &msg);
|
||||
|
||||
self.chargen = Some(None); // signal: no chargen needed
|
||||
self.enter_world(session, channel).await;
|
||||
} else {
|
||||
// New player — start chargen
|
||||
let cg = ChargenState::new();
|
||||
let state = self.state.lock().await;
|
||||
let prompt = cg.prompt_text(&state.world);
|
||||
drop(state);
|
||||
self.send_text(session, channel, &prompt);
|
||||
self.chargen = Some(Some(cg));
|
||||
}
|
||||
}
|
||||
|
||||
async fn enter_world(&mut self, session: &mut Session, channel: ChannelId) {
|
||||
let state = self.state.lock().await;
|
||||
|
||||
let (room_id, player_name) = match state.players.get(&self.id) {
|
||||
Some(c) => (c.player.room_id.clone(), c.player.name.clone()),
|
||||
None => return,
|
||||
};
|
||||
|
||||
// Broadcast arrival
|
||||
let arrival = CryptoVec::from(
|
||||
format!("\r\n{}\r\n{}", ansi::system_msg(&format!("{player_name} has entered the world.")), ansi::prompt()).as_bytes(),
|
||||
);
|
||||
let others: Vec<_> = state.players_in_room(&room_id, self.id).iter().map(|c| (c.channel, c.handle.clone())).collect();
|
||||
|
||||
// Render room
|
||||
let room_view = render_entry_room(&state, &room_id, &player_name, self.id);
|
||||
drop(state);
|
||||
|
||||
self.send_text(session, channel, &room_view);
|
||||
|
||||
for (ch, h) in others {
|
||||
let _ = h.data(ch, arrival.clone()).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn send_welcome(&self, session: &mut Session, channel: ChannelId) {
|
||||
let state = self.state.lock().await;
|
||||
let world_name = state.world.name.clone();
|
||||
async fn finish_chargen(&mut self, race_id: String, class_id: String, session: &mut Session, channel: ChannelId) {
|
||||
let handle = session.handle();
|
||||
let mut state = self.state.lock().await;
|
||||
|
||||
let race_name = state.world.races.iter().find(|r| r.id == race_id).map(|r| r.name.clone()).unwrap_or_default();
|
||||
let class_name = state.world.classes.iter().find(|c| c.id == class_id).map(|c| c.name.clone()).unwrap_or_default();
|
||||
|
||||
state.create_new_player(self.id, self.username.clone(), race_id, class_id, channel, handle);
|
||||
state.save_player_to_db(self.id);
|
||||
|
||||
drop(state);
|
||||
|
||||
let welcome = format!(
|
||||
"{}\r\n{}Welcome to {}, {}! Type {} to get started.\r\n\r\n",
|
||||
ansi::CLEAR_SCREEN,
|
||||
ansi::welcome_banner(),
|
||||
ansi::bold(&world_name),
|
||||
ansi::player_name(&self.username),
|
||||
ansi::color(ansi::YELLOW, "'help'")
|
||||
);
|
||||
let _ = session.data(channel, CryptoVec::from(welcome.as_bytes()));
|
||||
}
|
||||
let msg = format!("\r\n{}\r\n\r\n", ansi::system_msg(&format!("Character created: {} the {} {}", self.username, race_name, class_name)));
|
||||
self.send_text(session, channel, &msg);
|
||||
|
||||
async fn show_room(&self, session: &mut Session, channel: ChannelId) {
|
||||
let state = self.state.lock().await;
|
||||
let room_id = match state.players.get(&self.id) {
|
||||
Some(c) => c.player.room_id.clone(),
|
||||
None => return,
|
||||
};
|
||||
let room = match state.world.get_room(&room_id) {
|
||||
Some(r) => r,
|
||||
None => return,
|
||||
};
|
||||
|
||||
let mut out = String::new();
|
||||
out.push_str(&format!(
|
||||
"{} {}\r\n",
|
||||
ansi::room_name(&room.name),
|
||||
ansi::system_msg(&format!("[{}]", room.region))
|
||||
));
|
||||
out.push_str(&format!(" {}\r\n", room.description));
|
||||
|
||||
if !room.npcs.is_empty() {
|
||||
let npc_names: Vec<String> = room
|
||||
.npcs
|
||||
.iter()
|
||||
.filter_map(|id| state.world.get_npc(id))
|
||||
.map(|n| ansi::color(ansi::YELLOW, &n.name))
|
||||
.collect();
|
||||
if !npc_names.is_empty() {
|
||||
out.push_str(&format!(
|
||||
"\r\n{}{}\r\n",
|
||||
ansi::color(ansi::DIM, "Present: "),
|
||||
npc_names.join(", ")
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
if !room.exits.is_empty() {
|
||||
let mut dirs: Vec<&String> = room.exits.keys().collect();
|
||||
dirs.sort();
|
||||
let dir_strs: Vec<String> = dirs.iter().map(|d| ansi::direction(d)).collect();
|
||||
out.push_str(&format!(
|
||||
"{} {}\r\n",
|
||||
ansi::color(ansi::DIM, "Exits:"),
|
||||
dir_strs.join(", ")
|
||||
));
|
||||
}
|
||||
|
||||
out.push_str(&ansi::prompt());
|
||||
let _ = session.data(channel, CryptoVec::from(out.as_bytes()));
|
||||
self.chargen = Some(None);
|
||||
self.enter_world(session, channel).await;
|
||||
}
|
||||
|
||||
async fn handle_disconnect(&self) {
|
||||
let mut state = self.state.lock().await;
|
||||
if let Some(conn) = state.remove_player(self.id) {
|
||||
let departure = CryptoVec::from(
|
||||
format!(
|
||||
"\r\n{}\r\n{}",
|
||||
ansi::system_msg(&format!("{} has left the world.", conn.player.name)),
|
||||
ansi::prompt()
|
||||
)
|
||||
.as_bytes(),
|
||||
format!("\r\n{}\r\n{}", ansi::system_msg(&format!("{} has left the world.", conn.player.name)), ansi::prompt()).as_bytes(),
|
||||
);
|
||||
let others: Vec<_> = state
|
||||
.players_in_room(&conn.player.room_id, self.id)
|
||||
.iter()
|
||||
.map(|c| (c.channel, c.handle.clone()))
|
||||
.collect();
|
||||
let others: Vec<_> = state.players_in_room(&conn.player.room_id, self.id).iter().map(|c| (c.channel, c.handle.clone())).collect();
|
||||
drop(state);
|
||||
|
||||
for (ch, h) in others {
|
||||
let _ = h.data(ch, departure.clone()).await;
|
||||
}
|
||||
|
||||
for (ch, h) in others { let _ = h.data(ch, departure.clone()).await; }
|
||||
log::info!("{} disconnected (id={})", conn.player.name, self.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn render_entry_room(state: &crate::game::GameState, room_id: &str, player_name: &str, player_id: usize) -> String {
|
||||
let room = match state.world.get_room(room_id) { Some(r) => r, None => return String::new() };
|
||||
let mut out = String::new();
|
||||
out.push_str(&format!("{} {}\r\n", ansi::room_name(&room.name), ansi::system_msg(&format!("[{}]", room.region))));
|
||||
out.push_str(&format!(" {}\r\n", room.description));
|
||||
|
||||
let npc_strs: Vec<String> = room.npcs.iter().filter_map(|id| {
|
||||
let npc = state.world.get_npc(id)?;
|
||||
let alive = state.npc_instances.get(id).map(|i| i.alive).unwrap_or(true);
|
||||
if !alive { return None; }
|
||||
let att = state.npc_attitude_toward(id, player_name);
|
||||
let color = match att {
|
||||
crate::world::Attitude::Friendly => ansi::GREEN,
|
||||
crate::world::Attitude::Neutral | crate::world::Attitude::Wary => ansi::YELLOW,
|
||||
_ => ansi::RED,
|
||||
};
|
||||
Some(ansi::color(color, &npc.name))
|
||||
}).collect();
|
||||
if !npc_strs.is_empty() {
|
||||
out.push_str(&format!("\r\n{}{}\r\n", ansi::color(ansi::DIM, "Present: "), npc_strs.join(", ")));
|
||||
}
|
||||
|
||||
let others = state.players_in_room(room_id, player_id);
|
||||
if !others.is_empty() {
|
||||
let names: Vec<String> = others.iter().map(|c| ansi::player_name(&c.player.name)).collect();
|
||||
out.push_str(&format!("{}{}\r\n", ansi::color(ansi::GREEN, "Players here: "), names.join(", ")));
|
||||
}
|
||||
|
||||
if !room.exits.is_empty() {
|
||||
let mut dirs: Vec<&String> = room.exits.keys().collect();
|
||||
dirs.sort();
|
||||
let dir_strs: Vec<String> = dirs.iter().map(|d| ansi::direction(d)).collect();
|
||||
out.push_str(&format!("{} {}\r\n", ansi::color(ansi::DIM, "Exits:"), dir_strs.join(", ")));
|
||||
}
|
||||
out.push_str(&ansi::prompt());
|
||||
out
|
||||
}
|
||||
|
||||
impl russh::server::Handler for MudHandler {
|
||||
type Error = russh::Error;
|
||||
|
||||
@@ -180,13 +195,8 @@ impl russh::server::Handler for MudHandler {
|
||||
Ok(Auth::Accept)
|
||||
}
|
||||
|
||||
async fn auth_publickey(
|
||||
&mut self,
|
||||
user: &str,
|
||||
_key: &russh::keys::ssh_key::PublicKey,
|
||||
) -> Result<Auth, Self::Error> {
|
||||
async fn auth_publickey(&mut self, user: &str, _key: &russh::keys::ssh_key::PublicKey) -> Result<Auth, Self::Error> {
|
||||
self.username = user.to_string();
|
||||
log::info!("Pubkey auth accepted for '{}' (id={})", user, self.id);
|
||||
Ok(Auth::Accept)
|
||||
}
|
||||
|
||||
@@ -195,51 +205,24 @@ impl russh::server::Handler for MudHandler {
|
||||
Ok(Auth::Accept)
|
||||
}
|
||||
|
||||
async fn channel_open_session(
|
||||
&mut self,
|
||||
channel: Channel<Msg>,
|
||||
session: &mut Session,
|
||||
) -> Result<bool, Self::Error> {
|
||||
async fn channel_open_session(&mut self, channel: Channel<Msg>, session: &mut Session) -> Result<bool, Self::Error> {
|
||||
self.channel = Some(channel.id());
|
||||
self.handle = Some(session.handle());
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
async fn pty_request(
|
||||
&mut self,
|
||||
channel: ChannelId,
|
||||
_term: &str,
|
||||
_col_width: u32,
|
||||
_row_height: u32,
|
||||
_pix_width: u32,
|
||||
_pix_height: u32,
|
||||
_modes: &[(Pty, u32)],
|
||||
session: &mut Session,
|
||||
) -> Result<(), Self::Error> {
|
||||
async fn pty_request(&mut self, channel: ChannelId, _term: &str, _col_width: u32, _row_height: u32, _pix_width: u32, _pix_height: u32, _modes: &[(Pty, u32)], session: &mut Session) -> Result<(), Self::Error> {
|
||||
session.channel_success(channel)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn shell_request(
|
||||
&mut self,
|
||||
channel: ChannelId,
|
||||
session: &mut Session,
|
||||
) -> Result<(), Self::Error> {
|
||||
async fn shell_request(&mut self, channel: ChannelId, session: &mut Session) -> Result<(), Self::Error> {
|
||||
session.channel_success(channel)?;
|
||||
|
||||
self.send_welcome(session, channel).await;
|
||||
self.register_player(session, channel).await;
|
||||
self.show_room(session, channel).await;
|
||||
|
||||
self.start_session(session, channel).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn data(
|
||||
&mut self,
|
||||
channel: ChannelId,
|
||||
data: &[u8],
|
||||
session: &mut Session,
|
||||
) -> Result<(), Self::Error> {
|
||||
async fn data(&mut self, channel: ChannelId, data: &[u8], session: &mut Session) -> Result<(), Self::Error> {
|
||||
for &byte in data {
|
||||
match byte {
|
||||
3 | 4 => {
|
||||
@@ -254,15 +237,47 @@ impl russh::server::Handler for MudHandler {
|
||||
}
|
||||
}
|
||||
b'\r' | b'\n' => {
|
||||
if byte == b'\n' && self.line_buffer.is_empty() {
|
||||
if byte == b'\n' && self.line_buffer.is_empty() { continue; }
|
||||
session.data(channel, CryptoVec::from(&b"\r\n"[..]))?;
|
||||
let line = std::mem::take(&mut self.line_buffer);
|
||||
|
||||
// Handle chargen flow
|
||||
let mut chargen_done = None;
|
||||
let mut chargen_active = false;
|
||||
if let Some(ref mut chargen_opt) = self.chargen {
|
||||
if let Some(ref mut cg) = chargen_opt {
|
||||
chargen_active = true;
|
||||
let result = {
|
||||
let state = self.state.lock().await;
|
||||
cg.handle_input(&line, &state.world)
|
||||
};
|
||||
let msg_text = match result { Ok(msg) | Err(msg) => msg };
|
||||
let _ = session.data(channel, CryptoVec::from(msg_text.as_bytes()));
|
||||
if cg.is_done() {
|
||||
chargen_done = cg.result();
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some((race_id, class_id)) = chargen_done {
|
||||
self.chargen = None;
|
||||
self.finish_chargen(race_id, class_id, session, channel).await;
|
||||
continue;
|
||||
}
|
||||
if chargen_active {
|
||||
// Still in chargen, show next prompt
|
||||
if let Some(Some(ref cg)) = self.chargen {
|
||||
let state = self.state.lock().await;
|
||||
let prompt = cg.prompt_text(&state.world);
|
||||
drop(state);
|
||||
let _ = session.data(channel, CryptoVec::from(prompt.as_bytes()));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if self.chargen.is_none() {
|
||||
continue;
|
||||
}
|
||||
session.data(channel, CryptoVec::from(&b"\r\n"[..]))?;
|
||||
|
||||
let line = std::mem::take(&mut self.line_buffer);
|
||||
let keep_going =
|
||||
commands::execute(&line, self.id, &self.state, session, channel).await?;
|
||||
|
||||
let keep_going = commands::execute(&line, self.id, &self.state, session, channel).await?;
|
||||
if !keep_going {
|
||||
self.handle_disconnect().await;
|
||||
session.close(channel)?;
|
||||
@@ -280,20 +295,12 @@ impl russh::server::Handler for MudHandler {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn channel_eof(
|
||||
&mut self,
|
||||
_channel: ChannelId,
|
||||
_session: &mut Session,
|
||||
) -> Result<(), Self::Error> {
|
||||
async fn channel_eof(&mut self, _channel: ChannelId, _session: &mut Session) -> Result<(), Self::Error> {
|
||||
self.handle_disconnect().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn channel_close(
|
||||
&mut self,
|
||||
_channel: ChannelId,
|
||||
_session: &mut Session,
|
||||
) -> Result<(), Self::Error> {
|
||||
async fn channel_close(&mut self, _channel: ChannelId, _session: &mut Session) -> Result<(), Self::Error> {
|
||||
self.handle_disconnect().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user