Skip to content

Commit

Permalink
add auto set gacha relic type
Browse files Browse the repository at this point in the history
  • Loading branch information
moonheart committed Jan 2, 2024
1 parent 78303d1 commit d80c252
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 30 deletions.
81 changes: 81 additions & 0 deletions MementoMori.BlazorShared/Components/Settings/GachaSettings.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
@inject ISnackbar Snackbar
@inject IWritableOptions<PlayersOption> PlayersOptions
@using MementoMori.Option
@using MementoMori.Ortega.Common.Utils
@using MementoMori.Ortega.Share
@using MementoMori.Ortega.Share.Data.Item
@using MementoMori.Common.Localization
@using MementoMori.Ortega.Share.Enums
@inherits MementoMori.WebUI.Models.AccountComponent
@inject IWritableOptions<GameConfig> WritableGameConfig

<MudCard>
<MudCardHeader>
<CardHeaderContent>
<MudText>@Masters.TextResourceTable.Get("[CommonFooterGachaButtonLabel]") (Global)</MudText>
</CardHeaderContent>
</MudCardHeader>
<MudCardContent>
<MudField Label="@ResourceStrings.AutoGachaConsumeItems" Variant="Variant.Outlined">
<MudSelect T="UserItem" Label="" MultiSelection="true" Dense="true" Comparer="@(new UserItemComparer())"
@bind-SelectedValues="GachaConfigAutoGachaConsumeUserItems" MultiSelectionTextFunc="list => string.Format(ResourceStrings.SelectedXItems, list.Count)">
@foreach (var item in _gachaConsumeItems)
{
<MudSelectItem T="UserItem" Value="@item">@ItemUtil.GetItemName(item)</MudSelectItem>
}
</MudSelect>
</MudField>
<MudField Label="@Tr("[TradeShopTabName11]")" Variant="Variant.Outlined">
<MudSelect T="GachaRelicType" Label="@Tr("[GachaRelicChangeTitle]")" @bind-Value="_targetRelicType" Dense="true">
<MudSelectItem T="GachaRelicType" Value="GachaRelicType.None">None</MudSelectItem>
<MudSelectItem T="GachaRelicType" Value="GachaRelicType.ChaliceOfHeavenly">@Tr("[ItemName45]")</MudSelectItem>
<MudSelectItem T="GachaRelicType" Value="GachaRelicType.SilverOrderOfTheBlueSky">@Tr("[ItemName46]")</MudSelectItem>
<MudSelectItem T="GachaRelicType" Value="GachaRelicType.DivineWingsOfDesire">@Tr("[ItemName47]")</MudSelectItem>
<MudSelectItem T="GachaRelicType" Value="GachaRelicType.FruitOfTheGarden">@Tr("[ItemName48]")</MudSelectItem>
</MudSelect>
<MudSwitch Label="Auto Buy 3 times" @bind-Checked="_isAutoGacha"></MudSwitch>
</MudField>
</MudCardContent>
</MudCard>

@code {
IEnumerable<UserItem> GachaConfigAutoGachaConsumeUserItems
{
get => WritableGameConfig.Value.GachaConfig.AutoGachaConsumeUserItems;
set => WritableGameConfig.Update(x => x.GachaConfig.AutoGachaConsumeUserItems = value.ToList());
}

List<UserItem> _gachaConsumeItems = new();

GachaRelicType _targetRelicType = GachaRelicType.None;

bool _isAutoGacha;

protected override Task AccountChanged()
{
_gachaConsumeItems = Masters.ItemTable.GetArray()
.Where(d => d.ItemType == ItemType.GachaTicket || d.ItemType == ItemType.FriendPoint || d.ItemType == ItemType.Gold)
.Select(d => new UserItem() {ItemId = d.ItemId, ItemType = d.ItemType}).ToList();

return Task.CompletedTask;
}

private class UserItemComparer : IEqualityComparer<UserItem>
{
public bool Equals(UserItem x, UserItem y)
{
if (ReferenceEquals(x, y)) return true;
if (ReferenceEquals(x, null)) return false;
if (ReferenceEquals(y, null)) return false;
if (x.GetType() != y.GetType()) return false;
return x.ItemId == y.ItemId && x.ItemType == y.ItemType;
}

public int GetHashCode(UserItem obj)
{
return HashCode.Combine(obj.ItemId, (int) obj.ItemType);
}
}


}
30 changes: 1 addition & 29 deletions MementoMori.BlazorShared/Pages/Settings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,7 @@
<AutoJobNextExecution />
</MudItem>
<MudItem sm="12" md="6" lg="4" xl="3">
<MudCard>
<MudCardHeader>
<CardHeaderContent>
<MudText>@Masters.TextResourceTable.Get("[CommonFooterGachaButtonLabel]") (Global)</MudText>
</CardHeaderContent>
</MudCardHeader>
<MudCardContent>
<MudField Label="@ResourceStrings.AutoGachaConsumeItems" Variant="Variant.Outlined">
<MudSelect T="UserItem" Label="" MultiSelection="true" Dense="true" Comparer="@(new UserItemComparer())"
@bind-SelectedValues="GachaConfigAutoGachaConsumeUserItems" MultiSelectionTextFunc="list => string.Format(ResourceStrings.SelectedXItems, list.Count)">
@foreach (var item in _gachaConsumeItems)
{
<MudSelectItem T="UserItem" Value="@item">@ItemUtil.GetItemName(item)</MudSelectItem>
}
</MudSelect>
</MudField>

