2020-06-05 22:10:53 -06:00
|
|
|
use std::collections::HashMap;
|
2023-11-10 21:37:23 -07:00
|
|
|
use std::convert::{From, Into};
|
2022-10-18 17:55:47 -06:00
|
|
|
use async_std::sync::{Arc, RwLock, RwLockReadGuard};
|
2022-10-18 04:46:21 -06:00
|
|
|
use futures::future::BoxFuture;
|
2022-10-18 17:55:47 -06:00
|
|
|
use futures::stream::{FuturesOrdered, Stream};
|
2020-01-02 20:33:37 -08:00
|
|
|
|
2021-12-28 14:16:05 -07:00
|
|
|
use thiserror::Error;
|
2020-04-21 07:20:25 -06:00
|
|
|
use rand::Rng;
|
2022-10-18 17:55:47 -06:00
|
|
|
|
2023-11-10 21:37:23 -07:00
|
|
|
use maps::maps::Maps;
|
2023-11-11 14:28:41 -07:00
|
|
|
use drops::DropTable;
|
2023-11-10 21:37:23 -07:00
|
|
|
use entity::character::SectionID;
|
|
|
|
use entity::room::{RoomEntityId, RoomEntityMode};
|
|
|
|
use maps::monster::{load_monster_stats_table, MonsterType, MonsterStats};
|
|
|
|
use maps::area::MapAreaLookup;
|
2023-11-11 14:28:41 -07:00
|
|
|
use quests;
|
|
|
|
use maps::Holiday;
|
|
|
|
use location::{MAX_ROOMS, RoomId};
|
2022-10-18 04:46:21 -06:00
|
|
|
|
2023-11-10 21:37:23 -07:00
|
|
|
use maps::room::{Episode, Difficulty, RoomMode};
|
|
|
|
|
2023-11-11 14:28:41 -07:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum RoomError {
|
|
|
|
#[error("invalid room id {0}")]
|
|
|
|
Invalid(u32),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-18 04:46:21 -06:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Rooms([Arc<RwLock<Option<RoomState>>>; MAX_ROOMS]);
|
|
|
|
|
2023-11-11 14:28:41 -07:00
|
|
|
|
2022-10-18 04:46:21 -06:00
|
|
|
impl Default for Rooms {
|
|
|
|
fn default() -> Rooms {
|
|
|
|
Rooms(core::array::from_fn(|_| Arc::new(RwLock::new(None))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Rooms {
|
2023-01-28 20:12:20 -07:00
|
|
|
pub async fn add(&self, room_id: RoomId, room: RoomState) -> Result<(), anyhow::Error> {
|
2022-10-18 04:46:21 -06:00
|
|
|
*self.0
|
|
|
|
.get(room_id.0)
|
2023-11-11 14:41:00 -07:00
|
|
|
.ok_or(RoomError::Invalid(room_id.0 as u32))?
|
2022-10-18 04:46:21 -06:00
|
|
|
.write()
|
|
|
|
.await = Some(room);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn remove(&self, room_id: RoomId) {
|
|
|
|
if let Some(room) = self.0.get(room_id.0) {
|
|
|
|
*room
|
|
|
|
.write()
|
|
|
|
.await = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn exists(&self, room_id: RoomId) -> bool {
|
|
|
|
match self.0.get(room_id.0) {
|
|
|
|
Some(room) => {
|
|
|
|
room
|
|
|
|
.read()
|
|
|
|
.await
|
|
|
|
.is_some()
|
|
|
|
},
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
2023-02-18 00:51:28 -07:00
|
|
|
|
2023-01-28 20:12:20 -07:00
|
|
|
pub async fn with<'a, T, F>(&'a self, room_id: RoomId, func: F) -> Result<T, anyhow::Error>
|
2022-10-18 04:46:21 -06:00
|
|
|
where
|
|
|
|
T: Send,
|
|
|
|
F: for<'b> FnOnce(&'b RoomState) -> BoxFuture<'b, T> + Send + 'a
|
|
|
|
{
|
|
|
|
let room = self.0
|
|
|
|
.get(room_id.0)
|
2023-11-11 14:41:00 -07:00
|
|
|
.ok_or(RoomError::Invalid(room_id.0 as u32))?
|
2022-10-18 04:46:21 -06:00
|
|
|
.read()
|
|
|
|
.await;
|
|
|
|
if let Some(room) = room.as_ref() {
|
|
|
|
Ok(func(room).await)
|
|
|
|
}
|
|
|
|
else {
|
2023-11-11 14:28:41 -07:00
|
|
|
Err(RoomError::Invalid(room_id.0 as u32).into())
|
2022-10-18 04:46:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-28 20:12:20 -07:00
|
|
|
pub async fn with_mut<'a, T, F>(&'a self, room_id: RoomId, func: F) -> Result<T, anyhow::Error>
|
2022-10-18 04:46:21 -06:00
|
|
|
where
|
|
|
|
T: Send,
|
|
|
|
F: for<'b> FnOnce(&'b mut RoomState) -> BoxFuture<'b, T> + Send + 'a
|
|
|
|
{
|
|
|
|
let mut room = self.0
|
|
|
|
.get(room_id.0)
|
2023-11-11 14:41:00 -07:00
|
|
|
.ok_or(RoomError::Invalid(room_id.0 as u32))?
|
2022-10-18 04:46:21 -06:00
|
|
|
.write()
|
|
|
|
.await;
|
|
|
|
|
|
|
|
if let Some(room) = room.as_mut() {
|
|
|
|
Ok(func(room).await)
|
|
|
|
}
|
|
|
|
else {
|
2023-11-11 14:28:41 -07:00
|
|
|
Err(RoomError::Invalid(room_id.0 as u32).into())
|
2022-10-18 04:46:21 -06:00
|
|
|
}
|
|
|
|
}
|
2023-02-18 00:51:28 -07:00
|
|
|
|
2022-10-18 17:55:47 -06:00
|
|
|
pub async fn get(&self, room_id: RoomId) -> RwLockReadGuard<Option<RoomState>> {
|
2022-10-18 04:46:21 -06:00
|
|
|
self.0
|
|
|
|
.get(room_id.0)
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2022-10-18 17:55:47 -06:00
|
|
|
pub fn stream(&self) -> impl Stream<Item = RwLockReadGuard<Option<RoomState>>> {
|
2022-10-18 04:46:21 -06:00
|
|
|
self.0
|
|
|
|
.iter()
|
|
|
|
.map(|room| async move {
|
|
|
|
room
|
|
|
|
.read()
|
|
|
|
.await
|
|
|
|
})
|
|
|
|
.collect::<FuturesOrdered<_>>()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-23 18:18:36 +00:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
#[error("")]
|
2020-01-02 20:33:37 -08:00
|
|
|
pub enum RoomCreationError {
|
|
|
|
InvalidMode,
|
|
|
|
InvalidEpisode(u8),
|
|
|
|
InvalidDifficulty(u8),
|
2020-10-30 22:58:10 -06:00
|
|
|
CouldNotLoadMonsterStats(RoomMode),
|
2022-01-27 01:07:19 +00:00
|
|
|
CouldNotLoadQuests,
|
2020-01-02 20:33:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-07 03:01:29 +00:00
|
|
|
pub enum QuestCategoryType {
|
|
|
|
Standard,
|
|
|
|
Government,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<usize> for QuestCategoryType {
|
|
|
|
fn from(f: usize) -> QuestCategoryType {
|
|
|
|
match f {
|
|
|
|
0 => QuestCategoryType::Standard,
|
2023-02-18 00:51:28 -07:00
|
|
|
_ => QuestCategoryType::Government,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl From<u32> for QuestCategoryType {
|
|
|
|
fn from(f: u32) -> QuestCategoryType {
|
|
|
|
match f {
|
|
|
|
0 => QuestCategoryType::Standard,
|
|
|
|
_ => QuestCategoryType::Government,
|
2022-02-07 03:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 23:02:25 -08:00
|
|
|
|
2022-02-07 03:01:29 +00:00
|
|
|
impl QuestCategoryType {
|
|
|
|
pub fn value(&self) -> usize {
|
|
|
|
match self {
|
|
|
|
QuestCategoryType::Standard => 0,
|
|
|
|
QuestCategoryType::Government => 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 23:02:25 -08:00
|
|
|
|
2020-01-02 20:33:37 -08:00
|
|
|
pub struct RoomState {
|
2023-02-18 00:51:28 -07:00
|
|
|
pub room_id: RoomEntityId,
|
2020-03-17 08:02:17 -03:00
|
|
|
pub mode: RoomMode,
|
2020-01-18 23:49:15 -08:00
|
|
|
pub name: String,
|
2020-03-17 08:02:17 -03:00
|
|
|
pub password: [u16; 16],
|
2020-02-20 23:03:51 -08:00
|
|
|
pub maps: Maps,
|
2023-01-29 11:51:55 -07:00
|
|
|
pub drop_table: Box<DropTable>,
|
2020-04-21 07:20:25 -06:00
|
|
|
pub section_id: SectionID,
|
|
|
|
pub random_seed: u32,
|
2020-04-21 08:51:06 -06:00
|
|
|
pub bursting: bool,
|
2020-06-05 22:10:53 -06:00
|
|
|
pub monster_stats: Box<HashMap<MonsterType, MonsterStats>>,
|
2020-08-04 20:43:02 -06:00
|
|
|
pub map_areas: MapAreaLookup,
|
2022-02-07 03:01:29 +00:00
|
|
|
pub quest_group: QuestCategoryType,
|
2023-02-18 00:51:28 -07:00
|
|
|
pub standard_quests: quests::QuestList,
|
|
|
|
pub government_quests: quests::QuestList,
|
2020-01-02 20:33:37 -08:00
|
|
|
// enemy info
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RoomState {
|
2020-03-18 21:46:13 -03:00
|
|
|
pub fn get_flags_for_room_list(&self) -> u8 {
|
2020-03-18 01:36:46 -03:00
|
|
|
let mut flags = 0u8;
|
2023-02-18 00:51:28 -07:00
|
|
|
|
2020-03-18 21:46:13 -03:00
|
|
|
match self.mode {
|
2020-03-18 01:36:46 -03:00
|
|
|
RoomMode::Single {..} => {flags += 0x04}
|
|
|
|
RoomMode::Battle {..} => {flags += 0x10},
|
|
|
|
RoomMode::Challenge {..} => {flags += 0x20},
|
|
|
|
_ => {flags += 0x40},
|
|
|
|
};
|
2023-02-18 00:51:28 -07:00
|
|
|
|
2020-03-18 21:46:13 -03:00
|
|
|
if self.password[0] > 0 {
|
2020-03-18 01:36:46 -03:00
|
|
|
flags += 0x02;
|
|
|
|
}
|
|
|
|
flags
|
|
|
|
}
|
|
|
|
|
2020-03-18 21:46:13 -03:00
|
|
|
pub fn get_episode_for_room_list(&self) -> u8 {
|
|
|
|
let episode: u8 = self.mode.episode().into();
|
2020-03-18 01:36:46 -03:00
|
|
|
|
2020-03-18 21:46:13 -03:00
|
|
|
match self.mode {
|
|
|
|
RoomMode::Single {..} => episode + 0x10,
|
|
|
|
_ => episode + 0x40,
|
2020-03-18 01:36:46 -03:00
|
|
|
}
|
2020-03-18 21:46:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_difficulty_for_room_list(&self) -> u8 {
|
|
|
|
let difficulty: u8 = self.mode.difficulty().into();
|
|
|
|
difficulty + 0x22
|
2020-03-18 01:36:46 -03:00
|
|
|
}
|
|
|
|
|
2023-02-18 00:51:28 -07:00
|
|
|
pub fn quests(&self) -> &quests::QuestList {
|
|
|
|
match self.quest_group {
|
|
|
|
QuestCategoryType::Standard => &self.standard_quests,
|
|
|
|
QuestCategoryType::Government => &self.government_quests,
|
2020-01-02 20:33:37 -08:00
|
|
|
}
|
2023-02-18 00:51:28 -07:00
|
|
|
}
|
2022-02-06 21:53:11 +00:00
|
|
|
|
2023-11-11 14:41:00 -07:00
|
|
|
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
|
2023-02-18 00:51:28 -07:00
|
|
|
pub fn new (room_id: RoomEntityId,
|
|
|
|
mode: RoomEntityMode,
|
|
|
|
episode: Episode,
|
|
|
|
difficulty: Difficulty,
|
|
|
|
section_id: SectionID,
|
|
|
|
name: String,
|
|
|
|
password: [u16; 16],
|
2023-11-11 14:28:41 -07:00
|
|
|
event: Holiday,
|
|
|
|
map_builder: Arc<Box<dyn Fn(RoomMode, Holiday) -> Maps + Send + Sync>>,
|
2023-02-18 00:51:28 -07:00
|
|
|
drop_table_builder: Arc<Box<dyn Fn(Episode, Difficulty, SectionID) -> DropTable + Send + Sync>>,
|
|
|
|
) -> Result<RoomState, anyhow::Error> {
|
|
|
|
let mode = match mode {
|
|
|
|
RoomEntityMode::Single => RoomMode::Single {
|
|
|
|
episode,
|
|
|
|
difficulty,
|
|
|
|
},
|
|
|
|
RoomEntityMode::Multi => RoomMode::Multi {
|
|
|
|
episode,
|
|
|
|
difficulty,
|
|
|
|
},
|
|
|
|
RoomEntityMode::Challenge => RoomMode::Challenge {
|
|
|
|
episode,
|
|
|
|
},
|
|
|
|
RoomEntityMode::Battle => RoomMode::Battle {
|
|
|
|
episode,
|
|
|
|
difficulty,
|
|
|
|
},
|
2022-01-27 01:07:19 +00:00
|
|
|
};
|
2021-07-18 18:40:10 +00:00
|
|
|
|
2020-01-02 20:33:37 -08:00
|
|
|
Ok(RoomState {
|
2023-02-18 00:51:28 -07:00
|
|
|
room_id,
|
|
|
|
monster_stats: Box::new(load_monster_stats_table(&mode).map_err(|_| RoomCreationError::CouldNotLoadMonsterStats(mode))?),
|
|
|
|
mode,
|
2021-07-23 01:45:02 +00:00
|
|
|
random_seed: rand::thread_rng().gen(),
|
2023-02-18 00:51:28 -07:00
|
|
|
name,
|
|
|
|
password,
|
2023-11-11 14:28:41 -07:00
|
|
|
maps: map_builder(mode, event),
|
2021-06-18 20:38:29 -06:00
|
|
|
section_id,
|
2023-02-18 00:51:28 -07:00
|
|
|
drop_table: Box::new(drop_table_builder(episode, difficulty, section_id)),
|
2020-04-21 08:51:06 -06:00
|
|
|
bursting: false,
|
2023-02-18 00:51:28 -07:00
|
|
|
map_areas: MapAreaLookup::new(&episode),
|
2022-02-07 03:01:29 +00:00
|
|
|
quest_group: QuestCategoryType::Standard,
|
2023-02-18 00:51:28 -07:00
|
|
|
standard_quests: quests::load_standard_quests(mode)?,
|
|
|
|
government_quests: quests::load_government_quests(mode)?,
|
2020-01-02 20:33:37 -08:00
|
|
|
})
|
2023-02-18 00:51:28 -07:00
|
|
|
|
2020-01-02 20:33:37 -08:00
|
|
|
}
|
|
|
|
}
|