Created a chat name index.

Fixed list name parsing.
This commit is contained in:
vylion 2016-10-09 19:25:24 +02:00
parent ef2ea921cc
commit b41d1c4b4c
3 changed files with 61 additions and 21 deletions

View file

@ -31,6 +31,10 @@ public class Chat {
name = n; name = n;
} }
public String getName() {
return name;
}
public boolean exists(String list) { public boolean exists(String list) {
return lists.containsKey(list); return lists.containsKey(list);
} }
@ -83,15 +87,6 @@ public class Chat {
Iterator it = lists.entrySet().iterator(); Iterator it = lists.entrySet().iterator();
if(name != null) {
lines = new ArrayList<String>();
lines.add("#Chat name:");
lines.add("#" + name);
listDocs.add(lines);
}
while(it.hasNext()) { while(it.hasNext()) {
lines = new ArrayList<String>(); lines = new ArrayList<String>();
@ -99,6 +94,11 @@ public class Chat {
lines.add("#List name:"); lines.add("#List name:");
lines.add("#" + entry.getKey()); lines.add("#" + entry.getKey());
if(name != null) {
lines.add("#Chat name:");
lines.add("#" + name);
}
ArrayList<ChatListItem> list = entry.getValue(); ArrayList<ChatListItem> list = entry.getValue();
for(int i = 0; i < list.size(); i++) { for(int i = 0; i < list.size(); i++) {

View file

@ -30,19 +30,41 @@ public class Persistence {
} }
public void saveChatNames(List<String> lines) throws SaveErrorChatException {
try {
Path file;
file = Paths.get("geiserFiles/chat_names.txt");
if (!Files.exists(file.getParent()))
Files.createDirectories(file.getParent());
Files.write(file, lines, Charset.forName("UTF-8"));
} catch (IOException e) {
throw new SaveErrorChatException(e);
}
}
public void saveChat(long chat_id, Chat c) throws SaveErrorChatException { public void saveChat(long chat_id, Chat c) throws SaveErrorChatException {
try { try {
List<String> lines = new ArrayList<String>();
Path file;
//Save chat stats
lines.add("#Chat name:");
lines.add("#" + c.getName());
file = Paths.get("geiserFiles/" + chat_id + "/chat_names.txt");
if (!Files.exists(file.getParent()))
Files.createDirectories(file.getParent());
Files.write(file, lines, Charset.forName("UTF-8"));
//Save chat lists
ArrayList<List<String>> lists = c.saveLists(); ArrayList<List<String>> lists = c.saveLists();
List<String> lines;
for(int i = 0; i < lists.size(); i++) { for(int i = 0; i < lists.size(); i++) {
lines = lists.get(i); lines = lists.get(i);
String filename = lines.get(1).substring("#".length()); String filename = lines.get(1).substring("#".length());
if(lines.get(0).equals("#Chat name:")) { file = Paths.get("geiserFiles/" + chat_id + "/lists/" + filename + ".txt");
filename = "chat_name";
}
Path file = Paths.get("geiserFiles/" + chat_id + "/lists/" + filename + ".txt");
if (!Files.exists(file.getParent())) if (!Files.exists(file.getParent()))
Files.createDirectories(file.getParent()); Files.createDirectories(file.getParent());
Files.write(file, lines, Charset.forName("UTF-8")); Files.write(file, lines, Charset.forName("UTF-8"));
@ -50,8 +72,6 @@ public class Persistence {
} catch (IOException e) { } catch (IOException e) {
throw new SaveErrorChatException(e); throw new SaveErrorChatException(e);
} }
return;
} }
/* /*

View file

@ -318,7 +318,7 @@ public class Geiserbot {
"Debes poner un espacio entre el comando /list y el nombre de la lista."); "Debes poner un espacio entre el comando /list y el nombre de la lista.");
return; return;
} }
String list = reading[0]; String list = reading[1];
if(!exists(m.getCid())) if(!exists(m.getCid()))
chats.put(m.getCid(), new Chat(m.getChatName())); chats.put(m.getCid(), new Chat(m.getChatName()));
@ -335,16 +335,23 @@ public class Geiserbot {
"Debes poner un espacio entre el comando /list y el nombre de la lista."); "Debes poner un espacio entre el comando /list y el nombre de la lista.");
return; return;
} }
String list = reading[0]; String list = reading[1];
if(!exists(m.getCid())) if(!exists(m.getCid())) {
chats.put(m.getCid(), new Chat(m.getChatName())); try {
chats.put(m.getCid(), new Chat(m.getChatName()));
saveChatNames();
} catch (SaveErrorChatException e) {
sendMessage(m.getCid(), "Ha habido un error guardando los datos de este chat.");
return;
}
}
if(!exists(m.getCid(), list)) { if(!exists(m.getCid(), list)) {
try { try {
chats.get(m.getCid()).add(list); chats.get(m.getCid()).add(list);
saveChat(m.getCid()); saveChat(m.getCid());
} catch (SaveErrorChatException e) { } catch (SaveErrorChatException e) {
sendMessage(m.getCid(), "Ha habido un error guardando la lista."); sendMessage(m.getCid(), "Ha habido un error creando la lista.");
return; return;
} }
} }
@ -364,6 +371,19 @@ public class Geiserbot {
Persistence.getInstance().saveChat(chat_id, chats.get(chat_id)); Persistence.getInstance().saveChat(chat_id, chats.get(chat_id));
} }
private void saveChatNames() throws SaveErrorChatException {
ArrayList<String> lines = new ArrayList<String>();
Iterator<Map.Entry<Long, Chat>> it = chats.entrySet().iterator();
while(it.hasNext()) {
Map.Entry<Long, Chat> entry = it.next();
lines.add(entry.getKey() + " = " + entry.getValue().getName());
}
Persistence.getInstance().saveChatNames(lines);
}
private String getWarning(int warning_id) { private String getWarning(int warning_id) {
switch (warning_id) { switch (warning_id) {
case WARNING_UNKNOWN_ERROR: case WARNING_UNKNOWN_ERROR: