Skip to content

Commit

Permalink
Merge pull request #64 from nexus4880/item-events
Browse files Browse the repository at this point in the history
Item events and router restructuring
  • Loading branch information
seionmoya authored Oct 7, 2024
2 parents 3f6d894 + 1815775 commit 3f62dca
Show file tree
Hide file tree
Showing 57 changed files with 989 additions and 273 deletions.
8 changes: 8 additions & 0 deletions Fuyu.Backend.BSG/DTO/Items/EItemRotation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Fuyu.Backend.EFT.DTO.Items
{
public enum EItemRotation
{
Horizontal,
Vertical
}
}
10 changes: 7 additions & 3 deletions Fuyu.Backend.BSG/DTO/Items/ItemDogtagComponent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Fuyu.Backend.BSG.DTO.Profiles.Info;
using Fuyu.Common.Hashing;
using System;
using System.Runtime.Serialization;

Expand All @@ -7,11 +8,12 @@ namespace Fuyu.Backend.EFT.DTO.Items
[DataContract]
public class ItemDogtagComponent
{
// AccountId is the string version of the pmc.aid
[DataMember]
public string AccountId;

[DataMember]
public string ProfileId;
public MongoId ProfileId;

[DataMember]
public string Nickname;
Expand All @@ -28,18 +30,20 @@ public class ItemDogtagComponent
[DataMember]
public string Status;

[DataMember]
// AccountId is the string version of the pmc.aid
[DataMember]
public string KillerAccountId;

[DataMember]
public string KillerProfileId;
public MongoId KillerProfileId;

[DataMember]
public string KillerName;

[DataMember]
public string WeaponName;

// Whenever you carry out your own group member's dogtag it sells for 1 ruble
[DataMember]
public bool CarriedByGroupMember;
}
Expand Down
3 changes: 1 addition & 2 deletions Fuyu.Backend.BSG/DTO/Items/LocationInGrid.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Fuyu.Backend.BSG.DTO.Common;
using System.Runtime.Serialization;

