Ejemplo n.º 1
0
    def onMyAction(self, event):
        myline = self.inputText.GetValue().split()
        cmd = myline[0]

        if cmd == "admit":
            result = pb.Result()
            result.winner = otherColor(self.myColor())
            result.endType = pb.EndType.ADMIT

            self.gameover(result)

            msg = pb.Msg()
            msg.type = pb.MsgType.GAME_OVER
            msg.gameOver.gid = self.game.gid
            msg.gameOver.result.CopyFrom(result)

            self.sendMsg(msg)

        elif cmd == "count":
            self.doPaused()

            msg = pb.Msg()
            msg.type = pb.MsgType.COUNT_REQUEST
            msg.countRequest.gid = self.game.gid
            self.sendMsg(msg)

        elif cmd == "result":
            # logging.info(self.boardPane.getColorCount())
            self.sendCountResult()

        elif cmd == "cancel":
            msg = pb.Msg()
            msg.type = pb.MsgType.CANCEL
            msg.cancel.gid = self.game.gid
            self.sendMsg(msg)
Ejemplo n.º 2
0
    def on_run_button(self, event):
        self.output_text.Clear()

        myline = self.input_text.GetValue().split()
        cmd = myline[0]

        if cmd == "login":
            msg = pb.Msg()
            msg.type = pb.MsgType.LOGIN
            msg.login.pid = myline[1]
            msg.login.passwd = myline[2]
            self.sendMsg(msg)

        elif cmd == "invite":
            dialog = ProtoDialog(self, None)
            result = dialog.ShowModal()
            if result == ProtoDialog.OK:
                msg = pb.Msg()
                msg.type = pb.MsgType.INVITE
                msg.invite.pid = myline[1]
                msg.invite.proto.CopyFrom(dialog.getProto())
                self.sendMsg(msg)
            dialog.Destroy()

        elif cmd == "logout":
            msg = pb.Msg()
            msg.type = pb.MsgType.LOGOUT
            self.sendMsg(msg)

        elif cmd == "data":
            msg = pb.Msg()
            msg.type = pb.MsgType.DATA
            self.sendMsg(msg)

        self.input_text.Clear()
Ejemplo n.º 3
0
 def sendWillDeadStone(self, mystone, myaddOrRemove):
     msg = pb.Msg()
     msg.type = pb.MsgType.WILL_DEAD_STONE
     msg.willDeadStone.gid = self.game.gid
     msg.willDeadStone.addOrRemove = myaddOrRemove
     msg.willDeadStone.stone.CopyFrom(mystone)
     self.sendMsg(msg)
Ejemplo n.º 4
0
 def sendClockNotify(self):
     msg = pb.Msg()
     msg.type = pb.MsgType.CLOCK_NOTIFY
     msg.clockNotify.gid = self.gameFrame.game.gid
     msg.clockNotify.pid = self.gameFrame.myPlayer().pid
     msg.clockNotify.clock.CopyFrom(self.clock)
     self.gameFrame.sendMsg(msg)
Ejemplo n.º 5
0
    def cmd_loop(self):
        try:
            addr = ('localhost', 20000)
            self.sock = socket(AF_INET, SOCK_STREAM)
            self.sock.connect(addr)

            Thread(target=self.get_msg, daemon=True).start()

            while True:
                line = input(self.get_prompt())

                if line == '':
                    self.check_msgs()
                    continue

                cmd, *params = line.split()

                if cmd == "exit":
                    self.sock.shutdown(SHUT_RDWR)
                    break

                elif cmd == "login":
                    pid, password = params

                    msg = pb.Msg()
                    msg.login.pid = pid
                    msg.login.password = password

                    write_msg(self.sock, msg)

                time.sleep(1)
                self.check_msgs()
        finally:
            self.sock.close()
Ejemplo n.º 6
0
    def data_received(self, data):
        self.buf += data
        if len(self.buf) < self.headSize:
            return
        body_size, = struct.unpack('<I', self.buf[:self.headSize])
        if len(self.buf) < self.headSize + body_size:
            return
        bin_data = self.buf[self.headSize:self.headSize + body_size]
        # we should override the *process_msg* method!
        msg = pb.Msg()
        msg.ParseFromString(bin_data)
        self.process_msg(msg)

        self.buf = self.buf[self.headSize + body_size:]
Ejemplo n.º 7
0
    def countdownOverflow(self):
        "对面断线了,但是我等了两分钟还没回来"
        self.stopCountDown()

        myresult = pb.Result()
        myresult.winner = self.myColor()
        myresult.endType = pb.EndType.LINEBROKEN

        self.gameover(myresult)

        msg = pb.Msg()
        msg.type = pb.MsgType.GAME_OVER
        msg.gameOver.gid = self.game.gid
        msg.gameOver.result.CopyFrom(myresult)

        self.sendMsg(msg)
Ejemplo n.º 8
0
    def iamTimeout(self):
        "我超时了,输了,棋局结束了"
        myresult = pb.Result()
        myresult.winner = otherColor(self.myColor())
        myresult.endType = pb.EndType.TIMEOUT

        self.gameover(myresult)

        msg = pb.Msg()
        msg.type = pb.MsgType.GAME_OVER
        msg.gameOver.gid = self.game.gid
        msg.gameOver.result.CopyFrom(myresult)

        self.sendMsg(msg)


# ---------------------------------------------------------
Ejemplo n.º 9
0
    def putStone(self, stone):
        "下一个子,到棋盘上,这个子可能是你下的,也可能是对面下的"
        if self.game.state != pb.State.RUNNING:
            return
        if self.isMyTurn():
            self.boardPane.addStone(stone)
            self.myClockPane().stop()

            msg = pb.Msg()
            msg.type = pb.MsgType.HAND
            msg.hand.gid = self.game.gid
            msg.hand.stone.CopyFrom(stone)
            self.sendMsg(msg)
        else:
            self.boardPane.addStone(stone)
        # checkStart 是没副作用的
        self.checkStart()
Ejemplo n.º 10
0
    def data_received(self, data):
        self.buf += data
        if len(self.buf) < self.headSize:
            logging.debug("dataSize < headSize!")
            return
        bodySize, = struct.unpack('<I', self.buf[:self.headSize])
        logging.debug("bodySize={}".format(bodySize))
        if len(self.buf) < self.headSize + bodySize:
            logging.debug("message data not enougth!")
            return
        bin = self.buf[self.headSize:self.headSize + bodySize]

        msg = pb.Msg()
        msg.ParseFromString(bin)
        wx.CallAfter(self.uiObj.msg_received, msg)

        self.buf = self.buf[self.headSize + bodySize:]
