r2/DemoUnity/Assets/Scripts/LoginGUI.cs
lightings 3620fdae36 ...
2023-04-12 14:37:56 +08:00

561 lines
15 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using Pomelo.DotNetClient;
using SimpleJson;
using StarterAssets;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class LoginGUI : MonoBehaviour {
public static PomeloClient pc = null;
public TMP_InputField username;
public TMP_InputField password;
public TMP_InputField guildname;
public TMP_Text guildinfo;
public TMP_InputField param1;
public TMP_InputField param2;
public TMP_InputField param3;
public TMP_InputField param4;
public TMP_InputField param5;
public TMP_InputField teamId;
public TMP_InputField sendToWorld;
public TMP_InputField sendToTeam;
public TMP_Text chatContent;
public GameObject[] panels;
private BindingList<JsonObject> listItems = new BindingList<JsonObject>();
public Transform contentPanel;
private GameObject selectedItem;
public Color normalBackgroundColor;
public Color selectedBackgroundColor;
private int state = 0;
// Start is called before the first frame update
void Start()
{
panels[0].SetActive(true);
panels[1].SetActive(false);
panels[2].SetActive(false);
panels[3].SetActive(false);
listItems.OnListChanged += UpdateUI;
}
// Update is called once per frame
void Update()
{
switch (state)
{
case 0:
break;
case 1:
panels[0].SetActive(false);
panels[1].SetActive(true);
state = 2;
break;
case 2:
break;
case 3:
panels[0].SetActive(true);
panels[1].SetActive(false);
state = 0;
break;
case 4:
panels[1].SetActive(false);
panels[2].SetActive(true);
state = 0;
break;
case 5:
panels[1].SetActive(false);
panels[3].SetActive(true);
state = 0;
break;
}
}
private void UpdateUI()
{
foreach (Transform child in contentPanel)
{
Destroy(child.gameObject);
}
foreach (JsonObject text in listItems)
{
GameObject newItem = new GameObject("Guild");
newItem.AddComponent<RectTransform>();
GuildData d = newItem.AddComponent<GuildData>();
d.guildId = Convert.ToInt32(text["idx"]);
Image bgImage = newItem.AddComponent<Image>();
bgImage.color = normalBackgroundColor;
GameObject textObject = new GameObject("Text");
textObject.transform.SetParent(newItem.transform, false);
TMP_Text textComponent = textObject.AddComponent<TextMeshProUGUI>();
textComponent.text = (string)text["gname"];
textComponent.fontSize = 24;
textComponent.color = Color.black;
textComponent.alignment = TextAlignmentOptions.Center;
textComponent.rectTransform.anchorMin = Vector2.zero;
textComponent.rectTransform.anchorMax = Vector2.one;
textComponent.rectTransform.offsetMin = Vector2.zero;
textComponent.rectTransform.offsetMax = Vector2.zero;
Button itemButton = newItem.AddComponent<Button>();
itemButton.targetGraphic = bgImage;
itemButton.onClick.AddListener(() => SelectItem(newItem));
newItem.transform.SetParent(contentPanel, false);
}
}
public void Login()
{
string host = "bee.local";
int port = 3999;
// if (pc != null)
// {
// pc.disconnect();
// }
pc = new PomeloClient();
print("login - " + username.text + ":" + password.text);
pc.initClient(host, port, () => {
JsonObject user = new JsonObject();
pc.connect(data => {
print(data);
JsonObject msg = new JsonObject();
msg["uid"] = username.text;
msg["pwd"] = password.text;
pc.request("gate.gateHandler.queryEntry", msg, o => {
print(o);
host = Convert.ToString(o["host"]);
port = Convert.ToInt32(o["port"]);
pc.disconnect();
pc = new PomeloClient();
pc.NetWorkStateChangedEvent += OnNetworkStateChanged;
pc.initClient(host,port, () => {
pc.on("onChat", OnChat);
pc.connect(data => {
pc.request("connector.entryHandler.entry", msg, o => {
print(o);
OnLogined(o);
});
});
});
});
});
});
}
void OnLogined(JsonObject result)
{
int port = Convert.ToInt32(result["code"]);
if (port == 200)
{
print("login succeed");
state = 1;
RefreshGuildList();
}
else
{
print("login failed");
pc.disconnect();
}
}
public void OnToFriend()
{
state = 4;
}
public void OnToChat()
{
state = 5;
}
void OnNetworkStateChanged(NetWorkState state)
{
print(state);
}
public void CreateGuild()
{
JsonObject msg = new JsonObject();
msg["guildName"] = guildname.text;
pc.request("guild.guildHandler.createGuild", msg, o => {
print(o);
});
}
public void RefreshGuildList()
{
JsonObject msg = new JsonObject();
print("request listGuild");
pc.request("guild.guildHandler.listGuild", msg, o => {
MainThreadDispatcher.Enqueue(() => {
listItems.Clear();
print(o);
JsonArray guilds = (JsonArray)o["guilds"];
foreach (var g in guilds)
{
AddGuild((JsonObject)g);
}
});
});
}
private void AddGuild(JsonObject guild)
{
listItems.Add(guild);
}
private void SelectItem(GameObject item)
{
if (selectedItem != null)
{
selectedItem.GetComponent<Image>().color = normalBackgroundColor;
}
selectedItem = item;
selectedItem.GetComponent<Image>().color = selectedBackgroundColor;
GuildData d = selectedItem.GetComponent<GuildData>();
JsonObject msg = new JsonObject();
msg["guildId"] = d.guildId;
guildinfo.text = "";
msg["apply"] = 2;
pc.request("guild.guildHandler.getGuildMembers", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
guildinfo.text += o.ToString();
});
});
msg["apply"] = 0;
pc.request("guild.guildHandler.getGuildMembers", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
guildinfo.text += o.ToString();
});
});
}
public void JoinGuild()
{
if (selectedItem)
{
GuildData d = selectedItem.GetComponent<GuildData>();
JsonObject msg = new JsonObject();
msg["guildId"] = d.guildId;
pc.request("guild.guildHandler.joinGuild", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
}
public void InviteNewMember()
{
if (selectedItem)
{
GuildData d = selectedItem.GetComponent<GuildData>();
JsonObject msg = new JsonObject();
msg["guildId"] = d.guildId;
msg["newuid"] = param1.text;
pc.request("guild.guildHandler.inviteNewMember", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
}
public void ApproveMember()
{
GuildData d = selectedItem.GetComponent<GuildData>();
JsonObject msg = new JsonObject();
msg["guildId"] = d.guildId;
msg["newuid"] = param1.text;
pc.request("guild.guildHandler.approveGuild", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
public void ListInvites()
{
JsonObject msg = new JsonObject();
pc.request("guild.guildHandler.getInviteList", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
public void ConfirmInvite()
{
GuildData d = selectedItem.GetComponent<GuildData>();
JsonObject msg = new JsonObject();
msg["guildId"] = d.guildId;
pc.request("guild.guildHandler.confirmInvite", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
public void LeaveGuild()
{
JsonObject msg = new JsonObject();
pc.request("guild.guildHandler.leaveGuild", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
public void Kick()
{
JsonObject msg = new JsonObject();
msg["muid"] = "6513_2006_md9nVaGIooStTM1D56NLblLosFhWhgxB";
pc.request("guild.guildHandler.kick", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
public void disbandGuild()
{
JsonObject msg = new JsonObject();
pc.request("guild.guildHandler.disbandGuild", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
// 发出好友邀请
public void sendFriendRequest()
{
JsonObject msg = new JsonObject();
msg["fid"] = param4.text;
pc.request("friend.friendHandler.sendFriendRequest", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
// 接受好友邀请
public void acceptFriendRequest()
{
JsonObject msg = new JsonObject();
msg["fid"] = param4.text;
pc.request("friend.friendHandler.acceptFriendRequest", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
// 拒绝好友邀请
public void rejectFriendRequest()
{
JsonObject msg = new JsonObject();
pc.request("friend.friendHandler.rejectFriendRequest", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
// 删除好友
public void deleteFriend()
{
JsonObject msg = new JsonObject();
pc.request("friend.friendHandler.deleteFriend", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
// 获取好友列表
public void getFriendList()
{
JsonObject msg = new JsonObject();
pc.request("friend.friendHandler.getFriendList", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
// 获取好友申请列表
public void getFriendRequestList()
{
JsonObject msg = new JsonObject();
pc.request("friend.friendHandler.getPendingFriendRequests", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
// 获取好友邀请表
public void getFriendInvitationList()
{
JsonObject msg = new JsonObject();
pc.request("friend.friendHandler.getPendingFriendInvitations", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
// 增加到黑名单
public void addToBlackList()
{
JsonObject msg = new JsonObject();
msg["bid"] = param4.text;
pc.request("friend.friendHandler.addToBlackList", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
// 从黑名单移除
public void removeFromBlackList()
{
JsonObject msg = new JsonObject();
msg["bid"] = param4.text;
pc.request("friend.friendHandler.removeFromBlackList", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
// 获取黑名单列表
public void getBlackList()
{
JsonObject msg = new JsonObject();
pc.request("friend.friendHandler.getBlackList", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
// 加入世界聊天
public void joinWorldChat()
{
JsonObject msg = new JsonObject();
pc.request("chat.chatHandler.joinWorldChannel", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
// 离开世界聊天
public void leaveWorldChat()
{
JsonObject msg = new JsonObject();
pc.request("chat.chatHandler.leaveWorldChannel", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
// 加入组队聊天
public void joinTeamChat()
{
JsonObject msg = new JsonObject();
msg["teamId"] = teamId.text;
pc.request("chat.chatHandler.joinTeamChannel", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
// 离开组队聊天
public void leaveTeamChat()
{
JsonObject msg = new JsonObject();
msg["teamId"] = teamId.text;
pc.request("chat.chatHandler.leaveTeamChannel", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
// 发送世界聊天
public void sendWorldChat()
{
JsonObject msg = new JsonObject();
msg["content"] = sendToWorld.text;
pc.request("chat.chatHandler.worldChat", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
// 发送组队聊天
public void sendTeamChat()
{
JsonObject msg = new JsonObject();
msg["content"] = sendToTeam.text;
msg["teamId"] = teamId.text;
pc.request("chat.chatHandler.teamChat", msg, o => {
MainThreadDispatcher.Enqueue(() => {
print(o);
});
});
}
void OnChat(JsonObject data)
{
MainThreadDispatcher.Enqueue(() => {
print(data);
switch ((string)data["type"])
{
case "world":
print("world chat");
chatContent.text += "world: " + (string)data["content"] + "\n";
break;
case "team":
print("team chat");
chatContent.text += "team("+data["teamId"]+"): " + (string)data["content"] + "\n";
break;
}
});
}
}