コード例 #1
0
        // saves new Game object in database and returns partial view with info about game and links for opponent
        public ActionResult Create(string name)
        {
            // TODO: create fail view when there is no chosenSymbol in session state
            var symbol = (Symbol) Session["ChosenSymbol"];

            var player = new Player()
            {
                Name = name,
                Symbol = symbol
            };

            var game = new Game()
            {
                Player1 = player,
                Player2 = null
            };

            using (var context = new GameDbContext())
            {
                context.Players.Add(player);
                context.Games.Add(game);
                context.SaveChanges();
            }

            return PartialView(game);
        }
コード例 #2
0
        public ActionResult Submit(int id, string name)
        {
            var symbol = (Symbol) Session["ChosenSymbol"];

            var player = new Player()
            {
                Name = name,
                Symbol = symbol
            };

            using (var context = new GameDbContext())
            {
                var game = context.Games
                    .Include(g => g.Player1)
                    .Include(g => g.Player2)
                    .SingleOrDefault(g => g.Id == id);

                if (game == null)
                {
                    game = new Game();
                    game.Player1 = player;

                    // TODO: przemyśl czy:
                    // TODO: - powinieneś trzymać referencję do DataLoader w tym obiekcie, czy tworzyć każdorazowo, gdy jest potrzebny
                    // TODO: - jw. z BasicUrlGenerator

                    context.Players.Add(player);
                    context.Games.Add(game);
                    context.SaveChanges();

                    var urlGenerator = new BasicUrlGenerator();
                    game.RefLink = urlGenerator.GetUrl(game.Id);

                    context.SaveChanges();

                    return PartialView("~/Views/NewLayout/Create.cshtml", game);
                }
                else
                {
                    game.Player2 = player;

                    context.Players.Add(player);
                    context.SaveChanges();

                    return PartialView("~/Views/NewLayout/ShowPartial.cshtml", game);
                }
            }
        }