Skip to content

Market rework #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions TbsCore/Core/TaskExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,6 @@ public static void UpdateDorf2Info(Account acc)
var vill = acc.Villages.FirstOrDefault(x => x.Active);
if (vill == null) return;

//remove any further UpdateDorf2 BotTasks for this village (if below 5min)
acc.Tasks.Remove(typeof(UpdateDorf2), vill, 5);

UpdateCurrentlyBuilding(acc, vill);

var buildings = InfrastructureParser.GetBuildings(acc, acc.Wb.Html);
Expand All @@ -197,9 +194,6 @@ public static void UpdateDorf1Info(Account acc)
var vill = acc.Villages.FirstOrDefault(x => x.Active);
if (vill == null) return;

//remove any further UpdateDorf1 BotTasks for this village (if below 5min)
acc.Tasks.Remove(typeof(UpdateDorf1), vill, 5);

UpdateCurrentlyBuilding(acc, vill);

var dorf1Movements = TroopsMovementParser.ParseDorf1Movements(acc.Wb.Html);
Expand Down
145 changes: 97 additions & 48 deletions TbsCore/Helpers/AccountHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using Discord.Webhook;
using System;
using System.Collections.Generic;
using System.Linq;
using TbsCore.Models.AccModels;
using TbsCore.Models.Logging;
using TbsCore.Models.VillageModels;
using TbsCore.Tasks.LowLevel;
using TbsCore.Tasks.SecondLevel;
Expand Down Expand Up @@ -61,76 +64,122 @@ public static void StartAccountTasks(Account acc)
// Get the server info (on first running the account)
if (acc.AccInfo.ServerSpeed == 0 || acc.AccInfo.MapSize == 0)
{
acc.Tasks.Add(new GetServerInfo() { ExecuteAt = DateTime.MinValue.AddHours(2) });
acc.Tasks.Add(new GetServerInfo()
{
ExecuteAt = DateTime.MinValue.AddHours(2)
}, true);
}

if (acc.AccInfo.Tribe == null)
{
acc.Tasks.Add(new GetTribe() { ExecuteAt = DateTime.MinValue.AddHours(3) }, true);
acc.Tasks.Add(new GetTribe()
{
ExecuteAt = DateTime.MinValue.AddHours(3)
}, true);
}

//FL
if (acc.Farming.Enabled) acc.Tasks.Add(new SendFLs() { ExecuteAt = DateTime.Now }, true);

// Bot sleep
acc.Tasks.Add(new Sleep()
{
ExecuteAt = DateTime.Now + TimeHelper.GetWorkTime(acc),
AutoSleep = true
}, true);

// Access change
var nextAccessChange = TimeHelper.GetNextProxyChange(acc);
if (nextAccessChange != TimeSpan.MaxValue)
{
acc.Tasks.Add(new ChangeAccess() { ExecuteAt = DateTime.Now + nextAccessChange }, true);
acc.Tasks.Add(new ChangeAccess()
{
ExecuteAt = DateTime.Now + nextAccessChange
}, true);
}
//research / improve / train troops
foreach (var vill in acc.Villages)

