2021-12-05 17:54:36 +00:00
/*
* Darkflame Universe
* Copyright 2018
*/
# include "ClientPackets.h"
# include "UserManager.h"
# include "User.h"
# include "Character.h"
# include "EntityManager.h"
# include "Entity.h"
# include "ControllablePhysicsComponent.h"
# include "Game.h"
# include "dLogger.h"
# include "WorldPackets.h"
# include "NiPoint3.h"
# include "NiQuaternion.h"
# include "dCommonVars.h"
# include "BitStream.h"
# include "dChatFilter.h"
# include "WorldPackets.h"
# include "ChatPackets.h"
# include "dServer.h"
# include "GameMessages.h"
# include "dZoneManager.h"
# include "Player.h"
# include "Zone.h"
# include "PossessorComponent.h"
# include "PossessableComponent.h"
# include "VehiclePhysicsComponent.h"
# include "dConfig.h"
# include "CharacterComponent.h"
2022-07-17 08:40:34 +00:00
# include "Database.h"
2023-01-07 05:17:05 +00:00
# include "dMessageIdentifiers.h"
2022-07-17 08:40:34 +00:00
2021-12-05 17:54:36 +00:00
void ClientPackets : : HandleChatMessage ( const SystemAddress & sysAddr , Packet * packet ) {
User * user = UserManager : : Instance ( ) - > GetUser ( sysAddr ) ;
if ( ! user ) {
2022-07-25 02:26:51 +00:00
Game : : logger - > Log ( " ClientPackets " , " Unable to get user to parse chat message " ) ;
2021-12-05 17:54:36 +00:00
return ;
}
if ( user - > GetIsMuted ( ) ) {
user - > GetLastUsedChar ( ) - > SendMuteNotice ( ) ;
return ;
}
CINSTREAM ;
uint64_t header ;
inStream . Read ( header ) ;
char chatChannel ;
uint16_t unknown ;
uint32_t messageLength ;
std : : u16string message ;
inStream . Read ( chatChannel ) ;
inStream . Read ( unknown ) ;
inStream . Read ( messageLength ) ;
for ( uint32_t i = 0 ; i < ( messageLength - 1 ) ; + + i ) {
uint16_t character ;
inStream . Read ( character ) ;
message . push_back ( character ) ;
}
std : : string playerName = user - > GetLastUsedChar ( ) - > GetName ( ) ;
bool isMythran = user - > GetLastUsedChar ( ) - > GetGMLevel ( ) > 0 ;
if ( ! user - > GetLastChatMessageApproved ( ) & & ! isMythran ) return ;
std : : string sMessage = GeneralUtils : : UTF16ToWTF8 ( message ) ;
2022-07-25 02:26:51 +00:00
Game : : logger - > Log ( " Chat " , " %s: %s " , playerName . c_str ( ) , sMessage . c_str ( ) ) ;
2021-12-05 17:54:36 +00:00
ChatPackets : : SendChatMessage ( sysAddr , chatChannel , playerName , user - > GetLoggedInChar ( ) , isMythran , message ) ;
}
void ClientPackets : : HandleClientPositionUpdate ( const SystemAddress & sysAddr , Packet * packet ) {
User * user = UserManager : : Instance ( ) - > GetUser ( sysAddr ) ;
if ( ! user ) {
2022-07-25 02:26:51 +00:00
Game : : logger - > Log ( " ClientPackets " , " Unable to get user to parse position update " ) ;
2021-12-05 17:54:36 +00:00
return ;
}
CINSTREAM ;
uint64_t header ;
inStream . Read ( header ) ;
Entity * entity = EntityManager : : Instance ( ) - > GetEntity ( user - > GetLastUsedChar ( ) - > GetObjectID ( ) ) ;
if ( ! entity ) return ;
ControllablePhysicsComponent * comp = static_cast < ControllablePhysicsComponent * > ( entity - > GetComponent ( COMPONENT_TYPE_CONTROLLABLE_PHYSICS ) ) ;
if ( ! comp ) return ;
/*
//If we didn't move, this will match and stop our velocity
if ( packet - > length = = 37 ) {
NiPoint3 zeroVel ( 0.0f , 0.0f , 0.0f ) ;
comp - > SetVelocity ( zeroVel ) ;
comp - > SetAngularVelocity ( zeroVel ) ;
comp - > SetIsOnGround ( true ) ; //probably8
EntityManager : : Instance ( ) - > SerializeEntity ( entity ) ;
return ;
}
*/
auto * possessorComponent = entity - > GetComponent < PossessorComponent > ( ) ;
NiPoint3 position ;
inStream . Read ( position . x ) ;
inStream . Read ( position . y ) ;
inStream . Read ( position . z ) ;
NiQuaternion rotation ;
inStream . Read ( rotation . x ) ;
inStream . Read ( rotation . y ) ;
inStream . Read ( rotation . z ) ;
inStream . Read ( rotation . w ) ;
bool onGround = false ;
bool onRail = false ;
inStream . Read ( onGround ) ;
inStream . Read ( onRail ) ;
bool velocityFlag = false ;
inStream . Read ( velocityFlag ) ;
NiPoint3 velocity { } ;
if ( velocityFlag ) {
inStream . Read ( velocity . x ) ;
inStream . Read ( velocity . y ) ;
inStream . Read ( velocity . z ) ;
}
bool angVelocityFlag = false ;
inStream . Read ( angVelocityFlag ) ;
NiPoint3 angVelocity { } ;
if ( angVelocityFlag ) {
inStream . Read ( angVelocity . x ) ;
inStream . Read ( angVelocity . y ) ;
inStream . Read ( angVelocity . z ) ;
}
2022-09-02 18:49:19 +00:00
bool updateChar = true ;
2021-12-05 17:54:36 +00:00
if ( possessorComponent ! = nullptr ) {
auto * possassableEntity = EntityManager : : Instance ( ) - > GetEntity ( possessorComponent - > GetPossessable ( ) ) ;
if ( possassableEntity ! = nullptr ) {
2022-09-02 18:49:19 +00:00
auto * possessableComponent = possassableEntity - > GetComponent < PossessableComponent > ( ) ;
if ( possessableComponent ) {
// While possessing something, only update char if we are attached to the thing we are possessing
if ( possessableComponent - > GetPossessionType ( ) ! = ePossessionType : : ATTACHED_VISIBLE ) updateChar = false ;
}
2021-12-05 17:54:36 +00:00
2022-09-02 18:49:19 +00:00
auto * vehiclePhysicsComponent = possassableEntity - > GetComponent < VehiclePhysicsComponent > ( ) ;
2021-12-05 17:54:36 +00:00
if ( vehiclePhysicsComponent ! = nullptr ) {
// This is flipped for whatever reason
rotation = NiQuaternion ( rotation . z , rotation . y , rotation . x , rotation . w ) ;
vehiclePhysicsComponent - > SetPosition ( position ) ;
vehiclePhysicsComponent - > SetRotation ( rotation ) ;
vehiclePhysicsComponent - > SetIsOnGround ( onGround ) ;
vehiclePhysicsComponent - > SetIsOnRail ( onRail ) ;
vehiclePhysicsComponent - > SetVelocity ( velocity ) ;
vehiclePhysicsComponent - > SetDirtyVelocity ( velocityFlag ) ;
vehiclePhysicsComponent - > SetAngularVelocity ( angVelocity ) ;
vehiclePhysicsComponent - > SetDirtyAngularVelocity ( angVelocityFlag ) ;
2022-09-02 18:49:19 +00:00
} else {
// Need to get the mount's controllable physics
auto * controllablePhysicsComponent = possassableEntity - > GetComponent < ControllablePhysicsComponent > ( ) ;
if ( ! controllablePhysicsComponent ) return ;
controllablePhysicsComponent - > SetPosition ( position ) ;
controllablePhysicsComponent - > SetRotation ( rotation ) ;
controllablePhysicsComponent - > SetIsOnGround ( onGround ) ;
controllablePhysicsComponent - > SetIsOnRail ( onRail ) ;
controllablePhysicsComponent - > SetVelocity ( velocity ) ;
controllablePhysicsComponent - > SetDirtyVelocity ( velocityFlag ) ;
controllablePhysicsComponent - > SetAngularVelocity ( angVelocity ) ;
controllablePhysicsComponent - > SetDirtyAngularVelocity ( angVelocityFlag ) ;
2021-12-05 17:54:36 +00:00
}
2022-09-02 18:49:19 +00:00
EntityManager : : Instance ( ) - > SerializeEntity ( possassableEntity ) ;
2021-12-05 17:54:36 +00:00
}
}
2022-09-02 18:49:19 +00:00
if ( ! updateChar ) {
2021-12-05 17:54:36 +00:00
velocity = NiPoint3 : : ZERO ;
angVelocity = NiPoint3 : : ZERO ;
}
2022-09-02 18:49:19 +00:00
2021-12-05 17:54:36 +00:00
// Handle statistics
auto * characterComponent = entity - > GetComponent < CharacterComponent > ( ) ;
if ( characterComponent ! = nullptr ) {
2022-07-28 13:39:57 +00:00
characterComponent - > TrackPositionUpdate ( position ) ;
2021-12-05 17:54:36 +00:00
}
comp - > SetPosition ( position ) ;
comp - > SetRotation ( rotation ) ;
comp - > SetIsOnGround ( onGround ) ;
comp - > SetIsOnRail ( onRail ) ;
comp - > SetVelocity ( velocity ) ;
comp - > SetDirtyVelocity ( velocityFlag ) ;
comp - > SetAngularVelocity ( angVelocity ) ;
comp - > SetDirtyAngularVelocity ( angVelocityFlag ) ;
auto * player = static_cast < Player * > ( entity ) ;
player - > SetGhostReferencePoint ( position ) ;
EntityManager : : Instance ( ) - > QueueGhostUpdate ( player - > GetObjectID ( ) ) ;
2022-09-02 18:49:19 +00:00
if ( updateChar ) EntityManager : : Instance ( ) - > SerializeEntity ( entity ) ;
2021-12-05 17:54:36 +00:00
//TODO: add moving platform stuffs
/*bool movingPlatformFlag;
inStream . Read ( movingPlatformFlag ) ;
if ( movingPlatformFlag ) {
LWOOBJID objectID ;
NiPoint3 niData2 ;
inStream . Read ( objectID ) ;
inStream . Read ( niData2 . x ) ;
inStream . Read ( niData2 . y ) ;
inStream . Read ( niData2 . z ) ;
bool niData3Flag ;
inStream . Read ( niData3Flag ) ;
if ( niData3Flag ) {
NiPoint3 niData3 ;
inStream . Read ( niData3 . x ) ;
inStream . Read ( niData3 . y ) ;
inStream . Read ( niData3 . z ) ;
controllablePhysics - > GetLocationData ( ) - > GetMovingPlatformData ( ) - > SetData3 ( niData3 ) ;
}
} */
/*
for ( int i = 0 ; i < Game : : server - > GetReplicaManager ( ) - > GetParticipantCount ( ) ; + + i )
{
const auto & player = Game : : server - > GetReplicaManager ( ) - > GetParticipantAtIndex ( i ) ;
if ( entity - > GetSystemAddress ( ) = = player )
{
continue ;
}
EntityManager : : Instance ( ) - > SerializeEntity ( entity , player ) ;
}
*/
}
void ClientPackets : : HandleChatModerationRequest ( const SystemAddress & sysAddr , Packet * packet ) {
User * user = UserManager : : Instance ( ) - > GetUser ( sysAddr ) ;
if ( ! user ) {
2022-07-25 02:26:51 +00:00
Game : : logger - > Log ( " ClientPackets " , " Unable to get user to parse chat moderation request " ) ;
2021-12-05 17:54:36 +00:00
return ;
}
auto * entity = Player : : GetPlayer ( sysAddr ) ;
if ( entity = = nullptr ) {
2022-07-25 02:26:51 +00:00
Game : : logger - > Log ( " ClientPackets " , " Unable to get player to parse chat moderation request " ) ;
2021-12-05 17:54:36 +00:00
return ;
}
// Check if the player has restricted chat access
auto * character = entity - > GetCharacter ( ) ;
2022-07-28 13:39:57 +00:00
if ( character - > HasPermission ( PermissionMap : : RestrictedChatAccess ) ) {
2021-12-05 17:54:36 +00:00
// Send a message to the player
ChatPackets : : SendSystemMessage (
sysAddr ,
u " This character has restricted chat access. "
) ;
return ;
}
RakNet : : BitStream stream ( packet - > data , packet - > length , false ) ;
uint64_t header ;
stream . Read ( header ) ;
// Data
uint8_t chatLevel ;
uint8_t requestID ;
uint16_t messageLength ;
std : : string receiver = " " ;
std : : string message = " " ;
stream . Read ( chatLevel ) ;
stream . Read ( requestID ) ;
for ( uint32_t i = 0 ; i < 42 ; + + i ) {
uint16_t character ;
stream . Read ( character ) ;
receiver . push_back ( static_cast < uint8_t > ( character ) ) ;
}
2022-07-17 08:40:34 +00:00
if ( ! receiver . empty ( ) ) {
2022-07-18 09:01:43 +00:00
if ( std : : string ( receiver . c_str ( ) , 4 ) = = " [GM] " ) { // Shift the string forward if we are speaking to a GM as the client appends "[GM]" if they are
2022-07-17 08:40:34 +00:00
receiver = std : : string ( receiver . c_str ( ) + 4 , receiver . size ( ) - 4 ) ;
}
}
2021-12-05 17:54:36 +00:00
stream . Read ( messageLength ) ;
for ( uint32_t i = 0 ; i < messageLength ; + + i ) {
uint16_t character ;
stream . Read ( character ) ;
message . push_back ( static_cast < uint8_t > ( character ) ) ;
}
2022-07-17 08:40:34 +00:00
bool isBestFriend = false ;
if ( chatLevel = = 1 ) {
// Private chat
LWOOBJID idOfReceiver = LWOOBJID_EMPTY ;
{
sql : : PreparedStatement * stmt = Database : : CreatePreppedStmt ( " SELECT name FROM charinfo WHERE name = ? " ) ;
stmt - > setString ( 1 , receiver ) ;
sql : : ResultSet * res = stmt - > executeQuery ( ) ;
if ( res - > next ( ) ) {
idOfReceiver = res - > getInt ( " id " ) ;
}
2022-07-18 09:01:43 +00:00
delete stmt ;
delete res ;
2022-07-17 08:40:34 +00:00
}
if ( user - > GetIsBestFriendMap ( ) . find ( receiver ) = = user - > GetIsBestFriendMap ( ) . end ( ) & & idOfReceiver ! = LWOOBJID_EMPTY ) {
sql : : PreparedStatement * stmt = Database : : CreatePreppedStmt ( " SELECT * FROM friends WHERE (player_id = ? AND friend_id = ?) OR (player_id = ? AND friend_id = ?) LIMIT 1; " ) ;
stmt - > setInt ( 1 , entity - > GetObjectID ( ) ) ;
stmt - > setInt ( 2 , idOfReceiver ) ;
stmt - > setInt ( 3 , idOfReceiver ) ;
stmt - > setInt ( 4 , entity - > GetObjectID ( ) ) ;
sql : : ResultSet * res = stmt - > executeQuery ( ) ;
if ( res - > next ( ) ) {
isBestFriend = res - > getInt ( " best_friend " ) = = 3 ;
}
if ( isBestFriend ) {
auto tmpBestFriendMap = user - > GetIsBestFriendMap ( ) ;
tmpBestFriendMap [ receiver ] = true ;
user - > SetIsBestFriendMap ( tmpBestFriendMap ) ;
}
delete res ;
delete stmt ;
2022-07-28 13:39:57 +00:00
} else if ( user - > GetIsBestFriendMap ( ) . find ( receiver ) ! = user - > GetIsBestFriendMap ( ) . end ( ) ) {
2022-07-17 08:40:34 +00:00
isBestFriend = true ;
}
}
2022-07-18 09:01:43 +00:00
std : : vector < std : : pair < uint8_t , uint8_t > > segments = Game : : chatFilter - > IsSentenceOkay ( message , entity - > GetGMLevel ( ) , ! ( isBestFriend & & chatLevel = = 1 ) ) ;
2022-07-17 06:54:36 +00:00
bool bAllClean = segments . empty ( ) ;
2021-12-05 17:54:36 +00:00
if ( user - > GetIsMuted ( ) ) {
bAllClean = false ;
}
user - > SetLastChatMessageApproved ( bAllClean ) ;
2022-07-18 09:01:43 +00:00
WorldPackets : : SendChatModerationResponse ( sysAddr , bAllClean , requestID , receiver , segments ) ;
2021-12-05 17:54:36 +00:00
}