/// <summary> /// Ends the Capture The Flag game by awarding the winning team with . /// </summary> /// <param name="ctfBot">The <see cref="CaptureTheFlagBot"/> instance.</param> /// <param name="winningTeam">The team what won the game.</param> public void End(CaptureTheFlagBot ctfBot, Team winningTeam) { ctfBot.SayChatMessage($"Game over! Team {winningTeam.GetStringName()} has won the game."); int coinsWon = GetGameFundShare(ctfBot.JoinedWorld.Players, winningTeam); foreach (Player player in ctfBot.JoinedWorld.Players.Values) { PlayerData playerData = MySqlDatabase.GetRow(player.Username); if (playerData != null && player.IsPlayingGame) { if (player.Team == winningTeam) { playerData.Statistics.TotalWins++; playerData.Statistics.Coins += coinsWon; ctfBot.SendPrivateMessage(player, $"You received {coinsWon} coin{(coinsWon == 1 ? "" : "s")} for winning!"); } else { playerData.Statistics.TotalLosses++; } } } ResetRoundStatistics(); MySqlDatabase.Save(); ctfBot.ResetLevel(); }
/// <summary> /// Handles a player cheating if they enter God mode. A player is considered cheating when they are holding the enemy flag their <see cref="Player.IsInGodMode"/> status /// is set to true. /// </summary> /// <param name="ctfBot">The <see cref="CaptureTheFlagBot"/> instance.</param> /// <param name="player">The player to be handled.</param> private void OnGodModeToggled(CaptureTheFlagBot ctfBot, Player player) { // Remove flag from player if they have the enemy flag and enter God mode if (player.IsPlayingGame && player.IsInGodMode && player.HasEnemyFlag(ctfBot)) { ctfBot.SayChatMessage($"ANTI-CHEAT! Player {player.Username} has used God mode while carrying the {player.Team.GetOppositeTeam().GetStringName()} teams flag!"); ctfBot.FlagSystem.Flags[player.Team.GetOppositeTeam()].Return(ctfBot, null, false); } }
/// <summary> /// Handles a player trying to capture, return, or take a flag in the Capture The Flag game. If the <see cref="MaxScoreToWin"/> is reached once a flag is captured, /// then the game is ended via the <see cref="CtfGameRound.End(CaptureTheFlagBot, Team)"/> method. /// </summary> /// <param name="ctfBot">The <see cref="CaptureTheFlagBot"/> instance.</param> /// <param name="player">The player to be handled.</param> private void OnPlayerMoved(CaptureTheFlagBot ctfBot, Player player) { if (player.IsInGodMode || !player.IsPlayingGame) { return; } Flag friendlyFlag = Flags[player.Team]; Flag enemyFlag = Flags[player.Team.GetOppositeTeam()]; if (enemyFlag.CanBeCapturedBy(player, friendlyFlag)) { enemyFlag.Capture(ctfBot); ctfBot.CurrentGameRound.Scores[player.Team]++; ctfBot.CurrentGameRound.IncreaseGameFund(GameFundIncreaseReason.FlagCaptured); ctfBot.SayChatMessage(ctfBot.CurrentGameRound.GetScoresString()); if (ctfBot.CurrentGameRound.Scores[player.Team] >= MaxScoreToWin) { ctfBot.CurrentGameRound.End(ctfBot, player.Team); } else { CelebrateCapture(ctfBot, player.Team); } } else if (enemyFlag.CanBeTakenBy(player)) { enemyFlag.Take(ctfBot, player); ctfBot.CurrentGameRound.IncreaseGameFund(GameFundIncreaseReason.FlagTaken); } else if (friendlyFlag.CanBeReturnedBy(player)) { friendlyFlag.Return(ctfBot, player, false); } }
/// <summary> /// Handles the respawn system for the Capture The Flag game. The respawn system teleports a player to their respective respawn cooldown location, which depends on /// which team they are currently on. If a player is holding the flag and this method gets called, then the flag that they're holding is returned to its base. /// </summary> /// <param name="ctfBot">The <see cref="CaptureTheFlagBot"/> instance.</param> /// <param name="eventArgs">The arguments for when the player (or players) was/were reset.</param> private void OnPlayerReset(CaptureTheFlagBot ctfBot, PlayerResetEventArgs eventArgs) { if (!eventArgs.PropertiesReset) { foreach (Player player in eventArgs.PlayersReset) { if (player.HasEnemyFlag(ctfBot)) { Team enemyTeam = player.Team.GetOppositeTeam(); if (ctfBot.FlagSystem.Flags[enemyTeam].Holder == player) { ctfBot.SayChatMessage($"Player {player.Username} died while holding {enemyTeam.GetStringName()} teams flag."); ctfBot.FlagSystem.Flags[enemyTeam].Return(ctfBot, null, false); } ctfBot.RemoveEffects(player); } RespawnPlayer(ctfBot, player); } } }
/// <summary> /// Handles a player executing a game command. A game command can only be executed by a player either on the blue team or red team. /// </summary> /// <param name="ctfBot">The <see cref="CaptureTheFlagBot"/> instance.</param> /// <param name="player">The player executing the command.</param> /// <param name="parsedCommand">The command being executed.</param> /// <returns> /// True if the command was successfully handled, if not, false. A successful handle is when the parsed command is not equal to null and also the ValidCommands string /// array contains the parsed command. /// </returns> public override bool Handle(CaptureTheFlagBot ctfBot, Player player, ParsedCommand parsedCommand) { bool canHandle = base.Handle(ctfBot, player, parsedCommand); if (canHandle) { if (player.IsPlayingGame) { switch (parsedCommand.Command) { case "blueflag": case "redflag": { Team targetTeam = parsedCommand.Command == "blueflag" ? Team.Blue : Team.Red; string flagHolderUsername = ctfBot.FlagSystem.Flags[targetTeam].Holder?.Username ?? ""; string teamName = targetTeam.GetStringName(); string msgToSend = !string.IsNullOrEmpty(flagHolderUsername) ? $"Player {flagHolderUsername} has the {teamName} flag." : $"No one has {teamName} flag."; ctfBot.SayChatMessage(msgToSend); } break; case "dropflag": { Team enemyTeam = player.Team.GetOppositeTeam(); Flag enemyTeamFlag = ctfBot.FlagSystem.Flags[enemyTeam]; if (enemyTeamFlag.Holder == player) { if (ctfBot.JoinedWorld.Blocks != null) { if (ctfBot.JoinedWorld.Blocks[(uint)BlockLayer.Foreground, player.Location.X, player.Location.Y].Id == 0) { enemyTeamFlag.Drop(ctfBot); } else { ctfBot.SendPrivateMessage(player, "You can't drop the flag here!"); } } } else { ctfBot.SendPrivateMessage(player, "You are not holding the enemy flag."); } } break; case "gamefund": { ctfBot.SendPrivateMessage(player, $"The game fund is currently: {ctfBot.CurrentGameRound.GameFund} coins."); } break; case "heal": { string resultMsg = "You cannot heal yourself because you are not inside your base!"; // Default message if the player is not inside their base if ((player.Team == Team.Blue && player.IsInBlueBase) || (player.Team == Team.Red && player.IsInRedBase)) { player.RestoreHealth(); resultMsg = "Success! Your health was restored fully."; } ctfBot.SendPrivateMessage(player, resultMsg); } break; case "health": case "hp": { ctfBot.SendPrivateMessage(player, $"Your current health is: {player.Health} HP."); } break; case "lobby": case "quit": { player.GoToLobby(ctfBot); } break; case "maxflags": { ctfBot.SendPrivateMessage(player, $"The maximum number of flags to win is {FlagSystem.MaxScoreToWin} flag{(FlagSystem.MaxScoreToWin == 1 ? "" : "s")}."); } break; case "scores": { ctfBot.SendPrivateMessage(player, ctfBot.CurrentGameRound.GetScoresString()); } break; case "suicide": { if (!player.IsRespawning) { player.Die(ctfBot); } } break; } } else { ctfBot.SendPrivateMessage(player, "You must be on either team blue or team red to use this command."); } } return(canHandle); }