# -*- coding: utf-8 -*-
import os
import re
import time
import json
import pprint
import hashlib
import random
import http.client
import urllib.parse

GAMELOG_HOST = 'gamelog-dev.the7ys.com'

log_dict = json.loads(open('dict.json', 'r').read())
str_keys = {'str' + str(i) : True for i in range(1, 40)}
num_keys = {'num' + str(i) : True for i in range(1, 40)}

def genTestData(node):
    data = {}
    for key in node['__keys__']:
        if key in str_keys:
            data[key] = 'abcdef'
        elif key in num_keys:
            data[key] = 12345
        else:
            data[key] = 'xxxxx'
    return data

def md5Str(text):
    m1 = hashlib.md5()
    m1.update(text.encode('utf-8'))
    return m1.hexdigest()

def createSessionId(accountid, register_time, session_key):
    nowtime = register_time
    return nowtime + '_' + register_time + '_' + \
        md5Str(accountid + 'f3a6a9a5-217a-4079-ab99-b5d69b8212be' + register_time + nowtime) + '_' + \
        md5Str('f3a6a9a5-217a-4079-ab99-b5d69b8212be' + accountid + session_key)

def testReportUser():
    for key in range(1, 1000):
        accountid = str(int(time.time()))
        url = '/webapp/index.php?c=GameLog&a=reportUser&' + '&'.join([
            'gameid=' + '1004',
            'channel=' + '6000',
            'account_id=' + accountid,
            'session_id=' + createSessionId(accountid, accountid, 'abdef'),
            'access_token=' + '',
            'from_appid=' + 'from_appid',
        ])
        post_body = {
            'set': {'field' + str(i) : i for i in range(1, 40)},
            'set_once': {'once' + str(i) : i for i in range(1, 40)},
            'add': {'add' + str(i) : i for i in range(1, 40)},
        }

        conn = http.client.HTTPConnection(GAMELOG_HOST)
        conn.request("POST", url, json.dumps(post_body))
        data = conn.getresponse()
#        print(data.read().decode('utf-8'))
        print(json.loads(data.read().decode('utf-8')))

def testReportStat():
    for key in range(1, 1000):
        accountid = str(int(time.time()))
        url = '/webapp/index.php?c=GameLog&a=reportStat&' + '&'.join([
            'gameid=' + '1004',
            'channel=' + '6000',
            'account_id=' + accountid,
            'session_id=' + createSessionId(accountid, accountid, 'abdef'),
            'access_token=' + '',
            'from_appid=' + 'from_appid',
            'stat_type=' + 'y',
        ])
        post_body = {
            'add': {'add' + str(i) : i for i in range(1, 40)},
        }

        conn = http.client.HTTPConnection(GAMELOG_HOST)
        conn.request("POST", url, json.dumps(post_body))
        data = conn.getresponse()
#        print(data.read().decode('utf-8'))
        print(json.loads(data.read().decode('utf-8')))

def testReportLog():
    for key in log_dict['__keys__']:
        node = log_dict[key]
        strings = key.split('-')
        logclass1 = strings[0]
        logclass2 = strings[1]
        accountid = str(int(time.time()))
        url = '/webapp/index.php?c=GameLog&a=reportLog&' + '&'.join([
            'gameid=' + '1004',
            'channel=' + '6000',
            'localuuid=' + accountid,
            'account_id=' + accountid,
            'session_id=' + createSessionId(accountid, accountid, 'abdef'),
            'access_token=' + '',
            'from_appid=' + 'from_appid',
            'logclass1=' + logclass1,
            'logclass2=' + logclass2
        ])
        post_body = genTestData(node)

        conn = http.client.HTTPConnection(GAMELOG_HOST)
        conn.request("POST", url, json.dumps(post_body))
        data = conn.getresponse()
#        print(data.read().decode('utf-8'))
        print(json.loads(data.read().decode('utf-8')))

#testReportLog()
#testReportUser()
testReportStat()
