-
Notifications
You must be signed in to change notification settings - Fork 16
/
BingoService.java
168 lines (149 loc) · 6 KB
/
BingoService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Copyright 2016 Sven Loesekann
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ch.xxx.messenger.usecase.service;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import ch.xxx.messenger.domain.model.BingoGame;
import ch.xxx.messenger.domain.model.MyMongoRepository;
import reactor.core.publisher.Mono;
@Service
public class BingoService {
private static final Logger LOG = LoggerFactory.getLogger(BingoService.class);
private record BingoGameChanged(BingoGame bingoGame, AtomicBoolean changed) {
}
private final MyMongoRepository repository;
public BingoService(MyMongoRepository repository) {
this.repository = repository;
}
public Mono<BingoGame> startBingoGame(BingoGame bingoGame) {
bingoGame.setUuid(UUID.randomUUID().toString());
bingoGame.setLastUpdate(LocalDateTime.now());
bingoGame.getBingoBoards()
.addAll(bingoGame.getPlayerUserIds().stream()
.map(myUserId -> new BingoGame.BingoBoard(new int[5][5], new boolean[5][5]))
.map(this::initBingoBoard).toList());
return this.repository.save(bingoGame);
}
public Mono<BingoGame> updateBingoGame(String uuid) {
return this.repository.findOne(new Query().addCriteria(Criteria.where("uuid").is(uuid)), BingoGame.class)
.map(myBingoGame -> this.updateBingoGame(new BingoGameChanged(myBingoGame, new AtomicBoolean(false))))
.flatMap(myRecord -> myRecord.changed().get() ? this.repository.save(myRecord.bingoGame())
: Mono.just(myRecord.bingoGame()));
}
public Mono<Boolean> checkWin(String gameUuid, String userUuid) {
return this.repository.findOne(new Query().addCriteria(Criteria.where("uuid").is(gameUuid)), BingoGame.class)
.flatMap(myBingoGame -> this.checkBoard(myBingoGame, userUuid));
}
public Mono<Boolean> endGame(String gameUuid) {
return this.repository.remove(new Query().addCriteria(Criteria.where("uuid").is(gameUuid)), BingoGame.class).map(result -> result.wasAcknowledged());
}
public void cleanUpGames() {
LOG.info("Cleanup bingo games started.");
Date cutoffTimeStamp = Date.from(LocalDateTime.now().minusDays(1L).atZone(ZoneId.systemDefault()).toInstant());
this.repository.findAllAndRemove(new Query().addCriteria(Criteria.where("lastUpdate").lt(cutoffTimeStamp)), BingoGame.class).collectList().block();
LOG.info("Cleanup bingo games finished.");
}
private Mono<Boolean> checkBoard(BingoGame bingoGame, String userUuid) {
boolean result = false;
int boardIndex = bingoGame.getPlayerUserIds().indexOf(userUuid);
if(boardIndex < 0) {
return Mono.just(false);
}
for (int x = 0; x < 5; x++) {
int columnHits = 0;
for (int y = 0; y < 5; y++) {
if(bingoGame.getBingoBoards().get(boardIndex).hits()[x][y]) {
columnHits += 1;
}
}
if(columnHits > 4) {
result = true;
}
}
for (int y = 0; y < 5; y++) {
int rowHits = 0;
for (int x = 0; x < 5; x++) {
if(bingoGame.getBingoBoards().get(boardIndex).hits()[x][y]) {
rowHits += 1;
}
}
if(rowHits > 4) {
result = true;
}
}
int diaHits1 = 0;
int diaHits2 = 0;
for(int i = 0;i < 5; i++) {
if(bingoGame.getBingoBoards().get(boardIndex).hits()[i][i]) {
diaHits1 += 1;
}
if(bingoGame.getBingoBoards().get(boardIndex).hits()[4-i][i]) {
diaHits2 += 1;
}
}
if(diaHits1 > 4 || diaHits2 > 4) {
result = true;
}
return Mono.just(result);
}
private BingoGame.BingoBoard initBingoBoard(BingoGame.BingoBoard bingoBoard) {
AtomicReference<List<Integer>> atomicList = new AtomicReference<List<Integer>>(new ArrayList<Integer>());
for (int x = 0; x < 5; x++) {
for (int y = 0; y < 5; y++) {
AtomicInteger randVal = new AtomicInteger((int) (Math.round((Math.random() * 74)) + 1));
while (atomicList.get().stream().anyMatch(myVal -> myVal.equals(randVal.get()))) {
randVal.set((int) (Math.round((Math.random() * 74)) + 1));
}
atomicList.get().add(randVal.get());
bingoBoard.board()[x][y] = randVal.get();
}
}
return bingoBoard;
}
private BingoGameChanged updateBingoGame(BingoGameChanged bingoGameChanged) {
if (bingoGameChanged.bingoGame().getLastUpdate().isBefore(LocalDateTime.now().minus(15L, ChronoUnit.SECONDS))) {
AtomicInteger newRand = new AtomicInteger((int) (Math.round((Math.random() * 74)) + 1));
while (bingoGameChanged.bingoGame.getRandomValues().size() < 73 && bingoGameChanged.bingoGame().getRandomValues().stream()
.anyMatch(myRandValue -> myRandValue.equals(newRand.get()))) {
newRand.set((int) (Math.round((Math.random() * 74)) + 1));
}
bingoGameChanged.bingoGame().getRandomValues().add(newRand.get());
bingoGameChanged.bingoGame().getBingoBoards().forEach(myBingoBoard -> this.updateBingoBoardHits(myBingoBoard, newRand.get()));
bingoGameChanged.changed().set(true);
}
return bingoGameChanged;
}
private BingoGame.BingoBoard updateBingoBoardHits(BingoGame.BingoBoard bingoBoard, int newRand) {
for(int x = 0;x < 5; x++) {
for(int y = 0;y < 5; y++) {
if(bingoBoard.board()[x][y] == newRand) {
bingoBoard.hits()[x][y] = true;
}
}
}
return bingoBoard;
}
}