private void ApplyGameResult()
 {
     for (int i = 0; i < RoomManager.Instance.RU.Length; i++)
     {
         if (RoomManager.Instance.RU[i].seq == MyInfoManager.Instance.Seq)
         {
             MyInfoManager.Instance.Xp = RoomManager.Instance.RU[i].nextXp;
         }
         else
         {
             BrickManDesc desc = BrickManManager.Instance.GetDesc(RoomManager.Instance.RU[i].seq);
             if (desc != null)
             {
                 desc.Xp = RoomManager.Instance.RU[i].nextXp;
                 NameCard friend = MyInfoManager.Instance.GetFriend(desc.Seq);
                 if (friend != null)
                 {
                     friend.Lv = XpManager.Instance.GetLevel(desc.Xp);
                 }
                 NameCard user = ChannelUserManager.Instance.GetUser(desc.Seq);
                 if (user != null)
                 {
                     user.Lv = XpManager.Instance.GetLevel(desc.Xp);
                 }
             }
         }
     }
 }
예제 #2
0
    public void OnGUI()
    {
        NameCard[] squadMemberArrayInclueMe = SquadManager.Instance.GetSquadMemberArrayInclueMe();
        string[]   array = new string[squadMemberArrayInclueMe.Length];
        for (int i = 0; i < squadMemberArrayInclueMe.Length; i++)
        {
            array[i] = string.Empty;
        }
        GUI.Box(crdMemberListOutline, string.Empty, "LineBoxBlue");
        int num = squadMemberArrayInclueMe.Length / 2;

        if (squadMemberArrayInclueMe.Length % 2 != 0)
        {
            num++;
        }
        Rect position = new Rect(crdMemberList.x, crdMemberList.y, crdMemberList.width, crdMemberList.height / 4f * (float)num);

        selectedMember = null;
        curMember      = GUI.SelectionGrid(position, curMember, array, 2, "BoxGridStyle");
        for (int j = 0; j < squadMemberArrayInclueMe.Length && j < crdSquadMember.Length; j++)
        {
            if (curMember == j)
            {
                selectedMember = squadMemberArrayInclueMe[j];
            }
            aPlayer(crdSquadMember[j], squadMemberArrayInclueMe[j]);
        }
    }
예제 #3
0
        static void Main(string[] args)
        {
            Stream          ws         = new FileStream("a.txt", FileMode.Create);
            BinaryFormatter serializer = new BinaryFormatter();  // 객체를 직렬화/역직렬화 하는 클래스

            NameCard nc = new NameCard();

            nc.Name  = "아이유";
            nc.Phone = "010-444-4444";
            nc.Age   = 28;

            serializer.Serialize(ws, nc);  // nc객체를 직렬화하여 ws FileStream 작성
            ws.Close();

            // FileStrem에서 클래스 역직렬화
            Stream          rs           = new FileStream("a.txt", FileMode.Open);
            BinaryFormatter deserializer = new BinaryFormatter();

            NameCard nc2;

            nc2 = deserializer.Deserialize(rs) as NameCard;  // rs FileStream에서 직렬화된 데이터를 읽어와 NameCard 객체로 역직렬화
            rs.Close();

            Console.WriteLine($"Name: {nc2.Name}");
            Console.WriteLine($"Name: {nc2.Phone}");
            Console.WriteLine($"Name: {nc2.Age}");
        }
예제 #4
0
        public void Create(Space space, NameCard nameCard = null, SafeHavenCard safeHavenCard = null, Image image = null)
        {
            int imageId = 0;
            if (image != null)
            {
                this.Repository.Add<Image>(image);
                imageId = image.Id;
            }

            // add any dependent items
            if (space.Type == Haven.SpaceType.Challenge)
            {
                if (nameCard != null)
                {
                    // add name card
                    nameCard.ImageId = image.Id;
                    this.Repository.Add<NameCard>(nameCard);
                    space.NameCardId = nameCard.Id;
                }
                else
                {
                    // set space image
                    space.ImageId = image.Id;
                }
            }
            else if (space.Type == Haven.SpaceType.SafeHaven)
            {
                if (safeHavenCard != null)
                {
                    // add safe haven card
                    safeHavenCard.ImageId = image.Id;
                    this.Repository.Add<SafeHavenCard>(safeHavenCard);
                    space.SafeHavenCardId = nameCard.Id;
                }
                else
                {
                    // set space image
                    space.ImageId = image.Id;
                }
            }
            else
            {
                space.ImageId = image.Id;
            }

            this.Repository.Add<Space>(space);

            //// add challenge categories
            //if ((space.Type == Haven.SpaceType.Challenge) || (space.Type == Haven.SpaceType.War))
            //{
            //    foreach (SpaceChallengeCategory category in space.ChallengeCategories)
            //    {
            //        category.SpaceId = space.Id;
            //        category.ChallengeCategoryId = category.ChallengeCategory.Id;
            //        this.Repository.Add<SpaceChallengeCategory>(category);
            //    }
            //}
        }
