private IUnityTask OnConnectionRequest(ConnectionRequest connectionRequest, IPEndPoint senderAddress) { if (IsPlayerConnected(senderAddress) || IsFull) { return(EmptyTask); } //TODO (Optimization) Send whole player data only once and then send only meditation value var newPlayer = new PlayerData { Meditation = 0f, Name = connectionRequest.PlayerName }; _connectedPlayers.Add(senderAddress, newPlayer); if (!IsFull) { var response = new ConnectionResponse(); var responseTask = new Common.SendMessageTask(_connection, response, senderAddress); return(responseTask); } var opponentData = GetAnotherPlayer(newPlayer); var connectionResponse = new ConnectionResponse(opponentData.Value); var sendToPlayer = new Common.SendMessageTask(_connection, connectionResponse, senderAddress); var sendToOpponent = new Common.SendMessageTask(_connection, newPlayer, opponentData.Key); var fireEvent = new FireEventTask(matchCreated); return(new BatchTask(sendToOpponent, sendToPlayer, fireEvent)); }
private IUnityTask OnConnectionResponse(ConnectionResponse response, IPEndPoint senderAddress) { if (_isConnected || (_pendingConnection != null && !senderAddress.Equals(_pendingConnection))) { return(EmptyTask); } _isConnected = true; _connectedTo = senderAddress; _pendingConnection = null; _connectionTimer = 0f; _notificationTask.SetAddress(_connectedTo); var debug = new DebugTask($"Connected to {_connectedTo}", _logger); var connectedEvent = new FireEventTask(connected); if (response.Opponent != null) { var updateOpponent = new UpdateOpponentData(_synchronizationData, response.Opponent); return(new BatchTask(debug, connectedEvent, updateOpponent)); } return(new BatchTask(debug, connectedEvent)); }