-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShipHold.java
84 lines (67 loc) · 2.72 KB
/
ShipHold.java
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
import java.util.*;
import java.io.*;
// TODO: Make iteratable
public class ShipHold {
private Map<String, Integer> cargo;
public ShipHold(String logPath, Date startTime) throws IOException {
// Go over log and catch up to the hold's current state.
// Assume it starts empty.
cargo = new HashMap<String, Integer>();
FileReader fr = new FileReader(logPath);
BufferedReader logReader = new BufferedReader(fr);
// Go line by line, checking event type.
// Perform operations to map based on type.
String currentLine;
while ((currentLine = logReader.readLine()) != null) {
// TODO: Assumes English, some log entries have unlocalised types, some don't.
// Find the event type.
String eventType = readValue("event", currentLine);
if (eventType.equals("MiningRefined")) {
// Ore names on this entry are captialized, for some reason.
editCargo(readValue("Type_Localised", currentLine).toLowerCase(), 1);
} else if (eventType.equals("EjectCargo") || eventType.equals("MarketSell")) {
editCargo(readValue("Type", currentLine), -readInt("Count", currentLine));
} else if (eventType.equals("Died")) {
cargo = new HashMap<String, Integer>();
}
}
System.out.println(cargo);
logReader.close();
}
// Returns the text between the first encountered pair of double quotation marks.
// Used for reading names of minerals/event types.
// Warning: Pretty stupid about where in the structure the keyword is found, could be in a chat message or something.
// TODO: Make actual json/csv readers.
public static String readValue(String key, String line) {
int startIndex = line.indexOf("\"" + key + "\"") + key.length() + 3;
String readText = "";
// Move to the first character after a "
for (; line.charAt(startIndex) != '"'; startIndex++);
startIndex++;
for (; line.charAt(startIndex) != '"'; startIndex++) {
readText = readText + line.charAt(startIndex);
}
return readText;
}
public static int readInt(String key, String line) {
int startIndex = line.indexOf("\"" + key + "\"") + key.length() + 3;
String readText = "";
for (; line.charAt(startIndex) != ','; startIndex++) {
readText = readText + line.charAt(startIndex);
}
return Integer.parseInt(readText);
}
// Changes the amount of itemName in the hold by count.
// Exception if a negative amount of an item occurs.
private void editCargo (String itemName, int countChange) {
// Error checks
//if ((!cargo.containsKey(itemName) && countChange < 0) || (cargo.get(itemName) + countChange < 0)) {
// throw new IllegalArgumentException();
//}
if (!cargo.containsKey(itemName)) {
cargo.put(itemName, countChange);
} else {
cargo.put(itemName, cargo.get(itemName) + countChange);
}
}
}