Skip to content

Commit

Permalink
Merge pull request #30 from 1249993110/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
1249993110 authored Nov 14, 2024
2 parents 8e31c4a + c42bc1f commit ba2c3b6
Show file tree
Hide file tree
Showing 26 changed files with 307 additions and 106 deletions.
85 changes: 85 additions & 0 deletions src/SdtdServerKit/Commands/SetPlayerColoredChat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using SdtdServerKit.Data.Entities;
using SdtdServerKit.Data.IRepositories;

namespace SdtdServerKit.Commands
{
/// <summary>
/// Represents a command to set player colored chat.
/// </summary>
public class SetPlayerColoredChat : ConsoleCmdBase
{
/// <inheritdoc/>
public override string getDescription()
{
return "Set player colored chat.";
}

/// <inheritdoc/>
public override string getHelp()
{
return "Usage:\n" +
" 1. ty-SetPlayerColoredChat {PlayerId/EntityId/PlayerName} {NameColor} {TextColor} {CustomName} {Description}\n" +
" 2. ty-SetPlayerColoredChat {PlayerId/EntityId/PlayerName} {NameColor} {TextColor} {CustomName}\n" +
" 3. ty-SetPlayerColoredChat {PlayerId/EntityId/PlayerName} {NameColor} {TextColor}\n" +
" 4. ty-SetPlayerColoredChat {PlayerId/EntityId/PlayerName} {NameColor}\n" +
"1. Set player colored chat with name color, text color, custom name and description.\n" +
"2. Set player colored chat with name color, text color and custom name.\n" +
"3. Set player colored chat with name color and text color.\n" +
"4. Set player colored chat with name color.";
}

/// <inheritdoc/>
public override string[] getCommands()
{
return new string[]
{
"ty-SetPlayerColoredChat",
"ty-spcc"
};
}

/// <inheritdoc/>
public override async void Execute(List<string> args, CommandSenderInfo _senderInfo)
{
if (args.Count < 2)
{
Log("Wrong number of arguments.");
Log(getHelp());
return;
}

try
{
var cInfo = ConsoleHelper.ParseParamIdOrName(args[0]);
string? playerId = cInfo?.CrossplatformId.CombinedString ?? PlatformUserIdentifierAbs.FromCombinedString(args[0])?.CombinedString;

if (playerId == null)
{
Log("Unable to locate player '{0}' online or offline", args[0]);
return;
}

var entity = new T_ColoredChat()
{
Id = playerId,
NameColor = args[1],
TextColor = args[2],
CustomName = args.Count > 3 ? args[3] : null,
Description = args.Count > 4 ? args[4] : null,
CreatedAt = DateTime.Now,
};

var repository = ModApi.ServiceContainer.Resolve<IColoredChatRepository>();
bool result = repository.InsertOrReplace(entity) == 1;
if (result == false)
{
Log("Failed to set player '{0}' colored chat.", playerId);
}
}
catch (Exception ex)
{
Log("Error in SetPlayerColoredChat.Execute:" + ex.Message);
}
}
}
}
75 changes: 75 additions & 0 deletions src/SdtdServerKit/Commands/SetPlayerCustomVar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
namespace SdtdServerKit.Commands
{
/// <summary>
/// Set player custom var.
/// </summary>
public class SetPlayerCustomVar : ConsoleCmdBase
{
/// <inheritdoc/>
public override string getDescription()
{
return "Set player custom var.";
}

/// <inheritdoc/>
public override string getHelp()
{
return "Usage:\n" +
" 1. ty-setcvar {PlayerId/EntityId/PlayerName} {cvarName} {cvarValue}" +
" 2. ty-setcvar {cvarName} {cvarValue}" +
"1. Set player custom var." +
"2. Set yourself custom var.";
}

/// <inheritdoc/>
public override string[] getCommands()
{
return new string[]
{
"ty-SetPlayerCustomVar",
"ty-spcv",
"ty-setcvar"
};
}

/// <inheritdoc/>
public override void Execute(List<string> args, CommandSenderInfo _senderInfo)
{
ClientInfo cInfo;
string name, value;
if (args.Count == 2)
{
cInfo = _senderInfo.RemoteClientInfo;
name = args[0];
value = args[1];
}
else if (args.Count == 3)
{
cInfo = ConsoleHelper.ParseParamIdOrName(args[0]);
if (cInfo == null)
{
Log("Unable to locate player '{0}' online", args[0]);
return;
}
name = args[1];
value = args[2];
}
else
{
Log("Wrong number of arguments.");
Log(getHelp());
return;
}

int entityId = cInfo!.entityId;
if (GameManager.Instance.World.Players.dict.TryGetValue(entityId, out var player))
{
player.Buffs.SetCustomVar(name, float.Parse(value));
}
else
{
Log("Unable to locate player entity id '{0}' online", entityId);
}
}
}
}
25 changes: 25 additions & 0 deletions src/SdtdServerKit/Data/Dtos/ChatRecordQueryDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using IceCoffee.SimpleCRUD.Dtos;

namespace SdtdServerKit.Data.Dtos
{
/// <summary>
///
/// </summary>
public class ChatRecordQueryDto : PaginationQueryDto<ChatRecordQueryOrder>
{
/// <summary>
/// Start Date Time
/// </summary>
public DateTime? StartDateTime { get; set; }

/// <summary>
/// End Date Time
/// </summary>
public DateTime? EndDateTime { get; set; }

/// <summary>
/// Chat Type
/// </summary>
public ChatType? ChatType { get; set; }
}
}
17 changes: 0 additions & 17 deletions src/SdtdServerKit/Data/Dtos/DateTimeQueryDto.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace SdtdServerKit.Data.IRepositories
{
public interface IChatRecordRepository : IRepository<T_ChatRecord>
{
Task<PagedDto<T_ChatRecord>> GetPagedListAsync(DateTimeQueryDto dto);
Task<PagedDto<T_ChatRecord>> GetPagedListAsync(ChatRecordQueryDto dto);
}
}
13 changes: 9 additions & 4 deletions src/SdtdServerKit/Data/Repositories/ChatRecordRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ namespace SdtdServerKit.Data.Repositories
{
public class ChatRecordRepository : DefaultRepository<T_ChatRecord>, IChatRecordRepository
{
public Task<PagedDto<T_ChatRecord>> GetPagedListAsync(DateTimeQueryDto dto)
public Task<PagedDto<T_ChatRecord>> GetPagedListAsync(ChatRecordQueryDto dto)
{
var whereClauseSB = new StringBuilder("1=1");

if (dto.StartDateTime.HasValue)
{
whereClauseSB.Append($" AND CreatedAt>={dto.StartDateTime.Value:s}");
whereClauseSB.Append($" AND CreatedAt>=@StartDateTime");
}

if (dto.EndDateTime.HasValue)
{
whereClauseSB.Append($" AND CreatedAt<={dto.EndDateTime.Value:s}");
whereClauseSB.Append($" AND CreatedAt<=@EndDateTime");
}

if(dto.ChatType.HasValue)
{
whereClauseSB.Append(" AND ChatType=@ChatType");
}

if (string.IsNullOrEmpty(dto.Keyword) == false)
Expand All @@ -29,7 +34,7 @@ public Task<PagedDto<T_ChatRecord>> GetPagedListAsync(DateTimeQueryDto dto)

string orderByClause = dto.Order + (dto.Desc ? " DESC" : " ASC");

var param = new { Keyword = dto.Keyword };
var param = new { Keyword = dto.Keyword, StartDateTime = dto.StartDateTime, EndDateTime = dto.EndDateTime, ChatType = dto.ChatType };
return base.GetPagedListAsync(dto.PageNumber, dto.PageSize, whereClauseSB.ToString(), orderByClause, param);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/SdtdServerKit/Functions/AutoBackup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace SdtdServerKit.Functions
{
/// <summary>
/// 自动备份
/// AutoBackup
/// </summary>
public class AutoBackup : FunctionBase<AutoBackupSettings>
{
Expand Down
3 changes: 2 additions & 1 deletion src/SdtdServerKit/Functions/ColoredChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ private bool OnChatMessage(ClientInfo? clientInfo, EChatType eChatType, int send
{
if(string.IsNullOrEmpty(coloredChat.CustomName) == false)
{
playerName = StringTemplate.Render(coloredChat.CustomName!, new { PlayerName = playerName });
//playerName = StringTemplate.Render(coloredChat.CustomName!, new { PlayerName = playerName });
playerName = coloredChat.CustomName!.Replace("{PlayerName}", playerName);
}

message = $"[{coloredChat.NameColor}]{playerName}[{GetDefaultColor(eChatType)}]: [{coloredChat.TextColor}]{message}";
Expand Down
23 changes: 22 additions & 1 deletion src/SdtdServerKit/Functions/GameNotice.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using SdtdServerKit.FunctionSettings;
using SdtdServerKit.Managers;
using SdtdServerKit.Triggers;
using SdtdServerKit.Variables;

namespace SdtdServerKit.Functions
Expand Down Expand Up @@ -78,7 +79,7 @@ private void SendRotatingNotice()
string message = rotatingNotices[_lastRotatingIndex];
if(string.IsNullOrEmpty(message) == false)
{
SendGlobalMessage(message);
SendGlobalMessage(FormatCmd(message));
}

_lastRotatingIndex++;
Expand All @@ -90,6 +91,26 @@ private void SendRotatingNotice()
}
}

private string FormatCmd(string message, SpawnedPlayer? spawnedPlayer = null)
{
var changed = SkyChangeTrigger.LatestSkyState;
var gameNoticeVariables = new GameNoticeVariables()
{
BloodMoonDays = changed.BloodMoonDaysRemaining,
BloodMoonStartTime = changed.DuskHour + ":00",
BloodMoonEndTime = changed.DawnHour + ":00"
};

message = StringTemplate.Render(message, gameNoticeVariables);

if (spawnedPlayer == null)
{
return message;
}

return base.FormatCmd(message, spawnedPlayer);
}

private void OnSkyChanged(SkyChanged changed)
{
try
Expand Down
9 changes: 7 additions & 2 deletions src/SdtdServerKit/Functions/GameStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace SdtdServerKit.Functions
{
/// <summary>
/// 游戏商店
/// GameStore
/// </summary>
public class GameStore : FunctionBase<GameStoreSettings>
{
Expand Down Expand Up @@ -70,12 +70,17 @@ protected override async Task<bool> OnChatCmd(string message, ManagedPlayer mana
foreach (var item in itemList)
{
Utilities.Utils.GiveItem(playerId, item.ItemName, item.Count, item.Quality, item.Durability);
await Task.Delay(100);
}

var commandList = await _commandListRepository.GetListByGoodsIdAsync(goods.Id);
foreach (var item in commandList)
{
Utilities.Utils.ExecuteConsoleCommand(FormatCmd(item.Command, managedPlayer, goods), item.InMainThread);
foreach (var cmd in item.Command.Split('\n'))
{
Utilities.Utils.ExecuteConsoleCommand(FormatCmd(cmd, managedPlayer, goods), item.InMainThread);
}
await Task.Delay(100);
}

SendMessageToPlayer(playerId, FormatCmd(Settings.BuySuccessTip, managedPlayer, goods));
Expand Down
2 changes: 1 addition & 1 deletion src/SdtdServerKit/Functions/TeleportCity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected override async Task<bool> OnChatCmd(string message, ManagedPlayer mana

await _pointsInfoRepository.ChangePointsAsync(playerId, -cityPosition.PointsRequired);
Utilities.Utils.TeleportPlayer(managedPlayer.EntityId.ToString(), cityPosition.Position, cityPosition.ViewDirection);
SendGlobalMessage(FormatCmd(Settings.TeleSuccessTip, managedPlayer, cityPosition));
SendMessageToPlayer(managedPlayer.PlayerId, FormatCmd(Settings.TeleSuccessTip, managedPlayer, cityPosition));

await _teleRecordRepository.InsertAsync(new T_TeleRecord()
{
Expand Down
2 changes: 1 addition & 1 deletion src/SdtdServerKit/Functions/TeleportHome.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected override async Task<bool> OnChatCmd(string message, ManagedPlayer mana
await _pointsRepository.ChangePointsAsync(playerId, -Settings.PointsRequiredForTele);
Utilities.Utils.TeleportPlayer(managedPlayer.EntityId.ToString(), entity.Position);

SendGlobalMessage(FormatCmd(Settings.TeleSuccessTip, managedPlayer, entity));
SendMessageToPlayer(managedPlayer.PlayerId, FormatCmd(Settings.TeleSuccessTip, managedPlayer, entity));
await _teleRecordRepository.InsertAsync(new T_TeleRecord()
{
CreatedAt = DateTime.Now,
Expand Down
7 changes: 6 additions & 1 deletion src/SdtdServerKit/Functions/VipGift.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ protected override async Task<bool> OnChatCmd(string message, ManagedPlayer mana
foreach (var item in itemList)
{
Utilities.Utils.GiveItem(playerId, item.ItemName, item.Count, item.Quality, item.Durability);
await Task.Delay(100);
}

var commandList = await _commandListRepository.GetListByVipGiftIdAsync(vipGift.Id);
foreach (var item in commandList)
{
Utilities.Utils.ExecuteConsoleCommand(FormatCmd(item.Command, managedPlayer, vipGift), item.InMainThread);
foreach (var cmd in item.Command.Split('\n'))
{
Utilities.Utils.ExecuteConsoleCommand(FormatCmd(cmd, managedPlayer, vipGift), item.InMainThread);
}
await Task.Delay(100);
}

vipGift.ClaimState = true;
Expand Down
3 changes: 1 addition & 2 deletions src/SdtdServerKit/ModApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,6 @@ private static void RegisterModEventHandlers()
ModEvents.ChatMessage.RegisterHandler(ModEventHub.OnChatMessage);
ModEvents.PlayerSpawning.RegisterHandler(ModEventHub.OnPlayerSpawning);

GlobalTimer.RegisterSubTimer(new SubTimer(SkyChangeTrigger.Callback, 1) { IsEnabled = true });

ModEventHub.GameStartDone += OnGameStartDone;

CustomLogger.Info("Registered mod event handlers success.");
Expand All @@ -322,6 +320,7 @@ private static void RegisterModEventHandlers()
private static void OnGameStartDone()
{
WorldStaticDataHook.ReplaceXmls();
GlobalTimer.RegisterSubTimer(new SubTimer(SkyChangeTrigger.Callback, 1) { IsEnabled = true });
try
{
MapTileCache = (MapTileCache)MapRenderer.GetTileCache();
Expand Down
Loading

0 comments on commit ba2c3b6

Please sign in to comment.