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

Adding a change to limit how often users are prompted about strange fruits #19

Merged
merged 2 commits into from
Oct 25, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -277,23 +277,30 @@ private void handleRandomEvent(NPC npc)
public void onNpcSpawned(final NpcSpawned event)
{
final NPC npc = event.getNpc();
if (isStrangePlant(npc.getId()))
if (!isStrangePlant(npc.getId()))
{
Player player = client.getLocalPlayer();
if (player.getWorldLocation().distanceTo(npc.getWorldLocation()) == STRANGE_PLANT_SPAWN_RADIUS)
{
/**
* Unfortunately we cannot determine if the Strange Plant belongs to the player
* (See onInteractingChange)
* So we need to add an unconfirmed record (UI only) that allows the player to
* confirm if the plant belongs to them. Only then will it update the records.
*/
RandomEventRecord record = createRandomEventRecord(npc);
panel.addUnconfirmedRandom(record);
chatMessageManager.queue(QueuedMessage.builder().type(ChatMessageType.CONSOLE).runeLiteFormattedMessage(PLANT_SPAWNED_NOTIFICATION_MESSAGE).build());
notifier.notify(PLANT_SPAWNED_NOTIFICATION_MESSAGE);
}
return;
}
Player player = client.getLocalPlayer();
if (player.getWorldLocation().distanceTo(npc.getWorldLocation()) != STRANGE_PLANT_SPAWN_RADIUS)
{
return;
}
if (!timeTracking.isPossibleTimeForRandomEvent())
{
// We only want to notify about strange plants when it's possibly the user's random
return;
}
/**
* Unfortunately we cannot determine if the Strange Plant belongs to the player
* (See onInteractingChange)
* So we need to add an unconfirmed record (UI only) that allows the player to
* confirm if the plant belongs to them. Only then will it update the records.
*/
RandomEventRecord record = createRandomEventRecord(npc);
panel.addUnconfirmedRandom(record);
chatMessageManager.queue(QueuedMessage.builder().type(ChatMessageType.CONSOLE).runeLiteFormattedMessage(PLANT_SPAWNED_NOTIFICATION_MESSAGE).build());
notifier.notify(PLANT_SPAWNED_NOTIFICATION_MESSAGE);
}

@Subscribe
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/randomEventAnalytics/TimeTracking.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class TimeTracking
{
public static final int SPAWN_INTERVAL_SECONDS = 60 * 5;
private static final int SPAWN_INTERVAL_MARGIN_SECONDS = 0;
private static final int SPAWN_INTERVAL_TIMEFRAME_SECONDS = 15;
public static final int SECONDS_IN_AN_HOUR = 60 /*seconds in a minute*/ * 60 /*minutes in an hour*/;

private int sessionTicks;

Expand Down Expand Up @@ -100,6 +102,35 @@ public int getNextRandomEventEstimation()
return SPAWN_INTERVAL_SECONDS - secondsMod;
}

private boolean isInsideRandomEventWindow()
{
int loginTime = getSecondsSinceLogin();
int secondsMod = loginTime % SPAWN_INTERVAL_SECONDS;
// If we're within 15 seconds of an event window, it's considered a possible time.
return secondsMod <= SPAWN_INTERVAL_TIMEFRAME_SECONDS || secondsMod >= SPAWN_INTERVAL_SECONDS - SPAWN_INTERVAL_TIMEFRAME_SECONDS;
}

public boolean isPossibleTimeForRandomEvent()
{
if (lastRandomSpawnInstant == null)
{
return isInsideRandomEventWindow();
}

if (!hasLoggedInLongEnoughForSpawn())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are your thoughts about checking if there is a previously logged random here as well, as part of a solve for:

[...] the shortest is invalid data, because it's the first one. It essentially gives the time since logging instead of the time since the previous random since there was no previous random.

eg

private boolean isEligibleForRandom() { // TODO: Better method name, and maybe closure/lambda in isPossibleTimeForRandomEvent
  int loginTime = getSecondsSinceLogin();
  int secondsMod = loginTime % SPAWN_INTERVAL_SECONDS;
  // If we're within 15 seconds of an event window, it's considered a possible time.
  return secondsMod <= SPAWN_INTERVAL_TIMEFRAME_SECONDS || secondsMod >= SPAWN_INTERVAL_SECONDS - SPAWN_INTERVAL_TIMEFRAME_SECONDS;
}

public boolean isPossibleTimeForRandomEvent() {
  if (this.lastRandomSpawnInstant == null) { // This should determine if there's a previous random.
    return this.isEligibleForRandom();
  }
  // ...
  // your existing checks here
  return this.isEligibleForRandom();
}

Though I may be getting too edge-casey; there are probably other existing facets of this class that do not properly account for first time events. 😓

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that is an issue with this, so I say good catch.

I think isEligibleForRandom sounds like it would check if enough time has passed since the last random. I think it should be isInsideRandomEventWindow or something like that

{
return false;
}

if (getTotalSecondsSinceLastRandomEvent() < SECONDS_IN_AN_HOUR)
{
// There's a minimum of an hour between random events.
return false;
}

return isInsideRandomEventWindow();
}

public void setRandomEventSpawned()
{
lastRandomSpawnInstant = Instant.now();
Expand Down