//A little hack to add ready players!
		//Sometimes it seems that the connection does not respond early in the init stage, so we try until it works!
		//We only add the new clients players if he is registred in "GameRoomClients". 
		//This reg happens in AlbotGameRoom "handleHandleReceivedAccess".
		private IEnumerator handleConnectionReadyIem(){
			while (readyConnections.Count > 0) {
				yield return new WaitForEndOfFrame ();
				List<readyClient> workingConnections = new List<readyClient> ();

				foreach(readyClient r in readyConnections){
					ConnectedClient c = GameRoomClients.getMatchingClient (r.conn);
					if (c == null)
						continue;

					foreach(PlayerInfo p in r.players){
						//Reference does not survive Dll files it seems...
						//We let the gamecontroller give the newPlayer some game settings.. (color) change this later to only return a new Class gamesettings
						ConnectedPlayer changedPlayer = gameController.onPlayerJoined (new ConnectedPlayer(p.username, c));

						//Add the newplayer to matching client
						c.players.Add (new ConnectedPlayer(c, changedPlayer.roomID, p.username, changedPlayer.color, p.iconNumber));


						//Invoke event on newly added player
						if(onPlayerJoined != null) 
							onPlayerJoined.Invoke (c.players[c.players.Count-1]);
					}
					workingConnections.Add (r);
				}
				foreach (readyClient r in workingConnections)
					readyConnections.Remove (r);
			}
		}
    protected virtual void HandleReceivedAccess(NetworkMessage netmsg)
    {
        var token = netmsg.ReadMessage <StringMessage>().value;

        Controller.ValidateAccess(token, (validatedAccess, error) => {
            if (validatedAccess == null)
            {
                //Logger.Error("Failed to confirm access token:" + error);
                // Confirmation failed, disconnect the user
                netmsg.conn.Disconnect();
                return;
            }

            //Debug.LogError("Confirmed token access for peer: " + validatedAccess);

            // Get account info
            Msf.Server.Auth.GetPeerAccountInfo(validatedAccess.PeerId, (info, errorMsg) => {
                if (info == null)
                {
                    Logger.Error("Failed to get account info of peer " + validatedAccess.PeerId + "" +
                                 ". Error: " + errorMsg);
                    return;
                }

                //Debug.LogError("Got peer account info: " + info);


                ConnectedClient newC = new ConnectedClient(netmsg.conn, info.PeerId);
                GameRoomClients.clientConnected(newC);
            });
        });
    }
Esempio n. 3
0
    public void listenForClientMsg(NetworkMessage msg)
    {
        ConnectedClient c = GameRoomClients.getMatchingClient(msg.conn);

        CommProtocol.AlbotHandler h = currentHandlers.Find(x => x.msgType == msg.msgType);

        //There should be someway to do this with generics and without reflection right?
        try{
            byte[] bytes  = msg.reader.ReadBytesAndSize();
            object objMsg = null;

            if (h.type == typeof(CommProtocol.StringMessage))
            {
                objMsg = Game.ClientController.Deserialize <CommProtocol.StringMessage> (bytes);
            }
            if (h.type == typeof(Soldiers.PlayerCommands))
            {
                objMsg = Game.ClientController.Deserialize <Soldiers.PlayerCommands> (bytes);
            }
            if (h.type == typeof(Bomberman.PlayerCommand))
            {
                objMsg = Game.ClientController.Deserialize <Bomberman.PlayerCommand> (bytes);
            }
            if (h.type == typeof(Snake.GameCommand))
            {
                objMsg = Game.ClientController.Deserialize <Snake.GameCommand> (bytes);
            }

            h.func(objMsg, c);
        }
        catch {}
    }
 private void OnClientLeft(ConnectedClient c)
 {
     if (GameRoomClients.currentClientCount() == 0)
     {
         Application.Quit();
     }
 }
Esempio n. 5
0
    public void registerHandler(CommProtocol.AlbotHandler newHandler, int playerID)
    {
        ConnectedClient c = GameRoomClients.getMatchingClient(playerID);

        c.conn.RegisterHandler(newHandler.msgType, listenForClientMsg);
        currentHandlers.Add(newHandler);
    }
 public override void OnStartServer()
 {
     Debug.LogError("Server alive and well!");
     StartCoroutine(waitForPropertisToLoad());
     StartCoroutine(waitForPlayersInit());
     GameRoomClients.init();
     Instantiate(Resources.Load <GameObject>("Prefabs/ServerEntity"));
     Msf.Connection.AddConnectionListener(initServerGameLogic, true);              //We tell server to run "loadServerGameLogic" when we establish the connection or if we are connected now
 }
        public override void OnServerDisconnect(NetworkConnection conn)
        {
            ConnectedClient c = GameRoomClients.getMatchingClient(conn);

            //Debug.LogError ("Some shit dissconnected yo");

            if (onClientLeft != null)
            {
                onClientLeft.Invoke(c);
            }
            GameRoomClients.clientDisconnected(c);
        }
    private IEnumerator StartRandomPlayerChecks()
    {
        // Wait at least 5 seconds until first check
        yield return(new WaitForSeconds(30));

        while (true)
        {
            yield return(new WaitForSeconds(20));

            if (GameRoomClients.currentClientCount() == 0)
            {
                Logs.Error("Terminating game server, no players!");
                Application.Quit();
            }
        }
    }
Esempio n. 9
0
    //This is hijacked in Training wrapper
    public virtual void sendMsg(object msg, int targetID, short msgId)
    {
        Type msgType = currentProtocol.getMatchingType(msgId);

        ConnectedClient c = GameRoomClients.getMatchingClient(targetID);

        if (c == null)
        {
            throw new Exception("Tried to send msg to a non existing player!");
        }
        if (c.conn.isConnected == false)
        {
            throw new Exception("Error sending message " + msgType.ToString() + ", no connection to client");
        }

        sendMsg(msg, c.conn, msgId);
    }