Browse Source

分数

master
6 years ago
parent
commit
84ba41cd38
  1. 1
      game/src/main/java/com/ccsens/game/bean/dto/message/ChangeStatusMessageDto.java
  2. 5
      game/src/main/java/com/ccsens/game/bean/dto/message/GameMessageWithChangeStatusOut.java
  3. 35
      game/src/main/java/com/ccsens/game/service/ClientService.java
  4. 38
      game/src/main/java/com/ccsens/game/service/ScreenService.java
  5. 9
      game/src/main/java/com/ccsens/game/util/SendMsg.java
  6. 4
      game/src/main/resources/application.yml

1
game/src/main/java/com/ccsens/game/bean/dto/message/ChangeStatusMessageDto.java

@ -4,7 +4,6 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import java.util.List;
@Data @Data
public class ChangeStatusMessageDto { public class ChangeStatusMessageDto {

5
game/src/main/java/com/ccsens/game/bean/dto/message/GameMessageWithChangeStatusOut.java

@ -2,16 +2,13 @@ package com.ccsens.game.bean.dto.message;
import com.ccsens.util.WebConstant; import com.ccsens.util.WebConstant;
import lombok.Data; import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.util.List; import java.util.List;
@Data @Data
public class GameMessageWithChangeStatusOut extends BaseMessageDto{ public class GameMessageWithChangeStatusOut extends BaseMessageDto{
private Long userId; private Long userId;
@Setter @lombok.Data
@Getter
public static class Data{ public static class Data{
private Long recordId; private Long recordId;
private int gameStatus; private int gameStatus;

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

@ -120,38 +120,11 @@ public class ClientService implements IClientService {
} }
gameUserJoinDao.insertSelective(userJoin); gameUserJoinDao.insertSelective(userJoin);
// 3.更新redis(sort set key:分数 value:头像,姓名) // 3.更新redis(sort set key:分数 value:头像,姓名)
ClientDto.RedisUser user = ClientDto.RedisUser.getInstance(userJoin);
redisUtil.zsSet(GameConstant.generateGameKey(gameRecord.getId()), JSON.toJSONString(user), 0, GameConstant.REDIS_TIME);
//定时发送状态
ScheduledExecutorService executor = Executors.newScheduledThreadPool(10);
List<ClientDto.RedisUser> joins = new ArrayList<>();
joins.add(ClientDto.RedisUser.getInstance(userJoin));
//4.根据状态延时发送消息
if (prepare) {
//延时发送开始
long expiration = gameRecord.getStartTime() - System.currentTimeMillis();
sendMsg.sendMsg(executor, expiration, ()->{
try {
sendMsg.sendStatus(gameRecord, joins, GameConstant.GAME_PROCESSING);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
});
}
if (prepare || processing) { if (prepare || processing) {
//延时发送结束 ClientDto.RedisUser user = ClientDto.RedisUser.getInstance(userJoin);
long expiration = gameRecord.getStartTime() - System.currentTimeMillis(); redisUtil.zsSet(GameConstant.generateGameKey(gameRecord.getId()), JSON.toJSONString(user), 0, GameConstant.REDIS_TIME);
sendMsg.sendMsg(executor, expiration, ()->{
try {
sendMsg.sendStatus(gameRecord, joins, GameConstant.GAME_COMPLETED);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
});
} }
//5.返回状态
//5.返回状态
return joinResult(userJoin, gameRecord); return joinResult(userJoin, gameRecord);
} }
@ -320,7 +293,7 @@ public class ClientService implements IClientService {
if(CollectionUtil.isNotEmpty(userJoinList)){ if(CollectionUtil.isNotEmpty(userJoinList)){
GameUserJoin userJoin = userJoinList.get(0); GameUserJoin userJoin = userJoinList.get(0);
userJoin.setTimes(userJoin.getTimes() + 1); userJoin.setTimes(userJoin.getTimes() + 1);
userJoin.setScore((userJoin.getScore() + 1) * 100); userJoin.setScore(userJoin.getScore() + 100);
gameUserJoinDao.updateByPrimaryKeySelective(userJoin); gameUserJoinDao.updateByPrimaryKeySelective(userJoin);
//更新redis //更新redis
ClientDto.RedisUser user = ClientDto.RedisUser.getInstance(userJoin); ClientDto.RedisUser user = ClientDto.RedisUser.getInstance(userJoin);

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

@ -430,7 +430,7 @@ public class ScreenService implements IScreenService{
a++; a++;
} }
} }
over = a / userJoinList.size() * 100; over = a * 100 / userJoinList.size();
} }
completedData.setTotalTimes(totalTimes); completedData.setTotalTimes(totalTimes);
completedData.setTotalScore(totalScore); completedData.setTotalScore(totalScore);
@ -514,14 +514,27 @@ public class ScreenService implements IScreenService{
}); });
//更新用户游戏开始时间和结束时间 //更新用户游戏开始时间和结束时间
gameUserJoinDao.updateTimeBatch(gameRecord.getId(), gameRecord.getStartTime(), gameRecord.getEndTime()); gameUserJoinDao.updateTimeBatch(gameRecord.getId(), gameRecord.getStartTime(), gameRecord.getEndTime());
//查询游戏用户,通知游戏开始和结束
List<ClientDto.RedisUser> userJoins = sendMsg.getAllScore(gameRecord);
//推送客户端
pushClient(gameRecord, executor, userJoins);
//返回大屏开始时间 //返回大屏开始时间
ScreenVo.StartGame startGame = new ScreenVo.StartGame(); ScreenVo.StartGame startGame = new ScreenVo.StartGame();
startGame.setStartLocalTime(gameRecord.getStartTime() + moreTime); startGame.setStartLocalTime(gameRecord.getStartTime() + moreTime);
// 查询参与游戏的用户,添加到redis
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);
});
//推送客户端
pushClient(gameRecord, executor);
return startGame; return startGame;
} }
@ -531,12 +544,15 @@ public class ScreenService implements IScreenService{
* 推送客户端状态 * 推送客户端状态
* @param gameRecord * @param gameRecord
* @param executor * @param executor
* @param userJoins
*/ */
private void pushClient(GameRecord gameRecord, ScheduledExecutorService executor, List<ClientDto.RedisUser> userJoins) { private void pushClient(GameRecord gameRecord, ScheduledExecutorService executor) {
new Thread(()->{ new Thread(()->{
sendMsg.sendMsg(executor, 0, ()->{ sendMsg.sendMsg(executor, 0, ()->{
try { try {
//查询游戏用户,通知游戏倒计时
List<ClientDto.RedisUser> userJoins = sendMsg.getAllScore(gameRecord);
log.info("通知倒计时:{}", gameRecord);
sendMsg.sendStatus(gameRecord, userJoins, GameConstant.GAME_PREPARATION); sendMsg.sendStatus(gameRecord, userJoins, GameConstant.GAME_PREPARATION);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
e.printStackTrace(); e.printStackTrace();
@ -544,7 +560,10 @@ public class ScreenService implements IScreenService{
}); });
long startTime = gameRecord.getStartTime() - System.currentTimeMillis(); long startTime = gameRecord.getStartTime() - System.currentTimeMillis();
sendMsg.sendMsg(executor, startTime > 0 ? startTime : 0, ()->{ sendMsg.sendMsg(executor, startTime > 0 ? startTime : 0, ()->{
//查询游戏用户,通知游戏开始
List<ClientDto.RedisUser> userJoins = sendMsg.getAllScore(gameRecord);
try { try {
log.info("通知游戏进行中:{}", gameRecord);
sendMsg.sendStatus(gameRecord, userJoins, GameConstant.GAME_PROCESSING); sendMsg.sendStatus(gameRecord, userJoins, GameConstant.GAME_PROCESSING);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
e.printStackTrace(); e.printStackTrace();
@ -554,6 +573,9 @@ public class ScreenService implements IScreenService{
long endTime = gameRecord.getEndTime() - System.currentTimeMillis(); long endTime = gameRecord.getEndTime() - System.currentTimeMillis();
sendMsg.sendMsg(executor, endTime > 0 ? endTime : 0, ()->{ sendMsg.sendMsg(executor, endTime > 0 ? endTime : 0, ()->{
try { try {
//查询游戏用户,通知游戏结束
List<ClientDto.RedisUser> userJoins = sendMsg.getAllScore(gameRecord);
log.info("通知游戏结束:{}", gameRecord);
sendMsg.sendStatus(gameRecord, userJoins, GameConstant.GAME_COMPLETED); sendMsg.sendStatus(gameRecord, userJoins, GameConstant.GAME_COMPLETED);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
e.printStackTrace(); e.printStackTrace();
@ -561,6 +583,8 @@ public class ScreenService implements IScreenService{
}); });
}).start(); }).start();
} }

9
game/src/main/java/com/ccsens/game/util/SendMsg.java

@ -39,7 +39,11 @@ public class SendMsg {
private RedisUtil redisUtil; private RedisUtil redisUtil;
public void sendStatus(GameRecord gameRecord, List<ClientDto.RedisUser> userJoins, byte status) throws JsonProcessingException { public void sendStatus(GameRecord gameRecord, List<ClientDto.RedisUser> userJoins, byte status) throws JsonProcessingException {
log.info("userJoins:{}, status:{}", userJoins, status);
List<GameMessageWithChangeStatusOut> outs = new ArrayList<>(); List<GameMessageWithChangeStatusOut> outs = new ArrayList<>();
if (CollectionUtil.isEmpty(userJoins)) {
return;
}
userJoins.forEach(join -> { userJoins.forEach(join -> {
outs.add(getMsg(gameRecord, join, status)); outs.add(getMsg(gameRecord, join, status));
}); });
@ -60,8 +64,7 @@ public class SendMsg {
ChangeStatusMessageDto dtos = new ChangeStatusMessageDto(); ChangeStatusMessageDto dtos = new ChangeStatusMessageDto();
switch (gameStatus) { switch (gameStatus) {
case GameConstant.GAME_COMPLETED: case GameConstant.GAME_COMPLETED:
ChangeStatusMessageDto.CompletedData completedData = new ChangeStatusMessageDto.CompletedData();
ChangeStatusMessageDto.CompletedData completedData = gameUserJoinDao.gameCount(gameRecord.getId());
completedData.setTimes(join.getTimes()); completedData.setTimes(join.getTimes());
completedData.setScore(join.getScore()); completedData.setScore(join.getScore());
completedData.setSort(join.getSort()); completedData.setSort(join.getSort());
@ -108,11 +111,13 @@ public class SendMsg {
ClientDto.RedisUser user = JSON.parseObject((String)type.getValue(), ClientDto.RedisUser.class); ClientDto.RedisUser user = JSON.parseObject((String)type.getValue(), ClientDto.RedisUser.class);
user.setTimes(type.getScore().intValue()/100); user.setTimes(type.getScore().intValue()/100);
user.setScore(type.getScore().intValue()); user.setScore(type.getScore().intValue());
log.info("{} score: {}", type.getValue(), type.getScore());
Long sort = redisUtil.zsReverseRank(userKey, type.getValue()); Long sort = redisUtil.zsReverseRank(userKey, type.getValue());
user.setSort(sort == null ? 1 : sort.intValue()); user.setSort(sort == null ? 1 : sort.intValue());
//超过 //超过
Set<Object> overs = redisUtil.zsGet(userKey, 0, user.getScore() - 1); Set<Object> overs = redisUtil.zsGet(userKey, 0, user.getScore() - 1);
Long total = redisUtil.zsGetSize(userKey); Long total = redisUtil.zsGetSize(userKey);
log.info("超过用户数:{},总用户数:{}", overs == null ? 0 : overs.size(), total);
user.setOver(overs == null || total == null || total.intValue() == 0 ? 0 : overs.size()*100/total.intValue()); user.setOver(overs == null || total == null || total.intValue() == 0 ? 0 : overs.size()*100/total.intValue());
userJoins.add(user); userJoins.add(user);
}); });

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
Loading…
Cancel
Save