Skip to content

Commit

Permalink
Merge pull request #2 from remcowesterhoud/chat_command
Browse files Browse the repository at this point in the history
feat: add !taskman chat command
  • Loading branch information
remcowesterhoud authored Oct 10, 2022
2 parents 3aaca1a + 6d680e6 commit 4f676cf
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 20 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/westerhoud/osrs/taskman/TaskmanConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,13 @@ default String websitePassword() {
default boolean showOverlay() {
return false;
}

@ConfigItem(
position = 8,
keyName = "taskmanCommand",
name = "Enable !taskman chat command",
description = "Send your current progress and task into the chat")
default boolean taskmanCommand() {
return true;
}
}
96 changes: 90 additions & 6 deletions src/main/java/com/westerhoud/osrs/taskman/TaskmanPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,27 @@
import com.westerhoud.osrs.taskman.domain.AccountCredentials;
import com.westerhoud.osrs.taskman.domain.AccountProgress;
import com.westerhoud.osrs.taskman.domain.Task;
import com.westerhoud.osrs.taskman.domain.TaskmanCommandData;
import com.westerhoud.osrs.taskman.service.TaskService;
import com.westerhoud.osrs.taskman.ui.CurrentTaskOverlay;
import com.westerhoud.osrs.taskman.ui.TaskmanPluginPanel;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.inject.Inject;
import javax.swing.SwingUtilities;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.ChatMessageType;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.MessageNode;
import net.runelite.api.Player;
import net.runelite.api.events.ChatMessage;
import net.runelite.api.events.GameStateChanged;
import net.runelite.api.events.GameTick;
import net.runelite.client.callback.ClientThread;
import net.runelite.client.chat.ChatColorType;
import net.runelite.client.chat.ChatCommandManager;
import net.runelite.client.chat.ChatMessageBuilder;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.events.ConfigChanged;
Expand All @@ -21,30 +34,36 @@
import net.runelite.client.ui.NavigationButton;
import net.runelite.client.ui.overlay.OverlayManager;
import net.runelite.client.util.ImageUtil;
import net.runelite.client.util.Text;
import okhttp3.OkHttpClient;

@Slf4j
@PluginDescriptor(name = "Taskman")
public class TaskmanPlugin extends Plugin {

public static final String TASKMAN_CONFIG_GROUP = "taskman";
private static final String TASKMAN_CONFIG_GROUP = "taskman";
private static final String TASKMAN_CHAT_COMMAND = "!taskman";
@Inject private Client client;
@Inject private ClientThread clientThread;
@Inject private ClientToolbar clientToolbar;
@Inject private TaskmanConfig config;
@Inject private OkHttpClient okHttpClient;
@Inject private OverlayManager overlayManager;
@Inject private CurrentTaskOverlay currentTaskOverlay;
@Inject private ChatCommandManager chatCommandManager;

private TaskmanPluginPanel sidePanel;
private TaskService taskService;
private NavigationButton navigationButton;
private boolean loggedIn = false;
private boolean sidePanelInitialized = false;

@Override
protected void startUp() throws Exception {
// Sidebar
final BufferedImage icon = ImageUtil.loadImageResource(getClass(), "icon.png");

taskService = new TaskService(okHttpClient);
chatCommandManager.registerCommandAsync(TASKMAN_CHAT_COMMAND, this::getTaskmanCommandData);

sidePanel = new TaskmanPluginPanel(this);
navigationButton =
Expand All @@ -63,28 +82,67 @@ protected void shutDown() throws Exception {
// Sidebar
clientToolbar.removeNavigation(navigationButton);
overlayManager.remove(currentTaskOverlay);
chatCommandManager.unregisterCommand(TASKMAN_CHAT_COMMAND);
}

public Task getCurrentTask() throws Exception {
final Task task = taskService.getCurrentTask(getCredentials());
final Task task = taskService.getCurrentTask(getCredentials(), getRsn());
currentTaskOverlay.setTask(task);
return task;
}

public Task generateTask() throws Exception {
final Task task = taskService.generateTask(getCredentials());
final Task task = taskService.generateTask(getCredentials(), getRsn());
currentTaskOverlay.setTask(task);
return task;
}

public Task completeTask() throws Exception {
final Task task = taskService.completeTask(getCredentials());
final Task task = taskService.completeTask(getCredentials(), getRsn());
currentTaskOverlay.setTask(task);
return task;
}

public AccountProgress progress() throws Exception {
return taskService.getAccountProgress(getCredentials());
return taskService.getAccountProgress(getCredentials(), getRsn());
}

private void getTaskmanCommandData(final ChatMessage chatMessage, final String message) {
if (!config.taskmanCommand()) {
return;
}

final ChatMessageType type = chatMessage.getType();
final String rsn;
if (type == ChatMessageType.PRIVATECHATOUT) {
rsn = getRsn();
} else {
rsn = Text.removeTags(chatMessage.getName()).replace('\u00A0', ' ');
}

final TaskmanCommandData data;
try {
data = taskService.getChatCommandData(rsn);
} catch (final IOException ex) {
log.debug("Unable to get chat command data", ex);
return;
}

final String response =
new ChatMessageBuilder()
.append(ChatColorType.NORMAL)
.append("Progress: ")
.append(ChatColorType.HIGHLIGHT)
.append(data.getProgressPercentage() + "% " + data.getTier())
.append(ChatColorType.NORMAL)
.append(" Current task: ")
.append(ChatColorType.HIGHLIGHT)
.append(data.getTask().getName())
.build();

final MessageNode messageNode = chatMessage.getMessageNode();
messageNode.setRuneLiteFormatMessage(response);
client.refreshChat();
}

private AccountCredentials getCredentials() {
Expand All @@ -100,6 +158,32 @@ private AccountCredentials getCredentials() {
}
}

private String getRsn() {
final Player player = client.getLocalPlayer();
if (player == null) {
throw new IllegalArgumentException("Please login first!");
}
return player.getName();
}

@Subscribe
public void onGameStateChanged(final GameStateChanged gameStateChanged) {
if (gameStateChanged.getGameState() == GameState.LOGGED_IN) {
loggedIn = true;
} else if (gameStateChanged.getGameState() == GameState.LOGIN_SCREEN) {
loggedIn = false;
sidePanel.onLogout();
}
}

@Subscribe
public void onGameTick(final GameTick gameTick) {
if (!sidePanelInitialized && loggedIn) {
sidePanel.init();
sidePanelInitialized = true;
}
}

@Subscribe
public void onConfigChanged(final ConfigChanged configChanged) {
if (configChanged.getGroup().equals(TASKMAN_CONFIG_GROUP)) {
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/westerhoud/osrs/taskman/api/TaskService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
import com.westerhoud.osrs.taskman.domain.AccountCredentials;
import com.westerhoud.osrs.taskman.domain.AccountProgress;
import com.westerhoud.osrs.taskman.domain.Task;
import com.westerhoud.osrs.taskman.domain.TaskmanCommandData;
import java.io.IOException;

public interface TaskService {

Task getCurrentTask(final AccountCredentials credentials) throws IOException;
Task getCurrentTask(final AccountCredentials credentials, final String name) throws IOException;

Task generateTask(final AccountCredentials credentials) throws IOException;
Task generateTask(final AccountCredentials credentials, final String name) throws IOException;

Task completeTask(final AccountCredentials credentials) throws IOException;
Task completeTask(final AccountCredentials credentials, final String name) throws IOException;

AccountProgress getAccountProgress(final AccountCredentials credentials) throws IOException;
AccountProgress getAccountProgress(final AccountCredentials credentials, final String name)
throws IOException;

TaskmanCommandData getChatCommandData(String rsn) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.westerhoud.osrs.taskman.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class TaskmanCommandData {
private Task task;
private String tier;
private int progressPercentage;
}
33 changes: 29 additions & 4 deletions src/main/java/com/westerhoud/osrs/taskman/service/TaskService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import com.westerhoud.osrs.taskman.domain.AccountProgress;
import com.westerhoud.osrs.taskman.domain.ErrorResponse;
import com.westerhoud.osrs.taskman.domain.Task;
import com.westerhoud.osrs.taskman.domain.TaskmanCommandData;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Request.Builder;
import okhttp3.RequestBody;
import okhttp3.Response;

Expand All @@ -19,13 +21,15 @@ public class TaskService implements com.westerhoud.osrs.taskman.api.TaskService
public static final String TASKMAN_IDENTIFIER_HEADER = "x-taskman-identifier";
public static final String TASKMAN_PASSWORD_HEADER = "x-taskman-password";
public static final String TASKMAN_SOURCE_HEADER = "x-taskman-source";
public static final String TASKMAN_RSN_HEADER = "x-taskman-rsn";
private static final String BASE_URL = "https://osrs-taskman.herokuapp.com/task";
private final OkHttpClient client;
private final Gson gson;
private final String currentUrl;
private final String generateUrl;
private final String completeUrl;
private final String progressUrl;
private final String commandUrl;

public TaskService(final OkHttpClient okHttpClient) {
client = okHttpClient;
Expand All @@ -34,10 +38,12 @@ public TaskService(final OkHttpClient okHttpClient) {
generateUrl = BASE_URL + "/generate";
completeUrl = BASE_URL + "/complete";
progressUrl = BASE_URL + "/progress";
commandUrl = BASE_URL + "/command/%s";
}

@Override
public Task getCurrentTask(final AccountCredentials credentials) throws IOException {
public Task getCurrentTask(final AccountCredentials credentials, final String rsn)
throws IOException {
if (!credentials.isValid()) {
throw new IllegalArgumentException(
"Please configure your credentials in the plugin configurations");
Expand All @@ -49,38 +55,43 @@ public Task getCurrentTask(final AccountCredentials credentials) throws IOExcept
.addHeader(TASKMAN_IDENTIFIER_HEADER, credentials.getIdentifier())
.addHeader(TASKMAN_PASSWORD_HEADER, credentials.getPassword())
.addHeader(TASKMAN_SOURCE_HEADER, credentials.getSource().name())
.addHeader(TASKMAN_RSN_HEADER, rsn)
.get()
.build();

return executeRequest(request);
}

@Override
public Task generateTask(final AccountCredentials credentials) throws IOException {
public Task generateTask(final AccountCredentials credentials, final String rsn)
throws IOException {
final Request request =
new Request.Builder()
.url(generateUrl)
.header("Content-Type", "application/json")
.addHeader(TASKMAN_SOURCE_HEADER, credentials.getSource().name())
.addHeader(TASKMAN_RSN_HEADER, rsn)
.post(getRequestBody(credentials))
.build();
return executeRequest(request);
}

@Override
public Task completeTask(final AccountCredentials credentials) throws IOException {
public Task completeTask(final AccountCredentials credentials, final String rsn)
throws IOException {
final Request request =
new Request.Builder()
.url(completeUrl)
.header("Content-Type", "application/json")
.addHeader(TASKMAN_SOURCE_HEADER, credentials.getSource().name())
.addHeader(TASKMAN_RSN_HEADER, rsn)
.post(getRequestBody(credentials))
.build();
return executeRequest(request);
}

@Override
public AccountProgress getAccountProgress(final AccountCredentials credentials)
public AccountProgress getAccountProgress(final AccountCredentials credentials, final String rsn)
throws IOException {
if (!credentials.isValid()) {
throw new IllegalArgumentException(
Expand All @@ -93,6 +104,7 @@ public AccountProgress getAccountProgress(final AccountCredentials credentials)
.addHeader(TASKMAN_IDENTIFIER_HEADER, credentials.getIdentifier())
.addHeader(TASKMAN_PASSWORD_HEADER, credentials.getPassword())
.addHeader(TASKMAN_SOURCE_HEADER, credentials.getSource().name())
.addHeader(TASKMAN_RSN_HEADER, rsn)
.get()
.build();

Expand All @@ -106,6 +118,19 @@ public AccountProgress getAccountProgress(final AccountCredentials credentials)
throw new IllegalArgumentException(error.getMessage());
}

@Override
public TaskmanCommandData getChatCommandData(final String rsn) throws IOException {
final Request request = new Builder().url(String.format(commandUrl, rsn)).get().build();

final Response response = client.newCall(request).execute();

if (response.code() == 200) {
return gson.fromJson(response.body().string(), TaskmanCommandData.class);
} else {
throw new IllegalArgumentException("Could not get task command data for rsn: " + rsn);
}
}

private RequestBody getRequestBody(final AccountCredentials credentials) {
return RequestBody.create(MediaType.parse("application/json"), gson.toJson(credentials));
}
Expand Down
Loading

0 comments on commit 4f676cf

Please sign in to comment.