You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
3.0 KiB
83 lines
3.0 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using UnityEngine;
|
|
using Discord;
|
|
|
|
public class DiscordController : MonoBehaviour {
|
|
|
|
[System.NonSerialized]
|
|
public Discord.ActivityManager activityManager;
|
|
public Discord.Discord discord;
|
|
public Discord.RelationshipManager relationshipManager;
|
|
public List<Discord.Relationship> friends = new List<Discord.Relationship>();
|
|
public Discord.LobbyManager lobbyManager;
|
|
|
|
|
|
void Awake() {
|
|
DontDestroyOnLoad(this.gameObject);
|
|
}
|
|
// Use this for initialization
|
|
void Start() {
|
|
|
|
Debug.Log("Starting discord");
|
|
discord = new Discord.Discord(778707900293971979, (uint)Discord.CreateFlags.NoRequireDiscord);
|
|
Debug.Log("Started discord");
|
|
activityManager = discord.GetActivityManager();
|
|
relationshipManager = discord.GetRelationshipManager();
|
|
var userManager = discord.GetUserManager();
|
|
Discord.User currentUser = new Discord.User();
|
|
lobbyManager = discord.GetLobbyManager();
|
|
activityManager.RegisterCommand("cards-game://run");
|
|
relationshipManager.OnRefresh += () => {
|
|
friends.Clear();
|
|
relationshipManager.Filter((ref Discord.Relationship relationship) => {
|
|
return relationship.Type == Discord.RelationshipType.Friend && relationship.Presence.Status != Discord.Status.Offline;
|
|
});
|
|
|
|
for (var i = 0; i < relationshipManager.Count(); i++) {
|
|
var r = relationshipManager.GetAt((uint)i);
|
|
friends.Add(r);
|
|
|
|
//Debug.Log(r.Presence.Activity.Type);
|
|
}
|
|
};
|
|
|
|
userManager.OnCurrentUserUpdate += () => {
|
|
currentUser = userManager.GetCurrentUser();
|
|
};
|
|
|
|
activityManager.OnActivityJoin += secret => {
|
|
lobbyManager.ConnectLobbyWithActivitySecret(secret, (Discord.Result result, ref Discord.Lobby discordLobby) => {
|
|
Debug.Log(secret);
|
|
if (result == Result.Ok) {
|
|
var ip = lobbyManager.GetLobbyMetadataValue(discordLobby.Id, "server");
|
|
if (ip.Split(':')[0] == "127.0.0.1" || ip.Split(':')[0] == "localhost") {
|
|
ip = new WebClient().DownloadString("http://ipinfo.io/ip").Trim() + ":" + ip.Split(':')[1];
|
|
Debug.Log(ip);
|
|
}
|
|
Client.Connect(currentUser.Username, ip);
|
|
var conn = Client.GetConnection();
|
|
if (conn != null) {
|
|
MainMenuController mmc = GameObject.Find("MainMenuController").GetComponent<MainMenuController>();
|
|
if (lobbyManager.GetLobbyMetadataValue(discordLobby.Id, "lobby") != "" || lobbyManager.GetLobbyMetadataValue(discordLobby.Id, "lobby") != null) {
|
|
mmc.ConnectLobby(lobbyManager.GetLobbyMetadataValue(discordLobby.Id, "lobby"));
|
|
}
|
|
}
|
|
|
|
Debug.Log(discordLobby.Id);
|
|
lobbyManager.ConnectNetwork(discordLobby.Id);
|
|
lobbyManager.OpenNetworkChannel(discordLobby.Id, 0, true);
|
|
DiscordUtils.UpdateActivityLobby(discord, discordLobby, secret);
|
|
}else {
|
|
Debug.LogError("Error: "+result+"; lobbyId: "+discordLobby.Id);
|
|
}
|
|
});
|
|
};
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update() {
|
|
discord.RunCallbacks();
|
|
}
|
|
}
|
|
|