namespace Fuyu.Backend.EFT.DTO.Items
{
[DataContract]
[DataContract]
public class LocationInGrid
{
[DataMember]
Expand Down
16 changes: 16 additions & 0 deletions Fuyu.Backend.BSG/DTO/Responses/ItemEventResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
using Fuyu.Backend.BSG.ItemEvents.Models;

namespace Fuyu.Backend.BSG.DTO.Responses
{
[DataContract]
public class ItemEventResponse
{
[DataMember(Name = "profileChanges")]
public Dictionary<string, ProfileChange> ProfileChanges { get; set; } = [];

[DataMember(Name = "warnings")]
public InventoryWarning[] InventoryWarnings = [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Fuyu.Common.Networking;

namespace Fuyu.Backend.BSG.ItemEvents.Controllers
{
public interface IItemEventController : IRouterController<ItemEventContext>
{
public string Action { get; }
}
}
27 changes: 27 additions & 0 deletions Fuyu.Backend.BSG/ItemEvents/Controllers/ItemEventController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Fuyu.Backend.BSG.ItemEvents.Models;
using System.Threading.Tasks;

namespace Fuyu.Backend.BSG.ItemEvents.Controllers
{
public abstract class ItemEventController<TEvent> : IItemEventController where TEvent : BaseItemEvent
{
public string Action { get; private set; }

public ItemEventController(string action)
{
Action = action;
}

public virtual bool IsMatch(ItemEventContext context)
{
return context.Action == Action;
}

public Task RunAsync(ItemEventContext context)
{
return RunAsync(context, context.GetData<TEvent>());
}

public abstract Task RunAsync(ItemEventContext context, TEvent request);
}
}
27 changes: 27 additions & 0 deletions Fuyu.Backend.BSG/ItemEvents/ItemEventContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Fuyu.Backend.BSG.DTO.Responses;
using Fuyu.Common.Networking;
using Newtonsoft.Json.Linq;

namespace Fuyu.Backend.BSG.ItemEvents
{
public class ItemEventContext : IRouterContext
{
public string SessionId { get; }
public string Action { get; }
public JToken Data { get; }
public ItemEventResponse Response { get; }

public ItemEventContext(string sessionId, string action, JToken data, ItemEventResponse response)
{
SessionId = sessionId;
Action = action;
Data = data;
Response = response;
}

public T GetData<T>()
{
return Data.ToObject<T>();
}
}
}
9 changes: 9 additions & 0 deletions Fuyu.Backend.BSG/ItemEvents/ItemEventRouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Fuyu.Backend.BSG.ItemEvents.Controllers;
using Fuyu.Common.Networking;

namespace Fuyu.Backend.BSG.ItemEvents
{
public class ItemEventRouter : Router<IItemEventController, ItemEventContext>
{
}
}
11 changes: 11 additions & 0 deletions Fuyu.Backend.BSG/ItemEvents/Models/BaseItemEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Runtime.Serialization;

namespace Fuyu.Backend.BSG.ItemEvents.Models
{
[DataContract]
public class BaseItemEvent
{
[DataMember(Name = "Action")]
public string Action { get; }
}
}
20 changes: 20 additions & 0 deletions Fuyu.Backend.BSG/ItemEvents/Models/InventoryWarning.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Runtime.Serialization;

namespace Fuyu.Backend.BSG.ItemEvents.Models
{
[DataContract]
public class InventoryWarning
{
[DataMember(Name = "index")]
public int RequestIndex { get; set; }

[DataMember(Name = "errmsg")]
public string ErrorMessage { get; set; }

[DataMember(Name = "code")]
public string ErrorCode { get; set; }

[DataMember(Name = "data")]
public object Data;
}
}
15 changes: 15 additions & 0 deletions Fuyu.Backend.BSG/ItemEvents/Models/ProfileChange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Fuyu.Backend.BSG.ItemEvents.Models
{
[DataContract]
public class ProfileChange
{
[DataMember(Name = "experience")]
public int Experience;

[DataMember(Name = "recipeUnlocked")]
public Dictionary<string, bool> UnlockedRecipes = [];
}
}
14 changes: 7 additions & 7 deletions Fuyu.Backend.Core/Controllers/AccountLoginController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public AccountLoginController() : base("/account/login")
{
}

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

await context.SendJsonAsync(Json.Stringify(response));
}
}
await context.SendJsonAsync(Json.Stringify(response));
}
}
}
20 changes: 15 additions & 5 deletions Fuyu.Backend.Core/CoreOrm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ public static List<Account> GetAccounts()
public static Account GetAccount(string sessionId)
{
var accountId = GetSession(sessionId);
return CoreDatabase.Accounts.Get(accountId);
if (!CoreDatabase.Accounts.TryGet(accountId, out var account))
{
throw new Exception($"Failed to get account with sessionID: {sessionId}");
}

return account;
}

public static Account GetAccount(int accountId)
Expand All @@ -39,7 +44,7 @@ public static void SetOrAddAccount(Account account)
{
if (accounts[i].Id == account.Id)
{
CoreDatabase.Accounts.Set(i, account);
CoreDatabase.Accounts.TrySet(i, account);
return;
}
}
Expand All @@ -55,7 +60,7 @@ public static void RemoveAccount(int accountId)
{
if (accounts[i].Id == accountId)
{
CoreDatabase.Accounts.RemoveAt(i);
CoreDatabase.Accounts.TryRemoveAt(i);
return;
}
}
Expand All @@ -70,7 +75,12 @@ public static Dictionary<string, int> GetSessions()

public static int GetSession(string sessionId)
{
return CoreDatabase.Sessions.Get(sessionId);
if (!CoreDatabase.Sessions.TryGet(sessionId, out var id))
{
throw new Exception($"Failed to find ID for sessionId: {sessionId}");
}

return id;
}

public static void SetOrAddSession(string sessionId, int accountId)
Expand All @@ -81,7 +91,7 @@ public static void SetOrAddSession(string sessionId, int accountId)
}
else
{
CoreDatabase.Sessions.Add(sessionId, accountId);
CoreDatabase.Sessions.Set(sessionId, accountId);
}
}

