@ -1,3 +1,4 @@
using System.Linq ;
using System.Collections.Generic ;
using System.Collections.Concurrent ;
using System.Threading.Tasks ;
@ -13,6 +14,13 @@ public class Client : MonoBehaviour {
public static void Connect ( string name , string address = "127.0.0.1:50052" ) {
reference = new ConnectionImpl ( name , address ) ;
reference . eventManager . AddHandler ( "general_lobby_status_handler" , ( Lobby . LobbyStatus status ) = > {
reference . lobby_status . Set ( status ) ;
} ) ;
reference . eventManager . AddHandler ( "general_game_status_handler" , ( Game . GameStatus status ) = > {
reference . game_status . Set ( status ) ;
GameLoader . ReloadPiles ( ) ;
} ) ;
}
public static ref ConnectionImpl GetConnection ( ) {
@ -42,6 +50,10 @@ public class Client : MonoBehaviour {
private Dictionary < string , Action < Connection . LobbyCode > > returnCreateLobbyHandlers = new Dictionary < string , Action < Connection . LobbyCode > > ( ) ;
private Dictionary < string , Action < Protocol . Games > > returnGamesHandlers = new Dictionary < string , Action < Protocol . Games > > ( ) ;
private Dictionary < string , Action < Protocol . LobbyCodes > > returnPublicLobbiesHandlers = new Dictionary < string , Action < Protocol . LobbyCodes > > ( ) ;
private Dictionary < string , Action < Protocol . Names > > returnUsersHandlers = new Dictionary < string , Action < Protocol . Names > > ( ) ;
private Dictionary < string , Action < Lobby . LobbyStatus > > lobbyStatusHandlers = new Dictionary < string , Action < Lobby . LobbyStatus > > ( ) ;
private Dictionary < string , Action < Game . Image > > returnCardImageHandlers = new Dictionary < string , Action < Game . Image > > ( ) ;
private Dictionary < string , Action < Game . GameStatus > > gameStatusHandlers = new Dictionary < string , Action < Game . GameStatus > > ( ) ;
public ConcurrentQueue < Protocol . ServerClientPacket > queue ;
public void Update ( ) {
@ -56,7 +68,6 @@ public class Client : MonoBehaviour {
n ( p . ReturnName ) ;
}
}
break ;
case Protocol . ServerClientPacket . DataOneofCase . ReturnConnect : {
var v = new Action < Connection . UserID > [ this . returnConnectHandlers . Count ] ;
@ -90,6 +101,38 @@ public class Client : MonoBehaviour {
}
}
break ;
case Protocol . ServerClientPacket . DataOneofCase . ReturnUsers : {
var v = new Action < Protocol . Names > [ this . returnUsersHandlers . Count ] ;
this . returnUsersHandlers . Values . CopyTo ( v , 0 ) ;
foreach ( var n in v ) {
n ( p . ReturnUsers ) ;
}
}
break ;
case Protocol . ServerClientPacket . DataOneofCase . LobbyStatus : {
var v = new Action < Lobby . LobbyStatus > [ this . lobbyStatusHandlers . Count ] ;
this . lobbyStatusHandlers . Values . CopyTo ( v , 0 ) ;
foreach ( var n in v ) {
n ( p . LobbyStatus ) ;
}
}
break ;
case Protocol . ServerClientPacket . DataOneofCase . ReturnCardImage : {
var v = new Action < Game . Image > [ this . returnCardImageHandlers . Count ] ;
this . returnCardImageHandlers . Values . CopyTo ( v , 0 ) ;
foreach ( var n in v ) {
n ( p . ReturnCardImage ) ;
}
}
break ;
case Protocol . ServerClientPacket . DataOneofCase . GameStatus : {
var v = new Action < Game . GameStatus > [ this . gameStatusHandlers . Count ] ;
this . gameStatusHandlers . Values . CopyTo ( v , 0 ) ;
foreach ( var n in v ) {
n ( p . GameStatus ) ;
}
}
break ;
default :
break ;
}
@ -116,6 +159,22 @@ public class Client : MonoBehaviour {
returnPublicLobbiesHandlers . Add ( name , handler ) ;
}
public void AddHandler ( string name , Action < Protocol . Names > handler ) {
returnUsersHandlers . Add ( name , handler ) ;
}
public void AddHandler ( string name , Action < Lobby . LobbyStatus > handler ) {
lobbyStatusHandlers . Add ( name , handler ) ;
}
public void AddHandler ( string name , Action < Game . Image > handler ) {
returnCardImageHandlers . Add ( name , handler ) ;
}
public void AddHandler ( string name , Action < Game . GameStatus > handler ) {
gameStatusHandlers . Add ( name , handler ) ;
}
public void RemoveHandler ( Protocol . ServerClientPacket . DataOneofCase kind , string name ) {
switch ( kind ) {
case Protocol . ServerClientPacket . DataOneofCase . ReturnName :
@ -133,6 +192,18 @@ public class Client : MonoBehaviour {
case Protocol . ServerClientPacket . DataOneofCase . ReturnPublicLobbies :
returnPublicLobbiesHandlers . Remove ( name ) ;
break ;
case Protocol . ServerClientPacket . DataOneofCase . ReturnUsers :
returnUsersHandlers . Remove ( name ) ;
break ;
case Protocol . ServerClientPacket . DataOneofCase . LobbyStatus :
lobbyStatusHandlers . Remove ( name ) ;
break ;
case Protocol . ServerClientPacket . DataOneofCase . ReturnCardImage :
returnCardImageHandlers . Remove ( name ) ;
break ;
case Protocol . ServerClientPacket . DataOneofCase . GameStatus :
gameStatusHandlers . Remove ( name ) ;
break ;
default :
break ;
}
@ -149,7 +220,8 @@ public class Client : MonoBehaviour {
public NetEventManager eventManager ;
private Modifiable < Lobby . LobbyStatus > lobby_status = null ;
public Modifiable < Lobby . LobbyStatus > lobby_status = new Modifiable < Lobby . LobbyStatus > ( null ) ;
public Modifiable < Game . GameStatus > game_status = new Modifiable < Game . GameStatus > ( null ) ;
public ConnectionImpl ( string user , string address ) {
Debug . Log ( "Connecting with the tcp channel" ) ;
conn = new TcpConnection ( address ) ;
@ -176,12 +248,16 @@ public class Client : MonoBehaviour {
}
public void JoinLobby ( string code ) {
l obby = Base32 . FromString ( code ) ;
conn . SendMessage ( new Protocol . ClientServerPacket ( ) { JoinL obby = new Connection . LobbyCode { Code = Base32 . FromString ( code ) } } ) ;
// connection.joinLobbyWithCode(new ConnectionService.LobbyCode { Code = (uint)lobby }, new Metadata { new Metadata.Entry("client_id", connId) });
}
public void CreateLobby ( bool isPublic ) {
conn . SendMessage ( new Protocol . ClientServerPacket ( ) { CreateLobby = new Connection . LobbyConfig { Public = isPublic } } ) ;
eventManager . AddHandler ( "client_create_lobby_auto_join" , ( Connection . LobbyCode lobby1 ) = > {
lobby = lobby1 . Code ;
eventManager . RemoveHandler ( Protocol . ServerClientPacket . DataOneofCase . ReturnCreateLobby , "client_create_lobby_auto_join" ) ;
} ) ;
conn . SendMessage ( new Protocol . ClientServerPacket ( ) { CreateLobby = new Connection . LobbyConfig { Public = isPublic } } ) ;
// lobby = connection.createLobby(new ConnectionService.LobbyConfig {Public = isPublic}, new Metadata { new Metadata.Entry("client_id", connId) }).Code;
}
@ -193,141 +269,92 @@ public class Client : MonoBehaviour {
}
}
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 ( out bool hasChanged ) {
hasChanged = false ;
// if (lobby != null) {
// if (lobby_status != null) {
// var hasNew = lobby_client.pollStatus(
// new Common.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) }));
// hasChanged = true;
// }
// } 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) }));
// hasChanged = true;
// }
// return lobby_status.Get();
// } else {
// return null;
// }
return null ;
public void GetUsersInSameLobby ( ) {
conn . SendMessage ( new Protocol . ClientServerPacket ( ) { QueryUsers = new Empty ( ) } ) ;
}
public List < Lobby . Vote > GetLobbyVotes ( uint game ) {
bool _ b = false ;
Google . Protobuf . Collections . RepeatedField < Lobby . Vote > votes = GetLobbyStatus ( out _ b ) . Votes ;
var gameVotes = new List < Lobby . Vote > ( ) ;
foreach ( Lobby . Vote vote in votes ) {
if ( vote . Game = = game ) {
gameVotes . Add ( vote ) ;
if ( lobby_status . Get ( ) ! = null ) {
Google . Protobuf . Collections . RepeatedField < Lobby . Vote > votes = lobby_status . Get ( ) ? . Votes ;
foreach ( Lobby . Vote vote in votes ) {
if ( vote . Game = = game ) {
gameVotes . Add ( vote ) ;
}
}
}
return gameVotes ;
}
public int GetLobbyVoteCount ( uint game ) {
bool _ b = false ;
Google . Protobuf . Collections . RepeatedField < Lobby . Vote > votes = GetLobbyStatus ( out _ b ) . Votes ;
var gameVotes = new List < Lobby . Vote > ( ) ;
foreach ( Lobby . Vote vote in votes ) {
if ( vote . Game = = game ) {
gameVotes . Add ( vote ) ;
if ( lobby_status . Get ( ) ! = null ) {
Google . Protobuf . Collections . RepeatedField < Lobby . Vote > votes = lobby_status . Get ( ) ? . Votes ;
foreach ( Lobby . Vote vote in votes ) {
if ( vote . Game = = game ) {
gameVotes . Add ( vote ) ;
}
}
}
return gameVotes . Count ;
}
public Lobby . Vote GetSelfVote ( string player ) {
bool _ b = false ;
Google . Protobuf . Collections . RepeatedField < Lobby . Vote > votes = GetLobbyStatus ( out _ b ) . Votes ;
foreach ( Lobby . Vote vote in votes ) {
Debug . Log ( vote ) ;
if ( vote . Player = = player ) {
return vote ;
if ( lobby_status . Get ( ) ! = null ) {
Google . Protobuf . Collections . RepeatedField < Lobby . Vote > votes = lobby_status . Get ( ) . Votes ;
foreach ( Lobby . Vote vote in votes ) {
Debug . Log ( vote ) ;
if ( vote . Player = = player ) {
return vote ;
}
}
}
return null ;
}
public bool IsStarting ( ) {
return lobby_status . Get ( ) ! = null & & lobby_status . Get ( ) . IsStarting ;
}
public void Vote ( uint game ) {
conn . SendMessage ( new Protocol . ClientServerPacket ( ) { Vote = new Lobby . SingleVote { Game = game } } ) ;
// lobby_client.vote(new Lobby.SingleVote() { Game = game }, new Metadata { new Metadata.Entry("client_id", connId) });
}
public void SetReady ( ) {
conn . SendMessage ( new Protocol . ClientServerPacket ( ) { Ready = new Empty ( ) } ) ;
// 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) });
conn . SendMessage ( new Protocol . ClientServerPacket ( ) { Leave = new Empty ( ) } ) ;
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 void GetPublicLobbies ( ) {
conn . SendMessage ( new Protocol . ClientServerPacket ( ) { QueryPublicLobbies = new Empty ( ) } ) ;
}
// public Game.Image GetCardImage(string cardKind) {
// return game_client.getCardImage(new Game.CardKind() { Kind = cardKind }, new Metadata { new Metadata.Entry("client_id", connId) });
// }
// public ICollection<Game.MessageStatus.Types.Pile> GetCards(string user) {
// var status = game_client.status(new Google.Protobuf.WellKnownTypes.Empty(), new Metadata { new Metadata.Entry("client_id", connId) });
// return status.PlayerPiles[status.Names.IndexOf(new Common.Name() { Name_ = user })].Piles_.Values;
// }
// public Dictionary<string, Dictionary<string, Game.GameStatus.Types.Pile>> GetPiles(string user) {
// var status = game_client.status(new Google.Protobuf.WellKnownTypes.Empty(), new Metadata { new Metadata.Entry("client_id", connId) });
// var piles = new Dictionary<string, Dictionary<string, Game.GameStatus.Types.Pile>>();
// var playerPilesMap = status.PlayerPiles[status.Names.IndexOf(new Common.Name() { Name_ = user })].Piles_;
// var playerPiles = new Dictionary<string, Game.MessageStatus.Types.Pile>();
// foreach(string key in playerPilesMap.Keys) {
// playerPiles.Add(key, playerPilesMap[key]);
// }
// var commonPilesMap = status.CommonPiles.Piles_;
// var commonPiles = new Dictionary<string, Game.MessageStatus.Types.Pile>();
// foreach (string key in commonPilesMap.Keys) {
// commonPiles.Add(key, commonPilesMap[key]);
// }
// piles.Add("Owned", playerPiles);
// piles.Add("Common", commonPiles);
// return piles;
// }
// public int GetUserIndex(string user) {
// var status = game_client.status(new Google.Protobuf.WellKnownTypes.Empty(), new Metadata { new Metadata.Entry("client_id", connId) });
// return status.Names.IndexOf(new Common.Name { Name_ = user });
// }
public void GetCardImage ( string cardKind ) {
conn . SendMessage ( new Protocol . ClientServerPacket ( ) { QueryCardImage = new Game . CardKind { Kind = cardKind } } ) ;
}
public int GetUserIndex ( string user ) {
if ( game_status . Get ( ) ! = null ) return game_status . Get ( ) . Names . IndexOf ( new Common . Name { Name_ = user } ) ;
else return - 1 ;
}
public Google . Protobuf . Collections . MapField < string , Game . GameStatus . Types . Pile > GetPlayerPiles ( string user ) {
int idx = this . GetUserIndex ( user ) ;
if ( idx ! = - 1 ) {
return game_status . Get ( ) . PlayerPiles [ idx ] . Piles_ ;
}
return null ;
}
public Google . Protobuf . Collections . MapField < string , Game . GameStatus . Types . Pile > GetCommonPiles ( ) {
if ( game_status . Get ( ) ! = null ) return game_status . Get ( ) . CommonPiles . Piles_ ;
else return null ;
}
public void OnClickCard ( string pileName , bool isCommonPile , int cardIdx , string user ) {
Game . PileKind pileKind = new Game . PileKind ( ) { Owned = ( uint ) GetUserIndex ( user ) } ;
if ( isCommonPile ) pileKind = new Game . PileKind { Common = new Empty ( ) } ;
conn . SendMessage ( new Protocol . ClientServerPacket ( ) { CallOnClick = new Game . CardId { PileKind = pileKind , CardIndex = new Game . CardIndex { Index = ( uint ) cardIdx } , PileName = pileName } } ) ;
}
// public Dictionary<string, Dictionary<string, Game.MessageStatus.Types.Pile>> OnClickCard(string pileName, bool isCommonPile, int cardIdx, string user) {
// Game.PileKind pileKind = new Game.PileKind() { Owned = (uint)GetUserIndex(user) };
@ -335,22 +362,11 @@ public class Client : MonoBehaviour {
// return GetPiles(user);
// }
// 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;
// }
public void GetGames ( ) {
conn . SendMessage ( new Protocol . ClientServerPacket ( ) { QueryGames = new Empty ( ) } ) ;
}
static T runSync < T > ( Task < T > task ) {
static T runSync < T > ( Task < T > task ) {
task . Wait ( ) ;
return task . Result ;
}