Fix data structure

This commit is contained in:
sqozz 2018-05-11 17:00:01 +02:00
parent 0627f2564e
commit 395f5eca32

View file

@ -11,7 +11,7 @@ import time
MESSAGES = { "GameInfo" : 0x00, MESSAGES = { "GameInfo" : 0x00,
"WorldUpdate" : 0x01, "WorldUpdate" : 0x01,
"BotSpawn" : 0x20, "BotSpawn" : 0x20,
"BotKill" : 0x21, "BotKill" : 0x21,
"BotMove" : 0x22, "BotMove" : 0x22,
"BotStats" : 0x24 "BotStats" : 0x24
} }
@ -67,8 +67,9 @@ def main():
while True: while True:
try: try:
logger.warning("Trying to connect…") logger.warning("Trying to connect…")
global ws
ws = create_connection("wss://schlangen.bytewerk.org/websocket") ws = create_connection("wss://schlangen.bytewerk.org/websocket")
except Exception: except Exception as e:
game.uptime.observe(0) game.uptime.observe(0)
time.sleep(5) time.sleep(5)
try: try:
@ -76,7 +77,8 @@ def main():
data = ws.recv() data = ws.recv()
game.uptime.observe(1) game.uptime.observe(1)
game.parse_data(data) game.parse_data(data)
except Exception: except Exception as e:
print(e)
time.sleep(0.01) time.sleep(0.01)
@ -98,37 +100,39 @@ class Game():
if typ == MESSAGES["BotSpawn"]: if typ == MESSAGES["BotSpawn"]:
bot = js.get("bot") bot = js.get("bot")
self.users.update_by_db(db_id=bot.get("db_id"), data=bot) name = bot.get("name")
user = self.users.get_user(bot.get("db_id")) self.users.update_by_name(name, data=bot)
#print("{}({}): respawn".format(user.name, user.db_id))
elif typ == MESSAGES["GameInfo"]: elif typ == MESSAGES["GameInfo"]:
game.x = js.get("world_size_c") game.x = js.get("world_size_c")
game.y = js.get("world_size_y") game.y = js.get("world_size_y")
elif typ == MESSAGES["BotKill"]: elif typ == MESSAGES["BotKill"]:
try: try:
victim = self.users.get_user_by_rev(js.get("victim_id")) victim = self.users.get_user_by_worm(js.get("victim_id"))
killer = self.users.get_user_by_rev(js.get("killer_id")) killer = self.users.get_user_by_worm(js.get("killer_id"))
if js.get("killer_id") == js.get("victim_id"): if js.get("killer_id") == js.get("victim_id"):
#print("☕ {} killed himself".format(victim.name)) print("{} killed himself".format(victim.name))
pass pass
else: else:
#print("☠ {} fragged {}".format(killer.name, victim.name)) #print("☠ {} fragged {}".format(killer.name, victim.name))
pass pass
killer.kills += 1 #killer.kills += 1
victim.deaths += 1 #victim.deaths += 1
except AttributeError: except AttributeError as e:
print(e)
print("Error in kill: {}".format(js)) print("Error in kill: {}".format(js))
pass pass
elif typ == MESSAGES["BotStats"]: elif typ == MESSAGES["BotStats"]:
msg_data = js.get("data") msg_data = js.get("data")
for rev in msg_data: for worm in msg_data:
user = self.users.get_user_by_rev(rev) user = self.users.get_user_by_worm(worm)
if user != None:
print("updating {}".format(user.name))
pass pass
elif typ == MESSAGES["WorldUpdate"]: elif typ == MESSAGES["WorldUpdate"]:
bots = js.get("bots") bots = js.get("bots")
for bot in bots: for bot in bots:
bot = bots.get(bot) bot = bots.get(bot)
self.users.update_by_db(bot.get("db_id"), bot) self.users.update_by_name(bot.get("name"), data=bot)
elif msg_type != "FoodSpawn" and msg_type != "WorldUpdate" and msg_type != "FoodConsume" and msg_type != "BotMoveHead" and msg_type != "Tick" and msg_type != "FoodDecay": elif msg_type != "FoodSpawn" and msg_type != "WorldUpdate" and msg_type != "FoodConsume" and msg_type != "BotMoveHead" and msg_type != "Tick" and msg_type != "FoodDecay":
logging.fatal("Unhandled msg_type: {}".format(msg_type)) logging.fatal("Unhandled msg_type: {}".format(msg_type))
@ -167,26 +171,28 @@ class BotUser():
self.name = data.get("name") self.name = data.get("name")
self.dog_tag = data.get("dog_tag") self.dog_tag = data.get("dog_tag")
self.face = data.get("face") self.face = data.get("face")
self.revs.update({ data.get("id") : BotRevision(data) }) self.current_rev = data.get("db_id")
self.current_rev = data.get("id") self.current_worm = data.get("id")
self.revs.update({ self.current_rev : BotRevision(data) })
def update(self, data): def update(self, data):
if self.current_rev in self.revs.keys(): rev_id = data.get("db_id")
pass worm_id = data.get("id") #TODO: does this work?
if rev_id in self.revs.keys(): # revision already known
rev = self.revs.get(rev_id)
rev.update(data)
user = game.users.get_user_by_worm(worm_id)
else: else:
#print("new rev added") self.current_rev = rev_id
pass self.revs.update({ self.current_rev : BotRevision(data) })
if self.current_rev != data.get("id"):
self.current_rev = data.get("id")
else:
#print("{} new code-version".format(self.name))
pass
self.revs.update({ self.current_rev : BotRevision(data) })
class BotRevision(): class BotRevision():
def __init__(self, data): def __init__(self, data):
self.id = data.get("id") self.worms = {}
self.current_worm = data.get("id")
self.rev_id = data.get("db_id")
self.worms.update({self.current_worm : [self.rev_id]})
self.deaths = 0 self.deaths = 0
self.kills = 0 self.kills = 0
self.natural_food_consumed = 0 self.natural_food_consumed = 0
@ -197,22 +203,31 @@ class BotRevision():
def parse(self, data): def parse(self, data):
pass pass
def update(self, data):
self.current_worm = data.get("id")
self.rev_id = data.get("db_id")
self.worms.update({self.current_worm : [self.rev_id]})
pass
class UserList(): class UserList():
def __init__(self): def __init__(self):
self.users = {} self.users = {}
self.__usercount = Gauge('Users', 'Total count of active players') self.__usercount = Gauge('Users', 'Total count of active players')
def update_by_db(self, db_id, data): def update_by_name(self, name, data):
self.__usercount.set(len(self.users)) name = data.get("name")
if data.get("db_id") in self.users.keys(): user_obj = self.get_user(name)
self.users.get(data.get("db_id")).update(data) if user_obj != None:
self.users.get(name).update(data)
self.get_user(name).update(data)
else: else:
self.users.update({db_id : BotUser(data)}) self.users.update({name : BotUser(data)})
#print("{}({}): create user".format(self.users.get(db_id).name, db_id)) #print("{}({}): create user".format(self.users.get(db_id).name, db_id))
self.__usercount.set(len(self.users))
def get_user(self, db_id): def get_user(self, username=""):
return self.users.get(db_id) return self.users.get(username, None)
def get_user_by_rev(self, rev_id): def get_user_by_rev(self, rev_id):
for user in self.users: for user in self.users:
@ -222,5 +237,17 @@ class UserList():
if rev.id == rev_id: if rev.id == rev_id:
return user return user
def get_user_by_worm(self, worm_id):
for user in self.users:
user = self.users.get(user)
for rev in user.revs:
rev = user.revs.get(rev)
for worm in rev.worms:
if worm == worm_id:
return user
else:
#print("No worm found")
pass
main() main()