Skip to content

Commit

Permalink
Fix player relations
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacyway committed Oct 29, 2024
1 parent 60b8923 commit c9b9239
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Fika.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { IFikaConfigBackground } from "./models/fika/config/IFikaConfigBackgroun
import { IFikaConfigDedicated } from "./models/fika/config/IFikaConfigDedicated";
import { IFikaConfigNatPunchServer } from "./models/fika/config/IFikaConfigNatPunchServer";
import { Overrider } from "./overrides/Overrider";
import { FikaPlayerRelationsCacheService } from "./services/cache/FikaPlayerRelationsCacheService";
import { FikaDedicatedProfileService } from "./services/dedicated/FikaDedicatedProfileService";
import { FikaConfig } from "./utils/FikaConfig";
import { FikaServerTools } from "./utils/FikaServerTools";
Expand All @@ -29,6 +30,7 @@ export class Fika {
@inject("FikaDedicatedProfileService") protected fikaDedicatedProfileService: FikaDedicatedProfileService,
@inject("ImageRouter") protected imageRouter: ImageRouter,
@inject("ImporterUtil") protected importerUtil: ImporterUtil,
@inject("FikaPlayerRelationsCacheService") protected fikaPlayerRelationCacheServce: FikaPlayerRelationsCacheService
) {
this.modPath = fikaConfig.getModPath();
this.natPunchServerConfig = fikaConfig.getConfig().natPunchServer;
Expand All @@ -50,6 +52,7 @@ export class Fika {
}

this.addFikaClientLocales();
this.fikaPlayerRelationCacheServce.postInit();

if (this.backgroundConfig.enable) {
const image = this.backgroundConfig.easteregg ? "assets/images/launcher/bg-senko.png" : "assets/images/launcher/bg.png";
Expand Down
40 changes: 39 additions & 1 deletion src/services/cache/FikaPlayerRelationsCacheService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { inject, injectable } from "tsyringe";

import { ProfileHelper } from "@spt/helpers/ProfileHelper";
import { ILogger } from "@spt/models/spt/utils/ILogger";
import { JsonUtil } from "@spt/utils/JsonUtil";
import { VFS } from "@spt/utils/VFS";

Expand All @@ -14,6 +15,7 @@ export class FikaPlayerRelationsCacheService {
private readonly playerRelationsPath = "cache/playerRelations.json";

constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("ProfileHelper") protected profileHelper: ProfileHelper,
@inject("JsonUtil") protected jsonUtil: JsonUtil,
@inject("VFS") protected vfs: VFS,
Expand All @@ -26,16 +28,52 @@ export class FikaPlayerRelationsCacheService {
}

this.playerRelations = this.jsonUtil.deserialize(this.vfs.readFile(this.playerRelationsFullPath), this.playerRelationsFullPath);
}

public postInit() {
const profiles = this.profileHelper.getProfiles();
const profileIds = Object.keys(profiles);
var shouldSave = false;

for (const profileId of Object.keys(profiles)) {
for (const profileId of profileIds) {
if (!this.playerRelations[profileId]) {
this.storeValue(profileId, {
Friends: [],
Ignore: [],
});

continue;
}

const originalFriends = this.playerRelations[profileId].Friends;
const friendsToSearch = [...this.playerRelations[profileId].Friends];
for (const friend of friendsToSearch) {
if (!profileIds.includes(friend)) {
const index = originalFriends.indexOf(friend);
if (index > -1) {
this.logger.warning("Deleting missing profile from friends: " + friend);
originalFriends.splice(index, 1);
shouldSave = true;
}
}
}

const originalIgnore = this.playerRelations[profileId].Ignore;
const ignoreToSearch = [...this.playerRelations[profileId].Ignore];
for (const ignore of ignoreToSearch) {
if (!profileIds.includes(ignore)) {
const index = originalIgnore.indexOf(ignore);
if (index > -1) {
this.logger.warning("Deleting missing profile from ignores: " + ignore);
originalIgnore.splice(index, 1);
shouldSave = true;
}
}
}
}

if (shouldSave) {
this.vfs.writeFile(this.playerRelationsFullPath, this.jsonUtil.serialize(this.playerRelations));
}
}

Expand Down

0 comments on commit c9b9239

Please sign in to comment.