Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial launcher redesign #36

Merged
merged 14 commits into from
Oct 2, 2024
Merged
27 changes: 27 additions & 0 deletions Fuyu.Backend.Core/Controllers/AccountGamesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Threading.Tasks;
using Fuyu.Common.Networking;
using Fuyu.Common.Serialization;
using Fuyu.Backend.Core.DTO.Responses;
using Fuyu.Backend.Core.Services;

namespace Fuyu.Backend.Core.Controllers
TheSpartaPT marked this conversation as resolved.
Show resolved Hide resolved
{
public class AccountGamesController : HttpController
{
public AccountGamesController() : base("/account/games")
{
}

public override async Task RunAsync(HttpContext context)
{
var sessionId = context.GetSessionId();
var result = AccountService.GetGames(sessionId);
var response = new AccountGamesResponse()
{
Games = result
};

await context.SendJsonAsync(Json.Stringify(response));
}
}
}
20 changes: 20 additions & 0 deletions Fuyu.Backend.Core/Controllers/AccountLogoutController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Threading.Tasks;
using Fuyu.Common.Networking;

namespace Fuyu.Backend.Core.Controllers
{
public class AccountLogoutController : HttpController
{
public AccountLogoutController() : base("/account/logout")
{
}

public override async Task RunAsync(HttpContext context)
{
var sessionId = context.GetSessionId();
CoreOrm.RemoveSession(sessionId);

await context.SendJsonAsync("{}");
}
}
}
4 changes: 2 additions & 2 deletions Fuyu.Backend.Core/Controllers/AccountRegisterController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Threading.Tasks;
using Fuyu.Backend.Core.DTO.Requests;
using Fuyu.Backend.Core.DTO.Responses;
using Fuyu.Common.Networking;
using Fuyu.Common.Serialization;
using Fuyu.Backend.Core.DTO.Requests;
using Fuyu.Backend.Core.DTO.Responses;
using Fuyu.Backend.Core.Services;

namespace Fuyu.Backend.Core.Controllers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.Threading.Tasks;
using Fuyu.Backend.Core.DTO.Requests;
using Fuyu.Backend.Core.DTO.Responses;
using Fuyu.Common.Networking;
using Fuyu.Common.Serialization;
using Fuyu.Backend.Core.DTO.Requests;
using Fuyu.Backend.Core.Services;

namespace Fuyu.Backend.Core.Controllers
Expand All @@ -18,12 +17,8 @@ public override async Task RunAsync(HttpContext context)
var request = await context.GetJsonAsync<AccountRegisterGameRequest>();
var sessionId = context.GetSessionId();
var result = AccountService.RegisterGame(sessionId, request.Game, request.Edition);
var response = new AccountRegisterGameResponse()
{
Status = result
};

await context.SendJsonAsync(Json.Stringify(response));
await context.SendJsonAsync(Json.Stringify(result));
}
}
}
12 changes: 12 additions & 0 deletions Fuyu.Backend.Core/DTO/Response/AccountGamesResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Fuyu.Backend.Core.DTO.Responses
{
[DataContract]
public class AccountGamesResponse
{
[DataMember]
public Dictionary<string, int?> Games;
}
}
3 changes: 3 additions & 0 deletions Fuyu.Backend.Core/DTO/Response/AccountRegisterGameResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ public class AccountRegisterGameResponse
{
[DataMember]
public ERegisterStatus Status;

[DataMember]
public int AccountId;
}
}
2 changes: 2 additions & 0 deletions Fuyu.Backend.Core/Servers/CoreServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ public CoreServer() : base("core", "http://localhost:8000/")
public void RegisterServices()
{
AddHttpController<AccountLoginController>();
AddHttpController<AccountLogoutController>();
AddHttpController<AccountRegisterController>();
AddHttpController<AccountRegisterGameController>();
AddHttpController<AccountGamesController>();
}
}
}
20 changes: 17 additions & 3 deletions Fuyu.Backend.Core/Services/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,18 @@ public static ERegisterStatus RegisterAccount(string username, string password)
return ERegisterStatus.Success;
}

public static ERegisterStatus RegisterGame(string sessionId, string game, string edition)
public static AccountRegisterGameResponse RegisterGame(string sessionId, string game, string edition)
{
var account = CoreOrm.GetAccount(sessionId);

// find existing game
if (account.Games.ContainsKey(game) && account.Games[game].HasValue)
{
return ERegisterStatus.AlreadyExists;
return new AccountRegisterGameResponse()
{
Status = ERegisterStatus.AlreadyExists,
AccountId = -1
};
}

// register game
Expand All @@ -207,7 +211,17 @@ public static ERegisterStatus RegisterGame(string sessionId, string game, string
CoreOrm.SetOrAddAccount(account);
WriteToDisk(account);

return ERegisterStatus.Success;
return new AccountRegisterGameResponse()
{
Status = ERegisterStatus.Success,
AccountId = accountId
};
}

public static Dictionary<string, int?> GetGames(string sessionId)
{
var account = CoreOrm.GetAccount(sessionId);
return account.Games;
}

public static void WriteToDisk(Account account)
Expand Down
46 changes: 42 additions & 4 deletions Fuyu.Launcher.Core/Services/RequestService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Collections.Generic;
using Fuyu.Common.Collections;
using Fuyu.Common.Hashing;
using Fuyu.Common.Networking;
Expand All @@ -24,6 +25,16 @@ static RequestService()
_httpClients.Add("arena", new EftHttpClient(SettingsService.ArenaAddress, string.Empty));
}

