Skip to content

Commit

Permalink
feat 新增 ENABLEAUTOSTEAMSALEEVENT 命令
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Jun 28, 2024
1 parent 57934f5 commit da2d00c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
10 changes: 9 additions & 1 deletion ASFEnhance/ASFEnhance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ public Task OnLoaded()
"EX" when access >= EAccess.Master =>
Task.FromResult(Explorer.Command.ResponseExploreDiscoveryQueue(bot)),

"ENABLEAUTOSTEAMSALEEVENT" or
"EASSE" when access >= EAccess.Master =>
Explorer.Command.ResponseEnableAutoSteamSaleEvent(bot),

//Friend
"DELETEALLFRIEND" when access >= EAccess.Master =>
Friend.Command.ResponseDeleteAllFriend(bot),
Expand Down Expand Up @@ -750,6 +754,10 @@ public Task OnLoaded()
"EX" when access >= EAccess.Master =>
Explorer.Command.ResponseExploreDiscoveryQueue(Utilities.GetArgsAsText(args, 1, ",")),

"ENABLEAUTOSTEAMSALEEVENT" or
"EASSE" when access >= EAccess.Master =>
Explorer.Command.ResponseEnableAutoSteamSaleEvent(Utilities.GetArgsAsText(args, 1, ",")),

//Friend
"ADDBOTFRIEND" or
"ABF" when argLength > 2 && access >= EAccess.Master =>
Expand Down Expand Up @@ -1224,7 +1232,7 @@ public Task<bool> OnBotFriendRequest(Bot bot, ulong steamId)
/// <exception cref="NotImplementedException"></exception>
public async Task<Uri?> GetTargetReleaseURL(Version asfVersion, string asfVariant, bool asfUpdate, EUpdateChannel updateChannel, bool forced)
{
var releaseResponse = await GitHubService.GetLatestRelease("chr233/ASFEnhance", true, default).ConfigureAwait(false);
var releaseResponse = await GitHubService.GetLatestRelease("chr233/ASFEnhance", updateChannel == EUpdateChannel.Stable, default).ConfigureAwait(false);
if (releaseResponse == null)
{
return null;
Expand Down
86 changes: 86 additions & 0 deletions ASFEnhance/Explorer/Command.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Helpers.Json;
using ArchiSteamFarm.Localization;
using ArchiSteamFarm.Steam;
using ArchiSteamFarm.Steam.Storage;
using System.Text.Json;

namespace ASFEnhance.Explorer;

Expand Down Expand Up @@ -101,4 +104,87 @@ internal static class Command

return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}


/// <summary>
/// 开启AutoSteamSaleEvent
/// </summary>
/// <param name="bot"></param>
/// <returns></returns>
internal static async Task<string?> ResponseEnableAutoSteamSaleEvent(Bot bot)
{
if (bot.BotConfig.FarmingPreferences.HasFlag(BotConfig.EFarmingPreferences.AutoSteamSaleEvent))
{
return bot.FormatBotResponse("已经开启 SteamSaleEvent");
}

var filePath = Bot.GetFilePath(bot.BotName, Bot.EFileType.Config);
if (string.IsNullOrEmpty(filePath))
{
return bot.FormatBotResponse("找不到配置文件路径");
}

try
{
var currentJson = await File.ReadAllTextAsync(filePath).ConfigureAwait(false);
if (string.IsNullOrEmpty(currentJson))
{
return bot.FormatBotResponse("读取配置文件失败");
}

var newValue = bot.BotConfig.FarmingPreferences | BotConfig.EFarmingPreferences.AutoSteamSaleEvent;

var jsonObject = JsonSerializer.Deserialize<Dictionary<string, object>>(currentJson, JsonUtilities.DefaultJsonSerialierOptions);
if (jsonObject != null)
{
if (jsonObject.ContainsKey("FarmingPreferences"))
{
jsonObject["FarmingPreferences"] = newValue;
}
else
{
jsonObject.Add("FarmingPreferences", newValue);
}

currentJson = JsonSerializer.Serialize(jsonObject, JsonUtilities.IndentedJsonSerialierOptions);
}

await File.WriteAllTextAsync(filePath, currentJson).ConfigureAwait(false);
return bot.FormatBotResponse("更新配置文件成功");
}
catch (Exception ex)
{
ASFLogger.LogGenericException(ex);
return bot.FormatBotResponse("读取/修改配置文件出错");
}
}

/// <summary>
/// 开启AutoSteamSaleEvent (多个Bot)
/// </summary>
/// <param name="botNames"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseEnableAutoSteamSaleEvent(string botNames)
{
if (string.IsNullOrEmpty(botNames))
{
throw new ArgumentNullException(nameof(botNames));
}

var bots = Bot.GetBots(botNames);

if ((bots == null) || (bots.Count == 0))
{
return FormatStaticResponse(Strings.BotNotFound, botNames);
}

var semaphore = new SemaphoreSlim(1);

var results = await Utilities.InParallel(bots.Select(bot => ResponseEnableAutoSteamSaleEvent(bot))).ConfigureAwait(false);

var responses = new List<string?>(results.Where(result => !string.IsNullOrEmpty(result)));

return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}
}
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>2.1.9.0</Version>
<Version>2.1.9.1</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down

0 comments on commit da2d00c

Please sign in to comment.