示例#1
0
 public static SpecklePoint ToSpecklePoint(this PointPayload payload)
 {
     return(new SpecklePoint
     {
         Value = payload.Value
     });
 }
示例#2
0
        /// <summary>
        /// Class to interpret the json sent from the client, digest it, and determine what to send back
        /// </summary>
        /// <param name="jsonRequest">The json from the client</param>
        /// <var name="jsonResponse">The generated json response string if there is one, initialized to empty string</var>
        /// <var name="response">The object representing the json response</var>
        /// <returns>Generated json response string</returns>
        public string RequestInterpretor(string jsonRequest)
        {
            string          jsonResponse = "";
            ResponsePayload response     = new ResponsePayload();

            response.body = new StateUpdate();
            if (jsonRequest.Length > 0)
            {
                //Since body could be an object or a string depending on what the msg field is parse
                //the json to find out if msg is "NODE_CLICKED"
                var parsedJsonRequest = JObject.Parse(jsonRequest);
                var requestMessage    = (string)parsedJsonRequest["msg"];
                if (string.Equals(requestMessage, "NODE_CLICKED"))
                {
                    PointPayload pPayload = JsonConvert.DeserializeObject <PointPayload>(jsonRequest);
                    Point        point    = new Point();
                    point.x     = pPayload.body.x;
                    point.y     = pPayload.body.y;
                    response.id = pPayload.id;

                    //If nodeFlag is true, this node click represents the second attempted click to complete a line
                    if (nodeFlag)
                    {
                        bool validNode = isValidNode(point);
                        if (validNode)
                        {
                            Line newLine = new Line();
                            newLine.start = new Point();
                            newLine.end   = new Point();

                            board.nodes[activeNode.x, activeNode.y].Activated = true;

                            //If this is first valid move, only activate start node and don't close it
                            if (!board.GameStarted)
                            {
                                board.GameStarted = true;
                            }
                            else
                            {
                                board.nodes[activeNode.x, activeNode.y].Closed = true;
                            }
                            board.nodes[point.x, point.y].Activated = true;

                            //Activate and close nodes directly between start and end nodes
                            activateMiddleNodes(point);

                            if (isGameOver())
                            {
                                response.msg          = "GAME_OVER";
                                response.body.heading = "Game Over";
                                if (playerTwoFlag)
                                {
                                    response.body.message = "Player 1 Wins!";
                                }
                                else
                                {
                                    response.body.message = "Player 2 Wins!";
                                }
                            }
                            else
                            {
                                response.msg = "VALID_END_NODE";
                                if (playerTwoFlag)
                                {
                                    response.body.heading = "Player 1";
                                    playerTwoFlag         = false;
                                }
                                else
                                {
                                    response.body.heading = "Player 2";
                                    playerTwoFlag         = true;
                                }
                                response.body.message = null;
                            }
                            newLine.start.x       = activeNode.x;
                            newLine.start.y       = activeNode.y;
                            newLine.end.x         = point.x;
                            newLine.end.y         = point.y;
                            response.body.newLine = newLine;
                            jsonResponse          = JsonConvert.SerializeObject(response);
                        }
                        else
                        {
                            response.msg          = "INVALID_END_NODE";
                            response.body.newLine = null;
                            if (playerTwoFlag)
                            {
                                response.body.heading = "Player 2";
                            }
                            else
                            {
                                response.body.heading = "Player 1";
                            }
                            response.body.message = "Invalid move!";
                            jsonResponse          = JsonConvert.SerializeObject(response);
                        }
                        nodeFlag = false;
                    }
                    else
                    {
                        //If first move, any node is viable so less checks need to be performed
                        if (board.GameStarted)
                        {
                            if (board.nodes[point.x, point.y].Activated && !board.nodes[point.x, point.y].Closed)
                            {
                                nodeFlag              = true;
                                activeNode.x          = point.x;
                                activeNode.y          = point.y;
                                response.body.newLine = null;
                                response.body.heading = "Player 1";
                                response.body.message = "Select a second node to complete the line.";
                                response.msg          = "VALID_START_NODE";
                                jsonResponse          = JsonConvert.SerializeObject(response);
                            }
                            else
                            {
                                response.body.newLine = null;
                                if (playerTwoFlag)
                                {
                                    response.body.heading = "Player 2";
                                }
                                else
                                {
                                    response.body.heading = "Player 1";
                                }
                                response.body.message = "Not a valid starting position";
                                response.msg          = "INVALID_START_NODE";
                                response.body         = response.body;
                                jsonResponse          = JsonConvert.SerializeObject(response);
                            }
                        }
                        else
                        {
                            nodeFlag              = true;
                            activeNode.x          = point.x;
                            activeNode.y          = point.y;
                            response.body.newLine = null;
                            response.body.heading = "Player 1";
                            response.body.message = "Select a second node to complete the line.";
                            response.msg          = "VALID_START_NODE";
                            jsonResponse          = JsonConvert.SerializeObject(response);
                        }
                    }
                }
                else
                {
                    //Either reinitialize the board or don't respond with anything as there was an error
                    Payload payload = JsonConvert.DeserializeObject <Payload>(jsonRequest);
                    if (string.Equals(payload.msg, "INITIALIZE"))
                    {
                        board                 = new Board();
                        activeNode            = new Point();
                        nodeFlag              = false;
                        response.id           = payload.id;
                        response.msg          = payload.msg;
                        response.body.newLine = null;
                        response.body.heading = "Player 1";
                        response.body.message = "Awaiting Player 1's Move";
                        jsonResponse          = JsonConvert.SerializeObject(response);
                    }
                    else
                    {
                        jsonResponse = "";
                    }
                }
            }
            return(jsonResponse);
        }