GameGroups.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  3. *
  4. * You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
  5. * copy, modify, and distribute this software in source code or binary form for use
  6. * in connection with the web services and APIs provided by Facebook.
  7. *
  8. * As with any software that integrates with the Facebook platform, your use of
  9. * this software is subject to the Facebook Developer Principles and Policies
  10. * [http://developers.facebook.com/policy/]. This copyright notice shall be
  11. * included in all copies or substantial portions of the software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  15. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  16. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  17. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  18. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. */
  20. namespace Facebook.Unity.Example
  21. {
  22. using System.Collections.Generic;
  23. using UnityEngine;
  24. internal class GameGroups : MenuBase
  25. {
  26. private string gamerGroupName = "Test group";
  27. private string gamerGroupDesc = "Test group for testing.";
  28. private string gamerGroupPrivacy = "closed";
  29. private string gamerGroupCurrentGroup = string.Empty;
  30. protected override void GetGui()
  31. {
  32. if (this.Button("Game Group Create - Closed"))
  33. {
  34. FB.GameGroupCreate(
  35. "Test game group",
  36. "Test description",
  37. "CLOSED",
  38. this.HandleResult);
  39. }
  40. if (this.Button("Game Group Create - Open"))
  41. {
  42. FB.GameGroupCreate(
  43. "Test game group",
  44. "Test description",
  45. "OPEN",
  46. this.HandleResult);
  47. }
  48. this.LabelAndTextField("Group Name", ref this.gamerGroupName);
  49. this.LabelAndTextField("Group Description", ref this.gamerGroupDesc);
  50. this.LabelAndTextField("Group Privacy", ref this.gamerGroupPrivacy);
  51. if (this.Button("Call Create Group Dialog"))
  52. {
  53. this.CallCreateGroupDialog();
  54. }
  55. this.LabelAndTextField("Group To Join", ref this.gamerGroupCurrentGroup);
  56. bool enabled = GUI.enabled;
  57. GUI.enabled = enabled && !string.IsNullOrEmpty(this.gamerGroupCurrentGroup);
  58. if (this.Button("Call Join Group Dialog"))
  59. {
  60. this.CallJoinGroupDialog();
  61. }
  62. GUI.enabled = enabled && FB.IsLoggedIn;
  63. if (this.Button("Get All App Managed Groups"))
  64. {
  65. this.CallFbGetAllOwnedGroups();
  66. }
  67. if (this.Button("Get Gamer Groups Logged in User Belongs to"))
  68. {
  69. this.CallFbGetUserGroups();
  70. }
  71. GUI.enabled = enabled && !string.IsNullOrEmpty(this.gamerGroupCurrentGroup);
  72. if (this.Button("Make Group Post As User"))
  73. {
  74. this.CallFbPostToGamerGroup();
  75. }
  76. GUI.enabled = enabled;
  77. }
  78. private void GroupCreateCB(IGroupCreateResult result)
  79. {
  80. this.HandleResult(result);
  81. if (result.GroupId != null)
  82. {
  83. this.gamerGroupCurrentGroup = result.GroupId;
  84. }
  85. }
  86. private void GetAllGroupsCB(IGraphResult result)
  87. {
  88. if (!string.IsNullOrEmpty(result.RawResult))
  89. {
  90. this.LastResponse = result.RawResult;
  91. var resultDictionary = result.ResultDictionary;
  92. if (resultDictionary.ContainsKey("data"))
  93. {
  94. var dataArray = (List<object>)resultDictionary["data"];
  95. if (dataArray.Count > 0)
  96. {
  97. var firstGroup = (Dictionary<string, object>)dataArray[0];
  98. this.gamerGroupCurrentGroup = (string)firstGroup["id"];
  99. }
  100. }
  101. }
  102. if (!string.IsNullOrEmpty(result.Error))
  103. {
  104. this.LastResponse = result.Error;
  105. }
  106. }
  107. private void CallFbGetAllOwnedGroups()
  108. {
  109. FB.API(FB.AppId + "/groups", HttpMethod.GET, this.GetAllGroupsCB);
  110. }
  111. private void CallFbGetUserGroups()
  112. {
  113. FB.API("/me/groups?parent=" + FB.AppId, HttpMethod.GET, this.HandleResult);
  114. }
  115. private void CallCreateGroupDialog()
  116. {
  117. FB.GameGroupCreate(
  118. this.gamerGroupName,
  119. this.gamerGroupDesc,
  120. this.gamerGroupPrivacy,
  121. this.GroupCreateCB);
  122. }
  123. private void CallJoinGroupDialog()
  124. {
  125. FB.GameGroupJoin(
  126. this.gamerGroupCurrentGroup,
  127. this.HandleResult);
  128. }
  129. private void CallFbPostToGamerGroup()
  130. {
  131. Dictionary<string, string> dict = new Dictionary<string, string>();
  132. dict["message"] = "herp derp a post";
  133. FB.API(
  134. this.gamerGroupCurrentGroup + "/feed",
  135. HttpMethod.POST,
  136. this.HandleResult,
  137. dict);
  138. }
  139. }
  140. }