Новый тип юнитов - таймеры

Таймер срабатывает не сразу а через какое-то время, заставляет сработать любой другой тип юнита
This commit is contained in:
Mark Struchkov 2019-02-03 20:33:54 +03:00
parent 3027f7d5cf
commit ccdc44d689
8 changed files with 72 additions and 60 deletions

View File

@ -51,4 +51,16 @@ public class VkApi {
} }
return temp != null ? temp.get(0).getUniversities().get(0).getName() : null; return temp != null ? temp.get(0).getUniversities().get(0).getName() : null;
} }
public String getUserCity(Integer id) {
List<UserXtrCounters> temp = null;
try {
temp = vk.users().get(actor).userIds(String.valueOf(id)).fields(UserField.CITY).execute();
} catch (ApiException | ClientException e) {
e.printStackTrace();
}
return temp != null ? temp.get(0).getCity().getTitle() : null;
}
} }

View File

@ -1,29 +1,38 @@
package org.sadtech.vkbot.core; package org.sadtech.vkbot.core;
import org.sadtech.vkbot.core.entity.MailSend;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class VkInsertData { public class VkInsertData {
private VkApi vkApi; private VkApi vkApi;
private Integer userId;
public VkInsertData(VkConnect vkConnect) { public VkInsertData(VkConnect vkConnect) {
this.vkApi = new VkApi(vkConnect); this.vkApi = new VkApi(vkConnect);
} }
public Integer getUserId() { public String insertWords(MailSend mailSend, Integer idUser) {
return userId; Pattern pattern = Pattern.compile("%(\\w+)%");
} Matcher m = pattern.matcher(mailSend.getMessage());
StringBuffer result = new StringBuffer();
public void setUserId(Integer userId) { while (m.find()) {
this.userId = userId; m.appendReplacement(result, insert(m.group(0), idUser));
}
public String insert(String key) {
String string = "";
if (key.toLowerCase().equals("%firstname%")) {
string = vkApi.getUserMini(userId).getFirstName();
} }
if (key.toLowerCase().equals("%lastname%")) { m.appendTail(result);
string = vkApi.getUserMini(userId).getLastName(); return result.toString();
}
private String insert(String key, Integer userId) {
String string = "";
switch (key) {
case "%firstname%":
string = vkApi.getUserMini(userId).getFirstName();
break;
case "%lastname%":
string = vkApi.getUserMini(userId).getLastName();
break;
} }
return string; return string;
} }

View File

@ -4,7 +4,6 @@ public class MailSend {
private String message; private String message;
private String keyboard; private String keyboard;
private Integer idRecipient;
private Float lat; private Float lat;
private Float aLong; private Float aLong;
private Integer stickerId; private Integer stickerId;
@ -29,14 +28,6 @@ public class MailSend {
this.message = message; this.message = message;
} }
public Integer getIdRecipient() {
return idRecipient;
}
public void setIdRecipient(Integer idRecipient) {
this.idRecipient = idRecipient;
}
public Float getLat() { public Float getLat() {
return lat; return lat;
} }
@ -66,7 +57,6 @@ public class MailSend {
return "MailSend{" + return "MailSend{" +
"message='" + message + '\'' + "message='" + message + '\'' +
", keyboard='" + keyboard + '\'' + ", keyboard='" + keyboard + '\'' +
", idRecipient=" + idRecipient +
", lat=" + lat + ", lat=" + lat +
", aLong=" + aLong + ", aLong=" + aLong +
", stickerId=" + stickerId + ", stickerId=" + stickerId +

View File

@ -1,7 +0,0 @@
package org.sadtech.vkbot.core.keyboard;
public interface ButtonKeyBoardService {
}

View File

@ -9,6 +9,7 @@ public class KeyBoardService {
ButtonKeyBoard noButton = ButtonKeyBoard.builder().setColor(ColorButton.NEGATIVE).setLabel("Нет").setPayload("{\"button\": \"no\"}").build(); ButtonKeyBoard noButton = ButtonKeyBoard.builder().setColor(ColorButton.NEGATIVE).setLabel("Нет").setPayload("{\"button\": \"no\"}").build();
LineKeyBoard lineKeyBoard = LineKeyBoard.builder().setButtonKeyBoard(yesButton).setButtonKeyBoard(noButton).build(); LineKeyBoard lineKeyBoard = LineKeyBoard.builder().setButtonKeyBoard(yesButton).setButtonKeyBoard(noButton).build();
KeyBoard keyBoard = KeyBoard.builder().setLineKeyBoard(lineKeyBoard).build(); KeyBoard keyBoard = KeyBoard.builder().setLineKeyBoard(lineKeyBoard).build();
keyBoard.setOneTime(true);
return keyBoard; return keyBoard;
} }
@ -18,6 +19,7 @@ public class KeyBoardService {
ButtonKeyBoard buttonKeyBoard = ButtonKeyBoard.builder().setLabel(labelButton).setType("text").setPayload("{\"button\": \"" + labelButton + "\"}").build(); ButtonKeyBoard buttonKeyBoard = ButtonKeyBoard.builder().setLabel(labelButton).setType("text").setPayload("{\"button\": \"" + labelButton + "\"}").build();
keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build()); keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build());
} }
keyBoard.setOneTime(true);
return keyBoard; return keyBoard;
} }
@ -26,6 +28,7 @@ public class KeyBoardService {
for (ButtonKeyBoard buttonKeyBoard : buttonKeyBoards) { for (ButtonKeyBoard buttonKeyBoard : buttonKeyBoards) {
keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build()); keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build());
} }
keyBoard.setOneTime(true);
return keyBoard; return keyBoard;
} }
} }

