Advertisement
NomadicWarrior

brainCloud Automatch solution

Jun 19th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.41 KB | None | 0 0
  1. #region
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using BrainCloud;
  6. using BrainCloud.Entity;
  7. using LitJson;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. using Random = UnityEngine.Random;
  11.  
  12. #endregion
  13.  
  14. public class YaUmneeMatchSelect : YaUmneeGameScene
  15. {
  16. [SerializeField]
  17. private Text playerName;
  18. [SerializeField]
  19. private Text playerRank;
  20. [SerializeField]
  21. private Button newMatch;
  22. [SerializeField]
  23. private Button searchPlayer;
  24. [SerializeField]
  25. private Button facebookShare;
  26. [SerializeField]
  27. private Button logoutButton;
  28.  
  29.  
  30. private const int RANGE_DELTA = 500;
  31. private const int NUMBER_OF_MATCHES = 10;
  32. private readonly List<MatchInfo> completedMatches = new List<MatchInfo>();
  33. private readonly List<PlayerInfo> matchedProfiles = new List<PlayerInfo>();
  34. private readonly List<MatchInfo> matches = new List<MatchInfo>();
  35.  
  36. private Vector2 _scrollPos;
  37. private eState _state = eState.LOADING;
  38.  
  39. private string autoJoinText = "Auto Join";
  40.  
  41. private bool isLookingForMatch = false;
  42.  
  43.  
  44. void Start ()
  45. {
  46. playerName.text = yaUmneeApp.Name;
  47. playerRank.text = "Rank: " + yaUmneeApp.PlayerRating;
  48. newMatch.onClick.AddListener (NewMatch);
  49. searchPlayer.onClick.AddListener (SearchPlayer);
  50. facebookShare.onClick.AddListener (FacebookShare);
  51. logoutButton.onClick.AddListener (LogOut);
  52.  
  53. // Enable Match Making, so other Users can also challege this Profile
  54. // http://getbraincloud.com/apidocs/apiref/#capi-matchmaking-enablematchmaking
  55. yaUmneeApp.Bc.MatchMakingService.EnableMatchMaking(null, (status, code, error, cbObject) =>
  56. {
  57. Debug.Log("MatchMaking enabled failed");
  58. });
  59.  
  60.  
  61. yaUmneeApp.Bc.MatchMakingService.FindPlayers(RANGE_DELTA, NUMBER_OF_MATCHES, OnFindPlayers);
  62. }
  63.  
  64. private void OnFindPlayers(string responseData, object cbPostObject)
  65. {
  66. matchedProfiles.Clear();
  67.  
  68. // Construct our matched players list using response data
  69. var matchesData = JsonMapper.ToObject(responseData)["data"]["matchesFound"];
  70.  
  71.  
  72. foreach (JsonData match in matchesData) matchedProfiles.Add(new PlayerInfo(match));
  73.  
  74.  
  75. // After, fetch our game list from Braincloud
  76. yaUmneeApp.Bc.AsyncMatchService.FindMatches(OnFindMatches);
  77. }
  78.  
  79. private void OnFindMatches(string responseData, object cbPostObject)
  80. {
  81. matches.Clear();
  82.  
  83. // Construct our game list using response data
  84. var jsonMatches = JsonMapper.ToObject(responseData)["data"]["results"];
  85. for (var i = 0; i < jsonMatches.Count; ++i)
  86. {
  87. var jsonMatch = jsonMatches[i];
  88.  
  89. var match = new MatchInfo(jsonMatch, this);
  90. matches.Add(match);
  91. }
  92.  
  93. // Now, find completed matches so the user can go see the history
  94. yaUmneeApp.Bc.AsyncMatchService.OnFindCompleteMatches(OnFindCompletedMatches);
  95. }
  96.  
  97. private void OnFindCompletedMatches(string responseData, object cbPostObject)
  98. {
  99. completedMatches.Clear();
  100.  
  101. // Construct our game list using response data
  102. var jsonMatches = JsonMapper.ToObject(responseData)["data"]["results"];
  103. for (var i = 0; i < jsonMatches.Count; ++i)
  104. {
  105. var jsonMatch = jsonMatches[i];
  106. var match = new MatchInfo(jsonMatch, this);
  107. completedMatches.Add(match);
  108. }
  109.  
  110. _state = eState.GAME_PICKER;
  111. }
  112.  
  113.  
  114. private void NewMatch ()
  115. {
  116. isLookingForMatch = true;
  117. LookingForMatch ();
  118. print ("new match");
  119. }
  120.  
  121. private void LookingForMatch ()
  122. {
  123. if (isLookingForMatch == false)
  124. {
  125. //isLookingForMatch = false;
  126. var MATCH_STATE = "MATCH_STATE";
  127. var CANCEL_LOOKING = "CANCEL_LOOKING";
  128.  
  129. var attributesJson = new JsonData ();
  130. attributesJson [MATCH_STATE] = CANCEL_LOOKING;
  131.  
  132. yaUmneeApp.Bc.PlayerStateService.UpdateAttributes (attributesJson.ToJson (), false);
  133. }
  134. else
  135. {
  136. var scriptDataJson = new JsonData();
  137. scriptDataJson["rankRangeDelta"] = RANGE_DELTA;
  138. scriptDataJson["pushNotificationMessage"] = null;
  139.  
  140.  
  141.  
  142. yaUmneeApp.Bc.ScriptService.RunScript("AUTO_JOIN", scriptDataJson.ToJson(), OnCreateMatchSuccess, OnCreateMatchFailed);
  143. print ("Cloud Script");
  144. }
  145. }
  146.  
  147. private void OnCreateMatchSuccess(string responseData, object cbPostObject)
  148. {
  149. var data = JsonMapper.ToObject(responseData);
  150. MatchInfo match;
  151.  
  152. // Cloud Code returns wrap the data in a responseJson
  153. if (data["data"].Keys.Contains("response"))
  154. {
  155. if (data["data"]["response"].IsObject && data["data"]["response"].Keys.Contains("data"))
  156. {
  157. match = new MatchInfo(data["data"]["response"]["data"], this);
  158. }
  159. else
  160. {
  161. // No match found. Handle this result
  162. Debug.Log(data["data"]["response"].ToString());
  163. _state = eState.GAME_PICKER;
  164.  
  165. isLookingForMatch = true;
  166. autoJoinText = "Cancel Looking for Match";
  167.  
  168. return;
  169. }
  170.  
  171. }
  172. else
  173. {
  174. match = new MatchInfo(data["data"], this);
  175. }
  176.  
  177.  
  178.  
  179. // Go to the game if it's your turn
  180. if (match.yourTurn)
  181. EnterMatch(match);
  182. else
  183. yaUmneeApp.GotoMatchSelectScene();
  184. }
  185.  
  186. private void OnCreateMatchFailed(int a, int b, string responseData, object cbPostObject)
  187. {
  188. Debug.LogError("Failed to create Async Match");
  189. Debug.Log(a);
  190. Debug.Log(b);
  191. Debug.Log(responseData);
  192. _state = eState.GAME_PICKER; // Just go back to game selection
  193. }
  194.  
  195. private void EnterMatch(MatchInfo match)
  196. {
  197. yaUmneeApp.CurrentMatch = match;
  198. _state = eState.LOADING;
  199.  
  200. // Query more detail state about the match
  201. yaUmneeApp.Bc.AsyncMatchService
  202. .ReadMatch(match.ownerId, match.matchId, OnReadMatch, OnReadMatchFailed, match);
  203. }
  204.  
  205. private void OnReadMatch(string responseData, object cbPostObject)
  206. {
  207. var match = cbPostObject as MatchInfo;
  208. var data = JsonMapper.ToObject(responseData)["data"];
  209.  
  210.  
  211. // Setup a couple stuff into our TicTacToe scene
  212. yaUmneeApp.BoardState = (string) data["matchState"]["board"];
  213. yaUmneeApp.PlayerInfoX = match.playerXInfo;
  214. yaUmneeApp.PlayerInfoO = match.playerOInfo;
  215. yaUmneeApp.WhosTurn = match.yourToken == "X" ? yaUmneeApp.PlayerInfoX : match.playerOInfo;
  216. yaUmneeApp.OwnerId = match.ownerId;
  217. yaUmneeApp.MatchId = match.matchId;
  218. yaUmneeApp.MatchVersion = (ulong) match.version;
  219.  
  220. // Load the Tic Tac Toe scene
  221. print("Match Has Created");
  222. //App.GotoTicTacToeScene(gameObject);
  223. }
  224.  
  225. private void OnReadMatchFailed(int a, int b, string responseData, object cbPostObject)
  226. {
  227. Debug.LogError("Failed to Read Match");
  228. }
  229.  
  230.  
  231. private void SearchPlayer ()
  232. {
  233.  
  234. }
  235.  
  236. private void FacebookShare ()
  237. {
  238.  
  239. }
  240.  
  241. private void LogOut ()
  242. {
  243. yaUmneeApp.Bc.PlayerStateService.Logout((response, cbObject) => { yaUmneeApp.GotoLoginScene(gameObject); });
  244. }
  245.  
  246.  
  247. public class MatchInfo
  248. {
  249. private readonly YaUmneeMatchSelect matchSelect;
  250. public PlayerInfo matchedProfile;
  251. public string matchId;
  252. public string ownerId;
  253. public PlayerInfo playerOInfo = new PlayerInfo();
  254. public PlayerInfo playerXInfo = new PlayerInfo();
  255. public int version;
  256. public string yourToken;
  257. public bool yourTurn;
  258.  
  259. public MatchInfo(JsonData jsonMatch, YaUmneeMatchSelect matchSelect)
  260. {
  261. version = (int) jsonMatch["version"];
  262. ownerId = (string) jsonMatch["ownerId"];
  263. matchId = (string) jsonMatch["matchId"];
  264. yourTurn = (string) jsonMatch["status"]["currentPlayer"] == matchSelect.yaUmneeApp.ProfileId;
  265.  
  266. this.matchSelect = matchSelect;
  267.  
  268.  
  269. // Load player info
  270. LoadPlayerInfo(jsonMatch["summary"]["players"][0]);
  271. LoadPlayerInfo(jsonMatch["summary"]["players"][1]);
  272. }
  273.  
  274. private void LoadPlayerInfo(JsonData playerData)
  275. {
  276. var token = (string) playerData["token"];
  277. PlayerInfo playerInfo;
  278. if (token == "X") playerInfo = playerXInfo;
  279. else playerInfo = playerOInfo;
  280.  
  281. if ((string) playerData["profileId"] == matchSelect.yaUmneeApp.ProfileId)
  282. {
  283. playerInfo.PlayerName = matchSelect.yaUmneeApp.Name;
  284. playerInfo.PlayerRating = matchSelect.yaUmneeApp.PlayerRating;
  285. playerInfo.ProfileId = matchSelect.yaUmneeApp.ProfileId;
  286.  
  287. //playerInfo.picUrl = FacebookLogin.PlayerPicUrl;
  288. yourToken = token;
  289. }
  290. else
  291. {
  292. if (matchSelect.matchedProfiles.Count > 0)
  293. {
  294.  
  295. foreach (var profile in matchSelect.matchedProfiles)
  296. if (profile.ProfileId == (string) playerData["profileId"])
  297. {
  298. matchedProfile = profile;
  299. break;
  300. }
  301.  
  302. playerInfo.PlayerName = matchedProfile.PlayerName;
  303. playerInfo.ProfileId = matchedProfile.ProfileId;
  304. playerInfo.PlayerRating = matchedProfile.PlayerRating;
  305. }
  306. }
  307. }
  308. }
  309.  
  310. private enum eState
  311. {
  312. LOADING,
  313. GAME_PICKER,
  314. NEW_GAME,
  315. STARTING_MATCH
  316. }
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement