Our review
This guide explains how to create server protocols (Request-Response and Event types) for the moorestech game, using MessagePack for serialization.
Strengths
- Clear step-by-step procedure with complete code examples
- Covers both Request-Response and Event protocols
- Includes registration in the protocol registry and test creation
- Uses MessagePack serialization for efficiency
Limitations
- Specific to the moorestech game architecture and Unity
- Requires prior knowledge of MessagePack and dependency injection services
- Does not cover low-level protocol details like TCP/UDP transport
When you need to add new client-server communication in the moorestech project
For purely local code changes with no network exchange or for projects not using the moorestech framework
Security analysis
SafeThis skill provides guidance for implementing server communication protocols in a Unity game project. It contains no destructive commands, no data exfiltration, no obfuscated payloads, and no disabling of safety features. The compilation steps are standard development tasks.
No concerns found
Examples
Create a new request-response protocol to fetch a player's inventory. The request should include the player ID, and the response should return a list of item IDs and quantities.Add an event packet that notifies all clients when a player dies. The event should include the player ID and the cause of death.I need to implement a new protocol between client and server. It should be a request-response type that sends a command to the server and returns the result. Use the protocol tag 'va:myCustomAction'.name: creating-server-protocol description: | moorestechサーバーのプロトコル(Request-Response型・Event型)を作成するためのガイド。 Use when:
- 新しいサーバープロトコル(IPacketResponse)を実装する時
- 新しいイベントパケット(EventPacket)を実装する時
- 「プロトコルを作って」「パケットを追加して」と依頼された時
- クライアント-サーバー間の通信を新規追加する時
サーバープロトコル作成ガイド
プロトコル選択
| 条件 | 種類 | |------|------| | クライアントが明示的に情報を要求/操作を実行 | Request-Response型 | | サーバー側の状態変化をクライアントに通知 | Event型 |
詳細パターンとコード例: references/protocol-patterns.md を参照。
Request-Response型の作成手順
Step 1: プロトコルクラスを作成
moorestech_server/Assets/Scripts/Server.Protocol/PacketResponse/ に新規ファイルを作成。
using System;
using System.Collections.Generic;
using MessagePack;
using Server.Protocol;
namespace Server.Protocol.PacketResponse
{
public class YourProtocol : IPacketResponse
{
public const string ProtocolTag = "va:yourProtocol";
private readonly ISomeDependency _dependency;
public YourProtocol(ServiceProvider serviceProvider)
{
_dependency = serviceProvider.GetService<ISomeDependency>();
}
public ProtocolMessagePackBase GetResponse(byte[] payload)
{
var data = MessagePackSerializer.Deserialize<YourRequestMessagePack>(payload);
// ビジネスロジック
return new YourResponseMessagePack(/* result */);
}
#region MessagePack
[MessagePackObject]
public class YourRequestMessagePack : ProtocolMessagePackBase
{
// Key(0)=Tag, Key(1)=SequenceId は基底クラスで予約済み
[Key(2)] public int SomeField { get; set; }
[Obsolete("デシリアライズ用のコンストラクタです。基本的に使用しないでください。")]
public YourRequestMessagePack() { }
public YourRequestMessagePack(int someField)
{
Tag = ProtocolTag;
SomeField = someField;
}
}
[MessagePackObject]
public class YourResponseMessagePack : ProtocolMessagePackBase
{
[Key(2)] public string ResultData { get; set; }
[Obsolete("デシリアライズ用のコンストラクタです。基本的に使用しないでください。")]
public YourResponseMessagePack() { }
public YourResponseMessagePack(string resultData)
{
Tag = ProtocolTag;
ResultData = resultData;
}
}
#endregion
}
}
Step 2: PacketResponseCreatorに登録
moorestech_server/Assets/Scripts/Server.Protocol/PacketResponseCreator.cs のコンストラクタに追加:
_packetResponseDictionary.Add(YourProtocol.ProtocolTag, new YourProtocol(serviceProvider));
Step 3: テストを作成
/creating-server-tests スキルを使用してテストを作成。配置先: Tests/CombinedTest/Server/PacketTest/
Step 4: コンパイル確認
MCPツールまたはunity-test.shでコンパイルを確認。
Event型の作成手順
Step 1: イベントパケットクラスを作成
moorestech_server/Assets/Scripts/Server.Event/EventReceive/ に新規ファイルを作成。
using System;
using Game.Context;
using MessagePack;
using Server.Event;
namespace Server.Event.EventReceive
{
public class YourEventPacket
{
public const string EventTag = "va:event:yourEvent";
private readonly EventProtocolProvider _eventProtocolProvider;
public YourEventPacket(EventProtocolProvider eventProtocolProvider)
{
_eventProtocolProvider = eventProtocolProvider;
// ゲームイベントを購読(UniRx .Subscribe)
ServerContext.SomeEvent.OnSomething.Subscribe(OnSomething);
}
private void OnSomething(SomeEventData eventData)
{
var messagePack = new YourEventMessagePack(eventData.Value);
var payload = MessagePackSerializer.Serialize(messagePack);
// 全プレイヤー: AddBroadcastEvent / 特定プレイヤー: AddEvent(playerId, ...)
_eventProtocolProvider.AddBroadcastEvent(EventTag, payload);
}
#region MessagePack
[MessagePackObject]
public class YourEventMessagePack
{
// EventのMessagePackはProtocolMessagePackBaseを継承しない。Key(0)から開始
[Key(0)] public string Value { get; set; }
[Obsolete("デシリアライズ用のコンストラクタです。基本的に使用しないでください。")]
public YourEventMessagePack() { }
public YourEventMessagePack(string value)
{
Value = value;
}
}
#endregion
}
}
Step 2: イベントパケットを初期化
イベントを発火する責任を持つシステムのコンストラクタでインスタンス化し、フィールドに保持する(GC防止)。
例: ブロック配置イベント → BlockUpdateSystemで初期化、レールノード作成イベント → RailGraphDatastore関連で初期化。
Step 3: テストを作成
/creating-server-tests スキルを使用。配置先: Tests/CombinedTest/Server/PacketTest/Event/
Step 4: コンパイル確認
MCPツールまたはunity-test.shでコンパイルを確認。
注意事項
- タグの一意性を必ず確認する(既存タグと重複しないこと)
- MessagePackのKey番号: Request/ResponseはKey(2)から、EventデータはKey(0)から
[Obsolete]付き引数なしコンストラクタは省略不可- イベント購読はUniRxの
.Subscribe()を使用(/csharp-event-pattern参照) - コードのコメントは日本語・英語の2行セット
Next.js App Router Expert
Development
A skill that turns Claude into a Next.js App Router expert.
README Generator
Development
Creates professional and comprehensive README.md files for your projects.
API Documentation Writer
Development
Generates comprehensive API documentation in OpenAPI/Swagger format.