parent
d3fb35e8c6
commit
2b10bac007
@ -0,0 +1,96 @@
|
||||
package com.example.tankbattle.net;
|
||||
|
||||
import com.example.tankbattle.Bullet;
|
||||
import com.example.tankbattle.Dir;
|
||||
import com.example.tankbattle.Group;
|
||||
import com.example.tankbattle.TankFrame;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class BulletNewMessage extends Message {
|
||||
UUID playerID;
|
||||
UUID id;
|
||||
int x, y;
|
||||
Dir dir;
|
||||
Group group;
|
||||
|
||||
public BulletNewMessage() {
|
||||
}
|
||||
|
||||
public BulletNewMessage(Bullet bullet) {
|
||||
this.playerID = bullet.getPlayerId();
|
||||
this.id = bullet.getId();
|
||||
this.x = bullet.getX();
|
||||
this.y = bullet.getY();
|
||||
this.dir = bullet.getDir();
|
||||
this.group = bullet.getGroup();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle() {
|
||||
if (this.id.equals(TankFrame.INSTANCE.getMainTank().getId())) {
|
||||
return;
|
||||
}
|
||||
Bullet bullet = new Bullet(this.playerID, x, y, dir, group, TankFrame.INSTANCE);
|
||||
bullet.setId(this.id);
|
||||
TankFrame.INSTANCE.addBullet(bullet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBytes() {
|
||||
byte[] bytes = null;
|
||||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(baos)) {
|
||||
dos.writeLong(this.playerID.getMostSignificantBits());
|
||||
dos.writeLong(this.playerID.getLeastSignificantBits());
|
||||
dos.writeLong(id.getMostSignificantBits());
|
||||
dos.writeLong(id.getLeastSignificantBits());
|
||||
dos.writeInt(x);
|
||||
dos.writeInt(y);
|
||||
dos.writeInt(dir.ordinal());
|
||||
dos.writeInt(group.ordinal());
|
||||
dos.flush();
|
||||
bytes = baos.toByteArray();
|
||||
} catch (Exception exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(byte[] bytes) {
|
||||
try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes))) {
|
||||
this.playerID = new UUID(dis.readLong(), dis.readLong());
|
||||
this.id = new UUID(dis.readLong(), dis.readLong());
|
||||
|
||||
this.x = dis.readInt();
|
||||
this.y = dis.readInt();
|
||||
this.dir = Dir.values()[dis.readInt()];
|
||||
this.group = Group.values()[dis.readInt()];
|
||||
} catch (IOException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageType getMessageType() {
|
||||
return MessageType.BulletNew;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BulletNewMessage{" +
|
||||
"playerID=" + playerID +
|
||||
", id=" + id +
|
||||
", x=" + x +
|
||||
", y=" + y +
|
||||
", dir=" + dir +
|
||||
", group=" + group +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -1,28 +1,18 @@
|
||||
package com.example.tankbattle.net;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import com.example.tankbattle.TankFrame;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
import io.netty.util.ReferenceCountUtil;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
|
||||
public class ClientHandler extends ChannelInboundHandlerAdapter {
|
||||
public class ClientHandler extends SimpleChannelInboundHandler<Message> {
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
ByteBuf buf = null;
|
||||
try {
|
||||
buf = (ByteBuf) msg;
|
||||
byte[] bytes = new byte[buf.readableBytes()];
|
||||
// buf.getByte(buf.readerIndex(), bytes);
|
||||
String message = new String(bytes);
|
||||
} finally {
|
||||
if (buf!= null) {
|
||||
ReferenceCountUtil.release(buf);
|
||||
}
|
||||
}
|
||||
public void channelRead0(ChannelHandlerContext ctx, Message msg) throws Exception {
|
||||
System.out.println(msg);
|
||||
msg.handle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
ctx.writeAndFlush(new TankMessage(5, 8));
|
||||
ctx.writeAndFlush(new TankJoinMessage(TankFrame.INSTANCE.getMainTank()));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,8 @@
|
||||
package com.example.tankbattle.net;
|
||||
|
||||
public abstract class Message {
|
||||
public abstract void handle();
|
||||
public abstract byte[] toBytes();
|
||||
public abstract void parse(byte[] bytes);
|
||||
public abstract MessageType getMessageType();
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.example.tankbattle.net;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MessageDecoder extends ByteToMessageDecoder {
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
||||
if (in.readableBytes() < 8) {
|
||||
return;
|
||||
}
|
||||
in.markReaderIndex();
|
||||
MessageType messageType = MessageType.values()[in.readInt()];
|
||||
int length = in.readInt();
|
||||
if (in.readableBytes() < length) {
|
||||
in.resetReaderIndex();
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] bytes = new byte[length];
|
||||
in.readBytes(bytes);
|
||||
|
||||
Message message = (Message) Class.forName("com.example.tankbattle.net." + messageType.toString() + "Message").getDeclaredConstructor().newInstance();
|
||||
message.parse(bytes);
|
||||
out.add(message);
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.example.tankbattle.net;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
|
||||
public class MessageEncoder extends MessageToByteEncoder<Message> {
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception {
|
||||
out.writeInt(msg.getMessageType().ordinal());
|
||||
byte[] data = msg.toBytes();
|
||||
out.writeInt(data.length);
|
||||
out.writeBytes(data);}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.example.tankbattle.net;
|
||||
|
||||
public enum MessageType {
|
||||
TankJoin, TankDirChanged, TankStop, TankStartMoving, BulletNew, TankDie,
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
package com.example.tankbattle.net;
|
||||
|
||||
import com.example.tankbattle.Bullet;
|
||||
import com.example.tankbattle.Tank;
|
||||
import com.example.tankbattle.TankFrame;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class TankDieMessage extends Message{
|
||||
|
||||
UUID bulletId;
|
||||
UUID id;
|
||||
|
||||
public TankDieMessage() {
|
||||
}
|
||||
|
||||
public TankDieMessage(UUID bulletId, UUID id) {
|
||||
this.bulletId = bulletId;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle() {
|
||||
System.out.println("we got a tank die: " + id);
|
||||
System.out.println("and my tank is: " + TankFrame.INSTANCE.getMainTank().getId());
|
||||
Tank tank = TankFrame.INSTANCE.findTankByUUID(id);
|
||||
System.out.println("i found a tank with this id: " + tank);
|
||||
|
||||
Bullet bullet = TankFrame.INSTANCE.findBulletByUUID(bulletId);
|
||||
if (bullet != null) {
|
||||
bullet.die();
|
||||
}
|
||||
if (this.id.equals(TankFrame.INSTANCE.getMainTank().getId())) {
|
||||
TankFrame.INSTANCE.getMainTank().die();
|
||||
} else {
|
||||
Tank tanka = TankFrame.INSTANCE.findTankByUUID(id);
|
||||
if (tanka != null) {
|
||||
tanka.die();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBytes() {
|
||||
byte[] bytes = null;
|
||||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(baos)) {
|
||||
dos.writeLong(bulletId.getMostSignificantBits());
|
||||
dos.writeLong(bulletId.getLeastSignificantBits());
|
||||
dos.writeLong(id.getMostSignificantBits());
|
||||
dos.writeLong(id.getLeastSignificantBits());
|
||||
dos.flush();
|
||||
bytes = baos.toByteArray();
|
||||
|
||||
} catch (Exception exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(byte[] bytes) {
|
||||
try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes))) {
|
||||
this.bulletId = new UUID(dis.readLong(), dis.readLong());
|
||||
this.id = new UUID(dis.readLong(), dis.readLong());
|
||||
} catch (IOException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageType getMessageType() {
|
||||
return MessageType.TankDie;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TankDieMessage{" +
|
||||
"bulletId=" + bulletId +
|
||||
", id=" + id +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,131 @@
|
||||
package com.example.tankbattle.net;
|
||||
|
||||
import com.example.tankbattle.Dir;
|
||||
import com.example.tankbattle.Tank;
|
||||
import com.example.tankbattle.TankFrame;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class TankDirChangedMessage extends Message{
|
||||
|
||||
UUID id;
|
||||
|
||||
Dir dir;
|
||||
|
||||
int x, y;
|
||||
|
||||
public TankDirChangedMessage() {
|
||||
}
|
||||
|
||||
public TankDirChangedMessage(UUID id, int x, int y, Dir dir) {
|
||||
this.id = id;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.dir = dir;
|
||||
}
|
||||
|
||||
public TankDirChangedMessage(Tank tank) {
|
||||
this.id = tank.getId();
|
||||
this.dir = tank.getDir();
|
||||
this.x = tank.getX();
|
||||
this.y = tank.getY();
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public Dir getDir() {
|
||||
return dir;
|
||||
}
|
||||
|
||||
public void setDir(Dir dir) {
|
||||
this.dir = dir;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle() {
|
||||
if (this.id.equals(TankFrame.INSTANCE.getMainTank().getId())) {
|
||||
return;
|
||||
}
|
||||
Tank tank = TankFrame.INSTANCE.findTankByUUID(this.id);
|
||||
if (tank != null) {
|
||||
tank.setMoving(false);
|
||||
tank.setX(this.x);
|
||||
tank.setY(this.y);
|
||||
tank.setDir(this.dir);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBytes() {
|
||||
byte[] bytes = null;
|
||||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(baos)) {
|
||||
dos.writeLong(id.getMostSignificantBits());
|
||||
dos.writeLong(id.getLeastSignificantBits());
|
||||
dos.writeInt(x);
|
||||
dos.writeInt(y);
|
||||
dos.writeInt(dir.ordinal());
|
||||
dos.flush();
|
||||
bytes = baos.toByteArray();
|
||||
|
||||
} catch (Exception exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(byte[] bytes) {
|
||||
try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes))) {
|
||||
this.id = new UUID(dis.readLong(), dis.readLong());
|
||||
|
||||
this.x = dis.readInt();
|
||||
this.y = dis.readInt();
|
||||
this.dir = Dir.values()[dis.read()];
|
||||
} catch (IOException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageType getMessageType() {
|
||||
return MessageType.TankDirChanged;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TankDirChangedMessage{" +
|
||||
"id=" + id +
|
||||
", dir=" + dir +
|
||||
", x=" + x +
|
||||
", y=" + y +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,117 @@
|
||||
package com.example.tankbattle.net;
|
||||
|
||||
import com.example.tankbattle.Dir;
|
||||
import com.example.tankbattle.Group;
|
||||
import com.example.tankbattle.Tank;
|
||||
import com.example.tankbattle.TankFrame;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class TankJoinMessage extends Message{
|
||||
public int x;
|
||||
public int y;
|
||||
public Dir dir;
|
||||
public boolean moving;
|
||||
public Group group;
|
||||
public UUID id;
|
||||
|
||||
public TankJoinMessage() {
|
||||
}
|
||||
|
||||
public TankJoinMessage(int x, int y, Dir dir, boolean moving, Group group, UUID id) {
|
||||
super();
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.dir = dir;
|
||||
this.moving = moving;
|
||||
this.group = group;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
||||
public TankJoinMessage(Tank tank) {
|
||||
super();
|
||||
this.x = tank.getX();
|
||||
this.y = tank.getY();
|
||||
this.dir = tank.getDir();
|
||||
this.group = tank.getGroup();
|
||||
this.moving = tank.isMoving();
|
||||
this.id = tank.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(byte[] bytes) {
|
||||
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes));
|
||||
try {
|
||||
this.x = dis.readInt();
|
||||
this.y = dis.readInt();
|
||||
this.dir = Dir.values()[dis.readInt()];
|
||||
this.moving = dis.readBoolean();
|
||||
this.group = Group.values()[dis.readInt()];
|
||||
this.id = new UUID(dis.readLong(), dis.readLong());
|
||||
} catch (IOException exception) {
|
||||
exception.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
dis.close();
|
||||
}catch (IOException ex){
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBytes(){
|
||||
byte[] bytes = null;
|
||||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(baos)) {
|
||||
dos.writeInt(x);
|
||||
dos.writeInt(y);
|
||||
dos.writeInt(dir.ordinal());
|
||||
dos.writeBoolean(moving);
|
||||
dos.writeInt(group.ordinal());
|
||||
dos.writeLong(id.getMostSignificantBits());
|
||||
dos.writeLong(id.getLeastSignificantBits());
|
||||
dos.flush();
|
||||
bytes = baos.toByteArray();
|
||||
|
||||
} catch (Exception exception) {
|
||||
exception.printStackTrace();
|
||||
} finally {
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(){
|
||||
if (this.id.equals(TankFrame.INSTANCE.getMainTank().getId()) ||
|
||||
TankFrame.INSTANCE.findTankByUUID(this.id) != null) {
|
||||
return;
|
||||
}
|
||||
Tank tank = new Tank(this);
|
||||
TankFrame.INSTANCE.addTank(tank);
|
||||
Client.INSTANCE.send(new TankJoinMessage(TankFrame.INSTANCE.getMainTank()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageType getMessageType() {
|
||||
return MessageType.TankJoin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TankJoinMessage{" +
|
||||
"x=" + x +
|
||||
", y=" + y +
|
||||
", dir=" + dir +
|
||||
", moving=" + moving +
|
||||
", group=" + group +
|
||||
", id=" + id +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package com.example.tankbattle.net;
|
||||
|
||||
public class TankMessage {
|
||||
public int x;
|
||||
public int y;
|
||||
|
||||
public TankMessage(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package com.example.tankbattle.net;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TankMessageDecoder extends ByteToMessageDecoder {
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
||||
if (in.readableBytes() < 8) {
|
||||
return;
|
||||
}
|
||||
int x = in.readInt();
|
||||
int y = in.readInt();
|
||||
out.add(new TankMessage(x, y));
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
package com.example.tankbattle.net;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
|
||||
public class TankMessageEncoder extends MessageToByteEncoder<TankMessage> {
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, TankMessage msg, ByteBuf out) throws Exception {
|
||||
out.writeInt(msg.x);
|
||||
out.writeInt(msg.y);
|
||||
}
|
||||
}
|
@ -0,0 +1,131 @@
|
||||
package com.example.tankbattle.net;
|
||||
|
||||
import com.example.tankbattle.Dir;
|
||||
import com.example.tankbattle.Tank;
|
||||
import com.example.tankbattle.TankFrame;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class TankStartMovingMessage extends Message{
|
||||
|
||||
UUID id;
|
||||
|
||||
int x,y;
|
||||
|
||||
Dir dir;
|
||||
|
||||
public TankStartMovingMessage() {
|
||||
}
|
||||
|
||||
public TankStartMovingMessage(UUID id, int x, int y, Dir dir) {
|
||||
this.id = id;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.dir = dir;
|
||||
}
|
||||
|
||||
public TankStartMovingMessage(Tank tank) {
|
||||
this.id = tank.getId();
|
||||
this.x = tank.getX();
|
||||
this.y = tank.getY();
|
||||
this.dir = tank.getDir();
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public Dir getDir() {
|
||||
return dir;
|
||||
}
|
||||
|
||||
public void setDir(Dir dir) {
|
||||
this.dir = dir;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle() {
|
||||
if (this.id.equals(TankFrame.INSTANCE.getMainTank().getId())) {
|
||||
return;
|
||||
}
|
||||
Tank tank = TankFrame.INSTANCE.findTankByUUID(this.id);
|
||||
if (tank != null) {
|
||||
tank.setMoving(true);
|
||||
tank.setX(this.x);
|
||||
tank.setY(this.y);
|
||||
tank.setDir(this.dir);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBytes() {
|
||||
byte[] bytes = null;
|
||||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(baos)) {
|
||||
dos.writeLong(id.getMostSignificantBits());
|
||||
dos.writeLong(id.getLeastSignificantBits());
|
||||
dos.writeInt(x);
|
||||
dos.writeInt(y);
|
||||
dos.writeInt(dir.ordinal());
|
||||
dos.flush();
|
||||
bytes = baos.toByteArray();
|
||||
|
||||
} catch (Exception exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(byte[] bytes) {
|
||||
try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes))) {
|
||||
this.id = new UUID(dis.readLong(), dis.readLong());
|
||||
|
||||
this.x = dis.readInt();
|
||||
this.y = dis.readInt();
|
||||
this.dir = Dir.values()[dis.readInt()];
|
||||
} catch (IOException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageType getMessageType() {
|
||||
return MessageType.TankStartMoving;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TankStartMovingMessage{" +
|
||||
"id=" + id +
|
||||
", x=" + x +
|
||||
", y=" + y +
|
||||
", dir=" + dir +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,114 @@
|
||||
package com.example.tankbattle.net;
|
||||
|
||||
import com.example.tankbattle.Tank;
|
||||
import com.example.tankbattle.TankFrame;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class TankStopMessage extends Message{
|
||||
|
||||
UUID id;
|
||||
|
||||
int x,y;
|
||||
|
||||
public TankStopMessage() {
|
||||
}
|
||||
|
||||
public TankStopMessage(UUID id, int x, int y) {
|
||||
this.id = id;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public TankStopMessage(Tank tank) {
|
||||
this.id = tank.getId();
|
||||
this.x = tank.getX();
|
||||
this.y = tank.getY();
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle() {
|
||||
if (this.id.equals(TankFrame.INSTANCE.getMainTank().getId())) {
|
||||
return;
|
||||
}
|
||||
Tank tank = TankFrame.INSTANCE.findTankByUUID(this.id);
|
||||
if (tank != null) {
|
||||
tank.setMoving(false);
|
||||
tank.setX(this.x);
|
||||
tank.setY(this.y);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBytes() {
|
||||
byte[] bytes = null;
|
||||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(baos)) {
|
||||
dos.writeLong(id.getMostSignificantBits());
|
||||
dos.writeLong(id.getLeastSignificantBits());
|
||||
dos.writeInt(x);
|
||||
dos.writeInt(y);
|
||||
dos.flush();
|
||||
bytes = baos.toByteArray();
|
||||
|
||||
} catch (Exception exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(byte[] bytes) {
|
||||
try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes))) {
|
||||
this.id = new UUID(dis.readLong(), dis.readLong());
|
||||
|
||||
this.x = dis.readInt();
|
||||
this.y = dis.readInt();
|
||||
} catch (IOException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageType getMessageType() {
|
||||
return MessageType.TankStop;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TankStopMessage{" +
|
||||
"id=" + id +
|
||||
", x=" + x +
|
||||
", y=" + y +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package com.example.tankbattle.net;
|
||||
|
||||
import com.example.tankbattle.Dir;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.embedded.EmbeddedChannel;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class TankDirChangedMessageCoderTest {
|
||||
|
||||
@Test
|
||||
void encode() {
|
||||
EmbeddedChannel embeddedChannel = new EmbeddedChannel();
|
||||
UUID id = UUID.randomUUID();
|
||||
|
||||
TankDirChangedMessage message = new TankDirChangedMessage(id, 5, 10, Dir.DOWN);
|
||||
embeddedChannel.pipeline().addLast(new MessageEncoder());
|
||||
embeddedChannel.writeOutbound(message);
|
||||
|
||||
ByteBuf buf = embeddedChannel.readOutbound();
|
||||
MessageType messageType = MessageType.values()[buf.readInt()];
|
||||
assertEquals(MessageType.TankJoin, messageType);
|
||||
|
||||
int length = buf.readInt();
|
||||
assertEquals(33, length);
|
||||
|
||||
int x = buf.readInt();
|
||||
int y = buf.readInt();
|
||||
Dir dir = Dir.values()[buf.readInt()];
|
||||
UUID uuid = new UUID(buf.readLong(), buf.readLong());
|
||||
|
||||
assertEquals(5, x);
|
||||
assertEquals(10, y);
|
||||
assertEquals(Dir.DOWN, dir);
|
||||
assertEquals(id, uuid);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void decode() {
|
||||
EmbeddedChannel embeddedChannel = new EmbeddedChannel();
|
||||
|
||||
UUID id = UUID.randomUUID();
|
||||
TankDirChangedMessage message = new TankDirChangedMessage(id, 5, 10, Dir.DOWN);
|
||||
embeddedChannel.pipeline().addLast(new MessageDecoder());
|
||||
|
||||
ByteBuf buf = Unpooled.buffer();
|
||||
buf.writeInt(MessageType.TankJoin.ordinal());
|
||||
byte[] data = message.toBytes();
|
||||
buf.writeInt(data.length);
|
||||
buf.writeBytes(data);
|
||||
|
||||
|
||||
embeddedChannel.writeInbound(buf.duplicate());
|
||||
|
||||
TankDirChangedMessage msg = embeddedChannel.readInbound();
|
||||
|
||||
assertEquals(5, msg.x);
|
||||
assertEquals(10, msg.y);
|
||||
assertEquals(Dir.DOWN, msg.dir);
|
||||
assertEquals(id, msg.id);
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package com.example.tankbattle.net;
|
||||
|
||||
import com.example.tankbattle.Dir;
|
||||
import com.example.tankbattle.Group;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.embedded.EmbeddedChannel;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class TankJoinMessageCoderTest {
|
||||
|
||||
@Test
|
||||
void encode() {
|
||||
EmbeddedChannel embeddedChannel = new EmbeddedChannel();
|
||||
UUID id = UUID.randomUUID();
|
||||
|
||||
TankJoinMessage message = new TankJoinMessage(5, 10, Dir.DOWN, true, Group.BAD, id);
|
||||
embeddedChannel.pipeline().addLast(new MessageEncoder());
|
||||
embeddedChannel.writeOutbound(message);
|
||||
|
||||
ByteBuf buf = embeddedChannel.readOutbound();
|
||||
MessageType messageType = MessageType.values()[buf.readInt()];
|
||||
assertEquals(MessageType.TankJoin, messageType);
|
||||
|
||||
int length = buf.readInt();
|
||||
assertEquals(33, length);
|
||||
|
||||
int x = buf.readInt();
|
||||
int y = buf.readInt();
|
||||
Dir dir = Dir.values()[buf.readInt()];
|
||||
boolean moving = buf.readBoolean();
|
||||
Group group = Group.values()[buf.readInt()];
|
||||
UUID uuid = new UUID(buf.readLong(), buf.readLong());
|
||||
|
||||
assertEquals(5, x);
|
||||
assertEquals(10, y);
|
||||
assertEquals(Dir.DOWN, dir);
|
||||
assertEquals(true, moving);
|
||||
assertEquals(Group.BAD, group);
|
||||
assertEquals(id, uuid);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void decode() {
|
||||
EmbeddedChannel embeddedChannel = new EmbeddedChannel();
|
||||
|
||||
UUID id = UUID.randomUUID();
|
||||
TankJoinMessage message = new TankJoinMessage(5, 10, Dir.DOWN, true, Group.BAD, id);
|
||||
embeddedChannel.pipeline().addLast(new MessageDecoder());
|
||||
|
||||
ByteBuf buf = Unpooled.buffer();
|
||||
buf.writeInt(MessageType.TankJoin.ordinal());
|
||||
byte[] data = message.toBytes();
|
||||
buf.writeInt(data.length);
|
||||
buf.writeBytes(data);
|
||||
|
||||
|
||||
embeddedChannel.writeInbound(buf.duplicate());
|
||||
|
||||
TankJoinMessage msg = embeddedChannel.readInbound();
|
||||
|
||||
assertEquals(5, msg.x);
|
||||
assertEquals(10, msg.y);
|
||||
assertEquals(Dir.DOWN, msg.dir);
|
||||
assertEquals(true, msg.moving);
|
||||
assertEquals(Group.BAD, msg.group);
|
||||
assertEquals(id, msg.id);
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package com.example.tankbattle.net;
|
||||
|
||||
import com.example.tankbattle.Dir;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.embedded.EmbeddedChannel;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class TankStartMovingMessageTest {
|
||||
|
||||
@Test
|
||||
void encode() {
|
||||
EmbeddedChannel embeddedChannel = new EmbeddedChannel();
|
||||
UUID id = UUID.randomUUID();
|
||||
|
||||
TankStartMovingMessage message = new TankStartMovingMessage(id, 5, 10, Dir.LEFT);
|
||||
embeddedChannel.pipeline().addLast(new MessageEncoder());
|
||||
embeddedChannel.writeOutbound(message);
|
||||
|
||||
ByteBuf buf = embeddedChannel.readOutbound();
|
||||
MessageType messageType = MessageType.values()[buf.readInt()];
|
||||
assertEquals(MessageType.TankStartMoving, messageType);
|
||||
|
||||
int length = buf.readInt();
|
||||
assertEquals(28, length);
|
||||
|
||||
UUID uuid = new UUID(buf.readLong(), buf.readLong());
|
||||
int x = buf.readInt();
|
||||
int y = buf.readInt();
|
||||
Dir dir = Dir.values()[buf.readInt()];
|
||||
|
||||
|
||||
assertEquals(5, x);
|
||||
assertEquals(10, y);
|
||||
assertEquals(Dir.LEFT, dir);
|
||||
assertEquals(id, uuid);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void decode() {
|
||||
EmbeddedChannel embeddedChannel = new EmbeddedChannel();
|
||||
|
||||
UUID id = UUID.randomUUID();
|
||||
TankStartMovingMessage message = new TankStartMovingMessage(id, 5, 10, Dir.LEFT);
|
||||
embeddedChannel.pipeline().addLast(new MessageDecoder());
|
||||
|
||||
ByteBuf buf = Unpooled.buffer();
|
||||
buf.writeInt(MessageType.TankStartMoving.ordinal());
|
||||
byte[] data = message.toBytes();
|
||||
buf.writeInt(data.length);
|
||||
buf.writeBytes(data);
|
||||
|
||||
|
||||
embeddedChannel.writeInbound(buf.duplicate());
|
||||
|
||||
TankStartMovingMessage msg = (TankStartMovingMessage)embeddedChannel.readInbound();
|
||||
|
||||
assertEquals(5, msg.x);
|
||||
assertEquals(10, msg.y);
|
||||
assertEquals(Dir.LEFT, msg.dir);
|
||||
assertEquals(id, msg.id);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue