|
|
|
@ -1,13 +1,17 @@
|
|
|
|
|
package cz.jzitnik.chronos.controllers;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import cz.jzitnik.chronos.entities.Item;
|
|
|
|
|
import cz.jzitnik.chronos.entities.Message;
|
|
|
|
|
import cz.jzitnik.chronos.entities.MessageType;
|
|
|
|
|
import cz.jzitnik.chronos.entities.Player;
|
|
|
|
|
import cz.jzitnik.chronos.payload.errors.FullInventoryError;
|
|
|
|
|
import cz.jzitnik.chronos.payload.errors.ItemNotUsableException;
|
|
|
|
|
import cz.jzitnik.chronos.payload.errors.NotFoundError;
|
|
|
|
|
import cz.jzitnik.chronos.payload.requests.PlayerNameRequest;
|
|
|
|
|
import cz.jzitnik.chronos.payload.responses.InventoryFullResponse;
|
|
|
|
|
import cz.jzitnik.chronos.payload.responses.ItemMoveMessageResponse;
|
|
|
|
|
import cz.jzitnik.chronos.payload.responses.UnifiedResponse;
|
|
|
|
|
import cz.jzitnik.chronos.repository.GameRepository;
|
|
|
|
|
import cz.jzitnik.chronos.repository.ItemRepository;
|
|
|
|
@ -19,6 +23,8 @@ import org.springframework.http.HttpStatus;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
@CrossOrigin(origins = "*", maxAge = 3600)
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/game/players")
|
|
|
|
@ -116,4 +122,58 @@ public class PlayerController {
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok(UnifiedResponse.success(item));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping
|
|
|
|
|
@CheckUser
|
|
|
|
|
public ResponseEntity<UnifiedResponse<List<Player>, Error>> getAllPlayers(@RequestParam String playerKey) {
|
|
|
|
|
var player = playerRepository.findByPlayerKey(playerKey).get();
|
|
|
|
|
var game = player.getGame();
|
|
|
|
|
|
|
|
|
|
var players = game.getPlayers();
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok(UnifiedResponse.success(players));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/send_item")
|
|
|
|
|
@CheckUser
|
|
|
|
|
public ResponseEntity<UnifiedResponse<Object, Error>> sendItem(@RequestParam String playerKey, @RequestParam Long itemId, @RequestParam Long playerId) {
|
|
|
|
|
var player = playerRepository.findByPlayerKey(playerKey).get();
|
|
|
|
|
|
|
|
|
|
var player2Optional = playerRepository.findById(playerId);
|
|
|
|
|
if (player2Optional.isEmpty()) {
|
|
|
|
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(UnifiedResponse.failure(new NotFoundError("Player with id " + playerId + " was not found!")));
|
|
|
|
|
}
|
|
|
|
|
var player2 = player2Optional.get();
|
|
|
|
|
|
|
|
|
|
var itemOptional = itemRepository.findById(itemId);
|
|
|
|
|
if (itemOptional.isEmpty()) {
|
|
|
|
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(UnifiedResponse.failure(new NotFoundError("Item with id " + itemId + " was not found!")));
|
|
|
|
|
}
|
|
|
|
|
var item = itemOptional.get();
|
|
|
|
|
|
|
|
|
|
if (item.getOwner() == null || !item.getOwner().getId().equals(player.getId())) {
|
|
|
|
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(UnifiedResponse.failure(new Error("You don't own this item!")));
|
|
|
|
|
}
|
|
|
|
|
if (player.getInventorySize() - player.getInventory().size() == 0) {
|
|
|
|
|
// Inventory is full
|
|
|
|
|
return ResponseEntity.status(HttpStatus.CONFLICT).body(UnifiedResponse.failure(new FullInventoryError("Player has full inventory!")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item.setOwner(player2);
|
|
|
|
|
itemRepository.save(item);
|
|
|
|
|
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
try {
|
|
|
|
|
var json = objectMapper.writeValueAsString(new ItemMoveMessageResponse(item.getItemType(), player2.getName()));
|
|
|
|
|
|
|
|
|
|
var message = new Message(player, json, MessageType.ITEM_MOVED);
|
|
|
|
|
|
|
|
|
|
player.getMessages().add(message);
|
|
|
|
|
playerRepository.save(player);
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok(UnifiedResponse.success(null));
|
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|