Skip to content

Commit

Permalink
feat: sync combat xp with party
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeBryssinck committed Mar 15, 2022
1 parent 8fdfc47 commit a1c8149
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 3 deletions.
11 changes: 11 additions & 0 deletions Code/client/Events/AddExperienceEvent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

struct AddExperienceEvent
{
AddExperienceEvent(float aExperience)
: Experience(aExperience)
{
}

float Experience{};
};
8 changes: 5 additions & 3 deletions Code/client/Games/Skyrim/PlayerCharacter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <PlayerCharacter.h>

#include <Events/InventoryChangeEvent.h>
#include <Events/AddExperienceEvent.h>

#include <World.h>

Expand All @@ -17,6 +18,7 @@ static TAddSkillExperience* RealAddSkillExperience = nullptr;

void PlayerCharacter::AddSkillExperience(int32_t aSkill, float aExperience) noexcept
{
spdlog::info("Adding {} experience to skill {}", aExperience, aSkill);
ThisCall(RealAddSkillExperience, this, aSkill, aExperience);
}

Expand All @@ -35,11 +37,11 @@ void TP_MAKE_THISCALL(HookAddSkillExperience, PlayerCharacter, int32_t aSkill, f

if (combatSkills.contains(aSkill))
{
spdlog::info("Set new last used combat skill.");
spdlog::info("Set new last used combat skill to {}.", aSkill);
apThis->GetExtension()->LastUsedCombatSkill = aSkill;
}

// TODO: send event
World::Get().GetRunner().Trigger(AddExperienceEvent(aExperience));
}

spdlog::info("Skill: {}, experience: {}", aSkill, aExperience);
ThisCall(RealAddSkillExperience, apThis, aSkill, aExperience);
Expand Down
9 changes: 9 additions & 0 deletions Code/client/Services/CharacterService.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ struct MountEvent;
struct NotifyMount;
struct InitPackageEvent;
struct NotifyNewPackage;
struct AddExperienceEvent;
struct NotifySyncExperience;

struct Actor;
struct World;
Expand Down Expand Up @@ -58,6 +60,8 @@ struct CharacterService
void OnNotifyMount(const NotifyMount& acMessage) const noexcept;
void OnInitPackageEvent(const InitPackageEvent& acEvent) const noexcept;
void OnNotifyNewPackage(const NotifyNewPackage& acMessage) const noexcept;
void OnAddExperienceEvent(const AddExperienceEvent& acEvent) noexcept;
void OnNotifySyncExperience(const NotifySyncExperience& acMessage) noexcept;

private:

Expand All @@ -70,11 +74,14 @@ struct CharacterService
void RunRemoteUpdates() const noexcept;
void RunFactionsUpdates() const noexcept;
void RunSpawnUpdates() const noexcept;
void RunExperienceUpdates() noexcept;

World& m_world;
entt::dispatcher& m_dispatcher;
TransportService& m_transport;

float m_cachedExperience = 0.f;

entt::scoped_connection m_formIdAddedConnection;
entt::scoped_connection m_formIdRemovedConnection;
entt::scoped_connection m_updateConnection;
Expand All @@ -94,4 +101,6 @@ struct CharacterService
entt::scoped_connection m_notifyMountConnection;
entt::scoped_connection m_initPackageConnection;
entt::scoped_connection m_newPackageConnection;
entt::scoped_connection m_addExperienceEventConnection;
entt::scoped_connection m_syncExperienceConnection;
};
37 changes: 37 additions & 0 deletions Code/client/Services/Generic/CharacterService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <Events/ProjectileLaunchedEvent.h>
#include <Events/MountEvent.h>
#include <Events/InitPackageEvent.h>
#include <Events/AddExperienceEvent.h>

#include <Structs/ActionEvent.h>
#include <Messages/CancelAssignmentRequest.h>
Expand All @@ -55,6 +56,8 @@
#include <Messages/NotifyMount.h>
#include <Messages/NewPackageRequest.h>
#include <Messages/NotifyNewPackage.h>
#include <Messages/SyncExperienceRequest.h>
#include <Messages/NotifySyncExperience.h>

#include <World.h>
#include <Games/TES.h>
Expand Down Expand Up @@ -93,6 +96,9 @@ CharacterService::CharacterService(World& aWorld, entt::dispatcher& aDispatcher,

m_initPackageConnection = m_dispatcher.sink<InitPackageEvent>().connect<&CharacterService::OnInitPackageEvent>(this);
m_newPackageConnection = m_dispatcher.sink<NotifyNewPackage>().connect<&CharacterService::OnNotifyNewPackage>(this);

m_addExperienceEventConnection = m_dispatcher.sink<AddExperienceEvent>().connect<&CharacterService::OnAddExperienceEvent>(this);
m_syncExperienceConnection = m_dispatcher.sink<NotifySyncExperience>().connect<&CharacterService::OnNotifySyncExperience>(this);
}

