Skip to content

Commit

Permalink
Merge pull request #27 from seionmoya/modify-account-editions
Browse files Browse the repository at this point in the history
WIP: Account edition handling
  • Loading branch information
seionmoya authored Sep 3, 2024
2 parents ab2e2b4 + e5d7511 commit 5acb8f8
Show file tree
Hide file tree
Showing 18 changed files with 197 additions and 28 deletions.
8 changes: 8 additions & 0 deletions Fuyu.Launcher/Pages/Account.razor
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

private void StartEft_Clicked()
{
// NOTE: hack until wipe screen is created
// -- seionmoya, 2024/09/03
RequestService.RegisterGame(EGame.EFT, "unheard");

using (var process = ProcessService.StartEft(SettingsService.EftDirectory, sessionId, SettingsService.EftAddress))
{
process.Start();
Expand All @@ -25,6 +29,10 @@

private void StartArena_Clicked()
{
// NOTE: hack until wipe screen is created
// -- seionmoya, 2024/09/03
RequestService.RegisterGame(EGame.Arena, "ryzhy");

using (var process = ProcessService.StartArena(SettingsService.ArenaDirectory, sessionId, SettingsService.ArenaAddress))
{
process.Start();
Expand Down
7 changes: 3 additions & 4 deletions Fuyu.Launcher/Pages/Login.razor
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@
// * show error
// -- seionmoya, 2024/09/02
}
else
{
Navigation.NavigateTo($"/account?sessionid={sessionId}");
}

RequestService.CreateSession(sessionId);
Navigation.NavigateTo($"/account?sessionid={sessionId}");
}
}
11 changes: 6 additions & 5 deletions Fuyu.Launcher/Pages/Register.razor
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
<FluentTextField TextFieldType="TextFieldType.Text" @bind-Value="username" Style="width: 100%;"></FluentTextField>
<FluentLabel>Password:</FluentLabel>
<FluentTextField TextFieldType="TextFieldType.Text" @bind-Value="password" Style="width: 100%;"></FluentTextField>
<FluentLabel>Edition:</FluentLabel>
<FluentTextField TextFieldType="TextFieldType.Text" @bind-Value="edition" Style="width: 100%;"></FluentTextField>
<br />
<br />
<FluentButton Appearance="Appearance.Accent" OnClick="Register_Clicked">Register</FluentButton>
Expand All @@ -27,17 +25,18 @@
{
string username = string.Empty;
string password = string.Empty;
string edition = "unheard";

private void Register_Clicked(EventArgs e)
{
var hashedPassword = HashService.GetSHA256(password);
var registerStatus = RequestService.RegisterAccount(username, hashedPassword, edition);
var registerStatus = RequestService.RegisterAccount(username, hashedPassword);

switch (registerStatus)
{
case ERegisterStatus.Success:
Navigation.NavigateTo("/login");
// TODO:
// * notify user of successful registration
// -- seionmoya, 2024/09/03
break;

case ERegisterStatus.AlreadyExists:
Expand All @@ -46,5 +45,7 @@
// -- seionmoya, 2024/09/02
break;
}

Navigation.NavigateTo("/login");
}
}
3 changes: 3 additions & 0 deletions Fuyu.Platform.Common/Models/Fuyu/Accounts/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ public struct Account

[DataMember]
public EftSave EftSave;

[DataMember]
public ArenaSave ArenaSave;
}
}
18 changes: 18 additions & 0 deletions Fuyu.Platform.Common/Models/Fuyu/Accounts/ArenaProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Runtime.Serialization;
using Fuyu.Platform.Common.Models.EFT.Profiles;

namespace Fuyu.Platform.Common.Models.Fuyu.Accounts
{
[DataContract]
public struct ArenaProfile
{
[DataMember]
public Profile Pmc;

[DataMember]
public string[] Suites;

[DataMember]
public bool ShouldWipe;
}
}
14 changes: 14 additions & 0 deletions Fuyu.Platform.Common/Models/Fuyu/Accounts/ArenaSave.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Runtime.Serialization;

