浏览代码

wip : Sending Data to Lobby

/main/staging/2021_Upgrade/Async_Refactor
Jacob 2 年前
当前提交
a6fea812
共有 4 个文件被更改,包括 34 次插入70 次删除
  1. 15
      Assets/Scripts/GameLobby/Game/GameManager.cs
  2. 43
      Assets/Scripts/GameLobby/Game/LocalLobby.cs
  3. 41
      Assets/Scripts/GameLobby/Lobby/LobbyManager.cs
  4. 5
      Assets/Scripts/GameLobby/NGO/SetupInGame.cs

15
Assets/Scripts/GameLobby/Game/GameManager.cs


public void SetLocalUserEmote(EmoteType emote)
{
SetUserEmote(m_LocalUser.Index.Value, emote);
SendLocalUserData();
}
public void SetUserEmote(int playerID, EmoteType emote)

player.Emote.Value = emote;
}
SendLocalUserData();
}
public void SetUserStatus(int playerID, UserStatus status)

player.UserStatus.Value = status;
}
async void SendLocalLobbyData()
{
await LobbyManager.UpdateLobbyDataAsync(LobbyConverters.LocalToRemoteData(m_LocalLobby));
}
async void SendLocalUserData()
{
await LobbyManager.UpdatePlayerDataAsync(LobbyConverters.LocalToRemoteUserData(m_LocalUser));
}
public void CompleteCountDown()
{
Debug.Log("CountDown Complete!");

{
Debug.Log($"Setting Lobby user state {GameState.Lobby}");
SetGameState(GameState.Lobby);
SetUserStatus(m_LocalUser.Index.Value, UserStatus.Lobby);
SetLocalUserStatus(UserStatus.Lobby);
}
void ResetLocalLobby()

43
Assets/Scripts/GameLobby/Game/LocalLobby.cs


public bool CanSetChanged = true;
public Action<LocalPlayer> onUserJoined;
public Action<int> onUserLeft;
public Action<int> onUserLeft;
Dictionary<int, LocalPlayer> m_LocalPlayers = new Dictionary<int, LocalPlayer>();

/// <summary>Used only for visual output of the Relay connection info. The obfuscated Relay server IP is obtained during allocation in the RelayUtpSetup.</summary>
#endregion.
public CallbackValue<string> LobbyID = new CallbackValue<string>();
public CallbackValue<string> LobbyCode = new CallbackValue<string>();

LastUpdated.Value = DateTime.Now.ToFileTimeUtc();
}
/// <summary>
/// A locking mechanism for registering when something has looked at the Lobby to see if anything has changed
/// </summary>
/// <returns></returns>
public bool IsLobbyChanged()
{
bool isChanged = m_ValuesChanged;
m_ValuesChanged = false;
return isChanged;
}
void SetValueChanged()
{

Debug.Log($"Adding User: {user.DisplayName.Value} - {user.ID.Value}");
m_LocalPlayers.Add(user.Index.Value, user);
user.Emote.onChanged += EmoteChangedCallback;
user.DisplayName.onChanged += StringChangedCallback;
user.IsHost.onChanged += BoolChangedCallback;
user.UserStatus.onChanged += EmoteChangedCallback;
onUserJoined?.Invoke(user);
}

var player = m_LocalPlayers[removePlayer];
m_LocalPlayers.Remove(removePlayer);
player.Emote.onChanged -= EmoteChangedCallback;
player.DisplayName.onChanged -= StringChangedCallback;
player.IsHost.onChanged -= BoolChangedCallback;
player.UserStatus.onChanged -= EmoteChangedCallback;
void EmoteChangedCallback(EmoteType emote)
{
IsLobbyChanged();
}
void StringChangedCallback(string text)
{
IsLobbyChanged();
}
void BoolChangedCallback(bool changed)
{
IsLobbyChanged();
}
void EmoteChangedCallback(UserStatus status)
{
IsLobbyChanged();
}
public override string ToString()
{

41
Assets/Scripts/GameLobby/Lobby/LobbyManager.cs


using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using LobbyRelaySample.ngo;
using Unity.Services.Authentication;
using Unity.Services.Lobbies;
using Unity.Services.Lobbies.Models;

public Lobby CurrentLobby => m_CurrentLobby;
Lobby m_CurrentLobby;
LobbyEventCallbacks m_LobbyEventCallbacks = new LobbyEventCallbacks();
const int
k_maxLobbiesToShow = 16; // If more are necessary, consider retrieving paginated results or using filters.
const int k_maxLobbiesToShow = 16; // If more are necessary, consider retrieving paginated results or using filters.
Task m_HeartBeatTask;

{
localLobby.RemovePlayer(leftPlayerIndex);
}
throw new NotImplementedException("Need to switch to Player lists");
}
void PlayerDataChanged()

}
}
}
/// <summary>
/// Takes a lobby and a change applicator to update a given lobby in-place.
/// If LobbyDeleted is true, no changes will be applied and a warning will be logged.
/// </summary>
/// <param name="lobby">The lobby model to apply the changes to.</param>
//void ApplyToLobby(Models.Lobby lobby);
};
m_LobbyEventCallbacks.LobbyEventConnectionStateChanged += lobbyEventConnectionState =>

await LobbyService.Instance.SubscribeToLobbyEventsAsync(lobbyID, m_LobbyEventCallbacks);
}
List<QueryFilter> LobbyColorToFilters(LobbyColor limitToColor)
{
List<QueryFilter> filters = new List<QueryFilter>();
if (limitToColor == LobbyColor.Orange)
filters.Add(new QueryFilter(QueryFilter.FieldOptions.N1, ((int)LobbyColor.Orange).ToString(),
QueryFilter.OpOptions.EQ));
else if (limitToColor == LobbyColor.Green)
filters.Add(new QueryFilter(QueryFilter.FieldOptions.N1, ((int)LobbyColor.Green).ToString(),
QueryFilter.OpOptions.EQ));
else if (limitToColor == LobbyColor.Blue)
filters.Add(new QueryFilter(QueryFilter.FieldOptions.N1, ((int)LobbyColor.Blue).ToString(),
QueryFilter.OpOptions.EQ));
return filters;
}
public async Task<Lobby> GetLobbyAsync(string lobbyId = null)
{

#region HeartBeat
List<QueryFilter> LobbyColorToFilters(LobbyColor limitToColor)
{
List<QueryFilter> filters = new List<QueryFilter>();
if (limitToColor == LobbyColor.Orange)
filters.Add(new QueryFilter(QueryFilter.FieldOptions.N1, ((int)LobbyColor.Orange).ToString(),
QueryFilter.OpOptions.EQ));
else if (limitToColor == LobbyColor.Green)
filters.Add(new QueryFilter(QueryFilter.FieldOptions.N1, ((int)LobbyColor.Green).ToString(),
QueryFilter.OpOptions.EQ));
else if (limitToColor == LobbyColor.Blue)
filters.Add(new QueryFilter(QueryFilter.FieldOptions.N1, ((int)LobbyColor.Blue).ToString(),
QueryFilter.OpOptions.EQ));
return filters;
}
//Since the LobbyManager maintains the "connection" to the lobby, we will continue to heartbeat until host leaves.
async Task SendHeartbeatPingAsync()
{

5
Assets/Scripts/GameLobby/NGO/SetupInGame.cs


using System;
using System.Collections.Generic;
using System.Collections.Generic;
using LobbyRelaySample.relay;
using Unity.Netcode;
using Unity.Netcode.Transports.UTP;
using Unity.Networking.Transport;

using UnityEngine.SocialPlatforms;
namespace LobbyRelaySample.ngo
{

正在加载...
取消
保存