void CharacterService::OnFormIdComponentAdded(entt::registry& aRegistry, const entt::entity aEntity) const noexcept
Expand Down Expand Up @@ -883,6 +889,18 @@ void CharacterService::OnNotifyNewPackage(const NotifyNewPackage& acMessage) con
pActor->SetPackage(pPackage);
}

void CharacterService::OnAddExperienceEvent(const AddExperienceEvent& acEvent) noexcept
{
m_cachedExperience += 0.f;
}

void CharacterService::OnNotifySyncExperience(const NotifySyncExperience& acMessage) noexcept
{
PlayerCharacter* pPlayer = PlayerCharacter::Get();
ActorExtension* pPlayerEx = pPlayer->GetExtension();
pPlayer->AddSkillExperience(pPlayerEx->LastUsedCombatSkill, acMessage.Experience);
}

void CharacterService::RequestServerAssignment(entt::registry& aRegistry, const entt::entity aEntity) const noexcept
{
if (!m_transport.IsOnline())
Expand Down Expand Up @@ -1327,3 +1345,22 @@ void CharacterService::RunSpawnUpdates() const noexcept
}
}
}

void CharacterService::RunExperienceUpdates() noexcept
{
static std::chrono::steady_clock::time_point lastSendTimePoint;
constexpr auto cDelayBetweenSnapshots = 500ms;

const auto now = std::chrono::steady_clock::now();
if (now - lastSendTimePoint < cDelayBetweenSnapshots)
return;

lastSendTimePoint = now;

SyncExperienceRequest message;
message.Experience = m_cachedExperience;

m_cachedExperience = 0.f;

m_transport.Send(message);
}
13 changes: 13 additions & 0 deletions Code/encoding/Messages/NotifySyncExperience.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <Messages/NotifySyncExperience.h>

void NotifySyncExperience::SerializeRaw(TiltedPhoques::Buffer::Writer& aWriter) const noexcept
{
Serialization::WriteFloat(aWriter, Experience);
}

void NotifySyncExperience::DeserializeRaw(TiltedPhoques::Buffer::Reader& aReader) noexcept
{
ServerMessage::DeserializeRaw(aReader);

Experience = Serialization::ReadFloat(aReader);
}
23 changes: 23 additions & 0 deletions Code/encoding/Messages/NotifySyncExperience.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "Message.h"

struct NotifySyncExperience final : ServerMessage
{
static constexpr ServerOpcode Opcode = kNotifySyncExperience;

NotifySyncExperience() : ServerMessage(Opcode)
{
}

void SerializeRaw(TiltedPhoques::Buffer::Writer& aWriter) const noexcept override;
void DeserializeRaw(TiltedPhoques::Buffer::Reader& aReader) noexcept override;

bool operator==(const NotifySyncExperience& acRhs) const noexcept
{
return GetOpcode() == acRhs.GetOpcode() &&
Experience == acRhs.Experience;
}

float Experience{};
};
13 changes: 13 additions & 0 deletions Code/encoding/Messages/SyncExperienceRequest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <Messages/SyncExperienceRequest.h>

void SyncExperienceRequest::SerializeRaw(TiltedPhoques::Buffer::Writer& aWriter) const noexcept
{
Serialization::WriteFloat(aWriter, Experience);
}

void SyncExperienceRequest::DeserializeRaw(TiltedPhoques::Buffer::Reader& aReader) noexcept
{
ClientMessage::DeserializeRaw(aReader);

Experience = Serialization::ReadFloat(aReader);
}
23 changes: 23 additions & 0 deletions Code/encoding/Messages/SyncExperienceRequest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "Message.h"

struct SyncExperienceRequest final : ClientMessage
{
static constexpr ClientOpcode Opcode = kSyncExperienceRequest;

SyncExperienceRequest() : ClientMessage(Opcode)
{
}

void SerializeRaw(TiltedPhoques::Buffer::Writer& aWriter) const noexcept override;
void DeserializeRaw(TiltedPhoques::Buffer::Reader& aReader) noexcept override;

bool operator==(const SyncExperienceRequest& acRhs) const noexcept
{
return GetOpcode() == acRhs.GetOpcode() &&
Experience == acRhs.Experience;
}

float Experience{};
};
2 changes: 2 additions & 0 deletions Code/encoding/Opcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enum ClientOpcode : unsigned char
kDrawWeaponRequest,
kMountRequest,
kNewPackageRequest,
kSyncExperienceRequest,
kClientOpcodeMax
};

Expand Down Expand Up @@ -78,5 +79,6 @@ enum ServerOpcode : unsigned char
kNotifyDrawWeapon,
kNotifyMount,
kNotifyNewPackage,
kNotifySyncExperience,
kServerOpcodeMax
};
13 changes: 13 additions & 0 deletions Code/server/GameServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,19 @@ void GameServer::SendToPlayersInRange(const ServerMessage& acServerMessage, cons
}
}

