Exemple #1
0
        private async Task <TColValue> GetKeyCol <TColValue, TPrimaryKey>(Nuid id, string table_name, TPrimaryKey primary_key, int col)
        {
            if (NodeType == NodeType.Grain)
            {
                Entity entity = EntityManager.Get(id);

                if (entity != null)
                {
                    Table <TPrimaryKey> table = entity.GetTable(table_name) as Table <TPrimaryKey>;
                    if (table == null)
                    {
                        return(default(TColValue));
                    }

                    return(table.GetCol <TColValue>(primary_key, col));
                }
            }
            else if (NodeType == NodeType.Cache)
            {
                NList row_value = await GetCacheTableKeyValue(id, table_name, primary_key);

                return(row_value.Get <TColValue>(col));
            }

            return(default(TColValue));
        }
Exemple #2
0
        private async Task <T> GetField <T>(Nuid id, string field_name)
        {
            if (NodeType == NodeType.Grain)
            {
                Entity entity = EntityManager.Get(id);
                if (entity == null)
                {
                    return(default(T));
                }

                Field field = entity.GetField(field_name);
                if (field == null)
                {
                    return(default(T));
                }

                return(field.Get <T>());
            }
            else if (NodeType == NodeType.Cache)
            {
                return(await GetCacheField <T>(id, field_name));
            }

            return(default(T));
        }
Exemple #3
0
        private async Task SetKeyCol <TColValue, TPrimaryKey>(Nuid id, string table_name, TPrimaryKey primary_key, int col, TColValue col_value)
        {
            if (col_value == null)
            {
                _Logger.LogError($"{id} SetKeyCol {table_name} {primary_key}_{col} when col_value is null!");
                return;
            }

            if (NodeType == NodeType.Grain)
            {
                Entity entity = EntityManager.Get(id);
                if (entity == null)
                {
                    _Logger.LogError($"{id} SetKeyCol {table_name} {primary_key}_{col} when Grain dont found entity!");
                    return;
                }

                Table <TPrimaryKey> table = entity.GetTable(table_name) as Table <TPrimaryKey>;
                if (table == null)
                {
                    _Logger.LogError($"{id} SetKeyCol {table_name} {primary_key}_{col} when Grain dont found field!");
                    return;
                }

                NList result;
                if (!table.TrySetKeyCol(primary_key, col, col_value, out result))
                {
                    _Logger.LogError($"{id} SetKeyCol {table_name} {primary_key}_{col} when {col_value} TrySetRowCol failed!");
                    return;
                }

                await CallbackTable(id, table_name, TableEvent.SetCol, result);
            }
            else if (NodeType == NodeType.Cache)
            {
                NList value = await GetCacheTableKeyValue(id, table_name, primary_key);

                TColValue old_value = value.Get <TColValue>(col);
                if (old_value.Equals(col_value))
                {
                    _Logger.LogError($"{id} SetKeyCol {table_name} {primary_key}_{col} when new=old SetCacheRow failed!");
                }

                value.Set(col, col_value);

                if (!await SetCacheTableKeyValue(id, table_name, primary_key, value))
                {
                    _Logger.LogError($"{id} SetKeyCol {table_name} {primary_key}_{col} when {value} SetCacheTableKeyValue failed!");
                    return;
                }

                NList result = NList.New();
                result.Add(primary_key);
                result.Add(col);
                result.Add(old_value);
                result.Add(col_value);

                await CallbackTable(id, table_name, TableEvent.SetCol, result);
            }
        }
Exemple #4
0
        private async Task CallbackEntity(Nuid id, EntityEvent entity_event, NList args)
        {
            string entity_type = Global.NULL_STRING;
            Entity entity      = EntityManager.Get(id);

            if (entity != null)
            {
                entity_type = entity.Type;
            }
            else
            {
                entity_type = await GetCacheType(id);
            }

            EntityPrefab entity_prefab = Prefabs.GetEntity(entity_type);

            if (entity_prefab == null)
            {
                return;
            }

            if (entity_prefab.ancestors != null && entity_prefab.ancestors.Count > 0)
            {
                for (int i = entity_prefab.ancestors.Count - 1; i >= 0; i--)
                {
                    string parent_type = entity_prefab.ancestors[i];

                    await NModule.CallbackEntity(this, id, parent_type, entity_event, args);
                }
            }

            await NModule.CallbackEntity(this, id, entity_type, entity_event, args);
        }
Exemple #5
0
        public async Task <Nuid> Create(string type, Nuid origin, NList args)
        {
            long unique = IdUtils.UGen.CreateId();
            Nuid id     = Nuid.New(unique, origin.Origin);

            return(await Create(id, type, args));
        }
