Example #1
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);
            }
        }
Example #2
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);
                }
            }
        }
Example #3
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);
                }
            }
        }
Example #4
0
        internal void IncomingMessage(NList message)
        {
            int   message_id   = message.Get <int>(0);
            NList dispatch_msg = message.GetRange(1, message.Count - 1);

            if (message_id == SystemMsg.CLIENT.ACCESS_TOKEN && !Inited)
            {
                string       access_token = dispatch_msg.Get <string>(0);
                int          realm        = dispatch_msg.Get <int>(1);
                IAccessToken token        = _ClusterClient.GetGrain <IAccessToken>(access_token);
                Guid         user_id      = token.GetUserId().Result;
                _User = _ClusterClient.GetGrain <IUser>(user_id);
                long role = _User.GetRole(realm).Result;

                RoleAgent = _ClusterClient.GetGrain <IRoleAgent>(role);
                RoleAgent.BindSession(user_id, Protocol.ToString());
                _AgentObserver = new AgentObserver(this);
                var stream = _ClusterClient.GetStreamProvider(StreamProviders.AgentProvider).GetStream <NList>(user_id, Protocol.ToString());
                _SubscriptionHandle = stream.SubscribeAsync(_AgentObserver).Result;

                Inited = true;

                OutcomingMessage(NList.New().Add(SystemMsg.SERVER.ACCESS_TOKEN).Add(true));
            }
            else if (Inited)
            {
                if (RoleAgent != null)
                {
                    RoleAgent.OnResponse(message_id, dispatch_msg);
                }
            }
        }
Example #5
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);
        }
Example #6
0
        public async Task SendEntities()
        {
            NList entities = await _RoleNode.GetEntities();

            NList message = NList.New().Add(SystemMsg.SERVER.LOAD_ENTITIES).Add(Prefabs.JSON).Add(_RoleId).Add(entities);

            await SendMessage(message);
        }
Example #7
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));
                }
            }
        }
Example #8
0
        public async Task SetRowCol <T>(Nuid id, string table_name, long row, int col, T value)
        {
            if (value == null)
            {
                return;
            }

            Entity entity = EntityManager.Get(id);

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

                NList result;
                if (!table.TrySetRowCol(row, col, value, out result))
                {
                    return;
                }

                NList row_value = table.GetRow(row);
                BatchCahceList.Add(NList.New().Add((int)CacheOption.SetRow).Add(id).Add(table_name).Add(row).Add(row_value));
                await CallbackTable(id, table_name, TableEvent.SetCol, result);
            }
            else
            {
                if (id.Origin == Identity)
                {
                    NList row_value = NList.New();
                    NList old_value = await GetCacheRowValue(id, table_name, row);

                    T old_row_value = old_value.Get <T>(col);
                    row_value.Append(old_value);
                    row_value.Set(col, value);

                    if (await SetCacheRow(id, table_name, row, row_value))
                    {
                        NList result = NList.New();
                        result.Add(row);
                        result.Add(col);
                        result.Add(old_row_value);
                        result.Add(value);

                        await CallbackTable(id, table_name, TableEvent.SetCol, result);
                    }
                }
                else
                {
                    INode node = GrainFactory.GetGrain <INode>(id.Origin);
                    await node.SetRowCol(id, table_name, row, col, value);
                }
            }
        }
Example #9
0
        private async Task OnItemCreate(INode node, Nuid id, INList args)
        {
            int item_entry = args.Get <int>(0);
            int item_count = args.Get <int>(1);
            await node.SetFieldInt(id, Item.Fields.ENTRY, item_entry);

            await node.SetFieldInt(id, Item.Fields.COUNT, item_count);

            await node.AddKeyValue(id, Item.Tables.StarTable.TABLE_NAME, 1001, NList.New().Add(1));

            await node.AddKeyValue(id, Item.Tables.StarTable.TABLE_NAME, 101, NList.New().Add(1));
        }
