<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RMUD.Commands
{
	internal class Say : CommandFactory
	{
		public override void Create(CommandParser Parser)
		{
			Parser.AddCommand(
				new Sequence(
					new Or(
						new KeyWord("SAY", false),
						new KeyWord("'", false)),
                    new FailIfNoMatches(
					    new Rest("SPEECH"),
                        "Say what?")),
				new SayProcessor(SayProcessor.EmoteTypes.Speech),
				"Say something.");

            Parser.AddCommand(
                new GenericMatcher((pm, context) =>
                    {
                        var r = new List<PossibleMatch>();
                        if (pm.Next == null || pm.Next.Value.Length <= 1 || pm.Next.Value[0] != '\'')
                            return r;

                        pm.Next.Value = pm.Next.Value.Substring(1); //remove the leading '
                        r.Add(pm.EndWith("SPEECH", pm.Next));
                        return r;
                    }, "' [TEXT]"),
                new SayProcessor(SayProcessor.EmoteTypes.Speech),
                "Say something.")
                .Name("SAY");

			Parser.AddCommand(
				new Sequence(
					new Or(
						new KeyWord("EMOTE", false),
						new KeyWord("\"", false)),
                    new FailIfNoMatches(
					    new Rest("SPEECH"),
                        "You exist. Actually this is an error message, but that's what you just told me to say.")),
				new SayProcessor(SayProcessor.EmoteTypes.Emote),
				"Emote something.");
		}
	}

	internal class SayProcessor : CommandProcessor
	{
		public enum EmoteTypes
		{
			Speech,
			Emote
		}

		public EmoteTypes EmoteType;

		public SayProcessor(EmoteTypes EmoteType)
		{
			this.EmoteType = EmoteType;
		}

		public void Perform(PossibleMatch Match, Actor Actor)
		{
            var speechBuilder = new StringBuilder();

            speechBuilder.Append(Actor.Short);
            if (EmoteType == EmoteTypes.Speech)
                speechBuilder.Append(": \"");
            else
                speechBuilder.Append(" ");

            speechBuilder.Append(Match.Arguments["SPEECH"].ToString());

			if (EmoteType == EmoteTypes.Speech)
				speechBuilder.Append("\"");

			Mud.SendLocaleMessage(Actor, speechBuilder.ToString());
		}

        
	}
}<commit_msg>Ported say and emote commands<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RMUD.Commands
{
	internal class Say : CommandFactory, DeclaresRules
	{
		public override void Create(CommandParser Parser)
		{
			Parser.AddCommand(
                Or(
                    Sequence(
                        Or(
                            KeyWord("SAY"),
                            KeyWord("'")),
                        MustMatch("Say what?", Rest("SPEECH"))),
                    Generic((pm, context) =>
                    {
                        var r = new List<PossibleMatch>();
                        if (pm.Next == null || pm.Next.Value.Length <= 1 || pm.Next.Value[0] != '\'')
                            return r;

                        pm.Next.Value = pm.Next.Value.Substring(1); //remove the leading '

                        var builder = new StringBuilder();
                        var node = pm.Next;
                        for (; node != null; node = node.Next)
                        {
                            builder.Append(node.Value);
                            builder.Append(" ");
                        }

                        builder.Remove(builder.Length - 1, 1);
                        r.Add(pm.EndWith("SPEECH", builder.ToString()));
                        return r;
                    }, "'[TEXT => SPEECH]")),
                "Say something.")
                .Manual("Speak within your locale.")
                .Perform("speak", "ACTOR", "ACTOR", "SPEECH");


            Parser.AddCommand(
                Sequence(
                    Or(
                        KeyWord("EMOTE"),
                        KeyWord("\"")),
                    MustMatch("You exist. Actually this is an error message, but that's what you just told me to say.", Rest("SPEECH"))),
                "Emote something.")
                .Manual("Perform an action, visible within your locale.")
                .Perform("emote", "ACTOR", "ACTOR", "SPEECH");
		}

        public void InitializeGlobalRules()
        {
            GlobalRules.DeclarePerformRuleBook<MudObject, String>("speak", "[Actor, Text] : Handle the actor speaking the text.");

            GlobalRules.Perform<MudObject, String>("speak")
                .Do((actor, text) =>
                {
                    Mud.SendLocaleMessage(actor, "^<the0> : \"" + text + "\"", actor);
                    return PerformResult.Continue;
                })
                .Name("Default motormouth rule.");

            GlobalRules.DeclarePerformRuleBook<MudObject, String>("emote", "[Actor, Text] : Handle the actor emoting the text.");

            GlobalRules.Perform<MudObject, String>("emote")
                .Do((actor, text) =>
                {
                    Mud.SendLocaleMessage(actor, "^<the0> " + text, actor);
                    return PerformResult.Continue;
                })
                .Name("Default exhibitionist rule.");
        }
    }
}
{'fraction_non_alphanumeric': 0.11997767857142858, 'fraction_numerical': 0.0018601190476190475, 'mean_word_length': 1.471047794117647, 'pattern_counts': {'":': 1, '<': 13, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6497640', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1360, 'n_words': 318}