Пример #1
0
    void OnChangeLampState(NetworkMessage msg)
    {
        LampStateMessage lampStateMessage = msg.ReadMessage <LampStateMessage> ();

        lampIsOn = lampStateMessage._on;
        NetworkServer.SendToAll(LampStateMessageId, lampStateMessage);

        Logger.Instance.Log("PlayerId: " + connectionIdPalyerIdDict[msg.conn.connectionId] + ", lamp state: " + lampStateMessage._on);
    }
Пример #2
0
    void OnConnectedClient(NetworkMessage msg)
    {
        // Checking if there are free playerIds on server
        if (freePlayerIds.Count == 0)
        {
            Logger.Instance.Log("[Connection error]: server is full (4 clients)");
            msg.conn.Disconnect();
            return;
        }

        // Finding playerId and connectionId for new client
        int newConnectionId = msg.conn.connectionId;
        int newPlayerId     = freePlayerIds [0];

        // Sending to new client message with his new playerId for him to spawn himself
        PlayerSpawnMessage playerSpawnMessage = new PlayerSpawnMessage();

        playerSpawnMessage.playerId = newPlayerId;
        NetworkServer.SendToClient(newConnectionId, SpawnPlayerMessageId, playerSpawnMessage);

        // Sending to new client lamp state
        LampStateMessage lampStateMessage = new LampStateMessage();

        lampStateMessage._on = lampIsOn;
        NetworkServer.SendToClient(newConnectionId, LampStateMessageId, lampStateMessage);

        // Sending to new client messages with playerId of active clients for him to spawn them
        PLayerConnectMessage pLayerConnectionMessage = new PLayerConnectMessage();

        foreach (int playerId in connectionIdPalyerIdDict.Values)
        {
            pLayerConnectionMessage.playerId = playerId;
            NetworkServer.SendToClient(newConnectionId, PLayerConnectMessageId, pLayerConnectionMessage);
        }

        // Sending to active clients playerId of new client for them to spawn him
        pLayerConnectionMessage.playerId = newPlayerId;
        foreach (int connectionId in connectionIdPalyerIdDict.Keys)
        {
            NetworkServer.SendToClient(connectionId, PLayerConnectMessageId, pLayerConnectionMessage);
        }

        // Adding new client to dictionary of 'connetionId - playerId' and deleting his playerId from list of free Ids
        connectionIdPalyerIdDict.Add(newConnectionId, newPlayerId);
        freePlayerIds.Remove(newPlayerId);

        Logger.Instance.Log("Connected client playerId: " + newPlayerId + ", connectionId: " + newConnectionId);
    }