Pole system finished.

This commit is contained in:
Vylion 2016-04-21 22:34:36 +02:00
parent 915dc94955
commit 050b016143
9 changed files with 350 additions and 51 deletions

View file

@ -58,7 +58,7 @@
<ConfirmationsSetting value="0" id="Add" /> <ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" /> <ConfirmationsSetting value="0" id="Remove" />
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" default="false" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

123
src/elements/Chat.java Normal file
View file

@ -0,0 +1,123 @@
package elements;
import elements.exceptions.UserNotExistingException;
import java.time.LocalDate;
import java.util.*;
/**
* Created by vylion on 4/21/16.
*/
public class Chat {
private static final String DATE_TAG = "Date=";
private Map<String, Integer> users;
private LocalDate lastPole;
public Chat() {
users = new HashMap<String, Integer>();
lastPole = null;
}
public boolean exists(String user) {
return users.containsKey(user);
}
public void add(String user) {
users.put(user, 0);
}
public void add(String user, int p) {
users.put(user, p);
}
public void winPole(String user) {
if(!users.containsKey(user)) add(user);
users.replace(user, users.get(user)+1);
lastPole = LocalDate.now();
}
public boolean availablePole() {
LocalDate today = LocalDate.now();
return (lastPole == null || today.isAfter(lastPole));
}
public String getRanking() {
List<Map.Entry<String, Integer>> top;
String ranking = "";
Map.Entry<String, Integer>[] ts = new Map.Entry[users.size()];
top = Arrays.asList((users.entrySet().toArray(ts)));
top.sort(new UserComparator());
for(int i = 0; i < top.size(); i++) {
ranking += "[" + (i+1) + ".] @" + getUsername(top.get(i)) + ": ";
ranking += getPoles(top.get(i)) + "\n";
}
return ranking;
}
public String toTxt() {
String s = DATE_TAG;
if(lastPole == null) s += "null\n";
else s += lastPole + "\n";
Iterator it = users.entrySet().iterator();
while(it.hasNext()) {
Map.Entry<String, Integer> entry = (Map.Entry<String, Integer>) it.next();
s += getUsername(entry) + ":" + getPoles(entry) + "\n";
}
return s;
}
public static Chat fromTxt(List<String> lines) {
Chat c = new Chat();
if(lines.get(0).startsWith(DATE_TAG)) {
String pole = lines.get(0).substring(DATE_TAG.length());
String[] reading = pole.split("-");
LocalDate poleDate = LocalDate.of(Integer.parseInt(reading[0]),
Integer.parseInt(reading[1]), Integer.parseInt(reading[2]));
c.setPole(poleDate);
for(int i = 1; i < lines.size(); i++) {
reading = lines.get(i).split(":");
c.add(reading[0],Integer.parseInt(reading[1]));
}
}
else {
for(int i = 0; i < lines.size(); i++) {
String[] reading = lines.get(i).split(":");
c.add(reading[0],Integer.parseInt(reading[1]));
}
}
return c;
}
private void setPole(LocalDate d) {
lastPole = d;
}
private String getUsername(Map.Entry<String, Integer> e) {
return e.getKey();
}
private int getPoles(Map.Entry<String, Integer> e) {
return e.getValue();
}
}
class UserComparator implements Comparator<Map.Entry> {
@Override
public int compare(Map.Entry entr1, Map.Entry entr2) {
return ((Integer) entr2.getValue()).compareTo((Integer) entr1.getValue());
}
}

View file

