forked from remotemobprogramming/timer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix stupid webflux semantic difference
- Loading branch information
1 parent
880e44b
commit 41ae248
Showing
9 changed files
with
120 additions
and
100 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package sh.mob.timer.web; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
final class Room { | ||
|
||
private final String name; | ||
private final List<TimerRequest> timerRequests = new CopyOnWriteArrayList<>(); | ||
|
||
Room(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void add(Long timer, String user) { | ||
timerRequests.add(new TimerRequest(timer, Instant.now(), user)); | ||
} | ||
|
||
public TimeLeft timeLeft() { | ||
if (timerRequests.isEmpty()) { | ||
return new TimeLeft(Duration.ZERO, null, null, null); | ||
} | ||
|
||
var lastTimerRequest = timerRequests.get(timerRequests.size() - 1); | ||
var lastTimerRequestedTimestamp = lastTimerRequest.requested; | ||
var timer = lastTimerRequest.timer(); | ||
var result = | ||
Duration.between( | ||
Instant.now(), lastTimerRequestedTimestamp.plus(timer, ChronoUnit.MINUTES)); | ||
|
||
if (result.isNegative()) { | ||
return new TimeLeft( | ||
Duration.ZERO, timer, lastTimerRequestedTimestamp, lastTimerRequest.user()); | ||
} | ||
|
||
return new TimeLeft(result, timer, lastTimerRequestedTimestamp, lastTimerRequest.user()); | ||
} | ||
|
||
public record TimeLeft(Duration duration, Long timer, Instant requested, String name) {} | ||
|
||
public List<String> team() { | ||
return timerRequests().stream().map(TimerRequest::user).distinct().sorted().toList(); | ||
} | ||
|
||
public String name() { | ||
return name; | ||
} | ||
|
||
public List<TimerRequest> timerRequests() { | ||
return Collections.unmodifiableList(timerRequests); | ||
} | ||
|
||
record TimerRequest(Long timer, Instant requested, String user) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package sh.mob.timer.web; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public class RoomRepository { | ||
|
||
private final Map<String, Room> repository = new ConcurrentHashMap<>(); | ||
|
||
Room get(String room) { | ||
return repository.computeIfAbsent(room, Room::new); | ||
} | ||
|
||
public long count() { | ||
return repository.size(); | ||
} | ||
|
||
public long countUsers() { | ||
return repository.values().stream().mapToLong(room -> room.team().size()).sum(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters