{"id":19614813,"url":"https://github.com/leancloud/multiplayer-turn-based-game-demo","last_synced_at":"2026-03-12T19:15:20.823Z","repository":{"id":44115512,"uuid":"178122219","full_name":"leancloud/multiplayer-turn-based-game-demo","owner":"leancloud","description":"回合制对战游戏示例","archived":false,"fork":false,"pushed_at":"2023-01-23T22:26:22.000Z","size":2658,"stargazers_count":16,"open_issues_count":14,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-10T19:17:15.632Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://turn-game.leanapp.cn","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leancloud.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-28T03:50:22.000Z","updated_at":"2025-02-06T02:51:02.000Z","dependencies_parsed_at":"2023-01-25T22:46:00.484Z","dependency_job_id":null,"html_url":"https://github.com/leancloud/multiplayer-turn-based-game-demo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/leancloud/multiplayer-turn-based-game-demo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2Fmultiplayer-turn-based-game-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2Fmultiplayer-turn-based-game-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2Fmultiplayer-turn-based-game-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2Fmultiplayer-turn-based-game-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leancloud","download_url":"https://codeload.github.com/leancloud/multiplayer-turn-based-game-demo/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2Fmultiplayer-turn-based-game-demo/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30439658,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-12T14:34:45.044Z","status":"ssl_error","status_checked_at":"2026-03-12T14:09:33.793Z","response_time":114,"last_error":"SSL_read: 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":[],"created_at":"2024-11-11T10:53:51.797Z","updated_at":"2026-03-12T19:15:20.808Z","avatar_url":"https://github.com/leancloud.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 回合制在线对战 Demo\n\n这款 Demo 是用 LeanCloud [实时对战](https://leancloud.cn/docs/multiplayer-guide-js.html)和 [Client Engine](https://leancloud.cn/docs/client-engine.html) 实现的一款回合制对战游戏，全部服务端及客户端的代码总共花费了约 7 天的时间。试玩链接：https://turn-game.leanapp.cn\n\n主要界面如下：\n\n\u003cimg src=\"https://user-images.githubusercontent.com/10130959/55139395-a51f7500-5170-11e9-9eb8-26d082e42d66.png\" width=\"400\" /\u003e\n\n## 启动游戏\n### 服务端\n\n登录 LeanCloud 控制台，进入「游戏」-「Client Engine」-「部署」，选择「开始试用」。\n\n安装 LeanCloud [命令行工具](https://leancloud.cn/docs/leanengine_cli.html#hash1443149115)。\n\n```sh\ncd turn-based-game-client-engine\nlean login\nlean switch\n```\n分组时选择 `_client-engine` 分组。\n\n```sh\nnpm install\nDEBUG=ClientEngine*,RPS*,Play lean up\n```\n\n### 客户端\n客户端代码位于 `./turn-based-game-client/assets` ，找到 `Global.ts` 文件，修改其中的 APP_ID 和 APP_KEY 和服务端选择的应用相同，`roomRequestUrl` 使用 `localhost:3000`。\n\n安装依赖：\n\n```sh\nnpm install\n```\n\n依赖安装完成后，使用 Cocos Creator 运行 `./turn-based-game-client` 中的项目。\n\n\n## 实现方式\n\n客户端之间用实时对战云来通讯。MasterClient 在 Client Engine 中控制房间内的逻辑。在游戏过程中，客户端将全部的事件发送给 MasterClient，由 MasterClient 在服务端执行相关逻辑后，再通过自定义事件指示客户端播放相关动画。\n\n具体流程如下：\n\n* 客户端请求 Client Engine 获得房间名称、加入房间。\n* 房间人满后，位于 Client Engine 中的 MasterClient 为两个客户端分配人物属性。\n* MasterClient 向房间内广播「游戏开始」事件，客户端收到事件后加载 Room 场景。\n* 客户端做出选择，并将选项发送给 MasterClient。\n* MasterClient 收到两方客户端的选项后，计算英雄血值，并广播「开始本轮对战」事件。\n* 客户端播放对战动画，播放完成后根据服务端的计算结果校准 UI。\n* 重复每轮对战，直到某一名英雄血值为 0。\n\n### 服务端代码\n\n这里的服务端代码指的是 Client Engine 中的游戏逻辑代码，位于 `./turn-based-game-client-engine/src`。\n\n```\n├── configs.ts        // 配置文件\n├── index.ts          // 项目入口\n├── reception.ts      // Reception 类实现文件，GameManager 的子类，负责管理 Game，在这个文件中撰写了创建 Game 的自定义方法\n└── turn-based-game.ts       // 实现了 TurnBasedGame 类，用于控制房间内的具体逻辑。\n```\n\n服务端的代码沿用了 LeanCloud Client Engine [示例项目](https://github.com/leancloud/client-engine-nodejs-getting-started)中的代码，仅修改了示例项目的 `rps-game.ts` 为 `turn-based-game.ts`，在 `turn-based-game.ts` 文件中撰写了自己的游戏逻辑。\n\n关于如何使用 Client Engine 开发游戏逻辑，请参考 LeanCloud 官方文档：[《你的第一个 Client Engine 小游戏》](https://leancloud.cn/docs/client-engine-first-game-node.html)\n\n### 客户端代码\n\n客户端位于 `./turn-based-game-client/assets`。\n\n```\n├── Global.ts        // 一些全局的配置或变量\n├── Events.ts        // 接收所有通过 LeanCloud 实时对战云发送的自定义事件。\n├── Hero.ts          // 在这里撰写英雄的属性及自定义方法\n├── Login.ts      // 登录场景文件\n├── Lobby.ts      // 大厅场景文件\n└── Room.ts       // 房间场景文件\n```\n\n## 主要功能实现介绍\n\n### 快速开始\n随便找一个有空位的房间快速开始。服务端代码位于 `index.ts` 文件，详情请参考 LeanCloud 文档[快速开始](https://leancloud.cn/docs/client-engine-first-game-node.html#hash-1870869335)。\n\n客户端代码位于 `Lobby.ts`，详细介绍请参考[入口 API：快速开始](https://leancloud.cn/docs/client-engine-first-game-node.html#hash-65842943)\n\n### 设置人物属性\n当房间人满后，MasterClient 会设置位于房间内的人物属性，包括血值、攻击力、防御力、治疗能力、速度等，此段逻辑位于服务端 `turn-based-game.ts` 文件中。设置人物属性的详细介绍请参考 LeanCloud 文档[玩家自定义属性](https://leancloud.cn/docs/multiplayer-guide-js.html#hash700221845)。\n\n### 房间内对战\n房间内对战主要依靠[自定义事件](https://leancloud.cn/docs/multiplayer-guide-js.html#hash1368192228)进行通信，主要有这些自定义事件：\n* MasterClient 广播事件，位于服务端代码 `turn-based-game.ts` 文件中。\n  * 游戏开始（`game-start`）\n  * 游戏结束（`game-over`）\n  * 开始播放动画（`begin-round-anim`）\n* 客户端事件，位于客户端代码 `Room.ts` 及 `Events.ts` 文件中。\n  * 发送选项给 MasterClient（`action`）\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleancloud%2Fmultiplayer-turn-based-game-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleancloud%2Fmultiplayer-turn-based-game-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleancloud%2Fmultiplayer-turn-based-game-demo/lists"}