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

Rework HttpControllers #68

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions Fuyu.Backend.Core/Controllers/AccountLoginController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@

namespace Fuyu.Backend.Core.Controllers
{
public class AccountLoginController : HttpController
public class AccountLoginController : BodyAwareHttpController<AccountLoginRequest>
{
public AccountLoginController() : base("/account/login")
{
}

public override async Task RunAsync(HttpContext context)
public override Task RunAsync(HttpContext context, AccountLoginRequest body)
{
var request = await context.GetJsonAsync<AccountLoginRequest>();
var response = AccountService.LoginAccount(request.Username, request.Password);
var response = AccountService.LoginAccount(body.Username, body.Password);

await context.SendJsonAsync(Json.Stringify(response));
return context.SendJsonAsync(Json.Stringify(response));
}
}
}
7 changes: 3 additions & 4 deletions Fuyu.Backend.Core/Controllers/AccountRegisterController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@

namespace Fuyu.Backend.Core.Controllers
{
public class AccountRegisterController : HttpController
public class AccountRegisterController : BodyAwareHttpController<AccountRegisterRequest>
{
public AccountRegisterController() : base("/account/register")
{
}

public override async Task RunAsync(HttpContext context)
public override Task RunAsync(HttpContext context, AccountRegisterRequest request)
{
var request = await context.GetJsonAsync<AccountRegisterRequest>();
var result = AccountService.RegisterAccount(request.Username, request.Password);
var response = new AccountRegisterResponse()
{
Status = result
};

await context.SendJsonAsync(Json.Stringify(response));
return context.SendJsonAsync(Json.Stringify(response));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@

namespace Fuyu.Backend.Core.Controllers
{
public class AccountRegisterGameController : HttpController
{
public class AccountRegisterGameController : BodyAwareHttpController<AccountRegisterGameRequest>
{
public AccountRegisterGameController() : base("/account/register/game")
{
}

public override async Task RunAsync(HttpContext context)
public override Task RunAsync(HttpContext context, AccountRegisterGameRequest request)
{
var request = await context.GetJsonAsync<AccountRegisterGameRequest>();
var sessionId = context.GetSessionId();
var result = AccountService.RegisterGame(sessionId, request.Game, request.Edition);

await context.SendJsonAsync(Json.Stringify(result));
return context.SendJsonAsync(Json.Stringify(result));
}
}
}
7 changes: 3 additions & 4 deletions Fuyu.Backend.EFT/Controllers/FuyuGameLoginController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@

namespace Fuyu.Backend.EFT.Controllers
{
public class FuyuGameLoginController : HttpController
public class FuyuGameLoginController : BodyAwareHttpController<FuyuGameLoginRequest>
{
public FuyuGameLoginController() : base("/fuyu/game/login")
{
}

public override async Task RunAsync(HttpContext context)
public override Task RunAsync(HttpContext context, FuyuGameLoginRequest request)
{
var request = await context.GetJsonAsync<FuyuGameLoginRequest>();
var sessionId = AccountService.LoginAccount(request.AccountId);
var response = new FuyuGameLoginResponse()
{
SessionId = sessionId
};

await context.SendJsonAsync(Json.Stringify(response));
return context.SendJsonAsync(Json.Stringify(response));
}
}
}
7 changes: 3 additions & 4 deletions Fuyu.Backend.EFT/Controllers/FuyuGameRegisterController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@

namespace Fuyu.Backend.EFT.Controllers
{
public class FuyuGameRegisterController : HttpController
public class FuyuGameRegisterController : BodyAwareHttpController<FuyuGameRegisterRequest>
{
public FuyuGameRegisterController() : base("/fuyu/game/register")
{
}

public override async Task RunAsync(HttpContext context)
public override Task RunAsync(HttpContext context, FuyuGameRegisterRequest request)
{
var request = await context.GetJsonAsync<FuyuGameRegisterRequest>();
var accountId = AccountService.RegisterAccount(request.Username, request.Edition);
var response = new FuyuGameRegisterResponse()
{
AccountId = accountId
};

await context.SendJsonAsync(Json.Stringify(response));
return context.SendJsonAsync(Json.Stringify(response));
}
}
}
7 changes: 3 additions & 4 deletions Fuyu.Backend.EFT/Controllers/GameBotGenerateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@

namespace Fuyu.Backend.EFT.Controllers
{
public class GameBotGenerateController : HttpController
public class GameBotGenerateController : BodyAwareHttpController<GameBotGenerateRequest>
{
public GameBotGenerateController() : base("/client/game/bot/generate")
{
}

public override async Task RunAsync(HttpContext context)
public override Task RunAsync(HttpContext context, GameBotGenerateRequest request)
{
var request = await context.GetJsonAsync<GameBotGenerateRequest>();
var profiles = BotService.GetBots(request.conditions);
var response = new ResponseBody<Profile[]>()
{
data = profiles
};

await context.SendJsonAsync(Json.Stringify(response));
return context.SendJsonAsync(Json.Stringify(response));
}
}
}
12 changes: 3 additions & 9 deletions Fuyu.Backend.EFT/Controllers/GameProfileCreateController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System.Threading.Tasks;
using Fuyu.Common.Hashing;
using Fuyu.Common.Networking;
using Fuyu.Common.Serialization;
using Fuyu.Backend.BSG.DTO.Profiles;
using Fuyu.Backend.BSG.DTO.Profiles.Info;
using Fuyu.Backend.BSG.DTO.Responses;
using Fuyu.Backend.EFT.DTO.Requests;
using Fuyu.Backend.EFT.DTO.Responses;
Expand All @@ -14,21 +11,18 @@ namespace Fuyu.Backend.EFT.Controllers
// TODO:
// * move code into TemplateTable and ProfileService
// -- seionmoya, 2024/09/02
public class GameProfileCreateController : HttpController
public class GameProfileCreateController : BodyAwareHttpController<GameProfileCreateRequest>
{
public GameProfileCreateController() : base("/client/game/profile/create")
{
}

public override async Task RunAsync(HttpContext context)
public override Task RunAsync(HttpContext context, GameProfileCreateRequest request)
{
var request = await context.GetJsonAsync<GameProfileCreateRequest>();
var sessionId = context.GetSessionId();
var accountId = EftOrm.GetSession(sessionId);
var account = EftOrm.GetAccount(accountId);

var pmcId = ProfileService.WipeProfile(account, request.side, request.headId, request.voiceId);

var response = new ResponseBody<GameProfileCreateResponse>()
{
data = new GameProfileCreateResponse()
Expand All @@ -37,7 +31,7 @@ public override async Task RunAsync(HttpContext context)
}
};

await context.SendJsonAsync(Json.Stringify(response));
return context.SendJsonAsync(Json.Stringify(response));
}
}
}
13 changes: 8 additions & 5 deletions Fuyu.Backend.EFT/Controllers/GameProfileItemsMovingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Fuyu.Backend.EFT.Controllers
{
public class GameProfileItemsMovingController : HttpController
public class GameProfileItemsMovingController : BodyAwareHttpController<JObject>
{
private ItemEventRouter _router = new ItemEventRouter();

Expand All @@ -26,12 +26,15 @@ public GameProfileItemsMovingController() : base("/client/game/profile/items/mov
_router.AddController<ApplyInventoryChangesItemEventController>();
}

public override async Task RunAsync(HttpContext context)
public override async Task RunAsync(HttpContext context, JObject request)
{
if (!request.ContainsKey("data"))
{
return;
}

var sessionId = context.GetSessionId();
var requestText = await context.GetTextAsync();
var requestObject = JObject.Parse(requestText);
var requestData = requestObject.Value<JArray>("data");
var requestData = request.Value<JArray>("data");
var response = new ItemEventResponse
{
ProfileChanges = [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@

namespace Fuyu.Backend.EFT.Controllers
{
public class GameProfileNicknameValidateController : HttpController
public class GameProfileNicknameValidateController : BodyAwareHttpController<GameProfileNicknameValidateRequest>
{
public GameProfileNicknameValidateController() : base("/client/game/profile/nickname/validate")
{
}

public override async Task RunAsync(HttpContext context)
public override Task RunAsync(HttpContext context, GameProfileNicknameValidateRequest request)
{
var request = await context.GetJsonAsync<GameProfileNicknameValidateRequest>();

// TODO:
// * validate nickname usage
// -- seionmoya, 2024/08/28
Expand All @@ -29,7 +27,7 @@ public override async Task RunAsync(HttpContext context)
}
};

await context.SendJsonAsync(Json.Stringify(response));
return context.SendJsonAsync(Json.Stringify(response));
}
}
}
13 changes: 6 additions & 7 deletions Fuyu.Backend.EFT/Controllers/MatchLocalStartController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Fuyu.Backend.EFT.Controllers
{
public class MatchLocalStartController : HttpController
public class MatchLocalStartController : BodyAwareHttpController<MatchLocalStartRequest>
{
private readonly Dictionary<string, string> _locations;

Expand All @@ -15,10 +15,10 @@ public MatchLocalStartController() : base("/client/match/local/start")
_locations = new Dictionary<string, string>()
{
{ "bigmap", Resx.GetText("eft", "database.locations.bigmap.json") },
{ "factory4_day", Resx.GetText("eft", "database.locations.bigmap.json") },
{ "factory4_night", string.Empty },
{ "factory4_day", Resx.GetText("eft", "database.locations.factory4_day.json") },
{ "factory4_night", string.Empty },
{ "interchange", Resx.GetText("eft", "database.locations.interchange.json") },
{ "laboratory", string.Empty },
{ "laboratory", string.Empty },
{ "lighthouse", Resx.GetText("eft", "database.locations.lighthouse.json") },
{ "rezervbase", Resx.GetText("eft", "database.locations.rezervbase.json") },
{ "sandbox", Resx.GetText("eft", "database.locations.sandbox.json") },
Expand All @@ -28,12 +28,11 @@ public MatchLocalStartController() : base("/client/match/local/start")
};
}

public override async Task RunAsync(HttpContext context)
public override Task RunAsync(HttpContext context, MatchLocalStartRequest request)
{
var request = await context.GetJsonAsync<MatchLocalStartRequest>();
var location = request.location;

await context.SendJsonAsync(_locations[location]);
return context.SendJsonAsync(_locations[location]);
}
}
}
27 changes: 27 additions & 0 deletions Fuyu.Common/Networking/BodyAwareHttpController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Threading.Tasks;

namespace Fuyu.Common.Networking
{
public abstract class BodyAwareHttpController<TRequest> : HttpController where TRequest: class
{
public BodyAwareHttpController(string path) : base(path)
{
}

public override async Task RunAsync(HttpContext context)
{
TRequest body = null;

// NOTE: I'm not sure exactly how we should handle this. I imagine we still want the endpoint to be called?
// -- nexus4880, 2024-10-07
if (context.HasBody())
{
body = await context.GetJsonAsync<TRequest>();
}

await RunAsync(context, body);
}

public abstract Task RunAsync(HttpContext context, TRequest body);
}
}