Make draft logs compatible with MTGO format #5450 - https://github.com/magefree/mage/issues/5450

This commit is contained in:
John Hitchings 2018-12-19 19:41:44 -08:00
parent c7bc799f86
commit 7e621ee8af
7 changed files with 134 additions and 70 deletions

View file

@ -21,7 +21,6 @@ import mage.client.util.audio.AudioManager;
import mage.client.util.gui.BufferedImageBuilder; import mage.client.util.gui.BufferedImageBuilder;
import mage.constants.PlayerAction; import mage.constants.PlayerAction;
import mage.view.*; import mage.view.*;
import org.apache.log4j.Logger;
import javax.swing.*; import javax.swing.*;
import javax.swing.Timer; import javax.swing.Timer;
@ -31,12 +30,9 @@ import java.awt.event.ActionListener;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; 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.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.List;
/** /**
* *
@ -44,8 +40,6 @@ import java.util.*;
*/ */
public class DraftPanel extends javax.swing.JPanel { public class DraftPanel extends javax.swing.JPanel {
private static final Logger LOGGER = Logger.getLogger(DraftPanel.class);
private UUID draftId; private UUID draftId;
private Timer countdown; private Timer countdown;
private int timeout; private int timeout;
@ -63,8 +57,11 @@ public class DraftPanel extends javax.swing.JPanel {
// id of card with popup menu // id of card with popup menu
protected UUID cardIdPopupMenu; protected UUID cardIdPopupMenu;
// Filename for the draft log (only updated if writing the log). // Helper for writing the draft log.
private String logFilename; private DraftPickLogger draftLogger;
// List of set codes (for draft log writing).
private List<String> setCodes;
// Number of the current booster (for draft log writing). // Number of the current booster (for draft log writing).
private int packNo; private int packNo;
@ -73,7 +70,6 @@ public class DraftPanel extends javax.swing.JPanel {
private int pickNo; private int pickNo;
// Cached booster data to be written into the log (see logLastPick). // Cached booster data to be written into the log (see logLastPick).
private String currentBoosterHeader;
private String[] currentBooster; private String[] currentBooster;
private static final CardsView EMPTY_VIEW = new CardsView(); private static final CardsView EMPTY_VIEW = new CardsView();
@ -139,17 +135,11 @@ public class DraftPanel extends javax.swing.JPanel {
} }
if (isLogging()) { if (isLogging()) {
// If we are logging the draft create a file that will contain
// the log.
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
logFilename = "Draft_" + sdf.format(new Date()) + '_' + draftId + ".txt"; String logFilename = "Draft_" + sdf.format(new Date()) + '_' + draftId + ".txt";
try { draftLogger = new DraftPickLogger(new File("gamelogs"), logFilename);
Files.write(pathToDraftLog(), "".getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException ex) {
LOGGER.error(null, ex);
}
} else { } else {
logFilename = null; draftLogger = new DraftPickLogger();
} }
} }
@ -171,6 +161,8 @@ public class DraftPanel extends javax.swing.JPanel {
packNo = draftView.getBoosterNum(); packNo = draftView.getBoosterNum();
pickNo = draftView.getCardNum(); pickNo = draftView.getCardNum();
setCodes = draftView.getSetCodes();
draftLogger.updateDraft(draftId, draftView);
int right = draftView.getPlayers().size() / 2; int right = draftView.getPlayers().size() / 2;
int left = draftView.getPlayers().size() - right; int left = draftView.getPlayers().size() - right;
@ -416,13 +408,21 @@ public class DraftPanel extends javax.swing.JPanel {
if (currentBooster != null) { if (currentBooster != null) {
String lastPick = getCardName(getLastPick(pickView.getPicks().values())); String lastPick = getCardName(getLastPick(pickView.getPicks().values()));
if (lastPick != null && currentBooster.length > 1) { if (lastPick != null && currentBooster.length > 1) {
logPick(lastPick); draftLogger.logPick(getCurrentSetCode(), packNo, pickNo-1, lastPick, currentBooster);
} }
currentBooster = null; currentBooster = null;
} }
setCurrentBoosterForLog(pickView.getBooster()); setCurrentBoosterForLog(pickView.getBooster());
if (currentBooster.length == 1) { 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,38 +440,9 @@ public class DraftPanel extends javax.swing.JPanel {
} }
} }
currentBoosterHeader = "Pack " + packNo + " pick " + pickNo + ":\n";
currentBooster = cards.toArray(new String[cards.size()]); 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<SimpleCardView> picks) { private static SimpleCardView getLastPick(Collection<SimpleCardView> picks) {
SimpleCardView last = null; SimpleCardView last = null;
for (SimpleCardView pick : picks) { for (SimpleCardView pick : picks) {

View file

@ -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);
}
}
}
}

View file

@ -353,8 +353,9 @@ public class CallbackClientImpl implements CallbackClient {
} }
case DRAFT_UPDATE: { case DRAFT_UPDATE: {
DraftPanel panel = MageFrame.getDraft(callback.getObjectId()); DraftPanel panel = MageFrame.getDraft(callback.getObjectId());
DraftClientMessage message = (DraftClientMessage) callback.getData();
if (panel != null) { if (panel != null) {
panel.updateDraft((DraftView) callback.getData()); panel.updateDraft(message.getDraftView());
} }
break; break;
} }

