chip-gameV3/Assets/Scripts/Settings/PasswordCatcher.cs

75 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using UnityEngine;
public class PasswordCatcher : MonoBehaviour
{
private bool passwordForSettings;
public void Initialize()
{
passwordForSettings = SettingsManager.GetBool("PasswordOnSettings", false);
SettingsManager.OnSettingsChanged.AddListener(ApplySettings);
}
private void ApplySettings()
{
bool passwordForSettingsNewValue = SettingsManager.GetBool("PasswordOnSettings", false);
if (NeedToSetPassword(passwordForSettingsNewValue))
{
passwordForSettings = passwordForSettingsNewValue;
GameManager.instance.stateMachine.ChangeState(new SetupPasswordState());
}
if (NeedToRemovePassword(passwordForSettingsNewValue))
{
ExternalFilesGetter.SetPassword("");
print("Пароль сброшен");
}
}
private bool NeedToRemovePassword(bool newValue) =>
//значение поменялось на не нужен пароль
newValue != passwordForSettings && newValue == false;
private bool NeedToSetPassword(bool passwordForSettingsNewValue)
{
//поменялось значение настройки на "Пароль нужен"
bool changeStateToNeedPassword = passwordForSettingsNewValue != passwordForSettings && passwordForSettingsNewValue;
if (changeStateToNeedPassword) return true;
//Должен быть пароль но его нет или он не валидный.
if(passwordForSettingsNewValue && PasswordFileNonValid()) return true;
return false;
}
private bool PasswordFileNonValid()
{
string password = ExternalFilesGetter.GetSettingsPassword();
if (password == null) return true;
if(password.Length % 2 == 1) return true;//длина должны быть четной
if (!PwdCombinationsValid(SplitPasswordIntoPairs(password))) return true; // парольные комбинации валидны?
return false;
}
List<string> SplitPasswordIntoPairs(string password)
{
List<string> result = new List<string>();
for (int i = 0; i < password.Length; i += 2)
{
result.Add(password.Substring(i, 2));
}
return result;
}
private bool PwdCombinationsValid(List<string> combinations)
{
List<string> validCombinations = new List<string> { "L1", "L2", "R1", "R2", "A1", "A2", "P1", "P2" };
foreach (string combination in combinations)
{
if (!validCombinations.Contains(combination)) return false;
}
return true;
}
}