-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRandomEventAnalyticsLocalStorage.java
More file actions
executable file
·152 lines (132 loc) · 4.21 KB
/
RandomEventAnalyticsLocalStorage.java
File metadata and controls
executable file
·152 lines (132 loc) · 4.21 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
/*
* Copyright (c) 2018
* TheStonedTurtle <https://github.com/TheStonedTurtle>, zmanowar <https://github.com/zmanowar>
* All rights reserved.
*
* Modified source from https://github.com/TheStonedTurtle/Loot-Logger/
*/
package com.randomEventAnalytics.localstorage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.inject.Inject;
import lombok.Getter;
import static net.runelite.client.RuneLite.RUNELITE_DIR;
import net.runelite.http.api.RuneLiteAPI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* TODO: Either break the random events into seperate files
* or implement/import a DB system.
**/
public class RandomEventAnalyticsLocalStorage
{
private static final String FILE_EXTENSION = ".log";
private static final File RANDOM_EVENT_RECORD_DIR = new File(RUNELITE_DIR, "random-event-analytics");
private static final String RANDOM_EVENTS_FILE = "random-events";
private static final Logger log = LoggerFactory.getLogger(RandomEventAnalyticsLocalStorage.class);
private File playerFolder;
@Getter
private int numberOfLoggedEvents = 0;
@Getter
private String username;
@Inject
public RandomEventAnalyticsLocalStorage()
{
RANDOM_EVENT_RECORD_DIR.mkdir();
}
public boolean setPlayerUsername(final String username)
{
if (username.equalsIgnoreCase(this.username))
{
return false;
}
playerFolder = new File(RANDOM_EVENT_RECORD_DIR, username);
playerFolder.mkdir();
this.username = username;
return true;
}
private File getFile(String fileName)
{
return new File(playerFolder, fileName + FILE_EXTENSION);
}
public synchronized ArrayList<RandomEventRecord> loadRandomEventRecords()
{
final File file = getFile(RANDOM_EVENTS_FILE);
final ArrayList<RandomEventRecord> data = new ArrayList<>();
try (final BufferedReader br = new BufferedReader(new FileReader(file)))
{
String line;
while ((line = br.readLine()) != null)
{
// Skips the empty line at end of file
if (line.length() > 0)
{
final RandomEventRecord r = RuneLiteAPI.GSON.fromJson(line, RandomEventRecord.class);
data.add(r);
}
}
}
catch (FileNotFoundException e)
{
log.debug("File not found: {}", file.getName());
}
catch (IOException e)
{
log.warn("IOException for file {}: {}", file.getName(), e.getMessage());
}
numberOfLoggedEvents = data.size();
return data;
}
public synchronized RandomEventRecord getMostRecentRandom()
{
final ArrayList<RandomEventRecord> data = loadRandomEventRecords();
if (data.size() > 0)
{
return data.get(data.size() - 1);
}
return null;
}
public synchronized boolean renameUsernameFolderToAccountHash(final String username, final long hash)
{
final File usernameDir = new File(RANDOM_EVENT_RECORD_DIR, username);
if (!usernameDir.exists())
{
return true;
}
final File hashDir = new File(RANDOM_EVENT_RECORD_DIR, String.valueOf(hash));
if (hashDir.exists())
{
log.warn("Can't rename username folder to account hash as the folder for this account hash already exists" + "." + " This was most likely caused by running RL through the Jagex launcher before the migration code" + " was" + " added");
log.warn("Username: {} | AccountHash: {}", username, hash);
return false;
}
return usernameDir.renameTo(hashDir);
}
public synchronized boolean addRandomEventRecord(RandomEventRecord rec)
{
final File randomEventsFile = getFile(RANDOM_EVENTS_FILE);
// Convert entry to JSON
final String dataAsString = RuneLiteAPI.GSON.toJson(rec);
// Open File in append mode and write new data
try
{
final BufferedWriter file = new BufferedWriter(new FileWriter(String.valueOf(randomEventsFile), true));
file.append(dataAsString);
file.newLine();
file.close();
numberOfLoggedEvents += 1;
return true;
}
catch (IOException ioe)
{
log.warn("Error writing loot data to file {}: {}", randomEventsFile.getName(), ioe.getMessage());
return false;
}
}
}