|
|
@@ -2,6 +2,9 @@ package com.izouma.yags.service;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.izouma.yags.camp.api.Battle;
|
|
|
+import com.izouma.yags.camp.api.CampApiService;
|
|
|
+import com.izouma.yags.camp.api.QueryBattle;
|
|
|
import com.izouma.yags.config.Constants;
|
|
|
import com.izouma.yags.domain.*;
|
|
|
import com.izouma.yags.dto.PageQuery;
|
|
|
@@ -22,16 +25,19 @@ import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageImpl;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
|
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
+import javax.transaction.Transactional;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.net.URL;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneId;
|
|
|
import java.util.*;
|
|
|
import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -40,16 +46,18 @@ import java.util.stream.Collectors;
|
|
|
@AllArgsConstructor
|
|
|
public class RoomService {
|
|
|
|
|
|
- private RoomRepo roomRepo;
|
|
|
- private GameRepo gameRepo;
|
|
|
- private GameModeRepo gameModeRepo;
|
|
|
- private GameMapRepo gameMapRepo;
|
|
|
- private UserTicketRepo userTicketRepo;
|
|
|
- private JoinRoomRepo joinRoomRepo;
|
|
|
- private UserRepo userRepo;
|
|
|
- private UserBalanceService userBalanceService;
|
|
|
- private BindGameRepo bindGameRepo;
|
|
|
- private TicketConfigRepo ticketConfigRepo;
|
|
|
+ private RoomRepo roomRepo;
|
|
|
+ private GameRepo gameRepo;
|
|
|
+ private GameModeRepo gameModeRepo;
|
|
|
+ private GameMapRepo gameMapRepo;
|
|
|
+ private UserTicketRepo userTicketRepo;
|
|
|
+ private JoinRoomRepo joinRoomRepo;
|
|
|
+ private UserRepo userRepo;
|
|
|
+ private UserBalanceService userBalanceService;
|
|
|
+ private BindGameRepo bindGameRepo;
|
|
|
+ private TicketConfigRepo ticketConfigRepo;
|
|
|
+ private CampApiService campApiService;
|
|
|
+ private ThreadPoolTaskExecutor taskExecutor;
|
|
|
|
|
|
public Page<Room> all(PageQuery pageQuery) {
|
|
|
return roomRepo.findAll(JpaUtils.toSpecification(pageQuery, Room.class), JpaUtils.toPageRequest(pageQuery));
|
|
|
@@ -124,6 +132,8 @@ public class RoomService {
|
|
|
public JoinRoom joinRoom(Long userId, Long roomId, Long ticketId, String password) {
|
|
|
Room room = roomRepo.findById(roomId).orElseThrow(new BusinessException("房间无记录"));
|
|
|
UserTicket ticket = userTicketRepo.findById(ticketId).orElseThrow(new BusinessException("通行证无记录"));
|
|
|
+ BindGame bindGame = bindGameRepo.findByUserIdAndGameIdAndZoneAndActive(userId, room.getGameId(), room.getZone(), true)
|
|
|
+ .orElseThrow(new BusinessException("请先绑定游戏"));
|
|
|
if (room.getStatus() != RoomStatus.WAITING) {
|
|
|
throw new BusinessException("报名已结束");
|
|
|
}
|
|
|
@@ -167,6 +177,9 @@ public class RoomService {
|
|
|
.joinAt(LocalDateTime.now())
|
|
|
.status(JoinRoomStatus.WAITING)
|
|
|
.team(team)
|
|
|
+ .kohcampId(bindGame.getRongYaoRole().getUserId() + "")
|
|
|
+ .kohcampRoleId(bindGame.getRongYaoRole().getRoleId() + "")
|
|
|
+ .kohcampRoleName(bindGame.getRongYaoRole().getRoleName())
|
|
|
.build());
|
|
|
ticket.setUsed(true);
|
|
|
ticket.setUsedAt(LocalDateTime.now());
|
|
|
@@ -376,6 +389,10 @@ public class RoomService {
|
|
|
room.setEndAt(LocalDateTime.now());
|
|
|
}
|
|
|
roomRepo.save(room);
|
|
|
+
|
|
|
+ taskExecutor.execute(() -> {
|
|
|
+ apiJudge(room);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
public RoomDetail detail(Long id, boolean players) {
|
|
|
@@ -525,4 +542,56 @@ public class RoomService {
|
|
|
.map(JoinRoom::getUserId).collect(Collectors.toList()));
|
|
|
roomRepo.save(room);
|
|
|
}
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public void apiJudge(Room room) {
|
|
|
+ if (!(room.getStatus() == RoomStatus.AUDIT)) return;
|
|
|
+ List<JoinRoom> joinRoomList = joinRoomRepo.findByRoomIdAndStatusNotIn(room.getId(),
|
|
|
+ Arrays.asList(JoinRoomStatus.KICKED_OUT, JoinRoomStatus.QUIT));
|
|
|
+ if (room.getMaxPlayerNum() != 2) return;
|
|
|
+ if (joinRoomList.size() != 2) return;
|
|
|
+
|
|
|
+ QueryBattle queryBattle1, queryBattle2;
|
|
|
+ try {
|
|
|
+ queryBattle1 = campApiService.queryBattle(joinRoomList.get(0).getKohcampRoleId());
|
|
|
+ queryBattle2 = campApiService.queryBattle(joinRoomList.get(1).getKohcampRoleId());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (queryBattle1 == null || queryBattle2 == null) return;
|
|
|
+
|
|
|
+ long time = room.getCreatedAt().atZone(ZoneId.systemDefault()).toEpochSecond();
|
|
|
+ List<Battle> list1 = queryBattle1.getList().stream()
|
|
|
+ .filter(q -> Long.parseLong(q.getGameSeq()) > time).collect(Collectors.toList());
|
|
|
+ List<Battle> list2 = queryBattle2.getList().stream()
|
|
|
+ .filter(q -> Long.parseLong(q.getGameSeq()) > time).collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<Battle> commonBattles = list1.stream()
|
|
|
+ .filter(b -> list2.stream().anyMatch(b2 -> b2.getGameSeq().equals(b.getGameSeq())))
|
|
|
+ .sorted((a, b) -> ((int) (Long.parseLong(b.getGameSeq()) - Long.parseLong(a.getGameSeq()))))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ if (commonBattles.size() == 0) return;
|
|
|
+
|
|
|
+ Battle battle = commonBattles.get(0);
|
|
|
+ if (!(battle.getGameresult() == 1 || battle.getGameresult() == 2)) return;
|
|
|
+
|
|
|
+ String winTeam = battle.getGameresult() == 1 ? joinRoomList.get(0).getTeam() : joinRoomList.get(1).getTeam();
|
|
|
+ for (JoinRoom joinRoom : joinRoomList) {
|
|
|
+ joinRoom.setFinishAt(LocalDateTime.now());
|
|
|
+ if (joinRoom.getTeam().equals(winTeam)) {
|
|
|
+ joinRoom.setStatus(JoinRoomStatus.WIN);
|
|
|
+ userBalanceService.modifyBalance(joinRoom.getUserId(), room.getBonus(), Constants.BalanceDesc.BONUS);
|
|
|
+ } else {
|
|
|
+ joinRoom.setStatus(JoinRoomStatus.LOSE);
|
|
|
+ }
|
|
|
+ joinRoomRepo.save(joinRoom);
|
|
|
+ }
|
|
|
+ room.setStatus(RoomStatus.FINISH);
|
|
|
+ room.setFinishAt(LocalDateTime.now());
|
|
|
+ room.setWinner(joinRoomList.stream().filter(j -> j.getTeam().equals(winTeam))
|
|
|
+ .map(JoinRoom::getUserId).collect(Collectors.toList()));
|
|
|
+ roomRepo.save(room);
|
|
|
+ }
|
|
|
}
|