-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTaskmanCommandManager.java
More file actions
170 lines (135 loc) · 4.94 KB
/
TaskmanCommandManager.java
File metadata and controls
170 lines (135 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.collectionlogmaster.command;
import static com.collectionlogmaster.util.GsonOverride.GSON;
import com.collectionlogmaster.CollectionLogMasterConfig;
import com.collectionlogmaster.domain.Task;
import com.collectionlogmaster.domain.TaskTier;
import com.collectionlogmaster.domain.command.CommandRequest;
import com.collectionlogmaster.domain.command.CommandResponse;
import com.collectionlogmaster.taskapp.TaskService;
import com.collectionlogmaster.util.EventBusSubscriber;
import com.collectionlogmaster.util.HttpClient;
import com.collectionlogmaster.util.SimpleDebouncer;
import java.time.Instant;
import javax.inject.Inject;
import javax.inject.Singleton;
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.events.ChatMessage;
import net.runelite.api.events.GameStateChanged;
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.eventbus.Subscribe;
import net.runelite.client.events.ConfigChanged;
import net.runelite.client.util.Text;
import okhttp3.HttpUrl;
@Slf4j
@Singleton
public class TaskmanCommandManager extends EventBusSubscriber {
@Inject
private Client client;
@Inject
private ClientThread clientThread;
@Inject
private ChatCommandManager chatCommandManager;
@Inject
private CollectionLogMasterConfig config;
@Inject
private HttpClient httpClient;
@Inject
private TaskService taskService;
@Inject
private SimpleDebouncer updateDebouncer;
private final HttpUrl baseApiUrl = new HttpUrl.Builder()
.scheme("https")
.host("taskman.up.railway.app")
.addPathSegment("task")
.addPathSegment("command")
.build();
private final String COLLECTION_LOG_COMMAND = "!taskman";
public void startUp() {
super.startUp();
if (config.isCommandEnabled()) {
chatCommandManager.registerCommand(COLLECTION_LOG_COMMAND, this::executeCommand);
}
}
public void shutDown() {
super.shutDown();
if (config.isCommandEnabled()) {
chatCommandManager.unregisterCommand(COLLECTION_LOG_COMMAND);
}
}
@Subscribe
public void onConfigChanged(ConfigChanged event) {
if (!event.getGroup().equals(CollectionLogMasterConfig.CONFIG_GROUP)) return;
if (!event.getKey().equals(CollectionLogMasterConfig.IS_COMMAND_ENABLED_KEY)) return;
if (config.isCommandEnabled()) {
chatCommandManager.registerCommand(COLLECTION_LOG_COMMAND, this::executeCommand);
updateServerImmediately();
} else {
chatCommandManager.unregisterCommand(COLLECTION_LOG_COMMAND);
}
}
@Subscribe
public void onGameStateChanged(GameStateChanged e) {
if (e.getGameState() != GameState.LOGGED_IN) return;
clientThread.invokeAtTickEnd(this::updateServer);
}
private void executeCommand(ChatMessage chatMessage, String message) {
log.debug("Executing taskman command: {}", message);
String senderName = chatMessage.getType().equals(ChatMessageType.PRIVATECHATOUT)
? client.getLocalPlayer().getName()
: Text.sanitize(chatMessage.getName());
if (senderName == null) {
log.debug("Couldn't identify message sender");
return;
}
HttpUrl url = baseApiUrl.newBuilder().addPathSegment(senderName).build();
httpClient.get(url, CommandResponse.class)
.thenAccept(res ->
clientThread.invokeLater(() -> replaceChatMessage(chatMessage, res))
);
}
public void updateServer() {
log.debug("Scheduling command update; {}", Instant.now());
updateDebouncer.debounce(this::updateServerImmediately);
}
public void updateServerImmediately() {
if (!config.isCommandEnabled()) {
return;
}
log.debug("Executing command update; {}", Instant.now());
String rsn = client.getLocalPlayer().getName();
if (rsn == null) return;
HttpUrl url = baseApiUrl.newBuilder().addPathSegment(rsn).build();
String taskId = "complete";
Task currentTask = taskService.getActiveTask();
if (currentTask != null) {
taskId = currentTask.getId();
}
TaskTier currentTier = taskService.getCurrentTier();
float currentProgress = taskService.getProgress().get(currentTier) * 100;
CommandRequest data = new CommandRequest(taskId, taskService.getCurrentTier().displayName, (int) currentProgress);
httpClient.put(url, data, null);
}
private void replaceChatMessage(ChatMessage chatMessage, CommandResponse res) {
if (res == null) return;
final String msg = new ChatMessageBuilder()
.append(ChatColorType.NORMAL)
.append("Progress: ")
.append(ChatColorType.HIGHLIGHT)
.append(res.getProgressPercentage() + "% " + res.getTier())
.append(ChatColorType.NORMAL)
.append(" Current task: ")
.append(ChatColorType.HIGHLIGHT)
.append(res.getTask().getName())
.build();
final MessageNode messageNode = chatMessage.getMessageNode();
messageNode.setRuneLiteFormatMessage(msg);
client.refreshChat();
}
}