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)
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)
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
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)
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)
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))
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)
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