Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix player relations #85

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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