Пример #1
0
    def move(self, x, y):
        function_call = client_json.function_call.copy()
        function_call.update({"type": 'move'})
        function_call.get("args").update({"actor": self.id})
        function_call.get("args").update({'x': repr(x)})
        function_call.get("args").update({'y': repr(y)})

        utility.send_string(self._connection, json.dumps(function_call))

        received_status = False
        status = None
        while not received_status:
            message = utility.receive_string(self._connection)
            message = json.loads(message)

            if message.get("type") == "success":
                received_status = True
                status = True
            elif message.get("type") == "failure":
                received_status = True
                status = False
            if message.get("type") == "changes":
                self._parent_game.update_game(message)

        return status
Пример #2
0
    def move(self, x, y):
        function_call = client_json.function_call.copy()
        function_call.update({"type": 'move'})
        function_call.get("args").update({"actor": self.id})
        function_call.get("args").update({'x': repr(x)})
        function_call.get("args").update({'y': repr(y)})

        utility.send_string(self._connection, json.dumps(function_call))

        received_status = False
        status = None
        while not received_status:
            message = utility.receive_string(self._connection)
            message = json.loads(message)

            if message.get("type") == "success":
                received_status = True
                status = True
            elif message.get("type") == "failure":
                received_status = True
                status = False
            if message.get("type") == "changes":
                self._parent_game.update_game(message)

        return status
Пример #3
0
    def get_log(self):
        log_json = client_json.get_log.copy()
        utility.send_string(self.serv_conn, json.dumps(log_json))

        message = self.wait_for('success', 'failure')
        if message['type'] == "success":
            file = open(self.game_name + '.glog', 'wb')
            file.write(message['args']['log'].encode('utf-8'))
            file.close()
Пример #4
0
    def get_log(self):
        log_json = client_json.get_log.copy()
        utility.send_string(self.serv_conn, json.dumps(log_json))

        message = self.wait_for('success', 'failure')
        if message['type'] == "success":
            file = open(self.game_name + '.glog', 'wb')
            file.write(message['args']['log'].encode('utf-8'))
            file.close()
Пример #5
0
    def main_loop(self):
        while True:
            message = self.wait_for('start_turn', 'game_over')
            if message['type'] == 'game_over':
                return True

            if self.ai.my_player_id == self.ai.player_id:
                utility.v_print("Turn Number: {}".format(self.ai.turn_number))
                self.ai.run()
                utility.send_string(self.serv_conn, json.dumps(client_json.end_turn))
Пример #6
0
    def main_loop(self):
        while True:
            message = self.wait_for('start_turn', 'game_over')
            if message['type'] == 'game_over':
                return True

            if self.ai.my_player_id == self.ai.player_id:
                utility.v_print("Turn Number: {}".format(self.ai.turn_number))
                self.ai.run()
                utility.send_string(self.serv_conn, json.dumps(client_json.end_turn))
Пример #7
0
    def login(self):
        login_json = client_json.login.copy()
        login_json['args']['username'] = self.ai.username

        utility.send_string(self.serv_conn, json.dumps(login_json))

        message = self.wait_for('success', 'failure')
        if message['type'] == 'success':
            #Login success
            return True
        else:
            #Login failed
            return False
Пример #8
0
    def create_game(self):
        create_game_json = client_json.create_game.copy()
        if self.game_name is not None:
            create_game_json['args']['game_name'] = self.game_name

        utility.send_string(self.serv_conn, json.dumps(create_game_json))

        message = self.wait_for('success', 'failure')
        if message['type'] == "success":
            self.game_name = message['args']['name']
            print("Game created: {}".format(self.game_name))
            return True
        else:
            #Game creation failed
            return False
Пример #9
0
    def create_game(self):
        create_game_json = client_json.create_game.copy()
        if self.game_name is not None:
            create_game_json['args']['game_name'] = self.game_name

        utility.send_string(self.serv_conn, json.dumps(create_game_json))

        message = self.wait_for('success', 'failure')
        if message['type'] == "success":
            self.game_name = message['args']['name']
            print("Game created: {}".format(self.game_name))
            return True
        else:
            #Game creation failed
            return False