X-Git-Url: https://git.yukkurigames.com/?p=mlpccg-meta.git;a=blobdiff_plain;f=AI.py;fp=AI.py;h=3104accf61aa7d49fd11e66bb588ef05cbd992a3;hp=0000000000000000000000000000000000000000;hb=88d04533bca919d52194a1c3c0e6e982c926a75f;hpb=d0ebe84d9e01a7afa1916f3d1b4c9dfd7a32eccc diff --git a/AI.py b/AI.py new file mode 100644 index 0000000..3104acc --- /dev/null +++ b/AI.py @@ -0,0 +1,128 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import json +import urllib +import os +import time + +import logging +import operator +from collections import defaultdict + +from mlpccg.DeckList import DeckList +from mlpccg.CardDb import CARDDB + +class App: + def __init__(self): + self.settings = self.load_settings() + + # self.download_images() + + def load_settings(self, path='settings.json'): + logging.debug('loading settings from %s', path) + + settings = { + 'last_card_json_update': 0, + 'last_card_img_update': 0} + + try: + with open(path) as f: + settings.update(json.load(f)) + except: + logging.debug('could not load settings, using defaults') + self.save_settings(settings) + + return settings + + def save_settings(self, settings={}, path='settings.json'): + logging.debug('saving %s', path) + + try: + with open(path, 'w') as f: + json.dump(settings, f, sort_keys=True, indent=4, separators=(',', ': ')) + except: + logging.exception('failed') + + def download_images(self): + queue = [] + for card in CARDDB.all(): + path = 'img/cards/%s.jpg' % card['id'] + path_boosted = 'img/cards/%sb.jpg' % card['id'] + + if not os.path.isfile(path): + queue += [path] + + if card.get('type') == 'Mane' and not os.path.isfile(path_boosted): + queue += [path_boosted] + + logging.debug('downloading %d card images', len(queue)) + + for path in queue: + try: + urllib.urlretrieve('http://ponyhead.com/%s' % path, path) + except KeyError: + logging.exception('failed: %s', path) + + def new_game(self, teams): + self.game = Game(teams=teams) + + +class Action: + def __init__(self, obj): + self.obj = obj + + def do(self): + raise NotImplementedError + + def undo(self): + raise NotImplementedError + +class Pass(Action): pass +class Move(Action): pass +class Draw(Action): pass +class Play(Action): pass + +class Card: + uid_counter = 0 + + def __init__(self, **kwargs): + self.uid = Card.uid_counter + Card.uid_counter += 1 + + self.__dict__.update(kwargs) + # self.name = self.title + (', ' + self.subtitle if self.__dict__.get('subtitle') is not None else '') + + def __repr__(self): + return "#%d %s" % (self.uid, self.title.encode('utf-8')) + +class Player: + def __init__(self, id, team, decklist): + self.id = id + self.team = team + self.at = 0 + self.points = 0 + self.decklist = decklist + +class Game: + def __init__(self, players): + self.players = players + self.cards = {} + + for player in self.players: + for card_data in player.decklist.cards: + card = Card(owner=player.id, controller=player.id, **CARDDB.by_id(card_data['id'])) + + self.cards[card.uid] = card + +if __name__ == '__main__': + logging.basicConfig( + format='%(asctime)s %(levelname)s %(message)s', + datefmt='%H:%M:%S', + level=logging.DEBUG) + + APP = App() + + game = Game(players=( + 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')), + 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'))))