File tree Expand file tree Collapse file tree
src/main/java/com/example/cp_main_be/domain/garden/garden Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -54,6 +54,9 @@ public class Garden {
5454 @ Column (name = "last_watered_by_friend_at" )
5555 private LocalDateTime lastWateredByFriendAt ;
5656
57+ @ Column (name = "last_sunlight_received_at" )
58+ private LocalDateTime lastSunlightReceivedAt ;
59+
5760 @ CreatedDate
5861 @ Column (updatable = false )
5962 private LocalDateTime createdAt ;
@@ -86,6 +89,10 @@ public void recordFriendWateringTime() {
8689 this .lastWateredByFriendAt = LocalDateTime .now ();
8790 }
8891
92+ public void recordSunlightTime () {
93+ this .lastSunlightReceivedAt = LocalDateTime .now ();
94+ }
95+
8996 public void updateBackgroundImage (GardenBackground gardenBackground ) {
9097 this .gardenBackground = gardenBackground ;
9198 }
Original file line number Diff line number Diff line change @@ -116,8 +116,16 @@ public void sunlightGarden(Long actorId, Long gardenId) {
116116 throw new IllegalStateException ("자신의 정원에만 햇빛을 줄 수 있습니다." );
117117 }
118118
119+ // 하루에 한 번만 햇빛을 줄 수 있도록 체크 (초기화 시간: 오전 6시)
120+ LocalDateTime startOfSunlightDay = getStartOfCurrentSunlightDay ();
121+ if (garden .getLastSunlightReceivedAt () != null
122+ && garden .getLastSunlightReceivedAt ().isAfter (startOfSunlightDay )) {
123+ throw new IllegalStateException ("오늘은 이미 햇빛을 주었습니다. 내일 오전 6시 이후에 다시 시도해주세요." );
124+ }
125+
119126 garden .increaseSunlightCount ();
120127 userService .addExperience (actorId , SUNLIGHT_POINTS );
128+ garden .recordSunlightTime (); // 햇빛 준 시간 기록
121129 }
122130
123131 @ Transactional
@@ -185,4 +193,22 @@ private LocalDateTime getStartOfCurrentWateringDay() {
185193 return todayNoon ;
186194 }
187195 }
196+
197+ /**
198+ * 현재 시간 기준으로 햇빛 주기 횟수가 초기화되는 시간(오전 6시)을 계산합니다. - 현재 시간이 오전 6시 이전이면, 어제 오전 6시를 반환합니다. - 현재 시간이 오전
199+ * 6시 이후이면, 오늘 오전 6시를 반환합니다.
200+ *
201+ * @return 현재 햇빛 주기의 시작 시간
202+ */
203+ private LocalDateTime getStartOfCurrentSunlightDay () {
204+ // 서버 위치와 관계없이 항상 한국 시간 기준으로 동작하도록 시간대를 명시합니다.
205+ LocalDateTime now = LocalDateTime .now (ZoneId .of ("Asia/Seoul" ));
206+ LocalDateTime todaySixAM = now .toLocalDate ().atTime (6 , 0 );
207+
208+ if (now .isBefore (todaySixAM )) {
209+ return todaySixAM .minusDays (1 );
210+ } else {
211+ return todaySixAM ;
212+ }
213+ }
188214}
You can’t perform that action at this time.
0 commit comments