Jelajahi Sumber

加入對 Proposition 狀態的早期支援

0x0001F36D 5 tahun lalu
induk
melakukan
b6157e0d88

+ 7 - 0
src/Yuuna.Common/Utilities/RandomHelper.cs

@@ -7,6 +7,7 @@ namespace Yuuna.Common.Utilities
     using System.Collections.Concurrent;
     using System.Collections.Generic;
     using System.Linq;
+    using Yuuna.Common.Linq;
 
     public static class RandomHelper
     {
@@ -56,5 +57,11 @@ namespace Yuuna.Common.Utilities
                 return list[newIndex];
             }
         }
+
+
+        public static T RandomTakeOne<T>(this IImmutable<T> collection)
+        {
+            return collection.ToImmutable().RandomTakeOne();
+        }
     }
 }

+ 29 - 20
src/Yuuna.Contracts.Test/Fake.cs

@@ -15,9 +15,14 @@ namespace Yuuna.Contracts.Test
     public sealed class Fake : ModuleBase
     {
         private volatile bool _doorState;
+        private volatile bool _lightState;
         public Fake()
         {
             this._doorState = new[] { true, false, true, false }.RandomTakeOne();
+            Debug.WriteLine("門的狀態是: " + (this._doorState ? "開著的" : "關著的"));
+
+            this._lightState = new[] { true, false, true, false }.RandomTakeOne();
+            Debug.WriteLine("燈的狀態是: " + (this._lightState ? "開著的" : "關著的"));
         }
 
         protected override void BuildPatterns(IGroupManager g, IPatternBuilder p)
@@ -65,27 +70,31 @@ namespace Yuuna.Contracts.Test
                     .RandomTakeOne();
             }).OnIncomplete(s => throw new NotImplementedException());
 
-            //p.Build(g["open"], g["light"]).OnInvoke(score =>
-            //{
-            //    var LIGHT = new { IS_OPENED = new[] { true, false, true, false }.RandomTakeOne() };
-            //    Console.WriteLine("燈的狀態是: " + (LIGHT.IS_OPENED ? "開著的" : "關著的"));
-            //    // 開燈
-            //    if (!LIGHT.IS_OPENED)
-            //        return (Moods.Happy, "已經開好燈囉 <3");
-            //    else
-            //        return (Moods.Sad, "可是燈本來就是開的欸 QAQ");
-            //}).OnIncomplete(s => throw new NotImplementedException());
+            p.Build(g["open"], g["light"]).OnInvoke(score =>
+            {
+                Debug.WriteLine("燈的狀態是: " + (this._lightState ? "開著的" : "關著的"));
+                // 開燈
+                if (!this._lightState)
+                {
+                    this._lightState = !this._lightState;
+                    return (Moods.Happy, "已經開好燈囉 <3");
+                }
+                else
+                    return (Moods.Sad, "可是燈本來就是開的欸 QAQ");
+            }).OnIncomplete(s => throw new NotImplementedException());
 
-            //p.Build(g["close"], g["light"]).OnInvoke(score =>
-            //{
-            //    var LIGHT = new { IS_OPENED = new[] { true, false, true, false }.RandomTakeOne() };
-            //    Console.WriteLine("燈的狀態是: " + (LIGHT.IS_OPENED ? "開著的" : "關著的"));
-            //    // 開燈
-            //    if (!LIGHT.IS_OPENED)
-            //        return (Moods.Happy, "已經關好燈囉 <3");
-            //    else
-            //        return (Moods.Sad, "可是燈本來就是關的欸 QAQ");
-            //}).OnIncomplete(s => throw new NotImplementedException());
+            p.Build(g["close"], g["light"]).OnInvoke(score =>
+            { 
+                Debug.WriteLine("燈的狀態是: " + (this._lightState ? "開著的" : "關著的"));
+                // 開燈 
+                if (this._lightState)
+                {
+                    this._lightState = !this._lightState;
+                    return (Moods.Happy, "已經關好燈囉 <3");
+                }
+                else
+                    return (Moods.Sad, "可是燈本來就是關的欸 QAQ");
+            }).OnIncomplete(s => throw new NotImplementedException());
 
         }
     }

+ 3 - 3
src/Yuuna.Contracts/Optimization/DefaultStrategy.cs

@@ -25,15 +25,15 @@ namespace Yuuna.Contracts.Optimization
         /// </summary>
         Optimal,
         /// <summary>
