[WIP] Consumable JSON game logs

As discussed in https://github.com/magefree/mage/issues/4515

This exposes a JSON log of game interactions that can be analyzed.

This is just a first pass, to get up to speed with how the messaging works. It'd like to trim down the messages much
further so they don't include redundant information in each message. Also gson supports much more advances serialization
options; such as using the @Expose annotation. We should probably use that, but I ran into some issues (I'm not a java
developer, so still learning).

TODO:
  These currently only exist on the client side; ideally we'd submit this logs back up to a central server after the
  games completion; thinking this could be simple via an S3 file drop, and a Lambda function to process and expose the
  logs; maybe via a kafka stream.

Examples of log messages are below:

```
{
  "gameId": "2cede8c5-ff8e-4f8c-b9ac-66af53c0a254",
  "sessionId": "5c4o149-678483-je42ycva-1-je42ycw2-4",
  "type": "GAME_SELECT",
  "value": {
    "gameView": {
      "priorityTime": 3000,
      "players": [
        {
          "name": "computer",
          "life": 20,
          "counters": {}
        },
        {
          "name": "hooptie",
          "life": 20,
          "counters": {}
        }
      ],
      "hand": {
        "425d774f-ee0c-4a9b-8516-c98f886943f0": {
          "name": "Springleaf Drum",
          "power": "0",
          "toughness": "0",
          "loyalty": "",
          "manaCost": [
            "{1}"
          ],
          "convertedManaCost": 1,
          "type": 0,
          "paid": false,
          "id": "425d774f-ee0c-4a9b-8516-c98f886943f0"
        },
        "dd41bb4b-7fc3-4a3c-a69c-d18e281a1bff": {
          "name": "Blade of the Bloodchief",
          "power": "0",
          "toughness": "0",
          "loyalty": "",
          "manaCost": [
            "{1}"
          ],
          "convertedManaCost": 1,
          "type": 0,
          "paid": false,
          "id": "dd41bb4b-7fc3-4a3c-a69c-d18e281a1bff"
        },
        "06eb0a6c-1e70-4dc0-bd1c-93b6ea444806": {
          "name": "Ornithopter",
          "power": "0",
          "toughness": "2",
          "loyalty": "",
          "manaCost": [
            "{0}"
          ],
          "convertedManaCost": 0,
          "type": 0,
          "paid": false,
          "id": "06eb0a6c-1e70-4dc0-bd1c-93b6ea444806"
        },
        "a2f9cc13-e71a-4c9c-96aa-5424ea1a6b64": {
          "name": "Springleaf Drum",
          "power": "0",
          "toughness": "0",
          "loyalty": "",
          "manaCost": [
            "{1}"
          ],
          "convertedManaCost": 1,
          "type": 0,
          "paid": false,
          "id": "a2f9cc13-e71a-4c9c-96aa-5424ea1a6b64"
        },
        "91239f4f-9003-4c48-8ca1-4c318f892489": {
          "name": "Cranial Plating",
          "power": "0",
          "toughness": "0",
          "loyalty": "",
          "manaCost": [
            "{2}"
          ],
          "convertedManaCost": 2,
          "type": 0,
          "paid": false,
          "id": "91239f4f-9003-4c48-8ca1-4c318f892489"
        },
        "feb268d8-0535-4a9c-8915-83dd92a08c4c": {
          "name": "Arcbound Ravager",
          "power": "0",
          "toughness": "0",
          "loyalty": "",
          "manaCost": [
            "{2}"
          ],
          "convertedManaCost": 2,
          "type": 0,
          "paid": false,
          "id": "feb268d8-0535-4a9c-8915-83dd92a08c4c"
        }
      },
      "canPlayInHand": [
        "06eb0a6c-1e70-4dc0-bd1c-93b6ea444806"
      ],
      "stack": {},
      "combat": [],
      "phase": "PRECOMBAT_MAIN",
      "step": "PRECOMBAT_MAIN"
    },
    "message": "Play spells and abilities.",
    "options": {
      "queryType": "SELECT"
    }
  }
}
{
  "gameId": "2cede8c5-ff8e-4f8c-b9ac-66af53c0a254",
  "sessionId": "5c4o149-678483-je42ycva-1-je42ycw2-4",
  "type": "SEND_PLAYER_UUID",
  "value": "06eb0a6c-1e70-4dc0-bd1c-93b6ea444806"
}
{
  "gameId": "2cede8c5-ff8e-4f8c-b9ac-66af53c0a254",
  "sessionId": "5c4o149-678483-je42ycva-1-je42ycw2-4",
  "type": "GAME_CHOOSE_PILE",
  "value": {
    "choices": {
      "1ecf8671-be4c-4060-a76b-af614235a5b7": "Cast Ornithopter"
    }
  }
}
{
  "gameId": "2cede8c5-ff8e-4f8c-b9ac-66af53c0a254",
  "sessionId": "5c4o149-3v2cj2-je43178o-1-je43179f-4",
  "type": "GAME_INIT",
  "value": {
    "priorityTime": 3000,
    "players": [
      {
        "name": "computer",
        "life": 20,
        "counters": {}
      },
      {
        "name": "hooptie",
        "life": 20,
        "counters": {}
      }
    ],
    "hand": {
      "425d774f-ee0c-4a9b-8516-c98f886943f0": {
        "name": "Springleaf Drum",
        "power": "0",
        "toughness": "0",
        "loyalty": "",
        "manaCost": [
          "{1}"
        ],
        "convertedManaCost": 1,
        "type": 0,
        "paid": false,
        "id": "425d774f-ee0c-4a9b-8516-c98f886943f0"
      },
      "dd41bb4b-7fc3-4a3c-a69c-d18e281a1bff": {
        "name": "Blade of the Bloodchief",
        "power": "0",
        "toughness": "0",
        "loyalty": "",
        "manaCost": [
          "{1}"
        ],
        "convertedManaCost": 1,
        "type": 0,
        "paid": false,
        "id": "dd41bb4b-7fc3-4a3c-a69c-d18e281a1bff"
      },
      "06eb0a6c-1e70-4dc0-bd1c-93b6ea444806": {
        "name": "Ornithopter",
        "power": "0",
        "toughness": "2",
        "loyalty": "",
        "manaCost": [
          "{0}"
        ],
        "convertedManaCost": 0,
        "type": 0,
        "paid": false,
        "id": "06eb0a6c-1e70-4dc0-bd1c-93b6ea444806"
      },
      "a2f9cc13-e71a-4c9c-96aa-5424ea1a6b64": {
        "name": "Springleaf Drum",
        "power": "0",
        "toughness": "0",
        "loyalty": "",
        "manaCost": [
          "{1}"
        ],
        "convertedManaCost": 1,
        "type": 0,
        "paid": false,
        "id": "a2f9cc13-e71a-4c9c-96aa-5424ea1a6b64"
      },
      "91239f4f-9003-4c48-8ca1-4c318f892489": {
        "name": "Cranial Plating",
        "power": "0",
        "toughness": "0",
        "loyalty": "",
        "manaCost": [
          "{2}"
        ],
        "convertedManaCost": 2,
        "type": 0,
        "paid": false,
        "id": "91239f4f-9003-4c48-8ca1-4c318f892489"
      },
      "feb268d8-0535-4a9c-8915-83dd92a08c4c": {
        "name": "Arcbound Ravager",
        "power": "0",
        "toughness": "0",
        "loyalty": "",
        "manaCost": [
          "{2}"
        ],
        "convertedManaCost": 2,
        "type": 0,
        "paid": false,
        "id": "feb268d8-0535-4a9c-8915-83dd92a08c4c"
      }
    },
    "canPlayInHand": [
      "06eb0a6c-1e70-4dc0-bd1c-93b6ea444806"
    ],
    "stack": {},
    "combat": [],
    "phase": "PRECOMBAT_MAIN",
    "step": "PRECOMBAT_MAIN"
  }
}
{
  "gameId": "2cede8c5-ff8e-4f8c-b9ac-66af53c0a254",
  "sessionId": "5c4o149-3v2cj2-je43178o-1-je43179f-4",
  "type": "GAME_SELECT",
  "value": {
    "gameView": {
      "priorityTime": 3000,
      "players": [
        {
          "name": "computer",
          "life": 20,
          "counters": {}
        },
        {
          "name": "hooptie",
          "life": 20,
          "counters": {}
        }
      ],
      "hand": {
        "425d774f-ee0c-4a9b-8516-c98f886943f0": {
          "name": "Springleaf Drum",
          "power": "0",
          "toughness": "0",
          "loyalty": "",
          "manaCost": [
            "{1}"
          ],
          "convertedManaCost": 1,
          "type": 0,
          "paid": false,
          "id": "425d774f-ee0c-4a9b-8516-c98f886943f0"
        },
        "dd41bb4b-7fc3-4a3c-a69c-d18e281a1bff": {
          "name": "Blade of the Bloodchief",
          "power": "0",
          "toughness": "0",
          "loyalty": "",
          "manaCost": [
            "{1}"
          ],
          "convertedManaCost": 1,
          "type": 0,
          "paid": false,
          "id": "dd41bb4b-7fc3-4a3c-a69c-d18e281a1bff"
        },
        "06eb0a6c-1e70-4dc0-bd1c-93b6ea444806": {
          "name": "Ornithopter",
          "power": "0",
          "toughness": "2",
          "loyalty": "",
          "manaCost": [
            "{0}"
          ],
          "convertedManaCost": 0,
          "type": 0,
          "paid": false,
          "id": "06eb0a6c-1e70-4dc0-bd1c-93b6ea444806"
        },
        "a2f9cc13-e71a-4c9c-96aa-5424ea1a6b64": {
          "name": "Springleaf Drum",
          "power": "0",
          "toughness": "0",
          "loyalty": "",
          "manaCost": [
            "{1}"
          ],
          "convertedManaCost": 1,
          "type": 0,
          "paid": false,
          "id": "a2f9cc13-e71a-4c9c-96aa-5424ea1a6b64"
        },
        "91239f4f-9003-4c48-8ca1-4c318f892489": {
          "name": "Cranial Plating",
          "power": "0",
          "toughness": "0",
          "loyalty": "",
          "manaCost": [
            "{2}"
          ],
          "convertedManaCost": 2,
          "type": 0,
          "paid": false,
          "id": "91239f4f-9003-4c48-8ca1-4c318f892489"
        },
        "feb268d8-0535-4a9c-8915-83dd92a08c4c": {
          "name": "Arcbound Ravager",
          "power": "0",
          "toughness": "0",
          "loyalty": "",
          "manaCost": [
            "{2}"
          ],
          "convertedManaCost": 2,
          "type": 0,
          "paid": false,
          "id": "feb268d8-0535-4a9c-8915-83dd92a08c4c"
        }
      },
      "canPlayInHand": [
        "06eb0a6c-1e70-4dc0-bd1c-93b6ea444806"
      ],
      "stack": {},
      "combat": [],
      "phase": "PRECOMBAT_MAIN",
      "step": "PRECOMBAT_MAIN"
    },
    "message": "Play spells and abilities.",
    "options": {
      "queryType": "SELECT"
    }
  }
}
{
  "gameId": "2cede8c5-ff8e-4f8c-b9ac-66af53c0a254",
  "sessionId": "5c4o149-678483-je42ycva-1-je42ycw2-4",
  "type": "SEND_PLAYER_UUID",
  "value": "1ecf8671-be4c-4060-a76b-af614235a5b7"
}
```
This commit is contained in:
Shaun Hannah 2018-02-26 19:23:00 -05:00
parent 871b035bb4
commit 6aeb3c7c3a
7 changed files with 239 additions and 5 deletions

View file

@ -31,6 +31,9 @@ import java.awt.event.KeyEvent;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import javax.swing.*; import javax.swing.*;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import mage.cards.decks.Deck; import mage.cards.decks.Deck;
import mage.client.MageFrame; import mage.client.MageFrame;
import mage.client.SessionHandler; import mage.client.SessionHandler;
@ -47,6 +50,9 @@ import mage.client.util.audio.AudioManager;
import mage.client.util.object.SaveObjectUtil; import mage.client.util.object.SaveObjectUtil;
import mage.interfaces.callback.CallbackClient; import mage.interfaces.callback.CallbackClient;
import mage.interfaces.callback.ClientCallback; import mage.interfaces.callback.ClientCallback;
import mage.remote.ActionData;
import mage.remote.Session;
import mage.remote.SessionImpl;
import mage.utils.CompressUtil; import mage.utils.CompressUtil;
import mage.view.*; import mage.view.*;
import mage.view.ChatMessage.MessageType; import mage.view.ChatMessage.MessageType;
@ -102,7 +108,6 @@ public class CallbackClientImpl implements CallbackClient {
break; break;
case CHATMESSAGE: { case CHATMESSAGE: {
ChatMessage message = (ChatMessage) callback.getData(); ChatMessage message = (ChatMessage) callback.getData();
// Drop messages from ignored users // Drop messages from ignored users
if (message.getUsername() != null && IgnoreList.IGNORED_MESSAGE_TYPES.contains(message.getMessageType())) { if (message.getUsername() != null && IgnoreList.IGNORED_MESSAGE_TYPES.contains(message.getMessageType())) {
final String serverAddress = SessionHandler.getSession().getServerHostname().orElseGet(() -> ""); final String serverAddress = SessionHandler.getSession().getServerHostname().orElseGet(() -> "");
@ -183,6 +188,7 @@ public class CallbackClientImpl implements CallbackClient {
case GAME_INIT: { case GAME_INIT: {
GamePanel panel = MageFrame.getGame(callback.getObjectId()); GamePanel panel = MageFrame.getGame(callback.getObjectId());
if (panel != null) { if (panel != null) {
appendJsonEvent("GAME_INIT", callback.getObjectId(), (GameView) callback.getData());
panel.init((GameView) callback.getData()); panel.init((GameView) callback.getData());
} }
break; break;
@ -190,6 +196,7 @@ public class CallbackClientImpl implements CallbackClient {
case GAME_OVER: { case GAME_OVER: {
GamePanel panel = MageFrame.getGame(callback.getObjectId()); GamePanel panel = MageFrame.getGame(callback.getObjectId());
if (panel != null) { if (panel != null) {
appendJsonEvent("GAME_OVER", callback.getObjectId(), callback.getData());
panel.endMessage((String) callback.getData(), callback.getMessageId()); panel.endMessage((String) callback.getData(), callback.getMessageId());
} }
break; break;
@ -201,6 +208,7 @@ public class CallbackClientImpl implements CallbackClient {
GameClientMessage message = (GameClientMessage) callback.getData(); GameClientMessage message = (GameClientMessage) callback.getData();
GamePanel panel = MageFrame.getGame(callback.getObjectId()); GamePanel panel = MageFrame.getGame(callback.getObjectId());
if (panel != null) { if (panel != null) {
appendJsonEvent("GAME_ASK", callback.getObjectId(), message);
panel.ask(message.getMessage(), message.getGameView(), callback.getMessageId(), message.getOptions()); panel.ask(message.getMessage(), message.getGameView(), callback.getMessageId(), message.getOptions());
} }
break; break;
@ -208,8 +216,10 @@ public class CallbackClientImpl implements CallbackClient {
case GAME_TARGET: // e.g. Pick triggered ability case GAME_TARGET: // e.g. Pick triggered ability
{ {
GameClientMessage message = (GameClientMessage) callback.getData(); GameClientMessage message = (GameClientMessage) callback.getData();
GamePanel panel = MageFrame.getGame(callback.getObjectId()); GamePanel panel = MageFrame.getGame(callback.getObjectId());
if (panel != null) { if (panel != null) {
appendJsonEvent("GAME_TARGET", callback.getObjectId(), message);
panel.pickTarget(message.getMessage(), message.getCardsView(), message.getGameView(), panel.pickTarget(message.getMessage(), message.getCardsView(), message.getGameView(),
message.getTargets(), message.isFlag(), message.getOptions(), callback.getMessageId()); message.getTargets(), message.isFlag(), message.getOptions(), callback.getMessageId());
} }
@ -217,8 +227,10 @@ public class CallbackClientImpl implements CallbackClient {
} }
case GAME_SELECT: { case GAME_SELECT: {
GameClientMessage message = (GameClientMessage) callback.getData(); GameClientMessage message = (GameClientMessage) callback.getData();
GamePanel panel = MageFrame.getGame(callback.getObjectId()); GamePanel panel = MageFrame.getGame(callback.getObjectId());
if (panel != null) { if (panel != null) {
appendJsonEvent("GAME_SELECT", callback.getObjectId(), message);
panel.select(message.getMessage(), message.getGameView(), callback.getMessageId(), message.getOptions()); panel.select(message.getMessage(), message.getGameView(), callback.getMessageId(), message.getOptions());
} }
break; break;
@ -226,6 +238,7 @@ public class CallbackClientImpl implements CallbackClient {
case GAME_CHOOSE_ABILITY: { case GAME_CHOOSE_ABILITY: {
GamePanel panel = MageFrame.getGame(callback.getObjectId()); GamePanel panel = MageFrame.getGame(callback.getObjectId());
if (panel != null) { if (panel != null) {
appendJsonEvent("GAME_CHOOSE_PILE", callback.getObjectId(), callback.getData());
panel.pickAbility((AbilityPickerView) callback.getData()); panel.pickAbility((AbilityPickerView) callback.getData());
} }
break; break;
@ -234,15 +247,18 @@ public class CallbackClientImpl implements CallbackClient {
GameClientMessage message = (GameClientMessage) callback.getData(); GameClientMessage message = (GameClientMessage) callback.getData();
GamePanel panel = MageFrame.getGame(callback.getObjectId()); GamePanel panel = MageFrame.getGame(callback.getObjectId());
if (panel != null) { if (panel != null) {
appendJsonEvent("GAME_CHOOSE_PILE", callback.getObjectId(), message);
panel.pickPile(message.getMessage(), message.getPile1(), message.getPile2()); panel.pickPile(message.getMessage(), message.getPile1(), message.getPile2());
} }
break; break;
} }
case GAME_CHOOSE_CHOICE: { case GAME_CHOOSE_CHOICE: {
GameClientMessage message = (GameClientMessage) callback.getData(); GameClientMessage message = (GameClientMessage) callback.getData();
GamePanel panel = MageFrame.getGame(callback.getObjectId()); GamePanel panel = MageFrame.getGame(callback.getObjectId());
if (panel != null) { if (panel != null) {
appendJsonEvent("GAME_CHOOSE_CHOICE", callback.getObjectId(), message);
panel.getChoice(message.getChoice(), callback.getObjectId()); panel.getChoice(message.getChoice(), callback.getObjectId());
} }
break; break;
@ -251,34 +267,44 @@ public class CallbackClientImpl implements CallbackClient {
GameClientMessage message = (GameClientMessage) callback.getData(); GameClientMessage message = (GameClientMessage) callback.getData();
GamePanel panel = MageFrame.getGame(callback.getObjectId()); GamePanel panel = MageFrame.getGame(callback.getObjectId());
if (panel != null) { if (panel != null) {
appendJsonEvent("GAME_PLAY_MANA", callback.getObjectId(), message);
panel.playMana(message.getMessage(), message.getGameView(), message.getOptions(), callback.getMessageId()); panel.playMana(message.getMessage(), message.getGameView(), message.getOptions(), callback.getMessageId());
} }
break; break;
} }
case GAME_PLAY_XMANA: { case GAME_PLAY_XMANA: {
GameClientMessage message = (GameClientMessage) callback.getData(); GameClientMessage message = (GameClientMessage) callback.getData();
GamePanel panel = MageFrame.getGame(callback.getObjectId()); GamePanel panel = MageFrame.getGame(callback.getObjectId());
if (panel != null) { if (panel != null) {
appendJsonEvent("GAME_PLAY_XMANA", callback.getObjectId(), message);
panel.playXMana(message.getMessage(), message.getGameView(), callback.getMessageId()); panel.playXMana(message.getMessage(), message.getGameView(), callback.getMessageId());
} }
break; break;
} }
case GAME_GET_AMOUNT: { case GAME_GET_AMOUNT: {
GameClientMessage message = (GameClientMessage) callback.getData(); GameClientMessage message = (GameClientMessage) callback.getData();
GamePanel panel = MageFrame.getGame(callback.getObjectId()); GamePanel panel = MageFrame.getGame(callback.getObjectId());
if (panel != null) { if (panel != null) {
appendJsonEvent("GAME_GET_AMOUNT", callback.getObjectId(), message);
panel.getAmount(message.getMin(), message.getMax(), message.getMessage()); panel.getAmount(message.getMin(), message.getMax(), message.getMessage());
} }
break; break;
} }
case GAME_UPDATE: { case GAME_UPDATE: {
GamePanel panel = MageFrame.getGame(callback.getObjectId()); GamePanel panel = MageFrame.getGame(callback.getObjectId());
if (panel != null) { if (panel != null) {
appendJsonEvent("GAME_UPDATE", callback.getObjectId(), callback.getData());
panel.updateGame((GameView) callback.getData()); panel.updateGame((GameView) callback.getData());
} }
break; break;
} }
case END_GAME_INFO: case END_GAME_INFO:
appendJsonEvent("GAME_OVER", callback.getObjectId(), callback.getData());
MageFrame.getInstance().showGameEndDialog((GameEndView) callback.getData()); MageFrame.getInstance().showGameEndDialog((GameEndView) callback.getData());
break; break;
case SHOW_USERMESSAGE: case SHOW_USERMESSAGE:
@ -293,6 +319,7 @@ public class CallbackClientImpl implements CallbackClient {
GameClientMessage message = (GameClientMessage) callback.getData(); GameClientMessage message = (GameClientMessage) callback.getData();
GamePanel panel = MageFrame.getGame(callback.getObjectId()); GamePanel panel = MageFrame.getGame(callback.getObjectId());
if (panel != null) { if (panel != null) {
appendJsonEvent("GAME_INFORM", callback.getObjectId(), message);
panel.inform(message.getMessage(), message.getGameView(), callback.getMessageId()); panel.inform(message.getMessage(), message.getGameView(), callback.getMessageId());
} }
} }
@ -375,7 +402,12 @@ public class CallbackClientImpl implements CallbackClient {
} }
}); });
} }
private void appendJsonEvent(String name, UUID gameId, Object value) {
Session session = SessionHandler.getSession();
ActionData actionData = new ActionData(name, gameId);
actionData.value = value;
session.appendJsonLog(actionData);
}
private void createChatStartMessage(ChatPanelBasic chatPanel) { private void createChatStartMessage(ChatPanelBasic chatPanel) {
chatPanel.setStartMessageDone(true); chatPanel.setStartMessageDone(true);
ChatPanelBasic usedPanel = chatPanel; ChatPanelBasic usedPanel = chatPanel;

View file

@ -25,6 +25,7 @@
<artifactId>jspf-core</artifactId> <artifactId>jspf-core</artifactId>
<version>0.9.1</version> <version>0.9.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.jboss.remoting</groupId> <groupId>org.jboss.remoting</groupId>
<artifactId>jboss-remoting</artifactId> <artifactId>jboss-remoting</artifactId>
@ -50,7 +51,11 @@
<artifactId>trove</artifactId> <artifactId>trove</artifactId>
<version>1.0.2</version> <version>1.0.2</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
</dependencies> </dependencies>
<!-- to get the reference to local repository with com\googlecode\jspf\jspf-core\0.9.1\ --> <!-- to get the reference to local repository with com\googlecode\jspf\jspf-core\0.9.1\ -->
<repositories> <repositories>

View file

@ -0,0 +1,133 @@
/*
* 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.ExclusionStrategy;
import com.google.gson.FieldAttributes;
public class ActionData {
public UUID gameId;
public String sessionId;
public String type;
public Object value;
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.Replays;
import mage.remote.interfaces.ServerState; import mage.remote.interfaces.ServerState;
import mage.remote.interfaces.Testable; import mage.remote.interfaces.Testable;
import mage.remote.ActionData;
/** /**
* Extracted interface for SessionImpl class. * Extracted interface for SessionImpl class.
@ -45,5 +46,5 @@ import mage.remote.interfaces.Testable;
* @author noxx * @author noxx
*/ */
public interface Session extends ClientData, Connect, GamePlay, GameTypes, ServerState, ChatSession, Feedback, PlayerActions, Replays, Testable { 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.net.*;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; 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.MageException;
import mage.cards.decks.DeckCardLists; import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo; import mage.cards.repository.CardInfo;
@ -50,6 +55,7 @@ import mage.interfaces.callback.ClientCallback;
import mage.players.PlayerType; import mage.players.PlayerType;
import mage.players.net.UserData; import mage.players.net.UserData;
import mage.utils.CompressUtil; import mage.utils.CompressUtil;
import mage.remote.ActionData;
import mage.view.*; import mage.view.*;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jboss.remoting.*; import org.jboss.remoting.*;
@ -798,6 +804,9 @@ public class SessionImpl implements Session {
public boolean sendPlayerUUID(UUID gameId, UUID data) { public boolean sendPlayerUUID(UUID gameId, UUID data) {
try { try {
if (isConnected()) { if (isConnected()) {
ActionData actionData = new ActionData("SEND_PLAYER_UUID", gameId, getSessionId());
actionData.value = data;
appendJsonLog(actionData);
server.sendPlayerUUID(gameId, sessionId, data); server.sendPlayerUUID(gameId, sessionId, data);
return true; return true;
} }
@ -813,6 +822,10 @@ public class SessionImpl implements Session {
public boolean sendPlayerBoolean(UUID gameId, boolean data) { public boolean sendPlayerBoolean(UUID gameId, boolean data) {
try { try {
if (isConnected()) { if (isConnected()) {
ActionData actionData = new ActionData("SEND_PLAYER_BOOLEAN", gameId, getSessionId());
actionData.value = data;
appendJsonLog(actionData);
server.sendPlayerBoolean(gameId, sessionId, data); server.sendPlayerBoolean(gameId, sessionId, data);
return true; return true;
} }
@ -828,6 +841,10 @@ public class SessionImpl implements Session {
public boolean sendPlayerInteger(UUID gameId, int data) { public boolean sendPlayerInteger(UUID gameId, int data) {
try { try {
if (isConnected()) { if (isConnected()) {
ActionData actionData = new ActionData("SEND_PLAYER_INTEGER", gameId, getSessionId());
actionData.value = data;
appendJsonLog(actionData);
server.sendPlayerInteger(gameId, sessionId, data); server.sendPlayerInteger(gameId, sessionId, data);
return true; return true;
} }
@ -843,6 +860,10 @@ public class SessionImpl implements Session {
public boolean sendPlayerString(UUID gameId, String data) { public boolean sendPlayerString(UUID gameId, String data) {
try { try {
if (isConnected()) { if (isConnected()) {
ActionData actionData = new ActionData("SEND_PLAYER_STRING", gameId, getSessionId());
actionData.value = data;
appendJsonLog(actionData);
server.sendPlayerString(gameId, sessionId, data); server.sendPlayerString(gameId, sessionId, data);
return true; return true;
} }
@ -858,6 +879,9 @@ public class SessionImpl implements Session {
public boolean sendPlayerManaType(UUID gameId, UUID playerId, ManaType data) { public boolean sendPlayerManaType(UUID gameId, UUID playerId, ManaType data) {
try { try {
if (isConnected()) { if (isConnected()) {
ActionData actionData = new ActionData("SEND_PLAYER_MANA_TYPE", gameId, getSessionId());
actionData.value = data;
appendJsonLog(actionData);
server.sendPlayerManaType(gameId, playerId, sessionId, data); server.sendPlayerManaType(gameId, playerId, sessionId, data);
return true; return true;
} }
@ -869,6 +893,19 @@ public class SessionImpl implements Session {
return false; 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 @Override
public DraftPickView sendCardPick(UUID draftId, UUID cardId, Set<UUID> hiddenCards) { public DraftPickView sendCardPick(UUID draftId, UUID cardId, Set<UUID> hiddenCards) {
try { try {
@ -1274,6 +1311,11 @@ public class SessionImpl implements Session {
public boolean sendPlayerAction(PlayerAction passPriorityAction, UUID gameId, Object data) { public boolean sendPlayerAction(PlayerAction passPriorityAction, UUID gameId, Object data) {
try { try {
if (isConnected()) { if (isConnected()) {
ActionData actionData = new ActionData("SEND_PLAYER_ACTION", gameId, getSessionId());
actionData.value = data;
appendJsonLog(actionData);
server.sendPlayerAction(passPriorityAction, gameId, sessionId, data); server.sendPlayerAction(passPriorityAction, gameId, sessionId, data);
return true; return true;
} }

View file

@ -32,6 +32,9 @@ import java.io.Serializable;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import mage.choices.Choice; import mage.choices.Choice;
/** /**
@ -155,4 +158,11 @@ public class GameClientMessage implements Serializable {
return choice; return choice;
} }
public String toJson() {
Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.create();
return gson.toJson(this);
}
} }

View file

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