Example #1
0
        public async Task <string> Select(string json)
        {
            SelectOperationRequest request = JsonConvert.DeserializeObject <SelectOperationRequest>(json);

            m_logger.LogInformation($"User {Context.ConnectionId} going to select {request}");
            if (!m_checkableField.IsEmpty(request.X, request.Y))
            {
                return(new SelectOperationResponse($"Invalid values! Filed is not empty at ({request.X},{request.Y})").ToJson());
            }

            //TODO Check if user is active!
            UserModel currentUser = m_usersState[Context.ConnectionId];

            if (!currentUser.Id.Equals(m_activeUserState.Current))
            {
                return(new SelectOperationResponse($"Failed! User {currentUser.Id} active!").ToJson());
            }

            if (currentUser.Symbol != request.Symbol)
            {
                return(new SelectOperationResponse($"Invalid symbol for user {currentUser.Id}!").ToJson());
            }

            //Changing the game field
            m_settableField.Set(request.Symbol, request.X, request.Y);
            await Clients.All.OnStateChanged(new FieldChangedEvent(request.Symbol, request.X, request.Y).ToEvent());

            //Changing the active user
            UserModel otherUser = m_users.FirstOrDefault(u => !u.Id.Equals(currentUser.Id));

            if (otherUser == null)
            {
                return(new SelectOperationResponse("Failed to find other user!").ToJson());
            }

            m_activeUserState.Set(otherUser.Id);
            await Clients.All.OnStateChanged(new ActiveUserChangedEvent(m_activeUserState.Current).ToEvent());

            if (m_analysis.WinnerDefiner(m_fieldElements.Field, out IEnumerable <ValueTuple <int, int> > winPositions))
            {
                if (!winPositions.Any())
                {
                    //draw
                    m_logger.LogInformation($"The game is over. Draw");
                    await Clients.All.OnStateChanged(new GameOverEvent().ToEvent());
                }
                else
                {
                    CellModel cell = m_fieldElements.Field[winPositions.First().Item1, winPositions.First().Item2];
                    m_logger.LogInformation($"The game is over. Winner {cell.Symbol}");
                    await Clients.All.OnStateChanged(new GameOverEvent(cell.Symbol, winPositions).ToEvent());
                }
            }
            return(new SelectOperationResponse().ToJson());
        }
Example #2
0
        void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
        {
            Vector3Int cell = m_grid.WorldToCell(eventData.pointerCurrentRaycast.worldPosition);

            if (m_checkableField.IsEmpty(cell.x, cell.y))
            {
                UserModel me = m_clientUsersState.Users.FirstOrDefault(u => u.Id.Equals(m_userIdObservable.Id.Value));
                if (me == null)
                {
                    Debug.LogError($"{nameof(FieldPresenter)}: failed to find my user!");
                    return;
                }

                m_settableField.Set(me.Symbol, cell.x, cell.y);
            }
            else
            {
                Debug.Log($"{nameof(FieldPresenter)}: {cell} is not empty!");
            }
        }