Skip to content
This repository was archived by the owner on Feb 9, 2022. It is now read-only.

Commit 7561857

Browse files
ExplvExplv
authored andcommitted
Automatically load worlds from the OSRS website
Adding new world selector for better customisation
1 parent a38e3d7 commit 7561857

File tree

7 files changed

+289
-90
lines changed

7 files changed

+289
-90
lines changed

osbot_manager/src/bot_parameters/configuration/Configuration.java

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.net.ServerSocket;
2525
import java.nio.file.Paths;
2626
import java.util.*;
27+
import java.util.stream.Collectors;
2728

2829
public final class Configuration implements BotParameter, Copyable<Configuration>, Serializable {
2930

@@ -42,9 +43,7 @@ public final class Configuration implements BotParameter, Copyable<Configuration
4243
private SimpleBooleanProperty noRandoms = new SimpleBooleanProperty();
4344
private SimpleBooleanProperty noInterface = new SimpleBooleanProperty();
4445
private SimpleBooleanProperty noRender = new SimpleBooleanProperty();
45-
private SimpleObjectProperty<WorldType> worldType = new SimpleObjectProperty<>();
46-
private SimpleIntegerProperty world = new SimpleIntegerProperty(-1);
47-
private SimpleBooleanProperty randomizeWorld = new SimpleBooleanProperty();
46+
private SimpleListProperty<World> worlds = new SimpleListProperty<>(FXCollections.observableArrayList());
4847
private SimpleBooleanProperty isRunning = new SimpleBooleanProperty();
4948
private String logFileName;
5049

@@ -109,17 +108,7 @@ public final boolean isNoRender() {
109108
return noRender.get();
110109
}
111110

112-
public final WorldType getWorldType() {
113-
return worldType.get();
114-
}
115-
116-
public final Integer getWorld() {
117-
return world.get();
118-
}
119-
120-
public boolean isRandomizeWorld() {
121-
return randomizeWorld.get();
122-
}
111+
public final ObservableList<World> getWorlds() { return worlds.get(); }
123112

124113
public final void setRunescapeAccount(final RunescapeAccount runescapeAccount) {
125114
this.runescapeAccount.set(runescapeAccount);
@@ -173,16 +162,8 @@ public final void setNoRender(final boolean noRender) {
173162
this.noRender.set(noRender);
174163
}
175164

176-
public final void setWorldType(final WorldType worldType) {
177-
this.worldType.set(worldType);
178-
}
179-
180-
public final void setWorld(final Integer world) {
181-
this.world.set(world);
182-
}
183-
184-
public final void setRandomizeWorld(final boolean randomizeWorld) {
185-
this.randomizeWorld.set(randomizeWorld);
165+
public final void setWorlds(final List<World> worlds) {
166+
this.worlds.setAll(worlds);
186167
}
187168

188169
public final boolean isRunning() {
@@ -211,9 +192,7 @@ private void writeObject(ObjectOutputStream stream) throws IOException {
211192
stream.writeInt(getDebugPort());
212193
stream.writeBoolean(isLowCpuMode());
213194
stream.writeBoolean(isLowResourceMode());
214-
stream.writeObject(getWorldType());
215-
stream.writeInt(getWorld());
216-
stream.writeBoolean(isRandomizeWorld());
195+
stream.writeObject(new ArrayList<>(getWorlds()));
217196
stream.writeBoolean(isReflection());
218197
stream.writeBoolean(isNoRandoms());
219198
stream.writeBoolean(isNoInterface());
@@ -238,9 +217,19 @@ private void readObject(ObjectInputStream stream) throws ClassNotFoundException,
238217
debugPort = new SimpleIntegerProperty(stream.readInt());
239218
lowCpuMode = new SimpleBooleanProperty(stream.readBoolean());
240219
lowResourceMode = new SimpleBooleanProperty(stream.readBoolean());
241-
worldType = new SimpleObjectProperty<>((WorldType) stream.readObject());
242-
world = new SimpleIntegerProperty(stream.readInt());
243-
randomizeWorld = new SimpleBooleanProperty(stream.readBoolean());
220+
221+
Object worldObj = stream.readObject();
222+
223+
// If old options are being used
224+
if (worldObj instanceof WorldType) {
225+
stream.readInt(); // world num
226+
stream.readBoolean(); // randomize world
227+
worlds = new SimpleListProperty<>(FXCollections.observableArrayList(World.getWorlds()));
228+
} else {
229+
List<World> selectedWorlds = (List<World>) worldObj;
230+
worlds = new SimpleListProperty<>(FXCollections.observableArrayList(selectedWorlds));
231+
}
232+
244233
try {
245234
reflection = new SimpleBooleanProperty(stream.readBoolean());
246235
noRandoms = new SimpleBooleanProperty(stream.readBoolean());
@@ -319,16 +308,8 @@ public final String[] toParameter() {
319308
Collections.addAll(parameter, "-allow", String.join(",", allowParams));
320309
}
321310

322-
int worldVal;
323-
if (randomizeWorld.get()) {
324-
worldVal = worldType.get().worlds[new Random().nextInt(worldType.get().worlds.length)];
325-
} else {
326-
worldVal = world.get();
327-
}
328-
329-
if (worldVal != -1) {
330-
Collections.addAll(parameter, "-world", String.valueOf(worldVal));
331-
}
311+
World world = worlds.get(new Random().nextInt(worlds.size()));
312+
Collections.addAll(parameter, world.toParameter());
332313

333314
return parameter.toArray(new String[parameter.size()]);
334315
}
@@ -355,9 +336,7 @@ public Configuration createCopy() {
355336
configurationCopy.setDebugPort(getDebugPort());
356337
configurationCopy.setLowCpuMode(isLowCpuMode());
357338
configurationCopy.setLowResourceMode(isLowResourceMode());
358-
configurationCopy.setWorldType(getWorldType());
359-
configurationCopy.setWorld(getWorld());
360-
configurationCopy.setRandomizeWorld(isRandomizeWorld());
339+
configurationCopy.setWorlds(getWorlds());
361340
configurationCopy.setReflection(isReflection());
362341
configurationCopy.setNoRandoms(isNoRandoms());
363342
configurationCopy.setNoRender(isNoRender());
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package bot_parameters.configuration;
2+
3+
import bot_parameters.interfaces.BotParameter;
4+
import org.jsoup.Jsoup;
5+
import org.jsoup.nodes.Document;
6+
import org.jsoup.nodes.Element;
7+
import org.jsoup.select.Elements;
8+
9+
import java.io.IOException;
10+
import java.io.Serializable;
11+
import java.util.ArrayList;
12+
import java.util.Comparator;
13+
import java.util.List;
14+
15+
public class World implements BotParameter, Serializable {
16+
private static final long serialVersionUID = -9046100616950752889L;
17+
18+
private static List<World> worlds;
19+
20+
private static Comparator<World> worldComparator = (w1, w2) -> {
21+
int typeComparison = w1.getType().compareTo(w2.getType());
22+
23+
if (typeComparison != 0) {
24+
return typeComparison;
25+
}
26+
27+
return Integer.compare(w1.getNumber(), w2.getNumber());
28+
};
29+
30+
private final WorldType type;
31+
private final int number;
32+
private final String detail;
33+
34+
public World(final WorldType type, final int number, final String detail) {
35+
this.type = type;
36+
this.number = number;
37+
this.detail = detail;
38+
}
39+
40+
public final WorldType getType() {
41+
return type;
42+
}
43+
44+
public final int getNumber() {
45+
return number;
46+
}
47+
48+
public final String getDetail() {
49+
return detail;
50+
}
51+
52+
@Override
53+
public String toString() {
54+
String worldStr = getType().toString() + " " + getNumber();
55+
56+
if (!getDetail().isEmpty()) {
57+
worldStr += " - " + getDetail();
58+
}
59+
60+
return worldStr;
61+
}
62+
63+
public static List<World> getWorlds() {
64+
if (worlds == null) {
65+
loadWorlds();
66+
}
67+
return worlds;
68+
}
69+
70+
public static Comparator<World> getWorldComparator() {
71+
return worldComparator;
72+
}
73+
74+
private static void loadWorlds() {
75+
worlds = new ArrayList<>();
76+
77+
try {
78+
Document doc = Jsoup.connect("http://oldschool.runescape.com/slu").get();
79+
Elements servers = doc.select("tr.server-list__row");
80+
for (Element server : servers) {
81+
Element serverLink = server.selectFirst(".server-list__world-link");
82+
String worldIDStr = serverLink.id().replaceAll("slu-world-", "");
83+
int worldNum = Integer.parseInt(worldIDStr);
84+
85+
Element membershipType = server.selectFirst(".server-list__row-cell--type");
86+
boolean members = membershipType.html().equals("Members");
87+
88+
WorldType worldType = members ? WorldType.MEMBERS : WorldType.F2P;
89+
90+
String worldDetail = membershipType.nextElementSibling().html();
91+
92+
if (worldDetail.equals("-")) {
93+
worldDetail = "";
94+
}
95+
96+
worlds.add(new World(worldType, worldNum, worldDetail));
97+
}
98+
} catch (IOException e) {
99+
e.printStackTrace();
100+
}
101+
102+
worlds.sort(worldComparator);
103+
}
104+
105+
@Override
106+
public String[] toParameter() {
107+
return new String[] { "-world", String.valueOf(getNumber()) };
108+
}
109+
}

osbot_manager/src/bot_parameters/configuration/WorldType.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,17 @@
33
import java.io.Serializable;
44

55
public enum WorldType implements Serializable {
6-
F2P (301, 308, 316, 326, 335, 382, 383, 384, 393, 394),
7-
MEMBERS (302, 303, 304, 305, 306, 309, 310, 311, 312, 313, 314, 317, 318, 319, 320,
8-
321, 322, 327, 328, 329, 330, 333, 334, 336, 338, 341, 342, 343, 344,
9-
346, 350, 351, 352, 354, 357, 358, 359, 360, 362, 365,
10-
367, 368, 369, 370, 375, 376, 377, 386),
11-
PVP (325, 337),
12-
DEADMAN (345, 374, 378);
6+
F2P("F2P"),
7+
MEMBERS("Members");
138

14-
public final Integer[] worlds;
9+
private String name;
1510

16-
WorldType(final Integer... worlds) {
17-
this.worlds= worlds;
11+
WorldType(final String name) {
12+
this.name = name;
1813
}
1914

2015
@Override
2116
public String toString() {
22-
char[] name = name().toLowerCase().toCharArray();
23-
name[0] = Character.toUpperCase(name[0]);
24-
return new String(name);
17+
return name;
2518
}
2619
}

osbot_manager/src/gui/dialogues/input_dialog/ConfigurationDialog.java

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22

33
import bot_parameters.account.RunescapeAccount;
44
import bot_parameters.configuration.Configuration;
5+
import bot_parameters.configuration.World;
56
import bot_parameters.configuration.WorldType;
67
import bot_parameters.proxy.Proxy;
78
import bot_parameters.script.Script;
9+
import gui.dialogues.world_selector_dialog.WorldSelectorDialog;
810
import javafx.application.Platform;
911
import javafx.collections.FXCollections;
12+
import javafx.collections.ListChangeListener;
1013
import javafx.collections.ObservableList;
14+
import javafx.event.ActionEvent;
15+
import javafx.event.EventHandler;
1116
import javafx.scene.control.*;
1217
import javafx.scene.input.KeyCode;
1318
import javafx.scene.layout.FlowPane;
1419

20+
import java.util.ArrayList;
1521
import java.util.List;
22+
import java.util.Map;
1623

1724
public final class ConfigurationDialog extends InputDialog<Configuration> {
1825

@@ -30,9 +37,8 @@ public final class ConfigurationDialog extends InputDialog<Configuration> {
3037
private final CheckBox noRandoms;
3138
private final CheckBox noInterface;
3239
private final CheckBox noRender;
33-
private final ChoiceBox<WorldType> worldTypeSelector;
34-
private final CheckBox randomizeWorld;
35-
private final ChoiceBox<Integer> worldSelector;
40+
41+
private final WorldSelectorDialog worldSelectorDialog;
3642

3743
public ConfigurationDialog(ObservableList<RunescapeAccount> accountList, ObservableList<Script> scriptList, ObservableList<Proxy> proxyList) {
3844

@@ -71,16 +77,12 @@ public ConfigurationDialog(ObservableList<RunescapeAccount> accountList, Observa
7177
proxySelector = new ChoiceBox<>(proxyList);
7278
contentBox.getChildren().add(new FlowPane(10, 10, new Label("Proxy:"), proxySelector));
7379

74-
worldTypeSelector = new ChoiceBox<>(FXCollections.observableArrayList(WorldType.values()));
75-
worldSelector = new ChoiceBox<>(FXCollections.observableArrayList(WorldType.F2P.worlds));
76-
worldSelector.getSelectionModel().select(0);
77-
worldTypeSelector.getSelectionModel().selectedIndexProperty().addListener((observable, oldValue, newValue) -> {
78-
worldSelector.getItems().setAll(worldTypeSelector.getItems().get((Integer) newValue).worlds);
79-
worldSelector.getSelectionModel().select(0);
80+
final Button worldSelectorButton = new Button("World selector");
81+
worldSelectorDialog = new WorldSelectorDialog();
82+
worldSelectorButton.setOnAction(event -> {
83+
worldSelectorDialog.showAndWait();
8084
});
81-
randomizeWorld = new CheckBox("Randomize");
82-
contentBox.getChildren().add(new FlowPane(10, 10, new Label("World Type: "), worldTypeSelector, randomizeWorld, new Label("World: "), worldSelector));
83-
85+
contentBox.getChildren().add(new FlowPane(10, 10, worldSelectorButton));
8486

8587
memoryAllocation = new TextField();
8688
memoryAllocation.setPromptText("(Optional) Memory Allocation");
@@ -131,6 +133,10 @@ public ConfigurationDialog(ObservableList<RunescapeAccount> accountList, Observa
131133
selectedScripts.getItems().size() == 0);
132134
});
133135

136+
worldSelectorDialog.getSelectedWorlds().addListener((ListChangeListener<World>) c -> {
137+
okButton.setDisable(worldSelectorDialog.getSelectedWorlds().isEmpty());
138+
});
139+
134140
Platform.runLater(accountSelector::requestFocus);
135141
}
136142

@@ -148,9 +154,7 @@ public void setValues(final Configuration existingItem) {
148154
lowCpuMode.setSelected(false);
149155
enableReflection.setSelected(false);
150156
noRandoms.setSelected(false);
151-
worldTypeSelector.getSelectionModel().select(WorldType.F2P);
152-
worldSelector.getSelectionModel().select(0);
153-
randomizeWorld.setSelected(false);
157+
worldSelectorDialog.clearSelectedWorlds();
154158
noInterface.setSelected(false);
155159
noRender.setSelected(false);
156160
return;
@@ -166,9 +170,7 @@ public void setValues(final Configuration existingItem) {
166170
lowCpuMode.setSelected(existingItem.isLowCpuMode());
167171
enableReflection.setSelected(existingItem.isReflection());
168172
noRandoms.setSelected(existingItem.isNoRandoms());
169-
worldTypeSelector.getSelectionModel().select(existingItem.getWorldType());
170-
worldSelector.getSelectionModel().select(existingItem.getWorld());
171-
randomizeWorld.setSelected(existingItem.isRandomizeWorld());
173+
worldSelectorDialog.setSelectedWorlds(existingItem.getWorlds());
172174
noInterface.setSelected(existingItem.isNoInterface());
173175
noRender.setSelected(existingItem.isNoRender());
174176
okButton.setDisable(accountSelector.getSelectionModel().getSelectedItem() == null || selectedScripts.getItems().size() == 0);
@@ -192,9 +194,7 @@ protected final Configuration onAdd() {
192194
configuration.setLowResourceMode(lowResourceMode.isSelected());
193195
configuration.setReflection(enableReflection.isSelected());
194196
configuration.setNoRandoms(noRandoms.isSelected());
195-
configuration.setWorldType(worldTypeSelector.getValue());
196-
configuration.setWorld(worldSelector.getValue());
197-
configuration.setRandomizeWorld(randomizeWorld.isSelected());
197+
configuration.setWorlds(new ArrayList<>(worldSelectorDialog.getSelectedWorlds()));
198198
configuration.setNoInterface(noInterface.isSelected());
199199
configuration.setNoRender(noRender.isSelected());
200200

@@ -223,9 +223,7 @@ protected Configuration onEdit(final Configuration existingItem) {
223223
existingItem.setLowResourceMode(lowResourceMode.isSelected());
224224
existingItem.setReflection(enableReflection.isSelected());
225225
existingItem.setNoRandoms(noRandoms.isSelected());
226-
existingItem.setWorldType(worldTypeSelector.getValue());
227-
existingItem.setWorld(worldSelector.getValue());
228-
existingItem.setRandomizeWorld(randomizeWorld.isSelected());
226+
existingItem.setWorlds(new ArrayList<>(worldSelectorDialog.getSelectedWorlds()));
229227
existingItem.setNoInterface(noInterface.isSelected());
230228
existingItem.setNoRender(noRender.isSelected());
231229
return existingItem;

0 commit comments

Comments
 (0)