Skip to content

Commit

Permalink
🐳 #290 [对接文档] - 新增广播文档构建器,简化生成广播对接文档
Browse files Browse the repository at this point in the history
Banner News 补充
  • Loading branch information
iohao committed May 18, 2024
1 parent 26f071b commit dac7029
Show file tree
Hide file tree
Showing 18 changed files with 240 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
*/
package com.iohao.game.action.skeleton.core;

import com.iohao.game.action.skeleton.core.doc.ActionSendDoc;
import com.iohao.game.action.skeleton.core.doc.ActionSendDocs;
import com.iohao.game.action.skeleton.core.doc.BarSkeletonDoc;
import com.iohao.game.action.skeleton.core.doc.ErrorCodeDocs;
import com.iohao.game.action.skeleton.core.doc.*;
import com.iohao.game.action.skeleton.core.exception.MsgExceptionInfo;
import com.iohao.game.action.skeleton.core.flow.*;
import com.iohao.game.action.skeleton.core.flow.internal.*;
Expand Down Expand Up @@ -176,6 +173,10 @@ public BarSkeletonBuilder addActionSendDoc(ActionSendDoc actionSendDoc) {
return this;
}

public BarSkeletonBuilder addBroadcastDoc(BroadcastDocBuilder broadcastDocBuilder) {
return this.addActionSendDoc(broadcastDocBuilder.build());
}

public BarSkeletonBuilder addHandler(Handler handler) {
Objects.requireNonNull(handler);
// 先进先执行
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@
@Data
@Accessors(chain = true)
@FieldDefaults(level = AccessLevel.PRIVATE)
public class ActionSendDoc {
public final class ActionSendDoc {
/** 主路由 */
final int cmd;
/** 子路由 */
final int subCmd;
/** 业务类型 */
final Class<?> dataClass;
Class<?> dataClass;
/** 推送描述 */
final String description;
String description;
/** true 已经被读取过一次或以上 */
boolean read;

String dataClassName;

public ActionSendDoc(DocActionSend docActionSend) {
this(docActionSend.cmd(), docActionSend.subCmd(), docActionSend.dataClass(), docActionSend.description());
}
Expand All @@ -53,20 +55,15 @@ public ActionSendDoc(CmdInfo cmdInfo, Class<?> dataClass, String description) {
this(cmdInfo.getCmd(), cmdInfo.getSubCmd(), dataClass, description);
}

public ActionSendDoc(CmdInfo cmdInfo, String description) {
this(cmdInfo.getCmd(), cmdInfo.getSubCmd(), null, description);
}

public ActionSendDoc(int cmd, int subCmd, Class<?> dataClass, String description) {
this.cmd = cmd;
this.subCmd = subCmd;
this.dataClass = dataClass;
this.description = description;
}

public String getDataClassName() {
return Optional.ofNullable(this.dataClass)
.map(Class::getName)
.orElse("none");
public ActionSendDoc(CmdInfo cmdInfo) {
this.cmd = cmdInfo.getCmd();
this.subCmd = cmdInfo.getSubCmd();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* ioGame
* Copyright (C) 2021 - present 渔民小镇 ([email protected][email protected]) . All Rights Reserved.
* # iohao.com . 渔民小镇
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.iohao.game.action.skeleton.core.doc;

import com.iohao.game.action.skeleton.core.CmdInfo;

/**
* @author 渔民小镇
* @date 2024-05-18
* @since 21.8
*/
public interface BroadcastDoc {
static BroadcastDocBuilder newBuilder(CmdInfo cmdInfo) {
return new BroadcastDocBuilder(cmdInfo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* ioGame
* Copyright (C) 2021 - present 渔民小镇 ([email protected][email protected]) . All Rights Reserved.
* # iohao.com . 渔民小镇
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.iohao.game.action.skeleton.core.doc;

import com.iohao.game.action.skeleton.core.CmdInfo;
import com.iohao.game.action.skeleton.protocol.wrapper.ByteValueList;
import com.iohao.game.action.skeleton.protocol.wrapper.WrapperKit;
import lombok.AccessLevel;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.experimental.FieldDefaults;

import java.util.Objects;
import java.util.Optional;

/**
* 广播文档构建器
*
* @author 渔民小镇
* @date 2024-05-18
* @since 21.8
*/
@Setter
@Accessors(chain = true)
@FieldDefaults(level = AccessLevel.PACKAGE)
public final class BroadcastDocBuilder {
/** 路由 */
final CmdInfo cmdInfo;
/** 广播(推送)描述 */
String description;
@Setter(AccessLevel.PRIVATE)
String dataClassName;

BroadcastDocBuilder(CmdInfo cmdInfo) {
this.cmdInfo = cmdInfo;
}

/**
* set 推送的数据类型 List dataClass,ByteValueList dataClass。
*
* @param dataClass 数据类型
* @return this
*/
public BroadcastDocBuilder setDataClassList(Class<?> dataClass) {
String simpleName = ByteValueList.class.getSimpleName();
String simpleNameActualClazz = dataClass.getSimpleName();
dataClassName = String.format("%s<%s>", simpleName, simpleNameActualClazz);
return this;
}

/**
* set 推送的数据类型
*
* @param dataClass 推送的数据类型
* @return this
*/
public BroadcastDocBuilder setDataClass(Class<?> dataClass) {

dataClassName = WrapperKit.optionalRefType(dataClass)
.map(Class::getSimpleName)
.orElse(dataClass.getSimpleName());

return this;
}

/**
* 构建广播文档
*
* @return 广播文档
*/
public ActionSendDoc build() {

Objects.requireNonNull(this.description);

this.dataClassName = Optional.ofNullable(this.dataClassName).orElse("none");

ActionSendDoc actionSendDoc = new ActionSendDoc(this.cmdInfo);
actionSendDoc.setDescription(this.description);
actionSendDoc.setDataClassName(this.dataClassName);

return actionSendDoc;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ String render() {
return "";
}

String separator = System.getProperty("line.separator");
String separator = System.lineSeparator();

List<String> lineList = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
*
* 日志输出预览
*
* ┏━━━━━ Debug. [(ActivityAction.java:1).hello] ━━━━━ [cmd:1-0 65536] ━━━━━ [逻辑服 [xxx逻辑服] - id:[76526c134cc88232379167be83e4ddfc]]
* ┏━━━━━ Debug. [(ActivityAction.java:1).hello] ━━━━━ [cmd:1-0 65536] ━━━━━ [xxx逻辑服] ━━━━━ [id:76526c134cc88232379167be83e4ddfc]
* ┣ userId: 1
* ┣ 参数: active : Active(id=101, name=塔姆)
* ┣ 响应: 塔姆, I'm here
Expand Down Expand Up @@ -207,7 +207,7 @@ private void printValidate(FlowContext flowContext, Map<String, Object> paramMap
}

String template = """
┏━━错误━━━ Debug. [({className}.java:{lineNumber}).{actionMethodName}] ━━━ {cmdInfo} ━━━ [逻辑服 [{logicServerTag}] - id:[{logicServerId}]]
┏━━错误━━━ Debug. [({className}.java:{lineNumber}).{actionMethodName}] ━━━ {cmdInfo} ━━━ [{logicServerTag}] ━━━ [id:{logicServerId}]
┣ userId: {userId}
┣ 参数: {paramName} : {paramData}
┣ 错误码: {errorCode}
Expand All @@ -230,7 +230,7 @@ private void printNormal(FlowContext flowContext, Map<String, Object> paramMap)
}

String template = """
┏━━━━━ Debug. [({className}.java:{lineNumber}).{actionMethodName}] ━━━━━ {cmdInfo} ━━━━━ [逻辑服 [{logicServerTag}] - id:[{logicServerId}]]
┏━━━━━ Debug. [({className}.java:{lineNumber}).{actionMethodName}] ━━━━━ {cmdInfo} ━━━━━ [{logicServerTag}] ━━━━━ [id:{logicServerId}]
┣ userId: {userId}
┣ 参数: {paramName} : {paramData}
┣ 响应: {returnData}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ protected void disableSend() {
this.doSend = false;
}

/**
* 不检测空用户,如果没有任何用户,将不会触发广播(推送)
*
* @return me
*/
public RangeBroadcast disableEmptyUserCheck() {
this.checkEmptyUser = false;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
import com.baidu.bjf.remoting.protobuf.FieldType;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
import com.iohao.game.action.skeleton.core.DataCodecKit;
import com.iohao.game.common.kit.CollKit;
import lombok.ToString;

import java.util.Collection;
import java.util.List;

/**
Expand All @@ -39,8 +42,20 @@ public final class ByteValueList {
public List<byte[]> values;

public static ByteValueList of(List<byte[]> values) {
if (CollKit.isEmpty(values)) {
return new ByteValueList();
}

var theValue = new ByteValueList();
theValue.values = values;
return theValue;
}

public static <T> ByteValueList ofList(Collection<T> values) {
if (CollKit.isEmpty(values)) {
return new ByteValueList();
}

return of(values.stream().map(DataCodecKit::encode).toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@
*/
package com.iohao.game.action.skeleton.protocol.wrapper;

import com.iohao.game.action.skeleton.core.DataCodecKit;
import com.iohao.game.common.kit.CollKit;
import com.sun.jdi.BooleanValue;
import lombok.experimental.UtilityClass;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.*;

/**
* 装箱、拆箱包装工具
Expand Down Expand Up @@ -69,21 +64,15 @@ public StringValueList ofListStringValue(List<String> values) {
}

public <T> ByteValueList ofListByteValue(List<T> values) {
return ofList(values);
}

if (CollKit.isEmpty(values)) {
return new ByteValueList();
}

return ByteValueList.of(values.stream().map(DataCodecKit::encode).toList());
public <T> ByteValueList ofList(List<T> values) {
return ByteValueList.ofList(values);
}

public <T> ByteValueList ofListByteValue(Collection<T> values) {

if (CollKit.isEmpty(values)) {
return new ByteValueList();
}

return ByteValueList.of(values.stream().map(DataCodecKit::encode).toList());
return ByteValueList.ofList(values);
}

/** 框架支持的协议碎片类型 */
Expand Down Expand Up @@ -114,4 +103,27 @@ public <T> ByteValueList ofListByteValue(Collection<T> values) {
public boolean isWrapper(Class<?> clazz) {
return wrapperTypeSet.contains(clazz);
}

final Map<Class<?>, Class<?>> refTypeMap = new HashMap<>();

static {
refTypeMap.put(int.class, IntValue.class);
refTypeMap.put(Integer.class, IntValue.class);
refTypeMap.put(IntValue.class, IntValue.class);

refTypeMap.put(long.class, LongValue.class);
refTypeMap.put(Long.class, LongValue.class);
refTypeMap.put(LongValue.class, LongValue.class);

refTypeMap.put(boolean.class, BoolValue.class);
refTypeMap.put(Boolean.class, BoolValue.class);
refTypeMap.put(BoolValue.class, BoolValue.class);

refTypeMap.put(String.class, StringValue.class);
refTypeMap.put(StringValue.class, StringValue.class);
}

public Optional<Class<?>> optionalRefType(Class<?> clazz) {
return Optional.ofNullable(refTypeMap.get(clazz));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,5 @@ List<String> listData() {
bannerList.add(banner);

return bannerList;

}
}
Loading

0 comments on commit dac7029

Please sign in to comment.