Esempio n. 1
0
        public async Task <JsonResult> Last(int?id = null)
        {
            var model = await _warService.FindAsync();

            return(Json(model));
        }
Esempio n. 2
0
        public async Task Invoke(HttpContext context,
                                 IWarSessionManager sessionManager,
                                 UserManager <User> userManager,
                                 WarService warService,
                                 IActiveWar activeWar)
        {
            if (context.WebSockets.IsWebSocketRequest)
            {
                var requestSocket = await context.WebSockets.AcceptWebSocketAsync();

                var currentUserId = userManager.GetUserId(context.User);

                var newWarSession = new WarSession(currentUserId, requestSocket);
                sessionManager.Add(newWarSession);

                while (requestSocket.State == WebSocketState.Open)
                {
                    var buffer   = new ArraySegment <byte>(new byte[4096]);
                    var received = await requestSocket.ReceiveAsync(buffer, CancellationToken.None);

                    var competitorId = activeWar.CompetitorId;

                    switch (received.MessageType)
                    {
                    case WebSocketMessageType.Text:
                        var receivedJson = Encoding.UTF8.GetString(buffer.Array,
                                                                   buffer.Offset,
                                                                   buffer.Count);

                        var shotParam  = JsonConvert.DeserializeObject <ShotVm>(receivedJson);
                        var shotResult = await warService.ShotAsync(shotParam);

                        var warModel = shotResult.Item1;
                        var errorMsg = shotResult.Item2;

                        var currentPlayerRespMsg = new SocketRespMsg
                        {
                            Content      = warModel,
                            ErrorMessage = errorMsg,
                            State        = errorMsg == null ? Success : Failure
                        };
                        await SendAsync(requestSocket, currentPlayerRespMsg);

                        if (currentPlayerRespMsg.State == Success)
                        {
                            var competotorSession = sessionManager.Find(competitorId);
                            if (competotorSession != null)
                            {
                                var competotorSocket = competotorSession.Socket;
                                if (competotorSocket.State == WebSocketState.Open)
                                {
                                    var competitorWarModel = await warService.FindAsync(competitorId);
                                    await SendAsync(competotorSocket, new SocketRespMsg { Content = competitorWarModel, State = Success });
                                }
                            }
                        }

                        break;
                    }
                }

                await Task.FromResult(0);
            }
            else
            {
                await _next(context);
            }
        }