예제 #1
0
 public Player(Game game, User user, Field field, bool isFirst)
 {
     this.Game = game;
     this.User = user;
     this.SequenceNumber = game.Users.Count;
     this.Money = initialMoney;
     this.Position = field;
     this.OwnTurn = isFirst;
     this.HasMoved = false;
     this.HasPaid = false;
     this.CardUsed = false;
     this.IsBankrupted = false;
 }
        /// <summary>
        /// Creates a game.
        /// </summary>
        /// <param name="userEmails">The email address of the users.</param>
        /// <returns></returns>
        internal static int CreateGame(List<string> userEmails)
        {
            Game game = null;
            using (var ctx = new MonopolyModelContainer())
            {
                var q = from u in ctx.Users
                        where userEmails.Contains(u.Email)
                        select u;
                List<User> users = new List<User>();
                User creator = null;
                foreach (var u in q)
                {
                    if (u.Email != userEmails[0])
                    {
                        users.Add(u);
                    }
                    else
                    {
                        creator = u;
                    }

                    if (u.IsGuest)
                    {
                        MailSender.SendNewGuid(u.Email, u.GUID);
                    }
                }

                // cannot create without at least 2 users
                // creator not included
                if (users.Count < 1)
                {
                    return -1;
                }

                Field field = ctx.Fields.SingleOrDefault(f => f.Name == "Go");
                List<User> accepted = new List<User>() { creator };
                game = new Game() { Status = "Pending", StartDate = DateTime.Now, PendingUsers = users, Users = accepted };
                ctx.Games.Add(game);
                ctx.Players.Add(new Player(game, creator, field, true));
                ctx.SaveChanges();
            }

            return game == null ? -1 : game.Id;
        }