├── core ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── bin │ ├── .gitignore │ └── log4j.xml └── src │ ├── com │ └── game │ │ ├── message │ │ ├── MessagePool.java │ │ └── struct │ │ │ ├── Bean.java │ │ │ ├── Handler.java │ │ │ └── Message.java │ │ ├── netty │ │ ├── Client.java │ │ ├── Netty.java │ │ ├── Server.java │ │ ├── coder │ │ │ ├── Decoder.java │ │ │ └── Encoder.java │ │ └── handler │ │ │ └── Handler.java │ │ ├── script │ │ ├── ScriptLoader.java │ │ └── struct │ │ │ └── IScript.java │ │ ├── thread │ │ ├── pool │ │ │ ├── FixedPoolExecutor.java │ │ │ └── NormalThread.java │ │ └── queue │ │ │ ├── FixTaskThread.java │ │ │ └── ITask.java │ │ └── util │ │ ├── IdGenerator.java │ │ └── TimeUtil.java │ └── log4j.xml ├── res ├── .project ├── jar │ ├── c3p0-0.9.1.1.jar │ ├── commons-io-2.4.jar │ ├── dom4j-1.6.1.jar │ ├── ehcache-2.8.3.jar │ ├── freemarker-2.3.20.jar │ ├── guava-17.0.jar │ ├── log4j-1.2.17.jar │ ├── mybatis-3.2.6.jar │ ├── mysql-connector-java-5.1.30.jar │ ├── netty-all-5.0.0.Alpha1.jar │ ├── protobuf-java-2.5.0.jar │ ├── slf4j-api-1.6.6.jar │ └── slf4j-log4j12-1.7.7.jar └── message │ ├── account.xml │ ├── login.xml │ ├── map.xml │ ├── message.txt │ └── role.xml ├── robot-mmo ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── bin │ ├── Start.class │ ├── com │ │ └── game │ │ │ ├── client │ │ │ ├── GameClient$1.class │ │ │ ├── GameClient.class │ │ │ ├── GameHandler.class │ │ │ └── thread │ │ │ │ └── ClientThread.class │ │ │ ├── config │ │ │ └── ConfigManager.class │ │ │ ├── login │ │ │ ├── handler │ │ │ │ ├── ResLoginCreateRoleHandler.class │ │ │ │ └── ResLoginHandler.class │ │ │ └── message │ │ │ │ ├── ReqLoginCreateRoleMessage.class │ │ │ │ ├── ReqLoginMessage.class │ │ │ │ ├── ReqLoginSelectRoleMessage.class │ │ │ │ ├── ResLoginCreateRoleMessage.class │ │ │ │ ├── ResLoginMessage.class │ │ │ │ └── RoleBrief.class │ │ │ ├── manager │ │ │ ├── Manager.class │ │ │ └── ManagerPool.class │ │ │ └── message │ │ │ └── manager │ │ │ └── MessageManager.class │ ├── log4j.xml │ └── message.xml ├── build.xml ├── config │ └── config.xml └── src │ ├── Start.java │ ├── com │ └── game │ │ ├── client │ │ ├── GameClient.java │ │ ├── GameHandler.java │ │ └── thread │ │ │ └── ClientThread.java │ │ ├── config │ │ └── ConfigManager.java │ │ ├── login │ │ ├── handler │ │ │ ├── ResLoginCreateRoleHandler.java │ │ │ └── ResLoginHandler.java │ │ └── message │ │ │ ├── ReqLoginCreateRoleMessage.java │ │ │ ├── ReqLoginMessage.java │ │ │ ├── ReqLoginSelectRoleMessage.java │ │ │ ├── ResLoginCreateRoleMessage.java │ │ │ ├── ResLoginMessage.java │ │ │ └── RoleBrief.java │ │ ├── manager │ │ ├── Manager.java │ │ └── ManagerPool.java │ │ └── message │ │ └── manager │ │ └── MessageManager.java │ ├── log4j.xml │ └── message.xml ├── server-mmo ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── bin │ ├── .gitignore │ ├── Start.class │ ├── log4j.xml │ └── message.xml ├── build.xml ├── config │ ├── db-config.xml │ ├── db-data.xml │ ├── game.xml │ └── server.xml ├── proto.bat ├── proto │ └── role.proto └── src │ ├── Start.java │ ├── com │ ├── config │ │ ├── manager │ │ │ └── ConfigManager.java │ │ └── struct │ │ │ ├── GameConfig.java │ │ │ └── ServerConfig.java │ ├── db │ │ ├── DbFactory.java │ │ ├── config │ │ │ ├── DbConfigManager.java │ │ │ ├── bean │ │ │ │ └── MapBean.java │ │ │ ├── container │ │ │ │ └── MapContainer.java │ │ │ ├── dao │ │ │ │ └── MapDao.java │ │ │ └── mapper │ │ │ │ └── Map.xml │ │ └── data │ │ │ ├── bean │ │ │ ├── AccountBean.java │ │ │ └── RoleBean.java │ │ │ ├── dao │ │ │ ├── AccountDao.java │ │ │ └── RoleDao.java │ │ │ └── mapper │ │ │ ├── Account.xml │ │ │ └── Role.xml │ ├── game │ │ ├── account │ │ │ ├── cache │ │ │ │ ├── AccountCache.java │ │ │ │ └── Key.java │ │ │ ├── manager │ │ │ │ └── AccountManager.java │ │ │ └── struct │ │ │ │ └── Account.java │ │ ├── login │ │ │ ├── handler │ │ │ │ ├── ReqLoginCreateRoleHandler.java │ │ │ │ ├── ReqLoginHandler.java │ │ │ │ └── ReqLoginSelectRoleHandler.java │ │ │ ├── manager │ │ │ │ ├── LoginManager.java │ │ │ │ └── LoginStateManager.java │ │ │ ├── message │ │ │ │ ├── ReqLoginCreateRoleMessage.java │ │ │ │ ├── ReqLoginMessage.java │ │ │ │ ├── ReqLoginSelectRoleMessage.java │ │ │ │ ├── ResLoginCreateRoleMessage.java │ │ │ │ ├── ResLoginMessage.java │ │ │ │ └── RoleBrief.java │ │ │ └── struct │ │ │ │ └── LoginState.java │ │ ├── map │ │ │ ├── handler │ │ │ │ └── ReqMapMoveHandler.java │ │ │ ├── manager │ │ │ │ └── MapManager.java │ │ │ ├── message │ │ │ │ ├── MapInfo.java │ │ │ │ ├── Position.java │ │ │ │ ├── ReqMapMoveMessage.java │ │ │ │ ├── ResMapChangeMessage.java │ │ │ │ ├── ResMapPositionChangeMessage.java │ │ │ │ ├── ResMapRoleMessage.java │ │ │ │ ├── ResMapRoundAllMessage.java │ │ │ │ └── RoleInfo.java │ │ │ ├── struct │ │ │ │ ├── Area.java │ │ │ │ ├── Map.java │ │ │ │ ├── MapTask.java │ │ │ │ ├── Position.java │ │ │ │ └── RoleMapData.java │ │ │ ├── thread │ │ │ │ ├── MapThread.java │ │ │ │ └── MapThreadPool.java │ │ │ └── timer │ │ │ │ └── ITimer.java │ │ ├── name │ │ │ └── manager │ │ │ │ └── NameManager.java │ │ └── role │ │ │ ├── cache │ │ │ └── RoleCache.java │ │ │ ├── manager │ │ │ └── RoleManager.java │ │ │ └── struct │ │ │ ├── Role.java │ │ │ └── RoleSaveData.java │ ├── logger │ │ └── GlobalLogger.java │ ├── manager │ │ ├── Manager.java │ │ ├── ManagerPool.java │ │ └── PriorityEnum.java │ ├── message │ │ ├── manager │ │ │ └── MessageManager.java │ │ └── util │ │ │ ├── MessageDispatcher.java │ │ │ └── MessageUtil.java │ ├── script │ │ └── manager │ │ │ └── ScriptManager.java │ ├── server │ │ └── game │ │ │ ├── GameHandler.java │ │ │ └── GameServer.java │ └── thread │ │ └── manager │ │ └── ThreadManager.java │ ├── log4j.xml │ └── message.xml └── tools ├── .classpath ├── .project ├── .settings └── org.eclipse.jdt.core.prefs ├── bin └── .gitignore ├── config └── config.xml ├── ftl ├── db │ └── java │ │ ├── bean.ftl │ │ ├── config.ftl │ │ ├── container.ftl │ │ ├── dao.ftl │ │ ├── manager.ftl │ │ └── mapper.ftl └── message │ └── java │ ├── bean.ftl │ ├── handler.ftl │ ├── manager.ftl │ └── message.ftl └── src └── tool ├── db ├── DbOpt.java ├── Generator.java ├── Start.java └── struct │ ├── Bean.java │ ├── Config.java │ ├── Container.java │ ├── Dao.java │ ├── Field.java │ ├── Manager.java │ └── Mapper.java ├── ftl ├── FtlManager.java ├── FunctionEnum.java ├── IFtl.java └── LanguageEnum.java ├── message ├── Generator.java ├── MainUi.java └── struct │ ├── Bean.java │ ├── Field.java │ ├── Handler.java │ ├── Manager.java │ └── Message.java └── util ├── Config.java └── StringUtil.java /core/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /core/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | core 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /core/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /core/bin/.gitignore: -------------------------------------------------------------------------------- 1 | /com/ 2 | -------------------------------------------------------------------------------- /core/bin/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /core/src/com/game/message/MessagePool.java: -------------------------------------------------------------------------------- 1 | package com.game.message; 2 | 3 | import java.util.HashMap; 4 | 5 | import org.apache.log4j.Logger; 6 | 7 | import com.game.message.struct.Handler; 8 | import com.game.message.struct.Message; 9 | 10 | public class MessagePool { 11 | private static MessagePool instance = new MessagePool(); 12 | private MessagePool() {} 13 | public static MessagePool getInstance() { 14 | return instance; 15 | } 16 | 17 | private Logger logger = Logger.getLogger(this.getClass()); 18 | 19 | private HashMap> id2handler = new HashMap>(); 20 | private HashMap> id2message = new HashMap>(); 21 | 22 | public void register(int id, Class handlerClass, Class messageClass) { 23 | id2handler.put(id, handlerClass); 24 | id2message.put(id, messageClass); 25 | } 26 | 27 | public Handler createHandler(int id) { 28 | Class handlerClass = id2handler.get(id); 29 | if (handlerClass == null) { 30 | return null; 31 | } 32 | Handler handler = null; 33 | try { 34 | handler = handlerClass.newInstance(); 35 | } catch (InstantiationException e) { 36 | e.printStackTrace(); 37 | } catch (IllegalAccessException e) { 38 | e.printStackTrace(); 39 | } 40 | return handler; 41 | } 42 | 43 | public Message createMessage(int id) { 44 | Class messageClass = id2message.get(id); 45 | if (messageClass == null) { 46 | return null; 47 | } 48 | Message message = null; 49 | try { 50 | message = messageClass.newInstance(); 51 | } catch (InstantiationException e) { 52 | logger.error(e, e); 53 | } catch (IllegalAccessException e) { 54 | logger.error(e, e); 55 | } 56 | return message; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /core/src/com/game/message/struct/Bean.java: -------------------------------------------------------------------------------- 1 | package com.game.message.struct; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | import org.apache.log4j.Logger; 5 | 6 | import java.io.UnsupportedEncodingException; 7 | 8 | /** 9 | * Created by game on 4/2/14. 10 | */ 11 | public abstract class Bean { 12 | private static Logger log = Logger.getLogger(Bean.class); 13 | public abstract boolean read(ByteBuf buf); 14 | public abstract boolean write(ByteBuf buf); 15 | 16 | // byte 17 | public byte readByte(ByteBuf buf) { 18 | return buf.readByte(); 19 | } 20 | public void writeByte(ByteBuf buf, byte value) { 21 | buf.writeByte(value); 22 | } 23 | 24 | // short 25 | public short readShort(ByteBuf buf) { 26 | return buf.readShort(); 27 | } 28 | public void writeShort(ByteBuf buf, short value) { 29 | buf.writeShort(value); 30 | } 31 | 32 | // int 33 | public int readInt(ByteBuf buf) { 34 | return buf.readInt(); 35 | } 36 | public void writeInt(ByteBuf buf, int value) { 37 | buf.writeInt(value); 38 | } 39 | 40 | // long 41 | public long readLong(ByteBuf buf) { 42 | return buf.readLong(); 43 | } 44 | public void writeLong(ByteBuf buf, long value) { 45 | buf.writeLong(value); 46 | } 47 | 48 | // string 49 | public String readString(ByteBuf buf) { 50 | int length = buf.readInt(); 51 | if (length <= 0) { 52 | return null; 53 | } 54 | if (buf.readableBytes() < length) { 55 | return null; 56 | } 57 | byte[] bytes = new byte[length]; 58 | buf.readBytes(bytes); 59 | try { 60 | return new String(bytes, "UTF-8"); 61 | } catch (UnsupportedEncodingException e) { 62 | log.error(e, e); 63 | } 64 | return null; 65 | } 66 | public void writeString(ByteBuf buf, String value) { 67 | if (value == null) { 68 | buf.writeInt(0); 69 | return; 70 | } 71 | 72 | try { 73 | byte[] bytes = value.getBytes("UTF-8"); 74 | buf.writeInt(bytes.length); 75 | buf.writeBytes(bytes); 76 | } catch (UnsupportedEncodingException e) { 77 | log.error(e, e); 78 | } 79 | } 80 | 81 | // bean 82 | public Bean readBean(ByteBuf buf, Class clazz) { 83 | try{ 84 | Bean bean = clazz.newInstance(); 85 | bean.read(buf); 86 | return bean; 87 | }catch (Exception e) { 88 | log.error(e, e); 89 | } 90 | return null; 91 | } 92 | public void writeBean(ByteBuf buf, Bean value) { 93 | value.write(buf); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /core/src/com/game/message/struct/Handler.java: -------------------------------------------------------------------------------- 1 | package com.game.message.struct; 2 | 3 | import io.netty.channel.ChannelHandlerContext; 4 | 5 | /** 6 | * Created by game on 3/31/14. 7 | */ 8 | public abstract class Handler { 9 | private Message message; // message 10 | private ChannelHandlerContext context; 11 | 12 | public ChannelHandlerContext getContext() { 13 | return context; 14 | } 15 | 16 | public void setContext(ChannelHandlerContext context) { 17 | this.context = context; 18 | } 19 | 20 | public abstract void exec(); 21 | 22 | public Message getMessage() { 23 | return message; 24 | } 25 | 26 | public void setMessage(Message message) { 27 | this.message = message; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /core/src/com/game/message/struct/Message.java: -------------------------------------------------------------------------------- 1 | package com.game.message.struct; 2 | 3 | /** 4 | * Created by game on 4/2/14. 5 | */ 6 | public abstract class Message extends Bean { 7 | public abstract int getId(); 8 | } 9 | -------------------------------------------------------------------------------- /core/src/com/game/netty/Client.java: -------------------------------------------------------------------------------- 1 | package com.game.netty; 2 | 3 | import io.netty.bootstrap.Bootstrap; 4 | import io.netty.channel.ChannelFuture; 5 | import io.netty.channel.ChannelOption; 6 | import io.netty.channel.EventLoopGroup; 7 | import io.netty.channel.nio.NioEventLoopGroup; 8 | import io.netty.channel.socket.nio.NioSocketChannel; 9 | 10 | import org.apache.log4j.Logger; 11 | 12 | /** 13 | * Created by game on 3/30/14. 14 | */ 15 | public abstract class Client extends Netty { 16 | private static Logger logger = Logger.getLogger(Client.class); 17 | // private static 18 | 19 | protected abstract boolean init(); 20 | public boolean init(String host, int port) { 21 | if (!init()) { 22 | return false; 23 | } 24 | EventLoopGroup workerGroup = new NioEventLoopGroup(); 25 | Runtime.getRuntime().addShutdownHook(new Thread(new StopThread())); 26 | 27 | try { 28 | Bootstrap b = new Bootstrap(); 29 | b.group(workerGroup); 30 | b.channel(NioSocketChannel.class); 31 | b.option(ChannelOption.SO_KEEPALIVE, true); 32 | b.handler(getChannelInitializer()); 33 | 34 | // Start the client. 35 | ChannelFuture f = b.connect(host, port).sync(); 36 | 37 | // Wait until the connection is closed. 38 | f.channel().closeFuture().sync(); 39 | } catch (Exception e) { 40 | logger.error(e, e); 41 | return false; 42 | } finally { 43 | workerGroup.shutdownGracefully(); 44 | } 45 | 46 | return true; 47 | } 48 | 49 | private class StopThread implements Runnable { 50 | public void run() { 51 | stop(); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /core/src/com/game/netty/Netty.java: -------------------------------------------------------------------------------- 1 | package com.game.netty; 2 | 3 | import io.netty.channel.ChannelHandlerAdapter; 4 | import io.netty.channel.ChannelInitializer; 5 | import io.netty.channel.socket.SocketChannel; 6 | 7 | public abstract class Netty extends ChannelHandlerAdapter { 8 | public abstract void stop(); 9 | public abstract ChannelInitializer getChannelInitializer(); 10 | } 11 | -------------------------------------------------------------------------------- /core/src/com/game/netty/Server.java: -------------------------------------------------------------------------------- 1 | package com.game.netty; 2 | 3 | import io.netty.bootstrap.ServerBootstrap; 4 | import io.netty.channel.ChannelFuture; 5 | import io.netty.channel.ChannelOption; 6 | import io.netty.channel.EventLoopGroup; 7 | import io.netty.channel.nio.NioEventLoopGroup; 8 | import io.netty.channel.socket.nio.NioServerSocketChannel; 9 | 10 | import org.apache.log4j.Logger; 11 | 12 | public abstract class Server extends Netty { 13 | private static Logger logger = Logger.getLogger(Server.class); 14 | 15 | protected abstract boolean init(); 16 | public boolean init(int port) { 17 | if (!init()) { 18 | return false; 19 | } 20 | 21 | Runtime.getRuntime().addShutdownHook(new Thread(new StopThread())); 22 | 23 | EventLoopGroup accepterGroup = new NioEventLoopGroup(); 24 | EventLoopGroup clientGroup = new NioEventLoopGroup(); 25 | 26 | try { 27 | ServerBootstrap b = new ServerBootstrap(); 28 | b.group(accepterGroup, clientGroup) 29 | .channel(NioServerSocketChannel.class) 30 | .childHandler(getChannelInitializer()) 31 | .option(ChannelOption.SO_BACKLOG, 128) 32 | .childOption(ChannelOption.SO_KEEPALIVE, true); 33 | 34 | // Bind and start to accept incoming connections. 35 | ChannelFuture f = b.bind(port).sync(); 36 | 37 | // Wait until the server socket is closed. 38 | // In this example, this does not happen, but you can do that to gracefully 39 | // shut down your server. 40 | f.channel().closeFuture().sync(); 41 | } catch (Exception e) { 42 | logger.error(e, e); 43 | return false; 44 | } finally { 45 | clientGroup.shutdownGracefully(); 46 | accepterGroup.shutdownGracefully(); 47 | } 48 | 49 | return true; 50 | } 51 | 52 | private class StopThread implements Runnable { 53 | public void run() { 54 | stop(); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /core/src/com/game/netty/coder/Decoder.java: -------------------------------------------------------------------------------- 1 | package com.game.netty.coder; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | import io.netty.channel.ChannelHandlerContext; 5 | import io.netty.handler.codec.ByteToMessageDecoder; 6 | import io.netty.util.AttributeKey; 7 | 8 | import java.util.List; 9 | 10 | import org.apache.log4j.Logger; 11 | 12 | import com.game.message.MessagePool; 13 | import com.game.message.struct.Message; 14 | 15 | /** 16 | * Created by Administrator on 2014/4/17. 17 | */ 18 | public class Decoder extends ByteToMessageDecoder { 19 | private static Logger logger = Logger.getLogger(Decoder.class); 20 | private static final String MESSAGE_COUNT = "MESSAGE_COUNT"; 21 | private static final String MESSAGE_TIME = "MESSAGE_TIME"; 22 | private static final int MESSAGE_COUNT_NUM = 30; 23 | private static final int MESSAGE_COUNT_MSEC = 1000; 24 | 25 | @Override 26 | protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { 27 | if (in.readableBytes() < (Integer.SIZE / Byte.SIZE)) { 28 | return; 29 | } 30 | in.markReaderIndex(); 31 | 32 | int length = in.readInt(); 33 | if (length < 0) { 34 | ctx.close(); 35 | return; 36 | } 37 | 38 | if (length > in.readableBytes()) { // wait until bytes enough 39 | in.resetReaderIndex(); 40 | return; 41 | } 42 | 43 | if (!rateCheck(ctx, MESSAGE_COUNT_NUM, MESSAGE_COUNT_MSEC)) { 44 | ctx.close(); 45 | return; 46 | } 47 | 48 | int messageId = in.readInt(); 49 | 50 | Message message = MessagePool.getInstance().createMessage(messageId); 51 | if (message == null) { 52 | logger.error("找不到消息:" + messageId); 53 | ctx.close(); 54 | return; 55 | } 56 | 57 | if (!message.read(in)) { 58 | logger.error("读取消息错误:" + messageId); 59 | ctx.close(); 60 | return; 61 | } 62 | 63 | out.add(message); 64 | } 65 | 66 | private boolean rateCheck(ChannelHandlerContext ctx, int maxNum, int perMsec) { 67 | Object object = ctx.attr(AttributeKey.valueOf(MESSAGE_TIME)).get(); 68 | if (object == null) { 69 | object = System.currentTimeMillis(); 70 | ctx.attr(AttributeKey.valueOf(MESSAGE_TIME)).set(object); 71 | ctx.attr(AttributeKey.valueOf(MESSAGE_COUNT)).set(0); 72 | return true; 73 | } 74 | 75 | long time = (Long)object; 76 | long nowTime = System.currentTimeMillis(); 77 | if (time - nowTime > perMsec) { 78 | ctx.attr(AttributeKey.valueOf(MESSAGE_TIME)).set(nowTime); 79 | ctx.attr(AttributeKey.valueOf(MESSAGE_COUNT)).set(0); 80 | return true; 81 | } 82 | 83 | int count = (Integer)ctx.attr(AttributeKey.valueOf(MESSAGE_COUNT)).get(); 84 | if (count > maxNum) { 85 | return false; 86 | } 87 | ctx.attr(AttributeKey.valueOf(MESSAGE_COUNT)).set(count + 1); 88 | 89 | return true; 90 | } 91 | 92 | public static void main(String[] args) { 93 | System.out.println(Integer.SIZE); 94 | System.out.println(Byte.SIZE); 95 | System.out.println(Integer.SIZE / Byte.SIZE); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /core/src/com/game/netty/coder/Encoder.java: -------------------------------------------------------------------------------- 1 | package com.game.netty.coder; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | import io.netty.channel.ChannelHandlerContext; 5 | import io.netty.handler.codec.MessageToByteEncoder; 6 | 7 | import com.game.message.struct.Message; 8 | 9 | /** 10 | * Created by game on 4/3/14. 11 | */ 12 | public class Encoder extends MessageToByteEncoder { 13 | private ByteBuf buf; 14 | @Override 15 | protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception { 16 | if (buf == null) { 17 | buf = ctx.alloc().ioBuffer(); 18 | } 19 | 20 | buf.writeInt(msg.getId()); 21 | msg.write(buf); 22 | 23 | out.writeInt(buf.readableBytes()); 24 | out.writeBytes(buf, 0, buf.readableBytes()); 25 | 26 | buf.clear(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /core/src/com/game/netty/handler/Handler.java: -------------------------------------------------------------------------------- 1 | package com.game.netty.handler; 2 | 3 | import io.netty.channel.ChannelHandlerAdapter; 4 | import io.netty.channel.ChannelHandlerContext; 5 | 6 | import org.apache.log4j.Logger; 7 | 8 | import com.game.message.MessagePool; 9 | import com.game.message.struct.Message; 10 | 11 | public abstract class Handler extends ChannelHandlerAdapter { 12 | private Logger logger = Logger.getLogger(this.getClass()); 13 | 14 | @Override 15 | public void channelRead(ChannelHandlerContext ctx, Object obj) { // (2) 16 | if (!(obj instanceof Message)) { 17 | return ; 18 | } 19 | Message msg = (Message)obj; 20 | 21 | com.game.message.struct.Handler handler = null; 22 | try { 23 | handler = MessagePool.getInstance().createHandler(msg.getId()); 24 | } catch (Exception e) { 25 | ctx.close(); 26 | logger.error(e, e); 27 | return ; 28 | } 29 | 30 | if (handler == null) { 31 | ctx.close(); 32 | return ; 33 | } 34 | 35 | handler.setMessage(msg); 36 | handler.setContext(ctx); 37 | 38 | onRecvMsg(handler); 39 | } 40 | 41 | public void channelActive(ChannelHandlerContext ctx) throws Exception { 42 | super.channelActive(ctx); 43 | onActive(ctx); 44 | } 45 | 46 | @Override 47 | public void channelInactive(ChannelHandlerContext ctx) throws Exception { 48 | super.channelInactive(ctx); 49 | onInactive(ctx); 50 | } 51 | 52 | @Override 53 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4) 54 | logger.error(cause, cause); 55 | ctx.close(); 56 | } 57 | 58 | public abstract void onActive(ChannelHandlerContext ctx); 59 | public abstract void onInactive(ChannelHandlerContext ctx); 60 | public abstract void onRecvMsg(com.game.message.struct.Handler handler); 61 | } 62 | -------------------------------------------------------------------------------- /core/src/com/game/script/ScriptLoader.java: -------------------------------------------------------------------------------- 1 | package com.game.script; 2 | 3 | import java.io.File; 4 | import java.net.URL; 5 | import java.net.URLClassLoader; 6 | import java.util.ArrayList; 7 | import java.util.Enumeration; 8 | import java.util.HashMap; 9 | import java.util.List; 10 | import java.util.jar.JarEntry; 11 | import java.util.jar.JarFile; 12 | 13 | import com.game.script.struct.IScript; 14 | 15 | public class ScriptLoader { 16 | public HashMap loadJar(String fileName) throws Exception { 17 | HashMap scripts = new HashMap(); 18 | final File file = new File(fileName); 19 | URL[] urls = new URL[] { file.toURI().toURL() }; 20 | final URLClassLoader urlClassLoader = new URLClassLoader(urls); 21 | final List> classesFound = loadClass(urlClassLoader, file); 22 | for (final Class clazz : classesFound) { 23 | Object object = clazz.newInstance(); 24 | if (object instanceof IScript) { 25 | IScript script = (IScript)object; 26 | scripts.put(script.getId(), script); 27 | } 28 | } 29 | return scripts; 30 | } 31 | 32 | private List> loadClass(URLClassLoader loader, final File file) throws Exception { 33 | final List> clazzes = new ArrayList>(); 34 | @SuppressWarnings("resource") 35 | JarFile jarFile = new JarFile(file); 36 | Enumeration entries = jarFile.entries(); 37 | while (entries.hasMoreElements()) { 38 | JarEntry entry = entries.nextElement(); 39 | if (!entry.getName().endsWith("Script.class")) { 40 | continue; 41 | } 42 | Class clazz = loader.loadClass(convert(entry.getName())); 43 | clazzes.add(clazz); 44 | } 45 | return clazzes; 46 | } 47 | 48 | /** 49 | * 路径转换成类名s 50 | */ 51 | private String convert(final String path) { 52 | final String classString = path.replace(File.separator, ".").replace("/", ".").replace("\\", "").replace(".class", ""); 53 | return classString; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /core/src/com/game/script/struct/IScript.java: -------------------------------------------------------------------------------- 1 | package com.game.script.struct; 2 | 3 | public interface IScript { 4 | public int getId(); 5 | } 6 | -------------------------------------------------------------------------------- /core/src/com/game/thread/pool/FixedPoolExecutor.java: -------------------------------------------------------------------------------- 1 | package com.game.thread.pool; 2 | 3 | import java.util.concurrent.LinkedBlockingQueue; 4 | import java.util.concurrent.ThreadPoolExecutor; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | /** 8 | * 保证定量的任务不被拒绝的一个executor 9 | * 10 | * @author shell 11 | * 12 | */ 13 | public class FixedPoolExecutor { 14 | private ThreadPoolExecutor excutor; 15 | private int maxTask; 16 | 17 | public FixedPoolExecutor(int threadNum, int maxTask) { 18 | excutor = new ThreadPoolExecutor(threadNum, threadNum, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), new ThreadPoolExecutor.DiscardPolicy()); 19 | this.maxTask = maxTask; 20 | } 21 | 22 | public boolean execute(Runnable task) { 23 | if (excutor.getQueue().size() >= maxTask) { 24 | return false; 25 | } 26 | excutor.execute(task); 27 | return true; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /core/src/com/game/thread/pool/NormalThread.java: -------------------------------------------------------------------------------- 1 | package com.game.thread.pool; 2 | 3 | import com.game.message.struct.Handler; 4 | 5 | public class NormalThread implements Runnable { 6 | private Handler handler; 7 | 8 | public NormalThread(Handler handler) { 9 | this.handler = handler; 10 | } 11 | 12 | @Override 13 | public void run() { 14 | handler.exec(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /core/src/com/game/thread/queue/FixTaskThread.java: -------------------------------------------------------------------------------- 1 | package com.game.thread.queue; 2 | 3 | import java.util.concurrent.LinkedBlockingQueue; 4 | 5 | import org.apache.log4j.Logger; 6 | 7 | public class FixTaskThread extends Thread { 8 | private Logger logger; 9 | private LinkedBlockingQueue tasks; 10 | private boolean stopFlag = false; 11 | 12 | public FixTaskThread(String name, int size) { 13 | super(name); 14 | tasks = new LinkedBlockingQueue<>(size); 15 | logger = Logger.getLogger(name); 16 | } 17 | 18 | @Override 19 | public void run() { 20 | while (!stopFlag) { 21 | ITask task = tasks.poll(); 22 | if (task == null) { 23 | try { 24 | synchronized (this) { 25 | wait(); 26 | } 27 | } catch (Exception e) { 28 | logger.error(e, e); 29 | } 30 | } else { 31 | long s = System.currentTimeMillis(); 32 | task.exec(); 33 | long interval = System.currentTimeMillis() - s; 34 | if (s > 10) { 35 | logger.error(task.getClass().getName() + ":" + interval); 36 | } 37 | } 38 | } 39 | } 40 | 41 | public void addTask(ITask task) { 42 | try { 43 | tasks.add(task); 44 | synchronized (this) { 45 | notify(); 46 | } 47 | } catch (Exception e) { 48 | logger.error(e, e); 49 | } 50 | } 51 | 52 | public void onStop() { 53 | logger.error(getName() + "关闭..." + tasks.size()); 54 | stopFlag = true; 55 | for (ITask task = tasks.poll(); task != null; task = tasks.poll()) { 56 | long s = System.currentTimeMillis(); 57 | task.exec(); 58 | long interval = System.currentTimeMillis() - s; 59 | if (s > 10) { 60 | logger.error(task.getClass().getName() + ":" + interval); 61 | } 62 | } 63 | logger.error(getName() + "关闭"); 64 | } 65 | 66 | public static void main(String[] args) throws Exception { 67 | FixTaskThread thread = new FixTaskThread("shell", 10); 68 | thread.start(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /core/src/com/game/thread/queue/ITask.java: -------------------------------------------------------------------------------- 1 | package com.game.thread.queue; 2 | 3 | public interface ITask { 4 | public void exec(); 5 | } 6 | -------------------------------------------------------------------------------- /core/src/com/game/util/IdGenerator.java: -------------------------------------------------------------------------------- 1 | package com.game.util; 2 | 3 | import java.util.HashSet; 4 | 5 | public class IdGenerator { 6 | private static int seed = 0; 7 | /** 8 | * 产生唯一id(在server>0xffff 或者 每毫秒产生的id>0xffff的时候会出现重复) 9 | * @param server 10 | * @return 11 | */ 12 | public static synchronized long getId(int server) { 13 | ++seed; 14 | return (server & 0xFFFF) << 48 | (System.currentTimeMillis() / 1000L & 0xFFFFFFFF) << 16 | seed & 0xFFFF; 15 | } 16 | 17 | public static void main(String[] args) { 18 | HashSet set = new HashSet<>(); 19 | int server = 1; 20 | for (int i = 0; i < 0xFFFF + 5; ++i) { 21 | long id = getId(server); 22 | if (set.contains(id)) { 23 | System.out.println("重复"); 24 | } 25 | set.add(id); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /core/src/com/game/util/TimeUtil.java: -------------------------------------------------------------------------------- 1 | package com.game.util; 2 | 3 | import java.text.ParseException; 4 | import java.text.SimpleDateFormat; 5 | import java.util.Date; 6 | 7 | import org.apache.log4j.Logger; 8 | 9 | public class TimeUtil { 10 | private static Logger logger = Logger.getLogger(TimeUtil.class); 11 | 12 | /** 13 | * 字符串转日期("yyyy-MM-dd HH:mm:ss"); 14 | * @param date 15 | * @return 16 | */ 17 | public static Date getDateByString(String date) { 18 | try { 19 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 20 | return simpleDateFormat.parse(date); 21 | } catch (ParseException e) { 22 | logger.error("{}日期格式有误{}" + date + "yyyy-MM-dd HH:mm:ss"); 23 | return null; 24 | } 25 | } 26 | 27 | public static String getStringDate(Date date, String format) { 28 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); 29 | return simpleDateFormat.format(date); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /core/src/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /res/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | res 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /res/jar/c3p0-0.9.1.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/res/jar/c3p0-0.9.1.1.jar -------------------------------------------------------------------------------- /res/jar/commons-io-2.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/res/jar/commons-io-2.4.jar -------------------------------------------------------------------------------- /res/jar/dom4j-1.6.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/res/jar/dom4j-1.6.1.jar -------------------------------------------------------------------------------- /res/jar/ehcache-2.8.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/res/jar/ehcache-2.8.3.jar -------------------------------------------------------------------------------- /res/jar/freemarker-2.3.20.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/res/jar/freemarker-2.3.20.jar -------------------------------------------------------------------------------- /res/jar/guava-17.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/res/jar/guava-17.0.jar -------------------------------------------------------------------------------- /res/jar/log4j-1.2.17.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/res/jar/log4j-1.2.17.jar -------------------------------------------------------------------------------- /res/jar/mybatis-3.2.6.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/res/jar/mybatis-3.2.6.jar -------------------------------------------------------------------------------- /res/jar/mysql-connector-java-5.1.30.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/res/jar/mysql-connector-java-5.1.30.jar -------------------------------------------------------------------------------- /res/jar/netty-all-5.0.0.Alpha1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/res/jar/netty-all-5.0.0.Alpha1.jar -------------------------------------------------------------------------------- /res/jar/protobuf-java-2.5.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/res/jar/protobuf-java-2.5.0.jar -------------------------------------------------------------------------------- /res/jar/slf4j-api-1.6.6.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/res/jar/slf4j-api-1.6.6.jar -------------------------------------------------------------------------------- /res/jar/slf4j-log4j12-1.7.7.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/res/jar/slf4j-log4j12-1.7.7.jar -------------------------------------------------------------------------------- /res/message/account.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /res/message/login.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /res/message/map.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /res/message/message.txt: -------------------------------------------------------------------------------- 1 | login 100 2 | account 101 3 | role 102 4 | map 103 -------------------------------------------------------------------------------- /res/message/role.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /robot-mmo/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /robot-mmo/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | robot-mmo 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /robot-mmo/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /robot-mmo/bin/Start.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/robot-mmo/bin/Start.class -------------------------------------------------------------------------------- /robot-mmo/bin/com/game/client/GameClient$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/robot-mmo/bin/com/game/client/GameClient$1.class -------------------------------------------------------------------------------- /robot-mmo/bin/com/game/client/GameClient.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/robot-mmo/bin/com/game/client/GameClient.class -------------------------------------------------------------------------------- /robot-mmo/bin/com/game/client/GameHandler.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/robot-mmo/bin/com/game/client/GameHandler.class -------------------------------------------------------------------------------- /robot-mmo/bin/com/game/client/thread/ClientThread.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/robot-mmo/bin/com/game/client/thread/ClientThread.class -------------------------------------------------------------------------------- /robot-mmo/bin/com/game/config/ConfigManager.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/robot-mmo/bin/com/game/config/ConfigManager.class -------------------------------------------------------------------------------- /robot-mmo/bin/com/game/login/handler/ResLoginCreateRoleHandler.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/robot-mmo/bin/com/game/login/handler/ResLoginCreateRoleHandler.class -------------------------------------------------------------------------------- /robot-mmo/bin/com/game/login/handler/ResLoginHandler.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/robot-mmo/bin/com/game/login/handler/ResLoginHandler.class -------------------------------------------------------------------------------- /robot-mmo/bin/com/game/login/message/ReqLoginCreateRoleMessage.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/robot-mmo/bin/com/game/login/message/ReqLoginCreateRoleMessage.class -------------------------------------------------------------------------------- /robot-mmo/bin/com/game/login/message/ReqLoginMessage.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/robot-mmo/bin/com/game/login/message/ReqLoginMessage.class -------------------------------------------------------------------------------- /robot-mmo/bin/com/game/login/message/ReqLoginSelectRoleMessage.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/robot-mmo/bin/com/game/login/message/ReqLoginSelectRoleMessage.class -------------------------------------------------------------------------------- /robot-mmo/bin/com/game/login/message/ResLoginCreateRoleMessage.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/robot-mmo/bin/com/game/login/message/ResLoginCreateRoleMessage.class -------------------------------------------------------------------------------- /robot-mmo/bin/com/game/login/message/ResLoginMessage.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/robot-mmo/bin/com/game/login/message/ResLoginMessage.class -------------------------------------------------------------------------------- /robot-mmo/bin/com/game/login/message/RoleBrief.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/robot-mmo/bin/com/game/login/message/RoleBrief.class -------------------------------------------------------------------------------- /robot-mmo/bin/com/game/manager/Manager.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/robot-mmo/bin/com/game/manager/Manager.class -------------------------------------------------------------------------------- /robot-mmo/bin/com/game/manager/ManagerPool.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/robot-mmo/bin/com/game/manager/ManagerPool.class -------------------------------------------------------------------------------- /robot-mmo/bin/com/game/message/manager/MessageManager.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/robot-mmo/bin/com/game/message/manager/MessageManager.class -------------------------------------------------------------------------------- /robot-mmo/bin/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /robot-mmo/bin/message.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /robot-mmo/build.xml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/robot-mmo/build.xml -------------------------------------------------------------------------------- /robot-mmo/config/config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /robot-mmo/src/Start.java: -------------------------------------------------------------------------------- 1 | import com.game.client.thread.ClientThread; 2 | import com.game.manager.ManagerPool; 3 | import com.game.thread.pool.FixedPoolExecutor; 4 | 5 | 6 | public class Start { 7 | 8 | public static void main(String[] args) { 9 | ManagerPool.init(); 10 | FixedPoolExecutor executor = new FixedPoolExecutor(32, 1000000); 11 | for (int i = 0; i < ManagerPool.config.getRobot(); ++i) { 12 | executor.execute(new ClientThread(i, ManagerPool.config.getIp(), ManagerPool.config.getPort())); 13 | } 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /robot-mmo/src/com/game/client/GameClient.java: -------------------------------------------------------------------------------- 1 | package com.game.client; 2 | 3 | import io.netty.channel.ChannelInitializer; 4 | import io.netty.channel.socket.SocketChannel; 5 | 6 | import com.game.netty.Client; 7 | import com.game.netty.coder.Decoder; 8 | import com.game.netty.coder.Encoder; 9 | 10 | public class GameClient extends Client { 11 | 12 | @Override 13 | protected boolean init() { 14 | return true; 15 | } 16 | 17 | @Override 18 | public void stop() { 19 | } 20 | 21 | @Override 22 | public ChannelInitializer getChannelInitializer() { 23 | return new ChannelInitializer() { 24 | @Override 25 | protected void initChannel(SocketChannel ch) throws Exception { 26 | ch.pipeline().addLast(new Encoder()); 27 | ch.pipeline().addLast(new Decoder()); 28 | ch.pipeline().addLast(new GameHandler()); 29 | } 30 | }; 31 | } 32 | 33 | public static void main(String[] args) { 34 | GameClient client = new GameClient(); 35 | client.init("192.168.5.37", 5241); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /robot-mmo/src/com/game/client/GameHandler.java: -------------------------------------------------------------------------------- 1 | package com.game.client; 2 | 3 | import org.apache.log4j.Logger; 4 | 5 | import io.netty.channel.ChannelHandlerContext; 6 | 7 | import com.game.login.message.ReqLoginMessage; 8 | import com.game.netty.handler.Handler; 9 | import com.game.util.IdGenerator; 10 | 11 | public class GameHandler extends Handler { 12 | private static Logger logger = Logger.getLogger(GameHandler.class); 13 | 14 | @Override 15 | public void onActive(ChannelHandlerContext ctx) { 16 | ReqLoginMessage msg = new ReqLoginMessage(); 17 | msg.setAccountName(String.valueOf(IdGenerator.getId(1))); 18 | msg.setPlatform("37wan"); 19 | msg.setProtocol("gasdgfdsgfdsgfd"); 20 | msg.setServer(1); 21 | ctx.writeAndFlush(msg); 22 | } 23 | 24 | @Override 25 | public void onInactive(ChannelHandlerContext ctx) { 26 | logger.error("断开连接"); 27 | } 28 | 29 | @Override 30 | public void onRecvMsg(com.game.message.struct.Handler handler) { 31 | handler.exec(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /robot-mmo/src/com/game/client/thread/ClientThread.java: -------------------------------------------------------------------------------- 1 | package com.game.client.thread; 2 | 3 | import java.util.concurrent.atomic.AtomicInteger; 4 | 5 | import org.apache.log4j.Logger; 6 | 7 | import com.game.client.GameClient; 8 | 9 | public class ClientThread extends Thread { 10 | private String ip; 11 | private int port; 12 | private static Logger logger = Logger.getLogger(ClientThread.class); 13 | private static AtomicInteger count = new AtomicInteger(); 14 | private static AtomicInteger count2 = new AtomicInteger(); 15 | public ClientThread(int id, String ip, int port) { 16 | super(new StringBuilder().append("robot").append(id).toString()); 17 | this.ip = ip; 18 | this.port = port; 19 | } 20 | 21 | @Override 22 | public void run() { 23 | logger.error("start..." + count.incrementAndGet()); 24 | GameClient client = new GameClient(); 25 | client.init(ip, port); 26 | logger.error("close..." + count2.incrementAndGet()); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /robot-mmo/src/com/game/config/ConfigManager.java: -------------------------------------------------------------------------------- 1 | package com.game.config; 2 | 3 | import java.io.File; 4 | 5 | import org.apache.log4j.Logger; 6 | import org.dom4j.Document; 7 | import org.dom4j.DocumentException; 8 | import org.dom4j.Element; 9 | import org.dom4j.io.SAXReader; 10 | 11 | import com.game.manager.Manager; 12 | 13 | public class ConfigManager extends Manager { 14 | private static Logger logger = Logger.getLogger(ConfigManager.class); 15 | private int robot; 16 | private String ip; 17 | private int port; 18 | 19 | public int getRobot() { 20 | return robot; 21 | } 22 | 23 | public void setRobot(int robot) { 24 | this.robot = robot; 25 | } 26 | 27 | public String getIp() { 28 | return ip; 29 | } 30 | 31 | public void setIp(String ip) { 32 | this.ip = ip; 33 | } 34 | 35 | public int getPort() { 36 | return port; 37 | } 38 | 39 | public void setPort(int port) { 40 | this.port = port; 41 | } 42 | 43 | @Override 44 | public boolean init() { 45 | SAXReader reader = new SAXReader(); 46 | Document doc = null; 47 | try { 48 | doc = reader.read(new File("config/config.xml")); 49 | } catch (DocumentException e) { 50 | logger.error(e, e); 51 | } 52 | Element root = doc.getRootElement(); 53 | robot = Integer.parseInt(root.attributeValue("robot")); 54 | ip = root.attributeValue("ip"); 55 | port = Integer.parseInt(root.attributeValue("port")); 56 | return true; 57 | } 58 | 59 | @Override 60 | public void stop() { 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /robot-mmo/src/com/game/login/handler/ResLoginCreateRoleHandler.java: -------------------------------------------------------------------------------- 1 | package com.game.login.handler; 2 | 3 | import java.util.concurrent.atomic.AtomicInteger; 4 | 5 | import org.apache.log4j.Logger; 6 | 7 | import com.game.message.struct.Handler; 8 | 9 | public class ResLoginCreateRoleHandler extends Handler{ 10 | private static Logger logger = Logger.getLogger(ResLoginCreateRoleHandler.class); 11 | private static AtomicInteger count = new AtomicInteger(); 12 | 13 | @Override 14 | public void exec() { 15 | com.game.login.message.ResLoginCreateRoleMessage msg = (com.game.login.message.ResLoginCreateRoleMessage)this.getMessage(); 16 | if (msg.getRet() != 0) { 17 | logger.error("创建角色失败"); 18 | } 19 | if (count.incrementAndGet() % 10 == 0) { 20 | logger.error("当前创建角色完成数量:" + count); 21 | } 22 | this.getContext().close(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /robot-mmo/src/com/game/login/handler/ResLoginHandler.java: -------------------------------------------------------------------------------- 1 | package com.game.login.handler; 2 | 3 | import java.util.concurrent.atomic.AtomicInteger; 4 | 5 | import org.apache.log4j.Logger; 6 | 7 | import com.game.login.message.ReqLoginCreateRoleMessage; 8 | import com.game.message.struct.Handler; 9 | import com.game.util.IdGenerator; 10 | 11 | public class ResLoginHandler extends Handler{ 12 | private static Logger logger = Logger.getLogger(ResLoginHandler.class); 13 | 14 | private static AtomicInteger count = new AtomicInteger(); 15 | 16 | @Override 17 | public void exec() { 18 | com.game.login.message.ResLoginMessage msg = (com.game.login.message.ResLoginMessage)this.getMessage(); 19 | if (msg.getRet() != 0) { 20 | logger.error("登录失败"); 21 | } 22 | if (count.incrementAndGet() % 10 == 0) { 23 | logger.error("当前登录完成数量:" + count); 24 | } 25 | if (msg.getRoles() != null && !msg.getRoles().isEmpty()) { 26 | return ; 27 | } 28 | 29 | ReqLoginCreateRoleMessage ret = new ReqLoginCreateRoleMessage(); 30 | ret.setName("shell" + IdGenerator.getId(1)); 31 | this.getContext().writeAndFlush(ret); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /robot-mmo/src/com/game/login/message/ReqLoginCreateRoleMessage.java: -------------------------------------------------------------------------------- 1 | package com.game.login.message; 2 | 3 | import com.game.message.struct.Message; 4 | import io.netty.buffer.ByteBuf; 5 | import org.apache.log4j.Logger; 6 | 7 | /** 8 | * @author messageGenerator 9 | * 10 | * @version 1.0.0 11 | * 12 | * 创建角色消息 13 | */ 14 | public class ReqLoginCreateRoleMessage extends Message{ 15 | 16 | private static Logger log = Logger.getLogger(ReqLoginCreateRoleMessage.class); 17 | 18 | //角色名称 19 | private String name; 20 | 21 | 22 | /** 23 | * 写入字节缓存 24 | */ 25 | public boolean write(ByteBuf buf){ 26 | try { 27 | //角色名称 28 | writeString(buf, this.name); 29 | } catch (Exception e) { 30 | log.error(e, e); 31 | return false; 32 | } 33 | return true; 34 | } 35 | 36 | /** 37 | * 读取字节缓存 38 | */ 39 | public boolean read(ByteBuf buf){ 40 | try { 41 | //角色名称 42 | this.name = readString(buf); 43 | } catch (Exception e) { 44 | log.error(e, e); 45 | return false; 46 | } 47 | return true; 48 | } 49 | 50 | /** 51 | * get 角色名称 52 | * @return 53 | */ 54 | public String getName(){ 55 | return name; 56 | } 57 | 58 | /** 59 | * set 角色名称 60 | */ 61 | public void setName(String name){ 62 | this.name = name; 63 | } 64 | 65 | 66 | @Override 67 | public int getId() { 68 | return 100102; 69 | } 70 | 71 | @Override 72 | public String toString(){ 73 | StringBuffer buf = new StringBuffer("["); 74 | //角色名称 75 | if(this.name!=null) buf.append("name:" + name.toString() +","); 76 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 77 | buf.append("]"); 78 | return buf.toString(); 79 | } 80 | } -------------------------------------------------------------------------------- /robot-mmo/src/com/game/login/message/ReqLoginMessage.java: -------------------------------------------------------------------------------- 1 | package com.game.login.message; 2 | 3 | import com.game.message.struct.Message; 4 | import io.netty.buffer.ByteBuf; 5 | import org.apache.log4j.Logger; 6 | 7 | /** 8 | * @author messageGenerator 9 | * 10 | * @version 1.0.0 11 | * 12 | * 登录消息 13 | */ 14 | public class ReqLoginMessage extends Message{ 15 | 16 | private static Logger log = Logger.getLogger(ReqLoginMessage.class); 17 | 18 | //账号 19 | private String accountName; 20 | 21 | //协议 22 | private String protocol; 23 | 24 | //平台 25 | private String platform; 26 | 27 | //服务器 28 | private int server; 29 | 30 | 31 | /** 32 | * 写入字节缓存 33 | */ 34 | public boolean write(ByteBuf buf){ 35 | try { 36 | //账号 37 | writeString(buf, this.accountName); 38 | //协议 39 | writeString(buf, this.protocol); 40 | //平台 41 | writeString(buf, this.platform); 42 | //服务器 43 | writeInt(buf, this.server); 44 | } catch (Exception e) { 45 | log.error(e, e); 46 | return false; 47 | } 48 | return true; 49 | } 50 | 51 | /** 52 | * 读取字节缓存 53 | */ 54 | public boolean read(ByteBuf buf){ 55 | try { 56 | //账号 57 | this.accountName = readString(buf); 58 | //协议 59 | this.protocol = readString(buf); 60 | //平台 61 | this.platform = readString(buf); 62 | //服务器 63 | this.server = readInt(buf); 64 | } catch (Exception e) { 65 | log.error(e, e); 66 | return false; 67 | } 68 | return true; 69 | } 70 | 71 | /** 72 | * get 账号 73 | * @return 74 | */ 75 | public String getAccountName(){ 76 | return accountName; 77 | } 78 | 79 | /** 80 | * set 账号 81 | */ 82 | public void setAccountName(String accountName){ 83 | this.accountName = accountName; 84 | } 85 | 86 | /** 87 | * get 协议 88 | * @return 89 | */ 90 | public String getProtocol(){ 91 | return protocol; 92 | } 93 | 94 | /** 95 | * set 协议 96 | */ 97 | public void setProtocol(String protocol){ 98 | this.protocol = protocol; 99 | } 100 | 101 | /** 102 | * get 平台 103 | * @return 104 | */ 105 | public String getPlatform(){ 106 | return platform; 107 | } 108 | 109 | /** 110 | * set 平台 111 | */ 112 | public void setPlatform(String platform){ 113 | this.platform = platform; 114 | } 115 | 116 | /** 117 | * get 服务器 118 | * @return 119 | */ 120 | public int getServer(){ 121 | return server; 122 | } 123 | 124 | /** 125 | * set 服务器 126 | */ 127 | public void setServer(int server){ 128 | this.server = server; 129 | } 130 | 131 | 132 | @Override 133 | public int getId() { 134 | return 100100; 135 | } 136 | 137 | @Override 138 | public String toString(){ 139 | StringBuffer buf = new StringBuffer("["); 140 | //账号 141 | if(this.accountName!=null) buf.append("accountName:" + accountName.toString() +","); 142 | //协议 143 | if(this.protocol!=null) buf.append("protocol:" + protocol.toString() +","); 144 | //平台 145 | if(this.platform!=null) buf.append("platform:" + platform.toString() +","); 146 | //服务器 147 | buf.append("server:" + server +","); 148 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 149 | buf.append("]"); 150 | return buf.toString(); 151 | } 152 | } -------------------------------------------------------------------------------- /robot-mmo/src/com/game/login/message/ReqLoginSelectRoleMessage.java: -------------------------------------------------------------------------------- 1 | package com.game.login.message; 2 | 3 | import com.game.message.struct.Message; 4 | import io.netty.buffer.ByteBuf; 5 | import org.apache.log4j.Logger; 6 | 7 | /** 8 | * @author messageGenerator 9 | * 10 | * @version 1.0.0 11 | * 12 | * 选择角色进入游戏消息 13 | */ 14 | public class ReqLoginSelectRoleMessage extends Message{ 15 | 16 | private static Logger log = Logger.getLogger(ReqLoginSelectRoleMessage.class); 17 | 18 | //角色id 19 | private long roleId; 20 | 21 | 22 | /** 23 | * 写入字节缓存 24 | */ 25 | public boolean write(ByteBuf buf){ 26 | try { 27 | //角色id 28 | writeLong(buf, this.roleId); 29 | } catch (Exception e) { 30 | log.error(e, e); 31 | return false; 32 | } 33 | return true; 34 | } 35 | 36 | /** 37 | * 读取字节缓存 38 | */ 39 | public boolean read(ByteBuf buf){ 40 | try { 41 | //角色id 42 | this.roleId = readLong(buf); 43 | } catch (Exception e) { 44 | log.error(e, e); 45 | return false; 46 | } 47 | return true; 48 | } 49 | 50 | /** 51 | * get 角色id 52 | * @return 53 | */ 54 | public long getRoleId(){ 55 | return roleId; 56 | } 57 | 58 | /** 59 | * set 角色id 60 | */ 61 | public void setRoleId(long roleId){ 62 | this.roleId = roleId; 63 | } 64 | 65 | 66 | @Override 67 | public int getId() { 68 | return 100101; 69 | } 70 | 71 | @Override 72 | public String toString(){ 73 | StringBuffer buf = new StringBuffer("["); 74 | //角色id 75 | buf.append("roleId:" + roleId +","); 76 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 77 | buf.append("]"); 78 | return buf.toString(); 79 | } 80 | } -------------------------------------------------------------------------------- /robot-mmo/src/com/game/login/message/ResLoginCreateRoleMessage.java: -------------------------------------------------------------------------------- 1 | package com.game.login.message; 2 | 3 | import java.util.List; 4 | import java.util.ArrayList; 5 | import com.game.message.struct.Message; 6 | import io.netty.buffer.ByteBuf; 7 | import org.apache.log4j.Logger; 8 | 9 | /** 10 | * @author messageGenerator 11 | * 12 | * @version 1.0.0 13 | * 14 | * 创建角色消息 15 | */ 16 | public class ResLoginCreateRoleMessage extends Message{ 17 | 18 | private static Logger log = Logger.getLogger(ResLoginCreateRoleMessage.class); 19 | 20 | //返回值 21 | private byte ret; 22 | 23 | //角色信息 24 | private List roles = new ArrayList(); 25 | 26 | /** 27 | * 写入字节缓存 28 | */ 29 | public boolean write(ByteBuf buf){ 30 | try { 31 | //返回值 32 | writeByte(buf, this.ret); 33 | //角色信息 34 | writeShort(buf, (short)roles.size()); 35 | for (int i = 0; i < roles.size(); i++) { 36 | writeBean(buf, roles.get(i)); 37 | } 38 | } catch (Exception e) { 39 | log.error(e, e); 40 | return false; 41 | } 42 | return true; 43 | } 44 | 45 | /** 46 | * 读取字节缓存 47 | */ 48 | public boolean read(ByteBuf buf){ 49 | try { 50 | //返回值 51 | this.ret = readByte(buf); 52 | //角色信息 53 | int roles_length = readShort(buf); 54 | for (int i = 0; i < roles_length; i++) { 55 | roles.add((RoleBrief)readBean(buf, RoleBrief.class)); 56 | } 57 | } catch (Exception e) { 58 | log.error(e, e); 59 | return false; 60 | } 61 | return true; 62 | } 63 | 64 | /** 65 | * get 返回值 66 | * @return 67 | */ 68 | public byte getRet(){ 69 | return ret; 70 | } 71 | 72 | /** 73 | * set 返回值 74 | */ 75 | public void setRet(byte ret){ 76 | this.ret = ret; 77 | } 78 | 79 | /** 80 | * get 角色信息 81 | * @return 82 | */ 83 | public List getRoles(){ 84 | return roles; 85 | } 86 | 87 | /** 88 | * set 角色信息 89 | */ 90 | public void setRoles(List roles){ 91 | this.roles = roles; 92 | } 93 | 94 | 95 | @Override 96 | public int getId() { 97 | return 100201; 98 | } 99 | 100 | @Override 101 | public String toString(){ 102 | StringBuffer buf = new StringBuffer("["); 103 | //返回值 104 | buf.append("ret:" + ret +","); 105 | //角色信息 106 | buf.append("roles:{"); 107 | for (int i = 0; i < roles.size(); i++) { 108 | buf.append(roles.get(i).toString() +","); 109 | } 110 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 111 | buf.append("},"); 112 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 113 | buf.append("]"); 114 | return buf.toString(); 115 | } 116 | } -------------------------------------------------------------------------------- /robot-mmo/src/com/game/login/message/ResLoginMessage.java: -------------------------------------------------------------------------------- 1 | package com.game.login.message; 2 | 3 | import java.util.List; 4 | import java.util.ArrayList; 5 | import com.game.message.struct.Message; 6 | import io.netty.buffer.ByteBuf; 7 | import org.apache.log4j.Logger; 8 | 9 | /** 10 | * @author messageGenerator 11 | * 12 | * @version 1.0.0 13 | * 14 | * 登录消息 15 | */ 16 | public class ResLoginMessage extends Message{ 17 | 18 | private static Logger log = Logger.getLogger(ResLoginMessage.class); 19 | 20 | //返回值 21 | private byte ret; 22 | 23 | //角色信息 24 | private List roles = new ArrayList(); 25 | 26 | /** 27 | * 写入字节缓存 28 | */ 29 | public boolean write(ByteBuf buf){ 30 | try { 31 | //返回值 32 | writeByte(buf, this.ret); 33 | //角色信息 34 | writeShort(buf, (short)roles.size()); 35 | for (int i = 0; i < roles.size(); i++) { 36 | writeBean(buf, roles.get(i)); 37 | } 38 | } catch (Exception e) { 39 | log.error(e, e); 40 | return false; 41 | } 42 | return true; 43 | } 44 | 45 | /** 46 | * 读取字节缓存 47 | */ 48 | public boolean read(ByteBuf buf){ 49 | try { 50 | //返回值 51 | this.ret = readByte(buf); 52 | //角色信息 53 | int roles_length = readShort(buf); 54 | for (int i = 0; i < roles_length; i++) { 55 | roles.add((RoleBrief)readBean(buf, RoleBrief.class)); 56 | } 57 | } catch (Exception e) { 58 | log.error(e, e); 59 | return false; 60 | } 61 | return true; 62 | } 63 | 64 | /** 65 | * get 返回值 66 | * @return 67 | */ 68 | public byte getRet(){ 69 | return ret; 70 | } 71 | 72 | /** 73 | * set 返回值 74 | */ 75 | public void setRet(byte ret){ 76 | this.ret = ret; 77 | } 78 | 79 | /** 80 | * get 角色信息 81 | * @return 82 | */ 83 | public List getRoles(){ 84 | return roles; 85 | } 86 | 87 | /** 88 | * set 角色信息 89 | */ 90 | public void setRoles(List roles){ 91 | this.roles = roles; 92 | } 93 | 94 | 95 | @Override 96 | public int getId() { 97 | return 100200; 98 | } 99 | 100 | @Override 101 | public String toString(){ 102 | StringBuffer buf = new StringBuffer("["); 103 | //返回值 104 | buf.append("ret:" + ret +","); 105 | //角色信息 106 | buf.append("roles:{"); 107 | for (int i = 0; i < roles.size(); i++) { 108 | buf.append(roles.get(i).toString() +","); 109 | } 110 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 111 | buf.append("},"); 112 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 113 | buf.append("]"); 114 | return buf.toString(); 115 | } 116 | } -------------------------------------------------------------------------------- /robot-mmo/src/com/game/login/message/RoleBrief.java: -------------------------------------------------------------------------------- 1 | package com.game.login.message; 2 | 3 | 4 | import com.game.message.struct.Bean; 5 | 6 | import io.netty.buffer.ByteBuf; 7 | import org.apache.log4j.Logger; 8 | 9 | /** 10 | * @author messageGenerator 11 | * 12 | * @version 1.0.0 13 | * 14 | * 角色简要信息 15 | */ 16 | public class RoleBrief extends Bean { 17 | 18 | private static Logger log = Logger.getLogger(RoleBrief.class); 19 | 20 | //角色id 21 | private long roleId; 22 | 23 | //角色名称 24 | private String name; 25 | 26 | 27 | /** 28 | * 写入字节缓存 29 | */ 30 | public boolean write(ByteBuf buf){ 31 | try { 32 | //角色id 33 | writeLong(buf, this.roleId); 34 | //角色名称 35 | writeString(buf, this.name); 36 | } catch (Exception e) { 37 | log.error(e, e); 38 | return false; 39 | } 40 | return true; 41 | } 42 | 43 | /** 44 | * 读取字节缓存 45 | */ 46 | public boolean read(ByteBuf buf){ 47 | try { 48 | //角色id 49 | this.roleId = readLong(buf); 50 | //角色名称 51 | this.name = readString(buf); 52 | } catch (Exception e) { 53 | log.error(e, e); 54 | return false; 55 | } 56 | return true; 57 | } 58 | 59 | /** 60 | * get 角色id 61 | * @return 62 | */ 63 | public long getRoleId(){ 64 | return roleId; 65 | } 66 | 67 | /** 68 | * set 角色id 69 | */ 70 | public void setRoleId(long roleId){ 71 | this.roleId = roleId; 72 | } 73 | 74 | /** 75 | * get 角色名称 76 | * @return 77 | */ 78 | public String getName(){ 79 | return name; 80 | } 81 | 82 | /** 83 | * set 角色名称 84 | */ 85 | public void setName(String name){ 86 | this.name = name; 87 | } 88 | 89 | @Override 90 | public String toString(){ 91 | StringBuffer buf = new StringBuffer("["); 92 | //角色id 93 | buf.append("roleId:" + roleId +","); 94 | //角色名称 95 | if(this.name!=null) buf.append("name:" + name.toString() +","); 96 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 97 | buf.append("]"); 98 | return buf.toString(); 99 | } 100 | } -------------------------------------------------------------------------------- /robot-mmo/src/com/game/manager/Manager.java: -------------------------------------------------------------------------------- 1 | package com.game.manager; 2 | 3 | public abstract class Manager { 4 | protected Manager() { 5 | ManagerPool.regist(this); 6 | } 7 | 8 | public abstract boolean init(); 9 | public abstract void stop(); 10 | } 11 | -------------------------------------------------------------------------------- /robot-mmo/src/com/game/manager/ManagerPool.java: -------------------------------------------------------------------------------- 1 | package com.game.manager; 2 | 3 | import java.util.HashSet; 4 | 5 | import org.apache.log4j.Logger; 6 | 7 | import com.game.config.ConfigManager; 8 | import com.game.message.manager.MessageManager; 9 | 10 | public class ManagerPool { 11 | public static MessageManager message = new MessageManager(); 12 | public static ConfigManager config = new ConfigManager(); 13 | 14 | public static HashSet managers; 15 | private static Logger logger = Logger.getLogger(ManagerPool.class); 16 | public static boolean init() { 17 | if (managers == null) { 18 | return true; 19 | } 20 | for (Manager manager : managers) { 21 | if (!manager.init()) { 22 | logger.error(manager.getClass().getName() + "初始化失败"); 23 | return false; 24 | } 25 | logger.error(manager.getClass().getName() + "初始化完成"); 26 | } 27 | return true; 28 | } 29 | 30 | public static void regist(Manager manager) { 31 | if (managers == null) { 32 | managers = new HashSet<>(); 33 | } 34 | managers.add(manager); 35 | } 36 | 37 | public static void stop() { 38 | for (Manager manager : managers) { 39 | manager.stop(); 40 | } 41 | } 42 | 43 | public static void main(String[] args) { 44 | ManagerPool.init(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /robot-mmo/src/com/game/message/manager/MessageManager.java: -------------------------------------------------------------------------------- 1 | package com.game.message.manager; 2 | 3 | import com.game.manager.Manager; 4 | import com.game.message.MessagePool; 5 | 6 | public class MessageManager extends Manager { 7 | 8 | @Override 9 | public boolean init() { 10 | MessagePool.getInstance().register(100201, com.game.login.handler.ResLoginCreateRoleHandler.class, 11 | com.game.login.message.ResLoginCreateRoleMessage.class); 12 | MessagePool.getInstance().register(100200, com.game.login.handler.ResLoginHandler.class, com.game.login.message.ResLoginMessage.class); 13 | return true; 14 | } 15 | 16 | @Override 17 | public void stop() { 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /robot-mmo/src/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /robot-mmo/src/message.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /server-mmo/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /server-mmo/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | server-mmo 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /server-mmo/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /server-mmo/bin/.gitignore: -------------------------------------------------------------------------------- 1 | /com/ 2 | -------------------------------------------------------------------------------- /server-mmo/bin/Start.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/server-mmo/bin/Start.class -------------------------------------------------------------------------------- /server-mmo/bin/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /server-mmo/bin/message.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /server-mmo/build.xml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/junit/mmo-server/7f878816c33ee441e434f4a6ec46f42193f038fd/server-mmo/build.xml -------------------------------------------------------------------------------- /server-mmo/config/db-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /server-mmo/config/db-data.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /server-mmo/config/game.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /server-mmo/config/server.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /server-mmo/proto.bat: -------------------------------------------------------------------------------- 1 | set SRC="proto" 2 | set DST="src\main\java" 3 | 4 | protoc --java_out=%DST% --proto_path=%SRC% %SRC%/*.proto 5 | pause -------------------------------------------------------------------------------- /server-mmo/proto/role.proto: -------------------------------------------------------------------------------- 1 | package role; 2 | 3 | option java_package = "com.game.role.struct"; 4 | option java_outer_classname = "RoleSaveData"; 5 | 6 | message RoleMapData { 7 | required int32 server = 1; 8 | required int32 id = 2; 9 | required int 32 line = 3; 10 | } 11 | 12 | message Role { 13 | required int64 id = 1; 14 | required string name = 2; 15 | required int64 accountId = 3; 16 | required int64 createTime = 4; 17 | } 18 | -------------------------------------------------------------------------------- /server-mmo/src/Start.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | import com.manager.ManagerPool; 4 | import com.server.game.GameServer; 5 | 6 | public class Start { 7 | public static void main(String[] args) { 8 | ManagerPool.init(); 9 | GameServer.getInstance().init(ManagerPool.config.getServerConfig().getGameServerConfig().getPort()); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /server-mmo/src/com/config/manager/ConfigManager.java: -------------------------------------------------------------------------------- 1 | package com.config.manager; 2 | 3 | import com.config.struct.GameConfig; 4 | import com.config.struct.ServerConfig; 5 | import com.manager.Manager; 6 | import com.manager.PriorityEnum; 7 | 8 | public class ConfigManager extends Manager { 9 | private GameConfig gameCofnig = new GameConfig(); 10 | private ServerConfig serverConfig = new ServerConfig(); 11 | 12 | public GameConfig getGameCofnig() { 13 | return gameCofnig; 14 | } 15 | 16 | public void setGameCofnig(GameConfig gameCofnig) { 17 | this.gameCofnig = gameCofnig; 18 | } 19 | 20 | public ServerConfig getServerConfig() { 21 | return serverConfig; 22 | } 23 | 24 | public void setServerConfig(ServerConfig serverConfig) { 25 | this.serverConfig = serverConfig; 26 | } 27 | 28 | @Override 29 | public boolean init() { 30 | if (!gameCofnig.init()) { 31 | return false; 32 | } 33 | 34 | if (!serverConfig.init()) { 35 | return false; 36 | } 37 | 38 | return true; 39 | } 40 | 41 | @Override 42 | public void stop() { 43 | } 44 | 45 | @Override 46 | public PriorityEnum getPriority() { 47 | return PriorityEnum.BASE; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /server-mmo/src/com/config/struct/GameConfig.java: -------------------------------------------------------------------------------- 1 | package com.config.struct; 2 | 3 | import java.io.File; 4 | import java.util.Collection; 5 | import java.util.HashMap; 6 | import java.util.Iterator; 7 | 8 | import org.apache.log4j.Logger; 9 | import org.dom4j.Document; 10 | import org.dom4j.DocumentException; 11 | import org.dom4j.Element; 12 | import org.dom4j.io.SAXReader; 13 | 14 | import com.game.util.TimeUtil; 15 | 16 | class Server { 17 | private int id; 18 | private long opendate; 19 | public int getId() { 20 | return id; 21 | } 22 | public void setId(int id) { 23 | this.id = id; 24 | } 25 | public long getOpendate() { 26 | return opendate; 27 | } 28 | public void setOpendate(long opendate) { 29 | this.opendate = opendate; 30 | } 31 | 32 | } 33 | 34 | public class GameConfig { 35 | private static Logger logger = Logger.getLogger(GameConfig.class); 36 | private HashMap platfom2protocol; 37 | private HashMap servers ; 38 | 39 | /** 40 | * 根据平台获取验证key 41 | * @param platform 42 | * @return 43 | */ 44 | public String getProtocol(String platform) { 45 | return platfom2protocol.get(platform); 46 | } 47 | 48 | /** 49 | * 根据服务器id获取服务器的开服时间 50 | * @param server 51 | * @return 52 | */ 53 | public long getOpenDate(int server) { 54 | return servers.get(server).getOpendate(); 55 | } 56 | 57 | public boolean init() { 58 | try { 59 | SAXReader reader = new SAXReader(); 60 | Document doc; 61 | doc = reader.read(new File("config/game.xml")); 62 | Element root = doc.getRootElement(); 63 | for (Iterator i = root.elementIterator(); i.hasNext(); ) { 64 | Element element = (Element)i.next(); 65 | if (element.getName().equals("servers")) { 66 | initServers(element); 67 | } else if (element.getName().equals("protocols")) { 68 | initProtocols(element); 69 | } 70 | } 71 | } catch (DocumentException e) { 72 | logger.error(e, e); 73 | return false; 74 | } 75 | return true; 76 | } 77 | 78 | private void initProtocols(Element root) { 79 | HashMap map = new HashMap<>(); 80 | 81 | for (Iterator i = root.elementIterator(); i.hasNext(); ) { 82 | Element element = (Element)i.next(); 83 | if (!element.getName().equals("protocol")) { 84 | continue; 85 | } 86 | map.put(element.attributeValue("platfom"), element.attributeValue("key")); 87 | } 88 | 89 | platfom2protocol = map; 90 | } 91 | 92 | private void initServers(Element root) { 93 | HashMap map = new HashMap<>(); 94 | 95 | for (Iterator i = root.elementIterator(); i.hasNext(); ) { 96 | Element element = (Element)i.next(); 97 | if (!element.getName().equals("server")) { 98 | continue; 99 | } 100 | Server server = new Server(); 101 | server.setId(Integer.parseInt(element.attributeValue("id"))); 102 | server.setOpendate(TimeUtil.getDateByString(element.attributeValue("opendate")).getTime()); 103 | 104 | if (server.getId() >= 0xffff) { 105 | logger.error("服务器id配置错误,不能大于0xffff"); 106 | } 107 | 108 | map.put(server.getId(), server); 109 | } 110 | 111 | servers = map; 112 | } 113 | 114 | public Collection getServers() { 115 | return servers.keySet(); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /server-mmo/src/com/config/struct/ServerConfig.java: -------------------------------------------------------------------------------- 1 | package com.config.struct; 2 | 3 | import java.io.File; 4 | import java.util.Iterator; 5 | 6 | import org.apache.log4j.Logger; 7 | import org.dom4j.Document; 8 | import org.dom4j.DocumentException; 9 | import org.dom4j.Element; 10 | import org.dom4j.io.SAXReader; 11 | 12 | public class ServerConfig { 13 | public class GameServerConfig { 14 | private int port; 15 | public int getPort() { 16 | return port; 17 | } 18 | public void setPort(int port) { 19 | this.port = port; 20 | } 21 | } 22 | 23 | private static Logger logger = Logger.getLogger(ServerConfig.class); 24 | private GameServerConfig gameServerConfig; 25 | 26 | public boolean init() { 27 | try { 28 | SAXReader reader = new SAXReader(); 29 | Document doc; 30 | doc = reader.read(new File("config/server.xml")); 31 | Element root = doc.getRootElement(); 32 | for (Iterator i = root.elementIterator(); i.hasNext(); ) { 33 | Element element = (Element)i.next(); 34 | if (element.getName().equals("gameserver")) { 35 | initGameServerConfig(element); 36 | } 37 | } 38 | } catch (DocumentException e) { 39 | logger.error(e, e); 40 | return false; 41 | } 42 | return true; 43 | } 44 | 45 | private void initGameServerConfig(Element element) { 46 | GameServerConfig config = new GameServerConfig(); 47 | config.setPort(Integer.parseInt(element.attributeValue("port"))); 48 | gameServerConfig = config; 49 | } 50 | 51 | public GameServerConfig getGameServerConfig() { 52 | return gameServerConfig; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /server-mmo/src/com/db/DbFactory.java: -------------------------------------------------------------------------------- 1 | package com.db; 2 | 3 | import java.io.FileInputStream; 4 | import java.io.InputStream; 5 | 6 | import org.apache.ibatis.session.SqlSessionFactory; 7 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 8 | import org.apache.log4j.Logger; 9 | 10 | public class DbFactory { 11 | private static Logger logger = Logger.getLogger(DbFactory.class); 12 | private static DbFactory instance = new DbFactory(); 13 | private DbFactory() { 14 | try { 15 | InputStream in = new FileInputStream("config/db-data.xml"); 16 | dataFactory = new SqlSessionFactoryBuilder().build(in); 17 | in.close(); 18 | } catch (Exception e) { 19 | logger.error(e, e); 20 | } 21 | 22 | try { 23 | InputStream in = new FileInputStream("config/db-config.xml"); 24 | configFactory = new SqlSessionFactoryBuilder().build(in); 25 | in.close(); 26 | } catch (Exception e) { 27 | logger.error(e, e); 28 | } 29 | } 30 | public static DbFactory getInstance() { 31 | return instance; 32 | } 33 | 34 | private SqlSessionFactory dataFactory; 35 | private SqlSessionFactory configFactory; 36 | public SqlSessionFactory getDataFactory() { 37 | return dataFactory; 38 | } 39 | public SqlSessionFactory getConfigFactory() { 40 | return configFactory; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /server-mmo/src/com/db/config/DbConfigManager.java: -------------------------------------------------------------------------------- 1 | package com.db.config; 2 | 3 | import com.db.config.container.MapContainer; 4 | import com.manager.Manager; 5 | import com.manager.PriorityEnum; 6 | 7 | public class DbConfigManager extends Manager { 8 | public MapContainer map = new MapContainer(); 9 | 10 | @Override 11 | public boolean init() { 12 | if (!map.init()) return false; 13 | return true; 14 | } 15 | 16 | @Override 17 | public void stop() { 18 | } 19 | 20 | @Override 21 | public PriorityEnum getPriority() { 22 | return PriorityEnum.BASE; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /server-mmo/src/com/db/config/bean/MapBean.java: -------------------------------------------------------------------------------- 1 | package com.db.config.bean; 2 | 3 | public class MapBean { 4 | private int maxLine; 5 | private int defaultLine; 6 | private String name; 7 | private int width; 8 | private int id; 9 | private int height; 10 | 11 | public int getMaxLine(){ 12 | return maxLine; 13 | } 14 | 15 | public void setMaxLine(int maxLine){ 16 | this.maxLine = maxLine; 17 | } 18 | 19 | public int getDefaultLine(){ 20 | return defaultLine; 21 | } 22 | 23 | public void setDefaultLine(int defaultLine){ 24 | this.defaultLine = defaultLine; 25 | } 26 | 27 | public String getName(){ 28 | return name; 29 | } 30 | 31 | public void setName(String name){ 32 | this.name = name; 33 | } 34 | 35 | public int getWidth(){ 36 | return width; 37 | } 38 | 39 | public void setWidth(int width){ 40 | this.width = width; 41 | } 42 | 43 | public int getId(){ 44 | return id; 45 | } 46 | 47 | public void setId(int id){ 48 | this.id = id; 49 | } 50 | 51 | public int getHeight(){ 52 | return height; 53 | } 54 | 55 | public void setHeight(int height){ 56 | this.height = height; 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /server-mmo/src/com/db/config/container/MapContainer.java: -------------------------------------------------------------------------------- 1 | package com.db.config.container; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import com.db.config.dao.MapDao; 9 | import com.db.config.bean.MapBean; 10 | 11 | public class MapContainer { 12 | private List list = new ArrayList<>(); 13 | private Map map = new HashMap<>(); 14 | private MapDao dao = new MapDao(); 15 | public List getList() { 16 | return list; 17 | } 18 | public Map getMap() { 19 | return map; 20 | } 21 | public boolean init() { 22 | list = dao.select(); 23 | for (MapBean bean : list) { 24 | map.put(bean.getId(), bean); 25 | } 26 | return true; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /server-mmo/src/com/db/config/dao/MapDao.java: -------------------------------------------------------------------------------- 1 | package com.db.config.dao; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.ResultSet; 6 | import java.sql.Statement; 7 | import java.util.List; 8 | 9 | import org.apache.ibatis.session.SqlSession; 10 | import org.apache.ibatis.session.SqlSessionFactory; 11 | 12 | import com.db.DbFactory; 13 | import com.db.config.bean.MapBean; 14 | 15 | public class MapDao { 16 | SqlSessionFactory factory = DbFactory.getInstance().getConfigFactory(); 17 | 18 | public List select() { 19 | SqlSession session = factory.openSession(); 20 | try { 21 | long s = System.currentTimeMillis(); 22 | List list = session.selectList("map.select"); 23 | long interval = System.currentTimeMillis() - s; 24 | if (interval > 10) { 25 | com.logger.GlobalLogger.db.error(new StringBuilder().append("MapDao.").append("select:").append(interval)); 26 | } 27 | return list; 28 | } finally { 29 | session.close(); 30 | } 31 | } 32 | 33 | public void insert(MapBean bean) { 34 | long s = System.currentTimeMillis(); 35 | SqlSession session = factory.openSession(); 36 | try { 37 | session.insert("map.insert", bean); 38 | long interval = System.currentTimeMillis() - s; 39 | if (interval > 10) { 40 | com.logger.GlobalLogger.db.error(new StringBuilder().append("MapDao.").append("select:").append(interval)); 41 | } 42 | } finally { 43 | session.close(); 44 | } 45 | } 46 | 47 | public static void main(String[] args) throws Exception { 48 | Connection con = DriverManager.getConnection("jdbc:mysql://192.168.1.120:3307/shell_config", "game", "game"); 49 | long s = System.currentTimeMillis(); 50 | Statement stmt = con.createStatement(); 51 | ResultSet rs = stmt.executeQuery("select maxLine,defaultLine,name,width,id,height from map"); 52 | System.out.println(System.currentTimeMillis() - s); 53 | // while (rs.next()) { 54 | // int x = rs.getInt("a"); 55 | // String s = rs.getString("b"); 56 | // float f = rs.getFloat("c"); 57 | // } 58 | } 59 | } -------------------------------------------------------------------------------- /server-mmo/src/com/db/config/mapper/Map.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 18 | 19 | 20 | insert into role(maxLine,defaultLine,name,width,id,height) 21 | values (#{maxLine,jdbcType=INTEGER},#{defaultLine,jdbcType=INTEGER},#{name,jdbcType=VARCHAR},#{width,jdbcType=INTEGER},#{id,jdbcType=INTEGER},#{height,jdbcType=INTEGER}) 22 | 23 | -------------------------------------------------------------------------------- /server-mmo/src/com/db/data/bean/AccountBean.java: -------------------------------------------------------------------------------- 1 | package com.db.data.bean; 2 | 3 | public class AccountBean { 4 | private int server; 5 | private long createTime; 6 | private String name; 7 | private long id; 8 | private String platform; 9 | 10 | public int getServer(){ 11 | return server; 12 | } 13 | 14 | public void setServer(int server){ 15 | this.server = server; 16 | } 17 | 18 | public long getCreateTime(){ 19 | return createTime; 20 | } 21 | 22 | public void setCreateTime(long createTime){ 23 | this.createTime = createTime; 24 | } 25 | 26 | public String getName(){ 27 | return name; 28 | } 29 | 30 | public void setName(String name){ 31 | this.name = name; 32 | } 33 | 34 | public long getId(){ 35 | return id; 36 | } 37 | 38 | public void setId(long id){ 39 | this.id = id; 40 | } 41 | 42 | public String getPlatform(){ 43 | return platform; 44 | } 45 | 46 | public void setPlatform(String platform){ 47 | this.platform = platform; 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /server-mmo/src/com/db/data/bean/RoleBean.java: -------------------------------------------------------------------------------- 1 | package com.db.data.bean; 2 | 3 | public class RoleBean { 4 | private byte[] data; 5 | private long createTime; 6 | private String name; 7 | private long id; 8 | private long account; 9 | 10 | public byte[] getData(){ 11 | return data; 12 | } 13 | 14 | public void setData(byte[] data){ 15 | this.data = data; 16 | } 17 | 18 | public long getCreateTime(){ 19 | return createTime; 20 | } 21 | 22 | public void setCreateTime(long createTime){ 23 | this.createTime = createTime; 24 | } 25 | 26 | public String getName(){ 27 | return name; 28 | } 29 | 30 | public void setName(String name){ 31 | this.name = name; 32 | } 33 | 34 | public long getId(){ 35 | return id; 36 | } 37 | 38 | public void setId(long id){ 39 | this.id = id; 40 | } 41 | 42 | public long getAccount(){ 43 | return account; 44 | } 45 | 46 | public void setAccount(long account){ 47 | this.account = account; 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /server-mmo/src/com/db/data/dao/AccountDao.java: -------------------------------------------------------------------------------- 1 | package com.db.data.dao; 2 | 3 | import java.util.HashMap; 4 | import java.util.List; 5 | 6 | import org.apache.ibatis.session.SqlSession; 7 | import org.apache.ibatis.session.SqlSessionFactory; 8 | 9 | import com.db.DbFactory; 10 | import com.db.data.bean.AccountBean; 11 | 12 | public class AccountDao { 13 | private AccountDao() {} 14 | private static AccountDao instance = new AccountDao(); 15 | public static AccountDao getInstance() { 16 | return instance; 17 | } 18 | 19 | SqlSessionFactory factory = DbFactory.getInstance().getDataFactory(); 20 | 21 | public List select() { 22 | long s = System.currentTimeMillis(); 23 | SqlSession session = factory.openSession(); 24 | try{ 25 | List list = session.selectList("account.select"); 26 | long interval = System.currentTimeMillis() - s; 27 | if (interval > 10) { 28 | com.logger.GlobalLogger.db.error(new StringBuilder().append("AccountDao.").append("select:").append(interval)); 29 | } 30 | return list; 31 | }finally{ 32 | session.close(); 33 | } 34 | } 35 | 36 | public void insert(AccountBean bean) { 37 | long s = System.currentTimeMillis(); 38 | SqlSession session = factory.openSession(); 39 | try{ 40 | session.insert("account.insert", bean); 41 | session.commit(); 42 | long interval = System.currentTimeMillis() - s; 43 | if (interval > 10) { 44 | com.logger.GlobalLogger.db.error(new StringBuilder().append("AccountDao.").append("insert:").append(interval)); 45 | } 46 | }finally{ 47 | session.close(); 48 | } 49 | } 50 | 51 | public AccountBean select(String platform, int server, String name) { 52 | long s = System.currentTimeMillis(); 53 | SqlSession session = factory.openSession(); 54 | try{ 55 | HashMap map = new HashMap<>(); 56 | map.put("platform", platform); 57 | map.put("server", server); 58 | map.put("name", name); 59 | AccountBean bean = session.selectOne("account.selectOne", map); 60 | long interval = System.currentTimeMillis() - s; 61 | if (interval > 10) { 62 | com.logger.GlobalLogger.db.error(new StringBuilder().append("AccountDao.").append("selectOne:").append(interval)); 63 | } 64 | return bean; 65 | }finally{ 66 | session.close(); 67 | } 68 | } 69 | 70 | public void update(AccountBean bean) { 71 | long s = System.currentTimeMillis(); 72 | SqlSession session = factory.openSession(); 73 | try{ 74 | session.update("account.update", bean); 75 | session.commit(); 76 | long interval = System.currentTimeMillis() - s; 77 | if (interval > 10) { 78 | com.logger.GlobalLogger.db.error(new StringBuilder().append("AccountDao.").append("update:").append(interval)); 79 | } 80 | }finally{ 81 | session.close(); 82 | } 83 | } 84 | } -------------------------------------------------------------------------------- /server-mmo/src/com/db/data/dao/RoleDao.java: -------------------------------------------------------------------------------- 1 | package com.db.data.dao; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.session.SqlSession; 6 | import org.apache.ibatis.session.SqlSessionFactory; 7 | 8 | import com.db.DbFactory; 9 | import com.db.data.bean.RoleBean; 10 | 11 | public class RoleDao { 12 | private RoleDao() {} 13 | private static RoleDao instance = new RoleDao(); 14 | public static RoleDao getInstance() { 15 | return instance; 16 | } 17 | SqlSessionFactory factory = DbFactory.getInstance().getDataFactory(); 18 | 19 | public List select() { 20 | long s = System.currentTimeMillis(); 21 | SqlSession session = factory.openSession(); 22 | try { 23 | List list = session.selectList("role.select"); 24 | long interval = System.currentTimeMillis() - s; 25 | if (interval > 10) { 26 | com.logger.GlobalLogger.db.error(new StringBuilder().append("RoleDao.").append("select:").append(interval)); 27 | } 28 | return list; 29 | } finally { 30 | session.close(); 31 | } 32 | } 33 | 34 | public void insert(RoleBean bean) { 35 | long s = System.currentTimeMillis(); 36 | SqlSession session = factory.openSession(); 37 | try { 38 | session.insert("role.insert", bean); 39 | session.commit(); 40 | long interval = System.currentTimeMillis() - s; 41 | if (interval > 10) { 42 | com.logger.GlobalLogger.db.error(new StringBuilder().append("RoleDao.").append("insert:").append(interval)); 43 | } 44 | } catch (Exception e) { 45 | com.logger.GlobalLogger.db.error(e, e); 46 | } finally { 47 | session.close(); 48 | } 49 | } 50 | 51 | public List selectByAccountId(long accountId) { 52 | long s = System.currentTimeMillis(); 53 | SqlSession session = factory.openSession(); 54 | try { 55 | List list = session.selectList("role.selectByAccountId", accountId); 56 | long interval = System.currentTimeMillis() - s; 57 | if (interval > 10) { 58 | com.logger.GlobalLogger.db.error(new StringBuilder().append("RoleDao.").append("selectByAccountId:").append(interval)); 59 | } 60 | return list; 61 | } finally { 62 | session.close(); 63 | } 64 | } 65 | 66 | public List selectNames() { 67 | long s = System.currentTimeMillis(); 68 | SqlSession session = factory.openSession(); 69 | try { 70 | List list = session.selectList("role.selectNames"); 71 | long interval = System.currentTimeMillis() - s; 72 | if (interval > 10) { 73 | com.logger.GlobalLogger.db.error(new StringBuilder().append("RoleDao.").append("selectNames:").append(interval)); 74 | } 75 | return list; 76 | } finally { 77 | session.close(); 78 | } 79 | } 80 | 81 | public void update(RoleBean bean) { 82 | long s = System.currentTimeMillis(); 83 | SqlSession session = factory.openSession(); 84 | try { 85 | session.update("role.update", bean); 86 | session.commit(); 87 | long interval = System.currentTimeMillis() - s; 88 | if (interval > 10) { 89 | com.logger.GlobalLogger.db.error(new StringBuilder().append("RoleDao.").append("update:").append(interval)); 90 | } 91 | } finally { 92 | session.close(); 93 | } 94 | } 95 | } -------------------------------------------------------------------------------- /server-mmo/src/com/db/data/mapper/Account.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 17 | 18 | 19 | insert into account(server,createTime,name,id,platform) 20 | values (#{server,jdbcType=INTEGER},#{createTime,jdbcType=BIGINT},#{name,jdbcType=VARCHAR},#{id,jdbcType=BIGINT},#{platform,jdbcType=VARCHAR}) 21 | 22 | 23 | 29 | 30 | 31 | update account 32 | set server=#{server,jdbcType=INTEGER}, 33 | createTime=#{createTime,jdbcType=BIGINT}, 34 | name=#{name,jdbcType=VARCHAR}, 35 | platform=#{platform,jdbcType=VARCHAR} 36 | where id=#{id,jdbcType=BIGINT} 37 | 38 | -------------------------------------------------------------------------------- /server-mmo/src/com/db/data/mapper/Role.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 17 | 18 | 19 | insert into role(data,createTime,name,id,account) 20 | values (#{data,jdbcType=BLOB},#{createTime,jdbcType=BIGINT},#{name,jdbcType=VARCHAR},#{id,jdbcType=BIGINT},#{account,jdbcType=BIGINT}) 21 | 22 | 23 | 26 | 27 | 30 | 31 | 32 | update role 33 | set data=#{data,jdbcType=BLOB}, 34 | createTime=#{createTime,jdbcType=BIGINT}, 35 | name=#{name,jdbcType=VARCHAR}, 36 | account=#{account,jdbcType=BIGINT} 37 | where id=#{id,jdbcType=BIGINT} 38 | 39 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/account/cache/AccountCache.java: -------------------------------------------------------------------------------- 1 | package com.game.account.cache; 2 | 3 | import java.util.concurrent.ConcurrentHashMap; 4 | 5 | import org.apache.log4j.Logger; 6 | 7 | import com.db.data.bean.AccountBean; 8 | import com.db.data.dao.AccountDao; 9 | import com.game.account.struct.Account; 10 | import com.game.role.struct.Role; 11 | import com.manager.ManagerPool; 12 | 13 | 14 | public class AccountCache { 15 | public final int MAX=10000; 16 | private static Logger logger = Logger.getLogger(AccountCache.class); 17 | private ConcurrentHashMap accounts = new ConcurrentHashMap<>(); 18 | 19 | /** 20 | * 从内存中获取玩家数据 21 | * @param platform 平台 22 | * @param server 服务器 23 | * @param accountName 账号 24 | * @return 25 | */ 26 | public Account get(String platform, int server, String accountName) { 27 | return accounts.get(new Key(platform, server, accountName)); 28 | } 29 | 30 | /** 31 | * 从数据库中获取玩家数据 32 | * @param platform 平台 33 | * @param server 服务器 34 | * @param accountName 账号 35 | * @return 36 | */ 37 | public Account getFromDb(String platform, int server, String accountName) { 38 | AccountBean bean = AccountDao.getInstance().select(platform, server, accountName); 39 | if (bean == null) { 40 | return null; 41 | } 42 | Account account = ManagerPool.account.createAccount(bean); 43 | return account; 44 | } 45 | 46 | public void add(Account account) { 47 | accounts.put(getKey(account), account); 48 | if (accounts.size() > MAX) { 49 | clean(); 50 | } 51 | } 52 | 53 | private Key getKey(Account account) { 54 | return new Key(account.getPlatform(), account.getServer(), account.getName()); 55 | } 56 | 57 | private void clean() { 58 | logger.error("清理开始:" + accounts.size()); 59 | for (Object obj : accounts.values().toArray()) { 60 | Account account = (Account)obj; 61 | if (System.currentTimeMillis() - account.getOffLineTime() < 5 * 60 * 1000) { 62 | continue; 63 | } 64 | 65 | accounts.remove(getKey(account)); 66 | ManagerPool.account.update(account); 67 | } 68 | logger.error("清理结束:" + accounts.size()); 69 | } 70 | 71 | public void saveAll() { 72 | for (Object object : accounts.values().toArray()) { 73 | Account account = (Account)object; 74 | ManagerPool.account.update(account); 75 | 76 | for (Object object2 : account.getRoles().values().toArray()) { 77 | Role role = (Role)object2; 78 | ManagerPool.role.update(role); 79 | } 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/account/cache/Key.java: -------------------------------------------------------------------------------- 1 | package com.game.account.cache; 2 | 3 | public class Key { 4 | private String platform; // 平台 5 | private int server; // 服务器 6 | private String accountName; // 账号 7 | public Key(String platform, int server, String accountName) { 8 | this.platform = platform; 9 | this.server = server; 10 | this.accountName = accountName; 11 | } 12 | 13 | public boolean equals(Object obj) { 14 | if (!(obj instanceof Key)) { 15 | return false; 16 | } 17 | Key key = (Key)obj; 18 | if (this.platform.equals(key.platform) && this.server == key.server && this.accountName.equals(key.accountName)) { 19 | return true; 20 | } 21 | return false; 22 | } 23 | 24 | public int hashCode() { 25 | StringBuilder builder = new StringBuilder(); 26 | return builder.append(platform).append(String.valueOf(server)).append(accountName).toString().hashCode(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/account/struct/Account.java: -------------------------------------------------------------------------------- 1 | package com.game.account.struct; 2 | 3 | import java.util.HashMap; 4 | 5 | import com.game.role.struct.Role; 6 | 7 | 8 | public class Account { 9 | private long id; 10 | private String name; 11 | private int server; 12 | private String platform; 13 | private long createTime; 14 | private HashMap roles = new HashMap<>(); 15 | private Role role; // 当前游戏的角色 16 | private boolean online = true; // 在线标志 17 | private long offLineTime; 18 | private long loginTime; 19 | 20 | public boolean isOnline() { 21 | return online; 22 | } 23 | public void setOnline(boolean online) { 24 | this.online = online; 25 | } 26 | public HashMap getRoles() { 27 | return roles; 28 | } 29 | public void setRoles(HashMap roles) { 30 | this.roles = roles; 31 | } 32 | public Role getRole() { 33 | return role; 34 | } 35 | public void setRole(Role role) { 36 | this.role = role; 37 | } 38 | public long getId() { 39 | return id; 40 | } 41 | public void setId(long id) { 42 | this.id = id; 43 | } 44 | public String getName() { 45 | return name; 46 | } 47 | public void setName(String name) { 48 | this.name = name; 49 | } 50 | public int getServer() { 51 | return server; 52 | } 53 | public void setServer(int server) { 54 | this.server = server; 55 | } 56 | public String getPlatform() { 57 | return platform; 58 | } 59 | public void setPlatform(String platform) { 60 | this.platform = platform; 61 | } 62 | public long getCreateTime() { 63 | return createTime; 64 | } 65 | public void setCreateTime(long createTime) { 66 | this.createTime = createTime; 67 | } 68 | public long getOffLineTime() { 69 | return offLineTime; 70 | } 71 | public void setOffLineTime(long offLineTime) { 72 | this.offLineTime = offLineTime; 73 | } 74 | public long getLoginTime() { 75 | return loginTime; 76 | } 77 | public void setLoginTime(long loginTime) { 78 | this.loginTime = loginTime; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/login/handler/ReqLoginCreateRoleHandler.java: -------------------------------------------------------------------------------- 1 | package com.game.login.handler; 2 | 3 | import com.game.account.struct.Account; 4 | import com.game.login.message.ResLoginCreateRoleMessage; 5 | import com.game.message.struct.Handler; 6 | import com.game.role.struct.Role; 7 | import com.manager.ManagerPool; 8 | import com.message.util.MessageUtil; 9 | 10 | public class ReqLoginCreateRoleHandler extends Handler{ 11 | @Override 12 | public void exec() { 13 | com.game.login.message.ReqLoginCreateRoleMessage msg = (com.game.login.message.ReqLoginCreateRoleMessage)this.getMessage(); 14 | Account account = ManagerPool.account.getAccount(this.getContext()); 15 | Role role = ManagerPool.role.createRole(account, msg.getName()); 16 | 17 | ResLoginCreateRoleMessage ret = new ResLoginCreateRoleMessage(); 18 | ret.setRet((byte) 1); 19 | if (role != null) { 20 | ret.setRet((byte) 0); 21 | ret.getRoles().add(role.getBriefInfo()); 22 | } 23 | MessageUtil.send(account, ret); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/login/handler/ReqLoginHandler.java: -------------------------------------------------------------------------------- 1 | package com.game.login.handler; 2 | 3 | import com.game.login.message.ReqLoginMessage; 4 | import com.game.message.struct.Handler; 5 | import com.manager.ManagerPool; 6 | 7 | public class ReqLoginHandler extends Handler { 8 | 9 | @Override 10 | public void exec() { 11 | ManagerPool.login.login(this.getContext(), (ReqLoginMessage) this.getMessage()); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/login/handler/ReqLoginSelectRoleHandler.java: -------------------------------------------------------------------------------- 1 | package com.game.login.handler; 2 | 3 | import org.apache.log4j.Logger; 4 | 5 | import com.game.account.struct.Account; 6 | import com.game.message.struct.Handler; 7 | import com.manager.ManagerPool; 8 | 9 | public class ReqLoginSelectRoleHandler extends Handler{ 10 | @SuppressWarnings("unused") 11 | private static Logger logger = Logger.getLogger(ReqLoginSelectRoleHandler.class); 12 | @Override 13 | public void exec() { 14 | com.game.login.message.ReqLoginSelectRoleMessage msg = (com.game.login.message.ReqLoginSelectRoleMessage)this.getMessage(); 15 | Account account = ManagerPool.account.getAccount(this.getContext()); 16 | ManagerPool.login.login(account); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/login/manager/LoginManager.java: -------------------------------------------------------------------------------- 1 | package com.game.login.manager; 2 | 3 | import io.netty.channel.ChannelHandlerContext; 4 | 5 | import java.util.List; 6 | 7 | import org.apache.log4j.Logger; 8 | 9 | import com.game.account.struct.Account; 10 | import com.game.login.message.ReqLoginMessage; 11 | import com.game.login.message.ResLoginMessage; 12 | import com.game.login.struct.LoginState; 13 | import com.game.role.struct.Role; 14 | import com.manager.Manager; 15 | import com.manager.ManagerPool; 16 | import com.manager.PriorityEnum; 17 | import com.message.util.MessageUtil; 18 | 19 | public class LoginManager extends Manager { 20 | @SuppressWarnings("unused") 21 | private Logger logger = Logger.getLogger(this.getClass()); 22 | 23 | @Override 24 | public boolean init() { 25 | return true; 26 | } 27 | 28 | @Override 29 | public PriorityEnum getPriority() { 30 | return PriorityEnum.NORMAL; 31 | } 32 | 33 | @Override 34 | public void stop() { 35 | } 36 | 37 | public void login(ChannelHandlerContext context, ReqLoginMessage message) { 38 | if (!ManagerPool.account.checkProtocol(message.getPlatform(), message.getProtocol())) { 39 | ResLoginMessage ret = new ResLoginMessage(); 40 | ret.setRet((byte) 1); 41 | MessageUtil.send(context, ret); 42 | context.close(); 43 | return; 44 | } 45 | 46 | Account account = ManagerPool.account.getAccount(message.getPlatform(), message.getServer(), message.getAccountName()); 47 | if (account == null) { 48 | account = ManagerPool.account.createAccount(message.getPlatform(), message.getServer(), message.getAccountName()); 49 | } 50 | 51 | account.setLoginTime(System.currentTimeMillis()); 52 | List roles = ManagerPool.role.getRoleByAccountId(account.getId()); 53 | 54 | ResLoginMessage ret = new ResLoginMessage(); 55 | if (roles != null) { 56 | for (Role role : roles) { 57 | account.getRoles().put(role.getId(), role); 58 | ret.getRoles().add(role.getBriefInfo()); 59 | } 60 | } 61 | 62 | ManagerPool.account.bind(context, account); 63 | 64 | ret.setRet((byte) 0); 65 | MessageUtil.send(context, ret); 66 | 67 | ManagerPool.loginState.change(context, LoginState.LOGIN_END); 68 | } 69 | 70 | public void login(Account account) { 71 | ManagerPool.map.login(account); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/login/manager/LoginStateManager.java: -------------------------------------------------------------------------------- 1 | package com.game.login.manager; 2 | 3 | import io.netty.channel.ChannelHandlerContext; 4 | import io.netty.util.AttributeKey; 5 | 6 | import com.game.login.struct.LoginState; 7 | import com.manager.Manager; 8 | import com.manager.PriorityEnum; 9 | 10 | public class LoginStateManager extends Manager { 11 | private static final String LOGIN_STATE = "LOGIN_STATE"; 12 | 13 | @Override 14 | public boolean init() { 15 | return true; 16 | } 17 | 18 | @Override 19 | public PriorityEnum getPriority() { 20 | return PriorityEnum.NORMAL; 21 | } 22 | 23 | @Override 24 | public void stop() { 25 | } 26 | 27 | public boolean change(ChannelHandlerContext context, LoginState state) { 28 | synchronized (context) { 29 | LoginState nowState = (LoginState)context.attr(AttributeKey.valueOf(LOGIN_STATE)).get(); 30 | switch (state) { 31 | case LOGIN_ING: 32 | if (nowState == null) { 33 | context.attr(AttributeKey.valueOf(LOGIN_STATE)).set(state); 34 | return true; 35 | } 36 | case LOGIN_END: 37 | if (nowState == LoginState.LOGIN_ING) { 38 | context.attr(AttributeKey.valueOf(LOGIN_STATE)).set(state); 39 | return true; 40 | } 41 | case CREATE_ROLE_ING: 42 | if (nowState == LoginState.LOGIN_END) { 43 | context.attr(AttributeKey.valueOf(LOGIN_STATE)).set(state); 44 | return true; 45 | } 46 | case CREATE_ROLE_END: 47 | if (nowState == LoginState.CREATE_ROLE_ING) { 48 | context.attr(AttributeKey.valueOf(LOGIN_STATE)).set(state); 49 | return true; 50 | } 51 | case SELECT_ROLE_ING: 52 | if (nowState == LoginState.LOGIN_END || nowState == LoginState.CREATE_ROLE_END) { 53 | context.attr(AttributeKey.valueOf(LOGIN_STATE)).set(state); 54 | return true; 55 | } 56 | case SELECT_ROLE_END: 57 | if (nowState == LoginState.SELECT_ROLE_ING) { 58 | context.attr(AttributeKey.valueOf(LOGIN_STATE)).set(state); 59 | return true; 60 | } 61 | } 62 | return false; 63 | } 64 | } 65 | 66 | public boolean check(ChannelHandlerContext context, LoginState state) { 67 | synchronized (context) { 68 | return state == (LoginState)context.attr(AttributeKey.valueOf(LOGIN_STATE)).get(); 69 | } 70 | } 71 | 72 | public static void main(String[] args) { 73 | Integer tmp = (Integer)null; 74 | System.out.println(tmp); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/login/message/ReqLoginCreateRoleMessage.java: -------------------------------------------------------------------------------- 1 | package com.game.login.message; 2 | 3 | import com.game.message.struct.Message; 4 | import io.netty.buffer.ByteBuf; 5 | import org.apache.log4j.Logger; 6 | 7 | /** 8 | * @author messageGenerator 9 | * 10 | * @version 1.0.0 11 | * 12 | * 创建角色消息 13 | */ 14 | public class ReqLoginCreateRoleMessage extends Message{ 15 | 16 | private static Logger log = Logger.getLogger(ReqLoginCreateRoleMessage.class); 17 | 18 | //角色名称 19 | private String name; 20 | 21 | 22 | /** 23 | * 写入字节缓存 24 | */ 25 | public boolean write(ByteBuf buf){ 26 | try { 27 | //角色名称 28 | writeString(buf, this.name); 29 | } catch (Exception e) { 30 | log.error(e, e); 31 | return false; 32 | } 33 | return true; 34 | } 35 | 36 | /** 37 | * 读取字节缓存 38 | */ 39 | public boolean read(ByteBuf buf){ 40 | try { 41 | //角色名称 42 | this.name = readString(buf); 43 | } catch (Exception e) { 44 | log.error(e, e); 45 | return false; 46 | } 47 | return true; 48 | } 49 | 50 | /** 51 | * get 角色名称 52 | * @return 53 | */ 54 | public String getName(){ 55 | return name; 56 | } 57 | 58 | /** 59 | * set 角色名称 60 | */ 61 | public void setName(String name){ 62 | this.name = name; 63 | } 64 | 65 | 66 | @Override 67 | public int getId() { 68 | return 100102; 69 | } 70 | 71 | @Override 72 | public String toString(){ 73 | StringBuffer buf = new StringBuffer("["); 74 | //角色名称 75 | if(this.name!=null) buf.append("name:" + name.toString() +","); 76 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 77 | buf.append("]"); 78 | return buf.toString(); 79 | } 80 | } -------------------------------------------------------------------------------- /server-mmo/src/com/game/login/message/ReqLoginMessage.java: -------------------------------------------------------------------------------- 1 | package com.game.login.message; 2 | 3 | import com.game.message.struct.Message; 4 | import io.netty.buffer.ByteBuf; 5 | import org.apache.log4j.Logger; 6 | 7 | /** 8 | * @author messageGenerator 9 | * 10 | * @version 1.0.0 11 | * 12 | * 登录消息 13 | */ 14 | public class ReqLoginMessage extends Message{ 15 | 16 | private static Logger log = Logger.getLogger(ReqLoginMessage.class); 17 | 18 | //账号 19 | private String accountName; 20 | 21 | //协议 22 | private String protocol; 23 | 24 | //平台 25 | private String platform; 26 | 27 | //服务器 28 | private int server; 29 | 30 | 31 | /** 32 | * 写入字节缓存 33 | */ 34 | public boolean write(ByteBuf buf){ 35 | try { 36 | //账号 37 | writeString(buf, this.accountName); 38 | //协议 39 | writeString(buf, this.protocol); 40 | //平台 41 | writeString(buf, this.platform); 42 | //服务器 43 | writeInt(buf, this.server); 44 | } catch (Exception e) { 45 | log.error(e, e); 46 | return false; 47 | } 48 | return true; 49 | } 50 | 51 | /** 52 | * 读取字节缓存 53 | */ 54 | public boolean read(ByteBuf buf){ 55 | try { 56 | //账号 57 | this.accountName = readString(buf); 58 | //协议 59 | this.protocol = readString(buf); 60 | //平台 61 | this.platform = readString(buf); 62 | //服务器 63 | this.server = readInt(buf); 64 | } catch (Exception e) { 65 | log.error(e, e); 66 | return false; 67 | } 68 | return true; 69 | } 70 | 71 | /** 72 | * get 账号 73 | * @return 74 | */ 75 | public String getAccountName(){ 76 | return accountName; 77 | } 78 | 79 | /** 80 | * set 账号 81 | */ 82 | public void setAccountName(String accountName){ 83 | this.accountName = accountName; 84 | } 85 | 86 | /** 87 | * get 协议 88 | * @return 89 | */ 90 | public String getProtocol(){ 91 | return protocol; 92 | } 93 | 94 | /** 95 | * set 协议 96 | */ 97 | public void setProtocol(String protocol){ 98 | this.protocol = protocol; 99 | } 100 | 101 | /** 102 | * get 平台 103 | * @return 104 | */ 105 | public String getPlatform(){ 106 | return platform; 107 | } 108 | 109 | /** 110 | * set 平台 111 | */ 112 | public void setPlatform(String platform){ 113 | this.platform = platform; 114 | } 115 | 116 | /** 117 | * get 服务器 118 | * @return 119 | */ 120 | public int getServer(){ 121 | return server; 122 | } 123 | 124 | /** 125 | * set 服务器 126 | */ 127 | public void setServer(int server){ 128 | this.server = server; 129 | } 130 | 131 | 132 | @Override 133 | public int getId() { 134 | return 100100; 135 | } 136 | 137 | @Override 138 | public String toString(){ 139 | StringBuffer buf = new StringBuffer("["); 140 | //账号 141 | if(this.accountName!=null) buf.append("accountName:" + accountName.toString() +","); 142 | //协议 143 | if(this.protocol!=null) buf.append("protocol:" + protocol.toString() +","); 144 | //平台 145 | if(this.platform!=null) buf.append("platform:" + platform.toString() +","); 146 | //服务器 147 | buf.append("server:" + server +","); 148 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 149 | buf.append("]"); 150 | return buf.toString(); 151 | } 152 | } -------------------------------------------------------------------------------- /server-mmo/src/com/game/login/message/ReqLoginSelectRoleMessage.java: -------------------------------------------------------------------------------- 1 | package com.game.login.message; 2 | 3 | import com.game.message.struct.Message; 4 | import io.netty.buffer.ByteBuf; 5 | import org.apache.log4j.Logger; 6 | 7 | /** 8 | * @author messageGenerator 9 | * 10 | * @version 1.0.0 11 | * 12 | * 选择角色进入游戏消息 13 | */ 14 | public class ReqLoginSelectRoleMessage extends Message{ 15 | 16 | private static Logger log = Logger.getLogger(ReqLoginSelectRoleMessage.class); 17 | 18 | //角色id 19 | private long roleId; 20 | 21 | 22 | /** 23 | * 写入字节缓存 24 | */ 25 | public boolean write(ByteBuf buf){ 26 | try { 27 | //角色id 28 | writeLong(buf, this.roleId); 29 | } catch (Exception e) { 30 | log.error(e, e); 31 | return false; 32 | } 33 | return true; 34 | } 35 | 36 | /** 37 | * 读取字节缓存 38 | */ 39 | public boolean read(ByteBuf buf){ 40 | try { 41 | //角色id 42 | this.roleId = readLong(buf); 43 | } catch (Exception e) { 44 | log.error(e, e); 45 | return false; 46 | } 47 | return true; 48 | } 49 | 50 | /** 51 | * get 角色id 52 | * @return 53 | */ 54 | public long getRoleId(){ 55 | return roleId; 56 | } 57 | 58 | /** 59 | * set 角色id 60 | */ 61 | public void setRoleId(long roleId){ 62 | this.roleId = roleId; 63 | } 64 | 65 | 66 | @Override 67 | public int getId() { 68 | return 100101; 69 | } 70 | 71 | @Override 72 | public String toString(){ 73 | StringBuffer buf = new StringBuffer("["); 74 | //角色id 75 | buf.append("roleId:" + roleId +","); 76 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 77 | buf.append("]"); 78 | return buf.toString(); 79 | } 80 | } -------------------------------------------------------------------------------- /server-mmo/src/com/game/login/message/ResLoginCreateRoleMessage.java: -------------------------------------------------------------------------------- 1 | package com.game.login.message; 2 | 3 | import java.util.List; 4 | import java.util.ArrayList; 5 | import com.game.message.struct.Message; 6 | import io.netty.buffer.ByteBuf; 7 | import org.apache.log4j.Logger; 8 | 9 | /** 10 | * @author messageGenerator 11 | * 12 | * @version 1.0.0 13 | * 14 | * 创建角色消息 15 | */ 16 | public class ResLoginCreateRoleMessage extends Message{ 17 | 18 | private static Logger log = Logger.getLogger(ResLoginCreateRoleMessage.class); 19 | 20 | //返回值 21 | private byte ret; 22 | 23 | //角色信息 24 | private List roles = new ArrayList(); 25 | 26 | /** 27 | * 写入字节缓存 28 | */ 29 | public boolean write(ByteBuf buf){ 30 | try { 31 | //返回值 32 | writeByte(buf, this.ret); 33 | //角色信息 34 | writeShort(buf, (short)roles.size()); 35 | for (int i = 0; i < roles.size(); i++) { 36 | writeBean(buf, roles.get(i)); 37 | } 38 | } catch (Exception e) { 39 | log.error(e, e); 40 | return false; 41 | } 42 | return true; 43 | } 44 | 45 | /** 46 | * 读取字节缓存 47 | */ 48 | public boolean read(ByteBuf buf){ 49 | try { 50 | //返回值 51 | this.ret = readByte(buf); 52 | //角色信息 53 | int roles_length = readShort(buf); 54 | for (int i = 0; i < roles_length; i++) { 55 | roles.add((RoleBrief)readBean(buf, RoleBrief.class)); 56 | } 57 | } catch (Exception e) { 58 | log.error(e, e); 59 | return false; 60 | } 61 | return true; 62 | } 63 | 64 | /** 65 | * get 返回值 66 | * @return 67 | */ 68 | public byte getRet(){ 69 | return ret; 70 | } 71 | 72 | /** 73 | * set 返回值 74 | */ 75 | public void setRet(byte ret){ 76 | this.ret = ret; 77 | } 78 | 79 | /** 80 | * get 角色信息 81 | * @return 82 | */ 83 | public List getRoles(){ 84 | return roles; 85 | } 86 | 87 | /** 88 | * set 角色信息 89 | */ 90 | public void setRoles(List roles){ 91 | this.roles = roles; 92 | } 93 | 94 | 95 | @Override 96 | public int getId() { 97 | return 100201; 98 | } 99 | 100 | @Override 101 | public String toString(){ 102 | StringBuffer buf = new StringBuffer("["); 103 | //返回值 104 | buf.append("ret:" + ret +","); 105 | //角色信息 106 | buf.append("roles:{"); 107 | for (int i = 0; i < roles.size(); i++) { 108 | buf.append(roles.get(i).toString() +","); 109 | } 110 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 111 | buf.append("},"); 112 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 113 | buf.append("]"); 114 | return buf.toString(); 115 | } 116 | } -------------------------------------------------------------------------------- /server-mmo/src/com/game/login/message/ResLoginMessage.java: -------------------------------------------------------------------------------- 1 | package com.game.login.message; 2 | 3 | import java.util.List; 4 | import java.util.ArrayList; 5 | import com.game.message.struct.Message; 6 | import io.netty.buffer.ByteBuf; 7 | import org.apache.log4j.Logger; 8 | 9 | /** 10 | * @author messageGenerator 11 | * 12 | * @version 1.0.0 13 | * 14 | * 登录消息 15 | */ 16 | public class ResLoginMessage extends Message{ 17 | 18 | private static Logger log = Logger.getLogger(ResLoginMessage.class); 19 | 20 | //返回值 21 | private byte ret; 22 | 23 | //角色信息 24 | private List roles = new ArrayList(); 25 | 26 | /** 27 | * 写入字节缓存 28 | */ 29 | public boolean write(ByteBuf buf){ 30 | try { 31 | //返回值 32 | writeByte(buf, this.ret); 33 | //角色信息 34 | writeShort(buf, (short)roles.size()); 35 | for (int i = 0; i < roles.size(); i++) { 36 | writeBean(buf, roles.get(i)); 37 | } 38 | } catch (Exception e) { 39 | log.error(e, e); 40 | return false; 41 | } 42 | return true; 43 | } 44 | 45 | /** 46 | * 读取字节缓存 47 | */ 48 | public boolean read(ByteBuf buf){ 49 | try { 50 | //返回值 51 | this.ret = readByte(buf); 52 | //角色信息 53 | int roles_length = readShort(buf); 54 | for (int i = 0; i < roles_length; i++) { 55 | roles.add((RoleBrief)readBean(buf, RoleBrief.class)); 56 | } 57 | } catch (Exception e) { 58 | log.error(e, e); 59 | return false; 60 | } 61 | return true; 62 | } 63 | 64 | /** 65 | * get 返回值 66 | * @return 67 | */ 68 | public byte getRet(){ 69 | return ret; 70 | } 71 | 72 | /** 73 | * set 返回值 74 | */ 75 | public void setRet(byte ret){ 76 | this.ret = ret; 77 | } 78 | 79 | /** 80 | * get 角色信息 81 | * @return 82 | */ 83 | public List getRoles(){ 84 | return roles; 85 | } 86 | 87 | /** 88 | * set 角色信息 89 | */ 90 | public void setRoles(List roles){ 91 | this.roles = roles; 92 | } 93 | 94 | 95 | @Override 96 | public int getId() { 97 | return 100200; 98 | } 99 | 100 | @Override 101 | public String toString(){ 102 | StringBuffer buf = new StringBuffer("["); 103 | //返回值 104 | buf.append("ret:" + ret +","); 105 | //角色信息 106 | buf.append("roles:{"); 107 | for (int i = 0; i < roles.size(); i++) { 108 | buf.append(roles.get(i).toString() +","); 109 | } 110 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 111 | buf.append("},"); 112 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 113 | buf.append("]"); 114 | return buf.toString(); 115 | } 116 | } -------------------------------------------------------------------------------- /server-mmo/src/com/game/login/message/RoleBrief.java: -------------------------------------------------------------------------------- 1 | package com.game.login.message; 2 | 3 | 4 | import com.game.message.struct.Bean; 5 | 6 | import io.netty.buffer.ByteBuf; 7 | import org.apache.log4j.Logger; 8 | 9 | /** 10 | * @author messageGenerator 11 | * 12 | * @version 1.0.0 13 | * 14 | * 角色简要信息 15 | */ 16 | public class RoleBrief extends Bean { 17 | 18 | private static Logger log = Logger.getLogger(RoleBrief.class); 19 | 20 | //角色id 21 | private long roleId; 22 | 23 | //角色名称 24 | private String name; 25 | 26 | 27 | /** 28 | * 写入字节缓存 29 | */ 30 | public boolean write(ByteBuf buf){ 31 | try { 32 | //角色id 33 | writeLong(buf, this.roleId); 34 | //角色名称 35 | writeString(buf, this.name); 36 | } catch (Exception e) { 37 | log.error(e, e); 38 | return false; 39 | } 40 | return true; 41 | } 42 | 43 | /** 44 | * 读取字节缓存 45 | */ 46 | public boolean read(ByteBuf buf){ 47 | try { 48 | //角色id 49 | this.roleId = readLong(buf); 50 | //角色名称 51 | this.name = readString(buf); 52 | } catch (Exception e) { 53 | log.error(e, e); 54 | return false; 55 | } 56 | return true; 57 | } 58 | 59 | /** 60 | * get 角色id 61 | * @return 62 | */ 63 | public long getRoleId(){ 64 | return roleId; 65 | } 66 | 67 | /** 68 | * set 角色id 69 | */ 70 | public void setRoleId(long roleId){ 71 | this.roleId = roleId; 72 | } 73 | 74 | /** 75 | * get 角色名称 76 | * @return 77 | */ 78 | public String getName(){ 79 | return name; 80 | } 81 | 82 | /** 83 | * set 角色名称 84 | */ 85 | public void setName(String name){ 86 | this.name = name; 87 | } 88 | 89 | @Override 90 | public String toString(){ 91 | StringBuffer buf = new StringBuffer("["); 92 | //角色id 93 | buf.append("roleId:" + roleId +","); 94 | //角色名称 95 | if(this.name!=null) buf.append("name:" + name.toString() +","); 96 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 97 | buf.append("]"); 98 | return buf.toString(); 99 | } 100 | } -------------------------------------------------------------------------------- /server-mmo/src/com/game/login/struct/LoginState.java: -------------------------------------------------------------------------------- 1 | package com.game.login.struct; 2 | 3 | public enum LoginState { 4 | LOGIN_ING, 5 | LOGIN_END, 6 | CREATE_ROLE_ING, 7 | CREATE_ROLE_END, 8 | SELECT_ROLE_ING, 9 | SELECT_ROLE_END 10 | } 11 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/map/handler/ReqMapMoveHandler.java: -------------------------------------------------------------------------------- 1 | package com.game.map.handler; 2 | 3 | import com.game.message.struct.Handler; 4 | import com.game.account.struct.Account; 5 | import org.apache.log4j.Logger; 6 | import com.manager.ManagerPool; 7 | 8 | public class ReqMapMoveHandler extends Handler{ 9 | @SuppressWarnings("unused") 10 | private static Logger logger = Logger.getLogger(ReqMapMoveHandler.class); 11 | @Override 12 | public void exec() { 13 | com.game.map.message.ReqMapMoveMessage msg = (com.game.map.message.ReqMapMoveMessage)this.getMessage(); 14 | Account account = ManagerPool.account.getAccount(this.getContext()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/map/message/MapInfo.java: -------------------------------------------------------------------------------- 1 | package com.game.map.message; 2 | 3 | 4 | import com.game.message.struct.Bean; 5 | 6 | import io.netty.buffer.ByteBuf; 7 | import org.apache.log4j.Logger; 8 | 9 | /** 10 | * @author messageGenerator 11 | * 12 | * @version 1.0.0 13 | * 14 | * 地图对象属性 15 | */ 16 | public class MapInfo extends Bean { 17 | 18 | private static Logger log = Logger.getLogger(MapInfo.class); 19 | 20 | //地图id 21 | private int model; 22 | 23 | //地图线 24 | private int line; 25 | 26 | //坐标 27 | private Position position; 28 | 29 | 30 | /** 31 | * 写入字节缓存 32 | */ 33 | public boolean write(ByteBuf buf){ 34 | try { 35 | //地图id 36 | writeInt(buf, this.model); 37 | //地图线 38 | writeInt(buf, this.line); 39 | //坐标 40 | writeBean(buf, this.position); 41 | } catch (Exception e) { 42 | log.error(e, e); 43 | return false; 44 | } 45 | return true; 46 | } 47 | 48 | /** 49 | * 读取字节缓存 50 | */ 51 | public boolean read(ByteBuf buf){ 52 | try { 53 | //地图id 54 | this.model = readInt(buf); 55 | //地图线 56 | this.line = readInt(buf); 57 | //坐标 58 | this.position = (Position)readBean(buf, Position.class); 59 | } catch (Exception e) { 60 | log.error(e, e); 61 | return false; 62 | } 63 | return true; 64 | } 65 | 66 | /** 67 | * get 地图id 68 | * @return 69 | */ 70 | public int getModel(){ 71 | return model; 72 | } 73 | 74 | /** 75 | * set 地图id 76 | */ 77 | public void setModel(int model){ 78 | this.model = model; 79 | } 80 | 81 | /** 82 | * get 地图线 83 | * @return 84 | */ 85 | public int getLine(){ 86 | return line; 87 | } 88 | 89 | /** 90 | * set 地图线 91 | */ 92 | public void setLine(int line){ 93 | this.line = line; 94 | } 95 | 96 | /** 97 | * get 坐标 98 | * @return 99 | */ 100 | public Position getPosition(){ 101 | return position; 102 | } 103 | 104 | /** 105 | * set 坐标 106 | */ 107 | public void setPosition(Position position){ 108 | this.position = position; 109 | } 110 | 111 | @Override 112 | public String toString(){ 113 | StringBuffer buf = new StringBuffer("["); 114 | //地图id 115 | buf.append("model:" + model +","); 116 | //地图线 117 | buf.append("line:" + line +","); 118 | //坐标 119 | if(this.position!=null) buf.append("position:" + position.toString() +","); 120 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 121 | buf.append("]"); 122 | return buf.toString(); 123 | } 124 | } -------------------------------------------------------------------------------- /server-mmo/src/com/game/map/message/Position.java: -------------------------------------------------------------------------------- 1 | package com.game.map.message; 2 | 3 | 4 | import com.game.message.struct.Bean; 5 | 6 | import io.netty.buffer.ByteBuf; 7 | import org.apache.log4j.Logger; 8 | 9 | /** 10 | * @author messageGenerator 11 | * 12 | * @version 1.0.0 13 | * 14 | * 坐标 15 | */ 16 | public class Position extends Bean { 17 | 18 | private static Logger log = Logger.getLogger(Position.class); 19 | 20 | //坐标x 21 | private int x; 22 | 23 | //坐标y 24 | private int y; 25 | 26 | 27 | /** 28 | * 写入字节缓存 29 | */ 30 | public boolean write(ByteBuf buf){ 31 | try { 32 | //坐标x 33 | writeInt(buf, this.x); 34 | //坐标y 35 | writeInt(buf, this.y); 36 | } catch (Exception e) { 37 | log.error(e, e); 38 | return false; 39 | } 40 | return true; 41 | } 42 | 43 | /** 44 | * 读取字节缓存 45 | */ 46 | public boolean read(ByteBuf buf){ 47 | try { 48 | //坐标x 49 | this.x = readInt(buf); 50 | //坐标y 51 | this.y = readInt(buf); 52 | } catch (Exception e) { 53 | log.error(e, e); 54 | return false; 55 | } 56 | return true; 57 | } 58 | 59 | /** 60 | * get 坐标x 61 | * @return 62 | */ 63 | public int getX(){ 64 | return x; 65 | } 66 | 67 | /** 68 | * set 坐标x 69 | */ 70 | public void setX(int x){ 71 | this.x = x; 72 | } 73 | 74 | /** 75 | * get 坐标y 76 | * @return 77 | */ 78 | public int getY(){ 79 | return y; 80 | } 81 | 82 | /** 83 | * set 坐标y 84 | */ 85 | public void setY(int y){ 86 | this.y = y; 87 | } 88 | 89 | @Override 90 | public String toString(){ 91 | StringBuffer buf = new StringBuffer("["); 92 | //坐标x 93 | buf.append("x:" + x +","); 94 | //坐标y 95 | buf.append("y:" + y +","); 96 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 97 | buf.append("]"); 98 | return buf.toString(); 99 | } 100 | } -------------------------------------------------------------------------------- /server-mmo/src/com/game/map/message/ReqMapMoveMessage.java: -------------------------------------------------------------------------------- 1 | package com.game.map.message; 2 | 3 | import java.util.List; 4 | import java.util.ArrayList; 5 | import com.game.message.struct.Message; 6 | import io.netty.buffer.ByteBuf; 7 | import org.apache.log4j.Logger; 8 | 9 | /** 10 | * @author messageGenerator 11 | * 12 | * @version 1.0.0 13 | * 14 | * 移动消息 15 | */ 16 | public class ReqMapMoveMessage extends Message{ 17 | 18 | private static Logger log = Logger.getLogger(ReqMapMoveMessage.class); 19 | 20 | //当前坐标 21 | private Position position; 22 | //路径 23 | private List path = new ArrayList<>(); 24 | 25 | /** 26 | * 写入字节缓存 27 | */ 28 | public boolean write(ByteBuf buf){ 29 | try { 30 | //当前坐标 31 | writeBean(buf, this.position); 32 | //路径 33 | writeShort(buf, (short)path.size()); 34 | for (int i = 0; i < path.size(); i++) { 35 | writeByte(buf, path.get(i)); 36 | } 37 | } catch (Exception e) { 38 | log.error(e, e); 39 | return false; 40 | } 41 | return true; 42 | } 43 | 44 | /** 45 | * 读取字节缓存 46 | */ 47 | public boolean read(ByteBuf buf){ 48 | try { 49 | //当前坐标 50 | this.position = (Position)readBean(buf, Position.class); 51 | //路径 52 | int path_length = readShort(buf); 53 | for (int i = 0; i < path_length; i++) { 54 | path.add(readByte(buf)); 55 | } 56 | } catch (Exception e) { 57 | log.error(e, e); 58 | return false; 59 | } 60 | return true; 61 | } 62 | 63 | /** 64 | * get 当前坐标 65 | * @return 66 | */ 67 | public Position getPosition(){ 68 | return position; 69 | } 70 | 71 | /** 72 | * set 当前坐标 73 | */ 74 | public void setPosition(Position position){ 75 | this.position = position; 76 | } 77 | 78 | /** 79 | * get 路径 80 | * @return 81 | */ 82 | public List getPath(){ 83 | return path; 84 | } 85 | 86 | /** 87 | * set 路径 88 | */ 89 | public void setPath(List path){ 90 | this.path = path; 91 | } 92 | 93 | 94 | @Override 95 | public int getId() { 96 | return 103100; 97 | } 98 | 99 | @Override 100 | public String toString(){ 101 | StringBuffer buf = new StringBuffer("["); 102 | //当前坐标 103 | if(this.position!=null) buf.append("position:" + position.toString() +","); 104 | //路径 105 | buf.append("path:{"); 106 | for (int i = 0; i < path.size(); i++) { 107 | buf.append(path.get(i) +","); 108 | } 109 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 110 | buf.append("},"); 111 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 112 | buf.append("]"); 113 | return buf.toString(); 114 | } 115 | } -------------------------------------------------------------------------------- /server-mmo/src/com/game/map/message/ResMapChangeMessage.java: -------------------------------------------------------------------------------- 1 | package com.game.map.message; 2 | 3 | import com.game.message.struct.Message; 4 | import io.netty.buffer.ByteBuf; 5 | import org.apache.log4j.Logger; 6 | 7 | /** 8 | * @author messageGenerator 9 | * 10 | * @version 1.0.0 11 | * 12 | * 切换地图消息 13 | */ 14 | public class ResMapChangeMessage extends Message{ 15 | 16 | private static Logger log = Logger.getLogger(ResMapChangeMessage.class); 17 | 18 | //地图信息 19 | private MapInfo role; 20 | 21 | /** 22 | * 写入字节缓存 23 | */ 24 | public boolean write(ByteBuf buf){ 25 | try { 26 | //地图信息 27 | writeBean(buf, this.role); 28 | } catch (Exception e) { 29 | log.error(e, e); 30 | return false; 31 | } 32 | return true; 33 | } 34 | 35 | /** 36 | * 读取字节缓存 37 | */ 38 | public boolean read(ByteBuf buf){ 39 | try { 40 | //地图信息 41 | this.role = (MapInfo)readBean(buf, MapInfo.class); 42 | } catch (Exception e) { 43 | log.error(e, e); 44 | return false; 45 | } 46 | return true; 47 | } 48 | 49 | /** 50 | * get 地图信息 51 | * @return 52 | */ 53 | public MapInfo getRole(){ 54 | return role; 55 | } 56 | 57 | /** 58 | * set 地图信息 59 | */ 60 | public void setRole(MapInfo role){ 61 | this.role = role; 62 | } 63 | 64 | 65 | @Override 66 | public int getId() { 67 | return 103202; 68 | } 69 | 70 | @Override 71 | public String toString(){ 72 | StringBuffer buf = new StringBuffer("["); 73 | //地图信息 74 | if(this.role!=null) buf.append("role:" + role.toString() +","); 75 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 76 | buf.append("]"); 77 | return buf.toString(); 78 | } 79 | } -------------------------------------------------------------------------------- /server-mmo/src/com/game/map/message/ResMapPositionChangeMessage.java: -------------------------------------------------------------------------------- 1 | package com.game.map.message; 2 | 3 | import com.game.message.struct.Message; 4 | import io.netty.buffer.ByteBuf; 5 | import org.apache.log4j.Logger; 6 | 7 | /** 8 | * @author messageGenerator 9 | * 10 | * @version 1.0.0 11 | * 12 | * 坐标点改变消息 13 | */ 14 | public class ResMapPositionChangeMessage extends Message{ 15 | 16 | private static Logger log = Logger.getLogger(ResMapPositionChangeMessage.class); 17 | 18 | //角色id 19 | private long roleId; 20 | //坐标点 21 | private Position position; 22 | 23 | /** 24 | * 写入字节缓存 25 | */ 26 | public boolean write(ByteBuf buf){ 27 | try { 28 | //角色id 29 | writeLong(buf, this.roleId); 30 | //坐标点 31 | writeBean(buf, this.position); 32 | } catch (Exception e) { 33 | log.error(e, e); 34 | return false; 35 | } 36 | return true; 37 | } 38 | 39 | /** 40 | * 读取字节缓存 41 | */ 42 | public boolean read(ByteBuf buf){ 43 | try { 44 | //角色id 45 | this.roleId = readLong(buf); 46 | //坐标点 47 | this.position = (Position)readBean(buf, Position.class); 48 | } catch (Exception e) { 49 | log.error(e, e); 50 | return false; 51 | } 52 | return true; 53 | } 54 | 55 | /** 56 | * get 角色id 57 | * @return 58 | */ 59 | public long getRoleId(){ 60 | return roleId; 61 | } 62 | 63 | /** 64 | * set 角色id 65 | */ 66 | public void setRoleId(long roleId){ 67 | this.roleId = roleId; 68 | } 69 | 70 | /** 71 | * get 坐标点 72 | * @return 73 | */ 74 | public Position getPosition(){ 75 | return position; 76 | } 77 | 78 | /** 79 | * set 坐标点 80 | */ 81 | public void setPosition(Position position){ 82 | this.position = position; 83 | } 84 | 85 | 86 | @Override 87 | public int getId() { 88 | return 103203; 89 | } 90 | 91 | @Override 92 | public String toString(){ 93 | StringBuffer buf = new StringBuffer("["); 94 | //角色id 95 | buf.append("roleId:" + roleId +","); 96 | //坐标点 97 | if(this.position!=null) buf.append("position:" + position.toString() +","); 98 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 99 | buf.append("]"); 100 | return buf.toString(); 101 | } 102 | } -------------------------------------------------------------------------------- /server-mmo/src/com/game/map/message/ResMapRoleMessage.java: -------------------------------------------------------------------------------- 1 | package com.game.map.message; 2 | 3 | import com.game.message.struct.Message; 4 | import io.netty.buffer.ByteBuf; 5 | import org.apache.log4j.Logger; 6 | 7 | /** 8 | * @author messageGenerator 9 | * 10 | * @version 1.0.0 11 | * 12 | * 地图单个角色信息消息 13 | */ 14 | public class ResMapRoleMessage extends Message{ 15 | 16 | private static Logger log = Logger.getLogger(ResMapRoleMessage.class); 17 | 18 | //角色信息 19 | private RoleInfo role; 20 | 21 | /** 22 | * 写入字节缓存 23 | */ 24 | public boolean write(ByteBuf buf){ 25 | try { 26 | //角色信息 27 | writeBean(buf, this.role); 28 | } catch (Exception e) { 29 | log.error(e, e); 30 | return false; 31 | } 32 | return true; 33 | } 34 | 35 | /** 36 | * 读取字节缓存 37 | */ 38 | public boolean read(ByteBuf buf){ 39 | try { 40 | //角色信息 41 | this.role = (RoleInfo)readBean(buf, RoleInfo.class); 42 | } catch (Exception e) { 43 | log.error(e, e); 44 | return false; 45 | } 46 | return true; 47 | } 48 | 49 | /** 50 | * get 角色信息 51 | * @return 52 | */ 53 | public RoleInfo getRole(){ 54 | return role; 55 | } 56 | 57 | /** 58 | * set 角色信息 59 | */ 60 | public void setRole(RoleInfo role){ 61 | this.role = role; 62 | } 63 | 64 | 65 | @Override 66 | public int getId() { 67 | return 103201; 68 | } 69 | 70 | @Override 71 | public String toString(){ 72 | StringBuffer buf = new StringBuffer("["); 73 | //角色信息 74 | if(this.role!=null) buf.append("role:" + role.toString() +","); 75 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 76 | buf.append("]"); 77 | return buf.toString(); 78 | } 79 | } -------------------------------------------------------------------------------- /server-mmo/src/com/game/map/message/ResMapRoundAllMessage.java: -------------------------------------------------------------------------------- 1 | package com.game.map.message; 2 | 3 | import java.util.List; 4 | import java.util.ArrayList; 5 | import com.game.message.struct.Message; 6 | import io.netty.buffer.ByteBuf; 7 | import org.apache.log4j.Logger; 8 | 9 | /** 10 | * @author messageGenerator 11 | * 12 | * @version 1.0.0 13 | * 14 | * 附近所有信息消息 15 | */ 16 | public class ResMapRoundAllMessage extends Message{ 17 | 18 | private static Logger log = Logger.getLogger(ResMapRoundAllMessage.class); 19 | 20 | //角色信息 21 | private List roles = new ArrayList<>(); 22 | 23 | /** 24 | * 写入字节缓存 25 | */ 26 | public boolean write(ByteBuf buf){ 27 | try { 28 | //角色信息 29 | writeShort(buf, (short)roles.size()); 30 | for (int i = 0; i < roles.size(); i++) { 31 | writeBean(buf, roles.get(i)); 32 | } 33 | } catch (Exception e) { 34 | log.error(e, e); 35 | return false; 36 | } 37 | return true; 38 | } 39 | 40 | /** 41 | * 读取字节缓存 42 | */ 43 | public boolean read(ByteBuf buf){ 44 | try { 45 | //角色信息 46 | int roles_length = readShort(buf); 47 | for (int i = 0; i < roles_length; i++) { 48 | roles.add((RoleInfo)readBean(buf, RoleInfo.class)); 49 | } 50 | } catch (Exception e) { 51 | log.error(e, e); 52 | return false; 53 | } 54 | return true; 55 | } 56 | 57 | /** 58 | * get 角色信息 59 | * @return 60 | */ 61 | public List getRoles(){ 62 | return roles; 63 | } 64 | 65 | /** 66 | * set 角色信息 67 | */ 68 | public void setRoles(List roles){ 69 | this.roles = roles; 70 | } 71 | 72 | 73 | @Override 74 | public int getId() { 75 | return 103200; 76 | } 77 | 78 | @Override 79 | public String toString(){ 80 | StringBuffer buf = new StringBuffer("["); 81 | //角色信息 82 | buf.append("roles:{"); 83 | for (int i = 0; i < roles.size(); i++) { 84 | buf.append(roles.get(i).toString() +","); 85 | } 86 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 87 | buf.append("},"); 88 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 89 | buf.append("]"); 90 | return buf.toString(); 91 | } 92 | } -------------------------------------------------------------------------------- /server-mmo/src/com/game/map/message/RoleInfo.java: -------------------------------------------------------------------------------- 1 | package com.game.map.message; 2 | 3 | 4 | import com.game.message.struct.Bean; 5 | 6 | import io.netty.buffer.ByteBuf; 7 | import org.apache.log4j.Logger; 8 | 9 | /** 10 | * @author messageGenerator 11 | * 12 | * @version 1.0.0 13 | * 14 | * 角色地图展示信息 15 | */ 16 | public class RoleInfo extends Bean { 17 | 18 | private static Logger log = Logger.getLogger(RoleInfo.class); 19 | 20 | //角色id 21 | private long roleId; 22 | 23 | //角色名称 24 | private String name; 25 | 26 | //坐标 27 | private Position position; 28 | 29 | 30 | /** 31 | * 写入字节缓存 32 | */ 33 | public boolean write(ByteBuf buf){ 34 | try { 35 | //角色id 36 | writeLong(buf, this.roleId); 37 | //角色名称 38 | writeString(buf, this.name); 39 | //坐标 40 | writeBean(buf, this.position); 41 | } catch (Exception e) { 42 | log.error(e, e); 43 | return false; 44 | } 45 | return true; 46 | } 47 | 48 | /** 49 | * 读取字节缓存 50 | */ 51 | public boolean read(ByteBuf buf){ 52 | try { 53 | //角色id 54 | this.roleId = readLong(buf); 55 | //角色名称 56 | this.name = readString(buf); 57 | //坐标 58 | this.position = (Position)readBean(buf, Position.class); 59 | } catch (Exception e) { 60 | log.error(e, e); 61 | return false; 62 | } 63 | return true; 64 | } 65 | 66 | /** 67 | * get 角色id 68 | * @return 69 | */ 70 | public long getRoleId(){ 71 | return roleId; 72 | } 73 | 74 | /** 75 | * set 角色id 76 | */ 77 | public void setRoleId(long roleId){ 78 | this.roleId = roleId; 79 | } 80 | 81 | /** 82 | * get 角色名称 83 | * @return 84 | */ 85 | public String getName(){ 86 | return name; 87 | } 88 | 89 | /** 90 | * set 角色名称 91 | */ 92 | public void setName(String name){ 93 | this.name = name; 94 | } 95 | 96 | /** 97 | * get 坐标 98 | * @return 99 | */ 100 | public Position getPosition(){ 101 | return position; 102 | } 103 | 104 | /** 105 | * set 坐标 106 | */ 107 | public void setPosition(Position position){ 108 | this.position = position; 109 | } 110 | 111 | @Override 112 | public String toString(){ 113 | StringBuffer buf = new StringBuffer("["); 114 | //角色id 115 | buf.append("roleId:" + roleId +","); 116 | //角色名称 117 | if(this.name!=null) buf.append("name:" + name.toString() +","); 118 | //坐标 119 | if(this.position!=null) buf.append("position:" + position.toString() +","); 120 | if(buf.charAt(buf.length()-1)==',') buf.deleteCharAt(buf.length()-1); 121 | buf.append("]"); 122 | return buf.toString(); 123 | } 124 | } -------------------------------------------------------------------------------- /server-mmo/src/com/game/map/struct/Area.java: -------------------------------------------------------------------------------- 1 | package com.game.map.struct; 2 | 3 | import java.util.HashMap; 4 | 5 | import com.game.role.struct.Role; 6 | 7 | public class Area { 8 | private HashMap roles = new HashMap<>(); 9 | 10 | public HashMap getRoles() { 11 | return roles; 12 | } 13 | 14 | public void setRoles(HashMap roles) { 15 | this.roles = roles; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/map/struct/Map.java: -------------------------------------------------------------------------------- 1 | package com.game.map.struct; 2 | 3 | public class Map { 4 | private int server; 5 | private int model; 6 | private int line; 7 | private long id; 8 | 9 | private Area[][] areas; 10 | 11 | public int getServer() { 12 | return server; 13 | } 14 | public void setServer(int server) { 15 | this.server = server; 16 | } 17 | public int getModel() { 18 | return model; 19 | } 20 | public void setModel(int model) { 21 | this.model = model; 22 | } 23 | public int getLine() { 24 | return line; 25 | } 26 | public void setLine(int line) { 27 | this.line = line; 28 | } 29 | public long getId() { 30 | return id; 31 | } 32 | public void setId(long id) { 33 | this.id = id; 34 | } 35 | public Area[][] getAreas() { 36 | return areas; 37 | } 38 | public void setAreas(Area[][] areas) { 39 | this.areas = areas; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/map/struct/MapTask.java: -------------------------------------------------------------------------------- 1 | package com.game.map.struct; 2 | 3 | import com.game.message.struct.Handler; 4 | import com.game.thread.queue.ITask; 5 | 6 | public class MapTask implements ITask { 7 | private Handler handler; 8 | 9 | public MapTask(Handler handler) { 10 | this.handler = handler; 11 | } 12 | 13 | @Override 14 | public void exec() { 15 | handler.exec(); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/map/struct/Position.java: -------------------------------------------------------------------------------- 1 | package com.game.map.struct; 2 | 3 | public class Position { 4 | private int x; 5 | private int y; 6 | public int getX() { 7 | return x; 8 | } 9 | public void setX(int x) { 10 | this.x = x; 11 | } 12 | public int getY() { 13 | return y; 14 | } 15 | public void setY(int y) { 16 | this.y = y; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/map/struct/RoleMapData.java: -------------------------------------------------------------------------------- 1 | package com.game.map.struct; 2 | 3 | public class RoleMapData { 4 | private int server; 5 | private int model; 6 | private int line; 7 | private long id; 8 | private Position position = new Position(); 9 | public int getServer() { 10 | return server; 11 | } 12 | public void setServer(int server) { 13 | this.server = server; 14 | } 15 | public int getModel() { 16 | return model; 17 | } 18 | public void setModel(int model) { 19 | this.model = model; 20 | } 21 | public int getLine() { 22 | return line; 23 | } 24 | public void setLine(int line) { 25 | this.line = line; 26 | } 27 | public long getId() { 28 | return id; 29 | } 30 | public void setId(long id) { 31 | this.id = id; 32 | } 33 | public Position getPosition() { 34 | return position; 35 | } 36 | public void setPosition(Position position) { 37 | this.position = position; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/map/thread/MapThread.java: -------------------------------------------------------------------------------- 1 | package com.game.map.thread; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.concurrent.LinkedBlockingQueue; 6 | 7 | import org.apache.log4j.Logger; 8 | 9 | import com.game.map.timer.ITimer; 10 | import com.game.message.struct.Handler; 11 | import com.game.thread.queue.FixTaskThread; 12 | 13 | public class MapThread extends Thread { 14 | private Logger logger; 15 | private LinkedBlockingQueue tasks = new LinkedBlockingQueue<>(); 16 | private List timers = new ArrayList<>(); 17 | 18 | public MapThread(String name) { 19 | super(name); 20 | logger = Logger.getLogger(name); 21 | } 22 | 23 | @Override 24 | public void run() { 25 | while (true) { 26 | Handler handler = tasks.poll(); 27 | if (handler == null) { 28 | try { 29 | synchronized (this) { 30 | wait(); 31 | } 32 | } catch (Exception e) { 33 | logger.error(e, e); 34 | } 35 | } else { 36 | long s = System.currentTimeMillis(); 37 | handler.exec(); 38 | long interval = System.currentTimeMillis() - s; 39 | if (s > 10) { 40 | logger.error(handler.getClass().getName() + ":" + interval); 41 | } 42 | } 43 | 44 | for (ITimer timer : timers) { 45 | if (!timer.canExec()) { 46 | continue; 47 | } 48 | timer.exec(); 49 | } 50 | } 51 | } 52 | 53 | public void addTask(Handler handler) { 54 | try { 55 | tasks.add(handler); 56 | synchronized (this) { 57 | notify(); 58 | } 59 | } catch (Exception e) { 60 | logger.error(e, e); 61 | } 62 | } 63 | 64 | public void addTimer(ITimer timer) { 65 | timers.add(timer); 66 | } 67 | 68 | public static void main(String[] args) throws Exception { 69 | FixTaskThread thread = new FixTaskThread("shell", 10); 70 | thread.start(); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/map/thread/MapThreadPool.java: -------------------------------------------------------------------------------- 1 | package com.game.map.thread; 2 | 3 | import java.util.concurrent.ConcurrentHashMap; 4 | 5 | public class MapThreadPool { 6 | private ConcurrentHashMap threads = new ConcurrentHashMap<>(); 7 | 8 | public ConcurrentHashMap getThreads() { 9 | return threads; 10 | } 11 | 12 | public void setThreads(ConcurrentHashMap threads) { 13 | this.threads = threads; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/map/timer/ITimer.java: -------------------------------------------------------------------------------- 1 | package com.game.map.timer; 2 | 3 | public abstract class ITimer { 4 | private long lastTime; 5 | private long interval; 6 | public ITimer(long interval) { 7 | this.interval = interval; 8 | } 9 | 10 | public boolean canExec() { 11 | long now = System.currentTimeMillis(); 12 | if (now - lastTime < interval) { 13 | return false; 14 | } 15 | lastTime = now; 16 | return true; 17 | } 18 | 19 | public abstract void exec(); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/name/manager/NameManager.java: -------------------------------------------------------------------------------- 1 | package com.game.name.manager; 2 | 3 | import java.util.HashSet; 4 | 5 | import com.db.data.dao.RoleDao; 6 | import com.manager.Manager; 7 | import com.manager.PriorityEnum; 8 | 9 | public class NameManager extends Manager { 10 | private HashSet names = new HashSet<>(); // 角色名称,帮会名称等 11 | private HashSet illegalStrs = new HashSet<>(); // 非法字符 12 | 13 | @Override 14 | public boolean init() { 15 | { 16 | names.addAll(RoleDao.getInstance().selectNames()); 17 | } 18 | return true; 19 | } 20 | 21 | @Override 22 | public void stop() { 23 | } 24 | 25 | @Override 26 | public PriorityEnum getPriority() { 27 | return PriorityEnum.NORMAL; 28 | } 29 | 30 | public boolean isRepeat(String name, boolean insert) { 31 | synchronized (names) { 32 | if (names.contains(name)) { 33 | return true; 34 | } 35 | if (insert) { 36 | names.add(name); 37 | } 38 | return false; 39 | } 40 | } 41 | 42 | public boolean isLegal(String str) { 43 | for (String illegalStr : illegalStrs) { 44 | if (str.indexOf(illegalStr) != -1) { 45 | return false; 46 | } 47 | } 48 | return true; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/role/cache/RoleCache.java: -------------------------------------------------------------------------------- 1 | package com.game.role.cache; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.concurrent.ConcurrentHashMap; 6 | 7 | import com.db.data.bean.RoleBean; 8 | import com.db.data.dao.RoleDao; 9 | import com.game.role.struct.Role; 10 | import com.manager.ManagerPool; 11 | 12 | public class RoleCache { 13 | private ConcurrentHashMap> roles = new ConcurrentHashMap<>(); 14 | 15 | public List getRoleByAccountId(long accountId) { 16 | ConcurrentHashMap map = roles.get(accountId); 17 | if (map == null) { 18 | return null; 19 | } 20 | return new ArrayList(map.values()); 21 | } 22 | 23 | public List getRoleByAccountIdFromDb(long accountId) { 24 | List beans = RoleDao.getInstance().selectByAccountId(accountId); 25 | 26 | if (beans == null || beans.isEmpty()) { 27 | return null; 28 | } 29 | ArrayList roles = new ArrayList(); 30 | for (RoleBean bean : beans) { 31 | roles.add(ManagerPool.role.createRole(bean)); 32 | } 33 | return roles; 34 | } 35 | 36 | public void add(Role role) { 37 | ConcurrentHashMap map = roles.get(role.getAccountId()); 38 | if (map == null) { 39 | map = new ConcurrentHashMap(); 40 | roles.put(role.getAccountId(), map); 41 | } 42 | map.put(role.getId(), role); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /server-mmo/src/com/game/role/struct/Role.java: -------------------------------------------------------------------------------- 1 | package com.game.role.struct; 2 | 3 | import com.game.login.message.RoleBrief; 4 | import com.game.map.struct.RoleMapData; 5 | 6 | public class Role { 7 | private long id; 8 | private String name; 9 | private long accountId; 10 | private long createTime; 11 | private RoleMapData map; 12 | 13 | public long getId() { 14 | return id; 15 | } 16 | 17 | public void setId(long id) { 18 | this.id = id; 19 | } 20 | 21 | public RoleBrief getBriefInfo() { 22 | RoleBrief brief = new RoleBrief(); 23 | brief.setRoleId(id); 24 | brief.setName(name); 25 | return brief; 26 | } 27 | 28 | public long getAccountId() { 29 | return accountId; 30 | } 31 | 32 | public void setAccountId(long accountId) { 33 | this.accountId = accountId; 34 | } 35 | 36 | public String getName() { 37 | return name; 38 | } 39 | 40 | public void setName(String name) { 41 | this.name = name; 42 | } 43 | 44 | public long getCreateTime() { 45 | return createTime; 46 | } 47 | 48 | public void setCreateTime(long createTime) { 49 | this.createTime = createTime; 50 | } 51 | 52 | public RoleMapData getMap() { 53 | return map; 54 | } 55 | 56 | public void setMap(RoleMapData map) { 57 | this.map = map; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /server-mmo/src/com/logger/GlobalLogger.java: -------------------------------------------------------------------------------- 1 | package com.logger; 2 | 3 | import org.apache.log4j.Logger; 4 | 5 | public class GlobalLogger { 6 | public static Logger db = Logger.getLogger("DB"); 7 | } 8 | -------------------------------------------------------------------------------- /server-mmo/src/com/manager/Manager.java: -------------------------------------------------------------------------------- 1 | package com.manager; 2 | 3 | public abstract class Manager { 4 | protected Manager() { 5 | ManagerPool.regist(this); 6 | } 7 | 8 | public abstract boolean init(); 9 | public abstract void stop(); 10 | public abstract PriorityEnum getPriority(); 11 | } 12 | -------------------------------------------------------------------------------- /server-mmo/src/com/manager/ManagerPool.java: -------------------------------------------------------------------------------- 1 | package com.manager; 2 | 3 | import java.util.HashMap; 4 | import java.util.HashSet; 5 | 6 | import org.apache.log4j.Logger; 7 | 8 | import com.config.manager.ConfigManager; 9 | import com.db.config.DbConfigManager; 10 | import com.game.account.manager.AccountManager; 11 | import com.game.login.manager.LoginManager; 12 | import com.game.login.manager.LoginStateManager; 13 | import com.game.map.manager.MapManager; 14 | import com.game.name.manager.NameManager; 15 | import com.game.role.manager.RoleManager; 16 | import com.message.manager.MessageManager; 17 | import com.thread.manager.ThreadManager; 18 | 19 | public class ManagerPool { 20 | // public static ScriptManager script = new ScriptManager(); 21 | public static LoginManager login = new LoginManager(); 22 | public static AccountManager account = new AccountManager(); 23 | public static RoleManager role = new RoleManager(); 24 | public static LoginStateManager loginState = new LoginStateManager(); 25 | public static MessageManager message = new MessageManager(); 26 | public static ConfigManager config = new ConfigManager(); 27 | public static ThreadManager thread = new ThreadManager(); 28 | public static MapManager map = new MapManager(); 29 | public static DbConfigManager dbConfig = new DbConfigManager(); 30 | public static NameManager name = new NameManager(); 31 | 32 | private static HashMap> managers; 33 | private static Logger logger = Logger.getLogger(ManagerPool.class); 34 | public static boolean init() { 35 | if (managers == null) { 36 | return true; 37 | } 38 | for (Manager manager : managers.get(PriorityEnum.BASE)) { 39 | if (!manager.init()) { 40 | logger.error(manager.getClass().getName() + "初始化失败"); 41 | return false; 42 | } 43 | logger.error(manager.getClass().getName() + "初始化完成"); 44 | } 45 | 46 | for (Manager manager : managers.get(PriorityEnum.NORMAL)) { 47 | if (!manager.init()) { 48 | logger.error(manager.getClass().getName() + "初始化失败"); 49 | return false; 50 | } 51 | logger.error(manager.getClass().getName() + "初始化完成"); 52 | } 53 | return true; 54 | } 55 | 56 | public static void regist(Manager manager) { 57 | if (managers == null) { 58 | managers = new HashMap<>(); 59 | } 60 | HashSet map = managers.get(manager.getPriority()); 61 | if (map == null) { 62 | map = new HashSet<>(); 63 | managers.put(manager.getPriority(), map); 64 | } 65 | map.add(manager); 66 | } 67 | 68 | public static void stop() { 69 | for (Manager manager : managers.get(PriorityEnum.NORMAL)) { 70 | manager.stop(); 71 | } 72 | for (Manager manager : managers.get(PriorityEnum.BASE)) { 73 | manager.stop(); 74 | } 75 | } 76 | 77 | public static void main(String[] args) { 78 | ManagerPool.init(); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /server-mmo/src/com/manager/PriorityEnum.java: -------------------------------------------------------------------------------- 1 | package com.manager; 2 | 3 | public enum PriorityEnum { 4 | BASE, 5 | NORMAL, 6 | ; 7 | } 8 | -------------------------------------------------------------------------------- /server-mmo/src/com/message/manager/MessageManager.java: -------------------------------------------------------------------------------- 1 | package com.message.manager; 2 | 3 | import com.manager.Manager; 4 | import com.manager.PriorityEnum; 5 | import com.game.message.MessagePool; 6 | 7 | public class MessageManager extends Manager { 8 | 9 | @Override 10 | public boolean init() { 11 | MessagePool.getInstance().register(100101, com.game.login.handler.ReqLoginSelectRoleHandler.class, com.game.login.message.ReqLoginSelectRoleMessage.class); 12 | MessagePool.getInstance().register(100100, com.game.login.handler.ReqLoginHandler.class, com.game.login.message.ReqLoginMessage.class); 13 | MessagePool.getInstance().register(100102, com.game.login.handler.ReqLoginCreateRoleHandler.class, com.game.login.message.ReqLoginCreateRoleMessage.class); 14 | MessagePool.getInstance().register(103100, com.game.map.handler.ReqMapMoveHandler.class, com.game.map.message.ReqMapMoveMessage.class); 15 | return true; 16 | } 17 | 18 | @Override 19 | public void stop() { 20 | } 21 | 22 | @Override 23 | public PriorityEnum getPriority() { 24 | return PriorityEnum.BASE; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /server-mmo/src/com/message/util/MessageDispatcher.java: -------------------------------------------------------------------------------- 1 | package com.message.util; 2 | 3 | import org.apache.log4j.Logger; 4 | 5 | import com.game.account.struct.Account; 6 | import com.game.login.message.ReqLoginSelectRoleMessage; 7 | import com.game.login.struct.LoginState; 8 | import com.game.map.thread.MapThread; 9 | import com.game.role.struct.Role; 10 | import com.game.thread.pool.NormalThread; 11 | import com.manager.ManagerPool; 12 | 13 | public class MessageDispatcher { 14 | private static Logger logger = Logger.getLogger(MessageDispatcher.class); 15 | 16 | public void dispatch(com.game.message.struct.Handler handler) { 17 | try { 18 | switch (handler.getMessage().getId()) { 19 | case 100100: 20 | login(handler); 21 | break; 22 | case 100102: 23 | createRole(handler); 24 | break; 25 | case 100101: 26 | selectRole(handler); 27 | break; 28 | default: 29 | exec(handler); 30 | break; 31 | } 32 | } catch (Exception e) { 33 | logger.error(e, e); 34 | } 35 | } 36 | 37 | private void login(com.game.message.struct.Handler handler) { 38 | if (!ManagerPool.loginState.change(handler.getContext(), LoginState.LOGIN_ING)) { 39 | handler.getContext().close(); 40 | return; 41 | } 42 | 43 | ManagerPool.thread.getLoginExecutor().execute(new NormalThread(handler)); 44 | 45 | } 46 | 47 | private void exec(com.game.message.struct.Handler handler) { 48 | if (!ManagerPool.loginState.check(handler.getContext(), LoginState.SELECT_ROLE_END)) { 49 | handler.getContext().close(); 50 | return; 51 | } 52 | 53 | Account account = ManagerPool.account.getAccount(handler.getContext()); 54 | if (account == null) { 55 | handler.getContext().close(); 56 | return; 57 | } 58 | 59 | if (account.getRole() == null) { 60 | handler.getContext().close(); 61 | return; 62 | } 63 | 64 | MapThread thread = ManagerPool.map.getMapThread(account.getRole().getMap()); 65 | thread.addTask(handler); 66 | } 67 | 68 | private void selectRole(com.game.message.struct.Handler handler) { 69 | if (!ManagerPool.loginState.change(handler.getContext(), LoginState.SELECT_ROLE_ING)) { 70 | handler.getContext().close(); 71 | return; 72 | } 73 | 74 | Account account = ManagerPool.account.getAccount(handler.getContext()); 75 | if (account == null) { 76 | handler.getContext().close(); 77 | return; 78 | } 79 | 80 | if (account.getRole() != null) { 81 | handler.getContext().close(); 82 | return; 83 | } 84 | 85 | Role role = account.getRoles().get(((ReqLoginSelectRoleMessage)handler.getMessage()).getRoleId()); 86 | if (role == null) { 87 | handler.getContext().close(); 88 | return; 89 | } 90 | 91 | account.setRole(role); 92 | 93 | exec(handler); 94 | } 95 | 96 | private void createRole(com.game.message.struct.Handler handler) { 97 | if (!ManagerPool.loginState.change(handler.getContext(), LoginState.CREATE_ROLE_ING)) { 98 | handler.getContext().close(); 99 | return; 100 | } 101 | handler.exec(); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /server-mmo/src/com/message/util/MessageUtil.java: -------------------------------------------------------------------------------- 1 | package com.message.util; 2 | 3 | import io.netty.channel.ChannelHandlerContext; 4 | 5 | import java.util.HashMap; 6 | import java.util.HashSet; 7 | 8 | import org.apache.log4j.Logger; 9 | 10 | import com.game.account.struct.Account; 11 | import com.game.message.struct.Message; 12 | import com.game.role.struct.Role; 13 | import com.manager.ManagerPool; 14 | 15 | public class MessageUtil { 16 | private static Logger logger = Logger.getLogger(MessageUtil.class); 17 | private static HashMap contexts = new HashMap<>(); // accountId做key 18 | public static void add(long accountId, ChannelHandlerContext context) { 19 | contexts.put(accountId, context); 20 | } 21 | public static void remove(long accountId) { 22 | contexts.remove(accountId); 23 | } 24 | 25 | public static void send(Role role, Message msg) { 26 | send(role.getAccountId(), msg); 27 | } 28 | 29 | public static void send(Account account, Message msg) { 30 | send(account.getId(), msg); 31 | } 32 | 33 | public static void send(long accountId, Message msg) { 34 | ChannelHandlerContext context = contexts.get(accountId); 35 | if (context == null) { 36 | logger.error(new StringBuilder().append(accountId).append("连接不存在,发送消息失败").toString()); 37 | return ; 38 | } 39 | send(context, msg); 40 | } 41 | 42 | public static void send(ChannelHandlerContext context, Message msg) { 43 | context.writeAndFlush(msg); 44 | } 45 | 46 | public static void sendRound(Role role, Message msg) { 47 | HashSet roles = ManagerPool.map.getRoundRole(role); 48 | for (Role tmp : roles) { 49 | send(tmp, msg); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /server-mmo/src/com/script/manager/ScriptManager.java: -------------------------------------------------------------------------------- 1 | package com.script.manager; 2 | 3 | import java.util.HashMap; 4 | 5 | import com.game.script.ScriptLoader; 6 | import com.game.script.struct.IScript; 7 | import com.manager.Manager; 8 | import com.manager.PriorityEnum; 9 | 10 | 11 | public class ScriptManager extends Manager { 12 | private boolean flag = false; 13 | @Override 14 | public boolean init() { 15 | if (!flag) { 16 | return true; 17 | } 18 | if (System.getProperty("os.name").startsWith("Win")) { 19 | logger.error("windows 不加载脚本"); 20 | return true; 21 | } 22 | try { 23 | scripts = loader.loadJar("script.jar"); 24 | } catch (Exception e) { 25 | logger.error(e, e); 26 | return false; 27 | } 28 | return true; 29 | } 30 | 31 | @Override 32 | public PriorityEnum getPriority() { 33 | return PriorityEnum.BASE; 34 | } 35 | 36 | public IScript getScript(int id) { 37 | return scripts.get(id); 38 | } 39 | 40 | public boolean reload(int id) { 41 | try { 42 | HashMap tmp = loader.loadJar("script.jar"); 43 | IScript script = tmp.get(id); 44 | scripts.put(script.getId(), script); 45 | } catch (Exception e) { 46 | logger.error(e, e); 47 | return false; 48 | } 49 | return true; 50 | } 51 | 52 | private org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(this.getClass()); 53 | private ScriptLoader loader = new ScriptLoader(); 54 | private HashMap scripts; 55 | @Override 56 | public void stop() { 57 | } 58 | 59 | public static void main(String[] args) { 60 | System.out.println(System.getProperty("os.name")); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /server-mmo/src/com/server/game/GameHandler.java: -------------------------------------------------------------------------------- 1 | package com.server.game; 2 | 3 | import io.netty.channel.ChannelHandlerContext; 4 | 5 | import org.apache.log4j.Logger; 6 | 7 | import com.game.account.struct.Account; 8 | import com.game.netty.handler.Handler; 9 | import com.manager.ManagerPool; 10 | import com.message.util.MessageDispatcher; 11 | 12 | public class GameHandler extends Handler { 13 | private static Logger logger = Logger.getLogger(GameHandler.class); 14 | private MessageDispatcher dispatcher = new MessageDispatcher(); 15 | 16 | @Override 17 | public void onActive(ChannelHandlerContext ctx) { 18 | logger.error("建立连接成功:" + ctx.channel()); 19 | } 20 | 21 | @Override 22 | public void onInactive(ChannelHandlerContext ctx) { 23 | logger.error("断开连接成功:" + ctx.channel()); 24 | Account account = ManagerPool.account.unbind(ctx); 25 | if (account != null) { 26 | ManagerPool.account.onOffline(account); 27 | } 28 | } 29 | 30 | @Override 31 | public void onRecvMsg(com.game.message.struct.Handler handler) { 32 | dispatcher.dispatch(handler); 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /server-mmo/src/com/server/game/GameServer.java: -------------------------------------------------------------------------------- 1 | package com.server.game; 2 | 3 | import org.apache.log4j.Logger; 4 | 5 | import io.netty.channel.ChannelInitializer; 6 | import io.netty.channel.socket.SocketChannel; 7 | 8 | import com.game.netty.Server; 9 | import com.game.netty.coder.Decoder; 10 | import com.game.netty.coder.Encoder; 11 | import com.manager.ManagerPool; 12 | 13 | public class GameServer extends Server { 14 | private static Logger logger = Logger.getLogger(GameServer.class); 15 | private static GameServer instance = new GameServer(); 16 | private GameServer() {} 17 | public static GameServer getInstance() { 18 | return instance; 19 | } 20 | 21 | @Override 22 | protected boolean init() { 23 | return true; 24 | } 25 | 26 | @Override 27 | public void stop() { 28 | logger.error("关闭GameServer"); 29 | ManagerPool.stop(); 30 | } 31 | 32 | @Override 33 | public ChannelInitializer getChannelInitializer() { 34 | return new ChannelInitializer() { 35 | @Override 36 | protected void initChannel(SocketChannel ch) throws Exception { 37 | ch.pipeline().addLast(new Encoder()); 38 | ch.pipeline().addLast(new Decoder()); 39 | ch.pipeline().addLast(new GameHandler()); 40 | } 41 | }; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /server-mmo/src/com/thread/manager/ThreadManager.java: -------------------------------------------------------------------------------- 1 | package com.thread.manager; 2 | 3 | import com.game.map.thread.MapThreadPool; 4 | import com.game.thread.pool.FixedPoolExecutor; 5 | import com.game.thread.queue.FixTaskThread; 6 | import com.manager.Manager; 7 | import com.manager.PriorityEnum; 8 | 9 | public class ThreadManager extends Manager { 10 | private FixedPoolExecutor loginExecutor = new FixedPoolExecutor(16, 160); // 这个线程池会涉及到读取数据库 11 | private FixTaskThread dbThread = new FixTaskThread("dbThread", 10000); 12 | private MapThreadPool mapThreadPool = new MapThreadPool(); 13 | 14 | @Override 15 | public boolean init() { 16 | dbThread.start(); 17 | return true; 18 | } 19 | 20 | @Override 21 | public PriorityEnum getPriority() { 22 | return PriorityEnum.NORMAL; 23 | } 24 | 25 | @Override 26 | public void stop() { 27 | dbThread.onStop(); 28 | } 29 | 30 | public FixedPoolExecutor getLoginExecutor() { 31 | return loginExecutor; 32 | } 33 | 34 | public void setLoginExecutor(FixedPoolExecutor loginExecutor) { 35 | this.loginExecutor = loginExecutor; 36 | } 37 | 38 | public MapThreadPool getMapThreadPool() { 39 | return mapThreadPool; 40 | } 41 | 42 | public void setMapThreadPool(MapThreadPool mapThreadPool) { 43 | this.mapThreadPool = mapThreadPool; 44 | } 45 | 46 | public FixTaskThread getDbThread() { 47 | return dbThread; 48 | } 49 | 50 | public void setDbThread(FixTaskThread dbThread) { 51 | this.dbThread = dbThread; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /server-mmo/src/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /server-mmo/src/message.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /tools/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /tools/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | tools 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /tools/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /tools/bin/.gitignore: -------------------------------------------------------------------------------- 1 | /tool/ 2 | -------------------------------------------------------------------------------- /tools/config/config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /tools/ftl/db/java/bean.ftl: -------------------------------------------------------------------------------- 1 | package com.db.${type}.bean; 2 | 3 | public class ${name?cap_first}Bean { 4 | <#list fields as field> 5 | private ${field.clazz} ${field.name}; 6 | 7 | 8 | <#list fields as field> 9 | public ${field.clazz} get${field.name?cap_first}(){ 10 | return ${field.name}; 11 | } 12 | 13 | public void set${field.name?cap_first}(${field.clazz} ${field.name}){ 14 | this.${field.name} = ${field.name}; 15 | } 16 | 17 | 18 | } -------------------------------------------------------------------------------- /tools/ftl/db/java/config.ftl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | <#list names as name> 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /tools/ftl/db/java/container.ftl: -------------------------------------------------------------------------------- 1 | package com.db.config.container; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import com.db.config.dao.${name?cap_first}Dao; 9 | import com.db.config.bean.${name?cap_first}Bean; 10 | 11 | public class ${name?cap_first}Container { 12 | private List<${name?cap_first}Bean> list = new ArrayList<>(); 13 | private Map map = new HashMap<>(); 14 | private ${name?cap_first}Dao dao = new ${name?cap_first}Dao(); 15 | public List<${name?cap_first}Bean> getList() { 16 | return list; 17 | } 18 | public Map getMap() { 19 | return map; 20 | } 21 | public boolean init() { 22 | list = dao.select(); 23 | for (${name?cap_first}Bean bean : list) { 24 | map.put(bean.getId(), bean); 25 | } 26 | return true; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tools/ftl/db/java/dao.ftl: -------------------------------------------------------------------------------- 1 | package com.db.${type}.dao; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.session.SqlSession; 6 | import org.apache.ibatis.session.SqlSessionFactory; 7 | 8 | import com.db.DbFactory; 9 | import com.db.${type}.bean.${name?cap_first}Bean; 10 | 11 | public class ${name?cap_first}Dao { 12 | private ${name?cap_first}Dao() {} 13 | private static ${name?cap_first}Dao instance = new ${name?cap_first}Dao(); 14 | public static ${name?cap_first}Dao getInstance() { 15 | return instance; 16 | } 17 | SqlSessionFactory factory = DbFactory.getInstance().get${type?cap_first}Factory(); 18 | 19 | public List<${name?cap_first}Bean> select() { 20 | long s = System.currentTimeMillis(); 21 | SqlSession session = factory.openSession(); 22 | try{ 23 | List<${name?cap_first}Bean> list = session.selectList("${name}.select"); 24 | long interval = System.currentTimeMillis() - s; 25 | if (interval > 10) { 26 | com.logger.DbLogger.logger.error(new StringBuilder().append("${name?cap_first}Dao.").append("select:").append(interval)); 27 | } 28 | return list; 29 | }finally{ 30 | session.close(); 31 | } 32 | } 33 | 34 | public void insert(${name?cap_first}Bean bean) { 35 | long s = System.currentTimeMillis(); 36 | SqlSession session = factory.openSession(); 37 | try{ 38 | session.insert("${name}.insert", bean); 39 | session.commit(); 40 | long interval = System.currentTimeMillis() - s; 41 | if (interval > 10) { 42 | com.logger.DbLogger.logger.error(new StringBuilder().append("${name?cap_first}Dao.").append("select:").append(interval)); 43 | } 44 | }finally{ 45 | session.close(); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /tools/ftl/db/java/manager.ftl: -------------------------------------------------------------------------------- 1 | package com.db.config; 2 | 3 | <#list names as name> 4 | import com.db.config.container.${name?cap_first}Container; 5 | 6 | import com.manager.Manager; 7 | 8 | public class DbConfigManager extends Manager { 9 | <#list names as name> 10 | public ${name?cap_first}Container ${name} = new ${name?cap_first}Container(); 11 | 12 | 13 | @Override 14 | public boolean init() { 15 | <#list names as name> 16 | if (!${name}.init()) return false; 17 | 18 | return true; 19 | } 20 | 21 | @Override 22 | public void stop() { 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /tools/ftl/db/java/mapper.ftl: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | <#list fields as field> 8 | 9 | 10 | 11 | 12 | 15 | 16 | 17 | insert into ${name}(<#list fields as field><#if field_has_next>${field.name},<#else>${field.name}) 18 | values (<#list fields as field><#if field_has_next>${"#"}{${field.name},jdbcType=${field.dbType}},<#else>${"#"}{${field.name},jdbcType=${field.dbType}}) 19 | 20 | -------------------------------------------------------------------------------- /tools/ftl/message/java/handler.ftl: -------------------------------------------------------------------------------- 1 | package ${pkg}.handler; 2 | 3 | import com.game.message.struct.Handler; 4 | import com.game.account.struct.Account; 5 | import org.apache.log4j.Logger; 6 | import com.manager.ManagerPool; 7 | 8 | public class ${name}Handler extends Handler{ 9 | @SuppressWarnings("unused") 10 | private static Logger logger = Logger.getLogger(${name}Handler.class); 11 | @Override 12 | public void exec() { 13 | ${pkg}.message.${name}Message msg = (${pkg}.message.${name}Message)this.getMessage(); 14 | Account account = ManagerPool.account.getAccount(this.getContext()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tools/ftl/message/java/manager.ftl: -------------------------------------------------------------------------------- 1 | package com.message.manager; 2 | 3 | import com.manager.Manager; 4 | import com.game.message.MessagePool; 5 | 6 | public class MessageManager extends Manager { 7 | 8 | @Override 9 | public boolean init() { 10 | <#list details as detail> 11 | MessagePool.getInstance().register(${detail.id?c}, ${detail.pkg}.handler.${detail.name}Handler.class, ${detail.pkg}.message.${detail.name}Message.class); 12 | 13 | return true; 14 | } 15 | 16 | @Override 17 | public void stop() { 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /tools/src/tool/db/DbOpt.java: -------------------------------------------------------------------------------- 1 | package tool.db; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.ResultSet; 6 | import java.sql.ResultSetMetaData; 7 | import java.sql.Statement; 8 | import java.util.ArrayList; 9 | import java.util.HashMap; 10 | import java.util.List; 11 | import java.util.Map; 12 | 13 | import tool.util.Config; 14 | 15 | public class DbOpt { 16 | private static DbOpt instance; 17 | 18 | public static DbOpt getInstance() throws Exception { 19 | if (instance == null) { 20 | instance = new DbOpt(); 21 | } 22 | return instance; 23 | } 24 | 25 | private Connection config; 26 | private Connection data; 27 | 28 | private DbOpt() throws Exception { 29 | data = init(Config.getInstance().getData_url(), Config.getInstance().getData_usr(), Config.getInstance().getData_pwd()); 30 | config = init(Config.getInstance().getConfig_url(), Config.getInstance().getConfig_usr(), Config.getInstance().getConfig_pwd()); 31 | } 32 | 33 | private Connection init(String url, String user, String password) { 34 | Connection conn; 35 | // 驱动程序名 36 | String driver = "com.mysql.jdbc.Driver"; 37 | try { 38 | // 加载驱动程序 39 | Class.forName(driver); 40 | 41 | // 连续数据库 42 | conn = DriverManager.getConnection(url, user, password); 43 | 44 | if (!conn.isClosed()) 45 | System.out.println("Succeeded connecting to the Database!"); 46 | } catch (Exception e) { 47 | e.printStackTrace(); 48 | return null; 49 | } 50 | return conn; 51 | } 52 | 53 | public Object[] getDataTables() throws Exception { 54 | String sql = "show tables"; 55 | ResultSet tables = data.getMetaData().getTables(null, null, null, new String[] { "TABLE" }); 56 | List list = new ArrayList<>(); 57 | 58 | while (tables.next()) { 59 | list.add(tables.getString(3)); 60 | } 61 | 62 | return list.toArray(); 63 | } 64 | 65 | public Object[] getConfigTables() throws Exception { 66 | String sql = "show tables"; 67 | ResultSet tables = config.getMetaData().getTables(null, null, null, new String[] { "TABLE" }); 68 | List list = new ArrayList<>(); 69 | 70 | while (tables.next()) { 71 | list.add(tables.getString(3)); 72 | } 73 | 74 | return list.toArray(); 75 | } 76 | 77 | public Map getDataTableMap(String table) throws Exception { 78 | HashMap map = new HashMap<>(); 79 | String sql = new StringBuilder().append("select * from ").append(table).toString(); 80 | Statement statement = data.createStatement(); 81 | ResultSet rs = statement.executeQuery(sql); 82 | ResultSetMetaData rsmd = rs.getMetaData(); 83 | int colcount = rsmd.getColumnCount();// 取得全部列数 84 | for (int i = 1; i <= colcount; i++) { 85 | map.put(rsmd.getColumnName(i), rsmd.getColumnTypeName(i)); 86 | } 87 | return map; 88 | } 89 | 90 | public Map getConfigTableMap(String table) throws Exception { 91 | HashMap map = new HashMap<>(); 92 | String sql = new StringBuilder().append("select * from ").append(table).toString(); 93 | Statement statement = config.createStatement(); 94 | ResultSet rs = statement.executeQuery(sql); 95 | ResultSetMetaData rsmd = rs.getMetaData(); 96 | int colcount = rsmd.getColumnCount();// 取得全部列数 97 | for (int i = 1; i <= colcount; i++) { 98 | map.put(rsmd.getColumnName(i), rsmd.getColumnTypeName(i)); 99 | } 100 | return map; 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /tools/src/tool/db/struct/Bean.java: -------------------------------------------------------------------------------- 1 | package tool.db.struct; 2 | 3 | import java.io.File; 4 | import java.util.ArrayList; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | 8 | import tool.ftl.IFtl; 9 | import tool.ftl.LanguageEnum; 10 | import tool.util.Config; 11 | import tool.util.StringUtil; 12 | 13 | public class Bean implements IFtl { 14 | private String type; 15 | private String name; 16 | private List fields = new ArrayList<>(); 17 | 18 | @Override 19 | public HashMap getDataModel() { 20 | HashMap map = new HashMap<>(); 21 | map.put("type", type); 22 | map.put("name", name); 23 | map.put("fields", fields); 24 | return map; 25 | } 26 | 27 | @Override 28 | public String getFtlFileName() { 29 | return "bean.ftl"; 30 | } 31 | 32 | @Override 33 | public String getDstPath(LanguageEnum language) { 34 | String string = StringUtil.join(Config.getInstance().getPath(language), File.separator, "com.db.", type, ".bean."); 35 | return StringUtil.join(string.replace(".", File.separator), StringUtil.upFirstChar(name), "Bean.java"); 36 | } 37 | 38 | @Override 39 | public boolean isRewrite() { 40 | return true; 41 | } 42 | 43 | public String getType() { 44 | return type; 45 | } 46 | 47 | public void setType(String type) { 48 | this.type = type; 49 | } 50 | 51 | public String getName() { 52 | return name; 53 | } 54 | 55 | public void setName(String name) { 56 | this.name = name; 57 | } 58 | 59 | public List getFields() { 60 | return fields; 61 | } 62 | 63 | public void setFields(List fields) { 64 | this.fields = fields; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /tools/src/tool/db/struct/Config.java: -------------------------------------------------------------------------------- 1 | package tool.db.struct; 2 | 3 | import java.io.File; 4 | import java.util.ArrayList; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | 8 | import tool.ftl.IFtl; 9 | import tool.ftl.LanguageEnum; 10 | import tool.util.StringUtil; 11 | 12 | public class Config implements IFtl { 13 | private String url; 14 | private String usr; 15 | private String pwd; 16 | private String type; 17 | private List names = new ArrayList<>(); 18 | 19 | @Override 20 | public HashMap getDataModel() { 21 | HashMap map = new HashMap<>(); 22 | map.put("url", url); 23 | map.put("usr", usr); 24 | map.put("pwd", pwd); 25 | map.put("path", "com/db/" + type + "/mapper"); 26 | map.put("names", names); 27 | return map; 28 | } 29 | 30 | @Override 31 | public String getFtlFileName() { 32 | return "config.ftl"; 33 | } 34 | 35 | @Override 36 | public String getDstPath(LanguageEnum language) { 37 | String path = tool.util.Config.getInstance().getPath(language); 38 | String string = StringUtil.join(path.substring(0, path.length() - 4), File.separator, "config", File.separator); 39 | return StringUtil.join(string.replace(".", File.separator), "db-", type, ".xml"); 40 | } 41 | 42 | @Override 43 | public boolean isRewrite() { 44 | return true; 45 | } 46 | 47 | public String getUrl() { 48 | return url; 49 | } 50 | 51 | public void setUrl(String url) { 52 | this.url = url; 53 | } 54 | 55 | public String getUsr() { 56 | return usr; 57 | } 58 | 59 | public void setUsr(String usr) { 60 | this.usr = usr; 61 | } 62 | 63 | public String getPwd() { 64 | return pwd; 65 | } 66 | 67 | public void setPwd(String pwd) { 68 | this.pwd = pwd; 69 | } 70 | 71 | public String getType() { 72 | return type; 73 | } 74 | 75 | public void setType(String type) { 76 | this.type = type; 77 | } 78 | 79 | public List getNames() { 80 | return names; 81 | } 82 | 83 | public void setNames(List names) { 84 | this.names = names; 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /tools/src/tool/db/struct/Container.java: -------------------------------------------------------------------------------- 1 | package tool.db.struct; 2 | 3 | import java.io.File; 4 | import java.util.HashMap; 5 | 6 | import tool.ftl.IFtl; 7 | import tool.ftl.LanguageEnum; 8 | import tool.util.Config; 9 | import tool.util.StringUtil; 10 | 11 | public class Container implements IFtl { 12 | private String name; 13 | private String type; 14 | 15 | @Override 16 | public HashMap getDataModel() { 17 | HashMap map = new HashMap<>(); 18 | map.put("name", name); 19 | return map; 20 | } 21 | 22 | @Override 23 | public String getFtlFileName() { 24 | return "container.ftl"; 25 | } 26 | 27 | @Override 28 | public String getDstPath(LanguageEnum language) { 29 | String string = StringUtil.join(Config.getInstance().getPath(language), File.separator, "com.db.", type, ".container."); 30 | return StringUtil.join(string.replace(".", File.separator), StringUtil.upFirstChar(name), "Container.java"); 31 | } 32 | 33 | @Override 34 | public boolean isRewrite() { 35 | return false; 36 | } 37 | 38 | public String getName() { 39 | return name; 40 | } 41 | 42 | public void setName(String name) { 43 | this.name = name; 44 | } 45 | 46 | public String getType() { 47 | return type; 48 | } 49 | 50 | public void setType(String type) { 51 | this.type = type; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /tools/src/tool/db/struct/Dao.java: -------------------------------------------------------------------------------- 1 | package tool.db.struct; 2 | 3 | import java.io.File; 4 | import java.util.HashMap; 5 | 6 | import tool.ftl.IFtl; 7 | import tool.ftl.LanguageEnum; 8 | import tool.util.Config; 9 | import tool.util.StringUtil; 10 | 11 | public class Dao implements IFtl { 12 | private String type; 13 | private String name; 14 | 15 | public String getType() { 16 | return type; 17 | } 18 | 19 | public void setType(String type) { 20 | this.type = type; 21 | } 22 | 23 | public String getName() { 24 | return name; 25 | } 26 | 27 | public void setName(String name) { 28 | this.name = name; 29 | } 30 | 31 | @Override 32 | public HashMap getDataModel() { 33 | HashMap map = new HashMap<>(); 34 | map.put("type", type); 35 | map.put("name", name); 36 | return map; 37 | } 38 | 39 | @Override 40 | public String getFtlFileName() { 41 | return "dao.ftl"; 42 | } 43 | 44 | @Override 45 | public String getDstPath(LanguageEnum language) { 46 | String string = StringUtil.join(Config.getInstance().getPath(language), File.separator, "com.db.", type, ".dao."); 47 | return StringUtil.join(string.replace(".", File.separator), StringUtil.upFirstChar(name), "Dao.java"); 48 | } 49 | 50 | @Override 51 | public boolean isRewrite() { 52 | return false; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /tools/src/tool/db/struct/Field.java: -------------------------------------------------------------------------------- 1 | package tool.db.struct; 2 | 3 | public class Field { 4 | private String clazz; 5 | private String name; 6 | private String dbType; 7 | public String getClazz() { 8 | return clazz; 9 | } 10 | public void setClazz(String clazz) { 11 | this.clazz = clazz; 12 | } 13 | public String getName() { 14 | return name; 15 | } 16 | public void setName(String name) { 17 | this.name = name; 18 | } 19 | public String getDbType() { 20 | return dbType; 21 | } 22 | public void setDbType(String dbType) { 23 | this.dbType = dbType; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tools/src/tool/db/struct/Manager.java: -------------------------------------------------------------------------------- 1 | package tool.db.struct; 2 | 3 | import java.io.File; 4 | import java.util.ArrayList; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | 8 | import tool.ftl.IFtl; 9 | import tool.ftl.LanguageEnum; 10 | import tool.util.Config; 11 | import tool.util.StringUtil; 12 | 13 | public class Manager implements IFtl { 14 | private List names = new ArrayList<>(); 15 | private String type; 16 | 17 | @Override 18 | public HashMap getDataModel() { 19 | HashMap map = new HashMap<>(); 20 | map.put("names", names); 21 | return map; 22 | } 23 | 24 | @Override 25 | public String getFtlFileName() { 26 | return "manager.ftl"; 27 | } 28 | 29 | @Override 30 | public String getDstPath(LanguageEnum language) { 31 | String string = StringUtil.join(Config.getInstance().getPath(language), File.separator, "com.db.", type, "."); 32 | return StringUtil.join(string.replace(".", File.separator), "DbConfigManager.java"); 33 | } 34 | 35 | @Override 36 | public boolean isRewrite() { 37 | return true; 38 | } 39 | 40 | public List getNames() { 41 | return names; 42 | } 43 | 44 | public void setNames(List names) { 45 | this.names = names; 46 | } 47 | 48 | public String getType() { 49 | return type; 50 | } 51 | 52 | public void setType(String type) { 53 | this.type = type; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /tools/src/tool/db/struct/Mapper.java: -------------------------------------------------------------------------------- 1 | package tool.db.struct; 2 | 3 | import java.io.File; 4 | import java.util.ArrayList; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | 8 | import tool.ftl.IFtl; 9 | import tool.ftl.LanguageEnum; 10 | import tool.util.Config; 11 | import tool.util.StringUtil; 12 | 13 | public class Mapper implements IFtl { 14 | private String type; 15 | private String name; 16 | private List fields = new ArrayList<>(); 17 | 18 | public String getType() { 19 | return type; 20 | } 21 | 22 | public void setType(String type) { 23 | this.type = type; 24 | } 25 | 26 | public String getName() { 27 | return name; 28 | } 29 | 30 | public void setName(String name) { 31 | this.name = name; 32 | } 33 | 34 | public List getFields() { 35 | return fields; 36 | } 37 | 38 | public void setFields(List fields) { 39 | this.fields = fields; 40 | } 41 | 42 | @Override 43 | public HashMap getDataModel() { 44 | HashMap map = new HashMap<>(); 45 | map.put("type", type); 46 | map.put("name", name); 47 | map.put("fields", fields); 48 | return map; 49 | } 50 | 51 | @Override 52 | public String getFtlFileName() { 53 | return "mapper.ftl"; 54 | } 55 | 56 | @Override 57 | public String getDstPath(LanguageEnum language) { 58 | String string = StringUtil.join(Config.getInstance().getPath(language), File.separator, "com.db.", type, ".mapper."); 59 | return StringUtil.join(string.replace(".", File.separator), StringUtil.upFirstChar(name), ".xml"); 60 | } 61 | 62 | @Override 63 | public boolean isRewrite() { 64 | return false; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /tools/src/tool/ftl/FunctionEnum.java: -------------------------------------------------------------------------------- 1 | package tool.ftl; 2 | 3 | public enum FunctionEnum { 4 | DB, 5 | MESSAGE 6 | } 7 | -------------------------------------------------------------------------------- /tools/src/tool/ftl/IFtl.java: -------------------------------------------------------------------------------- 1 | package tool.ftl; 2 | 3 | import java.util.HashMap; 4 | 5 | public interface IFtl { 6 | 7 | public HashMap getDataModel(); 8 | 9 | public String getFtlFileName(); 10 | 11 | public String getDstPath(LanguageEnum language); 12 | 13 | public boolean isRewrite(); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /tools/src/tool/ftl/LanguageEnum.java: -------------------------------------------------------------------------------- 1 | package tool.ftl; 2 | 3 | public enum LanguageEnum { 4 | JAVA_SERVER, 5 | JAVA_CLIENT, 6 | AS 7 | } 8 | -------------------------------------------------------------------------------- /tools/src/tool/message/struct/Bean.java: -------------------------------------------------------------------------------- 1 | package tool.message.struct; 2 | 3 | import java.io.File; 4 | import java.util.ArrayList; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | 8 | import tool.ftl.IFtl; 9 | import tool.ftl.LanguageEnum; 10 | import tool.util.Config; 11 | import tool.util.StringUtil; 12 | 13 | public class Bean implements IFtl { 14 | private String pkg; 15 | private String note; 16 | private String name; 17 | private List fields = new ArrayList<>(); 18 | 19 | @Override 20 | public HashMap getDataModel() { 21 | HashMap map = new HashMap<>(); 22 | map.put("pkg", pkg); 23 | map.put("note", note); 24 | map.put("name", name); 25 | map.put("fields", fields); 26 | return map; 27 | } 28 | 29 | public String getPkg() { 30 | return pkg; 31 | } 32 | 33 | public void setPkg(String pkg) { 34 | this.pkg = pkg; 35 | } 36 | 37 | public String getNote() { 38 | return note; 39 | } 40 | 41 | public void setNote(String note) { 42 | this.note = note; 43 | } 44 | 45 | public String getName() { 46 | return name; 47 | } 48 | 49 | public void setName(String name) { 50 | this.name = name; 51 | } 52 | 53 | public List getFields() { 54 | return fields; 55 | } 56 | 57 | public void setFields(List fields) { 58 | this.fields = fields; 59 | } 60 | 61 | @Override 62 | public String getFtlFileName() { 63 | return "bean.ftl"; 64 | } 65 | 66 | @Override 67 | public String getDstPath(LanguageEnum language) { 68 | String path = StringUtil.join(Config.getInstance().getPath(language), File.separator, pkg, File.separator, "message", File.separator, name); 69 | return StringUtil.join(path.replace(".", File.separator), ".java"); 70 | } 71 | 72 | @Override 73 | public boolean isRewrite() { 74 | return true; 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /tools/src/tool/message/struct/Field.java: -------------------------------------------------------------------------------- 1 | package tool.message.struct; 2 | 3 | public class Field { 4 | private String clazz; 5 | private String name; 6 | private String note; 7 | private int listFlag; // 0非list 1为list 8 | 9 | public String getClazz() { 10 | return clazz; 11 | } 12 | 13 | public void setClazz(String clazz) { 14 | this.clazz = clazz; 15 | } 16 | 17 | public String getName() { 18 | return name; 19 | } 20 | 21 | public void setName(String name) { 22 | this.name = name; 23 | } 24 | 25 | public String getNote() { 26 | return note; 27 | } 28 | 29 | public void setNote(String note) { 30 | this.note = note; 31 | } 32 | 33 | public int getListFlag() { 34 | return listFlag; 35 | } 36 | 37 | public void setListFlag(int listFlag) { 38 | this.listFlag = listFlag; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tools/src/tool/message/struct/Handler.java: -------------------------------------------------------------------------------- 1 | package tool.message.struct; 2 | 3 | import java.io.File; 4 | import java.util.HashMap; 5 | 6 | import tool.ftl.IFtl; 7 | import tool.ftl.LanguageEnum; 8 | import tool.util.Config; 9 | import tool.util.StringUtil; 10 | 11 | public class Handler implements IFtl { 12 | private int id; 13 | private String pkg; 14 | private String name; 15 | 16 | @Override 17 | public HashMap getDataModel() { 18 | HashMap map = new HashMap<>(); 19 | map.put("pkg", pkg); 20 | map.put("name", name); 21 | return map; 22 | } 23 | 24 | public String getPkg() { 25 | return pkg; 26 | } 27 | 28 | public void setPkg(String pkg) { 29 | this.pkg = pkg; 30 | } 31 | 32 | public String getName() { 33 | return name; 34 | } 35 | 36 | public void setName(String name) { 37 | this.name = name; 38 | } 39 | 40 | @Override 41 | public String getFtlFileName() { 42 | return "handler.ftl"; 43 | } 44 | 45 | @Override 46 | public String getDstPath(LanguageEnum language) { 47 | String path = StringUtil.join(Config.getInstance().getPath(language), File.separator, pkg, File.separator, "handler", File.separator, name); 48 | return StringUtil.join(path.replace(".", File.separator), "Handler.java"); 49 | 50 | } 51 | 52 | @Override 53 | public boolean isRewrite() { 54 | return false; 55 | } 56 | 57 | public int getId() { 58 | return id; 59 | } 60 | 61 | public void setId(int id) { 62 | this.id = id; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /tools/src/tool/message/struct/Manager.java: -------------------------------------------------------------------------------- 1 | package tool.message.struct; 2 | 3 | import java.io.File; 4 | import java.util.HashMap; 5 | 6 | import tool.ftl.IFtl; 7 | import tool.ftl.LanguageEnum; 8 | import tool.util.Config; 9 | import tool.util.StringUtil; 10 | 11 | public class Manager implements IFtl { 12 | private HashMap details = new HashMap<>(); 13 | 14 | @Override 15 | public HashMap getDataModel() { 16 | HashMap map = new HashMap<>(); 17 | map.put("details", details.values()); 18 | return map; 19 | } 20 | 21 | public HashMap getDetails() { 22 | return details; 23 | } 24 | 25 | public void setDetails(HashMap details) { 26 | this.details = details; 27 | } 28 | 29 | public void add(int id, String pkg, String name) { 30 | Detail detail = new Detail(id, pkg, name); 31 | details.put(detail.getId(), detail); 32 | } 33 | 34 | public class Detail { 35 | private int id; 36 | private String pkg; 37 | private String name; 38 | 39 | public Detail(int id, String pkg, String name) { 40 | this.id = id; 41 | this.pkg = pkg; 42 | this.name = name; 43 | } 44 | 45 | public int getId() { 46 | return id; 47 | } 48 | 49 | public void setId(int id) { 50 | this.id = id; 51 | } 52 | 53 | public String getName() { 54 | return name; 55 | } 56 | 57 | public void setName(String name) { 58 | this.name = name; 59 | } 60 | 61 | public String getPkg() { 62 | return pkg; 63 | } 64 | 65 | public void setPkg(String pkg) { 66 | this.pkg = pkg; 67 | } 68 | } 69 | 70 | @Override 71 | public String getFtlFileName() { 72 | return "manager.ftl"; 73 | } 74 | 75 | @Override 76 | public boolean isRewrite() { 77 | return true; 78 | } 79 | 80 | @Override 81 | public String getDstPath(LanguageEnum language) { 82 | String path = StringUtil.join(Config.getInstance().getPath(language), File.separator, "com.message.manager", File.separator); 83 | return StringUtil.join(path.replace(".", File.separator), "MessageManager.java"); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /tools/src/tool/message/struct/Message.java: -------------------------------------------------------------------------------- 1 | package tool.message.struct; 2 | 3 | import java.io.File; 4 | import java.util.ArrayList; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | 8 | import tool.ftl.IFtl; 9 | import tool.ftl.LanguageEnum; 10 | import tool.util.Config; 11 | import tool.util.StringUtil; 12 | 13 | public class Message implements IFtl { 14 | private String pkg; 15 | private String note; 16 | private String name; 17 | private int id; 18 | private List fields = new ArrayList<>(); 19 | 20 | @Override 21 | public HashMap getDataModel() { 22 | HashMap map = new HashMap<>(); 23 | map.put("pkg", pkg); 24 | map.put("note", note); 25 | map.put("name", name); 26 | map.put("fields", fields); 27 | map.put("id", id); 28 | return map; 29 | } 30 | 31 | public String getPkg() { 32 | return pkg; 33 | } 34 | 35 | public void setPkg(String pkg) { 36 | this.pkg = pkg; 37 | } 38 | 39 | public String getNote() { 40 | return note; 41 | } 42 | 43 | public void setNote(String note) { 44 | this.note = note; 45 | } 46 | 47 | public String getName() { 48 | return name; 49 | } 50 | 51 | public void setName(String name) { 52 | this.name = name; 53 | } 54 | 55 | public List getFields() { 56 | return fields; 57 | } 58 | 59 | public void setFields(List fields) { 60 | this.fields = fields; 61 | } 62 | 63 | public int getId() { 64 | return id; 65 | } 66 | 67 | public void setId(int id) { 68 | this.id = id; 69 | } 70 | 71 | @Override 72 | public String getFtlFileName() { 73 | return "message.ftl"; 74 | } 75 | 76 | @Override 77 | public String getDstPath(LanguageEnum language) {String path = StringUtil.join(Config.getInstance().getPath(language), File.separator, pkg, File.separator, "message", File.separator, name); 78 | return StringUtil.join(path.replace(".", File.separator), "Message.java");} 79 | 80 | @Override 81 | public boolean isRewrite() { 82 | return true; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /tools/src/tool/util/StringUtil.java: -------------------------------------------------------------------------------- 1 | package tool.util; 2 | 3 | public class StringUtil { 4 | public static String join(String... strs) { 5 | StringBuilder builder = new StringBuilder(); 6 | for (String str : strs) { 7 | builder.append(str); 8 | } 9 | return builder.toString(); 10 | } 11 | 12 | public static String upFirstChar(String str) { 13 | return new StringBuilder().append(str.substring(0, 1).toUpperCase()).append(str.substring(1)).toString(); 14 | } 15 | 16 | public static String lowFirstChar(String str) { 17 | return new StringBuilder().append(str.substring(0, 1).toLowerCase()).append(str.substring(1)).toString(); 18 | } 19 | } 20 | --------------------------------------------------------------------------------