summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Seguin <guillaume@segu.in>2010-05-19 16:30:37 -0400
committerGuillaume Seguin <guillaume@segu.in>2010-05-19 16:30:37 -0400
commit74ef54d22611d269742ad55b6fd19599199dc85f (patch)
tree3133ae9036591d9714427bde61c024ed351f945a
parent8bc716675983985d46fe4e914e494e967d35ce36 (diff)
downloadsqwareapi-74ef54d22611d269742ad55b6fd19599199dc85f.tar.gz
sqwareapi-74ef54d22611d269742ad55b6fd19599199dc85f.tar.bz2
Add multimapdump and fast login process using sessionid
-rw-r--r--multimapdump.py81
-rw-r--r--sqwarea/__init__.py17
2 files changed, 93 insertions, 5 deletions
diff --git a/multimapdump.py b/multimapdump.py
new file mode 100644
index 0000000..36dfabb
--- /dev/null
+++ b/multimapdump.py
@@ -0,0 +1,81 @@
+import cPickle as pickle
+import gzip
+import sys
+
+import sqwarea
+
+from multiprocessing import Pool
+PROCESS_COUNT = 15
+
+if len (sys.argv) > 1:
+ openid = sys.argv[1]
+else:
+ import myids
+ login = myids.logins[0]
+ openid = login if "http" in login else myids.basepath % login
+
+def fetch_block_wrapper ((sessionId, viewport)):
+ try:
+ return fetch_block (sessionId, viewport)
+ except KeyboardInterrupt:
+ processing_pool.terminate ()
+ raise KeyboardInterrupt
+
+def fetch_block (sessionId, viewport):
+ api = sqwarea.API (openid, sessionId = sessionId)
+ return api._syncMapRequest (viewport)
+
+width = 100
+radius = 0
+had_items = True
+DEL_NEUTRAL = True
+
+if __name__ == '__main__':
+ processing_pool = Pool (PROCESS_COUNT)
+ api = sqwarea.API (openid)
+ sessionId = api._client.sessionId
+
+ print "Fetching map, %dx%d blocks" % (width, width)
+ while had_items:
+ radius += 1
+ print "Iteration %d" % radius
+ viewports = []
+ for i in range (2 * radius - 1):
+ viewport = sqwarea.Rectangle ((- radius + i) * width,
+ -radius * width,
+ width, width)
+ viewports.append ((sessionId, viewport))
+ viewport = sqwarea.Rectangle ((radius - 1) * width,
+ (-radius + i) * width,
+ width, width)
+ viewports.append ((sessionId, viewport))
+ viewport = sqwarea.Rectangle (- (- radius + i + 1) * width,
+ (radius - 1) * width,
+ width, width)
+ viewports.append ((sessionId, viewport))
+ viewport = sqwarea.Rectangle (- radius * width,
+ - (-radius + i + 1) * width,
+ width, width)
+ viewports.append ((sessionId, viewport))
+ replies = processing_pool.map (fetch_block_wrapper, viewports)
+ had_items = False
+ for reply in replies:
+ had_items = api._handleSyncMap (reply) > 0 or had_items
+
+ print "Stopped after iteration %d" % radius
+ if DEL_NEUTRAL:
+ print "Removing neutral squares"
+ to_del = []
+ for (x, y) in api.map._data:
+ cell = api.map._data[x, y]
+ if not cell.id:
+ to_del.append ((x, y))
+ for key in to_del:
+ del api.map._data[key]
+ del to_del
+ out_filename = "map.dump"
+ print "Dumping to %s" % out_filename
+ f = gzip.open (out_filename, "wb")
+ pickle.dump (api.map._data, f, -1)
+ pickle.dump (api.map._extents, f, -1)
+ f.close ()
diff --git a/sqwarea/__init__.py b/sqwarea/__init__.py
index 0f99dcb..c930d03 100644
--- a/sqwarea/__init__.py
+++ b/sqwarea/__init__.py
@@ -412,8 +412,14 @@ class API (object):
ongoingAttacks = None
incomingAttacks = None
- def __init__ (self, openid, login = None, password = None):
+ def __init__ (self, openid, login = None, password = None, sessionId = None):
+ self.ongoingAttacks = {}
+ self.incomingAttacks = {}
self._client = Client (openid)
+ if sessionId:
+ logging.log (logging.DEBUG, "sessionId provided, skipping login")
+ self._client.sessionId = sessionId
+ return
reply = self._client.request ("/Game", {})
if reply.url.endswith ("/Game"):
self._logged_in (openid, reply)
@@ -458,8 +464,6 @@ class API (object):
self._client.save ()
self._client.sessionId = parseSessionId (conn.read ())
logging.log (logging.DEBUG, "sessionId = %s", self._client.sessionId)
- self.ongoingAttacks = {}
- self.incomingAttacks = {}
self.sync ()
def _handleSync (self, reply):
@@ -539,10 +543,13 @@ class API (object):
reply = self._client.jsrequest ("/Game/Synchronize", {})
self._handleSync (reply)
+ def _syncMapRequest (self, viewport):
+ return self._client.jsrequest ("/Game/SynchronizeMap",
+ {"rectangles": viewport})
+
def syncMap (self):
logging.log (logging.DEBUG, "syncMap ()")
- reply = self._client.jsrequest ("/Game/SynchronizeMap",
- {"rectangles": self.viewport})
+ reply = self._syncMapRequest (viewport)
return self._handleSyncMap (reply)
def view (self, rectangle):