Example #1
0
        /// <summary>
        /// Returns a manifest for the specified game.
        /// </summary>
        /// <param name="clientIndex">The index of the client making the call.</param>
        /// <param name="gameId">The index of the client making the call.</param>
        /// <param name="callback">Callback to call when the operation is complete.</param>
        public void GetGameManifest(int clientIndex, string gameId, Action <RuyiNetGameManifest> callback)
        {
            EnqueueTask(() =>
            {
                try
                {
                    var data = mClient.BCService.Patch_GetGameManifest(gameId, clientIndex);
                    return(data);
                }
                catch (Exception e)
                {
                    // just log it for now, otherwise layer0 will crash, it happens when no response while switching high-low power mode
                    // throw;
                    Logging.Logger.Log(e.Message, Logging.LogLevel.Error);
                    var response = new RuyiNetResponse()
                    {
                        status  = 999,
                        message = e.ToString()
                    };

                    return(JsonConvert.SerializeObject(response));
                }
            }, (RuyiNetGetGameManifestResponse response) =>
            {
                callback(response);
            });
        }
Example #2
0
        /// <summary>
        /// Ends a telemetry session.
        /// </summary>
        /// <param name="clientIndex">The index of the client making the call.</param>
        /// <param name="timestamp">The timestamp when the session ended.</param>
        /// <param name="sessionId">The ID of the session to close.</param>
        /// <param name="callback">Callback to call when the operation is complete.</param>
        public void EndTelemetrySession(int clientIndex, int timestamp, string sessionId, RuyiNetTask <RuyiNetResponse> .CallbackType callback)
        {
            EnqueueTask(() =>
            {
                try
                {
                    var data = mClient.BCService.Telemetry_EndTelemetrySession(sessionId, timestamp, clientIndex);
                    return(data);
                }
                catch (Exception e)
                {
#if DEBUG
                    var response = new RuyiNetResponse()
                    {
                        status  = 999,
                        message = e.ToString()
                    };

                    return(JsonConvert.SerializeObject(response));
#else
                    throw;
#endif
                }
            }, callback);
        }
Example #3
0
        /// <summary>
        /// Returns a manifest for the specified game.
        /// </summary>
        /// <param name="clientIndex">The index of the client making the call.</param>
        /// <param name="gameId">The index of the client making the call.</param>
        public async Task <RuyiNetGameManifest> GetGameManifest(int clientIndex, string gameId)
        {
            string resp = null;

            try
            {
                resp = await mClient.BCService.Patch_GetGameManifestAsync(gameId, clientIndex, token);
            }
            catch (Exception e)
            {
                // just log it for now, otherwise layer0 will crash, it happens when no response while switching high-low power mode
                // throw;
                Logging.Logger.Log(e.Message, Logging.LogLevel.Error);
                var error = new RuyiNetResponse()
                {
                    status  = 999,
                    message = e.ToString()
                };

                resp = JsonConvert.SerializeObject(error);
            }
            var response = mClient.Process <RuyiNetGetGameManifestResponse>(resp);

            return(response);
        }
Example #4
0
        /// <summary>
        /// Starts a telemetry session on the online service
        /// </summary>
        /// <param name="clientIndex">Index of the client making the call.</param>
        /// <param name="timestamp">The timestamp when the session started.</param>
        /// <param name="callback">The callback that will receive the session ID.</param>
        public void StartTelemetrySession(int clientIndex, int timestamp, Action <RuyiNetTelemetrySession> callback)
        {
            EnqueueTask(() =>
            {
                try
                {
                    var data = mClient.BCService.Telemetry_StartTelemetrySession(timestamp, clientIndex);
                    return(data);
                }
                catch (Exception e)
                {
#if DEBUG
                    var response = new RuyiNetResponse()
                    {
                        status  = 999,
                        message = e.ToString()
                    };

                    return(JsonConvert.SerializeObject(response));
#else
                    throw;
#endif
                }
            }, (RuyiNetTelemetrySessionResponse response) =>
            {
                if (callback != null)
                {
                    callback(new RuyiNetTelemetrySession(response.data.telemetrySessionId, response.data.timestamp));
                }
            });
        }
