Beispiel #1
0
        private static void HandleNewJoke(SendRequest request)
        {
            TocTocJoke joke = AsyncListener.PendingJokes
                              .Where(j => j.UserId == request.UserId)
                              .FirstOrDefault();

            if (joke != null)
            {
                SendCommand.HandleJokeError(request.UserId, joke);
                return;
            }

            // Create joke for the request user:
            joke        = JokeProvider.GetNew();
            joke.UserId = request.UserId;

            // Add a joke message into the container of pending messages.
            // In this case, the next time a receive request is sent by this user,
            // It'll return the joke message.
            Message jokeMsg = new Message()
            {
                Data = joke.ServerInteractions[joke.ServerInteractionIndex],
                DestinationUserId = request.UserId
            };

            AsyncListener.PendingMessages.Add(jokeMsg);

            // Increment the server interaction index, so the next
            // interaction is called.
            joke.ServerInteractionIndex++;

            // Add this joke to the container of pending jokes:
            AsyncListener.PendingJokes.Add(joke);
        }
Beispiel #2
0
        private static void HandlePreviousJoke(SendRequest request)
        {
            TocTocJoke joke = AsyncListener.PendingJokes
                              .Where(j => j.UserId == request.UserId)
                              .FirstOrDefault();

            if (joke != null)
            {
                // If the interaction was correct:
                if (joke.ClientInteractions[joke.ClientInteractionIndex]
                    .Equals(request.Data, StringComparison.OrdinalIgnoreCase) == true)
                {
                    // Increments interaction from both client and server, and adds
                    // the new joke interaction to the pending messages container.
                    joke.ClientInteractionIndex++;

                    Message jokeMsg = new Message()
                    {
                        Data = joke.ServerInteractions[joke.ServerInteractionIndex],
                        DestinationUserId = request.UserId
                    };

                    joke.ServerInteractionIndex++;

                    AsyncListener.PendingMessages.Add(jokeMsg);

                    // If the joke is over, remove it from the bunch:
                    if (joke.ServerInteractionIndex >= joke.ServerInteractions.Length)
                    {
                        AsyncListener.PendingJokes.Remove(joke);
                    }
                }
                else
                {
                    SendCommand.HandleJokeError(request.UserId, joke);
                }
            }
        }