示例#1
0
文件: Program.cs 项目: nfink/Haven
        static void Main(string[] args)
        {
            using (var repository = new Repository())
            {
                var dl = new Haven.Data.DataLoad();
                dl.LoadTables();

                var game = new Game();
                game.Create(1, 2);
                Console.WriteLine(game.Id);

                string command;
                do
                {
                    ListActions(repository, game.Id);
                    Console.Write("Action: ");
                    command = Console.ReadLine();
                    int actionId;
                    if (Int32.TryParse(command, out actionId))
                    {
                        PerformAction(repository, game.Id, actionId);
                    }

                } while (command != "exit");

                Console.WriteLine("done");
                Console.ReadLine();
            }
        }
示例#2
0
 private Game SetupGame(IRepository repository)
 {
     var board = new Board();
     repository.Add(board);
     repository.Add(new Space() { BoardId = board.Id });
     var game = new Game();
     game.Repository = repository;
     game.Create(board.Id, 2);
     repository.Add(new Piece() { Name = "testpiece1" });
     repository.Add(new Piece() { Name = "testpiece2" });
     repository.Add(new Piece() { Name = "testpiece3" });
     repository.Add(new Color() { Name = "testcolor1" });
     repository.Add(new Color() { Name = "testcolor2" });
     repository.Add(new Color() { Name = "testcolor3" });
     return game;
 }
示例#3
0
        public void TurnAround()
        {
            // set up data
            var repository = new TestRepository();

            var board = new Board();
            repository.Add(board);
            var space = new Space() { Type = SpaceType.TurnAround, BoardId = board.Id };
            repository.Add(space);
            var game = new Game();
            game.Repository = repository;
            game.Create(board.Id, 2);

            // trigger onland
            var player = game.Players.First();
            space.OnLand(player);

            // verify that player was turned around and received the correct message
            player = repository.Get<Player>(player.Id);
            Assert.AreEqual(false, player.MovementDirection);
            var message = player.Messages.Single();
            Assert.AreEqual("Turned around.", message.Text);
        }
