package com.yangql;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/game/{gameId}/{username}")
public class GameServer {
private static Map<Long, serverGame> games = new Hashtable<>();
private static ObjectMapper mapper = new ObjectMapper();
private static class serverGame
{
public long gameId;
public Session player1;
public Session player2;
public Game Game;
}
@OnOpen
public void onOpen(Session session, @PathParam("gameId") long gameId,
@PathParam("username") String username)
{
try
{
Game hitBlockGame =Game.getActiveGame(gameId);
if(hitBlockGame != null)//正常情况下,游戏尚未激活
{
session.close(new CloseReason(
CloseReason.CloseCodes.UNEXPECTED_CONDITION,
"游戏已经开始"
));
}
List<String> actions = session.getRequestParameterMap().get("action");
if(actions != null && actions.size() == 1)
{
String action = actions.get(0);
if("start".equalsIgnoreCase(action))
{
serverGame game = new serverGame();
game.gameId = gameId;
game.player1 = session;
games.put(gameId, game);
}
else if("join".equalsIgnoreCase(action))
{
serverGame game = games.get(gameId);
game.player2 = session;
game.Game = Game.startGames(gameId, username);//激活游戏
this.sendJsonMessage(game.player1, game,
new GameReadyMessage(game.Game));//告诉player1游戏已准备
this.sendJsonMessage(game.player2, game,
new GameReadyMessage(game.Game));//告诉player1游戏已准备
}
}
}
catch(IOException e)
{
e.printStackTrace();
try
{
session.close(new CloseReason(
CloseReason.CloseCodes.UNEXPECTED_CONDITION, e.toString()
));
}
catch(IOException ignore) { }
}
}
@OnMessage
public void onMessage(Session session, String message,
@PathParam("gameId") long gameId)
{
serverGame game =games.get(gameId);
boolean isPlayer1 = session == game.player1;
try
{
MassageToServer msgToServer=mapper.readValue(message, MassageToServer.class);
//发球后游戏开始
if(msgToServer.type==1) {//start
this.sendJsonMessage((isPlayer1 ? game.player2 : game.player1), game,
new GameStartedMessage());
}
//将球和踏板的坐标发给对手
if(msgToServer.type==2) {//move
if(msgToServer.status==2) {
this.sendJsonMessage((isPlayer1 ? game.player2 : game.player1), game,
new GameMoveMessage(msgToServer));
}
if(msgToServer.status==0) {//消息发送者游戏失败
this.sendJsonMessage((isPlayer1 ? game.player2 : game.player1), game,
new GameWinMessage());//发送获胜信息给对手
this.sendJsonMessage((isPlayer1 ? game.player1 : game.player2), game,
new GameLoseMessage());//发送失败信息给自己
game.Game.setOver();
}
if(msgToServer.status==1) {//消息发送者游戏胜利
this.sendJsonMessage((isPlayer1 ? game.player2 : game.player1), game,
new GameLoseMessage());//发送失败信息给对手
this.sendJsonMessage((isPlayer1 ? game.player1 : game.player2), game,
new GameWinMessage());//发送获胜信息给自己
game.Game.setOver();
}
}
}
catch(IOException e)
{
this.handleException(e, game);
}
}
@OnClose
public void onClose(Session session, @PathParam("gameId") long gameId) {
serverGame game = games.get(gameId);
if(game == null)
return;
//服务器中存在游戏实例
boolean isPlayer1 = session == game.player1;
if(game.Game == null)//半初始化状态(只有player1连接到服务器,没有player2和Game实例)
{
Game.removeQueuedGame(game.gameId);
}
else if(!game.Game.isOver())//如果不是正常的游戏结束后退出,通知对方
{
game.Game.forfeit(isPlayer1 ? Game.Player.PLAYER1 :
Game.Player.PLAYER2);
Session opponent = (isPlayer1 ? game.player2 : game.player1);
this.sendJsonMessage(opponent, game, new GameForfeitedMessage());
try
{
opponent.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
//正常退出
else if(game.Game.isOver()){
try {
game.player1.close();
game.player2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* Send Message
*/
private void sendJsonMessage(Session session, serverGame game, Message message)
{
try
{
session.getBasicRemote()
.sendText(mapper.writeValueAsString(message));
}
catch(IOException e)
{
this.handleException(e, game);
}
}
/*
* Exception handler
*/
private void handleException(Throwable t, serverGame game)
{
t.printStackTrace();
String message = t.toString();
try
{
game.player1.close(new CloseReason(
CloseReason.CloseCodes.UNEXPECTED_CONDITION, message
));
}
catch(IOException ignore) { }
try
{
game.player2.close(new CloseReason(
CloseReason.CloseCodes.UNEXPECTED_CONDITION, message
));
}
catch(IOException ignore) { }
}
/*
* Message
*/
public static abstract class Message
{
private final String action;
public Message(String action)
{
this.action = action;
}
public String getAction()
{
return this.action;
}
}
public static class GameReadyMessage extends Message
{
private final Game game;
public GameReadyMessage(Game game)
{
super("gameReady");
this.game = game;
}
public Game getGame()
{
return game;
}
}
public static class GameStartedMessage extends Message
{
public GameStartedMessage()
{
super("gameStarted");
}
}
public static class GameMoveMessage extends Message
{
private final MassageToServer msg;
public GameMoveMessage(MassageToServer msg)
{
super("gameMove");
this.msg = msg;
}
public MassageToServer getMessageToServer()
{
return this.msg;
}
}
public static class GameLoseMessage extends Message
{
public GameLoseMessage() {
没有合适的资源?快使用搜索试试~ 我知道了~
基于java和web3联机打砖块游戏源码.zip
共32个文件
png:6个
prefs:4个
jsp:4个
需积分: 5 0 下载量 114 浏览量
2024-08-03
12:10:53
上传
评论
收藏 256KB ZIP 举报
温馨提示
基于java和web3联机打砖块游戏源码.zip
资源推荐
资源详情
资源评论
收起资源包目录
基于java和webapp联机打砖块游戏源码.zip (32个子文件)
.classpath 1018B
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.jdt.core.prefs 422B
org.eclipse.core.resources.prefs 172B
.jsdtscope 566B
org.eclipse.wst.validation.prefs 48B
org.eclipse.wst.common.component 530B
org.eclipse.wst.common.project.facet.core.xml 335B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.m2e.core.prefs 86B
pom.xml 3KB
src
com
yangql
GameServlet.java 3KB
GameServer.java 9KB
Game.java 2KB
screenshot
secondPlayerJoin.png 47KB
gameOver.png 64KB
list.png 35KB
createNewGame.png 32KB
openSecondWin.png 59KB
waitForOpponent.png 26KB
webapp
WEB-INF
web.xml 957B
jsp
base.jspf 198B
view
list.jsp 3KB
NewFile.jsp 340B
game.jsp 3KB
index.jsp 115B
resource
picture
ball.jpg 1KB
paddle.jpg 4KB
bricks1.jpg 4KB
game.js 13KB
META-INF
MANIFEST.MF 36B
.project 1KB
共 32 条
- 1
资源评论
不安分的小女孩博客
- 粉丝: 1895
- 资源: 257
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功