view pyikriam/buildings.py @ 261:2488d31240b7

next to CITY tag.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Wed, 19 Nov 2008 12:24:14 +0800
parents 550e20dd7573
children 60c4b4b78a01
line wrap: on
line source

from lazy.www import c
from lxml import etree
from StringIO import StringIO
from sync_utils import sync_tagclass, sync_tagvalue
from sync_utils import sync_tagcount, sync_tagclass_start_appear
from sync_utils import ikariam_zh_timeval

class position(object):
    def __init__(self, build_type, city_id, idx, baseurl):
        self._baseurl = baseurl + '/index.php'
        self.build_type = build_type
        self.city_id = city_id
        self.idx = idx
        self._params = {'view': 'buildingGround',
                        'id': city_id,
                        'position': idx}
        pass

    def get_page(self):
        page = c(self._baseurl).get(self._params).get_content()
        return page

    def sync(self):
        page = self.get_page()
        parser = etree.HTMLParser(encoding='utf8')
        page_dom = etree.parse(StringIO(page), parser)

        self._sync(page_dom)
        pass
    pass

## \brief Base class of all building class.
#
# This class extract information from page of building.  That are
# information all buildings have.
#
class building(position):
    class_patterns = {
        'level': 'buildingLevel'
        }
    appear_patterns = {
        'is_upgrading': 'isUpgrading'
        }
    upgrade_res_patterns = {
        'wood': 'wood',
        'marble': 'marble',
        'crystal': 'glass'
        }

    def _sync(self, page_dom):
        sync_tagclass(self, building.class_patterns, page_dom)

        sync_tagclass_start_appear(self, building.appear_patterns, page_dom)

        xpath_upgrade = '/descendant::ul[@class=\'actions\']/li[@class=\'upgrade\']/a'
        anodes = page_dom.xpath(xpath_upgrade)
        if len(anodes) == 1:
            anode = anodes[0]
            self._upgrade_uri = anode.get('href')
        else:
            self._upgrade_uri = None
            pass

        self.upgrade_wood = 0
        self.upgrade_marble = 0
        self.upgrade_crystal = 0
        self.upgrade_time = 0
        self.upgrade_countdown = 0

        if self.is_upgrading:
            xpath_countdown = '/descendant::div[@id=\'upgradeCountDown\']/text()'
            value = page_dom.xpath(xpath_countdown)[0]
            self.upgrade_countdown = ikariam_zh_timeval(value)
        else:
            xpath_res = '/descendant::div[@class=\'content\']/ul[@class=\'resources\']/li[starts-with(@class, \'%s\')]/text()'

            for resname, clzname in building.upgrade_res_patterns.items():
                xpath = xpath_res % (clzname)
                txts = page_dom.xpath(xpath)
                if len(txts) == 1:
                    value = txts[0].strip()
                    setattr(self, 'upgrade_' + resname, int(value))
                    pass
                pass

            xpath_time = xpath_res % ('time')
            txts = page_dom.xpath(xpath_time)
            if len(txts) == 1:
                value = txts[0].strip()
                self.upgrade_time = ikariam_zh_timeval(value)
                pass
            pass
        pass

    def upgrade(self):
        url = self._baseurl + self._upgrade_uri
        page = c(url).get().get_content()
        pass
    pass

class townhall(building):
    class_patterns = {
        'occupied': 'value occupied',
        'rooms': 'value total',
        }
    value_patterns = {
        'growth': 'growth',
        'happiness': 'happiness',
        'interest_base': 'base',
        'interest_research': 'research1',
        'interest_capital': 'capital',
        'overpopulation': 'cat overpopulation'
        }
    count_patterns = {
        'pop_citizens': 'citizens',
        'pop_woodworkers': 'woodworkers',
        'pop_specialworkers': 'specialworkers',
        'pop_scientists': 'scientists'
        }
    
    def __init__(self, city_id, idx, baseurl):
        super(townhall, self).__init__('townhall', city_id, idx, baseurl)
        pass

    def _sync(self, page_dom):
        sync_tagclass(self, townhall.class_patterns, page_dom)

        sync_tagvalue(self, townhall.value_patterns, page_dom)

        sync_tagcount(self, townhall.count_patterns, page_dom)

        super(townhall, self)._sync(page_dom)
        pass
    pass

