Skip to content

Commit 87006f8

Browse files
committed
Refactor patrol helicopter stuff
1 parent 6e431f1 commit 87006f8

3 files changed

Lines changed: 60 additions & 50 deletions

File tree

src/managers/rustPlusManager.ts

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -958,47 +958,50 @@ export class RustPlusInstance {
958958
const gInstance = gim.getGuildInstance(this.guildId) as GuildInstance;
959959
const language = gInstance.generalSettings.language;
960960

961+
if (!this.rpMapMarkers) {
962+
return '\u200B';
963+
}
964+
961965
const strings: string[] = [];
962-
if (this.rpMapMarkers) {
963-
if (this.rpMapMarkers.patrolHelicopters.length === 0) {
964-
const dateWhenDestroyed = this.rpMapMarkers.datePatrolHelicopterDestroyed;
965-
const dateWhenLeftMap = this.rpMapMarkers.datePatrolHelicopterLeftMap;
966-
const destroyedLocation = this.rpMapMarkers.patrolHelicopterLastDestroyedLocation;
967-
968-
if (dateWhenDestroyed !== null) {
969-
const timeSinceDestroyedSeconds = Math.floor(dateWhenDestroyed.getTime() / 1000);
970-
const timeSinceDestroyedString = `<t:${timeSinceDestroyedSeconds}:R>`;
971-
strings.push(lm.getIntl(language, 'infoChannelEmbedEventPhraseDestroyedTimeAtPos', {
972-
time: timeSinceDestroyedString,
973-
location: destroyedLocation ?? lm.getIntl(language, 'unknown')
974-
}));
975-
}
966+
if (this.rpMapMarkers.patrolHelicopters.length === 0) {
967+
const dateDestroyed = this.rpMapMarkers.datePatrolHelicopterDestroyed;
968+
const dateDespawned = this.rpMapMarkers.datePatrolHelicopterDespawned;
969+
const destroyedLocation = this.rpMapMarkers.patrolHelicopterLastDestroyedLocation;
970+
971+
if (dateDestroyed) {
972+
const unixTimestampDestroyed = Math.floor(dateDestroyed.getTime() / 1000);
973+
const timeDestroyed = Timer.getDiscordRelativeTime(unixTimestampDestroyed);
974+
strings.push(lm.getIntl(language, 'infoChannelEmbedEventPhraseDestroyedTimeAtPos', {
975+
time: timeDestroyed,
976+
location: destroyedLocation ?? lm.getIntl(language, 'unknown')
977+
}));
978+
}
976979

977-
if (dateWhenLeftMap !== null) {
978-
const timeSinceLeftMapSeconds = Math.floor(dateWhenLeftMap.getTime() / 1000);
979-
const timeSinceLeftMapString = `<t:${timeSinceLeftMapSeconds}:R>`;
980-
strings.push(lm.getIntl(language, 'infoChannelEmbedEventPhraseLeftTime', {
981-
time: timeSinceLeftMapString
982-
}));
983-
}
980+
if (dateDespawned) {
981+
const unixTimestampDespawned = Math.floor(dateDespawned.getTime() / 1000);
982+
const timeDespawned = Timer.getDiscordRelativeTime(unixTimestampDespawned);
983+
strings.push(lm.getIntl(language, 'infoChannelEmbedEventPhraseLeftTime', {
984+
time: timeDespawned
985+
}));
986+
}
984987

985-
if (strings.length === 0) {
986-
strings.push(lm.getIntl(language, 'infoChannelEmbedEventPhraseNotActive'));
987-
}
988+
if (strings.length === 0) {
989+
strings.push(lm.getIntl(language, 'infoChannelEmbedEventPhraseNotActive'));
988990
}
991+
}
989992

990-
for (const patrolHelicopter of this.rpMapMarkers.patrolHelicopters) {
991-
const metaData = this.rpMapMarkers.patrolHelicopterMetaData[patrolHelicopter.id];
992-
const pos = getPos(patrolHelicopter.x, patrolHelicopter.y, this);
993-
const posString = (pos !== null) ? getPosString(pos, this, true, false) :
994-
lm.getIntl(language, 'unknown');
993+
for (const patrolHelicopter of this.rpMapMarkers.patrolHelicopters) {
994+
const metaData = this.rpMapMarkers.patrolHelicopterMetaData[patrolHelicopter.id];
995995

996-
if (metaData.isLeaving) {
997-
strings.push(lm.getIntl(language, 'infoChannelEmbedEventPhraseLeavingAtPos', { pos: posString }));
998-
}
999-
else {
1000-
strings.push(lm.getIntl(language, 'infoChannelEmbedEventPhraseLocatedAtPos', { pos: posString }));
1001-
}
996+
const pos = getPos(patrolHelicopter.x, patrolHelicopter.y, this);
997+
const posString = (pos !== null) ? getPosString(pos, this, true, false) :
998+
lm.getIntl(language, 'unknown');
999+
1000+
if (metaData.isLeaving) {
1001+
strings.push(lm.getIntl(language, 'infoChannelEmbedEventPhraseLeavingAtPos', { pos: posString }));
1002+
}
1003+
else {
1004+
strings.push(lm.getIntl(language, 'infoChannelEmbedEventPhraseLocatedAtPos', { pos: posString }));
10021005
}
10031006
}
10041007

src/prefixCommands/heli.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,38 +54,39 @@ export async function execute(rpInstance: RustPlusInstance, args: string[],
5454

5555
if (rpInstance.rpMapMarkers === null) return false;
5656

57+
const unixTimestampNow = Math.floor(new Date().getTime() / 1000);
58+
5759
const response: string[] = [];
5860
if (rpInstance.rpMapMarkers.patrolHelicopters.length === 0) {
59-
const dateWhenDestroyed = rpInstance.rpMapMarkers.datePatrolHelicopterDestroyed;
60-
const dateWhenLeftMap = rpInstance.rpMapMarkers.datePatrolHelicopterLeftMap;
61+
const dateDestroyed = rpInstance.rpMapMarkers.datePatrolHelicopterDestroyed;
62+
const dateDespawned = rpInstance.rpMapMarkers.datePatrolHelicopterDespawned;
6163
const destroyedLocation = rpInstance.rpMapMarkers.patrolHelicopterLastDestroyedLocation;
6264

63-
if (dateWhenDestroyed !== null) {
64-
const timeSinceDestroyedSeconds = (new Date().getTime() - dateWhenDestroyed.getTime()) / 1000;
65+
if (dateDestroyed) {
66+
const unixTimestampDestroyed = Math.floor(dateDestroyed.getTime() / 1000);
67+
const secondsSinceDestroyed = unixTimestampNow - unixTimestampDestroyed;
6568
response.push(lm.getIntl(language, 'timeSinceDestroyed', {
66-
time: secondsToFullScale(timeSinceDestroyedSeconds),
69+
time: secondsToFullScale(secondsSinceDestroyed),
6770
location: destroyedLocation ?? lm.getIntl(language, 'unknown')
6871
}));
6972
}
7073

71-
if (dateWhenLeftMap !== null) {
72-
const timeSinceLeftMapSeconds = (new Date().getTime() - dateWhenLeftMap.getTime()) / 1000;
74+
if (dateDespawned) {
75+
const unixTimestampDespawned = Math.floor(dateDespawned.getTime() / 1000);
76+
const secondsSinceDespawned = unixTimestampNow - unixTimestampDespawned;
7377
response.push(lm.getIntl(language, 'timeSinceLeftMap', {
74-
time: secondsToFullScale(timeSinceLeftMapSeconds)
78+
time: secondsToFullScale(secondsSinceDespawned)
7579
}));
7680
}
7781

7882
if (response.length === 0) {
7983
response.push(lm.getIntl(language, 'patrolHelicopterNotOnMap'));
8084
}
81-
82-
rpInstance.sendPrefixCommandResponse(response.join(' '), inGame);
83-
log.info(`${fn} ${response.join(' ')}`, logParam);
84-
return true;
8585
}
8686

8787
for (const patrolHelicopter of rpInstance.rpMapMarkers.patrolHelicopters) {
8888
const metaData = rpInstance.rpMapMarkers.patrolHelicopterMetaData[patrolHelicopter.id];
89+
8990
const pos = getPos(patrolHelicopter.x, patrolHelicopter.y, rpInstance);
9091
const posString = (pos !== null) ? getPosString(pos, rpInstance, false, false) :
9192
lm.getIntl(language, 'unknown');

src/structures/rustPlusMapMarkers.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ export class RustPlusMapMarkers {
122122
public dateCh47Spawned: { [id: number]: Date };
123123
public dateCh47Despawned: Date | null;
124124
public dateCargoShipLeftMap: Date | null;
125-
public datePatrolHelicopterLeftMap: Date | null;
125+
public datePatrolHelicopterSpawned: { [id: number]: Date };
126+
public datePatrolHelicopterDespawned: Date | null;
126127
public datePatrolHelicopterDestroyed: Date | null;
127128
public dateTravellingVendorSpawned: { [id: number]: Date };
128129
public dateTravellingVendorDespawned: Date | null;
@@ -170,7 +171,8 @@ export class RustPlusMapMarkers {
170171
this.dateCh47Spawned = {};
171172
this.dateCh47Despawned = null;
172173
this.dateCargoShipLeftMap = null;
173-
this.datePatrolHelicopterLeftMap = null;
174+
this.datePatrolHelicopterSpawned = {};
175+
this.datePatrolHelicopterDespawned = null;
174176
this.datePatrolHelicopterDestroyed = null;
175177
this.dateTravellingVendorSpawned = {};
176178
this.dateTravellingVendorDespawned = null;
@@ -743,6 +745,9 @@ export class RustPlusMapMarkers {
743745
const eventText = lm.getIntl(language, phrase, { location: patrolHelicopterPosString });
744746
this.rpInstance.sendEventNotification('patrolHelicopterSpawned', eventText);
745747

748+
if (!this.firstPoll) {
749+
this.datePatrolHelicopterSpawned[marker.id] = new Date();
750+
}
746751
this.patrolHelicopters.push(marker);
747752
}
748753

@@ -760,7 +765,7 @@ export class RustPlusMapMarkers {
760765
this.rpInstance.sendEventNotification(settingsKey as keyof EventNotificationSettings, eventText);
761766
}
762767

763-
this.datePatrolHelicopterLeftMap = new Date();
768+
this.datePatrolHelicopterDespawned = new Date();
764769
if (!isOutside) {
765770
this.datePatrolHelicopterDestroyed = new Date();
766771
if (patrolHelicopterPosString) {
@@ -769,6 +774,7 @@ export class RustPlusMapMarkers {
769774
}
770775

771776
delete this.patrolHelicopterMetaData[marker.id];
777+
delete this.datePatrolHelicopterSpawned[marker.id];
772778
this.patrolHelicopters = this.patrolHelicopters.filter(e => e.id !== marker.id);
773779
}
774780

0 commit comments

Comments
 (0)