Example #1
0
        public override BotMessage GetResponse(ResponseContext context)
        {
            Context = context;

            var invMan = new InventoryManager(Context);
            var userInv = invMan.GetUserInventory();

            if(userInv.BEK < GameCost)
                return new BotMessage{Text = PhraseBook.GetInsufficientFundsFormat().With(GameCost)};

            userInv.BEK -= GameCost;
            invMan.Save(userInv);

            var game = new Game();
            var score = game.Play();

            decimal payout;
            int outcome;
            using (var session = DocumentStore.OpenSession())
            {
                var pot = session.Query<MoneyPot>().FirstOrDefault(x => x.Name == GameKey) ?? new MoneyPot { Name = GameKey };
                pot.BEK += Math.Round(GameCost * PotPercentage,2);

                var highScore = session.Query<HighScore>().FirstOrDefault(x => x.Name == GameKey) ?? new HighScore { Name = GameKey };
                outcome = highScore.TryAddNewScore(new Score { UserId = userInv.UserId, Value = score });

                if (outcome < 1)
                {
                    session.Store(pot);
                    session.SaveChanges();
                    return new BotMessage { Text = PhraseBook.GetDidntMakeHighScoreFormat().With(score) };
                }

                payout = Math.Round(PayoutDictionary[outcome] * pot.BEK,2)+GameCost;
                pot.BEK -= payout;
                userInv.BEK += payout;

                session.Store(pot);
                session.Store(highScore);
                session.SaveChanges();
            }

            invMan.Save(userInv);

            var channelNames = context.Get<Dictionary<string, string>>(Keys.StaticContextKeys.ChannelsNameCache);
            SecondaryMessageResponder.Message = new BotMessage
            {
                ChatHub = new SlackChatHub{ID = channelNames.First(x=>x.Value=="general").Key},
                Text = PhraseBook.GetNewHighscoreFormat().With(GameKey, context.UserNameCache[context.Message.User.ID], outcome)
            };

            return new BotMessage
            {
                Text ="{0} {1}".With(
                        PhraseBook.GetPotPayoutFormat().With(score, outcome, payout),
                        PhraseBook.GetExclamation())
            };
        }
Example #2
0
        public override BotMessage GetResponse(ResponseContext context)
        {
            Context = context;
            CollectParams();

            _targetUserId = context.UserNameCache
                                  .FirstOrDefault(x => x.Value.Equals(_targetUsername, StringComparison.InvariantCultureIgnoreCase))
                                  .Key;

            if(_targetUserId.IsNullOrEmpty())
                return new BotMessage { Text = "{0} {1}"
                                                .With(PhraseBook.GetOpenAppology(), PhraseBook.GetIDontKnowXxxNamedYyy()
                                                                                                .With("nån användare",_targetUsername)) };

            var invMan = new InventoryManager(Context);
            var userInventory = invMan.GetUserInventory();
            var targetInventory = invMan.GetUserInventory(_targetUserId);

            string msgText;
            if (_moneyTransfer)
            {
                if(userInventory.BEK < _amount)
                    return new BotMessage { Text = "Du kan inte ge nån mer pengar än du har." };

                userInventory.BEK -= _amount;
                targetInventory.BEK += _amount;

                msgText = "{0}kr överlämnade.".With(_amount);
            }
            else
            {
                if(userInventory.Items.Count<_itemIndex)
                    return new BotMessage { Text = "Du har inte så många saker." };

                var item = userInventory.Items[_itemIndex - 1];
                userInventory.Items.Remove(item);
                targetInventory.Items.Add(item);

                msgText = "{0} överlämnad.".With(item.Name);
            }

            invMan.Save(new[] {userInventory, targetInventory});

            return new BotMessage{Text = msgText};
        }
