전투시스템 중복 시간체크

전투시스템 이벤트 아이디 dynamodb 기준관리
This commit is contained in:
2025-02-18 15:54:26 +09:00
parent 78cc70488a
commit 255c2e13c9
13 changed files with 230 additions and 48 deletions

View File

@@ -22,5 +22,6 @@ public interface BattleMapper {
int updateStatusBattleEvent(Map map);
List<BattleEvent> getCheckBattleEventList(BattleEventRequest battleEventRequest);
List<BattleEvent> getScheduleBattleEventList();
}

View File

@@ -20,6 +20,8 @@ public class BattleEvent {
private Integer rowNum;
@JsonProperty("group_id")
private String groupId;
@JsonProperty("event_id")
private Integer eventId;
@JsonProperty("event_name")
private String eventName;
@JsonProperty("repeat_type")
@@ -64,7 +66,7 @@ public class BattleEvent {
public enum BATTLE_STATUS {
WAIT,
REGISTER,
CANCEL,
STOP,
END,
FAIL,
RUNNING

View File

@@ -20,6 +20,8 @@ public class BattleEventRequest {
private Long id;
@JsonProperty("group_id")
private String groupId;
@JsonProperty("event_id")
private Integer eventId;
@JsonProperty("event_name")
private String eventName;
@JsonProperty("repeat_type")

View File

@@ -26,8 +26,6 @@ import org.springframework.web.bind.annotation.RequestParam;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -126,12 +124,17 @@ public class BattleEventService {
battleEventRequest.setEventOperationTime(operation_time);
// int is_time = battleMapper.chkTimeOver(battleEventRequest);
// if(is_time > 0){
// return BattleEventResponse.builder()
// .status(CommonCode.ERROR.getHttpStatus())
// .result(ErrorCode.ERROR_BATTLE_EVENT_TIME_OVER.toString())
// .build();
// }
List<BattleEvent> existingList = battleMapper.getCheckBattleEventList(battleEventRequest);
boolean isTime = isTimeOverlapping(existingList, battleEventRequest);
if(isTime){
return BattleEventResponse.builder()
.status(CommonCode.ERROR.getHttpStatus())
.result(ErrorCode.ERROR_BATTLE_EVENT_TIME_OVER.toString())
.build();
}
int next_event_id = dynamodbBattleEventService.getEventId() + 1;
battleEventRequest.setEventId(next_event_id);
int result = battleMapper.postBattleEvent(battleEventRequest);
log.info("AdminToolDB BattleEvent Save: {}", battleEventRequest);
@@ -171,10 +174,10 @@ public class BattleEventService {
BattleEvent before_info = battleMapper.getBattleEventDetail(id);
if(!before_info.getStatus().equals(LandAuction.AUCTION_STATUS.WAIT) && !before_info.getStatus().equals(LandAuction.AUCTION_STATUS.RESV_START)){
if(!before_info.getStatus().equals(BattleEvent.BATTLE_STATUS.STOP)){
return BattleEventResponse.builder()
.status(CommonCode.ERROR.getHttpStatus())
.result(ErrorCode.ERROR_AUCTION_STATUS_IMPOSSIBLE.toString())
.result(ErrorCode.ERROR_BATTLE_EVENT_STATUS_IMPOSSIBLE.toString())
.build();
}
@@ -284,6 +287,18 @@ public class BattleEventService {
int total_time = round_time + ((round_count - 1) * (round_time + round_wait_time)) + result_wait_time + server_wait_time;
return total_time;
return total_time; // 초
}
private boolean isTimeOverlapping(List<BattleEvent> existingList, BattleEventRequest battleEventRequest){
LocalTime newStartTime = battleEventRequest.getEventStartDt().toLocalTime();
LocalTime newEndTime = newStartTime.plusSeconds(battleEventRequest.getEventOperationTime());
return existingList.stream().anyMatch(schedule -> {
LocalTime existingStartTime = schedule.getEventStartDt().toLocalTime();
LocalTime existingEndTime = existingStartTime.plusSeconds(schedule.getEventOperationTime());
return !existingStartTime.isAfter(newEndTime) && !newStartTime.isAfter(existingEndTime);
});
}
}

View File

@@ -20,7 +20,7 @@ import java.util.HashSet;
public class BattleEventAttrib extends DynamoDBAttribBase {
@JsonProperty("event_id")
private long eventId;
private Integer eventId;
@JsonProperty("is_active")
private boolean isActive;

View File

@@ -70,6 +70,11 @@ public abstract class BaseDynamoDBRepository<T> implements DynamoDBRepository<T>
@Override
public List<T> findByPrefix(String partitionKey, String sortKeyPrefix) {
return operations.getItemsByPrefix(partitionKey, sortKeyPrefix, entityClass);
if(sortKeyPrefix.isEmpty()){
return operations.getItemsByPrefix(partitionKey, entityClass);
}
else{
return operations.getItemsByPrefix(partitionKey, sortKeyPrefix, entityClass);
}
}
}

View File

@@ -4,5 +4,7 @@ import com.caliverse.admin.domain.request.BattleEventRequest;
import com.caliverse.admin.dynamodb.domain.doc.BattleEventDoc;
public interface BattleEventRepository extends DynamoDBRepository<BattleEventDoc> {
int findEventId();
void insert(BattleEventRequest battleEventRequest);
void updateStop(BattleEventRequest battleEventRequest);
}

View File

@@ -4,7 +4,9 @@ import com.caliverse.admin.domain.entity.BattleEvent;
import com.caliverse.admin.domain.entity.HISTORYTYPE;
import com.caliverse.admin.domain.request.BattleEventRequest;
import com.caliverse.admin.dynamodb.domain.atrrib.BattleEventAttrib;
import com.caliverse.admin.dynamodb.domain.atrrib.LandAuctionRegistryAttrib;
import com.caliverse.admin.dynamodb.domain.doc.BattleEventDoc;
import com.caliverse.admin.dynamodb.domain.doc.LandAuctionRegistryDoc;
import com.caliverse.admin.dynamodb.entity.EDayOfWeekType;
import com.caliverse.admin.dynamodb.entity.EOncePeriodRangeType;
import com.caliverse.admin.dynamodb.repository.BaseDynamoDBRepository;
@@ -19,9 +21,12 @@ import com.caliverse.admin.history.service.DynamodbHistoryLogService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.enhanced.dynamodb.Key;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import static com.caliverse.admin.global.common.utils.CommonUtils.convertUTCDate;
import static com.caliverse.admin.global.common.utils.DateUtils.stringToISODateTime;
@@ -33,6 +38,27 @@ public class BattleEventRepositoryImpl extends BaseDynamoDBRepository<BattleEven
super(operations, BattleEventDoc.class, dynamodbHistoryLogService, objectMapper);
}
@Override
public int findEventId() {
List<BattleEventDoc> docs = findByPrefix(
DynamoDBConstants.PK_KEY_BATTLE_EVENT,
""
);
if (docs.isEmpty()) return 0;
BattleEventDoc latestDoc = docs.stream().max(Comparator.comparing(BattleEventDoc::getSK))
.orElse(null);
try {
BattleEventAttrib attrib = objectMapper.readValue(latestDoc.getAttribValue(), BattleEventAttrib.class);
return attrib.getEventId();
}catch (Exception e){
log.error(e.getMessage());
throw new RestApiException(CommonCode.ERROR.getHttpStatus(), ErrorCode.DYNAMODB_CONNECTION_ERROR.getMessage());
}
}
@Override
public void insert(BattleEventRequest battleEventRequest) {
LocalDateTime nowDate = LocalDateTime.now();
@@ -48,7 +74,7 @@ public class BattleEventRepositoryImpl extends BaseDynamoDBRepository<BattleEven
try {
BattleEventAttrib attrib = new BattleEventAttrib();
attrib.setAttribType(DynamoDBConstants.ATTRIB_BATTLE_EVENT);
attrib.setEventId(battleEventRequest.getId());
attrib.setEventId(battleEventRequest.getEventId());
attrib.setStartDay(stringToISODateTime(start_dt.toLocalDate().atStartOfDay()));
attrib.setStartHour(start_dt.getHour());
attrib.setStartMin(start_dt.getMinute());
@@ -60,6 +86,7 @@ public class BattleEventRepositoryImpl extends BaseDynamoDBRepository<BattleEven
attrib.setRewardGroupId(battleEventRequest.getRewardGroupId());
attrib.setHotTime(battleEventRequest.getHotTime());
attrib.setRoundCount(battleEventRequest.getRoundCount());
attrib.setActive(true);
BattleEventDoc doc = new BattleEventDoc();
doc.setPK(DynamoDBConstants.PK_KEY_BATTLE_EVENT);
@@ -86,6 +113,43 @@ public class BattleEventRepositoryImpl extends BaseDynamoDBRepository<BattleEven
}
}
@Override
public void updateStop(BattleEventRequest battleEventRequest) {
try {
Key key = Key.builder()
.partitionValue(DynamoDBConstants.PK_KEY_BATTLE_EVENT)
.sortValue(battleEventRequest.getId())
.build();
BattleEventDoc beforeDoc = findById(key);
if (beforeDoc != null) {
BattleEventDoc afterDoc = deepCopy(beforeDoc, BattleEventDoc.class);
BattleEventAttrib attrib = objectMapper.readValue(afterDoc.getAttribValue(), BattleEventAttrib.class);
attrib.setActive(false);
afterDoc.setAttribValue(objectMapper.writeValueAsString(attrib));
afterDoc.setUpdatedDateTime(CommonUtils.convertUTCDate(LocalDateTime.now()));
update(afterDoc);
log.info("BattleEventDoc Update Success: {}", objectMapper.writeValueAsString(afterDoc));
dynamodbHistoryLogService.updateHistoryLog(
HISTORYTYPE.BATTLE_EVENT_DELETE,
HISTORYTYPE.BATTLE_EVENT_DELETE.name(),
beforeDoc,
afterDoc,
CommonUtils.getAdmin().getEmail(),
CommonUtils.getClientIp()
);
}
}catch (Exception e){
throw new RestApiException(CommonCode.ERROR.getHttpStatus(), ErrorCode.DYNAMODB_CONNECTION_ERROR.getMessage());
}
}
private EOncePeriodRangeType getOncePeriodRangeType(BattleEvent.BATTLE_REPEAT_TYPE repeatType) {
return switch (repeatType) {
case DAY -> EOncePeriodRangeType.Daily;

View File

@@ -227,4 +227,17 @@ public class DynamoDBOperations {
.toList();
}
public <T> List<T> getItemsByPrefix(String partitionKey, Class<T> itemClass) {
DynamoDbTable<T> table = getTable(itemClass);
QueryConditional queryConditional = QueryConditional.keyEqualTo(b -> b
.partitionValue(partitionKey)
.build());
return table.query(r -> r.queryConditional(queryConditional))
.items()
.stream()
.toList();
}
}

View File

@@ -18,4 +18,12 @@ public class DynamodbBattleEventService {
battleEventRepository.insert(battleEventRequest);
}
public int getEventId(){
return battleEventRepository.findEventId();
}
public void updateStopBattleEvent(BattleEventRequest battleEventRequest) {
}
}

View File

@@ -59,6 +59,7 @@ public enum ErrorCode {
//Battle
ERROR_BATTLE_EVENT_TIME_OVER("해당 시간에 속하는 이벤트가 존재합니다."),
ERROR_BATTLE_EVENT_STATUS_IMPOSSIBLE("수정할 수 없는 이벤트상태입니다."),
//------------------------------------------------------------------------------------------------------------------------------
// DyanamoDB

View File

@@ -83,6 +83,7 @@ public class DynamicScheduler {
return;
}
// 비정상종료일때가 있다고 상시체크로 해달라고 함(23.02.18)
// 경매중일때 경매 종료시간이 되지않았다면 체크하지 않는다
// if(auction_status.equals(LandAuction.AUCTION_STATUS.AUCTION_START) && nowDate.isBefore(auctionEndDate)){
// return;

View File

@@ -5,6 +5,7 @@
<id property="id" column="id"/>
<result property="rowNum" column="row_num"/>
<result property="groupId" column="group_id"/>
<result property="eventId" column="event_id"/>
<result property="eventName" column="event_name"/>
<result property="repeatType" column="repeat_type"/>
<result property="eventOperationTime" column="event_operation_time"/>
@@ -32,6 +33,7 @@
SELECT
a.id
, a.group_id
, a.event_id
, a.event_name
, a.repeat_type
, a.event_operation_time
@@ -51,33 +53,35 @@
WHERE 1 = 1
<choose>
<when test="land_type == 'NAME' or land_type == 'name' ">
<if test="land_data != null and land_data != ''">
AND a.land_name LIKE CONCAT('%',#{land_data},'%')
<when test="search_type == 'NAME' or search_type == 'name' ">
<if test="search_data != null and search_data != ''">
AND a.event_name LIKE CONCAT('%',#{search_data},'%')
</if>
</when>
<otherwise>
<if test="land_data != null and land_data != ''">
AND a.land_id = #{land_data}
<if test="search_data != null and search_data != ''">
AND a.event_id = #{search_data}
</if>
</otherwise>
</choose>
<choose>
<when test="user_type == 'NAME' or user_type == 'name' ">
<if test="user_data != null and user_data != ''">
AND a.bidder_nickname LIKE CONCAT('%',#{user_data},'%')
</if>
</when>
<otherwise>
<if test="user_data != null and user_data != ''">
AND a.bidder_guid = #{user_data}
</if>
</otherwise>
</choose>
<if test="land_size != null and land_size != ''">
<if test="config_id != null and config_id != ''">
<choose>
<when test="land_size != 'ALL' ">
AND a.land_size = #{land_size}
<when test="config_id != 'ALL' ">
AND a.config_id = #{config_id}
</when>
</choose>
</if>
<if test="reward_id != null and reward_id != ''">
<choose>
<when test="reward_id != 'ALL' ">
AND a.reward_id = #{reward_id}
</when>
</choose>
</if>
<if test="repeat_type != null and repeat_type != ''">
<choose>
<when test="repeat_type != 'ALL' ">
AND a.repeat_type = #{repeat_type}
</when>
</choose>
</if>
@@ -88,9 +92,23 @@
</when>
</choose>
</if>
<if test="hot_time != null and hot_time != ''">
<choose>
<when test="hot_time != 'ALL' ">
AND a.hot_time = #{hot_time}
</when>
</choose>
</if>
<if test="round_count != null and round_count != ''">
<choose>
<when test="round_count != 'ALL' ">
AND a.round_count = #{round_count}
</when>
</choose>
</if>
<if test="start_dt != null and start_dt != '' and end_dt !=null and end_dt!= ''">
AND a.auction_start_dt &gt;= #{start_dt, jdbcType=TIMESTAMP}
AND a.auction_end_dt &lt;= #{end_dt, jdbcType=TIMESTAMP}
AND a.event_start_dt &gt;= #{start_dt, jdbcType=TIMESTAMP}
AND a.event_end_dt &lt;= #{end_dt, jdbcType=TIMESTAMP}
</if>
GROUP BY a.id
ORDER BY status
@@ -137,19 +155,67 @@
</if>
</select>
<select id="getCheckBattleEventList" resultMap="BattleEventResultMap">
SELECT id
, event_id
, repeat_type
, event_operation_time
, event_start_dt
, event_end_dt
FROM battle_event
WHERE status NOT IN ('END', 'FAIL')
AND (
/* NONE 타입인 경우 */
(repeat_type = 'NONE' AND DATE (event_start_dt) BETWEEN
DATE (#{eventStartDt})
AND DATE (#{eventEndDt}))
OR
/* DAY 타입인 경우 - 기간이 조금이라도 겹치면 가져옴 */
(repeat_type = 'DAY'
AND DATE (event_start_dt) &lt;= DATE (#{eventEndDt})
AND DATE (event_end_dt) &gt;= DATE (#{eventStartDt}))
OR
/* 요일 타입인 경우 - 기간이 겹치고 같은 요일이 있는 경우만 */
(repeat_type = #{repeatType}
AND DATE (event_start_dt) &lt;= DATE (#{eventEndDt})
AND DATE (event_end_dt) &gt;= DATE (#{eventStartDt})
)
)
</select>
<select id="chkTimeOver" parameterType="com.caliverse.admin.domain.request.BattleEventRequest" resultType="integer">
SELECT COUNT(*)
FROM battle_event
WHERE
-- 시간 중복 체크
TIME(#{eventStartTime}) &lt; ADDTIME(TIME(event_start_dt), SEC_TO_TIME(event_operation_time))
AND ADDTIME(TIME(#{eventStartTime}), SEC_TO_TIME(#{eventOperationTime})) > TIME(event_start_dt)
-- 날짜 범위 중복 체크
WHERE status = 'REGISTER'
AND (
(DATE(#{eventStartTime}) BETWEEN DATE(event_start_dt) AND DATE(event_end_dt))
OR (DATE(#{eventEndTime}) BETWEEN DATE(event_start_dt) AND DATE(event_end_dt))
OR (DATE(event_start_dt) BETWEEN DATE(#{eventStartTime}) AND DATE(#{eventEndTime}))
OR (DATE(event_end_dt) BETWEEN DATE(#{eventStartTime}) AND DATE(#{eventEndTime}))
/* 실행 날짜 체크 */
CASE
/* 기존 데이터가 NONE 타입일 때 */
WHEN type = 'NONE' THEN
DATE(event_start_dt) = DATE(#{eventStartDt})
/* 기존 데이터가 DAY 타입일 때 */
WHEN type = 'DAY' THEN
DATE(event_start_dt) &lt;= DATE(#{eventStartDt}) AND
DATE(event_end_dt) &gt;= DATE(#{eventStartDt})
/* 기존 데이터가 요일 타입일 때 */
WHEN type IN ('SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY') THEN
DATE(event_start_dt) &lt;= DATE(#{eventStartDt}) AND
DATE(event_end_dt) &gt;= DATE(#{eventStartDt}) AND
DAYOFWEEK(event_start_dt) = DAYOFWEEK(#{eventStartDt})
END
)
/* 시간 겹침 체크 */
AND (
TIME(event_start_dt) &lt;= TIME(#{eventStartDt})
AND TIME(DATE_ADD(event_start_dt, INTERVAL event_operation_time SECOND)) &gt; TIME(#{eventStartDt})
OR
TIME(event_start_dt) &lt; TIME(DATE_ADD(#{eventStartDt}, INTERVAL #{event_operation_time} SECOND))
AND TIME(DATE_ADD(event_start_dt, INTERVAL event_operation_time SECOND)) &gt;= TIME(DATE_ADD(#{eventStartDt}, INTERVAL #{eventOperationTime} SECOND))
OR
TIME(event_start_dt) &gt;= TIME(#{eventStartDt})
AND TIME(DATE_ADD(event_start_dt, INTERVAL event_operation_time SECOND)) &lt;= TIME(DATE_ADD(#{eventStartDt}, INTERVAL #{eventOperationTime} SECOND))
)
</select>
@@ -158,6 +224,7 @@
SELECT
a.id
, a.group_id
, a.event_id
, a.event_name
, a.repeat_type
, a.event_operation_time
@@ -179,8 +246,8 @@
<!--저장-->
<insert id="postBattleEvent" parameterType="com.caliverse.admin.domain.request.BattleEventRequest" useGeneratedKeys="true" keyProperty="id">
INSERT INTO battle_event (group_id, event_name, repeat_type, event_operation_time, event_start_dt, event_end_dt, config_id, reward_group_id, round_time, round_count, hot_time, instance_id, create_by, update_by)
VALUES (#{groupId}, #{eventName}, #{repeatType}, #{eventOperationTime}, #{eventStartDt}, #{eventEndDt}, #{configId}, #{rewardGroupId}, #{roundTime}, #{roundCount}, #{hotTime}, #{instanceId} #{createBy}, #{updateBy})
INSERT INTO battle_event (group_id, event_id, event_name, repeat_type, event_operation_time, event_start_dt, event_end_dt, config_id, reward_group_id, round_time, round_count, hot_time, instance_id, create_by, update_by)
VALUES (#{groupId}, #{eventId}, #{eventName}, #{repeatType}, #{eventOperationTime}, #{eventStartDt}, #{eventEndDt}, #{configId}, #{rewardGroupId}, #{roundTime}, #{roundCount}, #{hotTime}, #{instanceId}, #{createBy}, #{updateBy})
</insert>
<!--수정-->
@@ -215,6 +282,7 @@
<select id="getScheduleBattleEventList" resultMap="BattleEventResultMap">
SELECT id
, a.group
, a.event_id
, a.event_name
, a.repeat_type
, a.event_operation_time