private static void HttpPut<T1>(string id, string path, T1 request)
{
var httpc = _httpClients.Get(id);

var requestJson = Json.Stringify(request);
var requestBytes = Encoding.UTF8.GetBytes(requestJson);

httpc.Put(path, requestBytes);
}

private static T2 HttpPost<T1, T2>(string id, string path, T1 request)
{
var httpc = _httpClients.Get(id);
Expand All @@ -38,6 +49,13 @@ private static T2 HttpPost<T1, T2>(string id, string path, T1 request)
return responseValue;
}

public static void ResetSessions()
{
_httpClients.Set("fuyu", new EftHttpClient(SettingsService.FuyuAddress, string.Empty));
_httpClients.Set("eft", new EftHttpClient(SettingsService.EftAddress, string.Empty));
_httpClients.Set("arena", new EftHttpClient(SettingsService.ArenaAddress, string.Empty));
}

public static void CreateSession(string id, string address, string sessionId)
{
_httpClients.Set(id, new EftHttpClient(address, sessionId));
Expand All @@ -58,7 +76,7 @@ public static ERegisterStatus RegisterAccount(string username, string password)
return response.Status;
}

public static string LoginAccount(string username, string password)
public static AccountLoginResponse LoginAccount(string username, string password)
{
var hashedPassword = Sha256.Generate(password);
var request = new AccountLoginRequest()
Expand All @@ -71,10 +89,30 @@ public static string LoginAccount(string username, string password)
"/account/login",
request);

return response.SessionId;
return response;
}

public static ERegisterStatus RegisterGame(string game, string edition)
public static void LogoutAccount()
{
HttpPut<object>(
"fuyu",
"/account/logout",
null);

ResetSessions();
}

public static Dictionary<string, int?> GetGames()
{
var response = HttpPost<object, AccountGamesResponse>(
"fuyu",
"/account/games",
null);

return response.Games;
TheSpartaPT marked this conversation as resolved.
Show resolved Hide resolved
}

public static AccountRegisterGameResponse RegisterGame(string game, string edition)
{
var request = new AccountRegisterGameRequest()
{
Expand All @@ -86,7 +124,7 @@ public static ERegisterStatus RegisterGame(string game, string edition)
"/account/register/game",
request);

return response.Status;
return response;
}

public static string LoginGame(string game, int accountId)
Expand Down
40 changes: 40 additions & 0 deletions Fuyu.Launcher/Components/AboutDialog.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@using System.Diagnostics;

<MudDialog>
<DialogContent>
<MudStack Class="my-6" AlignItems="AlignItems.Center" Justify="Justify.Center">
<MudAvatar Style="height: 70px; width: 70px;" Rounded="true">
<MudImage Src="../img/icon.png"></MudImage>
</MudAvatar>
<MudText Typo="Typo.h6">冬 Fuyu</MudText>
<MudText Typo="Typo.subtitle2">1.0.0</MudText>
<MudStack Row="true">
<MudTooltip Text="Website" ShowOnHover="true" Arrow="true" Placement="Placement.Bottom">
<MudIconButton Icon="@Icons.Material.Filled.Link" Color="Color.Tertiary" OnClick="@(() => OpenUrlOnDefaultBrowser("https://placeholder.com"))" />
</MudTooltip>
<MudTooltip Text="Discord" ShowOnHover="true" Arrow="true" Placement="Placement.Bottom">
<MudIconButton Icon="@Icons.Custom.Brands.Discord" Style="color: #5865F2;" OnClick="@(() => OpenUrlOnDefaultBrowser("https://discord.com/invite/placeholder"))" />
</MudTooltip>
<MudTooltip Text="GitHub" ShowOnHover="true" Arrow="true" Placement="Placement.Bottom">
<MudIconButton Icon="@Icons.Custom.Brands.GitHub" Style="color: #F0F6FC;" OnClick="@(() => OpenUrlOnDefaultBrowser("https://github.com/project-fika/Fuyu"))" />
</MudTooltip>
</MudStack>
<MudText Typo="Typo.body2">Copyright © 2024 seionmoya</MudText>
</MudStack>
</DialogContent>
</MudDialog>

@code {
[CascadingParameter]
private MudDialogInstance MudDialog { get; set; }

private void OpenUrlOnDefaultBrowser(string link)
{
var info = new ProcessStartInfo(link)
{
UseShellExecute = true
};

Process.Start(info);
}
}
Loading