View file

@ -13,21 +13,12 @@ public class DraftClientMessage implements Serializable {
private DraftView draftView; private DraftView draftView;
private DraftPickView draftPickView; private DraftPickView draftPickView;
private String message;
public DraftClientMessage(DraftView draftView) { public DraftClientMessage(DraftView draftView, DraftPickView draftPickView) {
this.draftView = draftView; this.draftView = draftView;
}
public DraftClientMessage(DraftPickView draftPickView) {
this.draftPickView = draftPickView; this.draftPickView = draftPickView;
} }
public DraftClientMessage(DraftView draftView, String message) {
this.message = message;
this.draftView = draftView;
}
public DraftPickView getDraftPickView() { public DraftPickView getDraftPickView() {
return draftPickView; return draftPickView;
} }

View file

@ -7,6 +7,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import mage.cards.ExpansionSet; import mage.cards.ExpansionSet;
import mage.game.draft.Draft; import mage.game.draft.Draft;
import mage.game.draft.DraftCube;
import mage.game.draft.DraftPlayer; import mage.game.draft.DraftPlayer;
/** /**
@ -17,6 +18,7 @@ public class DraftView implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final List<String> sets = new ArrayList<>(); private final List<String> sets = new ArrayList<>();
private final List<String> setCodes = new ArrayList<>();
private final int boosterNum; private final int boosterNum;
private final int cardNum; private final int cardNum;
private final List<String> players = new ArrayList<>(); private final List<String> players = new ArrayList<>();
@ -24,11 +26,14 @@ public class DraftView implements Serializable {
public DraftView(Draft draft) { public DraftView(Draft draft) {
if (draft.getDraftCube() != null) { if (draft.getDraftCube() != null) {
for (int i = 0; i < draft.getNumberBoosters(); i++) { 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 { } else {
for (ExpansionSet set: draft.getSets()) { for (ExpansionSet set: draft.getSets()) {
sets.add(set.getName()); sets.add(set.getName());
setCodes.add(set.getCode());
} }
} }
this.boosterNum = draft.getBoosterNum(); this.boosterNum = draft.getBoosterNum();
@ -42,6 +47,10 @@ public class DraftView implements Serializable {
return sets; return sets;
} }
public List<String> getSetCodes() {
return setCodes;
}
public List<String> getPlayers() { public List<String> getPlayers() {
return players; return players;
} }

View file

@ -48,7 +48,8 @@ public class DraftSession {
if (user.isPresent()) { if (user.isPresent()) {
if (futureTimeout != null && !futureTimeout.isDone()) { if (futureTimeout != null && !futureTimeout.isDone()) {
int remaining = (int) futureTimeout.getDelay(TimeUnit.SECONDS); 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; return true;
} }
@ -60,8 +61,8 @@ public class DraftSession {
if (!killed) { if (!killed) {
UserManager.instance UserManager.instance
.getUser(userId). .getUser(userId).
ifPresent(user -> user.fireCallback( ifPresent(user -> user.fireCallback(new ClientCallback(ClientCallbackMethod.DRAFT_UPDATE, draft.getId(),
new ClientCallback(ClientCallbackMethod.DRAFT_UPDATE, draft.getId(), getDraftView()))); new DraftClientMessage(getDraftView(), null))));
} }
} }
@ -70,7 +71,6 @@ public class DraftSession {
UserManager.instance UserManager.instance
.getUser(userId) .getUser(userId)
.ifPresent(user -> user.fireCallback(new ClientCallback(ClientCallbackMethod.DRAFT_OVER, draft.getId()))); .ifPresent(user -> user.fireCallback(new ClientCallback(ClientCallbackMethod.DRAFT_OVER, draft.getId())));
} }
} }
@ -79,7 +79,8 @@ public class DraftSession {
setupTimeout(timeout); setupTimeout(timeout);
UserManager.instance UserManager.instance
.getUser(userId) .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)))));
} }
} }

View file

@ -46,6 +46,7 @@ public abstract class DraftCube {
private static final Logger logger = Logger.getLogger(DraftCube.class); private static final Logger logger = Logger.getLogger(DraftCube.class);
private final String name; private final String name;
private final String code;
private static final int boosterSize = 15; private static final int boosterSize = 15;
protected List<CardIdentity> cubeCards = new ArrayList<>(); protected List<CardIdentity> cubeCards = new ArrayList<>();
@ -53,12 +54,17 @@ public abstract class DraftCube {
public DraftCube(String name) { public DraftCube(String name) {
this.name = name; this.name = name;
this.code = getClass().getSimpleName();
} }
public String getName() { public String getName() {
return name; return name;
} }
public String getCode() {
return code;
}
public List<CardIdentity> getCubeCards() { public List<CardIdentity> getCubeCards() {
return cubeCards; return cubeCards;
} }