示例#1
0
        public override async Task CallAction(SignalRHub signalRHub, ApplicationUser appUser, string hubGroupId, HubPayload hubPayload)
        {
            switch (hubPayload.Method)
            {
            case "ResetGame":
                signalRHub.Logger.LogInformation($"MathRace ResetGame {appUser.UserName}");
                this.ResetGame();
                this.Status = $"{appUser.UserName} reset the game";
                await this.JoinGroup(signalRHub, appUser);

                await signalRHub.Clients.Group(this.HubGroupId).SendAsync("GameJson", this.GetGameJson());

                break;

            case "CheckAnswer":
                signalRHub.Logger.LogInformation($"MathRace CheckAnswer ({hubPayload.Param1} {appUser.UserName})");
                if (this.CheckAnswer(appUser, int.Parse(hubPayload.Param1)))
                {
                    await signalRHub.Clients.Caller.SendAsync("AnswerRight", hubPayload.Param1);

                    await signalRHub.Clients.Group(this.HubGroupId).SendAsync("GameJson", this.GetGameJson());
                }
                else
                {
                    await signalRHub.Clients.Caller.SendAsync("AnswerWrong", hubPayload.Param1);
                }
                break;

            case "AddPlayer":
                signalRHub.Logger.LogInformation($"MathRace AddPlayer");
                await this.JoinGroup(signalRHub, appUser);

                //await this.JoinGroup(_signalRHub);
                signalRHub.Logger.LogInformation($"AddPlayer ({appUser.UserName})");

                await signalRHub.Clients.Group(this.HubGroupId).SendAsync("GameJson", this.GetGameJson());

                break;

            case "RemovePlayer":
                signalRHub.Logger.LogInformation($"MathRace RemovePlayer");
                await this.UnjoinGroup(signalRHub, appUser);

                signalRHub.Logger.LogInformation($"RemovePlayer ({appUser.UserName})");

                await signalRHub.Clients.Group(this.HubGroupId).SendAsync("GameJson", this.GetGameJson());

                break;

            default:
                signalRHub.Logger.LogInformation($"MathRace UNKNOWN METHOD({hubPayload.Method})");
                break;
            }
        }
示例#2
0
 public abstract Task CallAction(SignalRHub signalRHub, ApplicationUser appUser, string hubGroupId, HubPayload hubPayload);
示例#3
0
        public async Task CallAction(SignalRHub signalRHub, ApplicationUser appUser, string hubGroupId, HubPayload hubPayload)
        {
            // If hubGroupId is blank, this is a global action (not group specific)
            if (string.IsNullOrWhiteSpace(hubGroupId))
            {
                switch (hubPayload.Method)
                {
                case "JoinGroup":
                    signalRHub.Logger.LogInformation($"HubGroupManager.JoinGroup ({hubPayload.Param1})");
                    HubGroup groupToJoin = this.HubGroups.Where(x => x.HubGroupId == hubPayload.Param1).FirstOrDefault();
                    if (groupToJoin != null)
                    {
                        await groupToJoin.JoinGroup(signalRHub, appUser);
                    }
                    break;

                case "UnjoinGroup":
                    signalRHub.Logger.LogInformation($"HubGroupManager.UnjoinGroup ({hubPayload.Param1})");
                    HubGroup groupToUnjoin = this.HubGroups.Where(x => x.HubGroupId == hubPayload.Param1).FirstOrDefault();
                    if (groupToUnjoin != null)
                    {
                        await groupToUnjoin.UnjoinGroup(signalRHub, appUser);
                    }
                    break;

                case "GetHubGroups":
                    signalRHub.Logger.LogInformation($"HubGroupManager.GetHubGroups ({hubPayload.Param1})");
                    await signalRHub.Clients.Caller.SendAsync("HubGroups", this.GetHubGroupsJson());

                    break;
                }
            }
            else // Handle group (chatroom or game) specific action
            {
                foreach (HubGroup group in this.HubGroups)
                {
                    if (hubGroupId == group.HubGroupId)
                    {
                        await group.CallAction(signalRHub, appUser, hubGroupId, hubPayload);
                    }
                }
            }
        }