reworking of file structure
[mlpccg-meta.git] / AI.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 import json
5 import urllib
6 import os
7 import time
8
9 import logging
10 import operator
11 from collections import defaultdict
12
13 from mlpccg.DeckList import DeckList
14 from mlpccg.CardDb import CARDDB
15
16 class App:
17 def __init__(self):
18 self.settings = self.load_settings()
19
20 # self.download_images()
21
22 def load_settings(self, path='settings.json'):
23 logging.debug('loading settings from %s', path)
24
25 settings = {
26 'last_card_json_update': 0,
27 'last_card_img_update': 0}
28
29 try:
30 with open(path) as f:
31 settings.update(json.load(f))
32 except:
33 logging.debug('could not load settings, using defaults')
34 self.save_settings(settings)
35
36 return settings
37
38 def save_settings(self, settings={}, path='settings.json'):
39 logging.debug('saving %s', path)
40
41 try:
42 with open(path, 'w') as f:
43 json.dump(settings, f, sort_keys=True, indent=4, separators=(',', ': '))
44 except:
45 logging.exception('failed')
46
47 def download_images(self):
48 queue = []
49 for card in CARDDB.all():
50 path = 'img/cards/%s.jpg' % card['id']
51 path_boosted = 'img/cards/%sb.jpg' % card['id']
52
53 if not os.path.isfile(path):
54 queue += [path]
55
56 if card.get('type') == 'Mane' and not os.path.isfile(path_boosted):
57 queue += [path_boosted]
58
59 logging.debug('downloading %d card images', len(queue))
60
61 for path in queue:
62 try:
63 urllib.urlretrieve('http://ponyhead.com/%s' % path, path)
64 except KeyError:
65 logging.exception('failed: %s', path)
66
67 def new_game(self, teams):
68 self.game = Game(teams=teams)
69
70
71 class Action:
72 def __init__(self, obj):
73 self.obj = obj
74
75 def do(self):
76 raise NotImplementedError
77
78 def undo(self):
79 raise NotImplementedError
80
81 class Pass(Action): pass
82 class Move(Action): pass
83 class Draw(Action): pass
84 class Play(Action): pass
85
86 class Card:
87 uid_counter = 0
88
89 def __init__(self, **kwargs):
90 self.uid = Card.uid_counter
91 Card.uid_counter += 1
92
93 self.__dict__.update(kwargs)
94 # self.name = self.title + (', ' + self.subtitle if self.__dict__.get('subtitle') is not None else '')
95
96 def __repr__(self):
97 return "#%d %s" % (self.uid, self.title.encode('utf-8'))
98
99 class Player:
100 def __init__(self, id, team, decklist):
101 self.id = id
102 self.team = team
103 self.at = 0
104 self.points = 0
105 self.decklist = decklist
106
107 class Game:
108 def __init__(self, players):
109 self.players = players
110 self.cards = {}
111
112 for player in self.players:
113 for card_data in player.decklist.cards:
114 card = Card(owner=player.id, controller=player.id, **CARDDB.by_id(card_data['id']))
115
116 self.cards[card.uid] = card
117
118 if __name__ == '__main__':
119 logging.basicConfig(
120 format='%(asctime)s %(levelname)s %(message)s',
121 datefmt='%H:%M:%S',
122 level=logging.DEBUG)
123
124 APP = App()
125
126 game = Game(players=(
127 Player(id=1, team=1, decklist=DeckList(url='http://ponyhead.com/deckbuilder?v1code=pr206x3-cn185x2-pr79x3-pr161x1-pr108x3-cn117x2-pr165x1-pr85x3-pr82x3-pr83x3-pr163x2-cn80x3-cn86x3-pr67x1-pr194x2-cn7x1-pr130x3-pr118x3-pr179x2-pr94x3-pr11x3-cn95x3-cn98x3')),
128 Player(id=2, team=2, decklist=DeckList(url='http://ponyhead.com/deckbuilder?v1code=pr127x1-cn185x2-pr79x3-cn75x3-pr167x2-pr163x2-cn21x1-pr67x3-pr198x1-cn191x1-pr206x3-pr118x3-pr171x2-pr151x3-pr176x2-cn1x1-pr152x3-pr115x3-pr117x3-pr11x3-pr10x3-pr13x3-pr14x2-pr19x3'))))