namespace Fuyu.Platform.Common.Models.Fuyu.Accounts
{
[DataContract]
public struct ArenaSave
{
[DataMember]
public string Edition;

[DataMember]
public ArenaProfile PvP;
}
}
8 changes: 8 additions & 0 deletions Fuyu.Platform.Common/Models/Fuyu/Accounts/EGame.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Fuyu.Platform.Common.Models.Fuyu.Accounts
{
public enum EGame
{
EFT,
Arena
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Runtime.Serialization;
using Fuyu.Platform.Common.Models.Fuyu.Accounts;

namespace Fuyu.Platform.Common.Models.Fuyu.Requests
{
[DataContract]
public struct AccountRegisterGameRequest
{
[DataMember]
public EGame Game;

[DataMember]
public string Edition;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@ public struct AccountRegisterRequest

[DataMember]
public string Password;

[DataMember]
public string Edition;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Runtime.Serialization;
using Fuyu.Platform.Common.Models.Fuyu.Accounts;

namespace Fuyu.Platform.Common.Models.Fuyu.Responses
{
[DataContract]
public struct AccountRegisterGameResponse
{
[DataMember]
public ERegisterStatus Status;
}
}
34 changes: 28 additions & 6 deletions Fuyu.Platform.Launcher/Services/RequestService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using Fuyu.Platform.Common.Collections;
using Fuyu.Platform.Common.Models.Fuyu.Accounts;
using Fuyu.Platform.Common.Models.Fuyu.Requests;
using Fuyu.Platform.Common.Models.Fuyu.Responses;
Expand All @@ -9,30 +10,39 @@ namespace Fuyu.Platform.Launcher.Services
{
public static class RequestService
{
private static readonly HttpClient _httpClient;
private static ThreadObject<HttpClient> _httpClient;

static RequestService()
{
_httpClient = new HttpClient(SettingsService.FuyuAddress);
CreateSession(string.Empty);
}

private static T2 HttpPost<T1, T2>(string path, T1 request)
{
var httpc = _httpClient.Get();

var requestJson = Json.Stringify(request);
var requestBytes = Encoding.UTF8.GetBytes(requestJson);
var responseBytes = _httpClient.Post(path, requestBytes);

var responseBytes = httpc.Post(path, requestBytes);
var responseJson = Encoding.UTF8.GetString(responseBytes);
var response = Json.Parse<T2>(responseJson);

return response;
}

public static ERegisterStatus RegisterAccount(string username, string password, string edition)
public static void CreateSession(string sessionId)
{
var httpc = new HttpClient(SettingsService.FuyuAddress, sessionId);
_httpClient = new ThreadObject<HttpClient>(httpc);
}

public static ERegisterStatus RegisterAccount(string username, string password)
{
var request = new AccountRegisterRequest()
{
Username = username,
Password = password,
Edition = edition
Password = password
};
var response = HttpPost<AccountRegisterRequest, AccountRegisterResponse>("/account/register", request);

Expand All @@ -50,5 +60,17 @@ public static string LoginAccount(string username, string password)

return response.SessionId;
}

public static ERegisterStatus RegisterGame(EGame game, string edition)
{
var request = new AccountRegisterGameRequest()
{
Game = game,
Edition = edition
};
var response = HttpPost<AccountRegisterGameRequest, AccountRegisterGameResponse>("/account/register/game", request);

return response.Status;
}
}
}
2 changes: 1 addition & 1 deletion Fuyu.Platform.Server/Behaviours/EFT/GameProfileCreate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public override void Run(HttpContext context)
account.EftSave.PvE.ShouldWipe = false;

// store account
FuyuDatabase.Accounts.SetAccount(accountId, account);
FuyuDatabase.Accounts.SetAccount(account);
AccountService.WriteAccountToDisk(account);

// respond
Expand Down
2 changes: 1 addition & 1 deletion Fuyu.Platform.Server/Behaviours/Fuyu/AccountRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public AccountRegister() : base("/account/register")
public override void Run(HttpContext context)
{
var request = context.GetJson<AccountRegisterRequest>();
var result = AccountService.RegisterAccount(request.Username, request.Password, request.Edition);
var result = AccountService.RegisterAccount(request.Username, request.Password);
var response = new AccountRegisterResponse()
{
Status = result
Expand Down
28 changes: 28 additions & 0 deletions Fuyu.Platform.Server/Behaviours/Fuyu/AccountRegisterGame.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Fuyu.Platform.Common.Models.Fuyu.Requests;
using Fuyu.Platform.Common.Models.Fuyu.Responses;
using Fuyu.Platform.Common.Networking;
using Fuyu.Platform.Common.Serialization;
using Fuyu.Platform.Server.Services.Fuyu;

namespace Fuyu.Platform.Server.Behaviours.EFT
{
public class AccountRegisterGame : HttpBehaviour
{
public AccountRegisterGame() : base("/account/register/game")
{
}

public override void Run(HttpContext context)
{
var request = context.GetJson<AccountRegisterGameRequest>();
var sessionId = context.GetSessionId();
var result = AccountService.RegisterGame(sessionId, request.Game, request.Edition);
var response = new AccountRegisterGameResponse()
{
Status = result
};

SendJson(context, Json.Stringify(response));
}
}
}
4 changes: 2 additions & 2 deletions Fuyu.Platform.Server/Databases/Fuyu/AccountTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ public Account GetAccount(int accountId)
throw new Exception($"Account with {accountId} does not exist.");
}

public void SetAccount(int accountId, Account account)
public void SetAccount(Account account)
{
var accounts = _accounts.ToList();

for (var i = 0; i < accounts.Count; ++i)
{
if (accounts[i].Id == accountId)
if (accounts[i].Id == account.Id)
{
_accounts.Set(i, account);
return;
Expand Down
1 change: 1 addition & 0 deletions Fuyu.Platform.Server/Servers/FuyuServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public override void RegisterServices()
{
AddHttpBehaviour<AccountLogin>();
AddHttpBehaviour<AccountRegister>();
AddHttpBehaviour<AccountRegisterGame>();
}
}
}
51 changes: 46 additions & 5 deletions Fuyu.Platform.Server/Services/Fuyu/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public static string LoginAccount(string username, string password)
}

// NOTE: MongoId's are used internally, but EFT's launcher uses
// a different ID system (hwid+timestamp hash). Instead of
// fully mimicking this, I decided to generate a new MongoId
// for each login.
// a different ID system (hwid+timestamp hash). Instead of
// fully mimicking this, I decided to generate a new MongoId
// for each login.
// -- seionmoya, 2024/09/02
var sessionId = new MongoId().ToString();
FuyuDatabase.Accounts.AddSession(sessionId, accountId);
Expand Down Expand Up @@ -105,7 +105,7 @@ private static int GetNewAccountId()
}
}

public static ERegisterStatus RegisterAccount(string username, string password, string edition)
public static ERegisterStatus RegisterAccount(string username, string password)
{
if (AccountExists(username, password) != -1)
{
Expand All @@ -120,7 +120,7 @@ public static ERegisterStatus RegisterAccount(string username, string password,
Password = password,
EftSave = new EftSave()
{
Edition = edition,
Edition = string.Empty,
PvE = new EftProfile()
{
Savage = default,
Expand All @@ -135,6 +135,16 @@ public static ERegisterStatus RegisterAccount(string username, string password,
Suites = [],
ShouldWipe = true
}
},
ArenaSave = new ArenaSave()
{
Edition = string.Empty,
PvP = new ArenaProfile()
{
Pmc = default,
Suites = [],
ShouldWipe = true
}
}
};

Expand All @@ -144,6 +154,37 @@ public static ERegisterStatus RegisterAccount(string username, string password,
return ERegisterStatus.Success;
}

public static ERegisterStatus RegisterGame(string sessionId, EGame game, string edition)
{
var account = FuyuDatabase.Accounts.GetAccount(sessionId);

switch (game)
{
case EGame.EFT:
if (account.EftSave.Edition == edition)
{
return ERegisterStatus.AlreadyExists;
}

account.EftSave.Edition = edition;
break;

case EGame.Arena:
if (account.ArenaSave.Edition == edition)
{
return ERegisterStatus.AlreadyExists;
}

account.ArenaSave.Edition = edition;
break;
}

FuyuDatabase.Accounts.SetAccount(account);
WriteAccountToDisk(account);

return ERegisterStatus.Success;
}

public static void WriteAccountToDisk(Account account)
{
VFS.WriteTextFile(
Expand Down
Loading

0 comments on commit 5acb8f8

Please sign in to comment.