-        /// 條件。表示缺少一個群組(Group)。
+        /// 條件。表示缺少一個群組(Group)。
         /// </summary>
         Condition,
         /// <summary>
-        /// 命題。表示具有兩個近似解(<see cref="Condition"/>)。
+        /// 命題。表示具有兩個條件式(<see cref="Condition"/>)。
         /// </summary>
         Proposition,
         /// <summary>
-        /// 悖論。表示具有多個最佳解(<see cref="Optimal"/>),這實際上屬於模組設計上的問題。
+        /// 悖論。表示具有兩個(含)以上的最佳解(<see cref="Optimal"/>),這實際上屬於模組設計上的問題。
         /// </summary>
         Paradox,
 

+ 69 - 16
src/Yuuna/EntryPoint.cs

@@ -146,22 +146,19 @@ namespace Yuuna
                         single.Pattern.Owner.Patterns.TryGet(single.Pattern, out var bag);
                         var r = bag.Invoke(single);
                         interactor.OnReceive(r);
+                        break;
                     }
-                    break;
 
                 case Status.Condition:
                     {
                         var sb = new StringBuilder();
-                        sb.Append("你是想要");
-                        foreach (var matched in alternative.Matches[0].SequentialMatches)
-                        {
-                            sb.Append(matched.ToImmutable().RandomTakeOne());
-                        }
-                        foreach (var missing in alternative.Matches[0].Missing)
+                        sb.Append("你是想要"); 
+
+                        foreach (var g in alternative.Matches[0].Pattern.ToImmutable())
                         {
-                            foreach (var unmatch in missing.ToImmutable())
-                                sb.Append(unmatch.ToImmutable().RandomTakeOne());
+                            sb.Append(g.RandomTakeOne().RandomTakeOne());
                         }
+
                         sb.Append("嗎?");
                         interactor.OnReceive(sb.ToString());
 
@@ -180,21 +177,77 @@ namespace Yuuna
                         }
 
                         interactor.OnReceive(canResponses.RandomTakeOne());
+                        Leave:
+                        break;
                     }
-                    Leave: 
-                    break;
 
                 case Status.Proposition:
-                    Console.WriteLine("proposition");
-                    break;
+                    {
+                        var sb = new StringBuilder(); 
+                        var current = alternative.Matches[0];
+
+                        sb.Append("你是想要");
+
+                        foreach (var g in current.Pattern.ToImmutable())
+                        {
+                            sb.Append(g.RandomTakeOne().RandomTakeOne());
+                        }
+
+                        sb.Append("嗎?");
+                        interactor.OnReceive(sb.ToString());
+
+                        text = interactor.Accept();
+
+                        foreach (var antonym in antonyms)
+                        {
+                            if (antonym.Judge(text, out var type) && type.Equals(Antonym.TypeKinds.Positive))
+                            {
+                                current.Pattern.Owner.Patterns.TryGet(current.Pattern, out var bag);
+                                var r = bag.Invoke(current);
+                                interactor.OnReceive(r);
+                                goto Leave;
+                            }
+                        }
+
+                        sb.Clear();
+
+
+                        current = alternative.Matches[1];
+                        sb.Append("還是");
+                        sb.Append("你是想"); 
+
+                        foreach (var g in current.Pattern.ToImmutable())
+                        {
+                            sb.Append(g.RandomTakeOne().RandomTakeOne());
+                        }
+
+                        sb.Append("?");
+                        interactor.OnReceive(sb.ToString());
+
+                        text = interactor.Accept();
+
+                        foreach (var antonym in antonyms)
+                        {
+                            if (antonym.Judge(text, out var type) && type.Equals(Antonym.TypeKinds.Positive))
+                            { 
+                                current.Pattern.Owner.Patterns.TryGet(current.Pattern, out var bag);
+                                var r = bag.Invoke(current);
+                                interactor.OnReceive(r);
+                                goto Leave;
+                            }
+                        }
+
+                        interactor.OnReceive(canResponses.RandomTakeOne()); 
+
+                        Leave:
+                        break;
+                    }
 
                 case Status.Paradox:
-                    Console.WriteLine("paradox");
                     break;
 
                 case Status.NoModuleInstalled:
-                    Console.WriteLine("noModuleInstalled");
-                    Console.WriteLine("無效的模組");
+                    interactor.OnReceive((Moods.Sad, "無效的模組"));
                     break;
 
                 default: