Exemplo n.º 1
0
 def get_message_request(self, message):
     r = Request()
     r.headers['Action'] = 'UPDATE_CHAT'
     data = {
         'message': message
     }
     r.headers['Data'] = json.dumps(data)
     return r
Exemplo n.º 2
0
 def find_exceptions(self, request): 
     try:
         request.validate()
     except ServerErrorException as e:
         error_request = Request()
         error_request.headers['Action'] = 'ERROR'
         error_request.to_users.append(request.user)
         error_request.headers['Error-Message'] = str(e)
         return error_request
     return None
Exemplo n.º 3
0
    def on_touch_move(self, touch):
        line = touch.ud.get('line')
        line_id = touch.ud.get('line_id')

        request = Request()
        request.headers['Action'] = 'DRAW'

        data = {'id': line_id, 'x': int(touch.x), 'y': int(touch.y)}

        request.headers['Data'] = json.dumps(data)
        q_sender.put(request)
Exemplo n.º 4
0
    def send_message(self):
        text_input = self.ids.text_input
        text = text_input.text.strip()
        if text == '':
            return

        data_object = {'message': text}
        json_data = dumps(data_object)

        request = Request()
        request.headers['Action'] = 'SEND_MESSAGE'
        request.headers['Data'] = json_data
        request.headers['Content-Length'] = len(data_object)
        q_sender.put(request)

        text_input.text = ''
Exemplo n.º 5
0
    def send_draw(self, request: Request):
        draw_request: DrawRequest = DrawRequest()
        draw_request.parse_from_base(request)
        draw_request.parse_draw()
        
        error_request = self.find_exceptions(draw_request)
        if error_request != None:
            return [error_request]

        if self.current_drawing == request.user:
            r = Request()
            r.headers['Action'] = 'UPDATE_DRAW'
            r.headers['Data'] = json.dumps(request.data)
            return [r]
        else:
            return []
Exemplo n.º 6
0
    def get_points_request(self):
        r = Request()
        r.headers['Action'] = 'UPDATE_POINTS'
        player_points = list()
        for username in self.clients.get_all_usernames():
            points = self.points.get(username)
            if points is None:
                self.points[username] = 0
                points = 0

            player_points.append({
                'username': username,
                'points': points
            })

        data = {
            'message': player_points
        }

        r.headers['Data'] = json.dumps(data)
        return r
Exemplo n.º 7
0
    def get_current_word_request(self, request):
        current_word_request = CurrentWordRequest()
        current_word_request.parse_from_base(request)
        current_word_request.headers['Action'] = 'CURRENT_WORD'

        
        r = Request()
        r.headers['Action'] = 'CURRENT_WORD'

        if request.user == self.current_drawing:
            data = {
                'message': self.game_logic.current_word
            }
        else:
            data = {
                'message': self.game_logic.current_word_underlines()
            }

        r.headers['Data'] = json.dumps(data)
        r.users_to_send.append(request.user)
        return r
Exemplo n.º 8
0
    def run(self):
        self.run_worker(self.game_worker)
        self.run_worker(self.sender_worker)

        k = 1
        while True:
            logging.info('Socket accept')
            client, addr = self.ssl_socket.accept()

            username = f'Player {k}'
            self.clients.add_client(client, username)
            k += 1

            r = Request()

            r.headers['Action'] = 'INIT_PLAYER'
            r.user = username
            r.session_id = self.clients.get_session_id(username)

            self.queue_client.put((client, r))

            logging.info('Starting client worker thread')
            self.run_worker(self.worker, args=(client, ))
Exemplo n.º 9
0
    def send_message(self, request: Request):

        request_message = MessageRequest()
        request_message.parse_from_base(request)

        try:
            request_message.validate()
        except ServerErrorException as e:
            request_error = Request()
            request_error.headers['Action'] = 'ERROR'
            request_error.to_users.append(request.user)
            request_error.headers['Error-Message'] = str(e)
            return [request_error]

        data = request.data
        user = request.user
        message = data['message']

        requests = list()

        result, points = self.game_logic.answer_result(message)
        if result:
            if self.current_drawing == request.user:
                return []
            requests.append(self.get_message_request(f'{user}: Answer correct!'))
            self.points[user] += points
            requests.append(self.get_points_request())

            self.start_round()
            requests.append(self.get_message_request(f'New round, {self.current_drawing} is drawing'))
            requests.extend(self.get_new_round_request())
         
        else:
            requests.append(self.get_message_request(f'{user}: {message}'))

        return requests
Exemplo n.º 10
0
    def get_new_round_request(self):
        drawing_player_r = Request()
        other_players_r = Request()

        drawing_player_r.headers['Action'] = 'NEW_ROUND'
        other_players_r.headers['Action'] = 'NEW_ROUND'

        usernames = self.clients.get_all_usernames()
        usernames.remove(self.current_drawing)
        other_players_r.users_to_send.extend(usernames)

        drawing_player_r.users_to_send.append(self.current_drawing)
        
        drawing_player_data = {
            'message': self.game_logic.current_word
        }
        other_players_data = {
            'message': self.game_logic.current_word_underlines()
        }
        drawing_player_r.headers['Data'] = json.dumps(drawing_player_data)
        other_players_r.headers['Data'] = json.dumps(other_players_data)

        return [ drawing_player_r, other_players_r ]
Exemplo n.º 11
0
def receive_request(client) -> Request:
    rcv_data = receive(client)
    request = Request()
    request.parse_request(rcv_data)
    return request