Merge pull request #4554 from hooptie45/topic/json-logs

[WIP] Consumable JSON game logs
This commit is contained in:
LevelX2 2018-03-08 17:32:32 +01:00 committed by GitHub
commit fedf254219
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 339 additions and 7 deletions

View file

@ -0,0 +1,139 @@
/*
* Copyright 2018 nanarpuss_at_googlemail.com. 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 BetaSteward_at_googlemail.com ``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 BetaSteward_at_googlemail.com 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.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.remote;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import mage.remote.interfaces.*;
import java.util.UUID;
import com.google.gson.annotations.Expose;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
public class ActionData {
@Expose
public UUID gameId;
@Expose
public String sessionId;
@Expose
public String type;
@Expose
public Object value;
@Expose
public String message;
public String toJson() {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.setExclusionStrategies(new CustomExclusionStrategy()).create();
return gson.toJson(this);
}
public ActionData(String type, UUID gameId, String sessionId) {
this.type = type;
this.sessionId = sessionId;
this.gameId = gameId;
}
public ActionData(String type, UUID gameId) {
this.type = type;
this.gameId = gameId;
}
public class CustomExclusionStrategy implements ExclusionStrategy {
// FIXME: Very crude way of whitelisting, as it applies to all levels of the JSON tree.
private final java.util.Set<String> KEEP = new java.util.HashSet<String>(
java.util.Arrays.asList(
new String[]{
"id",
"choice",
"damage",
"abilityType",
"ability",
"abilities",
"method",
"data",
"options",
"life",
"players",
"zone",
"step",
"phase",
"attackers",
"blockers",
"tapped",
"damage",
"combat",
"paid",
"hand",
"stack",
"convertedManaCost",
"gameId",
"canPlayInHand",
"gameView",
"sessionId",
"power",
"choices",
"targets",
"loyalty",
"toughness",
"power",
"type",
"priorityTime",
"manaCost",
"value",
"message",
"cardsView",
"name",
"count",
"counters",
"battlefield",
"parentId"
}));
public CustomExclusionStrategy() {}
// This method is called for all fields. if the method returns true the
// field is excluded from serialization
@Override
public boolean shouldSkipField(FieldAttributes f) {
String name = f.getName();
return !KEEP.contains(name);
}
// This method is called for all classes. If the method returns true the
// class is excluded.
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
}
}

View file

@ -38,6 +38,7 @@ import mage.remote.interfaces.PlayerActions;
import mage.remote.interfaces.Replays;
import mage.remote.interfaces.ServerState;
import mage.remote.interfaces.Testable;
import mage.remote.ActionData;
/**
* Extracted interface for SessionImpl class.
@ -45,5 +46,5 @@ import mage.remote.interfaces.Testable;
* @author noxx
*/
public interface Session extends ClientData, Connect, GamePlay, GameTypes, ServerState, ChatSession, Feedback, PlayerActions, Replays, Testable {
public void appendJsonLog(ActionData actionData);
}

View file

@ -32,6 +32,11 @@ import java.lang.reflect.UndeclaredThrowableException;
import java.net.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.io.FileWriter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import mage.MageException;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
@ -50,6 +55,7 @@ import mage.interfaces.callback.ClientCallback;
import mage.players.PlayerType;
import mage.players.net.UserData;
import mage.utils.CompressUtil;
import mage.remote.ActionData;
import mage.view.*;
import org.apache.log4j.Logger;
import org.jboss.remoting.*;
@ -798,6 +804,9 @@ public class SessionImpl implements Session {
public boolean sendPlayerUUID(UUID gameId, UUID data) {
try {
if (isConnected()) {
ActionData actionData = new ActionData("SEND_PLAYER_UUID", gameId, getSessionId());
actionData.value = data;
appendJsonLog(actionData);
server.sendPlayerUUID(gameId, sessionId, data);
return true;
}
@ -813,6 +822,10 @@ public class SessionImpl implements Session {
public boolean sendPlayerBoolean(UUID gameId, boolean data) {
try {
if (isConnected()) {
ActionData actionData = new ActionData("SEND_PLAYER_BOOLEAN", gameId, getSessionId());
actionData.value = data;
appendJsonLog(actionData);
server.sendPlayerBoolean(gameId, sessionId, data);
return true;
}
@ -828,6 +841,10 @@ public class SessionImpl implements Session {
public boolean sendPlayerInteger(UUID gameId, int data) {
try {
if (isConnected()) {
ActionData actionData = new ActionData("SEND_PLAYER_INTEGER", gameId, getSessionId());
actionData.value = data;
appendJsonLog(actionData);
server.sendPlayerInteger(gameId, sessionId, data);
return true;
}
@ -843,6 +860,10 @@ public class SessionImpl implements Session {
public boolean sendPlayerString(UUID gameId, String data) {
try {
if (isConnected()) {
ActionData actionData = new ActionData("SEND_PLAYER_STRING", gameId, getSessionId());
actionData.value = data;
appendJsonLog(actionData);
server.sendPlayerString(gameId, sessionId, data);
return true;
}
@ -858,6 +879,9 @@ public class SessionImpl implements Session {
public boolean sendPlayerManaType(UUID gameId, UUID playerId, ManaType data) {
try {
if (isConnected()) {
ActionData actionData = new ActionData("SEND_PLAYER_MANA_TYPE", gameId, getSessionId());
actionData.value = data;
appendJsonLog(actionData);
server.sendPlayerManaType(gameId, playerId, sessionId, data);
return true;
}
@ -869,6 +893,19 @@ public class SessionImpl implements Session {
return false;
}
@Override
public void appendJsonLog(ActionData actionData) {
actionData.sessionId = getSessionId();
String logFileName = "game-" + actionData.gameId + ".json";
System.out.println("Logging to " + logFileName);
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(logFileName, true)))) {
out.println(actionData.toJson());
} catch (IOException e) {
System.err.println(e);
}
}
@Override
public DraftPickView sendCardPick(UUID draftId, UUID cardId, Set<UUID> hiddenCards) {
try {
@ -1274,6 +1311,11 @@ public class SessionImpl implements Session {
public boolean sendPlayerAction(PlayerAction passPriorityAction, UUID gameId, Object data) {
try {
if (isConnected()) {
ActionData actionData = new ActionData("SEND_PLAYER_ACTION", gameId, getSessionId());
actionData.value = data;
appendJsonLog(actionData);
server.sendPlayerAction(passPriorityAction, gameId, sessionId, data);
return true;
}

View file

@ -53,6 +53,8 @@ import mage.target.Target;
import mage.target.Targets;
import mage.util.SubTypeList;
import com.google.gson.annotations.Expose;
/**
* @author BetaSteward_at_googlemail.com
*/
@ -61,11 +63,17 @@ public class CardView extends SimpleCardView {
private static final long serialVersionUID = 1L;
protected UUID parentId;
@Expose
protected String name;
@Expose
protected String displayName;
@Expose
protected List<String> rules;
@Expose
protected String power;
@Expose
protected String toughness;
@Expose
protected String loyalty = "";
protected String startingLoyalty;
protected EnumSet<CardType> cardTypes;
@ -110,7 +118,6 @@ public class CardView extends SimpleCardView {
protected ArtRect artRect = ArtRect.NORMAL;
protected List<UUID> targets;
protected UUID pairedCard;
protected List<UUID> bandedCards;
protected boolean paid;

View file

@ -32,6 +32,11 @@ import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import mage.choices.Choice;
/**
@ -39,18 +44,29 @@ import mage.choices.Choice;
* @author BetaSteward_at_googlemail.com
*/
public class GameClientMessage implements Serializable {
@Expose
private static final long serialVersionUID = 1L;
@Expose
private GameView gameView;
@Expose
private CardsView cardsView;
@Expose
private CardsView cardsView2;
@Expose
private String message;
@Expose
private boolean flag;
@Expose
private String[] strings;
@Expose
private Set<UUID> targets;
@Expose
private int min;
@Expose
private int max;
@Expose
private Map<String, Serializable> options;
@Expose
private Choice choice;
public GameClientMessage(GameView gameView) {
@ -155,4 +171,11 @@ public class GameClientMessage implements Serializable {
return choice;
}
public String toJson() {
Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.create();
return gson.toJson(this);
}
}

View file

@ -28,11 +28,17 @@
package mage.view;
import java.io.Serializable;
import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import mage.MageObject;
import mage.abilities.costs.Cost;
import mage.cards.Card;
@ -54,6 +60,8 @@ import mage.game.stack.StackObject;
import mage.players.Player;
import mage.watchers.common.CastSpellLastTurnWatcher;
import org.apache.log4j.Logger;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/**
*
@ -64,7 +72,6 @@ public class GameView implements Serializable {
private static final long serialVersionUID = 1L;
private static final Logger LOGGER = Logger.getLogger(GameView.class);
private final int priorityTime;
private final List<PlayerView> players = new ArrayList<>();
private CardsView hand;
@ -351,4 +358,8 @@ public class GameView implements Serializable {
return rollbackTurnsAllowed;
}
public String toJson() {
Gson gson = new GsonBuilder().create();
return gson.toJson(this);
}
}

View file

@ -28,6 +28,8 @@
package mage.view;
import com.google.gson.annotations.Expose;
import java.io.Serializable;
import java.util.UUID;
@ -36,6 +38,7 @@ import java.util.UUID;
* @author BetaSteward_at_googlemail.com
*/
public class SimpleCardView implements Serializable {
@Expose
protected UUID id;
protected String expansionSetCode;
protected String tokenSetCode;