Exemplo n.º 1
0
def uttt_cli_main(data, send_queue):
    while True:
        data.Display()
        print """
commands:
        1- ping 2- pong
        3- quit
        4- turn       5- update 6- whoami
        7- login      8- signup
"""
        xstr = raw_input("cmd? ")
        try:
            x = int(xstr)
        except:
            x = 0

        try:
            msg = None
            if x == 1:
                msg = uttt_messages.PingMsg()
                send_queue.put(uttt_messages.Serialize(msg))
            elif x == 2:
                msg = uttt_messages.PongMsg()
                send_queue.put(uttt_messages.Serialize(msg))
            elif x == 3:
                break
            elif x == 4:
                board = input("board? ")
                position = input("position? ")
                text = data.SendTurn(board, position)
                send_queue.put(text)
            elif x == 5:
                text = data.SendUpdate()
                send_queue.put(text)
            elif x == 6:
                text = data.SendWhoami()
                send_queue.put(text)
            elif x == 7:
                email = raw_input("email? ")
                password = raw_input("password? ")
                text = data.SendLogin(email, password)
                send_queue.put(text)
            elif x == 8:
                username = raw_input("username? ")
                email = raw_input("email? ")
                password = raw_input("password? ")
                confirm = raw_input("password? ")
                text = data.SendSignUp(username, email, password, confirm)
                send_queue.put(text)
            else:
                print "Unknown command"
        except AttributeError as e:
            print "AttributeError:", str(e)
        except:
            print "Unexpected error:", sys.exc_info()[0]

    return
Exemplo n.º 2
0
    def HandleMessage(self, msg):
        typeName = msg.GetTypeName()
        retmsg = ""
        if typeName == uttt_messages.PingMsg.GTypeName:
            print "Received Ping"
            retmsg = uttt_messages.Serialize(PongMsg())
        elif typeName == uttt_messages.PongMsg.GTypeName:
            print "Received Pong"
        elif typeName == uttt_messages.BoardStateMsg.GTypeName:
            self.SetBoardOwner(msg.GetBoard(), msg.GetPlayer())
            if g_debug:
                print "Received BoardState"
        elif typeName == uttt_messages.MarkerMsg.GTypeName:
            self.SetBoardMarker(msg.GetBoard(), msg.GetPosition(),
                                msg.GetPlayer())
            if g_debug:
                print "Received Marker"
        elif typeName == uttt_messages.NextTurnMsg.GTypeName:
            self.SetNextTurn(msg.GetBoard(), msg.GetPlayer())
            if g_debug:
                print "Received NextTurn"
        elif typeName == uttt_messages.WinStateMsg.GTypeName:
            self.SetWinner(msg.GetPlayer())
            if g_debug:
                print "Received WinState"
        elif typeName == uttt_messages.YouAreMsg.GTypeName:
            self.SetThisPlayer(msg.GetPlayer(), msg.GetPlayerName())
            if g_debug:
                print "Received YouAre"
        elif typeName == uttt_messages.TurnFailedMsg.GTypeName:
            self.SetTurnFailed()
            if g_debug:
                print "Received TurnFailed"
        elif typeName == uttt_messages.LoginReplyMsg.GTypeName:
            self.SetLoginStatus(msg.GetValid())
            if g_debug:
                print "Received LoginReply"
        elif typeName == uttt_messages.SignUpReplyMsg.GTypeName:
            self.SetSignupStatus(msg.GetUsernameValid(), msg.GetEmailValid(),
                                 msg.GetPasswordState())
            if g_debug:
                print "Received SignUpReply"
        elif typeName == uttt_messages.OpponentNameMsg.GTypeName:
            self.SetOtherPlayer(msg.GetPlayerName())
            if g_debug:
                print "Received OpponentName"
        elif typeName == uttt_messages.UpdateFinishedMsg.GTypeName:
            self.SetUpdateFinished()
            if g_debug:
                print "Received UpdateFinished"
        else:
            print "Unknown Message Type:", typeName, str(msg)

        #self.Display()
        return retmsg
Exemplo n.º 3
0
 def SendLogin(self, email, password):
     if self.state == STATE_SHOW_LOGIN or self.state == STATE_SIGNUP_OK:
         email = re.sub(" ", "", email)
         password = re.sub(" ", "", password)
         if email != "" and password != "":
             self.login_name = email
             self.login_password = password
             self.state = STATE_WAIT_LOGIN
             msg = uttt_messages.LoginMsg(email, password)
             return uttt_messages.Serialize(msg)
     return ""
Exemplo n.º 4
0
 def SendSignUp(self, username, email, password, password_confirm):
     if self.state == STATE_SHOW_SIGNUP:
         username = re.sub(" ", "", username)
         email = re.sub(" ", "", email)
         password = re.sub(" ", "", password)
         password_confirm = re.sub(" ", "", password_confirm)
         if username != "" and email != "" and password != "" and password_confirm != "":
             self.login_name = email
             self.login_password = password
             self.state = STATE_WAIT_SIGNUP
             msg = uttt_messages.SignUpMsg(username, email, password, password_confirm)
             return uttt_messages.Serialize(msg)
     return ""
Exemplo n.º 5
0
 def SendTurn(self, board, position):
     # check board is legal
     # check board isn't won
     # check position is legal
     # check board position is empty
     # check is my turn
     if (((self.next_board == BOARD_ANY and self.LegalBoardIndex(board)) or self.next_board == board)
         and self.board_owners[board] == PLAYER_N
         and self.LegalPositionIndex(board, position) and self.board[board][position] == PLAYER_N
         and self.next_player == self.this_player and self.next_player != PLAYER_N):
         
         
         self.state = STATE_WAIT_TURN
         msg = uttt_messages.TurnMsg(self.this_player, board, position)
         return uttt_messages.Serialize(msg)
     return ""
Exemplo n.º 6
0
 def SendWhoami(self):
     msg = uttt_messages.WhoamiMsg()
     return uttt_messages.Serialize(msg)
     return True
Exemplo n.º 7
0
 def SendUpdate(self):
     msg = uttt_messages.UpdateMsg()
     return uttt_messages.Serialize(msg)