Giter Club home page Giter Club logo

Comments (1)

iohao avatar iohao commented on July 17, 2024

使用文档 https://www.yuque.com/iohao/game/ieimzn#Aqc1C

action 支持 List 参数与返回值,可以有效的减少协议碎片、减少工作量等。在没有支持 List 之前的代码,如果想要传输一个列表的数据,通常需要将 pb 对象包装到另一个 pb 响应对象中。

让我们先看一个示例,这个示例中 action 方法的的逻辑很简单,将查询到的数据列表给到请求端。由于之前不支持 List 返回值,开发者想要将列表中的数据给到请求端,还需要额外的定义一个与之对应的响应类,只有这样才能将列表数据给到请求端。

我们可以想象一下,如果你的系统中有很多固定的配置数据,比如装备、道具、活动信息、英雄人物介绍、敌人相关信息、地图信息、技能信息、宠物基本信息...等等,通常会有几十、上百个这样的响应对象。

为了将这些固定的配置数据给到请求端,而建立与之对应的响应对象,想想这是一件多么无聊的一件事情。这些多出来的响应对象,就是协议碎片,是一种可有可无的协议;此外还有如下缺点:

  • 将会变成干扰项
  • 增加维护成本
  • 增加工作量(每次有新的配置表都要新建、在每个 action 中,都要创建这个响应对象)

不使用 List 时的写法

@ProtobufClass
@FieldDefaults(level = AccessLevel.PUBLIC)
public class Animal {
    /** id */
    int id;
}

@ProtobufClass
@FieldDefaults(level = AccessLevel.PUBLIC)
public class AnimalResponse {
    List<Animal> animals;
}

@ActionController(3)
public class HallAction {
    @ActionMethod(10)
    public AnimalResponse listAnimal1() {
        // 查询出列表
        var list = IntStream.range(1, 4).mapToObj(id -> {
            Animal animal = new Animal();
            animal.id = id;
            return animal;
        }).collect(Collectors.toList());

        // 将列表存放到 Animal 的响应对象中
        AnimalResponse animalResponse = new AnimalResponse();
        animalResponse.animals = list;

        return animalResponse;
    }
}

通过上面的介绍,知道协议碎片是多么恐怖的一件事了把。其实我们的需求也很简单,只是想把列表中的数据给到请求端就可以了。此时,我们可以利用 action 将列表数据通过 List 直接返回,这样可以避免上面所说的各种缺点。同时,还可以让我们的代码更加的简洁,这种方式可以使前端与后端都受益。

用更少的代码实现了同样的功能,减少了工作量,避免了协议碎片。这样,开发者就不在需要额外的建立一个与之对应的响应协议了;当使用了框架提供的 List 返回值后,可以帮助你的系统减少几十、上百个类似 xxxResponse 的协议。

来,让我们看看修改后的代码是有多么简洁的吧。这种编码方式,即使你是一个新手,也能快速的看懂;

Action 使用 List 返回数据

@ActionController(3)
public class HallAction {
    @ActionMethod(9)
    public List<Animal> listAnimal() {
        // 查询出列表
        return IntStream.range(1, 4).mapToObj(id -> {
            Animal animal = new Animal();
            animal.id = id;
            return animal;
        }).collect(Collectors.toList());
    }
}

原理
ioGame 在游戏对外服的协议中,提供了一个公共的 message,用于存放 List 数据。

// pb 对象 list 包装类
message ByteValueList {
  // pb 对象 List、pb 对象 Array
  repeated bytes values = 1;
}

游戏对外服协议请看:https://www.yuque.com/iohao/game/zfg3ci

from iogame.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.