Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<packaging>jar</packaging>
<name>ai-tank</name>
<description>For Ai Tank Server</description>

<properties>
<netty.version>4.1.27.Final</netty.version>
<gson.version>2.7</gson.version>
Expand Down Expand Up @@ -173,6 +173,36 @@
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.aitank.ApplicationEntrance</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
Expand Down
185 changes: 171 additions & 14 deletions src/main/java/com/aitank/server/component/ObjectMultiAction.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package com.aitank.server.component;

import com.aitank.server.context.SpringContext;
import com.aitank.server.enums.EventType2;
import com.aitank.server.handler.TcpHandler;
import com.aitank.server.protocol.PlayInfoData;
import com.aitank.server.protocol.PlayInfoDataList;
import com.aitank.server.protocol.SocketModel;
import com.aitank.server.protocol.UserLoginData;
import com.aitank.server.protocol.*;
import com.aitank.utils.ChannelHandlerContextInfo;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Component
public class ObjectMultiAction {

public void MsgLogin(ChannelHandlerContext ctx, SocketModel message){
/**
* 用户登录
*
* @param ctx
* @param message
*/
public void MsgLogin(ChannelHandlerContext ctx, SocketModel message) {
UserLoginData UserLoginData = message.deserialize(UserLoginData.class);

//获取数值
Expand Down Expand Up @@ -42,27 +49,40 @@ public void MsgLogin(ChannelHandlerContext ctx, SocketModel message){
response.serialize(UserLoginData);
ctx.writeAndFlush(response);
}
public void MsgPlayInfoData(ChannelHandlerContext ctx, SocketModel message){

/**
* 用户信息
*
* @param ctx
* @param message
*/
public void MsgPlayInfoData(ChannelHandlerContext ctx, SocketModel message) {
PlayInfoData playInfoData = message.deserialize(PlayInfoData.class);

ChannelHandlerContextInfo currentPlayer = TcpHandler.players.get(ctx.channel().id().toString());
currentPlayer.setPosition(new float[]{playInfoData.position1,playInfoData.position2,playInfoData.position3});
currentPlayer.setRotation(new float[]{playInfoData.rotation1,playInfoData.rotation2,playInfoData.rotation3,playInfoData.rotation4});
currentPlayer.setPosition(new float[]{playInfoData.position1, playInfoData.position2, playInfoData.position3});
currentPlayer.setRotation(new float[]{playInfoData.rotation1, playInfoData.rotation2, playInfoData.rotation3, playInfoData.rotation4});
currentPlayer.setShoot(playInfoData.shoot);
currentPlayer.serverHealth = playInfoData.serverHealth;
SocketModel response = new SocketModel();
response.setProtocolName(EventType2.MsgPlayInfoData.getName());//("MsgPlayInfoData");

response.serialize(playInfoData);
for (String cid : TcpHandler.players.keySet())
{
if(cid.equals(ctx.channel().id().toString()))
for (String cid : TcpHandler.players.keySet()) {
if (cid.equals(ctx.channel().id().toString()))
continue;
ChannelHandlerContextInfo player = TcpHandler.players.get(cid);
player.getChx().writeAndFlush(response);
}
}
public void MsgInitPlay(ChannelHandlerContext ctx, SocketModel message){

/**
* 角色信息
*
* @param ctx
* @param message
*/
public void MsgInitPlay(ChannelHandlerContext ctx, SocketModel message) {
PlayInfoDataList playInfoDataList = new PlayInfoDataList();

for (String cid : TcpHandler.players.keySet()) {
Expand All @@ -80,7 +100,7 @@ public void MsgInitPlay(ChannelHandlerContext ctx, SocketModel message){
playInfoData.serverHealth = player.serverHealth;
playInfoData.userId = player.getChx().channel().id().toString();
playInfoData.playName = player.getName();
playInfoData.playNumber = playInfoDataList.playInfoDataList.size();
playInfoData.playerNumber = playInfoDataList.playInfoDataList.size();

playInfoDataList.playInfoDataList.add(playInfoData);
}
Expand All @@ -92,4 +112,141 @@ public void MsgInitPlay(ChannelHandlerContext ctx, SocketModel message){

ctx.writeAndFlush(response);
}

/**
* 创建信息
*/
public void MsgCreateRoom(ChannelHandlerContext ctx, SocketModel msg) {
RoomData room = msg.deserialize(RoomData.class);
String userId = ctx.channel().id().toString();

room.setId(UUID.randomUUID().toString());
room.setMasterId(userId);
room.join(userId);

// 设置用户所在房间 id
TcpHandler.players.get(userId).setRoomId(room.getId());

System.out.println("Create Room: " + userId + "->" + room.getName());
/*
* 维护房间 Map
*/
TcpHandler.rooms.put(room.getId(), room);
System.out.println("Room Number: " + TcpHandler.rooms.size());

/*
* 通知创建用户结果
*/
SocketModel response = new SocketModel();
response.setProtocolName(EventType2.MsgCreateRoom);
response.serialize(room);
ctx.writeAndFlush(response);

RoomDataList roomList = new RoomDataList(new ArrayList<>(TcpHandler.rooms.values()));
SocketModel notifyRoomListRefresh = new SocketModel();
notifyRoomListRefresh.setProtocolName(EventType2.MsgRoomList);
notifyRoomListRefresh.serialize(roomList);
notificationPlayers(userId, notifyRoomListRefresh);
}

/**
* 加入房间
*/
public void MsgJoinRoom(ChannelHandlerContext ctx, SocketModel msg) {
String userId = ctx.channel().id().toString();
Map<String, RoomData> rooms = TcpHandler.rooms;

System.out.println("Join Room: " + userId);

RoomData room = msg.deserialize(RoomData.class);
room = rooms.get(room.getId());
room.join(userId);
TcpHandler.players.get(userId).setRoomId(room.getId());

SocketModel response = new SocketModel();
response.setProtocolName(EventType2.MsgJoinRoom);
response.serialize(room);
notificationPlayers(ctx.channel().id().toString(), room.getPlayers(), response);
}

/**
* 离开房间
*/
public void MsgExitRoom(ChannelHandlerContext ctx, SocketModel msg) {
String userId = ctx.channel().id().toString();
Map<String, RoomData> rooms = TcpHandler.rooms;
ChannelHandlerContextInfo userInfo = TcpHandler.players.get(userId);

RoomData room = rooms.get(userInfo.getRoomId());
room.exit(userId);
userInfo.setRoomId(null);

System.out.println("Exit Room: " + userId);
if (room.getPlayers().size() == 0) {
rooms.remove(room.getId());
RoomDataList roomList = new RoomDataList(new ArrayList<>(rooms.values()));
SocketModel notifyRoomListRefresh = new SocketModel();
notifyRoomListRefresh.setProtocolName(EventType2.MsgRoomList);
notifyRoomListRefresh.serialize(roomList);
notificationPlayers(userId, notifyRoomListRefresh);
System.out.println("===" + room.getName() + " Room Close ===");
}

SocketModel response = new SocketModel();
response.setProtocolName(EventType2.MsgExitRoom);
response.serialize(room);
notificationPlayers(ctx.channel().id().toString(), room.getPlayers(), response);
}

/**
* 发送房间列表
*/
public void MsgRoomList(ChannelHandlerContext ctx, SocketModel msg) {
Map<String, RoomData> roomMap = TcpHandler.rooms;
List<RoomData> rooms = new ArrayList<>(roomMap.values());
RoomDataList roomList = new RoomDataList(rooms);

SocketModel response = new SocketModel();
response.setProtocolName(EventType2.MsgRoomList);
response.serialize(roomList);
ctx.writeAndFlush(response);
}

/**
* 通知所有玩家(排除当前会话玩家)
* @param currentPlayer
* @param msg
*/
private void notificationPlayers(String currentPlayer, SocketModel msg) {
for (String cid : TcpHandler.players.keySet()) {
if (cid.equals(currentPlayer)) continue;
ChannelHandlerContextInfo player = TcpHandler.players.get(cid);
player.getChx().writeAndFlush(msg);
}
}

/**
* 通知所有玩家
* @param msg
*/
private void notificationPlayers(SocketModel msg) {
for (String cid : TcpHandler.players.keySet()) {
ChannelHandlerContextInfo player = TcpHandler.players.get(cid);
player.getChx().writeAndFlush(msg);
}
}

/**
* 通知所有房间玩家
* @param currentPlayer
* @param userIds
* @param msg
*/
private void notificationPlayers(String currentPlayer, List<String> userIds, SocketModel msg) {
for (String cid : userIds) {
if (cid.equals(currentPlayer)) continue;
ChannelHandlerContextInfo player = TcpHandler.players.get(cid);
player.getChx().writeAndFlush(msg);
}
}
}
6 changes: 5 additions & 1 deletion src/main/java/com/aitank/server/enums/EventType2.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ public enum EventType2 {
MsgLogin("MsgLogin", 1),
MsgInitPlay("MsgInitPlay", 2),
MsgPlayInfoData("MsgPlayInfoData", 3),
MsgOnLogout("MsgOnLogout", 4);
MsgOnLogout("MsgOnLogout", 4),
MsgCreateRoom("MsgCreateRoom", 5),
MsgJoinRoom("MsgJoinRoom", 6),
MsgRoomList("MsgRoomList", 7),
MsgExitRoom("MsgExitRoom", 8);


private String name ;
Expand Down
30 changes: 24 additions & 6 deletions src/main/java/com/aitank/server/handler/TcpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.aitank.server.component.ObjectMultiAction;
import com.aitank.server.context.SpringContext;
import com.aitank.server.dao.UserDao;
import com.aitank.server.enums.EventType2;
import com.aitank.server.protocol.PlayInfoData;
import com.aitank.server.protocol.RoomData;
import com.aitank.server.protocol.SocketModel;
import com.aitank.utils.ChannelHandlerContextInfo;
import io.netty.channel.Channel;
Expand All @@ -14,8 +14,6 @@
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.apache.ibatis.executor.statement.BaseStatementHandler;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Method;
import java.util.HashMap;
Expand All @@ -26,6 +24,7 @@ public class TcpHandler extends ChannelInboundHandlerAdapter {


public static Map<String, ChannelHandlerContextInfo> players = new HashMap<String, ChannelHandlerContextInfo>();
public static Map<String, RoomData> rooms = new HashMap<>();
public static final ChannelGroup group = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
public Map<EventType2, BaseHandler> handlerMap = new HashMap<>();

Expand Down Expand Up @@ -76,22 +75,41 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception {

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("room:" + ctx.channel().id() + " leave room");
System.out.println("clinet:" + ctx.channel().id() + " leave server");
super.channelInactive(ctx);

if (TcpHandler.players.containsKey(ctx.channel().id().toString())) {
TcpHandler.players.remove(ctx.channel().id().toString());
String userId = ctx.channel().id().toString();

if (TcpHandler.players.containsKey(userId)) {
String roomId = TcpHandler.players.get(userId).getRoomId();
if (TcpHandler.rooms.containsKey(roomId)) {
RoomData room = TcpHandler.rooms.get(roomId);
room.exit(userId);
if (room.getPlayers().size() == 0)
TcpHandler.rooms.remove(roomId);
}
TcpHandler.players.remove(userId);
System.out.println("PlayerNumbers : " + TcpHandler.players.size());
System.out.println("RoomNumbers : " + TcpHandler.rooms.size());
SocketModel response = new SocketModel();
response.setProtocolName(EventType2.MsgOnLogout.getName());//("MsgOnLogout");

PlayInfoData playInfoData = new PlayInfoData();
playInfoData.userId = ctx.channel().id().toString();
playInfoData.userId = userId;

response.serialize(playInfoData);

/*
* 退出房间
*/
SocketModel room = new SocketModel();
room.setProtocolName(EventType2.MsgExitRoom);
room.serialize(userId);

for (String cid : TcpHandler.players.keySet()) {
ChannelHandlerContextInfo player = TcpHandler.players.get(cid);
player.getChx().writeAndFlush(room);
player.getChx().writeAndFlush(response);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/aitank/server/protocol/PlayInfoData.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public class PlayInfoData {
public float force = 15f;
public int serverHealth=1 ;
public String playName="";
public int playNumber;
public int playerNumber;
}
Loading