| 1 | package edu.ucsb.cs156.happiercows.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 5 | import edu.ucsb.cs156.happiercows.entities.GameStats; | |
| 6 | import edu.ucsb.cs156.happiercows.entities.Game; | |
| 7 | import edu.ucsb.cs156.happiercows.entities.GamePlus; | |
| 8 | import edu.ucsb.cs156.happiercows.entities.User; | |
| 9 | import edu.ucsb.cs156.happiercows.entities.Farmer; | |
| 10 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
| 11 | import edu.ucsb.cs156.happiercows.models.CreateGameParams; | |
| 12 | import edu.ucsb.cs156.happiercows.models.DashboardSettingsParams; | |
| 13 | import edu.ucsb.cs156.happiercows.models.HealthUpdateStrategyList; | |
| 14 | import edu.ucsb.cs156.happiercows.errors.CourseAccessDeniedException; | |
| 15 | import edu.ucsb.cs156.happiercows.repositories.GameStatsRepository; | |
| 16 | import edu.ucsb.cs156.happiercows.repositories.GameRepository; | |
| 17 | import edu.ucsb.cs156.happiercows.repositories.FarmerRepository; | |
| 18 | import edu.ucsb.cs156.happiercows.services.CourseAccessService; | |
| 19 | import edu.ucsb.cs156.happiercows.strategies.CowHealthUpdateStrategies; | |
| 20 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 21 | import io.swagger.v3.oas.annotations.Operation; | |
| 22 | import io.swagger.v3.oas.annotations.Parameter; | |
| 23 | import org.springframework.beans.factory.annotation.Value; | |
| 24 | import lombok.extern.slf4j.Slf4j; | |
| 25 | import org.springframework.beans.factory.annotation.Autowired; | |
| 26 | import org.springframework.http.HttpStatus; | |
| 27 | import org.springframework.http.ResponseEntity; | |
| 28 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 29 | import org.springframework.web.bind.annotation.*; | |
| 30 | import edu.ucsb.cs156.happiercows.services.GamePlusBuilderService; | |
| 31 | import edu.ucsb.cs156.happiercows.errors.GameHiddenException; | |
| 32 | ||
| 33 | import java.util.ArrayList; | |
| 34 | import java.util.Comparator; | |
| 35 | import java.util.List; | |
| 36 | import java.util.Map; | |
| 37 | import java.util.Optional; | |
| 38 | import java.util.stream.Collectors; | |
| 39 | import java.util.stream.StreamSupport; | |
| 40 | ||
| 41 | ||
| 42 | @Slf4j | |
| 43 | @Tag(name = "Game") | |
| 44 | @RequestMapping("/api/game") | |
| 45 | @RestController | |
| 46 | public class GameController extends ApiController { | |
| 47 | @Autowired | |
| 48 | private GameRepository gameRepository; | |
| 49 | ||
| 50 | @Autowired | |
| 51 | private FarmerRepository farmerRepository; | |
| 52 | ||
| 53 | @Autowired | |
| 54 | ObjectMapper mapper; | |
| 55 | ||
| 56 | @Autowired | |
| 57 | GamePlusBuilderService gamePlusBuilderService; | |
| 58 | ||
| 59 | @Autowired | |
| 60 | GameStatsRepository gameStatsRepository; | |
| 61 | ||
| 62 | @Autowired | |
| 63 | CourseAccessService courseAccessService; | |
| 64 | ||
| 65 | @Value("${app.game.default.startingBalance}") | |
| 66 | private double defaultStartingBalance; | |
| 67 | ||
| 68 | @Value("${app.game.default.cowPrice}") | |
| 69 | private double defaultCowPrice; | |
| 70 | ||
| 71 | @Value("${app.game.default.milkPrice}") | |
| 72 | private double defaultMilkPrice; | |
| 73 | ||
| 74 | @Value("${app.game.default.degradationRate}") | |
| 75 | private double defaultDegradationRate; | |
| 76 | ||
| 77 | @Value("${app.game.default.carryingCapacity}") | |
| 78 | private int defaultCarryingCapacity; | |
| 79 | ||
| 80 | @Value("${app.game.default.capacityPerUser}") | |
| 81 | private int defaultCapacityPerUser; | |
| 82 | ||
| 83 | @Value("${app.game.default.aboveCapacityHealthUpdateStrategy}") | |
| 84 | private String defaultAboveCapacityHealthUpdateStrategy; | |
| 85 | ||
| 86 | @Value("${app.game.default.belowCapacityHealthUpdateStrategy}") | |
| 87 | private String defaultBelowCapacityHealthUpdateStrategy; | |
| 88 | ||
| 89 | @Operation(summary = "Get default common values") | |
| 90 | @GetMapping("/defaults") | |
| 91 | public ResponseEntity<Game> getDefaultGame() throws JsonProcessingException { | |
| 92 | log.info("getDefaultGame()..."); | |
| 93 | ||
| 94 | Game defaultGame = Game.builder() | |
| 95 | .startingBalance(defaultStartingBalance) | |
| 96 | .cowPrice(defaultCowPrice) | |
| 97 | .milkPrice(defaultMilkPrice) | |
| 98 | .degradationRate(defaultDegradationRate) | |
| 99 | .carryingCapacity(defaultCarryingCapacity) | |
| 100 | .capacityPerUser(defaultCapacityPerUser) | |
| 101 | .aboveCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(defaultAboveCapacityHealthUpdateStrategy)) | |
| 102 | .belowCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(defaultBelowCapacityHealthUpdateStrategy)) | |
| 103 | .hidden(false) | |
| 104 | .build(); | |
| 105 | ||
| 106 |
1
1. getDefaultGame : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::getDefaultGame → KILLED |
return ResponseEntity.ok().body(defaultGame); |
| 107 | } | |
| 108 | ||
| 109 | @Operation(summary = "Get a list of all game") | |
| 110 | @GetMapping("/all") | |
| 111 | public ResponseEntity<String> getGame() throws JsonProcessingException { | |
| 112 | log.info("getGame()..."); | |
| 113 | Iterable<Game> game = gameRepository.findAll(); | |
| 114 | String body = mapper.writeValueAsString(game); | |
| 115 |
1
1. getGame : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::getGame → KILLED |
return ResponseEntity.ok().body(body); |
| 116 | } | |
| 117 | ||
| 118 | @Operation(summary = "Get a list of all game and number of cows/users, newest first") | |
| 119 | @GetMapping("/allplus") | |
| 120 | public ResponseEntity<String> getGamePlus() throws JsonProcessingException { | |
| 121 | log.info("getGamePlus()..."); | |
| 122 | Iterable<Game> gameListIter = gameRepository.findAll(); | |
| 123 | ||
| 124 | // findAll() has no defined order, so sort newest first for a deterministic response | |
| 125 | List<Game> gameList = new ArrayList<>(); | |
| 126 |
1
1. getGamePlus : removed call to java/lang/Iterable::forEach → KILLED |
gameListIter.forEach(gameList::add); |
| 127 |
1
1. getGamePlus : removed call to java/util/List::sort → KILLED |
gameList.sort(Comparator.comparingLong(Game::getId).reversed()); |
| 128 | ||
| 129 | Iterable<GamePlus> gamePlusList = gamePlusBuilderService.convertToGamePlus(gameList); | |
| 130 | ||
| 131 | String body = mapper.writeValueAsString(gamePlusList); | |
| 132 |
1
1. getGamePlus : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::getGamePlus → KILLED |
return ResponseEntity.ok().body(body); |
| 133 | } | |
| 134 | ||
| 135 | @Operation(summary = "Get the number of cows/users in a game") | |
| 136 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 137 | @GetMapping("/plus") | |
| 138 | public GamePlus getGamePlusById( | |
| 139 | @Parameter(name="id") @RequestParam long id) throws JsonProcessingException { | |
| 140 | GamePlus gamePlus = gamePlusBuilderService.toGamePlus(gameRepository.findById(id) | |
| 141 |
1
1. lambda$getGamePlusById$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::lambda$getGamePlusById$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException("Game", id))); |
| 142 | ||
| 143 |
1
1. getGamePlusById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::getGamePlusById → KILLED |
return gamePlus; |
| 144 | } | |
| 145 | ||
| 146 | @Operation(summary = "Update a game") | |
| 147 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 148 | @PutMapping("/update") | |
| 149 | public ResponseEntity<String> updateGame( | |
| 150 | @Parameter(name="id") @RequestParam long id, | |
| 151 | @Parameter(name="request body") @RequestBody CreateGameParams params | |
| 152 | ) { | |
| 153 | Optional<Game> existing = gameRepository.findById(id); | |
| 154 | ||
| 155 | Game updated; | |
| 156 | HttpStatus status; | |
| 157 | ||
| 158 |
1
1. updateGame : negated conditional → KILLED |
if (existing.isPresent()) { |
| 159 | updated = existing.get(); | |
| 160 | status = HttpStatus.NO_CONTENT; | |
| 161 | } else { | |
| 162 | updated = new Game(); | |
| 163 | status = HttpStatus.CREATED; | |
| 164 | } | |
| 165 | ||
| 166 |
1
1. updateGame : removed call to edu/ucsb/cs156/happiercows/entities/Game::setName → KILLED |
updated.setName(params.getName()); |
| 167 |
1
1. updateGame : removed call to edu/ucsb/cs156/happiercows/entities/Game::setCowPrice → KILLED |
updated.setCowPrice(params.getCowPrice()); |
| 168 |
1
1. updateGame : removed call to edu/ucsb/cs156/happiercows/entities/Game::setMilkPrice → KILLED |
updated.setMilkPrice(params.getMilkPrice()); |
| 169 |
1
1. updateGame : removed call to edu/ucsb/cs156/happiercows/entities/Game::setStartingBalance → KILLED |
updated.setStartingBalance(params.getStartingBalance()); |
| 170 |
1
1. updateGame : removed call to edu/ucsb/cs156/happiercows/entities/Game::setStartingDate → KILLED |
updated.setStartingDate(params.getStartingDate()); |
| 171 |
1
1. updateGame : removed call to edu/ucsb/cs156/happiercows/entities/Game::setLastDate → KILLED |
updated.setLastDate(params.getLastDate()); |
| 172 |
1
1. updateGame : removed call to edu/ucsb/cs156/happiercows/entities/Game::setShowLeaderboard → KILLED |
updated.setShowLeaderboard(params.getShowLeaderboard()); |
| 173 |
1
1. updateGame : removed call to edu/ucsb/cs156/happiercows/entities/Game::setShowChat → KILLED |
updated.setShowChat(params.getShowChat()); |
| 174 |
1
1. updateGame : removed call to edu/ucsb/cs156/happiercows/entities/Game::setDegradationRate → KILLED |
updated.setDegradationRate(params.getDegradationRate()); |
| 175 |
1
1. updateGame : removed call to edu/ucsb/cs156/happiercows/entities/Game::setCapacityPerUser → KILLED |
updated.setCapacityPerUser(params.getCapacityPerUser()); |
| 176 |
1
1. updateGame : removed call to edu/ucsb/cs156/happiercows/entities/Game::setCarryingCapacity → KILLED |
updated.setCarryingCapacity(params.getCarryingCapacity()); |
| 177 |
1
1. updateGame : negated conditional → KILLED |
if (params.getAboveCapacityHealthUpdateStrategy() != null) { |
| 178 |
1
1. updateGame : removed call to edu/ucsb/cs156/happiercows/entities/Game::setAboveCapacityHealthUpdateStrategy → KILLED |
updated.setAboveCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getAboveCapacityHealthUpdateStrategy())); |
| 179 | } | |
| 180 |
1
1. updateGame : negated conditional → KILLED |
if (params.getBelowCapacityHealthUpdateStrategy() != null) { |
| 181 |
1
1. updateGame : removed call to edu/ucsb/cs156/happiercows/entities/Game::setBelowCapacityHealthUpdateStrategy → KILLED |
updated.setBelowCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getBelowCapacityHealthUpdateStrategy())); |
| 182 | } | |
| 183 | ||
| 184 |
2
1. updateGame : changed conditional boundary → KILLED 2. updateGame : negated conditional → KILLED |
if (params.getDegradationRate() < 0) { |
| 185 | throw new IllegalArgumentException("Degradation Rate cannot be negative"); | |
| 186 | } | |
| 187 | ||
| 188 | // Reference: frontend/src/main/components/Game/GameForm.js | |
| 189 |
1
1. updateGame : negated conditional → KILLED |
if (params.getName().equals("")) { |
| 190 | throw new IllegalArgumentException("Name cannot be empty"); | |
| 191 | } | |
| 192 | ||
| 193 |
2
1. updateGame : negated conditional → KILLED 2. updateGame : changed conditional boundary → KILLED |
if (params.getCowPrice() < 0.01) { |
| 194 | throw new IllegalArgumentException("Cow Price cannot be less than 0.01"); | |
| 195 | } | |
| 196 | ||
| 197 |
2
1. updateGame : changed conditional boundary → KILLED 2. updateGame : negated conditional → KILLED |
if (params.getMilkPrice() < 0.01) { |
| 198 | throw new IllegalArgumentException("Milk Price cannot be less than 0.01"); | |
| 199 | } | |
| 200 | ||
| 201 |
2
1. updateGame : negated conditional → KILLED 2. updateGame : changed conditional boundary → KILLED |
if (params.getStartingBalance() < 0) { |
| 202 | throw new IllegalArgumentException("Starting Balance cannot be negative"); | |
| 203 | } | |
| 204 | ||
| 205 |
2
1. updateGame : changed conditional boundary → KILLED 2. updateGame : negated conditional → KILLED |
if (params.getCarryingCapacity() < 1) { |
| 206 | throw new IllegalArgumentException("Carrying Capacity cannot be less than 1"); | |
| 207 | } | |
| 208 | ||
| 209 |
1
1. updateGame : removed call to edu/ucsb/cs156/happiercows/controllers/GameController::validateDates → KILLED |
validateDates(params); |
| 210 | ||
| 211 |
1
1. updateGame : removed call to edu/ucsb/cs156/happiercows/entities/Game::setHidden → KILLED |
updated.setHidden(params.isHidden()); |
| 212 |
1
1. updateGame : removed call to edu/ucsb/cs156/happiercows/entities/Game::setCourseId → KILLED |
updated.setCourseId(params.getCourseId()); |
| 213 | gameRepository.save(updated); | |
| 214 | ||
| 215 |
1
1. updateGame : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::updateGame → KILLED |
return ResponseEntity.status(status).build(); |
| 216 | } | |
| 217 | ||
| 218 | @Operation(summary = "Get a specific game") | |
| 219 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 220 | @GetMapping("") | |
| 221 | public Game getGameById( | |
| 222 | @Parameter(name="id") @RequestParam Long id) throws JsonProcessingException { | |
| 223 | ||
| 224 | Game game = gameRepository.findById(id) | |
| 225 |
1
1. lambda$getGameById$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::lambda$getGameById$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException("Game", id)); |
| 226 | ||
| 227 |
1
1. getGameById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::getGameById → KILLED |
return game; |
| 228 | } | |
| 229 | ||
| 230 | @Operation(summary = "Create a new game") | |
| 231 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 232 | @PostMapping(value = "/new", produces = "application/json") | |
| 233 | public ResponseEntity<String> createGame( | |
| 234 | @Parameter(name="request body") @RequestBody CreateGameParams params | |
| 235 | ) throws JsonProcessingException { | |
| 236 | ||
| 237 | var builder = Game.builder() | |
| 238 | .name(params.getName()) | |
| 239 | .cowPrice(params.getCowPrice()) | |
| 240 | .milkPrice(params.getMilkPrice()) | |
| 241 | .startingBalance(params.getStartingBalance()) | |
| 242 | .startingDate(params.getStartingDate()) | |
| 243 | .lastDate(params.getLastDate()) | |
| 244 | .degradationRate(params.getDegradationRate()) | |
| 245 | .showLeaderboard(params.getShowLeaderboard()) | |
| 246 | .showChat(params.getShowChat()) | |
| 247 | .capacityPerUser(params.getCapacityPerUser()) | |
| 248 | .carryingCapacity(params.getCarryingCapacity()) | |
| 249 | .hidden(params.isHidden()) | |
| 250 | .courseId(params.getCourseId()); | |
| 251 | ||
| 252 | // ok to set null values for these, so old backend still works | |
| 253 |
1
1. createGame : negated conditional → KILLED |
if (params.getAboveCapacityHealthUpdateStrategy() != null) { |
| 254 | builder.aboveCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getAboveCapacityHealthUpdateStrategy())); | |
| 255 | } | |
| 256 |
1
1. createGame : negated conditional → KILLED |
if (params.getBelowCapacityHealthUpdateStrategy() != null) { |
| 257 | builder.belowCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getBelowCapacityHealthUpdateStrategy())); | |
| 258 | } | |
| 259 | ||
| 260 | Game game = builder.build(); | |
| 261 | ||
| 262 | // Reference: frontend/src/main/components/Game/GameForm.js | |
| 263 |
1
1. createGame : negated conditional → KILLED |
if (params.getName().equals("")) { |
| 264 | throw new IllegalArgumentException("Name cannot be empty"); | |
| 265 | } | |
| 266 | ||
| 267 |
2
1. createGame : changed conditional boundary → KILLED 2. createGame : negated conditional → KILLED |
if (params.getCowPrice() < 0.01) { |
| 268 | throw new IllegalArgumentException("Cow Price cannot be less than 0.01"); | |
| 269 | } | |
| 270 | ||
| 271 |
2
1. createGame : changed conditional boundary → KILLED 2. createGame : negated conditional → KILLED |
if (params.getMilkPrice() < 0.01) { |
| 272 | throw new IllegalArgumentException("Milk Price cannot be less than 0.01"); | |
| 273 | } | |
| 274 | ||
| 275 |
2
1. createGame : negated conditional → KILLED 2. createGame : changed conditional boundary → KILLED |
if (params.getStartingBalance() < 0) { |
| 276 | throw new IllegalArgumentException("Starting Balance cannot be negative"); | |
| 277 | } | |
| 278 | ||
| 279 | // throw exception for degradation rate | |
| 280 |
2
1. createGame : negated conditional → KILLED 2. createGame : changed conditional boundary → KILLED |
if (params.getDegradationRate() < 0) { |
| 281 | throw new IllegalArgumentException("Degradation Rate cannot be negative"); | |
| 282 | } | |
| 283 | ||
| 284 |
2
1. createGame : negated conditional → KILLED 2. createGame : changed conditional boundary → KILLED |
if (params.getCarryingCapacity() < 1) { |
| 285 | throw new IllegalArgumentException("Carrying Capacity cannot be less than 1"); | |
| 286 | } | |
| 287 | ||
| 288 |
1
1. createGame : removed call to edu/ucsb/cs156/happiercows/controllers/GameController::validateDates → KILLED |
validateDates(params); |
| 289 | ||
| 290 | Game saved = gameRepository.save(game); | |
| 291 | String body = mapper.writeValueAsString(saved); | |
| 292 | ||
| 293 |
1
1. createGame : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::createGame → KILLED |
return ResponseEntity.ok().body(body); |
| 294 | } | |
| 295 | ||
| 296 | /** | |
| 297 | * Enforce that both dates are present and that the last date is strictly | |
| 298 | * after the starting date. (See issue #250; the frontend form enforces | |
| 299 | * the same rules, but the backend must not rely on that.) | |
| 300 | * | |
| 301 | * @param params the params to validate | |
| 302 | */ | |
| 303 | public static void validateDates(CreateGameParams params) { | |
| 304 |
1
1. validateDates : negated conditional → KILLED |
if (params.getStartingDate() == null) { |
| 305 | throw new IllegalArgumentException("Starting Date is required"); | |
| 306 | } | |
| 307 |
1
1. validateDates : negated conditional → KILLED |
if (params.getLastDate() == null) { |
| 308 | throw new IllegalArgumentException("Last Date is required"); | |
| 309 | } | |
| 310 |
1
1. validateDates : negated conditional → KILLED |
if (!params.getLastDate().isAfter(params.getStartingDate())) { |
| 311 | throw new IllegalArgumentException("Last Date must be after Starting Date"); | |
| 312 | } | |
| 313 | } | |
| 314 | ||
| 315 | ||
| 316 | @Operation(summary = "List all cow health update strategies") | |
| 317 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 318 | @GetMapping("/all-health-update-strategies") | |
| 319 | public ResponseEntity<String> listCowHealthUpdateStrategies() throws JsonProcessingException { | |
| 320 | var result = HealthUpdateStrategyList.create(); | |
| 321 | String body = mapper.writeValueAsString(result); | |
| 322 |
1
1. listCowHealthUpdateStrategies : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::listCowHealthUpdateStrategies → KILLED |
return ResponseEntity.ok().body(body); |
| 323 | } | |
| 324 | ||
| 325 | @Operation(summary = "Get the ids of the courses the current user belongs to as a student or staff member") | |
| 326 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 327 | @GetMapping("/mycourses") | |
| 328 | public ResponseEntity<List<Long>> getMyCourseIds() { | |
| 329 | User u = getCurrentUser().getUser(); | |
| 330 | List<Long> courseIds = courseAccessService.getCourseIdsForUser(u); | |
| 331 |
1
1. getMyCourseIds : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::getMyCourseIds → KILLED |
return ResponseEntity.ok().body(courseIds); |
| 332 | } | |
| 333 | ||
| 334 | @Operation(summary = "Join a game") | |
| 335 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 336 | @PostMapping(value = "/join", produces = "application/json") | |
| 337 | public ResponseEntity<String> joinCommon( | |
| 338 | @Parameter(name="gameId") @RequestParam Long gameId) throws Exception { | |
| 339 | ||
| 340 | User u = getCurrentUser().getUser(); | |
| 341 | Long userId = u.getId(); | |
| 342 | String username = u.getFullName(); | |
| 343 | ||
| 344 | Game joinedGame = gameRepository.findById(gameId) | |
| 345 |
1
1. lambda$joinCommon$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::lambda$joinCommon$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException("Game", gameId)); |
| 346 | ||
| 347 |
1
1. joinCommon : negated conditional → KILLED |
if (joinedGame.isHidden()) { |
| 348 | throw new GameHiddenException(gameId); | |
| 349 | } | |
| 350 | ||
| 351 |
2
1. joinCommon : negated conditional → KILLED 2. joinCommon : negated conditional → KILLED |
if (joinedGame.getCourseId() != null && !courseAccessService.isEligibleForGame(u, joinedGame)) { |
| 352 | throw new CourseAccessDeniedException(gameId); | |
| 353 | } | |
| 354 | ||
| 355 | Optional<Farmer> farmerLookup = farmerRepository.findByGameIdAndUserId(gameId, userId); | |
| 356 | ||
| 357 |
1
1. joinCommon : negated conditional → KILLED |
if (farmerLookup.isPresent()) { |
| 358 | // user is already a member of this game | |
| 359 | String body = mapper.writeValueAsString(joinedGame); | |
| 360 |
1
1. joinCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::joinCommon → KILLED |
return ResponseEntity.ok().body(body); |
| 361 | } | |
| 362 | ||
| 363 | Farmer uc = Farmer.builder() | |
| 364 | .user(u) | |
| 365 | .game(joinedGame) | |
| 366 | .username(username) | |
| 367 | .totalWealth(joinedGame.getStartingBalance()) | |
| 368 | .numOfCows(0) | |
| 369 | .cowHealth(100) | |
| 370 | .cowsBought(0) | |
| 371 | .cowsSold(0) | |
| 372 | .cowDeaths(0) | |
| 373 | .build(); | |
| 374 | ||
| 375 | farmerRepository.save(uc); | |
| 376 | ||
| 377 | String body = mapper.writeValueAsString(joinedGame); | |
| 378 |
1
1. joinCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::joinCommon → KILLED |
return ResponseEntity.ok().body(body); |
| 379 | } | |
| 380 | ||
| 381 | @Operation(summary = "Delete a Game") | |
| 382 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 383 | @DeleteMapping("") | |
| 384 | public Object deleteGame( | |
| 385 | @Parameter(name="id") @RequestParam Long id) { | |
| 386 | | |
| 387 | Iterable<Farmer> farmer = farmerRepository.findByGameId(id); | |
| 388 | ||
| 389 | for (Farmer game : farmer) { | |
| 390 |
1
1. deleteGame : removed call to edu/ucsb/cs156/happiercows/repositories/FarmerRepository::delete → KILLED |
farmerRepository.delete(game); |
| 391 | } | |
| 392 | ||
| 393 | gameRepository.findById(id) | |
| 394 |
1
1. lambda$deleteGame$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::lambda$deleteGame$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException("Game", id)); |
| 395 | ||
| 396 |
1
1. deleteGame : removed call to edu/ucsb/cs156/happiercows/repositories/GameRepository::deleteById → KILLED |
gameRepository.deleteById(id); |
| 397 | ||
| 398 | String responseString = String.format("game with id %d deleted", id); | |
| 399 |
1
1. deleteGame : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::deleteGame → KILLED |
return genericMessage(responseString); |
| 400 | ||
| 401 | } | |
| 402 | ||
| 403 | @Operation(summary="Delete a user from a game") | |
| 404 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 405 | @DeleteMapping("/{gameId}/users/{userId}") | |
| 406 | public Object deleteUserFromCommon(@PathVariable("gameId") Long gameId, | |
| 407 | @PathVariable("userId") Long userId) throws Exception { | |
| 408 | ||
| 409 | Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId) | |
| 410 |
1
1. lambda$deleteUserFromCommon$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::lambda$deleteUserFromCommon$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException( |
| 411 | Farmer.class, "gameId", gameId, "userId", userId) | |
| 412 | ); | |
| 413 | ||
| 414 |
1
1. deleteUserFromCommon : removed call to edu/ucsb/cs156/happiercows/repositories/FarmerRepository::delete → KILLED |
farmerRepository.delete(farmer); |
| 415 | ||
| 416 | String responseString = String.format("user with id %d deleted from game with id %d, %d users remain", userId, gameId, gameRepository.getNumUsers(gameId).orElse(0)); | |
| 417 | ||
| 418 |
1
1. deleteUserFromCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::deleteUserFromCommon → KILLED |
return genericMessage(responseString); |
| 419 | } | |
| 420 | ||
| 421 | @Operation(summary = "Update the dashboard visibility settings for a game (admin only)") | |
| 422 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 423 | @PutMapping("/dashboardSettings") | |
| 424 | public ResponseEntity<Game> updateDashboardSettings( | |
| 425 | @Parameter(name="id") @RequestParam long id, | |
| 426 | @Parameter(name="request body") @RequestBody DashboardSettingsParams params | |
| 427 | ) { | |
| 428 | Game game = gameRepository.findById(id) | |
| 429 |
1
1. lambda$updateDashboardSettings$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::lambda$updateDashboardSettings$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException("Game", id)); |
| 430 | ||
| 431 |
1
1. updateDashboardSettings : removed call to edu/ucsb/cs156/happiercows/entities/Game::setShowLeaderboard → KILLED |
game.setShowLeaderboard(params.isShowLeaderboard()); |
| 432 |
1
1. updateDashboardSettings : removed call to edu/ucsb/cs156/happiercows/entities/Game::setShowOverviewSection → KILLED |
game.setShowOverviewSection(params.isShowOverviewSection()); |
| 433 |
1
1. updateDashboardSettings : removed call to edu/ucsb/cs156/happiercows/entities/Game::setShowCowsPerFarmerSection → KILLED |
game.setShowCowsPerFarmerSection(params.isShowCowsPerFarmerSection()); |
| 434 |
1
1. updateDashboardSettings : removed call to edu/ucsb/cs156/happiercows/entities/Game::setShowCapacitySection → KILLED |
game.setShowCapacitySection(params.isShowCapacitySection()); |
| 435 |
1
1. updateDashboardSettings : removed call to edu/ucsb/cs156/happiercows/entities/Game::setShowHistogramSection → KILLED |
game.setShowHistogramSection(params.isShowHistogramSection()); |
| 436 |
1
1. updateDashboardSettings : removed call to edu/ucsb/cs156/happiercows/entities/Game::setShowTrendsSection → KILLED |
game.setShowTrendsSection(params.isShowTrendsSection()); |
| 437 |
1
1. updateDashboardSettings : removed call to edu/ucsb/cs156/happiercows/entities/Game::setShowHealthSection → KILLED |
game.setShowHealthSection(params.isShowHealthSection()); |
| 438 |
1
1. updateDashboardSettings : removed call to edu/ucsb/cs156/happiercows/entities/Game::setShowTotalCowsSection → KILLED |
game.setShowTotalCowsSection(params.isShowTotalCowsSection()); |
| 439 |
1
1. updateDashboardSettings : removed call to edu/ucsb/cs156/happiercows/entities/Game::setShowCapacityOverTimeSection → KILLED |
game.setShowCapacityOverTimeSection(params.isShowCapacityOverTimeSection()); |
| 440 |
1
1. updateDashboardSettings : removed call to edu/ucsb/cs156/happiercows/entities/Game::setShowFarmerLeaderboardSection → KILLED |
game.setShowFarmerLeaderboardSection(params.isShowFarmerLeaderboardSection()); |
| 441 | ||
| 442 | Game saved = gameRepository.save(game); | |
| 443 | ||
| 444 |
1
1. updateDashboardSettings : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::updateDashboardSettings → KILLED |
return ResponseEntity.ok().body(saved); |
| 445 | } | |
| 446 | ||
| 447 | @Operation(summary = "Get the number of cows for each farmer in a game") | |
| 448 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 449 | @GetMapping("/numcows") | |
| 450 | public ResponseEntity<List<Integer>> getNumCowsForGameId( | |
| 451 | @Parameter(name="gameId") @RequestParam Long gameId) { | |
| 452 | Iterable<Farmer> farmerList = farmerRepository.findByGameId(gameId); | |
| 453 | List<Integer> numCowsList = StreamSupport.stream(farmerList.spliterator(), false) | |
| 454 | .map(Farmer::getNumOfCows) | |
| 455 | .collect(Collectors.toList()); | |
| 456 |
1
1. getNumCowsForGameId : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::getNumCowsForGameId → KILLED |
return ResponseEntity.ok().body(numCowsList); |
| 457 | } | |
| 458 | ||
| 459 | @Operation(summary = "Get timeseries stats for a game") | |
| 460 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 461 | @GetMapping("/timeseries") | |
| 462 | public ResponseEntity<List<Map<String, Object>>> getGameTimeSeries( | |
| 463 | @Parameter(name="commonId") @RequestParam Long commonId) { | |
| 464 | Iterable<GameStats> gameStats = gameStatsRepository.findAllByGameId(commonId); | |
| 465 | List<GameStats> sortedStats = StreamSupport.stream(gameStats.spliterator(), false) | |
| 466 | .sorted(Comparator.comparing(GameStats::getCreateDate)) | |
| 467 | .collect(Collectors.toList()); | |
| 468 | ||
| 469 | List<Map<String, Object>> healthValues = sortedStats.stream() | |
| 470 |
1
1. lambda$getGameTimeSeries$0 : replaced return value with Collections.emptyMap for edu/ucsb/cs156/happiercows/controllers/GameController::lambda$getGameTimeSeries$0 → KILLED |
.map(stat -> Map.<String, Object>of( |
| 471 | "date", stat.getCreateDate().toString(), | |
| 472 | "value", stat.getAvgHealth())) | |
| 473 | .collect(Collectors.toList()); | |
| 474 | ||
| 475 | List<Map<String, Object>> totalCowsValues = sortedStats.stream() | |
| 476 |
1
1. lambda$getGameTimeSeries$1 : replaced return value with Collections.emptyMap for edu/ucsb/cs156/happiercows/controllers/GameController::lambda$getGameTimeSeries$1 → KILLED |
.map(stat -> Map.<String, Object>of( |
| 477 | "date", stat.getCreateDate().toString(), | |
| 478 | "value", stat.getNumCows())) | |
| 479 | .collect(Collectors.toList()); | |
| 480 | ||
| 481 | List<Map<String, Object>> effectiveCapacityValues = sortedStats.stream() | |
| 482 |
1
1. lambda$getGameTimeSeries$2 : replaced return value with Collections.emptyMap for edu/ucsb/cs156/happiercows/controllers/GameController::lambda$getGameTimeSeries$2 → KILLED |
.map(stat -> Map.<String, Object>of( |
| 483 | "date", stat.getCreateDate().toString(), | |
| 484 | "value", stat.getEffectiveCapacity())) | |
| 485 | .collect(Collectors.toList()); | |
| 486 | ||
| 487 | List<Map<String, Object>> timeSeries = List.of( | |
| 488 | Map.of( | |
| 489 | "name", "Health", | |
| 490 | "color", "#0088FE", | |
| 491 | "percentage", true, | |
| 492 | "values", healthValues), | |
| 493 | Map.of( | |
| 494 | "name", "Total Cows", | |
| 495 | "color", "#FF8042", | |
| 496 | "values", totalCowsValues), | |
| 497 | Map.of( | |
| 498 | "name", "Effective Capacity", | |
| 499 | "color", "#00C49F", | |
| 500 | "values", effectiveCapacityValues)); | |
| 501 | ||
| 502 |
1
1. getGameTimeSeries : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameController::getGameTimeSeries → KILLED |
return ResponseEntity.ok().body(timeSeries); |
| 503 | } | |
| 504 | ||
| 505 | | |
| 506 | } | |
Mutations | ||
| 106 |
1.1 |
|
| 115 |
1.1 |
|
| 126 |
1.1 |
|
| 127 |
1.1 |
|
| 132 |
1.1 |
|
| 141 |
1.1 |
|
| 143 |
1.1 |
|
| 158 |
1.1 |
|
| 166 |
1.1 |
|
| 167 |
1.1 |
|
| 168 |
1.1 |
|
| 169 |
1.1 |
|
| 170 |
1.1 |
|
| 171 |
1.1 |
|
| 172 |
1.1 |
|
| 173 |
1.1 |
|
| 174 |
1.1 |
|
| 175 |
1.1 |
|
| 176 |
1.1 |
|
| 177 |
1.1 |
|
| 178 |
1.1 |
|
| 180 |
1.1 |
|
| 181 |
1.1 |
|
| 184 |
1.1 2.2 |
|
| 189 |
1.1 |
|
| 193 |
1.1 2.2 |
|
| 197 |
1.1 2.2 |
|
| 201 |
1.1 2.2 |
|
| 205 |
1.1 2.2 |
|
| 209 |
1.1 |
|
| 211 |
1.1 |
|
| 212 |
1.1 |
|
| 215 |
1.1 |
|
| 225 |
1.1 |
|
| 227 |
1.1 |
|
| 253 |
1.1 |
|
| 256 |
1.1 |
|
| 263 |
1.1 |
|
| 267 |
1.1 2.2 |
|
| 271 |
1.1 2.2 |
|
| 275 |
1.1 2.2 |
|
| 280 |
1.1 2.2 |
|
| 284 |
1.1 2.2 |
|
| 288 |
1.1 |
|
| 293 |
1.1 |
|
| 304 |
1.1 |
|
| 307 |
1.1 |
|
| 310 |
1.1 |
|
| 322 |
1.1 |
|
| 331 |
1.1 |
|
| 345 |
1.1 |
|
| 347 |
1.1 |
|
| 351 |
1.1 2.2 |
|
| 357 |
1.1 |
|
| 360 |
1.1 |
|
| 378 |
1.1 |
|
| 390 |
1.1 |
|
| 394 |
1.1 |
|
| 396 |
1.1 |
|
| 399 |
1.1 |
|
| 410 |
1.1 |
|
| 414 |
1.1 |
|
| 418 |
1.1 |
|
| 429 |
1.1 |
|
| 431 |
1.1 |
|
| 432 |
1.1 |
|
| 433 |
1.1 |
|
| 434 |
1.1 |
|
| 435 |
1.1 |
|
| 436 |
1.1 |
|
| 437 |
1.1 |
|
| 438 |
1.1 |
|
| 439 |
1.1 |
|
| 440 |
1.1 |
|
| 444 |
1.1 |
|
| 456 |
1.1 |
|
| 470 |
1.1 |
|
| 476 |
1.1 |
|
| 482 |
1.1 |
|
| 502 |
1.1 |