# Path: whyattend/model.py
# def init_db():
#     def __init__(self, wot_id, openid, member_since, name, clan, role, locked=False):
#     def is_recruit(self):
#     def battles_played(self):
#     def battles_reserve(self):
#     def __repr__(self):
#     def to_dict(self):
#     def player_role_value(self):
#     def __init__(self, player, battle, reserve=False):
#     def __init__(self, date, clan, enemy_clan, victory, draw, creator, battle_commander, map_name, map_province,
#                  duration, description='', paid=False):
#     def outcome_str(self):
#     def outcome_repr(self):
#     def has_player(self, player):
#     def has_reserve(self, player):
#     def get_players(self):
#     def get_reserve_players(self):
#     def __str__(self):
#     def __init__(self, title, description, clan, date):
#     def get_players(self):
#     def get_reserves(self):
#     def get_final_battle(self):
#     def get_representative_battle(self):
#     def __init__(self, replay_blob, replay_pickle):
#     def unpickle(self):
#     def get(cls):
# class Player(Base):
# class BattleAttendance(Base):
# class Battle(Base):
# class BattleGroup(Base):
# class Replay(Base):
# class WebappData(Base):
# 
# Path: whyattend/config.py
# SERVER_TIMEZONE = 'Europe/Amsterdam'
# USER_TIMEZONES = ('Europe/Amsterdam',
#                   'Europe/London',
#                   'Europe/Helsinki')
# DATABASE_URI = 'mysql://user@host/database?charset=utf8&use_unicode=0'  # forces UTF-8 encoding in DB
# OID_STORE_PATH = 'tmp/oid'
# SECRET_KEY = ''
# CLAN_NAMES = ['CLAN', 'STRONK']
# CLAN_IDS = {
#     'CLAN': '23456789',
#     'STRONK': '34567890'
# }
# UPLOAD_FOLDER = 'tmp/uploads'
# API_URL = 'http://api.worldoftanks.eu/'
# API_TOKEN = ''
# WOT_SERVER_REGION_CODE = 'eu'
# API_KEY = 'mysecretkey'
# ROLE_LABELS = {
#     "personnel_officer": "Personnel Officer",
#     "quartermaster": "Quartermaster",
#     "executive_officer": "Executive Officer",
#     "recruit": "Recruit",
#     "private": "Private",
#     "commander": "Commander",
#     "reservist": "Reservist",
#     "combat_officer": "Combat Officer",
#     "recruitment_officer": "Recruitment Officer",
#     "intelligence_officer": "Intelligence Officer",
#     "junior_officer": "Junior Officer",
# }
# ADMINS = ['fantastico', ]
# CREATE_BATTLE_ROLES = ('commander', 'executive_officer', 'quartermaster')
# DELETE_BATTLE_ROLES = ('commander', 'executive_officer', 'quartermaster')
# PAYOUT_ROLES = ('commander', 'executive_officer', 'quartermaster')
# ADMIN_ROLES = ('commander', )
# PLAYER_PERFORMANCE_ROLES = ('commander', )
# COMMANDED_ROLES = ('commander', 'executive_officer')
# DOWNLOAD_REPLAY_ROLES = ('commander', 'personnel_officer', 'executive_officer', 'private',
#                          'combat_officer', 'recruitment_officer', 'intelligence_officer', 'quartermaster')
# RESERVE_SIGNUP_DURATION = datetime.timedelta(days=7)
# RESERVE_SIGNUP_ALLOWED = False
# STORE_REPLAYS_IN_DB = True
# ERROR_LOG_FILE = '/tmp/error.log'
# LOG_FILE = '/tmp/whyattend.log'
# CELERY_BROKER_URL = 'redis://localhost:6379'
# CELERY_RESULT_BACKEND = 'redis://localhost:6379'
# MENU_LINKS = [
#     ('Clan Forum', '#forum'),
#     ('Global Map', 'http://worldoftanks.eu/clanwars/maps/globalmap/'),
#     ('Campaign Map', 'http://worldoftanks.eu/clanwars/eventmap/'),
# ]
# MAP_URL = "http://worldoftanks.eu/clanwars/maps/globalmap/"
# MAP_SUBDOMAIN = "http://cw1.worldoftanks.eu"
# MAP_REGIONS = [1, 2, 3]  # 3 map regions with IDs 1,2,3 in EU
# STATISTICS_VISIBLE = {
#     'win_rate_by_commander': True,
# }
# API_REQUEST_TIMEOUT = 30
# 
# Path: whyattend/replays.py
# def parse_replay(replay_blob):
# def players_list(replay_json, team):
# def player_won(replay_json):
# def get_own_team(replay_json):
# def player_team(replay_json):
# def is_stronghold(replay_json):
# def is_cw(replay_json):
# def guess_clan(replay_json):
# def guess_enemy_clan(replay_json):
# def score(replay_json):
# def resources_earned(json_second, player_id):
# def player_performance(json_second, vehicles, players):


import os, datetime
from whyattend.model import db_session, Battle
from whyattend import config, replays
""" Replay import
    ~~~~~~~~~~~~~

    Import replays from the webapp upload folder into DB by matching
    them against the battles present in the database. The script does not
    overwrite existing replay blobs in the database.

    The whyattend package and (local_)config.py have to be in the PYTHONPATH.
"""



if __name__ == '__main__':
    for root, subfolders, files in os.walk(config.UPLOAD_FOLDER):
        for file in files:
            try:
                replay_blob = open(os.path.join(root, file), 'rb').read()

