refactor: improved code to increase readability. (#11418)

Moved the cardViewEquals method from CardPanelRenderModeMTGO to CardView
Decomposed conditional to increase readability.
Changed the variable/constant names to increase the readability.
This commit is contained in:
Tirth Bharatiya 2023-11-17 12:17:53 +05:30 committed by GitHub
parent bea33c7493
commit 44b8a0faf4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 55 deletions

View file

@ -264,21 +264,22 @@ public class ChatPanelBasic extends javax.swing.JPanel {
messageToTest = message.replaceFirst("<font bgcolor=orange.*?</font>", ""); messageToTest = message.replaceFirst("<font bgcolor=orange.*?</font>", "");
} }
if (messageType == MessageType.USER_INFO || messageType == MessageType.GAME || messageType == MessageType.STATUS String cachedProfanityFilterValue = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_GAME_USE_PROFANITY_FILTER, "0");
|| PreferencesDialog.getCachedValue(PreferencesDialog.KEY_GAME_USE_PROFANITY_FILTER, "0").equals("0") boolean isContainsSwearing = !containsSwearing(messageToTest, cachedProfanityFilterValue);
|| !PreferencesDialog.getCachedValue(PreferencesDialog.KEY_GAME_USE_PROFANITY_FILTER, "0").equals("0") && !containsSwearing(messageToTest, PreferencesDialog.getCachedValue(PreferencesDialog.KEY_GAME_USE_PROFANITY_FILTER, "0"))) { boolean isUserInfoOrGameOrStatus = messageType == MessageType.USER_INFO || messageType == MessageType.GAME || messageType == MessageType.STATUS;
if (isUserInfoOrGameOrStatus || cachedProfanityFilterValue.equals("0") || (!cachedProfanityFilterValue.equals("0") && !isContainsSwearing)) {
if (username != null && !username.isEmpty()) { if (username != null && !username.isEmpty()) {
text.append(getColoredText(userColor, username + userSeparator)); text.append(getColoredText(userColor, username + userSeparator));
} }
text.append(getColoredText(textColor, ManaSymbols.replaceSymbolsWithHTML(message, ManaSymbols.Type.CHAT))); text.append(getColoredText(textColor, ManaSymbols.replaceSymbolsWithHTML(message, ManaSymbols.Type.CHAT)));
this.txtConversation.append(text.toString()); this.txtConversation.append(text.toString());
} else if (PreferencesDialog.getCachedValue(PreferencesDialog.KEY_GAME_USE_PROFANITY_FILTER, "0").equals("1")) { } else if (cachedProfanityFilterValue.equals("1")) {
if (username != null && !username.isEmpty()) { if (username != null && !username.isEmpty()) {
text.append(getColoredText("black", username + userSeparator)); text.append(getColoredText("black", username + userSeparator));
} }
text.append(getColoredText(textColor, ManaSymbols.replaceSymbolsWithHTML("<font color=black size=-2>" + message + "</font> <font size=-2>Profanity detected. Type: <font color=green>/w " + SessionHandler.getUserName() + " profanity 0</font>' to turn the filter off</font></font>", ManaSymbols.Type.CHAT))); text.append(getColoredText(textColor, ManaSymbols.replaceSymbolsWithHTML("<font color=black size=-2>" + message + "</font> <font size=-2>Profanity detected. Type: <font color=green>/w " + SessionHandler.getUserName() + " profanity 0</font>' to turn the filter off</font></font>", ManaSymbols.Type.CHAT)));
this.txtConversation.append(text.toString()); this.txtConversation.append(text.toString());
} else if (PreferencesDialog.getCachedValue(PreferencesDialog.KEY_GAME_USE_PROFANITY_FILTER, "0").equals("2")) { } else if (cachedProfanityFilterValue.equals("2")) {
text.append(getColoredText(textColor, ManaSymbols.replaceSymbolsWithHTML("<font color=black size=-2>" + username + ": Profanity detected. To make it less strict, type: </font> <font color=green size=-2>/w " + SessionHandler.getUserName() + " profanity 1</font>", ManaSymbols.Type.CHAT))); text.append(getColoredText(textColor, ManaSymbols.replaceSymbolsWithHTML("<font color=black size=-2>" + username + ": Profanity detected. To make it less strict, type: </font> <font color=green size=-2>/w " + SessionHandler.getUserName() + " profanity 1</font>", ManaSymbols.Type.CHAT)));
this.txtConversation.append(text.toString()); this.txtConversation.append(text.toString());
} }

View file

