diff --git a/ratel-landlords/landlords-common/pom.xml b/ratel-landlords/landlords-common/pom.xml new file mode 100644 index 0000000..5ecf3fe --- /dev/null +++ b/ratel-landlords/landlords-common/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + landlords-common + Landlords-common + This is a console version of the fight landlord game + + + com.smallnico.ratel + landlords + 1.0.0 + + + + + com.smallnico + noson + 1.1.4 + + + \ No newline at end of file diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/trap/landlords/entity/Poker.java b/ratel-landlords/landlords-common/src/main/java/org/nico/trap/landlords/entity/Poker.java new file mode 100644 index 0000000..5762b6b --- /dev/null +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/trap/landlords/entity/Poker.java @@ -0,0 +1,47 @@ +package org.nico.trap.landlords.entity; + +import org.nico.trap.landlords.enums.PokerLevel; +import org.nico.trap.landlords.enums.PokerType; + +/** + * Poke, with {@link PokerLevel} and {@link PokerType} + * + * @author nico + */ +public class Poker { + + private PokerLevel level; + + private PokerType type; + + public final PokerLevel getLevel() { + return level; + } + + public final void setLevel(PokerLevel level) { + this.level = level; + } + + public final PokerType getType() { + return type; + } + + public final void setType(PokerType type) { + this.type = type; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Poker other = (Poker) obj; + if (level != other.level) + return false; + return true; + } + +} diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/trap/landlords/enums/PokerLevel.java b/ratel-landlords/landlords-common/src/main/java/org/nico/trap/landlords/enums/PokerLevel.java new file mode 100644 index 0000000..a69584d --- /dev/null +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/trap/landlords/enums/PokerLevel.java @@ -0,0 +1,67 @@ +package org.nico.trap.landlords.enums; + +/** + * Poker level + * + * @author nico + */ +public enum PokerLevel { + + LEVEL_3(3, "3"), + + LEVEL_4(4, "4"), + + LEVEL_5(5, "5"), + + LEVEL_6(6, "6"), + + LEVEL_7(7, "7"), + + LEVEL_8(8, "8"), + + LEVEL_9(9, "9"), + + LEVEL_10(10, "10"), + + LEVEL_J(11, "J"), + + LEVEL_Q(12, "Q"), + + LEVEL_K(13, "K"), + + LEVEL_A(14, "A"), + + LEVEL_2(15, "2"), + + LEVEL_SMALL_KING(16, "SMALL JOKER"), + + LEVEL_BIG_KING(17, "BIG JOKER"), + ; + + private int level; + + private String name; + + + + private PokerLevel(int level, String name) { + this.name = name; + this.level = level; + } + + public final String getName() { + return name; + } + + public final void setName(String name) { + this.name = name; + } + + public final int getLevel() { + return level; + } + + public final void setLevel(int level) { + this.level = level; + } +} diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/trap/landlords/enums/PokerType.java b/ratel-landlords/landlords-common/src/main/java/org/nico/trap/landlords/enums/PokerType.java new file mode 100644 index 0000000..ee19996 --- /dev/null +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/trap/landlords/enums/PokerType.java @@ -0,0 +1,28 @@ +package org.nico.trap.landlords.enums; + +/** + * Poker type Spade、 Heart、 Diamond、 Club + * + * @author nico + */ +public enum PokerType { + + DIAMOND("♦"), + + CLUB("♣"), + + SPADE("♠"), + + HEART("♥") + ; + + private String name; + + private PokerType(String name) { + this.name = name; + } + + public final String getName() { + return name; + } +} diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/trap/landlords/transfer/ByteKit.java b/ratel-landlords/landlords-common/src/main/java/org/nico/trap/landlords/transfer/ByteKit.java new file mode 100644 index 0000000..6be5400 --- /dev/null +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/trap/landlords/transfer/ByteKit.java @@ -0,0 +1,48 @@ +package org.nico.trap.landlords.transfer; + +/** + * Byte manipulation tool + * + * @author nico + */ +public class ByteKit { + + /** + * Target byte array + */ + private byte[] bytes; + + public ByteKit(byte[] bytes) { + this.bytes = bytes; + } + + /** + * Gets the index of the incoming array in the target array, not matched to return -1 + * + * @param bs Incoming array + * @param start Matching start index + * @return Match index, not match to return -1 + */ + public int indexOf(byte[] bs, int start) { + int targetIndex = -1; + if(bs == null) { + return targetIndex; + } + for(int index = start; index < bytes.length; index ++) { + byte cbyte = bytes[index]; + if(bs[0] == cbyte) { + boolean isEquals = true; + for(int sindex = 1; sindex < bs.length; sindex ++) { + if(index + sindex >= bytes.length || bs[sindex] != bytes[index + sindex]) { + isEquals = false; + break; + } + } + if(isEquals) { + targetIndex = index; + } + } + } + return targetIndex; + } +} diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/trap/landlords/transfer/ByteLink.java b/ratel-landlords/landlords-common/src/main/java/org/nico/trap/landlords/transfer/ByteLink.java new file mode 100644 index 0000000..07dbbd8 --- /dev/null +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/trap/landlords/transfer/ByteLink.java @@ -0,0 +1,81 @@ +package org.nico.trap.landlords.transfer; + +public class ByteLink{ + + private ByteNode start; + + private ByteNode current; + + private int size; + + public void append(byte b){ + if(start == null){ + start = new ByteNode(b); + current = start; + }else{ + ByteNode node = new ByteNode(b); + current.setNext(node); + current = node; + } + size ++; + } + + public void append(byte[] bs){ + if(bs != null) { + for(byte b: bs) { + append(b); + } + } + } + + public byte[] toArray(){ + if(size == 0){ + return null; + } + byte[] bytes = new byte[size]; + int index = 0; + ByteNode s = start.clone(); + while(s != null){ + bytes[index ++] = s.getB(); + s = s.getNext(); + } + return bytes; + } + + public static class ByteNode{ + + private byte b; + + private ByteNode next; + + public ByteNode(byte b) { + this.b = b; + } + + public ByteNode(byte b, ByteNode next) { + this.b = b; + this.next = next; + } + + protected ByteNode clone(){ + return new ByteNode(b, next); + } + + public byte getB() { + return b; + } + + public void setB(byte b) { + this.b = b; + } + + public ByteNode getNext() { + return next; + } + + public void setNext(ByteNode next) { + this.next = next; + } + + } +} \ No newline at end of file diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/trap/landlords/transfer/TransferProtocolUtils.java b/ratel-landlords/landlords-common/src/main/java/org/nico/trap/landlords/transfer/TransferProtocolUtils.java new file mode 100644 index 0000000..8e7b617 --- /dev/null +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/trap/landlords/transfer/TransferProtocolUtils.java @@ -0,0 +1,43 @@ +package org.nico.trap.landlords.transfer; + +import org.nico.noson.Noson; + +/** + * Protocol transport related tools + * + * @author nico + * @time 2018-11-01 20:43 + */ +public class TransferProtocolUtils { + + /** + * A protocol header that represents the beginning of an available stream of data + */ + public static final byte[] PROTOCOL_HAED = "#".getBytes(); + + /** + * The end of the protocol used to represent the end of an available stream of data + */ + public static final byte[] PROTOCOL_TAIL = "$".getBytes(); + + /** + * Serialize the poker list to transportable bytes + * + * @param pokers Poker list + * @return Transportable byte array + */ + public static byte[] serialize(T object) { + ByteLink bl = new ByteLink(); + bl.append(PROTOCOL_HAED); + bl.append(Noson.reversal(object).getBytes()); + bl.append(PROTOCOL_TAIL); + return bl.toArray(); + } + + public static T unserialize(byte[] bytes) { + ByteLink bl = new ByteLink(); + + return null; + } + +} diff --git a/ratel-landlords/landlords-server/src/main/java/org/nico/trap/landlords/handler/EchoHandler.java b/ratel-landlords/landlords-server/src/main/java/org/nico/trap/landlords/handler/EchoHandler.java new file mode 100644 index 0000000..fec486c --- /dev/null +++ b/ratel-landlords/landlords-server/src/main/java/org/nico/trap/landlords/handler/EchoHandler.java @@ -0,0 +1,15 @@ +package org.nico.trap.landlords.handler; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +public class EchoHandler extends ChannelInboundHandlerAdapter{ + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + System.out.println( + "Server received: " + msg); + ctx.writeAndFlush("ok"); + } + +} diff --git a/ratel-landlords/landlords-server/src/main/java/org/nico/trap/landlords/handler/ServerDecoder.java b/ratel-landlords/landlords-server/src/main/java/org/nico/trap/landlords/handler/ServerDecoder.java new file mode 100644 index 0000000..6b097dd --- /dev/null +++ b/ratel-landlords/landlords-server/src/main/java/org/nico/trap/landlords/handler/ServerDecoder.java @@ -0,0 +1,25 @@ +package org.nico.trap.landlords.handler; + +import java.util.List; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; + +public class ServerDecoder extends ByteToMessageDecoder{ + + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { + if (in.readableBytes() >= 4) { + byte[] bs = new byte[4]; + in.getBytes(0, bs); + for(byte b: bs) { + System.out.print(b + " "); + } + System.out.println(); + int i = in.readInt(); + out.add(i); + } + } + +} diff --git a/ratel-landlords/landlords-server/src/main/java/org/nico/trap/landlords/server/SimpleServer.java b/ratel-landlords/landlords-server/src/main/java/org/nico/trap/landlords/server/SimpleServer.java new file mode 100644 index 0000000..58da28f --- /dev/null +++ b/ratel-landlords/landlords-server/src/main/java/org/nico/trap/landlords/server/SimpleServer.java @@ -0,0 +1,43 @@ +package org.nico.trap.landlords.server; + +import java.net.InetSocketAddress; + +import org.nico.trap.landlords.handler.EchoHandler; +import org.nico.trap.landlords.handler.ServerDecoder; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; + +public class SimpleServer { + + public static void main(String[] args) throws InterruptedException { + + EventLoopGroup group = new NioEventLoopGroup(); + + try { + ServerBootstrap b = new ServerBootstrap(); + b.group(group) + .channel(NioServerSocketChannel.class) + .localAddress(new InetSocketAddress(8000)) + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new ServerDecoder()); + ch.pipeline().addLast(new EchoHandler()); + } + }); + + ChannelFuture f = b.bind().sync(); + f.channel().closeFuture().sync(); + } finally { + group.shutdownGracefully().sync(); + } + + + } +} diff --git a/ratel-landlords/pom.xml b/ratel-landlords/pom.xml index 857fe34..45b12c0 100644 --- a/ratel-landlords/pom.xml +++ b/ratel-landlords/pom.xml @@ -47,6 +47,13 @@ 2.0.5.RELEASE + + + io.netty + netty-all + + + @@ -151,6 +158,7 @@ landlords-client landlords-server + landlords-common