Example #10
0
        public Task <NList> GetEntities()
        {
            NList list = NList.New();
            IReadOnlyList <Entity> entities = EntityManager.GetEntities();

            foreach (Entity entity in entities)
            {
                list.Add(entity);
            }

            return(Task.FromResult(list));
        }
Example #11
0
        public async Task DelKey <TPrimaryKey>(Nuid id, string table_name, TPrimaryKey primary_key)
        {
            if (id.Origin != Identity)
            {
                INode node = GrainFactory.GetGrain <INode>(id.Origin);
                await node.DelKey(id, table_name, primary_key);

                return;
            }

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

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

                NList result;
                if (!table.TryDelKey(primary_key, out result))
                {
                    _Logger.LogError($"{id} DelKey {table_name} {primary_key} when Grain dont found primary_key!");
                    return;
                }

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

                if (!await DelCacheTableKey(id, table_name, primary_key))
                {
                    _Logger.LogError($"{id} DelCacheTableKey {table_name} {primary_key} when Grain dont found row!");
                    return;
                }

                NList result = NList.New();
                result.Add(primary_key);
                result.Add(row_value);
                await CallbackTable(id, table_name, TableEvent.DelKey, result);
            }
        }
Example #12
0
        private static async Task OnPlayerCreate(INode node, Nuid id, INList args)
        {
            await node.SetField(id, "level", 1100);

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

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

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

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

            await node.Create("item", id, NList.New());
        }
Example #13
0
        public async Task AddKeyValue <TPrimaryKey>(Nuid id, string table_name, TPrimaryKey primary_key, NList value)
        {
            if (id.Origin != Identity)
            {
                INode node = GrainFactory.GetGrain <INode>(id.Origin);
                await node.AddKeyValue(id, table_name, primary_key, value);

                return;
            }

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

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

                NList result;
                if (!table.TrySetKeyValue(primary_key, value, out result))
                {
                    _Logger.LogError($"{id} AddKeyValue {table_name} when Grain found row exist {primary_key}!");
                    return;
                }

                await CallbackTable(id, table_name, TableEvent.AddKey, result);
            }
            else if (NodeType == NodeType.Cache)
            {
                if (!await SetCacheTableKeyValue(id, table_name, primary_key, value))
                {
                    _Logger.LogError($"{id} AddKeyValue {table_name} when SetCacheTableKeyValue failed {primary_key} {value}!");
                    return;
                }

                NList result = NList.New();
                result.Add(primary_key);
                result.Add(value);

                await CallbackTable(id, table_name, TableEvent.AddKey, result);
            }
        }
Example #14
0
        private async Task SetField <T>(Nuid id, string field_name, T field_value)
        {
            if (field_value == null)
            {
                _Logger.LogError($"{id} SetField {field_name} when field_value is null!");
                return;
            }

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

                Field field = entity.GetField(field_name);
                if (field == null)
                {
                    _Logger.LogError($"{id} SetField {field_name} when Grain dont found field!");
                    return;
                }

                NList result;
                if (!field.TrySet(field_value, out result))
                {
                    _Logger.LogError($"{id} SetField {field_name} when {field_value} TrySet failed!");
                    return;
                }

                await CallbackField(id, field_name, FieldEvent.Change, result);
            }
            else if (NodeType == NodeType.Cache)
            {
                T old_value = await GetCacheField <T>(id, field_name);

                if (!await SetCacheField(id, field_name, field_value))
                {
                    return;
                }

                NList result = NList.New();
                result.Add(old_value);
                result.Add(field_value);
                await CallbackField(id, field_name, FieldEvent.Change, result);
            }
        }
