Browse Source

Join

master
6 years ago
parent
commit
485471b11d
  1. 7
      cloudutil/src/main/resources/application-util-test.yml
  2. 2
      game/pom.xml
  3. 7
      game/src/main/java/com/ccsens/game/bean/dto/ClientDto.java
  4. 43
      game/src/main/java/com/ccsens/game/service/ClientService.java
  5. 11
      game/src/main/java/com/ccsens/game/util/GameConstant.java
  6. 3
      game/src/main/resources/application-test.yml
  7. 4
      game/src/main/resources/application.yml
  8. 4
      game/src/main/resources/druid-test.yml

7
cloudutil/src/main/resources/application-util-test.yml

@ -20,7 +20,7 @@ eureka:
service-url: service-url:
# 指定eureka server通信地址,注意/eureka/小尾巴不能少 # 指定eureka server通信地址,注意/eureka/小尾巴不能少
#defaultZone: http://admin:admin@peer1:8761/eureka/,http://admin:admin@peer2:8762/eureka/ #defaultZone: http://admin:admin@peer1:8761/eureka/,http://admin:admin@peer2:8762/eureka/
defaultZone: http://admin:admin@127.0.0.1:7010/eureka/ defaultZone: http://admin:admin@49.233.89.188:7010/eureka/
instance: instance:
# 是否注册IP到eureka server,如不指定或设为false,那就回注册主机名到eureka server # 是否注册IP到eureka server,如不指定或设为false,那就回注册主机名到eureka server
prefer-ip-address: true prefer-ip-address: true
@ -50,8 +50,11 @@ logging:
org.springframework.cloud.sleuth: DEBUG org.springframework.cloud.sleuth: DEBUG
spring: spring:
zipkin: zipkin:
base-url: http://127.0.0.1:9411 base-url: http://49.233.89.188:9411
sleuth: sleuth:
sampler: sampler:
# 采样率,模式0.1,也就是10%,为了便于观察效果,改为1.0,也就是100%。生产环境建议保持默认。 # 采样率,模式0.1,也就是10%,为了便于观察效果,改为1.0,也就是100%。生产环境建议保持默认。
probability: 1.0 probability: 1.0
cloud:
inetutils:
ignored-interfaces: ['VMware.*']

2
game/pom.xml

@ -57,7 +57,7 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
<configuration> <configuration>
<mainClass>com.ccsens.ht.HtApplication</mainClass> <mainClass>com.ccsens.game.GameApplication</mainClass>
<!--<skip>true</skip>--> <!--<skip>true</skip>-->
</configuration> </configuration>
<executions> <executions>

7
game/src/main/java/com/ccsens/game/bean/dto/ClientDto.java

@ -27,4 +27,11 @@ public class ClientDto {
private Long projectId; private Long projectId;
} }
@Data
public static class RedisUser{
private Long id;
private String avatarurl;
private String nickname;
}
} }

43
game/src/main/java/com/ccsens/game/service/ClientService.java

