Use parking_lot::RwLock instead of tokio::sync::RwLock

See https://docs.rs/tokio/latest/tokio/sync/struct.Mutex.html\#which-kind-of-mutex-should-you-use (same applies for RwLock). We don't need an asynchronous Mutex/RwLock for data.
This commit is contained in:
xeon 2024-06-02 22:27:43 +03:00
parent a7f7102266
commit 07560e605a
16 changed files with 222 additions and 294 deletions

View file

@ -20,6 +20,7 @@ leb128 = "0.2.5"
paste = "1.0.14"
sysinfo = "0.30.7"
rand = "0.8.5"
parking_lot = "0.12.3"
csv = "1.3.0"
serde = { version = "1.0.197", features = ["derive"] }

View file

@ -29,6 +29,7 @@ tracing-bunyan-formatter.workspace = true
common.workspace = true
protocol.workspace = true
qwer.workspace = true
parking_lot.workspace = true
[[bin]]
name = "nap-gameserver"

View file

@ -1,8 +1,8 @@
use std::sync::Arc;
use anyhow::Result;
use parking_lot::RwLock;
use protocol::{PlayerInfo, PtcPlayerInfoChangedArg};
use tokio::sync::RwLock;
use crate::net::NetworkSession;

View file

@ -1,7 +1,7 @@
use anyhow::{anyhow, bail, Result};
use parking_lot::RwLock;
use protocol::*;
use std::sync::Arc;
use tokio::sync::RwLock;
use crate::game::{manager::UniqueIDManager, util, PlayerOperationResult};
use qwer::{
@ -23,9 +23,9 @@ impl DungeonManager {
}
}
pub async fn enter_main_city(&self) -> Result<PlayerOperationResult<PtcEnterSceneArg>> {
pub fn enter_main_city(&self) -> Result<PlayerOperationResult<PtcEnterSceneArg>> {
let (player_uid, scene_position, scene_rotation) = {
let player = self.player.read().await;
let player = self.player.read();
let pos_in_main_city = player.pos_in_main_city.as_ref().unwrap();
(
@ -35,7 +35,7 @@ impl DungeonManager {
)
};
let mut player = self.player.write().await;
let mut player = self.player.write();
let default_scene_uid = player
.dungeon_collection
.as_ref()
@ -113,12 +113,12 @@ impl DungeonManager {
))
}
pub async fn enter_scene_section(
pub fn enter_scene_section(
&self,
scene_uid: u64,
section_id: i32,
) -> PlayerOperationResult<PtcEnterSectionArg> {
let mut player = self.player.write().await;
let mut player = self.player.write();
let scene_info = player
.dungeon_collection
.as_mut()
@ -145,12 +145,9 @@ impl DungeonManager {
)
}
pub async fn enter_scene(
&self,
scene_uid: u64,
) -> Result<PlayerOperationResult<PtcEnterSceneArg>> {
pub fn enter_scene(&self, scene_uid: u64) -> Result<PlayerOperationResult<PtcEnterSceneArg>> {
let (player_uid, prev_scene_uid) = {
let player = self.player.read().await;
let player = self.player.read();
(
*player.uid.as_ref().unwrap(),
@ -158,7 +155,7 @@ impl DungeonManager {
)
};
let mut player = self.player.write().await;
let mut player = self.player.write();
player.scene_uid.replace(scene_uid);
player.prev_scene_uid.replace(prev_scene_uid);
let dungeon_collection = player.dungeon_collection.as_mut().unwrap();
@ -248,10 +245,10 @@ impl DungeonManager {
))
}
pub async fn hollow_finished(&self) -> PlayerOperationResult<u64> {
let cur_scene_uid = self.get_cur_scene_uid().await;
pub fn hollow_finished(&self) -> PlayerOperationResult<u64> {
let cur_scene_uid = self.get_cur_scene_uid();
let mut player = self.player.write().await;
let mut player = self.player.write();
let hollow_scene = player
.dungeon_collection
@ -313,10 +310,9 @@ impl DungeonManager {
)
}
pub async fn get_default_scene_uid(&self) -> u64 {
pub fn get_default_scene_uid(&self) -> u64 {
self.player
.read()
.await
.dungeon_collection
.as_ref()
.unwrap()
@ -325,10 +321,9 @@ impl DungeonManager {
}
#[allow(dead_code)]
pub async fn get_default_scene_uid_for_dungeon(&self, dungeon_uid: u64) -> u64 {
pub fn get_default_scene_uid_for_dungeon(&self, dungeon_uid: u64) -> u64 {
self.player
.read()
.await
.dungeon_collection
.as_ref()
.unwrap()
@ -340,12 +335,12 @@ impl DungeonManager {
.default_scene_uid
}
pub async fn get_cur_scene_uid(&self) -> u64 {
self.player.read().await.scene_uid.unwrap()
pub fn get_cur_scene_uid(&self) -> u64 {
self.player.read().scene_uid.unwrap()
}
async fn add_default_hollow_properties(&self, scene_uid: u64) {
let mut props = self.scene_properties.write().await;
fn add_default_hollow_properties(&self, scene_uid: u64) {
let mut props = self.scene_properties.write();
for (sub_key, value) in &[
(1001, 0),
@ -380,11 +375,11 @@ impl DungeonManager {
}
}
pub async fn leave_battle(&self) -> Result<PlayerOperationResult<PtcEnterSceneArg>> {
let back_scene_uid = self.get_back_scene_uid().await;
pub fn leave_battle(&self) -> Result<PlayerOperationResult<PtcEnterSceneArg>> {
let back_scene_uid = self.get_back_scene_uid();
{
let mut player = self.player.write().await;
let mut player = self.player.write();
let hollow_scene = player
.dungeon_collection
@ -404,11 +399,11 @@ impl DungeonManager {
}
}
self.enter_scene(back_scene_uid).await
self.enter_scene(back_scene_uid)
}
async fn get_back_scene_uid(&self) -> u64 {
let player = self.player.read().await;
fn get_back_scene_uid(&self) -> u64 {
let player = self.player.read();
let fight_scene_uid = player.scene_uid.as_ref().unwrap();
let fight_scene = player
.dungeon_collection
@ -423,14 +418,12 @@ impl DungeonManager {
fight_scene.get_back_scene_uid()
}
pub async fn enter_battle(&self, scene_uid: u64) -> PlayerOperationResult<PtcEnterSceneArg> {
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)
.await;
let ptc_enter_scene = self.enter_scene(scene_uid).await.unwrap().unwrap().clone();
pub fn enter_battle(&self, scene_uid: u64) -> PlayerOperationResult<PtcEnterSceneArg> {
let hollow_scene_uid = *self.player.read().scene_uid.as_ref().unwrap();
let hollow_scene = self.set_cur_hollow_battle(scene_uid, hollow_scene_uid);
let ptc_enter_scene = self.enter_scene(scene_uid).unwrap().unwrap().clone();
let player = self.player.read().await;
let player = self.player.read();
let dungeon_collection = player.dungeon_collection.as_ref().unwrap();
let fight_scene = dungeon_collection
.scenes
@ -459,8 +452,8 @@ impl DungeonManager {
)
}
async fn set_cur_hollow_battle(&self, scene_uid: u64, hollow_scene_uid: u64) -> SceneInfo {
let mut player = self.player.write().await;
fn set_cur_hollow_battle(&self, scene_uid: u64, hollow_scene_uid: u64) -> SceneInfo {
let mut player = self.player.write();
let hollow_scene = player
.dungeon_collection
.as_mut()
@ -485,8 +478,8 @@ impl DungeonManager {
hollow_scene.clone()
}
pub async fn create_fight(&self, id: i32, hollow_scene_uid: u64) -> PlayerOperationResult<u64> {
let mut player = self.player.write().await;
pub fn create_fight(&self, id: i32, hollow_scene_uid: u64) -> PlayerOperationResult<u64> {
let mut player = self.player.write();
let dungeon_collection = player.dungeon_collection.as_mut().unwrap();
let scenes = dungeon_collection.scenes.as_mut().unwrap();
let hollow_scene = scenes.get_mut(&hollow_scene_uid).unwrap();
@ -526,10 +519,10 @@ impl DungeonManager {
}
#[allow(dead_code)]
pub async fn is_in_tutorial(&self) -> bool {
let cur_scene_uid = self.get_cur_scene_uid().await;
pub fn is_in_tutorial(&self) -> bool {
let cur_scene_uid = self.get_cur_scene_uid();
let player = self.player.read().await;
let player = self.player.read();
let cur_scene = player
.dungeon_collection
.as_ref()
@ -543,24 +536,22 @@ impl DungeonManager {
matches!(cur_scene, SceneInfo::Fresh { .. })
}
pub async fn create_hollow(
pub fn create_hollow(
&self,
id: i32,
world_quest_id: i32,
avatar_uids: &[u64],
) -> PlayerOperationResult<(u64, u64)> {
let back_scene_uid = self.get_default_scene_uid().await;
let back_scene_uid = self.get_default_scene_uid();
let mut dungeon = self
.create_base_dungeon(id, back_scene_uid, world_quest_id)
.await;
let mut dungeon = self.create_base_dungeon(id, back_scene_uid, world_quest_id);
dungeon.hollow_event_version = 526;
let scene_uid = self.uid_mgr.next();
dungeon.default_scene_uid = scene_uid;
dungeon.scene_properties_uid = scene_uid;
self.add_default_hollow_properties(scene_uid).await;
self.add_default_hollow_properties(scene_uid);
for (index, avatar_uid) in avatar_uids.iter().enumerate() {
dungeon.avatar_map.insert(
@ -642,10 +633,10 @@ impl DungeonManager {
};
{
let mut player = self.player.write().await;
let mut player = self.player.write();
player
.scene_properties
.replace(self.scene_properties.read().await.clone());
.replace(self.scene_properties.read().clone());
let dungeon_collection = player.dungeon_collection.as_mut().unwrap();
@ -660,7 +651,7 @@ impl DungeonManager {
.unwrap()
.insert(scene_uid, scene.clone());
}
let mut player = self.player.write().await;
let mut player = self.player.write();
let items = player.items.as_mut().unwrap();
let mut updated_items = Vec::new();
@ -674,7 +665,7 @@ impl DungeonManager {
}
let mut prop_changes = Vec::new();
for (key, sub_key, value) in &*self.scene_properties.write().await {
for (key, sub_key, value) in &*self.scene_properties.write() {
prop_changes.push((*key, *sub_key, *value));
}
@ -705,8 +696,8 @@ impl DungeonManager {
)
}
pub async fn create_hall(&self, id: i32) -> PlayerOperationResult<u64> {
let mut dungeon = self.create_base_dungeon(id, 0, 0).await;
pub fn create_hall(&self, id: i32) -> PlayerOperationResult<u64> {
let mut dungeon = self.create_base_dungeon(id, 0, 0);
let dungeon_uid = dungeon.uid;
let scene_uid = self.uid_mgr.next();
@ -726,7 +717,7 @@ impl DungeonManager {
dungeon.default_scene_uid = scene_uid;
let mut player = self.player.write().await;
let mut player = self.player.write();
let dungeon_collection = player.dungeon_collection.as_mut().unwrap();
dungeon_collection
@ -762,8 +753,8 @@ impl DungeonManager {
)
}
pub async fn create_fresh(&self) -> PlayerOperationResult<u64> {
let mut dungeon = self.create_base_dungeon(2, 0, 0).await;
pub fn create_fresh(&self) -> PlayerOperationResult<u64> {
let mut dungeon = self.create_base_dungeon(2, 0, 0);
let dungeon_uid = dungeon.uid;
let scene_uid = self.uid_mgr.next();
@ -783,7 +774,7 @@ impl DungeonManager {
dungeon.default_scene_uid = scene_uid;
let mut player = self.player.write().await;
let mut player = self.player.write();
let dungeon_collection = player.dungeon_collection.as_mut().unwrap();
dungeon_collection
@ -817,13 +808,13 @@ impl DungeonManager {
)
}
async fn create_base_dungeon(
fn create_base_dungeon(
&self,
id: i32,
back_scene_uid: u64,
world_quest_id: i32,
) -> DungeonInfo {
let player = self.player.read().await;
let player = self.player.read();
let uid = self.uid_mgr.next();
DungeonInfo {

View file

@ -3,9 +3,9 @@ use std::{
sync::Arc,
};
use parking_lot::RwLock;
use protocol::*;
use qwer::{phashmap, phashset, PropertyHashMap, PropertyHashSet};
use tokio::sync::RwLock;
use crate::data::{self, ConfigAction, ConfigEventType, ConfigValue};
@ -24,25 +24,24 @@ impl HollowGridManager {
}
}
pub async fn get_cur_position_in_hollow(&self) -> u16 {
self.map.read().await.as_ref().unwrap().start_grid
pub fn get_cur_position_in_hollow(&self) -> u16 {
self.map.read().as_ref().unwrap().start_grid
}
pub async fn move_to(
pub fn move_to(
&self,
destination_grid: u16,
scene_uid: u64,
) -> (PtcHollowGridArg, Option<PtcSyncHollowEventInfoArg>) {
let mut map = self.map.write().await;
let mut map = self.map.write();
let map = map.as_mut().unwrap();
map.start_grid = destination_grid;
let grid = map.grids.get_mut(&destination_grid).unwrap();
self.update_position_to_scene(scene_uid, destination_grid)
.await;
self.update_position_to_scene(scene_uid, destination_grid);
let mut events = self.events.write().await;
let mut events = self.events.write();
let sync_event_info =
if let Entry::Vacant(entry) = events.entry(u64::from(destination_grid)) {
let event_info = EventInfo {
@ -83,7 +82,7 @@ impl HollowGridManager {
(
PtcHollowGridArg {
player_uid: self.player.read().await.uid.unwrap(),
player_uid: self.player.read().uid.unwrap(),
is_partial: true,
scene_uid,
hollow_level: 1,
@ -93,8 +92,8 @@ impl HollowGridManager {
)
}
pub async fn battle_finished(&self) -> (PtcSyncHollowEventInfoArg, bool) {
let map = self.map.read().await;
pub fn battle_finished(&self) -> (PtcSyncHollowEventInfoArg, bool) {
let map = self.map.read();
let map = map.as_ref().unwrap();
let cur_grid = map.grids.get(&map.start_grid).unwrap();
@ -130,16 +129,16 @@ impl HollowGridManager {
)
}
pub async fn get_cur_event_template_id(&self) -> i32 {
let map = self.map.read().await;
pub fn get_cur_event_template_id(&self) -> i32 {
let map = self.map.read();
let map = map.as_ref().unwrap();
let cur_grid = map.grids.get(&map.start_grid).unwrap();
cur_grid.grid.event_graph_info.hollow_event_template_id
}
async fn update_position_to_scene(&self, scene_uid: u64, pos: u16) {
let mut player = self.player.write().await;
fn update_position_to_scene(&self, scene_uid: u64, pos: u16) {
let mut player = self.player.write();
let scene = player
.dungeon_collection
.as_mut()
@ -166,7 +165,7 @@ impl HollowGridManager {
}
}
pub async fn run_event_graph(
pub fn run_event_graph(
&self,
event_graph_uid: u64,
_event_id: i32,
@ -178,12 +177,12 @@ impl HollowGridManager {
bool,
) {
let (player_uid, scene_uid) = {
let player = self.player.read().await;
let player = self.player.read();
(player.uid.unwrap(), player.scene_uid.unwrap())
};
let mut map = self.map.write().await;
let mut map = self.map.write();
let map = map.as_mut().unwrap();
let mut trigger_battle_id = None;
@ -316,23 +315,19 @@ impl HollowGridManager {
)
}
pub async fn sync_hollow_maps(
&self,
player_uid: u64,
scene_uid: u64,
) -> PtcSyncHollowGridMapsArg {
pub fn sync_hollow_maps(&self, player_uid: u64, scene_uid: u64) -> PtcSyncHollowGridMapsArg {
PtcSyncHollowGridMapsArg {
player_uid,
scene_uid,
hollow_level: 1,
main_map: self.map.read().await.clone().unwrap(),
main_map: self.map.read().clone().unwrap(),
time_period: TimePeriodType::Random,
weather: WeatherType::Random,
}
}
pub async fn init_default_map(&self) {
*self.map.write().await = Some(HollowGridMapProtocolInfo {
pub fn init_default_map(&self) {
*self.map.write() = Some(HollowGridMapProtocolInfo {
row: 5,
col: 11,
start_grid: 22,

View file

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

View file

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

View file

@ -1,7 +1,7 @@
use std::sync::Arc;
use parking_lot::RwLock;
use qwer::PropertyDoubleKeyHashMap;
use tokio::sync::RwLock;
use crate::game::PlayerOperationResult;
@ -18,11 +18,10 @@ impl QuestManager {
Self { uid_mgr, player }
}
pub async fn add_world_quest(&self, quest: QuestInfo) -> PlayerOperationResult<u64> {
pub fn add_world_quest(&self, quest: QuestInfo) -> PlayerOperationResult<u64> {
let mut world_quest_collection_uid = self
.player
.read()
.await
.quest_data
.as_ref()
.unwrap()
@ -33,7 +32,6 @@ impl QuestManager {
world_quest_collection_uid = self.uid_mgr.next();
self.player
.write()
.await
.quest_data
.as_mut()
.unwrap()
@ -42,15 +40,14 @@ impl QuestManager {
}
self.add_quest_to_collection(world_quest_collection_uid, quest)
.await
}
pub async fn add_quest_to_collection(
pub fn add_quest_to_collection(
&self,
collection_uid: u64,
mut quest: QuestInfo,
) -> PlayerOperationResult<u64> {
let mut player = self.player.write().await;
let mut player = self.player.write();
let quest_data = player.quest_data.as_mut().unwrap();
quest.set_collection_uid(collection_uid);

View file

@ -1,9 +1,9 @@
use std::{collections::HashMap, sync::Arc};
use anyhow::{bail, Result};
use parking_lot::RwLock;
use protocol::*;
use qwer::{phashmap, PropertyHashMap};
use tokio::sync::RwLock;
use crate::data;
@ -22,7 +22,7 @@ impl SceneUnitManager {
}
}
pub async fn create_npc(
pub fn create_npc(
&self,
id: i32,
tag: i32,
@ -31,7 +31,7 @@ impl SceneUnitManager {
) -> u64 {
let uid = self.uid_mgr.next();
self.units.write().await.insert(
self.units.write().insert(
uid,
SceneUnitProtocolInfo::NpcProtocolInfo {
uid,
@ -45,11 +45,11 @@ impl SceneUnitManager {
uid
}
pub async fn get(&self, uid: u64) -> Option<SceneUnitProtocolInfo> {
self.units.read().await.get(&uid).map(|u| u.clone())
pub fn get(&self, uid: u64) -> Option<SceneUnitProtocolInfo> {
self.units.read().get(&uid).map(|u| u.clone())
}
pub async fn sync(&self, scene_uid: u64, section_id: i32) -> PtcSyncSceneUnitArg {
pub fn sync(&self, scene_uid: u64, section_id: i32) -> PtcSyncSceneUnitArg {
PtcSyncSceneUnitArg {
scene_uid,
section_id,
@ -58,14 +58,13 @@ impl SceneUnitManager {
scene_units: self
.units
.read()
.await
.iter()
.map(|(_, unit)| unit.clone())
.collect(),
}
}
pub async fn add_scene_units(&self, section_id: i32) {
pub fn add_scene_units(&self, section_id: i32) {
for o in data::iter_main_city_object_collection().filter(|o| {
o.create_type == 0
&& data::is_transform_in_section(&o.create_position, section_id)
@ -93,8 +92,7 @@ impl SceneUnitManager {
})
.collect(),
),
)
.await;
);
}
}

View file

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

View file

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

View file

@ -13,23 +13,17 @@ pub async fn on_rpc_hollow_move(
tracing::info!("Hollow movement {:?}", &arg);
let destination_pos = *arg.positions.last().unwrap();
let scene_uid = session
.ns_prop_mgr
.player_info
.read()
.await
.scene_uid
.unwrap();
let scene_uid = session.ns_prop_mgr.player_info.read().scene_uid.unwrap();
let (ptc_hollow_grid, ptc_sync_hollow_event) = session
.context
.hollow_grid_manager
.move_to(destination_pos, scene_uid)
.await;
.move_to(destination_pos, scene_uid);
session
.send_rpc_arg(PTC_HOLLOW_GRID_ID, &ptc_hollow_grid)
.await?;
if let Some(ptc_sync_hollow_event) = ptc_sync_hollow_event {
session
.send_rpc_arg(PTC_SYNC_HOLLOW_EVENT_INFO_ID, &ptc_sync_hollow_event)
@ -59,7 +53,7 @@ pub async fn on_rpc_end_battle(
tracing::info!("RpcEndBattle: {:?}", &arg);
let player_uid = session.player_uid().raw();
let (sync_event, hollow_finished) = session.context.hollow_grid_manager.battle_finished().await;
let (sync_event, hollow_finished) = session.context.hollow_grid_manager.battle_finished();
if !hollow_finished {
session
@ -70,7 +64,6 @@ pub async fn on_rpc_end_battle(
.context
.dungeon_manager
.hollow_finished()
.await
.send_changes(session)
.await?;
@ -91,7 +84,6 @@ pub async fn on_rpc_end_battle(
.context
.dungeon_manager
.leave_battle()
.await
.unwrap()
.send_changes(session)
.await?
@ -100,14 +92,10 @@ pub async fn on_rpc_end_battle(
session
.send_rpc_arg(
PTC_SYNC_HOLLOW_GRID_MAPS_ID,
&session
.context
.hollow_grid_manager
.sync_hollow_maps(
&session.context.hollow_grid_manager.sync_hollow_maps(
player_uid,
session.context.dungeon_manager.get_cur_scene_uid().await,
)
.await,
session.context.dungeon_manager.get_cur_scene_uid(),
),
)
.await?;
@ -117,8 +105,7 @@ pub async fn on_rpc_end_battle(
position: session
.context
.hollow_grid_manager
.get_cur_position_in_hollow()
.await,
.get_cur_position_in_hollow(),
};
session
@ -136,8 +123,7 @@ pub async fn on_rpc_end_battle(
session
.context
.hollow_grid_manager
.get_cur_event_template_id()
.await,
.get_cur_event_template_id(),
HashMap::new(),
))
}
@ -148,13 +134,7 @@ pub async fn on_rpc_run_hollow_event_graph(
) -> Result<RpcRunHollowEventGraphRet> {
tracing::info!("Run hollow event graph {:?}", arg);
let scene_uid = session
.ns_prop_mgr
.player_info
.read()
.await
.scene_uid
.unwrap();
let scene_uid = session.ns_prop_mgr.player_info.read().scene_uid.unwrap();
if arg.event_graph_uid == 3405096459205834 {
// Perform (cutscene)
@ -179,11 +159,8 @@ pub async fn on_rpc_run_hollow_event_graph(
.send_rpc_arg(PTC_SYNC_HOLLOW_EVENT_INFO_ID, &finish_perform)
.await?;
let (ptc_hollow_grid, ptc_sync_hollow_event) = session
.context
.hollow_grid_manager
.move_to(22, scene_uid)
.await;
let (ptc_hollow_grid, ptc_sync_hollow_event) =
session.context.hollow_grid_manager.move_to(22, scene_uid);
session
.send_rpc_arg(PTC_HOLLOW_GRID_ID, &ptc_hollow_grid)
@ -197,8 +174,7 @@ pub async fn on_rpc_run_hollow_event_graph(
let (sync_hollow_event, hollow_grid, trigger_battle_id, hollow_finished) = session
.context
.hollow_grid_manager
.run_event_graph(arg.event_graph_uid, arg.event_id, arg.move_path.clone())
.await;
.run_event_graph(arg.event_graph_uid, arg.event_id, arg.move_path.clone());
if !hollow_finished {
session
@ -214,7 +190,6 @@ pub async fn on_rpc_run_hollow_event_graph(
.context
.dungeon_manager
.hollow_finished()
.await
.send_changes(session)
.await?;
@ -236,7 +211,6 @@ pub async fn on_rpc_run_hollow_event_graph(
.ns_prop_mgr
.player_info
.read()
.await
.scene_uid
.as_ref()
.unwrap();
@ -244,7 +218,6 @@ pub async fn on_rpc_run_hollow_event_graph(
.context
.dungeon_manager
.create_fight(trigger_battle_id, hollow_uid)
.await
.send_changes(session)
.await?;
@ -254,8 +227,7 @@ pub async fn on_rpc_run_hollow_event_graph(
position: session
.context
.hollow_grid_manager
.get_cur_position_in_hollow()
.await,
.get_cur_position_in_hollow(),
};
session
@ -272,7 +244,6 @@ pub async fn on_rpc_run_hollow_event_graph(
.context
.dungeon_manager
.enter_battle(battle_scene_uid)
.await
.send_changes(session)
.await?,
)
@ -291,7 +262,8 @@ pub async fn on_rpc_start_hollow_quest(
// Set avatar HP properties
for (_idx, avatar_uid) in &arg.avatar_map {
let player_info = session.ns_prop_mgr.player_info.read().await;
let update_properties = {
let player_info = session.ns_prop_mgr.player_info.read();
let items = player_info.items.as_ref().unwrap();
let Some(ItemInfo::Avatar { id, .. }) = items
.iter()
@ -308,10 +280,11 @@ pub async fn on_rpc_start_hollow_quest(
.find(|c| c.id == *id)
.unwrap();
let update_properties = PtcPropertyChangedArg {
PtcPropertyChangedArg {
scene_unit_uid: *avatar_uid,
is_partial: true,
changed_properties: phashmap![(1, avatar_config.hp), (111, avatar_config.hp)],
}
};
session
@ -329,7 +302,6 @@ pub async fn on_rpc_start_hollow_quest(
.context
.dungeon_manager
.create_hollow(10001, 10010001, &avatars)
.await
.send_changes(session)
.await?;
@ -350,20 +322,18 @@ pub async fn on_rpc_start_hollow_quest(
sort_id: 2000,
},
)
.await
.send_changes(session)
.await?;
let ptc_enter_scene = session
.context
.dungeon_manager
.enter_scene(scene_uid)
.await?
.enter_scene(scene_uid)?
.send_changes(session)
.await?
.clone();
session.context.hollow_grid_manager.init_default_map().await;
session.context.hollow_grid_manager.init_default_map();
session
.send_rpc_arg(
@ -371,8 +341,7 @@ pub async fn on_rpc_start_hollow_quest(
&session
.context
.hollow_grid_manager
.sync_hollow_maps(session.player_uid().raw(), scene_uid)
.await,
.sync_hollow_maps(session.player_uid().raw(), scene_uid),
)
.await?;
@ -382,8 +351,7 @@ pub async fn on_rpc_start_hollow_quest(
position: session
.context
.hollow_grid_manager
.get_cur_position_in_hollow()
.await,
.get_cur_position_in_hollow(),
};
session

View file

@ -11,15 +11,12 @@ const DEFAULT_ACCOUNT_ID: u64 = 1;
pub async fn on_rpc_login(session: &NetworkSession, arg: &RpcLoginArg) -> Result<RpcLoginRet> {
tracing::info!("Received rpc login arg: {}", arg.account_name);
match session
.logged_in(
match session.logged_in(
AccountUID(DEFAULT_ACCOUNT_ID),
util::create_default_account(DEFAULT_ACCOUNT_ID),
)
.await
{
) {
Ok(()) => Ok(RpcLoginRet::new(
session.ns_prop_mgr.serialize_account_info().await,
session.ns_prop_mgr.serialize_account_info(),
)),
Err(_) => Ok(RpcLoginRet::error(ErrorCode::RepeatedLogin, Vec::new())),
}
@ -34,7 +31,6 @@ pub async fn on_rpc_create_player(
.ns_prop_mgr
.account_info
.read()
.await
.players
.as_ref()
.unwrap()
@ -45,7 +41,6 @@ pub async fn on_rpc_create_player(
.ns_prop_mgr
.account_info
.write()
.await
.players
.as_mut()
.unwrap()

View file

@ -6,17 +6,19 @@ pub async fn on_rpc_mod_nick_name(
) -> Result<RpcModNickNameRet> {
tracing::info!("creating character");
let mut player = session.ns_prop_mgr.player_info.write().await;
let player_info_changed = {
let mut player = session.ns_prop_mgr.player_info.write();
player.nick_name.replace(arg.nick_name.clone());
player.avatar_id.replace(arg.avatar_id);
let player_info_changed = PtcPlayerInfoChangedArg {
PtcPlayerInfoChangedArg {
player_uid: player.uid.unwrap(),
player_info: PlayerInfo {
nick_name: Some(arg.nick_name.clone()),
avatar_id: Some(arg.avatar_id),
..Default::default()
},
}
};
session

View file

@ -13,7 +13,7 @@ pub async fn on_rpc_run_event_graph(
session: &NetworkSession,
arg: &RpcRunEventGraphArg,
) -> Result<RpcRunEventGraphRet> {
let unit = session.context.scene_unit_manager.get(arg.owner_uid).await;
let unit = session.context.scene_unit_manager.get(arg.owner_uid);
let interact_id = match unit {
Some(SceneUnitProtocolInfo::NpcProtocolInfo { interacts_info, .. })
@ -105,7 +105,7 @@ pub async fn on_rpc_interact_with_unit(
arg.interaction
);
let unit = session.context.scene_unit_manager.get(arg.unit_uid).await;
let unit = session.context.scene_unit_manager.get(arg.unit_uid);
let interact_id = match unit {
Some(SceneUnitProtocolInfo::NpcProtocolInfo { interacts_info, .. })
@ -203,11 +203,7 @@ fn create_player(id: u64) -> PlayerInfo {
}
pub async fn enter_main_city(session: &NetworkSession) -> Result<()> {
let hall_scene_uid = session
.context
.dungeon_manager
.get_default_scene_uid()
.await;
let hall_scene_uid = session.context.dungeon_manager.get_default_scene_uid();
session
.send_rpc_arg(
@ -216,7 +212,6 @@ pub async fn enter_main_city(session: &NetworkSession) -> Result<()> {
.context
.dungeon_manager
.enter_scene_section(hall_scene_uid, 2)
.await
.unwrap(),
)
.await?;
@ -224,11 +219,7 @@ pub async fn enter_main_city(session: &NetworkSession) -> Result<()> {
session
.send_rpc_arg(
PTC_SYNC_SCENE_UNIT_ID,
&session
.context
.scene_unit_manager
.sync(hall_scene_uid, 2)
.await,
&session.context.scene_unit_manager.sync(hall_scene_uid, 2),
)
.await?;
@ -238,8 +229,7 @@ pub async fn enter_main_city(session: &NetworkSession) -> Result<()> {
session
.context
.dungeon_manager
.enter_main_city()
.await?
.enter_main_city()?
.send_changes(session)
.await?,
)
@ -250,35 +240,34 @@ pub async fn on_rpc_enter_world(
session: &NetworkSession,
_arg: &RpcEnterWorldArg,
) -> Result<RpcEnterWorldRet> {
let account = session.ns_prop_mgr.account_info.read().await;
{
let account = session.ns_prop_mgr.account_info.read();
let player_uid = *account.players.as_ref().unwrap().first().unwrap(); // get first id from list
session
.set_cur_player(PlayerUID::from(player_uid), create_player(player_uid))
.await?;
session.set_cur_player(PlayerUID::from(player_uid), create_player(player_uid))?;
}
let item_manager = &session.context.item_manager;
item_manager.add_resource(501, 120).await;
item_manager.add_resource(10, 228).await;
item_manager.add_resource(100, 1337).await;
item_manager.add_resource(501, 120);
item_manager.add_resource(10, 228);
item_manager.add_resource(100, 1337);
for avatar_id in data::iter_avatar_config_collection()
.filter(|c| c.camp != 0)
.map(|c| c.id)
{
item_manager.unlock_avatar(avatar_id).await;
item_manager.unlock_avatar(avatar_id);
}
for unlock_id in data::iter_unlock_config_collection().map(|c| c.id) {
session.context.unlock_manager.unlock(unlock_id).await;
session.context.unlock_manager.unlock(unlock_id);
}
session.context.dungeon_manager.create_hall(1).await;
session.context.scene_unit_manager.add_scene_units(2).await;
session.context.dungeon_manager.create_hall(1);
session.context.scene_unit_manager.add_scene_units(2);
let quest_manager = session.context.quest_manager.clone();
quest_manager
.add_world_quest(QuestInfo::MainCity {
quest_manager.add_world_quest(QuestInfo::MainCity {
id: 10020002,
finished_count: 0,
collection_uid: 0,
@ -289,11 +278,9 @@ pub async fn on_rpc_enter_world(
progress_time: 2111012,
sort_id: 1000,
bound_npc_and_interact: phashmap![],
})
.await;
});
quest_manager
.add_world_quest(QuestInfo::Hollow {
quest_manager.add_world_quest(QuestInfo::Hollow {
id: 10010002,
finished_count: 0,
collection_uid: 3405096459205774,
@ -307,24 +294,18 @@ pub async fn on_rpc_enter_world(
progress_time: 0,
finish_condition_progress: phashmap![],
dungeon_uid: 0,
})
.await;
});
session
.context
.yorozuya_quest_manager
.add_hollow_quest(102, HollowQuestType::SideQuest, 10010002)
.await;
session.context.yorozuya_quest_manager.add_hollow_quest(
102,
HollowQuestType::SideQuest,
10010002,
);
if CONFIGURATION.skip_tutorial {
Box::pin(enter_main_city(session)).await?;
} else {
let fresh_scene_uid = *session
.context
.dungeon_manager
.create_fresh()
.await
.unwrap();
let fresh_scene_uid = *session.context.dungeon_manager.create_fresh().unwrap();
session
.send_rpc_arg(
PTC_ENTER_SCENE_ID,
@ -332,7 +313,6 @@ pub async fn on_rpc_enter_world(
.context
.dungeon_manager
.enter_scene(fresh_scene_uid)
.await
.unwrap()
.unwrap(),
)
@ -350,7 +330,7 @@ pub async fn on_rpc_enter_world(
.await?;
Ok(RpcEnterWorldRet::new(
session.ns_prop_mgr.serialize_player_info().await,
session.ns_prop_mgr.serialize_player_info(),
))
}

View file

@ -60,16 +60,16 @@ impl NetworkSession {
self.client_socket.lock().await
}
pub async fn logged_in(&self, uid: AccountUID, account: AccountInfo) -> Result<()> {
pub fn logged_in(&self, uid: AccountUID, account: AccountInfo) -> Result<()> {
self.account_uid.set(uid)?;
*self.ns_prop_mgr.account_info.write().await = account;
*self.ns_prop_mgr.account_info.write() = account;
Ok(())
}
pub async fn set_cur_player(&self, uid: PlayerUID, player: PlayerInfo) -> Result<()> {
pub fn set_cur_player(&self, uid: PlayerUID, player: PlayerInfo) -> Result<()> {
self.player_uid.set(uid)?;
*self.ns_prop_mgr.player_info.write().await = player;
*self.ns_prop_mgr.player_info.write() = player;
Ok(())
}