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 lombok.Data;
import java.util.List;
@Data
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 lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Data
public class GameMessageWithChangeStatusOut extends BaseMessageDto{
private Long userId;
@Setter
@Getter
@lombok.Data
public static class Data{
private Long recordId;
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);
// 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) {
//延时发送结束
long expiration = gameRecord.getStartTime() - System.currentTimeMillis();
sendMsg.sendMsg(executor, expiration, ()->{
try {
sendMsg.sendStatus(gameRecord, joins, GameConstant.GAME_COMPLETED);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
});
ClientDto.RedisUser user = ClientDto.RedisUser.getInstance(userJoin);
redisUtil.zsSet(GameConstant.generateGameKey(gameRecord.getId()), JSON.toJSONString(user), 0, GameConstant.REDIS_TIME);
}
//5.返回状态
//5.返回状态
return joinResult(userJoin, gameRecord);
}
@ -320,7 +293,7 @@ public class ClientService implements IClientService {
if(CollectionUtil.isNotEmpty(userJoinList)){
GameUserJoin userJoin = userJoinList.get(0);
userJoin.setTimes(userJoin.getTimes() + 1);
userJoin.setScore((userJoin.getScore() + 1) * 100);
userJoin.setScore(userJoin.getScore() + 100);
gameUserJoinDao.updateByPrimaryKeySelective(userJoin);
//更新redis
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++;
}
}
over = a / userJoinList.size() * 100;
over = a * 100 / userJoinList.size();
}
completedData.setTotalTimes(totalTimes);
completedData.setTotalScore(totalScore);
@ -514,14 +514,27 @@ public class ScreenService implements IScreenService{
});
//更新用户游戏开始时间和结束时间
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();
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;
}
@ -531,12 +544,15 @@ public class ScreenService implements IScreenService{
* 推送客户端状态
* @param gameRecord
* @param executor
* @param userJoins
*/
private void pushClient(GameRecord gameRecord, ScheduledExecutorService executor, List<ClientDto.RedisUser> userJoins) {
private void pushClient(GameRecord gameRecord, ScheduledExecutorService executor) {
new Thread(()->{
sendMsg.sendMsg(executor, 0, ()->{
try {
//查询游戏用户,通知游戏倒计时
List<ClientDto.RedisUser> userJoins = sendMsg.getAllScore(gameRecord);
log.info("通知倒计时:{}", gameRecord);
sendMsg.sendStatus(gameRecord, userJoins, GameConstant.GAME_PREPARATION);
} catch (JsonProcessingException e) {
e.printStackTrace();
@ -544,7 +560,10 @@ public class ScreenService implements IScreenService{
});
long startTime = gameRecord.getStartTime() - System.currentTimeMillis();
sendMsg.sendMsg(executor, startTime > 0 ? startTime : 0, ()->{
//查询游戏用户,通知游戏开始
List<ClientDto.RedisUser> userJoins = sendMsg.getAllScore(gameRecord);
try {
log.info("通知游戏进行中:{}", gameRecord);
sendMsg.sendStatus(gameRecord, userJoins, GameConstant.GAME_PROCESSING);
} catch (JsonProcessingException e) {
e.printStackTrace();
@ -554,6 +573,9 @@ public class ScreenService implements IScreenService{
long endTime = gameRecord.getEndTime() - System.currentTimeMillis();
sendMsg.sendMsg(executor, endTime > 0 ? endTime : 0, ()->{
try {
//查询游戏用户,通知游戏结束
List<ClientDto.RedisUser> userJoins = sendMsg.getAllScore(gameRecord);
log.info("通知游戏结束:{}", gameRecord);
sendMsg.sendStatus(gameRecord, userJoins, GameConstant.GAME_COMPLETED);
} catch (JsonProcessingException e) {
e.printStackTrace();
@ -561,6 +583,8 @@ public class ScreenService implements IScreenService{
});
}).start();
}

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

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

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