@ -0,0 +1,76 @@
package elements;
import elements.exceptions.ReadErrorException;
import elements.exceptions.SaveErrorException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
/**
* Created by vylion on 4/14/16.
*/
public class Persistence {
private static Persistence persistence;
private Persistence() {
}
public static Persistence getInstance() {
if(persistence == null) persistence = new Persistence();
return persistence;
}
public void saveChat(long chat_id, Chat c) throws SaveErrorException {
try {
List<String> lines = Arrays.asList(c.toTxt().split("\n"));
Path file = Paths.get("chats/" + chat_id + ".txt");
if (!Files.exists(file.getParent()))
Files.createDirectories(file.getParent());
Files.write(file, lines, Charset.forName("UTF-8"));
} catch (IOException e) {
throw new SaveErrorException(chat_id);
}
return;
}
public HashMap<Long, Chat> readChats() throws ReadErrorException {
HashMap<Long, Chat> chats = new HashMap<Long, Chat>();
if(!Files.exists(Paths.get("chats/"))) return chats;
try {
String filename;
Chat chat;
List<Path> files = Files.walk(Paths.get("chats/"), 1).collect(Collectors.toList());
Path file;
List<String> lines;
for(int i = 0; i < files.size(); i++) {
file = files.get(i);
filename = file.getFileName().toString();
if(filename.endsWith(".txt")) {
filename = filename.substring(0,filename.length() - 4);
System.out.println("Reading " + filename);
lines = Files.readAllLines(file);
chats.put(Long.parseLong(filename), Chat.fromTxt(lines));
}
}
} catch (IOException e) {
e.printStackTrace();
throw new ReadErrorException();
}
return chats;
}
}

View file

@ -0,0 +1,11 @@
package elements.exceptions;
/**
* Created by vylion on 4/21/16.
*/
public class PoleBlockedException extends Exception {
public PoleBlockedException() {
super("La pole de hoy ya ha sido conseguida. Vuélvelo a intentar mañana.");
}
}

View file

@ -0,0 +1,26 @@
package elements.exceptions;
import java.io.IOException;
/**
* Created by vylion on 4/15/16.
*/
public class ReadErrorException extends IOException {
public ReadErrorException() {
super("Ha ocurrido un error al leer los datos. " +
"Contacta con mi creador o algo. io k se xdxd");
}
public ReadErrorException(long chat_id) {
super("Ha ocurrido un error al leer los datos del chat " + chat_id +
". Contacta con mi creador o algo. io k se xdxd");
}
@Override
public String getLocalizedMessage() {
return "Couldn't read file. You're on your own on this one.";
}
public String getLocalizedMessage(long chat_id) {
return "Couldn't read file " + chat_id + ". You're on your own on this one.";
}
}

View file

@ -0,0 +1,26 @@
package elements.exceptions;
import java.io.IOException;
/**
* Created by vylion on 4/15/16.
*/
public class SaveErrorException extends IOException {
public SaveErrorException() {
super("Ha ocurrido un error al guardar los datos. Contacta con mi creador o algo. io k se xdxd");
}
public SaveErrorException(long chat_id) {
super("Ha ocurrido un error al guardar los datos del chat " + chat_id +
". Contacta con mi creador o algo. io k se xdxd");
}
@Override
public String getLocalizedMessage() {
return "Couldn't save to file. You're on your own on this one.";
}
public String getLocalizedMessage(long chat_id) {
return "Couldn't save " + chat_id + " to file. You're on your own on this one.";
}
}

View file

@ -0,0 +1,11 @@
package elements.exceptions;
/**
* Created by vylion on 4/21/16.
*/
public class UserNotExistingException extends Exception {
public UserNotExistingException() {
super("No tengo constancia de ninguna persona con ese nombre de usuario.");
}
}

View file

@ -1,6 +1,7 @@
package main; package main;
import com.mashape.unirest.http.exceptions.UnirestException; import com.mashape.unirest.http.exceptions.UnirestException;
import elements.exceptions.ReadErrorException;
/** /**
* Created by vylion on 4/1/16. * Created by vylion on 4/1/16.
@ -16,6 +17,9 @@ public class Main {
} catch (UnirestException e) { } catch (UnirestException e) {
System.out.println("Unirest Error catched."); System.out.println("Unirest Error catched.");
e.printStackTrace(); e.printStackTrace();
} catch (ReadErrorException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} }
} }
} }

View file

@ -4,6 +4,11 @@ import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode; import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest; import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException; import com.mashape.unirest.http.exceptions.UnirestException;
import elements.Chat;
import elements.Persistence;
import elements.exceptions.PoleBlockedException;
import elements.exceptions.ReadErrorException;
import elements.exceptions.SaveErrorException;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
@ -23,8 +28,9 @@ public class Vylbot {
private static final int COMMAND_COUNT_START = 0; private static final int COMMAND_COUNT_START = 0;
private static final int COMMAND_START = COMMAND_COUNT_START; private static final int COMMAND_START = COMMAND_COUNT_START;
private static final int COMMAND_HELP = COMMAND_START + 1; private static final int COMMAND_HELP = COMMAND_START + 1;
private static final int COMMAND_POLE = COMMAND_HELP + 1; private static final int COMMAND_RANKING = COMMAND_HELP + 1;
private static final int COMMAND_COUNT_END = COMMAND_POLE + 1; private static final int COMMAND_TALIBOT_STATS = COMMAND_RANKING + 1;
private static final int COMMAND_COUNT_END = COMMAND_TALIBOT_STATS + 1;
private static final int WARNING_COUNT_START = COMMAND_COUNT_END; private static final int WARNING_COUNT_START = COMMAND_COUNT_END;
private static final int WARNING_UNKNOWN_ERROR = WARNING_COUNT_START; private static final int WARNING_UNKNOWN_ERROR = WARNING_COUNT_START;
@ -34,13 +40,17 @@ public class Vylbot {
private static final int WARNING_NO_GROUP = WARNING_NOT_IMPLEMENTED + 1; private static final int WARNING_NO_GROUP = WARNING_NOT_IMPLEMENTED + 1;
private static final int WARNING_COUNT_END = WARNING_NO_GROUP + 1; private static final int WARNING_COUNT_END = WARNING_NO_GROUP + 1;
private ArrayList<String> commands; private List<String> commands;
private Map<Long, Chat> chats;
public Vylbot() { public Vylbot() throws ReadErrorException {
chats = Persistence.getInstance().readChats();
commands = new ArrayList<>();
commands.add(COMMAND_START, "/start"); commands.add(COMMAND_START, "/start");
commands.add(COMMAND_HELP, "/help"); commands.add(COMMAND_HELP, "/help");
commands.add(COMMAND_POLE, "/pole"); commands.add(COMMAND_RANKING, "/ranking");
commands.add(COMMAND_TALIBOT_STATS, "!pole stats");
} }
public String getUsername() { public String getUsername() {
@ -181,10 +191,9 @@ public class Vylbot {
if (message.has("text")) { if (message.has("text")) {
String text = message.getString("text"); String text = message.getString("text");
if (text.startsWith("/")) { if (text.startsWith("/") || text.startsWith("!")) handleInput(chat_id, chatType.contains("group"), user, text);
if (user == null) sendMessage(chat_id, getInformation(WARNING_NO_USERNAME)); else handleText(chat_id, chatType.contains("group"), user, text);
else handleInput(chat_id, chatType.contains("group"), user, text);
}
return; return;
} }
} }
@ -216,11 +225,11 @@ public class Vylbot {
sendMessage(chat_id, getInformation(COMMAND_HELP), PARSE_MARKDOWN); sendMessage(chat_id, getInformation(COMMAND_HELP), PARSE_MARKDOWN);
return; return;
case COMMAND_POLE: case COMMAND_TALIBOT_STATS:
sendMessage(chat_id, getInformation(WARNING_NOT_IMPLEMENTED)); case COMMAND_RANKING:
sendMessage(chat_id, chats.get(chat_id).getRanking());
return; return;
default: return; default: return;
} }
} }
@ -236,26 +245,49 @@ public class Vylbot {
return; return;
default: default:
sendMessage(chat_id, getInformation(WARNING_NO_GROUP));
return; return;
} }
} }
private void handleText(long chat_id, boolean group, String user, String text) throws UnirestException {
/*private boolean exists(long chat_id, String user) { if(text.toLowerCase().contains("pole")) {
return chats.containsKey(chat_id) && chats.get(chat_id).exists(user); if(group) {
}*/ if (user == null) sendMessage(chat_id, getInformation(WARNING_NO_USERNAME));
else try {
private String getPole(long chat_id, String user) { sendMessage(chat_id, winPole(chat_id, user));
} catch (PoleBlockedException e) {
return "Congratulations " + user + ", you got the daily pole position! " + return;
"You win 200 pepes. Try again in 24 hours."; }
}
else sendMessage(chat_id, getInformation(WARNING_NO_GROUP));
}
} }
/*private void saveChat(long chat_id) throws SaveErrorException { private boolean exists(long chat_id, String user) {
return chats.containsKey(chat_id) && chats.get(chat_id).exists(user);
}
private String winPole(long chat_id, String user) throws PoleBlockedException {
if(!chats.containsKey(chat_id)) chats.put(chat_id, new Chat());
if(!chats.get(chat_id).availablePole()) throw new PoleBlockedException();
try {
chats.get(chat_id).winPole(user);
saveChat(chat_id);
return "El usuario @" + user + " ha hecho la pole!";
} catch (SaveErrorException e) {
return e.getMessage();
}
}
private void saveChat(long chat_id) throws SaveErrorException {
try {
Persistence.getInstance().saveChat(chat_id, chats.get(chat_id)); Persistence.getInstance().saveChat(chat_id, chats.get(chat_id));
chats.get(chat_id).setDirty(false); } catch (SaveErrorException e) {
}*/ e.printStackTrace();
throw e;
}
}
private String getInformation(int command) { private String getInformation(int command) {
switch (command) { switch (command) {
@ -263,28 +295,12 @@ public class Vylbot {
return "Hola. Soy el bot personal de Vylion."; return "Hola. Soy el bot personal de Vylion.";
case COMMAND_HELP: case COMMAND_HELP:
return "Here is a list of the commands I accept:\n\n" + return "Aquí tienes una lista de comandos que acepto:\n\n" +
"/register - Open a new vault in this chat under your name\n" + "/start - Repite el saludo inicial\n" +
"/newbet - Start the creation of a new bet. _*This will lock the " + "/help - Saca esta lista\n" +
"bot in a bet creation until finished*_\n" + "/pole - Para hacer la pole diaria\n" +
"/done - While on a bet creation, it finishes it and asks you to " + "/ranking - Muestra la lista de poles conseguidas.\n" +
"designate the bet moderator. _*Only the bet creator can use " + "\nEa.";
"this*_\n" +
"/checkbet - Shows the current state of the bet.\n" +
"/closebet - If there's a current active bet, it finishes it " +
"and let's you choose the outcome. _*Only the bet moderator " +
"can use this*_\n" +
"/cancelbet - If something went wrong, it let's you cancel a bet. " +
"_*Only the bet creator (during creation) or the bet moderator " +
"(if creation has finished) can use this*_\n" +
"/me - Tells your vault's contents.\n" +
"/give [username] [amount] - Let's you altruistically give money " +
"to another user.\n" +
"/ranking - Shows the top 5 richest players.\n" +
"\nRemember: this bot is geared towards group chats and you won't " +
"be able to use me in a one-on-one chat.\n" +
"Also, if I die during the creation of a bet, you can expect me " +
"not to remember it.";
case WARNING_UNKNOWN_ERROR: case WARNING_UNKNOWN_ERROR:
return "Algo ha ido MUY mal."; return "Algo ha ido MUY mal.";
@ -299,6 +315,9 @@ public class Vylbot {
return "No tienes nombre de usuario. Regístrate con uno en la " + return "No tienes nombre de usuario. Regístrate con uno en la " +
"configuración de Telegram para poder participar."; "configuración de Telegram para poder participar.";
case WARNING_POLE_BLOCKED:
return "La pole de hoy ya ha sido conseguida. Vuélvelo a intentar mañana.";
default: return "Entrada incorrecta."; default: return "Entrada incorrecta.";
} }
} }
@ -311,8 +330,11 @@ public class Vylbot {
case COMMAND_HELP: case COMMAND_HELP:
return commands.get(COMMAND_HELP); return commands.get(COMMAND_HELP);
case COMMAND_POLE: case COMMAND_RANKING:
return commands.get(COMMAND_POLE); return commands.get(COMMAND_RANKING);
case COMMAND_TALIBOT_STATS:
return commands.get(COMMAND_TALIBOT_STATS);
default: return "Entrada incorrecta."; default: return "Entrada incorrecta.";
} }