{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# links\n",
    "https://brotherbored.com/guest-post-is-the-diplomacy-map-unbalanced/\n",
    "http://ukdp.diplomatic-pouch.com/pouch/Zine/F2007R/Burton/statistician3.htm\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = \"/data/oam_patel/AI_Diplomacy/results/20250227_004209/\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 141492 characters]",
      "text/plain": [
       "<Figure size 600x800 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import json\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "# Set Seaborn style to \"white\"\n",
    "sns.set_style(\"white\")\n",
    "\n",
    "# Load game data (adjust the filename as needed)\n",
    "with open(path + 'lmvsgame.json', \"r\") as f:\n",
    "    game_data = json.load(f)\n",
    "\n",
    "# Define the list of countries\n",
    "countries = [\"AUSTRIA\", \"ENGLAND\", \"FRANCE\", \"GERMANY\", \"ITALY\", \"RUSSIA\", \"TURKEY\"]\n",
    "\n",
    "# load jsonl\n",
    "with open(path + 'overview.jsonl', \"r\") as f:\n",
    "    overview = [json.loads(line) for line in f]\n",
    "\n",
    "model_map = overview[1]\n",
    "\n",
    "# Prepare containers for our statistics over turns\n",
    "turns = []  # List of phase names\n",
    "unit_counts = {country: [] for country in countries}\n",
    "center_counts = {country: [] for country in countries}\n",
    "conflict_counts = []  # Global statistic per turn\n",
    "\n",
    "# Iterate over each phase (turn) in the game\n",
    "for phase in game_data.get(\"phases\", []):\n",
    "    turn_name = phase.get(\"name\", \"Unknown\")\n",
    "    turns.append(turn_name)\n",
    "\n",
    "    state = phase.get(\"state\", {})\n",
    "\n",
    "    # Extract unit and center counts per country\n",
    "    for country in countries:\n",
    "        units = state.get(\"units\", {}).get(country, [])\n",
    "        centers = state.get(\"centers\", {}).get(country, [])\n",
    "        unit_counts[country].append(len(units))\n",
    "        center_counts[country].append(len(centers))\n",
    "\n",
    "    # Count conflicts: orders that resulted in a 'bounce'\n",
    "    results = phase.get(\"results\", {})\n",
    "    conflicts = sum(1 for outcome in results.values() if \"bounce\" in outcome)\n",
    "    conflict_counts.append(conflicts)\n",
    "\n",
    "# Create subplots for the three statistics\n",
    "fig, axs = plt.subplots(3, 1, figsize=(6, 8), constrained_layout=True)\n",
    "\n",
    "# Plot unit counts per country\n",
    "for country in countries:\n",
    "    axs[0].plot(turns, unit_counts[country], label=model_map[country])\n",
    "axs[0].set_title(\"Unit Counts per Country Over Turns\")\n",
    "axs[0].set_ylabel(\"Number of Units\")\n",
    "axs[0].set_xlabel(\"Turns\")\n",
    "axs[0].legend()\n",
    "sns.despine(ax=axs[0], top=True, right=True)\n",
    "axs[0].set_xticklabels([])  # Hide x-axis labels\n",
    "\n",
    "# Plot center counts per country\n",
    "for country in countries:\n",
    "    axs[1].plot(turns, center_counts[country], label=model_map[country])\n",
    "axs[1].set_title(\"Center Counts per Country Over Turns\")\n",
    "axs[1].set_ylabel(\"Number of Centers\")\n",
    "axs[1].set_xlabel(\"Turns\")\n",
    "axs[1].legend()\n",
    "sns.despine(ax=axs[1], top=True, right=True)\n",
    "axs[1].set_xticklabels([])  # Hide x-axis labels\n",
    "\n",
    "# Plot global conflict counts\n",
    "axs[2].plot(turns, conflict_counts, color=\"red\")\n",
    "axs[2].set_title(\"Global Bounces Over Turns\")\n",
    "axs[2].set_ylabel(\"Number of Conflicts\")\n",
    "axs[2].set_xlabel(\"Turns\")\n",
    "sns.despine(ax=axs[2], top=True, right=True)\n",
    "axs[2].set_xticklabels([])  # Hide x-axis labels\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "diplomo",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
