Пример #1
0
        public GetMovesResponse GetGameMoves(string gameId, [FromUri] int?start = null, [FromUri] int?until = null)
        {
            try
            {
                var moves       = GameKeeper.GetGameMoves(gameId, start, until);
                var moveResults = new List <GetMoveResponse>();

                foreach (var move in moves)
                {
                    var moveResult = new GetMoveResponse()
                    {
                        player = move.Player,
                        type   = move.MoveType
                    };
                    if (move is TokenMove)
                    {
                        moveResult.column = ((TokenMove)move).Column;
                    }
                    ;
                    moveResults.Add(moveResult);
                }

                return(new GetMovesResponse()
                {
                    moves = moveResults
                });
            }
            catch (KeyNotFoundException ex)
            {
                // game not found
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new StringContent(ex.Message)
                });
            }
            catch (ApplicationException ex)
            {
                // moves not found
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new StringContent(ex.Message)
                });
            }
            catch (IndexOutOfRangeException ex)
            {
                // invalid start or end indexes = malformed request
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent(ex.Message)
                });
            }
            catch (ArgumentException ex)
            {
                // the end index is less than the start index - malformed request
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent(ex.Message)
                });
            }
        }
Пример #2
0
        public GetMoveResponse GetMove(string gameId, int move_number)
        {
            try
            {
                var move = GameKeeper.GetGameMove(gameId, move_number);

                var response = new GetMoveResponse()
                {
                    type   = move.MoveType,
                    player = move.Player
                };

                if (move.MoveType == MoveTypes.MOVE)
                {
                    response.column = ((TokenMove)move).Column;
                }

                return(response);
            }
            catch (KeyNotFoundException ex)
            {
                // game not found
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new StringContent(ex.Message)
                });
            }
            catch (ApplicationException ex)
            {
                // game moves not found
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new StringContent(ex.Message)
                });
            }
            catch (ArgumentOutOfRangeException ex)
            {
                // invalid move number
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new StringContent(ex.Message)
                });
            }
        }
Пример #3
0
        private void ProcessIncomingMessage(HttpListenerContext context)
        {
            string       body = null;
            StreamReader sr   = new StreamReader(context.Request.InputStream);

            using (sr)
            {
                body = sr.ReadToEnd();
            }

            var method = context.Request.Url.AbsolutePath.Replace("/", "").ToLower();

            switch (method)
            {
            case "startgame":
                var data = JsonConvert.DeserializeObject <StartGame>(body);

                context.Response.StatusCode  = (int)HttpStatusCode.OK;
                context.Response.ContentType = "text/plain";
                using (StreamWriter sw = new StreamWriter(context.Response.OutputStream))
                {
                    sw.WriteLine(context.Request.RawUrl);
                }

                _bot.StartGame(data);

                break;

            case "endgame":
                var endGameData = JsonConvert.DeserializeObject <EndGame>(body);

                context.Response.StatusCode  = (int)HttpStatusCode.OK;
                context.Response.ContentType = "text/plain";
                using (StreamWriter sw = new StreamWriter(context.Response.OutputStream))
                {
                    sw.WriteLine(context.Request.RawUrl);
                }

                _bot.EndGame(endGameData);

                break;

            case "endround":
                var endRoundData = JsonConvert.DeserializeObject <EndRound>(body);

                context.Response.StatusCode  = (int)HttpStatusCode.OK;
                context.Response.ContentType = "text/plain";
                using (StreamWriter sw = new StreamWriter(context.Response.OutputStream))
                {
                    sw.WriteLine(context.Request.RawUrl);
                }

                _bot.EndRound(endRoundData);

                break;

            case "getmove":
                var getMoveData = JsonConvert.DeserializeObject <GetMove>(body);

                context.Response.StatusCode  = (int)HttpStatusCode.OK;
                context.Response.ContentType = "text/plain";
                using (StreamWriter sw = new StreamWriter(context.Response.OutputStream))
                {
                    var responseData = new GetMoveResponse
                    {
                        Direction = _bot.GetMove(getMoveData)
                    };
                    sw.WriteLine(JsonConvert.SerializeObject(responseData));
                }

                break;

            default:
                break;
            }
        }