@ -16,7 +16,6 @@ import org.mage.plugins.card.images.ImageCacheData;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -54,43 +53,6 @@ public class CardPanelRenderModeMTGO extends CardPanel {
private int updateArtImageStamp; private int updateArtImageStamp;
private static boolean cardViewEquals(CardView a, CardView b) { // TODO: This belongs in CardView
if (a == b) {
return true;
}
if (a == null || b == null || a.getClass() != b.getClass()) {
return false;
}
if (!(a.getDisplayName().equals(b.getDisplayName()) // TODO: Original code not checking everything. Why is it only checking these values?
&& a.getPower().equals(b.getPower())
&& a.getToughness().equals(b.getToughness())
&& a.getLoyalty().equals(b.getLoyalty())
&& a.getDefense().equals(b.getDefense())
&& 0 == a.getColor().compareTo(b.getColor())
&& a.getCardTypes().equals(b.getCardTypes())
&& a.getSubTypes().equals(b.getSubTypes())
&& a.getSuperTypes().equals(b.getSuperTypes())
&& a.getManaCostStr().equals(b.getManaCostStr())
&& a.getRules().equals(b.getRules())
&& Objects.equals(a.getRarity(), b.getRarity())
&& Objects.equals(a.getCardNumber(), b.getCardNumber())
&& Objects.equals(a.getExpansionSetCode(), b.getExpansionSetCode())
&& a.getFrameStyle() == b.getFrameStyle()
&& Objects.equals(a.getCounters(), b.getCounters())
&& a.isFaceDown() == b.isFaceDown())) {
return false;
}
if (!(a instanceof PermanentView)) {
return true;
}
PermanentView aa = (PermanentView) a;
PermanentView bb = (PermanentView) b;
return aa.hasSummoningSickness() == bb.hasSummoningSickness()
&& aa.getDamage() == bb.getDamage();
}
private static class ImageKey { private static class ImageKey {
final BufferedImage artImage; final BufferedImage artImage;
final int width; final int width;
@ -178,7 +140,7 @@ public class CardPanelRenderModeMTGO extends CardPanel {
&& this.height == that.height && this.height == that.height
&& this.isChoosable == that.isChoosable && this.isChoosable == that.isChoosable
&& this.isSelected == that.isSelected && this.isSelected == that.isSelected
&& cardViewEquals(this.view, that.view); && CardView.cardViewEquals(this.view, that.view);
} }
} }

View file

