261 lines
9.3 KiB
Java
Raw Normal View History

2021-12-06 11:19:00 +08:00
package com.yutou.qqbot.models.setu;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
2021-12-06 11:19:00 +08:00
2024-05-04 17:26:27 +08:00
import com.yutou.napcat.QQDatabase;
import com.yutou.napcat.enums.MessageEnum;
import com.yutou.napcat.event.GroupMessageEvent;
import com.yutou.napcat.handle.At;
import com.yutou.napcat.handle.BaseHandle;
import com.yutou.napcat.handle.Image;
import com.yutou.napcat.handle.Text;
import com.yutou.napcat.http.NapCatApi;
import com.yutou.napcat.model.MessageBean;
import com.yutou.okhttp.HttpBody;
2021-12-07 21:59:41 +08:00
import com.yutou.qqbot.Annotations.UseModel;
2021-12-06 11:19:00 +08:00
import com.yutou.qqbot.QQBotManager;
2024-05-04 17:26:27 +08:00
import com.yutou.qqbot.data.MessageChainBuilder;
2021-12-07 21:59:41 +08:00
import com.yutou.qqbot.models.Model;
2021-12-06 11:19:00 +08:00
import com.yutou.qqbot.utlis.AppTools;
import com.yutou.qqbot.utlis.Log;
import com.yutou.qqbot.utlis.RedisTools;
2024-05-04 17:26:27 +08:00
import com.yutou.napcat.event.MessageEvent;
import com.yutou.qqbot.utlis.StringUtils;
2021-12-06 11:19:00 +08:00
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
2021-12-07 21:59:41 +08:00
import java.text.SimpleDateFormat;
import java.util.*;
2021-12-06 11:19:00 +08:00
2021-12-07 21:59:41 +08:00
@UseModel
public class QQSetu extends Model {
2024-05-04 17:26:27 +08:00
private int db_print = 1;//统计结果
private int db_user = 3;//当次数据
2021-12-07 21:59:41 +08:00
private long group;
private Timer timer;
private static final Map<String, Float> setuScore = new HashMap<>();
2021-12-07 21:59:41 +08:00
2024-05-04 17:26:27 +08:00
public void printTodaySetu() throws Exception {
String redisKey = AppTools.getToDayTime() + "_setu";
2021-12-06 11:19:00 +08:00
Log.i("今日涩图 redisKey = " + redisKey);
2021-12-07 21:59:41 +08:00
String js = RedisTools.get(redisKey, db_print);
2021-12-06 11:19:00 +08:00
if (js != null) {
JSONObject json = JSON.parseObject(js);
2024-05-04 17:26:27 +08:00
if (json.containsKey("isPrint") && json.getBooleanValue("isPrint")) {
2021-12-06 11:19:00 +08:00
return;
}
2024-05-04 17:26:27 +08:00
Map<String, Float> groupAverage = new HashMap<>();
Map<String, String> groupImage = new HashMap<>();
JSONObject setu = null;
2021-12-06 11:19:00 +08:00
for (String id : json.keySet()) {
2024-05-04 17:26:27 +08:00
String group = json.getJSONObject(id).getJSONObject("info").getLong("group") + "";
if (groupAverage.containsKey(group)) {
if (groupAverage.get(group) <= json.getJSONObject(id).getFloat("average")) {
groupAverage.put(group, json.getJSONObject(id).getFloat("average"));
groupImage.put(group, id);
2021-12-06 11:19:00 +08:00
}
2024-05-04 17:26:27 +08:00
} else {
groupAverage.put(group, json.getJSONObject(id).getFloat("average"));
groupImage.put(group, id);
2021-12-06 11:19:00 +08:00
}
}
for (String id : groupImage.keySet()) {
2024-05-04 17:26:27 +08:00
setu = json.getJSONObject(groupImage.get(id));
if (setu != null) {
json.put("isPrint", true);
RedisTools.set(db_print, redisKey, json.toJSONString());
JSONObject info = setu.getJSONObject("info");
JSONObject score = setu.getJSONObject("score");
HttpBody<MessageBean> body = NapCatApi.getMessageApi().getMessage(info.getLong("id")).execute().body();
List<BaseHandle<?>> sendList = new ArrayList<>();
if (body == null) {
sendList.add(new Text("[图片获取失败]"));
return;
}
MessageEvent handle = MessageEvent.parseHandleHttp(body.getSrc());
Image image = handle.findType(Image.class);
if (image == null) {
sendList.add(new Text("[图片获取失败]"));
} else {
sendList.add(image);
}
//builder.append(Image.fromId(info.getString("id")));
sendList.add(new Text(
"本日最佳涩图由", false
));
sendList.add(new At(handle.getSource().getUserId()));
sendList.add(new Text("提供", false));
sendList.add(new Text("获得分数 ", false));
sendList.add(new Text(String.valueOf(setu.getFloat("average")), false));
sendList.add(new Text("共有 ", false));
sendList.add(new Text(String.valueOf(score.getIntValue("userNumber")), false));
sendList.add(new Text(" 人参与投票", false));
QQBotManager.getInstance().sendMessage(handle.isUser(), handle.getGroupId(), sendList);
2021-12-06 11:19:00 +08:00
}
}
2024-05-04 17:26:27 +08:00
} else {
2021-12-06 11:19:00 +08:00
Log.i("今日没有涩图");
}
}
2024-05-04 17:26:27 +08:00
private boolean isSetu(Long length) {
return length > 50000;
2021-12-06 11:19:00 +08:00
}
2024-05-04 17:26:27 +08:00
private void setuBuilder(Image image, MessageEvent event) {
if (StringUtils.isEmpty(image.getData().getFileSize()) || !isSetu(Long.parseLong(image.getData().getFileSize()))) {
2021-12-06 11:19:00 +08:00
return;
}
2024-05-04 17:26:27 +08:00
if (RedisTools.get(event.getGroupId() + "setu", db_user) != null) {
printSetu(event.getGroupId());
2021-12-06 11:19:00 +08:00
}
setuScore.clear();
JSONObject json = new JSONObject();
2024-05-04 17:26:27 +08:00
json.put("id", event.getMessageId());
RedisTools.set(event.getGroupId() + "setu", json.toJSONString(), 6 * 60, db_user);
2021-12-06 11:19:00 +08:00
if (timer != null) {
timer.cancel();
timer = null;
}
2021-12-07 21:59:41 +08:00
startTimer();
2021-12-06 11:19:00 +08:00
}
2021-12-07 21:59:41 +08:00
private void startTimer() {
2021-12-06 11:19:00 +08:00
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
2024-05-04 17:26:27 +08:00
if (!setuScore.isEmpty()) {
2021-12-06 11:19:00 +08:00
printSetu(group);
}
timer.cancel();
2021-12-06 11:19:00 +08:00
timer = null;
}
2021-12-07 21:59:41 +08:00
}, (long) (0.9 * 60 * 1000));
2021-12-06 11:19:00 +08:00
}
private void printSetu(long group) {
2024-05-04 17:26:27 +08:00
JSONObject jt = JSON.parseObject(RedisTools.get(group + "setu", db_user));
2021-12-06 11:19:00 +08:00
String id = jt.getString("id");
float average = 0;
float max = 0;
float min = 10;
float length = 0;
String maxName = "";
String minName = "";
2024-05-04 17:26:27 +08:00
if (setuScore.size() <= 1) {
2021-12-06 11:19:00 +08:00
return;
}
for (String name : setuScore.keySet()) {
length += setuScore.get(name);
average += setuScore.get(name);
if (setuScore.get(name) > max) {
max = setuScore.get(name);
maxName = name.split("\\|")[0];
}
if (setuScore.get(name) < min) {
min = setuScore.get(name);
minName = name.split("\\|")[0];
}
}
JSONObject score = new JSONObject();
score.put("max", max);
score.put("min", min);
score.put("sum", length);
score.put("maxName", maxName);
score.put("minName", minName);
score.put("userNumber", setuScore.size());
average = average / setuScore.size();
2024-05-04 17:26:27 +08:00
String builder = "涩图评分:" + average + "\n " +
"其中最高分由:" + maxName + " 给与:" + max + "\n " +
2021-12-06 11:19:00 +08:00
"其中最低分由:" + minName + " 给与:" + min;
QQBotManager.getInstance().sendMessage(group, builder);
2021-12-07 21:59:41 +08:00
String st = RedisTools.get(AppTools.getToDayTime() + "_setu", db_print);
2021-12-06 11:19:00 +08:00
JSONObject json;
if (st == null) {
json = new JSONObject();
} else {
json = JSON.parseObject(st);
2021-12-06 11:19:00 +08:00
}
if (!json.containsKey(id)) {
JSONObject item;
if (json.containsKey("item")) {
item = json.getJSONObject("item");
} else {
item = new JSONObject();
}
item.put("score", score);
item.put("info", jt);
item.put("average", average);
json.put(id, item);
2021-12-07 21:59:41 +08:00
RedisTools.set(db_print, AppTools.getToDayTime() + "_setu", json.toJSONString());
2021-12-06 11:19:00 +08:00
}
2024-05-04 17:26:27 +08:00
RedisTools.remove(group + "setu", db_user);
2021-12-06 11:19:00 +08:00
setuScore.clear();
}
2024-05-04 17:26:27 +08:00
2021-12-07 21:59:41 +08:00
@Override
2024-05-04 17:26:27 +08:00
public void onMessage(Long qq, MessageEvent event, boolean isGroup) {
if (!isGroup) {
2021-12-07 21:59:41 +08:00
return;
}
2024-05-04 17:26:27 +08:00
Image image = event.hasType(MessageEnum.IMAGE) ? event.findType(Image.class) : null;
if (image != null) {
setuBuilder(image, event);
return;
}
Text text = event.hasType(MessageEnum.TEXT) ? event.findType(Text.class) : null;
if (text == null) {
return;
2021-12-06 11:19:00 +08:00
}
try {
2024-05-04 17:26:27 +08:00
if (text.toString().trim().length() > 3) {
2021-12-06 11:19:00 +08:00
return;
}
2024-05-04 17:26:27 +08:00
float i = Float.parseFloat(text.toString().trim());
2021-12-06 11:19:00 +08:00
if (i > 0 && i <= 10) {
2024-05-04 17:26:27 +08:00
String name = event.getSource().getNickname();
String sender = event.getSource().getUserId() + "";
2021-12-06 11:19:00 +08:00
if (!setuScore.containsKey(name)) {
2024-05-04 17:26:27 +08:00
setuScore.put(name + "|" + sender, i);
2021-12-06 11:19:00 +08:00
}
}
} catch (Exception ignored) {
}
}
2021-12-07 21:59:41 +08:00
@Override
2024-05-04 17:26:27 +08:00
public void onTime(Long qq, String time) {
super.onTime(qq, time);
if ("23:59:00".equals(time)) {
try {
printTodaySetu();
} catch (Exception e) {
e.printStackTrace();
}
2021-12-07 21:59:41 +08:00
}
}
@Override
public boolean isUserPublic() {
return true;
}
@Override
public String[] getUsePowers() {
return new String[0];
}
@Override
public String getModelName() {
return "涩图评分";
}
2021-12-06 11:19:00 +08:00
}