Exemple #6
0
        public async Task Destroy(Nuid id)
        {
            Entity entity = EntityManager.Get(id);

            if (entity != null)
            {
                await CallbackEntity(id, EntityEvent.OnDestroy, NList.Empty);

                EntityManager.Remove(id);
            }
            else
            {
                if (id.Origin == Identity)
                {
                    return;
                }

                INode node = GrainFactory.GetGrain <INode>(id.Origin);

                if (await node.IsActive())
                {
                    await node.Destroy(id);
                }
            }
        }
Exemple #7
0
        public async Task <INList> GetKeys(Nuid id, string table_name)
        {
            if (id.Origin != Identity)
            {
                INode node = GrainFactory.GetGrain <INode>(id.Origin);
                return(await node.GetKeys(id, table_name));
            }

            if (NodeType == NodeType.Grain)
            {
                Entity entity = EntityManager.Get(id);
                if (entity == null)
                {
                    _Logger.LogError($"{id} GetKeys {table_name} when Grain dont found entity!");
                    return(NList.Empty);
                }

                Table table = entity.GetTable(table_name);
                if (table == null)
                {
                    _Logger.LogError($"{id} GetKeys {table_name} when Grain dont found table!");
                    return(NList.Empty);
                }

                return(table.GetKeys());
            }
            else if (NodeType == NodeType.Cache)
            {
                return(await GetCacheKeys(id, table_name));
            }

            return(NList.Empty);
        }
Exemple #8
0
        private async Task <long> FindCacheRow <T>(Nuid id, string table_name, int col, T value)
        {
            if (!await CacheExist(id))
            {
                return(Global.INVALID_ROW);
            }

            long row = Global.INVALID_ROW;

            try
            {
                IRedisDatabase             db         = GetCache(id);
                string                     key        = CacheUtils.BuildTable(id, table_name);
                Dictionary <string, NList> row_values = await db.HashGetAllAsync <NList>(key);

                foreach (KeyValuePair <string, NList> pair in row_values)
                {
                    if (pair.Value.Get <T>(col).Equals(value))
                    {
                        row = long.Parse(pair.Key);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                _Logger.LogError(ex, "'{0} FindCacheRow error for table={1}", id, table_name);
            }

            return(row);
        }
Exemple #9
0
        public async Task <long> FindRow <T>(Nuid id, string table_name, int col, T value)
        {
            Entity entity = EntityManager.Get(id);

            if (entity != null)
            {
                Table table = entity.GetTable(table_name);
                if (table == null)
                {
                    return(Global.INVALID_ROW);
                }

                return(table.FindRow(col, value));
            }
            else
            {
                if (id.Origin == Identity)
                {
                    return(await FindCacheRow <T>(id, table_name, col, value));
                }
                else
                {
                    INode node = GrainFactory.GetGrain <INode>(id.Origin);
                    return(await node.FindRow(id, table_name, col, value));
                }
            }
        }
Exemple #10
0
        public async Task <Nuid> Create(Nuid id, string type, NList args)
        {
            if (id.Origin != Identity)
            {
                INode node = GrainFactory.GetGrain <INode>(id.Origin);
                return(await node.Create(id, type, args));
            }

            if (NodeType == NodeType.Grain)
            {
                Entity entity = EntityManager.Create(id, type);
                if (entity == null)
                {
                    _Logger.LogError($"{id} Create Entity Failed when {id} {type}!");
                    return(Nuid.Empty);
                }
            }
            else if (NodeType == NodeType.Cache)
            {
                if (!await SetCacheType(id, type))
                {
                    _Logger.LogError($"{id} Create Entity when SetCacheType Failed {id} {type}!");
                    return(Nuid.Empty);
                }
            }

            await CallbackEntity(id, EntityEvent.OnCreate, args);

            return(id);
        }
Exemple #11
0
        public async Task Custom(Nuid id, int custom, NList msg)
        {
            Entity entity = EntityManager.Get(id);

            if (entity != null)
            {
                await NModule.CallbackCustom(this, id, custom, msg);

                await SyncManager.Callback(SyncType.Custom, this, id, NList.New().Add(custom).Append(msg));
            }
            else
            {
                if (id.Origin == Identity)
                {
                    return;
                }

                INode node = GrainFactory.GetGrain <INode>(id.Origin);

                if (await node.IsActive())
                {
                    await node.Custom(id, custom, msg);
                }
            }
        }
Exemple #12
0
        private static async Task OnPlayerCreate(INode node, Nuid id, INList args)
        {
            await node.SetFieldInt(id, Player.Fields.LEVEL, 1100);

            await node.SetFieldString(id, "nick_name", "1sadasdasd");

            await node.AddHeartbeat(id, "test", 10000, 10, OnHeartbeat);

            await node.AddKeyValue(id, Player.Tables.QuestTable.TABLE_NAME, 1001, NList.New().Add(1).Add(TimeUtils.NowMilliseconds));

            await node.AddKeyValue(id, Player.Tables.QuestTable.TABLE_NAME, 2002, NList.New().Add(2).Add(TimeUtils.NowMilliseconds));

            await node.AddKeyValue(id, Player.Tables.QuestTable.TABLE_NAME, 3003, NList.New().Add(3).Add(TimeUtils.NowMilliseconds));

            await node.Create("item", id, NList.New().Add(20001).Add(1));

            await node.Create("item", id, NList.New().Add(20002).Add(2));

            await node.Create("item", id, NList.New().Add(20003).Add(3));

            await node.Create("item", id, NList.New().Add(20004).Add(4));

            await node.Create("item", id, NList.New().Add(20005).Add(5));

            await node.Create("item", id, NList.New().Add(20006).Add(6));

            int status = await node.GetColInt(id, Player.Tables.QuestTable.TABLE_NAME, 1001, Player.Tables.QuestTable.COL_STATUS);
        }
Exemple #13
0
        public async Task <T> GetRowCol <T>(Nuid id, string table_name, long row, int col)
        {
            Entity entity = EntityManager.Get(id);

            if (entity != null)
            {
                Table table = entity.GetTable(table_name);
                if (table == null)
                {
                    return(default(T));
                }

                return(table.GetRowCol <T>(row, col));
            }
            else
            {
                if (id.Origin == Identity)
                {
                    NList row_value = await GetCacheRowValue(id, table_name, row);

                    return(row_value.Get <T>(col));
                }
                else
                {
                    INode node = GrainFactory.GetGrain <INode>(id.Origin);
                    return(await node.GetRowCol <T>(id, table_name, row, col));
                }
            }
        }
Exemple #14
0
        public async Task <Nuid> Create(string type, Nuid origin, NList args)
        {
            long unique = IdGenerator.NewIdentity();
            Nuid id     = Nuid.New(unique, origin.Origin);

            return(await Create(id, type, args));
        }
Exemple #15
0
        private async Task <string> GetCacheType(Nuid id)
        {
            IRedisDatabase db  = GetCache(id);
            string         key = CacheUtils.BuildEntities(id);

            return(await db.HashGetAsync <string>(key, id.Unique.ToString()));
        }
Exemple #16
0
        public Task AddHeartbeat(Nuid id, string timer, long gap_millseconds, int count, NEventCallback handler)
        {
            TimerManager.AddTimer(id, timer, gap_millseconds, count, handler);
            CheckTimer();

            return(Task.CompletedTask);
        }
Exemple #17
0
        private async Task <bool> CacheExist(Nuid id)
        {
            IRedisDatabase db  = GetCache(id);
            string         key = CacheUtils.BuildEntities(id);

            return(await db.HashExistsAsync(key, id.Unique.ToString()));
        }
Exemple #18
0
        public async Task <INList> GetRows(Nuid id, string table_name)
        {
            Entity entity = EntityManager.Get(id);

            if (entity != null)
            {
                Table table = entity.GetTable(table_name);
                if (table == null)
                {
                    return(NList.Empty);
                }

                return(table.GetRows());
            }
            else
            {
                if (id.Origin == Identity)
                {
                    return(await GetCacheRows(id, table_name));
                }
                else
                {
                    INode node = GrainFactory.GetGrain <INode>(id.Origin);
                    return(await node.GetRows(id, table_name));
                }
            }
        }
Exemple #19
0
        public Task AddCountdown(Nuid id, string timer, long over_millseconds, NEventCallback handler)
        {
            TimerManager.AddTimer(id, timer, over_millseconds, handler);
            CheckTimer();

            return(Task.CompletedTask);
        }
Exemple #20
0
        public async Task <string> GetType(Nuid id)
        {
            if (id.Origin != Identity)
            {
                INode node = GrainFactory.GetGrain <INode>(id.Origin);
                return(await node.GetType(id));
            }

            if (NodeType == NodeType.Grain)
            {
                Entity entity = EntityManager.Get(id);
                if (entity == null)
                {
                    _Logger.LogError($"{id} GetType when Grain dont found entity!");
                    return(Global.NULL_STRING);
                }

                return(entity.Type);
            }
            else if (NodeType == NodeType.Cache)
            {
                return(await GetCacheType(id));
            }

            return(Global.NULL_STRING);
        }
Exemple #21
0
        public async Task Leave(Nuid id)
        {
            Entity entity = EntityManager.Get(id);

            if (entity != null)
            {
                await CallbackEntity(id, EntityEvent.OnLeave, NList.Empty);

                await SyncEntity(id, NList.New().Add(id).Add((int)EntityEvent.OnLeave));
            }
            else
            {
                if (id.Origin == Identity)
                {
                    return;
                }

                INode node = GrainFactory.GetGrain <INode>(id.Origin);

                if (await node.IsActive())
                {
                    await node.Leave(id);
                }
            }
        }
Exemple #22
0
        public async Task <T> GetField <T>(Nuid id, string field_name)
        {
            Entity entity = EntityManager.Get(id);

            if (entity != null)
            {
                Field field = entity.GetField(field_name);
                if (field == null)
                {
                    return(default(T));
                }

                return(field.Get <T>());
            }
            else
            {
                if (id.Origin == Identity)
                {
                    return(await GetCacheField <T>(id, field_name));
                }
                else
                {
                    INode node = GrainFactory.GetGrain <INode>(id.Origin);

                    return(await node.GetField <T>(id, field_name));
                }
            }
        }
Exemple #23
0
        private async Task <bool> SetCacheType(Nuid id, string entity_type)
        {
            IRedisDatabase db  = GetCache(id);
            string         key = CacheUtils.BuildEntities(id);

            return(await db.Database.HashSetAsync(key, id.Unique, entity_type));
        }
Exemple #24
0
        private static async Task OnHeartbeat(INode node, Nuid id, INList args)
        {
            string timer_name      = args.Get <string>(0);
            long   now_ticks       = args.Get <long>(1);
            int    RemainBeatCount = args.Get <int>(2);

            await node.Error(string.Format("{0} beat {1}", timer_name, RemainBeatCount));
        }
Exemple #25
0
        public async Task <long> AddRow(Nuid id, string table_name, NList value)
        {
            if (NList.IsEmpty(value))
            {
                return(Global.INVALID_ROW);
            }

            Entity entity = EntityManager.Get(id);

            if (entity != null)
            {
                Table table = entity.GetTable(table_name);
                if (table == null)
                {
                    return(Global.INVALID_ROW);
                }

                NList result;
                if (!table.TryAddRow(value, out result))
                {
                    return(Global.INVALID_ROW);
                }

                long  row       = result.Get <long>(0);
                NList row_value = result.Get <NList>(1);
                BatchCahceList.Add(NList.New().Add((int)CacheOption.SetRow).Add(id).Add(table_name).Add(row).Add(row_value));

                await CallbackTable(id, table_name, TableEvent.AddRow, result);

                return(row);
            }
            else
            {
                if (id.Origin == Identity)
                {
                    long row = IdUtils.RGen.CreateId();
                    if (await SetCacheRow(id, table_name, row, value))
                    {
                        NList result = NList.New();
                        result.Add(row);
                        result.Append(value);

                        await CallbackTable(id, table_name, TableEvent.AddRow, result);

                        return(row);
                    }
                    else
                    {
                        return(Global.INVALID_ROW);
                    }
                }
                else
                {
                    INode node = GrainFactory.GetGrain <INode>(id.Origin);
                    return(await node.AddRow(id, table_name, value));
                }
            }
        }
Exemple #26
0
        private static string BuildEntity(Nuid entity_id)
        {
            StringBuilder key = new StringBuilder();
            key.Append(entity_id.Origin);
            key.Append(Global.NUID_MEMBERS_FLAG);
            key.Append(entity_id.Unique);

            return key.ToString();
        }
Exemple #27
0
        public bool Find(Nuid id)
        {
            if (_CurEntity != null && _CurEntity.Id == id)
            {
                return(true);
            }

            return(_EntityDic.ContainsKey(id));
        }
Exemple #28
0
        private static async Task OnCommand_Test(INode node, Nuid id, INList args)
        {
            Nuid target_id = args.Get <Nuid>(0);

            int level = await node.GetField <int>(id, "level");

            level++;
            await node.SetField(target_id, "level", level);
        }
Exemple #29
0
        public static string BuildEntities(Nuid entity_id)
        {
            StringBuilder key = new StringBuilder();
            key.Append(entity_id.Origin);
            key.Append(Global.NUID_MEMBERS_FLAG);
            key.Append(MARK_ENTITIES);

            return key.ToString();
        }
Exemple #30
0
        public static string BuildFields(Nuid entity_id)
        {
            StringBuilder key = new StringBuilder();
            key.Append(BuildEntity(entity_id));
            key.Append(Global.NUID_MEMBERS_FLAG);
            key.Append(MARK_FIELDS);

            return key.ToString();
        }