Remove AtomicRefCell

This commit is contained in:
xeon 2024-05-26 19:00:45 +03:00
parent 7434dabceb
commit 650dab7606
16 changed files with 394 additions and 324 deletions

View file

@ -8,7 +8,6 @@ version = "0.1.0"
[workspace.dependencies] [workspace.dependencies]
ansi_term = "0.12.1" ansi_term = "0.12.1"
anyhow = "1.0.81" anyhow = "1.0.81"
atomic_refcell = "0.1.13"
axum = "0.7.4" axum = "0.7.4"
axum-server = "0.6.0" axum-server = "0.6.0"
byteorder = "1.5.0" byteorder = "1.5.0"

View file

@ -6,7 +6,6 @@ version.workspace = true
[dependencies] [dependencies]
ansi_term.workspace = true ansi_term.workspace = true
anyhow.workspace = true anyhow.workspace = true
atomic_refcell.workspace = true
dirs.workspace = true dirs.workspace = true
env_logger.workspace = true env_logger.workspace = true
hex.workspace = true hex.workspace = true

View file

@ -1,8 +1,8 @@
use std::sync::Arc; use std::sync::Arc;
use anyhow::Result; use anyhow::Result;
use atomic_refcell::AtomicRefCell;
use protocol::{PlayerInfo, PtcPlayerInfoChangedArg}; use protocol::{PlayerInfo, PtcPlayerInfoChangedArg};
use tokio::sync::RwLock;
use crate::net::NetworkSession; use crate::net::NetworkSession;
@ -13,40 +13,29 @@ use super::manager::{
pub struct GameContext { pub struct GameContext {
#[allow(unused)] #[allow(unused)]
pub uid_manager: Arc<AtomicRefCell<UniqueIDManager>>, pub uid_manager: Arc<UniqueIDManager>,
pub item_manager: Arc<AtomicRefCell<ItemManager>>, pub item_manager: Arc<ItemManager>,
pub dungeon_manager: Arc<AtomicRefCell<DungeonManager>>, pub dungeon_manager: Arc<DungeonManager>,
pub quest_manager: Arc<AtomicRefCell<QuestManager>>, pub quest_manager: Arc<QuestManager>,
pub scene_unit_manager: Arc<AtomicRefCell<SceneUnitManager>>, pub scene_unit_manager: Arc<SceneUnitManager>,
pub hollow_grid_manager: Arc<AtomicRefCell<HollowGridManager>>, pub hollow_grid_manager: Arc<HollowGridManager>,
pub unlock_manager: Arc<AtomicRefCell<UnlockManager>>, pub unlock_manager: Arc<UnlockManager>,
pub yorozuya_quest_manager: Arc<AtomicRefCell<YorozuyaQuestManager>>, pub yorozuya_quest_manager: Arc<YorozuyaQuestManager>,
} }
impl GameContext { impl GameContext {
pub fn new(player: Arc<AtomicRefCell<PlayerInfo>>) -> Self { pub fn new(player: Arc<RwLock<PlayerInfo>>) -> Self {
let uid_manager = Arc::new(AtomicRefCell::new(UniqueIDManager::new())); let uid_manager = Arc::new(UniqueIDManager::new());
Self { Self {
uid_manager: uid_manager.clone(), uid_manager: uid_manager.clone(),
item_manager: Arc::new(AtomicRefCell::new(ItemManager::new( item_manager: Arc::new(ItemManager::new(uid_manager.clone(), player.clone())),
uid_manager.clone(), dungeon_manager: Arc::new(DungeonManager::new(uid_manager.clone(), player.clone())),
player.clone(), quest_manager: Arc::new(QuestManager::new(uid_manager.clone(), player.clone())),
))), scene_unit_manager: Arc::new(SceneUnitManager::new(uid_manager)),
dungeon_manager: Arc::new(AtomicRefCell::new(DungeonManager::new( hollow_grid_manager: Arc::new(HollowGridManager::new(player.clone())),
uid_manager.clone(), unlock_manager: Arc::new(UnlockManager::new(player.clone())),
player.clone(), yorozuya_quest_manager: Arc::new(YorozuyaQuestManager::new(player)),
))),
quest_manager: Arc::new(AtomicRefCell::new(QuestManager::new(
uid_manager.clone(),
player.clone(),
))),
scene_unit_manager: Arc::new(AtomicRefCell::new(SceneUnitManager::new(uid_manager))),
hollow_grid_manager: Arc::new(AtomicRefCell::new(HollowGridManager::new(
player.clone(),
))),
unlock_manager: Arc::new(AtomicRefCell::new(UnlockManager::new(player.clone()))),
yorozuya_quest_manager: Arc::new(AtomicRefCell::new(YorozuyaQuestManager::new(player))),
} }
} }
} }
@ -70,7 +59,7 @@ where
pub async fn send_changes(&mut self, session: &NetworkSession) -> Result<&T> { pub async fn send_changes(&mut self, session: &NetworkSession) -> Result<&T> {
if self.player_info_changes.is_some() { if self.player_info_changes.is_some() {
let ptc_player_info_changed = PtcPlayerInfoChangedArg { let ptc_player_info_changed = PtcPlayerInfoChangedArg {
player_uid: session.get_player_uid(), player_uid: session.get_player_uid().await,
player_info: self.player_info_changes.take().unwrap(), player_info: self.player_info_changes.take().unwrap(),
}; };

View file

@ -1,7 +1,7 @@
use anyhow::{anyhow, bail, Result}; use anyhow::{anyhow, bail, Result};
use atomic_refcell::AtomicRefCell;
use protocol::*; use protocol::*;
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::RwLock;
use crate::game::{manager::UniqueIDManager, util, PlayerOperationResult}; use crate::game::{manager::UniqueIDManager, util, PlayerOperationResult};
use qwer::{ use qwer::{
@ -9,26 +9,23 @@ use qwer::{
}; };
pub struct DungeonManager { pub struct DungeonManager {
uid_mgr: Arc<AtomicRefCell<UniqueIDManager>>, uid_mgr: Arc<UniqueIDManager>,
player: Arc<AtomicRefCell<PlayerInfo>>, player: Arc<RwLock<PlayerInfo>>,
scene_properties: AtomicRefCell<PropertyDoubleKeyHashMap<u64, u16, i32>>, scene_properties: RwLock<PropertyDoubleKeyHashMap<u64, u16, i32>>,
} }
impl DungeonManager { impl DungeonManager {
pub fn new( pub fn new(uid_mgr: Arc<UniqueIDManager>, player: Arc<RwLock<PlayerInfo>>) -> Self {
uid_mgr: Arc<AtomicRefCell<UniqueIDManager>>,
player: Arc<AtomicRefCell<PlayerInfo>>,
) -> Self {
Self { Self {
uid_mgr, uid_mgr,
player, player,
scene_properties: AtomicRefCell::new(pdkhashmap![]), scene_properties: RwLock::new(pdkhashmap![]),
} }
} }
pub fn enter_main_city(&self) -> Result<PlayerOperationResult<PtcEnterSceneArg>> { pub async fn enter_main_city(&self) -> Result<PlayerOperationResult<PtcEnterSceneArg>> {
let (player_uid, scene_position, scene_rotation) = { let (player_uid, scene_position, scene_rotation) = {
let player = self.player.borrow(); let player = self.player.read().await;
let pos_in_main_city = player.pos_in_main_city.as_ref().unwrap(); let pos_in_main_city = player.pos_in_main_city.as_ref().unwrap();
( (
@ -38,7 +35,7 @@ impl DungeonManager {
) )
}; };
let mut player = self.player.borrow_mut(); let mut player = self.player.write().await;
let default_scene_uid = player let default_scene_uid = player
.dungeon_collection .dungeon_collection
.as_ref() .as_ref()
@ -116,12 +113,12 @@ impl DungeonManager {
)) ))
} }
pub fn enter_scene_section( pub async fn enter_scene_section(
&self, &self,
scene_uid: u64, scene_uid: u64,
section_id: i32, section_id: i32,
) -> PlayerOperationResult<PtcEnterSectionArg> { ) -> PlayerOperationResult<PtcEnterSectionArg> {
let mut player = self.player.borrow_mut(); let mut player = self.player.write().await;
let scene_info = player let scene_info = player
.dungeon_collection .dungeon_collection
.as_mut() .as_mut()
@ -148,9 +145,12 @@ impl DungeonManager {
) )
} }
pub fn enter_scene(&self, scene_uid: u64) -> Result<PlayerOperationResult<PtcEnterSceneArg>> { pub async fn enter_scene(
&self,
scene_uid: u64,
) -> Result<PlayerOperationResult<PtcEnterSceneArg>> {
let (player_uid, prev_scene_uid) = { let (player_uid, prev_scene_uid) = {
let player = self.player.borrow(); let player = self.player.read().await;
( (
*player.uid.as_ref().unwrap(), *player.uid.as_ref().unwrap(),
@ -158,7 +158,7 @@ impl DungeonManager {
) )
}; };
let mut player = self.player.borrow_mut(); let mut player = self.player.write().await;
player.scene_uid.replace(scene_uid); player.scene_uid.replace(scene_uid);
player.prev_scene_uid.replace(prev_scene_uid); player.prev_scene_uid.replace(prev_scene_uid);
let dungeon_collection = player.dungeon_collection.as_mut().unwrap(); let dungeon_collection = player.dungeon_collection.as_mut().unwrap();
@ -248,10 +248,10 @@ impl DungeonManager {
)) ))
} }
pub fn hollow_finished(&self) -> PlayerOperationResult<u64> { pub async fn hollow_finished(&self) -> PlayerOperationResult<u64> {
let cur_scene_uid = self.get_cur_scene_uid(); let cur_scene_uid = self.get_cur_scene_uid().await;
let mut player = self.player.borrow_mut(); let mut player = self.player.write().await;
let hollow_scene = player let hollow_scene = player
.dungeon_collection .dungeon_collection
@ -313,9 +313,10 @@ impl DungeonManager {
) )
} }
pub fn get_default_scene_uid(&self) -> u64 { pub async fn get_default_scene_uid(&self) -> u64 {
self.player self.player
.borrow() .read()
.await
.dungeon_collection .dungeon_collection
.as_ref() .as_ref()
.unwrap() .unwrap()
@ -324,9 +325,10 @@ impl DungeonManager {
} }
#[allow(dead_code)] #[allow(dead_code)]
pub fn get_default_scene_uid_for_dungeon(&self, dungeon_uid: u64) -> u64 { pub async fn get_default_scene_uid_for_dungeon(&self, dungeon_uid: u64) -> u64 {
self.player self.player
.borrow() .read()
.await
.dungeon_collection .dungeon_collection
.as_ref() .as_ref()
.unwrap() .unwrap()
@ -338,12 +340,12 @@ impl DungeonManager {
.default_scene_uid .default_scene_uid
} }
pub fn get_cur_scene_uid(&self) -> u64 { pub async fn get_cur_scene_uid(&self) -> u64 {
self.player.borrow().scene_uid.unwrap() self.player.read().await.scene_uid.unwrap()
} }
fn add_default_hollow_properties(&self, scene_uid: u64) { async fn add_default_hollow_properties(&self, scene_uid: u64) {
let mut props = self.scene_properties.borrow_mut(); let mut props = self.scene_properties.write().await;
for (sub_key, value) in &[ for (sub_key, value) in &[
(1001, 0), (1001, 0),
@ -378,11 +380,11 @@ impl DungeonManager {
} }
} }
pub fn leave_battle(&self) -> Result<PlayerOperationResult<PtcEnterSceneArg>> { pub async fn leave_battle(&self) -> Result<PlayerOperationResult<PtcEnterSceneArg>> {
let back_scene_uid = self.get_back_scene_uid(); let back_scene_uid = self.get_back_scene_uid().await;
{ {
let mut player = self.player.borrow_mut(); let mut player = self.player.write().await;
let hollow_scene = player let hollow_scene = player
.dungeon_collection .dungeon_collection
@ -402,11 +404,11 @@ impl DungeonManager {
} }
} }
self.enter_scene(back_scene_uid) self.enter_scene(back_scene_uid).await
} }
fn get_back_scene_uid(&self) -> u64 { async fn get_back_scene_uid(&self) -> u64 {
let player = self.player.borrow(); let player = self.player.read().await;
let fight_scene_uid = player.scene_uid.as_ref().unwrap(); let fight_scene_uid = player.scene_uid.as_ref().unwrap();
let fight_scene = player let fight_scene = player
.dungeon_collection .dungeon_collection
@ -421,12 +423,14 @@ impl DungeonManager {
fight_scene.get_back_scene_uid() fight_scene.get_back_scene_uid()
} }
pub fn enter_battle(&self, scene_uid: u64) -> PlayerOperationResult<PtcEnterSceneArg> { pub async fn enter_battle(&self, scene_uid: u64) -> PlayerOperationResult<PtcEnterSceneArg> {
let hollow_scene_uid = *self.player.borrow().scene_uid.as_ref().unwrap(); let hollow_scene_uid = *self.player.read().await.scene_uid.as_ref().unwrap();
let hollow_scene = self.set_cur_hollow_battle(scene_uid, hollow_scene_uid); let hollow_scene = self
let ptc_enter_scene = self.enter_scene(scene_uid).unwrap().unwrap().clone(); .set_cur_hollow_battle(scene_uid, hollow_scene_uid)
.await;
let ptc_enter_scene = self.enter_scene(scene_uid).await.unwrap().unwrap().clone();
let player = self.player.borrow(); let player = self.player.read().await;
let dungeon_collection = player.dungeon_collection.as_ref().unwrap(); let dungeon_collection = player.dungeon_collection.as_ref().unwrap();
let fight_scene = dungeon_collection let fight_scene = dungeon_collection
.scenes .scenes
@ -455,8 +459,8 @@ impl DungeonManager {
) )
} }
fn set_cur_hollow_battle(&self, scene_uid: u64, hollow_scene_uid: u64) -> SceneInfo { async fn set_cur_hollow_battle(&self, scene_uid: u64, hollow_scene_uid: u64) -> SceneInfo {
let mut player = self.player.borrow_mut(); let mut player = self.player.write().await;
let hollow_scene = player let hollow_scene = player
.dungeon_collection .dungeon_collection
.as_mut() .as_mut()
@ -481,13 +485,13 @@ impl DungeonManager {
hollow_scene.clone() hollow_scene.clone()
} }
pub fn create_fight(&self, id: i32, hollow_scene_uid: u64) -> PlayerOperationResult<u64> { pub async fn create_fight(&self, id: i32, hollow_scene_uid: u64) -> PlayerOperationResult<u64> {
let mut player = self.player.borrow_mut(); let mut player = self.player.write().await;
let dungeon_collection = player.dungeon_collection.as_mut().unwrap(); let dungeon_collection = player.dungeon_collection.as_mut().unwrap();
let scenes = dungeon_collection.scenes.as_mut().unwrap(); let scenes = dungeon_collection.scenes.as_mut().unwrap();
let hollow_scene = scenes.get_mut(&hollow_scene_uid).unwrap(); let hollow_scene = scenes.get_mut(&hollow_scene_uid).unwrap();
let fight_scene_uid = self.uid_mgr.borrow().next(); let fight_scene_uid = self.uid_mgr.next();
let fight_scene = SceneInfo::Fight { let fight_scene = SceneInfo::Fight {
uid: fight_scene_uid, uid: fight_scene_uid,
id, id,
@ -522,10 +526,10 @@ impl DungeonManager {
} }
#[allow(dead_code)] #[allow(dead_code)]
pub fn is_in_tutorial(&self) -> bool { pub async fn is_in_tutorial(&self) -> bool {
let cur_scene_uid = self.get_cur_scene_uid(); let cur_scene_uid = self.get_cur_scene_uid().await;
let player = self.player.borrow(); let player = self.player.read().await;
let cur_scene = player let cur_scene = player
.dungeon_collection .dungeon_collection
.as_ref() .as_ref()
@ -539,29 +543,31 @@ impl DungeonManager {
matches!(cur_scene, SceneInfo::Fresh { .. }) matches!(cur_scene, SceneInfo::Fresh { .. })
} }
pub fn create_hollow( pub async fn create_hollow(
&self, &self,
id: i32, id: i32,
world_quest_id: i32, world_quest_id: i32,
avatar_uids: &[u64], avatar_uids: &[u64],
) -> PlayerOperationResult<(u64, u64)> { ) -> PlayerOperationResult<(u64, u64)> {
let back_scene_uid = self.get_default_scene_uid(); let back_scene_uid = self.get_default_scene_uid().await;
let mut dungeon = self.create_base_dungeon(id, back_scene_uid, world_quest_id); let mut dungeon = self
.create_base_dungeon(id, back_scene_uid, world_quest_id)
.await;
dungeon.hollow_event_version = 526; dungeon.hollow_event_version = 526;
let scene_uid = self.uid_mgr.borrow().next(); let scene_uid = self.uid_mgr.next();
dungeon.default_scene_uid = scene_uid; dungeon.default_scene_uid = scene_uid;
dungeon.scene_properties_uid = scene_uid; dungeon.scene_properties_uid = scene_uid;
self.add_default_hollow_properties(scene_uid); self.add_default_hollow_properties(scene_uid).await;
for (index, avatar_uid) in avatar_uids.iter().enumerate() { for (index, avatar_uid) in avatar_uids.iter().enumerate() {
dungeon.avatar_map.insert( dungeon.avatar_map.insert(
index.try_into().unwrap(), index.try_into().unwrap(),
AvatarUnitInfo { AvatarUnitInfo {
uid: *avatar_uid, uid: *avatar_uid,
properties_uid: self.uid_mgr.borrow().next(), properties_uid: self.uid_mgr.next(),
is_banned: false, is_banned: false,
modified_property: pdkhashmap![], modified_property: pdkhashmap![],
hp_add_hollow: 0, hp_add_hollow: 0,
@ -636,10 +642,10 @@ impl DungeonManager {
}; };
{ {
let mut player = self.player.borrow_mut(); let mut player = self.player.write().await;
player player
.scene_properties .scene_properties
.replace(self.scene_properties.borrow().clone()); .replace(self.scene_properties.read().await.clone());
let dungeon_collection = player.dungeon_collection.as_mut().unwrap(); let dungeon_collection = player.dungeon_collection.as_mut().unwrap();
@ -654,7 +660,7 @@ impl DungeonManager {
.unwrap() .unwrap()
.insert(scene_uid, scene.clone()); .insert(scene_uid, scene.clone());
} }
let mut player = self.player.borrow_mut(); let mut player = self.player.write().await;
let items = player.items.as_mut().unwrap(); let items = player.items.as_mut().unwrap();
let mut updated_items = Vec::new(); let mut updated_items = Vec::new();
@ -668,7 +674,7 @@ impl DungeonManager {
} }
let mut prop_changes = Vec::new(); let mut prop_changes = Vec::new();
for (key, sub_key, value) in &*self.scene_properties.borrow_mut() { for (key, sub_key, value) in &*self.scene_properties.write().await {
prop_changes.push((*key, *sub_key, *value)); prop_changes.push((*key, *sub_key, *value));
} }
@ -699,11 +705,11 @@ impl DungeonManager {
) )
} }
pub fn create_hall(&self, id: i32) -> PlayerOperationResult<u64> { pub async fn create_hall(&self, id: i32) -> PlayerOperationResult<u64> {
let mut dungeon = self.create_base_dungeon(id, 0, 0); let mut dungeon = self.create_base_dungeon(id, 0, 0).await;
let dungeon_uid = dungeon.uid; let dungeon_uid = dungeon.uid;
let scene_uid = self.uid_mgr.borrow().next(); let scene_uid = self.uid_mgr.next();
let hall_scene_info = SceneInfo::Hall { let hall_scene_info = SceneInfo::Hall {
uid: scene_uid, uid: scene_uid,
id, id,
@ -720,7 +726,7 @@ impl DungeonManager {
dungeon.default_scene_uid = scene_uid; dungeon.default_scene_uid = scene_uid;
let mut player = self.player.borrow_mut(); let mut player = self.player.write().await;
let dungeon_collection = player.dungeon_collection.as_mut().unwrap(); let dungeon_collection = player.dungeon_collection.as_mut().unwrap();
dungeon_collection dungeon_collection
@ -756,11 +762,11 @@ impl DungeonManager {
) )
} }
pub fn create_fresh(&self) -> PlayerOperationResult<u64> { pub async fn create_fresh(&self) -> PlayerOperationResult<u64> {
let mut dungeon = self.create_base_dungeon(2, 0, 0); let mut dungeon = self.create_base_dungeon(2, 0, 0).await;
let dungeon_uid = dungeon.uid; let dungeon_uid = dungeon.uid;
let scene_uid = self.uid_mgr.borrow().next(); let scene_uid = self.uid_mgr.next();
let fresh_scene_info = SceneInfo::Fresh { let fresh_scene_info = SceneInfo::Fresh {
uid: scene_uid, uid: scene_uid,
id: 2, id: 2,
@ -777,7 +783,7 @@ impl DungeonManager {
dungeon.default_scene_uid = scene_uid; dungeon.default_scene_uid = scene_uid;
let mut player = self.player.borrow_mut(); let mut player = self.player.write().await;
let dungeon_collection = player.dungeon_collection.as_mut().unwrap(); let dungeon_collection = player.dungeon_collection.as_mut().unwrap();
dungeon_collection dungeon_collection
@ -811,14 +817,14 @@ impl DungeonManager {
) )
} }
fn create_base_dungeon( async fn create_base_dungeon(
&self, &self,
id: i32, id: i32,
back_scene_uid: u64, back_scene_uid: u64,
world_quest_id: i32, world_quest_id: i32,
) -> DungeonInfo { ) -> DungeonInfo {
let player = self.player.borrow(); let player = self.player.read().await;
let uid = self.uid_mgr.borrow().next(); let uid = self.uid_mgr.next();
DungeonInfo { DungeonInfo {
uid, uid,

View file

@ -4,43 +4,44 @@ use std::{
}; };
use crate::game::data; use crate::game::data;
use atomic_refcell::AtomicRefCell;
use protocol::*; use protocol::*;
use qwer::{phashmap, phashset, PropertyHashMap, PropertyHashSet}; use qwer::{phashmap, phashset, PropertyHashMap, PropertyHashSet};
use tokio::sync::RwLock;
pub struct HollowGridManager { pub struct HollowGridManager {
player: Arc<AtomicRefCell<PlayerInfo>>, player: Arc<RwLock<PlayerInfo>>,
map: AtomicRefCell<Option<HollowGridMapProtocolInfo>>, map: RwLock<Option<HollowGridMapProtocolInfo>>,
events: AtomicRefCell<HashMap<u64, EventInfo>>, events: RwLock<HashMap<u64, EventInfo>>,
} }
impl HollowGridManager { impl HollowGridManager {
pub fn new(player: Arc<AtomicRefCell<PlayerInfo>>) -> Self { pub fn new(player: Arc<RwLock<PlayerInfo>>) -> Self {
Self { Self {
player, player,
map: AtomicRefCell::new(None), map: RwLock::new(None),
events: AtomicRefCell::new(HashMap::new()), events: RwLock::new(HashMap::new()),
} }
} }
pub fn get_cur_position_in_hollow(&self) -> u16 { pub async fn get_cur_position_in_hollow(&self) -> u16 {
self.map.borrow().as_ref().unwrap().start_grid self.map.read().await.as_ref().unwrap().start_grid
} }
pub fn move_to( pub async fn move_to(
&self, &self,
destination_grid: u16, destination_grid: u16,
scene_uid: u64, scene_uid: u64,
) -> (PtcHollowGridArg, Option<PtcSyncHollowEventInfoArg>) { ) -> (PtcHollowGridArg, Option<PtcSyncHollowEventInfoArg>) {
let mut map = self.map.borrow_mut(); let mut map = self.map.write().await;
let map = map.as_mut().unwrap(); let map = map.as_mut().unwrap();
map.start_grid = destination_grid; map.start_grid = destination_grid;
let grid = map.grids.get_mut(&destination_grid).unwrap(); let grid = map.grids.get_mut(&destination_grid).unwrap();
self.update_position_to_scene(scene_uid, destination_grid); self.update_position_to_scene(scene_uid, destination_grid)
.await;
let mut events = self.events.borrow_mut(); let mut events = self.events.write().await;
let sync_event_info = let sync_event_info =
if let Entry::Vacant(entry) = events.entry(u64::from(destination_grid)) { if let Entry::Vacant(entry) = events.entry(u64::from(destination_grid)) {
let event_info = EventInfo { let event_info = EventInfo {
@ -81,7 +82,7 @@ impl HollowGridManager {
( (
PtcHollowGridArg { PtcHollowGridArg {
player_uid: self.player.borrow().uid.unwrap(), player_uid: self.player.read().await.uid.unwrap(),
is_partial: true, is_partial: true,
scene_uid, scene_uid,
hollow_level: 1, hollow_level: 1,
@ -91,8 +92,8 @@ impl HollowGridManager {
) )
} }
pub fn battle_finished(&self) -> (PtcSyncHollowEventInfoArg, bool) { pub async fn battle_finished(&self) -> (PtcSyncHollowEventInfoArg, bool) {
let map = self.map.borrow(); let map = self.map.read().await;
let map = map.as_ref().unwrap(); let map = map.as_ref().unwrap();
let cur_grid = map.grids.get(&map.start_grid).unwrap(); let cur_grid = map.grids.get(&map.start_grid).unwrap();
@ -129,16 +130,16 @@ impl HollowGridManager {
) )
} }
pub fn get_cur_event_template_id(&self) -> i32 { pub async fn get_cur_event_template_id(&self) -> i32 {
let map = self.map.borrow(); let map = self.map.read().await;
let map = map.as_ref().unwrap(); let map = map.as_ref().unwrap();
let cur_grid = map.grids.get(&map.start_grid).unwrap(); let cur_grid = map.grids.get(&map.start_grid).unwrap();
cur_grid.grid.event_graph_info.hollow_event_template_id cur_grid.grid.event_graph_info.hollow_event_template_id
} }
fn update_position_to_scene(&self, scene_uid: u64, pos: u16) { async fn update_position_to_scene(&self, scene_uid: u64, pos: u16) {
let mut player = self.player.borrow_mut(); let mut player = self.player.write().await;
let scene = player let scene = player
.dungeon_collection .dungeon_collection
.as_mut() .as_mut()
@ -165,7 +166,7 @@ impl HollowGridManager {
} }
} }
pub fn run_event_graph( pub async fn run_event_graph(
&self, &self,
event_graph_uid: u64, event_graph_uid: u64,
_event_id: i32, _event_id: i32,
@ -177,12 +178,12 @@ impl HollowGridManager {
bool, bool,
) { ) {
let (player_uid, scene_uid) = { let (player_uid, scene_uid) = {
let player = self.player.borrow(); let player = self.player.read().await;
(player.uid.unwrap(), player.scene_uid.unwrap()) (player.uid.unwrap(), player.scene_uid.unwrap())
}; };
let mut map = self.map.borrow_mut(); let mut map = self.map.write().await;
let map = map.as_mut().unwrap(); let map = map.as_mut().unwrap();
let mut trigger_battle_id = None; let mut trigger_battle_id = None;
@ -216,6 +217,7 @@ impl HollowGridManager {
if let Some(action) = actions.get(index as usize) { if let Some(action) = actions.get(index as usize) {
last_exec_type = action["$type"].as_str().unwrap(); last_exec_type = action["$type"].as_str().unwrap();
// ugly, we have to parse these configs properly
match action["$type"].as_str().unwrap() { match action["$type"].as_str().unwrap() {
"Share.CConfigSetMapState" => { "Share.CConfigSetMapState" => {
let x = action["X"].as_i64().unwrap() as u16; let x = action["X"].as_i64().unwrap() as u16;
@ -320,19 +322,23 @@ impl HollowGridManager {
) )
} }
pub fn sync_hollow_maps(&self, player_uid: u64, scene_uid: u64) -> PtcSyncHollowGridMapsArg { pub async fn sync_hollow_maps(
&self,
player_uid: u64,
scene_uid: u64,
) -> PtcSyncHollowGridMapsArg {
PtcSyncHollowGridMapsArg { PtcSyncHollowGridMapsArg {
player_uid, player_uid,
scene_uid, scene_uid,
hollow_level: 1, hollow_level: 1,
main_map: self.map.borrow().clone().unwrap(), main_map: self.map.read().await.clone().unwrap(),
time_period: TimePeriodType::Random, time_period: TimePeriodType::Random,
weather: WeatherType::Random, weather: WeatherType::Random,
} }
} }
pub fn init_default_map(&self) { pub async fn init_default_map(&self) {
*self.map.borrow_mut() = Some(HollowGridMapProtocolInfo { *self.map.write().await = Some(HollowGridMapProtocolInfo {
row: 5, row: 5,
col: 11, col: 11,
start_grid: 22, start_grid: 22,

View file

@ -1,30 +1,27 @@
use atomic_refcell::AtomicRefCell;
use protocol::{ItemInfo, PlayerInfo}; use protocol::{ItemInfo, PlayerInfo};
use qwer::{phashmap, PropertyHashMap}; use qwer::{phashmap, PropertyHashMap};
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::RwLock;
use crate::game::{util, PlayerOperationResult}; use crate::game::{util, PlayerOperationResult};
use super::UniqueIDManager; use super::UniqueIDManager;
pub struct ItemManager { pub struct ItemManager {
uid_mgr: Arc<AtomicRefCell<UniqueIDManager>>, uid_mgr: Arc<UniqueIDManager>,
player_info: Arc<AtomicRefCell<PlayerInfo>>, player_info: Arc<RwLock<PlayerInfo>>,
} }
impl ItemManager { impl ItemManager {
pub fn new( pub fn new(uid_mgr: Arc<UniqueIDManager>, player_info: Arc<RwLock<PlayerInfo>>) -> Self {
uid_mgr: Arc<AtomicRefCell<UniqueIDManager>>,
player_info: Arc<AtomicRefCell<PlayerInfo>>,
) -> Self {
Self { Self {
uid_mgr, uid_mgr,
player_info, player_info,
} }
} }
pub fn add_resource(&self, currency_id: i32, amount: i32) -> PlayerOperationResult<i32> { pub async fn add_resource(&self, currency_id: i32, amount: i32) -> PlayerOperationResult<i32> {
let mut player_info = self.player_info.borrow_mut(); let mut player_info = self.player_info.write().await;
for (uid, item) in player_info.items.as_mut().unwrap() { for (uid, item) in player_info.items.as_mut().unwrap() {
if let ItemInfo::Resource { id, count, .. } = item { if let ItemInfo::Resource { id, count, .. } = item {
@ -45,7 +42,7 @@ impl ItemManager {
} }
} }
let uid = self.uid_mgr.borrow().next(); let uid = self.uid_mgr.next();
let item = ItemInfo::Resource { let item = ItemInfo::Resource {
uid, uid,
id: currency_id, id: currency_id,
@ -69,8 +66,8 @@ impl ItemManager {
) )
} }
pub fn unlock_avatar(&self, id: i32) -> PlayerOperationResult<u64> { pub async fn unlock_avatar(&self, id: i32) -> PlayerOperationResult<u64> {
let uid = self.uid_mgr.borrow().next(); let uid = self.uid_mgr.next();
let avatar = ItemInfo::Avatar { let avatar = ItemInfo::Avatar {
uid, uid,
@ -89,10 +86,10 @@ impl ItemManager {
}; };
// Unlock & equip default weapon // Unlock & equip default weapon
let weapon_uid = *self.unlock_weapon(10012).unwrap(); let weapon_uid = *self.unlock_weapon(10012).await.unwrap();
self.equip_weapon(weapon_uid, uid); self.equip_weapon(weapon_uid, uid).await;
let mut player_info = self.player_info.borrow_mut(); let mut player_info = self.player_info.write().await;
let items = player_info.items.as_mut().unwrap(); let items = player_info.items.as_mut().unwrap();
items.insert(uid, avatar.clone()); items.insert(uid, avatar.clone());
@ -111,11 +108,11 @@ impl ItemManager {
) )
} }
pub fn unlock_weapon(&self, id: i32) -> PlayerOperationResult<u64> { pub async fn unlock_weapon(&self, id: i32) -> PlayerOperationResult<u64> {
let mut player_info = self.player_info.borrow_mut(); let mut player_info = self.player_info.write().await;
let items = player_info.items.as_mut().unwrap(); let items = player_info.items.as_mut().unwrap();
let uid = self.uid_mgr.borrow().next(); let uid = self.uid_mgr.next();
let weapon = ItemInfo::Weapon { let weapon = ItemInfo::Weapon {
uid, uid,
@ -144,12 +141,12 @@ impl ItemManager {
) )
} }
pub fn equip_weapon( pub async fn equip_weapon(
&self, &self,
weapon_uid: u64, weapon_uid: u64,
equip_avatar_uid: u64, equip_avatar_uid: u64,
) -> PlayerOperationResult<bool> { ) -> PlayerOperationResult<bool> {
let mut player_info = self.player_info.borrow_mut(); let mut player_info = self.player_info.write().await;
let items = player_info.items.as_mut().unwrap(); let items = player_info.items.as_mut().unwrap();
let Some(ItemInfo::Weapon { avatar_uid, .. }) = items.get_mut(&weapon_uid) else { let Some(ItemInfo::Weapon { avatar_uid, .. }) = items.get_mut(&weapon_uid) else {

View file

@ -1,23 +1,23 @@
use atomic_refcell::AtomicRefCell;
use protocol::{AccountInfo, PlayerInfo, PropertyBlob}; use protocol::{AccountInfo, PlayerInfo, PropertyBlob};
use qwer::OctData; use qwer::OctData;
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::RwLock;
const CLIENT_PROP_FLAG: u16 = 1; const CLIENT_PROP_FLAG: u16 = 1;
#[derive(Default)] #[derive(Default)]
pub struct PropertyManager { pub struct PropertyManager {
pub account_info: Arc<AtomicRefCell<AccountInfo>>, pub account_info: Arc<RwLock<AccountInfo>>,
pub player_info: Arc<AtomicRefCell<PlayerInfo>>, pub player_info: Arc<RwLock<PlayerInfo>>,
} }
impl PropertyManager { impl PropertyManager {
pub fn serialize_account_info(&self) -> PropertyBlob { pub async fn serialize_account_info(&self) -> PropertyBlob {
Self::serialize_property(&*self.account_info.borrow()).unwrap() Self::serialize_property(&*self.account_info.read().await).unwrap()
} }
pub fn serialize_player_info(&self) -> PropertyBlob { pub async fn serialize_player_info(&self) -> PropertyBlob {
Self::serialize_property(&*self.player_info.borrow()).unwrap() Self::serialize_property(&*self.player_info.read().await).unwrap()
} }
pub fn serialize_property(prop: &impl OctData) -> Result<PropertyBlob, std::io::Error> { pub fn serialize_property(prop: &impl OctData) -> Result<PropertyBlob, std::io::Error> {

View file

@ -1,7 +1,7 @@
use std::sync::Arc; use std::sync::Arc;
use atomic_refcell::AtomicRefCell;
use qwer::PropertyDoubleKeyHashMap; use qwer::PropertyDoubleKeyHashMap;
use tokio::sync::RwLock;
use crate::game::PlayerOperationResult; use crate::game::PlayerOperationResult;
@ -9,22 +9,20 @@ use super::UniqueIDManager;
use protocol::*; use protocol::*;
pub struct QuestManager { pub struct QuestManager {
uid_mgr: Arc<AtomicRefCell<UniqueIDManager>>, uid_mgr: Arc<UniqueIDManager>,
player: Arc<AtomicRefCell<PlayerInfo>>, player: Arc<RwLock<PlayerInfo>>,
} }
impl QuestManager { impl QuestManager {
pub fn new( pub fn new(uid_mgr: Arc<UniqueIDManager>, player: Arc<RwLock<PlayerInfo>>) -> Self {
uid_mgr: Arc<AtomicRefCell<UniqueIDManager>>,
player: Arc<AtomicRefCell<PlayerInfo>>,
) -> Self {
Self { uid_mgr, player } Self { uid_mgr, player }
} }
pub fn add_world_quest(&self, quest: QuestInfo) -> PlayerOperationResult<u64> { pub async fn add_world_quest(&self, quest: QuestInfo) -> PlayerOperationResult<u64> {
let mut world_quest_collection_uid = self let mut world_quest_collection_uid = self
.player .player
.borrow() .read()
.await
.quest_data .quest_data
.as_ref() .as_ref()
.unwrap() .unwrap()
@ -32,9 +30,10 @@ impl QuestManager {
.unwrap(); .unwrap();
if world_quest_collection_uid == 0 { if world_quest_collection_uid == 0 {
world_quest_collection_uid = self.uid_mgr.borrow().next(); world_quest_collection_uid = self.uid_mgr.next();
self.player self.player
.borrow_mut() .write()
.await
.quest_data .quest_data
.as_mut() .as_mut()
.unwrap() .unwrap()
@ -43,14 +42,15 @@ impl QuestManager {
} }
self.add_quest_to_collection(world_quest_collection_uid, quest) self.add_quest_to_collection(world_quest_collection_uid, quest)
.await
} }
pub fn add_quest_to_collection( pub async fn add_quest_to_collection(
&self, &self,
collection_uid: u64, collection_uid: u64,
mut quest: QuestInfo, mut quest: QuestInfo,
) -> PlayerOperationResult<u64> { ) -> PlayerOperationResult<u64> {
let mut player = self.player.borrow_mut(); let mut player = self.player.write().await;
let quest_data = player.quest_data.as_mut().unwrap(); let quest_data = player.quest_data.as_mut().unwrap();
quest.set_collection_uid(collection_uid); quest.set_collection_uid(collection_uid);

View file

@ -1,34 +1,34 @@
use std::{collections::HashMap, sync::Arc}; use std::{collections::HashMap, sync::Arc};
use atomic_refcell::AtomicRefCell;
use protocol::*; use protocol::*;
use qwer::{phashmap, PropertyHashMap}; use qwer::{phashmap, PropertyHashMap};
use tokio::sync::RwLock;
use super::UniqueIDManager; use super::UniqueIDManager;
pub struct SceneUnitManager { pub struct SceneUnitManager {
uid_mgr: Arc<AtomicRefCell<UniqueIDManager>>, uid_mgr: Arc<UniqueIDManager>,
units: AtomicRefCell<HashMap<u64, SceneUnitProtocolInfo>>, units: RwLock<HashMap<u64, SceneUnitProtocolInfo>>,
} }
impl SceneUnitManager { impl SceneUnitManager {
pub fn new(uid_mgr: Arc<AtomicRefCell<UniqueIDManager>>) -> Self { pub fn new(uid_mgr: Arc<UniqueIDManager>) -> Self {
Self { Self {
uid_mgr, uid_mgr,
units: AtomicRefCell::new(HashMap::new()), units: RwLock::new(HashMap::new()),
} }
} }
pub fn create_npc( pub async fn create_npc(
&self, &self,
id: i32, id: i32,
tag: i32, tag: i32,
quest_id: i32, quest_id: i32,
interacts_info: PropertyHashMap<i32, InteractInfo>, interacts_info: PropertyHashMap<i32, InteractInfo>,
) -> u64 { ) -> u64 {
let uid = self.uid_mgr.borrow().next(); let uid = self.uid_mgr.next();
self.units.borrow_mut().insert( self.units.write().await.insert(
uid, uid,
SceneUnitProtocolInfo::NpcProtocolInfo { SceneUnitProtocolInfo::NpcProtocolInfo {
uid, uid,
@ -42,11 +42,11 @@ impl SceneUnitManager {
uid uid
} }
pub fn get(&self, uid: u64) -> SceneUnitProtocolInfo { pub async fn get(&self, uid: u64) -> SceneUnitProtocolInfo {
self.units.borrow().get(&uid).unwrap().clone() self.units.read().await.get(&uid).unwrap().clone()
} }
pub fn sync(&self, scene_uid: u64, section_id: i32) -> PtcSyncSceneUnitArg { pub async fn sync(&self, scene_uid: u64, section_id: i32) -> PtcSyncSceneUnitArg {
PtcSyncSceneUnitArg { PtcSyncSceneUnitArg {
scene_uid, scene_uid,
section_id, section_id,
@ -54,7 +54,8 @@ impl SceneUnitManager {
removed_scene_units: Vec::new(), removed_scene_units: Vec::new(),
scene_units: self scene_units: self
.units .units
.borrow() .read()
.await
.iter() .iter()
.map(|(_, unit)| unit.clone()) .map(|(_, unit)| unit.clone())
.collect(), .collect(),
@ -64,7 +65,7 @@ impl SceneUnitManager {
// TODO: partial_sync for newly added/removed units // TODO: partial_sync for newly added/removed units
// currently hardcoded for Main City section 2 // currently hardcoded for Main City section 2
pub fn add_default_units(&self) { pub async fn add_default_units(&self) {
self.create_npc( self.create_npc(
100171011, 100171011,
3, 3,
@ -83,7 +84,8 @@ impl SceneUnitManager {
phashmap![(0, String::new())] phashmap![(0, String::new())]
) )
)], )],
); )
.await;
self.create_npc( self.create_npc(
100171011, 100171011,
@ -103,7 +105,8 @@ impl SceneUnitManager {
phashmap![(0, String::new())] phashmap![(0, String::new())]
) )
)], )],
); )
.await;
self.create_npc( self.create_npc(
100171011, 100171011,
@ -123,7 +126,8 @@ impl SceneUnitManager {
phashmap![(0, String::new())] phashmap![(0, String::new())]
) )
)], )],
); )
.await;
self.create_npc( self.create_npc(
100171011, 100171011,
@ -143,7 +147,8 @@ impl SceneUnitManager {
phashmap![(1001, String::from("A"))] phashmap![(1001, String::from("A"))]
) )
)], )],
); )
.await;
self.create_npc( self.create_npc(
100171011, 100171011,
@ -163,7 +168,8 @@ impl SceneUnitManager {
phashmap![(1005, String::from("A"))] phashmap![(1005, String::from("A"))]
) )
)], )],
); )
.await;
self.create_npc( self.create_npc(
100173001, 100173001,
@ -183,7 +189,8 @@ impl SceneUnitManager {
phashmap![(2028, String::from("A"))] phashmap![(2028, String::from("A"))]
) )
)], )],
); )
.await;
self.create_npc( self.create_npc(
100172011, 100172011,
@ -203,9 +210,10 @@ impl SceneUnitManager {
phashmap![(2000, String::from("A")), (2052, String::from("B"))] phashmap![(2000, String::from("A")), (2052, String::from("B"))]
) )
)], )],
); )
.await;
self.create_npc(100172081, 2052, 0, phashmap![]); self.create_npc(100172081, 2052, 0, phashmap![]).await;
} }
} }

View file

@ -1,22 +1,22 @@
use std::sync::Arc; use std::sync::Arc;
use atomic_refcell::AtomicRefCell;
use protocol::*; use protocol::*;
use qwer::PropertyHashSet; use qwer::PropertyHashSet;
use tokio::sync::RwLock;
use crate::game::PlayerOperationResult; use crate::game::PlayerOperationResult;
pub struct UnlockManager { pub struct UnlockManager {
player: Arc<AtomicRefCell<PlayerInfo>>, player: Arc<RwLock<PlayerInfo>>,
} }
impl UnlockManager { impl UnlockManager {
pub fn new(player: Arc<AtomicRefCell<PlayerInfo>>) -> Self { pub fn new(player: Arc<RwLock<PlayerInfo>>) -> Self {
Self { player } Self { player }
} }
pub fn unlock(&self, unlock_id: i32) -> PlayerOperationResult<PtcUnlockArg> { pub async fn unlock(&self, unlock_id: i32) -> PlayerOperationResult<PtcUnlockArg> {
let mut player = self.player.borrow_mut(); let mut player = self.player.write().await;
let unlock_info = player.unlock_info.as_mut().unwrap(); let unlock_info = player.unlock_info.as_mut().unwrap();
unlock_info unlock_info

View file

@ -1,29 +1,29 @@
use std::collections::HashSet; use std::collections::HashSet;
use std::sync::Arc; use std::sync::Arc;
use atomic_refcell::AtomicRefCell;
use qwer::{PropertyDoubleKeyHashMap, PropertyHashSet}; use qwer::{PropertyDoubleKeyHashMap, PropertyHashSet};
use tokio::sync::RwLock;
use crate::game::PlayerOperationResult; use crate::game::PlayerOperationResult;
use protocol::*; use protocol::*;
pub struct YorozuyaQuestManager { pub struct YorozuyaQuestManager {
player: Arc<AtomicRefCell<PlayerInfo>>, player: Arc<RwLock<PlayerInfo>>,
} }
impl YorozuyaQuestManager { impl YorozuyaQuestManager {
pub fn new(player: Arc<AtomicRefCell<PlayerInfo>>) -> Self { pub fn new(player: Arc<RwLock<PlayerInfo>>) -> Self {
Self { player } Self { player }
} }
pub fn add_hollow_quest( pub async fn add_hollow_quest(
&self, &self,
yorozuya_collection_id: i32, yorozuya_collection_id: i32,
hollow_quest_type: HollowQuestType, hollow_quest_type: HollowQuestType,
id: i32, id: i32,
) -> PlayerOperationResult<i32> { ) -> PlayerOperationResult<i32> {
let mut player = self.player.borrow_mut(); let mut player = self.player.write().await;
let yorozuya = player.yorozuya_info.as_mut().unwrap(); let yorozuya = player.yorozuya_info.as_mut().unwrap();
let hollow_quests = yorozuya.hollow_quests.as_mut().unwrap(); let hollow_quests = yorozuya.hollow_quests.as_mut().unwrap();

View file

@ -13,11 +13,19 @@ pub async fn on_rpc_hollow_move(
tracing::info!("Hollow movement {:?}", &arg); tracing::info!("Hollow movement {:?}", &arg);
let destination_pos = *arg.positions.last().unwrap(); let destination_pos = *arg.positions.last().unwrap();
let scene_uid = session.get_player().scene_uid.unwrap(); let scene_uid = session
.ns_prop_mgr
.player_info
.read()
.await
.scene_uid
.unwrap();
let hollow_grid_manager = session.context.hollow_grid_manager.borrow(); let (ptc_hollow_grid, ptc_sync_hollow_event) = session
let (ptc_hollow_grid, ptc_sync_hollow_event) = .context
hollow_grid_manager.move_to(destination_pos, scene_uid); .hollow_grid_manager
.move_to(destination_pos, scene_uid)
.await;
session session
.send_rpc_arg(PTC_HOLLOW_GRID_ID, &ptc_hollow_grid) .send_rpc_arg(PTC_HOLLOW_GRID_ID, &ptc_hollow_grid)
@ -50,19 +58,19 @@ pub async fn on_rpc_end_battle(
) -> Result<RpcEndBattleRet> { ) -> Result<RpcEndBattleRet> {
tracing::info!("RpcEndBattle: {:?}", &arg); tracing::info!("RpcEndBattle: {:?}", &arg);
let player_uid = session.get_player_uid(); let player_uid = session.get_player_uid().await;
let hollow_grid_manager = session.context.hollow_grid_manager.borrow(); let (sync_event, hollow_finished) = session.context.hollow_grid_manager.battle_finished().await;
let (sync_event, hollow_finished) = hollow_grid_manager.battle_finished();
if !hollow_finished { if !hollow_finished {
session session
.send_rpc_arg(PTC_SYNC_HOLLOW_EVENT_INFO_ID, &sync_event) .send_rpc_arg(PTC_SYNC_HOLLOW_EVENT_INFO_ID, &sync_event)
.await?; .await?;
} else { } else {
let dungeon_manager = session.context.dungeon_manager.borrow(); let _ = *session
let _ = *dungeon_manager .context
.dungeon_manager
.hollow_finished() .hollow_finished()
.await
.send_changes(session) .send_changes(session)
.await?; .await?;
@ -79,9 +87,11 @@ pub async fn on_rpc_end_battle(
.await?; .await?;
} }
let dungeon_manager = session.context.dungeon_manager.borrow(); let ptc_enter_scene = session
let ptc_enter_scene = dungeon_manager .context
.dungeon_manager
.leave_battle() .leave_battle()
.await
.unwrap() .unwrap()
.send_changes(session) .send_changes(session)
.await? .await?
@ -90,14 +100,25 @@ pub async fn on_rpc_end_battle(
session session
.send_rpc_arg( .send_rpc_arg(
PTC_SYNC_HOLLOW_GRID_MAPS_ID, PTC_SYNC_HOLLOW_GRID_MAPS_ID,
&hollow_grid_manager.sync_hollow_maps(player_uid, dungeon_manager.get_cur_scene_uid()), &session
.context
.hollow_grid_manager
.sync_hollow_maps(
player_uid,
session.context.dungeon_manager.get_cur_scene_uid().await,
)
.await,
) )
.await?; .await?;
let ptc_position_in_hollow_changed = PtcPositionInHollowChangedArg { let ptc_position_in_hollow_changed = PtcPositionInHollowChangedArg {
player_uid, player_uid,
hollow_level: 1, hollow_level: 1,
position: hollow_grid_manager.get_cur_position_in_hollow(), position: session
.context
.hollow_grid_manager
.get_cur_position_in_hollow()
.await,
}; };
session session
@ -112,7 +133,11 @@ pub async fn on_rpc_end_battle(
.await?; .await?;
Ok(RpcEndBattleRet::new( Ok(RpcEndBattleRet::new(
hollow_grid_manager.get_cur_event_template_id(), session
.context
.hollow_grid_manager
.get_cur_event_template_id()
.await,
HashMap::new(), HashMap::new(),
)) ))
} }
@ -123,7 +148,13 @@ pub async fn on_rpc_run_hollow_event_graph(
) -> Result<RpcRunHollowEventGraphRet> { ) -> Result<RpcRunHollowEventGraphRet> {
tracing::info!("Run hollow event graph {:?}", arg); tracing::info!("Run hollow event graph {:?}", arg);
let scene_uid = session.get_player().scene_uid.unwrap(); let scene_uid = session
.ns_prop_mgr
.player_info
.read()
.await
.scene_uid
.unwrap();
if arg.event_graph_uid == 3405096459205834 { if arg.event_graph_uid == 3405096459205834 {
// Perform (cutscene) // Perform (cutscene)
@ -148,8 +179,11 @@ pub async fn on_rpc_run_hollow_event_graph(
.send_rpc_arg(PTC_SYNC_HOLLOW_EVENT_INFO_ID, &finish_perform) .send_rpc_arg(PTC_SYNC_HOLLOW_EVENT_INFO_ID, &finish_perform)
.await?; .await?;
let hollow_grid_manager = session.context.hollow_grid_manager.borrow(); let (ptc_hollow_grid, ptc_sync_hollow_event) = session
let (ptc_hollow_grid, ptc_sync_hollow_event) = hollow_grid_manager.move_to(22, scene_uid); .context
.hollow_grid_manager
.move_to(22, scene_uid)
.await;
session session
.send_rpc_arg(PTC_HOLLOW_GRID_ID, &ptc_hollow_grid) .send_rpc_arg(PTC_HOLLOW_GRID_ID, &ptc_hollow_grid)
@ -160,13 +194,11 @@ pub async fn on_rpc_run_hollow_event_graph(
.await?; .await?;
} }
} else { } else {
let hollow_grid_manager = session.context.hollow_grid_manager.borrow(); let (sync_hollow_event, hollow_grid, trigger_battle_id, hollow_finished) = session
let (sync_hollow_event, hollow_grid, trigger_battle_id, hollow_finished) = .context
hollow_grid_manager.run_event_graph( .hollow_grid_manager
arg.event_graph_uid, .run_event_graph(arg.event_graph_uid, arg.event_id, arg.move_path.clone())
arg.event_id, .await;
arg.move_path.clone(),
);
if !hollow_finished { if !hollow_finished {
session session
@ -178,9 +210,11 @@ pub async fn on_rpc_run_hollow_event_graph(
.await?; .await?;
if hollow_finished { if hollow_finished {
let dungeon_manager = session.context.dungeon_manager.borrow(); let _ = *session
let _ = *dungeon_manager .context
.dungeon_manager
.hollow_finished() .hollow_finished()
.await
.send_changes(session) .send_changes(session)
.await?; .await?;
@ -198,17 +232,30 @@ pub async fn on_rpc_run_hollow_event_graph(
} }
if let Some(trigger_battle_id) = trigger_battle_id { if let Some(trigger_battle_id) = trigger_battle_id {
let dungeon_manager = session.context.dungeon_manager.borrow(); let hollow_uid = *session
let hollow_uid = *session.get_player().scene_uid.as_ref().unwrap(); .ns_prop_mgr
let battle_scene_uid = *dungeon_manager .player_info
.read()
.await
.scene_uid
.as_ref()
.unwrap();
let battle_scene_uid = *session
.context
.dungeon_manager
.create_fight(trigger_battle_id, hollow_uid) .create_fight(trigger_battle_id, hollow_uid)
.await
.send_changes(session) .send_changes(session)
.await?; .await?;
let ptc_position_in_hollow_changed = PtcPositionInHollowChangedArg { let ptc_position_in_hollow_changed = PtcPositionInHollowChangedArg {
player_uid: 1337, player_uid: 1337,
hollow_level: 1, hollow_level: 1,
position: hollow_grid_manager.get_cur_position_in_hollow(), position: session
.context
.hollow_grid_manager
.get_cur_position_in_hollow()
.await,
}; };
session session
@ -221,8 +268,11 @@ pub async fn on_rpc_run_hollow_event_graph(
session session
.send_rpc_arg( .send_rpc_arg(
PTC_ENTER_SCENE_ID, PTC_ENTER_SCENE_ID,
dungeon_manager session
.context
.dungeon_manager
.enter_battle(battle_scene_uid) .enter_battle(battle_scene_uid)
.await
.send_changes(session) .send_changes(session)
.await?, .await?,
) )
@ -241,7 +291,7 @@ pub async fn on_rpc_start_hollow_quest(
// Set avatar HP properties // Set avatar HP properties
for (_idx, avatar_uid) in &arg.avatar_map { for (_idx, avatar_uid) in &arg.avatar_map {
let player_info = session.get_player(); let player_info = session.ns_prop_mgr.player_info.read().await;
let items = player_info.items.as_ref().unwrap(); let items = player_info.items.as_ref().unwrap();
let Some(ItemInfo::Avatar { id, .. }) = items let Some(ItemInfo::Avatar { id, .. }) = items
.iter() .iter()
@ -269,21 +319,23 @@ pub async fn on_rpc_start_hollow_quest(
.await?; .await?;
} }
let dungeon_manager = session.context.dungeon_manager.borrow();
let avatars = arg let avatars = arg
.avatar_map .avatar_map
.iter() .iter()
.sorted_by_key(|kv| kv.0) .sorted_by_key(|kv| kv.0)
.map(|(_idx, uid)| *uid) .map(|(_idx, uid)| *uid)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let (dungeon_uid, scene_uid) = *dungeon_manager let (dungeon_uid, scene_uid) = *session
.context
.dungeon_manager
.create_hollow(10001, 10010001, &avatars) .create_hollow(10001, 10010001, &avatars)
.await
.send_changes(session) .send_changes(session)
.await?; .await?;
let quest_manager = session.context.quest_manager.borrow(); session
quest_manager .context
.quest_manager
.add_quest_to_collection( .add_quest_to_collection(
dungeon_uid, dungeon_uid,
QuestInfo::DungeonInner { QuestInfo::DungeonInner {
@ -298,29 +350,40 @@ pub async fn on_rpc_start_hollow_quest(
sort_id: 2000, sort_id: 2000,
}, },
) )
.await
.send_changes(session) .send_changes(session)
.await?; .await?;
let ptc_enter_scene = dungeon_manager let ptc_enter_scene = session
.enter_scene(scene_uid)? .context
.dungeon_manager
.enter_scene(scene_uid)
.await?
.send_changes(session) .send_changes(session)
.await? .await?
.clone(); .clone();
let hollow_grid_manager = session.context.hollow_grid_manager.borrow(); session.context.hollow_grid_manager.init_default_map().await;
hollow_grid_manager.init_default_map();
session session
.send_rpc_arg( .send_rpc_arg(
PTC_SYNC_HOLLOW_GRID_MAPS_ID, PTC_SYNC_HOLLOW_GRID_MAPS_ID,
&hollow_grid_manager.sync_hollow_maps(session.get_player_uid(), scene_uid), &session
.context
.hollow_grid_manager
.sync_hollow_maps(session.get_player_uid().await, scene_uid)
.await,
) )
.await?; .await?;
let ptc_position_in_hollow_changed = PtcPositionInHollowChangedArg { let ptc_position_in_hollow_changed = PtcPositionInHollowChangedArg {
player_uid: 1337, player_uid: 1337,
hollow_level: 1, hollow_level: 1,
position: hollow_grid_manager.get_cur_position_in_hollow(), position: session
.context
.hollow_grid_manager
.get_cur_position_in_hollow()
.await,
}; };
session session

View file

@ -7,10 +7,11 @@ const DEFAULT_ACCOUNT_ID: u64 = 1337;
pub async fn on_rpc_login(session: &NetworkSession, arg: &RpcLoginArg) -> Result<RpcLoginRet> { pub async fn on_rpc_login(session: &NetworkSession, arg: &RpcLoginArg) -> Result<RpcLoginRet> {
tracing::info!("Received rpc login arg: {}", arg.account_name); tracing::info!("Received rpc login arg: {}", arg.account_name);
*session.get_account_mut() = util::create_default_account(DEFAULT_ACCOUNT_ID); *session.ns_prop_mgr.account_info.write().await =
util::create_default_account(DEFAULT_ACCOUNT_ID);
Ok(RpcLoginRet::new( Ok(RpcLoginRet::new(
session.ns_prop_mgr.serialize_account_info(), session.ns_prop_mgr.serialize_account_info().await,
)) ))
} }

View file

@ -6,7 +6,7 @@ pub async fn on_rpc_mod_nick_name(
) -> Result<RpcModNickNameRet> { ) -> Result<RpcModNickNameRet> {
tracing::info!("creating character"); tracing::info!("creating character");
let mut player = session.get_player_mut(); let mut player = session.ns_prop_mgr.player_info.write().await;
player.nick_name.replace(arg.nick_name.clone()); player.nick_name.replace(arg.nick_name.clone());
player.avatar_id.replace(arg.avatar_id); player.avatar_id.replace(arg.avatar_id);

View file

@ -14,8 +14,7 @@ pub async fn on_rpc_run_event_graph(
) -> Result<RpcRunEventGraphRet> { ) -> Result<RpcRunEventGraphRet> {
tracing::info!("RunEventGraph requested"); tracing::info!("RunEventGraph requested");
let scene_unit_mgr = session.context.scene_unit_manager.borrow(); let unit = session.context.scene_unit_manager.get(arg.owner_uid).await;
let unit = scene_unit_mgr.get(arg.owner_uid);
let SceneUnitProtocolInfo::NpcProtocolInfo { tag, id, .. } = unit; let SceneUnitProtocolInfo::NpcProtocolInfo { tag, id, .. } = unit;
let main_city_object = data::get_main_city_object(tag, id).unwrap(); let main_city_object = data::get_main_city_object(tag, id).unwrap();
@ -90,8 +89,7 @@ pub async fn on_rpc_interact_with_unit(
) -> Result<RpcInteractWithUnitRet> { ) -> Result<RpcInteractWithUnitRet> {
tracing::info!("InteractWithUnit"); tracing::info!("InteractWithUnit");
let scene_unit_mgr = session.context.scene_unit_manager.borrow(); let unit = session.context.scene_unit_manager.get(arg.unit_uid).await;
let unit = scene_unit_mgr.get(arg.unit_uid);
let SceneUnitProtocolInfo::NpcProtocolInfo { tag, id, .. } = unit; let SceneUnitProtocolInfo::NpcProtocolInfo { tag, id, .. } = unit;
let main_city_object = data::get_main_city_object(tag, id).unwrap(); let main_city_object = data::get_main_city_object(tag, id).unwrap();
@ -170,16 +168,20 @@ fn create_player(id: u64) -> PlayerInfo {
} }
pub async fn enter_main_city(session: &NetworkSession) -> Result<()> { pub async fn enter_main_city(session: &NetworkSession) -> Result<()> {
let dungeon_manager = session.context.dungeon_manager.borrow(); let hall_scene_uid = session
let scene_unit_mgr = session.context.scene_unit_manager.borrow(); .context
.dungeon_manager
let hall_scene_uid = dungeon_manager.get_default_scene_uid(); .get_default_scene_uid()
.await;
session session
.send_rpc_arg( .send_rpc_arg(
PTC_ENTER_SECTION_ID, PTC_ENTER_SECTION_ID,
dungeon_manager session
.context
.dungeon_manager
.enter_scene_section(hall_scene_uid, 2) .enter_scene_section(hall_scene_uid, 2)
.await
.unwrap(), .unwrap(),
) )
.await?; .await?;
@ -187,15 +189,22 @@ pub async fn enter_main_city(session: &NetworkSession) -> Result<()> {
session session
.send_rpc_arg( .send_rpc_arg(
PTC_SYNC_SCENE_UNIT_ID, PTC_SYNC_SCENE_UNIT_ID,
&scene_unit_mgr.sync(hall_scene_uid, 2), &session
.context
.scene_unit_manager
.sync(hall_scene_uid, 2)
.await,
) )
.await?; .await?;
session session
.send_rpc_arg( .send_rpc_arg(
PTC_ENTER_SCENE_ID, PTC_ENTER_SCENE_ID,
dungeon_manager session
.enter_main_city()? .context
.dungeon_manager
.enter_main_city()
.await?
.send_changes(session) .send_changes(session)
.await?, .await?,
) )
@ -206,76 +215,87 @@ pub async fn on_rpc_enter_world(
session: &NetworkSession, session: &NetworkSession,
_arg: &RpcEnterWorldArg, _arg: &RpcEnterWorldArg,
) -> Result<RpcEnterWorldRet> { ) -> Result<RpcEnterWorldRet> {
let account = session.get_account(); let account = session.ns_prop_mgr.account_info.read().await;
let id = *account.players.as_ref().unwrap().first().unwrap(); // get first id from list let id = *account.players.as_ref().unwrap().first().unwrap(); // get first id from list
*session.get_player_mut() = create_player(id); *session.ns_prop_mgr.player_info.write().await = create_player(id);
let item_manager = session.context.item_manager.borrow(); let item_manager = &session.context.item_manager;
item_manager.add_resource(501, 120).await;
item_manager.add_resource(501, 120); item_manager.add_resource(10, 228).await;
item_manager.add_resource(10, 228); item_manager.add_resource(100, 1337).await;
item_manager.add_resource(100, 1337);
for avatar_id in data::iter_avatar_config_collection() for avatar_id in data::iter_avatar_config_collection()
.filter(|c| c.camp != 0) .filter(|c| c.camp != 0)
.map(|c| c.id) .map(|c| c.id)
{ {
item_manager.unlock_avatar(avatar_id); item_manager.unlock_avatar(avatar_id).await;
} }
let unlock_manager = session.context.unlock_manager.borrow();
for unlock_id in data::iter_unlock_config_collection().map(|c| c.id) { for unlock_id in data::iter_unlock_config_collection().map(|c| c.id) {
unlock_manager.unlock(unlock_id); session.context.unlock_manager.unlock(unlock_id).await;
} }
let dungeon_manager = session.context.dungeon_manager.borrow(); session.context.dungeon_manager.create_hall(1).await;
dungeon_manager.create_hall(1); session.context.scene_unit_manager.add_default_units().await;
let scene_unit_mgr = session.context.scene_unit_manager.borrow();
scene_unit_mgr.add_default_units();
let quest_manager = session.context.quest_manager.borrow(); let quest_manager = session.context.quest_manager.clone();
quest_manager.add_world_quest(QuestInfo::MainCity { quest_manager
id: 10020002, .add_world_quest(QuestInfo::MainCity {
finished_count: 0, id: 10020002,
collection_uid: 0, finished_count: 0,
progress: 0, collection_uid: 0,
parent_quest_id: 0, progress: 0,
state: QuestState::InProgress, parent_quest_id: 0,
finish_condition_progress: phashmap![], state: QuestState::InProgress,
progress_time: 2111012, finish_condition_progress: phashmap![],
sort_id: 1000, progress_time: 2111012,
bound_npc_and_interact: phashmap![], sort_id: 1000,
}); bound_npc_and_interact: phashmap![],
})
.await;
quest_manager.add_world_quest(QuestInfo::Hollow { quest_manager
id: 10010002, .add_world_quest(QuestInfo::Hollow {
finished_count: 0, id: 10010002,
collection_uid: 3405096459205774, finished_count: 0,
progress: 0, collection_uid: 3405096459205774,
parent_quest_id: 0, progress: 0,
state: QuestState::Ready, parent_quest_id: 0,
sort_id: 1001, state: QuestState::Ready,
statistics: phashmap![], sort_id: 1001,
statistics_ext: pdkhashmap![], statistics: phashmap![],
acquired_hollow_challenge_reward: 0, statistics_ext: pdkhashmap![],
progress_time: 0, acquired_hollow_challenge_reward: 0,
finish_condition_progress: phashmap![], progress_time: 0,
dungeon_uid: 0, finish_condition_progress: phashmap![],
}); dungeon_uid: 0,
})
.await;
let yorozuya_quest_manager = session.context.yorozuya_quest_manager.borrow(); session
yorozuya_quest_manager.add_hollow_quest(102, HollowQuestType::SideQuest, 10010002); .context
.yorozuya_quest_manager
.add_hollow_quest(102, HollowQuestType::SideQuest, 10010002)
.await;
if CONFIGURATION.skip_tutorial { if CONFIGURATION.skip_tutorial {
Box::pin(enter_main_city(session)).await?; Box::pin(enter_main_city(session)).await?;
} else { } else {
let fresh_scene_uid = *dungeon_manager.create_fresh().unwrap(); let fresh_scene_uid = *session
.context
.dungeon_manager
.create_fresh()
.await
.unwrap();
session session
.send_rpc_arg( .send_rpc_arg(
PTC_ENTER_SCENE_ID, PTC_ENTER_SCENE_ID,
dungeon_manager session
.context
.dungeon_manager
.enter_scene(fresh_scene_uid) .enter_scene(fresh_scene_uid)
.await
.unwrap() .unwrap()
.unwrap(), .unwrap(),
) )
@ -293,6 +313,6 @@ pub async fn on_rpc_enter_world(
.await?; .await?;
Ok(RpcEnterWorldRet::new( Ok(RpcEnterWorldRet::new(
session.ns_prop_mgr.serialize_player_info(), session.ns_prop_mgr.serialize_player_info().await,
)) ))
} }

View file

@ -1,6 +1,4 @@
use anyhow::Result; use anyhow::Result;
use atomic_refcell::{AtomicRef, AtomicRefMut};
use protocol::*;
use qwer::{OctData, ProtocolHeader}; use qwer::{OctData, ProtocolHeader};
use std::io::Cursor; use std::io::Cursor;
use std::sync::Arc; use std::sync::Arc;
@ -37,24 +35,8 @@ impl NetworkSession {
self.client_socket.lock().await self.client_socket.lock().await
} }
pub fn get_player_uid(&self) -> u64 { pub async fn get_player_uid(&self) -> u64 {
self.get_player().uid.unwrap() self.ns_prop_mgr.player_info.read().await.uid.unwrap()
}
pub fn get_account(&self) -> AtomicRef<AccountInfo> {
self.ns_prop_mgr.account_info.borrow()
}
pub fn get_player(&self) -> AtomicRef<PlayerInfo> {
self.ns_prop_mgr.player_info.borrow()
}
pub fn get_account_mut(&self) -> AtomicRefMut<'_, AccountInfo> {
self.ns_prop_mgr.account_info.try_borrow_mut().unwrap()
}
pub fn get_player_mut(&self) -> AtomicRefMut<'_, PlayerInfo> {
self.ns_prop_mgr.player_info.try_borrow_mut().unwrap()
} }
pub async fn run(&mut self) -> Result<()> { pub async fn run(&mut self) -> Result<()> {