WAReborn / WorldsAdriftReborn

63 stars 15 forks source link

Handle membership REST request #18

Open sp00ktober opened 1 year ago

sp00ktober commented 1 year ago

This is a followup to #17 . If the UID is valid the game will issue a REST request to /memberships/character/{UID}. The relevant method where this happens is GetCharacterMemberships(string characterUid)

MonoToast commented 1 year ago

The rest api url is set in the ConfigKeys under AlliancesUrl = SharedConfigKeys.AlliancesServerUrl;

I was able to Change the data in the config by hooking into the GetOrDefault

        class GetOrDefault_String
        {
            [HarmonyTargetMethod]
            public static MethodBase GetTargetMethod()
            {
                return AccessTools.Method(
                                            AccessTools.TypeByName("WAConfig"),
                                            "GetOrDefault",
                                            new Type[]
                                            {
                                            typeof(string)
                                            }).MakeGenericMethod(typeof(string));
            }

            [HarmonyPrefix]
            public static bool Get_Prefix( ref string __result, string key )
            {
                ModSettings.modConfig.Reload();

                if (key == "Alliances.ServerUrl")
                {
                    __result = ModSettings.restServerUrl.Value;
                    return false;
                }

                Debug.LogWarning("not touching " + key);

                return true;
            }
        }

This ends up sending these GET request to the REST server

Image

The first request comes from Bossa.Travellers.Social.SocialServerImpl.GetCharacterMemberships

the second request comes from Bossa.Travellers.Social.SocialServerImpl.GetInvitesAndApplicationsForPlayer

I made these classes to handle both request

namespace WorldsAdriftServer.Handlers.SocialScreen
{
    internal class SocialServerHandler
    {
        internal static void HandleCharacterMemberships( HttpSession session, HttpRequest request )
        {

            PlayerMembershipModel playerMembershipModel = new PlayerMembershipModel();

            ResponseSchema responseSchema = new ResponseSchema();
            responseSchema.data = JToken.FromObject(playerMembershipModel);

            JObject respO = (JObject)JToken.FromObject(responseSchema);
            if (respO != null)
            {
                HttpResponse resp = new HttpResponse();

                resp.SetBegin(200);
                resp.SetBody(respO.ToString());

                Console.WriteLine(resp);

                session.SendResponseAsync(resp);
            }
        }

        internal static void HandleInvitesAndApplicationsForPlayer( HttpSession session, HttpRequest request )
        {
            List<MembershipChangeRequestDataModel> members = new List<MembershipChangeRequestDataModel>();

            ItemArray itemArray = new ItemArray();
            itemArray.items = JArray.FromObject(members);

            ResponseSchema responseSchema = new ResponseSchema();
            responseSchema.data = JToken.FromObject(itemArray);

            JObject respO = (JObject)JToken.FromObject(responseSchema);
            if (respO != null)
            {
                HttpResponse resp = new HttpResponse();

                resp.SetBegin(200);
                resp.SetBody(respO.ToString());

                Console.WriteLine(resp);

                session.SendResponseAsync(resp);
            }
        }
    }

    [Serializable]
    internal class ItemArray
    {
        public JArray items;
    }
}

This allows the Social screen to load

Image

But non of the buttons work and they send there own set of API calls as well as non of the data is actual data being passed to the client