HuangFuSL / Werewolf

The final project of Computer Network in 2020A
MIT License
1 stars 3 forks source link

server发送的包 #17

Closed prprpr777 closed 3 years ago

prprpr777 commented 3 years ago
  1. election: server sends 3, require a -3 from client. action: whether join the election or not. server会让每个人去说话,speak method, 发type= 6,收type= -6的包;server请求非候选人玩家投票,投票用type= 7的包。(白天选警长的过程)
  2. 白天: server宣布死讯:第一步, 发type= 4包,第二步,如果死的是猎人,发type= 3的包(单独发给猎人/狼王)。公布完死讯之后,发给警长一个type = 7的包,让警长选择死左死右。然后server要求每个人发言,发type=6的包。然后投票,用type= 7的包。然后公布投票结果,type= 4的包,然后公布死讯,type= 4的包。
  3. 晚上:多为type= 3的包(技能请求),server会问狼人,发type= 3的包,狼人会回type= 5/-3的包,5用于狼人之间交流,-3用于向server回复要刀的人。女巫/守卫/预言家都是发3回-3,预言家:server会再发一个-3的包(告知身份)。
HuangFuSL commented 3 years ago

Client 输入部分子线程已经完成,代码如下:

def getInput(prompt: str, inputType: type = str, timeout: float = 0) -> Any:
    flag: bool = True
    temp: str
    print("Action timeout: %fs" % (timeout, ))
    while flag:
        try:
            temp = input(prompt)
        except EOFError:
            return KeyboardInterrupt()
        if inputType != str:
            try:
                return eval(temp)
            except:
                print("Input type mismatch!")
        else:
            return temp

class ReadInput(Thread):
    """
    The input thread, will be interrupted by KeyBoardInterruption
    """

    def __init__(self, prompt: str, inputType: type = str, timeout: float = 0):
        super().__init__()
        self.inputType = inputType
        self.timeout = timeout
        self.result: Any = None
        self.prompt = prompt

    def run(self) -> Any:
        if self.timeout == 0:
            self.result = getInput(self.prompt, self.inputType, self.timeout)
        else:
            dest: ReadInput = ReadInput(self.prompt, self.inputType)
            dest.setDaemon(True)
            dest.start()
            dest.join(self.timeout)
            self.result = dest.getResult()
            if self.result is None:
                print("Input timeout.")

    def getResult(self) -> Any:
        """
        Get the return value of the input

        - inputType: if the input is correctly processed
        - None: if timeout
        - KeyboardInterrupt: if Ctrl-C is pressed
        """
        return self.result