public void Setup()
        {
            // Get references to the components.
            m_Movement         = m_Instance.GetComponent <TankMovement> ();
            m_Shooting         = m_Instance.GetComponent <TankShooting> ();
            m_CanvasGameObject = m_Instance.GetComponentInChildren <Canvas> ().gameObject;

            // Set the player numbers to be consistent across the scripts.
            m_Movement.m_PlayerNumber = m_PlayerNumber;
            m_Shooting.m_PlayerNumber = m_PlayerNumber;

            // Create a string using the correct color that says 'PLAYER 1' etc based on the tank's color and the player's number.
            m_ColoredPlayerText = "<color=#" + ColorUtility.ToHtmlStringRGB(m_PlayerColor) + ">PLAYER " + m_PlayerNumber + "</color>";

            // Get all of the renderers of the tank.
            MeshRenderer[] renderers = m_Instance.GetComponentsInChildren <MeshRenderer> ();

            // Go through all the renderers...
            for (int i = 0; i < renderers.Length; i++)
            {
                // ... set their material color to the color specific to this tank.
                renderers[i].material.color = m_PlayerColor;
            }

            m_online       = m_Instance.GetComponent <OnlineIdentity>();
            m_online.m_uid = (ulong)m_PlayerNumber - 1;
            m_online.m_localPlayerAuthority = (uint)m_PlayerNumber - 1;
        }
Esempio n. 2
0
    public void Init()
    {
        m_syncedFields = GetType().GetFields(BindingFlags.NonPublic
                                             | BindingFlags.Public
                                             | BindingFlags.FlattenHierarchy
                                             | BindingFlags.Instance
                                             | BindingFlags.Static)
                         .Where(prop => Attribute.IsDefined(prop, typeof(Sync))).ToArray();

        m_rpcs = GetType().GetMethods(BindingFlags.NonPublic
                                      | BindingFlags.Public
                                      | BindingFlags.FlattenHierarchy
                                      | BindingFlags.Instance
                                      | BindingFlags.Static)
                 .Where(prop => Attribute.IsDefined(prop, typeof(RPC))).ToArray();

        m_cmds = GetType().GetMethods(BindingFlags.NonPublic
                                      | BindingFlags.Public
                                      | BindingFlags.FlattenHierarchy
                                      | BindingFlags.Instance
                                      | BindingFlags.Static)
                 .Where(prop => Attribute.IsDefined(prop, typeof(CMD))).ToArray();

        m_onlineIdentity = GetComponent <OnlineIdentity>();
        OnlineObjectManager.Instance.RegisterOnlineBehavior(this);

        //find OnlineIdentity Component
        m_ObjectReaders.Add(typeof(Vector3), ReadVector3);
        m_ObjectWriter.Add(typeof(Vector3), WriteVector3);
        m_ObjectReaders.Add(typeof(Quaternion), ReadQuaternion);
        m_ObjectWriter.Add(typeof(Quaternion), WriteQuaternion);
        m_ObjectReaders.Add(typeof(int), ReadInt);
        m_ObjectWriter.Add(typeof(int), WriteInt);
    }
Esempio n. 3
0
        public async Task <bool> DeleteOnlineIdentityAsync(OnlineIdentity onlineIdentity)
        {
            if (onlineIdentity == null)
            {
                throw new NullReferenceException("The OnlineIdentity parameter was null.");
            }

            try
            {
                using (var response = await _client.DeleteAsync($"onlineidentities?id={onlineIdentity.PrivateSiteId}"))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        return(true);
                    }

                    if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.Forbidden)
                    {
                        AccessTokenExpired?.Invoke(this, new ApiServiceEventArgs {
                            IsTokenRefreshNeeded = true
                        });
                    }
                    else if (response.StatusCode == HttpStatusCode.BadRequest)
                    {
                        var message = await response.Content.ReadAsStringAsync();

                        RequestErrorOccurred?.Invoke(this, new ApiServiceEventArgs {
                            IsBadRequest = true, Message = message
                        });
                    }
                }
            }
            catch (HttpRequestException e)
            {
                await e.LogExceptionAsync();

                if (e.Message.Contains("500"))
                {
                    RequestErrorOccurred?.Invoke(this, new ApiServiceEventArgs {
                        IsServerError = true
                    });
                }

                Debug.WriteLine($"SubmitOnlineIdentitiesAsync HttpRequestException: {e}");
            }
            catch (Exception e)
            {
                await e.LogExceptionAsync();

                Debug.WriteLine($"SubmitOnlineIdentitiesAsync Exception: {e}");
            }

            return(false);
        }
 /// <summary>
 /// Updates an existing online identity.
 /// </summary>
 /// <param name="identity">The online identity to update.</param>
 /// <param name="cancellationToken">An optional cancellation token.</param>
 /// <returns>True if the online identity is updated successfully; otherwise, false.</returns>
 public async Task <bool> UpdateOnlineIdentityAsync(
     OnlineIdentity identity,
     CancellationToken cancellationToken = default)
 {
     return(await this.PutAsync(
                OnlineIdentityEndpoint,
                identity,
                true,
                null,
                cancellationToken));
 }
 /// <summary>
 /// Creates a new online identity.
 /// </summary>
 /// <param name="identity">The online identity to add.</param>
 /// <param name="cancellationToken">An optional cancellation token.</param>
 /// <returns>The created online identity item.</returns>
 public async Task <OnlineIdentity> AddOnlineIdentityAsync(
     OnlineIdentity identity,
     CancellationToken cancellationToken = default)
 {
     return(await this.PostAsync <OnlineIdentity>(
                OnlineIdentityEndpoint,
                identity,
                true,
                null,
                cancellationToken));
 }
