Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

Commit

Permalink
Reworked reserved slots, gave up on chat logging
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxar-tc committed Feb 11, 2016
1 parent f11d8ce commit dc64a50
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 40 deletions.
Binary file modified .vs/EssentialsPlugin/EssentialsPlugin.scgdat
Binary file not shown.
6 changes: 3 additions & 3 deletions EssentialsPlugin/AssemblyFileVersion.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//166
//167
//
// This code was generated by a tool. Any changes made manually will be lost
// the next time this code is regenerated.
//

using System.Reflection;

[assembly: AssemblyFileVersion("1.13.5.166")]
[assembly: AssemblyVersion("1.13.5.166")]
[assembly: AssemblyFileVersion("1.13.5.167")]
[assembly: AssemblyVersion("1.13.5.167")]
3 changes: 2 additions & 1 deletion EssentialsPlugin/Essentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,8 @@ public void OnChatReceived( ChatManager.ChatEvent obj )
{
if ( obj.Message[0] != '/' )
{
ChatHistory.AddChat( obj );
//one day this will work. today is not that day :(
//ChatHistory.AddChat( obj );
return;
}

Expand Down
123 changes: 88 additions & 35 deletions EssentialsPlugin/ProcessHandlers/ProcessReservedSlots.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sandbox.Engine.Multiplayer;
using Sandbox.ModAPI;
using SEModAPIExtensions.API;
using SEModAPIInternal.API.Common;
using SEModAPIInternal.API.Server;
using SteamSDK;

namespace EssentialsPlugin.ProcessHandlers
namespace EssentialsPlugin.ProcessHandlers
{
using System;
using System.Collections.Generic;
using System.Linq;
using Sandbox.Engine.Multiplayer;
using Sandbox.Game.AI.BrainSimulatorIntegration;
using Sandbox.ModAPI;
using SEModAPIExtensions.API;
using SEModAPIInternal.API.Common;
using SEModAPIInternal.API.Server;
using SteamSDK;
using VRageMath;

public class ProcessReservedSlots : ProcessHandlerBase
{
private static List<ulong> reservedPlayers = new List<ulong>( );
private static List<ulong> waitingPlayers = new List<ulong>( );
private static List<IMyPlayer> connectedPlayers = new List<IMyPlayer>( );
private static bool _init = false;
private static readonly List<ulong> ReservedPlayers = new List<ulong>( );
private static readonly List<ulong> WaitingPlayers = new List<ulong>( );
private static readonly List<IMyPlayer> ConnectedPlayers = new List<IMyPlayer>( );
private static bool _init;

public static void Init( )
{
if ( _init )
if (_init)
return;

_init = true;
SteamSDK.SteamServerAPI.Instance.GameServer.UserGroupStatus += GameServer_UserGroupStatus;
//SteamSDK.SteamServerAPI.Instance.GameServer.ValidateAuthTicketResponse -= MyDedicatedServerBase.GameServer_ValidateAuthTicketResponse( );
SteamServerAPI.Instance.GameServer.UserGroupStatus += GameServer_UserGroupStatus;
SteamServerAPI.Instance.GameServer.ValidateAuthTicketResponse += GameServer_ValidateAuthTicketResponse;

Essentials.Log.Info( "Reserved slots initialized" );
}

private static void GameServer_ValidateAuthTicketResponse( ulong remoteUserId, AuthSessionResponseEnum response,
ulong ownerSteamId )
{
//using the player join event takes too long, sometimes they can load in before we boot them
//we're not replacing the lobby yet, but hooking into this event will give us more time to verify players

if (!PluginSettings.Instance.ReservedSlotsEnabled)
return;

if (response != AuthSessionResponseEnum.OK)
return;

if (PluginSettings.Instance.ReservedSlotsPlayers.Contains( remoteUserId.ToString( ) ))
{
ReservedPlayers.Add( remoteUserId );
Essentials.Log.Info( "Whitelisted player connected: " + remoteUserId );
Essentials.Log.Info( "{0} whitelisted players connected. {1} of {2} reserved slots allocated.",
ReservedPlayers.Count,
Math.Min( ReservedPlayers.Count, PluginSettings.Instance.ReservedSlotsCount ),
PluginSettings.Instance.ReservedSlotsCount );
return;
}

if (PluginSettings.Instance.ReservedSlotsAdmins && PlayerManager.Instance.IsUserAdmin( remoteUserId ))
{
ReservedPlayers.Add( remoteUserId );
Essentials.Log.Info( "Whitelisted admin connected: " + remoteUserId );
Essentials.Log.Info( "{0} whitelisted players connected. {1} of {2} reserved slots allocated.",
ReservedPlayers.Count,
Math.Min( ReservedPlayers.Count, PluginSettings.Instance.ReservedSlotsCount ),
PluginSettings.Instance.ReservedSlotsCount );

return;
}

if (PluginSettings.Instance.ReservedSlotsGroup != 0)
{
//ask Steam if the connecting player is in the whitelisted group. response is raised as an event; GameServer_UserGroupStatus
SteamServerAPI.Instance.GameServer.RequestGroupStatus( remoteUserId,
PluginSettings.Instance.ReservedSlotsGroup );
WaitingPlayers.Add( remoteUserId );
return;
}

DenyPlayer( remoteUserId );
}

/*
public override void OnPlayerJoined( ulong remoteUserId )
{
//it might be better to hook into ValidateAuthTicketResponse, but doing it this way lets the game
Expand Down Expand Up @@ -69,25 +117,27 @@ public override void OnPlayerJoined( ulong remoteUserId )
DenyPlayer( remoteUserId );
}
*/

private static void DenyPlayer( ulong remoteUserId )
{
//get the list of current players just so we can count them (this is a stupid solution)
MyAPIGateway.Players.GetPlayers( connectedPlayers );
MyAPIGateway.Players.GetPlayers( ConnectedPlayers );

int publicPlayers = connectedPlayers.Count - Math.Min( reservedPlayers.Count, PluginSettings.Instance.ReservedSlotsCount );
int publicPlayers = ConnectedPlayers.Count -
Math.Min( ReservedPlayers.Count, PluginSettings.Instance.ReservedSlotsCount );
int publicSlots = Server.Instance.Config.MaxPlayers - PluginSettings.Instance.ReservedSlotsCount;

if ( publicPlayers < publicSlots )
if (publicPlayers < publicSlots)
return;

//don't do anything while we're waiting for group authorization
if ( waitingPlayers.Contains( remoteUserId ) )
if (WaitingPlayers.Contains( remoteUserId ))
return;

//kick the player with the "Server is full" message
//too bad we can't send a custom message, but they're hardcoded into the client
Essentials.Log.Info( "Player denied: " + remoteUserId.ToString( ) );
Essentials.Log.Info( "Player denied: " + remoteUserId );
JoinResultMsg msg = new JoinResultMsg( );
msg.JoinResult = JoinResult.ServerFull;
msg.Admin = 0;
Expand All @@ -96,17 +146,19 @@ private static void DenyPlayer( ulong remoteUserId )

private static void GameServer_UserGroupStatus( ulong userId, ulong groupId, bool member, bool officier )
{
if ( !PluginSettings.Instance.ReservedSlotsEnabled )
if (!PluginSettings.Instance.ReservedSlotsEnabled)
return;

if ( groupId == PluginSettings.Instance.ReservedSlotsGroup && waitingPlayers.Remove( userId ) )
if (groupId == PluginSettings.Instance.ReservedSlotsGroup && WaitingPlayers.Remove( userId ))
{
if ( member || officier )
if (member || officier)
{
reservedPlayers.Add( userId );
Essentials.Log.Info( "Whitelisted player connected: " + userId.ToString( ) );
Essentials.Log.Info( string.Format( "{0} whitelisted players connected. {1} of {2} reserved slots allocated.",
reservedPlayers.Count, Math.Min( reservedPlayers.Count, PluginSettings.Instance.ReservedSlotsCount ), PluginSettings.Instance.ReservedSlotsCount ) );
ReservedPlayers.Add( userId );
Essentials.Log.Info( "Whitelisted player connected: " + userId );
Essentials.Log.Info( "{0} whitelisted players connected. {1} of {2} reserved slots allocated.",
ReservedPlayers.Count,
Math.Min( ReservedPlayers.Count, PluginSettings.Instance.ReservedSlotsCount ),
PluginSettings.Instance.ReservedSlotsCount );
}
else
DenyPlayer( userId );
Expand All @@ -116,11 +168,12 @@ private static void GameServer_UserGroupStatus( ulong userId, ulong groupId, boo
public override void OnPlayerLeft( ulong remoteUserId )
{
//free up allocated slots so someone else can use it
if ( reservedPlayers.Contains( remoteUserId ) )
if (ReservedPlayers.Contains( remoteUserId ))
{
Essentials.Log.Info( "Freed slot from: " + remoteUserId );
reservedPlayers.Remove( remoteUserId );
Essentials.Log.Info( string.Format( "{0} slots of {1} allocated.", reservedPlayers.Count, PluginSettings.Instance.ReservedSlotsCount ) );
ReservedPlayers.Remove( remoteUserId );
Essentials.Log.Info( "{0} slots of {1} allocated.", ReservedPlayers.Count,
PluginSettings.Instance.ReservedSlotsCount );
}
}
}
Expand Down
1 change: 0 additions & 1 deletion EssentialsPlugin/Utility/ChatHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public static void AddChat( ChatManager.ChatEvent obj )
MethodInfo globalMessageInfo = syncCharacter.GetMethod( "SendNewGlobalMessage",
BindingFlags.Public | BindingFlags.Instance );
globalMessageInfo = globalMessageInfo.MakeGenericMethod( typeof( MyPlayer.PlayerId ), typeof( string ) );
MyPlayer.PlayerId id = new MyPlayer.PlayerId( obj.SourceUserId );
object classInstance = Activator.CreateInstance( syncCharacter );
globalMessageInfo.Invoke( classInstance, new object[ ] { new MyPlayer.PlayerId( obj.SourceUserId ), obj.Message } );
}
Expand Down

0 comments on commit dc64a50

Please sign in to comment.