public async Task <PrintMessage> ProcessMessage(NewMessage message)
        {
            // Check Global Commands
            switch (message.text.ToLower().Trim())
            {
            case "wiki-bot: leaders":
            case "wiki-bot leaders":
            case "wiki-bot: leaderboard":
            case "wiki-bot leaderboard":
            case "wiki-bot leader board":
            case "wiki-bot: leader board":
                var leaderboardString = _leaderBoardGenerator.GenerateLeaderBoardString();
                if (string.IsNullOrEmpty(leaderboardString))
                {
                    return(new PrintMessage {
                        IsReply = false, MessageText = "No players are playering; Cannot generate leaderboard"
                    });
                }
                else
                {
                    return(new PrintMessage {
                        IsReply = false, MessageText = leaderboardString
                    });
                }
            }

            // Check if a new game has started
            GameStartData gameStartData = GetGameStartData(message);

            if (gameStartData.IsValid)
            {
                Console.WriteLine("New Game Started");
                _gameReaderWriter.CreateNewGame(gameStartData);
                return(new PrintMessage {
                    IsReply = true, MessageText = $"New Game Started! To end game, type \n> wiki-bot: end game", ThreadTs = message.ts
                });
            }
            else
            {
                var game = _gameReaderWriter.GetGame(message);

                if (game != null)
                {
                    switch (message.text.ToLower().Trim())
                    {
                    case "wiki-bot stats":
                    case "wiki-bot: stats":
                        var stats = _gameReaderWriter.GetGameStatistics(game.Id);
                        return(PrintStats(stats, game.Id));

                    case "wiki-bot end":
                    case "wiki-bot end game":
                    case "wiki-bot endgame":
                    case "wiki-bot: end":
                    case "wiki-bot: end game":
                    case "wiki-bot: endgame":
                        return(_gameReaderWriter.EndGame(game.Id));

                    default:
                        break;
                    }

                    var pathValidationOutput = await FindAndProcessPath(game, message);

                    // Only process valid messages as determined by validator output
                    if (pathValidationOutput != null && pathValidationOutput.IsValid)
                    {
                        var responce = _gameReaderWriter.AddGameEntry(new GameEntry
                        {
                            LinkCount = pathValidationOutput.PathLength,
                            User      = message.user,
                            RawText   = message.text,
                            UserName  = message.username,
                            GameId    = game.Id
                        });

                        if (responce == LoaderResponse.Success)
                        {
                            return(new PrintMessage {
                                IsReply = true, MessageText = $"{message.username}'s Entry Received! Number of clicks: {pathValidationOutput.PathLength}", ThreadTs = message.thread_ts
                            });
                        }
                        else
                        {
                            return(new PrintMessage {
                                IsReply = true, MessageText = $"You have already played this round. You only get one chance per game", ThreadTs = message.thread_ts
                            });
                        }
                    }
                    else
                    {
                        if (pathValidationOutput != null)
                        {
                            return(new PrintMessage {
                                IsReply = true, MessageText = pathValidationOutput.ValidationMessage, ThreadTs = message.thread_ts
                            });
                        }
                    }
                }
            }
            return(null);
        }
Esempio n. 2
0
        public async Task Connect()
        {
            Console.WriteLine("Initializing Client");
            string token = Environment.GetEnvironmentVariable("WIKI_BOT_USER_OATH_TOKEN");

            if (string.IsNullOrWhiteSpace(token))
            {
                throw new ArgumentNullException();
            }
            _client = new SlackSocketClient(token);

            MessageProcessor processor = new MessageProcessor(_gameReaderWriter);

            Console.WriteLine("RTM Client Connecting...");
            ManualResetEventSlim clientReady = new ManualResetEventSlim(false);

            _gameReaderWriter.TestDatabaseConnection();

            _client.Connect((connected) => {
                // This is called once the client has emitted the RTM start command
                clientReady.Set();
            }, () => {
                // This is called once the RTM client has connected to the end point
            });
            _client.OnMessageReceived += async(message) =>
            {
                // Ignore Bots
                if (message.user != null)
                {
                    // Overwrite username with poster's real name
                    User poster = new User();
                    _client.UserLookup.TryGetValue(message.user, out poster);
                    Console.WriteLine(poster.real_name);
                    message.username = poster.real_name;

                    var res = await processor.ProcessMessage(message);

                    if (res != null)
                    {
                        var chan = _client.Channels.Find(x => x.name.Equals("wiki-game-test-chan"));
                        if (res.ThreadTs.HasValue)
                        {
                            var thread_ts = res.ThreadTs.Value.ToProperTimeStamp();
                            _client.PostMessage(x => Console.WriteLine(res), chan.id, res.MessageText, thread_ts: thread_ts);
                        }
                        else
                        {
                            _client.PostMessage(x => Console.WriteLine(res), chan.id, res.MessageText);
                        }
                    }
                }
            };

            clientReady.Wait();

            // Send heartbeat
            var c = _client.Channels.Find(x => x.name.Equals("wiki-game-test-chan"));

            _client.PostMessage(x => Console.WriteLine(x.error), c.id, "Hello! Enter in two wikipedia links to get started!\n" +
                                "Afterwards, reply to that post in the formal \"Starting Page -> Click 1 -> Click 2 -> ... -> Ending Page");

            // Generate Leadboard
            var leaderboardString = _leaderBoardGenerator.GenerateLeaderBoardString();

            if (string.IsNullOrEmpty(leaderboardString) == false)
            {
                _client.PostMessage(x => Console.WriteLine(x.error), c.id, $"{leaderboardString}\nType \"wiki-bot: leaderboard\" to bring back the leader board at any time.");
            }

            while (true)
            {
                try
                {
                    await _gameAutomation.RunAutomatedTasksAsync();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
            ;
        }