Example #15
0
        public async Task SetField <T>(Nuid id, string field_name, T field_value)
        {
            if (field_value == null)
            {
                return;
            }

            Entity entity = EntityManager.Get(id);

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

                NList result;
                if (!field.TrySet(field_value, out result))
                {
                    return;
                }

                BatchCahceList.Add(NList.New().Add((int)CacheOption.SetField).Add(id).Add(field_name).Add(ProtoUtils.Serialize(field_value)));

                await CallbackField(id, field_name, FieldEvent.Change, result);
            }
            else
            {
                if (id.Origin == Identity)
                {
                    T old_value = await GetCacheField <T>(id, field_name);

                    if (await SetCacheField(id, field_name, field_value))
                    {
                        NList result = NList.New();
                        result.Add(old_value);
                        result.Add(field_value);
                        await CallbackField(id, field_name, FieldEvent.Change, result);
                    }
                }
                else
                {
                    INode node = GrainFactory.GetGrain <INode>(id.Origin);
                    await node.SetField(id, field_name, field_value);
                }
            }
        }
Example #16
0
        public async Task SetRowValue(Nuid id, string table_name, long row, NList value)
        {
            if (NList.IsEmpty(value))
            {
                return;
            }

            Entity entity = EntityManager.Get(id);

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

                NList result;
                if (!table.TrySetRow(row, value, out result))
                {
                    return;
                }

                BatchCahceList.Add(NList.New().Add((int)CacheOption.SetRow).Add(id).Add(table_name).Add(row).Add(value));
                await CallbackTable(id, table_name, TableEvent.SetRow, result);
            }
            else
            {
                if (id.Origin == Identity)
                {
                    if (await SetCacheRow(id, table_name, row, value))
                    {
                        NList result = NList.New();
                        result.Add(row);
                        result.Append(value);
                        await CallbackTable(id, table_name, TableEvent.SetRow, result);
                    }
                }
                else
                {
                    INode node = GrainFactory.GetGrain <INode>(id.Origin);
                    await node.SetRowValue(id, table_name, row, value);
                }
            }
        }
Example #17
0
        private async Task <NList> GetCacheRows(Nuid id, string table_name)
        {
            if (!await CacheExist(id))
            {
                return(NList.Empty);
            }

            IRedisDatabase db  = GetCache(id);
            string         key = CacheUtils.BuildTable(id, table_name);

            NList rows = NList.New();

            foreach (string hash_key in await db.HashKeysAsync(key))
            {
                int row = int.Parse(hash_key);
                rows.Add(row);
            }

            return(rows.Count > 0 ? rows : NList.Empty);
        }
Example #18
0
        private async Task CallbackTable(Nuid id, string table_name, TableEvent table_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.CallbackTable(this, id, parent_type, table_name, table_event, args);
                }
            }

            await NModule.CallbackTable(this, id, entity_type, table_name, table_event, args);

            TablePrefab table_prefab = entity_prefab.tables[table_name];

            if (table_prefab != null && table_prefab.sync)
            {
                NList msg = NList.New().Add(id).Add(table_name).Add((int)table_event).Append(args);
                await SyncTable(id, msg);
            }
        }
Example #19
0
        private async Task CallbackField(Nuid id, string field_name, FieldEvent field_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.CallbackField(this, id, parent_type, field_name, field_event, args);
                }
            }

            await NModule.CallbackField(this, id, entity_type, field_name, field_event, args);

            FieldPrefab field_prefab = entity_prefab.fields[field_name];

            if (field_prefab != null && field_prefab.sync)
            {
                NList msg = NList.New().Add(id).Add(field_name).Add((int)field_event).Append(args);
                await SyncManager.Callback(SyncType.Field, this, id, msg);
            }
        }
Example #20
0
        private async Task SyncTable(Nuid id, NList args)
        {
            if (!Activated)
            {
                return;
            }

            Entity entity = EntityManager.Get(id);

            if (entity == null)
            {
                return;
            }

            if (!entity.Activated)
            {
                return;
            }

            await Agent.SendMessage(NList.New().Add(SystemMsg.SERVER.SYNC_TABLE).Append(args));
        }
Example #21
0
        public async Task <Nuid> Create(Nuid id, string type, NList args)
        {
            if (id.Origin == Identity)
            {
                Entity entity = EntityManager.Create(id, type);
                if (entity == null)
                {
                    return(Nuid.Empty);
                }

                BatchCahceList.Add(NList.New().Add((int)CacheOption.SetEntity).Add(id).Add(type));

                await CallbackEntity(id, EntityEvent.OnCreate, args);
            }
            else
            {
                INode node = GrainFactory.GetGrain <INode>(id.Origin);
                return(await node.Create(id, type, args));
            }

            return(id);
        }