var rand = new Random();
var vills = acc.Villages.OrderBy((item) => rand.Next());
foreach (var vill in vills)
{
//if (vill.Troops.Researched.Count == 0) acc.Tasks.Add( new UpdateTroops() { ExecuteAt = DateTime.Now, vill = vill });
TroopsHelper.ReStartResearchAndImprovement(acc, vill);
TroopsHelper.ReStartTroopTraining(acc, vill);
BuildingHelper.ReStartBuilding(acc, vill);
BuildingHelper.ReStartDemolishing(acc, vill);
MarketHelper.ReStartSendingToMain(acc, vill);
ReStartCelebration(acc, vill);
VillageHelper.SetNextRefresh(acc, vill);
if (vill.FarmingNonGold.OasisFarmingEnabled)
// update info
var min = vill.Settings.RefreshMin * 60;
var max = vill.Settings.RefreshMax * 60;
int timeUpdate;

timeUpdate = rand.Next(min, max);
acc.Tasks.Add(new UpdateVillage()
{
ExecuteAt = DateTime.Now.AddSeconds(timeUpdate),
Vill = vill,
NewVillage = false
}, true, vill);

// this is for task delay, i will add this in next time ~ VINAGHOST
min = 0;
max = 3;
// building

if (vill.Build.Tasks.Count > 0)
{
acc.Tasks.Add(new AttackOasis() { Vill = vill }, true, vill);
timeUpdate = rand.Next(min, max);

acc.Tasks.Add(new UpgradeBuilding()
{
ExecuteAt = DateTime.Now.AddMilliseconds(timeUpdate),
Vill = vill,
}, true, vill);
}

// Remove in later updates!
if (vill.Settings.RefreshMin == 0) vill.Settings.RefreshMin = 30;
if (vill.Settings.RefreshMax == 0) vill.Settings.RefreshMax = 60;
}
// Remove in later updates!
if (acc.Hero.Settings.MinUpdate == 0) acc.Hero.Settings.MinUpdate = 40;
if (acc.Hero.Settings.MaxUpdate == 0) acc.Hero.Settings.MaxUpdate = 80;
// demolish

// Hero update info
if (acc.Hero.Settings.AutoRefreshInfo)
{
Random ran = new Random();
acc.Tasks.Add(new HeroUpdateInfo()
if (vill.Build.DemolishTasks.Count > 0)
{
ExecuteAt = DateTime.Now.AddMinutes(ran.Next(40, 80)),
Priority = Tasks.BotTask.TaskPriority.Low
});
timeUpdate = rand.Next(min, max);
acc.Tasks.Add(new DemolishBuilding()
{
ExecuteAt = DateTime.Now.AddMilliseconds(timeUpdate),
Vill = vill,
}, true, vill);
}

TroopsHelper.ReStartResearchAndImprovement(acc, vill);
TroopsHelper.ReStartTroopTraining(acc, vill);
}
}

public static void ReStartCelebration(Account acc, Village vill)
public static void Loaded(Account acc)
{
// If we don't want auto-celebrations, return
if (vill.Expansion.Celebrations == CelebrationEnum.None) return;
if (acc.Settings.DiscordWebhook && !string.IsNullOrEmpty(acc.AccInfo.WebhookUrl))
{
acc.WebhookClient = new DiscordWebhookClient(acc.AccInfo.WebhookUrl);
}

SerilogSingleton.LogOutput.AddUsername(acc.AccInfo.Nickname);
acc.Logger = new Logger(acc.AccInfo.Nickname);
acc.Tasks = new TaskList(acc);
acc.Tasks.LoadFromFile(IoHelperCore.GetTaskFileUrl(acc.AccInfo.Nickname, acc.AccInfo.ServerUrl));
var sleepTask = acc.Tasks?.FindTask(typeof(Sleep));
if (sleepTask != null)
{
sleepTask.ExecuteAt = DateTime.Now + TimeHelper.GetWorkTime(acc);
}
else
{
acc.Tasks.Add(new Sleep()
{
AutoSleep = true,
ExecuteAt = DateTime.Now + TimeHelper.GetWorkTime(acc),
}, true);
}

acc.Tasks.Add(new Celebration()
var rand = new Random();
var min = 0;
var max = 3;
foreach (var task in acc.Tasks.ToList())
{
ExecuteAt = vill.Expansion.CelebrationEnd.AddSeconds(7),
Vill = vill
}, true, vill);
if (task.GetType() == typeof(Sleep)) continue;
//reset Vill, obj Vill we save is different with current Vill object account hold
task.Vill = acc.Villages.FirstOrDefault(x => x.Id == task.Vill?.Id);
//reset time execute
if (task.ExecuteAt < DateTime.Now)
{
var timeUpdate = rand.Next(min, max);
task.ExecuteAt = DateTime.Now.AddMilliseconds(timeUpdate);
}
}

