Beispiel #1
0
        public void Join(Entities.GameClient client, Packets.TeamActionPacket packet)
        {
            if (Members.Count >= 5)
            {
                return;
            }

            if (Leader == client.EntityUID && packet.Action == Enums.TeamAction.AcceptJoin)
            {
                // accept
                Entities.GameClient newMember;
                if (Core.Kernel.Clients.TrySelect(NextJoin, out newMember))
                {
                    if (Members.TryAdd(newMember.EntityUID, newMember))
                    {
                        newMember.Team = this;
                        // send team member ...
                        using (var teammember = new Packets.TeamMemberPacket())
                        {
                            teammember.Name          = newMember.Name;
                            teammember.EntityUID     = newMember.EntityUID;
                            teammember.Mesh          = newMember.Mesh;
                            teammember.MaxHealth     = (ushort)newMember.MaxHP;
                            teammember.CurrentHealth = (ushort)newMember.HP;
                            foreach (Entities.GameClient member in Members.Values)
                            {
                                if (member.EntityUID != newMember.EntityUID)
                                {
                                    member.Send(teammember);
                                    using (var teammember2 = new Packets.TeamMemberPacket())
                                    {
                                        teammember2.Name          = member.Name;
                                        teammember2.EntityUID     = member.EntityUID;
                                        teammember2.Mesh          = member.Mesh;
                                        teammember2.MaxHealth     = (ushort)member.MaxHP;
                                        teammember2.CurrentHealth = (ushort)member.HP;
                                        newMember.Send(teammember2);
                                    }
                                }
                            }
                            newMember.Send(teammember);
                        }

                        packet.EntityUID = client.EntityUID;
                        newMember.Send(packet);
                    }
                }
            }
            else if (packet.EntityUID == Leader && packet.Action == Enums.TeamAction.RequestJoin)
            {
                Entities.GameClient LeaderClient;
                if (Members.TryGetValue(Leader, out LeaderClient))
                {
                    NextJoin         = client.EntityUID;
                    packet.EntityUID = client.EntityUID;
                    LeaderClient.Send(packet);
                }
            }
        }
        protected async Task Init(CancellationToken stoppingToken)
        {
            var leader = _cluster.Leader;

            // block until leader is elected
            while (leader == null && !stoppingToken.IsCancellationRequested)
            {
                leader = _cluster.Leader;
                await Task.Delay(1000, stoppingToken);
            }

            if (stoppingToken.IsCancellationRequested)
            {
                return;
            }

            // if we are leader, there's nothing to do
            if (leader != _appConfig.HostName)
            {
                using var channel = GrpcChannel.ForAddress($"http://{leader}:5000");
                var leaderClient = new LeaderClient(channel);

                using var streamingCall = leaderClient.CatchUp(new CatchUpRequest
                {
                    Offset = _storage.LatestOffset
                }, cancellationToken: stoppingToken);

                while (await streamingCall.ResponseStream.MoveNext(stoppingToken))
                {
                    var catchUpData = streamingCall.ResponseStream.Current;
                    _storage.Add(catchUpData.Offset, catchUpData.Message.ToByteArray());
                }

                _storage.Drain();
            }
        }