Example #22
0
        public override async Task OnActivateAsync()
        {
            await base.OnActivateAsync();

            _SystemListener = new Dictionary <int, Func <NList, Task> >();
            _Logger         = ServiceProvider.GetService <ILoggerFactory>().CreateLogger("RoleAgent[" + Role + "]");

            Role      = this.GetPrimaryKeyLong();
            _RoleNode = GrainFactory.GetGrain <INode>(Role);
            _RoleId   = Nuid.New(Role, Role);

            if (!await EntityDB.IsPersist(Role))
            {
                await EntityDB.SetNodeType(Role, NodeType.Grain);

                await _RoleNode.Create(_RoleId, Player.TYPE, NList.New());
            }

            await _RoleNode.BindAgent(this);

            RegisterSystem(SystemMsg.CLIENT.CUSTOM, OnRecv_Custom);
        }
Example #23
0
        public async Task Entry(Nuid id)
        {
            if (id.Origin != Identity)
            {
                INode node = GrainFactory.GetGrain <INode>(id.Origin);
                if (await node.IsActive())
                {
                    await node.Entry(id);

                    return;
                }
            }

            Entity entity = null;

            if (NodeType == NodeType.Grain)
            {
                entity = EntityManager.Get(id);
                if (entity == null)
                {
                    _Logger.LogError($"{id} Entry Entity Failed when {id} not found!");
                    return;
                }
            }
            else if (NodeType == NodeType.Cache)
            {
                if (!await CacheExist(id))
                {
                    _Logger.LogError($"{id} Entry Entity Failed when not CacheExist!");
                    return;
                }

                entity = await GetCacheEntity(id);
            }

            await CallbackEntity(id, EntityEvent.OnEntry, NList.Empty);

            await SyncEntity(id, NList.New().Add(id).Add((int)EntityEvent.OnEntry).Add(entity));
        }
Example #24
0
        public async Task ClearTable(Nuid id, string table_name)
        {
            Entity entity = EntityManager.Get(id);

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

                if (table.IsEmpty)
                {
                    return;
                }

                table.Clear();

                BatchCahceList.Add(NList.New().Add((int)CacheOption.ClearTable).Add(id).Add(table_name));
                await CallbackTable(id, table_name, TableEvent.Clear, NList.Empty);
            }
            else
            {
                if (id.Origin == Identity)
                {
                    if (await ClearCacheTable(id, table_name))
                    {
                        await CallbackTable(id, table_name, TableEvent.Clear, NList.Empty);
                    }
                }
                else
                {
                    INode node = GrainFactory.GetGrain <INode>(id.Origin);
                    await node.ClearTable(id, table_name);
                }
            }
        }
Example #25
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);

            NList msg = NList.New().Add(id).Add((int)entity_event).Append(args);
            await SyncManager.Callback(SyncType.Entity, this, id, msg);
        }
Example #26
0
        public async Task <NList> GetEntities()
        {
            NList list = NList.New();

            if (NodeType == NodeType.Grain)
            {
                IReadOnlyList <Entity> entities = EntityManager.GetEntities();
                foreach (Entity entity in entities)
                {
                    list.Add(entity);
                }
            }
            else if (NodeType == NodeType.Cache)
            {
                IReadOnlyList <Entity> entities = await GetCacheEntities();

                foreach (Entity entity in entities)
                {
                    list.Add(entity);
                }
            }

            return(list);
        }
