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;
        }
        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();
                }
            }
        }
 private void WithdrawMoveProcessor(Move move)
 {
     using (var ctx = new MonopolyModelContainer())
     {
         ctx.Offers.Remove(move.Offer);
         ctx.SaveChanges();
     }
 }
 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();
     }
 }
 private void SellMoveProcessor(Move move)
 {
     using (var ctx = new MonopolyModelContainer())
     {
         Player player = ctx.Players.SingleOrDefault(p => p.Id == move.PlayerId);
         Reality reality = player.Realities.SingleOrDefault(r => r.Field.Name == move.Param);
         // let out exception (it shouldnt be any)
         //if (reality != null)
         {
             reality.Houses = reality.Hotels == 1 ? 4 : reality.Houses - 1;
             reality.Hotels = 0;
             player.Money += reality.Field.HousePrice;
         }
     }
 }
 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();
     }
 }
 /// <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();
     }
 }
        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();
            }
        }
        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();
            }
        }
 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();
     }
 }
        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();
            }
        }
        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();
            }
        }
 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();
     }
 }
 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();
         }
     }
 }
        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();
            }
        }