acc.Villages.ForEach(vill => vill.UnfinishedTasks = new List<VillUnfinishedTask>());
}
}
}
19 changes: 0 additions & 19 deletions TbsCore/Helpers/DefaultConfigurations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,5 @@ public static void OffVillagePlan(Account acc, Village vill)
BuildingHelper.AddBuildingTask(acc, vill, new BuildingTask() { TaskType = BuildingType.General, Building = BuildingEnum.RallyPoint, Level = 15 });
BuildingHelper.AddBuildingTask(acc, vill, new BuildingTask() { TaskType = BuildingType.General, Building = BuildingEnum.TournamentSquare, Level = 1 });
}

public static void SetDefaultTransitConfiguration(Account acc, Village vill)
{
var res = vill.Market.Settings.Configuration;
var transit = new Resources();
var limit = new Resources();
transit.Wood = 90; //%
transit.Clay = 90;
transit.Iron = 90;
transit.Crop = 90;
limit.Wood = 20000;
limit.Clay = 20000;
limit.Iron = 20000;
limit.Crop = 15000;
res.Enabled = true;
res.BalanceType = Tasks.ResourcesConfiguration.BalanceType.RecieveFrom;
res.FillLimit = limit;
res.TargetLimit = transit;
}
}
}
35 changes: 26 additions & 9 deletions TbsCore/Helpers/DriverHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public static string GetBearerToken(Account acc, bool log = true)
internal static async Task WriteCoordinates(Account acc, Coordinates coordinates)
{
await WriteById(acc, "xCoordInput", coordinates.x);
await Task.Delay(AccountHelper.Delay(acc));
await WriteById(acc, "yCoordInput", coordinates.y);
}

Expand Down Expand Up @@ -132,22 +133,38 @@ public static async Task<bool> SelectIndexByName(Account acc, string query, int
private static async Task<bool> ExecuteAction(Account acc, Query query, Action action, bool log = true, bool update = true) =>
await ExecuteScript(acc, $"document.{query.val}{action.val}", log, update);

public class QueryById : Query { public QueryById(string str) => base.val = $"getElementById('{str}')"; }
public class QueryById : Query

public class QueryByName : Query { public QueryByName(string str) => base.val = $"getElementsByName('{str}')[0]"; }
{ public QueryById(string str) => base.val = $"getElementById('{str}')"; }

public class QueryByClassName : Query { public QueryByClassName(string str) => base.val = $"getElementsByClassName('{str}')[0]"; }
public class QueryByName : Query

public class ActionWrite : Action { public ActionWrite(object str) => base.val = $".value='{str}';"; }
{ public QueryByName(string str) => base.val = $"getElementsByName('{str}')[0]"; }

public class ActionClick : Action { public ActionClick() => base.val = ".click();"; }
public class QueryByClassName : Query

public class ActionCheck : Action { public ActionCheck(bool check) => base.val = $".checked={(check ? "true" : "false")};"; }
{ public QueryByClassName(string str) => base.val = $"getElementsByClassName('{str}')[0]"; }

public class ActionSelectIndex : Action { public ActionSelectIndex(int index) => base.val = $".selectedIndex = {index};"; }
public class ActionWrite : Action

public abstract class Action { public string val; }
{ public ActionWrite(object str) => base.val = $".value='{str}';"; }

public abstract class Query { public string val; }
public class ActionClick : Action

{ public ActionClick() => base.val = ".click();"; }

public class ActionCheck : Action

{ public ActionCheck(bool check) => base.val = $".checked={(check ? "true" : "false")};"; }

public class ActionSelectIndex : Action

{ public ActionSelectIndex(int index) => base.val = $".selectedIndex = {index};"; }

public abstract class Action
{ public string val; }

public abstract class Query
{ public string val; }
}
}
Loading