FriendView.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. public class FriendView : MonoBehaviour
  9. {
  10. [SerializeField] GameObject myFriendBox;
  11. [SerializeField] GameObject friendRequestBox;
  12. [SerializeField] GameObject friendRecommendBox;
  13. [SerializeField] GameObject searchPlayerBox;
  14. void Awake()
  15. {
  16. InitBtnTabs();
  17. InitMyFriendBox();
  18. InitFriendRequestBox();
  19. InitFriendRecommendBox();
  20. InitSearchPlayerBox();
  21. }
  22. void Start()
  23. {
  24. if (UserPlayer.ins != null) {
  25. UserPlayer.ins.tempData.onUpdate += onUserPlayerTempDataUpdate;
  26. onUserPlayerTempDataUpdate();
  27. }
  28. SetBtnTabSelected(btnTabs[0]);
  29. }
  30. void OnDestroy() {
  31. if (UserPlayer.ins != null) {
  32. UserPlayer.ins.tempData.onUpdate -= onUserPlayerTempDataUpdate;
  33. }
  34. }
  35. void onUserPlayerTempDataUpdate() {
  36. try {
  37. var data = UserPlayer.ins.tempData;
  38. btnTabs[1].transform.Find("FriendTip").gameObject.SetActive(data.hasFriendRequest);
  39. } catch (System.Exception e) {
  40. Debug.LogError(e.Message);
  41. Debug.LogError(e.StackTrace);
  42. }
  43. }
  44. #region myFriend
  45. void InitMyFriendBox() {
  46. myFriendBox.SetActive(false);
  47. GetMyFriendItemPrefab().SetActive(false);
  48. }
  49. Transform GetMyFriendItemParent() {
  50. return myFriendBox.transform.Find("ScrollView/Viewport/Content");
  51. }
  52. GameObject GetMyFriendItemPrefab() {
  53. return GetMyFriendItemParent().Find("Item").gameObject;
  54. }
  55. void EnterMyFriendBox() {
  56. myFriendBox.SetActive(true);
  57. for (int i = 1; i < GetMyFriendItemParent().childCount; i++) {
  58. Destroy(GetMyFriendItemParent().GetChild(i).gameObject);
  59. }
  60. Action<JArray> cb = delegate(JArray list) {
  61. if (list.Count > 0) {
  62. foreach (var itemInfo in list) {
  63. int friendID = itemInfo.Value<int>("friendID");
  64. int avatarID = itemInfo.Value<int>("avatarID");
  65. string nickname = itemInfo.Value<string>("nickname");
  66. bool online = itemInfo.Value<bool>("online");
  67. long offlineTime = itemInfo.Value<long>("offlineTime");
  68. GameObject o = GameObject.Instantiate(GetMyFriendItemPrefab(), GetMyFriendItemParent());
  69. o.SetActive(true);
  70. o.name = friendID.ToString();
  71. o.transform.Find("Avatar/Sprite").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  72. o.transform.Find("Name").GetComponent<Text>().text = nickname;
  73. o.transform.Find("BG0").gameObject.SetActive(!online);
  74. o.transform.Find("BG1").gameObject.SetActive(online);
  75. Text onlineTip = o.transform.Find("OnlineTip").GetComponent<Text>();
  76. onlineTip.text = TimeUtil.GetOfflineTimeStr(offlineTime, online);
  77. onlineTip.color = online ? Color.green : Color.gray;
  78. Button btnDelete = o.transform.Find("BtnDelete").GetComponent<Button>();
  79. btnDelete.onClick.RemoveAllListeners();
  80. btnDelete.onClick.AddListener(delegate() {
  81. AudioMgr.ins.PlayBtn();
  82. Action<bool> cb = delegate(bool res) {
  83. if (res) {
  84. Destroy(o);
  85. }
  86. };
  87. UserPlayer.ins.call("friendComp.deleteMyFriend", new object[]{friendID}, cb);
  88. });
  89. }
  90. } else {
  91. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("好友列表为空"));
  92. }
  93. };
  94. UserPlayer.ins.call("friendComp.getMyFriends", null, cb);
  95. PopupMgr.ins.ClearAllTip();
  96. }
  97. #endregion
  98. #region friendRequest
  99. void InitFriendRequestBox() {
  100. friendRequestBox.SetActive(false);
  101. GetFriendRequestItemPrefab().SetActive(false);
  102. }
  103. Transform GetFriendRequestItemParent() {
  104. return friendRequestBox.transform.Find("ScrollView/Viewport/Content");
  105. }
  106. GameObject GetFriendRequestItemPrefab() {
  107. return GetFriendRequestItemParent().Find("Item").gameObject;
  108. }
  109. void EnterFriendRequestBox() {
  110. friendRequestBox.SetActive(true);
  111. for (int i = 1; i < GetFriendRequestItemParent().childCount; i++) {
  112. Destroy(GetFriendRequestItemParent().GetChild(i).gameObject);
  113. }
  114. Action<JArray> cb = delegate(JArray list) {
  115. if (list.Count > 0) {
  116. foreach (var itemInfo in list) {
  117. int otherID = itemInfo.Value<int>("otherID");
  118. int avatarID = itemInfo.Value<int>("avatarID");
  119. string nickname = itemInfo.Value<string>("nickname");
  120. bool online = itemInfo.Value<bool>("online");
  121. long offlineTime = itemInfo.Value<long>("offlineTime");
  122. GameObject o = GameObject.Instantiate(GetFriendRequestItemPrefab(), GetFriendRequestItemParent());
  123. o.SetActive(true);
  124. o.name = otherID.ToString();
  125. o.transform.Find("Avatar/Sprite").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  126. o.transform.Find("Name").GetComponent<Text>().text = nickname;
  127. o.transform.Find("BG0").gameObject.SetActive(!online);
  128. o.transform.Find("BG1").gameObject.SetActive(online);
  129. Text onlineTip = o.transform.Find("OnlineTip").GetComponent<Text>();
  130. onlineTip.text = TimeUtil.GetOfflineTimeStr(offlineTime, online);
  131. onlineTip.color = online ? Color.green : Color.gray;
  132. Button btnNo = o.transform.Find("BtnNo").GetComponent<Button>();
  133. btnNo.onClick.RemoveAllListeners();
  134. btnNo.onClick.AddListener(delegate() {
  135. AudioMgr.ins.PlayBtn();
  136. UserPlayer.ins.call("friendComp.decideAddFriend", new object[]{false, otherID});
  137. Destroy(o);
  138. UserPlayer.ins.tempData.hasFriendRequest = GetFriendRequestItemParent().childCount - 1 > 1;
  139. });
  140. Button btnYes = o.transform.Find("BtnYes").GetComponent<Button>();
  141. btnYes.onClick.RemoveAllListeners();
  142. btnYes.onClick.AddListener(delegate() {
  143. AudioMgr.ins.PlayBtn();
  144. UserPlayer.ins.call("friendComp.decideAddFriend", new object[]{true, otherID});
  145. Destroy(o);
  146. UserPlayer.ins.tempData.hasFriendRequest = GetFriendRequestItemParent().childCount - 1 > 1;
  147. });
  148. }
  149. } else {
  150. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("好友请求列表为空"));
  151. }
  152. };
  153. UserPlayer.ins.call("friendComp.getFriendRequestList", null, cb);
  154. PopupMgr.ins.ClearAllTip();
  155. }
  156. #endregion
  157. #region friendRecommend
  158. void InitFriendRecommendBox() {
  159. friendRecommendBox.SetActive(false);
  160. GetFriendRecommendItemPrefab().SetActive(false);
  161. }
  162. Transform GetFriendRecommendItemParent() {
  163. return friendRecommendBox.transform.Find("ScrollView/Viewport/Content");
  164. }
  165. GameObject GetFriendRecommendItemPrefab() {
  166. return GetFriendRecommendItemParent().Find("Item").gameObject;
  167. }
  168. void EnterFriendRecommendBox() {
  169. friendRecommendBox.SetActive(true);
  170. for (int i = 1; i < GetFriendRecommendItemParent().childCount; i++) {
  171. Destroy(GetFriendRecommendItemParent().GetChild(i).gameObject);
  172. }
  173. Action<JArray> cb = delegate(JArray list) {
  174. for (int i = 1; i < GetFriendRecommendItemParent().childCount; i++) {
  175. Destroy(GetFriendRecommendItemParent().GetChild(i).gameObject);
  176. }
  177. if (list.Count > 0) {
  178. foreach (var itemInfo in list) {
  179. int userID = itemInfo.Value<int>("id");
  180. int avatarID = itemInfo.Value<int>("avatarID");
  181. string nickname = itemInfo.Value<string>("nickname");
  182. bool online = itemInfo.Value<bool>("online");
  183. long offlineTime = itemInfo.Value<long>("offlineTime");
  184. int playCount = itemInfo.Value<int>("playCount");
  185. GameObject o = GameObject.Instantiate(GetFriendRecommendItemPrefab(), GetFriendRecommendItemParent());
  186. o.SetActive(true);
  187. o.name = userID.ToString();
  188. o.transform.Find("Avatar/Sprite").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  189. o.transform.Find("Name").GetComponent<Text>().text = nickname;
  190. o.transform.Find("PlayCount").GetComponent<Text>().text =
  191. String.Format(
  192. TextAutoLanguage2.GetTextByKey("friend_record_play-again-count"), playCount
  193. );
  194. o.transform.Find("BG0").gameObject.SetActive(!online);
  195. o.transform.Find("BG1").gameObject.SetActive(online);
  196. Text onlineTip = o.transform.Find("OnlineTip").GetComponent<Text>();
  197. onlineTip.text = TimeUtil.GetOfflineTimeStr(offlineTime, online);
  198. onlineTip.color = online ? Color.green : Color.gray;
  199. Button btnYes = o.transform.Find("BtnAdd").GetComponent<Button>();
  200. btnYes.onClick.RemoveAllListeners();
  201. btnYes.onClick.AddListener(delegate() {
  202. AudioMgr.ins.PlayBtn();
  203. UserPlayer.ins.call("friendComp.requestAddFriend", userID);
  204. btnYes.interactable = false;
  205. });
  206. }
  207. } else {
  208. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("没有可推荐的好友"));
  209. }
  210. };
  211. UserPlayer.ins.call("friendComp.getFriendRecommendList", null, cb);
  212. PopupMgr.ins.ClearAllTip();
  213. }
  214. #endregion
  215. #region searchPlayer
  216. void InitSearchPlayerBox() {
  217. searchPlayerBox.SetActive(false);
  218. GetSearchPlayerItemPrefab().SetActive(false);
  219. }
  220. Transform GetSearchPlayerItemParent() {
  221. return searchPlayerBox.transform.Find("ScrollView/Viewport/Content");
  222. }
  223. GameObject GetSearchPlayerItemPrefab() {
  224. return GetSearchPlayerItemParent().Find("Item").gameObject;
  225. }
  226. void EnterSearchPlayerBox() {
  227. searchPlayerBox.SetActive(true);
  228. PopupMgr.ins.ClearAllTip();
  229. }
  230. InputField GetSearchInputField() {
  231. return searchPlayerBox.transform.Find("SearchBox/InputField").GetComponent<InputField>();
  232. }
  233. long _lastSearchTime = 0;
  234. public void btnEvent_SearchPlayer() {
  235. AudioMgr.ins.PlayBtn();
  236. if (JC.CS.Utility.GetTimestamp() - _lastSearchTime < 3000) {
  237. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  238. return;
  239. } else {
  240. _lastSearchTime = JC.CS.Utility.GetTimestamp();
  241. }
  242. string searchText = GetSearchInputField().text;
  243. Action<JArray> cb = delegate(JArray list) {
  244. for (int i = 1; i < GetSearchPlayerItemParent().childCount; i++) {
  245. Destroy(GetSearchPlayerItemParent().GetChild(i).gameObject);
  246. }
  247. if (list.Count > 0) {
  248. foreach (var itemInfo in list) {
  249. int userID = itemInfo.Value<int>("id");
  250. int avatarID = itemInfo.Value<int>("avatarID");
  251. string nickname = itemInfo.Value<string>("nickname");
  252. bool online = itemInfo.Value<bool>("online");
  253. long offlineTime = itemInfo.Value<long>("offlineTime");
  254. GameObject o = GameObject.Instantiate(GetSearchPlayerItemPrefab(), GetSearchPlayerItemParent());
  255. o.SetActive(true);
  256. o.name = userID.ToString();
  257. o.transform.Find("Avatar/Sprite").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  258. o.transform.Find("Name").GetComponent<Text>().text = nickname;
  259. o.transform.Find("BG0").gameObject.SetActive(!online);
  260. o.transform.Find("BG1").gameObject.SetActive(online);
  261. Text onlineTip = o.transform.Find("OnlineTip").GetComponent<Text>();
  262. onlineTip.text = TimeUtil.GetOfflineTimeStr(offlineTime, online);
  263. onlineTip.color = online ? Color.green : Color.gray;
  264. Button btnYes = o.transform.Find("BtnAdd").GetComponent<Button>();
  265. btnYes.onClick.RemoveAllListeners();
  266. btnYes.onClick.AddListener(delegate() {
  267. AudioMgr.ins.PlayBtn();
  268. UserPlayer.ins.call("friendComp.requestAddFriend", userID);
  269. btnYes.interactable = false;
  270. });
  271. }
  272. } else {
  273. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("没有搜索到目标玩家"));
  274. }
  275. };
  276. UserPlayer.ins.call("friendComp.searchPlayersByID", new object[]{searchText}, cb);
  277. }
  278. #endregion
  279. void HideAllBox() {
  280. myFriendBox.SetActive(false);
  281. friendRequestBox.SetActive(false);
  282. friendRecommendBox.SetActive(false);
  283. searchPlayerBox.SetActive(false);
  284. }
  285. public void Back() {
  286. AudioMgr.ins.PlayBtn();
  287. Destroy(this.gameObject);
  288. }
  289. #region 左栏条目切换
  290. [SerializeField] Sprite[] btnTabTextures;
  291. Button[] btnTabs = {null, null, null, null};
  292. void InitBtnTabs() {
  293. btnTabs[0] = this.transform.Find("LineV/BtnTab0").GetComponent<Button>();
  294. btnTabs[1] =this.transform.Find("LineV/BtnTab1").GetComponent<Button>();
  295. btnTabs[2] =this.transform.Find("LineV/BtnTab2").GetComponent<Button>();
  296. btnTabs[3] =this.transform.Find("LineV/BtnTab3").GetComponent<Button>();
  297. for (int i = 0; i < btnTabs.Length; i++) {
  298. Button btnTab = btnTabs[i];
  299. int btnTabIndex = i;
  300. btnTab.onClick.AddListener(() => {
  301. AudioMgr.ins.PlayBtn();
  302. if (!IsBtnTabSelected(btnTab)) {
  303. SetBtnTabSelected(btnTab);
  304. }
  305. });
  306. }
  307. }
  308. bool IsBtnTabSelected(Button btn) {
  309. Image img = btn.GetComponent<Image>();
  310. return img.sprite.name.Equals(btnTabTextures[1].name);
  311. }
  312. void SetBtnTabSelected(Button btn) {
  313. foreach (var item in btnTabs) {
  314. Image img = item.GetComponent<Image>();
  315. img.sprite = item == btn ? btnTabTextures[1] : btnTabTextures[0];
  316. }
  317. int index = Array.IndexOf(btnTabs, btn);
  318. HandleBtnTabSelectedLogic(index);
  319. }
  320. void HandleBtnTabSelectedLogic(int btnIndex) {
  321. HideAllBox();
  322. if (btnIndex == 0) {
  323. EnterMyFriendBox();
  324. } else if (btnIndex == 1) {
  325. EnterFriendRequestBox();
  326. } else if (btnIndex == 2) {
  327. EnterFriendRecommendBox();
  328. } else if (btnIndex == 3) {
  329. EnterSearchPlayerBox();
  330. }
  331. }
  332. #endregion
  333. }