diff --git a/README.md b/README.md index 6083f48..bc9e1ab 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,10 @@ with [v-rising-discord-bot](https://github.com/DarkAtra/v-rising-discord-bot#v-r It is recommended to **not** expose the server's api port to the internet. +## Support + +If you have questions or need support, feel free to join [this discord server](https://discord.gg/KcMcYKa6Nt). + ## Endpoints ### `/v-rising-discord-bot/characters` @@ -43,7 +47,7 @@ Content-Type: application/json Returns a list of connect and disconnect events for the last 10 minutes. Intended to be used in conjunction with the [v-rising-discord-bot](https://github.com/DarkAtra/v-rising-discord-bot) to log connect and disconnect messages on discord. -Note that this activity list is not persistent across server restarts. +Note that this is not persistent across server restarts. #### Example Response @@ -62,7 +66,36 @@ Content-Type: application/json "type": "DISCONNECTED", "playerName": "Atra", "occurred": "2023-01-01T01:00:00Z" - }, + } +] +``` + +### `/v-rising-discord-bot/pvp-kills` + +Returns the most recent pvp kills. Intended to be used in conjunction with the [v-rising-discord-bot](https://github.com/DarkAtra/v-rising-discord-bot) to +display a kill feed on discord. + +Note that this is not persistent across server restarts. + +#### Example Response + +```http +HTTP/1.1 200 OK +Transfer-Encoding: chunked +Content-Type: application/json + +[ + { + "killer": { + "name": "Atra", + "gearLevel": 71 + }, + "victim": { + "name": "Testi", + "gearLevel": 11 + }, + "occurred": "2023-01-01T00:00:00Z" + } ] ``` diff --git a/character/CharacterInfoCommand.cs b/character/CharacterInfoCommand.cs index 7e31b6d..590a993 100644 --- a/character/CharacterInfoCommand.cs +++ b/character/CharacterInfoCommand.cs @@ -30,7 +30,7 @@ public static List GetCharacters() { return new CharacterResponse( Name: ((VCharacter) player.VCharacter!).Character.Name.ToString(), - GearLevel: (int) entityManager.GetComponentData(((VCharacter) player.VCharacter!).CharacterEntity).GetFullLevel(), + GearLevel: ((VCharacter) player.VCharacter!).getGearLevel(), Clan: clan, KilledVBloods: killedVBloods ); diff --git a/command/ServerWebAPISystemPatches.cs b/command/ServerWebAPISystemPatches.cs index dab2a17..1286fa2 100644 --- a/command/ServerWebAPISystemPatches.cs +++ b/command/ServerWebAPISystemPatches.cs @@ -12,6 +12,7 @@ using ProjectM.Network; using v_rising_discord_bot_companion.activity; using v_rising_discord_bot_companion.character; +using v_rising_discord_bot_companion.killfeed; using v_rising_discord_bot_companion.query; namespace v_rising_discord_bot_companion.command; @@ -43,6 +44,12 @@ public static void OnCreate(ServerWebAPISystem __instance) { "GET", BuildAdapter(_ => ServerBootstrapSystemPatches.getPlayerActivities()) )); + + __instance._HttpReceiveService.AddRoute(new HttpServiceReceiveThread.Route( + new Regex("/v-rising-discord-bot/pvp-kills"), + "GET", + BuildAdapter(_ => VampireDownedServerEventSystemPatches.getPvpKills()) + )); } private static HttpServiceReceiveThread.RequestHandler BuildAdapter(Func commandHandler) { diff --git a/game/VPlayer.cs b/game/VPlayer.cs index 02179b8..7fc69cd 100644 --- a/game/VPlayer.cs +++ b/game/VPlayer.cs @@ -28,6 +28,7 @@ public static VPlayer from(Entity userEntity) { VCharacter: game.VCharacter.from(vUser) ); } + public static List GetAllPlayers() { return ListUtils.Convert( VWorld.Server.EntityManager @@ -63,10 +64,17 @@ Entity CharacterEntity ) { public static VCharacter from(VUser vUser) { - var characterEntity = vUser.User.LocalCharacter._Entity; + return from(vUser.User.LocalCharacter._Entity); + } + + public static VCharacter from(Entity characterEntity) { return new VCharacter( Character: VWorld.Server.EntityManager.GetComponentData(characterEntity), CharacterEntity: characterEntity ); } + + public int getGearLevel() { + return (int) VWorld.Server.EntityManager.GetComponentData(CharacterEntity).GetFullLevel(); + } } diff --git a/http-requests.http b/http-requests.http new file mode 100644 index 0000000..cc31805 --- /dev/null +++ b/http-requests.http @@ -0,0 +1,8 @@ +### Get player characters +GET http://localhost:25570/v-rising-discord-bot/characters + +### Get player activities +GET http://localhost:25570/v-rising-discord-bot/player-activities + +### Get pvp kills +GET http://localhost:25570/v-rising-discord-bot/pvp-kills diff --git a/killfeed/PvpKill.cs b/killfeed/PvpKill.cs new file mode 100644 index 0000000..20adf1c --- /dev/null +++ b/killfeed/PvpKill.cs @@ -0,0 +1,14 @@ +using System; + +namespace v_rising_discord_bot_companion.killfeed; + +public readonly record struct PvpKill( + Player Killer, + Player Victim, + DateTime Occurred +); + +public readonly record struct Player( + string Name, + int GearLevel +); diff --git a/killfeed/VampireDownedServerEventSystemPatches.cs b/killfeed/VampireDownedServerEventSystemPatches.cs new file mode 100644 index 0000000..d5c9e2d --- /dev/null +++ b/killfeed/VampireDownedServerEventSystemPatches.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using Bloodstone.API; +using HarmonyLib; +using ProjectM; +using Unity.Collections; +using Unity.Entities; +using v_rising_discord_bot_companion.game; + +namespace v_rising_discord_bot_companion.killfeed; + +[HarmonyPatch(typeof(VampireDownedServerEventSystem))] +public class VampireDownedServerEventSystemPatches { + + private readonly static List _pvpKills = new(); + + public static List getPvpKills() { + removeExpiredPvpKills(); + return _pvpKills; + } + + private static void removeExpiredPvpKills() { + _pvpKills.RemoveAll(pvpKill => DateTime.UtcNow > pvpKill.Occurred.AddMinutes(10)); + } + + [HarmonyPostfix] + [HarmonyPatch("OnUpdate")] + public static void Postfix(VampireDownedServerEventSystem __instance) { + + if (__instance.__OnUpdate_LambdaJob0_entityQuery == null) { + return; + } + + var entities = __instance.__OnUpdate_LambdaJob0_entityQuery.ToEntityArray(Allocator.Temp); + foreach (var entity in entities) { + handleDownedEntity(entity); + } + + removeExpiredPvpKills(); + } + + private static void handleDownedEntity(Entity entity) { + + VampireDownedServerEventSystem.TryFindRootOwner(entity, 1, VWorld.Server.EntityManager, out var victimEntity); + VWorld.Server.EntityManager.TryGetComponentData(entity, out var buff); + VampireDownedServerEventSystem.TryFindRootOwner(buff.Source, 1, VWorld.Server.EntityManager, out var killerEntity); + + if (!VWorld.Server.EntityManager.HasComponent(killerEntity) + || !VWorld.Server.EntityManager.HasComponent(victimEntity) || victimEntity.Equals(killerEntity)) { + return; + } + + var killer = VCharacter.from(killerEntity); + var victim = VCharacter.from(victimEntity); + + _pvpKills.Add(new PvpKill( + Killer: new Player( + Name: killer.Character.Name.ToString(), + GearLevel: killer.getGearLevel() + ), + Victim: new Player( + Name: victim.Character.Name.ToString(), + GearLevel: victim.getGearLevel() + ), + Occurred: DateTime.UtcNow + )); + } +} diff --git a/v-rising-discord-bot-companion.csproj b/v-rising-discord-bot-companion.csproj index 9a8662d..36a6cdc 100644 --- a/v-rising-discord-bot-companion.csproj +++ b/v-rising-discord-bot-companion.csproj @@ -2,7 +2,7 @@ v-rising-discord-bot-companion A companion mod for DarkAtra's v-rising-discord-bot. - 0.1.6 + 0.3.3 net6.0 latest @@ -16,7 +16,7 @@ - +