diff --git a/.idea/artifacts/vylionbot_jar.xml b/.idea/artifacts/vylionbot_jar.xml
new file mode 100644
index 0000000..8f6adca
--- /dev/null
+++ b/.idea/artifacts/vylionbot_jar.xml
@@ -0,0 +1,9 @@
+
+
+ $PROJECT_DIR$/out/artifacts/vylionbot_jar
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..6948c82
--- /dev/null
+++ b/src/META-INF/MANIFEST.MF
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Main-Class: main.Main
+
diff --git a/src/elements/Chat.java b/src/elements/Chat.java
index 2cc2636..e284e35 100644
--- a/src/elements/Chat.java
+++ b/src/elements/Chat.java
@@ -1,8 +1,9 @@
package elements;
-import elements.exceptions.UserNotExistingException;
+import elements.exceptions.PoleBlockedException;
import java.time.LocalDate;
+import java.time.ZoneId;
import java.util.*;
/**
@@ -10,19 +11,33 @@ import java.util.*;
*/
public class Chat {
private static final String DATE_TAG = "Date=";
+ private static final String WINNER_TAG = "Winner=";
+
+ public static final String MODE_NORMAL = "mode_normal";
+ public static final String MODE_RANDOM_HOUR = "mode_random_hour";
+ public static final String MODE_COLORS = "mode_colors";
+ public static final String MODE_FAR_WEST = "mode_far_west";
private Map users;
private LocalDate lastPole;
+ private String poleWinner;
+ private String nextMode;
public Chat() {
users = new HashMap();
lastPole = null;
+ poleWinner = null;
+ nextMode = null;
}
public boolean exists(String user) {
return users.containsKey(user);
}
+ public boolean isModeChosen() {
+ return (nextMode != null);
+ }
+
public void add(String user) {
users.put(user, 0);
}
@@ -31,10 +46,22 @@ public class Chat {
users.put(user, p);
}
- public void winPole(String user) {
+ public void winPole(String user, int date) throws PoleBlockedException {
if(!users.containsKey(user)) add(user);
+ if(!availablePole()) {
+ if(poleWinner != null) throw new PoleBlockedException(poleWinner);
+ else throw new PoleBlockedException();
+ }
users.replace(user, users.get(user)+1);
- lastPole = LocalDate.now();
+
+ Date d = new Date((new Long(date))*1000);
+ lastPole = d.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
+
+ poleWinner = user;
+ }
+
+ public void choseMode(String mode) {
+ nextMode = mode;
}
public boolean availablePole() {
@@ -52,7 +79,7 @@ public class Chat {
top.sort(new UserComparator());
for(int i = 0; i < top.size(); i++) {
- ranking += "[" + (i+1) + ".] @" + getUsername(top.get(i)) + ": ";
+ ranking += "[" + (i+1) + ".] " + getUsername(top.get(i)) + ": ";
ranking += getPoles(top.get(i)) + "\n";
}
@@ -63,6 +90,11 @@ public class Chat {
String s = DATE_TAG;
if(lastPole == null) s += "null\n";
else s += lastPole + "\n";
+
+ s += WINNER_TAG;
+ if(poleWinner == null) s += "null\n";
+ else s += poleWinner + "\n";
+
Iterator it = users.entrySet().iterator();
while(it.hasNext()) {
@@ -75,27 +107,33 @@ public class Chat {
public static Chat fromTxt(List lines) {
Chat c = new Chat();
+ int i = 0;
+ System.out.println("Reading " + lines.get(i));
- 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]));
+ if(lines.get(i).startsWith(DATE_TAG)) {
+ System.out.println("Reading " + lines.get(i));
+ String pole = lines.get(i).substring(DATE_TAG.length());
+ if(!pole.equals("null")) {
+ String[] reading = pole.split("-");
+ LocalDate poleDate = LocalDate.of(Integer.parseInt(reading[0]),
+ Integer.parseInt(reading[1]), Integer.parseInt(reading[2]));
+ c.setPole(poleDate);
}
+ i++;
}
- else {
- for(int i = 0; i < lines.size(); i++) {
- String[] reading = lines.get(i).split(":");
- c.add(reading[0],Integer.parseInt(reading[1]));
- }
+ if(lines.get(i).startsWith(WINNER_TAG)) {
+ System.out.println("Reading " + lines.get(i));
+ String winner = lines.get(i).substring(WINNER_TAG.length());
+ c.setWinner(winner);
+ i++;
}
+ for(; i < lines.size(); i++) {
+ System.out.println("Reading " + lines.get(i));
+ String[] reading = lines.get(i).split(":");
+ c.add(reading[0],Integer.parseInt(reading[1]));
+ }
return c;
}
@@ -104,6 +142,10 @@ public class Chat {
lastPole = d;
}
+ private void setWinner(String poleWinner) {
+ this.poleWinner = poleWinner;
+ }
+
private String getUsername(Map.Entry e) {
return e.getKey();
}
diff --git a/src/elements/exceptions/PoleBlockedException.java b/src/elements/exceptions/PoleBlockedException.java
index db889ec..2521fd2 100644
--- a/src/elements/exceptions/PoleBlockedException.java
+++ b/src/elements/exceptions/PoleBlockedException.java
@@ -6,6 +6,12 @@ package elements.exceptions;
public class PoleBlockedException extends Exception {
public PoleBlockedException() {
- super("La pole de hoy ya ha sido conseguida. Vuélvelo a intentar mañana.");
+ super("Today's daily pole position has already been won. " +
+ "Try again tomorrow.");
+ }
+
+ public PoleBlockedException(String user) {
+ super("Today's daily pole position has already been won by " + user +". " +
+ "Try again tomorrow.");
}
}
diff --git a/src/main/Vylbot.java b/src/main/Vylbot.java
index 685ab39..dcc3e11 100644
--- a/src/main/Vylbot.java
+++ b/src/main/Vylbot.java
@@ -30,7 +30,8 @@ public class Vylbot {
private static final int COMMAND_HELP = COMMAND_START + 1;
private static final int COMMAND_RANKING = COMMAND_HELP + 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 COMMAND_POLE = COMMAND_TALIBOT_STATS + 1;
+ private static final int COMMAND_COUNT_END = COMMAND_POLE + 1;
private static final int WARNING_COUNT_START = COMMAND_COUNT_END;
private static final int WARNING_UNKNOWN_ERROR = WARNING_COUNT_START;
@@ -51,6 +52,7 @@ public class Vylbot {
commands.add(COMMAND_HELP, "/help");
commands.add(COMMAND_RANKING, "/ranking");
commands.add(COMMAND_TALIBOT_STATS, "!pole stats");
+ commands.add(COMMAND_POLE, "/pole");
}
public String getUsername() {
@@ -143,9 +145,11 @@ public class Vylbot {
else last_upd_id = responses.getJSONObject(responses.length() - 1).getInt("update_id") + 1;
for (int i = 0; i < responses.length(); i++) {
- JSONObject message = responses.getJSONObject(i).getJSONObject("message");
+ if(responses.getJSONObject(i).has("message")) {
+ JSONObject message = responses.getJSONObject(i).getJSONObject("message");
- processMessage(message);
+ processMessage(message);
+ }
}
}
}
@@ -183,7 +187,7 @@ public class Vylbot {
System.out.println("\nMessage received from user @" + user +
" (chat id: " + chat_id + ")");
else
- System.out.println("\nMessage received from user " + name +
+ System.out.println("\nMessage received from user named " + name +
" (chat id: " + chat_id + ")");
}
@@ -227,7 +231,9 @@ public class Vylbot {
case COMMAND_TALIBOT_STATS:
case COMMAND_RANKING:
- sendMessage(chat_id, chats.get(chat_id).getRanking());
+ if(!chats.containsKey(chat_id)) sendMessage(chat_id, "Nadie ha conseguido ninguna pole " +
+ "en este grupo aún.");
+ else sendMessage(chat_id, chats.get(chat_id).getRanking());
return;
default: return;
@@ -241,45 +247,26 @@ public class Vylbot {
return;
case COMMAND_HELP:
- sendMessage(chat_id, getInformation(COMMAND_HELP));
+ sendMessage(chat_id, getInformation(COMMAND_HELP), PARSE_MARKDOWN);
return;
+ case COMMAND_TALIBOT_STATS:
+ case COMMAND_RANKING:
+ sendMessage(chat_id, getInformation(WARNING_NO_GROUP));
+
default:
return;
}
}
private void handleText(long chat_id, boolean group, String user, String text) throws UnirestException {
- if(text.toLowerCase().contains("pole")) {
- if(group) {
- if (user == null) sendMessage(chat_id, getInformation(WARNING_NO_USERNAME));
- else try {
- sendMessage(chat_id, winPole(chat_id, user));
- } catch (PoleBlockedException e) {
- return;
- }
- }
- else sendMessage(chat_id, getInformation(WARNING_NO_GROUP));
- }
+
}
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));
@@ -298,9 +285,11 @@ public class Vylbot {
return "Aquí tienes una lista de comandos que acepto:\n\n" +
"/start - Repite el saludo inicial\n" +
"/help - Saca esta lista\n" +
- "/pole - Para hacer la pole diaria\n" +
- "/ranking - Muestra la lista de poles conseguidas.\n" +
- "\nEa.";
+ "/ranking - Muestra la lista de poles conseguidas." +
+ "\n\n*Golsario:*\n" +
+ "Poles: sólo se puede conseguir una por chat al día. " +
+ "Que gane el mejor." +
+ "\n\n_Vylbot v1.0.4_";
case WARNING_UNKNOWN_ERROR:
return "Algo ha ido MUY mal.";