@ -2,6 +2,10 @@ package com.ccsens.game.service;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Snowflake; import cn.hutool.core.lang.Snowflake;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ccsens.cloudutil.bean.tall.vo.MemberVo;
import com.ccsens.cloudutil.feign.TallFeignClient;
import com.ccsens.game.bean.dto.ClientDto; import com.ccsens.game.bean.dto.ClientDto;
import com.ccsens.game.bean.po.GameRecord; import com.ccsens.game.bean.po.GameRecord;
import com.ccsens.game.bean.po.GameUserJoin; import com.ccsens.game.bean.po.GameUserJoin;
@ -11,6 +15,7 @@ import com.ccsens.game.persist.dao.GameRecordDao;
import com.ccsens.game.persist.dao.GameUserJoinDao; import com.ccsens.game.persist.dao.GameUserJoinDao;
import com.ccsens.game.util.GameConstant; import com.ccsens.game.util.GameConstant;
import com.ccsens.util.CodeEnum; import com.ccsens.util.CodeEnum;
import com.ccsens.util.JsonResponse;
import com.ccsens.util.RedisUtil; import com.ccsens.util.RedisUtil;
import com.ccsens.util.exception.BaseException; import com.ccsens.util.exception.BaseException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -40,6 +45,8 @@ public class ClientService implements IClientService {
private Snowflake snowflake; private Snowflake snowflake;
@Autowired @Autowired
private RedisUtil redisUtil; private RedisUtil redisUtil;
@Autowired
private TallFeignClient tallFeignClient;
@Override @Override
public ClientVo.Join join(ClientDto.Join join, Long userId) { public ClientVo.Join join(ClientDto.Join join, Long userId) {
@ -76,6 +83,8 @@ public class ClientService implements IClientService {
joinVo.setCompletedData(completedData); joinVo.setCompletedData(completedData);
return joinVo; return joinVo;
} }
boolean prepare = gameRecord.getGameStatus().byteValue() == GameConstant.GAME_PREPARATION;
boolean processing = gameRecord.getGameStatus().byteValue() == GameConstant.GAME_PROCESSING;
// 2.保存游戏路径用户表(设置游戏路径ID+用户ID组合为唯一索引 replace) // 2.保存游戏路径用户表(设置游戏路径ID+用户ID组合为唯一索引 replace)
GameUserJoin userJoin = new GameUserJoin(); GameUserJoin userJoin = new GameUserJoin();
@ -83,19 +92,26 @@ public class ClientService implements IClientService {
userJoin.setUserId(userId); userJoin.setUserId(userId);
userJoin.setRecordId(join.getUrlId()); userJoin.setRecordId(join.getUrlId());
userJoin.setTimeDifference(timeMore); userJoin.setTimeDifference(timeMore);
boolean prepare = gameRecord.getGameStatus().byteValue() == GameConstant.GAME_PREPARATION;
boolean processing = gameRecord.getGameStatus().byteValue() == GameConstant.GAME_PROCESSING;
userJoin.setLocalStartTime(prepare || processing ? gameRecord.getStartTime() + timeMore : 0); userJoin.setLocalStartTime(prepare || processing ? gameRecord.getStartTime() + timeMore : 0);
userJoin.setLocalStartTime(prepare || processing ? gameRecord.getEndTime() + timeMore : 0); userJoin.setLocalStartTime(prepare || processing ? gameRecord.getEndTime() + timeMore : 0);
// 获取头像和用户名
JsonResponse<MemberVo.MemberInfo> memberInfo = tallFeignClient.getMemberByUserId(userId, join.getProjectId());
if (memberInfo.getCode().intValue() == CodeEnum.SUCCESS.getCode() && memberInfo.getData() != null) {
userJoin.setAvatarurl(memberInfo.getData().getAvatarUrl());
userJoin.setNickname(memberInfo.getData().getNickname());
}
gameUserJoinDao.insertSelective(userJoin); gameUserJoinDao.insertSelective(userJoin);
// 3.更新redis(sort set key:分数 value:头像,姓名) // 3.更新redis(sort set key:分数 value:头像,姓名)
ClientDto.RedisUser user = new ClientDto.RedisUser();
user.setId(userJoin.getId());
user.setAvatarurl(userJoin.getAvatarurl());
user.setNickname(userJoin.getNickname());
redisUtil.zsSet(GameConstant.generateGameKey(gameRecord.getId()), JSON.toJSONString(user), 0, GameConstant.REDIS_TIME);
//4.根据状态延时发送消息 //4.根据状态延时发送消息
//5.返回状态 //5.返回状态
return null; return joinResult(userJoin, gameRecord);
} }
private ClientVo.Join joinResult(GameUserJoin join, GameRecord gameRecord){ private ClientVo.Join joinResult(GameUserJoin join, GameRecord gameRecord){
@ -122,8 +138,8 @@ public class ClientService implements IClientService {
case GameConstant.GAME_COMPLETED : case GameConstant.GAME_COMPLETED :
//已结束 //已结束
ClientVo.CompletedData completedData = new ClientVo.CompletedData(); ClientVo.CompletedData completedData = new ClientVo.CompletedData();
completedData.setTimes(Integer.parseInt(join.getTimes())); completedData.setTimes(join.getTimes());
completedData.setScore(Integer.parseInt(join.getScore())); completedData.setScore(join.getScore());
completedData.setSort(gameUserJoinDao.getRanking(join.getUserId(), join.getUserId())); completedData.setSort(gameUserJoinDao.getRanking(join.getUserId(), join.getUserId()));
// TODO 超过百分之几的用户 // TODO 超过百分之几的用户
joinVo.setCompletedData(completedData); joinVo.setCompletedData(completedData);
@ -143,10 +159,13 @@ public class ClientService implements IClientService {
// 游戏状态 // 游戏状态
joinVo.setGameStatus(gameRecord.getGameStatus()); joinVo.setGameStatus(gameRecord.getGameStatus());
// 总人数 // 总人数
// GameUserJoinExample userJoinExample = new GameUserJoinExample(); long count = redisUtil.zsGetSize(GameConstant.generateGameKey(gameRecord.getId()));
// userJoinExample.createCriteria().andRecordIdEqualTo(gameRecord.getId()); if (count <= 0) {
// long count = gameUserJoinDao.countByExample(userJoinExample); GameUserJoinExample userJoinExample = new GameUserJoinExample();
joinVo.setCountMembers(redisUtil.zsGetSize(gameRecord.getId() + GameConstant.GAME_KEY)); userJoinExample.createCriteria().andRecordIdEqualTo(gameRecord.getId());
count = gameUserJoinDao.countByExample(userJoinExample);
}
joinVo.setCountMembers(count);
return joinVo; return joinVo;
} }
} }

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

@ -17,6 +17,17 @@ public class GameConstant {
public static final byte GAME_COMPLETED = 3; public static final byte GAME_COMPLETED = 3;
/**游戏key*/ /**游戏key*/
public static final String GAME_KEY = "_game"; public static final String GAME_KEY = "_game";
/**数据默认保存 10分钟*/
public static final long REDIS_TIME = 600 ;
/**
* 生成游戏key
* @param recordId
* @return
*/
public static String generateGameKey(Long recordId) {
return recordId + GAME_KEY;
}
} }

3
game/src/main/resources/application-test.yml

@ -26,3 +26,6 @@ spring:
timeout: 1000ms timeout: 1000ms
swagger: swagger:
enable: true enable: true
eureka:
instance:
ip-address: 119.28.76.62

4
game/src/main/resources/application.yml

@ -1,4 +1,4 @@
spring: spring:
profiles: profiles:
active: dev active: test
include: common, util-dev include: common, util-test

4
game/src/main/resources/druid-test.yml

@ -15,7 +15,7 @@ spring:
maxWait: 60000 maxWait: 60000
minEvictableIdleTimeMillis: 300000 minEvictableIdleTimeMillis: 300000
minIdle: 5 minIdle: 5
password: password: 37080c1f223685592316b02dad8816c019290a476e54ebb638f9aa3ba8b6bdb9
poolPreparedStatements: true poolPreparedStatements: true
servletLogSlowSql: true servletLogSlowSql: true
servletLoginPassword: 111111 servletLoginPassword: 111111
@ -27,7 +27,7 @@ spring:
testOnReturn: false testOnReturn: false
testWhileIdle: true testWhileIdle: true
timeBetweenEvictionRunsMillis: 60000 timeBetweenEvictionRunsMillis: 60000
url: jdbc:mysql://127.0.0.1/game?useUnicode=true&characterEncoding=UTF-8 url: jdbc:mysql://49.233.89.188/game?useUnicode=true&characterEncoding=UTF-8
username: root username: root
validationQuery: SELECT 1 FROM DUAL validationQuery: SELECT 1 FROM DUAL
env: CCSENS_GAME env: CCSENS_GAME
Loading…
Cancel
Save