View File

@ -1,4 +0,0 @@
package org.sadtech.vkbot.core.keyboard;
public interface LineKeyBoardService {
}

View File

@ -9,9 +9,9 @@ import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.VkConnect; import org.sadtech.vkbot.core.VkConnect;
import org.sadtech.vkbot.core.VkInsertData; import org.sadtech.vkbot.core.VkInsertData;
import org.sadtech.vkbot.core.entity.MailSend; import org.sadtech.vkbot.core.entity.MailSend;
import org.sadtech.vkbot.core.insert.InsertWords;
import java.util.regex.Matcher; import java.util.List;
import java.util.regex.Pattern;
public class MailSanderVk implements MailSandler { public class MailSanderVk implements MailSandler {
@ -28,12 +28,8 @@ public class MailSanderVk implements MailSandler {
this.vkInsertData = new VkInsertData(vkConnect); this.vkInsertData = new VkInsertData(vkConnect);
} }
@Override private MessagesSendQuery createMessage (MailSend mailSend, Integer idUser) {
public void send(MailSend mailSend) { MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(idUser);
MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(mailSend.getIdRecipient());
if (mailSend.getMessage() != null) {
messages.message(insertWords(mailSend));
}
if (mailSend.getKeyboard() != null) { if (mailSend.getKeyboard() != null) {
messages.keyboard(mailSend.getKeyboard()); messages.keyboard(mailSend.getKeyboard());
} else { } else {
@ -44,30 +40,39 @@ public class MailSanderVk implements MailSandler {
} }
if (mailSend.getStickerId() != null) { if (mailSend.getStickerId() != null) {
try { try {
vkApiClient.messages().send(groupActor).peerId(mailSend.getIdRecipient()).stickerId(mailSend.getStickerId()).execute(); vkApiClient.messages().send(groupActor).peerId(idUser).stickerId(mailSend.getStickerId()).execute();
} catch (ApiException | ClientException e) { } catch (ApiException | ClientException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
return messages;
}
@Override
public void send(MailSend mailSend, Integer idUser) {
MessagesSendQuery messages = createMessage(mailSend, idUser);
if (mailSend.getMessage() != null) {
messages.message(vkInsertData.insertWords(mailSend, idUser));
}
sendMessage(messages);
}
public void send(MailSend mailSend, Integer idUser, List<String> insertWords) {
MessagesSendQuery messages = createMessage(mailSend, idUser);
if (mailSend.getMessage() != null) {
InsertWords insert = new InsertWords();
insert.setInText(vkInsertData.insertWords(mailSend, idUser));
insert.insert(insertWords);
messages.message(insert.getOutText());
}
sendMessage(messages);
}
private void sendMessage(MessagesSendQuery messages) {
try { try {
log.info(mailSend);
messages.execute(); messages.execute();
} catch (ApiException | ClientException e) { } catch (ApiException | ClientException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private String insertWords(MailSend mailSend) {
vkInsertData.setUserId(mailSend.getIdRecipient());
Pattern pattern = Pattern.compile("\\%(\\w+)%");
Matcher m = pattern.matcher(mailSend.getMessage());
StringBuffer result = new StringBuffer();
while (m.find()) {
m.appendReplacement(result, vkInsertData.insert(m.group(0)));
}
m.appendTail(result);
return result.toString();
}
} }

View File

@ -2,8 +2,12 @@ package org.sadtech.vkbot.core.sender;
import org.sadtech.vkbot.core.entity.MailSend; import org.sadtech.vkbot.core.entity.MailSend;
import java.util.List;
public interface MailSandler { public interface MailSandler {
void send(MailSend mailSend); void send(MailSend mailSend, Integer idUser);
void send(MailSend mailSend, Integer idUser, List<String> insertWords);
} }