feat: add auth screen & buggy auth logic
This commit is contained in:
parent
662b65a605
commit
f433a7c515
File diff suppressed because it is too large
Load Diff
Binary file not shown.
2242
Assets/Scenes/SampleScene.unity
generated
2242
Assets/Scenes/SampleScene.unity
generated
File diff suppressed because it is too large
Load Diff
95
Assets/_Scripts/Managers/AuthenticationManager.cs
Normal file
95
Assets/_Scripts/Managers/AuthenticationManager.cs
Normal file
@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Linq;
|
||||
using DatabaseModels.Requests;
|
||||
using DatabaseModels.Responses;
|
||||
using UnityEngine;
|
||||
|
||||
public class AuthenticationManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private UIManager _uiManager;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (SessionStore.UserData == null)
|
||||
{
|
||||
_uiManager._authenticationMenu.SetActive(true);
|
||||
}
|
||||
}
|
||||
|
||||
public async void Login()
|
||||
{
|
||||
var credentials = _uiManager.GetAuthenticationCredentials();
|
||||
|
||||
var authResponse = await HttpClient.Post<AuthenticationResponse>(
|
||||
$"{SessionStore.ApiUrl}/auth/login",
|
||||
new AuthenticationRequest
|
||||
{
|
||||
Username = credentials.username, Password = credentials.password
|
||||
});
|
||||
|
||||
if (authResponse == null)
|
||||
{
|
||||
_uiManager.SetAuthenticationErrorMessage("Server is unavailable.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (authResponse.IsError)
|
||||
{
|
||||
_uiManager.SetAuthenticationErrorMessage(authResponse.ErrorMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
SessionStore.Jwt = authResponse.Token;
|
||||
|
||||
var handler = new JwtSecurityTokenHandler();
|
||||
var token = handler.ReadJwtToken(authResponse.Token);
|
||||
|
||||
SessionStore.UserData = new UserData
|
||||
{
|
||||
Id = Int32.Parse(token.Claims.First(c => c.Type == "id").Value),
|
||||
Username = credentials.username,
|
||||
Password = credentials.password
|
||||
};
|
||||
|
||||
_uiManager._authenticationMenu.SetActive(false);
|
||||
}
|
||||
|
||||
public async void Register()
|
||||
{
|
||||
var credentials = _uiManager.GetAuthenticationCredentials();
|
||||
|
||||
var authResponse = await HttpClient.Post<AuthenticationResponse>(
|
||||
$"{SessionStore.ApiUrl}/auth/register",
|
||||
new AuthenticationRequest
|
||||
{
|
||||
Username = credentials.username, Password = credentials.password
|
||||
});
|
||||
|
||||
if (authResponse == null)
|
||||
{
|
||||
_uiManager.SetAuthenticationErrorMessage("Server is unavailable.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (authResponse.IsError)
|
||||
{
|
||||
_uiManager.SetAuthenticationErrorMessage(authResponse.ErrorMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
SessionStore.Jwt = authResponse.Token;
|
||||
|
||||
var handler = new JwtSecurityTokenHandler();
|
||||
var token = handler.ReadJwtToken(authResponse.Token);
|
||||
|
||||
SessionStore.UserData = new UserData
|
||||
{
|
||||
Id = Int32.Parse(token.Claims.First(c => c.Type == "id").Value),
|
||||
Username = credentials.username,
|
||||
Password = credentials.password
|
||||
};
|
||||
|
||||
_uiManager._authenticationMenu.SetActive(false);
|
||||
}
|
||||
}
|
11
Assets/_Scripts/Managers/AuthenticationManager.cs.meta
generated
Normal file
11
Assets/_Scripts/Managers/AuthenticationManager.cs.meta
generated
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 550f95f310a8759f696f97ff1fda138f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -99,11 +99,6 @@ public class ScoreManager : MonoBehaviour
|
||||
|
||||
private async Task SaveHighScore()
|
||||
{
|
||||
if (_currentScore <= _highScore)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_highScore = _currentScore;
|
||||
SessionStore.HighScore = _highScore;
|
||||
|
||||
@ -140,7 +135,8 @@ public class ScoreManager : MonoBehaviour
|
||||
case GameState.Game:
|
||||
break;
|
||||
case GameState.GameOver:
|
||||
await SaveHighScore();
|
||||
_uiManager.SetGameOverScore(_currentScore);
|
||||
if (_currentScore > _highScore) await SaveHighScore();
|
||||
await _scoreboardManager.SpawnScoreboardRecords();
|
||||
break;
|
||||
default:
|
||||
|
@ -18,9 +18,9 @@ public class ScoreboardManager : MonoBehaviour
|
||||
}
|
||||
|
||||
_uiManager._scoreboardLoadingScreen.SetActive(true);
|
||||
_uiManager.DestroyAllScoreboardRecords();
|
||||
await _uiManager.DestroyAllScoreboardRecords();
|
||||
var filteredScoreboard = await GetFilteredScoreboard();
|
||||
_uiManager.InstantiateScoreboardRecords(filteredScoreboard.records, filteredScoreboard.firstRecordIndex);
|
||||
await _uiManager.InstantiateScoreboardRecords(filteredScoreboard.records, filteredScoreboard.firstRecordIndex);
|
||||
_uiManager._scoreboardLoadingScreen.SetActive(false);
|
||||
|
||||
async Task<(ScoreboardRecordDto[] records, int firstRecordIndex)> GetFilteredScoreboard()
|
@ -19,6 +19,15 @@ public class UIManager : MonoBehaviour
|
||||
[SerializeField] private Slider _experienceSlider;
|
||||
[SerializeField] private float _sliderSmoothTime;
|
||||
private float _sliderVelocity;
|
||||
|
||||
[Header("Game Over Menu")]
|
||||
[SerializeField] private TextMeshProUGUI _gameOverScoreText;
|
||||
|
||||
[Header("Authentication Menu")]
|
||||
[SerializeField] public GameObject _authenticationMenu;
|
||||
[SerializeField] private TMP_InputField _usernameInputField;
|
||||
[SerializeField] private TMP_InputField _passwordInputField;
|
||||
[SerializeField] private TextMeshProUGUI _authenticationErrorText;
|
||||
|
||||
[Header("Scoreboard")]
|
||||
[SerializeField] private RectTransform _scoreboardScrollViewContent;
|
||||
@ -67,7 +76,7 @@ public class UIManager : MonoBehaviour
|
||||
isFull = Math.Abs(_experienceSlider.value - _experienceSlider.maxValue) < 0.1f;
|
||||
}
|
||||
|
||||
public void InstantiateScoreboardRecords(ScoreboardRecordDto[] records, int firstRecordIndex)
|
||||
public Task InstantiateScoreboardRecords(ScoreboardRecordDto[] records, int firstRecordIndex)
|
||||
{
|
||||
_scoreboardScrollViewContent.sizeDelta = new Vector2(0, records.Length * 100);
|
||||
|
||||
@ -77,6 +86,10 @@ public class UIManager : MonoBehaviour
|
||||
{
|
||||
yPos = _scoreboardScrollViewContent.sizeDelta.y / records.Length / 100;
|
||||
}
|
||||
else if (records.First().User.Username == SessionStore.UserData.Username)
|
||||
{
|
||||
yPos = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
yPos = _scoreboardScrollViewContent.sizeDelta.y / records.Length * 2f;
|
||||
@ -100,14 +113,33 @@ public class UIManager : MonoBehaviour
|
||||
|
||||
record.GetComponentInChildren<TextMeshProUGUI>().text = $"{firstRecordIndex + i + 1}. {records[i].User.Username}: {records[i].Score}";
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void DestroyAllScoreboardRecords()
|
||||
public Task DestroyAllScoreboardRecords()
|
||||
{
|
||||
for (int i = 0; i < _scoreboardScrollViewContent.transform.childCount; i++)
|
||||
{
|
||||
DestroyImmediate(_scoreboardScrollViewContent.transform.GetChild(i).gameObject);
|
||||
Destroy(_scoreboardScrollViewContent.transform.GetChild(i).gameObject);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void SetGameOverScore(int value)
|
||||
{
|
||||
_gameOverScoreText.text = value.ToString();
|
||||
}
|
||||
|
||||
public (string username, string password) GetAuthenticationCredentials()
|
||||
{
|
||||
return (_usernameInputField.text, _passwordInputField.text);
|
||||
}
|
||||
|
||||
public void SetAuthenticationErrorMessage(string message)
|
||||
{
|
||||
_authenticationErrorText.text = message;
|
||||
}
|
||||
|
||||
private async void OnGameStateChange(GameState newGameState)
|
||||
|
@ -44,7 +44,7 @@ public static class HttpClient
|
||||
await Task.Delay(10);
|
||||
}
|
||||
|
||||
while (!postRequest.downloadHandler.isDone)
|
||||
while (!postRequest.downloadHandler.isDone && postRequest.result != UnityWebRequest.Result.ProtocolError && postRequest.result != UnityWebRequest.Result.ConnectionError)
|
||||
{
|
||||
await Task.Delay(10);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user