# Path: srs/match_stats.py
# class MatchStatsGeneration(object):
#     def __init__(self, file_path):
#         self.file_path = os.path.abspath(file_path)
#         self.demofile = DemoFileReader(self.file_path)
# 
#     def make_teamstat_graphs(cls, teamstats):
#         # type: (Dict[str, List[TeamStatistics]]) -> Dict[str, Dict[str, List[float]]]
#         res = OrderedDict()  # type: Dict
#         for ts_attr, label in TeamStats.graphid2label.items():
#             res[ts_attr] = dict((player, [getattr(s, ts_attr) for s in stats]) for player, stats in teamstats.items())
#         return res
# 
#     @staticmethod
#     def checkteamstatlength(demofile):  # type: (DemoFileReader) -> bool
#         """
#         Copied from SpringStatsViewer/SpringStatsViewer.py to not have to depend on Tix.
# 
#         Return true if the length of at least one entry in the demo file teamstats > 1, false otherwise
#         """
#         vlength = 0
#         for p in demofile.teamstatistics:
#             if len(demofile.teamstatistics[p]) > vlength:
#                 vlength = len(demofile.teamstatistics[p])
#                 if vlength > 1:
#                     return True
#         if vlength > 1:
#             return True
#         else:
#             return False
# 
#     def make_stats(self):
#         # type: () -> Tuple[Dict[str, Dict[str, int]], Dict[str, List[Dict[str, Union[str, List[float]]]]]]
#         playerstats_res = {}  # type: Dict[str, Dict[str, int]]
#         teamstats_plotly_kwargs = {}  # type: Dict[str, List[Dict[str, Union[str, List[float]]]]]
#         if (
#             self.demofile.header()
#             and self.demofile.script()
#             and not self.demofile.incomplete
#             and not self.demofile.crashed
#         ):
#             playerstats = self.demofile.playerstats()
#             if playerstats:
#                 logger.debug("** We have playerstats: {!r}".format(playerstats))
#                 playerstats_res = dict((k, v.__dict__) for k, v in playerstats.items())
#                 teamstats = self.demofile.teamstats()
#                 if teamstats and self.checkteamstatlength(self.demofile):
#                     logger.debug("** We have teamstats.")
#                     graphs = self.make_teamstat_graphs(teamstats)
#                     # create x-axis units
#                     teamstatperiod_in_minutes = self.demofile.teamstatperiod / 60.0
#                     x_axis = [0.0]
#                     a_stat = graphs.values()[0]  # type: Dict[str, List[float]]
#                     a_stat_list = a_stat.values()[0]  # type: List[float]
#                     for i in range(len(a_stat_list) - 1):
#                         x_axis.append(x_axis[-1] + teamstatperiod_in_minutes)
#                     for ts_type, stats in graphs.items():
#                         plot_data_kwargs = [
#                             dict(x=x_axis, y=p_stats, name=player) for player, p_stats in stats.items()
#                         ]  # type: List[Dict[str, Union[str, List[float]]]]
#                         teamstats_plotly_kwargs[ts_type] = plot_data_kwargs
#                 else:
#                     logger.debug("** We have NO teamstats.")
#             else:
#                 logger.debug("** We have NO playerstats.")
#         return playerstats_res, teamstats_plotly_kwargs
# 
#     @staticmethod
#     def get_team_stat_plot(title, plotly_kwargs, graph_type="lines"):
#         # type: (str, List[Dict[str, Union[str, List[float]]]], Optional[str]) -> str
#         plot_data = [go.Scatter(mode=graph_type, **kwargs) for kwargs in plotly_kwargs]
#         layout = go.Layout(
#             title=title,
#             autosize=True,
#             # paper_bgcolor='#1c1e22',
#             # plot_bgcolor='#2e3338',
#             # legend = {'bgcolor': '#2e3338'},
#             xaxis={"title": "minute"},
#             yaxis={"title": "per second"},
#         )
#         return plotly.offline.plot(
#             figure_or_data=go.Figure(data=plot_data, layout=layout),
#             auto_open=False,
#             output_type="div",
#             include_plotlyjs=False,
#             show_link=False,
#         )