Example #5
0
        /// <summary>
        /// Starts a telemetry event.
        /// </summary>
        /// <param name="clientIndex">The index of the client making the call.</param>
        /// <param name="timestamp">The timestamp when the event occurred.</param>
        /// <param name="sessionId">The ID of the session to Start the event with.</param>
        /// <param name="eventType">The type of event.</param>
        /// <param name="participantId">The ID or the participant in the event.</param>
        /// <param name="customData">The custom data to attach to the event.</param>
        /// <param name="callback">The callback to call when the operation is complete.</param>
        public void StartTelemetryEvent(int clientIndex, int timestamp,
                                        string sessionId, string eventType, string participantId,
                                        Dictionary <string, string> customData,
                                        RuyiNetTask <RuyiNetResponse> .CallbackType callback)
        {
            EnqueueTask(() =>
            {
                try
                {
                    var data = mClient.BCService.Telemetry_StartTelemetryEvent(
                        sessionId, timestamp, eventType, participantId,
                        customData, clientIndex);
                    return(data);
                }
                catch (Exception e)
                {
#if DEBUG
                    var response = new RuyiNetResponse()
                    {
                        status  = 999,
                        message = e.ToString()
                    };

                    return(JsonConvert.SerializeObject(response));
#else
                    throw;
#endif
                }
            }, callback);
        }
Example #6
0
        /// <summary>
        /// Destroys a lobby.
        /// </summary>
        /// <param name="clientIndex">The index of user</param>
        /// <param name="lobbyId">The ID of the lobby to close.</param>
        /// <param name="callback">The function to call when the task completes.</param>
        public void DestroyLobby(int clientIndex, string lobbyId, Action <RuyiNetLobby> callback)
        {
            EnqueueTask(() =>
            {
                try
                {
                    return(mClient.BCService.Lobby_DestroyLobby(lobbyId, clientIndex));
                }
                catch (Exception e)
                {
                    Logging.Logger.Log("", Logging.LogLevel.Error);
                    var response = new RuyiNetResponse()
                    {
                        status  = 999,
                        message = e.ToString()
                    };

                    return(JsonConvert.SerializeObject(response));
                }
            }, (RuyiNetLobbyResponse response) => OnLobbyResponse(callback, response));
        }
Example #7
0
        /// <summary>
        /// Creates a lobby that other players can find and join.
        /// </summary>
        /// <param name="clientIndex">The index of user</param>
        /// <param name="maxSlots">The maximum number of players that can join this lobby.</param>
        /// <param name="isOpen">Whether or not the lobby is open by default.</param>
        /// <param name="lobbyType">Whether or not this lobby is for a RANKED MATCH or a PLAYER MATCH.</param>
        /// <param name="jsonAttributes">JSON string of custom attributes to attach to this lobby.</param>
        /// <param name="callback">The function to call when the task completes.</param>
        public void CreateLobby(int clientIndex, int maxSlots, RuyiNetLobbyType lobbyType, bool isOpen, string jsonAttributes, Action <RuyiNetLobby> callback)
        {
            EnqueueTask(() =>
            {
                try
                {
                    return(mClient.BCService.Lobby_CreateLobby((BrainCloudApi.LobbyType)lobbyType, maxSlots, isOpen, jsonAttributes, clientIndex));
                }
                catch (Exception e)
                {
                    Logging.Logger.Log("", Logging.LogLevel.Error);
                    var response = new RuyiNetResponse()
                    {
                        status  = 999,
                        message = e.ToString()
                    };

                    return(JsonConvert.SerializeObject(response));
                }
            }, (RuyiNetLobbyResponse response) => OnLobbyResponse(callback, response));
        }
