Skip to content

Commit

Permalink
Http requests with FightGame. Absolutely needs to be tested, though
Browse files Browse the repository at this point in the history
  • Loading branch information
nrakocevic committed Dec 9, 2024
1 parent 3119242 commit a8b7b0f
Show file tree
Hide file tree
Showing 15 changed files with 233 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.backend.backend.fightGame.Dto;

import com.backend.backend.fightGame.FightGame.Move;
import com.backend.backend.user.User;
import com.backend.backend.user.Dto.UserDto;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -15,11 +15,12 @@
@NoArgsConstructor
@AllArgsConstructor
public class FightGameDto {
private long id;
private Long id;
private Integer winnerID;
private User user1;
private User user2;
private Long requesterID;
private UserDto user1;
private UserDto user2;
private Integer user1HP;
private Integer user2HP;
private Map<Integer, List<Move>> moves;
private Map<Long, List<Move>> moves;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.backend.backend.fightGame.Dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import com.backend.backend.fightGame.FightGame.Move;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class UserMoveDto {
private Long userID;
Move move;
}
26 changes: 20 additions & 6 deletions backend/src/main/java/com/backend/backend/fightGame/FightGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MapKeyColumn;
import jakarta.persistence.OneToOne;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Arrays;
import java.util.Map;
import java.util.List;

Expand Down Expand Up @@ -48,10 +48,23 @@ public enum Move {
@Column(name = "winner_id")
private Integer winnerID;

@Column(name = "user_1")
@Column(name = "requester_id")
private Long requesterID;

@ManyToOne
@JoinTable(
name = "fight_game_session_user_1", // Name of the join table
joinColumns = @JoinColumn(name = "game_id"), // Current entity's column
inverseJoinColumns = @JoinColumn(name = "user_id") // Friend entity's column
)
private User user1;

@Column(name = "user_2")
@ManyToOne
@JoinTable(
name = "fight_game_session_user_2", // Name of the join table
joinColumns = @JoinColumn(name = "game_id"), // Current entity's column
inverseJoinColumns = @JoinColumn(name = "user_id") // Friend entity's column
)
private User user2;

// Starts at 5?
Expand All @@ -66,7 +79,8 @@ public enum Move {
@Column(name="user_move") //value
@CollectionTable(name="game_user_moves",
joinColumns=@JoinColumn(name="game_id"))//fk from Areas->contacts.id
private Map<Integer, List<Move>> moves;
@Enumerated(EnumType.STRING)
private Map<Long, List<Move>> moves;

@PrePersist
protected void onCreate() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.backend.backend.fightGame;

import com.backend.backend.fightGame.Dto.FightGameDto;
import com.backend.backend.friendrequest.FriendRequestDto;
import com.backend.backend.fightGame.FightGameService;
import com.backend.backend.user.User;
import com.backend.backend.user.UserMapper;
import com.backend.backend.user.Dto.UserDto;
import com.backend.backend.fightGame.Dto.UserMoveDto;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -18,25 +23,39 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import com.backend.backend.fightGame.FightGameService;

@RestController
@RequestMapping("/api/fightgames")
@AllArgsConstructor
// @CrossOrigin(origins = "http://localhost:60966")
@CrossOrigin(origins = "*")
public class FightGameController {
private FightGameService fightGameService;

@PostMapping
public ResponseEntity<FightGameDto> createFightGame(@RequestBody FightGameDto fightGameDto) {
FightGameDto savedFightGame = fightGameService.createFightGame(fightGameDto);
Long user1 = fightGameDto.getUser1().getId();
Long user2 = fightGameDto.getUser2().getId();
Long requestID = fightGameDto.getRequesterID();
FightGameDto savedFightGame = fightGameService.createFightGame(user1, user2, requestID);
return new ResponseEntity<>(savedFightGame, HttpStatus.CREATED);
}

@PutMapping("{id}/setMove")
public ResponseEntity<FightGameDto> setMove(@PathVariable("id") Long id, @RequestBody UserMoveDto userMoveDto) {
FightGameDto updatedGame = fightGameService.setMove(id, userMoveDto);
return new ResponseEntity<>(updatedGame, HttpStatus.OK);
}

@PutMapping("newTurn")
public ResponseEntity<FightGameDto> setNewTurn(@RequestBody FightGameDto fightGameDto) {
long id = fightGameDto.getId();
FightGameDto updatedGame = fightGameService.setNewTurn(id);
return new ResponseEntity<>(updatedGame, HttpStatus.OK);
}

@PutMapping("{id}/winner")
public ResponseEntity<FightGameDto> declareWinner(@PathVariable("id") Long id) {
FightGameDto updatedGame = fightGameService.declareWinner(id);
return new ResponseEntity<>(updatedGame, HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
package com.backend.backend.fightGame;

import java.util.List;
import java.util.stream.Collectors;

import com.backend.backend.fightGame.FightGame;
import com.backend.backend.fightGame.Dto.FightGameDto;
import com.backend.backend.user.UserMapper;
import com.backend.backend.user.Dto.UserDto;
import com.backend.backend.user.User;

public class FightGameMapper {
public static FightGameDto mapToFightGameDto(FightGame fightGame) {
UserDto user1 = UserMapper.mapToUserDto(fightGame.getUser1());
UserDto user2 = UserMapper.mapToUserDto(fightGame.getUser2());
return new FightGameDto(
fightGame.getId(),
fightGame.getWinnerID(),
fightGame.getUser1(),
fightGame.getUser2(),
fightGame.getRequesterID(),
user1,
user2,
fightGame.getUser1HP(),
fightGame.getUser2HP(),
fightGame.getMoves()
);
}

public static FightGame mapToFightGame(FightGameDto fightGameDto) {
User user1 = UserMapper.mapToUser(fightGameDto.getUser1());
User user2 = UserMapper.mapToUser(fightGameDto.getUser2());
return new FightGame(
fightGameDto.getId(),
fightGameDto.getWinnerID(),
fightGameDto.getUser1(),
fightGameDto.getUser2(),
fightGameDto.getRequesterID(),
user1,
user2,
fightGameDto.getUser1HP(),
fightGameDto.getUser2HP(),
fightGameDto.getMoves()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.backend.backend.fightGame.FightGame;

@Repository
public interface FightGameRepository extends JpaRepository<FightGame, Long> {

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.backend.backend.fightGame;

import org.springframework.web.bind.annotation.RequestBody;

import com.backend.backend.fightGame.Dto.FightGameDto;
import com.backend.backend.fightGame.Dto.UserMoveDto;

public interface FightGameService {
FightGameDto createFightGame(FightGameDto fightGameDto);
FightGameDto createFightGame(long user1, long user2, long requestID);
FightGameDto declareWinner(long id);
FightGameDto setMove(long id, UserMoveDto userMoveDto);
FightGameDto setNewTurn(long id);
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,101 @@
package com.backend.backend.fightGame;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.backend.backend.ResourceNotFoundException;
import com.backend.backend.chatroom.ChatroomRepository;
import com.backend.backend.chatroom.ChatroomService;
import com.backend.backend.fightGame.FightGame;
import com.backend.backend.fightGame.FightGameRepository;
import com.backend.backend.fightGame.FightGameService;
import com.backend.backend.user.UserMapper;
import com.backend.backend.user.UserRepository;
import com.backend.backend.user.UserService;
import com.backend.backend.user.User;
import com.backend.backend.user.Dto.FriendDto;
import com.backend.backend.user.Dto.UserDto;
import com.backend.backend.friendrequest.FriendRequest;

import lombok.AllArgsConstructor;

import com.backend.backend.fightGame.Dto.FightGameDto;
import com.backend.backend.fightGame.Dto.UserMoveDto;
import com.backend.backend.friendrequest.FriendRequestRepository;
import com.backend.backend.friendrequest.FriendRequestService;
import com.backend.backend.fightGame.FightGame.Move;

@Service
@AllArgsConstructor
public class FightGameServiceImpl implements FightGameService{
private FightGameRepository fightGameRepository;
private FriendRequestRepository friendRequestRepository;
private FriendRequestService friendRequestService;
private UserRepository userRepository;
private UserService userService;
private ChatroomService chatroomService;

// START OF ALL CREATE

@Override
public FightGameDto createFightGame(FightGameDto fightGameDto) {
FightGame fightGame = FightGameMapper.mapToFightGame(fightGameDto);
public FightGameDto createFightGame(long user1ID, long user2ID, long requestID) {
User user1 = userRepository.findById(user1ID)
.orElseThrow(() -> new ResourceNotFoundException("User not found" + user1ID));
User user2 = userRepository.findById(user2ID)
.orElseThrow(() -> new ResourceNotFoundException("User not found" + user2ID));
if (requestID != 0 && requestID != user1ID && requestID != user2ID) {
throw new ResourceNotFoundException("A faulty request id was given");
}
FightGame fightGame = new FightGame();
fightGame.setUser1(user1);
fightGame.setUser2(user2);
fightGame.setRequesterID(requestID);
Map<Long, List<Move>> initMoves = new HashMap<>();
initMoves.put(user1.getId(), Arrays.asList(Move.NONE));
initMoves.put(user2.getId(), Arrays.asList(Move.NONE));
fightGame.setMoves(initMoves);
FightGame savedGame = fightGameRepository.save(fightGame);
return FightGameMapper
.mapToFightGameDto(savedGame);
}

@Override
public FightGameDto setMove(long id, UserMoveDto userMove) {
FightGame fightGame = fightGameRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Game not found " + id));
Map<Long, List<Move>> moves = fightGame.getMoves();
moves.get(userMove.getUserID()).set(moves.get(userMove.getUserID()).size() - 1, userMove.getMove());
fightGame.setMoves(moves);
FightGame savedGame = fightGameRepository.save(fightGame);
return FightGameMapper
.mapToFightGameDto(savedGame);
}

@Override
public FightGameDto setNewTurn(long id) {
FightGame fightGame = fightGameRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Game not found " + id));
Move user1Move = fightGame.getMoves().get(fightGame.getUser1().getId()).getLast();
Move user2Move = fightGame.getMoves().get(fightGame.getUser2().getId()).getLast();
if (user1Move == Move.NONE || user2Move == Move.NONE) {
throw new ResourceNotFoundException("The turn is not over");
}
else {
Map<Long, List<Move>> moves = fightGame.getMoves();
moves.get(fightGame.getUser1().getId()).add(Move.NONE);
moves.get(fightGame.getUser2().getId()).add(Move.NONE);
fightGame.setMoves(moves);
FightGame savedGame = fightGameRepository.save(fightGame);
return FightGameMapper
.mapToFightGameDto(savedGame);
}
}

@Override
public FightGameDto declareWinner(long id) {
FightGame fightGame = fightGameRepository.findById(id)
Expand All @@ -40,6 +110,18 @@ else if (fightGame.getUser1HP() != 0 && fightGame.getUser2HP() == 0) {
else {
fightGame.setWinnerID(-1);
}
if (fightGame.getRequesterID() != 0) {
if (fightGame.getWinnerID() == fightGame.getRequesterID().intValue()) {

userService.addFriend(fightGame.getRequesterID(), new FriendDto(fightGame.getUser1().getId().intValue() == fightGame.getWinnerID() ? fightGame.getUser2().getId() : fightGame.getUser1().getId()));
chatroomService.createChatroom(null);
}
else {
FriendRequest incoming = friendRequestRepository.findByFromUserIDAndToUserID(fightGame.getRequesterID(), Long.valueOf(fightGame.getWinnerID()))
.orElseThrow(() -> new ResourceNotFoundException("FriendRequest not found " + fightGame.getRequesterID() + " " + Long.valueOf(fightGame.getWinnerID())));
friendRequestService.rejectFriendRequest(incoming.getId());
}
}
FightGame savedGame = fightGameRepository.save(fightGame);
return FightGameMapper.mapToFightGameDto(savedGame);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

import com.backend.backend.fightGame.FightGame;
import com.backend.backend.fightGame.Dto.FightGameDto;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -28,7 +28,7 @@ public class UserDto {
private Long theme;
private List<Long> unlockedThemes;
private List<Long> friends; //changed to list long
private List<FightGame> gameSessions;
private List<FightGameDto> gameSessions;
private String email;
private String password;

Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/java/com/backend/backend/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class User {
@JoinTable(
name = "user_fight_game_sessions", // Name of the join table
joinColumns = @JoinColumn(name = "user_id"), // Current entity's column
inverseJoinColumns = @JoinColumn(name = "fight_game_id") // Friend entity's column
inverseJoinColumns = @JoinColumn(name = "game_id") // Friend entity's column
)
private List<FightGame> gameSessions;

Expand Down
Loading

0 comments on commit a8b7b0f

Please sign in to comment.