战斗记录逻辑优化,但是未启用(可能会增加io和内存)
parent
be12e006a2
commit
a32bec7c08
|
|
@ -1,13 +1,5 @@
|
|||
package com.ljsd.jieling.kefu;
|
||||
|
||||
import com.ljsd.jieling.logic.activity.eventhandler.AddItemHandler;
|
||||
import com.ljsd.jieling.logic.dao.UserManager;
|
||||
import com.ljsd.jieling.logic.dao.root.User;
|
||||
import com.ljsd.jieling.logic.family.GuildLogic;
|
||||
import com.ljsd.jieling.logic.fight.FightRecordLogic;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class Cmd_fightrecord extends GmAbstract {
|
||||
|
||||
@Override
|
||||
|
|
@ -17,36 +9,35 @@ public class Cmd_fightrecord extends GmAbstract {
|
|||
|
||||
@Override
|
||||
public String exec2(String[] args){
|
||||
// 获取玩家战报系统
|
||||
String sign = args.length>0 ? args[0]:"";
|
||||
String content = args.length>1 ? args[1]:"";
|
||||
String result = "";
|
||||
switch (sign) {
|
||||
case "delete":
|
||||
FightRecordLogic.getInstance().delete();
|
||||
LOGGER.info("战报信息,delete");
|
||||
break;
|
||||
case "all":
|
||||
result = FightRecordLogic.getInstance().getAll(Integer.parseInt(content));
|
||||
LOGGER.info("战报信息,all:{}", result);
|
||||
break;
|
||||
case "tuigonghui":
|
||||
try {
|
||||
int uid = Integer.parseInt(args[1]);
|
||||
User user = UserManager.getUser(uid);
|
||||
GuildLogic.OnUserLeveFamily(user);
|
||||
user.getPlayerInfoManager().setGuildId(0);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
LOGGER.info("神尊等级初始化......");
|
||||
break;
|
||||
default:
|
||||
result = FightRecordLogic.getInstance().getOneRecord(content);
|
||||
LOGGER.info("战报信息,OneRecord:{}", result);
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
// // 获取玩家战报系统
|
||||
// String sign = args.length>0 ? args[0]:"";
|
||||
// String content = args.length>1 ? args[1]:"";
|
||||
// String result = "";
|
||||
// switch (sign) {
|
||||
// case "delete":
|
||||
// FightRecordLogic.getInstance().delete();
|
||||
// LOGGER.info("战报信息,delete");
|
||||
// break;
|
||||
// case "all":
|
||||
// result = FightRecordLogic.getInstance().getAll(Integer.parseInt(content));
|
||||
// LOGGER.info("战报信息,all:{}", result);
|
||||
// break;
|
||||
// case "tuigonghui":
|
||||
// try {
|
||||
// int uid = Integer.parseInt(args[1]);
|
||||
// User user = UserManager.getUser(uid);
|
||||
// GuildLogic.OnUserLeveFamily(user);
|
||||
// user.getPlayerInfoManager().setGuildId(0);
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// LOGGER.info("神尊等级初始化......");
|
||||
// break;
|
||||
// default:
|
||||
// result = FightRecordLogic.getInstance().getOneRecord(content);
|
||||
// LOGGER.info("战报信息,OneRecord:{}", result);
|
||||
// break;
|
||||
// }
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,23 +1,22 @@
|
|||
package com.ljsd.jieling.logic.fight;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.googlecode.protobuf.format.JsonFormat;
|
||||
import com.ljsd.jieling.logic.dao.root.User;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import rpc.protocols.CommonProto;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
public class FightRecordLogic {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(FightRecordLogic.class);
|
||||
//私有构造方法
|
||||
private FightRecordLogic (){
|
||||
|
||||
}
|
||||
//声明成员变量
|
||||
private static FightRecordLogic instance ;
|
||||
private static volatile FightRecordLogic instance ;
|
||||
//对外提供接口获取该实例
|
||||
public static FightRecordLogic getInstance(){
|
||||
if(instance == null){
|
||||
|
|
@ -31,89 +30,33 @@ public class FightRecordLogic {
|
|||
return instance;
|
||||
}
|
||||
|
||||
private ConcurrentHashMap<Integer,MultiValueMap<Integer,String>> recordMap = new ConcurrentHashMap<>();
|
||||
public static final int QUEUE_LENGTH = 1000;
|
||||
|
||||
public ConcurrentHashMap<Integer, MultiValueMap<Integer, String>> getRecordMap() {
|
||||
return recordMap;
|
||||
}
|
||||
/**
|
||||
* 基于内存的阻塞队列
|
||||
*/
|
||||
private static BlockingQueue<String> queue = new LinkedBlockingQueue<>(QUEUE_LENGTH);
|
||||
|
||||
// 一种类型存储上限
|
||||
private static int max = 10;
|
||||
|
||||
public void setRecordMap(ConcurrentHashMap<Integer, MultiValueMap<Integer, String>> recordMap) {
|
||||
this.recordMap = recordMap;
|
||||
}
|
||||
|
||||
public void addRecordMap(User user, CommonProto.FightData fightData) throws JsonProcessingException {
|
||||
// 玩家id
|
||||
// int uid = user.getId();
|
||||
// // 战斗类型
|
||||
// int multiKey = fightData.getFightType();
|
||||
// // 获取玩家的战斗数据
|
||||
// MultiValueMap<Integer, String> multiValueMap = recordMap.getOrDefault(uid, new LinkedMultiValueMap<Integer, String>());
|
||||
// // 根据类型获取战斗数据
|
||||
// List<String> strings = multiValueMap.getOrDefault(multiKey,new ArrayList<>());
|
||||
// // 类型大于十条删除最早的一条
|
||||
// if (strings.size() >= max){
|
||||
// strings.remove(0);
|
||||
// }
|
||||
// // protobuf转字符串
|
||||
// String str = JsonFormat.printToString(fightData);
|
||||
// strings.add(str);
|
||||
// // 重新赋值
|
||||
// multiValueMap.put(multiKey,strings);
|
||||
// recordMap.put(uid,multiValueMap);
|
||||
/**
|
||||
* 消息处理
|
||||
*/
|
||||
public static void sessionHandler(){
|
||||
String str;
|
||||
while ((str = queue.poll()) != null){
|
||||
LOGGER.info("战斗记录:{}", str);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一条记录
|
||||
* @param fightId 玩家id_服务器id_战斗类型id_时间戳
|
||||
* @return
|
||||
* @throws JsonProcessingException
|
||||
* 添加信息至队列中
|
||||
*/
|
||||
public String getOneRecord(String fightId){
|
||||
String[] split = fightId.split("_");
|
||||
// 玩家id
|
||||
int uid = Integer.parseInt(split[0]);
|
||||
// 战斗类型,key
|
||||
int multiKey = Integer.parseInt(split[2]);
|
||||
// 获取玩家的全部战斗数据
|
||||
MultiValueMap<Integer, String> valueMap = recordMap.get(uid);
|
||||
if (valueMap == null){
|
||||
return null;
|
||||
public void addRecordMap(User user, CommonProto.FightData fightData){
|
||||
try {
|
||||
String str = JsonFormat.printToString(fightData);
|
||||
queue.offer(str);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 获取类型数据
|
||||
List<String> strings = valueMap.get(multiKey);
|
||||
if (strings == null || strings.isEmpty()){
|
||||
return null;
|
||||
}
|
||||
// 遍历
|
||||
for (int i = 0; i < strings.size(); i++) {
|
||||
String item = strings.get(i);
|
||||
// 初始化
|
||||
CommonProto.FightData.Builder data = CommonProto.FightData.newBuilder();
|
||||
try {
|
||||
// json转protobuf
|
||||
JsonFormat.merge(item,data);
|
||||
} catch (JsonFormat.ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 找到该战斗id,直接返回
|
||||
if (fightId.equals(data.getFightId())){
|
||||
// 返回结果
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void delete(){
|
||||
recordMap.clear();
|
||||
}
|
||||
|
||||
public String getAll(int uid){
|
||||
MultiValueMap<Integer, String> valueMap = recordMap.getOrDefault(uid, new LinkedMultiValueMap<>());
|
||||
return valueMap.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue