Beispiel #1
0
        public void TestIthakaInitMethod()
        {
            TestLambdaContext       context;
            APIGatewayProxyRequest  request;
            APIGatewayProxyResponse response;

            Functions functions = new Functions();

            //ping

            //metadata

            //init
            request      = new APIGatewayProxyRequest();
            request.Body = "{\"mode\": \"init\", \"players\": [10, 20]}";
            context      = new TestLambdaContext();
            response     = functions.ProcessIthaka(request, context);
            Assert.Equal(200, response.StatusCode);
            dynamic body  = JsonConvert.DeserializeObject(response.Body);
            string  state = (string)body.state.ToObject(typeof(string));
            Ithaka  g     = new Ithaka(state);

            Assert.Equal(new string[2] {
                "10", "20"
            }, g.players);

            //move
            request      = new APIGatewayProxyRequest();
            request.Body = "{\"mode\": \"move\", \"player\": \"20\", \"move\": \"a1-b2\", \"state\": " + JsonConvert.ToString(state) + "}";
            context      = new TestLambdaContext();
            response     = functions.ProcessIthaka(request, context);
            Assert.Equal(400, response.StatusCode);
        }
Beispiel #2
0
        public void TestIthakaClassMethod()
        {
            //constructor
            Action a = () => new Ithaka(new string[2] {
                "10", "10"
            });
            Action b = () => new Ithaka(new string[1] {
                "10"
            });

            Assert.Throws <System.ArgumentException>(a);
            Assert.Throws <System.ArgumentException>(b);

            //LegalMoves
            Ithaka basegame = new Ithaka(new string[2] {
                "10", "20"
            });
            HashSet <string> moves = new HashSet <string>()
            {
                "a1-b2", "a1-c3",
                "b1-b2", "b1-b3", "b1-c2",
                "c1-b2", "c1-c2", "c1-c3",
                "d1-c2", "d1-b3",
                "a2-b2", "a2-c2", "a2-b3",
                "d2-c2", "d2-b2", "d2-c3",
                "a3-b2", "a3-b3", "a3-c3",
                "d3-c2", "d3-c3", "d3-b3",
                "a4-b3", "a4-c2",
                "b4-b3", "b4-b2", "b4-c3",
                "c4-b3", "c4-c3", "c4-c2",
                "d4-c3", "d4-b2"
            };
            HashSet <string> actual = new HashSet <string>(basegame.LegalMoves());

            Assert.Equal(moves, actual);

            //Move
            Action wrongplayer = () => basegame.Move("20", "a1-b2");

            Assert.Throws <ArgumentOutOfRangeException>(wrongplayer);
            Action badmoveform = () => basegame.Move("10", "asdf");

            Assert.Throws <ArgumentException>(badmoveform);
            Action fromoor   = () => basegame.Move("10", "z1-b2");
            Action fromempty = () => basegame.Move("10", "b2-b3");

            Assert.Throws <ArgumentException>(fromoor);
            Assert.Throws <ArgumentException>(fromempty);
            Action tooor = () => basegame.Move("10", "a1-z10");
            Action toocc = () => basegame.Move("10", "a1-b1");

            Assert.Throws <ArgumentException>(tooor);
            Assert.Throws <ArgumentException>(toocc);
            basegame.Move("10", "a1-b2");
            Assert.Equal(1, basegame.currplayer);
            Assert.Equal("-YRRYY-RB--GBBGG", new string(basegame.board));
            Assert.False(basegame.gameover);
            basegame.Move("20", "a3-c3");
            Assert.False(basegame.gameover);
            basegame.Move("10", "b1-c2");
            Assert.Equal("--RRYYYR--BGBBGG", new string(basegame.board));
            Assert.True(basegame.gameover);
            Assert.Equal("10", basegame.winner);
        }
