AppRequests.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 System.Linq;
  24. using UnityEngine;
  25. internal class AppRequests : MenuBase
  26. {
  27. private string requestMessage = string.Empty;
  28. private string requestTo = string.Empty;
  29. private string requestFilter = string.Empty;
  30. private string requestExcludes = string.Empty;
  31. private string requestMax = string.Empty;
  32. private string requestData = string.Empty;
  33. private string requestTitle = string.Empty;
  34. private string requestObjectID = string.Empty;
  35. private int selectedAction = 0;
  36. private string[] actionTypeStrings =
  37. {
  38. "NONE",
  39. OGActionType.SEND.ToString(),
  40. OGActionType.ASKFOR.ToString(),
  41. OGActionType.TURN.ToString()
  42. };
  43. protected override void GetGui()
  44. {
  45. if (this.Button("Select - Filter None"))
  46. {
  47. FB.AppRequest("Test Message", callback: this.HandleResult);
  48. }
  49. if (this.Button("Select - Filter app_users"))
  50. {
  51. List<object> filter = new List<object>() { "app_users" };
  52. // workaround for mono failing with named parameters
  53. FB.AppRequest("Test Message", null, filter, null, 0, string.Empty, string.Empty, this.HandleResult);
  54. }
  55. if (this.Button("Select - Filter app_non_users"))
  56. {
  57. List<object> filter = new List<object>() { "app_non_users" };
  58. FB.AppRequest("Test Message", null, filter, null, 0, string.Empty, string.Empty, this.HandleResult);
  59. }
  60. // Custom options
  61. this.LabelAndTextField("Message: ", ref this.requestMessage);
  62. this.LabelAndTextField("To (optional): ", ref this.requestTo);
  63. this.LabelAndTextField("Filter (optional): ", ref this.requestFilter);
  64. this.LabelAndTextField("Exclude Ids (optional): ", ref this.requestExcludes);
  65. this.LabelAndTextField("Filters: ", ref this.requestExcludes);
  66. this.LabelAndTextField("Max Recipients (optional): ", ref this.requestMax);
  67. this.LabelAndTextField("Data (optional): ", ref this.requestData);
  68. this.LabelAndTextField("Title (optional): ", ref this.requestTitle);
  69. GUILayout.BeginHorizontal();
  70. GUILayout.Label(
  71. "Request Action (optional): ",
  72. this.LabelStyle,
  73. GUILayout.MaxWidth(200 * this.ScaleFactor));
  74. this.selectedAction = GUILayout.Toolbar(
  75. this.selectedAction,
  76. this.actionTypeStrings,
  77. this.ButtonStyle,
  78. GUILayout.MinHeight(ConsoleBase.ButtonHeight * this.ScaleFactor),
  79. GUILayout.MaxWidth(ConsoleBase.MainWindowWidth - 150));
  80. GUILayout.EndHorizontal();
  81. this.LabelAndTextField("Request Object ID (optional): ", ref this.requestObjectID);
  82. if (this.Button("Custom App Request"))
  83. {
  84. OGActionType? action = this.GetSelectedOGActionType();
  85. if (action != null)
  86. {
  87. FB.AppRequest(
  88. this.requestMessage,
  89. action.Value,
  90. this.requestObjectID,
  91. string.IsNullOrEmpty(this.requestTo) ? null : this.requestTo.Split(','),
  92. this.requestData,
  93. this.requestTitle,
  94. this.HandleResult);
  95. }
  96. else
  97. {
  98. FB.AppRequest(
  99. this.requestMessage,
  100. string.IsNullOrEmpty(this.requestTo) ? null : this.requestTo.Split(','),
  101. string.IsNullOrEmpty(this.requestFilter) ? null : this.requestFilter.Split(',').OfType<object>().ToList(),
  102. string.IsNullOrEmpty(this.requestExcludes) ? null : this.requestExcludes.Split(','),
  103. string.IsNullOrEmpty(this.requestMax) ? 0 : int.Parse(this.requestMax),
  104. this.requestData,
  105. this.requestTitle,
  106. this.HandleResult);
  107. }
  108. }
  109. }
  110. private OGActionType? GetSelectedOGActionType()
  111. {
  112. string actionString = this.actionTypeStrings[this.selectedAction];
  113. if (actionString == OGActionType.SEND.ToString())
  114. {
  115. return OGActionType.SEND;
  116. }
  117. else if (actionString == OGActionType.ASKFOR.ToString())
  118. {
  119. return OGActionType.ASKFOR;
  120. }
  121. else if (actionString == OGActionType.TURN.ToString())
  122. {
  123. return OGActionType.TURN;
  124. }
  125. else
  126. {
  127. return null;
  128. }
  129. }
  130. }
  131. }