示例#4
0
        public AdminModule(IRootPathProvider pathProvider, TinyIoCContainer container)
        {
            var formsAuthConfiguration =
                new FormsAuthenticationConfiguration()
                {
                    RedirectUrl = "~/Login",
                    UserMapper = new UserMapper(container.Resolve<IRepository>()),
                };

            FormsAuthentication.Enable(this, formsAuthConfiguration);

            Get["/"] = parameters =>
            {
                return View["Views/Haven.cshtml"];
            };

            Get["/SpaceTypes"] = parameters =>
            {
                var types = new List<AdminModule.SpaceType>();
                foreach (Haven.SpaceType type in Enum.GetValues(typeof(Haven.SpaceType)))
                {
                    types.Add(new AdminModule.SpaceType(type));
                }

                return JsonConvert.SerializeObject(types);
            };

            Get["/User"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    var user = repository.Get<User>(userId);
                    return JsonConvert.SerializeObject(user);
                }
            };

            Put["/User"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    var user = repository.Get<User>(userId);
                    user.Username = (string)this.Request.Form.Username;
                    repository.Update(user);
                    repository.Commit();
                    return JsonConvert.SerializeObject(user);
                }
            };

            Get["/Games"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    return JsonConvert.SerializeObject(repository.Find<Game>(x => x.OwnerId == userId));
                }
            };

            Get["/Games/Active"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    return JsonConvert.SerializeObject(repository.Find<Game>(x => x.OwnerId == userId).Where(x => !x.Ended));
                }
            };

            Get["/Games/Completed"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    return JsonConvert.SerializeObject(repository.Find<Game>(x => x.OwnerId == userId).Where(x => x.Ended));
                }
            };

            Post["/Games"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var game = new Game { Name = (string)this.Request.Form.Name };
                    game.Repository = repository;
                    game.Create((int)this.Request.Form.BoardId, (int)this.Request.Form.NumberOfPlayers);
                    repository.Commit();
                    return JsonConvert.SerializeObject(game);
                }
            };

            Delete["/Games/{id}"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var gameId = (int)parameters.id;
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    var game = repository.Find<Game>(x => (x.Id == gameId) && (x.OwnerId == userId)).FirstOrDefault();
                    if (game != null)
                    {
                        game.Delete();
                        repository.Commit();
                        return new HtmlResponse(HttpStatusCode.OK);
                    }
                    else
                    {
                        return new HtmlResponse(HttpStatusCode.NotFound);
                    }
                }
            };

            Get["/Boards"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    return JsonConvert.SerializeObject(repository.Find<Board>(x => x.OwnerId == userId));
                }
            };

            Post["/Boards"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var board = new Board();
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    board.OwnerId = userId;
                    repository.Add(board);
                    repository.Commit();
                    return JsonConvert.SerializeObject(board);
                }
            };

            Get["/Boards/{id}"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    var boardId = (int)parameters.id;
                    var board = repository.Find<Board>(x => (x.Id == boardId) && (x.OwnerId == userId)).FirstOrDefault();
                    if (board != null)
                    {
                        return JsonConvert.SerializeObject(board);
                    }
                    else
                    {
                        return new HtmlResponse(HttpStatusCode.NotFound);
                    }
                }
            };

            Put["/Boards/{id}"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    var boardId = (int)parameters.id;
                    var board = repository.Find<Board>(x => (x.Id == boardId) && (x.OwnerId == userId)).FirstOrDefault();
                    if (board != null)
                    {
                        board.Name = (string)this.Request.Form.Name;
                        board.Description = (string)this.Request.Form.Description;
                        board.TurnsToEnd = (int)this.Request.Form.TurnsToEnd;
                        board.NameCardsToEnd = (int)this.Request.Form.NameCardsToEnd;
                        board.SafeHavenCardsToEnd = (int)this.Request.Form.SafeHavenCardsToEnd;
                        var imageFile = this.Request.Files.FirstOrDefault();
                        var image = this.UpdateImage(pathProvider, repository, board.Image, imageFile);
                        if (image != null)
                        {
                            board.ImageId = image.Id;
                        }
                        repository.Update(board);
                        repository.Commit();
                        return JsonConvert.SerializeObject(board);
                    }
                    else
                    {
                        return new HtmlResponse(HttpStatusCode.NotFound);
                    }
                }
            };

            Delete["/Boards/{id}"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var boardId = (int)parameters.id;
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    var board = repository.Find<Board>(x => (x.Id == boardId) && (x.OwnerId == userId)).FirstOrDefault();
                    if (board != null)
                    {
                        board.Delete();
                        repository.Commit();
                        return new HtmlResponse(HttpStatusCode.OK);
                    }
                    else
                    {
                        return new HtmlResponse(HttpStatusCode.NotFound);
                    }
                }
            };

            Post["/Boards/{id}/ChallengeCategories"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    var boardId = (int)parameters.id;
                    var board = repository.Find<Board>(x => (x.Id == boardId) && (x.OwnerId == userId)).SingleOrDefault();
                    if (board != null)
                    {
                        foreach (BoardChallengeCategory category in board.ChallengeCategories.ToList())
                        {
                            repository.Remove(category);
                        }

                        var categoryIds = JsonConvert.DeserializeObject<IEnumerable<int>>((string)this.Request.Form.ChallengeCategories);

                        foreach (int categoryId in categoryIds)
                        {
                            repository.Add(new BoardChallengeCategory() { BoardId = board.Id, ChallengeCategoryId = categoryId });
                        }

                        repository.Commit();
                        return JsonConvert.SerializeObject(board.Challenges);
                    }
                    else
                    {
                        return new HtmlResponse(HttpStatusCode.NotFound);
                    }
                }
            };

            Post["/Boards/{id}/Validate"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    var boardId = (int)parameters.id;
                    var board = repository.Find<Board>(x => (x.Id == boardId) && (x.OwnerId == userId)).SingleOrDefault();
                    if (board != null)
                    {
                        var validations = board.Validate();
                        var valid = (validations.Errors.Count < 1);
                        if (board.Active != valid)
                        {
                            board.Active = valid;
                            repository.Update(board);
                        }
                        repository.Commit();
                        return JsonConvert.SerializeObject(validations);
                    }
                    else
                    {
                        return new HtmlResponse(HttpStatusCode.NotFound);
                    }
                }
            };

            Post["/Boards/{id}/Copy"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    var boardId = (int)parameters.id;
                    var board = repository.Find<Board>(x => (x.Id == boardId) && (x.OwnerId == userId)).SingleOrDefault();
                    if (board != null)
                    {
                        board.Name = board.Name + " (copy)";
                        var copiedBoard = board.Copy();
                        repository.Commit();
                        return JsonConvert.SerializeObject(copiedBoard);
                    }
                    else
                    {
                        return new HtmlResponse(HttpStatusCode.NotFound);
                    }
                }
            };

            Get["/Challenges"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    return JsonConvert.SerializeObject(repository.Find<Challenge>(x => x.OwnerId == userId));
                }
            };

            Post["/Challenges"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var challenge = this.Bind<Challenge>();
                    challenge.OwnerId = int.Parse(this.Context.CurrentUser.UserName);
                    repository.Add(challenge);

                    var answers = JsonConvert.DeserializeObject<IEnumerable<ChallengeAnswer>>((string)this.Request.Form.Answers);

                    foreach (ChallengeAnswer answer in answers)
                    {
                        repository.Add(new ChallengeAnswer() { ChallengeId = challenge.Id, Answer = answer.Answer, Correct = answer.Correct });
                    }

                    repository.Commit();
                    return JsonConvert.SerializeObject(challenge);
                }
            };

            Put["/Challenges/{id}"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var challengeId = (int)parameters.id;
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    var challenge = repository.Find<Challenge>(x => (x.Id == challengeId) && (x.OwnerId == userId)).SingleOrDefault();
                    if (challenge != null)
                    {
                        challenge.Question = (string)this.Request.Form.Question;
                        challenge.OpenEnded = (bool)this.Request.Form.OpenEnded;
                        challenge.OwnerId = userId;
                        challenge.ChallengeCategoryId = (int)this.Request.Form.ChallengeCategoryId;
                        repository.Update(challenge);
                        foreach (ChallengeAnswer answer in challenge.Answers.ToList())
                        {
                            repository.Remove(answer);
                        }

                        var answers = JsonConvert.DeserializeObject<IEnumerable<ChallengeAnswer>>((string)this.Request.Form.Answers);

                        foreach (ChallengeAnswer answer in answers)
                        {
                            repository.Add(new ChallengeAnswer() { ChallengeId = challenge.Id, Answer = answer.Answer, Correct = answer.Correct });
                        }

                        repository.Commit();
                        return JsonConvert.SerializeObject(challenge);
                    }
                    else
                    {
                        return new HtmlResponse(HttpStatusCode.NotFound);
                    }
                }
            };

            Delete["/Challenges/{id}"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var challengeId = (int)parameters.id;
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    var challenge = repository.Find<Challenge>(x => (x.Id == challengeId) && (x.OwnerId == userId)).SingleOrDefault();
                    if (challenge != null)
                    {
                        challenge.Delete();
                        repository.Commit();
                        return new HtmlResponse(HttpStatusCode.OK);
                    }
                    else
                    {
                        return new HtmlResponse(HttpStatusCode.NotFound);
                    }
                }
            };

            Get["/ChallengeCategories"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    return JsonConvert.SerializeObject(repository.Find<ChallengeCategory>(x => x.OwnerId == userId));
                }
            };

            Post["/ChallengeCategories"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var challengeCategory = this.Bind<ChallengeCategory>();
                    challengeCategory.OwnerId = int.Parse(this.Context.CurrentUser.UserName);
                    repository.Add(challengeCategory);
                    repository.Commit();
                    return JsonConvert.SerializeObject(challengeCategory);
                }
            };

            Put["/ChallengeCategories/{id}"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var challengeCategoryId = (int)parameters.id;
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    var challengeCategory = repository.Find<ChallengeCategory>(x => (x.Id == challengeCategoryId) && (x.OwnerId == userId)).SingleOrDefault();
                    if (challengeCategory != null)
                    {
                        challengeCategory.Name = (string)this.Request.Form.Name;
                        repository.Update(challengeCategory);
                        repository.Commit();
                        return JsonConvert.SerializeObject(challengeCategory);
                    }
                    else
                    {
                        return new HtmlResponse(HttpStatusCode.NotFound);
                    }
                }
            };

            Delete["/ChallengeCategories/{id}"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var challengeCategoryId = (int)parameters.id;
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    var challengeCategory = repository.Find<ChallengeCategory>(x => (x.Id == challengeCategoryId) && (x.OwnerId == userId)).SingleOrDefault();
                    if (challengeCategory != null)
                    {
                        challengeCategory.Delete();
                        repository.Commit();
                        return new HtmlResponse(HttpStatusCode.OK);
                    }
                    else
                    {
                        return new HtmlResponse(HttpStatusCode.NotFound);
                    }
                }
            };

            Post["/Spaces"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var space = this.Bind<Space>();

                    var imageFile = this.Request.Files.FirstOrDefault();
                    Image image = null;

                    // add any dependent records
                    if (space.Type == Haven.SpaceType.Challenge)
                    {
                        var nameCard = new NameCard() { Name = (string)this.Request.Form.CardName, Details = (string)this.Request.Form.CardDetails };
                        image = this.UpdateImage(pathProvider, repository, image, imageFile);
                        if (image != null)
                        {
                            nameCard.ImageId = image.Id;
                        }
                        repository.Add(nameCard);
                        space.NameCardId = nameCard.Id;
                    }
                    else if (space.Type == Haven.SpaceType.SafeHaven)
                    {
                        var safeHavenCard = new SafeHavenCard() { Name = (string)this.Request.Form.CardName, Details = (string)this.Request.Form.CardDetails };
                        image = this.UpdateImage(pathProvider, repository, image, imageFile);
                        if (image != null)
                        {
                            safeHavenCard.ImageId = image.Id;
                        }
                        repository.Add(safeHavenCard);
                        space.SafeHavenCardId = safeHavenCard.Id;
                    }

                    repository.Add(space);

                    // add challenge categories
                    if ((space.Type == Haven.SpaceType.Challenge) || (space.Type == Haven.SpaceType.War))
                    {
                        var categoryIds = JsonConvert.DeserializeObject<IEnumerable<int>>((string)this.Request.Form.ChallengeCategories);
                        foreach (int categoryId in categoryIds)
                        {
                            repository.Add(new SpaceChallengeCategory() { ChallengeCategoryId = categoryId, SpaceId = space.Id });
                        }
                    }

                    repository.Commit();
                    return JsonConvert.SerializeObject(space);
                }
            };

            Put["/Spaces/{id}"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var spaceId = (int)parameters.id;
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    var space = repository.Find<Space>(x => x.Id == spaceId).Where(x => repository.Find<Board>(y => y.OwnerId == userId && y.Id == x.BoardId).Count() > 0).SingleOrDefault();
                    if (space != null)
                    {
                        // update fields
                        space.Type = (Haven.SpaceType)((int)this.Request.Form.Type);
                        space.Order = (int)this.Request.Form.Order;
                        space.IconId = (int)this.Request.Form.IconId;
                        space.BackgroundColorId = (int)this.Request.Form.BackgroundColorId;
                        space.TextColorId = (int)this.Request.Form.TextColorId;

                        var imageFile = this.Request.Files.FirstOrDefault();

                        // add/update any dependent records
                        if (space.Type == Haven.SpaceType.Challenge)
                        {
                            // delete other records
                            if (space.SafeHavenCardId != 0)
                            {
                                repository.Remove(space.SafeHavenCard);
                                space.SafeHavenCardId = 0;
                            }

                            // add/update name card record
                            NameCard nameCard = space.NameCard;
                            if (nameCard == null)
                            {
                                nameCard = new NameCard() { Name = (string)this.Request.Form.CardName, Details = (string)this.Request.Form.CardDetails };
                                repository.Add(nameCard);
                            }
                            else
                            {
                                nameCard.Name = (string)this.Request.Form.CardName;
                                nameCard.Details = (string)this.Request.Form.CardDetails;
                            }

                            var image = nameCard.Image;
                            image = this.UpdateImage(pathProvider, repository, image, imageFile);
                            if (image != null)
                            {
                                nameCard.ImageId = image.Id;
                            }

                            repository.Update(nameCard);
                            space.NameCardId = nameCard.Id;
                        }
                        else if (space.Type == Haven.SpaceType.SafeHaven)
                        {
                            // delete other records
                            if (space.NameCardId != 0)
                            {
                                repository.Remove(space.NameCard);
                                space.NameCardId = 0;
                            }

                            // add/update safe haven card record
                            SafeHavenCard safeHavenCard = space.SafeHavenCard;
                            if (safeHavenCard == null)
                            {
                                safeHavenCard = new SafeHavenCard() { Name = (string)this.Request.Form.CardName, Details = (string)this.Request.Form.CardDetails };
                                repository.Add(safeHavenCard);
                            }
                            else
                            {
                                safeHavenCard.Name = (string)this.Request.Form.CardName;
                                safeHavenCard.Details = (string)this.Request.Form.CardDetails;
                            }

                            var image = safeHavenCard.Image;
                            image = this.UpdateImage(pathProvider, repository, image, imageFile);
                            if (image != null)
                            {
                                safeHavenCard.ImageId = image.Id;
                            }

                            repository.Update(safeHavenCard);
                            space.SafeHavenCardId = safeHavenCard.Id;
                        }
                        else
                        {
                            // delete unused records
                            if (space.NameCardId != 0)
                            {
                                if (space.NameCard.Image != null)
                                {
                                    space.NameCard.Image.Delete();
                                }
                                repository.Remove(space.NameCard);
                                space.NameCardId = 0;
                            }
                            if (space.SafeHavenCardId != 0)
                            {
                                if (space.SafeHavenCard.Image != null)
                                {
                                    space.SafeHavenCard.Image.Delete();
                                }
                                repository.Remove(space.SafeHavenCard);
                                space.SafeHavenCardId = 0;
                            }
                        }

                        // update challenge categories
                        foreach (SpaceChallengeCategory category in space.ChallengeCategories.ToList())
                        {
                            repository.Remove(category);
                        }

                        if ((space.Type == Haven.SpaceType.Challenge) || (space.Type == Haven.SpaceType.War))
                        {
                            var categoryIds = JsonConvert.DeserializeObject<IEnumerable<int>>((string)this.Request.Form.ChallengeCategories);
                            foreach (int categoryId in categoryIds)
                            {
                                repository.Add(new SpaceChallengeCategory() { ChallengeCategoryId = categoryId, SpaceId = space.Id });
                            }
                        }

                        repository.Update(space);
                        repository.Commit();
                        return JsonConvert.SerializeObject(space);
                    }
                    else
                    {
                        return new HtmlResponse(HttpStatusCode.NotFound);
                    }
                }
            };

            Delete["/Spaces/{id}"] = parameters =>
            {
                using (var repository = container.Resolve<IRepository>())
                {
                    var spaceId = (int)parameters.id;
                    var userId = int.Parse(this.Context.CurrentUser.UserName);
                    var space = repository.Find<Space>(x => x.Id == spaceId).Where(x => repository.Find<Board>(y => y.OwnerId == userId && y.Id == x.BoardId).Count() > 0).SingleOrDefault();
                    if (space != null)
                    {
                        space.Delete();
                        repository.Commit();
                        return new HtmlResponse(HttpStatusCode.OK);
                    }
                    else
                    {
                        return new HtmlResponse(HttpStatusCode.NotFound);
                    }
                }
            };
        }