Example #27
0
        private async Task <NList> GetCacheKeys(Nuid id, string table_name)
        {
            if (!await CacheExist(id))
            {
                return(NList.Empty);
            }

            IRedisDatabase db  = GetCache(id);
            string         key = CacheUtils.BuildTable(id, table_name);

            string type = await GetCacheType(id);

            EntityPrefab entity_prefab = Prefabs.GetEntity(type);

            if (entity_prefab == null)
            {
                return(NList.Empty);
            }

            TablePrefab table_prefab = entity_prefab.tables[table_name];

            if (table_prefab == null)
            {
                return(NList.Empty);
            }

            NList rows = NList.New();

            foreach (string hash_key in await db.HashKeysAsync(key))
            {
                switch (table_prefab.primary_key.type)
                {
                case VarType.Bool:
                {
                    bool row = bool.Parse(hash_key);
                    rows.Add(row);
                }
                break;

                case VarType.Int:
                {
                    int row = int.Parse(hash_key);
                    rows.Add(row);
                }
                break;

                case VarType.Long:
                {
                    long row = long.Parse(hash_key);
                    rows.Add(row);
                }
                break;

                case VarType.Nuid:
                {
                    Nuid row = Nuid.Parse(hash_key);
                    rows.Add(row);
                }
                break;

                case VarType.String:
                {
                    string row = hash_key;
                    rows.Add(row);
                }
                break;

                default:
                    break;
                }
            }

            return(rows.Count > 0 ? rows : NList.Empty);
        }
Example #28
0
        public async Task Execute()
        {
            while (_DelTimers.Count > 0)
            {
                int   serial = _DelTimers.Dequeue();
                Timer timer  = null;
                if (!_TimerDic.TryGetValue(serial, out timer))
                {
                    continue;
                }

                Tuple <Nuid, string> group = new Tuple <Nuid, string>(timer.Id, timer.Name);

                if (_ObjectDic.ContainsKey(group))
                {
                    _ObjectDic.Remove(group);
                }

                HashSet <int> timers = null;
                if (_TimerHeap.TryGetValue(timer.StopTicks, out timers))
                {
                    if (timers.Contains(serial))
                    {
                        timers.Remove(serial);
                    }
                }

                _TimerDic.Remove(serial);
            }

            while (_TimerHeap.Count > 0)
            {
                long now_ticks = TimeUtils.NowMilliseconds;

                if (_TimerHeap.ElementAt(0).Key > now_ticks)
                {
                    break;
                }

                foreach (int serial in _TimerHeap.Values.First())
                {
                    Timer timer = null;
                    if (!_TimerDic.TryGetValue(serial, out timer))
                    {
                        continue;
                    }

                    timer.Beat();

                    if (timer.RemainBeatCount == 0)
                    {
                        _DelTimers.Enqueue(timer.Serial);
                    }
                    else if (timer.RemainBeatCount > 0)
                    {
                        _AddTimers.Enqueue(timer.Serial);
                        timer.Reset();
                    }

                    NList args = NList.New();
                    args.Add(timer.Name);
                    args.Add(now_ticks);
                    args.Add(timer.RemainBeatCount);

                    await timer.Func.Invoke(_Node, timer.Id, args);
                }

                _TimerHeap.Remove(_TimerHeap.Keys.First());
            }

            while (_AddTimers.Count > 0)
            {
                int   serial = _AddTimers.Dequeue();
                Timer timer  = null;
                if (!_TimerDic.TryGetValue(serial, out timer))
                {
                    continue;
                }

                HashSet <int> timers = null;
                if (!_TimerHeap.TryGetValue(timer.StopTicks, out timers))
                {
                    timers = new HashSet <int>();
                    _TimerHeap.Add(timer.StopTicks, timers);
                }

                timers.Add(timer.Serial);
            }
        }
