Пример #1
0
def handle_message(data):
    cli = Client()

    client_uuid, command = data.split("|")
    cli.set_uuid(client_uuid)
    cli.send(NEW_CLIENT)

    if command == "up":
        cli.send(GO_UP)
    elif command == "down":
        cli.send(GO_DOWN)
    elif command == "left":
        cli.send(GO_LEFT)
    elif command == "right":
        cli.send(GO_RIGHT)
    elif command == "r":
        cli.send(NEW_CLIENT)
Пример #2
0
class SnakeClient(object):
    def __init__(self, stupid=False):
        self.uuid = None
        self.ui = None

        self.net_cli = Client()

        if not stupid:
            self.ui = ui.SnakeUI()

    def create(self):
        self.uuid = str(uuid.uuid1())
        self.net_cli.set_uuid(self.uuid)
        self.net_cli.send(NEW_CLIENT)

    def get_world(self):
        res = self.net_cli.send(GET_WORLD)
        try:
            return json.loads(res)
        except json.decoder.JSONDecodeError:
            with open("dump", "w") as fl:
                print("Broken world recieved.")
                fl.write(res)
            return self.get_world()

    def commands_handler(self):
        cmd = self.ui.get_event()
        if cmd in [GO_UP, GO_DOWN, GO_RIGHT, GO_LEFT]:
            self.net_cli.send(cmd)
        if cmd == CLIENT_RESET:
            self.create()

    def display_map(self):
        w = self.get_world()
        self.ui.draw(w)

    def run(self):
        while True:
            self.commands_handler()
            self.display_map()
Пример #3
0
def handle_message(user_uuid):
    cli = Client()
    cli.set_uuid(user_uuid)
    resp = cli.send(GET_WORLD)
    emit("map", resp)