feat(pk): 支持平局双方加分逻辑
- PkResultPointsDTO 新增 draw 字段标识平局 - 服务层识别平局后读取配置 PK平局增加积分 并给双方加相同积分 - 新增 grantDrawPoints 方法处理平局场景 - 补充单元测试验证平局加分及失败场景
This commit is contained in:
BIN
.idea/.cache/.Apifox_Helper/.toolWindow.db
generated
BIN
.idea/.cache/.Apifox_Helper/.toolWindow.db
generated
Binary file not shown.
@@ -6,4 +6,5 @@ import lombok.Data;
|
||||
public class PkResultPointsDTO {
|
||||
private Integer winnerUserId;
|
||||
private Integer loserUserId;
|
||||
private Boolean draw;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ public class PkResultPointServiceImpl implements PkResultPointService {
|
||||
|
||||
private static final String PK_WIN_COIN_CONFIG_NAME = "PK胜利增加积分";
|
||||
private static final String PK_LOSE_COIN_CONFIG_NAME = "PK失败增加积分";
|
||||
private static final String PK_DRAW_COIN_CONFIG_NAME = "PK平局增加积分";
|
||||
private static final int COIN_RECORD_ADD = 1;
|
||||
|
||||
private final UserDao userDao;
|
||||
@@ -40,6 +41,9 @@ public class PkResultPointServiceImpl implements PkResultPointService {
|
||||
public String grantPkResultPoints(PkResultPointsDTO request) {
|
||||
validateRequest(request);
|
||||
int now = (int) epochSecondProvider.nowEpochSecond();
|
||||
if (isDraw(request)) {
|
||||
return grantDrawPoints(request, now);
|
||||
}
|
||||
int winPoints = parsePositivePoints(PK_WIN_COIN_CONFIG_NAME);
|
||||
int losePoints = parsePositivePoints(PK_LOSE_COIN_CONFIG_NAME);
|
||||
|
||||
@@ -48,6 +52,13 @@ public class PkResultPointServiceImpl implements PkResultPointService {
|
||||
return String.format("操作成功,胜利方增加%d积分,失败方增加%d积分", winPoints, losePoints);
|
||||
}
|
||||
|
||||
private String grantDrawPoints(PkResultPointsDTO request, int now) {
|
||||
int drawPoints = parsePositivePoints(PK_DRAW_COIN_CONFIG_NAME);
|
||||
grantPoints(request.getWinnerUserId(), drawPoints, PK_DRAW_COIN_CONFIG_NAME, now);
|
||||
grantPoints(request.getLoserUserId(), drawPoints, PK_DRAW_COIN_CONFIG_NAME, now);
|
||||
return String.format("操作成功,平局双方各增加%d积分", drawPoints);
|
||||
}
|
||||
|
||||
private static void validateRequest(PkResultPointsDTO request) {
|
||||
if (request == null || request.getWinnerUserId() == null || request.getLoserUserId() == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
|
||||
@@ -57,6 +68,10 @@ public class PkResultPointServiceImpl implements PkResultPointService {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isDraw(PkResultPointsDTO request) {
|
||||
return Boolean.TRUE.equals(request.getDraw());
|
||||
}
|
||||
|
||||
private int parsePositivePoints(String configName) {
|
||||
String value = functionConfigProvider.getValue(configName);
|
||||
if (value == null) {
|
||||
@@ -64,7 +79,7 @@ public class PkResultPointServiceImpl implements PkResultPointService {
|
||||
}
|
||||
try {
|
||||
int points = Integer.parseInt(value);
|
||||
if (points <= 0) {
|
||||
if (points < 0) {
|
||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR, configName + "配置错误");
|
||||
}
|
||||
return points;
|
||||
|
||||
@@ -51,6 +51,41 @@ class PkResultPointServiceImplTests {
|
||||
Assertions.assertEquals(Integer.valueOf(1234), captor.getAllValues().get(1).getTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldGrantDrawPointsToBothUsers() {
|
||||
UserDao userDao = mock(UserDao.class);
|
||||
CoinRecordsDao coinRecordsDao = mock(CoinRecordsDao.class);
|
||||
FunctionConfigProvider functionConfigProvider = mock(FunctionConfigProvider.class);
|
||||
EpochSecondProvider epochSecondProvider = () -> 1_234L;
|
||||
|
||||
when(functionConfigProvider.getValue("PK平局增加积分")).thenReturn("5");
|
||||
when(userDao.increasePoints(1001, 5)).thenReturn(1);
|
||||
when(userDao.increasePoints(1002, 5)).thenReturn(1);
|
||||
when(coinRecordsDao.insert(any())).thenReturn(1);
|
||||
|
||||
PkResultPointServiceImpl service = new PkResultPointServiceImpl(
|
||||
userDao, coinRecordsDao, functionConfigProvider, epochSecondProvider
|
||||
);
|
||||
PkResultPointsDTO request = new PkResultPointsDTO();
|
||||
request.setWinnerUserId(1001);
|
||||
request.setLoserUserId(1002);
|
||||
request.setDraw(true);
|
||||
|
||||
String result = service.grantPkResultPoints(request);
|
||||
|
||||
Assertions.assertEquals("操作成功,平局双方各增加5积分", result);
|
||||
ArgumentCaptor<CoinRecords> captor = ArgumentCaptor.forClass(CoinRecords.class);
|
||||
verify(coinRecordsDao, times(2)).insert(captor.capture());
|
||||
Assertions.assertEquals("PK平局增加积分", captor.getAllValues().get(0).getInfo());
|
||||
Assertions.assertEquals(Integer.valueOf(1001), captor.getAllValues().get(0).getUserId());
|
||||
Assertions.assertEquals(Integer.valueOf(5), captor.getAllValues().get(0).getNumber());
|
||||
Assertions.assertEquals("PK平局增加积分", captor.getAllValues().get(1).getInfo());
|
||||
Assertions.assertEquals(Integer.valueOf(1002), captor.getAllValues().get(1).getUserId());
|
||||
Assertions.assertEquals(Integer.valueOf(5), captor.getAllValues().get(1).getNumber());
|
||||
Assertions.assertEquals(Integer.valueOf(1234), captor.getAllValues().get(0).getTime());
|
||||
Assertions.assertEquals(Integer.valueOf(1234), captor.getAllValues().get(1).getTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFailWhenWinnerAndLoserAreSameUser() {
|
||||
UserDao userDao = mock(UserDao.class);
|
||||
|
||||
Reference in New Issue
Block a user