Pole system removed.
Added support for edit messages (for now it just ignores them).
This commit is contained in:
parent
050b016143
commit
a2b32ae61c
5 changed files with 102 additions and 53 deletions
9
.idea/artifacts/vylionbot_jar.xml
Normal file
9
.idea/artifacts/vylionbot_jar.xml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<component name="ArtifactManager">
|
||||||
|
<artifact type="jar" name="vylionbot:jar">
|
||||||
|
<output-path>$PROJECT_DIR$/out/artifacts/vylionbot_jar</output-path>
|
||||||
|
<root id="archive" name="vylionbot.jar">
|
||||||
|
<element id="module-output" name="vylionbot" />
|
||||||
|
<element id="extracted-dir" path="$PROJECT_DIR$/libraries/unirest-java-1.4.10-SNAPSHOT-jar-with-dependencies.jar" path-in-jar="/" />
|
||||||
|
</root>
|
||||||
|
</artifact>
|
||||||
|
</component>
|
3
src/META-INF/MANIFEST.MF
Normal file
3
src/META-INF/MANIFEST.MF
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
Manifest-Version: 1.0
|
||||||
|
Main-Class: main.Main
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
package elements;
|
package elements;
|
||||||
|
|
||||||
import elements.exceptions.UserNotExistingException;
|
import elements.exceptions.PoleBlockedException;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
import java.time.ZoneId;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -10,19 +11,33 @@ import java.util.*;
|
||||||
*/
|
*/
|
||||||
public class Chat {
|
public class Chat {
|
||||||
private static final String DATE_TAG = "Date=";
|
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<String, Integer> users;
|
private Map<String, Integer> users;
|
||||||
private LocalDate lastPole;
|
private LocalDate lastPole;
|
||||||
|
private String poleWinner;
|
||||||
|
private String nextMode;
|
||||||
|
|
||||||
public Chat() {
|
public Chat() {
|
||||||
users = new HashMap<String, Integer>();
|
users = new HashMap<String, Integer>();
|
||||||
lastPole = null;
|
lastPole = null;
|
||||||
|
poleWinner = null;
|
||||||
|
nextMode = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean exists(String user) {
|
public boolean exists(String user) {
|
||||||
return users.containsKey(user);
|
return users.containsKey(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isModeChosen() {
|
||||||
|
return (nextMode != null);
|
||||||
|
}
|
||||||
|
|
||||||
public void add(String user) {
|
public void add(String user) {
|
||||||
users.put(user, 0);
|
users.put(user, 0);
|
||||||
}
|
}
|
||||||
|
@ -31,10 +46,22 @@ public class Chat {
|
||||||
users.put(user, p);
|
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(!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);
|
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() {
|
public boolean availablePole() {
|
||||||
|
@ -52,7 +79,7 @@ public class Chat {
|
||||||
top.sort(new UserComparator());
|
top.sort(new UserComparator());
|
||||||
|
|
||||||
for(int i = 0; i < top.size(); i++) {
|
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";
|
ranking += getPoles(top.get(i)) + "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,6 +90,11 @@ public class Chat {
|
||||||
String s = DATE_TAG;
|
String s = DATE_TAG;
|
||||||
if(lastPole == null) s += "null\n";
|
if(lastPole == null) s += "null\n";
|
||||||
else s += lastPole + "\n";
|
else s += lastPole + "\n";
|
||||||
|
|
||||||
|
s += WINNER_TAG;
|
||||||
|
if(poleWinner == null) s += "null\n";
|
||||||
|
else s += poleWinner + "\n";
|
||||||
|
|
||||||
Iterator it = users.entrySet().iterator();
|
Iterator it = users.entrySet().iterator();
|
||||||
|
|
||||||
while(it.hasNext()) {
|
while(it.hasNext()) {
|
||||||
|
@ -75,27 +107,33 @@ public class Chat {
|
||||||
|
|
||||||
public static Chat fromTxt(List<String> lines) {
|
public static Chat fromTxt(List<String> lines) {
|
||||||
Chat c = new Chat();
|
Chat c = new Chat();
|
||||||
|
int i = 0;
|
||||||
|
System.out.println("Reading " + lines.get(i));
|
||||||
|
|
||||||
if(lines.get(0).startsWith(DATE_TAG)) {
|
if(lines.get(i).startsWith(DATE_TAG)) {
|
||||||
String pole = lines.get(0).substring(DATE_TAG.length());
|
System.out.println("Reading " + lines.get(i));
|
||||||
|
String pole = lines.get(i).substring(DATE_TAG.length());
|
||||||
|
if(!pole.equals("null")) {
|
||||||
String[] reading = pole.split("-");
|
String[] reading = pole.split("-");
|
||||||
LocalDate poleDate = LocalDate.of(Integer.parseInt(reading[0]),
|
LocalDate poleDate = LocalDate.of(Integer.parseInt(reading[0]),
|
||||||
Integer.parseInt(reading[1]), Integer.parseInt(reading[2]));
|
Integer.parseInt(reading[1]), Integer.parseInt(reading[2]));
|
||||||
c.setPole(poleDate);
|
c.setPole(poleDate);
|
||||||
|
|
||||||
for(int i = 1; i < lines.size(); i++) {
|
|
||||||
reading = lines.get(i).split(":");
|
|
||||||
c.add(reading[0],Integer.parseInt(reading[1]));
|
|
||||||
}
|
}
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
if(lines.get(i).startsWith(WINNER_TAG)) {
|
||||||
for(int i = 0; i < lines.size(); i++) {
|
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(":");
|
String[] reading = lines.get(i).split(":");
|
||||||
c.add(reading[0],Integer.parseInt(reading[1]));
|
c.add(reading[0],Integer.parseInt(reading[1]));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
@ -104,6 +142,10 @@ public class Chat {
|
||||||
lastPole = d;
|
lastPole = d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setWinner(String poleWinner) {
|
||||||
|
this.poleWinner = poleWinner;
|
||||||
|
}
|
||||||
|
|
||||||
private String getUsername(Map.Entry<String, Integer> e) {
|
private String getUsername(Map.Entry<String, Integer> e) {
|
||||||
return e.getKey();
|
return e.getKey();
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,12 @@ package elements.exceptions;
|
||||||
public class PoleBlockedException extends Exception {
|
public class PoleBlockedException extends Exception {
|
||||||
|
|
||||||
public PoleBlockedException() {
|
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.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,8 @@ public class Vylbot {
|
||||||
private static final int COMMAND_HELP = COMMAND_START + 1;
|
private static final int COMMAND_HELP = COMMAND_START + 1;
|
||||||
private static final int COMMAND_RANKING = COMMAND_HELP + 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_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_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;
|
||||||
|
@ -51,6 +52,7 @@ public class Vylbot {
|
||||||
commands.add(COMMAND_HELP, "/help");
|
commands.add(COMMAND_HELP, "/help");
|
||||||
commands.add(COMMAND_RANKING, "/ranking");
|
commands.add(COMMAND_RANKING, "/ranking");
|
||||||
commands.add(COMMAND_TALIBOT_STATS, "!pole stats");
|
commands.add(COMMAND_TALIBOT_STATS, "!pole stats");
|
||||||
|
commands.add(COMMAND_POLE, "/pole");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUsername() {
|
public String getUsername() {
|
||||||
|
@ -143,6 +145,7 @@ public class Vylbot {
|
||||||
else last_upd_id = responses.getJSONObject(responses.length() - 1).getInt("update_id") + 1;
|
else last_upd_id = responses.getJSONObject(responses.length() - 1).getInt("update_id") + 1;
|
||||||
|
|
||||||
for (int i = 0; i < responses.length(); i++) {
|
for (int i = 0; i < responses.length(); i++) {
|
||||||
|
if(responses.getJSONObject(i).has("message")) {
|
||||||
JSONObject message = responses.getJSONObject(i).getJSONObject("message");
|
JSONObject message = responses.getJSONObject(i).getJSONObject("message");
|
||||||
|
|
||||||
processMessage(message);
|
processMessage(message);
|
||||||
|
@ -150,6 +153,7 @@ public class Vylbot {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void processMessage(JSONObject message) throws UnirestException {
|
private void processMessage(JSONObject message) throws UnirestException {
|
||||||
//Get chat id, the most important thing
|
//Get chat id, the most important thing
|
||||||
|
@ -183,7 +187,7 @@ public class Vylbot {
|
||||||
System.out.println("\nMessage received from user @" + user +
|
System.out.println("\nMessage received from user @" + user +
|
||||||
" (chat id: " + chat_id + ")");
|
" (chat id: " + chat_id + ")");
|
||||||
else
|
else
|
||||||
System.out.println("\nMessage received from user " + name +
|
System.out.println("\nMessage received from user named " + name +
|
||||||
" (chat id: " + chat_id + ")");
|
" (chat id: " + chat_id + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,7 +231,9 @@ public class Vylbot {
|
||||||
|
|
||||||
case COMMAND_TALIBOT_STATS:
|
case COMMAND_TALIBOT_STATS:
|
||||||
case COMMAND_RANKING:
|
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;
|
return;
|
||||||
|
|
||||||
default: return;
|
default: return;
|
||||||
|
@ -241,45 +247,26 @@ public class Vylbot {
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case COMMAND_HELP:
|
case COMMAND_HELP:
|
||||||
sendMessage(chat_id, getInformation(COMMAND_HELP));
|
sendMessage(chat_id, getInformation(COMMAND_HELP), PARSE_MARKDOWN);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
case COMMAND_TALIBOT_STATS:
|
||||||
|
case COMMAND_RANKING:
|
||||||
|
sendMessage(chat_id, getInformation(WARNING_NO_GROUP));
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleText(long chat_id, boolean group, String user, String text) throws UnirestException {
|
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) {
|
private boolean exists(long chat_id, String user) {
|
||||||
return chats.containsKey(chat_id) && chats.get(chat_id).exists(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 {
|
private void saveChat(long chat_id) throws SaveErrorException {
|
||||||
try {
|
try {
|
||||||
Persistence.getInstance().saveChat(chat_id, chats.get(chat_id));
|
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" +
|
return "Aquí tienes una lista de comandos que acepto:\n\n" +
|
||||||
"/start - Repite el saludo inicial\n" +
|
"/start - Repite el saludo inicial\n" +
|
||||||
"/help - Saca esta lista\n" +
|
"/help - Saca esta lista\n" +
|
||||||
"/pole - Para hacer la pole diaria\n" +
|
"/ranking - Muestra la lista de poles conseguidas." +
|
||||||
"/ranking - Muestra la lista de poles conseguidas.\n" +
|
"\n\n*Golsario:*\n" +
|
||||||
"\nEa.";
|
"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:
|
case WARNING_UNKNOWN_ERROR:
|
||||||
return "Algo ha ido MUY mal.";
|
return "Algo ha ido MUY mal.";
|
||||||
|
|
Reference in a new issue