Ejemplo n.º 11
0
    def countRequest(self):
        "接到对面发出的数子请求"
        dialog = wx.MessageDialog(self, 'Are you want to count?', 'Question',
                                  wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
        result = dialog.ShowModal()

        isAgree = True if result == wx.ID_YES else False

        msg = pb.Msg()
        msg.type = pb.MsgType.COUNT_REQUEST_ANSWER
        msg.countRequestAnswer.gid = self.game.gid
        msg.countRequestAnswer.agree = isAgree
        self.sendMsg(msg)

        if isAgree:
            self.doPaused()
            # change to select mode to select will dead stones
            self.boardPane.setSelectMode(True)
Ejemplo n.º 12
0
    def sendCountResult(self):
        "死的都选完了,可以结算了"
        gameResult = pb.Result()
        gameResult.endType = pb.EndType.COUNT
        gameResult.winner, gameResult.mount = self.computePoints()

        # 弹出对话框,问一下是否同意结果。发消息给对面,以确定下一步。
        words = "color:" + str(gameResult.winner) + ",mount:" + str(
            gameResult.mount)
        dialog = wx.MessageDialog(self, words, 'Agree?',
                                  wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
        result = dialog.ShowModal()

        msg = pb.Msg()
        msg.type = pb.MsgType.COUNT_RESULT
        msg.countResult.gid = self.game.gid
        msg.countResult.result.CopyFrom(gameResult)
        msg.countResult.agree = True if result == wx.ID_YES else False

        self.sendMsg(msg)
Ejemplo n.º 13
0
def read_msg(sock):
    """maybe *BLOCK*,when sock recv no data"""
    head_size = 4
    buf = bytes()
    while True:
        data = sock.recv(8192)
        if not data:
            break
        buf += data

        while True:
            if len(buf) < head_size:
                break
            body_size, = struct.unpack('<I', buf[:head_size])
            if len(buf) < head_size + body_size:
                break
            bin_data = buf[head_size:head_size + body_size]

            msg = pb.Msg()
            msg.ParseFromString(bin_data)
            yield msg

            buf = buf[head_size + body_size:]
Ejemplo n.º 14
0
    def msg_received(self, msg):
        logging.info("=========received==========")

        if msg.type == pb.MsgType.LOGIN_OK:
            self.player = msg.loginOk.player
            self.SetTitle("----{}----".format(self.player.pid))

        elif msg.type == pb.MsgType.INVITE:
            # show dialog,get yes or no,or change the proto to resend invite msg again
            # if yes or no,send a invite_answer msg
            dialog = ProtoDialog(self, msg.invite.proto)
            result = dialog.ShowModal()
            if result == ProtoDialog.CHANGE:
                # resend a invite to pid,use the changed proto
                msg.invite.proto.CopyFrom(dialog.getProto())
                self.sendMsg(msg)
            else:
                # not change,agree or refuse
                msg1 = pb.Msg()
                msg1.type = pb.MsgType.INVITE_ANSWER
                msg1.inviteAnswer.agree = True if result == ProtoDialog.OK else False
                msg1.inviteAnswer.pid = msg.invite.pid
                msg1.inviteAnswer.proto.CopyFrom(msg.invite.proto)
                self.sendMsg(msg1)
            dialog.Destroy()

        elif msg.type == pb.MsgType.INVITE_ANSWER:
            logging.info("invite answer: isagree = {}".format(
                msg.inviteAnswer.isAgree))

        elif msg.type == pb.MsgType.GAME:
            # create a window and set the game in
            self.gameFrames.append(GameFrame(self, msg.game))

        elif msg.type == pb.MsgType.HAND:
            self.withGameFrame(msg.hand.gid,
                               lambda gf: gf.putStone(msg.hand.stone))

        elif msg.type == pb.MsgType.GAME_OVER:
            # timeout or admit 超时或者认输,棋局就真的结束了!
            logging.info(msg)
            self.withGameFrame(msg.gameOver.gid,
                               lambda gf: gf.gameover(msg.gameOver.result))

        elif msg.type == pb.MsgType.COUNT_REQUEST:
            # 收到数目请求
            logging.info(msg)
            self.withGameFrame(msg.countRequest.gid,
                               lambda gf: gf.countRequest())

        elif msg.type == pb.MsgType.COUNT_REQUEST_ANSWER:
            # 收到数目回答。同意就开始数子,不同意,就继续。
            logging.info(msg)
            cra = msg.countRequestAnswer

            def myfun(gf):
                if cra.agree:
                    gf.boardPane.setSelectMode(True)
                else:
                    gf.doContinue()

            self.withGameFrame(cra.gid, myfun)

        elif msg.type == pb.MsgType.DO_CONTINUE:
            # 目前,对于数子的结果,如果两人没达成一致,服务器会发出docontinue消息。
            logging.info(msg)
            self.withGameFrame(msg.doContinue.gid, lambda gf: gf.doContinue())

        elif msg.type == pb.MsgType.CLOCK_NOTIFY:
            cn = msg.clockNotify
            self.withGameFrame(cn.gid,
                               lambda gf: gf.clockNotify(cn.pid, cn.clock))

        elif msg.type == pb.MsgType.LINE_BROKEN:
            logging.info(msg)
            self.withGameFrame(msg.lineBroken.gid, lambda gf: gf.lineBroken())

        elif msg.type == pb.MsgType.COMEBACK:
            logging.info(msg)
            self.withGameFrame(msg.comeback.gid, lambda gf: gf.comeback())

        elif msg.type == pb.MsgType.WILL_DEAD_STONE:
            logging.info(msg)
            ds = msg.willDeadStone
            self.withGameFrame(
                ds.gid, lambda gf: gf.boardPane.willDeadStone(
                    ds.addOrRemove, ds.stone))