Ejemplo n.º 1
0
        public async Task ManageWebSocketRequestAsync(HttpContext context)
        {
            var newController = new ClientConnectionController();

            newController.MessageReceived += (Object source, EventArgs rawArgs) =>
            {
                var args = (IClientConnectionController.MessageReceivedArgs)rawArgs;
                clientMessages.Enqueue(args.Message);
            };

            clientControllers.Add(newController);

            // socket needs to be initialized before we can continue
            newController.InitializeAsync(context).Wait();

            // user has to ask for token with "getToken" or "reclaim", if he still has one;
            // in former case, server sends token;
            // in latter case, server sends "accept" (if user exists) or "token" (if user expired)

            await newController.HandleAsync(context);

            clientControllers.Remove(newController);

            // find user that has left but keep him in case that reclaim is demanded
            var leftUser = users.Find(user => user.Token == newController.Token);

            leftUser.Online = false;

            sendService.SendTo(from clientController in clientControllers select clientController.Socket, new ServerMessage {
                Type = Message.MessageType.Meta, SubType = "quit", Text = leftUser.Name
            });
        }
Ejemplo n.º 2
0
        public void HandleMessage(List <User> users, List <IClientConnectionController> clientConnectionControllers, IUserPermissionService userPermissionService, ClientMessage message)
        {
            if (userPermissionService.IsMessageFromNamedUser(users, message))
            {
                var existingUser = users.FirstOrDefault(user => user.Token == message.Token);

                var otherClientsAtSameTableOrGlobal = clientConnectionControllers.Where(client =>
                                                                                        users.Exists(user => client.Token == user.Token &&
                                                                                                     user.TableID == existingUser.TableID && user.Token != existingUser.Token));

                var serverMessage = new ServerMessage(message, existingUser.Name);

                sendService.SendTo(from otherClient in otherClientsAtSameTableOrGlobal select otherClient.Socket, serverMessage);
            }
        }
Ejemplo n.º 3
0
        private void handleCreateTableSubType(List <User> users, List <IClientConnectionController> clientConnectionControllers, List <IGameController> gameControllers, IUserPermissionService userPermissionService, ClientMessage message)
        {
            if (!userPermissionService.IsMessageFromNamedUser(users, message))
            {
                // drop?
                return;
            }

            var numbers    = Enumerable.Range(1, 10000); // limit of 10000 tables but should never be an issue
            var newTableID = numbers.FirstOrDefault(number => !gameControllers.Exists(gameController => gameController.TableID == number));

            if (newTableID != 0)
            {
                try
                {
                    var     parts = message.Text.Split(",");
                    string  name = "", password = null;
                    bool    hidden  = false;
                    RuleSet ruleSet = new RuleSet();

                    if (parts.Count() > 0)
                    {
                        name = parts[0];
                        if (parts.Count() > 1)
                        {
                            password = parts[1];
                            if (parts.Count() > 2)
                            {
                                hidden = parts[2] == "true";

                                if (parts.Count() > 3)
                                {
                                    // split all parts to retrieve rules
                                    for (int partID = 3; partID < parts.Count(); ++partID)
                                    {
                                        var keyValue = parts[partID].Split(":");
                                        if (keyValue.Count() != 2)
                                        {
                                            continue;
                                        }

                                        switch (keyValue[0])
                                        {
                                        case "useNines": ruleSet.UseNines = keyValue[1] == "true"; break;

                                        case "withArmut": ruleSet.WithArmut = keyValue[1] == "true"; break;

                                        case "withFleischlos": ruleSet.WithFleischlos = keyValue[1] == "true"; break;

                                        case "countingReContra": ruleSet.CountReContraBy = keyValue[1] == "+2" ? RuleSet.ReContraCounting.ADDING_TWO : RuleSet.ReContraCounting.DOUBLING; break;

                                        case "secondDulleTrumpsFirst": ruleSet.SecondDulleTrumpsFirst = keyValue[1] == "true"; break;

                                        case "bothPigletsTrumpAll": ruleSet.BothPigletsTrumpAll = keyValue[1] == "true"; break;

                                        case "reContraAtHochzeitAfterFinderTrick": ruleSet.ReContraAtHochzeitAfterFinderTrick = keyValue[1] == "true"; break;

                                        case "withReshufflingAtFiveKings": ruleSet.WithReshufflingAtFiveKings = keyValue[1] == "true"; break;

                                        case "withReshufflingAtEightyPoints": ruleSet.WithReshufflingAtEightyPoints = keyValue[1] == "true"; break;

                                        case "soloPlayerFirstToAct": ruleSet.SoloPlayerFirstToAct = keyValue[1] == "true"; break;

                                        case "numberOfNestedBuckRounds":
                                            if (int.TryParse(keyValue[1], out int numberOfNestedBuckRounds))
                                            {
                                                ruleSet.NumberOfNestedBuckRounds = numberOfNestedBuckRounds;
                                            }
                                            break;

                                        case "addBuckRoundAtLostSolo": ruleSet.AddBuckRoundAtLostSolo = keyValue[1] == "true"; break;

                                        case "addBuckRoundAtFullHeartTrick": ruleSet.AddBuckRoundAtFullHeartTrick = keyValue[1] == "true"; break;

                                        case "addBuckRoundAtLostContra": ruleSet.AddBuckRoundAtLostContra = keyValue[1] == "true"; break;

                                        case "addBuckRoundAtZeroGame": ruleSet.AddBuckRoundAtZeroGame = keyValue[1] == "true"; break;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    var founder = users.FirstOrDefault(user => user.Token == message.Token);

                    gameControllers.Add(new GameController(sendService, users, clientConnectionControllers, newTableID, name, password, hidden, founder, ruleSet));

                    founder.TableID = newTableID;

                    sendService.SendTo(clientConnectionControllers.Select(c => c.Socket), new ServerMessage
                    {
                        Type    = Message.MessageType.Meta,
                        SubType = "tablesUpdated"
                    });
                }
                catch (JsonException)
                {
                    // drop?
                }
            }
        }