Example #1
0
        public void GetNextChallengeBoardCategory()
        {
            var repository = new TestRepository();

            // create a game with some challenges
            var board = new Board();
            repository.Add(board);
            var game = new Game() { BoardId = board.Id };
            repository.Add(game);
            var category = new ChallengeCategory();
            repository.Add(category);
            var challenge1 = new Challenge() { ChallengeCategoryId = category.Id };
            var challenge2 = new Challenge() { ChallengeCategoryId = category.Id };
            var challenge3 = new Challenge() { ChallengeCategoryId = category.Id };
            var challenges = new Challenge[] { challenge1, challenge2, challenge3 };
            repository.AddAll(challenges);
            repository.Add(new BoardChallengeCategory() { BoardId = board.Id, ChallengeCategoryId = category.Id });

            // keep getting the next challenge until all challenges should have been used
            var usedChallenges = new List<Challenge>();
            foreach (Challenge challenge in challenges)
            {
                usedChallenges.Add(game.GetNextChallenge(0));
            }

            // verify that all challenges were used
            CollectionAssert.AreEqual(challenges.Select(x => x.Id).OrderBy(x => x), usedChallenges.Select(x => x.Id).OrderBy(x => x));

            // verify that more challenges can be retrieved
            Assert.IsNotNull(game.GetNextChallenge(0));
        }
Example #2
0
        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();
            }
        }
Example #3
0
        public void DeleteEmptyGame()
        {
            var repository = new TestRepository();

            // create a game without players or used challenges
            var game = new Game();
            repository.Add(game);

            // delete the game
            game.Delete();

            // verify that deletion works
            Assert.IsEmpty(repository.Find<Game>(x => x.Id == game.Id));
        }
Example #4
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;
 }
Example #5
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);
        }
Example #6
0
        public void GetNextChallengeMultipleBoardCategories()
        {
            var repository = new TestRepository();

            // create a game with some challenges
            var board = new Board();
            repository.Add(board);
            var game = new Game() { BoardId = board.Id };
            repository.Add(game);
            var category1 = new ChallengeCategory();
            var category2 = new ChallengeCategory();
            repository.Add(category1);
            repository.Add(category2);
            var challenge1 = new Challenge() { ChallengeCategoryId = category1.Id };
            var challenge2 = new Challenge() { ChallengeCategoryId = category1.Id };
            var challenge3 = new Challenge() { ChallengeCategoryId = category2.Id };
            var challenge4 = new Challenge() { ChallengeCategoryId = category2.Id };
            var challenge5 = new Challenge() { ChallengeCategoryId = category2.Id };
            var challenges = new Challenge[] { challenge1, challenge2, challenge3, challenge4, challenge5 };
            repository.AddAll(challenges);
            repository.Add(new BoardChallengeCategory() { BoardId = board.Id, ChallengeCategoryId = category1.Id });
            repository.Add(new BoardChallengeCategory() { BoardId = board.Id, ChallengeCategoryId = category2.Id });

            // get some challenges (which ones will be used is indeterminate)
            for (int i = 0; i < 10; i++)
            {
                game.GetNextChallenge(0);
            }
        }
Example #7
0
        private Game CreateGameData(TestRepository repository)
        {
            // create game
            var board = new Board();
            repository.Add(board);
            repository.Add(new Space() { BoardId = board.Id });
            var game = new Game() { BoardId = board.Id };
            repository.Add(game);

            // add challenges
            var challenge1 = new Challenge();
            var challenge2 = new Challenge();
            var challenge3 = new Challenge();
            var challenge4 = new Challenge();
            repository.AddAll(new Challenge[] { challenge1, challenge2, challenge3, challenge4 });

            // mark some challenges as used
            repository.Add(new UsedChallenge() { GameId = game.Id, ChallengeId = challenge1.Id });
            repository.Add(new UsedChallenge() { GameId = game.Id, ChallengeId = challenge2.Id });

            // add players
            game.AddPlayer();
            game.AddPlayer();
            game.AddPlayer();

            return game;
        }
Example #8
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);
                    }
                }
            };
        }