Skip to content

Commit

Permalink
feat: server decides game difficulty
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeBryssinck committed May 2, 2022
1 parent 1aba439 commit df053b2
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Code/client/Games/Skyrim/PlayerCharacter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,20 @@ static TCalculateExperience* RealCalculateExperience = nullptr;

void PlayerCharacter::SetDifficulty(const int32_t aDifficulty) noexcept
{
if (aDifficulty > 5)
return;

// TODO(cosideci): cache pre-connect difficulty

// Can't find "iDifficulty:GamePlay"
/*
auto* pSettings = INISettingCollection::Get();
Setting* pSetting = pSettings->GetSetting("iDifficulty:GamePlay");
pSetting->data = aDifficulty;
*/

POINTER_SKYRIMSE(int32_t, s_difficulty, 381472);
*s_difficulty = aDifficulty;

difficulty = aDifficulty;
}
Expand Down
9 changes: 9 additions & 0 deletions Code/client/Services/Generic/PlayerService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <Messages/EnterExteriorCellRequest.h>
#include <Messages/EnterInteriorCellRequest.h>

#include <Structs/ServerSettings.h>

#include <PlayerCharacter.h>
#include <Forms/TESObjectCELL.h>
#include <Games/Overrides.h>
Expand All @@ -20,6 +22,7 @@ PlayerService::PlayerService(World& aWorld, entt::dispatcher& aDispatcher, Trans
: m_world(aWorld), m_dispatcher(aDispatcher), m_transport(aTransport)
{
m_updateConnection = m_dispatcher.sink<UpdateEvent>().connect<&PlayerService::OnUpdate>(this);
m_settingsConnection = m_dispatcher.sink<ServerSettings>().connect<&PlayerService::OnServerSettingsReceived>(this);
m_notifyRespawnConnection = m_dispatcher.sink<NotifyPlayerRespawn>().connect<&PlayerService::OnNotifyPlayerRespawn>(this);
m_gridCellChangeConnection = m_dispatcher.sink<GridCellChangeEvent>().connect<&PlayerService::OnGridCellChangeEvent>(this);
m_cellChangeConnection = m_dispatcher.sink<CellChangeEvent>().connect<&PlayerService::OnCellChangeEvent>(this);
Expand All @@ -30,6 +33,12 @@ void PlayerService::OnUpdate(const UpdateEvent& acEvent) noexcept
RunRespawnUpdates(acEvent.Delta);
}

void PlayerService::OnServerSettingsReceived(const ServerSettings& acSettings) const noexcept
{
if (acSettings.Difficulty <= 5)
PlayerCharacter::Get()->SetDifficulty(acSettings.Difficulty);
}

void PlayerService::OnNotifyPlayerRespawn(const NotifyPlayerRespawn& acMessage) const noexcept
{
PlayerCharacter::Get()->PayGold(acMessage.GoldLost);
Expand Down
1 change: 1 addition & 0 deletions Code/client/Services/Generic/TransportService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ void TransportService::HandleAuthenticationResponse(const AuthenticationResponse
{
m_connected = true;
m_dispatcher.trigger(acMessage.UserMods);
m_dispatcher.trigger(acMessage.Settings);
m_dispatcher.trigger(ConnectedEvent());
break;
}
Expand Down
3 changes: 3 additions & 0 deletions Code/client/Services/PlayerService.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ struct World;
struct TransportService;

struct UpdateEvent;
struct ServerSettings;
struct GridCellChangeEvent;
struct CellChangeEvent;

Expand All @@ -22,6 +23,7 @@ struct PlayerService
protected:

void OnUpdate(const UpdateEvent& acEvent) noexcept;
void OnServerSettingsReceived(const ServerSettings& acSettings) const noexcept;
void OnNotifyPlayerRespawn(const NotifyPlayerRespawn& acMessage) const noexcept;
void OnGridCellChangeEvent(const GridCellChangeEvent& acEvent) const noexcept;
void OnCellChangeEvent(const CellChangeEvent& acEvent) const noexcept;
Expand All @@ -40,6 +42,7 @@ struct PlayerService
double m_respawnTimer = 0.0;

entt::scoped_connection m_updateConnection;
entt::scoped_connection m_settingsConnection;
entt::scoped_connection m_notifyRespawnConnection;
entt::scoped_connection m_gridCellChangeConnection;
entt::scoped_connection m_cellChangeConnection;
Expand Down
2 changes: 2 additions & 0 deletions Code/encoding/Messages/AuthenticationResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ void AuthenticationResponse::SerializeRaw(TiltedPhoques::Buffer::Writer& aWriter
Serialization::WriteVarInt(aWriter, static_cast<uint32_t>(Type));
Serialization::WriteString(aWriter, Version);
UserMods.Serialize(aWriter);
Settings.Serialize(aWriter);
}

void AuthenticationResponse::DeserializeRaw(TiltedPhoques::Buffer::Reader& aReader) noexcept
{
Type = static_cast<ResponseType>(Serialization::ReadVarInt(aReader) & 0xFFFFFFFF);
Version = Serialization::ReadString(aReader);
UserMods.Deserialize(aReader);
Settings.Deserialize(aReader);
}
7 changes: 6 additions & 1 deletion Code/encoding/Messages/AuthenticationResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Message.h"
#include <Structs/Mods.h>
#include <Structs/ServerSettings.h>

struct AuthenticationResponse final : ServerMessage
{
Expand All @@ -23,10 +24,14 @@ struct AuthenticationResponse final : ServerMessage

bool operator==(const AuthenticationResponse& achRhs) const noexcept
{
return GetOpcode() == achRhs.GetOpcode() && Type == achRhs.Type && UserMods == achRhs.UserMods;
return GetOpcode() == achRhs.GetOpcode() &&
Type == achRhs.Type &&
UserMods == achRhs.UserMods &&
Settings == achRhs.Settings;
}

ResponseType Type;
String Version;
Mods UserMods{};
ServerSettings Settings{};
};
25 changes: 25 additions & 0 deletions Code/encoding/Structs/ServerSettings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "ServerSettings.h"
#include <TiltedCore/Serialization.hpp>

using TiltedPhoques::Serialization;

bool ServerSettings::operator==(const ServerSettings& acRhs) const noexcept
{
return Difficulty == acRhs.Difficulty;
}

bool ServerSettings::operator!=(const ServerSettings& acRhs) const noexcept
{
return !this->operator==(acRhs);
}

void ServerSettings::Serialize(TiltedPhoques::Buffer::Writer& aWriter) const noexcept
{
Serialization::WriteVarInt(aWriter, Difficulty);
}

void ServerSettings::Deserialize(TiltedPhoques::Buffer::Reader& aReader) noexcept
{
Difficulty = Serialization::ReadVarInt(aReader) & 0xFFFFFFFF;
}

16 changes: 16 additions & 0 deletions Code/encoding/Structs/ServerSettings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <TiltedCore/Buffer.hpp>

using TiltedPhoques::Buffer;

struct ServerSettings
{
bool operator==(const ServerSettings& acRhs) const noexcept;
bool operator!=(const ServerSettings& acRhs) const noexcept;

void Serialize(TiltedPhoques::Buffer::Writer& aWriter) const noexcept;
void Deserialize(TiltedPhoques::Buffer::Reader& aReader) noexcept;

uint32_t Difficulty;
};
4 changes: 4 additions & 0 deletions Code/server/GameServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Console::StringSetting sAdminPassword{"GameServer:sAdminPassword", "Admin authen
Console::StringSetting sToken{"GameServer:sToken", "Admin token", ""};
Console::Setting bEnableMoPo{"ModPolicy:bEnabled", "Bypass the mod policy restrictions.", true,
Console::SettingsFlags::kHidden | Console::SettingsFlags::kLocked};
// TODO: if difficulty is higher than 5, close server with error
Console::Setting uDifficulty{"Gameplay:uDifficulty", "In game difficulty (0 to 5)", 4u};
// -- Commands --
Console::Command<bool> TogglePremium("TogglePremium", "Toggle the premium mode",
[](Console::ArgStack& aStack) { bPremiumTickrate = aStack.Pop<bool>(); });
Expand Down Expand Up @@ -554,6 +556,8 @@ void GameServer::HandleAuthenticationRequest(const ConnectionId_t aConnectionId,
spdlog::info("New player {:x} connected with {} mods\n\t: {}", aConnectionId,
acRequest->UserMods.ModList.size(), modList.c_str());

serverResponse.Settings.Difficulty = uDifficulty.value_as<uint8_t>();

serverResponse.Type = AuthenticationResponse::ResponseType::kAccepted;
Send(aConnectionId, serverResponse);

Expand Down

0 comments on commit df053b2

Please sign in to comment.