Skip to content

Commit

Permalink
更新了 EVENTENDLESS 算法,有望
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Jun 15, 2022
1 parent 1277746 commit 39cd2a8
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 45 deletions.
14 changes: 10 additions & 4 deletions ASFEnhance/ASFEnhance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,13 @@ public Task OnLoaded()
//Event
case "EVENT" when access >= EAccess.Operator:
case "E" when access >= EAccess.Operator:
return await Event.Command.ResponseEvent(bot, false).ConfigureAwait(false);
return await Event.Command.ResponseEvent(bot).ConfigureAwait(false);
case "EVENTENDLESS" when access >= EAccess.Operator:
case "EE" when access >= EAccess.Operator:
return await Event.Command.ResponseEvent(bot, true).ConfigureAwait(false);
return await Event.Command.ResponseEventEndless(bot).ConfigureAwait(false);
case "EVENTSTATUS" when access >= EAccess.Operator:
case "ES" when access >= EAccess.Operator:
return await Event.Command.ResponseEventStatus(bot).ConfigureAwait(false);

//Shortcut
case "P":
Expand Down Expand Up @@ -233,10 +236,13 @@ public Task OnLoaded()
//Event
case "EVENT" when access >= EAccess.Operator:
case "E" when access >= EAccess.Operator:
return await Event.Command.ResponseEvent(Utilities.GetArgsAsText(message, 1), false).ConfigureAwait(false);
return await Event.Command.ResponseEvent(Utilities.GetArgsAsText(message, 1)).ConfigureAwait(false);
case "EVENTENDLESS" when access >= EAccess.Operator:
case "EE" when access >= EAccess.Operator:
return await Event.Command.ResponseEvent(Utilities.GetArgsAsText(message, 1), true).ConfigureAwait(false);
return await Event.Command.ResponseEventEndless(Utilities.GetArgsAsText(message, 1)).ConfigureAwait(false);
case "EVENTSTATUS" when access >= EAccess.Operator:
case "ES" when access >= EAccess.Operator:
return await Event.Command.ResponseEventStatus(Utilities.GetArgsAsText(message, 1)).ConfigureAwait(false);

//Shortcut
case "AL":
Expand Down
4 changes: 2 additions & 2 deletions ASFEnhance/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Reflection;

[assembly: System.CLSCompliant(false)]
[assembly: AssemblyVersion("1.6.5.519")]
[assembly: AssemblyFileVersion("1.6.5.519")]
[assembly: AssemblyVersion("1.6.5.536")]
[assembly: AssemblyFileVersion("1.6.5.536")]

[assembly: AssemblyCopyright("Copyright © 2022 Chr_")]
[assembly: AssemblyProduct("ASFEnhance")]
Expand Down
223 changes: 185 additions & 38 deletions ASFEnhance/Event/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,236 @@
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Localization;
using ArchiSteamFarm.Steam;
using ASFEnhance.Localization;
using System.Globalization;
using System.Text;
using System.Collections.Concurrent;
using static ASFEnhance.Utils;
using ASFEnhance.Localization;

