using System; using System.Collections.Generic; using UnityEngine; public class TournamentManager : MonoBehaviour { [Serializable] public class TournamentFish { public Fish.Species species = Fish.Species.COUNT; public float weight = 1f; public Vector2 weightMinMax = new Vector2(4f, 6f); } [Serializable] public class TournamentDefinition { public enum FinishCondition { TIME = 0, GOAL_FIRST = 1, COUNT = 2 } public enum GoalType { AMOUNT = 0, WEIGHT_SUM = 1, WEIGHT_MAX = 2, COUNT = 3 } public FinishCondition finishCondition; public GoalType goalType; public int nrOfPlayers = 4; public float duration = 60f; public float goalAmount = 10f; public static string FinishConditionToString(FinishCondition enumValue) { switch (enumValue) { case FinishCondition.TIME: return Utilities.GetTranslation("GUI/TOURNAMENTS_TIME"); case FinishCondition.GOAL_FIRST: return Utilities.GetTranslation("GUI/TOURNAMENTS_GOAL"); default: return string.Empty; } } public static string GoalTypeToString(GoalType enumValue) { switch (enumValue) { case GoalType.AMOUNT: return Utilities.GetTranslation("GUI/TOURNAMENTS_FISH_AMOUNT"); case GoalType.WEIGHT_SUM: return Utilities.GetTranslation("GUI/TOURNAMENTS_WEIGHT_SUM"); case GoalType.WEIGHT_MAX: return Utilities.GetTranslation("GUI/TOURNAMENTS_WEIGHT_MAX"); default: return string.Empty; } } } [HideInInspector] public HUDTournament hudTournament; [HideInInspector] public GameController gameController; public bool tournamentActive; public bool tournamentStarted = true; public float currentTime; public TournamentPlayer tournamentPlayerPrefab; public List players = new List(); public List sortedPlayers = new List(); public TournamentDefinition tournamentDefinition = new TournamentDefinition(); public List tournamentSpecies = new List(); public int currentPlayerPosition; public float networkTimeUpdateTimer = 15f; public void Awake() { } public void Initialize() { gameController = GameController.Instance; if ((bool)GlobalSettings.Instance) { tournamentDefinition = GlobalSettings.Instance.tournamentDefinition; } currentTime = tournamentDefinition.duration * 60f; for (int i = 0; i < (gameController.isMultiplayer ? 1 : tournamentDefinition.nrOfPlayers); i++) { TournamentPlayer tournamentPlayer = UnityEngine.Object.Instantiate(tournamentPlayerPrefab); tournamentPlayer.transform.parent = base.transform; tournamentPlayer.isAi = true; tournamentPlayer.playerId = i; tournamentPlayer.playerName = "AI Player " + i; tournamentPlayer.aiLevel = Mathf.FloorToInt((float)i / (float)tournamentDefinition.nrOfPlayers * 10f); AddPlayer(tournamentPlayer, false); tournamentPlayer.Initialize(); } gameController.fishingPlayer.SetTournamentPlayer(players[0]); UpdateSortedPlayers(); tournamentSpecies = gameController.GetAvailableSpecies(); hudTournament = HUDManager.Instance.hudTournament; hudTournament.Initialize(); hudTournament.UpdateTime(currentTime); hudTournament.UpdateGoal((int)tournamentDefinition.goalAmount); hudTournament.UpdatePlayerWidgets(); tournamentActive = true; } public void UpdateMultiplayerSettings() { hudTournament.UpdateMultiplayerSettings(); hudTournament.UpdateTime(currentTime); hudTournament.UpdateGoal((int)tournamentDefinition.goalAmount); hudTournament.UpdatePlayerWidgets(); } public void MakeUpdate() { hudTournament.UpdateStartInfo(); if (!tournamentActive || !tournamentStarted) { return; } if (!gameController.isMultiplayer) { for (int i = 0; i < players.Count; i++) { players[i].MakeUpdate(); } } if (tournamentDefinition.finishCondition != TournamentDefinition.FinishCondition.TIME || !(currentTime > 0f)) { return; } UpdateNetworkTime(); currentTime -= Time.deltaTime; hudTournament.UpdateTime(currentTime); if (currentTime < 0.8f) { currentTime = 0f; hudTournament.UpdateTime(currentTime); if (!gameController.isMultiplayer || PhotonNetwork.isMasterClient) { FinishTournament(); } } } private void UpdateNetworkTime() { if (gameController.isMultiplayer && PhotonNetwork.isMasterClient) { networkTimeUpdateTimer -= Time.deltaTime; if (networkTimeUpdateTimer <= 0f) { networkTimeUpdateTimer = 15f; gameController.multiplayerManager.UpdateMyTime(); } } } public void AddPlayer(TournamentPlayer tournamentPlayer, bool sort = true) { players.Add(tournamentPlayer); if (sort) { UpdateSortedPlayers(); } } public void AddNetworkPlayer(PhotonPlayer photonPlayer, bool sort = true) { TournamentPlayer tournamentPlayer = UnityEngine.Object.Instantiate(tournamentPlayerPrefab); tournamentPlayer.transform.parent = base.transform; tournamentPlayer.isAi = false; tournamentPlayer.playerId = photonPlayer.ID; tournamentPlayer.playerName = photonPlayer.name; tournamentPlayer.Initialize(); players.Add(tournamentPlayer); if (sort) { UpdateSortedPlayers(); } hudTournament.UpdatePlayerWidgets(); } public TournamentPlayer GetPlayer(int playerId) { for (int i = 0; i < players.Count; i++) { if (players[i].playerId == playerId) { return players[i]; } } return null; } public TournamentPlayer GetMyPlayer() { return gameController.fishingPlayer.tournamentPlayer; } public void RemoveAllPlayers(bool alsoMe) { players.Clear(); sortedPlayers.Clear(); players.Add(GetMyPlayer()); UpdateSortedPlayers(); hudTournament.UpdatePlayerWidgets(); } public void RemovePlayer(int playerId) { int num = -1; for (int i = 0; i < players.Count; i++) { if (players[i].playerId == playerId) { num = i; break; } } if (players.Count > num) { players.RemoveAt(num); } UpdateSortedPlayers(); hudTournament.UpdatePlayerWidgets(); } public void UpdateSortedPlayers() { sortedPlayers.Clear(); sortedPlayers.AddRange(players); SortPlayers(); } public void SortPlayers() { sortedPlayers.Sort((TournamentPlayer p1, TournamentPlayer p2) => p2.GetScore().CompareTo(p1.GetScore())); int sortedPosition = 0; float num = -1f; for (int num2 = 0; num2 < sortedPlayers.Count; num2++) { if (sortedPlayers[num2].GetScore() != num) { sortedPosition = num2 + 1; num = sortedPlayers[num2].GetScore(); } sortedPlayers[num2].sortedPosition = sortedPosition; } float score = players[0].GetScore(); int num3 = -1; for (int num4 = 0; num4 < sortedPlayers.Count; num4++) { if (sortedPlayers[num4].GetScore() == score && num3 == -1) { num3 = num4; } if (sortedPlayers[num4].isLocalPlayer) { currentPlayerPosition = num4; break; } } if (num3 != -1 && currentPlayerPosition != num3) { TournamentPlayer value = sortedPlayers[num3]; sortedPlayers[num3] = sortedPlayers[currentPlayerPosition]; sortedPlayers[currentPlayerPosition] = value; currentPlayerPosition = num3; } } public void FinalSortPlayers(float[] sendTable) { sortedPlayers.Clear(); for (int i = 0; i < sendTable.Length; i += 2) { TournamentPlayer player = GetPlayer((int)sendTable[i]); if ((bool)player) { player.SetScore(tournamentDefinition.goalType, sendTable[i + 1]); sortedPlayers.Add(player); } } } public static TournamentFish FishToTournamentFish(Fish fish) { TournamentFish tournamentFish = new TournamentFish(); tournamentFish.species = fish.species; tournamentFish.weight = fish.Weight; tournamentFish.weightMinMax = fish.WeightMinMax; return tournamentFish; } public void PlayerCatchFish(int playerId, TournamentFish fish) { Debug.Log("Tournament Catch Fish: " + ((!GetPlayer(playerId)) ? "null" : GetPlayer(playerId).playerName) + " " + fish.species.ToString() + " " + fish.weight); SortPlayers(); hudTournament.UpdatePlayerWidgets(); if (gameController.isMultiplayer) { gameController.multiplayerManager.UpdateMyScore(); } if (CheckFinishCondition()) { FinishTournament(); } } public bool CheckFinishCondition() { if (!PhotonNetwork.isMasterClient) { return false; } if (tournamentDefinition.finishCondition == TournamentDefinition.FinishCondition.TIME) { return false; } if (tournamentDefinition.goalType == TournamentDefinition.GoalType.AMOUNT) { for (int i = 0; i < players.Count; i++) { if (players[i].GetScore() >= tournamentDefinition.goalAmount) { return true; } } } else if (tournamentDefinition.goalType == TournamentDefinition.GoalType.WEIGHT_SUM) { for (int j = 0; j < players.Count; j++) { if (players[j].GetScore() >= tournamentDefinition.goalAmount) { return true; } } } else if (tournamentDefinition.goalType == TournamentDefinition.GoalType.WEIGHT_MAX) { for (int k = 0; k < players.Count; k++) { if (players[k].GetScore() >= tournamentDefinition.goalAmount) { return true; } } } return false; } public void StartTournament() { tournamentStarted = true; hudTournament.UpdatePlayerWidgets(); } public void FinishTournament() { Debug.Log("Tournament Finished"); tournamentActive = false; if (PhotonNetwork.isMasterClient) { LeanTween.delayedCall(4f, ShowFinishScreen); } else { ShowFinishScreen(); } if (gameController.isMultiplayer) { gameController.multiplayerManager.FinishTournament(); } } private void ShowFinishScreen() { gameController.hudManager.ShowTournamentFinishScreen(true); hudTournament.UpdateFinishWidgets(); } }