Exemplo n.º 1
0
    public bool handleState(string connectedTo, SocketController controller, string operatorSocket, ConversationHandler convHandle, StrikeCounter counter)
    {
        if (previousState != state)
        {
            Debug.Log(incomingPort + " has changed from " + previousState + " to " + state);
        }

        switch (state)
        {
        case State.UNANSWERED:
            if (previousState != state)
            {
                controller.setLED(incomingPort, Socket.LEDColor.Red_Flash);
                unansweredTimer.Start();
                previousState = state;
            }

            if (unansweredTimedout)
            {
                state = State.DISCONNECT_NEGATIVE;
            }
            else if (connectedTo == operatorSocket)
            {
                state = State.OPERATOR_CONNECTED;
            }
            else if (connectedTo != null)
            {
                state = State.WRONG_PERSON_CONNECTED;
            }
            break;

        case State.OPERATOR_CONNECTED:
            if (previousState != state)
            {
                Debug.Log("OH HI THE ANSWER IS " + targetPort);
                convHandle.setConversation(operatorConv, true);
                if (controller.isTapped(incomingPort))
                {
                    Debug.Log("TAPPED");
                    convHandle.setConversation(operatorConv, false);
                }
                controller.setLED(incomingPort, Socket.LEDColor.Green);
                controller.setLED(operatorSocket, Socket.LEDColor.Green);

                // Only call once, they can jump back to this state, and it shouldn't restart the timer.
                if (!afterOperatorTimeoutCalled)
                {
                    afterOperatorTimeoutCalled = true;
                    afterOperatorTimer.Start();
                }

                previousState = state;
            }
            //EVERY FRAME

            if (afterOperatorTimedout)   // connected too long
            {
                state = State.DISCONNECT_NEGATIVE;
            }
            else if (connectedTo != operatorSocket)
            {
                state = State.DISCONNECTED_AFTER_OPERATOR;
            }
            break;

        case State.DISCONNECTED_AFTER_OPERATOR:
            if (previousState != state)
            {
                controller.setLED(incomingPort, Socket.LEDColor.Red_Flash);
                controller.setLED(operatorSocket, Socket.LEDColor.Off);
                previousState = state;
            }
            if (afterOperatorTimedout)               // connected too long
            {
                state = State.DISCONNECT_NEGATIVE;
            }
            else if (connectedTo == targetPort)
            {
                state = State.TALKING;
            }
            else if (connectedTo == operatorSocket)
            {
                state = State.OPERATOR_CONNECTED;
            }
            else if (connectedTo != null)
            {
                state = State.WRONG_PERSON_CONNECTED;
            }
            break;

        case State.TALKING:
            if (previousState != state)
            {
                if (controller.isTapped(incomingPort))
                {
                    convHandle.setConversation(tappedConv, false);
                }
                controller.setLED(incomingPort, Socket.LEDColor.Green);
                controller.setLED(targetPort, Socket.LEDColor.Green);

                // Lock both
                controller.LockSocket(incomingPort);
                controller.LockSocket(targetPort);

                convoTimer.Start();
                previousState = state;
            }
            if (convoTimedout && !controller.isTapped(incomingPort))                // convo success
            {
                state = State.DISCONNECT_POSITIVE;
            }
            else if (controller.isTapped(incomingPort))
            {
                bool convend = !tappedConv.hasNextSentance();
                bool textend = !convHandle.txtwritetapped.speaking;
                if (convend && textend)
                {
                    state = State.DISCONNECT_POSITIVE;
                }
            }
            break;

        case State.DISCONNECT_POSITIVE:
            if (previousState != state)
            {
                controller.happyAt(incomingPort);
                controller.setLED(incomingPort, Socket.LEDColor.Off);
                controller.setLED(targetPort, Socket.LEDColor.Off);

                // Unlock both
                controller.UnlockSocket(incomingPort);
                controller.UnlockSocket(targetPort);

                disconnectGraceTimer.Start();
                previousState = state;
            }
            // TODO Positive feedback
            if (disconnectGraceTimedout)
            {
                controller.unreserveForCall(incomingPort);
                controller.unreserveForCall(targetPort);
                // we want to die, return false!
                return(false);
            }
            break;

        case State.DISCONNECT_NEGATIVE:
            if (previousState != state)
            {
                controller.sadAt(incomingPort);
                counter.addCitation();
                controller.setLED(incomingPort, Socket.LEDColor.Off);
                controller.setLED(targetPort, Socket.LEDColor.Off);
                if (connectedTo != null)
                {
                    controller.setLED(connectedTo, Socket.LEDColor.Off);
                    controller.UnlockSocket(connectedTo);
                    controller.unreserveForCall(connectedTo);
                }

                // Unlock both
                controller.UnlockSocket(incomingPort);
                controller.UnlockSocket(targetPort);

                disconnectGraceTimer.Start();
                previousState = state;
            }
            if (disconnectGraceTimedout)
            {
                controller.unreserveForCall(incomingPort);
                controller.unreserveForCall(targetPort);
                // we want to die, return false!
                return(false);
            }
            break;

        case State.WRONG_PERSON_CONNECTED:
            if (previousState != state)
            {
                controller.setLED(incomingPort, Socket.LEDColor.Red);
                controller.setLED(connectedTo, Socket.LEDColor.Red);

                // Lock both
                controller.LockSocket(incomingPort);
                controller.LockSocket(connectedTo);
                controller.reserveForCall(connectedTo);

                wrongConvoTimer.Start();
                previousState = state;
            }
            if (wrongConvoTimedout)
            {
                state = State.DISCONNECT_NEGATIVE;
            }
            break;
        }
        // We still want to live, return true!
        return(true);
    }