Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using GameSparks.Core;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class GameSparksManager : MonoBehaviour {
- public bool authenticated = false;
- public string userID;
- public string firstPlayer;
- public string opponentID;
- public string playerName;
- public string opponentName;
- public int elo = 1000;
- string challengeID;
- string matchID;
- bool tookTurn = false;
- public static GameSparksManager instance = null;
- void Awake() {
- if(instance == null) {
- instance = this;
- DontDestroyOnLoad(this.gameObject);
- } else {
- Destroy(this.gameObject);
- }
- GameSparks.Api.Messages.MatchFoundMessage.Listener += matchFound;
- GameSparks.Api.Messages.ChallengeIssuedMessage.Listener += challengeInvite;
- GameSparks.Api.Messages.ChallengeStartedMessage.Listener += challengeStarted;
- GameSparks.Api.Messages.ChallengeTurnTakenMessage.Listener += turnTaken;
- }
- public void authenticate(Func<bool, bool> callback = null) {
- new GameSparks.Api.Requests.DeviceAuthenticationRequest().SetDisplayName(PlayerPrefs.GetString("DisplayName")).Send((response) => {
- if(!response.HasErrors) {
- Debug.Log("Authenticated");
- userID = response.UserId;
- playerName = response.DisplayName;
- authenticated = true;
- if(callback != null)
- callback(true);
- } else {
- Debug.Log("Error Authenticating");
- authenticated = false;
- if(callback != null)
- callback(false);
- }
- });
- }
- public void startRankedMatchmaking() {
- new GameSparks.Api.Requests.LogEventRequest()
- .SetEventKey("GETELO")
- .Send((response) => {
- if(!response.HasErrors) {
- Debug.Log("Got Elo");
- elo = (int)response.ScriptData.GetInt("ELO");
- }else {
- Debug.Log("Error Getting Elo");
- }
- });
- new GameSparks.Api.Requests.MatchmakingRequest()
- .SetSkill(elo)
- .SetMatchShortCode("RANKED")
- .Send((response) => {
- if(response.HasErrors) {
- Debug.Log("Matchmaking Failed");
- }else {
- Debug.Log("Searching for players...");
- }
- });
- }
- void matchFound(GameSparks.Api.Messages.MatchFoundMessage message) {
- matchID = message.MatchId;
- firstPlayer = message.ScriptData.GetString("FirstPlayer");
- string secondPlayer = message.ScriptData.GetString("SecondPlayer");
- Debug.Log("MatchFound " + firstPlayer + " vs " + secondPlayer);
- if(userID == firstPlayer) {
- List<string> opponent = new List<string>(); opponent.Add(secondPlayer);
- new GameSparks.Api.Requests.CreateChallengeRequest()
- .SetAccessType("PRIVATE")
- .SetMaxPlayers(2)
- .SetAutoStartJoinedChallengeOnMaxPlayers(true)
- .SetChallengeShortCode("RANKED")
- .SetUsersToChallenge(opponent)
- .SetEndTime(new DateTime(2020, 4, 13))
- .Send((response) => {
- if(response.HasErrors) {
- Debug.Log("You're a failure. Go kill yourself");
- }else {
- Debug.Log("Created Challenge");
- }
- });
- opponentID = secondPlayer;
- opponentName = message.ScriptData.GetString("SecondPlayerName");
- }else {
- opponentID = firstPlayer;
- opponentName = message.ScriptData.GetString("FirstPlayerName");
- }
- }
- void challengeInvite(GameSparks.Api.Messages.ChallengeIssuedMessage message) {
- Debug.Log("Challenge Issued");
- if(userID != firstPlayer) {
- new GameSparks.Api.Requests.AcceptChallengeRequest()
- .SetChallengeInstanceId(message.Challenge.ChallengeId)
- .Send((response)=> {
- if(response.HasErrors) {
- Debug.Log("Error in accepting challenge");
- }else {
- Debug.Log("Challenge Accepted");
- }
- });
- }
- }
- void challengeStarted(GameSparks.Api.Messages.ChallengeStartedMessage message) {
- challengeID = message.Challenge.ChallengeId;
- if(opponentID == message.Challenge.NextPlayer)
- firstPlayer = opponentID;
- else
- firstPlayer = userID;
- GameManager.isAIEnabled = false;
- GameManager.isSimulation = false;
- GameManager.isMultiplayer = true;
- FadeRect.changeSceneFade("GameScene");
- }
- public void takeTurn(Vector2[] sects) {
- GSRequestData POS = new GSRequestData();
- POS.AddNumber("LEN", sects.Length);
- for(int i = 0; i < sects.Length; i++) {
- POS.AddNumber("x" + i, sects[i].x);
- POS.AddNumber("y" + i, sects[i].y);
- }
- Debug.Log("ChallengeID = " + challengeID);
- tookTurn = true;
- new GameSparks.Api.Requests.LogChallengeEventRequest()
- .SetChallengeInstanceId(challengeID)
- .SetEventKey("TAKETURN")
- .SetEventAttribute("POS", POS)
- .Send((response) => {
- if(response.HasErrors) {
- Debug.Log("Error taking turn");
- }else {
- Debug.Log("Turn taken");
- }
- });
- }
- void turnTaken(GameSparks.Api.Messages.ChallengeTurnTakenMessage message) {
- if(tookTurn) {
- tookTurn = false;
- } else {
- GSData data = message.Challenge.ScriptData.GetGSData("POS");
- int length = (int)data.GetNumber("LEN");
- Vector2[] sects = new Vector2[length];
- for(int i = 0; i < length; i++)
- sects[i] = new Vector2((int)data.GetNumber("x" + i), (int)data.GetNumber("y" + i));
- GameManager.input(sects, true);
- }
- }
- public void win(Func<int, int, bool> followUp = null) {
- int newRank = -1;
- new GameSparks.Api.Requests.LogEventRequest()
- .SetEventKey("WINRANKED")
- .SetEventAttribute("OPPID", opponentID)
- .SetEventAttribute("CHALID", challengeID)
- .SetEventAttribute("MATCHID", matchID)
- .Send((response) => {
- if(!response.HasErrors) {
- Debug.Log("You Won");
- newRank = (int)response.ScriptData.GetDouble("NEWRANK");
- if(followUp != null)
- followUp(elo, newRank);
- elo = newRank;
- } else {
- Debug.Log("Error winning");
- }
- });
- new GameSparks.Api.Requests.MatchmakingRequest()
- .SetAction("cancel")
- .SetSkill(elo)
- .SetMatchShortCode("RANKED")
- .Send((response) => {
- if(!response.HasErrors)
- Debug.Log("Ended Match");
- else
- Debug.Log("Error ending match");
- });
- }
- public void tie(Func<int, bool> followUp = null) {
- if(firstPlayer == userID) {
- new GameSparks.Api.Requests.LogEventRequest()
- .SetEventKey("TIERANKED")
- .SetEventAttribute("CHALID", challengeID)
- .SetEventAttribute("MATCHID", matchID)
- .Send((response) => {
- if(!response.HasErrors) {
- Debug.Log("Drawed Challenge");
- if(followUp != null)
- followUp(elo);
- }else {
- Debug.Log("Error Drawing Challenge");
- }
- });
- }
- new GameSparks.Api.Requests.MatchmakingRequest()
- .SetAction("cancel")
- .SetSkill(elo)
- .SetMatchShortCode("RANKED")
- .Send((response) => {
- if(!response.HasErrors)
- Debug.Log("Ended Match");
- else
- Debug.Log("Error ending match");
- });
- }
- public void lose(Func<int, int, bool> followUp = null) {
- int newRank = -1;
- new GameSparks.Api.Requests.LogEventRequest()
- .SetEventKey("LOSERANKED")
- .SetEventAttribute("OPPID", opponentID)
- .Send((response) => {
- if(!response.HasErrors) {
- Debug.Log("You Lost");
- newRank = (int)response.ScriptData.GetDouble("NEWRANK");
- if(followUp != null)
- followUp(elo, newRank);
- elo = newRank;
- } else {
- Debug.Log("Error losing");
- }
- });
- new GameSparks.Api.Requests.MatchmakingRequest()
- .SetAction("cancel")
- .SetSkill(elo)
- .SetMatchShortCode("RANKED")
- .Send((response) => {
- if(!response.HasErrors)
- Debug.Log("Ended Match");
- else
- Debug.Log("Error ending match");
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement