Добавлена команда кек4

This commit is contained in:
Victor 2016-04-29 16:53:36 +03:00
parent 7b5154ece8
commit e8b0acfcde
3 changed files with 65 additions and 46 deletions

View File

@ -13,7 +13,7 @@ import org.json.JSONObject;
* @author HoldFast * @author HoldFast
*/ */
public class MainThread implements Runnable { public class MainThread implements Runnable {
private static final String[] MESSAGES = { private static final String[] MESSAGES = {
"Ору с вас!", "Проигрываю", "Славка, го осу", "Олег, рисуй!", "Эд, го кодить", "Ору с вас!", "Проигрываю", "Славка, го осу", "Олег, рисуй!", "Эд, го кодить",
"Листочек, потри мне сосочек", "Пойду вскроюсь", "Не за надпись" "Листочек, потри мне сосочек", "Пойду вскроюсь", "Не за надпись"
@ -21,7 +21,7 @@ public class MainThread implements Runnable {
private static final int ACCOUNT_ID = 258220262; private static final int ACCOUNT_ID = 258220262;
public static boolean run = false; public static boolean run = false;
private final Command[] commands; private final Command[] commands;
private int lastChatId = 2; private int lastChatId = 2;
@ -46,15 +46,16 @@ public class MainThread implements Runnable {
new When(), new When(),
new Shakal(), new Shakal(),
new ImageEffect(ImageEffect.Type.KEK), new ImageEffect(ImageEffect.Type.KEK),
new ImageEffect(ImageEffect.Type.KEK4),
new ImageEffect(ImageEffect.Type.DECOLOR), new ImageEffect(ImageEffect.Type.DECOLOR),
new ImageEffect(ImageEffect.Type.NEGATIVE), new ImageEffect(ImageEffect.Type.NEGATIVE),
new ImageEffect(ImageEffect.Type.SOLARIZE), new ImageEffect(ImageEffect.Type.SOLARIZE),
new ImageEffect(ImageEffect.Type.EFFECT), new ImageEffect(ImageEffect.Type.EFFECT),
new AnnimonResponse() new AnnimonResponse()
}; };
} }
@Override @Override
public void run() { public void run() {
try { try {
@ -62,7 +63,7 @@ public class MainThread implements Runnable {
int pts = VK.getLastPTS(); int pts = VK.getLastPTS();
while (run) { while (run) {
Thread.sleep(5000); Thread.sleep(5000);
String obj = VK.getUnread(pts); String obj = VK.getUnread(pts);
if (obj.isEmpty()) { if (obj.isEmpty()) {
countEmptyUnread++; countEmptyUnread++;
@ -72,13 +73,13 @@ public class MainThread implements Runnable {
} }
continue; continue;
} }
JSONObject response = new JSONObject(obj).getJSONObject("response"); JSONObject response = new JSONObject(obj).getJSONObject("response");
pts = response.getInt("new_pts"); pts = response.getInt("new_pts");
JSONArray jsonMessages = response.getJSONObject("messages").getJSONArray("items"); JSONArray jsonMessages = response.getJSONObject("messages").getJSONArray("items");
JSONArray jsonProfiles = response.getJSONArray("profiles"); JSONArray jsonProfiles = response.getJSONArray("profiles");
final int messagesLength = jsonMessages.length(); final int messagesLength = jsonMessages.length();
if (messagesLength == 0 && Util.random(80) == 5) { if (false && messagesLength == 0 && Util.random(80) == 5) {
String message; String message;
if (Util.random(6) == 2) { if (Util.random(6) == 2) {
message = MESSAGES[Util.random(MESSAGES.length)]; message = MESSAGES[Util.random(MESSAGES.length)];
@ -93,7 +94,7 @@ public class MainThread implements Runnable {
final JSONObject profile = jsonProfiles.getJSONObject(i); final JSONObject profile = jsonProfiles.getJSONObject(i);
profiles.put(profile.getInt("id"), profile); profiles.put(profile.getInt("id"), profile);
} }
for (int i = 0; i < messagesLength; i++) { for (int i = 0; i < messagesLength; i++) {
final JSONObject currentMessage = jsonMessages.getJSONObject(i); final JSONObject currentMessage = jsonMessages.getJSONObject(i);
final int userId = currentMessage.getInt("user_id"); final int userId = currentMessage.getInt("user_id");
@ -113,14 +114,14 @@ public class MainThread implements Runnable {
String message = currentMessage.getString("body"); String message = currentMessage.getString("body");
final int chatId = lastChatId = currentMessage.optInt("chat_id", currentMessage.getInt("user_id")); final int chatId = lastChatId = currentMessage.optInt("chat_id", currentMessage.getInt("user_id"));
final String userName = profile.getString("first_name"); final String userName = profile.getString("first_name");
Matcher match = Pattern.compile("^("+Config.BOT_NAMES + "),? ?(.+)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE).matcher(message); Matcher match = Pattern.compile("^("+Config.BOT_NAMES + "),? ?(.+)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE).matcher(message);
if (!match.matches()) return; if (!match.matches()) return;
message = match.group(2).trim(); message = match.group(2).trim();
final String[] words = message.split("\\s+"); final String[] words = message.split("\\s+");
if (words.length == 0) return; if (words.length == 0) return;
final String cmd = words[0].toLowerCase(); final String cmd = words[0].toLowerCase();
for (Command command : commands) { for (Command command : commands) {
if (!command.match(cmd)) continue; if (!command.match(cmd)) continue;

View File

@ -2,14 +2,11 @@ package holdfast.samobot.commands;
import holdfast.samobot.IOUtil; import holdfast.samobot.IOUtil;
import holdfast.samobot.Util; import holdfast.samobot.Util;
import holdfast.samobot.VK;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.geom.AffineTransform; import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp; import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import javax.imageio.ImageIO;
/** /**
* *
@ -19,11 +16,12 @@ public class ImageEffect extends PhotoAttachmentCommand {
public enum Type { public enum Type {
KEK(new String[] { "кек" }), KEK(new String[] { "кек" }),
KEK4(new String[] { "кек4" }),
DECOLOR(new String[] { "обесцвечивание", "ч/б", "чб", "decolor" }), DECOLOR(new String[] { "обесцвечивание", "ч/б", "чб", "decolor" }),
NEGATIVE(new String[] { "негатив", "negative" }), NEGATIVE(new String[] { "негатив", "negative" }),
SOLARIZE(new String[] { "передержка", "solarize" }), SOLARIZE(new String[] { "передержка", "solarize" }),
EFFECT(new String[] { "эффект", "effect" }); EFFECT(new String[] { "эффект", "effect" });
private final String[] command; private final String[] command;
Type(String[] command) { Type(String[] command) {
@ -34,13 +32,13 @@ public class ImageEffect extends PhotoAttachmentCommand {
return command; return command;
} }
} }
private final Type type; private final Type type;
public ImageEffect(Type type) { public ImageEffect(Type type) {
this.type = type; this.type = type;
} }
@Override @Override
protected String[] command() { protected String[] command() {
return type.getCommand(); return type.getCommand();
@ -56,7 +54,7 @@ public class ImageEffect extends PhotoAttachmentCommand {
final int index = Util.random(Mode.values().length); final int index = Util.random(Mode.values().length);
result = processEffect(image, Mode.values()[index]); result = processEffect(image, Mode.values()[index]);
break; break;
case DECOLOR: case DECOLOR:
result = processEffect(image, Mode.DECOLOR); result = processEffect(image, Mode.DECOLOR);
break; break;
@ -66,30 +64,37 @@ public class ImageEffect extends PhotoAttachmentCommand {
case SOLARIZE: case SOLARIZE:
result = processEffect(image, Mode.SOLARIZE); result = processEffect(image, Mode.SOLARIZE);
break; 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: case KEK:
default: default:
result = processKek(image); result = processKek(image);
} }
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); send(userName, upload(result));
ImageIO.write(result, "jpg", baos);
baos.flush();
final String photo = VK.uploadPhoto(baos.toByteArray(), "jpg");
baos.close();
send(userName, photo);
return true; return true;
} }
private BufferedImage processKek(BufferedImage image) { 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 width = image.getWidth();
final int height = image.getHeight(); final int height = image.getHeight();
final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
final Graphics2D g2d = result.createGraphics(); final Graphics2D g2d = result.createGraphics();
g2d.drawImage(image, 0, 0, null); g2d.drawImage(image, 0, 0, null);
switch (Util.random(4)) { switch (mode) {
case 0: { case 0: {
// copy top part to bottom // copy top part to bottom
AffineTransform tx = AffineTransform.getScaleInstance(1, -1); AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
@ -100,7 +105,7 @@ public class ImageEffect extends PhotoAttachmentCommand {
0, 0, width, height / 2, 0, 0, width, height / 2,
null); null);
} break; } break;
case 1: { case 1: {
// copy bottom part to top // copy bottom part to top
AffineTransform tx = AffineTransform.getScaleInstance(1, -1); AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
@ -111,8 +116,8 @@ public class ImageEffect extends PhotoAttachmentCommand {
0, height / 2, width, height, 0, height / 2, width, height,
null); null);
} break; } break;
case 2: { case 2: {
// copy right part to left // copy right part to left
AffineTransform tx = AffineTransform.getScaleInstance(-1, 1); AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
@ -123,7 +128,7 @@ public class ImageEffect extends PhotoAttachmentCommand {
0, 0, width / 2, height, 0, 0, width / 2, height,
null); null);
} break; } break;
case 3: case 3:
default: { default: {
// copy left part to right // copy left part to right
@ -136,25 +141,25 @@ public class ImageEffect extends PhotoAttachmentCommand {
null); null);
} }
} }
g2d.dispose(); g2d.dispose();
return result; return result;
} }
private BufferedImage processEffect(BufferedImage image, Mode mode) { private BufferedImage processEffect(BufferedImage image, Mode mode) {
final int width = image.getWidth(); final int width = image.getWidth();
final int height = image.getHeight(); final int height = image.getHeight();
final int length = width * height; final int length = width * height;
final int[] argb = new int[length]; final int[] argb = new int[length];
image.getRGB(0, 0, width, height, argb, 0, width); image.getRGB(0, 0, width, height, argb, 0, width);
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
final int pixel = argb[i]; final int pixel = argb[i];
int qr = (pixel >> 16) & 0xff; int qr = (pixel >> 16) & 0xff;
int qg = (pixel >> 8) & 0xff; int qg = (pixel >> 8) & 0xff;
int qb = pixel & 0xff; int qb = pixel & 0xff;
switch(mode) { switch(mode) {
case DECOLOR:// обесцвеч case DECOLOR:// обесцвеч
qr = (qr + qg + qb) / 3; qr = (qr + qg + qb) / 3;
@ -185,15 +190,15 @@ public class ImageEffect extends PhotoAttachmentCommand {
qb = (qb > 127) ? (2 * (qb - 128)) : (2 * (127 - qb)); qb = (qb > 127) ? (2 * (qb - 128)) : (2 * (127 - qb));
break; break;
} }
argb[i] = (argb[i] & 0xFF000000) | (qr << 16) | (qg << 8) | qb; argb[i] = (argb[i] & 0xFF000000) | (qr << 16) | (qg << 8) | qb;
} }
final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
result.setRGB(0, 0, width, height, argb, 0, width); result.setRGB(0, 0, width, height, argb, 0, width);
return result; return result;
} }
private enum Mode { private enum Mode {
DECOLOR, NEGATIVE, SEPIA, DRUGS, SOLARIZE; DECOLOR, NEGATIVE, SEPIA, DRUGS, SOLARIZE;
} }

View File

@ -1,6 +1,10 @@
package holdfast.samobot.commands; package holdfast.samobot.commands;
import holdfast.samobot.VK;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import javax.imageio.ImageIO;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
@ -9,7 +13,7 @@ import org.json.JSONObject;
* @author aNNiMON * @author aNNiMON
*/ */
public abstract class PhotoAttachmentCommand extends Command { public abstract class PhotoAttachmentCommand extends Command {
@Override @Override
public boolean execute(String message, String userName) throws IOException { public boolean execute(String message, String userName) throws IOException {
final String photoUrl = getPhotoAttachmentUrl(); final String photoUrl = getPhotoAttachmentUrl();
@ -17,19 +21,19 @@ public abstract class PhotoAttachmentCommand extends Command {
send(userName + ", прикрепите изображение."); send(userName + ", прикрепите изображение.");
return true; return true;
} }
return execute(message, userName, photoUrl); return execute(message, userName, photoUrl);
} }
private String getPhotoAttachmentUrl() throws IOException { private String getPhotoAttachmentUrl() throws IOException {
if (!currentMessage.has("attachments")) return null; if (!currentMessage.has("attachments")) return null;
final JSONArray attachments = currentMessage.getJSONArray("attachments"); final JSONArray attachments = currentMessage.getJSONArray("attachments");
if (attachments.length() == 0) return null; if (attachments.length() == 0) return null;
final JSONObject attachment = attachments.getJSONObject(0); final JSONObject attachment = attachments.getJSONObject(0);
if (!attachment.getString("type").equalsIgnoreCase("photo")) return null; if (!attachment.getString("type").equalsIgnoreCase("photo")) return null;
final JSONObject photoAttachment = attachment.getJSONObject("photo"); final JSONObject photoAttachment = attachment.getJSONObject("photo");
final int[] sizes = {2560, 1280, 807, 604, 130, 75}; final int[] sizes = {2560, 1280, 807, 604, 130, 75};
for (int size : sizes) { 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 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;
}
} }