Skip to content

Commit

Permalink
Basic room setting, fixes #87, #71, #69, #25
Browse files Browse the repository at this point in the history
  • Loading branch information
Zehao-Zhang committed Apr 9, 2024
1 parent 30ef456 commit b7dd40e
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 36 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package ch.uzh.ifi.hase.soprafs24.controller;
import ch.uzh.ifi.hase.soprafs24.entity.Room;
import ch.uzh.ifi.hase.soprafs24.entity.User;
import ch.uzh.ifi.hase.soprafs24.rest.dto.*;
import ch.uzh.ifi.hase.soprafs24.service.RoomService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import ch.uzh.ifi.hase.soprafs24.rest.mapper.DTOMapper;

/**
* User Controller
Expand All @@ -17,45 +20,47 @@
public class RoomController {


private final RoomService roomService;

RoomController(RoomService roomService) {
this.roomService = roomService;
}

@GetMapping("/games")
//This method is used to get all rooms in the lobby
@GetMapping("/games/lobby")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public List<RoomGetDTO> getAllRooms() {
// fetch all rooms in the internal representation,
List<Room> rooms = roomService.getRooms();
List<RoomGetDTO> roomGetDTOs = new ArrayList<>();

// convert each user to the API representation
for (Room room : rooms) {
roomGetDTOs.add(DTOMapper.INSTANCE.convertEntityToRoomGetDTO(room));
}
return roomGetDTOs;
}

@PostMapping("/games/room")
//This method is used to create a new room
@PostMapping("/games")
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public RoomGetDTO createRoom(@RequestBody RoomPostDTO roomPostDTO) {
// convert API room to internal representation
Room roomInput = DTOMapper.INSTANCE.convertRoomPostDTOtoEntity(roomPostDTO);
// create room
Room createdRoom = roomService.createRoom(roomInput);
// convert internal representation of room back to API
return null;
}

//Get method for getting one room
@GetMapping("/games/{roomId}")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public RoomGetDTO roomInfo (@PathVariable("roomId") Long roomId) {
return null;
return DTOMapper.INSTANCE.convertEntityToRoomGetDTO(createdRoom);
}


@PutMapping("/room/{roomId}/players")
@PutMapping("/games{roomId}")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public void enterRoom(@PathVariable("roomId") Long roomId, @RequestBody UserPutDTO userPutDTO) {
}

@PutMapping("/room/{roomId}/vote/{voterId}={voteeId}")
@ResponseStatus(HttpStatus.OK)
public void castVote(@PathVariable Long roomId, @PathVariable Long voterId, @PathVariable Long voteeId) {
public void enterRoom(@PathVariable String roomId,@RequestBody UserPutDTO userPutDTO) {
User userInput = DTOMapper.INSTANCE.convertUserPutDTOtoEntity(userPutDTO);
Room enteredRoom = roomService.findRoomById(roomId);
roomService.enterRoom(enteredRoom, userInput);
}

@PostMapping("/games/guard")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.uzh.ifi.hase.soprafs24.controller;

import ch.uzh.ifi.hase.soprafs24.entity.User;
import ch.uzh.ifi.hase.soprafs24.rest.dto.RoomGetDTO;
import ch.uzh.ifi.hase.soprafs24.rest.dto.UserGetDTO;
import ch.uzh.ifi.hase.soprafs24.rest.dto.UserPostDTO;
import ch.uzh.ifi.hase.soprafs24.rest.dto.UserPutDTO;
Expand Down Expand Up @@ -103,4 +104,6 @@ public void userEditProfile(@PathVariable("userId") String userId, @RequestBody
User userInput = DTOMapper.INSTANCE.convertUserPutDTOtoEntity(userPutDTO);
userService.userEditProfile(userInput);
}


}
18 changes: 12 additions & 6 deletions src/main/java/ch/uzh/ifi/hase/soprafs24/entity/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public class Room implements Serializable {

private Theme theme;

private long roomOwnerId;
private String roomOwnerId;

private int maxPlayersNum;

private RoomProperty roomProperty;

private List<Long> roomPlayersList = new ArrayList<>();
private List<String> roomPlayersList = new ArrayList<>();

private List<Long> alivePlayersList = new ArrayList<>();

Expand All @@ -51,11 +51,11 @@ public void setTheme(Theme theme) {
this.theme = theme;
}

public long getRoomOwnerId() {
public String getRoomOwnerId() {
return roomOwnerId;
}

public void setRoomOwnerId(long roomOwnerId) {
public void setRoomOwnerId(String roomOwnerId) {
this.roomOwnerId = roomOwnerId;
}

Expand All @@ -75,11 +75,11 @@ public void setRoomProperty(RoomProperty roomProperty) {
this.roomProperty = roomProperty;
}

public List<Long> getRoomPlayersList() {
public List<String> getRoomPlayersList() {
return roomPlayersList;
}

public void setRoomPlayersList(List<Long> roomPlayersList) {
public void setRoomPlayersList(List<String> roomPlayersList) {
this.roomPlayersList = roomPlayersList;
}

Expand Down Expand Up @@ -114,4 +114,10 @@ public Map<Long, Long> getVotingResult() {
public void setVotingResult(Map<Long, Long> votingResult) {
this.votingResult = votingResult;
}

public void addRoomPlayerList(String id) {
if (id!=null) {
this.roomPlayersList.add(id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Optional;


@Repository("roomRepository")
public interface RoomRepository extends MongoRepository<Room, String> {
Optional<Room> findByRoomId(String roomId);

@Override
List<Room> findAll();
}

30 changes: 27 additions & 3 deletions src/main/java/ch/uzh/ifi/hase/soprafs24/rest/mapper/DTOMapper.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package ch.uzh.ifi.hase.soprafs24.rest.mapper;

import ch.uzh.ifi.hase.soprafs24.entity.Room;
import ch.uzh.ifi.hase.soprafs24.entity.User;
import ch.uzh.ifi.hase.soprafs24.rest.dto.UserGetDTO;
import ch.uzh.ifi.hase.soprafs24.rest.dto.UserPostDTO;
import ch.uzh.ifi.hase.soprafs24.rest.dto.UserPutDTO;
import ch.uzh.ifi.hase.soprafs24.rest.dto.*;

import org.mapstruct.*;
import org.mapstruct.factory.Mappers;
Expand Down Expand Up @@ -40,4 +39,29 @@ public interface DTOMapper {
@Mapping(source = "username", target = "username")
@Mapping(source = "birthday", target = "birthday")
User convertUserPutDTOtoEntity(UserPutDTO userPutDTO);


@Mapping(source = "roomId", target = "roomId")
@Mapping(source = "theme", target = "theme")
@Mapping(source = "roomProperty", target = "roomProperty")
@Mapping(source = "roomOwnerId", target = "roomOwnerId")
//@Mapping(source = "roomPlayers", target = "roomPlayers")
//@Mapping(source = "token", target = "token")
Room convertRoomPostDTOtoEntity(RoomPostDTO roomPostDTO);

@Mapping(source = "roomId", target = "roomId")
@Mapping(source = "theme", target = "theme")
@Mapping(source = "roomProperty", target = "roomProperty")
@Mapping(source = "roomOwnerId", target = "roomOwnerId")
@Mapping(source = "roomPlayersList", target = "roomPlayersList")
//@Mapping(source = "token", target = "token")
RoomGetDTO convertEntityToRoomGetDTO(Room room);

@Mapping(source = "roomId", target = "roomId")
@Mapping(source = "theme", target = "theme")
@Mapping(source = "roomProperty", target = "roomProperty")
@Mapping(source = "roomOwnerId", target = "roomOwnerId")
@Mapping(source = "roomPlayersList", target = "roomPlayersList")
//@Mapping(source = "token", target = "token")
Room convertRoomPutDTOtoEntity(RoomPutDTO roomPutDTO);
}
35 changes: 28 additions & 7 deletions src/main/java/ch/uzh/ifi/hase/soprafs24/service/RoomService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package ch.uzh.ifi.hase.soprafs24.service;
import ch.uzh.ifi.hase.soprafs24.constant.RoomProperty;
import ch.uzh.ifi.hase.soprafs24.entity.Room;
import ch.uzh.ifi.hase.soprafs24.entity.User;
import ch.uzh.ifi.hase.soprafs24.repository.RoomRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;

import java.io.IOException;
import java.util.*;
Expand All @@ -18,21 +25,35 @@
@Transactional
public class RoomService {


public RoomService() {
private final Logger log = LoggerFactory.getLogger(UserService.class);
private final RoomRepository roomRepository;
public RoomService(@Qualifier("roomRepository") RoomRepository roomRepository) {
this.roomRepository = roomRepository;
}

public List<Room> getRooms() {
return this.roomRepository.findAll();
}

//Here we create a new room and we need to set the room property and theme according to the input from client
public Room createRoom() {
return null;
//Here we create a new room, and we need to set the room property and theme according to the input from client
public Room createRoom(Room newRoom){
newRoom.setRoomProperty(RoomProperty.WAITING);
newRoom.addRoomPlayerList(newRoom.getRoomOwnerId());
newRoom = roomRepository.save(newRoom);
roomRepository.save(newRoom);
log.debug("Created Information for Room: {}", newRoom);
return newRoom;
}

public Room findRoomById() {
return null;
public Room findRoomById(String roomId){
return roomRepository.findByRoomId(roomId).get();
}

public void enterRoom(Room room, User user){
if (room.getRoomPlayersList().size()<room.getMaxPlayersNum()){
room.addRoomPlayerList(user.getId());
}
else throw new ResponseStatusException(HttpStatus.FORBIDDEN, "This room is full!");
}


Expand Down

0 comments on commit b7dd40e

Please sign in to comment.