Esempio n. 6
0
    public bool NeedUpdateFields()
    {
        if (m_onlineIdentity == null)
        {
            m_onlineIdentity = GetComponent <OnlineIdentity>();
        }

        if (m_onlineIdentity == null)
        {
            return(false);
        }

        if (m_onlineIdentity.HasAuthority())
        {
            if (m_syncedFields.Count() > 0)
            {
                return(NeedSync());
            }
        }
        return(false);
    }
Esempio n. 7
0
        /// <summary>
        /// Saves an OnlineIdentity
        /// </summary>
        /// <param name="onlineIdentity"></param>
        /// <returns></returns>
        public async Task <OnlineIdentity> SubmitOnlineIdentityAsync(OnlineIdentity onlineIdentity)
        {
            if (onlineIdentity == null)
            {
                throw new NullReferenceException($"The {nameof(onlineIdentity)} parameter was null.");
            }

            try
            {
                return(await api.AddOnlineIdentity(onlineIdentity));
            }
            catch (ApiException e)
            {
                HandleApiException(e);
                return(null);
            }
            catch (Exception e)
            {
                analyticsService.Report(e);
                return(null);
            }
        }
Esempio n. 8
0
        public async Task <bool> DeleteOnlineIdentityAsync(OnlineIdentity onlineIdentity)
        {
            if (onlineIdentity == null)
            {
                throw new NullReferenceException($"The {nameof(onlineIdentity)} parameter was null.");
            }

            try
            {
                await api.DeleteOnlineIdentity(onlineIdentity.PrivateSiteId ?? 0);

                return(true);
            }
            catch (ApiException e)
            {
                HandleApiException(e);
                return(false);
            }
            catch (Exception e)
            {
                analyticsService.Report(e);
                return(false);
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Saves an OnlineIdentity
        /// </summary>
        /// <param name="onlineIdentity"></param>
        /// <returns></returns>
        public async Task <OnlineIdentity> SubmitOnlineIdentityAsync(OnlineIdentity onlineIdentity)
        {
            if (onlineIdentity == null)
            {
                throw new NullReferenceException("The OnlineIdentity parameter was null.");
            }

            try
            {
                var    serializedOnlineIdentity = JsonConvert.SerializeObject(onlineIdentity);
                byte[] byteData = Encoding.UTF8.GetBytes(serializedOnlineIdentity);

                using (var content = new ByteArrayContent(byteData))
                {
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    using (var response = await _client.PostAsync("onlineidentities?", content))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            var json = await response.Content.ReadAsStringAsync();

                            Debug.WriteLine($"OnlineIdentity Save JSON: {json}");

                            var result = JsonConvert.DeserializeObject <OnlineIdentity>(json);
                            Debug.WriteLine($"OnlineIdentity Save Result: ID {result.PrivateSiteId}");

                            return(result);
                        }

                        if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.Forbidden)
                        {
                            AccessTokenExpired?.Invoke(this, new ApiServiceEventArgs {
                                IsTokenRefreshNeeded = true
                            });
                        }
                        else if (response.StatusCode == HttpStatusCode.BadRequest)
                        {
                            var message = await response.Content.ReadAsStringAsync();

                            RequestErrorOccurred?.Invoke(this, new ApiServiceEventArgs {
                                IsBadRequest = true, Message = message
                            });
                        }
                    }
                }
            }
            catch (HttpRequestException e)
            {
                await e.LogExceptionAsync();

                if (e.Message.Contains("500"))
                {
                    RequestErrorOccurred?.Invoke(this, new ApiServiceEventArgs {
                        IsServerError = true
                    });
                }

                Debug.WriteLine($"SubmitOnlineIdentitiesAsync HttpRequestException: {e}");
            }
            catch (Exception e)
            {
                await e.LogExceptionAsync();

                Debug.WriteLine($"SubmitOnlineIdentitiesAsync Exception: {e}");
            }

            return(null);
        }
Esempio n. 10
0
 public async Task <bool> UpdateOnlineIdentityAsync(OnlineIdentity identity, CancellationTokenSource cts = null)
 {
     return(await this.PutAsync(OnlineIdentityEndpoint, identity, true, null, cts));
 }
Esempio n. 11
0
 public async Task <OnlineIdentityBase> AddOnlineIdentityAsync(
     OnlineIdentity identity,
     CancellationTokenSource cts = null)
 {
     return(await this.PostAsync <OnlineIdentityBase>(OnlineIdentityEndpoint, identity, true, null, cts));
 }
Esempio n. 12
0
 // Start is called before the first frame update
 void Start()
 {
     startPos = transform.position;
     net      = GetComponent <OnlineIdentity>();
 }