diff pyikriam/buildings.py @ 177:6adad3bcca78

Refactory to functions for sychronizing object attributes with pages. - We add more building types. - All of them need to sync attributes with content of Ikariam's online page. - These functions are refactoried to functions.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 02 Nov 2008 15:14:51 +0800
parents 3ba3edda6d1e
children bff16e6ee3ef
line wrap: on
line diff
--- a/pyikriam/buildings.py	Sun Nov 02 11:32:59 2008 +0800
+++ b/pyikriam/buildings.py	Sun Nov 02 15:14:51 2008 +0800
@@ -1,6 +1,7 @@
 from lazy.www import c
 from lxml import etree
 from StringIO import StringIO
+from sync_utils import *
 
 class position(object):
     def __init__(self, build_type, city_id, idx, baseurl):
@@ -16,70 +17,28 @@
     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
 
-class townhall(position):
+class building(position):
     class_patterns = {
-        'level': 'buildingLevel',
-        'occupied': 'value occupied',
-        'rooms': 'value total',
-        }
-    value_patterns = {
-        'growth': 'growth',
-        'happiness': 'happiness',
-        'interest_base': 'base',
-        'interest_research': 'research1',
-        'interest_capital': 'capital'
-        }
-    count_patterns = {
-        'pop_citizens': 'citizens',
-        'pop_woodworkers': 'woodworkers',
-        'pop_specialworkers': 'specialworkers',
-        'pop_scientists': 'scientists'
+        'level': 'buildingLevel'
         }
     appear_patterns = {
         'is_upgrading': 'isUpgrading'
         }
-    
-    def __init__(self, city_id, idx, baseurl):
-        super(townhall, self).__init__('townhall', city_id, idx, baseurl)
-        pass
-
-    def _sync(self, page):
-        parser = etree.HTMLParser(encoding='utf8')
-        page_dom = etree.parse(StringIO(page), parser)
-
-        xpath_building = '/html/body/descendant::*[@class=\'%s\']/text()'
-        for name, clzname in self.class_patterns.items():
-            path = xpath_building % (clzname)
-            value = page_dom.xpath(path)[0]
-            setattr(self, name, value)
-            pass
 
-        xpath_value = '/html/body/descendant::*[starts-with(@class,\'%s\')]/descendant::*[@class=\'value\']/text()'
-        for name, clzname in self.value_patterns.items():
-            path = xpath_value % (clzname)
-            value = page_dom.xpath(path)[0]
-            setattr(self, name, value)
-            pass
+    def _sync(self, page_dom):
+        sync_tagclass(self, building.class_patterns, page_dom)
 
-        xpath_count = '/html/body/descendant::*[starts-with(@class,\'%s\')]/descendant::*[@class=\'count\']/text()'
-        for name, clzname in self.count_patterns.items():
-            path = xpath_count % (clzname)
-            value = page_dom.xpath(path)[0]
-            setattr(self, name, value)
-            pass
-
-        xpath_appear = '/html/body/descendant::*[starts-with(@class,\'%s\')]'
-        for name, clzname in self.appear_patterns.items():
-            path = xpath_appear % (clzname)
-            cnt = len(page_dom.xpath(path))
-            if cnt != 0:
-                setattr(self, name, True)
-            else:
-                setattr(self, name, False)
-                pass
-            pass
+        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)
@@ -89,11 +48,14 @@
         else:
             self.upgrade_uri = None
             pass
-        pass
 
-    def sync(self):
-        page = self.get_page()
-        self._sync(page)
+        if self.is_upgrading:
+            xpath_countdown = '/descendant::div[@id=\'upgradeCountDown\']/text()'
+            value = page_dom.xpath(xpath_countdown)[0]
+            self.upgrade_countdown = value
+        else:
+            self.upgrade_countdown = None
+            pass
         pass
 
     def upgrade(self):
@@ -101,3 +63,75 @@
         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
+    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
+