示例#1
0
文件: main.py 项目: jerrywu64/HeroRTS
 def dataReceived(self, data):
     if not '\n' in data:
         self.buf += data
         return
     full = (self.buf + data).split("\n")
     self.buf = full[-1]
     for line in full[:-1]:
         json_data = json.loads(line)
         if json_data["message_type"] == "hello":
             global game_map
             print "Hello message"
             units = [Unit.from_dict(u) for u in json_data["units"]]
             bullets = [Bullet.from_dict(b) for b in json_data["bullets"]]
             hero = Hero.from_dict(json_data["hero"])
             commander = Commander.from_dict(json_data["commander"])
             game_map = GameMap(json_data["rows"], json_data["cols"],
                                json_data["map"], hero, commander, units, bullets)
         elif json_data["message_type"] == "update":
             # Drop any removed units/bullets, then update values for remaining
             if len(game_map.units) > len(json_data["units"]):
                 game_map.units = game_map.units[:len(json_data["units"])]
             for i in xrange(len(json_data["units"]) - len(game_map.units)):
                 game_map.units.append(Unit(1, 1999, 1999, 0, 0))
             for u_old, u_new in zip(game_map.units, json_data["units"]):
                 u_old.update_from_dict(u_new)
             if len(game_map.bullets) > len(json_data["bullets"]):
                 game_map.bullets = game_map.bullets[:len(json_data["bullets"])]
             for i in xrange(len(json_data["bullets"]) - len(game_map.bullets)):
                 game_map.bullets.append(Bullet(0, 999, 999, 0, 0))
             for b_old, b_new in zip(game_map.bullets, json_data["bullets"]):
                 b_old.update_from_dict(b_new)
             game_map.hero.update_from_dict(json_data["hero"])
             game_map.commander.update_from_dict(json_data["commander"])