class academy(building):
    def __init__(self, city_id, idx, baseurl):
        super(academy, self).__init__('academy', city_id, idx, baseurl)
        pass

    def _sync(self, page_dom):
        xpath_research_name = '/descendant::*[@class=\'researchName\']/a'
        anodes = page_dom.xpath(xpath_research_name)
        if len(anodes) == 1:
            anode = anodes[0]
            self.researching = anode.get('title')
            xpath_countdown = '/descendant::div[@id=\'researchCountDown\']/text()'
            txtnodes = page_dom.xpath(xpath_countdown)
            self.researching_countdown = ikariam_zh_timeval(txtnodes[0])
        else:
            self.researching = None
            self.researching_countdown = None
            pass

        super(academy, self)._sync(page_dom)
        pass
    pass

class warehouse(building):
    def __init__(self, city_id, idx, baseurl):
        super(warehouse, self).__init__('warehouse', city_id, idx, baseurl)
        pass
    pass

class barracks(building):
    def __init__(self, city_id, idx, baseurl):
        super(barracks, self).__init__('barracks', city_id, idx, baseurl)
        pass
    pass

class branchoffice(building):
    def __init__(self, city_id, idx, baseurl):
        super(branchoffice, self).__init__('branchoffice', city_id, idx, baseurl)
        pass
    pass

class port(building):
    def __init__(self, city_id, idx, baseurl):
        super(port, self).__init__('port', city_id, idx, baseurl)
        pass
    pass

class wall(building):
    def __init__(self, city_id, idx, baseurl):
        super(wall, self).__init__('wall', city_id, idx, baseurl)
        pass
    pass

class shipyard(building):
    def __init__(self, city_id, idx, baseurl):
        super(shipyard, self).__init__('shipyard', city_id, idx, baseurl)
        pass
    pass

class empty_pos(position):
    res_patterns = {
        'wood': 'wood',
        'marble': 'marble',
        'crystal': 'glass'
        }

    def _sync(self, page_dom):
        self.building_info = None
        self.res_wood = 0
        self.res_marble = 0
        self.res_crystal = 0
        self.building_time = 0
        self._building_uri = None

        xpath_building = '/descendant::div[@class=\'buildinginfo\']/h4/text()'
        buildings = page_dom.xpath(xpath_building)
        if len(buildings) == 1:
            self.building_info = buildings[0]
            pass

        xpath_costs = '/descendant::div[@class=\'costs\']/ul/li[@class=\'%s\']/text()'
        for res, ptn in empty_pos.res_patterns.items():
            xpath = xpath_costs % (ptn)
            txts = page_dom.xpath(xpath)
            if len(txts) == 1:
                value = int(txts[0])
                setattr(self, 'res_' + res, value)
                pass
            pass

        xpath = xpath_costs % ('time')
        txts = page_dom.xpath(xpath)
        if len(txts) == 1:
            value = ikariam_zh_timeval(txts[0])
            self.building_time = value
            pass

        xpath_button = '/descendant::a[@class=\'button build\']'
        anodes = page_dom.xpath(xpath_button)
        if len(anodes) == 1:
            self._building_uri = anodes[0].get('href')
            pass
        pass

    def build(self):
        url = self._baseurl + self._building_uri
        page = c(url).get().get_content()
        pass
    pass

class land(empty_pos):
    def __init__(self, city_id, idx, baseurl):
        super(land, self).__init__('land', city_id, idx, baseurl)
        pass
    pass

class shore(empty_pos):
    def __init__(self, city_id, idx, baseurl):
        super(shore, self).__init__('shore', city_id, idx, baseurl)
        pass
    pass