Beispiel #3
0
        /// <summary>
        /// Processes POST requests for the game Ithaka
        /// </summary>
        /// <param name="request"></param>
        /// <returns>The list of blogs</returns>
        public APIGatewayProxyResponse ProcessIthaka(APIGatewayProxyRequest request, ILambdaContext context)
        {
            dynamic body = JsonConvert.DeserializeObject(request.Body);
            string  mode = (string)body.mode.ToObject(typeof(string));

            APIGatewayProxyResponse response = new APIGatewayProxyResponse();

            if (mode == "ping")
            {
                ResponsePing ret = new ResponsePing()
                {
                    state = Ithaka.meta_state
                };
                response = new APIGatewayProxyResponse
                {
                    StatusCode = (int)HttpStatusCode.OK,
                    Body       = JsonConvert.SerializeObject(ret),
                    Headers    = new Dictionary <string, string> {
                        { "Content-Type", "application/json; charset=utf-8" }
                    }
                };
            }
            else if (mode == "metadata")
            {
                ResponseMetadata ret = new ResponseMetadata()
                {
                    state        = Ithaka.meta_state,
                    version      = Ithaka.meta_version,
                    playercounts = Ithaka.meta_playercounts,
                    description  = Ithaka.meta_description,
                    changelog    = Ithaka.meta_changelog,
                    variants     = new Variants[0]
                };
                response = new APIGatewayProxyResponse
                {
                    StatusCode = (int)HttpStatusCode.OK,
                    Body       = JsonConvert.SerializeObject(ret),
                    Headers    = new Dictionary <string, string> {
                        { "Content-Type", "application/json; charset=utf-8" }
                    }
                };
            }
            else if (mode == "init")
            {
                string[]     players = (string[])body.players.ToObject(typeof(string[]));
                Ithaka       g       = new Ithaka(players);
                ResponseMove ret     = new ResponseMove()
                {
                    state     = g.Serialize(),
                    whoseturn = new string[1] {
                        g.Whoseturn()
                    },
                    renderrep = g.Render()
                };
                response = new APIGatewayProxyResponse
                {
                    StatusCode = (int)HttpStatusCode.OK,
                    Body       = JsonConvert.SerializeObject(ret),
                    Headers    = new Dictionary <string, string> {
                        { "Content-Type", "application/json; charset=utf-8" }
                    }
                };
            }
            else if (mode == "move")
            {
                string player = body.player;
                string move   = body.move;
                string state  = body.state;
                Ithaka g;
                try
                {
                    g = new Ithaka(state);
                    g.Move(player, move);
                } catch (Exception e)
                {
                    ResponseError r = new ResponseError()
                    {
                        request = JsonConvert.SerializeObject(body),
                        message = e.ToString()
                    };
                    response = new APIGatewayProxyResponse
                    {
                        StatusCode = (int)HttpStatusCode.BadRequest,
                        Body       = JsonConvert.SerializeObject(r),
                        Headers    = new Dictionary <string, string> {
                            { "Content-Type", "application/json; charset=utf-8" }
                        }
                    };
                    return(response);
                }
                ResponseMove ret = new ResponseMove()
                {
                    state     = g.Serialize(),
                    whoseturn = new string[1] {
                        g.Whoseturn()
                    },
                    renderrep = g.Render(),
                    chat      = String.Join('\n', g.chatmsgs.ToArray())
                };
                response = new APIGatewayProxyResponse
                {
                    StatusCode = (int)HttpStatusCode.OK,
                    Body       = JsonConvert.SerializeObject(ret),
                    Headers    = new Dictionary <string, string> {
                        { "Content-Type", "application/json; charset=utf-8" }
                    }
                };
            }
            else
            {
                response = new APIGatewayProxyResponse
                {
                    StatusCode = (int)HttpStatusCode.BadRequest,
                    Body       = JsonConvert.SerializeObject(new Dictionary <string, string> {
                        { "message", "Missing or invalid 'mode' parameter provided. It must be one of 'ping', 'metadata', 'init', or 'move'." }
                    }),
                    Headers = new Dictionary <string, string> {
                        { "Content-Type", "application/json; charset=utf-8" }
                    }
                };
            }
            return(response);
        }