import requests
from datetime import datetime, timedelta
from threading import Thread
import os,time
from random import randrange


def generateGames():
	os.environ['TZ'] = 'Asia/Shanghai'
	time.tzset()
	apiURL = f'https://www.alcodetailing.com/vcapi/CreateGameRecord.php'
	timeNow = datetime.now()
	timeNext = datetime.now() + timedelta(hours=1)
	game1 = timeNow.strftime('%Y-%m-%d %H:10:00')
	game1end = timeNow.strftime('%Y-%m-%d %H:20:00')

	game2 = timeNow.strftime('%Y-%m-%d %H:20:00')
	game2end = timeNow.strftime('%Y-%m-%d %H:30:00')

	game3 = timeNow.strftime('%Y-%m-%d %H:30:00')
	game3end = timeNow.strftime('%Y-%m-%d %H:40:00')

	game4 = timeNow.strftime('%Y-%m-%d %H:40:00')
	game4end = timeNow.strftime('%Y-%m-%d %H:50:00')

	game5 = timeNow.strftime('%Y-%m-%d %H:50:00')
	game5end = timeNext.strftime('%Y-%m-%d %H:00:00')

	game6 = timeNext.strftime('%Y-%m-%d %H:00:00')
	game6end = timeNext.strftime('%Y-%m-%d %H:10:00')


	startAry = [game1,game2,game3,game4,game5,game6]
	endAry = [game1end,game2end,game3end,game4end,game5end,game6end]
	current_timestamp = int(time.time())
	length = len(str(current_timestamp))
	str2 = str(current_timestamp)[length - 5:]
	intStr = int(str2)
	for x in range(12):
		print("gameId: "+str(intStr+x))
		print("start: "+str(startAry[x])+" end: "+str(endAry[x]))
		n1 = randrange(10)
		n2 = randrange(10)
		n3 = randrange(10)
		finalValue = n1 + n2 + n3
		print("value: "+str(n1)+"+"+str(n2)+"+"+str(n3)+"+"+str(finalValue))
		try:
			print(str(intStr+x))
			response = requests.post(apiURL, data={'id_show': str(intStr+x), 'create_time': str(startAry[x]), 'end_time': str(endAry[x]), 'n1number': str(n1), 'n2number': str(n2), 'n3number': str(n3), 'n4number': str(finalValue), 'game_time': '10'})
			print(response.content)
		except Exception as e:
			print(e)


def checkTime():
	while True:
		os.environ['TZ'] = 'Asia/Shanghai'
		time.tzset()
		timeNow = datetime.now()
		print(timeNow.strftime('%Y-%m-%d %H:%M:%S'))
		if ":00:00" in timeNow.strftime('%Y-%m-%d %H:%M:%S'):
			generateGames()
		time.sleep(1)


if __name__ == '__main__':
	checkTime()

#def checkTime(t):
	#while t:
		#mins,secs = divmod(t, 60)
		#timer = '{:02d}:{:02d}'.format(mins, secs)
		#print(timer, end="\r")
		#time.sleep(1)
		#t -= 1


#if __name__ == '__main__':
	#checkTime(10)