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