{"id":28399937,"url":"https://github.com/fish-tennis/gentity","last_synced_at":"2026-05-09T13:27:21.992Z","repository":{"id":59044253,"uuid":"527804682","full_name":"fish-tennis/gentity","owner":"fish-tennis","description":"Data Mapping for database(such as mongodb) and cache(such as redis),with Entity-Component pattern","archived":false,"fork":false,"pushed_at":"2024-10-14T07:48:47.000Z","size":1707,"stargazers_count":4,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2026-04-12T07:38:02.908Z","etag":null,"topics":["game","mongodb","protobuf","redis","server"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fish-tennis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-08-23T02:30:48.000Z","updated_at":"2024-12-25T06:56:18.000Z","dependencies_parsed_at":"2024-04-26T09:43:28.244Z","dependency_job_id":"b05ad2dd-134a-4ea9-8913-48a0eefa6fca","html_url":"https://github.com/fish-tennis/gentity","commit_stats":null,"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"purl":"pkg:github/fish-tennis/gentity","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fish-tennis%2Fgentity","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fish-tennis%2Fgentity/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fish-tennis%2Fgentity/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fish-tennis%2Fgentity/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fish-tennis","download_url":"https://codeload.github.com/fish-tennis/gentity/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fish-tennis%2Fgentity/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31707953,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-12T06:22:27.080Z","status":"ssl_error","status_checked_at":"2026-04-12T06:21:52.710Z","response_time":58,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["game","mongodb","protobuf","redis","server"],"created_at":"2025-06-01T08:11:45.652Z","updated_at":"2026-04-12T07:38:27.281Z","avatar_url":"https://github.com/fish-tennis.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gentity\n实体对象的数据绑定(类似gorm),数据读写(序列化),缓存等接口,通过配置即可实现对象的数据保存加载,缓存更新,以简化业务编码,并使业务代码和数据库\u0026缓存解耦\n\n基于gentity,游戏服务器框架可以更快的构建\n\n![gentity](https://github.com/fish-tennis/doc/blob/master/imgs/gentity/gentity.png)\n\n## Entity-Component\nEntity-Component模式是类似Unity的GameObject-Component的实体组件模式,便于组件解耦\n\n比如游戏服务器中的玩家对象就属于Entity,玩家的任务模块就是一个Component\n\n- 组件模块注册\n- 组件消息回调接口注册\n- 组件事件分发\n- 组件事件响应接口注册\n\n## 数据库和缓存\n实体数据的加载和保存是游戏服务器最基础的功能,gentity利用go的struct tag,大大简化了实体数据加载和保存的接口\n\ngentity抽象出了实体的数据库接口EntityDb和实体的缓存接口KvCache,并且可以自动检查增量更新,只把修改过的数据保存到数据库和缓存\n\ngentity内置了EntityDb的mongodb实现,和KvCache的redis实现\n\n## 数据绑定\n类似gorm(go Object Relation Mapping)对SQL进行对象映射,gentity的数据绑定对组件进行数据库和缓存的映射\n\n利用go的struct tag,设置对象组件的字段,框架接口会自动对这些字段进行数据库读取保存和缓存更新,极大的简化了业务代码对数据库和缓存的操作\n\n设置组件保存数据\n```go\n// entity的一个组件\ntype Money struct {\n    DataComponent\n    // 该字段必须导出(首字母大写)\n    // 使用struct tag来标记该字段需要存数据库\n    Data *pb.Money `db:\"\"`\n}\n```\n\n支持明文方式保存数据\n```go\n// 玩家基础信息组件\ntype BaseInfo struct {\n    DataComponent\n    // plain表示明文存储,在保存到mongodb时,不会进行proto序列化\n    Data *pb.BaseInfo `db:\"plain\"`\n}\n```\n\n支持多个保存字段\n```go\n// 任务组件\ntype Quest struct {\n    BaseComponent\n    // 保存数据的子模块:已完成的任务 使用明文保存方式\n    // wrapper of []int32\n    Finished *gentity.SliceData[int32] `child:\"Finished;plain\"`\n    // 保存数据的子模块:当前任务列表\n    // wrapper of map[int32]*pb.QuestData\n    Quests *gentity.MapData[int32, *pb.QuestData] `child:\"Quests\"`\n}\n```\n\n## 消息回调\n支持自动注册消息回调,事件响应\n```go\n// 客户端发给服务器的完成任务的消息回调\n// 这种格式写的函数可以自动注册客户端消息回调\nfunc (this *Quest) OnFinishQuestReq(reqCmd gnet.PacketCommand, req *pb.FinishQuestReq) {\n\t// logic code ...\n}\n```\n```go\n// 这种格式写的函数可以自动注册非客户端的消息回调\nfunc (this *BaseInfo) HandlePlayerEntryGameOk(cmd gnet.PacketCommand, msg *pb.PlayerEntryGameOk) { \n\t// logic code ...\n}\n```\n```go\n// 这种格式写的函数可以自动注册事件响应接口\n// 当执行player.FireEvent(\u0026EventPlayerEntryGame{})时,该响应接口会被调用\nfunc (this *Quest) TriggerPlayerEntryGame(event *EventPlayerEntryGame) {\n\t// logic code ...\n}\n```\n\n## 独立协程实体RoutineEntity\n每个RoutineEntity分配一个独立的逻辑协程,在自己的独立协程中执行只涉及自身数据的代码,无需加锁\n\n同时,RoutineEntity内置了一个协程安全的计时器\n\n![routine entity](https://github.com/fish-tennis/doc/blob/master/imgs/gentity/routineentity.png)\n\n示例:[gserver](https://github.com/fish-tennis/gserver) 里的玩家对象Player\n\n## 分布式实体DistributedEntity\n分布式实体DistributedEntity在RoutineEntity的基础上增加了数据库加载接口,分布式锁接口\n\n示例:[gserver](https://github.com/fish-tennis/gserver) 里的公会对象Guild\n\n![distributed entity](https://github.com/fish-tennis/doc/blob/master/imgs/gentity/distributedentity.png)\n\n## 项目演示\n分布式游戏服务器框架[gserver](https://github.com/fish-tennis/gserver)\n\n## 讨论\nQQ群: 764912827\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffish-tennis%2Fgentity","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffish-tennis%2Fgentity","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffish-tennis%2Fgentity/lists"}