Tutorial 3 - Host/Search MatchMaker
最終更新
役に立ちましたか?
役に立ちましたか?
private const string HOST = "127.0.0.1:7000";
private const string CLIENT_KEY = "";
private const string UID = ""; // 空文字の場合はランダム生成private const string PROFILE_ID = "RankMatch"; // Host 側と Search 側で一致させる
private const int MAX_MEMBERS = 4;DiarkisInterface diarkis = DiarkisNetworkManager.GetDiarkisInterface(INTERFACE_NAME);
_matchMaker = diarkis.MatchMaker;DiarkisEventHandler handler = diarkis.EventHandler;
handler.OnUDPConnect(OnConnect, this);
handler.OnUDPDisconnect(OnDisconnect, this);
handler.OnUDPFail(_ => SetNetworkState("接続失敗", ColorRed), this);
handler.OnHttpError(_ => SetNetworkState("HTTP 認証エラー", ColorRed), this);
handler.OnMMHost(OnMMHost, this);
handler.OnMMResult(OnMMResult, this);
handler.OnMMJoin(OnMMJoin, this);
handler.OnMMMemberJoin(_ => RefreshMemberList(), this);
handler.OnMMMemberLeave(_ => RefreshMemberList(), this);
handler.OnMMDisband(_ => OnMMDisband(), this);
handler.OnMMLeave(_ => OnMMLeave(), this);private void OnHostClicked()
{
_matchMaker.HostMatchMaking(
maxMembers: (ushort)MAX_MEMBERS,
ttl: 60, // ルームの有効時間(秒)
profileID: PROFILE_ID,
tag: PROFILE_ID); // グルーピング用タグ。ここでは profileID と同じ値を使用
}private void OnSearchClicked()
{
var props = new Dictionary<string, uint> { { "rank", 10 } };
_matchMaker.Search(
profileID: PROFILE_ID,
tag: PROFILE_ID,
props: props, // サーバー側でフィルタされる数値条件
joinFlag: true, // true なら見つかり次第自動参加
howmany: 1,
message: "");
}private void OnMMHost(DiarkisMMHostEventArgs args)
{
if (!args.IsSuccess())
{
AddChatLine($"[エラー] ホスト失敗 (code: {args.GetErrorCode()})");
return;
}
string roomID = args.GetRoomID();
if (_statusText != null) _statusText.text = roomID;
if (_ownerUIDText != null) _ownerUIDText.text = /* 自分の UID */;
RefreshMemberList();
RefreshButtons();
}
private void OnMMJoin(DiarkisMMJoinResponseEventArgs args)
{
if (!args.IsSuccess())
{
AddChatLine($"[エラー] 参加失敗 (code: {args.GetErrorCode()})");
return;
}
// _matchMaker.GetHostUID() でホストの UID を取得できます
if (_ownerUIDText != null) _ownerUIDText.text = _matchMaker.GetHostUID();
RefreshMemberList();
RefreshButtons();
}private void RefreshMemberList()
{
List<string> members = _matchMaker?.GetMembers(DiarkisMatchMakerType.HostSearch);
_memberListText.text = members != null && members.Count > 0
? string.Join("\n", members)
: "";
}private void RefreshButtons()
{
MatchMakerHostSearchState state = _matchMaker.HostSearchState;
bool notStarted = state == MatchMakerHostSearchState.None;
bool isHost = state == MatchMakerHostSearchState.Host;
bool isInRoom = state == MatchMakerHostSearchState.MatchingRoomJoined;
bool isSearching = state == MatchMakerHostSearchState.Search;
if (_hostButton) _hostButton.interactable = notStarted && _connected;
if (_searchButton) _searchButton.interactable = notStarted && _connected;
if (_leaveButton) _leaveButton.interactable = isInRoom || isSearching;
if (_disbandButton) _disbandButton.interactable = isHost;
bool canMessage = isHost || isInRoom;
if (_messageButton1) _messageButton1.interactable = canMessage;
}