Добавлена команда кек4
This commit is contained in:
parent
7b5154ece8
commit
e8b0acfcde
@ -13,7 +13,7 @@ import org.json.JSONObject;
|
||||
* @author HoldFast
|
||||
*/
|
||||
public class MainThread implements Runnable {
|
||||
|
||||
|
||||
private static final String[] MESSAGES = {
|
||||
"Ору с вас!", "Проигрываю", "Славка, го осу", "Олег, рисуй!", "Эд, го кодить",
|
||||
"Листочек, потри мне сосочек", "Пойду вскроюсь", "Не за надпись"
|
||||
@ -21,7 +21,7 @@ public class MainThread implements Runnable {
|
||||
|
||||
private static final int ACCOUNT_ID = 258220262;
|
||||
public static boolean run = false;
|
||||
|
||||
|
||||
private final Command[] commands;
|
||||
private int lastChatId = 2;
|
||||
|
||||
@ -46,15 +46,16 @@ public class MainThread implements Runnable {
|
||||
new When(),
|
||||
new Shakal(),
|
||||
new ImageEffect(ImageEffect.Type.KEK),
|
||||
new ImageEffect(ImageEffect.Type.KEK4),
|
||||
new ImageEffect(ImageEffect.Type.DECOLOR),
|
||||
new ImageEffect(ImageEffect.Type.NEGATIVE),
|
||||
new ImageEffect(ImageEffect.Type.SOLARIZE),
|
||||
new ImageEffect(ImageEffect.Type.EFFECT),
|
||||
|
||||
|
||||
new AnnimonResponse()
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
@ -62,7 +63,7 @@ public class MainThread implements Runnable {
|
||||
int pts = VK.getLastPTS();
|
||||
while (run) {
|
||||
Thread.sleep(5000);
|
||||
|
||||
|
||||
String obj = VK.getUnread(pts);
|
||||
if (obj.isEmpty()) {
|
||||
countEmptyUnread++;
|
||||
@ -72,13 +73,13 @@ public class MainThread implements Runnable {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
JSONObject response = new JSONObject(obj).getJSONObject("response");
|
||||
pts = response.getInt("new_pts");
|
||||
JSONArray jsonMessages = response.getJSONObject("messages").getJSONArray("items");
|
||||
JSONArray jsonProfiles = response.getJSONArray("profiles");
|
||||
final int messagesLength = jsonMessages.length();
|
||||
if (messagesLength == 0 && Util.random(80) == 5) {
|
||||
if (false && messagesLength == 0 && Util.random(80) == 5) {
|
||||
String message;
|
||||
if (Util.random(6) == 2) {
|
||||
message = MESSAGES[Util.random(MESSAGES.length)];
|
||||
@ -93,7 +94,7 @@ public class MainThread implements Runnable {
|
||||
final JSONObject profile = jsonProfiles.getJSONObject(i);
|
||||
profiles.put(profile.getInt("id"), profile);
|
||||
}
|
||||
|
||||
|
||||
for (int i = 0; i < messagesLength; i++) {
|
||||
final JSONObject currentMessage = jsonMessages.getJSONObject(i);
|
||||
final int userId = currentMessage.getInt("user_id");
|
||||
@ -113,14 +114,14 @@ public class MainThread implements Runnable {
|
||||
String message = currentMessage.getString("body");
|
||||
final int chatId = lastChatId = currentMessage.optInt("chat_id", currentMessage.getInt("user_id"));
|
||||
final String userName = profile.getString("first_name");
|
||||
|
||||
|
||||
Matcher match = Pattern.compile("^("+Config.BOT_NAMES + "),? ?(.+)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE).matcher(message);
|
||||
if (!match.matches()) return;
|
||||
|
||||
message = match.group(2).trim();
|
||||
final String[] words = message.split("\\s+");
|
||||
if (words.length == 0) return;
|
||||
|
||||
|
||||
final String cmd = words[0].toLowerCase();
|
||||
for (Command command : commands) {
|
||||
if (!command.match(cmd)) continue;
|
||||
|
@ -2,14 +2,11 @@ package holdfast.samobot.commands;
|
||||
|
||||
import holdfast.samobot.IOUtil;
|
||||
import holdfast.samobot.Util;
|
||||
import holdfast.samobot.VK;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.image.AffineTransformOp;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -19,11 +16,12 @@ public class ImageEffect extends PhotoAttachmentCommand {
|
||||
|
||||
public enum Type {
|
||||
KEK(new String[] { "кек" }),
|
||||
KEK4(new String[] { "кек4" }),
|
||||
DECOLOR(new String[] { "обесцвечивание", "ч/б", "чб", "decolor" }),
|
||||
NEGATIVE(new String[] { "негатив", "negative" }),
|
||||
SOLARIZE(new String[] { "передержка", "solarize" }),
|
||||
EFFECT(new String[] { "эффект", "effect" });
|
||||
|
||||
|
||||
private final String[] command;
|
||||
|
||||
Type(String[] command) {
|
||||
@ -34,13 +32,13 @@ public class ImageEffect extends PhotoAttachmentCommand {
|
||||
return command;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private final Type type;
|
||||
|
||||
public ImageEffect(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected String[] command() {
|
||||
return type.getCommand();
|
||||
@ -56,7 +54,7 @@ public class ImageEffect extends PhotoAttachmentCommand {
|
||||
final int index = Util.random(Mode.values().length);
|
||||
result = processEffect(image, Mode.values()[index]);
|
||||
break;
|
||||
|
||||
|
||||
case DECOLOR:
|
||||
result = processEffect(image, Mode.DECOLOR);
|
||||
break;
|
||||
@ -66,30 +64,37 @@ public class ImageEffect extends PhotoAttachmentCommand {
|
||||
case SOLARIZE:
|
||||
result = processEffect(image, Mode.SOLARIZE);
|
||||
break;
|
||||
|
||||
|
||||
case KEK4:
|
||||
send(userName, String.format("%s,%s,%s,%s",
|
||||
upload(processKek(image, 0)),
|
||||
upload(processKek(image, 1)),
|
||||
upload(processKek(image, 2)),
|
||||
upload(processKek(image, 3))
|
||||
));
|
||||
return true;
|
||||
|
||||
case KEK:
|
||||
default:
|
||||
result = processKek(image);
|
||||
}
|
||||
|
||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ImageIO.write(result, "jpg", baos);
|
||||
baos.flush();
|
||||
final String photo = VK.uploadPhoto(baos.toByteArray(), "jpg");
|
||||
baos.close();
|
||||
|
||||
send(userName, photo);
|
||||
|
||||
send(userName, upload(result));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private BufferedImage processKek(BufferedImage image) {
|
||||
return processKek(image, Util.random(4));
|
||||
}
|
||||
|
||||
private BufferedImage processKek(BufferedImage image, int mode) {
|
||||
final int width = image.getWidth();
|
||||
final int height = image.getHeight();
|
||||
final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
final Graphics2D g2d = result.createGraphics();
|
||||
g2d.drawImage(image, 0, 0, null);
|
||||
|
||||
switch (Util.random(4)) {
|
||||
|
||||
switch (mode) {
|
||||
case 0: {
|
||||
// copy top part to bottom
|
||||
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
|
||||
@ -100,7 +105,7 @@ public class ImageEffect extends PhotoAttachmentCommand {
|
||||
0, 0, width, height / 2,
|
||||
null);
|
||||
} break;
|
||||
|
||||
|
||||
case 1: {
|
||||
// copy bottom part to top
|
||||
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
|
||||
@ -111,8 +116,8 @@ public class ImageEffect extends PhotoAttachmentCommand {
|
||||
0, height / 2, width, height,
|
||||
null);
|
||||
} break;
|
||||
|
||||
|
||||
|
||||
|
||||
case 2: {
|
||||
// copy right part to left
|
||||
AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
|
||||
@ -123,7 +128,7 @@ public class ImageEffect extends PhotoAttachmentCommand {
|
||||
0, 0, width / 2, height,
|
||||
null);
|
||||
} break;
|
||||
|
||||
|
||||
case 3:
|
||||
default: {
|
||||
// copy left part to right
|
||||
@ -136,25 +141,25 @@ public class ImageEffect extends PhotoAttachmentCommand {
|
||||
null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
g2d.dispose();
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
private BufferedImage processEffect(BufferedImage image, Mode mode) {
|
||||
final int width = image.getWidth();
|
||||
final int height = image.getHeight();
|
||||
final int length = width * height;
|
||||
final int[] argb = new int[length];
|
||||
image.getRGB(0, 0, width, height, argb, 0, width);
|
||||
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
final int pixel = argb[i];
|
||||
int qr = (pixel >> 16) & 0xff;
|
||||
int qg = (pixel >> 8) & 0xff;
|
||||
int qb = pixel & 0xff;
|
||||
|
||||
|
||||
switch(mode) {
|
||||
case DECOLOR:// обесцвеч
|
||||
qr = (qr + qg + qb) / 3;
|
||||
@ -185,15 +190,15 @@ public class ImageEffect extends PhotoAttachmentCommand {
|
||||
qb = (qb > 127) ? (2 * (qb - 128)) : (2 * (127 - qb));
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
argb[i] = (argb[i] & 0xFF000000) | (qr << 16) | (qg << 8) | qb;
|
||||
}
|
||||
|
||||
|
||||
final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
result.setRGB(0, 0, width, height, argb, 0, width);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
private enum Mode {
|
||||
DECOLOR, NEGATIVE, SEPIA, DRUGS, SOLARIZE;
|
||||
}
|
||||
|
@ -1,6 +1,10 @@
|
||||
package holdfast.samobot.commands;
|
||||
|
||||
import holdfast.samobot.VK;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import javax.imageio.ImageIO;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
@ -9,7 +13,7 @@ import org.json.JSONObject;
|
||||
* @author aNNiMON
|
||||
*/
|
||||
public abstract class PhotoAttachmentCommand extends Command {
|
||||
|
||||
|
||||
@Override
|
||||
public boolean execute(String message, String userName) throws IOException {
|
||||
final String photoUrl = getPhotoAttachmentUrl();
|
||||
@ -17,19 +21,19 @@ public abstract class PhotoAttachmentCommand extends Command {
|
||||
send(userName + ", прикрепите изображение.");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
return execute(message, userName, photoUrl);
|
||||
}
|
||||
|
||||
private String getPhotoAttachmentUrl() throws IOException {
|
||||
if (!currentMessage.has("attachments")) return null;
|
||||
|
||||
|
||||
final JSONArray attachments = currentMessage.getJSONArray("attachments");
|
||||
if (attachments.length() == 0) return null;
|
||||
|
||||
|
||||
final JSONObject attachment = attachments.getJSONObject(0);
|
||||
if (!attachment.getString("type").equalsIgnoreCase("photo")) return null;
|
||||
|
||||
|
||||
final JSONObject photoAttachment = attachment.getJSONObject("photo");
|
||||
final int[] sizes = {2560, 1280, 807, 604, 130, 75};
|
||||
for (int size : sizes) {
|
||||
@ -42,4 +46,13 @@ public abstract class PhotoAttachmentCommand extends Command {
|
||||
}
|
||||
|
||||
protected abstract boolean execute(String message, String userName, String photoUrl) throws IOException;
|
||||
|
||||
protected String upload(BufferedImage image) throws IOException {
|
||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ImageIO.write(image, "jpg", baos);
|
||||
baos.flush();
|
||||
final String photo = VK.uploadPhoto(baos.toByteArray(), "jpg");
|
||||
baos.close();
|
||||
return photo;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user