完善联机框架
This commit is contained in:
parent
d83e912d5b
commit
92d868d459
|
@ -31,4 +31,13 @@ public class LetterAction implements Serializable {
|
|||
this.type = type;
|
||||
this.letter = letter;
|
||||
}
|
||||
|
||||
public LetterAction(LetterActionType type, long letterId) {
|
||||
this.type = type;
|
||||
this.letterId = letterId;
|
||||
}
|
||||
|
||||
public LetterAction(LetterActionType type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ public class PlayerInGame implements Serializable {
|
|||
private int health;
|
||||
private String currentWord;
|
||||
private String currentAnswer;
|
||||
private long dropTime;
|
||||
|
||||
public PlayerInGame(String userId, User user) {
|
||||
this.userId = userId;
|
||||
|
|
|
@ -8,8 +8,8 @@ import java.io.Serializable;
|
|||
|
||||
@Data
|
||||
public class PlayerInput implements Serializable {
|
||||
public InputKeyType key;
|
||||
public InputState state;
|
||||
private InputKeyType key;
|
||||
private InputState state;
|
||||
|
||||
public PlayerInput(InputKeyType key, InputState state) {
|
||||
this.key = key;
|
||||
|
|
|
@ -7,5 +7,6 @@ package com.example.catchTheLetters.enums;
|
|||
*/
|
||||
public enum LetterActionType {
|
||||
CREATE,
|
||||
GET
|
||||
GET,
|
||||
CLEAR
|
||||
}
|
||||
|
|
|
@ -69,9 +69,6 @@ public class WebSocketHandler extends TextWebSocketHandler {
|
|||
case CANCEL_START:
|
||||
roomService.cancelStartGame(gameMessage.getRoomId(), session);
|
||||
break;
|
||||
case END:
|
||||
roomService.endGame(gameMessage.getRoomId());
|
||||
break;
|
||||
default:
|
||||
roomService.sendMessage(session, new GameMessage<>(MessageType.ERROR, "未知的房间操作"));
|
||||
}
|
||||
|
|
|
@ -46,12 +46,6 @@ public interface RoomService {
|
|||
*/
|
||||
void cancelStartGame(long roomId, WebSocketSession session);
|
||||
|
||||
/**
|
||||
* 结束游戏
|
||||
* @param roomId 房间号
|
||||
*/
|
||||
void endGame(long roomId);
|
||||
|
||||
/**
|
||||
* 处理玩家输入
|
||||
* @param roomId 房间号
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
package com.example.catchTheLetters.service.impl;
|
||||
|
||||
import com.example.catchTheLetters.entity.*;
|
||||
import com.example.catchTheLetters.enums.LetterActionType;
|
||||
import com.example.catchTheLetters.enums.MessageType;
|
||||
import com.example.catchTheLetters.enums.RoomActionType;
|
||||
import com.example.catchTheLetters.enums.RoomStatus;
|
||||
import com.example.catchTheLetters.enums.*;
|
||||
import com.example.catchTheLetters.model.vo.Letter;
|
||||
import com.example.catchTheLetters.service.AuthService;
|
||||
import com.example.catchTheLetters.service.RoomService;
|
||||
|
@ -143,25 +140,30 @@ public class RoomServiceImpl implements RoomService {
|
|||
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
|
||||
public void handleInput(long roomId, WebSocketSession session, PlayerInput input) {
|
||||
var message = new GameMessage<>(MessageType.INPUT, input);
|
||||
var room = rooms.get(roomId);
|
||||
var players = room.getPlayers();
|
||||
// 发送消息通知其他玩家有人输入了
|
||||
for (var player : players.keySet())
|
||||
if (player != session)
|
||||
sendMessage(player, message);
|
||||
|
||||
if (input.getKey() == InputKeyType.SPACE) {
|
||||
var player = players.get(session);
|
||||
// 如果是空格,按下则记录,松开则获取记录时间并判断是否有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
|
||||
|
@ -291,4 +293,14 @@ public class RoomServiceImpl implements RoomService {
|
|||
// 游戏结束后需要向玩家发送排行榜数据(用户ID、分数、排名)
|
||||
// 最大的问题:丢包后如何处理?比如其他玩家按下按键后,自己没有接收到松开消息,导致其他玩家在画面中一直持续运动,网络连接稳定后,不同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 初始化玩家数据
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue