コード例 #1
0
ファイル: Client.cs プロジェクト: golf1052/godbot
        public async Task Receive()
        {
            while (!webSocket.CloseStatus.HasValue)
            {
                MemoryStream stream    = new MemoryStream();
                StreamReader reader    = new StreamReader(stream);
                bool         endOfData = false;
                while (!endOfData)
                {
                    byte[] buf = new byte[8192];
                    ArraySegment <byte>    buffer   = new ArraySegment <byte>(buf);
                    WebSocketReceiveResult response = await webSocket.ReceiveAsync(buffer, CancellationToken.None);

                    stream.Write(buffer.Array, buffer.Offset, buffer.Count);
                    endOfData = response.EndOfMessage;
                }
                stream.Seek(0, SeekOrigin.Begin);
                while (!reader.EndOfStream)
                {
                    string  read = reader.ReadLine();
                    JObject o    = JObject.Parse(read);
                    if (o["type"] != null)
                    {
                        string messageType = (string)o["type"];
                        if (messageType == "message")
                        {
                            SlackMessageEventArgs newMessage = new SlackMessageEventArgs();
                            newMessage.Message = o;
                            MessageReceived(this, newMessage);
                        }
                    }
                }
            }
        }
コード例 #2
0
ファイル: Client.cs プロジェクト: golf1052/godbot
        private async void Client_MessageReceived(object sender, SlackMessageEventArgs e)
        {
            string text    = (string)e.Message["text"];
            string channel = (string)e.Message["channel"];

            if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(channel))
            {
                // subtype or message changed
            }
            if (debug && channel == ChannelId)
            {
                if (text.ToLower() == "godbot start game")
                {
                    if (currentGame == null)
                    {
                        string user = (string)e.Message["user"];
                        if (user == golf1052UserId)
                        {
                            currentGame = new SlackGameManager(user, golf1052Dm, user, golf1052Dm, debug, ChannelId);
                            await SendSlackMessage("Do you have a die that you can roll? (Answer yes or no)", channel);
                        }
                        else
                        {
                            await SendSlackMessage("Only Sanders can start games right now.", channel);
                        }
                    }
                    else
                    {
                        await SendSlackMessage("Cannot start game. Game already in progress.", channel);
                    }
                    return;
                }
            }
            if (text.ToLower() == "godbot ping")
            {
                await SendSlackMessage("pong", channel);
            }
            else if (text.ToLower() == "godbot help")
            {
                await SendSlackMessage("not implemented yet", channel);
            }
            else if (text.ToLower().StartsWith("gotbot"))
            {
                await SendSlackMessage("My name is godbot.", channel);
            }
            else if (text.ToLower() == "godbot progress")
            {
                if (currentGame != null)
                {
                    await SendSlackMessage("Game in progress.", channel);
                }
                else
                {
                    await SendSlackMessage("No game in progress.", channel);
                }
            }
            else if (text.ToLower() == "godbot stop game")
            {
                if (currentGame != null)
                {
                    currentGame = null;
                    await SendSlackMessage("Ended game", channel);
                }
            }
            else if (currentGame != null)
            {
                if (currentGame.WaitingForDieResponse)
                {
                    if (text.ToLower() == "yes")
                    {
                        currentGame.WaitingForDieResponse = false;
                        currentGame.PlayersHaveDie        = true;
                    }
                    else if (text.ToLower() == "no")
                    {
                        currentGame.WaitingForDieResponse = false;
                        currentGame.PlayersHaveDie        = false;
                    }
                    await SendGameInstructions(currentGame.StartYear());
                }
                else if (currentGame.WaitingForDieRollResponse)
                {
                    await SendGameInstructions(currentGame.ProcessDieRoll(text));
                }
                else if (currentGame.WaitingForRoundResponse)
                {
                    var i = currentGame.ContinueRound(text.ToUpper());
                    if (!currentGame.GameIsOver)
                    {
                        await SendGameInstructions(i);
                    }

                    if (currentGame.GameIsOver)
                    {
                        currentGame = null;
                    }
                }
            }
        }