完善联机框架

This commit is contained in:
石皮幼鸟 2024-06-16 00:25:47 +08:00
parent d83e912d5b
commit 92d868d459
7 changed files with 44 additions and 30 deletions

View File

@ -31,4 +31,13 @@ public class LetterAction implements Serializable {
this.type = type; this.type = type;
this.letter = letter; this.letter = letter;
} }
public LetterAction(LetterActionType type, long letterId) {
this.type = type;
this.letterId = letterId;
}
public LetterAction(LetterActionType type) {
this.type = type;
}
} }

View File

@ -17,6 +17,7 @@ public class PlayerInGame implements Serializable {
private int health; private int health;
private String currentWord; private String currentWord;
private String currentAnswer; private String currentAnswer;
private long dropTime;
public PlayerInGame(String userId, User user) { public PlayerInGame(String userId, User user) {
this.userId = userId; this.userId = userId;

View File

@ -8,8 +8,8 @@ import java.io.Serializable;
@Data @Data
public class PlayerInput implements Serializable { public class PlayerInput implements Serializable {
public InputKeyType key; private InputKeyType key;
public InputState state; private InputState state;
public PlayerInput(InputKeyType key, InputState state) { public PlayerInput(InputKeyType key, InputState state) {
this.key = key; this.key = key;

View File

@ -7,5 +7,6 @@ package com.example.catchTheLetters.enums;
*/ */
public enum LetterActionType { public enum LetterActionType {
CREATE, CREATE,
GET GET,
CLEAR
} }

View File

@ -69,9 +69,6 @@ public class WebSocketHandler extends TextWebSocketHandler {
case CANCEL_START: case CANCEL_START:
roomService.cancelStartGame(gameMessage.getRoomId(), session); roomService.cancelStartGame(gameMessage.getRoomId(), session);
break; break;
case END:
roomService.endGame(gameMessage.getRoomId());
break;
default: default:
roomService.sendMessage(session, new GameMessage<>(MessageType.ERROR, "未知的房间操作")); roomService.sendMessage(session, new GameMessage<>(MessageType.ERROR, "未知的房间操作"));
} }

View File

@ -46,12 +46,6 @@ public interface RoomService {
*/ */
void cancelStartGame(long roomId, WebSocketSession session); void cancelStartGame(long roomId, WebSocketSession session);
/**
* 结束游戏
* @param roomId 房间号
*/
void endGame(long roomId);
/** /**
* 处理玩家输入 * 处理玩家输入
* @param roomId 房间号 * @param roomId 房间号

View File

@ -1,10 +1,7 @@
package com.example.catchTheLetters.service.impl; package com.example.catchTheLetters.service.impl;
import com.example.catchTheLetters.entity.*; import com.example.catchTheLetters.entity.*;
import com.example.catchTheLetters.enums.LetterActionType; import com.example.catchTheLetters.enums.*;
import com.example.catchTheLetters.enums.MessageType;
import com.example.catchTheLetters.enums.RoomActionType;
import com.example.catchTheLetters.enums.RoomStatus;
import com.example.catchTheLetters.model.vo.Letter; import com.example.catchTheLetters.model.vo.Letter;
import com.example.catchTheLetters.service.AuthService; import com.example.catchTheLetters.service.AuthService;
import com.example.catchTheLetters.service.RoomService; import com.example.catchTheLetters.service.RoomService;
@ -143,25 +140,30 @@ public class RoomServiceImpl implements RoomService {
sendMessage(player, message); sendMessage(player, message);
} }
@Override
public void endGame(long roomId) {
var room = rooms.get(roomId);
room.setStatus(RoomStatus.WAITING);
// 发送消息通知所有玩家游戏结束
var message = new GameMessage<>(MessageType.ROOM, new RoomAction(RoomActionType.END));
for (var player : room.getPlayers().keySet())
sendMessage(player, message);
}
@Override @Override
public void handleInput(long roomId, WebSocketSession session, PlayerInput input) { public void handleInput(long roomId, WebSocketSession session, PlayerInput input) {
var message = new GameMessage<>(MessageType.INPUT, input); var message = new GameMessage<>(MessageType.INPUT, input);
var room = rooms.get(roomId); var room = rooms.get(roomId);
var players = room.getPlayers(); var players = room.getPlayers();
// 发送消息通知其他玩家有人输入了
for (var player : players.keySet()) if (input.getKey() == InputKeyType.SPACE) {
if (player != session) var player = players.get(session);
sendMessage(player, message); // 如果是空格按下则记录松开则获取记录时间并判断是否有1秒有则对玩家发送清空答案的消息并清空答案
if (input.getState() == InputState.PRESS)
player.setDropTime(System.currentTimeMillis());
else {
var dropTime = player.getDropTime();
if (System.currentTimeMillis() - dropTime >= 1000) {
player.clearCurrentAnswer();
sendMessage(session, new GameMessage<>(MessageType.LETTER, new LetterAction(LetterActionType.CLEAR)));
}
}
} else {
// 如果是移动发送消息通知其他玩家有人输入了
for (var player : players.keySet())
if (player != session)
sendMessage(player, message);
}
} }
@Override @Override
@ -291,4 +293,14 @@ public class RoomServiceImpl implements RoomService {
// 游戏结束后需要向玩家发送排行榜数据用户ID分数排名 // 游戏结束后需要向玩家发送排行榜数据用户ID分数排名
// 最大的问题丢包后如何处理比如其他玩家按下按键后自己没有接收到松开消息导致其他玩家在画面中一直持续运动网络连接稳定后不同C端的玩家位置不一致 // 最大的问题丢包后如何处理比如其他玩家按下按键后自己没有接收到松开消息导致其他玩家在画面中一直持续运动网络连接稳定后不同C端的玩家位置不一致
} }
public void endGame(long roomId) {
var room = rooms.get(roomId);
room.setStatus(RoomStatus.WAITING);
// 发送消息通知所有玩家游戏结束
var message = new GameMessage<>(MessageType.ROOM, new RoomAction(RoomActionType.END));
for (var player : room.getPlayers().keySet())
sendMessage(player, message);
// TODO 初始化玩家数据
}
} }