77 lines
2.6 KiB
C++
77 lines
2.6 KiB
C++
#include "network/connection_phase.h"
|
|
|
|
namespace metin2::network {
|
|
|
|
const char* ToString(ConnectionPhase phase) {
|
|
switch (phase) {
|
|
case ConnectionPhase::Disconnected: return "Disconnected";
|
|
case ConnectionPhase::Handshake: return "Handshake";
|
|
case ConnectionPhase::Auth: return "Auth";
|
|
case ConnectionPhase::CharacterSelect: return "CharacterSelect";
|
|
case ConnectionPhase::GameLoading: return "GameLoading";
|
|
case ConnectionPhase::InGame: return "InGame";
|
|
default: return "Unknown";
|
|
}
|
|
}
|
|
|
|
PhaseStateMachine::PhaseStateMachine()
|
|
: m_current_phase(ConnectionPhase::Disconnected) {}
|
|
|
|
PhaseStateMachine::PhaseStateMachine(ConnectionPhase initial_phase)
|
|
: m_current_phase(initial_phase) {}
|
|
|
|
bool PhaseStateMachine::CanTransitionTo(ConnectionPhase target_phase) const {
|
|
if (m_current_phase == target_phase) {
|
|
return true;
|
|
}
|
|
|
|
// Always allowed to disconnect
|
|
if (target_phase == ConnectionPhase::Disconnected) {
|
|
return true;
|
|
}
|
|
|
|
switch (m_current_phase) {
|
|
case ConnectionPhase::Disconnected:
|
|
return target_phase == ConnectionPhase::Handshake || target_phase == ConnectionPhase::Auth;
|
|
case ConnectionPhase::Handshake:
|
|
return target_phase == ConnectionPhase::Auth;
|
|
case ConnectionPhase::Auth:
|
|
return target_phase == ConnectionPhase::CharacterSelect;
|
|
case ConnectionPhase::CharacterSelect:
|
|
return target_phase == ConnectionPhase::GameLoading || target_phase == ConnectionPhase::Auth;
|
|
case ConnectionPhase::GameLoading:
|
|
return target_phase == ConnectionPhase::InGame || target_phase == ConnectionPhase::CharacterSelect;
|
|
case ConnectionPhase::InGame:
|
|
return target_phase == ConnectionPhase::CharacterSelect || target_phase == ConnectionPhase::GameLoading;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool PhaseStateMachine::TransitionTo(ConnectionPhase target_phase) {
|
|
if (!CanTransitionTo(target_phase)) {
|
|
return false;
|
|
}
|
|
|
|
if (m_current_phase != target_phase) {
|
|
ConnectionPhase old = m_current_phase;
|
|
m_current_phase = target_phase;
|
|
if (m_callback) {
|
|
m_callback(old, target_phase);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void PhaseStateMachine::ForcePhase(ConnectionPhase phase) {
|
|
if (m_current_phase != phase) {
|
|
ConnectionPhase old = m_current_phase;
|
|
m_current_phase = phase;
|
|
if (m_callback) {
|
|
m_callback(old, phase);
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace metin2::network
|