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