public CampaignController getCampaignController(string campaignId)
        {
            CampaignController result = null;
            using (var ctx = new GcmlDbContext())
            {
                var existing = ctx.CampaignStates.FirstOrDefault(c => c.CampaignId == campaignId);
                if (existing != null)
                {
                    CampaignEngine engine = CampaignEngine.restoreFromState(existing);
                    result = new CampaignController(engine);
                }
            }

            return result;
        }
 public List<CampaignInfo> getCampaignsForPlayer(string playername)
 {
     List<CampaignInfo> result =  new List<CampaignInfo>();
     using(var ctx = new GcmlDbContext())
     {
         PlayerInfo player = ctx.Players.FirstOrDefault(p => p.playerName == playername);
         if (player != null)
         {
             var states = from state in ctx.CampaignStates
                          where state.ListPlayers.Contains(player)
                          select new CampaignInfo()
                                    {
                                        campaignId = state.CampaignId,
                                        campaignName = state.CampaignName,
                                        ListPlayerInfo = state.ListPlayers
                                    };
             result = states.ToList<CampaignInfo>();
         }
     }
     return result;
 }
        public PlayerInfo getPlayer(string playername)
        {
            PlayerInfo result = null;
            using (var ctx = new GcmlDbContext())
            {
                PlayerInfo player = (from p in ctx.Players
                    where p.playerName == playername
                    select p).FirstOrDefault();

                if (player == null)
                {
                    player = new PlayerInfo() { playerName = playername };
                    ctx.Players.Add(player);
                    ctx.SaveChanges();
                }

                result = player;
            }

            return result;
        }
        public bool safeCampaignState(CampaignController controller)
        {
            bool result = false;
            CampaignState state = controller.CampaignEngine.getState();

            using (var ctx = new GcmlDbContext())
            {
                //var existing = ctx.CampaignStates.Find(state.CampaignId);
                //if (existing == null)
                //{
                //    ctx.CampaignStates.Add(state);
                //}
                //else
                //{
                //    existing = state;
                //}

                ctx.CampaignStates.AddOrUpdate(state);
                ctx.SaveChanges();
                result = true;
            }
            return result;
        }