From 7e621ee8af57123e089ebd2ff5be39a5d9528df2 Mon Sep 17 00:00:00 2001 From: John Hitchings Date: Wed, 19 Dec 2018 19:41:44 -0800 Subject: [PATCH] Make draft logs compatible with MTGO format #5450 - https://github.com/magefree/mage/issues/5450 --- .../java/mage/client/draft/DraftPanel.java | 77 ++++++----------- .../mage/client/draft/DraftPickLogger.java | 85 +++++++++++++++++++ .../client/remote/CallbackClientImpl.java | 3 +- .../java/mage/view/DraftClientMessage.java | 11 +-- .../src/main/java/mage/view/DraftView.java | 11 ++- .../java/mage/server/draft/DraftSession.java | 11 +-- .../main/java/mage/game/draft/DraftCube.java | 6 ++ 7 files changed, 134 insertions(+), 70 deletions(-) create mode 100644 Mage.Client/src/main/java/mage/client/draft/DraftPickLogger.java diff --git a/Mage.Client/src/main/java/mage/client/draft/DraftPanel.java b/Mage.Client/src/main/java/mage/client/draft/DraftPanel.java index 2ebb71df7a..9cbff6f1cd 100644 --- a/Mage.Client/src/main/java/mage/client/draft/DraftPanel.java +++ b/Mage.Client/src/main/java/mage/client/draft/DraftPanel.java @@ -21,7 +21,6 @@ import mage.client.util.audio.AudioManager; import mage.client.util.gui.BufferedImageBuilder; import mage.constants.PlayerAction; import mage.view.*; -import org.apache.log4j.Logger; import javax.swing.*; import javax.swing.Timer; @@ -31,21 +30,16 @@ import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardOpenOption; import java.text.SimpleDateFormat; import java.util.*; +import java.util.List; -/** + /** * * @author BetaSteward_at_googlemail.com */ public class DraftPanel extends javax.swing.JPanel { - private static final Logger LOGGER = Logger.getLogger(DraftPanel.class); - private UUID draftId; private Timer countdown; private int timeout; @@ -63,8 +57,11 @@ public class DraftPanel extends javax.swing.JPanel { // id of card with popup menu protected UUID cardIdPopupMenu; - // Filename for the draft log (only updated if writing the log). - private String logFilename; + // Helper for writing the draft log. + private DraftPickLogger draftLogger; + + // List of set codes (for draft log writing). + private List setCodes; // Number of the current booster (for draft log writing). private int packNo; @@ -73,7 +70,6 @@ public class DraftPanel extends javax.swing.JPanel { private int pickNo; // Cached booster data to be written into the log (see logLastPick). - private String currentBoosterHeader; private String[] currentBooster; private static final CardsView EMPTY_VIEW = new CardsView(); @@ -139,17 +135,11 @@ public class DraftPanel extends javax.swing.JPanel { } if (isLogging()) { - // If we are logging the draft create a file that will contain - // the log. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); - logFilename = "Draft_" + sdf.format(new Date()) + '_' + draftId + ".txt"; - try { - Files.write(pathToDraftLog(), "".getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); - } catch (IOException ex) { - LOGGER.error(null, ex); - } + String logFilename = "Draft_" + sdf.format(new Date()) + '_' + draftId + ".txt"; + draftLogger = new DraftPickLogger(new File("gamelogs"), logFilename); } else { - logFilename = null; + draftLogger = new DraftPickLogger(); } } @@ -171,6 +161,8 @@ public class DraftPanel extends javax.swing.JPanel { packNo = draftView.getBoosterNum(); pickNo = draftView.getCardNum(); + setCodes = draftView.getSetCodes(); + draftLogger.updateDraft(draftId, draftView); int right = draftView.getPlayers().size() / 2; int left = draftView.getPlayers().size() - right; @@ -251,7 +243,7 @@ public class DraftPanel extends javax.swing.JPanel { } } - public void loadBooster(DraftPickView draftPickView) { + public void loadBooster(DraftPickView draftPickView) { logLastPick(draftPickView); // upper area that shows the picks loadCardsToPickedCardsArea(draftPickView.getPicks()); @@ -416,13 +408,21 @@ public class DraftPanel extends javax.swing.JPanel { if (currentBooster != null) { String lastPick = getCardName(getLastPick(pickView.getPicks().values())); if (lastPick != null && currentBooster.length > 1) { - logPick(lastPick); + draftLogger.logPick(getCurrentSetCode(), packNo, pickNo-1, lastPick, currentBooster); } currentBooster = null; } setCurrentBoosterForLog(pickView.getBooster()); if (currentBooster.length == 1) { - logPick(currentBooster[0]); + draftLogger.logPick(getCurrentSetCode(), packNo, pickNo, currentBooster[0], currentBooster); + } + } + + private String getCurrentSetCode() { + if (!setCodes.isEmpty()) { + return setCodes.get(packNo-1); + } else { + return ""; } } @@ -440,39 +440,10 @@ public class DraftPanel extends javax.swing.JPanel { } } - currentBoosterHeader = "Pack " + packNo + " pick " + pickNo + ":\n"; currentBooster = cards.toArray(new String[cards.size()]); } - private void logPick(String pick) { - StringBuilder b = new StringBuilder(); - b.append(currentBoosterHeader); - for (String name : currentBooster) { - b.append(pick.equals(name) ? "--> " : " "); - b.append(name); - b.append('\n'); - } - b.append('\n'); - appendToDraftLog(b.toString()); - } - - private Path pathToDraftLog() { - File saveDir = new File("gamelogs"); - if (!saveDir.exists()) { - saveDir.mkdirs(); - } - return new File(saveDir, logFilename).toPath(); - } - - private void appendToDraftLog(String data) { - try { - Files.write(pathToDraftLog(), data.getBytes(), StandardOpenOption.APPEND); - } catch (IOException ex) { - LOGGER.error(null, ex); - } - } - - private static SimpleCardView getLastPick(Collection picks) { + private static SimpleCardView getLastPick(Collection picks) { SimpleCardView last = null; for (SimpleCardView pick : picks) { last = pick; diff --git a/Mage.Client/src/main/java/mage/client/draft/DraftPickLogger.java b/Mage.Client/src/main/java/mage/client/draft/DraftPickLogger.java new file mode 100644 index 0000000000..db585e1a22 --- /dev/null +++ b/Mage.Client/src/main/java/mage/client/draft/DraftPickLogger.java @@ -0,0 +1,85 @@ +package mage.client.draft; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.UUID; + +import org.apache.log4j.Logger; + +import mage.view.DraftView; + +public class DraftPickLogger { + + private static final Logger LOGGER = Logger.getLogger(DraftPickLogger.class); + + private final Path logPath; + private final boolean logging; + private boolean headerWritten = false; + + public DraftPickLogger(File directory, String logFilename) { + this.logging = true; + if (!directory.exists()) { + directory.mkdirs(); + } + this.logPath = new File(directory, logFilename).toPath(); + try { + Files.write(logPath, new byte[0], StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); + } catch (IOException ex) { + LOGGER.error(null, ex); + } + } + + public DraftPickLogger() { + this.logging = false; + this.logPath = null; + } + + public void updateDraft(UUID draftId, DraftView draftView) { + if (headerWritten) { + return; + } + headerWritten = true; + Date now = new Date(); + SimpleDateFormat formatter = new SimpleDateFormat("M/d/yyyy h:mm:ss a"); + StringBuilder buffer = new StringBuilder() + .append("Event #: ").append(draftId).append("\n") + .append("Time: ").append(formatter.format(now)).append('\n'); + buffer.append("Players:\n"); + for (String player : draftView.getPlayers()) { + buffer.append(" ").append(player).append('\n'); + } + buffer.append('\n'); + appendToDraftLog(buffer.toString()); + } + + public void logPick(String setCode, int packNo, int pickNo, String pick, String[] currentBooster) { + StringBuilder b = new StringBuilder(); + if (pickNo == 1) { + b.append("------ ").append(setCode).append(" ------\n\n"); + } + b.append("Pack ").append(packNo).append(" pick ").append(pickNo).append(":\n"); + for (String name : currentBooster) { + b.append(pick.equals(name) ? "--> " : " "); + b.append(name); + b.append('\n'); + } + b.append('\n'); + appendToDraftLog(b.toString()); + } + + private void appendToDraftLog(String data) { + if (logging) { + try { + Files.write(logPath, data.getBytes(), StandardOpenOption.APPEND); + } catch (IOException ex) { + LOGGER.error(null, ex); + } + } + } + +} diff --git a/Mage.Client/src/main/java/mage/client/remote/CallbackClientImpl.java b/Mage.Client/src/main/java/mage/client/remote/CallbackClientImpl.java index f3bc3dc5d2..894683a8be 100644 --- a/Mage.Client/src/main/java/mage/client/remote/CallbackClientImpl.java +++ b/Mage.Client/src/main/java/mage/client/remote/CallbackClientImpl.java @@ -353,8 +353,9 @@ public class CallbackClientImpl implements CallbackClient { } case DRAFT_UPDATE: { DraftPanel panel = MageFrame.getDraft(callback.getObjectId()); + DraftClientMessage message = (DraftClientMessage) callback.getData(); if (panel != null) { - panel.updateDraft((DraftView) callback.getData()); + panel.updateDraft(message.getDraftView()); } break; } diff --git a/Mage.Common/src/main/java/mage/view/DraftClientMessage.java b/Mage.Common/src/main/java/mage/view/DraftClientMessage.java index 33bf75f212..dfaaf2bc22 100644 --- a/Mage.Common/src/main/java/mage/view/DraftClientMessage.java +++ b/Mage.Common/src/main/java/mage/view/DraftClientMessage.java @@ -13,21 +13,12 @@ public class DraftClientMessage implements Serializable { private DraftView draftView; private DraftPickView draftPickView; - private String message; - public DraftClientMessage(DraftView draftView) { + public DraftClientMessage(DraftView draftView, DraftPickView draftPickView) { this.draftView = draftView; - } - - public DraftClientMessage(DraftPickView draftPickView) { this.draftPickView = draftPickView; } - public DraftClientMessage(DraftView draftView, String message) { - this.message = message; - this.draftView = draftView; - } - public DraftPickView getDraftPickView() { return draftPickView; } diff --git a/Mage.Common/src/main/java/mage/view/DraftView.java b/Mage.Common/src/main/java/mage/view/DraftView.java index 241c6a840f..b1f2b960a4 100644 --- a/Mage.Common/src/main/java/mage/view/DraftView.java +++ b/Mage.Common/src/main/java/mage/view/DraftView.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.List; import mage.cards.ExpansionSet; import mage.game.draft.Draft; +import mage.game.draft.DraftCube; import mage.game.draft.DraftPlayer; /** @@ -17,6 +18,7 @@ public class DraftView implements Serializable { private static final long serialVersionUID = 1L; private final List sets = new ArrayList<>(); + private final List setCodes = new ArrayList<>(); private final int boosterNum; private final int cardNum; private final List players = new ArrayList<>(); @@ -24,11 +26,14 @@ public class DraftView implements Serializable { public DraftView(Draft draft) { if (draft.getDraftCube() != null) { for (int i = 0; i < draft.getNumberBoosters(); i++) { - sets.add(draft.getDraftCube().getName()); + DraftCube cube = draft.getDraftCube(); + sets.add(cube.getName()); + setCodes.add(cube.getCode()); } } else { for (ExpansionSet set: draft.getSets()) { sets.add(set.getName()); + setCodes.add(set.getCode()); } } this.boosterNum = draft.getBoosterNum(); @@ -42,6 +47,10 @@ public class DraftView implements Serializable { return sets; } + public List getSetCodes() { + return setCodes; + } + public List getPlayers() { return players; } diff --git a/Mage.Server/src/main/java/mage/server/draft/DraftSession.java b/Mage.Server/src/main/java/mage/server/draft/DraftSession.java index d1e087899a..1ab6320e97 100644 --- a/Mage.Server/src/main/java/mage/server/draft/DraftSession.java +++ b/Mage.Server/src/main/java/mage/server/draft/DraftSession.java @@ -48,7 +48,8 @@ public class DraftSession { if (user.isPresent()) { if (futureTimeout != null && !futureTimeout.isDone()) { int remaining = (int) futureTimeout.getDelay(TimeUnit.SECONDS); - user.get().fireCallback(new ClientCallback(ClientCallbackMethod.DRAFT_INIT, draft.getId(), new DraftClientMessage(getDraftPickView(remaining)))); + user.get().fireCallback(new ClientCallback(ClientCallbackMethod.DRAFT_INIT, draft.getId(), + new DraftClientMessage(getDraftView(), getDraftPickView(remaining)))); } return true; } @@ -60,8 +61,8 @@ public class DraftSession { if (!killed) { UserManager.instance .getUser(userId). - ifPresent(user -> user.fireCallback( - new ClientCallback(ClientCallbackMethod.DRAFT_UPDATE, draft.getId(), getDraftView()))); + ifPresent(user -> user.fireCallback(new ClientCallback(ClientCallbackMethod.DRAFT_UPDATE, draft.getId(), + new DraftClientMessage(getDraftView(), null)))); } } @@ -70,7 +71,6 @@ public class DraftSession { UserManager.instance .getUser(userId) .ifPresent(user -> user.fireCallback(new ClientCallback(ClientCallbackMethod.DRAFT_OVER, draft.getId()))); - } } @@ -79,7 +79,8 @@ public class DraftSession { setupTimeout(timeout); UserManager.instance .getUser(userId) - .ifPresent(user -> user.fireCallback(new ClientCallback(ClientCallbackMethod.DRAFT_PICK, draft.getId(), new DraftClientMessage(getDraftPickView(timeout))))); + .ifPresent(user -> user.fireCallback(new ClientCallback(ClientCallbackMethod.DRAFT_PICK, draft.getId(), + new DraftClientMessage(getDraftView(), getDraftPickView(timeout))))); } } diff --git a/Mage/src/main/java/mage/game/draft/DraftCube.java b/Mage/src/main/java/mage/game/draft/DraftCube.java index 439a5c6a6d..7854940ccb 100644 --- a/Mage/src/main/java/mage/game/draft/DraftCube.java +++ b/Mage/src/main/java/mage/game/draft/DraftCube.java @@ -46,6 +46,7 @@ public abstract class DraftCube { private static final Logger logger = Logger.getLogger(DraftCube.class); private final String name; + private final String code; private static final int boosterSize = 15; protected List cubeCards = new ArrayList<>(); @@ -53,12 +54,17 @@ public abstract class DraftCube { public DraftCube(String name) { this.name = name; + this.code = getClass().getSimpleName(); } public String getName() { return name; } + public String getCode() { + return code; + } + public List getCubeCards() { return cubeCards; }