public IHttpActionResult MakeAccusation([FromBody] Accusation accusation) { AuthResult auth = authorizeAndVerifyGameStart(); if (auth.result != null) { return(auth.result); } var game = auth.game.getGame(); var player = auth.player; if (!isPlayerTurn(game, player)) { return(Unauthorized()); } bool isCorrect = game.makeAccusation(player.Name, accusation); AccusationData data = new AccusationData { accusation = accusation, playerName = player.Name, accusationCorrect = isCorrect }; var cmd = new Command { command = CommandType.AccusationMade, data = new CommandData { accusationData = data } }; CommandInterface.SetCommandForEveryone(auth.game, cmd); return(Created("", data)); }
private void MakeAccusation() { /*bool success = client.MakeAccusation(ConvertStringToPerson(AccusePerson), * ConvertStringToRoom(AccuseRoom), * ConvertStringToWeapon(AccuseWeapon));*/ MessageBox.Show($"You have accused " + AccusePerson + " of killing the victim using the " + AccuseWeapon + " in the " + AccuseRoom); Model.Game.Accusation accusation = new Model.Game.Accusation(ConvertStringToRoom(AccuseRoom), ConvertStringToPerson(AccusePerson), ConvertStringToWeapon(AccuseWeapon)); AccusationData data = connect.Gameplay.MakeAccusation(accusation); string accusationMessage = (data.accusationCorrect) ? "correct. The game is now over." : "incorrect. Gameplay will continue."; MessageBoxResult result = MessageBox.Show("Your accusation was " + accusationMessage, "Accusation Received", MessageBoxButton.YesNo); }
public void TestCreateJoinAndStartGame() { // CluelessServerConnection is not designed for async, so async verifications are // being commented out CluelessServerConnection connect = CluelessServerConnection.getConnection( "localhost", 50351); // Host actions Assert.IsTrue(connect.registerAsPlayer("Harry Potter")); { // Success var lobbies = connect.Lobbies.GetLobbies(); Assert.IsNotNull(lobbies); var lobby = connect.Lobbies.CreateLobby(); Assert.IsNotNull(lobby); lobbies = connect.Lobbies.GetLobbies(); Assert.IsNotNull(lobbies); Assert.AreNotEqual(0, lobbies.Count); // Harry is host, he is already in game Assert.IsFalse(connect.Lobbies.JoinLobby(lobby)); } // Other player actions // Task<bool> ronWaitForGame = null; Assert.IsTrue(connect.registerAsPlayer("Ron Weasley")); { // Success var lobbies = connect.Lobbies.GetLobbies(); var lobby = lobbies[lobbies.Count - 1]; // Ron is not host, he can join Assert.IsTrue(connect.Lobbies.JoinLobby(lobby)); connect.registerToGame(lobby); // registerToGame makes it so 'lobby' isn't needed // as a parameter for further API calls // This function is a blocking call if game has not started. // The client should call this to wait for the game to begin. // There currently isn't a way to cancel a wait. Ryan can // implement that later if desired // if (!connect.Lobbies.WaitForGameStart()) // throw new Exception("Unsuccessful Wait for game start"); // Ron cannot start game because he is not the host Assert.IsFalse(connect.Lobbies.StartGame()); // ronWaitForGame = connect.Lobbies.WaitForGameStartAsync(); // Assert.IsFalse(ronWaitForGame.IsCompleted); } // Have a third person join // Task<bool> hermioneWaitForGame = null; Assert.IsTrue(connect.registerAsPlayer("Hermione")); { // Success var lobbies = connect.Lobbies.GetLobbies(); var lobby = lobbies[lobbies.Count - 1]; Assert.IsTrue(connect.Lobbies.JoinLobby(lobby)); connect.registerToGame(lobby); // hermioneWaitForGame = connect.Lobbies.WaitForGameStartAsync(); // Assert.IsFalse(hermioneWaitForGame.IsCompleted); } // Host can start game Assert.IsTrue(connect.registerAsPlayer("Harry Potter")); { Assert.IsTrue(connect.Lobbies.StartGame()); // Allow call to be sent to Hermione and Ron // Thread.Sleep(3100); // Assert.IsTrue(ronWaitForGame.IsCompleted); // response has come back // Assert.IsTrue(ronWaitForGame.Result); // game has started // Assert.IsTrue(hermioneWaitForGame.IsCompleted); // Assert.IsTrue(hermioneWaitForGame.Result); } // other players 'WaitForGameStart()' will now return true Assert.IsTrue(connect.registerAsPlayer("Ron Weasley")); { Assert.IsTrue(connect.Lobbies.WaitForGameStart()); } Assert.IsTrue(connect.registerAsPlayer("Hermione")); { Assert.IsTrue(connect.Lobbies.WaitForGameStart()); } // Play the game now // Get cards String[] players = { "Harry Potter", "Ron Weasley", "Hermione" }; List <Card> previousPlayersCards = null; Command lastSeenCommand = null; foreach (var playerName in players) { // verify that each player has a hand of cards Assert.IsTrue(connect.registerAsPlayer(playerName)); if (!playerName.Equals("Harry Potter")) // Harry will get message 'Take Turn' { // WaitForCommand should return immediately and not wait here Command command = connect.Gameplay.WaitForCommand(); Assert.AreEqual(CommandType.GameStart, command.command); lastSeenCommand = command; // remove last seen command so that hermione won't block connect.Gameplay.TestOnlySetLastSeenCommand(null); } var cards = connect.Gameplay.GetPlayerHand(); Assert.IsNotNull(cards); Assert.AreNotEqual(0, cards.Count); //// Make sure each hand is unique Assert.AreNotEqual(previousPlayersCards, cards); previousPlayersCards = cards; } // The async code doesn't work with a ClientController task that is // intended for a single process // Task<Command> ronWaitForCommand, hermioneWaitForCommand; // take turn Assert.IsTrue(connect.registerAsPlayer("Harry Potter")); { // Get game state to verify correct information Game game = connect.Gameplay.GetState(); Assert.IsNotNull(game); Command command = connect.Gameplay.WaitForCommand(); Assert.AreEqual(CommandType.TakeTurn, command.command); { // Restore Ron & Hermione's last seen command, since they've seen it, // the request for a command should block connect.Gameplay.TestOnlySetLastSeenCommand(lastSeenCommand); /* * Assert.IsTrue(connect.registerAsPlayer("Ron Weasley")); * ronWaitForCommand = connect.Gameplay.WaitForCommandAsync(); * * Assert.IsTrue(connect.registerAsPlayer("Hermione")); * hermioneWaitForCommand = connect.Gameplay.WaitForCommandAsync(); * * Assert.IsFalse(ronWaitForCommand.IsCompleted); * Assert.IsFalse(hermioneWaitForCommand.IsCompleted); */ } // Harry's starting spot is Scarlet's: new Location(0,3,"Hallway") var expectedMoveData = new MoveData { playerName = "Harry Potter", location = new Location(0, 2, "Hallway") }; Assert.IsTrue(connect.registerAsPlayer(expectedMoveData.playerName)); bool successful = connect.Gameplay.MovePlayerTo(expectedMoveData.location); Assert.IsTrue(successful); // Make sure that Ron & Hermione received message { /* * Thread.Sleep(3100); * Assert.IsTrue(ronWaitForCommand.IsCompleted); * Assert.IsTrue(hermioneWaitForCommand.IsCompleted); * * Command cmd = ronWaitForCommand.Result; */ connect.Gameplay.TestOnlySetLastSeenCommand(lastSeenCommand); Assert.IsTrue(connect.registerAsPlayer("Ron Weasley")); Command cmd = connect.Gameplay.WaitForCommand(); connect.Gameplay.TestOnlySetLastSeenCommand(lastSeenCommand); Assert.IsTrue(connect.registerAsPlayer("Hermione")); Command cmd2 = connect.Gameplay.WaitForCommand(); // Ron & Hermione received the same command Assert.AreEqual(cmd, cmd2); // This is the expected command data Assert.AreEqual(CommandType.MovePlayer, cmd.command); Assert.IsNotNull(cmd.data.moveData); Assert.AreEqual(expectedMoveData, cmd.data.moveData); lastSeenCommand = cmd; } Assert.IsTrue(connect.registerAsPlayer("Harry Potter")); // This blocks // DisproveData result = connect.Gameplay.MakeSuggestion(new Accusation(Room.Ballroom, Suspect.Mustard, Weapon.Pipe)); // if null, no one could disprove // Otherwise, result.card is the proof, and result.playerName is the owner of 'card' // This is called by the 'other' player, not by Harry // successful = connect.Gameplay.DisproveSuggestion(new Card(Weapon.Pipe)); Accusation accusation = new Accusation(Room.Ballroom, Suspect.Mustard, Weapon.Pipe); AccusationData data = connect.Gameplay.MakeAccusation(accusation); Assert.IsNotNull(data); Assert.AreEqual(accusation, data.accusation); Assert.IsFalse(data.accusationCorrect); successful = connect.Gameplay.EndTurn(); Assert.IsTrue(successful); Accusation solution = connect.Gameplay.GetSolution(); Assert.IsNull(solution); // game is not finished. Solution not available until then // it is now Ron's turn Assert.IsTrue(connect.registerAsPlayer("Ron Weasley")); Command finalCmd = connect.Gameplay.WaitForCommand(); Assert.IsNotNull(finalCmd); Assert.AreEqual(CommandType.TakeTurn, finalCmd.command); successful = connect.Gameplay.EndTurn(); Assert.IsTrue(successful); // Hermione's turn Assert.IsTrue(connect.registerAsPlayer("Hermione")); finalCmd = connect.Gameplay.WaitForCommand(); Assert.IsNotNull(finalCmd); Assert.AreEqual(CommandType.TakeTurn, finalCmd.command); successful = connect.Gameplay.EndTurn(); Assert.IsTrue(successful); // Harry's turn again Assert.IsTrue(connect.registerAsPlayer("Harry Potter")); finalCmd = connect.Gameplay.WaitForCommand(); Assert.IsNotNull(finalCmd); Assert.AreEqual(CommandType.TakeTurn, finalCmd.command); } }
// End of the functions used to interact //Start of the list of event handlers that take data from the client private async void WaitForCommand() { while (true) { var incCommand = await connect.Gameplay.WaitForCommandAsync(); if (incCommand.command == CommandType.MovePlayer) { MoveData data = incCommand.data.moveData; List <Model.Game.Player> players = connect.Gameplay.GetState().getPlayers(); foreach (Model.Game.Player player in players) { if (player.name == data.playerName) { //this isn't ideal, I'm clearing that one person off the board //then adding them to their new spot foreach (Room room in Board) { RemovePersonFromRoom(player.character.ToString(), room); } AddPersonToRoom(player.character.ToString(), Board[player.location.x, player.location.y]); } } } else if (incCommand.command == CommandType.TakeTurn) { MessageBox.Show("It's your turn"); TurnEnabled = true; RaisePropertyChangedEvent("TurnEnabled"); break; } else if (incCommand.command == CommandType.AccusationMade) { AccusationData data = incCommand.data.accusationData; bool accusationWasCorrect = data.accusationCorrect; string accusationMessage = (accusationWasCorrect) ? "The accusation was correct." : "The accusation was incorrect."; //TODO: Add logic for when this is received MessageBoxResult result = MessageBox.Show(data.playerName + " has accused " + data.accusation.suspect + " of killing the victim using the " + data.accusation.weapon + " in the " + data.accusation.room + ". " + accusationMessage, "Accusation Received", MessageBoxButton.YesNo); if (accusationWasCorrect) { // TODO: further end game process? break; } } else if (incCommand.command == CommandType.DisproveResult) { DisproveData data = incCommand.data.disproveData; MessageBox.Show("The suggestion was disproven by " + data.disprovingPlayer + " by revealing " + data.card.cardValue); break; // This is the active player } else if (incCommand.command == CommandType.SuggestionMade) { SuggestionData data = incCommand.data.suggestData; string disprovePlayerName = data.disprovingPlayer; string disproveMessage = (disprovePlayerName == null) ? "No one can disprove." : disprovePlayerName + " can disprove the suggestion."; MessageBoxResult result = MessageBox.Show(data.playerName + " has suggested that " + data.accusation.suspect + " killed the victim using the " + data.accusation.weapon + " in the " + data.accusation.room + ". " + disproveMessage, "Suggestion Received", MessageBoxButton.YesNo); } else if (incCommand.command == CommandType.DisproveSuggestion) { SuggestionData data = incCommand.data.suggestData; //TODO: Add logic for when this is received MessageBoxResult result = MessageBox.Show(data.playerName + "Has suggested that " + data.accusation.suspect + " killed the victim using the " + data.accusation.weapon + " in the " + data.accusation.room + ". Choose how to disprove this.", "Suggestion Received", MessageBoxButton.YesNo); //If the user says they can disprove the suggestion, enable the disprove button and combobox //Added a call to tell the client to stop what it's doing until it receives the disprove info //May not be the best idea, but I'm open to suggestions if (result == MessageBoxResult.Yes) { //If the user says they can disprove the suggestion, enable the disprove button and combobox //Added a call to tell the client to stop what it's doing until it receives the disprove info //May not be the best idea, but I'm open to suggestions if (result == MessageBoxResult.Yes) { DisproveEnabled = true; RaisePropertyChangedEvent("DisproveEnabled"); break; } else { DisproveEnabled = false; RaisePropertyChangedEvent("DisproveEnabled"); } } else if (incCommand.command == CommandType.TurnEnd) { //TODO: Add logic for when this is received } else if (incCommand.command == CommandType.Wait) { // Ryan: No logic neccessary. Wait is used by WaitForCommand() // to keep waiting for command } else if (incCommand.command == CommandType.GameStart) { } } } }