xptracker: fix calculating xp offset from offline xp

This commit is contained in:
Adam
2019-08-15 13:33:03 -04:00
parent 3a31996ad8
commit af5aa42a1d
2 changed files with 134 additions and 1 deletions

View File

@@ -25,6 +25,7 @@
*/
package net.runelite.client.plugins.xptracker;
import com.google.common.annotations.VisibleForTesting;
import static com.google.common.base.MoreObjects.firstNonNull;
import com.google.common.collect.ImmutableList;
import com.google.inject.Binder;
@@ -36,6 +37,8 @@ import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import javax.inject.Inject;
import lombok.AccessLevel;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Actor;
import net.runelite.api.Client;
@@ -114,6 +117,8 @@ public class XpTrackerPlugin extends Plugin
private OverlayManager overlayManager;
private NavigationButton navButton;
@Setter(AccessLevel.PACKAGE)
@VisibleForTesting
private XpPanel xpPanel;
private XpWorldType lastWorldType;
private String lastUsername;
@@ -412,7 +417,7 @@ public class XpTrackerPlugin extends Plugin
log.debug("Skill xp for {} changed when offline: {} -> {}", skill, skillState.getCurrentXp(), currentXp);
// Offset start xp for offline gains
long diff = skillState.getCurrentXp() - currentXp;
long diff = currentXp - skillState.getCurrentXp();
skillState.setStartXp(skillState.getStartXp() + diff);
}
}
@@ -512,6 +517,11 @@ public class XpTrackerPlugin extends Plugin
}
}
XpStateSingle getSkillState(Skill skill)
{
return xpState.getSkill(skill);
}
XpSnapshotSingle getSkillSnapshot(Skill skill)
{
return xpState.getSkillSnapshot(skill);

View File

@@ -0,0 +1,123 @@
/*
* Copyright (c) 2019, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.plugins.xptracker;
import com.google.inject.Guice;
import com.google.inject.testing.fieldbinder.Bind;
import com.google.inject.testing.fieldbinder.BoundFieldModule;
import java.util.EnumSet;
import javax.inject.Inject;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.Player;
import net.runelite.api.Skill;
import net.runelite.api.WorldType;
import net.runelite.api.events.ExperienceChanged;
import net.runelite.api.events.GameStateChanged;
import net.runelite.api.events.GameTick;
import net.runelite.client.game.NPCManager;
import net.runelite.client.game.SkillIconManager;
import net.runelite.client.ui.ClientToolbar;
import net.runelite.client.ui.overlay.OverlayManager;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class XpTrackerPluginTest
{
@Inject
private XpTrackerPlugin xpTrackerPlugin;
@Mock
@Bind
private ClientToolbar clientToolbar;
@Mock
@Bind
private Client client;
@Mock
@Bind
private SkillIconManager skillIconManager;
@Mock
@Bind
private XpTrackerConfig xpTrackerConfig;
@Mock
@Bind
private NPCManager npcManager;
@Mock
@Bind
private OverlayManager overlayManager;
@Before
public void before()
{
Guice.createInjector(BoundFieldModule.of(this)).injectMembers(this);
when(client.getWorldType()).thenReturn(EnumSet.of(WorldType.MEMBERS));
when(client.getLocalPlayer()).thenReturn(mock(Player.class));
xpTrackerPlugin.setXpPanel(mock(XpPanel.class));
}
@Test
public void testOfflineXp()
{
GameStateChanged gameStateChanged = new GameStateChanged();
gameStateChanged.setGameState(GameState.LOGGING_IN);
// Flag initialization of tracker
xpTrackerPlugin.onGameStateChanged(gameStateChanged);
when(client.getSkillExperience(Skill.ATTACK)).thenReturn(42);
// Initialize tracker
xpTrackerPlugin.onGameTick(new GameTick());
// Gain attack xp
when(client.getSkillExperience(Skill.ATTACK)).thenReturn(100);
ExperienceChanged experienceChanged = new ExperienceChanged();
experienceChanged.setSkill(Skill.ATTACK);
xpTrackerPlugin.onExperienceChanged(experienceChanged);
// Offline gain
when(client.getSkillExperience(Skill.ATTACK)).thenReturn(42000);
// Flag initialization of tracker
xpTrackerPlugin.onGameStateChanged(gameStateChanged);
// Initialize tracker
xpTrackerPlugin.onGameTick(new GameTick());
// Start at 42 xp, gain of 58 xp, offline gain of 41900 xp - offset start XP: 42 + 41900
XpStateSingle skillState = xpTrackerPlugin.getSkillState(Skill.ATTACK);
assertEquals(41942, skillState.getStartXp());
}
}