|
- using CIG.Translation;
- using SUISS.Cloud;
- using SUISS.Core;
- using SUISS.Scheduling;
- using SUISSEngine;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- [PersistentManager]
- public sealed class CIGSparkSocServices : SingletonMonobehaviour<CIGSparkSocServices>
- {
- public string sparkSocGameId;
-
- public string leaderboardName = "boards";
-
- public int leaderboardSize = 100;
-
- public int leaderboardSyncInterval = 600;
-
- public string leaderboardSecret = "blaht";
-
- private IPlayernameController _sparkSocController;
-
- private ILeaderboardController _leaderboardController;
-
- private string _countryCode;
-
- private bool _failedToLoad;
-
- public Comparison<LeaderboardEntry> LeaderboardSort => delegate (LeaderboardEntry y, LeaderboardEntry x)
- {
- if (x.PrimaryScore != y.PrimaryScore)
- {
- return x.PrimaryScore - y.PrimaryScore;
- }
- if (x.SecondaryScore != y.SecondaryScore)
- {
- return x.SecondaryScore - y.SecondaryScore;
- }
- return (x.TertiaryScore != y.TertiaryScore) ? (x.TertiaryScore - y.TertiaryScore) : (x.QuaternaryScore - y.QuaternaryScore);
- };
-
- protected override void Awake()
- {
- base.Awake();
- if (string.IsNullOrEmpty(_countryCode) || string.IsNullOrEmpty(sparkSocGameId) || string.IsNullOrEmpty(leaderboardSecret))
- {
- _failedToLoad = true;
- UnityEngine.Debug.LogWarning("CIG sparksoc services was loaded with missing editor variables! (Game not loaded from loading screen?)");
- }
- _sparkSocController = new PlayernameController(sparkSocGameId, 26088, "");
- _leaderboardController = new LeaderboardController(_sparkSocController, leaderboardName, _countryCode, leaderboardSize, leaderboardSyncInterval, sparkSocGameId, 26088);
- }
-
- protected override void OnDestroy()
- {
- base.OnDestroy();
- }
-
- public void StartLeaderboards()
- {
- if (!_failedToLoad)
- {
- if (!_sparkSocController.IsInstallRegistered)
- {
- _sparkSocController.RegisterInstall();
- }
- StartCoroutine(leaderboardUpdateLoop());
- }
- }
-
- private IEnumerator leaderboardUpdateLoop()
- {
- while (true)
- {
- _leaderboardController.UpdateLeaderboards();
- yield return new WaitForSeconds(leaderboardSyncInterval);
- }
- }
-
- public string GetInstallUuid()
- {
- return _sparkSocController.InstallUuid;
- }
-
- public bool IsRegisteredInstall()
- {
- return _sparkSocController.IsInstallRegistered;
- }
-
- public string GetPlayerName()
- {
- return _sparkSocController.PlayerName;
- }
-
- public string GetPlayerUuid()
- {
- return _sparkSocController.PlayerUuid;
- }
-
- public void VoteForPlayer(string installUuid)
- {
- _leaderboardController.Vote(installUuid, upVote: true);
- }
-
- public bool IsLinkedToPlayername()
- {
- return _sparkSocController.IsLinkedToPlayerName && _sparkSocController.IsPlayerLoggedIn;
- }
-
- public bool IsTopPlayer()
- {
- return _leaderboardController.InGlobalTop || _leaderboardController.InCountryTop;
- }
-
- public bool IsLogedInAndLinked()
- {
- return _sparkSocController.IsLinkedToPlayerName && _sparkSocController.IsPlayerLoggedIn;
- }
-
- public bool IsValidUsernameAndPassword(string username, string password)
- {
- return _sparkSocController.IsValidPassword(password) && _sparkSocController.IsValidPlayername(username);
- }
-
- public ILocalizedString LocalizedServerErrorCode(PlayernameErrors errorcode)
- {
- switch (errorcode)
- {
- case PlayernameErrors.DuplicatePlayername:
- return Localization.Key("SSP_DUPLICATE_USER");
- case PlayernameErrors.ProfanePlayername:
- return Localization.Key("SSP_INVALID_USERNAME_OR_PASSWORD");
- case PlayernameErrors.PlayernameNotFound:
- return Localization.Key("SSP_INVALID_USERNAME_OR_PASSWORD");
- case PlayernameErrors.IncorrectPassword:
- return Localization.Key("SSP_INVALID_USERNAME_OR_PASSWORD");
- default:
- return Localization.Key("SSP_ERROR");
- }
- }
-
- public CloudRequest<PlayernameErrors?> CreateSparkSocPlayer(string username, string password)
- {
- return _sparkSocController.CreatePlayerName(username, password);
- }
-
- public CloudRequest<IAccessToken, PlayernameErrors?> GetVisitingIslandsAccessToken()
- {
- return _sparkSocController.RequestInstallAccessToken("gamestates");
- }
-
- public CloudRequest<PlayernameErrors?> LoginSparkSocPlayer(string username, string password)
- {
- return _sparkSocController.LinkToPlayerName(username, password);
- }
-
- public Leaderboard GetGlobalLeaderboard(int score1, int score2, int score3, int score4)
- {
- Leaderboard leaderboard = _leaderboardController.GetGlobalTop().ReOrder(LeaderboardSort);
- CapPlayerLevels(leaderboard);
- if (_sparkSocController.IsLinkedToPlayerName)
- {
- leaderboard.UpdateRecord(_sparkSocController.InstallUuid, _sparkSocController.PlayerName, score1, score2, score3, score4, LeaderboardSort);
- }
- return leaderboard;
- }
-
- public Leaderboard GetCountryLeaderboard(int score1, int score2, int score3, int score4)
- {
- Leaderboard leaderboard = _leaderboardController.GetCountryTop().ReOrder(LeaderboardSort);
- CapPlayerLevels(leaderboard);
- if (_sparkSocController.IsLinkedToPlayerName)
- {
- leaderboard.UpdateRecord(_sparkSocController.InstallUuid, _sparkSocController.PlayerName, score1, score2, score3, score4, LeaderboardSort);
- }
- return leaderboard;
- }
-
- public Leaderboard GetPersonalCountryLeaderboard(int score1, int score2, int score3, int score4)
- {
- Leaderboard leaderboard = _leaderboardController.GetCountryLocal().ReOrder(LeaderboardSort);
- CapPlayerLevels(leaderboard);
- if (leaderboard.Records.Count != 0)
- {
- leaderboard.UpdateRecord(_sparkSocController.InstallUuid, _sparkSocController.PlayerName, score1, score2, score3, score4, LeaderboardSort);
- leaderboard.ShrinkBoardAround(_sparkSocController.InstallUuid, 4, LeaderboardSort);
- }
- return leaderboard;
- }
-
- public Leaderboard GetPersonalGlobalLeaderboard(int score1, int score2, int score3, int score4)
- {
- Leaderboard leaderboard = _leaderboardController.GetGlobalLocal().ReOrder(LeaderboardSort);
- CapPlayerLevels(leaderboard);
- if (leaderboard.Records.Count != 0)
- {
- leaderboard.UpdateRecord(_sparkSocController.InstallUuid, _sparkSocController.PlayerName, score1, score2, score3, score4, LeaderboardSort);
- leaderboard.ShrinkBoardAround(_sparkSocController.InstallUuid, 4, LeaderboardSort);
- }
- return leaderboard;
- }
-
- public int GetMyGlobalServerRank()
- {
- return _leaderboardController.GlobalRank;
- }
-
- public void LogoutSparkSocPlayer()
- {
- _sparkSocController.LogOutPlayer();
- }
-
- public void PostNewHighScores(int score1, int score2, int score3, int score4)
- {
- if (!_failedToLoad && _sparkSocController != null && SingletonMonobehaviour<CIGVisitingIslandsManager>.IsAvailable && _sparkSocController.IsLinkedToPlayerName && _sparkSocController.IsInstallRegistered && _sparkSocController.IsPlayerLoggedIn)
- {
- ScoreRecord scoreRecord = new ScoreRecord();
- scoreRecord.Username = _sparkSocController.PlayerName;
- scoreRecord.Sauid = _sparkSocController.PlayerUuid;
- scoreRecord.Sagid = _sparkSocController.InstallUuid;
- scoreRecord.GameUuid = sparkSocGameId;
- scoreRecord.CountryCode = _countryCode;
- scoreRecord.Data = string.Empty;
- scoreRecord.PrimaryScore = score1;
- scoreRecord.SecondaryScore = score2;
- scoreRecord.TertiaryScore = score3;
- scoreRecord.QuaternaryScore = score4;
- scoreRecord.IslandReady = SingletonMonobehaviour<CIGVisitingIslandsManager>.Instance.IslandIsSent();
- scoreRecord.Signature = _leaderboardController.SignRecord(scoreRecord, leaderboardSecret);
- _leaderboardController.UpdateScore(scoreRecord);
- }
- }
-
- public void Run(IEnumerator func)
- {
- SingletonMonobehaviour<Scheduler>.Instance.StartRoutine(func);
- }
-
- public void Run(string name, IEnumerator func)
- {
- throw new NotImplementedException("Use CIGSparkSocService.Run(IEnumerator func)");
- }
-
- public void Stop(string name)
- {
- throw new NotImplementedException("Not supported by CIGSparkSocService");
- }
-
- private void CapPlayerLevels(Leaderboard leaderboard)
- {
- if (SingletonMonobehaviour<CIGGameState>.Instance.Level > 150)
- {
- return;
- }
- List<LeaderboardEntry> list = leaderboard.Records.FindAll((LeaderboardEntry x) => x.PrimaryScore > 150);
- int count = list.Count;
- int num = count - 1;
- for (int i = 0; i < count; i++)
- {
- LeaderboardEntry leaderboardEntry = list[i];
- if (leaderboardEntry.PrimaryScore >= 150)
- {
- leaderboard.UpdateRecord(leaderboardEntry.Sagid, leaderboardEntry.Username, 150, leaderboardEntry.SecondaryScore, leaderboardEntry.TertiaryScore, 0, (i != num) ? null : LeaderboardSort);
- }
- }
- }
- }
|