Fix data structure
This commit is contained in:
parent
0627f2564e
commit
395f5eca32
1 changed files with 62 additions and 35 deletions
97
schlafana.py
97
schlafana.py
|
@ -11,7 +11,7 @@ import time
|
|||
MESSAGES = { "GameInfo" : 0x00,
|
||||
"WorldUpdate" : 0x01,
|
||||
"BotSpawn" : 0x20,
|
||||
"BotKill" : 0x21,
|
||||
"BotKill" : 0x21,
|
||||
"BotMove" : 0x22,
|
||||
"BotStats" : 0x24
|
||||
}
|
||||
|
@ -67,8 +67,9 @@ def main():
|
|||
while True:
|
||||
try:
|
||||
logger.warning("Trying to connect…")
|
||||
global ws
|
||||
ws = create_connection("wss://schlangen.bytewerk.org/websocket")
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
game.uptime.observe(0)
|
||||
time.sleep(5)
|
||||
try:
|
||||
|
@ -76,7 +77,8 @@ def main():
|
|||
data = ws.recv()
|
||||
game.uptime.observe(1)
|
||||
game.parse_data(data)
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
print(e)
|
||||
time.sleep(0.01)
|
||||
|
||||
|
||||
|
@ -98,37 +100,39 @@ class Game():
|
|||
|
||||
if typ == MESSAGES["BotSpawn"]:
|
||||
bot = js.get("bot")
|
||||
self.users.update_by_db(db_id=bot.get("db_id"), data=bot)
|
||||
user = self.users.get_user(bot.get("db_id"))
|
||||
#print("{}({}): respawn".format(user.name, user.db_id))
|
||||
name = bot.get("name")
|
||||
self.users.update_by_name(name, data=bot)
|
||||
elif typ == MESSAGES["GameInfo"]:
|
||||
game.x = js.get("world_size_c")
|
||||
game.y = js.get("world_size_y")
|
||||
elif typ == MESSAGES["BotKill"]:
|
||||
try:
|
||||
victim = self.users.get_user_by_rev(js.get("victim_id"))
|
||||
killer = self.users.get_user_by_rev(js.get("killer_id"))
|
||||
victim = self.users.get_user_by_worm(js.get("victim_id"))
|
||||
killer = self.users.get_user_by_worm(js.get("killer_id"))
|
||||
if js.get("killer_id") == js.get("victim_id"):
|
||||
#print("☕ {} killed himself".format(victim.name))
|
||||
print("☕ {} killed himself".format(victim.name))
|
||||
pass
|
||||
else:
|
||||
#print("☠ {} fragged {}".format(killer.name, victim.name))
|
||||
pass
|
||||
killer.kills += 1
|
||||
victim.deaths += 1
|
||||
except AttributeError:
|
||||
#killer.kills += 1
|
||||
#victim.deaths += 1
|
||||
except AttributeError as e:
|
||||
print(e)
|
||||
print("Error in kill: {}".format(js))
|
||||
pass
|
||||
elif typ == MESSAGES["BotStats"]:
|
||||
msg_data = js.get("data")
|
||||
for rev in msg_data:
|
||||
user = self.users.get_user_by_rev(rev)
|
||||
for worm in msg_data:
|
||||
user = self.users.get_user_by_worm(worm)
|
||||
if user != None:
|
||||
print("updating {}".format(user.name))
|
||||
pass
|
||||
elif typ == MESSAGES["WorldUpdate"]:
|
||||
bots = js.get("bots")
|
||||
for bot in bots:
|
||||
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":
|
||||
logging.fatal("Unhandled msg_type: {}".format(msg_type))
|
||||
|
@ -167,26 +171,28 @@ class BotUser():
|
|||
self.name = data.get("name")
|
||||
self.dog_tag = data.get("dog_tag")
|
||||
self.face = data.get("face")
|
||||
self.revs.update({ data.get("id") : BotRevision(data) })
|
||||
self.current_rev = data.get("id")
|
||||
self.current_rev = data.get("db_id")
|
||||
self.current_worm = data.get("id")
|
||||
self.revs.update({ self.current_rev : BotRevision(data) })
|
||||
|
||||
def update(self, data):
|
||||
if self.current_rev in self.revs.keys():
|
||||
pass
|
||||
rev_id = data.get("db_id")
|
||||
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:
|
||||
#print("new rev added")
|
||||
pass
|
||||
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) })
|
||||
self.current_rev = rev_id
|
||||
self.revs.update({ self.current_rev : BotRevision(data) })
|
||||
|
||||
|
||||
class BotRevision():
|
||||
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.kills = 0
|
||||
self.natural_food_consumed = 0
|
||||
|
@ -197,22 +203,31 @@ class BotRevision():
|
|||
def parse(self, data):
|
||||
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():
|
||||
def __init__(self):
|
||||
self.users = {}
|
||||
self.__usercount = Gauge('Users', 'Total count of active players')
|
||||
|
||||
def update_by_db(self, db_id, data):
|
||||
self.__usercount.set(len(self.users))
|
||||
if data.get("db_id") in self.users.keys():
|
||||
self.users.get(data.get("db_id")).update(data)
|
||||
def update_by_name(self, name, data):
|
||||
name = data.get("name")
|
||||
user_obj = self.get_user(name)
|
||||
if user_obj != None:
|
||||
self.users.get(name).update(data)
|
||||
self.get_user(name).update(data)
|
||||
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))
|
||||
self.__usercount.set(len(self.users))
|
||||
|
||||
def get_user(self, db_id):
|
||||
return self.users.get(db_id)
|
||||
def get_user(self, username=""):
|
||||
return self.users.get(username, None)
|
||||
|
||||
def get_user_by_rev(self, rev_id):
|
||||
for user in self.users:
|
||||
|
@ -222,5 +237,17 @@ class UserList():
|
|||
if rev.id == rev_id:
|
||||
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()
|
||||
|
|
Loading…
Reference in a new issue