@ -796,6 +796,43 @@ public class CardView extends SimpleCardView {
fillEmpty(null, false); fillEmpty(null, false);
} }
public static boolean cardViewEquals(CardView a, CardView b) { // TODO: This belongs in CardView
if (a == b) {
return true;
}
if (a == null || b == null || a.getClass() != b.getClass()) {
return false;
}
if (!(a.getDisplayName().equals(b.getDisplayName()) // TODO: Original code not checking everything. Why is it only checking these values?
&& a.getPower().equals(b.getPower())
&& a.getToughness().equals(b.getToughness())
&& a.getLoyalty().equals(b.getLoyalty())
&& a.getDefense().equals(b.getDefense())
&& 0 == a.getColor().compareTo(b.getColor())
&& a.getCardTypes().equals(b.getCardTypes())
&& a.getSubTypes().equals(b.getSubTypes())
&& a.getSuperTypes().equals(b.getSuperTypes())
&& a.getManaCostStr().equals(b.getManaCostStr())
&& a.getRules().equals(b.getRules())
&& Objects.equals(a.getRarity(), b.getRarity())
&& Objects.equals(a.getCardNumber(), b.getCardNumber())
&& Objects.equals(a.getExpansionSetCode(), b.getExpansionSetCode())
&& a.getFrameStyle() == b.getFrameStyle()
&& Objects.equals(a.getCounters(), b.getCounters())
&& a.isFaceDown() == b.isFaceDown())) {
return false;
}
if (!(a instanceof PermanentView)) {
return true;
}
PermanentView aa = (PermanentView) a;
PermanentView bb = (PermanentView) b;
return aa.hasSummoningSickness() == bb.hasSummoningSickness()
&& aa.getDamage() == bb.getDamage();
}
private void fillEmpty(Card card, boolean controlled) { private void fillEmpty(Card card, boolean controlled) {
this.name = "Face Down"; this.name = "Face Down";
this.displayName = name; this.displayName = name;

View file

@ -12,14 +12,14 @@ public class GlickoRatingSystem {
// rating deviation will grow back from 50 to max 350 in 2 years // rating deviation will grow back from 50 to max 350 in 2 years
public static final double C = 0.00137934314767061324980397708525; public static final double C = 0.00137934314767061324980397708525;
public static final double BaseRating = 1500; public static final double BASE_RATING = 1500;
public static final double BaseRD = 350; public static final double BASE_RD = 350;
public static final double MinRD = 30; public static final double MIN_RD = 30;
private static final double Q = Math.log(10) / 400; private static final double Q = Math.log(10) / 400;
public static GlickoRating getInitialRating() { public static GlickoRating getInitialRating() {
return new GlickoRating(GlickoRatingSystem.BaseRating, GlickoRatingSystem.BaseRD, 0); return new GlickoRating(GlickoRatingSystem.BASE_RATING, GlickoRatingSystem.BASE_RD, 0);
} }
public static int getDisplayedRating(GlickoRating rating) { public static int getDisplayedRating(GlickoRating rating) {
@ -55,17 +55,17 @@ public class GlickoRatingSystem {
double newRD = Math.sqrt( double newRD = Math.sqrt(
rating.getRatingDeviation() * rating.getRatingDeviation() rating.getRatingDeviation() * rating.getRatingDeviation()
+ C * C * Math.max(gameTimeMs - rating.getLastGameTimeMs(), 0)); + C * C * Math.max(gameTimeMs - rating.getLastGameTimeMs(), 0));
newRatingDeviation = Math.max(Math.min(BaseRD, newRD), MinRD); newRatingDeviation = Math.max(Math.min(BASE_RD, newRD), MIN_RD);
} }
else else
{ {
newRatingDeviation = BaseRD; newRatingDeviation = BASE_RD;
} }
return newRatingDeviation; return newRatingDeviation;
} }
private GlickoRating getNewRating(GlickoRating playerRating, GlickoRating opponentRating, double outcome) { private GlickoRating getNewRating(GlickoRating playerRating, GlickoRating opponentRating, double outcome) {
double RD = playerRating.getRatingDeviation(); double playerRatingDeviation = playerRating.getRatingDeviation();
double g = gFunc(opponentRating.getRatingDeviation()); double g = gFunc(opponentRating.getRatingDeviation());
double p = -g * (playerRating.getRating() - opponentRating.getRating()) / 400; double p = -g * (playerRating.getRating() - opponentRating.getRating()) / 400;
@ -73,8 +73,8 @@ public class GlickoRatingSystem {
double d2 = 1 / (Q * Q * g * g * e * (1 - e)); double d2 = 1 / (Q * Q * g * g * e * (1 - e));
// todo: set minimum K? // todo: set minimum K?
double newRating = playerRating.getRating() + Q / (1 / RD / RD + 1 / d2) * g * (outcome - e); double newRating = playerRating.getRating() + Q / (1 / playerRatingDeviation / playerRatingDeviation + 1 / d2) * g * (outcome - e);
double newRD = Math.sqrt(1 / (1 / RD / RD + 1 / d2)); double newRD = Math.sqrt(1 / (1 / playerRatingDeviation / playerRatingDeviation + 1 / d2));
return new GlickoRating(newRating, newRD); return new GlickoRating(newRating, newRD);
} }

View file

@ -18,7 +18,7 @@ public class GlickoRatingSystemTest {
int count = 1000; int count = 1000;
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
double startRating = RandomUtil.nextDouble() * 2500 + 500; double startRating = RandomUtil.nextDouble() * 2500 + 500;
double startRatingDeviation = Math.min(RandomUtil.nextDouble() * 300 + 100, GlickoRatingSystem.BaseRD); double startRatingDeviation = Math.min(RandomUtil.nextDouble() * 300 + 100, GlickoRatingSystem.BASE_RD);
GlickoRating player1 = new GlickoRating(startRating, startRatingDeviation, 1); GlickoRating player1 = new GlickoRating(startRating, startRatingDeviation, 1);
GlickoRating player2 = new GlickoRating(startRating, startRatingDeviation, 1); GlickoRating player2 = new GlickoRating(startRating, startRatingDeviation, 1);
@ -40,7 +40,7 @@ public class GlickoRatingSystemTest {
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
double startRating1 = RandomUtil.nextDouble() * 2500 + 500; double startRating1 = RandomUtil.nextDouble() * 2500 + 500;
double startRating2 = RandomUtil.nextDouble() * 2500 + 500; double startRating2 = RandomUtil.nextDouble() * 2500 + 500;
double startRatingDeviation = Math.min(RandomUtil.nextDouble() * 300 + 100, GlickoRatingSystem.BaseRD); double startRatingDeviation = Math.min(RandomUtil.nextDouble() * 300 + 100, GlickoRatingSystem.BASE_RD);
GlickoRating player1 = new GlickoRating(startRating1, startRatingDeviation, 1); GlickoRating player1 = new GlickoRating(startRating1, startRatingDeviation, 1);
GlickoRating player2 = new GlickoRating(startRating2, startRatingDeviation, 1); GlickoRating player2 = new GlickoRating(startRating2, startRatingDeviation, 1);