예제 #5
0
        // 객체를 매개변수로 받아 출력하는 메소드
        private static void threadFunc2(object obj)
        {
            NameCard nc = (NameCard)obj;

            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine($"threadFunc2 : Name {nc.Name}, Age {nc.Age}");
                Thread.Sleep(1000);
            }
        }
예제 #6
0
    private void aPlayer(Vector2 lt, NameCard member)
    {
        Texture2D badge = XpManager.Instance.GetBadge(member.Lv, member.Rank);

        if (null != badge)
        {
            TextureUtil.DrawTexture(new Rect(lt.x + crdBadge.x, lt.y + crdBadge.y, crdBadgeSize.x, crdBadgeSize.y), badge, ScaleMode.StretchToFill);
        }
        LabelUtil.TextOut(new Vector2(lt.x + crdNickname.x, lt.y + crdNickname.y), member.Nickname, "MiniLabel", Color.white, GlobalVars.txtEmptyColor, TextAnchor.UpperLeft);
    }
예제 #7
0
    private float aPlayer(NameCard clan, float y)
    {
        Texture2D badge = XpManager.Instance.GetBadge(clan.Lv, clan.Rank);

        if (null != badge)
        {
            TextureUtil.DrawTexture(new Rect(crdBadge.x, y + crdBadge.y, crdBadgeSize.x, crdBadgeSize.y), badge);
        }
        LabelUtil.TextOut(new Vector2(crdNickname.x, crdNickname.y + y), clan.Nickname, "MiniLabel", Color.white, new Color(0f, 0f, 0f, 0f), TextAnchor.UpperLeft);
        return(y + clanMemberOffset);
    }
예제 #8
0
    public NameCard[] GetSquadMemberArrayInclueMe()
    {
        List <NameCard> list  = new List <NameCard>();
        int             level = XpManager.Instance.GetLevel(MyInfoManager.Instance.Xp);
        NameCard        item  = new NameCard(MyInfoManager.Instance.Seq, MyInfoManager.Instance.Nickname, level, -1, MyInfoManager.Instance.Rank);

        list.Add(item);
        foreach (KeyValuePair <int, NameCard> item2 in dicMember)
        {
            list.Add(item2.Value);
        }
        return(list.ToArray());
    }
예제 #9
0
        static void Main(string[] args)
        {
            /* 스레드 실행에 하나의 매개변수 전달 */
            Thread t1 = new Thread(threadFunc1);

            t1.Start(5);   // 매개변수 하나를 전달하여 실행되는 스레드

            /* 스레드 실행에 여러 값을 가진 객체 전달 */
            NameCard nameCard = new NameCard("아이유", 28);
            Thread   t2       = new Thread(threadFunc2);

            t2.Start(nameCard);
        }
예제 #10
0
        public ComplaintsViewModel(IRegionManager regionManager, IEventAggregator ea)
        {
            _regionManager    = regionManager;
            _ea               = ea;
            deptList          = ComplaintsModel.XmlParser();
            RetrunViewCommand = new DelegateCommand <object>(RetrunView);
            var dept      = new string[] { "주민", "지적", "인감" };
            var deptIndex = 0;

            for (int i = 0; i < deptList.Count; i++)
            {
                var user = new NameCard();
                user.Name = deptList[i].Name;
                user.Team = deptList[i].Team;
                user.Work = deptList[i].Work;
                user.Tel  = deptList[i].Tel;

                user.ImagePath = System.IO.Path.GetFullPath($"D:\\images\\Complaunts\\{i}.jpg");

                user.DeptIndex = i;
                user.Dept      = deptList[i].Dept;

                user.OnClick += User_OnClick;
                UserList.Add(user);
            }

            SelectCommand = new DelegateCommand <string>(x =>
            {
                //_ea.GetEvent<VideoTimerResetEvent>().Publish(Message);

                var n = int.Parse(x);
                foreach (var item in UserList)
                {
                    item.IsSelected = item.DeptIndex == n;
                }
            });

            PopupCloseCommand = new DelegateCommand(() =>
            {
                //_ea.GetEvent<VideoTimerResetEvent>().Publish(Message);

                ShowPopup    = Visibility.Collapsed;
                SelectedItem = null;
            });

            SetPosition();
        }
