示例#1
0
    def speak(self, timeout: float = _default_timeout):
        """
        Send a package to a player to talk about the situation before the vote.

        Parameters:

            timeout: float, time to wait for the client

        Returns:
            
            ChunckedData, the data received
        """
        packet = self._getBasePacket()
        packet['timeLimit'] = timeout
        packetSend = ChunckedData(6, **packet)
        sendingThread = Thread(target=packetSend.send(), args=(self.socket, ))
        sendingThread.start()
        return _startListening(timeout=timeout)
示例#2
0
    def vote(self, timeout: float = _default_timeout):
        """
        Send a package to a player to vote for the exiled.

        Parameters:

            timeout: float, time to wait for the client

        Returns:
            
            ChunckedData, the data received
        """
        packet = self._getBasePacket()
        pakcet['prompt'] = "Please vote for the people to be banished:"
        packetSend = ChunckedData(7, **packet)
        sendingThread = Thread(target=packetSend.send(), args=(self.socket, ))
        sendingThread.start()
        return _startListening(timeout=timeout)
示例#3
0
    def voteForPolice(self, timeout: float = _default_timeout):
        """
        Send a package to a player to vote for the police.

        Parameters:

            timeout: float, time to wait for the client

        Returns:
            
            ChunckedData, the data received
        """
        if not self.police:
            packet = self._getBasePacket()
            packet['prompt'] = "Please vote for the police:"
            packetSend = ChunckedData(7, **packet)
            sendingThread = Thread(target=packetSend.send(),
                                   args=(self.socket, ))
            sendingThread.start()
            return _startListening(timeout=timeout)
        else:
            return None
示例#4
0
    def joinElection(self, timeout: float = _default_timeout):
        """
        Send a package to a player to join the police election.

        Parameters:

            timeout: float, time to wait for the client

        Returns:
            
            ChunckedData, the data received
        """
        packet = self._getBasePacket()
        packet['format'] = bool
        packet[
            'prompt'] = 'Do you want to be the policeman?\nYou have %f seconds to decide.' % (
                timeout, )
        packet['timeout'] = timeout
        packetSend = ChunckedData(3, **packet)
        sendingThread = Thread(target=packetSend.send(), args=(self.socket, ))
        sendingThread.start()
        return _startListening(timeout=timeout)
示例#5
0
    def skill(self,
              prompt: str = "",
              timeout: float = _default_timeout,
              format=int):
        """
        Ask the player whether to use the skill

        Parameters:

            timeout: float, time to wait for the client
            format: the accepted parameter type

        Returns:

            ChunckedData, the data received
        """
        packet = self._getBasePacket()
        packet['format'] = format
        packet['prompt'] = prompt
        packet['timeout'] = timeout
        packetSend = ChunckedData(3, **packet)
        sendingThread = Thread(target=packetSend.send(), args=(self.socket, ))
        sendingThread.start()
        return self._startListening(timeout)
示例#6
0
 def skill(self, killed: int = 0, timeout: float = _default_timeout):
     packet = self._getBasePacket()
     if self.used % 2 == 0:
         killed = 0
     packet['content'] = "The player %s is killed at night." % (
         str(killed) if killed else "unknown", )
     packetSend = ChunckedData(5, **packet)
     if self.used == 0:  # Not ever used
         prompt = """Please select a person to use the poison. If you want to save the victim, enter "save".
 You have %f seconds to decide.""" % (timeout, )
     elif self.used == 1:  # Saved somebody.
         prompt = """Please select a person to use the poison. You have %f seconds to decide.""" % (
             timeout, )
     elif self.used == 2:  # Killed somebody
         prompt = """If you want to save the victim, enter "save". You have %f seconds to decide.""" % (
             timeout, )
     else:
         return None
     return SkilledPerson.skill(self, prompt, timeout, (int, str))
示例#7
0
    def onDead(self, withFinalWords: bool, timeouts: tuple):
        """
        Called on the death of a player.

        Parameters:

            withFinalWords: bool, whether the player can talk at death.
            timeouts: tuple, the timeout limit for two actions.

        Returns:

            a tuple, containing the following item:
            ChunckedData or None: the player inherit the police
            ChunckedData or None: the comment of the player
        """
        self.alive = False
        ret = []
        if self.police:
            packet = self._getBasePacket()
            packet[
                'prompt'] = "Please select the player you want to inherit the police:"
            packetSend = ChunckedData(7, **packet)
            sendingThread = Thread(target=packetSend.send(),
                                   args=(self.socket, ))
            sendingThread.start()
            ret.append(self._startListening(timeout=timeouts[0]))
        else:
            ret.append(None)
        if withFinalWords:
            packet = self._getBasePacket()
            packet['timeLimit'] = timeouts[1]
            packetSend = ChunckedData(6, **packet)
            sendingThread = Thread(target=packetSend.send(),
                                   args=(self.socket, ))
            sendingThread.start()
            ret.append(self._startListening(timeout=timeouts[1]))
        else:
            ret.append(None)
        return tuple(ret)
示例#8
0
    def kill(self, timeout: float = _default_timeout):
        """
        Wolves communicate with each other and specifying the victim

        Parameters:

            timeout: float, time to wait for the client
            
        Returns:

            ChunckedData, the data received
        """
        packet = self._getBasePacket()
        packet['format'] = int
        packet[
            'prompt'] = "Please select a person to kill.\nYou have %f seconds to decide with your partner" % (
                timeout, )
        packet['timeout'] = timeout
        packetSend = ChunckedData(3, **packet)
        sendingThread = Thread(target=packetSend.send(), args=(self.socket, ))
        sendingThread.start()
        timer = TimeLock(timeout)
        timer.setDaemon(True)
        timer.start()
        while not timer.getStatus():
            dataRecv = self._startListening(timeout)
            if dataRecv is None or dataRecv['type'] == -3:
                return dataRecv
            elif dataRecv['type'] == 5:
                dataRecv.pop('type')
                packetSend = ChunckedData(5, dataRecv)
                sendingThreads = [
                    Thread(target=packetSend.send(), args=(_, ))
                    for _ in self.peerList.socket
                ]
                for thread in sendingThreads:
                    thread.start()
        return None