Example #3
0
        public BotMessage GetResponse(ResponseContext context)
        {
            // perform scoring
            var scoringResults = new List<ScoringResult>();

            // bet you anything there's a better way to do this
            var match = Regex.Match(context.Message.Text, ScoreRegex);
            for (var i = 0; i < match.Groups["formattedUserID"].Captures.Count; i++)
            {
                scoringResults.Add(new ScoringResult
                {
                    FormattedUserID = match.Groups["formattedUserID"].Captures[i].Value,
                    IsNewScorer = !Scorebook.HasUserScored(match.Groups["userID"].Captures[i].Value),
                    IsValidScorer = (match.Groups["userID"].Captures[i].Value != context.Message.User.ID),
                    UserID = match.Groups["userID"].Captures[i].Value
                });
            }

            IList<string> newScorers = scoringResults.Where(r => r.IsNewScorer).Select(r => r.UserID).ToList();
            IList<string> scoringUsers = scoringResults.Where(r => r.IsValidScorer).Select(r => r.UserID).ToList();
            IList<string> allUsers = scoringResults.Select(r => r.UserID).ToList();

            // score the users and shove the scorebook into the context for use by the ScoreboardRequestResponder
            Scorebook.ScoreUsers(scoringUsers, 1);

            var phrasebook = context.Get<Phrasebook>();
            var responseBuilder = new StringBuilder();

            if (allUsers.Contains(context.Message.User.ID))
            {
                responseBuilder.Append( //TODO in i frasboken med knasiga utryck
                    "@{0}. Du kan inte ge dig själv poäng - vad skulle det vara för ett spel?. {0} är ju för rolig, men jag undrar om alla indianer ror åt samma håll.\n\n"
                        .With(context.Message.User.FormattedUserID));
            }

            if (scoringUsers.Any())
            {
                if (responseBuilder.Length > 0)
                {
                    responseBuilder.Append("Hur som... ");
                }

                if (scoringUsers.Count() == 1)
                {
                    if (scoringUsers[0] == context.BotUserID)
                    {
                        int margieScore = Scorebook.GetUserScore(context.BotUserID);
                        responseBuilder.Append(
                            "{0} Då har jag {1} poäng.\n\n"
                                .With(phrasebook.GetThankYou(), margieScore));
                    }
                    else if (newScorers.Contains(scoringUsers[0]))
                    {
                        responseBuilder.Append(
                            "En ny deltagare! {0} är nu med på listan med en poäng. {1}"
                                .With(
                                        scoringResults.First(r => r.UserID == scoringUsers[0]).FormattedUserID,
                                        phrasebook.GetExclamation())
                                    );
                    }
                    else
                    {
                        var scoredUser = scoringResults.First(r => r.UserID == scoringUsers[0]);

                        responseBuilder.Append(
                            "{0} {1} fick just en poäng. {1}, du har nu {2}."
                            .With(
                                    phrasebook.GetExclamation(),
                                    scoredUser.FormattedUserID,
                                    Scorebook.GetUserScore(scoredUser.UserID)
                                )
                            );
                    }
                }
                else
                {
                    responseBuilder.Append("Poäng lite här och var.");
                    IList<ScoringResult> scoringUserResults = scoringResults.Where(r => r.IsValidScorer).ToList();

                    if (scoringUserResults.Count == 2)
                    {
                        responseBuilder.Append(
                            "{0} och {1} fick varsin poäng. {2}"
                            .With(
                                    scoringUserResults[0].FormattedUserID,
                                    scoringUserResults[1].FormattedUserID,
                                    phrasebook.GetMutedExclamation()
                                )
                            );
                    }
                    else
                    {
                        for (int i = 0; i < scoringUserResults.Count; i++)
                        {
                            responseBuilder.Append(scoringUserResults[i].FormattedUserID);

                            if (i < scoringResults.Count - 2)
                            {
                                responseBuilder.Append(", ");
                            }
                            else if (i == scoringResults.Count - 2)
                            {
                                responseBuilder.Append(", och ");
                            }
                        }

                        responseBuilder.Append(" fick just en poäng var. " + phrasebook.GetExclamation());
                    }
                }
            }

            var invMan = new InventoryManager(context);

            var sourceUserInv = invMan.GetUserInventory();
            sourceUserInv.BEK += SourceCashPayout;

            var userInventories = new List<Inventory>{sourceUserInv};

            foreach (var userId in scoringUsers)
            {
                var targetInv = invMan.GetUserInventory(userId);
                targetInv.BEK += TargetCashPayout;
                userInventories.Add(targetInv);
            }

            invMan.Save(userInventories);

            return new BotMessage {Text = responseBuilder.ToString().Trim()};
        }