Add promote/demote buttons to player tab.
Add player promoted event to IMultiplayerManagerServer
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using VRage.Game.ModAPI;
|
||||
|
||||
namespace Torch.API.Managers
|
||||
{
|
||||
@@ -21,6 +22,25 @@ namespace Torch.API.Managers
|
||||
/// </summary>
|
||||
void BanPlayer(ulong steamId, bool banned = true);
|
||||
|
||||
/// <summary>
|
||||
/// Promotes user if possible.
|
||||
/// </summary>
|
||||
/// <param name="steamId"></param>
|
||||
void PromoteUser(ulong steamId);
|
||||
|
||||
/// <summary>
|
||||
/// Demotes user if possible.
|
||||
/// </summary>
|
||||
/// <param name="steamId"></param>
|
||||
void DemoteUser(ulong steamId);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a user's promote level.
|
||||
/// </summary>
|
||||
/// <param name="steamId"></param>
|
||||
/// <returns></returns>
|
||||
MyPromoteLevel GetUserPromoteLevel(ulong steamId);
|
||||
|
||||
/// <summary>
|
||||
/// List of the banned SteamID's
|
||||
/// </summary>
|
||||
@@ -42,5 +62,10 @@ namespace Torch.API.Managers
|
||||
/// Raised when a player is banned or unbanned. Passes SteamID of player, and true if banned, false if unbanned.
|
||||
/// </summary>
|
||||
event Action<ulong, bool> PlayerBanned;
|
||||
|
||||
/// <summary>
|
||||
/// Raised when a player is promoted or demoted. Passes SteamID of player, and new promote level.
|
||||
/// </summary>
|
||||
event Action<ulong, MyPromoteLevel> PlayerPromoted;
|
||||
}
|
||||
}
|
||||
|
@@ -4,9 +4,11 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Sandbox.Engine.Multiplayer;
|
||||
using Sandbox.Game.World;
|
||||
using Torch.API;
|
||||
using Torch.API.Managers;
|
||||
using Torch.Managers;
|
||||
using VRage.Game.ModAPI;
|
||||
|
||||
namespace Torch.Client.Manager
|
||||
{
|
||||
@@ -24,6 +26,34 @@ namespace Torch.Client.Manager
|
||||
/// <inheritdoc />
|
||||
public void BanPlayer(ulong steamId, bool banned = true) => Torch.Invoke(() => MyMultiplayer.Static.BanClient(steamId, banned));
|
||||
|
||||
/// <inheritdoc />
|
||||
public void PromoteUser(ulong steamId)
|
||||
{
|
||||
Torch.Invoke(() =>
|
||||
{
|
||||
var p = MySession.Static.GetUserPromoteLevel(steamId);
|
||||
if (p < MyPromoteLevel.Admin) //cannot promote to owner by design
|
||||
MySession.Static.SetUserPromoteLevel(steamId, p + 1);
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void DemoteUser(ulong steamId)
|
||||
{
|
||||
Torch.Invoke(() =>
|
||||
{
|
||||
var p = MySession.Static.GetUserPromoteLevel(steamId);
|
||||
if (p > MyPromoteLevel.None && p < MyPromoteLevel.Owner) //owner cannot be demoted by design
|
||||
MySession.Static.SetUserPromoteLevel(steamId, p - 1);
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public MyPromoteLevel GetUserPromoteLevel(ulong steamId)
|
||||
{
|
||||
return MySession.Static.GetUserPromoteLevel(steamId);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsBanned(ulong steamId) => false;
|
||||
|
||||
@@ -41,6 +71,13 @@ namespace Torch.Client.Manager
|
||||
remove => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public event Action<ulong, MyPromoteLevel> PlayerPromoted
|
||||
{
|
||||
add => throw new NotImplementedException();
|
||||
remove => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Attach()
|
||||
{
|
||||
|
@@ -10,6 +10,7 @@ using NLog.Fluent;
|
||||
using Sandbox;
|
||||
using Sandbox.Engine.Multiplayer;
|
||||
using Sandbox.Engine.Networking;
|
||||
using Sandbox.Game.Gui;
|
||||
using Sandbox.Game.World;
|
||||
using Steamworks;
|
||||
using Torch.API;
|
||||
@@ -62,6 +63,36 @@ namespace Torch.Server.Managers
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void PromoteUser(ulong steamId)
|
||||
{
|
||||
Torch.Invoke(() =>
|
||||
{
|
||||
var p = MySession.Static.GetUserPromoteLevel(steamId);
|
||||
if (p < MyPromoteLevel.Admin) //cannot promote to owner by design
|
||||
//MySession.Static.SetUserPromoteLevel(steamId, p + 1);
|
||||
MyGuiScreenPlayers.PromoteImplementation(steamId, true);
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void DemoteUser(ulong steamId)
|
||||
{
|
||||
Torch.Invoke(() =>
|
||||
{
|
||||
var p = MySession.Static.GetUserPromoteLevel(steamId);
|
||||
if (p > MyPromoteLevel.None && p < MyPromoteLevel.Owner) //owner cannot be demoted by design
|
||||
//MySession.Static.SetUserPromoteLevel(steamId, p - 1);
|
||||
MyGuiScreenPlayers.PromoteImplementation(steamId, false);
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public MyPromoteLevel GetUserPromoteLevel(ulong steamId)
|
||||
{
|
||||
return MySession.Static.GetUserPromoteLevel(steamId);
|
||||
}
|
||||
|
||||
internal void RaiseClientBanned(ulong user, bool banned)
|
||||
{
|
||||
PlayerBanned?.Invoke(user, banned);
|
||||
@@ -87,6 +118,14 @@ namespace Torch.Server.Managers
|
||||
/// <inheritdoc />
|
||||
public event Action<ulong, bool> PlayerBanned;
|
||||
|
||||
/// <inheritdoc />
|
||||
public event Action<ulong, MyPromoteLevel> PlayerPromoted;
|
||||
|
||||
internal void RaisePromoteChanged(ulong steamId, MyPromoteLevel level)
|
||||
{
|
||||
PlayerPromoted?.Invoke(steamId, level);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Attach()
|
||||
{
|
||||
|
38
Torch.Server/Patches/PromotePatch.cs
Normal file
38
Torch.Server/Patches/PromotePatch.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using NLog;
|
||||
using Sandbox.Game.World;
|
||||
using Torch.Managers.PatchManager;
|
||||
using VRage.Game.ModAPI;
|
||||
using Torch.API.Managers;
|
||||
using Torch.Server.Managers;
|
||||
|
||||
namespace Torch.Patches
|
||||
{
|
||||
[PatchShim]
|
||||
internal static class PromotePatch
|
||||
{
|
||||
private static Logger _log = LogManager.GetCurrentClassLogger();
|
||||
private static IMultiplayerManagerServer _backing;
|
||||
|
||||
private static IMultiplayerManagerServer ServerManager => _backing ?? (_backing = TorchBase.Instance?.CurrentSession?.Managers.GetManager<IMultiplayerManagerServer>());
|
||||
|
||||
public static void Patch(PatchContext ctx)
|
||||
{
|
||||
_log.Info("patching promote");
|
||||
ctx.GetPattern(typeof(MySession).GetMethod("OnPromoteLevelSet", BindingFlags.NonPublic | BindingFlags.Static)).Prefixes.Add(typeof(PromotePatch).GetMethod(nameof(PromotePrefix)));
|
||||
}
|
||||
|
||||
public static void PromotePrefix(ulong steamId, MyPromoteLevel level)
|
||||
{
|
||||
if (ServerManager is MultiplayerManagerDedicated d)
|
||||
d.RaisePromoteChanged(steamId, level);
|
||||
else
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
}
|
@@ -227,6 +227,7 @@
|
||||
<Compile Include="Managers\MultiplayerManagerDedicatedPatchShim.cs" />
|
||||
<Compile Include="NativeMethods.cs" />
|
||||
<Compile Include="Initializer.cs" />
|
||||
<Compile Include="Patches\PromotePatch.cs" />
|
||||
<Compile Include="Properties\Annotations.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="TorchConfig.cs" />
|
||||
|
@@ -46,9 +46,9 @@ namespace Torch.Server
|
||||
|
||||
private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
_log.Info($"VisibleChanged: {IsVisible}");
|
||||
if (IsVisible)
|
||||
{
|
||||
//I hate this and I hate myself. You should hate me too
|
||||
Task.Run(() =>
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
|
@@ -8,12 +8,14 @@
|
||||
<StackPanel DockPanel.Dock="Bottom">
|
||||
<Button x:Name="KickButton" Content="Kick" Margin="5,5,5,5" Click="KickButton_Click"/>
|
||||
<Button x:Name="BanButton" Content="Ban" Margin="5,5,5,5" Click="BanButton_Click"/>
|
||||
<Button x:Name="PromoteButton" Content ="Promote" Margin="5,5,5,5" Click="PromoteButton_OnClick"/>
|
||||
<Button x:Name="DemoteButton" Content ="Demote" Margin="5,5,5,5" Click="DemoteButton_OnClick"/>
|
||||
</StackPanel>
|
||||
<ListView x:Name="PlayerList" ItemsSource="{Binding Players}" DockPanel.Dock="Top" Margin="5,5,5,5">
|
||||
<ListView.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<WrapPanel>
|
||||
<TextBlock Text="{Binding Value.Name}" FontWeight="Bold"/>
|
||||
<TextBlock Text="{Binding Value.PromotedName}" FontWeight="Bold"/>
|
||||
<TextBlock Text=" ("/>
|
||||
<TextBlock Text="{Binding Value.State}"/>
|
||||
<TextBlock Text=")"/>
|
||||
|
@@ -16,6 +16,7 @@ using NLog;
|
||||
using Torch;
|
||||
using Sandbox;
|
||||
using Sandbox.Engine.Multiplayer;
|
||||
using Sandbox.Game.Gui;
|
||||
using Sandbox.Game.Multiplayer;
|
||||
using Sandbox.Game.World;
|
||||
using Sandbox.ModAPI;
|
||||
@@ -24,6 +25,7 @@ using Torch.API.Managers;
|
||||
using Torch.API.Session;
|
||||
using Torch.Managers;
|
||||
using Torch.Server.Managers;
|
||||
using Torch.Utils;
|
||||
using Torch.ViewModels;
|
||||
using VRage.Game.ModAPI;
|
||||
|
||||
@@ -37,12 +39,18 @@ namespace Torch.Server
|
||||
private static readonly Logger _log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
private ITorchServer _server;
|
||||
private IMultiplayerManagerServer _mpServer;
|
||||
|
||||
public PlayerListControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void OnPlayerPromoted(ulong arg1, MyPromoteLevel arg2)
|
||||
{
|
||||
Dispatcher.InvokeAsync(() => PlayerList.Items.Refresh());
|
||||
}
|
||||
|
||||
public void BindServer(ITorchServer server)
|
||||
{
|
||||
_server = server;
|
||||
@@ -61,6 +69,8 @@ namespace Torch.Server
|
||||
{
|
||||
case TorchSessionState.Loaded:
|
||||
Dispatcher.InvokeAsync(() => DataContext = _server?.CurrentSession?.Managers.GetManager<MultiplayerManagerDedicated>());
|
||||
_mpServer = _server.CurrentSession.Managers.GetManager<IMultiplayerManagerServer>();
|
||||
_mpServer.PlayerPromoted += OnPlayerPromoted;
|
||||
break;
|
||||
case TorchSessionState.Unloading:
|
||||
Dispatcher.InvokeAsync(() => DataContext = null);
|
||||
@@ -93,5 +103,31 @@ namespace Torch.Server
|
||||
_log.Warn(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void PromoteButton_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var player = (KeyValuePair<ulong, PlayerViewModel>)PlayerList.SelectedItem;
|
||||
try
|
||||
{
|
||||
_server.CurrentSession.Managers.GetManager<IMultiplayerManagerServer>().PromoteUser(player.Key);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.Warn(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void DemoteButton_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var player = (KeyValuePair<ulong, PlayerViewModel>)PlayerList.SelectedItem;
|
||||
try
|
||||
{
|
||||
_server.CurrentSession.Managers.GetManager<IMultiplayerManagerServer>().DemoteUser(player.Key);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.Warn(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -4,7 +4,9 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Sandbox.Engine.Multiplayer;
|
||||
using Sandbox.Game.World;
|
||||
using Torch.API;
|
||||
using VRage.Game.ModAPI;
|
||||
using VRage.Replication;
|
||||
|
||||
namespace Torch.ViewModels
|
||||
@@ -15,6 +17,19 @@ namespace Torch.ViewModels
|
||||
public string Name { get; }
|
||||
private ConnectionState _state;
|
||||
public ConnectionState State { get => _state; set { _state = value; OnPropertyChanged(); } }
|
||||
public MyPromoteLevel PromoteLevel => MySession.Static.GetUserPromoteLevel(SteamId);
|
||||
|
||||
public string PromotedName
|
||||
{
|
||||
get
|
||||
{
|
||||
var p = PromoteLevel;
|
||||
if (p <= MyPromoteLevel.None)
|
||||
return Name;
|
||||
else
|
||||
return $"{Name} ({p})";
|
||||
}
|
||||
}
|
||||
|
||||
public PlayerViewModel(ulong steamId, string name = null)
|
||||
{
|
||||
|
Reference in New Issue
Block a user