# Path: whyattend/model.py
# def init_db():
#     def __init__(self, wot_id, openid, member_since, name, clan, role, locked=False):
#     def is_recruit(self):
#     def battles_played(self):
#     def battles_reserve(self):
#     def __repr__(self):
#     def to_dict(self):
#     def player_role_value(self):
#     def __init__(self, player, battle, reserve=False):
#     def __init__(self, date, clan, enemy_clan, victory, draw, creator, battle_commander, map_name, map_province,
#                  duration, description='', paid=False):
#     def outcome_str(self):
#     def outcome_repr(self):
#     def has_player(self, player):
#     def has_reserve(self, player):
#     def get_players(self):
#     def get_reserve_players(self):
#     def __str__(self):
#     def __init__(self, title, description, clan, date):
#     def get_players(self):
#     def get_reserves(self):
#     def get_final_battle(self):
#     def get_representative_battle(self):
#     def __init__(self, replay_blob, replay_pickle):
#     def unpickle(self):
#     def get(cls):
# class Player(Base):
# class BattleAttendance(Base):
# class Battle(Base):
# class BattleGroup(Base):
# class Replay(Base):
# class WebappData(Base):
# 
# Path: whyattend/config.py
# SERVER_TIMEZONE = 'Europe/Amsterdam'
# USER_TIMEZONES = ('Europe/Amsterdam',
#                   'Europe/London',
#                   'Europe/Helsinki')
# DATABASE_URI = 'mysql://user@host/database?charset=utf8&use_unicode=0'  # forces UTF-8 encoding in DB
# OID_STORE_PATH = 'tmp/oid'
# SECRET_KEY = ''
# CLAN_NAMES = ['CLAN', 'STRONK']
# CLAN_IDS = {
#     'CLAN': '23456789',
#     'STRONK': '34567890'
# }
# UPLOAD_FOLDER = 'tmp/uploads'
# API_URL = 'http://api.worldoftanks.eu/'
# API_TOKEN = ''
# WOT_SERVER_REGION_CODE = 'eu'
# API_KEY = 'mysecretkey'
# ROLE_LABELS = {
#     "personnel_officer": "Personnel Officer",
#     "quartermaster": "Quartermaster",
#     "executive_officer": "Executive Officer",
#     "recruit": "Recruit",
#     "private": "Private",
#     "commander": "Commander",
#     "reservist": "Reservist",
#     "combat_officer": "Combat Officer",
#     "recruitment_officer": "Recruitment Officer",
#     "intelligence_officer": "Intelligence Officer",
#     "junior_officer": "Junior Officer",
# }
# ADMINS = ['fantastico', ]
# CREATE_BATTLE_ROLES = ('commander', 'executive_officer', 'quartermaster')
# DELETE_BATTLE_ROLES = ('commander', 'executive_officer', 'quartermaster')
# PAYOUT_ROLES = ('commander', 'executive_officer', 'quartermaster')
# ADMIN_ROLES = ('commander', )
# PLAYER_PERFORMANCE_ROLES = ('commander', )
# COMMANDED_ROLES = ('commander', 'executive_officer')
# DOWNLOAD_REPLAY_ROLES = ('commander', 'personnel_officer', 'executive_officer', 'private',
#                          'combat_officer', 'recruitment_officer', 'intelligence_officer', 'quartermaster')
# RESERVE_SIGNUP_DURATION = datetime.timedelta(days=7)
# RESERVE_SIGNUP_ALLOWED = False
# STORE_REPLAYS_IN_DB = True
# ERROR_LOG_FILE = '/tmp/error.log'
# LOG_FILE = '/tmp/whyattend.log'
# CELERY_BROKER_URL = 'redis://localhost:6379'
# CELERY_RESULT_BACKEND = 'redis://localhost:6379'
# MENU_LINKS = [
#     ('Clan Forum', '#forum'),
#     ('Global Map', 'http://worldoftanks.eu/clanwars/maps/globalmap/'),
#     ('Campaign Map', 'http://worldoftanks.eu/clanwars/eventmap/'),
# ]
# MAP_URL = "http://worldoftanks.eu/clanwars/maps/globalmap/"
# MAP_SUBDOMAIN = "http://cw1.worldoftanks.eu"
# MAP_REGIONS = [1, 2, 3]  # 3 map regions with IDs 1,2,3 in EU
# STATISTICS_VISIBLE = {
#     'win_rate_by_commander': True,
# }
# API_REQUEST_TIMEOUT = 30
# 
# Path: whyattend/replays.py
# def parse_replay(replay_blob):
# def players_list(replay_json, team):
# def player_won(replay_json):
# def get_own_team(replay_json):
# def player_team(replay_json):
# def is_stronghold(replay_json):
# def is_cw(replay_json):
# def guess_clan(replay_json):
# def guess_enemy_clan(replay_json):
# def score(replay_json):
# def resources_earned(json_second, player_id):
# def player_performance(json_second, vehicles, players):


# Path: scripts/import_replays.py
import os, datetime
from whyattend.model import db_session, Battle
from whyattend import config, replays

""" Replay import
    ~~~~~~~~~~~~~

    Import replays from the webapp upload folder into DB by matching
    them against the battles present in the database. The script does not
    overwrite existing replay blobs in the database.

    The whyattend package and (local_)config.py have to be in the PYTHONPATH.
"""



if __name__ == '__main__':
    for root, subfolders, files in os.walk(config.UPLOAD_FOLDER):
        for file in files:
            try:
                replay_blob = open(os.path.join(root, file), 'rb').read()

                replay = replays.parse_replay(replay_blob)