import gzip
import logging
import pprint
import re
import struct
import sys
import threading
import zlib
import magic
import ujson
from datetime import timedelta
from struct import unpack
from time import localtime, strftime
from typing import Union
from .match_stats import MatchStatsGeneration
    from django.conf import settings
    from .demoparser import Demoparser
    from .script import (
        Script,
        ScriptAI,
        ScriptAlly,
        ScriptGamesetup,
        ScriptMapoptions,
        ScriptModoptions,
        ScriptPlayer,
        ScriptRestrictions,
        ScriptTeam,
    )
    from ..spring_replay_site.settings import DEBUG
    from .demoparser import Demoparser
    from .script import (
        Script,
        ScriptAI,
        ScriptAlly,
        ScriptGamesetup,
        ScriptMapoptions,
        ScriptModoptions,
        ScriptPlayer,
        ScriptRestrictions,
        ScriptTeam,
    )
#  *         sum of the elements in the array of dwords).
#  *
#  * The header is designed to be extensible: it contains a version field and a
#  * headerSize field to support this. The version field is a major version number
#  * of the file format, if this changes, anything may have changed in the file.
#  * It is not supposed to change often (if at all). The headerSize field is a
#  * minor version number, which happens to be equal to sizeof(DemoFileHeader).
#  *
#  * If Spring did not cleanup properly (crashed), the demoStreamSize is 0 and it
#  * can be assumed the demo stream continues until the end of the file.
#
#
#  * The demo stream layout is as follows:
#  *
#  * - DemoStreamChunkHeader
#  * - length bytes raw data from network stream
#  * - DemoStreamChunkHeader
#  * - length bytes raw data from network stream
#  * - ...
#


class ParseError(Exception):
    pass


class BadFileType(ParseError):
    pass