</MudCardContent>
</MudCard>
<GachaSettings />
</MudItem>
<MudItem sm="12" md="6" lg="4" xl="3">
<MudCard>
Expand Down Expand Up @@ -157,7 +139,6 @@

@code {

List<UserItem> _gachaConsumeItems = new();
List<UserItem> _bountyQuestRewardItems = new();
List<UserItem> _dungeonBattleShopItems = new();
private IEnumerable<UserItem> _selected;
Expand All @@ -174,9 +155,6 @@
{
await base.OnInitializedAsync();

_gachaConsumeItems = Masters.ItemTable.GetArray()
.Where(d => d.ItemType == ItemType.GachaTicket || d.ItemType == ItemType.FriendPoint || d.ItemType == ItemType.Gold)
.Select(d => new UserItem() {ItemId = d.ItemId, ItemType = d.ItemType}).ToList();
_bountyQuestRewardItems.AddRange(new[]
{
new UserItem() {ItemType = ItemType.Gold, ItemId = 1},
Expand Down Expand Up @@ -240,12 +218,6 @@
}


IEnumerable<UserItem> GachaConfigAutoGachaConsumeUserItems
{
get => WritableGameConfig.Value.GachaConfig.AutoGachaConsumeUserItems;
set => WritableGameConfig.Update(x => x.GachaConfig.AutoGachaConsumeUserItems = value.ToList());
}

IEnumerable<UserItem> BountyQuestAutoTargetItems
{
get => WritableGameConfig.Value.BountyQuestAuto.TargetItems;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using MementoMori.Ortega.Share.Enums;
using MessagePack;

namespace MementoMori.Ortega.Share.Data.ApiInterface.Gacha
{
[MessagePackObject(true)]
[OrtegaApi("gacha/changeGachaRelic", true, false)]
public class ChangeGachaRelicRequest : ApiRequestBase
{
public GachaRelicType GachaRelicType { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Runtime.CompilerServices;
using MementoMori.Ortega.Share.Data.Gacha;
using MessagePack;

namespace MementoMori.Ortega.Share.Data.ApiInterface.Gacha
{
[MessagePackObject(true)]
public class ChangeGachaRelicResponse : ApiResponseBase, IUserSyncApiResponse
{
public List<GachaCaseInfo> GachaCaseInfoList { get; set; }

public UserSyncData UserSyncData { get; set; }
}
}
15 changes: 14 additions & 1 deletion MementoMori/AuthOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public class DungeonBattleRelicSortInfo
public class GachaConfigModel
{
public List<UserItem> AutoGachaConsumeUserItems { get; set; } = new();

public GachaRelicType TargetRelicType { get; set; } = GachaRelicType.None;

public bool AutoGachaRelic { get; set; }
}

public class ShopConfig
Expand All @@ -81,6 +85,7 @@ public class AutoJobModel
public bool AutoOpenGuildRaid { get; set; }
public bool AutoLocalRaid { get; set; }
public bool AutoDeployGuildDefense { get; set; }
public bool AutoChangeGachaRelic { get; set; }

public string DailyJobCron { get; set; } = "0 10 4 ? * *";
public string HourlyJobCron { get; set; } = "0 30 0,4,8,12,16,20 ? * *";
Expand All @@ -90,6 +95,7 @@ public class AutoJobModel
public string AutoBuyShopItemJobCron { get; set; } = "0 9 9,12,15,18 ? * *";
public string AutoLocalRaidJobCron { get; set; } = "0 31 12,19 ? * *";
public string AutoDeployGuildDefenseJobCron { get; set; } = "0 20 19 ? * *";
public string AutoChangeGachaRelicJobCron { get; set; } = "0 0 5 ? * MON *";
}

public class BountyQuestAutoModel
Expand Down Expand Up @@ -124,7 +130,7 @@ public class LocalRaidConfig
// new WeightedItem(ItemType.CharacterTrainingMaterial, 1, 2), // 经验珠
// new WeightedItem(ItemType.EquipmentReinforcementItem, 1, 1), // 强化水
// };

public bool SelfCreateRoom { get; set; }

public int WaitSeconds { get; set; } = 3;
Expand Down Expand Up @@ -210,16 +216,21 @@ public enum AutoUseItemType


public AutoJobModel AutoJob { get; set; } = new();

[Obsolete("Use config in PlayerOption")]
public GachaConfigModel GachaConfig { get; set; } = new();

public DungeonBattleRelicSortInfo[] DungeonBattleRelicSort { get; set; }
public int AutoRequestDelay { get; set; }
public bool RecordBattleLog { get; set; } = true;
public string BattleLogDir { get; set; } = "BattleLogs/";
public BountyQuestAutoModel BountyQuestAuto { get; set; } = new();
public DungeonBattleConfig DungeonBattle { get; set; } = new();
public ShopConfig Shop { get; set; } = new();

[Obsolete("Use config in PlayerOption")]
public LocalRaidConfig LocalRaid { get; set; } = new();

public LoginConfig Login { get; set; } = new();
public ItemsConfig Items { get; set; } = new();
}
Expand All @@ -234,6 +245,8 @@ public class PlayerOption
public PvpOption BattleLeague { get; set; } = new();
public PvpOption LegendLeague { get; set; } = new();
public GameConfig.LocalRaidConfig LocalRaid { get; set; } = new();

public GameConfig.GachaConfigModel GachaConfig { get; set; } = new();
}

public class PvpOption
Expand Down
28 changes: 28 additions & 0 deletions MementoMori/Jobs/AutoChangeGachaRelicJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MementoMori.Option;
using Quartz;

namespace MementoMori.Jobs;

internal class AutoChangeGachaRelicJob : IJob
{
private AccountManager _accountManager;

public AutoChangeGachaRelicJob(AccountManager accountManager)
{
_accountManager = accountManager;
}

public async Task Execute(IJobExecutionContext context)
{
var userId = context.MergedJobDataMap.GetLongValue("userId");
if (userId <= 0) return;
var account = _accountManager.Get(userId);
if (!account.Funcs.IsQuickActionExecuting) await account.Funcs.Login();
await account.Funcs.AutoSetGachaRelic();
}
}
59 changes: 59 additions & 0 deletions MementoMori/MementoMoriFuncs.Ops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2288,6 +2288,65 @@ await ExecuteQuickAction(async (log, token) =>
});
}

public async Task AutoSetGachaRelic()
{
await ExecuteQuickAction(async (log, token) =>
{
var targetRelicType = PlayerOption.GachaConfig.TargetRelicType;
if (targetRelicType == GachaRelicType.None) return;

var listResponse = await GetResponse<GachaGetListRequest, GachaGetListResponse>(new GachaGetListRequest());
if (!listResponse.IsFreeChangeRelicGacha) return;

var gachaCaseInfo = listResponse.GachaCaseInfoList.Find(d=>d.GachaGroupType == GachaGroupType.HolyAngel);
if (gachaCaseInfo == null) return;

if (gachaCaseInfo.GachaRelicType == targetRelicType) return;

await GetResponse<ChangeGachaRelicRequest, ChangeGachaRelicResponse>(new ChangeGachaRelicRequest(){GachaRelicType = targetRelicType});

var name = targetRelicType switch
{
GachaRelicType.ChaliceOfHeavenly => TextResourceTable.Get("[ItemName45]"),
GachaRelicType.SilverOrderOfTheBlueSky => TextResourceTable.Get("[ItemName46]"),
GachaRelicType.DivineWingsOfDesire => TextResourceTable.Get("[ItemName47]"),
GachaRelicType.FruitOfTheGarden => TextResourceTable.Get("[ItemName48]"),
_ => ""
};

log($"{TextResourceTable.Get("[GachaRelicChangeTitle]")} {name} {ResourceStrings.Success}");
});
}

public async Task BuyGachaRelic()
{
await ExecuteQuickAction(async (log, token) =>
{
var targetRelicType = PlayerOption.GachaConfig.TargetRelicType;
if (targetRelicType == GachaRelicType.None) return;

var listResponse = await GetResponse<GachaGetListRequest, GachaGetListResponse>(new GachaGetListRequest());
if (!listResponse.IsFreeChangeRelicGacha) return;

var gachaCaseInfo = listResponse.GachaCaseInfoList.Find(d=>d.GachaGroupType == GachaGroupType.HolyAngel);
if (gachaCaseInfo == null) return;

if (gachaCaseInfo.GachaRelicType == targetRelicType) return;

await GetResponse<ChangeGachaRelicRequest, ChangeGachaRelicResponse>(new ChangeGachaRelicRequest(){GachaRelicType = targetRelicType});

var name = targetRelicType switch
{
GachaRelicType.ChaliceOfHeavenly => TextResourceTable.Get("[ItemName45]"),
GachaRelicType.SilverOrderOfTheBlueSky => TextResourceTable.Get("[ItemName46]"),
GachaRelicType.DivineWingsOfDesire => TextResourceTable.Get("[ItemName47]"),
GachaRelicType.FruitOfTheGarden => TextResourceTable.Get("[ItemName48]"),
_ => ""
};

log($"{TextResourceTable.Get("[GachaRelicChangeTitle]")} {name} {ResourceStrings.Success}");
});
}

public async Task ExecuteAllQuickAction()
{
Expand Down

0 comments on commit d80c252

Please sign in to comment.