add records for brisbane regional
[mlpccg-meta.git] / mlpccg / AI.py
1 #!/usr/bin/python
2
3 import json
4 import urllib
5 import os
6 import time
7
8 import logging
9 import operator
10 from collections import defaultdict
11
12
13 class App:
14 def __init__(self):
15 self.settings = self.load_settings()
16
17 self.download_images()
18
19 def load_settings(self, path='settings.json'):
20 logging.debug('loading settings from %s', path)
21
22 settings = {
23 'last_card_json_update': 0,
24 'last_card_img_update': 0}
25
26 try:
27 with open(path) as f:
28 settings.update(json.load(f))
29 except:
30 logging.debug('could not load settings, using defaults')
31 self.save_settings(settings)
32
33 return settings
34
35 def save_settings(self, settings={}, path='settings.json'):
36 logging.debug('saving %s', path)
37
38 try:
39 with open(path, 'w') as f:
40 json.dump(settings, f, sort_keys=True, indent=4, separators=(',', ': '))
41 except:
42 logging.exception('failed')
43
44 def download_images(self):
45 queue = []
46 for card in CARDDB.all():
47 path = 'img/cards/%s.jpg' % card['id']
48 path_boosted = 'img/cards/%sb.jpg' % card['id']
49
50 if not os.path.isfile(path):
51 queue += [path]
52
53 if card.get('type') == 'Mane' and not os.path.isfile(path_boosted):
54 queue += [path_boosted]
55
56 logging.debug('downloading %d card images', len(queue))
57
58 for path in queue:
59 try:
60 urllib.urlretrieve('http://ponyhead.com/%s' % path, path)
61 except KeyError:
62 logging.exception('failed: %s', path)
63
64 def new_game(self, teams):
65 self.game = Game(teams=teams)
66
67
68 class Action:
69 def __init__(self, obj):
70 self.obj = obj
71
72 def do(self):
73 raise NotImplementedError
74
75 def undo(self):
76 raise NotImplementedError
77
78 class Pass(Action): pass
79 class Move(Action): pass
80 class Draw(Action): pass
81 class Play(Action): pass
82
83 class Card:
84 uid_counter = 0
85
86 def __init__(self, **kwargs):
87 self.uid = Card.uid_counter
88 Card.uid_counter += 1
89
90 self.tags = []
91 self.__dict__.update(kwargs)
92 self.name = self.title + (', ' + self.subtitle if self.__dict__.get('subtitle') is not None else '')
93
94 def __repr__(self):
95 return "#%d %s (%s)" % (self.uid, self.name, ','.join(self.tags))
96
97 def tag(self, tag):
98 self.tags += [tag]
99
100
101 class Player:
102 def __init__(self, id, team, decklist):
103 self.id = id
104 self.team = team
105 self.at = 0
106 self.points = 0
107 self.decklist = decklist
108
109 class Game:
110 def __init__(self, players):
111 self.players = players
112 self.running = True
113 self.cards = []
114
115 for player in self.players:
116 tag_owner = 'owner=%d' % player.id
117 tag_controller = 'controller=%d' % player.id
118
119 for card_id in player.decklist.cards:
120 card = Card(tags=[tag_owner, tag_controller], **CARDDB.by_id(card_id))
121
122 self.cards += [card]
123
124 print self.cards
125
126
127 if __name__ == '__main__':
128 logging.basicConfig(
129 format='%(asctime)s %(levelname)s %(message)s',
130 datefmt='%H:%M:%S',
131 level=logging.DEBUG)
132
133 APP = App()
134
135 game = Game(players=(
136 Player(id=1, team=1, decklist=DeckList(xml='deck.xml')),
137 Player(id=2, team=2, decklist=DeckList(xml='deck.xml'))))