# Path: srs/match_stats.py
# class MatchStatsGeneration(object):
#     def __init__(self, file_path):
#         self.file_path = os.path.abspath(file_path)
#         self.demofile = DemoFileReader(self.file_path)
# 
#     def make_teamstat_graphs(cls, teamstats):
#         # type: (Dict[str, List[TeamStatistics]]) -> Dict[str, Dict[str, List[float]]]
#         res = OrderedDict()  # type: Dict
#         for ts_attr, label in TeamStats.graphid2label.items():
#             res[ts_attr] = dict((player, [getattr(s, ts_attr) for s in stats]) for player, stats in teamstats.items())
#         return res
# 
#     @staticmethod
#     def checkteamstatlength(demofile):  # type: (DemoFileReader) -> bool
#         """
#         Copied from SpringStatsViewer/SpringStatsViewer.py to not have to depend on Tix.
# 
#         Return true if the length of at least one entry in the demo file teamstats > 1, false otherwise
#         """
#         vlength = 0
#         for p in demofile.teamstatistics:
#             if len(demofile.teamstatistics[p]) > vlength:
#                 vlength = len(demofile.teamstatistics[p])
#                 if vlength > 1:
#                     return True
#         if vlength > 1:
#             return True
#         else:
#             return False
# 
#     def make_stats(self):
#         # type: () -> Tuple[Dict[str, Dict[str, int]], Dict[str, List[Dict[str, Union[str, List[float]]]]]]
#         playerstats_res = {}  # type: Dict[str, Dict[str, int]]
#         teamstats_plotly_kwargs = {}  # type: Dict[str, List[Dict[str, Union[str, List[float]]]]]
#         if (
#             self.demofile.header()
#             and self.demofile.script()
#             and not self.demofile.incomplete
#             and not self.demofile.crashed
#         ):
#             playerstats = self.demofile.playerstats()
#             if playerstats:
#                 logger.debug("** We have playerstats: {!r}".format(playerstats))
#                 playerstats_res = dict((k, v.__dict__) for k, v in playerstats.items())
#                 teamstats = self.demofile.teamstats()
#                 if teamstats and self.checkteamstatlength(self.demofile):
#                     logger.debug("** We have teamstats.")
#                     graphs = self.make_teamstat_graphs(teamstats)
#                     # create x-axis units
#                     teamstatperiod_in_minutes = self.demofile.teamstatperiod / 60.0
#                     x_axis = [0.0]
#                     a_stat = graphs.values()[0]  # type: Dict[str, List[float]]
#                     a_stat_list = a_stat.values()[0]  # type: List[float]
#                     for i in range(len(a_stat_list) - 1):
#                         x_axis.append(x_axis[-1] + teamstatperiod_in_minutes)
#                     for ts_type, stats in graphs.items():
#                         plot_data_kwargs = [
#                             dict(x=x_axis, y=p_stats, name=player) for player, p_stats in stats.items()
#                         ]  # type: List[Dict[str, Union[str, List[float]]]]
#                         teamstats_plotly_kwargs[ts_type] = plot_data_kwargs
#                 else:
#                     logger.debug("** We have NO teamstats.")
#             else:
#                 logger.debug("** We have NO playerstats.")
#         return playerstats_res, teamstats_plotly_kwargs
# 
#     @staticmethod
#     def get_team_stat_plot(title, plotly_kwargs, graph_type="lines"):
#         # type: (str, List[Dict[str, Union[str, List[float]]]], Optional[str]) -> str
#         plot_data = [go.Scatter(mode=graph_type, **kwargs) for kwargs in plotly_kwargs]
#         layout = go.Layout(
#             title=title,
#             autosize=True,
#             # paper_bgcolor='#1c1e22',
#             # plot_bgcolor='#2e3338',
#             # legend = {'bgcolor': '#2e3338'},
#             xaxis={"title": "minute"},
#             yaxis={"title": "per second"},
#         )
#         return plotly.offline.plot(
#             figure_or_data=go.Figure(data=plot_data, layout=layout),
#             auto_open=False,
#             output_type="div",
#             include_plotlyjs=False,
#             show_link=False,
#         )


# Path: srs/parse_demo_file.py
import gzip
import logging
import pprint
import re
import struct
import sys
import threading
import zlib
import magic
import ujson
from datetime import timedelta
from struct import unpack
from time import localtime, strftime
from typing import Union
from .match_stats import MatchStatsGeneration
    from django.conf import settings
    from .demoparser import Demoparser
    from .script import (
        Script,
        ScriptAI,
        ScriptAlly,
        ScriptGamesetup,
        ScriptMapoptions,
        ScriptModoptions,
        ScriptPlayer,
        ScriptRestrictions,
        ScriptTeam,
    )
    from ..spring_replay_site.settings import DEBUG
    from .demoparser import Demoparser
    from .script import (
        Script,
        ScriptAI,
        ScriptAlly,
        ScriptGamesetup,
        ScriptMapoptions,
        ScriptModoptions,
        ScriptPlayer,
        ScriptRestrictions,
        ScriptTeam,
    )

#  *         sum of the elements in the array of dwords).
#  *
#  * The header is designed to be extensible: it contains a version field and a
#  * headerSize field to support this. The version field is a major version number
#  * of the file format, if this changes, anything may have changed in the file.
#  * It is not supposed to change often (if at all). The headerSize field is a
#  * minor version number, which happens to be equal to sizeof(DemoFileHeader).
#  *
#  * If Spring did not cleanup properly (crashed), the demoStreamSize is 0 and it
#  * can be assumed the demo stream continues until the end of the file.
#
#
#  * The demo stream layout is as follows:
#  *
#  * - DemoStreamChunkHeader
#  * - length bytes raw data from network stream
#  * - DemoStreamChunkHeader
#  * - length bytes raw data from network stream
#  * - ...
#


class ParseError(Exception):
    pass


class BadFileType(ParseError):
    pass



class Parse_demo_file: