Skip to content

Commit

Permalink
feat: add endpoints for most recent pvp kills (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkAtra authored Aug 4, 2023
1 parent fdd3228 commit 7ab4442
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 6 deletions.
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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

Expand All @@ -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"
}
]
```

Expand Down
2 changes: 1 addition & 1 deletion character/CharacterInfoCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static List<CharacterResponse> GetCharacters() {

return new CharacterResponse(
Name: ((VCharacter) player.VCharacter!).Character.Name.ToString(),
GearLevel: (int) entityManager.GetComponentData<Equipment>(((VCharacter) player.VCharacter!).CharacterEntity).GetFullLevel(),
GearLevel: ((VCharacter) player.VCharacter!).getGearLevel(),
Clan: clan,
KilledVBloods: killedVBloods
);
Expand Down
7 changes: 7 additions & 0 deletions command/ServerWebAPISystemPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<HttpListenerContext, object> commandHandler) {
Expand Down
10 changes: 9 additions & 1 deletion game/VPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static VPlayer from(Entity userEntity) {
VCharacter: game.VCharacter.from(vUser)
);
}

public static List<VPlayer> GetAllPlayers() {
return ListUtils.Convert(
VWorld.Server.EntityManager
Expand Down Expand Up @@ -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<PlayerCharacter>(characterEntity),
CharacterEntity: characterEntity
);
}

public int getGearLevel() {
return (int) VWorld.Server.EntityManager.GetComponentData<Equipment>(CharacterEntity).GetFullLevel();
}
}
8 changes: 8 additions & 0 deletions http-requests.http
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions killfeed/PvpKill.cs
Original file line number Diff line number Diff line change
@@ -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
);
68 changes: 68 additions & 0 deletions killfeed/VampireDownedServerEventSystemPatches.cs
Original file line number Diff line number Diff line change
@@ -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<PvpKill> _pvpKills = new();

public static List<PvpKill> 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<VampireDownedBuff>(entity, out var buff);
VampireDownedServerEventSystem.TryFindRootOwner(buff.Source, 1, VWorld.Server.EntityManager, out var killerEntity);

if (!VWorld.Server.EntityManager.HasComponent<PlayerCharacter>(killerEntity)
|| !VWorld.Server.EntityManager.HasComponent<PlayerCharacter>(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
));
}
}
4 changes: 2 additions & 2 deletions v-rising-discord-bot-companion.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<AssemblyName>v-rising-discord-bot-companion</AssemblyName>
<Description>A companion mod for DarkAtra's v-rising-discord-bot.</Description>
<Version>0.1.6</Version>
<Version>0.3.3</Version>

<TargetFramework>net6.0</TargetFramework>
<LangVersion>latest</LangVersion>
Expand All @@ -16,7 +16,7 @@
<PackageReference Include="BepInEx.Unity.IL2CPP" Version="6.0.0-be.668" IncludeAssets="compile"/>
<PackageReference Include="BepInEx.Core" Version="6.0.0-be.668" IncludeAssets="compile"/>
<PackageReference Include="BepInEx.PluginInfoProps" Version="2.1.0"/>
<PackageReference Include="VRising.Bloodstone" Version="0.1.4"/>
<PackageReference Include="VRising.Bloodstone" Version="0.1.6"/>
<PackageReference Include="VRising.Unhollowed.Client" Version="0.6.5.57575090"/>
</ItemGroup>

Expand Down

0 comments on commit 7ab4442

Please sign in to comment.