JaneDoe-ZS/nap_gameserver/src/handlers/world.rs
xeon 99123a15ef newtype fun
use newtypes for template ids, pros:
1) enforces id validation
2) ease of use (helper method for getting template struct right away)
2024-07-25 01:24:48 +03:00

205 lines
5.6 KiB
Rust

use data::tables::{ProcedureConfigID, TrainingQuestID};
use super::core::NetError;
use crate::{
logic::{game::*, procedure::ProcedureAction, ELocalPlayType},
net::NetSessionState,
};
use super::*;
pub async fn on_enter_world(
session: &NetSession,
player: &mut Player,
_req: EnterWorldCsReq,
) -> NetResult<EnterWorldScRsp> {
session.set_state(NetSessionState::EndBasicsReq);
if let Some(procedure_id) = player.basic_data_model.beginner_procedure_id {
player.game_instance = GameInstance::Fresh(FreshGame::new(procedure_id))
} else {
player.init_frontend_game()?;
}
let world_init_notify = player.game_instance.create_world_init_notify()?;
session.notify(world_init_notify).await?;
session.set_state(NetSessionState::EnterWorldScRsp);
Ok(EnterWorldScRsp {
retcode: Retcode::RetSucc.into(),
})
}
pub async fn on_advance_beginner_procedure(
session: &NetSession,
player: &mut Player,
req: AdvanceBeginnerProcedureCsReq,
) -> NetResult<AdvanceBeginnerProcedureScRsp> {
let is_end = {
let GameInstance::Fresh(fresh_game) = &mut player.game_instance else {
return Err(NetError::from(Retcode::RetFail));
};
let procedure_id =
ProcedureConfigID::new(req.procedure_id as u32).ok_or(Retcode::RetFail)?;
fresh_game
.procedure_mgr
.try_complete_procedure(procedure_id)
.map_err(LogicError::from)?;
player.basic_data_model.beginner_procedure_id = fresh_game.procedure_mgr.procedure_id();
fresh_game.procedure_mgr.is_end()
};
if is_end {
player.init_frontend_game()?;
let world_init_notify = player.game_instance.create_world_init_notify()?;
session.notify(world_init_notify).await?;
}
Ok(AdvanceBeginnerProcedureScRsp {
retcode: Retcode::RetSucc.into(),
next_procedure_id: req.procedure_id,
})
}
pub async fn on_beginner_battle_begin(
_session: &NetSession,
player: &mut Player,
req: BeginnerBattleBeginCsReq,
) -> NetResult<BeginnerBattleBeginScRsp> {
let GameInstance::Fresh(fresh_game) = &mut player.game_instance else {
return Err(NetError::from(Retcode::RetFail));
};
fresh_game
.procedure_mgr
.on_action(ProcedureAction::BeginnerBattleBegin)
.map_err(LogicError::from)?;
Ok(BeginnerBattleBeginScRsp {
retcode: Retcode::RetSucc.into(),
battle_uid: req.battle_id as i64,
})
}
pub async fn on_beginner_battle_end(
_session: &NetSession,
player: &mut Player,
req: BeginnerBattleEndCsReq,
) -> NetResult<BeginnerBattleEndScRsp> {
let GameInstance::Fresh(fresh_game) = &mut player.game_instance else {
return Err(NetError::from(Retcode::RetFail));
};
tracing::info!(
"beginner battle end, id: {}, uid: {}, statistics: {:?}",
req.battle_id,
req.battle_uid,
req.battle_statistics
);
fresh_game
.procedure_mgr
.on_action(ProcedureAction::BeginnerBattleEnd)
.map_err(LogicError::from)?;
Ok(BeginnerBattleEndScRsp {
retcode: Retcode::RetSucc.into(),
})
}
pub async fn on_beginner_battle_rebegin(
_session: &NetSession,
_player: &mut Player,
_req: BeginnerBattleRebeginCsReq,
) -> NetResult<BeginnerBattleRebeginScRsp> {
Ok(BeginnerBattleRebeginScRsp {
retcode: Retcode::RetSucc.into(),
})
}
pub async fn on_enter_section(
_session: &NetSession,
_player: &mut Player,
_req: EnterSectionCsReq,
) -> NetResult<EnterSectionScRsp> {
Ok(EnterSectionScRsp {
retcode: Retcode::RetSucc.into(),
})
}
pub async fn on_save_pos_in_main_city(
_session: &NetSession,
player: &mut Player,
req: SavePosInMainCityCsReq,
) -> NetResult<SavePosInMainCityScRsp> {
if let Some(transform) = req.position {
player
.main_city_model
.update_position(transform.position, transform.rotation);
}
Ok(SavePosInMainCityScRsp {
retcode: Retcode::RetSucc.into(),
})
}
pub async fn on_world_init_finish(
_session: &NetSession,
_player: &mut Player,
_req: WorldInitFinishCsReq,
) -> NetResult<WorldInitFinishScRsp> {
Ok(WorldInitFinishScRsp {
retcode: Retcode::RetSucc.into(),
})
}
pub async fn on_start_trial_fighting_mission(
session: &NetSession,
player: &mut Player,
req: StartTrialFightingMissionCsReq,
) -> NetResult<StartTrialFightingMissionScRsp> {
let quest_id = TrainingQuestID::new(req.quest_id).ok_or(Retcode::RetFail)?;
player.game_instance = GameInstance::Hollow(
HollowGame::create_training_game(quest_id, ELocalPlayType::TrainingRoomFight)
.map_err(LogicError::from)?,
);
let world_init_notify = player.game_instance.create_world_init_notify()?;
session.notify(world_init_notify).await?;
Ok(StartTrialFightingMissionScRsp {
retcode: Retcode::RetSucc.into(),
})
}
pub async fn on_end_battle(
_session: &NetSession,
_player: &mut Player,
_req: EndBattleCsReq,
) -> NetResult<EndBattleScRsp> {
Ok(EndBattleScRsp {
battle_reward: Some(BattleRewardInfo::default()),
retcode: Retcode::RetSucc.into(),
})
}
pub async fn on_leave_cur_dungeon(
session: &NetSession,
player: &mut Player,
_req: LeaveCurDungeonCsReq,
) -> NetResult<LeaveCurDungeonScRsp> {
player.init_frontend_game()?;
let world_init_notify = player.game_instance.create_world_init_notify()?;
session.notify(world_init_notify).await?;
Ok(LeaveCurDungeonScRsp {
retcode: Retcode::RetSucc.into(),
})
}