-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTesterLoginMenu.cs
More file actions
111 lines (96 loc) · 3.64 KB
/
TesterLoginMenu.cs
File metadata and controls
111 lines (96 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
namespace MyGame.PlayTesters
{
public class TesterLoginMenu : MonoBehaviour
{
[SerializeField]private PlaytimeReporter _playtimeReporter;
[SerializeField]private TMP_InputField _accessKeyInput;
[SerializeField]private TMP_Text _statusText;
[SerializeField]private float _loginCooldownSeconds = 30f;
[SerializeField]private int _maxFailedAttempts = 4;
#if UNITY_EDITOR || DEVELOPMENT_BUILD
private const string BaseUrl = "http://localhost:5183/api";
#else
private const string BaseUrl = "https://my-playtestersapi-prod.up.railway.app/api";
#endif
private bool _isLoggingIn;
private int _failedAttemptCount;
[Serializable]
private class LoginRequest
{
public string AccessKey;
}
[Serializable]
private class LoginResponse
{
public bool success;
public string status;
public string message;
}
public void OnLoginClicked()
{
if (_isLoggingIn)
return;
var accessKey = _accessKeyInput.text.Trim();
if (string.IsNullOrWhiteSpace(accessKey))
{
_statusText.text = "Please enter your key.";
return;
}
StartCoroutine(LoginCoroutine(accessKey));
}
private IEnumerator LoginCoroutine(string accessKey)
{
_isLoggingIn = true;
_statusText.text = "Validating...";
var request = new LoginRequest
{
AccessKey = accessKey
};
var json = JsonUtility.ToJson(request);
var apiUrl = $"{BaseUrl}/testers/validate-access";
using var www = new UnityWebRequest(apiUrl, "POST");
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(json);
www.uploadHandler = new UploadHandlerRaw(bodyRaw);
www.downloadHandler = new DownloadHandlerBuffer();
www.SetRequestHeader("Content-Type", "application/json");
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError)
{
_statusText.text = $"{www.error}.";
_isLoggingIn = false;
yield break;
}
var response = JsonUtility.FromJson<LoginResponse>(www.downloadHandler.text);
if (!response.success)
{
var errorMessage = response.status switch
{
"Invalid" => "Invalid access key. Please try again.",
"CriticalError" => "Server error. Please try again later.",
_ => "Unexpected error. Please contact the dev team."
};
_statusText.text = errorMessage;
_isLoggingIn = false;
_failedAttemptCount++;
if (_failedAttemptCount == _maxFailedAttempts)
{
_statusText.text = "Too many attempts.\n " +
$"Try again in {_loginCooldownSeconds} seconds.";
_isLoggingIn = true;
yield return new WaitForSeconds(_loginCooldownSeconds);
_failedAttemptCount = 0;
_isLoggingIn = false;
}
yield break;
}
_playtimeReporter.StartReporting(request.AccessKey);
SceneManager.LoadScene("MainMenu");
}
}
}