Browse Source

排行榜接口

master
nili 1 year ago
parent
commit
d030e01418
  1. 1
      game-dao/src/main/java/awesome/group/game/dao/bean/WeGameUser.java
  2. 9
      game-dao/src/main/java/awesome/group/game/dao/mapper/WeGameUserMapper.java
  3. 31
      game-service/src/main/java/awesome/group/game/service/WxService.java
  4. 15
      game-service/src/main/java/awesome/group/game/service/bo/RankList.java
  5. 10
      game-service/src/main/java/awesome/group/game/service/bo/UserInfo.java
  6. 24
      game-web/src/main/java/awesome/group/controller/LoginController.java
  7. 6
      game-web/src/main/resources/application-default.yml

1
game-dao/src/main/java/awesome/group/game/dao/bean/WeGameUser.java

@ -18,4 +18,5 @@ public class WeGameUser {
private String avatarUrl; private String avatarUrl;
private String unionId; private String unionId;
private String sessionKey; private String sessionKey;
private Integer score;
} }

9
game-dao/src/main/java/awesome/group/game/dao/mapper/WeGameUserMapper.java

@ -1,15 +1,24 @@
package awesome.group.game.dao.mapper; package awesome.group.game.dao.mapper;
import awesome.group.game.dao.bean.WeGameUser; import awesome.group.game.dao.bean.WeGameUser;
import awesome.group.game.dao.bean.WxUserInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update; import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface WeGameUserMapper extends BaseMapper<WeGameUser> { public interface WeGameUserMapper extends BaseMapper<WeGameUser> {
@Select("select * from we_game_user where app_id = #{appId} and open_id = #{openId}") @Select("select * from we_game_user where app_id = #{appId} and open_id = #{openId}")
WeGameUser query(@Param("appId")String appId, @Param("openId")String openId); WeGameUser query(@Param("appId")String appId, @Param("openId")String openId);
@Update("update we_game_user set session_key = #{sessionKey} where id = #{id}") @Update("update we_game_user set session_key = #{sessionKey} where id = #{id}")
int updateSessionKey(@Param("sessionKey")String sessionKey, @Param("id")Integer id); int updateSessionKey(@Param("sessionKey")String sessionKey, @Param("id")Integer id);
@Update("update we_game_user set score = #{score} where id = #{id}")
int updateScore(@Param("score")Integer score, @Param("id")Integer id);
@Select("select * from we_game_user where app_id = #{appId} order by score desc limit #{size}")
List<WeGameUser> queryTop(@Param("appId")String appId, @Param("size")Integer size);
} }

31
game-service/src/main/java/awesome/group/game/service/WxService.java

@ -2,6 +2,8 @@ package awesome.group.game.service;
import awesome.group.game.dao.bean.GameApp; import awesome.group.game.dao.bean.GameApp;
import awesome.group.game.service.bo.RankList;
import awesome.group.game.service.bo.UserInfo;
import awesome.group.game.dao.bean.WeGameUser; import awesome.group.game.dao.bean.WeGameUser;
import awesome.group.game.dao.bean.WxUserInfo; import awesome.group.game.dao.bean.WxUserInfo;
import awesome.group.game.dao.mapper.GameAppMapper; import awesome.group.game.dao.mapper.GameAppMapper;
@ -18,7 +20,9 @@ import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
@Service @Service
@ -62,6 +66,33 @@ public class WxService {
weGameUserMapper.updateById(u); weGameUserMapper.updateById(u);
} }
public void updateScore(Integer userId, Integer score) {
weGameUserMapper.updateScore(score, userId);
}
public RankList rankList(Integer userId, Integer size) {
WeGameUser my = weGameUserMapper.selectById(userId);
String appId = my.getAppId();
List<WeGameUser> data = weGameUserMapper.queryTop(appId, size);
List<UserInfo> list = new ArrayList<>();
RankList res = new RankList();
for(int i = 0; i < data.size(); i ++) {
WeGameUser x = data.get(i);
if (x.getId().equals(userId)) {
res.myRank = (i + 1) + "";
}
UserInfo info = new UserInfo();
info.avatarUrl = x.getAvatarUrl();
info.nickName = x.getNickname();
info.score = x.getScore();
list.add(info);
}
res.list = list;
res.myScore = my.getScore();
res.myNickName = my.getNickname();
res.myAvatarUrl = my.getAvatarUrl();
return res;
}
public boolean verify(Integer userId, String rawData, String signature) { public boolean verify(Integer userId, String rawData, String signature) {
WeGameUser u = weGameUserMapper.selectById(userId); WeGameUser u = weGameUserMapper.selectById(userId);

15
game-service/src/main/java/awesome/group/game/service/bo/RankList.java

@ -0,0 +1,15 @@
package awesome.group.game.service.bo;
import lombok.Data;
import java.util.List;
@Data
public class RankList {
public List<UserInfo> list;
public String myRank;
public Integer myScore;
public String myAvatarUrl;
public String myNickName;
}

10
game-service/src/main/java/awesome/group/game/service/bo/UserInfo.java

@ -0,0 +1,10 @@
package awesome.group.game.service.bo;
import lombok.Data;
@Data
public class UserInfo {
public String avatarUrl;
public String nickName;
public Integer score;
}

24
game-web/src/main/java/awesome/group/controller/LoginController.java

@ -2,13 +2,14 @@ package awesome.group.controller;
import awesome.group.RequestContext; import awesome.group.RequestContext;
import awesome.group.aop.RestApi; import awesome.group.aop.RestApi;
import awesome.group.game.service.bo.RankList;
import awesome.group.game.service.bo.UserInfo;
import awesome.group.game.service.WxService; import awesome.group.game.service.WxService;
import awesome.group.game.service.common.response.R; import awesome.group.game.service.common.response.R;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import java.util.List;
import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@RequestMapping("/api/game/auth") @RequestMapping("/api/game/auth")
@ -44,4 +45,19 @@ public class LoginController {
return new R<>(R.CODE_SUCCESS, "", null); return new R<>(R.CODE_SUCCESS, "", null);
} }
@PostMapping("/updateScore")
@RestApi
public R<Void> updateScore(@RequestParam Integer score) {
Integer userId = RequestContext.getUid();
wxService.updateScore(userId, score);
return new R<>(R.CODE_SUCCESS, "", null);
}
@GetMapping("/rankList")
@RestApi
public R<RankList> rankList(@RequestParam Integer size) {
Integer userId = RequestContext.getUid();
return new R<>(R.CODE_SUCCESS, "", wxService.rankList(userId, size));
}
} }

6
game-web/src/main/resources/application-default.yml

@ -12,9 +12,9 @@ spring:
datasource: datasource:
game: game:
type: com.zaxxer.hikari.HikariDataSource type: com.zaxxer.hikari.HikariDataSource
jdbcUrl: jdbc:mysql://localhost:3306/game?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&connectTimeout=300&socketTimeout=2000&serverTimezone=Hongkong&zeroDateTimeBehavior=convertToNull jdbcUrl: jdbc:mysql://47.108.240.181:3306/game?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&connectTimeout=300&socketTimeout=2000&serverTimezone=Hongkong&zeroDateTimeBehavior=convertToNull
username: root username: game
password: password: Vd12luSjmwAXuwDH
connectionTimeout: 300 connectionTimeout: 300
maximumPoolSize: 5 maximumPoolSize: 5
minimumIdle: 1 minimumIdle: 1

Loading…
Cancel
Save