475 lines
13 KiB
C#
475 lines
13 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using BitStrap;
|
|
using CodeStage.AntiCheat.ObscuredTypes;
|
|
using UnityEngine;
|
|
|
|
public class GlobalTournamentManager : MonoBehaviour
|
|
{
|
|
[Serializable]
|
|
public class GlobalTournamentDefinition
|
|
{
|
|
public string tournamentName = "Tournament Name";
|
|
|
|
public Sprite icon;
|
|
|
|
public GoalType goalType;
|
|
|
|
public FinishCondition finishCondition;
|
|
|
|
public List<ObscuredInt> fisheryIds = new List<ObscuredInt>();
|
|
|
|
public List<ObscuredInt> fishIds = new List<ObscuredInt>();
|
|
|
|
public ObscuredBool techniqueSpinning = true;
|
|
|
|
public ObscuredBool techniqueFloat = true;
|
|
|
|
public ObscuredBool techniqueFly = true;
|
|
|
|
public ObscuredBool techniqueGround = true;
|
|
|
|
public ObscuredBool fromBoat = true;
|
|
|
|
public ObscuredBool fromCoast = true;
|
|
|
|
public ObscuredFloat limit = 20f;
|
|
|
|
public Vector2 playerLevel = new Vector2(0f, 666f);
|
|
|
|
public List<PlayerSettingsMy.Difficulty> difficulties = new List<PlayerSettingsMy.Difficulty>();
|
|
|
|
public Vector2 hours = new Vector2(0f, 24f);
|
|
|
|
public ObscuredString startTime = "2018.03.05#15.00";
|
|
|
|
public ObscuredString finishTime = "2018.03.06#15.00";
|
|
|
|
[ReadOnly]
|
|
public string timeLeftString = string.Empty;
|
|
|
|
[ReadOnly]
|
|
public DateTime startDateTime = default(DateTime);
|
|
|
|
[ReadOnly]
|
|
public DateTime finishDateTime = default(DateTime);
|
|
|
|
public List<int> moneyRewards = new List<int>();
|
|
|
|
public List<string> equipmentRewards = new List<string>();
|
|
|
|
[HideInInspector]
|
|
public ObscuredUInt startDayInt = 0u;
|
|
|
|
[HideInInspector]
|
|
public ObscuredUInt startHourInt = 0u;
|
|
|
|
[HideInInspector]
|
|
public ObscuredUInt finishDayInt = 0u;
|
|
|
|
[HideInInspector]
|
|
public ObscuredUInt finishHourInt = 0u;
|
|
|
|
public bool isSignedUp;
|
|
|
|
public TournamentState state;
|
|
|
|
public bool HasAnyRestrictions()
|
|
{
|
|
return HasTechniqueRestrictions() || HasBoatRestrictions() || HasHourRestrictions() || HasLevelRestrictions() || HasDifficultyRestrictions();
|
|
}
|
|
|
|
public bool HasTechniqueRestrictions()
|
|
{
|
|
return !techniqueSpinning || !techniqueFloat || !techniqueFly || !techniqueGround;
|
|
}
|
|
|
|
public bool HasBoatRestrictions()
|
|
{
|
|
return !fromBoat || !fromCoast;
|
|
}
|
|
|
|
public bool HasHourRestrictions()
|
|
{
|
|
return hours.x != 0f || hours.y != 24f;
|
|
}
|
|
|
|
public bool HasLevelRestrictions()
|
|
{
|
|
return playerLevel.x != 0f || playerLevel.y != 666f;
|
|
}
|
|
|
|
public bool HasDifficultyRestrictions()
|
|
{
|
|
return difficulties.Count > 0;
|
|
}
|
|
|
|
public bool CheckPlayerLevel(int level)
|
|
{
|
|
return (float)level >= playerLevel.x && (float)level <= playerLevel.y;
|
|
}
|
|
|
|
public bool CheckPlayerDifficulty(PlayerSettingsMy.Difficulty difficulty)
|
|
{
|
|
return difficulties.Count == 0 || difficulties.Contains(difficulty);
|
|
}
|
|
|
|
public string GetStateName()
|
|
{
|
|
if (state == TournamentState.INACTIVE)
|
|
{
|
|
return Utilities.GetTranslation("TOURNAMENT/INACTIVE");
|
|
}
|
|
if (state == TournamentState.ACTIVE)
|
|
{
|
|
return Utilities.GetTranslation("TOURNAMENT/ACTIVE");
|
|
}
|
|
if (state == TournamentState.FINISHED)
|
|
{
|
|
return Utilities.GetTranslation("TOURNAMENT/FINISHED");
|
|
}
|
|
return string.Empty;
|
|
}
|
|
|
|
public int GetMoneyReward(int place)
|
|
{
|
|
if (place >= moneyRewards.Count)
|
|
{
|
|
return 0;
|
|
}
|
|
return moneyRewards[place];
|
|
}
|
|
|
|
public string GetEquipmentReward(int place)
|
|
{
|
|
if (place >= equipmentRewards.Count)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
return equipmentRewards[place];
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
isSignedUp = false;
|
|
state = TournamentState.INACTIVE;
|
|
}
|
|
}
|
|
|
|
public enum TournamentState
|
|
{
|
|
INACTIVE = 0,
|
|
ACTIVE = 1,
|
|
FINISHED = 2,
|
|
COUNT = 3
|
|
}
|
|
|
|
public enum GoalType
|
|
{
|
|
AMOUNT = 0,
|
|
WEIGHT_SUM = 1,
|
|
WEIGHT_MAX = 2,
|
|
COUNT = 3
|
|
}
|
|
|
|
public enum FinishCondition
|
|
{
|
|
TIME = 0,
|
|
GOAL_FIRST = 1,
|
|
COUNT = 2
|
|
}
|
|
|
|
private static GlobalTournamentManager instance;
|
|
|
|
public GlobalTournamentDefinition currentTournament = new GlobalTournamentDefinition();
|
|
|
|
public GlobalTournamentScreen globalTournamentScreen;
|
|
|
|
private Coroutine checkServerTimeCoroutine;
|
|
|
|
public float serverTimeTimer;
|
|
|
|
public bool updateServerTimeTimer;
|
|
|
|
public static GlobalTournamentManager Instance
|
|
{
|
|
get
|
|
{
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
private GlobalTournamentManager()
|
|
{
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
if (instance == null)
|
|
{
|
|
instance = this;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
Initialize();
|
|
}
|
|
|
|
public void Initialize()
|
|
{
|
|
uint dayInt;
|
|
uint hourInt;
|
|
Utilities.ServerTimeToInts(currentTournament.startTime, out dayInt, out hourInt);
|
|
currentTournament.startDayInt = dayInt;
|
|
currentTournament.startHourInt = hourInt;
|
|
Utilities.ServerTimeToInts(currentTournament.finishTime, out dayInt, out hourInt);
|
|
currentTournament.finishDayInt = dayInt;
|
|
currentTournament.finishHourInt = hourInt;
|
|
string text = currentTournament.startTime;
|
|
text = text.Replace("#", ".");
|
|
string[] array = text.Split("."[0]);
|
|
currentTournament.startDateTime = new DateTime(Convert.ToInt32(array[0]), Convert.ToInt32(array[1]), Convert.ToInt32(array[2]), Convert.ToInt32(array[3]), Convert.ToInt32(array[4]), 0);
|
|
text = currentTournament.startTime;
|
|
text = currentTournament.finishTime;
|
|
text = text.Replace("#", ".");
|
|
array = text.Split("."[0]);
|
|
currentTournament.finishDateTime = new DateTime(Convert.ToInt32(array[0]), Convert.ToInt32(array[1]), Convert.ToInt32(array[2]), Convert.ToInt32(array[3]), Convert.ToInt32(array[4]), 0);
|
|
updateServerTimeTimer = true;
|
|
updateServerTimeTimer = false;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (updateServerTimeTimer)
|
|
{
|
|
serverTimeTimer -= Time.unscaledDeltaTime;
|
|
if (serverTimeTimer <= 0f)
|
|
{
|
|
CheckServerTime();
|
|
serverTimeTimer = 60f;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void FishCaught(Fish fish)
|
|
{
|
|
if (currentTournament.state != TournamentState.ACTIVE)
|
|
{
|
|
Debug.Log("GlobalTournamentManager !ACTIVE");
|
|
}
|
|
else if (!currentTournament.isSignedUp)
|
|
{
|
|
Debug.Log("GlobalTournamentManager !IS_SIGNED_IN");
|
|
}
|
|
else
|
|
{
|
|
if (!CheckRequirements(fish))
|
|
{
|
|
return;
|
|
}
|
|
SteamStatsManager.SteamUser mySteamUser = SteamStatsManager.Instance.GetMySteamUser();
|
|
if (mySteamUser == null)
|
|
{
|
|
return;
|
|
}
|
|
if ((float)(int)mySteamUser.tournamentTotalFishCount >= (float)currentTournament.limit)
|
|
{
|
|
Debug.Log(string.Concat("GlobalTournamentManager !CheckRequirements: tournamentTotalFishCount >= ", mySteamUser.tournamentTotalFishCount, " / ", currentTournament.limit));
|
|
return;
|
|
}
|
|
++mySteamUser.tournamentTotalFishCount;
|
|
mySteamUser.tournamentTotalFishWeight = (int)mySteamUser.tournamentTotalFishWeight + Mathf.RoundToInt(fish.Weight * 1000f);
|
|
if (fish.Weight * 1000f > (float)(int)mySteamUser.tournamentBiggestFishWeight)
|
|
{
|
|
mySteamUser.tournamentBiggestFishWeight = Mathf.RoundToInt(fish.Weight * 1000f);
|
|
mySteamUser.tournamentBiggestFishSpecies = (int)fish.species;
|
|
}
|
|
SteamStatsManager.Instance.StoreStats();
|
|
GlobalTournamentSteamworks.Instance.UploadScore(GlobalTournamentSteamworks.Instance.GetCurrentLeaderboardSettings());
|
|
Debug.Log("GlobalTournamentManager FISH_CAUGHT: " + Utilities.GetTranslation(fish.fishName) + " " + fish.Weight);
|
|
}
|
|
}
|
|
|
|
public bool CheckRequirements(Fish fish)
|
|
{
|
|
if ((bool)GameController.Instance.fisheryEditorGame)
|
|
{
|
|
Debug.Log("GlobalTournamentManager !CheckRequirements: fisheryEditorGame");
|
|
return false;
|
|
}
|
|
Debug.Log("playersLevel: " + GlobalSettings.Instance.playerSettings.playersLevel);
|
|
if (!currentTournament.CheckPlayerLevel(GlobalSettings.Instance.playerSettings.playersLevel))
|
|
{
|
|
Debug.LogError("GlobalTournamentManager !CheckRequirements: PLAYER_LEVEL");
|
|
return false;
|
|
}
|
|
if (!currentTournament.CheckPlayerDifficulty(GlobalSettings.Instance.playerSettings.difficulty))
|
|
{
|
|
Debug.LogError("GlobalTournamentManager !CheckRequirements: DIFFICULTY");
|
|
return false;
|
|
}
|
|
if (currentTournament.fisheryIds.Count > 0 && !currentTournament.fisheryIds.Contains(GameController.Instance.fisheryId))
|
|
{
|
|
Debug.LogError("GlobalTournamentManager !CheckRequirements: FISHERY");
|
|
return false;
|
|
}
|
|
if (currentTournament.fishIds.Count > 0 && !currentTournament.fishIds.Contains((int)fish.species))
|
|
{
|
|
Debug.LogError("GlobalTournamentManager !CheckRequirements: FISH");
|
|
return false;
|
|
}
|
|
if ((GameController.Instance.fishingPlayer.currentHands.isFloatRig && !currentTournament.techniqueFloat) || (GameController.Instance.fishingPlayer.currentHands.isSpinningRig && !currentTournament.techniqueSpinning) || (GameController.Instance.fishingPlayer.currentHands.isGroundRig && !currentTournament.techniqueGround) || (GameController.Instance.fishingPlayer.currentHands.isFlyRig && !currentTournament.techniqueFly))
|
|
{
|
|
Debug.LogError("GlobalTournamentManager !CheckRequirements: TECHNIQUE");
|
|
return false;
|
|
}
|
|
if (((bool)GameController.Instance.fishingPlayer.boatSimulator && !currentTournament.fromBoat) || (!GameController.Instance.fishingPlayer.boatSimulator && !currentTournament.fromCoast))
|
|
{
|
|
Debug.LogError("GlobalTournamentManager !CheckRequirements: BOAT");
|
|
return false;
|
|
}
|
|
float num = GameController.Instance.weatherLevelManager.GetHour();
|
|
Debug.Log("hour: " + num);
|
|
if (currentTournament.hours.x < currentTournament.hours.y && (num < currentTournament.hours.x || num >= currentTournament.hours.y))
|
|
{
|
|
Debug.LogError("GlobalTournamentManager !CheckRequirements: HOUR");
|
|
return false;
|
|
}
|
|
if (currentTournament.hours.x > currentTournament.hours.y && num < currentTournament.hours.x && num >= currentTournament.hours.y)
|
|
{
|
|
Debug.LogError("GlobalTournamentManager !CheckRequirements: HOUR NIGHT");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public void StartTournament()
|
|
{
|
|
if (SteamStatsManager.Instance.GetMySteamUser() != null)
|
|
{
|
|
SteamStatsManager.Instance.mySteamUser.tournamentCanGatherReward = 1;
|
|
SteamStatsManager.Instance.StoreStats();
|
|
}
|
|
Debug.Log("GlobalTournamentManager StartTournament");
|
|
}
|
|
|
|
public void FinishTournament()
|
|
{
|
|
if ((bool)SteamStatsManager.Instance)
|
|
{
|
|
SteamStatsManager.Instance.ResetTournamentStats();
|
|
}
|
|
currentTournament.isSignedUp = false;
|
|
if ((bool)GlobalSettings.Instance && (bool)GlobalSettings.Instance.playerSettings && GlobalSettings.Instance.saveManager.isProfileLoaded)
|
|
{
|
|
GlobalSettings.Instance.playerSettings.Save();
|
|
}
|
|
Debug.Log("GlobalTournamentManager FinishTournament");
|
|
}
|
|
|
|
public int GetCurrentScoreInt()
|
|
{
|
|
if (!SteamStatsManager.Instance)
|
|
{
|
|
return -1;
|
|
}
|
|
SteamStatsManager.SteamUser mySteamUser = SteamStatsManager.Instance.GetMySteamUser();
|
|
if (mySteamUser != null)
|
|
{
|
|
if (currentTournament.goalType == GoalType.AMOUNT)
|
|
{
|
|
return mySteamUser.tournamentTotalFishCount;
|
|
}
|
|
if (currentTournament.goalType == GoalType.WEIGHT_MAX)
|
|
{
|
|
return mySteamUser.tournamentBiggestFishWeight;
|
|
}
|
|
if (currentTournament.goalType == GoalType.WEIGHT_SUM)
|
|
{
|
|
return mySteamUser.tournamentTotalFishWeight;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
[Button]
|
|
public void CheckServerTime()
|
|
{
|
|
if (checkServerTimeCoroutine != null)
|
|
{
|
|
StopCoroutine(checkServerTimeCoroutine);
|
|
checkServerTimeCoroutine = null;
|
|
}
|
|
checkServerTimeCoroutine = StartCoroutine(CheckServerTimeCoroutine());
|
|
}
|
|
|
|
public IEnumerator CheckServerTimeCoroutine()
|
|
{
|
|
uint dayInt = 0u;
|
|
uint hourInt = 0u;
|
|
DateTime currentDateTime = Utilities.GetCurrentDateTime();
|
|
Utilities.PlatformTimeToInts(currentDateTime, out dayInt, out hourInt);
|
|
if (dayInt == 0)
|
|
{
|
|
Debug.LogError("Sth wrong with server string " + currentDateTime.ToLongDateString());
|
|
yield break;
|
|
}
|
|
if (dayInt > (uint)currentTournament.finishDayInt || (dayInt == (uint)currentTournament.finishDayInt && hourInt >= (uint)currentTournament.finishHourInt))
|
|
{
|
|
if (currentTournament.state == TournamentState.ACTIVE || currentTournament.state == TournamentState.INACTIVE)
|
|
{
|
|
FinishTournament();
|
|
}
|
|
currentTournament.state = TournamentState.FINISHED;
|
|
}
|
|
else if (dayInt > (uint)currentTournament.startDayInt || (dayInt == (uint)currentTournament.startDayInt && hourInt >= (uint)currentTournament.startHourInt))
|
|
{
|
|
if (currentTournament.state == TournamentState.INACTIVE)
|
|
{
|
|
StartTournament();
|
|
}
|
|
currentTournament.state = TournamentState.ACTIVE;
|
|
}
|
|
else
|
|
{
|
|
currentTournament.state = TournamentState.INACTIVE;
|
|
if (SteamStatsManager.Instance.GetMySteamUser() != null && (int)SteamStatsManager.Instance.mySteamUser.tournamentTotalFishCount > 0)
|
|
{
|
|
SteamStatsManager.Instance.ResetTournamentStats();
|
|
}
|
|
}
|
|
CalculateTimeLeft(string.Empty, currentDateTime);
|
|
if (!globalTournamentScreen)
|
|
{
|
|
globalTournamentScreen.ServerTimeUpdated();
|
|
}
|
|
}
|
|
|
|
public void CalculateTimeLeft(string currentString, DateTime currentDateTime)
|
|
{
|
|
if (currentTournament.state == TournamentState.FINISHED)
|
|
{
|
|
currentTournament.timeLeftString = string.Empty;
|
|
return;
|
|
}
|
|
TimeSpan timeSpan = ((currentTournament.state != TournamentState.INACTIVE) ? currentTournament.finishDateTime : currentTournament.startDateTime) - currentDateTime;
|
|
currentTournament.timeLeftString = string.Empty + timeSpan.Days + Utilities.GetTranslation("d") + " " + timeSpan.Hours + Utilities.GetTranslation("h") + " " + timeSpan.Minutes + Utilities.GetTranslation("m");
|
|
Debug.Log("timeLeftString 1: " + currentTournament.timeLeftString);
|
|
}
|
|
|
|
public void SplitHour(int source, out int hour, out int minutes)
|
|
{
|
|
hour = Mathf.FloorToInt((float)source * 0.01f);
|
|
minutes = source - hour * 100;
|
|
}
|
|
|
|
public string GetDateString(string date)
|
|
{
|
|
string[] array = date.Split("#"[0]);
|
|
array[1] = array[1].Replace(".", ":");
|
|
return array[0] + " " + array[1] + " (UTC)";
|
|
}
|
|
}
|