今日目標,「退出房間」和「踢出房間」的功能。
其實「退出房間」跟「踢出房間」是同個方法,指定 username 將其踢出去即可,而退出房間小弟也是用 API 的調用來實現。
owner
增加 @Setter
註解destroy()
用於銷毀房間,加入的片段程式碼為:
public void destroy(String roomId) {
this.roomList.remove(roomId);
}
quitRoom
,加入的片段程式碼為:
public boolean quitRoom(String user, String roomId) {
// 根本沒有這個 user
if (!this.userStatus.containsUser(user)) {
return false;
}
// user 根本不在房間
if (!this.userStatus.isUserInRoom(user)) {
return false;
}
// 這個房號不存在
if (!this.roomList.containRoomId(roomId)) {
return false;
}
Room room = roomList.getRoomById(roomId);
// 如果退出的是房主,要另外處理
if (room.getOwner().equals(user)) {
// 如果房間人數等於 1,表示整個房間只有房主,那就需要把這個房間銷毀
// 否則,從其他成員選擇一人擔任新房主
if (room.count() == 1) {
this.roomList.destroy(roomId);
}
else {
String newOwner = room.getAllMembers().get(1);
room.setOwner(newOwner);
room.removeGuest(newOwner);
this.userStatus.setUserReady(newOwner, false);
}
}
else {
room.removeGuest(user);
}
this.userStatus.setUserInRoom(user, false);
this.userStatus.setUserRoomId(user, "");
Map<String, Object> response = new HashMap<>();
response.put("roomInfo", room.getInfo());
simpMessagingTemplate.convertAndSendToUser(user, "/queue/room-info", response);
return true;
}
package com.example.room;
import lombok.Getter;
import lombok.Setter;
@Getter @Setter
public class UserQuitRoomMessage {
private String username;
private String roomId;
}
quitRoomProcess()
的方法,加入的片段程式碼為:
@PostMapping("/room/quit")
@ResponseBody
public ResponseEntity<Map<String, Object>> quitRoomProcess(UserQuitRoomMessage userQuitRoomMessage) {
Map<String, Object> response = new HashMap<>();
HttpStatus httpStatus;
String message;
if (!userService.isLogin()) {
httpStatus = HttpStatus.FORBIDDEN;
message = "未登入";
response.put("message", message);
return ResponseEntity.status(httpStatus).body(response);
}
String username = userQuitRoomMessage.getUsername();
String roomId = userQuitRoomMessage.getRoomId();
String me = userService.getUsername();
// 避免有人利用 API 在非房主的情況下把其他人踢掉
if (!username.equals(me) && !roomList.getRoomById(roomId).getOwner().equals(me)) {
httpStatus = HttpStatus.FORBIDDEN;
message = "權限不足";
response.put("message", message);
return ResponseEntity.status(httpStatus).body(response);
}
// 將使用者移除
if (roomService.quitRoom(username, roomId)) {
httpStatus = HttpStatus.OK;
message = "成功";
roomService.sendRoomInfo(roomId);
}
else {
httpStatus = HttpStatus.BAD_REQUEST;
message = "Error";
}
response.put("message", message);
return ResponseEntity.status(httpStatus).body(response);
}
room.js
,新增 quitRoom()
的方法,加入的片段程式碼為:
// 綁定退出圖案的按鈕
$("#quit-room-button").click(() => {
quitRoom(myUsername);
})
function quitRoom(username) {
let data = {
username : username,
roomId : roomId,
};
jq.post(
"/room/quit",
data,
(response) => {
console.log(response);
}
)
.fail(function(e) {
console.log(e);
})
}
今天實作了退出和踢出的功能,也因為之前思考不周,所以又回去補了相關類別的操作