Example #29
0
        public async Task Destroy(Nuid id)
        {
            if (id.Origin != Identity)
            {
                INode node = GrainFactory.GetGrain <INode>(id.Origin);
                if (await node.IsActive())
                {
                    await node.Destroy(id);

                    return;
                }
            }

            if (NodeType == NodeType.Grain)
            {
                Entity entity = EntityManager.Get(id);
                if (entity == null)
                {
                    _Logger.LogError($"{id} Destroy Entity Failed When not found!");
                    return;
                }

                if (EntityManager.Remove(id))
                {
                    _Logger.LogError($"{id} Destroy Entity Failed When EntityManager Remove!");
                    return;
                }
            }
            else if (NodeType == NodeType.Cache)
            {
                if (!await CacheExist(id))
                {
                    _Logger.LogError($"{id} Destroy Entity Failed when not CacheExist!");
                    return;
                }

                string entity_type = await GetCacheType(id);

                EntityPrefab entity_prefab = Prefabs.GetEntity(entity_type);
                if (entity_prefab == null)
                {
                    _Logger.LogError($"{id} Destroy Entity Failed when not EntityPrefab {entity_type}!");
                    return;
                }

                int            db    = (int)(Identity % CacheUtils.EntityDBs);
                IRedisDatabase redis = _CacheClient.GetDb(db);

                ITransaction trans = redis.Database.CreateTransaction();

                foreach (TablePrefab table_prefab in entity_prefab.tables.Values)
                {
                    string table_key  = CacheUtils.BuildTable(id, table_prefab.name);
                    Task   table_task = trans.KeyDeleteAsync(table_key);
                }

                string field_key  = CacheUtils.BuildFields(id);
                Task   field_task = trans.KeyDeleteAsync(field_key);

                string entity_key  = CacheUtils.BuildEntities(id);
                Task   entity_task = trans.HashDeleteAsync(entity_key, id.Unique);

                bool result = await trans.ExecuteAsync();

                if (!result)
                {
                    _Logger.LogError($"{id} Destroy Entity Failed when ITransaction Execute");
                    return;
                }
            }

            await CallbackEntity(id, EntityEvent.OnDestroy, NList.Empty);

            await SyncEntity(id, NList.New().Add(id).Add((int)EntityEvent.OnDestroy));
        }
Example #30
0
        private async Task <IReadOnlyList <Entity> > LoadPersistEntities()
        {
            var database = _IMongoClient.GetDatabase(PersistUtils.ENTITY_DB);

            try
            {
                var collection = database.GetCollection <EntityList>(PersistUtils.ENTITIES);

                var filter = Builders <EntityList> .Filter.Eq(n => n.origin, Identity);

                IAsyncCursor <EntityList> res = await collection.FindAsync(filter);

                EntityList entity_list = await res.FirstOrDefaultAsync();

                if (entity_list == null)
                {
                    return(null);
                }

                List <Entity> entities = new List <Entity>();

                foreach (EntityChild entity_child in entity_list.entities)
                {
                    EntityPrefab entity_prefab = Prefabs.GetEntity(entity_child.type);
                    if (entity_prefab == null)
                    {
                        continue;
                    }

                    BsonDocument doc    = entity_child.entity;
                    long         unique = doc.GetValue(Global.MARK_UNIQUE).AsInt64;
                    Entity       entity = Entity.Gen(entity_child.type);
                    entity.Id = Nuid.New(unique, Identity);

                    foreach (FieldPrefab field_prefab in entity_prefab.fields.Values)
                    {
                        if (!field_prefab.save)
                        {
                            continue;
                        }

                        Field field = entity.GetField(field_prefab.name);
                        if (field == null)
                        {
                            continue;
                        }

                        BsonValue bsonValue = doc.GetValue(field_prefab.name);
                        switch (field_prefab.type)
                        {
                        case VarType.Bool:
                        {
                            bool value = bsonValue.AsBoolean;
                            field.TrySet(value, out _);
                        }
                        break;

                        case VarType.Int:
                        {
                            int value = bsonValue.AsInt32;
                            field.TrySet(value, out _);
                        }
                        break;

                        case VarType.Long:
                        {
                            long value = bsonValue.AsInt64;
                            field.TrySet(value, out _);
                        }
                        break;

                        case VarType.Float:
                        {
                            float value = (float)bsonValue.AsDouble;
                            field.TrySet(value, out _);
                        }
                        break;

                        case VarType.String:
                        {
                            string value = bsonValue.AsString;
                            field.TrySet(value, out _);
                        }
                        break;

                        case VarType.Nuid:
                        {
                            string value = bsonValue.AsBsonDocument.ToJson();
                            Nuid   nuid  = JsonUtils.ToObject <Nuid>(value);
                            field.TrySet(value, out _);
                        }
                        break;

                        case VarType.List:
                        {
                            string value = bsonValue.AsBsonDocument.ToJson();
                            NList  lst   = JsonUtils.ToObject <NList>(value);
                            field.TrySet(value, out _);
                        }
                        break;

                        default:
                            break;
                        }
                    }

                    foreach (TablePrefab table_prefab in entity_prefab.tables.Values)
                    {
                        if (!table_prefab.save)
                        {
                            continue;
                        }

                        BsonArray bsonarr = doc.GetValue(table_prefab.name) as BsonArray;
                        foreach (BsonDocument key_value_bson in bsonarr.Values)
                        {
                            BsonValue pk_bson = key_value_bson.GetValue(nameof(TablePrefab.primary_key));
                            switch (table_prefab.primary_key.type)
                            {
                            case VarType.Bool:
                            {
                                bool value = pk_bson.AsBoolean;
                                LoadTableKeyValue(value);
                            }
                            break;

                            case VarType.Int:
                            {
                                int value = pk_bson.AsInt32;
                                LoadTableKeyValue(value);
                            }
                            break;

                            case VarType.Long:
                            {
                                long value = pk_bson.AsInt64;
                                LoadTableKeyValue(value);
                            }
                            break;

                            case VarType.String:
                            {
                                string value = pk_bson.AsString;
                                LoadTableKeyValue(value);
                            }
                            break;

                            case VarType.Nuid:
                            {
                                string value = pk_bson.AsBsonDocument.ToJson();
                                Nuid   nuid  = JsonUtils.ToObject <Nuid>(value);
                                LoadTableKeyValue(nuid);
                            }
                            break;

                            default:
                                break;
                            }

                            void LoadTableKeyValue <TPrimaryKey>(TPrimaryKey primary_key)
                            {
                                Table <TPrimaryKey> table = entity.GetTable(table_prefab.name) as Table <TPrimaryKey>;

                                NList key_value = NList.New();

                                for (int col = 0; col < table_prefab.cols; col++)
                                {
                                    TablePrefab.ColumnPrefab column = table_prefab.columns[col];

                                    BsonValue col_bson = key_value_bson.GetValue(column.name);
                                    switch (column.type)
                                    {
                                    case VarType.Bool:
                                    {
                                        bool value = col_bson.AsBoolean;
                                        key_value.Add(value);
                                    }
                                    break;

                                    case VarType.Int:
                                    {
                                        int value = col_bson.AsInt32;
                                        key_value.Add(value);
                                    }
                                    break;

                                    case VarType.Long:
                                    {
                                        long value = col_bson.AsInt64;
                                        key_value.Add(value);
                                    }
                                    break;

                                    case VarType.Float:
                                    {
                                        float value = (float)col_bson.AsDouble;
                                        key_value.Add(value);
                                    }
                                    break;

                                    case VarType.String:
                                    {
                                        string value = col_bson.AsString;
                                        key_value.Add(value);
                                    }
                                    break;

                                    case VarType.Nuid:
                                    {
                                        string value = col_bson.AsBsonDocument.ToJson();
                                        Nuid   nuid  = JsonUtils.ToObject <Nuid>(value);
                                        key_value.Add(value);
                                    }
                                    break;

                                    case VarType.List:
                                    {
                                        string value = col_bson.AsBsonDocument.ToJson();
                                        NList  lst   = JsonUtils.ToObject <NList>(value);
                                        key_value.Add(value);
                                    }
                                    break;

                                    default:
                                        break;
                                    }
                                }

                                table.TrySetKeyValue(primary_key, key_value, out _);
                            }
                        }
                    }

                    entities.Add(entity);
                }

                return(entities);
            }
            catch (Exception ex)
            {
                _Logger.LogError(ex, "LoadPersisitEntities Failed!");
            }

            return(null);
        }