feat: 游戏房间
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
package com.webgame.webgamebackend.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaIgnore;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.webgame.webgamebackend.common.dto.ApiResponse;
|
||||
import com.webgame.webgamebackend.common.dto.game.*;
|
||||
import com.webgame.webgamebackend.service.GameService;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* 游戏控制器
|
||||
*
|
||||
* 管理游戏列表查询、房间的创建/加入/离开/踢人/准备/开始等操作。
|
||||
*/
|
||||
@Validated
|
||||
@RestController
|
||||
@RequestMapping("/game")
|
||||
@RequiredArgsConstructor
|
||||
public class GameController {
|
||||
|
||||
private final GameService gameService;
|
||||
|
||||
/**
|
||||
* 获取游戏列表
|
||||
*
|
||||
* 无需登录即可调用。
|
||||
*/
|
||||
@SaIgnore
|
||||
@GetMapping("/list")
|
||||
public ApiResponse<GameListResponse> list() {
|
||||
GameListResponse result = gameService.getGameList();
|
||||
return ApiResponse.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取房间列表
|
||||
*
|
||||
* 支持按状态筛选。无需登录即可调用。
|
||||
*
|
||||
* @param gameId 游戏 ID(必填)
|
||||
* @param status 状态筛选(可选)
|
||||
*/
|
||||
@SaIgnore
|
||||
@GetMapping("/room/list")
|
||||
public ApiResponse<RoomListResponse> roomList(
|
||||
@RequestParam String gameId,
|
||||
@RequestParam(required = false) String status) {
|
||||
RoomListResponse result = gameService.getRoomList(gameId, status);
|
||||
return ApiResponse.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建房间
|
||||
*
|
||||
* 需登录。房主自动加入房间并设为已准备。
|
||||
*/
|
||||
@PostMapping("/room/create")
|
||||
public ApiResponse<CreateRoomResponse> createRoom(@Valid @RequestBody CreateRoomRequest request) {
|
||||
String userId = StpUtil.getLoginIdAsString();
|
||||
CreateRoomResponse result = gameService.createRoom(request, userId);
|
||||
return ApiResponse.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加入房间
|
||||
*
|
||||
* 需登录。公开房间无需传密码。
|
||||
*/
|
||||
@PostMapping("/room/join")
|
||||
public ApiResponse<RoomItemResponse> joinRoom(@Valid @RequestBody JoinRoomRequest request) {
|
||||
String userId = StpUtil.getLoginIdAsString();
|
||||
RoomItemResponse result = gameService.joinRoom(request, userId);
|
||||
return ApiResponse.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 离开房间
|
||||
*
|
||||
* 需登录。房主离开时顺位转让。无人时房间自动结束。
|
||||
*/
|
||||
@PostMapping("/room/leave")
|
||||
public ApiResponse<Void> leaveRoom(@Valid @RequestBody LeaveRoomRequest request) {
|
||||
String userId = StpUtil.getLoginIdAsString();
|
||||
gameService.leaveRoom(request.roomId(), userId);
|
||||
return ApiResponse.ok(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 踢出玩家
|
||||
*
|
||||
* 需登录。仅房主可操作。
|
||||
*/
|
||||
@PostMapping("/room/kick")
|
||||
public ApiResponse<Void> kickPlayer(@Valid @RequestBody KickPlayerRequest request) {
|
||||
String ownerId = StpUtil.getLoginIdAsString();
|
||||
gameService.kickPlayer(request.roomId(), ownerId, request.userId());
|
||||
return ApiResponse.ok(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换准备状态
|
||||
*
|
||||
* 需登录。在等待中的房间里切换准备/取消准备。
|
||||
*/
|
||||
@PostMapping("/room/ready")
|
||||
public ApiResponse<Boolean> toggleReady(@Valid @RequestBody ReadyRequest request) {
|
||||
String userId = StpUtil.getLoginIdAsString();
|
||||
boolean ready = gameService.toggleReady(request.roomId(), userId);
|
||||
return ApiResponse.ok(ready);
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始游戏
|
||||
*
|
||||
* 需登录。仅房主可操作。需全部玩家准备且人数 >= 2。
|
||||
* 开始后扣除所有玩家的底注。
|
||||
*/
|
||||
@PostMapping("/room/start")
|
||||
public ApiResponse<Void> startGame(@Valid @RequestBody StartGameRequest request) {
|
||||
String ownerId = StpUtil.getLoginIdAsString();
|
||||
gameService.startGame(request.roomId(), ownerId);
|
||||
return ApiResponse.ok(null);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,14 @@
|
||||
package com.webgame.webgamebackend.controller;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.webgame.webgamebackend.common.dto.ApiResponse;
|
||||
import com.webgame.webgamebackend.common.dto.user.UpdateUserInfoRequest;
|
||||
import com.webgame.webgamebackend.common.dto.user.UserInfoResponse;
|
||||
import com.webgame.webgamebackend.service.UserService;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* 用户控制器
|
||||
@@ -28,4 +29,16 @@ public class UserController {
|
||||
UserInfoResponse result = userService.getCurrentUserInfo();
|
||||
return ApiResponse.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新当前登录用户信息
|
||||
*
|
||||
* 所有字段可选,传什么更新什么。
|
||||
*/
|
||||
@PutMapping("/info")
|
||||
public ApiResponse<UserInfoResponse> updateInfo(@Valid @RequestBody UpdateUserInfoRequest request) {
|
||||
String accountId = StpUtil.getLoginIdAsString();
|
||||
UserInfoResponse result = userService.updateUserInfo(accountId, request);
|
||||
return ApiResponse.ok(result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user