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.
99 lines
2.2 KiB
99 lines
2.2 KiB
using Grpc.Core;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
public static class Client
|
|
{
|
|
private static Connection reference;
|
|
|
|
public static void Connect(string name, string address = "127.0.0.1:50052")
|
|
{
|
|
reference = new Connection(name, address);
|
|
}
|
|
|
|
public static ref Connection GetConnection()
|
|
{
|
|
return ref reference;
|
|
}
|
|
|
|
public static void CloseConnection()
|
|
{
|
|
if (reference != null)
|
|
{
|
|
reference.Close();
|
|
reference = null;
|
|
}
|
|
}
|
|
|
|
public class Connection // : MonoBehaviour
|
|
{
|
|
private Game.Connection.ConnectionClient connection;
|
|
private Game.Lobby.LobbyClient lobby_client;
|
|
private Channel channel;
|
|
private string connId;
|
|
private uint? lobby = null;
|
|
public Connection(string user, string address)
|
|
{
|
|
channel = new Channel(address, ChannelCredentials.Insecure);
|
|
connection = new Game.Connection.ConnectionClient(channel);
|
|
lobby_client = new Game.Lobby.LobbyClient(channel);
|
|
connId = connection.connect(new Game.Name { Name_ = user }).Id;
|
|
}
|
|
|
|
public string Name() {
|
|
return connection.name(new Game.Null()).Name_;
|
|
}
|
|
|
|
public void JoinLobby(string code)
|
|
{
|
|
lobby = Base32.FromString(code);
|
|
connection.joinLobbyWithCode(new Game.LobbyCode { Code = (uint)lobby }, new Metadata { new Metadata.Entry("client_id", connId) });
|
|
}
|
|
|
|
public string CreateLobby()
|
|
{
|
|
lobby = connection.joinLobbyWithoutCode(new Game.Null(), new Metadata { new Metadata.Entry("client_id", connId) }).Code;
|
|
return Base32.ToString((uint)lobby);
|
|
}
|
|
|
|
public string GetLobby()
|
|
{
|
|
if (lobby != null)
|
|
{
|
|
return Base32.ToString((uint)lobby);
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public List<Game.Game> getGames()
|
|
{
|
|
AsyncServerStreamingCall<Game.Game> stream = lobby_client.getGames(new Game.Null());
|
|
List<Game.Game> l = new List<Game.Game>();
|
|
|
|
while (runSync(stream.ResponseStream.MoveNext()))
|
|
{
|
|
Game.Game g = stream.ResponseStream.Current;
|
|
l.Add(g);
|
|
// Debug.Log("Received " + feature.ToString());
|
|
}
|
|
// Debug.Log(stream.ResponseStream.Current);
|
|
return l;
|
|
}
|
|
|
|
static T runSync<T>(Task<T> task)
|
|
{
|
|
task.Wait();
|
|
return task.Result;
|
|
}
|
|
|
|
public void Close()
|
|
{
|
|
channel.ShutdownAsync().Wait();
|
|
}
|
|
}
|
|
|
|
|
|
}
|