Example #8
0
        /// <summary>
        /// Searches for lobbies created by other players.
        /// </summary>
        /// <param name="clientIndex">The index of user</param>
        /// <param name="numResults">The maximum number of lobbies to return.</param>
        /// <param name="lobbyType">Whether or not this lobby is for a RANKED MATCH or a PLAYER MATCH.</param>
        /// <param name="freeSlots">The number of free slots needed.</param>
        /// <param name="jsonAttributes">JSON string representing parameters to search for.</param>
        /// <param name="callback">The function to call when the task completes.</param>
        public void FindLobbies(int clientIndex, int numResults, RuyiNetLobbyType lobbyType, int freeSlots, string jsonAttributes, Action <RuyiNetLobby[]> callback)
        {
            EnqueueTask(() =>
            {
                try
                {
                    return(mClient.BCService.Lobby_FindLobbies(freeSlots, numResults, jsonAttributes, clientIndex));;
                }
                catch (Exception e)
                {
                    Logging.Logger.Log("", Logging.LogLevel.Error);
                    var response = new RuyiNetResponse()
                    {
                        status  = 999,
                        message = e.ToString()
                    };

                    return(JsonConvert.SerializeObject(response));
                }
            }, (RuyiNetLobbyFindResponse response) => OnLobbyFindResponse(callback, response));
        }
Example #9
0
        /// <summary>
        /// Starts a telemetry session on the online service
        /// </summary>
        /// <param name="clientIndex">Index of the client making the call.</param>
        /// <param name="timestamp">The timestamp when the session started.</param>
        public async Task <RuyiNetTelemetrySession> StartTelemetrySession(int clientIndex, int timestamp)
        {
            string resp = null;

            try
            {
                resp = await mClient.BCService.Telemetry_StartTelemetrySessionAsync(timestamp, clientIndex, token);
            }
            catch (Exception e)
            {
#if DEBUG
                var error = new RuyiNetResponse()
                {
                    status  = 999,
                    message = e.ToString()
                };
                resp = JsonConvert.SerializeObject(error);
#else
                throw;
#endif
            }
            var response = mClient.Process <RuyiNetTelemetrySessionResponse>(resp);
            return(new RuyiNetTelemetrySession(response.data.telemetrySessionId, response.data.timestamp));
        }
Example #10
0
        /// <summary>
        /// Initialise the RUYI net client and switch to the game context.
        /// </summary>
        /// <param name="appId">The App ID of the game to initialise for.</param>
        /// <param name="appSecret">The App secret of the game. NOTE: This is a password and should be treated as such.</param>
        /// <param name="onInitialised">The function to call whe initialisation completes.</param>
        public void Initialise(string appId, string appSecret, Action onInitialised)
        {
            if (Initialised)
            {
                onInitialised?.Invoke();

                return;
            }

            AppId     = appId;
            AppSecret = appSecret;

            EnqueueTask(() =>
            {
                var hostString = Dns.GetHostName();

                IPHostEntry hostInfo = Dns.GetHostEntry(hostString);
                foreach (IPAddress ip in hostInfo.AddressList)
                {
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                    {
                        RemoteIpAddress = ip.ToString();
                    }
                }

                for (int i = 0; i < MAX_PLAYERS; ++i)
                {
                    CurrentPlayers[i] = null;
                    var jsonResponse  = BCService.Identity_SwitchToSingletonChildProfile(AppId, true, i);
                    var childProfile  = JsonConvert.DeserializeObject <RuyiNetSwitchToChildProfileResponse>(jsonResponse);
                    if (childProfile.status != RuyiNetHttpStatus.OK)
                    {
                        continue;
                    }

                    var profileId   = childProfile.data.parentProfileId;
                    var profileName = childProfile.data.playerName;

                    NewUser = childProfile.data.newUser;

                    var payload = new RuyiNetProfileIdRequest()
                    {
                        profileId = profileId
                    };
                    jsonResponse = BCService.Script_RunParentScript("GetProfile", JsonConvert.SerializeObject(payload), "RUYI", i);

                    var profileData = JsonConvert.DeserializeObject <RuyiNetGetProfileResponse>(jsonResponse);
                    if (profileData.status != RuyiNetHttpStatus.OK ||
                        profileData.data.success == false)
                    {
                        continue;
                    }

                    CurrentPlayers[i] = profileData.data.response;
                }

                var response = new RuyiNetResponse()
                {
                    status = RuyiNetHttpStatus.OK
                };

                return(JsonConvert.SerializeObject(response));
            }, (RuyiNetResponse response) =>
            {
                Initialised = true;

                onInitialised?.Invoke();
            });
        }
Example #11
0
        /// <summary>
        /// Manually restore the save data up to this point.
        /// </summary>
        /// <param name="index">The index of user</param>
        /// <param name="callback">The function to call when the task completes.</param>
        public void RestoreData(int index, RuyiNetTask <RuyiNetResponse> .CallbackType callback)
        {
            EnqueueTask(() =>
            {
                try
                {
                    var path = GetPersistentDataPath(index);
                    path     = Path.GetFullPath(path);

                    //  Backup persistent data path

                    /*var backupPath = Path.Combine(path, BACKUP_LOCATION);
                     * if (Directory.Exists(backupPath))
                     * {
                     *  var x = new DirectoryInfo(backupPath);
                     *  x.Delete(true);
                     * }
                     *
                     * foreach (var i in Directory.GetDirectories(path, "*", SearchOption.AllDirectories))
                     * {
                     *  var directoryName = Path.GetFileName(i);
                     *  if (Array.IndexOf(IGNORE, directoryName) >= 0)
                     *  {
                     *      continue;
                     *  }
                     *
                     *  Directory.CreateDirectory(i.Replace(path, backupPath));
                     * }
                     *
                     * //Copy all the files & Replaces any files with the same name
                     * foreach (var i in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories))
                     * {
                     *  var fileName = Path.GetFileName(i);
                     *  if (Array.IndexOf(IGNORE, fileName) >= 0)
                     *  {
                     *      continue;
                     *  }
                     *
                     *  File.Copy(i, i.Replace(path, backupPath), true);
                     *  File.Delete(i);
                     * }*/

                    //  List files
                    var data     = mClient.BCService.File_ListUserFiles_SNSFO(GetCloudLocation(index), true, index);
                    var response = JsonConvert.DeserializeObject <RuyiNetListUserFilesResponse>(data, new JsonSerializerSettings
                    {
                        NullValueHandling = NullValueHandling.Ignore
                    });

                    if (response.status != RuyiNetHttpStatus.OK)
                    {
                        return(data);
                    }

                    //  Download one by one to correct path
                    //  TODO: Need to get the session ID and pass it in as a URL parameter.
                    foreach (var i in response.data.fileList)
                    {
                        EnqueueTask(() =>
                        {
                            return(mClient.BCService.File_DownloadFile(i.cloudPath, i.cloudFilename, true, index));
                        }, callback);
                    }

                    //  Delete backup
                    //var backupInfo = new DirectoryInfo(backupPath);
                    //backupInfo.Delete(true);

                    return(JsonConvert.SerializeObject(new RuyiNetResponse()
                    {
                        status = RuyiNetHttpStatus.OK, message = "Restore Data Successful"
                    }));
                }
                catch (Exception e)
                {
                    if (e is ArgumentException ||
                        e is NotSupportedException ||
                        e is PathTooLongException ||
                        e is DirectoryNotFoundException)
                    {
                        var response = new RuyiNetResponse()
                        {
                            status  = 400,
                            message = e.ToString()
                        };

                        return(JsonConvert.SerializeObject(response));
                    }
                    else if (e is SecurityException ||
                             e is IOException ||
                             e is UnauthorizedAccessException ||
                             e is RuyiNetException)
                    {
                        var response = new RuyiNetResponse()
                        {
                            status  = 500,
                            message = e.ToString()
                        };

                        return(JsonConvert.SerializeObject(response));
                    }
                    else
                    {
#if DEBUG
                        var response = new RuyiNetResponse()
                        {
                            status  = 999,
                            message = e.ToString()
                        };

                        return(JsonConvert.SerializeObject(response));
#else
                        throw;
#endif
                    }
                }
            }, callback);
        }
Example #12
0
        internal void BackupData(int index, bool cleanMode, RuyiNetTask <RuyiNetResponse> .CallbackType callback)
        {
            EnqueueTask(() =>
            {
                try
                {
                    var path = GetPersistentDataPath(index);
                    path     = Path.GetFullPath(path);
                    BackupPath(index, GetCloudLocation(index), path);
                    if (cleanMode)
                    {
                        var di = new DirectoryInfo(path);
                        foreach (var i in di.GetFiles())
                        {
                            if (Array.IndexOf(IGNORE, i.Name) >= 0)
                            {
                                continue;
                            }

                            i.Delete();
                        }

                        foreach (var i in di.GetDirectories())
                        {
                            if (Array.IndexOf(IGNORE, i.Name) >= 0)
                            {
                                continue;
                            }

                            i.Delete(true);
                        }
                    }

                    return(JsonConvert.SerializeObject(new RuyiNetResponse()
                    {
                        status = RuyiNetHttpStatus.OK
                    }));
                }
                catch (Exception e)
                {
                    if (e is ArgumentException ||
                        e is NotSupportedException ||
                        e is PathTooLongException ||
                        e is DirectoryNotFoundException)
                    {
                        var response = new RuyiNetResponse()
                        {
                            status  = 400,
                            message = e.ToString()
                        };

                        return(JsonConvert.SerializeObject(response));
                    }
                    else if (e is SecurityException ||
                             e is IOException ||
                             e is UnauthorizedAccessException ||
                             e is RuyiNetException)
                    {
                        var response = new RuyiNetResponse()
                        {
                            status  = 500,
                            message = e.ToString()
                        };

                        return(JsonConvert.SerializeObject(response));
                    }
                    else
                    {
                        var response = new RuyiNetResponse()
                        {
                            status  = 501,
                            message = e.ToString()
                        };

                        return(JsonConvert.SerializeObject(response));

                        //throw;
                    }
                }
            }, callback);
        }
Example #13
0
        /// <summary>
        /// Initialise the RUYI net client and switch to the game context.
        /// </summary>
        /// <param name="appId">The App ID of the game to initialise for.</param>
        /// <param name="appSecret">The App secret of the game. NOTE: This is a password and should be treated as such.</param>
        /// <param name="onInitialised">The function to call whe initialisation completes.</param>
        public void Initialise(string appId, string appSecret, Action onInitialised)
        {
            if (Initialised)
            {
                onInitialised?.Invoke();

                return;
            }

            AppId     = appId;
            AppSecret = appSecret;

            EnqueueTask(() =>
            {
                var hostString = Dns.GetHostName();

                IPHostEntry hostInfo = Dns.GetHostEntry(hostString);
                foreach (IPAddress ip in hostInfo.AddressList)
                {
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                    {
                        RemoteIpAddress = ip.ToString();
                    }
                }

                for (int i = 0; i < MAX_PLAYERS; ++i)
                {
                    CurrentPlayers[i] = null;
                    var jsonResponse  = BCService.Identity_SwitchToSingletonChildProfile(AppId, true, i);
                    var childProfile  = JsonConvert.DeserializeObject <RuyiNetSwitchToChildProfileResponse>(jsonResponse);
                    if (childProfile.status != RuyiNetHttpStatus.OK)
                    {
                        continue;
                    }

                    var profileId   = childProfile.data.parentProfileId;
                    var profileName = childProfile.data.playerName;

                    NewUser = childProfile.data.newUser;

                    jsonResponse = BCService.Friend_GetSummaryDataForProfileId(profileId, i);
                    var pdata    = JsonConvert.DeserializeObject <RuyiNetGetSummaryDataForProfileIdResponse>(jsonResponse);
                    if (pdata.status != RuyiNetHttpStatus.OK)
                    {
                        continue;
                    }
                    CurrentPlayers[i] = new RuyiNetProfile()
                    {
                        profileId   = pdata.data.playerId,
                        profileName = pdata.data.playerName,
                        pictureUrl  = pdata.data.pictureUrl,
                        email       = pdata.data.email,
                    };
                    Logging.Logger.Log($"{pdata.data.playerId} {pdata.data.playerName} {pdata.data.pictureUrl} {pdata.data.email}", Logging.LogLevel.Info);
                }

                var response = new RuyiNetResponse()
                {
                    status = RuyiNetHttpStatus.OK
                };

                return(JsonConvert.SerializeObject(response));
            }, (RuyiNetResponse response) =>
            {
                Initialised = true;

                onInitialised?.Invoke();
            });
        }