Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fighting game are now properly stored in the backend. Upon the requester winning the friend request fight, friends and chatroom will be made #75

Merged
merged 18 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
1150bc5
Merge branch 'main' of https://github.com/ChicoState/FightMe into fri…
nrakocevic Dec 2, 2024
eb8ff62
Merge branch 'main' of https://github.com/ChicoState/FightMe into fri…
nrakocevic Dec 6, 2024
fe839e6
Including statsProvider in implementation
nrakocevic Dec 6, 2024
a99210c
Default avatar was facing the wrong way
nrakocevic Dec 6, 2024
4917c97
Added get friend Request functionality, fleshed out fight game in the…
nrakocevic Dec 9, 2024
3119242
Merge branch 'main' of https://github.com/ChicoState/FightMe into fri…
nrakocevic Dec 9, 2024
a8b7b0f
Http requests with FightGame. Absolutely needs to be tested, though
nrakocevic Dec 9, 2024
14b0cd7
Will this compile?
nrakocevic Dec 9, 2024
2cc622d
Infernal project. Let me sleep already
nrakocevic Dec 9, 2024
e02cb6d
Does this solve null list?
nrakocevic Dec 9, 2024
f4d5521
Maybe this will solve null list
nrakocevic Dec 9, 2024
2b043b6
Any second now
nrakocevic Dec 9, 2024
3fc6541
I think it's working now
nrakocevic Dec 9, 2024
690fcee
FightGame is now a proper entity. SetMove and newTurn work.
nrakocevic Dec 10, 2024
8e03023
Small issue of buyableProfilePictures getting called when it was supp…
nrakocevic Dec 11, 2024
dd54d0e
Fight game works in chatrooms. Get requests working with it and we sh…
nrakocevic Dec 11, 2024
994c54e
Small change to spacing
nrakocevic Dec 12, 2024
4a80d6e
Friends and chatrooms are made when requester wins a fight. There are…
nrakocevic Dec 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.backend.backend.fightGame.Dto;

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

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class FightGameDto {
private Long id;
private Integer winnerID;
private Long requesterID;
private UserDto user1;
private UserDto user2;
private Integer user1HP;
private Integer user2HP;
private List<Move> user1Moves;
private List<Move> user2Moves;
}
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;
}
110 changes: 110 additions & 0 deletions backend/src/main/java/com/backend/backend/fightGame/FightGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.backend.backend.fightGame;
import jakarta.persistence.CascadeType;
import jakarta.persistence.CollectionTable;
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.OneToMany;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

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

import com.backend.backend.user.User;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "fight_games")
public class FightGame {

public enum Move {
ATTACK,
DEFENSE,
MAGIC,
NONE
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

// Until someone wins: 0
// If they draw: -1
@Column(name = "winner_id")
private Integer winnerID;

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

@ManyToOne
@JoinColumn(name = "fight_game_session_user_1")
private User user1;

@ManyToOne
@JoinColumn(name = "fight_game_session_user_2")
private User user2;

// Starts at 5?
@Column(name = "user_1_hp")
private Integer user1HP;

@Column(name = "user_2_hp")
private Integer user2HP;

@ElementCollection(targetClass = Move.class)
@CollectionTable(name = "game_user_1_moves", joinColumns = @JoinColumn(name = "game_id"))
@Column(name = "user_1_move")
@Enumerated(EnumType.STRING)
private List<Move> user1Moves;

@ElementCollection(targetClass = Move.class)
@CollectionTable(name = "game_user_2_moves", joinColumns = @JoinColumn(name = "game_id"))
@Enumerated(EnumType.STRING)
@Column(name = "user_2_move")
private List<Move> user2Moves;

@PrePersist
protected void onCreate() {
this.winnerID = 0;
this.user1HP = 5;
this.user2HP = 5;
this.user1Moves = Arrays.asList(Move.NONE);
this.user2Moves = Arrays.asList(Move.NONE);
}

public Boolean doesMoveHit(Move user1Move, Move user2Move) {
if (user1Move != Move.DEFENSE) {
if (user1Move == user2Move) {
return true;
}
if (user1Move == Move.ATTACK && user2Move == Move.MAGIC) {
return true;
}
if (user1Move == Move.MAGIC && user2Move == Move.DEFENSE) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.backend.backend.fightGame;

import com.backend.backend.fightGame.Dto.FightGameDto;
import com.backend.backend.friendrequest.FriendRequestDto;
import com.backend.backend.fightGame.FightGame.Move;
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 java.util.List;
import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

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

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

@GetMapping("{user1ID}/{user2ID}")
public ResponseEntity<FightGameDto> getFightGame(@PathVariable("user1ID") Long user1ID, @PathVariable("user2ID") Long user2ID) {
FightGameDto fightGameDto = fightGameService.getFightGame(user1ID, user2ID);
return new ResponseEntity<>(fightGameDto, HttpStatus.OK);
}

@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
@@ -0,0 +1,44 @@
package com.backend.backend.fightGame;

import java.util.stream.Collectors;

import com.backend.backend.fightGame.Dto.FightGameDto;
import com.backend.backend.fightGame.Dto.UserMoveDto;
import com.backend.backend.user.UserMapper;
import com.backend.backend.user.Dto.UserDto;
import com.backend.backend.user.User;
import java.util.List;

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.getRequesterID(),
user1,
user2,
fightGame.getUser1HP(),
fightGame.getUser2HP(),
fightGame.getUser1Moves(),
fightGame.getUser2Moves()
);
}

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.getRequesterID(),
user1,
user2,
fightGameDto.getUser1HP(),
fightGameDto.getUser2HP(),
fightGameDto.getUser1Moves(),
fightGameDto.getUser2Moves()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.backend.backend.fightGame;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.backend.backend.user.User;
import java.util.Optional;
import java.util.List;

@Repository
public interface FightGameRepository extends JpaRepository<FightGame, Long> {
List<FightGame> findByUser1AndUser2(User user1, User user2);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.backend.backend.fightGame;

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

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

public interface FightGameService {
FightGameDto createFightGame(long user1, long user2, long requesterID);
FightGameDto getFightGame(long user1ID, long user2ID);
FightGameDto declareWinner(long id);
FightGameDto setMove(long id, UserMoveDto userMoveDto);
Boolean doesMoveHit(Move user1Move, Move user2Move);
FightGameDto setNewTurn(long id);
}
Loading
Loading