internal void DoIntercept(List<Move> moves)
        {
            // delete unused actions
            using (var ctx = new MonopolyModelContainer())
            {
                var actions = ctx.MoveActions.Where(ma => ma.Player.Id == player.Id);
                ctx.MoveActions.RemoveRange(actions);
                ctx.SaveChanges();
            }

            foreach (MoveInterceptor interceptor in interceptors)
            {
                if (interceptor.ShouldCall(player))
                {
                    interceptor.CheckMove(moves, player.Id);
                }
            }

            using (var ctx = new MonopolyModelContainer())
            {
                Player p2 = ctx.Players.SingleOrDefault(p => p.Id == player.Id);
                foreach (var move in moves)
                {
                    if (move.Type != Move.MoveType.MSG)
                    {
                        var action = new MoveAction() { Player = p2, Type = move.Type.ToString(), Offer = move.Offer };
                        ctx.MoveActions.Add(action);
                        ctx.SaveChanges();
                        move.Id = action.Id;
                    }
                }
            }
        }
        /// <summary>
        /// When a user accepts a game invitation.
        /// </summary>
        /// <param name="userEmail"></param>
        /// <param name="gameId"></param>
        /// <returns></returns>
        internal static bool Accept(string userEmail, int gameId)
        {
            Game game = null;
            using (var ctx = new MonopolyModelContainer())
            {
                game = ctx.Games.SingleOrDefault(g => g.Id == gameId);
                if (game != null)
                {
                    User user = game.PendingUsers.SingleOrDefault(pu => pu.Email == userEmail);
                    if (user != null)
                    {
                        Field field = ctx.Fields.SingleOrDefault(f => f.Name == "Go");
                        game.Users.Add(user);
                        game.PendingUsers.Remove(user);
                        ctx.Players.Add(new Player(game, user, field, false));
                        user.GUID = Guid.NewGuid().ToString("N");
                        ctx.SaveChanges();
                        if (user.IsGuest)
                        {
                            MailSender.SendNewGuid(user.Email, user.GUID);
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }

            return game != null;
        }
示例#3
0
        internal object Process(int actionId, string param)
        {
            ProcessMove processor = null;
            Move move = null;
            int playerId = 0;
            using (var ctx = new MonopolyModelContainer())
            {
                MoveAction action = ctx.MoveActions.SingleOrDefault(a => a.Id == actionId);
                playerId = action.Player.Id;
                move = new Move()
                {
                    Type = (Move.MoveType)Enum.Parse(typeof(Move.MoveType), action.Type),
                    Param = param,
                    PlayerId = playerId,
                    Offer = action.Offer
                };
                if (action.Offer != null)
                {
                    string[] offerParams = param.Split(';');
                    move.OfferField = offerParams[0];
                    int price = 0;
                    if (!Int32.TryParse(offerParams[1], out price))
                    {
                        // without price the action cannot be processed
                        move.Type = Move.MoveType.MSG;
                    }

                    move.OfferPrice = price;
                }

            }
            if (processors.TryGetValue(move.Type, out processor))
            {
                processor(move);
            }

            // remove unwanted actions
            using (var ctx = new MonopolyModelContainer())
            {
                var actions = ctx.MoveActions.Where(ma => ma.Player.Id == playerId);
                foreach (var action in actions)
                {
                    ctx.MoveActions.Remove(action);
                }

                ctx.SaveChanges();
            }

            return null;
        }
        /// <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;
        }
        internal static bool AddUserToGame(string userEmail, int gameId)
        {
            using (var ctx = new MonopolyModelContainer())
            {
                Game game = ctx.Games.SingleOrDefault(g => g.Id == gameId);
                User user = ctx.Users.SingleOrDefault(u => u.Email == userEmail);
                if (user == null || game == null)
                {
                    return false;
                }

                if (game.Users.Contains(user) || game.PendingUsers.Contains(user) || game.DeclinedUsers.Contains(user))
                {
                    return false;
                }

                game.PendingUsers.Add(user);
                ctx.SaveChanges();
            }

            return true;
        }
示例#6
0
        private void FinishAuctionProcessor(Move move)
        {
            using (var ctx = new MonopolyModelContainer())
            {
                Player player = ctx.Players.SingleOrDefault(p => p.Id == move.PlayerId);
                // process ongoing auctions
                var auctions = ctx.Auctions.Where(au => au.Game.Id == player.Game.Id);
                foreach (var auction in auctions)
                {
                    if (auction.Winner != null)
                    {
                        Reality reality = ctx.Realities1.SingleOrDefault(r => r.Field.Name == auction.Field.Name && r.Player.Game.Id == player.Game.Id);
                        if (reality != null)
                        {
                            Player owner = reality.Player;
                            player.Money -= auction.Price;
                            owner.Money += auction.Price;
                            owner.Realities.Remove(reality);
                        }
                        else
                        {
                            player.Money -= auction.Price;
                            reality = new Reality()
                            {
                                Field = auction.Field,
                                Hotels = 0,
                                Houses = 0,
                                IsMortgaged = false,
                                Player = player
                            };
                        }

                        player.Realities.Add(reality);
                        ctx.SaveChanges();
                    }

                    ctx.Auctions.Remove(auction);
                }

                ctx.SaveChanges();
            }
        }
示例#7
0
 private void BuyMoveProcessor(Move move)
 {
     using (var ctx = new MonopolyModelContainer())
     {
         Player player = ctx.Players.SingleOrDefault(p => p.Id == move.PlayerId);
         Reality reality = new Reality()
         {
             Field = player.Position,
             Hotels = 0,
             Houses = 0,
             IsMortgaged = false,
             Player = player
         };
         player.Realities.Add(reality);
         player.Money -= player.Position.Price;
         ctx.SaveChanges();
     }
 }
示例#8
0
 private void WithdrawMoveProcessor(Move move)
 {
     using (var ctx = new MonopolyModelContainer())
     {
         ctx.Offers.Remove(move.Offer);
         ctx.SaveChanges();
     }
 }
示例#9
0
        private void RollMoveProcessor(Move move)
        {
            Console.WriteLine("Rollling the dice");
            using (var ctx = new MonopolyModelContainer())
            {
                Player player = ctx.Players.SingleOrDefault(p => p.Id == move.PlayerId);
                Random rnd = new Random();
                int score = rnd.Next(1, 7);
                score += rnd.Next(1, 7);
                int nextPos = player.Position.Id + score;
                if (nextPos > 40)
                {
                    nextPos -= 40;
                    player.Money += 200;
                }

                player.Position = ctx.Fields.SingleOrDefault(f => f.Id == nextPos);
                player.HasMoved = true;
                ctx.SaveChanges();
            }
        }
示例#10
0
 private void PayJailMoveProcessor(Move move)
 {
     using (var ctx = new MonopolyModelContainer())
     {
         Player player = ctx.Players.SingleOrDefault(p => p.Id == move.PlayerId);
         player.Money -= 50;
         player.JailValue = 0;
         ctx.SaveChanges();
     }
 }
示例#11
0
        private void CollectPayCard(Card card, Player player, MonopolyModelContainer ctx)
        {
            if (card.Parameters.Contains('|'))
            {
                // TODO process params
                int houseCost = -40;
                int hotelCost = -115;
                int houses = player.Realities.Sum(r => r.Houses);
                int hotels = player.Realities.Sum(r => r.Hotels);
                player.Money += houseCost * houses + hotelCost * hotels;
            }
            else
            {
                int amount = Int32.Parse(card.Parameters);
                if (card.From == "Players")
                {
                    var players = ctx.Players.Where(p => p.Game.Id == player.Game.Id && p.Id != player.Id);
                    int counter = 0;
                    foreach (var p in players)
                    {
                        counter++;
                        p.Money -= amount;
                    }

                    player.Money += counter * amount;
                }
                else
                {
                    player.Money += amount;
                }
            }

            player.CardUsed = true;
            ctx.SaveChanges();
        }
示例#12
0
        private void GotoCard(Card card, Player player, MonopolyModelContainer ctx)
        {
            Field newPos = null;
            int fieldId = 0;
            switch (card.Parameters)
            {
                case "-3":
                    fieldId = player.Position.Id < 3 ? player.Position.Id + 37 : player.Position.Id - 3;
                    newPos = ctx.Fields.SingleOrDefault(f => f.Id == fieldId);
                    break;
                case "Railway":
                    var railways = ctx.Fields.Where(f => f.Type == "Railway");
                    foreach (var railway in railways)
                    {
                        if (railway.Id > player.Position.Id)
                        {
                            newPos = railway;
                            break;
                        }
                    }

                    if (newPos == null)
                    {
                        // first railway
                        newPos = ctx.Fields.SingleOrDefault(f => f.Id == 6);
                    }

                    break;
                case "Utility":
                    fieldId = player.Position.Id < 13 || player.Position.Id > 29 ? 13 : 29;
                    newPos = ctx.Fields.SingleOrDefault(f => f.Id == fieldId);
                    break;
                default:
                    newPos = ctx.Fields.SingleOrDefault(f => f.Name == card.Parameters);
                    break;
            }

            if (player.Position.Id > newPos.Id && newPos.Name != "Jail")
            {
                player.Money += 200;
            }

            if (newPos.Name == "Jail")
            {
                player.JailValue = 1;
            }

            player.CardUsed = true;
            ctx.SaveChanges();
        }
示例#13
0
 private void GetOutOfJailCard(Card card, Player player, MonopolyModelContainer ctx)
 {
     player.HasJailCard = true;
     ctx.SaveChanges();
 }
        /// <summary>
        /// When a user declines a game invitation.
        /// </summary>
        /// <param name="userEmail"></param>
        /// <param name="gameId"></param>
        /// <returns></returns>
        internal static bool Decline(string userEmail, int gameId)
        {
            Game game = null;
            using (var ctx = new MonopolyModelContainer())
            {
                game = ctx.Games.SingleOrDefault(g => g.Id == gameId);
                if (game != null)
                {
                    User user = game.PendingUsers.SingleOrDefault(pu => pu.Email == userEmail);
                    if (user != null)
                    {
                        game.DeclinedUsers.Add(user);
                        game.PendingUsers.Remove(user);
                        ctx.SaveChanges();
                    }
                    else
                    {
                        return false;
                    }
                }
            }

            return game != null;
        }
示例#15
0
        private void OfferToBuyMoveProcessor(Move move)
        {
            using (var ctx = new MonopolyModelContainer())
            {
                Player player = ctx.Players.SingleOrDefault(p => p.Id == move.PlayerId);
                Player partner = ctx.Players.SingleOrDefault(p => p.User.Email == move.OfferPartner);
                Reality reality = partner.Realities.SingleOrDefault(r => r.Field.Name == move.OfferField);
                Offer offer = new Offer()
                {
                    From = player,
                    To = partner,
                    Price = move.OfferPrice,
                    Reality = reality
                };

                ctx.Offers.Add(offer);
                ctx.SaveChanges();
            }
        }
示例#16
0
        private void PassProcessor(Move move)
        {
            using (var ctx = new MonopolyModelContainer())
            {
                Player player = ctx.Players.SingleOrDefault(p => p.Id == move.PlayerId);
                player.User.GUID = Guid.NewGuid().ToString("N");
                player.OwnTurn = false;
                int sequence = player.SequenceNumber + 1;
                if (sequence > player.Game.ActivePlayers)
                {
                    sequence = 1;
                }

                Player nextPlayer = ctx.Players.SingleOrDefault(p => p.Game.Id == player.Game.Id && p.SequenceNumber == sequence);
                nextPlayer.OwnTurn = true;
                nextPlayer.ResetTurn();

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

                ctx.SaveChanges();
            }
        }
        public void CheckMove(List<Move> moves, int playerId)
        {
            using (var ctx = new MonopolyModelContainer())
            {
                Player player = ctx.Players.SingleOrDefault(p => p.Id == playerId);
                Player other = ctx.Players.SingleOrDefault(p => p.Game.Id == player.Game.Id && p.Realities.Count(r => r.Field.Id == player.Position.Id) == 1);
                if (other == null)
                {
                    if (player.Position.Price <= player.Money)
                    {
                        moves.Add(new Move() { Type = Move.MoveType.BUY, Description = "Buy the property", Param = player.Position.Name, PlayerId = player.Id });
                    }

                    // not handled
                    moves.Add(new Move()
                    {
                        Type = Move.MoveType.BIDONAUCTION,
                        Description = "Auction the property",
                        Param = "",
                        PlayerId = player.Id,
                        OfferField = player.Position.Name,
                        OfferPrice = (player.Position.Price / 2)
                    });

                    var auction = new Auction()
                    {
                        Field = player.Position,
                        Game = player.Game,
                        Price = player.Position.Price / 2
                    };

                    ctx.Auctions.Add(auction);
                    ctx.SaveChanges();
                }
                else if (other != null && !player.HasPaid && player.Id != other.Id)
                {
                    Reality reality = other.Realities.SingleOrDefault(r => r.Field.Id == player.Position.Id);
                    if (reality.IsMortgaged)
                    {
                        // shouldn't pay if it is mortgaged.
                        player.HasPaid = true;
                        return;
                    }

                    long fee = 0;
                    if (player.Position.Type == "Railway")
                    {
                        int ownedRailways = other.Realities.Count(r => r.Field.Type == "Railway" && !r.IsMortgaged);
                        fee = player.Position.Price * ownedRailways / 4;
                    }
                    else if (player.Position.Type == "Utility")
                    {
                        Random rnd = new Random();
                        int score = rnd.Next(1, 7);
                        score += rnd.Next(1, 7);
                        fee = score * 10;
                    }
                    else if (player.Position.Type == "Tax")
                    {
                        player.Money -= player.Position.Price;
                        moves.Add(new Move() { Type = Move.MoveType.MSG, Description = "You paid tax ", Param = player.Position.Price.ToString() });
                    }
                    else if (player.Position.Type == "ToJail")
                    {
                        Field jail = ctx.Fields.SingleOrDefault(f => f.Type == "Jail");
                        player.Position = jail;
                        player.JailValue = 1;
                        ctx.SaveChanges();
                        moves.Add(new Move() { Type = Move.MoveType.MSG, Description = "You went to jail." });
                    }
                    else if (player.Position.Type != "Free")
                    {
                        int sameColor = other.Realities.Count(r => r.Field.Type == player.Position.Type);
                        int maxThisColor = ctx.Fields.Count(f => f.Type == player.Position.Type);
                        if (reality.Houses + reality.Hotels > 0 || maxThisColor > sameColor)
                        {
                            fee = player.Position.InitialFee + player.Position.InitialFee * reality.Houses ^ 2;
                        }
                        else
                        {
                            fee = player.Position.InitialFee * 2;
                        }
                    }

                    // Pay fee
                    player.Money -= fee;
                    other.Money += fee;
                    player.HasPaid = true;
                    ctx.SaveChanges();
                    if (fee <= player.Money)
                    {
                        moves.Add(new Move() { Type = Move.MoveType.MSG, Description = "You paid the rent for " + player.Position.Name + " which cost you ", Param = fee.ToString() });
                    }
                    else
                    {
                        moves.Add(new Move() { Type = Move.MoveType.MSG, Description = "Staying at " + player.Position.Name + " is too expensive for you. You need to mortgage or sell some of your properties. It costs ", Param = fee.ToString() });
                    }
                }
            }
        }
示例#18
0
 /// <summary>
 /// Makes the sale and removes the offer from the database.
 /// If the offer is declined it just removes the offer from the database.
 /// </summary>
 /// <param name="move"></param>
 private void ReplyOfferMoveProcessor(Move move)
 {
     using (var ctx = new MonopolyModelContainer())
     {
         Player player = ctx.Players.SingleOrDefault(p => p.Id == move.PlayerId);
         Offer offer = ctx.Offers.SingleOrDefault(o => o.Id == move.Offer.Id);
         if (offer != null && move.Param == "OK")
         {
             Player partner = ctx.Players.SingleOrDefault(p => p.User.Email == offer.From.User.Email);
             Reality reality = offer.Reality;
             player.Money += move.OfferPrice;
             partner.Money -= move.OfferPrice;
             reality.Player = partner;
             partner.Realities.Add(reality);
             player.Realities.Remove(reality);
         }
         ctx.Offers.Remove(offer);
         ctx.SaveChanges();
     }
 }
 /// <summary>
 /// Registers a new user.
 /// </summary>
 /// <param name="email"></param>
 /// <param name="isGuest"></param>
 internal static void RegisterUser(string email, bool isGuest)
 {
     using (var ctx = new MonopolyModelContainer())
     {
         // check whether he is already in the system
         User user = ctx.Users.SingleOrDefault(u => u.Email == email);
         if (user == null)
         {
             user = new User() { Email = email, IsGuest = isGuest, GUID = Guid.NewGuid().ToString("N") };
             ctx.Users.Add(user);
             ctx.SaveChanges();
         }
         else if (!isGuest && user.IsGuest)
         {
             user.IsGuest = false;
             ctx.SaveChanges();
         }
     }
 }
示例#20
0
 private void UnmortgageMoveProcessor(Move move)
 {
     using (var ctx = new MonopolyModelContainer())
     {
         Player player = ctx.Players.SingleOrDefault(p => p.Id == move.PlayerId);
         Reality reality = player.Realities.SingleOrDefault(f => f.Field.Name == move.Param);
         player.Money -= reality.Field.Price * 55 / 100;
         reality.IsMortgaged = false;
         ctx.SaveChanges();
     }
 }
示例#21
0
        private void BankruptcyMoveProcessor(Move move)
        {
            using (var ctx = new MonopolyModelContainer())
            {
                Player player = ctx.Players.SingleOrDefault(p => p.Id == move.PlayerId);
                player.IsBankrupted = true;
                player.Realities.Clear();
                player.Money = 0;
                player.JailValue = 0;
                if (player.SequenceNumber != player.Game.ActivePlayers)
                {
                    foreach (Player p in player.Game.Players)
                    {
                        if (p.SequenceNumber > player.SequenceNumber)
                        {
                            p.SequenceNumber--;
                        }
                    }
                }

                player.Game.ActivePlayers--;
                player.SequenceNumber = 0;
                ctx.SaveChanges();
            }
        }
示例#22
0
 private void UseJailCardMoveProcessor(Move move)
 {
     using (var ctx = new MonopolyModelContainer())
     {
         Player player = ctx.Players.SingleOrDefault(p => p.Id == move.PlayerId);
         player.HasJailCard = false;
         player.JailValue = 0;
         ctx.SaveChanges();
     }
 }
示例#23
0
 private void BidOnAuctionProcessor(Move move)
 {
     using (var ctx = new MonopolyModelContainer())
     {
         Player player = ctx.Players.SingleOrDefault(p => p.Id == move.PlayerId);
         // process ongoing auctions
         var auction = ctx.Auctions.SingleOrDefault(au => au.Game.Id == player.Game.Id && au.Field.Name == move.OfferField);
         if (auction != null && auction.Price < move.OfferPrice)
         {
             auction.Price = move.OfferPrice;
             auction.Winner = player;
             ctx.SaveChanges();
         }
     }
 }
示例#24
0
        private void AuctionMoveProcessor(Move move)
        {
            using (var ctx = new MonopolyModelContainer())
            {
                Player player = ctx.Players.SingleOrDefault(p => p.Id == move.PlayerId);
                Field field = ctx.Fields.SingleOrDefault(f => f.Name == move.OfferField);
                if (player != null && field != null)
                {
                    var auction = new Auction()
                    {
                        Game = player.Game,
                        Field = field,
                        Price = move.OfferPrice,
                    };

                    ctx.Auctions.Add(auction);
                    ctx.SaveChanges();
                }
            }
        }
        internal static bool StartGame(int gameId)
        {
            Game game = null;
            using (var ctx = new MonopolyModelContainer())
            {
                game = ctx.Games.SingleOrDefault(g => g.Id == gameId);
                if (game.Users.Count >= 2)
                {
                    game.Status = "Started";
                    game.StartDate = DateTime.Now;
                    game.ActivePlayers = game.Players.Count();
                    ctx.SaveChanges();
                    return true;
                }
            }

            return false;
        }