-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathFightLogFrame.java
More file actions
393 lines (350 loc) · 14 KB
/
FightLogFrame.java
File metadata and controls
393 lines (350 loc) · 14 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Copyright (c) 2021, Mazhar <https://twitter.com/maz_rs>
* Copyright (c) 2021, Matsyir <https://github.com/Matsyir>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package matsyir.pvpperformancetracker.views;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.stream.Collectors;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import lombok.extern.slf4j.Slf4j;
import matsyir.pvpperformancetracker.controllers.AnalyzedFightPerformance;
import matsyir.pvpperformancetracker.models.AnimationData;
import matsyir.pvpperformancetracker.models.FightLogEntry;
import matsyir.pvpperformancetracker.controllers.FightPerformance;
import static matsyir.pvpperformancetracker.PvpPerformanceTrackerPlugin.PLUGIN;
import static matsyir.pvpperformancetracker.PvpPerformanceTrackerPlugin.PLUGIN_ICON;
import matsyir.pvpperformancetracker.utils.PvpPerformanceTrackerUtils;
import net.runelite.api.ItemID;
import net.runelite.api.SpriteID;
@Slf4j
public class FightLogFrame extends JFrame
{
private static JFrame fightLogFrame; // save frame as static instance so there's only one at a time, to avoid window clutter.
private static final NumberFormat nf = NumberFormat.getInstance();
private static final NumberFormat nfPercent = NumberFormat.getPercentInstance(); // For KO Chance %
static
{
// initialize number format
nf.setMaximumFractionDigits(2);
nf.setRoundingMode(RoundingMode.HALF_UP);
// initialize percent format
nfPercent.setMaximumFractionDigits(1);
nfPercent.setRoundingMode(RoundingMode.HALF_UP);
}
private FightLogDetailFrame fightLogDetailFrame;
private JTable table;
private ListSelectionListener onRowSelected;
private ArrayList<FightLogEntry> fightLogEntries;
public static JFrame createFightLogFrame(FightPerformance fight, AnalyzedFightPerformance analyzedFight, JRootPane rootPane)
{
// destroy current frame if it exists so we only have one at a time (static field)
if (fightLogFrame != null)
{
fightLogFrame.dispose();
}
// show error modal if the fight has no log entries to display.
ArrayList<FightLogEntry> fightLogEntries = new ArrayList<>(fight.getAllFightLogEntries());
fightLogEntries.removeIf(e -> !e.isFullEntry());
if (fightLogEntries.isEmpty())
{
PLUGIN.createConfirmationModal(false, "This fight has no attack logs to display, or the data is outdated.");
}
else if (analyzedFight != null) // if analyzed fight is set, then show an analyzed fight's fightLogFrame.
{
fightLogFrame = new FightLogFrame(analyzedFight, rootPane);
}
else
{
fightLogFrame = new FightLogFrame(fight,
fightLogEntries,
rootPane);
}
return fightLogFrame;
}
// expects logEntries composing of only "full" log entries, that contain full attack data, not defender entries.
private FightLogFrame(FightPerformance fight, ArrayList<FightLogEntry> logEntries, JRootPane rootPane)
{
//String title = fight.getCompetitor().getName() + " vs " + fight.getOpponent().getName();
super("Fight Log - " + fight.getCompetitor().getName() + " vs " + fight.getOpponent().getName()
+ " on world " + fight.getWorld());
fightLogEntries = logEntries;
fightLogEntries.removeIf(e -> !e.isFullEntry());
// if always on top is supported, and the core RL plugin has "always on top" set, make the frame always
// on top as well so it can be above the client.
if (isAlwaysOnTopSupported())
{
setAlwaysOnTop(PLUGIN.getRuneliteConfig().gameAlwaysOnTop());
}
setIconImage(PLUGIN_ICON);
setSize(860, 503); // Reverted width increase, removed debug columns
setLocation(rootPane.getLocationOnScreen());
JPanel mainPanel = new JPanel(new BorderLayout(4, 4));
Object[][] stats = new Object[fightLogEntries.size()][14]; // Updated column count to include Actual Dmg
int i = 0;
int initialTick = 0;
for (FightLogEntry fightEntry : fightLogEntries)
{
if (i == 0)
{
initialTick = fightEntry.getTick();
}
int styleIcon = fightEntry.getAnimationData().attackStyle.getStyleSpriteId();
JLabel styleIconLabel = new JLabel();
PLUGIN.addSpriteToLabelIfValid(styleIconLabel, styleIcon, this::repaint);
styleIconLabel.setToolTipText(fightEntry.getAnimationData().attackStyle.toString());
stats[i][0] = fightEntry.getAttackerName();
stats[i][1] = styleIconLabel;
stats[i][2] = fightEntry.getHitRange();
stats[i][3] = nf.format(fightEntry.getAccuracy() * 100) + '%';
stats[i][4] = nf.format(fightEntry.getExpectedDamage());
// Actual Dmg column (Index 5)
JLabel dmgLabel = new JLabel();
if (fightEntry.getAnimationData().attackStyle == AnimationData.AttackStyle.MAGIC && fightEntry.isSplash())
{
int splashIcon = SpriteID.SPELL_ICE_BARRAGE_DISABLED;
PLUGIN.addSpriteToLabelIfValid(dmgLabel, splashIcon, this::repaint);
}
else
{
Integer actualDmg = fightEntry.getActualDamageSum();
String dmgText = actualDmg != null ? nf.format(actualDmg) : "-";
dmgLabel.setText(dmgText);
}
stats[i][5] = dmgLabel;
// HP column (Index 6) - Display as Current/Max
Integer hp = fightEntry.getDisplayHpBefore();
Integer maxHp = fightEntry.getOpponentMaxHp();
stats[i][6] = (hp != null && maxHp != null) ? hp + "/" + maxHp : (hp != null ? String.valueOf(hp) : "-");
// KO Chance column (Index 7)
Double koChance = fightEntry.getKoChance();
stats[i][7] = koChance != null ? nfPercent.format(koChance) : "-";
// Special? (Index 8)
stats[i][8] = fightEntry.getAnimationData().isSpecial ? "✔" : "";
// Off-Pray? (Index 9)
stats[i][9] = fightEntry.success() ? "✔" : "";
// Def Prayer (Index 10)
int prayIcon = PvpPerformanceTrackerUtils.getSpriteForHeadIcon(fightEntry.getDefenderOverhead());
boolean hasPray = prayIcon > 0;
boolean hasEly = fightEntry.isElyProc();
boolean hasStaffReduction = fightEntry.isStaffMeleeReductionProc();
if (!hasPray && !hasEly && !hasStaffReduction)
{
stats[i][10] = "";
}
else if ((hasPray ? 1 : 0) + (hasEly ? 1 : 0) + (hasStaffReduction ? 1 : 0) == 1)
{
JLabel label = new JLabel();
if (hasPray)
{
PLUGIN.addSpriteToLabelIfValid(label, prayIcon, this::repaint);
label.setToolTipText("Defensive Prayer");
}
else if (hasEly)
{
PLUGIN.addItemToLabelIfValid(label, ItemID.ELYSIAN_SPIRIT_SHIELD, false, this::repaint, "Elysian proc");
}
else
{
PLUGIN.addItemToLabelIfValid(label, ItemID.STAFF_OF_THE_DEAD, false, this::repaint, "Staff spec damage reduction");
}
stats[i][10] = label;
}
else
{
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 0));
panel.setOpaque(false);
if (hasPray)
{
JLabel defPrayLabel = new JLabel();
PLUGIN.addSpriteToLabelIfValid(defPrayLabel, prayIcon, this::repaint);
defPrayLabel.setToolTipText("Defensive Prayer");
panel.add(defPrayLabel);
}
if (hasEly)
{
JLabel elyLabel = new JLabel();
PLUGIN.addItemToLabelIfValid(elyLabel, ItemID.ELYSIAN_SPIRIT_SHIELD, false, this::repaint, "Elysian proc");
panel.add(elyLabel);
}
if (hasStaffReduction)
{
JLabel staffLabel = new JLabel();
PLUGIN.addItemToLabelIfValid(staffLabel, ItemID.STAFF_OF_THE_DEAD, false, this::repaint, "Staff spec damage reduction");
panel.add(staffLabel);
}
stats[i][10] = panel;
}
// Splash (Index 11)
if (fightEntry.getAnimationData().attackStyle == AnimationData.AttackStyle.MAGIC)
{
int splashIcon = fightEntry.isSplash() ? SpriteID.SPELL_ICE_BARRAGE_DISABLED : SpriteID.SPELL_ICE_BARRAGE;
JLabel splashLabel = new JLabel();
PLUGIN.addSpriteToLabelIfValid(splashLabel, splashIcon, this::repaint);
stats[i][11] = splashLabel;
}
else
{
stats[i][11] = "";
}
// Offensive Pray (Index 12)
JLabel attPrayLabel = new JLabel();
if (fightEntry.getAttackerOffensivePray() > 0)
{
PLUGIN.addSpriteToLabelIfValid(attPrayLabel, fightEntry.getAttackerOffensivePray(), this::repaint);
stats[i][12] = attPrayLabel;
}
else
{
stats[i][12] = "";
}
int tickDuration = fightEntry.getTick() - initialTick;
int durationMillis = (tickDuration * 600); // (* 0.6) to get duration in secs from ticks, so *600 for ms
Duration duration = Duration.ofMillis(durationMillis);
String time = String.format("%02d:%02d.%01d",
duration.toMinutes(),
duration.getSeconds() % 60,
durationMillis % 1000 / 100) + " (" + tickDuration + ")";
stats[i][13] = time;
i++;
}
String[] header = {"Attacker", "Style", "Hit Range", "Accuracy", "Avg Hit", "Actual Dmg", "HP", "KO Chance", "Special?",
"Off-Pray?", "Def Prayer", "Splash", "Offensive Pray", "Time, (Tick)"};
table = new JTable(stats, header);
table.setRowHeight(30);
table.setDefaultEditor(Object.class, null);
table.getColumnModel().getColumn(10).setPreferredWidth(96); // room for def pray + proc icons
table.getColumnModel().getColumn(1).setCellRenderer(new BufferedImageCellRenderer()); // Style
table.getColumnModel().getColumn(5).setCellRenderer(new BufferedImageCellRenderer()); // Actual Dmg
table.getColumnModel().getColumn(10).setCellRenderer(new BufferedImageCellRenderer()); // Def Prayer
table.getColumnModel().getColumn(11).setCellRenderer(new BufferedImageCellRenderer()); // Splash
table.getColumnModel().getColumn(12).setCellRenderer(new BufferedImageCellRenderer()); // Offensive Pray
onRowSelected = e ->
{
int row = table.getSelectedRow();
if (fightLogDetailFrame != null)
{
if (fightLogDetailFrame.rowIdx == row)
{
return;
}
fightLogDetailFrame.dispose();
fightLogDetailFrame = null;
}
fightLogDetailFrame = new FightLogDetailFrame(fight, fightLogEntries.get(row), row,
new Point( // place the new detail frame roughly to the right of the fight log window.
this.getLocation().x + this.getSize().width,
this.getLocation().y)
);
};
table.getSelectionModel().addListSelectionListener(onRowSelected);
mainPanel.add(new JScrollPane(table), BorderLayout.CENTER);
add(mainPanel);
setVisible(true);
}
private static class BufferedImageCellRenderer extends DefaultTableCellRenderer
{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (value instanceof JPanel)
{
JPanel panel = (JPanel) value;
panel.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
panel.setOpaque(true);
return panel;
}
else if (value instanceof BufferedImage)
{
setText("");
setIcon(new ImageIcon((BufferedImage) value));
}
else if (value instanceof JLabel)
{
JLabel val = (JLabel) value;
setIcon(val.getIcon());
setText(val.getText());
setToolTipText(val.getToolTipText());
}
else
{
setText("");
setIcon(null);
}
return this;
}
}
// initialize frame using an AnalyzedFight, in order to pass the analyzed fight data
// to the detailed frame.
private FightLogFrame(AnalyzedFightPerformance fight, JRootPane rootPane)
{
this(fight,
new ArrayList(fight.getAllFightLogEntries().stream()
.filter(FightLogEntry::isFullEntry) // send only attacker logs, and don't use the matching logs since
.collect(Collectors.toList())), // those have old 'dps' values, they're only used for defender lvls/pray/etc client data
rootPane);
// test
if (new ArrayList(fight.getAllFightLogEntries().stream()
.filter(FightLogEntry::isFullEntry) // send only attacker logs
.collect(Collectors.toList())).size() != fight.getAnalyzedMatchingLogs().size())
{
log.info("FIGHT ANALYSIS: ERROR! allFightLogEntries.filter::isFullEntry different size than analyzedMatchingLogs - should not happen");
}
table.getSelectionModel().removeListSelectionListener(onRowSelected);
onRowSelected = e -> {
int row = table.getSelectedRow();
if (fightLogDetailFrame != null)
{
if (fightLogDetailFrame.rowIdx == row)
{
return;
}
fightLogDetailFrame.dispose();
fightLogDetailFrame = null;
}
fightLogDetailFrame = new FightLogDetailFrame(fight, fightLogEntries.get(row), fight.getAnalyzedMatchingLogs().get(row)[1], row,
new Point( // place the new detail frame roughly to the right of the fight log window.
this.getLocationOnScreen().x + (this.getSize().width),
this.getLocationOnScreen().y)
);
};
table.getSelectionModel().addListSelectionListener(onRowSelected);
}
}