Beispiel #1
0
 def __init__(self):
     self.users = []
     self.round1 = Round([], 5, 10, "pissfuck")
     self.controller1 = RoundController(self.round1, NetListener, 2424, "")
Beispiel #2
0
class Launcher():
    def __init__(self):
        self.users = []
        self.round1 = Round([], 5, 10, "pissfuck")
        self.controller1 = RoundController(self.round1, NetListener, 2424, "")

    def listen(self):
        PM = PokerMessage
        MT = MessageTypes
        PCF = PlayerControlFields
        PCT = PlayerControlTypes
        PCRF = PlayerControlResponseFields
        ET = ErrorTypes
        self.controller1.start()
        HOST = ''
        PORT = 2423
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind((HOST, PORT))
        s.listen(50)
        while len(self.users) < 5:
            conn, addr = s.accept()
            messages = NetMessage.get_message(conn)
            responses = []
            for message in messages:
                if message[PM.MESSAGE_TYPE] == MT.PLAYER_CONTROL:
                    player_control = message[MT.PLAYER_CONTROL]
                    if player_control[PCF.CONTROL_TYPE] == PCT.JOIN:
                        chips = player_control[PCF.CHIPS]
                        player_id = player_control[PCF.PLAYER_ID]
                        newUser = NetUser(player_id, chips)
                        self.users.append(newUser)
                        response = {}
                        response[PM.MESSAGE_TYPE] = MT.PLAYER_CONTROL_RESPONSE
                        join_success = {}
                        join_success[PCRF.CONTROL_TYPE] = PCT.JOIN
                        join_success[PCRF.PLAYER_ID] = player_id
                        join_success[PCRF.SUCCESS] = True
                        join_success[PCRF.PORT] = 2424
                        response[MT.PLAYER_CONTROL_RESPONSE] = join_success
                        responses.append(response)
                    else:
                        player_id = player_control[PCF.PLAYER_ID]
                        text = "Expected JOIN message, got "
                        text = text + player_control[PCF.CONTROL_TYPE]
                        error_type = ET.UNEXPECTED_CONTROL_TYPE
                        error = PlayerError(player_id=player_id,
                                            text=text,
                                            error_type=error_type)
                        response = error.make_message()
                        responses.append(response)
                else:
                    text = "Expected PLAYER_CONTROL message, got "
                    text = text + message[PM.MESSAGE_TYPE]
                    error_type = ET.UNEXPECTED_MESSAGE
                    error = PlayerError(text=text, error_type=error_type)
                    response = error.make_message()
                    responses.append(response)
            NetMessage.send_message(conn, responses)
            conn.close()
        players = []
        for user in self.users:
            players.append(Player(user, user.chips))
        self.round1.players = players
        self.roundthread = RoundThread(self.round1)
        self.roundthread.start()
        print('started the roundthread')
        self.roundthread.join()
        print('joined with roundthread')
        time.sleep(30)
        self.controller1.stop_listener()
        print('waiting for the listener thread to end')
        self.controller1.join()
        print('joined with the controller')