示例#1
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();
                }
            }
        }
        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() });
                    }
                }
            }
        }