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.
205 lines
6.0 KiB
205 lines
6.0 KiB
using Grpc.Core;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
using Utilities;
|
|
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 ConnectionService.Connection.ConnectionClient connection;
|
|
private Lobby.Lobby.LobbyClient lobby_client;
|
|
private Channel channel;
|
|
private string connId;
|
|
private uint? lobby = null;
|
|
private string address;
|
|
|
|
private Modifiable<Lobby.LobbyStatus> lobby_status = null;
|
|
public Connection(string user, string address)
|
|
{
|
|
channel = new Channel(address, ChannelCredentials.Insecure);
|
|
connection = new ConnectionService.Connection.ConnectionClient(channel);
|
|
lobby_client = new Lobby.Lobby.LobbyClient(channel);
|
|
connId = connection.connect(new Common.Name { Name_ = user }).Id;
|
|
this.address = address;
|
|
}
|
|
|
|
public string Name() {
|
|
return connection.name(new Google.Protobuf.WellKnownTypes.Empty()).Name_;
|
|
}
|
|
public string GetServerAddress() {
|
|
return address;
|
|
}
|
|
|
|
public void JoinLobby(string code)
|
|
{
|
|
lobby = Base32.FromString(code);
|
|
connection.joinLobbyWithCode(new ConnectionService.LobbyCode { Code = (uint)lobby }, new Metadata { new Metadata.Entry("client_id", connId) });
|
|
}
|
|
|
|
public string CreateLobby(bool isPublic)
|
|
{
|
|
lobby = connection.createLobby(new ConnectionService.LobbyConfig {Public = isPublic}, 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<string> GetUsersInSameLobby(){
|
|
AsyncServerStreamingCall<Common.Name> stream = lobby_client.users(new Google.Protobuf.WellKnownTypes.Empty(), new Metadata { new Metadata.Entry("client_id", connId) });
|
|
List<string> l = new List<string>();
|
|
|
|
while (runSync(stream.ResponseStream.MoveNext()))
|
|
{
|
|
Common.Name g = stream.ResponseStream.Current;
|
|
l.Add(g.Name_);
|
|
// Debug.Log("Received " + feature.ToString());
|
|
}
|
|
// Debug.Log(stream.ResponseStream.Current);
|
|
return l;
|
|
}
|
|
|
|
public Lobby.LobbyStatus GetLobbyStatus() {
|
|
if (lobby != null) {
|
|
if (lobby_status != null) {
|
|
var hasNew = lobby_client.hasNewStatus(
|
|
new Lobby.LastStatusTimestamp {
|
|
Time = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTime(lobby_status.GetLastModfication()),
|
|
Lobby = (uint)lobby,
|
|
},
|
|
new Metadata { new Metadata.Entry("client_id", connId) }
|
|
).Value;
|
|
// Debug.Log("HasNewStatus: " + hasNew);
|
|
if (hasNew) {
|
|
lobby_status.Set(lobby_client.getStatus(new Google.Protobuf.WellKnownTypes.Empty(), new Metadata { new Metadata.Entry("client_id", connId) }));
|
|
}
|
|
} else {
|
|
Debug.Log("Getting status");
|
|
lobby_status = new Modifiable<Lobby.LobbyStatus>(lobby_client.getStatus(new Google.Protobuf.WellKnownTypes.Empty(), new Metadata { new Metadata.Entry("client_id", connId) }));
|
|
}
|
|
Debug.Log(lobby_status.Get());
|
|
return lobby_status.Get();
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public List<Lobby.Vote> GetLobbyVotes(uint game) {
|
|
Google.Protobuf.Collections.RepeatedField<Lobby.Vote> votes = GetLobbyStatus().Votes;
|
|
var gameVotes = new List<Lobby.Vote>();
|
|
foreach (Lobby.Vote vote in votes) {
|
|
if (vote.Game == game) {
|
|
gameVotes.Add(vote);
|
|
}
|
|
}
|
|
return gameVotes;
|
|
}
|
|
|
|
public int GetLobbyVoteCount(uint game) {
|
|
Google.Protobuf.Collections.RepeatedField<Lobby.Vote> votes = GetLobbyStatus().Votes;
|
|
var gameVotes = new List<Lobby.Vote>();
|
|
foreach(Lobby.Vote vote in votes) {
|
|
if (vote.Game == game) {
|
|
gameVotes.Add(vote);
|
|
}
|
|
}
|
|
return gameVotes.Count;
|
|
}
|
|
public Lobby.Vote GetSelfVote(string player) {
|
|
Google.Protobuf.Collections.RepeatedField<Lobby.Vote> votes = GetLobbyStatus().Votes;
|
|
foreach (Lobby.Vote vote in votes) {
|
|
Debug.Log(vote);
|
|
if (vote.Player == player) {
|
|
return vote;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public void Vote(uint game) {
|
|
lobby_client.vote(new Lobby.SingleVote() { Game = game }, new Metadata { new Metadata.Entry("client_id", connId) });
|
|
}
|
|
public void SetReady() {
|
|
lobby_client.ready(new Google.Protobuf.WellKnownTypes.Empty(), new Metadata { new Metadata.Entry("client_id", connId) });
|
|
}
|
|
|
|
public void LeaveLobby() {
|
|
lobby_client.leave(new Google.Protobuf.WellKnownTypes.Empty(), new Metadata { new Metadata.Entry("client_id", connId) });
|
|
lobby = null;
|
|
}
|
|
|
|
public List<string> GetPublicLobbies() {
|
|
AsyncServerStreamingCall<ConnectionService.LobbyCode> stream = connection.getPublicLobbies(new Google.Protobuf.WellKnownTypes.Empty());
|
|
List<string> l = new List<string>();
|
|
|
|
while (runSync(stream.ResponseStream.MoveNext()))
|
|
{
|
|
ConnectionService.LobbyCode g = stream.ResponseStream.Current;
|
|
l.Add(Base32.ToString(g.Code));
|
|
// Debug.Log("Received " + feature.ToString());
|
|
}
|
|
// Debug.Log(stream.ResponseStream.Current);
|
|
return l;
|
|
}
|
|
|
|
public List<ConnectionService.Game> GetGames()
|
|
{
|
|
AsyncServerStreamingCall<ConnectionService.Game> stream = connection.getGames(new Google.Protobuf.WellKnownTypes.Empty());
|
|
List<ConnectionService.Game> l = new List<ConnectionService.Game>();
|
|
|
|
while (runSync(stream.ResponseStream.MoveNext()))
|
|
{
|
|
ConnectionService.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()
|
|
{
|
|
connection.disconnect(new Google.Protobuf.WellKnownTypes.Empty(), new Metadata { new Metadata.Entry("client_id", connId) });
|
|
channel.ShutdownAsync().Wait();
|
|
}
|
|
}
|
|
|
|
|
|
}
|