예제 #11
0
파일: SpaceTests.cs 프로젝트: nfink/Haven
        public void CloneChallengeSpace()
        {
            var repository = new TestRepository();

            // create a space with a name card
            var nameCard = new NameCard() { Name = "test1" };
            repository.Add(nameCard);
            var space = new Space() { NameCardId = nameCard.Id, Order = 10 };
            repository.Add(space);

            // clone the space
            var clonedSpace = space.Clone();

            // verify that space and subobject were cloned
            Assert.AreNotEqual(space.Id, clonedSpace.Id);
            Assert.AreEqual(clonedSpace.Order, space.Order);
            Assert.AreNotEqual(clonedSpace.NameCardId, space.NameCardId);
            Assert.AreEqual(clonedSpace.NameCard.Name, space.NameCard.Name);
        }
예제 #12
0
    public void OnGUI()
    {
        Squad curSquad = SquadManager.Instance.CurSquad;

        if (curSquad != null && curSquad.Leader == MyInfoManager.Instance.Seq)
        {
            NameCard selectedMember = squadMemberList.SelectedMember;
            if (selectedMember != null && selectedMember.Nickname != MyInfoManager.Instance.Nickname && GlobalVars.Instance.MyButton(crdCreate, StringMgr.Instance.Get("EXILE"), "BtnAction"))
            {
                CSNetManager.Instance.Sock.SendCS_KICK_SQUAD_REQ(selectedMember.Seq);
            }
        }
        if (GlobalVars.Instance.MyButton(crdBack, string.Empty, "BtnClose") || GlobalVars.Instance.IsEscapePressed())
        {
            CSNetManager.Instance.Sock.SendCS_LEAVE_SQUAD_REQ();
            SquadManager.Instance.Leave();
            Application.LoadLevel("Squading");
        }
    }
예제 #13
0
        public void CloneNameCard()
        {
            var repository = new TestRepository();

            // create a name card
            var image = new Image() { Filename = "test1" };
            repository.Add<Image>(image);
            var nameCard = new NameCard() { Name = "test1", Details = "test2", ImageId = image.Id };
            repository.Add<NameCard>(nameCard);

            // clone the name card
            var clonedCard = nameCard.Clone();

            // verify that the name card was cloned
            Assert.AreNotEqual(nameCard.Id, clonedCard.Id);
            Assert.AreEqual(nameCard.Name, clonedCard.Name);
            Assert.AreEqual(nameCard.Details, clonedCard.Details);
            Assert.AreNotEqual(nameCard.ImageId, clonedCard.ImageId);
        }
예제 #14
0
    private void aPlayer(Vector2 pos, NameCard player)
    {
        Texture2D badge = XpManager.Instance.GetBadge(player.Lv, player.Rank);

        if (null != badge)
        {
            TextureUtil.DrawTexture(new Rect(pos.x + crdBadge.x, pos.y + crdBadge.y, crdBadgeSize.x, crdBadgeSize.y), badge);
        }
        if (GlobalVars.Instance.MyButton(new Rect(pos.x, pos.y, crdUser.x, crdUser.y), string.Empty, "InvisibleButton") && Event.current.button == 1)
        {
            UserMenu userMenu = ContextMenuManager.Instance.Popup();
            if (userMenu != null)
            {
                bool flag = MyInfoManager.Instance.IsClanee(player.Seq);
                userMenu.InitDialog(MouseUtil.ScreenToPixelPoint(Input.mousePosition), player.Seq, player.Nickname, !flag, masterAssign: false);
            }
        }
        LabelUtil.TextOut(new Vector2(pos.x + crdNick.x, pos.y + crdNick.y), player.Nickname, "MiniLabel", Color.white, GlobalVars.txtEmptyColor, TextAnchor.UpperLeft);
    }
예제 #15
0
파일: SpaceTests.cs 프로젝트: nfink/Haven
        public void DeletedChallengeSpace()
        {
            var repository = new TestRepository();

            // create challenge spaces
            var nameCard1 = new NameCard();
            var nameCard2 = new NameCard();
            repository.AddAll(new NameCard[] { nameCard1, nameCard2 });
            var space1 = new Space() { NameCardId = nameCard1.Id };
            var space2 = new Space() { NameCardId = nameCard2.Id };
            repository.AddAll(new Space[] { space1, space2 });

            // delete a space
            space1.Delete();

            // verify that space is deleted
            Assert.IsEmpty(repository.Find<Space>(x => x.Id == space1.Id));

            // verify that name card is deleted
            Assert.IsEmpty(repository.Find<NameCard>(x => x.Id == space1.NameCardId));

            // verify that other space data is not deleted
            Assert.IsNotEmpty(repository.Find<Space>(x => x.Id == space2.Id));
            Assert.IsNotEmpty(repository.Find<NameCard>(x => x.Id == space2.NameCardId));
        }
예제 #16
0
파일: AdminModule.cs 프로젝트: nfink/Haven
        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);
                    }
                }
            };
        }