Expand Down
10 changes: 5 additions & 5 deletions Fuyu.Backend.Core/Servers/CoreServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public CoreServer() : base("core", "http://localhost:8000/")

public void RegisterServices()
{
AddHttpController<AccountLoginController>();
AddHttpController<AccountLogoutController>();
AddHttpController<AccountRegisterController>();
AddHttpController<AccountRegisterGameController>();
AddHttpController<AccountGamesController>();
HttpRouter.AddController<AccountLoginController>();
HttpRouter.AddController<AccountLogoutController>();
HttpRouter.AddController<AccountRegisterController>();
HttpRouter.AddController<AccountRegisterGameController>();
HttpRouter.AddController<AccountGamesController>();
}
}
}
12 changes: 8 additions & 4 deletions Fuyu.Backend.Core/Services/RequestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Fuyu.Backend.Common.DTO.Responses;
using Fuyu.Common.Networking;
using Fuyu.Common.Serialization;
using System;

namespace Fuyu.Backend.Core.Services
{
Expand All @@ -18,14 +19,17 @@ static RequestService()
// TODO:
// * get address from config
// -- seionmoya, 2024/09/08
_httpClients.Add("fuyu", new EftHttpClient("http://localhost:8000", string.Empty));
_httpClients.Add("eft", new EftHttpClient("http://localhost:8010", string.Empty));
_httpClients.Add("arena", new EftHttpClient("http://localhost:8020", string.Empty));
_httpClients.Set("fuyu", new EftHttpClient("http://localhost:8000", string.Empty));
_httpClients.Set("eft", new EftHttpClient("http://localhost:8010", string.Empty));
_httpClients.Set("arena", new EftHttpClient("http://localhost:8020", string.Empty));
}

private static T2 HttpPost<T1, T2>(string id, string path, T1 request)
{
var httpc = _httpClients.Get(id);
if (!_httpClients.TryGet(id, out var httpc))
{
throw new Exception($"Id '{id}' not found");
}

var requestJson = Json.Stringify(request);
var requestBytes = Encoding.UTF8.GetBytes(requestJson);
Expand Down
54 changes: 54 additions & 0 deletions Fuyu.Backend.EFT/Controllers/ClientGameProfileItemsMoving.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Fuyu.Backend.BSG.DTO.Responses;
using Fuyu.Backend.BSG.ItemEvents;
using Fuyu.Backend.EFT.ItemEvents.Controllers;
using Fuyu.Common.Networking;
using Fuyu.Common.Serialization;
using Newtonsoft.Json.Linq;
using System.Threading.Tasks;

namespace Fuyu.Backend.EFT.Controllers
{
public class ClientGameProfileItemsMoving : HttpController
{
private ItemEventRouter _router = new ItemEventRouter();

public ClientGameProfileItemsMoving() : base("/client/game/profile/items/moving")
{
_router.AddController<CustomizationBuyEventController>();
_router.AddController<EatItemEventController>();
_router.AddController<InsureEventController>();
_router.AddController<InterGameTransferEventController>();
_router.AddController<MoveItemEventController>();
_router.AddController<ReadEncyclopediaEventController>();
_router.AddController<SellAllFromSavageEventController>();
_router.AddController<TraderRepairEventController>();
_router.AddController<TradingConfirmEventController>();
_router.AddController<ApplyInventoryChangesItemEventController>();
}

public override async Task RunAsync(HttpContext context)
{
var sessionId = context.GetSessionId();
var requestText = await context.GetTextAsync();
var requestObject = JObject.Parse(requestText);
var requestData = requestObject.Value<JArray>("data");
var response = new ItemEventResponse
{
ProfileChanges = [],
InventoryWarnings = []
};

foreach (var itemRequest in requestData)
{
var action = itemRequest.Value<string>("Action");
var itemEventContext = new ItemEventContext(sessionId, action, itemRequest, response);
await _router.RouteAsync(itemEventContext);
}

await context.SendJsonAsync(Json.Stringify(new ResponseBody<ItemEventResponse>
{
data = response
}));
}
}
}
Loading

0 comments on commit 3f62dca

Please sign in to comment.