forked from tylerxprice/hive-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.py
138 lines (108 loc) · 4.87 KB
/
scheduler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import argparse
import json
import logging
import os
import shlex
import sys
from time import time
from datetime import datetime
from django.conf import settings
settings.configure(DEBUG=True, DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(os.path.join(os.path.dirname(__file__), 'website'), 'tournaments.sqlite3'),
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}},
MEDIA_ROOT=os.path.join(os.path.dirname(__file__), 'website/media')
)
from django.db import transaction
from django.core.exceptions import ObjectDoesNotExist
from django.core.files.base import ContentFile
from website.tournaments.models import Bot, Tournament, Participant, Game
from framework import Framework
logging.basicConfig(level=logging.DEBUG)
class Scheduler(object):
def __init__(self, args):
self.args = self.parse_args(args)
def parse_args(self, args):
parser = argparse.ArgumentParser(prog='scheduler', argument_default='')
parser.add_argument(args[0], default='')
args = parser.parse_args(args)
args = vars(args)
return args
def run(self):
try:
tournament = Tournament()
tournament.status = Tournament.STATUS_CHOICES[0][0]
tournament.save()
logging.debug('Scheduler.run: tournament = ' + str(tournament))
bots = Bot.objects.filter(is_deleted=0)
start_time = time()
for white in bots:
try:
white_participant = Participant.objects.get(tournament__id=tournament.id, bot__id=white.id)
except ObjectDoesNotExist:
white_participant = Participant(tournament=tournament, bot=white)
logging.debug('Scheduler.run: white = ' + str(white))
for black in bots:
if not white == black:
logging.debug(white.name + ' v. ' + black.name)
try:
black_participant = Participant.objects.get(tournament__id=tournament.id, bot__id=black.id)
except ObjectDoesNotExist:
black_participant = Participant(tournament=tournament, bot=black)
logging.debug('Scheduler.run: black = ' + str(black))
game = Game(tournament=tournament, white=white, black=black)
logging.debug('Scheduler.run: game = ' + str(game))
commandLine = 'framework --white="' + white.name + '" --black="' + black.name + '" --times="600000,0,0" --moves=", " --expansions=""'
commandLine = commandLine.encode('ascii')
framework = Framework(shlex.split(commandLine))
results = framework.norun()
logging.debug('Scheduler.run: results = ' + str(results))
white_participant.wins += results['white']['wins']
white_participant.losses += results['white']['loses']
white_participant.draws += results['white']['draws']
white_participant.number_of_moves += results['white']['number_of_moves']
white_participant.time += results['white']['time']
white_participant.errors += results['white']['errors']
white_participant.save()
black_participant.wins += results['black']['wins']
black_participant.losses += results['black']['loses']
black_participant.draws += results['black']['draws']
black_participant.number_of_moves += results['black']['number_of_moves']
black_participant.time += results['black']['time']
black_participant.errors += results['black']['errors']
black_participant.save()
game.duration = results['white']['time'] + results['black']['time']
game.duration = results['white']['time'] + results['black']['time']
game.number_of_moves = results['white']['number_of_moves'] + results['black']['number_of_moves']
if results['white']['wins'] == 1:
game.winner = game.WINNER_CHOICES[0][0]
elif results['black']['wins'] == 1:
game.winner = game.WINNER_CHOICES[1][0]
else:
game.winner = game.WINNER_CHOICES[2][0]
result_json = json.dumps({
'white': white.name,
'black': black.name,
'times': results['args']['times'],
'moves': results['args']['moves'],
'expansions': results['args']['expansions'],
'date_played': datetime.utcnow().isoformat(),
'duration': game.duration,
'winner': game.winner,
'play_by_play': results['play_by_play']
}, indent=2)
game.result_file.save('result.json', ContentFile(result_json))
game.save()
end_time = time()
tournament.duration = round((end_time - start_time) * 100)
tournament.status = Tournament.STATUS_CHOICES[2][0]
tournament.save()
except:
print "Unexpected error:", sys.exc_info()
if __name__ == "__main__":
Scheduler(sys.argv).run()