mirror of
https://github.com/rafi1212122/BLHX.Server.git
synced 2026-02-04 08:45:07 +01:00
extending command for user and prevent err in build ship menu
This commit is contained in:
@@ -335,15 +335,21 @@ namespace BLHX.Server.Common.Database
|
|||||||
case ResourceFieldType.Gold:
|
case ResourceFieldType.Gold:
|
||||||
if (Data.Data.GoldFieldTemplate.TryGetValue((int)Level, out var goldTemplate))
|
if (Data.Data.GoldFieldTemplate.TryGetValue((int)Level, out var goldTemplate))
|
||||||
{
|
{
|
||||||
|
var res = Player.Resources.FirstOrDefault(x => x.Id == 7);
|
||||||
var num = (goldTemplate.HourTime * goldTemplate.Production) / 3600f * LastHarvestTime.GetSecondsPassed();
|
var num = (goldTemplate.HourTime * goldTemplate.Production) / 3600f * LastHarvestTime.GetSecondsPassed();
|
||||||
Player.DoResource(7, (int)Math.Min((uint)Math.Floor(num), goldTemplate.Store));
|
|
||||||
|
if (res is not null)
|
||||||
|
res.Num = Math.Min((uint)Math.Floor(num), goldTemplate.Store);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ResourceFieldType.Oil:
|
case ResourceFieldType.Oil:
|
||||||
if (Data.Data.OilFieldTemplate.TryGetValue((int)Level, out var oilTemplate))
|
if (Data.Data.OilFieldTemplate.TryGetValue((int)Level, out var oilTemplate))
|
||||||
{
|
{
|
||||||
|
var res = Player.Resources.FirstOrDefault(x => x.Id == 5);
|
||||||
var num = (oilTemplate.HourTime * oilTemplate.Production) / 3600f * LastHarvestTime.GetSecondsPassed();
|
var num = (oilTemplate.HourTime * oilTemplate.Production) / 3600f * LastHarvestTime.GetSecondsPassed();
|
||||||
Player.DoResource(5, (int)Math.Min((uint)Math.Floor(num), oilTemplate.Store));
|
|
||||||
|
if (res is not null)
|
||||||
|
res.Num = Math.Min((uint)Math.Floor(num), oilTemplate.Store);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,8 +30,30 @@ public class ArgumentAttribute : Attribute
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Flags]
|
||||||
|
public enum CommandUsage
|
||||||
|
{
|
||||||
|
None = 0,
|
||||||
|
Console = 1,
|
||||||
|
User = 2
|
||||||
|
}
|
||||||
|
|
||||||
public abstract class Command
|
public abstract class Command
|
||||||
{
|
{
|
||||||
|
public virtual CommandUsage Usage
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var usage = CommandUsage.None;
|
||||||
|
if (GetType().GetMethod(nameof(Execute), [typeof(Dictionary<string, string>), typeof(Connection)])?.DeclaringType == GetType())
|
||||||
|
usage |= CommandUsage.User;
|
||||||
|
if (GetType().GetMethod(nameof(Execute), [typeof(Dictionary<string, string>)])?.DeclaringType == GetType())
|
||||||
|
usage |= CommandUsage.Console;
|
||||||
|
|
||||||
|
return usage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
readonly Dictionary<string, PropertyInfo> argsProperties;
|
readonly Dictionary<string, PropertyInfo> argsProperties;
|
||||||
|
|
||||||
public Command()
|
public Command()
|
||||||
@@ -50,6 +72,11 @@ public abstract class Command
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual void Execute(Dictionary<string, string> args, Connection connection)
|
||||||
|
{
|
||||||
|
Execute(args);
|
||||||
|
}
|
||||||
|
|
||||||
protected T Parse<T>(string? value, T fallback = default!)
|
protected T Parse<T>(string? value, T fallback = default!)
|
||||||
{
|
{
|
||||||
var tryParseMethod = typeof(T).GetMethod("TryParse", [typeof(string), typeof(T).MakeByRefType()]);
|
var tryParseMethod = typeof(T).GetMethod("TryParse", [typeof(string), typeof(T).MakeByRefType()]);
|
||||||
@@ -72,10 +99,12 @@ public static class CommandHandlerFactory
|
|||||||
public static readonly List<Command> Commands = new List<Command>();
|
public static readonly List<Command> Commands = new List<Command>();
|
||||||
|
|
||||||
static readonly Dictionary<string, Action<Dictionary<string, string>>> commandFunctions;
|
static readonly Dictionary<string, Action<Dictionary<string, string>>> commandFunctions;
|
||||||
|
static readonly Dictionary<string, Action<Dictionary<string, string>, Connection>> commandFunctionsConn;
|
||||||
|
|
||||||
static CommandHandlerFactory()
|
static CommandHandlerFactory()
|
||||||
{
|
{
|
||||||
commandFunctions = new Dictionary<string, Action<Dictionary<string, string>>>(StringComparer.OrdinalIgnoreCase);
|
commandFunctions = new Dictionary<string, Action<Dictionary<string, string>>>(StringComparer.OrdinalIgnoreCase);
|
||||||
|
commandFunctionsConn = new Dictionary<string, Action<Dictionary<string, string>, Connection>>(StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
RegisterCommands(Assembly.GetExecutingAssembly());
|
RegisterCommands(Assembly.GetExecutingAssembly());
|
||||||
}
|
}
|
||||||
@@ -91,24 +120,23 @@ public static class CommandHandlerFactory
|
|||||||
if (commandAttribute != null)
|
if (commandAttribute != null)
|
||||||
{
|
{
|
||||||
var commandInstance = (Command)Activator.CreateInstance(commandType)!;
|
var commandInstance = (Command)Activator.CreateInstance(commandType)!;
|
||||||
commandFunctions[commandAttribute.Name] = commandInstance.Execute;
|
|
||||||
|
if (commandInstance.Usage.HasFlag(CommandUsage.Console))
|
||||||
|
commandFunctions[commandAttribute.Name] = commandInstance.Execute;
|
||||||
|
if (commandInstance.Usage.HasFlag(CommandUsage.User))
|
||||||
|
commandFunctionsConn[commandAttribute.Name] = commandInstance.Execute;
|
||||||
|
|
||||||
Commands.Add(commandInstance);
|
Commands.Add(commandInstance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void HandleCommand(string commandLine)
|
public static void HandleCommand(string commandLine, Connection? connection = null)
|
||||||
{
|
{
|
||||||
var parts = commandLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
var parts = commandLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
||||||
if (parts.Length == 0)
|
if (parts.Length == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!commandFunctions.TryGetValue(parts[0], out var command))
|
|
||||||
{
|
|
||||||
Logger.c.Warn($"Unknown command: {parts[0]}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var arguments = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
var arguments = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||||
for (var i = 1; i < parts.Length; i++)
|
for (var i = 1; i < parts.Length; i++)
|
||||||
{
|
{
|
||||||
@@ -117,6 +145,25 @@ public static class CommandHandlerFactory
|
|||||||
arguments[argParts[0]] = argParts[1];
|
arguments[argParts[0]] = argParts[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
command(arguments);
|
if (connection is not null)
|
||||||
|
{
|
||||||
|
if (!(commandFunctionsConn).TryGetValue(parts[0], out var command))
|
||||||
|
{
|
||||||
|
Logger.c.Warn($"Unknown command: {parts[0]}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
command(arguments, connection);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!(commandFunctions).TryGetValue(parts[0], out var command))
|
||||||
|
{
|
||||||
|
Logger.c.Warn($"Unknown command: {parts[0]}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
command(arguments);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Reflection;
|
using BLHX.Server.Game.Handlers;
|
||||||
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace BLHX.Server.Game.Commands;
|
namespace BLHX.Server.Game.Commands;
|
||||||
@@ -15,7 +16,29 @@ public class HelpCommand : Command
|
|||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
sb.AppendLine("Available Commands: ");
|
sb.AppendLine("Available Commands: ");
|
||||||
foreach (var command in CommandHandlerFactory.Commands)
|
foreach (var command in CommandHandlerFactory.Commands.Where(x => x.Usage.HasFlag(CommandUsage.Console)))
|
||||||
|
{
|
||||||
|
if (!commandAttributes.TryGetValue(command.GetType(), out var attr))
|
||||||
|
{
|
||||||
|
attr = command.GetType().GetCustomAttribute(typeof(CommandHandler)) as CommandHandler;
|
||||||
|
commandAttributes[command.GetType()] = attr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (attr != null)
|
||||||
|
sb.AppendLine($" {attr.Name} - {attr.Description} (Example: {attr.Example}), Usage: {command.Usage}");
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.Write(sb.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Execute(Dictionary<string, string> args, Connection connection)
|
||||||
|
{
|
||||||
|
base.Execute(args);
|
||||||
|
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
sb.AppendLine("Available Commands: ");
|
||||||
|
foreach (var command in CommandHandlerFactory.Commands.Where(x => x.Usage.HasFlag(CommandUsage.User)))
|
||||||
{
|
{
|
||||||
if (!commandAttributes.TryGetValue(command.GetType(), out var attr))
|
if (!commandAttributes.TryGetValue(command.GetType(), out var attr))
|
||||||
{
|
{
|
||||||
@@ -27,6 +50,6 @@ public class HelpCommand : Command
|
|||||||
sb.AppendLine($" {attr.Name} - {attr.Description} (Example: {attr.Example})");
|
sb.AppendLine($" {attr.Name} - {attr.Description} (Example: {attr.Example})");
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.Write(sb.ToString());
|
connection.SendSystemMsg(sb.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ using ProtoBuf;
|
|||||||
using System.Buffers.Binary;
|
using System.Buffers.Binary;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Reflection;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace BLHX.Server.Game
|
namespace BLHX.Server.Game
|
||||||
@@ -148,7 +147,7 @@ namespace BLHX.Server.Game
|
|||||||
DBManager.PlayerContext.Save();
|
DBManager.PlayerContext.Save();
|
||||||
this.NotifyResourceList();
|
this.NotifyResourceList();
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
c.Log($"Tick");
|
c.Log("Ticked!");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,6 +166,8 @@ namespace BLHX.Server.Game
|
|||||||
this.NotifyShopMonthData();
|
this.NotifyShopMonthData();
|
||||||
this.NotifyChapterData();
|
this.NotifyChapterData();
|
||||||
this.NotifyBagData();
|
this.NotifyBagData();
|
||||||
|
this.NotifyBuildShipData();
|
||||||
|
this.NotifyActivityData();
|
||||||
this.NotifyDormData();
|
this.NotifyDormData();
|
||||||
this.NotifyNavalAcademy();
|
this.NotifyNavalAcademy();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -113,5 +113,10 @@ namespace BLHX.Server.Game.Handlers
|
|||||||
{
|
{
|
||||||
connection.Send(new Sc11752());
|
connection.Send(new Sc11752());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void NotifyActivityData(this Connection connection)
|
||||||
|
{
|
||||||
|
connection.Send(new Sc11200());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,5 +48,10 @@ namespace BLHX.Server.Game.Handlers
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void NotifyBuildShipData(this Connection connection)
|
||||||
|
{
|
||||||
|
connection.Send(new Sc12024());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,12 @@ namespace BLHX.Server.Game.Handlers
|
|||||||
{
|
{
|
||||||
var req = packet.Decode<Cs50102>();
|
var req = packet.Decode<Cs50102>();
|
||||||
|
|
||||||
|
if (req.Content.StartsWith("/"))
|
||||||
|
{
|
||||||
|
Commands.CommandHandlerFactory.HandleCommand(req.Content.Substring(1), connection);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
GameServer.ChatManager.SendChat(new()
|
GameServer.ChatManager.SendChat(new()
|
||||||
{
|
{
|
||||||
Content = req.Content,
|
Content = req.Content,
|
||||||
@@ -35,4 +41,27 @@ namespace BLHX.Server.Game.Handlers
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class P50ConnectionNotifyExtensions
|
||||||
|
{
|
||||||
|
public static void SendSystemMsg(this Connection connection, string msg)
|
||||||
|
{
|
||||||
|
|
||||||
|
var ntf = new Sc50101()
|
||||||
|
{
|
||||||
|
Content = msg,
|
||||||
|
Player = new()
|
||||||
|
{
|
||||||
|
Name = "System",
|
||||||
|
Display = new()
|
||||||
|
{
|
||||||
|
Icon = 107061
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Type = 1
|
||||||
|
};
|
||||||
|
|
||||||
|
connection.Send(ntf);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ namespace BLHX.Server.Game.Managers
|
|||||||
{
|
{
|
||||||
List<MsgInfo> messages = [];
|
List<MsgInfo> messages = [];
|
||||||
|
|
||||||
public void SendChat(MsgInfo msgInfo)
|
public void SendChat(MsgInfo msgInfo, bool broadcast = true)
|
||||||
{
|
{
|
||||||
msgInfo.Timestamp = (uint)DateTimeOffset.Now.ToUnixTimeSeconds();
|
msgInfo.Timestamp = (uint)DateTimeOffset.Now.ToUnixTimeSeconds();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user