Browse Source

Merge branch 'master' of gitee.com:ccsens_s/ccsenscloud

master
zhangye 5 years ago
parent
commit
532ce2023b
  1. 2
      game/src/main/java/com/ccsens/game/bean/dto/ScreenDto.java
  2. 10
      game/src/main/java/com/ccsens/game/config/BeanConfig.java
  3. 106
      game/src/main/java/com/ccsens/game/intercept/MybatisInterceptor.java
  4. 24
      game/src/main/java/com/ccsens/game/service/ScreenService.java
  5. 3
      game/src/main/java/com/ccsens/game/util/GameConstant.java

2
game/src/main/java/com/ccsens/game/bean/dto/ScreenDto.java

@ -51,6 +51,8 @@ public class ScreenDto {
@NotNull(message = "请输入时间")
@ApiModelProperty("本地时间")
private Long localTime;
@ApiModelProperty("是否为单独的再来一次 1:再玩一次")
private Byte startStatus;
}

10
game/src/main/java/com/ccsens/game/config/BeanConfig.java

@ -26,9 +26,9 @@ public class BeanConfig {
/**
* 注册拦截器
*/
// @Bean
// public MybatisInterceptor mybatisInterceptor() {
// MybatisInterceptor interceptor = new MybatisInterceptor();
// return interceptor;
// }
@Bean
public MybatisInterceptor mybatisInterceptor() {
MybatisInterceptor interceptor = new MybatisInterceptor();
return interceptor;
}
}

106
game/src/main/java/com/ccsens/game/intercept/MybatisInterceptor.java

@ -12,8 +12,10 @@ import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
@ -23,9 +25,9 @@ import java.util.Properties;
*/
@Intercepts({
@Signature(
type = Executor.class,
method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
type = Executor.class,
method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
)
})
public class MybatisInterceptor implements Interceptor {
@ -34,43 +36,21 @@ public class MybatisInterceptor implements Interceptor {
String selectByExample = "selectByExample";
String countByExample = "countByExample";
String countByExample2 = "selectByExample_COUNT";
String selectByPrimaryKey = "selectByPrimaryKey";
Object[] args = invocation.getArgs();
Object[] args = invocation.getArgs();
MappedStatement statement = (MappedStatement) args[0];
if (statement.getId().endsWith(selectByExample)) {
if (statement.getId().endsWith(selectByExample)
|| statement.getId().endsWith(countByExample)
|| statement.getId().endsWith(countByExample2)) {
//XXXExample
Object example = args[1];
Method method = example.getClass().getMethod("getOredCriteria", null);
//获取到条件数组,第一个是Criteria
List list = (List)method.invoke(example);
if (CollectionUtil.isEmpty(list)) {
Class clazz = ((ResultMap)statement.getResultMaps().get(0)).getType();
String exampleName = clazz.getName() + "Example";
Object paramExample = Class.forName(exampleName).newInstance();
Method createCriteria = paramExample.getClass().getMethod("createCriteria");
Object criteria = createCriteria.invoke(paramExample);
Method andIsDelEqualTo = criteria.getClass().getMethod("andRecStatusEqualTo", Byte.class);
andIsDelEqualTo.invoke(criteria, WebConstant.REC_STATUS.Normal.value);
list.add(criteria);
} else {
Object criteria = list.get(0);
Method getCriteria = criteria.getClass().getMethod("getCriteria");
List params = (List)getCriteria.invoke(criteria);
boolean hasDel = false;
for(Object param: params) {
Method getCondition = param.getClass().getMethod("getCondition");
Object condition = getCondition.invoke(param);
if ("iis_del =".equals(condition)) {
hasDel = true;
}
}
if (!hasDel) {
Method andIsDelEqualTo = criteria.getClass().getMethod("andRecStatusEqualTo", Byte.class);
andIsDelEqualTo.invoke(criteria, WebConstant.REC_STATUS.Normal.value);
}
}
addCondition(statement, example);
} else if (statement.getId().endsWith(selectByPrimaryKey)) {
@ -85,6 +65,45 @@ public class MybatisInterceptor implements Interceptor {
return invocation.proceed();
}
private void addCondition(MappedStatement statement, Object example) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException {
if (example instanceof Map) {
example = ((Map) example).get("_ORIGINAL_PARAMETER_OBJECT");
}
Method method = example.getClass().getMethod("getOredCriteria", null);
//获取到条件数组,第一个是Criteria
List list = (List) method.invoke(example);
if (CollectionUtil.isEmpty(list)) {
Class clazz = ((ResultMap) statement.getResultMaps().get(0)).getType();
String exampleName = clazz.getName() + "Example";
Object paramExample = Class.forName(exampleName).newInstance();
Method createCriteria = paramExample.getClass().getMethod("createCriteria");
Object criteria = createCriteria.invoke(paramExample);
Method andRecStatusEqualTo = criteria.getClass().getMethod("andRecStatusEqualTo", Byte.class);
andRecStatusEqualTo.invoke(criteria, WebConstant.REC_STATUS.Normal.value);
list.add(criteria);
} else {
Object criteria = list.get(0);
Method getCriteria = criteria.getClass().getMethod("getCriteria");
List params = (List) getCriteria.invoke(criteria);
boolean hasDel = false;
for (Object param : params) {
Method getCondition = param.getClass().getMethod("getCondition");
Object condition = getCondition.invoke(param);
if ("rec_status =".equals(condition)) {
hasDel = true;
}
}
if (!hasDel) {
Method andRecStatusEqualTo = criteria.getClass().getMethod("andRecStatusEqualTo", Byte.class);
andRecStatusEqualTo.invoke(criteria, WebConstant.REC_STATUS.Normal.value);
}
}
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
@ -121,24 +140,7 @@ public class MybatisInterceptor implements Interceptor {
return builder.build();
}
private String getOperateType(Invocation invocation) {
final Object[] args = invocation.getArgs();
MappedStatement ms = (MappedStatement) args[0];
SqlCommandType commondType = ms.getSqlCommandType();
if (commondType.compareTo(SqlCommandType.SELECT) == 0) {
return "select";
}
if (commondType.compareTo(SqlCommandType.INSERT) == 0) {
return "insert";
}
if (commondType.compareTo(SqlCommandType.UPDATE) == 0) {
return "update";
}
if (commondType.compareTo(SqlCommandType.DELETE) == 0) {
return "delete";
}
return null;
}
// 定义一个内部辅助类,作用是包装sq
class BoundSqlSqlSource implements SqlSource {
private BoundSql boundSql;

24
game/src/main/java/com/ccsens/game/service/ScreenService.java

@ -698,7 +698,16 @@ public class ScreenService implements IScreenService{
gameRecordDao.updateByPrimaryKeySelective(gameRecord);
//设置redis 游戏状态 准备中
redisUtil.set(GameConstant.generateGameStatusKey(gameRecord.getId()), JSON.toJSONString(gameRecord), GameConstant.REDIS_TIME);
//再玩一次
if (start.getStartStatus() != null && start.getStartStatus().byteValue() == GameConstant.GAME_RESTART_STATUS) {
//再玩一次,设置参见的用户为无效用户
GameUserJoin delUserJoin = new GameUserJoin();
delUserJoin.setRecStatus(WebConstant.REC_STATUS.Deleted.value);
GameUserJoinExample example = new GameUserJoinExample();
example.createCriteria().andRecordIdEqualTo(start.getMemberRecord());
gameUserJoinDao.updateByExampleSelective(delUserJoin,example);
redisUtil.del(GameConstant.generateGameKey(start.getMemberRecord()));
}
//延时通知
long startSend = gameRecord.getStartTime() - System.currentTimeMillis();
long endSend = gameRecord.getEndTime() - System.currentTimeMillis();
@ -729,14 +738,13 @@ public class ScreenService implements IScreenService{
GameUserJoinExample joinExample = new GameUserJoinExample();
joinExample.createCriteria().andRecordIdEqualTo(gameRecord.getId());
List<GameUserJoin> userJoins = gameUserJoinDao.selectByExample(joinExample);
// if (CollectionUtil.isEmpty(userJoins)) {
// return startGame;
// }
userJoins.forEach( userJoin -> {
ClientDto.RedisUser user = ClientDto.RedisUser.getInstance(userJoin);
redisUtil.zsSet(GameConstant.generateGameKey(gameRecord.getId()), JSON.toJSONString(user), 0, GameConstant.REDIS_TIME);
});
if (CollectionUtil.isEmpty(userJoins)) {
userJoins.forEach( userJoin -> {
ClientDto.RedisUser user = ClientDto.RedisUser.getInstance(userJoin);
redisUtil.zsSet(GameConstant.generateGameKey(gameRecord.getId()), JSON.toJSONString(user), 0, GameConstant.REDIS_TIME);
});
}
//推送客户端
pushClient(gameRecord, executor);

3
game/src/main/java/com/ccsens/game/util/GameConstant.java

@ -24,7 +24,8 @@ public class GameConstant {
public static final long COUNT_DOWN_TIME = 10*1000;
/**游戏时长:1分钟 ms值*/
public static final long GAME_TIME = 60*1000;
/**再来一次*/
public static final byte GAME_RESTART_STATUS = 1;
/**
* 生成游戏key

Loading…
Cancel
Save