aboutsummaryrefslogtreecommitdiffstats
path: root/Malmstone/Plugin.cs
blob: 5ed6a6525e95ae27a4ddd4b5a3d3a5fe6343679e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using Dalamud.Game.Command;
using Dalamud.IoC;
using Dalamud.Plugin;
using Dalamud.Interface.Windowing;
using Dalamud.Plugin.Services;
using Malmstone.Windows;
using Malmstone.Services;
using System;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Game.Text.SeStringHandling.Payloads;
using System.Collections.Generic;
using System.Linq;
using Malmstone.Utils;
using Malmstone.Addons;
using Dalamud.Game.Addon.Lifecycle;

namespace Malmstone;

public sealed class Plugin : IDalamudPlugin
{
    [PluginService] internal static IDalamudPluginInterface PluginInterface { get; private set; } = null!;
    [PluginService] internal static ITextureProvider TextureProvider { get; private set; } = null!;
    [PluginService] internal static ICommandManager CommandManager { get; private set; } = null!;
    [PluginService] internal static IChatGui Chat { get; private set; } = null!;
    [PluginService] internal static IAddonLifecycle AddonLifeCycle { get; private set; } = null!;
    [PluginService] internal static IToastGui ToastGui { get; private set; } = null!;

    private const string CommandName = "/pmalm";

    public Configuration Configuration { get; init; }

    public readonly WindowSystem WindowSystem = new("Malmstone");
    private ConfigWindow ConfigWindow { get; init; }
    private MainWindow MainWindow { get; init; }

    internal readonly PvPService PvPService;
    internal PvPMatchAddon PvPAddon;

    public Plugin()
    {
        Configuration = PluginInterface.GetPluginConfig() as Configuration ?? new Configuration();

        ConfigWindow = new ConfigWindow(this);
        MainWindow = new MainWindow(this);
        PvPService = new PvPService();
        PvPAddon = new PvPMatchAddon(this);
        if (Configuration.ShowProgressionChatPostCC)
            PvPAddon.EnableCrystallineConflictPostMatch();
        if (Configuration.ShowProgressionChatPostRW)
            PvPAddon.EnableRivalWingsPostMatch();
        if (Configuration.ShowProgressionChatPostFL)
            PvPAddon.EnableFrontlinePostMatch();
        if (Configuration.ShowProgressionToastPostMatch)
            PvPAddon.EnablePostMatchProgressionToast();
        if (Configuration.ShowMainWindowOnPVPReward)
            EnablePVPRewardWindowAddon();
        
        if (Configuration.PostmatchProgressionToastType < 0 || Configuration.PostmatchProgressionToastType > 2)
        {
            Configuration.PostmatchProgressionToastType = 0;
        }

        WindowSystem.AddWindow(ConfigWindow);
        WindowSystem.AddWindow(MainWindow);

        CommandManager.AddHandler(CommandName, new CommandInfo(OnCommand)
        {
            HelpMessage = "/pmalm <rank> <all/cc/fl/rw> -- Displays PVP games left until a target rank. cc = Crystalline Conflict, fl = Frontlines, rw = Rivalwings"
        });

        PluginInterface.UiBuilder.Draw += DrawUI;
        PluginInterface.UiBuilder.OpenConfigUi += ToggleConfigUI;
        PluginInterface.UiBuilder.OpenMainUi += ToggleMainUI;
    }

    public void Dispose()
    {
        WindowSystem.RemoveAllWindows();

        ConfigWindow.Dispose();
        MainWindow.Dispose();

        if (Configuration.ShowProgressionChatPostCC)
            PvPAddon.DisableCrystallineConflictPostMatch();
        if (Configuration.ShowProgressionChatPostRW)
            PvPAddon.DisableRivalWingsPostMatch();
        if (Configuration.ShowProgressionChatPostFL)
            PvPAddon.DisableFrontlinePostMatch();
        if (Configuration.ShowProgressionToastPostMatch)
            PvPAddon.DisablePostMatchProgressionToast();
        if(Configuration.ShowMainWindowOnPVPReward)
            DisablePVPRewardWindowAddon();

        CommandManager.RemoveHandler(CommandName);
    }

private void OnCommand(string command, string args)
{
    if (string.IsNullOrWhiteSpace(args))
    {
        ToggleMainUI();
        return;
    }

    var splitArgs = args.Split(' ', StringSplitOptions.RemoveEmptyEntries);
    var specs = new HashSet<string>(splitArgs.Skip(1).Select(spec => spec.ToLower()));

    var pvpInfo = PvPService.GetPvPSeriesInfo();

    if (pvpInfo == null) return;
    if (!int.TryParse(splitArgs[0], out int targetRank))
    {
        if (splitArgs[0] == "next") targetRank = pvpInfo.CurrentSeriesRank + 1;
        else if (splitArgs[0] == "config")
        {
            ToggleConfigUI();
            return;
        }
        else return;

    }
    // Show games left in chat log when there are args

    if (targetRank < 1)
    {
        Chat.PrintError("Can't have a target rank less than 1");
        return;
    }

    if (targetRank > 107397)
    {
        Chat.PrintError("Can't have a target rank greater than 107397 (are you really gonna be able to reach that anyways?)");
        return;
    }

    if (targetRank < pvpInfo.CurrentSeriesRank)
    {
        Chat.PrintError("You've already surpassed Rank " + targetRank);
        return;
    }

    var xpResult = MalmstoneXPCalculator.CalculateXp(
        pvpInfo.CurrentSeriesRank,
        targetRank,
        pvpInfo.SeriesExperience);

    bool includeAll = specs.Contains("all");
    if (!specs.Any())
    {
        includeAll = true;
    }
    var seString = new SeString(new List<Payload>());
    seString.Append(new TextPayload("\n[To Series Level " + targetRank + "]"));

    // Crystalline Conflict
    if (includeAll || specs.Contains("cc"))
    {
        seString.Append(new TextPayload("\nCrystalline Conflict:\n"));
        seString.Append(new UIForegroundPayload(35));

        if (xpResult.CrystallineConflictWin > 0)
        {
            seString.Append(new TextPayload($"Win: {xpResult.CrystallineConflictWin} " + (xpResult.CrystallineConflictWin == 1 ? "time" : "times") + "\n"));
        }

        if (xpResult.CrystallineConflictLose > 0)
        {
            seString.Append(new TextPayload($"Lose: {xpResult.CrystallineConflictLose} " + (xpResult.CrystallineConflictLose == 1 ? "time" : "times") + "\n"));
        }

        seString.Append(UIForegroundPayload.UIForegroundOff);
    }

    //Frontlines
    if (includeAll || specs.Contains("fl"))
    {
        seString.Append(new TextPayload("\nFrontlines:\n"));
        seString.Append(new UIForegroundPayload(518));

        if (xpResult.FrontlineWin > 0)
        {
            seString.Append(new TextPayload($"Take 1st Place: {xpResult.FrontlineWin} " + (xpResult.FrontlineWin == 1 ? "time" : "times") + "\n"));
        }

        if (xpResult.FrontlineLose2nd > 0)
        {
            seString.Append(new TextPayload($"Take 2nd Place: {xpResult.FrontlineLose2nd} " + (xpResult.FrontlineLose2nd == 1 ? "time" : "times") + "\n"));
        }

        if (xpResult.FrontlineLose3rd > 0)
        {
            seString.Append(new TextPayload($"Take 3rd Place: {xpResult.FrontlineLose3rd} " + (xpResult.FrontlineLose3rd == 1 ? "time" : "times") + "\n"));
        }

        seString.Append(UIForegroundPayload.UIForegroundOff);

        seString.Append(new TextPayload("\nFrontlines (Roulette):\n"));
        seString.Append(new UIForegroundPayload(518));

        if (xpResult.FrontlineDailyWin > 0)
        {
            seString.Append(new TextPayload($"Take 1st Place: {xpResult.FrontlineDailyWin} " + (xpResult.FrontlineDailyWin == 1 ? "time" : "times") + "\n"));
        }

        if (xpResult.FrontlineDailyLose2nd > 0)
        {
            seString.Append(new TextPayload($"Take 2nd Place: {xpResult.FrontlineDailyLose2nd} " + (xpResult.FrontlineDailyLose2nd == 1 ? "time" : "times") + "\n"));
        }

        if (xpResult.FrontlineDailyLose3rd > 0)
        {
            seString.Append(new TextPayload($"Take 3rd Place: {xpResult.FrontlineDailyLose3rd} " + (xpResult.FrontlineDailyLose3rd == 1 ? "time" : "times") + "\n"));
        }

        seString.Append(UIForegroundPayload.UIForegroundOff);
    }

    // Rival Wings
    if (includeAll || specs.Contains("rw"))
    {
        seString.Append(new TextPayload("\nRival Wings:\n"));
        seString.Append(new UIForegroundPayload(43));

        if (xpResult.RivalWingsWin > 0)
        {
            seString.Append(new TextPayload($"Win: {xpResult.RivalWingsWin} " + (xpResult.RivalWingsWin == 1 ? "time" : "times") + "\n"));
            }

            if (xpResult.RivalWingsLose > 0)
        {
            seString.Append(new TextPayload($"Lose: {xpResult.RivalWingsLose} " + (xpResult.RivalWingsLose == 1 ? "time" : "times") + "\n"));
        }

        seString.Append(UIForegroundPayload.UIForegroundOff);
    }

    if (seString.Payloads.Count > 0) Chat.Print(seString);
}



    private void DrawUI() => WindowSystem.Draw();

    public void ToggleConfigUI() => ConfigWindow.Toggle();
    public void ToggleMainUI() => MainWindow.Toggle();

    public void EnablePVPRewardWindowAddon()
    {
        AddonLifeCycle.RegisterListener(AddonEvent.PostSetup, "PvpReward", MainWindow.OnOpenPVPRewardWindow);
        AddonLifeCycle.RegisterListener(AddonEvent.PreFinalize, "PvpReward", MainWindow.OnClosePVPRewardWindow);
    }
    public void DisablePVPRewardWindowAddon()
    {
        AddonLifeCycle.UnregisterListener(AddonEvent.PostSetup, "PvpReward", MainWindow.OnOpenPVPRewardWindow);
        AddonLifeCycle.UnregisterListener(AddonEvent.PreFinalize, "PvpReward", MainWindow.OnClosePVPRewardWindow);
    }
}

send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage