Advertisement
MrSpaceCow

GameSparks Script

Oct 17th, 2017
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.52 KB | None | 0 0
  1. using GameSparks.Core;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6.  
  7. public class GameSparksManager : MonoBehaviour {
  8.     public bool authenticated = false;
  9.     public string userID;
  10.     public string firstPlayer;
  11.     public string opponentID;
  12.     public string playerName;
  13.     public string opponentName;
  14.     public int elo = 1000;
  15.  
  16.     string challengeID;
  17.     string matchID;
  18.     bool tookTurn = false;
  19.  
  20.     public static GameSparksManager instance = null;
  21.  
  22.     void Awake() {
  23.         if(instance == null) {
  24.             instance = this;
  25.             DontDestroyOnLoad(this.gameObject);
  26.         } else {
  27.             Destroy(this.gameObject);
  28.         }
  29.         GameSparks.Api.Messages.MatchFoundMessage.Listener += matchFound;
  30.         GameSparks.Api.Messages.ChallengeIssuedMessage.Listener += challengeInvite;
  31.         GameSparks.Api.Messages.ChallengeStartedMessage.Listener += challengeStarted;
  32.         GameSparks.Api.Messages.ChallengeTurnTakenMessage.Listener += turnTaken;
  33.     }
  34.  
  35.     public void authenticate(Func<bool, bool> callback = null) {
  36.         new GameSparks.Api.Requests.DeviceAuthenticationRequest().SetDisplayName(PlayerPrefs.GetString("DisplayName")).Send((response) => {
  37.             if(!response.HasErrors) {
  38.                 Debug.Log("Authenticated");
  39.                 userID = response.UserId;
  40.                 playerName = response.DisplayName;
  41.                 authenticated = true;
  42.                 if(callback != null)
  43.                     callback(true);
  44.             } else {
  45.                 Debug.Log("Error Authenticating");
  46.                 authenticated = false;
  47.                 if(callback != null)
  48.                     callback(false);
  49.             }
  50.         });
  51.     }
  52.  
  53.     public void startRankedMatchmaking() {
  54.         new GameSparks.Api.Requests.LogEventRequest()
  55.             .SetEventKey("GETELO")
  56.             .Send((response) => {
  57.                 if(!response.HasErrors) {
  58.                     Debug.Log("Got Elo");
  59.                     elo = (int)response.ScriptData.GetInt("ELO");
  60.                 }else {
  61.                     Debug.Log("Error Getting Elo");
  62.                 }
  63.             });
  64.         new GameSparks.Api.Requests.MatchmakingRequest()
  65.             .SetSkill(elo)
  66.             .SetMatchShortCode("RANKED")
  67.             .Send((response) => {
  68.                 if(response.HasErrors) {
  69.                     Debug.Log("Matchmaking Failed");
  70.                 }else {
  71.                     Debug.Log("Searching for players...");
  72.                 }
  73.             });
  74.     }
  75.  
  76.     void matchFound(GameSparks.Api.Messages.MatchFoundMessage message) {
  77.         matchID = message.MatchId;
  78.         firstPlayer = message.ScriptData.GetString("FirstPlayer");
  79.         string secondPlayer = message.ScriptData.GetString("SecondPlayer");
  80.        
  81.         Debug.Log("MatchFound " + firstPlayer + " vs " + secondPlayer);
  82.         if(userID == firstPlayer) {
  83.             List<string> opponent = new List<string>(); opponent.Add(secondPlayer);
  84.             new GameSparks.Api.Requests.CreateChallengeRequest()
  85.                 .SetAccessType("PRIVATE")
  86.                 .SetMaxPlayers(2)
  87.                 .SetAutoStartJoinedChallengeOnMaxPlayers(true)
  88.                 .SetChallengeShortCode("RANKED")
  89.                 .SetUsersToChallenge(opponent)
  90.                 .SetEndTime(new DateTime(2020, 4, 13))
  91.                 .Send((response) => {
  92.                     if(response.HasErrors) {
  93.                         Debug.Log("You're a failure. Go kill yourself");
  94.                     }else {
  95.                         Debug.Log("Created Challenge");
  96.                     }
  97.                 });
  98.             opponentID = secondPlayer;
  99.             opponentName = message.ScriptData.GetString("SecondPlayerName");
  100.         }else {
  101.             opponentID = firstPlayer;
  102.             opponentName = message.ScriptData.GetString("FirstPlayerName");
  103.         }
  104.     }
  105.  
  106.     void challengeInvite(GameSparks.Api.Messages.ChallengeIssuedMessage message) {
  107.         Debug.Log("Challenge Issued");
  108.         if(userID != firstPlayer) {
  109.             new GameSparks.Api.Requests.AcceptChallengeRequest()
  110.                 .SetChallengeInstanceId(message.Challenge.ChallengeId)
  111.                 .Send((response)=> {
  112.                     if(response.HasErrors) {
  113.                         Debug.Log("Error in accepting challenge");
  114.                     }else {
  115.                            Debug.Log("Challenge Accepted");
  116.                     }
  117.                 });
  118.         }
  119.     }
  120.  
  121.     void challengeStarted(GameSparks.Api.Messages.ChallengeStartedMessage message) {
  122.         challengeID = message.Challenge.ChallengeId;
  123.         if(opponentID == message.Challenge.NextPlayer)
  124.             firstPlayer = opponentID;
  125.         else
  126.             firstPlayer = userID;
  127.         GameManager.isAIEnabled = false;
  128.         GameManager.isSimulation = false;
  129.         GameManager.isMultiplayer = true;
  130.         FadeRect.changeSceneFade("GameScene");
  131.     }
  132.  
  133.     public void takeTurn(Vector2[] sects) {
  134.         GSRequestData POS = new GSRequestData();
  135.         POS.AddNumber("LEN", sects.Length);
  136.         for(int i = 0; i < sects.Length; i++) {
  137.             POS.AddNumber("x" + i, sects[i].x);
  138.             POS.AddNumber("y" + i, sects[i].y);
  139.         }
  140.         Debug.Log("ChallengeID = " + challengeID);
  141.         tookTurn = true;
  142.         new GameSparks.Api.Requests.LogChallengeEventRequest()
  143.             .SetChallengeInstanceId(challengeID)
  144.             .SetEventKey("TAKETURN")
  145.             .SetEventAttribute("POS", POS)
  146.             .Send((response) => {
  147.                 if(response.HasErrors) {
  148.                     Debug.Log("Error taking turn");
  149.                 }else {
  150.                     Debug.Log("Turn taken");
  151.                 }
  152.             });
  153.     }
  154.  
  155.     void turnTaken(GameSparks.Api.Messages.ChallengeTurnTakenMessage message) {
  156.         if(tookTurn) {
  157.             tookTurn = false;
  158.         } else {
  159.             GSData data = message.Challenge.ScriptData.GetGSData("POS");
  160.             int length = (int)data.GetNumber("LEN");
  161.             Vector2[] sects = new Vector2[length];
  162.             for(int i = 0; i < length; i++)
  163.                 sects[i] = new Vector2((int)data.GetNumber("x" + i), (int)data.GetNumber("y" + i));
  164.             GameManager.input(sects, true);
  165.         }
  166.     }
  167.  
  168.     public void win(Func<int, int, bool> followUp = null) {
  169.         int newRank = -1;
  170.         new GameSparks.Api.Requests.LogEventRequest()
  171.             .SetEventKey("WINRANKED")
  172.             .SetEventAttribute("OPPID", opponentID)
  173.             .SetEventAttribute("CHALID", challengeID)
  174.             .SetEventAttribute("MATCHID", matchID)
  175.             .Send((response) => {
  176.                 if(!response.HasErrors) {
  177.                     Debug.Log("You Won");
  178.                     newRank = (int)response.ScriptData.GetDouble("NEWRANK");
  179.                     if(followUp != null)
  180.                         followUp(elo, newRank);
  181.                     elo = newRank;
  182.                 } else {
  183.                     Debug.Log("Error winning");
  184.                 }
  185.             });
  186.         new GameSparks.Api.Requests.MatchmakingRequest()
  187.             .SetAction("cancel")
  188.             .SetSkill(elo)
  189.             .SetMatchShortCode("RANKED")
  190.             .Send((response) => {
  191.                 if(!response.HasErrors)
  192.                     Debug.Log("Ended Match");
  193.                 else
  194.                     Debug.Log("Error ending match");
  195.             });
  196.     }
  197.  
  198.     public void tie(Func<int, bool> followUp = null) {
  199.         if(firstPlayer == userID) {
  200.             new GameSparks.Api.Requests.LogEventRequest()
  201.                 .SetEventKey("TIERANKED")
  202.                 .SetEventAttribute("CHALID", challengeID)
  203.                 .SetEventAttribute("MATCHID", matchID)
  204.                 .Send((response) => {
  205.                     if(!response.HasErrors) {
  206.                         Debug.Log("Drawed Challenge");
  207.                         if(followUp != null)
  208.                             followUp(elo);
  209.                     }else {
  210.                         Debug.Log("Error Drawing Challenge");
  211.                     }
  212.                 });
  213.         }
  214.         new GameSparks.Api.Requests.MatchmakingRequest()
  215.             .SetAction("cancel")
  216.             .SetSkill(elo)
  217.             .SetMatchShortCode("RANKED")
  218.             .Send((response) => {
  219.                 if(!response.HasErrors)
  220.                     Debug.Log("Ended Match");
  221.                 else
  222.                     Debug.Log("Error ending match");
  223.             });
  224.     }
  225.  
  226.     public void lose(Func<int, int, bool> followUp = null) {
  227.         int newRank = -1;
  228.         new GameSparks.Api.Requests.LogEventRequest()
  229.             .SetEventKey("LOSERANKED")
  230.             .SetEventAttribute("OPPID", opponentID)
  231.             .Send((response) => {
  232.                 if(!response.HasErrors) {
  233.                     Debug.Log("You Lost");
  234.                     newRank = (int)response.ScriptData.GetDouble("NEWRANK");
  235.                     if(followUp != null)
  236.                         followUp(elo, newRank);
  237.                     elo = newRank;
  238.                 } else {
  239.                     Debug.Log("Error losing");
  240.                 }
  241.             });
  242.         new GameSparks.Api.Requests.MatchmakingRequest()
  243.             .SetAction("cancel")
  244.             .SetSkill(elo)
  245.             .SetMatchShortCode("RANKED")
  246.             .Send((response) => {
  247.                 if(!response.HasErrors)
  248.                     Debug.Log("Ended Match");
  249.                 else
  250.                     Debug.Log("Error ending match");
  251.             });
  252.     }
  253.  
  254.  
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement