public async Task <OrType <GameCreated, GameJoined, Exception> > Send(CreateOrJoinGame createOrJoinGame) { var taskCompletionSource = new TaskCompletionSource <OrType <GameCreated, GameJoined, Exception> >(); (Action <GameCreated>, Action <GameJoined>)actions = (null, null); actions = ( (GameCreated x) => { if (x.Id == createOrJoinGame.Id) { taskCompletionSource.SetResult(new OrType <GameCreated, GameJoined, Exception>(x)); } }, (GameJoined x) => { if (x.Id == createOrJoinGame.Id) { taskCompletionSource.SetResult(new OrType <GameCreated, GameJoined, Exception>(x)); } } ); gameCreatedHandlers.Add(actions.Item1); gameJoinedHandlers.Add(actions.Item2); try { await connection.InvokeAsync(nameof(CreateOrJoinGame), createOrJoinGame); return(await taskCompletionSource.Task); } catch (TimeoutException e) { taskCompletionSource.SetResult(new OrType <GameCreated, GameJoined, Exception>(e)); return(await taskCompletionSource.Task); } catch (InvalidOperationException e) { taskCompletionSource.SetResult(new OrType <GameCreated, GameJoined, Exception>(e)); return(await taskCompletionSource.Task); } finally { gameCreatedHandlers.Remove(actions.Item1); gameJoinedHandlers.Remove(actions.Item2); } }
public async Task CreateOrJoinGame(CreateOrJoinGame createOrJoinGame) { var myGame = new RemoteGame(); var game = state.games.GetOrAdd(createOrJoinGame.Id, myGame); //if (myGame == game) { // myGame.Init(getOnUpdateScore(createOrJoinGame.Id), createOrJoinGame.FieldDimensions); //} if (!state.connectionIdToGameName.TryAdd(Context.ConnectionId, createOrJoinGame.Id)) { // error! // I mean you are already in so who cares? } if (myGame == game) { await Clients.Caller.SendAsync(nameof(GameCreated), new GameCreated(createOrJoinGame.Id)); } else { await Clients.Caller.SendAsync(nameof(GameJoined), new GameJoined(createOrJoinGame.Id)); } }