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

2
game/pom.xml

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

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

@ -27,4 +27,11 @@ public class ClientDto {
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.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.po.GameRecord;
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.util.GameConstant;
import com.ccsens.util.CodeEnum;
import com.ccsens.util.JsonResponse;
import com.ccsens.util.RedisUtil;
import com.ccsens.util.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
@ -40,6 +45,8 @@ public class ClientService implements IClientService {
private Snowflake snowflake;
@Autowired
private RedisUtil redisUtil;
@Autowired
private TallFeignClient tallFeignClient;
@Override
public ClientVo.Join join(ClientDto.Join join, Long userId) {
@ -76,6 +83,8 @@ public class ClientService implements IClientService {
joinVo.setCompletedData(completedData);
return joinVo;
}
boolean prepare = gameRecord.getGameStatus().byteValue() == GameConstant.GAME_PREPARATION;
boolean processing = gameRecord.getGameStatus().byteValue() == GameConstant.GAME_PROCESSING;
// 2.保存游戏路径用户表(设置游戏路径ID+用户ID组合为唯一索引 replace)
GameUserJoin userJoin = new GameUserJoin();
@ -83,19 +92,26 @@ public class ClientService implements IClientService {
userJoin.setUserId(userId);
userJoin.setRecordId(join.getUrlId());
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.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);
// 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.根据状态延时发送消息
//5.返回状态
return null;
return joinResult(userJoin, gameRecord);
}
private ClientVo.Join joinResult(GameUserJoin join, GameRecord gameRecord){
@ -122,8 +138,8 @@ public class ClientService implements IClientService {
case GameConstant.GAME_COMPLETED :
//已结束
ClientVo.CompletedData completedData = new ClientVo.CompletedData();
completedData.setTimes(Integer.parseInt(join.getTimes()));
completedData.setScore(Integer.parseInt(join.getScore()));
completedData.setTimes(join.getTimes());
completedData.setScore(join.getScore());
completedData.setSort(gameUserJoinDao.getRanking(join.getUserId(), join.getUserId()));
// TODO 超过百分之几的用户
joinVo.setCompletedData(completedData);
@ -143,10 +159,13 @@ public class ClientService implements IClientService {
// 游戏状态
joinVo.setGameStatus(gameRecord.getGameStatus());
// 总人数
// GameUserJoinExample userJoinExample = new GameUserJoinExample();
// userJoinExample.createCriteria().andRecordIdEqualTo(gameRecord.getId());
// long count = gameUserJoinDao.countByExample(userJoinExample);
joinVo.setCountMembers(redisUtil.zsGetSize(gameRecord.getId() + GameConstant.GAME_KEY));
long count = redisUtil.zsGetSize(GameConstant.generateGameKey(gameRecord.getId()));
if (count <= 0) {
GameUserJoinExample userJoinExample = new GameUserJoinExample();
userJoinExample.createCriteria().andRecordIdEqualTo(gameRecord.getId());
count = gameUserJoinDao.countByExample(userJoinExample);
}
joinVo.setCountMembers(count);
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;
/**游戏key*/
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;
}
}

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

@ -7,22 +7,25 @@ spring:
name: game
datasource:
type: com.alibaba.druid.pool.DruidDataSource
rabbitmq:
host: api.ccsens.com
password: 111111
port: 5672
username: admin
redis:
database: 0
host: 127.0.0.1
jedis:
pool:
max-active: 200
max-idle: 10
max-wait: -1ms
min-idle: 0
password: ''
port: 6379
timeout: 1000ms
rabbitmq:
host: api.ccsens.com
password: 111111
port: 5672
username: admin
redis:
database: 0
host: 127.0.0.1
jedis:
pool:
max-active: 200
max-idle: 10
max-wait: -1ms
min-idle: 0
password: ''
port: 6379
timeout: 1000ms
swagger:
enable: true
eureka:
instance:
ip-address: 119.28.76.62

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

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

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

@ -15,7 +15,7 @@ spring:
maxWait: 60000
minEvictableIdleTimeMillis: 300000
minIdle: 5
password:
password: 37080c1f223685592316b02dad8816c019290a476e54ebb638f9aa3ba8b6bdb9
poolPreparedStatements: true
servletLogSlowSql: true
servletLoginPassword: 111111
@ -27,7 +27,7 @@ spring:
testOnReturn: false
testWhileIdle: true
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
validationQuery: SELECT 1 FROM DUAL
env: CCSENS_GAME
Loading…
Cancel
Save