From 485471b11d77f10a34e806b46cdbda813fe3084e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AD=A6?= <吴æ武慧娟> Date: Fri, 27 Dec 2019 13:23:38 +0800 Subject: [PATCH] Join --- .../main/resources/application-util-test.yml | 7 ++- game/pom.xml | 2 +- .../com/ccsens/game/bean/dto/ClientDto.java | 7 +++ .../ccsens/game/service/ClientService.java | 43 +++++++++++++------ .../com/ccsens/game/util/GameConstant.java | 11 +++++ game/src/main/resources/application-test.yml | 37 ++++++++-------- game/src/main/resources/application.yml | 4 +- game/src/main/resources/druid-test.yml | 4 +- 8 files changed, 79 insertions(+), 36 deletions(-) diff --git a/cloudutil/src/main/resources/application-util-test.yml b/cloudutil/src/main/resources/application-util-test.yml index 501826c1..348a879c 100644 --- a/cloudutil/src/main/resources/application-util-test.yml +++ b/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.*'] \ No newline at end of file diff --git a/game/pom.xml b/game/pom.xml index 7d79952b..7aa7b830 100644 --- a/game/pom.xml +++ b/game/pom.xml @@ -57,7 +57,7 @@ org.springframework.boot spring-boot-maven-plugin - com.ccsens.ht.HtApplication + com.ccsens.game.GameApplication diff --git a/game/src/main/java/com/ccsens/game/bean/dto/ClientDto.java b/game/src/main/java/com/ccsens/game/bean/dto/ClientDto.java index 1ba7daf3..99afb93d 100644 --- a/game/src/main/java/com/ccsens/game/bean/dto/ClientDto.java +++ b/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; + } } diff --git a/game/src/main/java/com/ccsens/game/service/ClientService.java b/game/src/main/java/com/ccsens/game/service/ClientService.java index e99da89b..d62897cf 100644 --- a/game/src/main/java/com/ccsens/game/service/ClientService.java +++ b/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 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; } } diff --git a/game/src/main/java/com/ccsens/game/util/GameConstant.java b/game/src/main/java/com/ccsens/game/util/GameConstant.java index 0a0015ff..47111930 100644 --- a/game/src/main/java/com/ccsens/game/util/GameConstant.java +++ b/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; + } } diff --git a/game/src/main/resources/application-test.yml b/game/src/main/resources/application-test.yml index c71c01cc..c8e7cbc9 100644 --- a/game/src/main/resources/application-test.yml +++ b/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 \ No newline at end of file diff --git a/game/src/main/resources/application.yml b/game/src/main/resources/application.yml index 5c2cd5c4..5889ff7f 100644 --- a/game/src/main/resources/application.yml +++ b/game/src/main/resources/application.yml @@ -1,4 +1,4 @@ spring: profiles: - active: dev - include: common, util-dev \ No newline at end of file + active: test + include: common, util-test \ No newline at end of file diff --git a/game/src/main/resources/druid-test.yml b/game/src/main/resources/druid-test.yml index bc9a6a82..79fab21f 100644 --- a/game/src/main/resources/druid-test.yml +++ b/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 \ No newline at end of file