namespace ASFEnhance.Event
{
internal static class Command
{
internal static async Task<string?> ResponseEvent(Bot bot, bool endless)
internal static async Task<string?> ResponseEvent(Bot bot)
{
if (!bot.IsConnectedAndLoggedOn)
{
return bot.FormatBotResponse(Strings.BotNotConnected);
}

HashSet<DemoAppResponse>? demoAppIDs = await Event.WebRequest.FetchDemoAppIDs(bot).ConfigureAwait(false);
bool Paused = bot.CardsFarmer.Paused;

if (demoAppIDs == null)
if (!Paused)
{
return bot.FormatBotResponse(Langs.NetworkError);
await bot.Commands.Response(EAccess.Master, $"PAUSE {bot.BotName}").ConfigureAwait(false);
}

bool Paused = bot.CardsFarmer.Paused;
List<uint> demos = new() { 1431000, 1805630, 1937760, 1923740, 1993130, 1490520, 2015730, 1991740, 2012980, 1402220 };
string demoStr = string.Join(',', demos.Select(x => $"app/{x}"));
string demoStr2 = string.Join(',', demos);

await bot.Commands.Response(EAccess.Master, $"ADDLICENSE {bot.BotName} {demoStr}").ConfigureAwait(false);
await bot.Commands.Response(EAccess.Master, $"PLAY {bot.BotName} {demoStr2}").ConfigureAwait(false);

if (!Paused)
{
await bot.Commands.Response(EAccess.Master, "pause").ConfigureAwait(false);
await bot.Commands.Response(EAccess.Master, $"RESUME {bot.BotName}").ConfigureAwait(false);
}

return bot.FormatBotResponse("Done!");
}

internal static async Task<string?> ResponseEvent(string botNames)
{
if (string.IsNullOrEmpty(botNames))
{
throw new ArgumentNullException(nameof(botNames));
}

HashSet<Bot>? bots = Bot.GetBots(botNames);

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

IList<string?> results = await Utilities.InParallel(bots.Select(bot => ResponseEvent(bot))).ConfigureAwait(false);

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

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

private static ConcurrentDictionary<string, HashSet<uint>> FailedDemos = new();
private static ConcurrentDictionary<string, HashSet<uint>> AddedDemos = new();
private static ConcurrentDictionary<string, bool> Status = new();

internal static Task<string?> ResponseEventEndless(Bot bot)
{
if (!bot.IsConnectedAndLoggedOn)
{
return Task.FromResult(bot.FormatBotResponse(Strings.BotNotConnected));
}

_ = Task.Run(async () => {
int i = 0;
foreach (var demo in demoAppIDs)
string ownStr = bot.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.BotOwnedAlready, "")[..^1]);

bool Paused = bot.CardsFarmer.Paused;

string botName = bot.BotName;

try
{
uint appID = demo.DemoAppID;
List<uint> demos = new();
List<uint> tmp = new();

if (appID != 0)
{
ASFLogger.LogGenericInfo($"{bot.BotName} 入库 Demo: {appID}");
await bot.Commands.Response(EAccess.Master, $"addlicense {bot.BotName} a/{appID}").ConfigureAwait(false);
await Task.Delay(TimeSpan.FromSeconds(10)).ConfigureAwait(false);
HashSet<uint> failedDemos = new();
HashSet<uint> addedDemos = new();

ASFLogger.LogGenericInfo($"{bot.BotName} 游玩 Demo: {appID}");
await bot.Commands.Response(EAccess.Master, $"play {bot.BotName} {appID}").ConfigureAwait(false);
await Task.Delay(TimeSpan.FromSeconds(10)).ConfigureAwait(false);
FailedDemos[botName] = failedDemos;
AddedDemos[botName] = addedDemos;
Status[botName] = true;

if (endless)
int index = 0;
while (index < DemosDB.Demos.Count)
{
demos.Clear();
tmp.Clear();

int error = 5;
while (!bot.IsConnectedAndLoggedOn)
{
if (i++ >= 40)
await Task.Delay(10000).ConfigureAwait(false);
if (error-- == 0)
{
i = 0;
await bot.Commands.Response(EAccess.Master, $"resume {bot.BotName}").ConfigureAwait(false);
await Task.Delay(TimeSpan.FromHours(1)).ConfigureAwait(false);
if (!Paused)
{
await bot.Commands.Response(EAccess.Master, "pause").ConfigureAwait(false);
}
return;
}
}
else

while (demos.Count < 45 && index < DemosDB.Demos.Count)
{
if (i++ >= 10)
if (!Paused)
{
await bot.Commands.Response(EAccess.Master, $"PAUSE {botName}").ConfigureAwait(false);
}

//检查是否已拥有
uint appid = DemosDB.Demos[index++];
string result = await bot.Commands.Response(EAccess.Owner, $"OWNS {botName} {appid}").ConfigureAwait(false) ?? "";
bool owned = result.StartsWith(ownStr);

if (!owned)
{
await bot.Commands.Response(EAccess.Owner, $"ADDLICENSE {botName} app/{appid}").ConfigureAwait(false);
await Task.Delay(1000).ConfigureAwait(false);
result = await bot.Commands.Response(EAccess.Owner, $"OWNS {botName} {appid}").ConfigureAwait(false) ?? "";
owned = result.StartsWith(ownStr);
}

if (!owned)
{
break;
failedDemos.Add(appid);
}

tmp.Add(appid);

//游玩Demo
if (tmp.Count >= 20)
{
string arg = string.Join(',', tmp);
await bot.Commands.Response(EAccess.Owner, $"PLAY {botName} {arg}").ConfigureAwait(false);
await Task.Delay(5000).ConfigureAwait(false);

foreach (var i in tmp)
{
addedDemos.Add(i);
}

if (!Paused)
{
await bot.Commands.Response(EAccess.Master, $"RESUME {botName}").ConfigureAwait(false);
}

tmp.Clear();
}
}

await Task.Delay(TimeSpan.FromMinutes(65)).ConfigureAwait(false);

}

if (!bot.IsConnectedAndLoggedOn)
if (tmp.Count > 0)
{
break;
string arg = string.Join(',', tmp);
await bot.Commands.Response(EAccess.Owner, $"PLAY {botName} {arg}").ConfigureAwait(false);
await Task.Delay(5000).ConfigureAwait(false);

foreach (var i in tmp)
{
addedDemos.Add(i);
}

if (!Paused)
{
await bot.Commands.Response(EAccess.Master, $"RESUME {botName}").ConfigureAwait(false);
}
tmp.Clear();
}
}

if (!Paused)
finally
{
await bot.Commands.Response(EAccess.Master, "resume").ConfigureAwait(false);
Status[botName] = false;
}
});

return bot.FormatBotResponse("任务将在后台运行");
return Task.FromResult(bot.FormatBotResponse("Task will running in background"));
}

internal static async Task<string?> ResponseEventEndless(string botNames)
{
if (string.IsNullOrEmpty(botNames))
{
throw new ArgumentNullException(nameof(botNames));
}

HashSet<Bot>? bots = Bot.GetBots(botNames);

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

IList<string?> results = await Utilities.InParallel(bots.Select(bot => ResponseEventEndless(bot))).ConfigureAwait(false);

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

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

internal static Task<string?> ResponseEventStatus(Bot bot)
{
string botName = bot.BotName;

if (!FailedDemos.ContainsKey(botName))
{
return Task.FromResult(bot.FormatBotResponse("No record"));
}

var failedDemos = FailedDemos[botName];
var addedDemos = AddedDemos[botName];
var status = Status[botName];

StringBuilder sb = new();
sb.AppendLine(bot.FormatBotResponse(Langs.MultipleLineResult));
sb.AppendLine(string.Format("Task Status: {0}", status ? "Running" : "Stopped"));
sb.AppendLine(string.Format("Added Demos Count: {0}", addedDemos.Count));
sb.AppendLine(string.Format("Failed Demos Count: {0}", failedDemos.Count));
sb.AppendLine(string.Format("Total Demos Count: {0}", DemosDB.Demos.Count));

return Task.FromResult(sb.ToString());
}

internal static async Task<string?> ResponseEvent(string botNames, bool endless)
internal static async Task<string?> ResponseEventStatus(string botNames)
{
if (string.IsNullOrEmpty(botNames))
{
Expand All @@ -98,7 +245,7 @@ internal static class Command
return FormatStaticResponse(string.Format(Strings.BotNotFound, botNames));
}

IList<string?> results = await Utilities.InParallel(bots.Select(bot => ResponseEvent(bot, endless))).ConfigureAwait(false);
IList<string?> results = await Utilities.InParallel(bots.Select(bot => ResponseEventStatus(bot))).ConfigureAwait(false);

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

Expand Down
2 changes: 1 addition & 1 deletion ASFEnhance/Event/DemosDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ internal static class DemosDB
/// <summary>
/// Pulled from SteamDB.info, total 2000 demos
/// </summary>
internal static HashSet<uint> Demos { get; } = new() {
internal static List<uint> Demos { get; } = new() {
2027340,2053780,1949530,2020580,1924750,1973480,1954220,2051550,1724820,1489640,1046810,2057050,1689380,1661050,1761410,374220,1452730,1976560,
1536130,2014290,2053480,2009050,2057560,2053180,1373970,2018090,1641480,2025010,1971060,2009700,1960130,1990950,1591770,2051610,1652590,1936870,
2027770,1899940,2013310,1952070,1838520,1740280,1998210,2056140,2023260,2020290,2015290,2013940,2012920,2011670,2010860,2008660,2007310,2000700,
Expand Down

0 comments on commit 39cd2a8

Please sign in to comment.