Example #1
0
    private void SaveProcess(List <Level> avatars)
    {
        var context = new ucsdbEntities(m_vConnectionString);

        context.Configuration.AutoDetectChangesEnabled = false;
        context.Configuration.ValidateOnSaveEnabled    = false;
        var transactionCount = 0;

        try
        {
            foreach (var pl in avatars)
            {
                lock (pl)
                {
                    context = pl.SaveToDatabse(context);
                }
                transactionCount++;
                if (transactionCount >= 100)
                {
                    context.SaveChanges();
                    transactionCount = 0;
                }
            }
            context.SaveChanges();
            context.Dispose();
        }
        catch (Exception ex)
        {
            MainWindow.RemoteWindow.WriteConsole("Exception when saving: " + ex, (int)MainWindow.level.FATAL);
        }
    }
Example #2
0
        public Clan GetAlliance(long allianceId)
        {
            Clan alliance = null;

            try
            {
                using (var db = new ucsdbEntities(m_vConnectionString))
                {
                    var p = db.Clans.Find(allianceId);

                    // Check if player exists
                    if (p != null)
                    {
                        alliance = new Clan();
                        alliance.LoadFromJSON(p.Data);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Write("An exception occured during GetAlliance processing:" + Debug.FlattenException(ex));
            }

            return(alliance);
        }
Example #3
0
        public ConcurrentDictionary <long, Level> GetAllPlayers()
        {
            ConcurrentDictionary <long, Level> players = new ConcurrentDictionary <long, Level>();

            try
            {
                using (ucsdbEntities db = new ucsdbEntities(m_vConnectionString))
                {
                    DbSet <player> a     = db.player;
                    int            count = 0;
                    foreach (player c in a)
                    {
                        Level pl = new Level();
                        players.TryAdd(pl.GetPlayerAvatar().GetId(), pl);
                        if (count++ >= 500)
                        {
                            break;
                        }
                    }
                    //Debugger.WriteLine("[UCS]    The server loaded " + count + " players");
                }
            }
            catch (Exception ex)
            {
                //Debugger.WriteLine("[UCS]    An exception occured during GetPlayers processing:", ex);
            }
            return(players);
        }
Example #4
0
        /// <summary>
        ///     This function return all alliances in database, in a list<>.
        /// </summary>
        /// <returns>
        ///     Return a list<> containing all alliances.
        /// </returns>
        public List <Alliance> GetAllAlliances()
        {
            var alliances = new List <Alliance>();

            try
            {
                using (var db = new ucsdbEntities(m_vConnectionString))
                {
                    var a     = db.clan;
                    var count = 0;
                    foreach (var c in a)
                    {
                        var alliance = new Alliance();
                        alliance.LoadFromJSON(c.Data);
                        alliances.Add(alliance);
                        if (count++ >= 200)
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debugger.WriteLine("[UCS]    An exception occured during GetAlliance processing:", ex);
            }
            return(alliances);
        }
Example #5
0
 /// <summary>
 ///     This function create a new player in the database, with default parameters.
 /// </summary>
 /// <param name="l">The level of the player.</param>
 public void CreateAccount(Level l)
 {
     try
     {
         using (ucsdbEntities db = new ucsdbEntities(m_vConnectionString))
         {
             db.player.Add(
                 new player
             {
                 PlayerId          = l.GetPlayerAvatar().GetId(),
                 AccountStatus     = l.GetAccountStatus(),
                 AccountPrivileges = l.GetAccountPrivileges(),
                 LastUpdateTime    = l.GetTime(),
                 IPAddress         = l.GetIPAddress(),
                 Avatar            = l.GetPlayerAvatar().SaveToJSON(),
                 GameObjects       = l.SaveToJSON()
             }
                 );
             db.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         Logger.Write("Error when try to Create an Account " + ex);
     }
 }
Example #6
0
        /// <summary>
        ///     This function get the player data.
        /// </summary>
        /// <param name="playerId">The (int64) ID of the player.</param>
        /// <returns>The level of the player.</returns>
        public Level GetAccount(long playerId)
        {
            Level account = null;

            try
            {
                using (ucsdbEntities db = new ucsdbEntities(m_vConnectionString))
                {
                    player p = db.player.Find(playerId);

                    if (p != null)
                    {
                        account = new Level();
                        account.SetAccountStatus(p.AccountStatus);
                        account.SetAccountPrivileges(p.AccountPrivileges);
                        account.SetTime(p.LastUpdateTime);
                        account.SetIPAddress(p.IPAddress);
                        account.GetPlayerAvatar().LoadFromJSON(p.Avatar);
                        account.LoadFromJSON(p.GameObjects);
                    }
                }
            }
            catch (Exception ex)
            {
                //Debugger.WriteLine("[UCS]    An exception occured during GetAccount processing :", ex);
            }
            return(account);
        }
Example #7
0
        public ConcurrentDictionary <long, Level> GetAllPlayers()
        {
            ConcurrentDictionary <long, Level> players = new ConcurrentDictionary <long, Level>();

            try
            {
                using (ucsdbEntities db = new ucsdbEntities(m_vConnectionString))
                {
                    DbSet <player> a     = db.player;
                    int            count = 0;
                    foreach (player c in a)
                    {
                        Level pl = new Level();
                        players.TryAdd(pl.GetPlayerAvatar().GetId(), pl);
                        if (count++ >= 100)
                        {
                            break;
                        }
                    }
                    _Logger.Print("     The server loaded " + count + " players", Types.DEBUG);
                }
            }
            catch (Exception ex)
            {
                _Logger.Print("      An exception occured during GetPlayers processing:", Types.ERROR);
            }
            return(players);
        }
Example #8
0
        /// <summary>
        ///     This function return all alliances in database, in a list<>.
        /// </summary>
        /// <returns>
        ///     Return a list<> containing all alliances.
        /// </returns>
        public List <Alliance> GetAllAlliances()
        {
            List <Alliance> alliances = new List <Alliance>();

            try
            {
                using (ucsdbEntities db = new ucsdbEntities(m_vConnectionString))
                {
                    DbSet <clan> a     = db.clan;
                    int          count = 0;
                    foreach (clan c in a)
                    {
                        Alliance alliance = new Alliance();
                        alliance.LoadFromJSON(c.Data);
                        alliances.Add(alliance);
                        if (count++ >= 100)
                        {
                            break;
                        }
                    }
                    //Logger.Write("Loaded  " + count + " Alliances");
                }
            }
            catch (Exception ex)
            {
                Logger.Write("Error when try to get All Alliances: " + ex);
            }
            return(alliances);
        }
        public void CreateLevel(Level level)
        {
            try
            {
                using (var ctx = new ucsdbEntities(m_vConnectionString))
                {
                    var newPlayer = new player
                    {
                        PlayerId          = level.GetPlayerAvatar().GetId(),
                        AccountStatus     = level.GetAccountStatus(),
                        AccountPrivileges = level.GetAccountPrivileges(),
                        LastUpdateTime    = level.GetTime(),
                        IPAddress         = level.GetIPAddress(),
                        Avatar            = level.GetPlayerAvatar().SaveToJson(),
                        GameObjects       = level.SaveToJson()
                    };

                    ctx.player.Add(newPlayer);
                    ctx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                ExceptionLogger.Log(ex, "Exception while trying to create a new player account in database.");
            }
        }
Example #10
0
        /// <summary>
        ///     This function return all alliances in database, in a list<>.
        /// </summary>
        /// <returns>
        ///     Return a list<> containing all alliances.
        /// </returns>
        public List <Alliance> GetAllAlliances()
        {
            List <Alliance> alliances = new List <Alliance>();

            try
            {
                using (ucsdbEntities db = new ucsdbEntities(m_vConnectionString))
                {
                    DbSet <clan> a     = db.clan;
                    int          count = 0;
                    foreach (clan c in a)
                    {
                        Alliance alliance = new Alliance();
                        alliance.LoadFromJSON(c.Data);
                        alliances.Add(alliance);
                        if (count++ >= 100)
                        {
                            break;
                        }
                    }
                    _Logger.Print("     The server loaded " + count + " alliances", Types.DEBUG);
                }
            }
            catch (Exception ex)
            {
                _Logger.Print("      An exception occured during GetAlliance processing:", Types.ERROR);
            }
            return(alliances);
        }
        public async Task Save(Alliance alliance)
        {
            using (ucsdbEntities ctx = new ucsdbEntities(m_vConnectionString))
            {
                ctx.Configuration.AutoDetectChangesEnabled = false;
                ctx.Configuration.ValidateOnSaveEnabled    = false;
                clan c = await ctx.clan.FindAsync((int)alliance.GetAllianceId());

                if (c != null)
                {
                    c.LastUpdateTime   = DateTime.Now;
                    c.Data             = alliance.SaveToJson();
                    ctx.Entry(c).State = EntityState.Modified;
                }
                //else
                //{
                //    context.clan.Add(
                //        new clan
                //        {
                //            ClanId = alliance.GetAllianceId(),
                //            LastUpdateTime = DateTime.Now,
                //            Data = alliance.SaveToJSON()
                //        });
                //}
                await ctx.SaveChangesAsync();
            }
        }
        public Alliance GetAlliance(long allianceId)
        {
            var alliance = default(Alliance);

            try
            {
                using (var ctx = new ucsdbEntities(m_vConnectionString))
                {
                    var clan = ctx.clan.Find(allianceId);
                    if (clan != null)
                    {
                        alliance = new Alliance();
                        alliance.LoadFromJson(clan.Data);
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionLogger.Log(ex, "Exception while trying to get alliance from database.");

                // In case it fails to LoadFromJSON.
                alliance = null;
            }

            return(alliance);
        }
        public Level GetLevel(long userId)
        {
            var level = default(Level);

            try
            {
                using (var ctx = new ucsdbEntities(m_vConnectionString))
                {
                    var player = ctx.player.Find(userId);
                    if (player != null)
                    {
                        level = new Level();
                        level.SetAccountStatus(player.AccountStatus);
                        level.SetAccountPrivileges(player.AccountPrivileges);
                        level.SetTime(player.LastUpdateTime);
                        level.GetPlayerAvatar().LoadFromJson(player.Avatar);
                        level.LoadFromJson(player.GameObjects);
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionLogger.Log(ex, $"Exception while trying to get a level {userId} from the database.");

                // In case the level instance was already created before the exception.
                level = null;
            }
            return(level);
        }
Example #14
0
        public Level GetAccount(long playerId)
        {
            Level level = null;

            try
            {
                using (ucsdbEntities ucsdbEntities = new ucsdbEntities(this.m_vConnectionString))
                {
                    player player = ucsdbEntities.player.Find(new object[]
                    {
                        playerId
                    });
                    if (player != null)
                    {
                        level = new Level();
                        level.SetAccountStatus(player.AccountStatus);
                        level.SetAccountPrivileges(player.AccountPrivileges);
                        level.SetTime(player.LastUpdateTime);
                        level.SetIPAddress(player.IPAddress);
                        level.GetPlayerAvatar().LoadFromJSON(player.Avatar);
                        level.LoadFromJSON(player.GameObjects);
                    }
                }
            }
            catch (Exception ex)
            {
                Debugger.WriteLine("[CRS]    An exception occured during GetAccount processing :", ex, 4);
            }
            return(level);
        }
Example #15
0
 public void CheckConnection()
 {
     try
     {
         using (ucsdbEntities db = new ucsdbEntities(m_vConnectionString))
         {
             (from ep in db.player select(long?) ep.PlayerId ?? 0).DefaultIfEmpty().Max();
         }
     }
     catch (EntityException)
     {
         if (ConfigurationManager.AppSettings["databaseConnectionName"] == "mysql")
         {
             _Logger.Print("     An exception occured when connecting to the MySQL Server.", Types.ERROR);
             _Logger.Print("     Please check your database configuration !", Types.ERROR);
             Console.ReadLine();
             Environment.Exit(0);
         }
         else
         {
             _Logger.Print("     An exception occured when connecting to the SQLite database.", Types.ERROR);
             _Logger.Print("     Please check your database configuration !", Types.ERROR);
             Console.ReadLine();
             Environment.Exit(0);
         }
     }
     catch (Exception)
     {
         _Logger.Print("     An exception occured when connecting to the SQL Server.", Types.ERROR);
         _Logger.Print("     Please check your database configuration !", Types.ERROR);
         Console.ReadKey();
         Environment.Exit(0);
     }
 }
Example #16
0
 public void Save(Alliance alliance)
 {
     Debugger.WriteLine("Starting saving clan " + alliance.GetAllianceName() + " from memory to database at " + DateTime.Now);
     using (var context = new ucsdbEntities(m_vConnectionString))
     {
         context.Configuration.AutoDetectChangesEnabled = false;
         context.Configuration.ValidateOnSaveEnabled    = false;
         var c = context.clan.Find((int)alliance.GetAllianceId());
         if (c != null)
         {
             c.LastUpdateTime       = DateTime.Now;
             c.Data                 = alliance.SaveToJSON();
             context.Entry(c).State = EntityState.Modified;
         }
         else
         {
             context.clan.Add(
                 new clan
             {
                 ClanId         = alliance.GetAllianceId(),
                 LastUpdateTime = DateTime.Now,
                 Data           = alliance.SaveToJSON()
             }
                 );
         }
         context.SaveChanges();
         Debugger.WriteLine("Finished saving clan " + alliance.GetAllianceName() + " from memory to database at " + DateTime.Now);
     }
 }
Example #17
0
        public List <Alliance> Get100Alliances()
        {
            List <Alliance> alliances = new List <Alliance>();

            try
            {
                using (ucsdbEntities db = new ucsdbEntities(m_vConnectionString))
                {
                    DbSet <clan> a     = db.clan;
                    int          count = 0;
                    Parallel.ForEach(a, (c, state) =>
                    {
                        Alliance alliance = new Alliance();
                        alliance.LoadFromJSON(c.Data);
                        alliances.Add(alliance);
                        if (count++ >= 100)
                        {
                            state.Stop();
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                Logger.Write("Error when try to get 100 Alliances: " + ex);
            }
            return(alliances);
        }
Example #18
0
        public ConcurrentDictionary <long, Level> GetAllPlayers()
        {
            ConcurrentDictionary <long, Level> players = new ConcurrentDictionary <long, Level>();

            try
            {
                using (ucsdbEntities db = new ucsdbEntities(m_vConnectionString))
                {
                    DbSet <player> a     = db.player;
                    int            count = 0;
                    foreach (player c in a)
                    {
                        Level pl = new Level();
                        players.TryAdd(pl.GetPlayerAvatar().GetId(), pl);
                        if (count++ >= 100)
                        {
                            break;
                        }
                    }
                    //Logger.Write("Loaded " + count + " Players");
                }
            }
            catch (Exception ex)
            {
                Logger.Write("Error when try to get All Players: " + ex);
            }
            return(players);
        }
Example #19
0
        public ucsdbEntities SaveToDatabse(ucsdbEntities context)
        {
            var p = context.player.Find(GetPlayerAvatar().GetId());

            if (p != null)
            {
                p.LastUpdateTime       = GetTime();
                p.AccountStatus        = GetAccountStatus();
                p.AccountPrivileges    = GetAccountPrivileges();
                p.Avatar               = GetPlayerAvatar().SaveToJSON();
                p.GameObjects          = SaveToJSON();
                context.Entry(p).State = EntityState.Modified;
            }
            else
            {
                context.player.Add(
                    new player
                {
                    PlayerId          = GetPlayerAvatar().GetId(),
                    AccountStatus     = GetAccountStatus(),
                    AccountPrivileges = GetAccountPrivileges(),
                    LastUpdateTime    = GetTime(),
                    Avatar            = GetPlayerAvatar().SaveToJSON(),
                    GameObjects       = SaveToJSON()
                }
                    );
            }
            return(context);
        }
Example #20
0
        /// <summary>
        ///     This function get the player data.
        /// </summary>
        /// <param name="playerId">The (int64) ID of the player.</param>
        /// <returns>The level of the player.</returns>
        public Level GetAccount(long playerId)
        {
            Level account = null;

            try
            {
                using (ucsdbEntities db = new ucsdbEntities(m_vConnectionString))
                {
                    player p = db.player.Find(playerId);

                    if (p != null)
                    {
                        account = new Level();
                        account.SetAccountStatus(p.AccountStatus);
                        account.SetAccountPrivileges(p.AccountPrivileges);
                        account.SetTime(p.LastUpdateTime);
                        account.SetIPAddress(p.IPAddress);
                        account.GetPlayerAvatar().LoadFromJSON(p.Avatar);
                        account.LoadFromJSON(p.GameObjects);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Write("Error when try to get an Account " + ex);
            }
            return(account);
        }
 public void CreateAccount(Level l)
 {
     try
     {
         Debugger.WriteLine("Saving new account to database (player id: " + l.GetPlayerAvatar().GetId() + ")",
                            null, 0);
         using (var db = new ucsdbEntities(m_vConnectionString))
         {
             db.player.Add(
                 new player
             {
                 PlayerId          = l.GetPlayerAvatar().GetId(),
                 AccountStatus     = l.GetAccountStatus(),
                 AccountPrivileges = l.GetAccountPrivileges(),
                 LastUpdateTime    = l.GetTime(),
                 Avatar            = l.GetPlayerAvatar().SaveToJSON(),
                 GameObjects       = l.SaveToJSON()
             }
                 );
             db.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         Debugger.WriteLine("An exception occured during CreateAccount processing:", ex);
     }
 }
Example #22
0
        /// <summary>
        ///     This function return all alliances in database, in a list<>.
        /// </summary>
        /// <returns>
        ///     Return a list<> containing all alliances.
        /// </returns>
        public List <Alliance> GetAllAlliances()
        {
            List <Alliance> alliances = new List <Alliance>();

            try
            {
                using (ucsdbEntities db = new ucsdbEntities(m_vConnectionString))
                {
                    DbSet <clan> a     = db.clan;
                    int          count = 0;
                    foreach (clan c in a)
                    {
                        Alliance alliance = new Alliance();
                        alliance.LoadFromJSON(c.Data);
                        alliances.Add(alliance);
                        if (count++ >= 500)
                        {
                            break;
                        }
                    }
                    //Debugger.WriteLine("[UCS]    The server loaded " + count + " alliances");
                }
            }
            catch (Exception ex)
            {
                //Debugger.WriteLine("[UCS]    An exception occured during GetAlliance processing:", ex);
            }
            return(alliances);
        }
Example #23
0
        public Level GetAccount(long playerId)
        {
            Level account = null;

            try
            {
                using (var db = new ucsdbEntities(m_vConnectionString))
                {
                    var p = db.Players.Find(playerId);

                    // Check if player exists
                    if (p != null)
                    {
                        account = new Level();
                        account.SetAccountStatus(p.AccountStatus);
                        account.SetAccountPrivileges(p.AccountPrivileges);
                        account.SetTime(p.LastUpdateTime);
                        account.GetPlayerAvatar().LoadFromJSON(p.Avatar);
                        account.LoadFromJSON(p.GameObjects);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Write("An exception occured during GetAccount processing:" + Debug.FlattenException(ex));
            }

            return(account);
        }
Example #24
0
 /// <summary>
 ///     This function create a new player in the database, with default parameters.
 /// </summary>
 /// <param name="l">The level of the player.</param>
 public void CreateAccount(Level l)
 {
     try
     {
         using (ucsdbEntities db = new ucsdbEntities(m_vConnectionString))
         {
             db.player.Add(
                 new player
             {
                 PlayerId          = l.GetPlayerAvatar().GetId(),
                 AccountStatus     = l.GetAccountStatus(),
                 AccountPrivileges = l.GetAccountPrivileges(),
                 LastUpdateTime    = l.GetTime(),
                 IPAddress         = l.GetIPAddress(),
                 Avatar            = l.GetPlayerAvatar().SaveToJSON(),
                 GameObjects       = l.SaveToJSON()
             }
                 );
             db.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         //Debugger.WriteLine("[UCS]    An exception occured during CreateAccount processing :", ex);
     }
 }
Example #25
0
        public Alliance GetAlliance(long allianceId)
        {
            Alliance alliance = null;

            try
            {
                using (ucsdbEntities ucsdbEntities = new ucsdbEntities(this.m_vConnectionString))
                {
                    clan clan = ucsdbEntities.clan.Find(new object[]
                    {
                        allianceId
                    });
                    if (clan != null)
                    {
                        alliance = new Alliance();
                        alliance.LoadFromJSON(clan.Data);
                    }
                }
            }
            catch (Exception ex)
            {
                Debugger.WriteLine("[CRS]    An exception occured during GetAlliance processing :", ex, 4);
            }
            return(alliance);
        }
Example #26
0
        /// <summary>
        ///     This function save a specific player in the database.
        /// </summary>
        /// <param name="avatar">The level of the player.</param>
        public void Save(Level avatar)
        {
            ucsdbEntities context = new ucsdbEntities(m_vConnectionString);

            context.Configuration.AutoDetectChangesEnabled = false;
            context.Configuration.ValidateOnSaveEnabled    = false;
            player p = context.player.Find(avatar.GetPlayerAvatar().GetId());

            if (p != null)
            {
                p.LastUpdateTime       = avatar.GetTime();
                p.AccountStatus        = avatar.GetAccountStatus();
                p.AccountPrivileges    = avatar.GetAccountPrivileges();
                p.IPAddress            = avatar.GetIPAddress();
                p.Avatar               = avatar.GetPlayerAvatar().SaveToJSON();
                p.GameObjects          = avatar.SaveToJSON();
                context.Entry(p).State = EntityState.Modified;
            }
            else
            {
                context.player.Add(
                    new player
                {
                    PlayerId          = avatar.GetPlayerAvatar().GetId(),
                    AccountStatus     = avatar.GetAccountStatus(),
                    AccountPrivileges = avatar.GetAccountPrivileges(),
                    LastUpdateTime    = avatar.GetTime(),
                    IPAddress         = avatar.GetIPAddress(),
                    Avatar            = avatar.GetPlayerAvatar().SaveToJSON(),
                    GameObjects       = avatar.SaveToJSON()
                }
                    );
            }
            context.SaveChanges();
        }
Example #27
0
 public void Save(Alliance alliance)
 {
     using (ucsdbEntities context = new ucsdbEntities(m_vConnectionString))
     {
         context.Configuration.AutoDetectChangesEnabled = false;
         context.Configuration.ValidateOnSaveEnabled    = false;
         clan c = context.clan.Find((int)alliance.GetAllianceId());
         if (c != null)
         {
             c.LastUpdateTime       = DateTime.Now;
             c.Data                 = alliance.SaveToJSON();
             context.Entry(c).State = EntityState.Modified;
         }
         else
         {
             context.clan.Add(
                 new clan
             {
                 ClanId         = alliance.GetAllianceId(),
                 LastUpdateTime = DateTime.Now,
                 Data           = alliance.SaveToJSON()
             }
                 );
         }
         context.SaveChanges();
     }
 }
Example #28
0
        /// <summary>
        ///     The function return the highest player id stored in database.
        /// </summary>
        /// <returns>An int64 long ID.</returns>
        public long GetMaxPlayerId()
        {
            long max = 0;

            using (ucsdbEntities db = new ucsdbEntities(m_vConnectionString))
                max = (from ep in db.player select(long?) ep.PlayerId ?? 0).DefaultIfEmpty().Max();
            return(max);
        }
Example #29
0
        /// <summary>
        ///     This function return the highest alliance id stored in database.
        /// </summary>
        /// <returns>An int64 (ID) .</returns>
        public long GetMaxAllianceId()
        {
            long max = 0;

            using (ucsdbEntities db = new ucsdbEntities(m_vConnectionString))
                max = (from alliance in db.clan select(long?) alliance.ClanId ?? 0).DefaultIfEmpty().Max();
            return(max);
        }
Example #30
0
        /// <summary>
        ///     This function get all players id in a list.
        /// </summary>
        /// <returns>A list of all players id.</returns>
        public List <long> GetAllPlayerIds()
        {
            List <long> ids = new List <long>();

            using (ucsdbEntities db = new ucsdbEntities(m_vConnectionString))
                ids.AddRange(db.player.Select(p => p.PlayerId));
            return(ids);
        }