void GameServer::SendToParty(const ServerMessage& acServerMessage, const PartyComponent& acPartyComponent, const Player* apExcludeSender) const
{
for (Player* pPlayer : m_pWorld->GetPlayerManager())
{
if (pPlayer == apExcludeSender)
continue;

const auto& partyComponent = pPlayer->GetParty();
if (partyComponent.JoinedPartyId == acPartyComponent.JoinedPartyId)
pPlayer->Send(acServerMessage);
}
}

void GameServer::HandleAuthenticationRequest(const ConnectionId_t aConnectionId,
const UniquePtr<AuthenticationRequest>& acRequest)
{
Expand Down
1 change: 1 addition & 0 deletions Code/server/GameServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct GameServer final : Server
void SendToLoaded(const ServerMessage& acServerMessage) const;
void SendToPlayers(const ServerMessage& acServerMessage, const Player* apExcludeSender = nullptr) const;
void SendToPlayersInRange(const ServerMessage& acServerMessage, const entt::entity acOrigin, const Player* apExcludeSender = nullptr) const;
void SendToParty(const ServerMessage& acServerMessage, const PartyComponent& acPartyComponent, const Player* apExcludeSender = nullptr) const;

const Info& GetInfo() const noexcept
{
Expand Down
13 changes: 13 additions & 0 deletions Code/server/Services/CharacterService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include <Messages/NotifyMount.h>
#include <Messages/NewPackageRequest.h>
#include <Messages/NotifyNewPackage.h>
#include <Messages/SyncExperienceRequest.h>
#include <Messages/NotifySyncExperience.h>

CharacterService::CharacterService(World& aWorld, entt::dispatcher& aDispatcher) noexcept
: m_world(aWorld)
Expand All @@ -53,6 +55,7 @@ CharacterService::CharacterService(World& aWorld, entt::dispatcher& aDispatcher)
, m_projectileLaunchConnection(aDispatcher.sink<PacketEvent<ProjectileLaunchRequest>>().connect<&CharacterService::OnProjectileLaunchRequest>(this))
, m_mountConnection(aDispatcher.sink<PacketEvent<MountRequest>>().connect<&CharacterService::OnMountRequest>(this))
, m_newPackageConnection(aDispatcher.sink<PacketEvent<NewPackageRequest>>().connect<&CharacterService::OnNewPackageRequest>(this))
, m_syncExperienceConnection(aDispatcher.sink<PacketEvent<SyncExperienceRequest>>().connect<&CharacterService::OnSyncExperienceRequest>(this))
{
}

Expand Down Expand Up @@ -514,6 +517,16 @@ void CharacterService::OnNewPackageRequest(const PacketEvent<NewPackageRequest>&
GameServer::Get()->SendToPlayersInRange(notify, cEntity, acMessage.GetSender());
}

void CharacterService::OnSyncExperienceRequest(const PacketEvent<SyncExperienceRequest>& acMessage) const noexcept
{
NotifySyncExperience notify;
notify.Experience = acMessage.Packet.Experience;

const auto& partyComponent = acMessage.pPlayer->GetParty();

GameServer::Get()->SendToParty(notify, partyComponent, acMessage.GetSender());
}

void CharacterService::CreateCharacter(const PacketEvent<AssignCharacterRequest>& acMessage) const noexcept
{
auto& message = acMessage.Packet;
Expand Down
3 changes: 3 additions & 0 deletions Code/server/Services/CharacterService.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct OwnershipTransferEvent;
struct ProjectileLaunchRequest;
struct MountRequest;
struct NewPackageRequest;
struct SyncExperienceRequest;

struct CharacterService
{
Expand Down Expand Up @@ -47,6 +48,7 @@ struct CharacterService
void OnProjectileLaunchRequest(const PacketEvent<ProjectileLaunchRequest>& acMessage) const noexcept;
void OnMountRequest(const PacketEvent<MountRequest>& acMessage) const noexcept;
void OnNewPackageRequest(const PacketEvent<NewPackageRequest>& acMessage) const noexcept;
void OnSyncExperienceRequest(const PacketEvent<SyncExperienceRequest>& acMessage) const noexcept;

void CreateCharacter(const PacketEvent<AssignCharacterRequest>& acMessage) const noexcept;

Expand All @@ -72,4 +74,5 @@ struct CharacterService
entt::scoped_connection m_projectileLaunchConnection;
entt::scoped_connection m_mountConnection;
entt::scoped_connection m_newPackageConnection;
entt::scoped_connection m_syncExperienceConnection;
};

0 comments on commit a1c8149

Please sign in to comment.