void choosePorts() { //if toOperator, one port needs to be operator port Call call = new Call(); string socketA = getAvailablePort(); if (socketA == null) // nothing available { return; } sockControl.reserveForCall(socketA); string socketB; socketB = getAvailablePort(); if (socketB == null) // nothing available, clear A. { sockControl.unreserveForCall(socketA); return; } sockControl.reserveForCall(socketB); call.targetPort = socketA; call.incomingPort = socketB; call.fromName = sockControl.getSocket(call.incomingPort).getRandomName(); call.toName = sockControl.getSocket(call.targetPort).getRandomName(); call.operatorConv = loader.getRandomOperatorConversation(); call.operatorConv.setFormatter(call.toName); bool to = false; bool from = false; if (badGuyNames.Contains(call.toName)) { to = true; } if (badGuyNames.Contains(call.fromName)) { from = true; } call.tappedConv = loader.getRandomBadConvo(from, to); call.tappedConv.toReplace = call.toName; call.tappedConv.fromReplace = call.fromName; //Display the input socket as lit up //tell andy code to listen for connection callsToday++; calls.Add(call); }
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); }