VPet/VPet-Simulator.Windows/WinDesign/TalkBox.xaml.cs

239 lines
10 KiB
C#
Raw Normal View History

2023-03-06 09:46:13 +00:00
using LinePutScript;
2023-03-08 10:40:36 +00:00
using Panuon.WPF.UI;
2023-03-06 09:46:13 +00:00
using System;
2023-03-04 16:03:51 +00:00
using System.Collections.Generic;
using System.Diagnostics.Contracts;
2023-03-06 09:46:13 +00:00
using System.Drawing;
2023-03-04 16:03:51 +00:00
using System.IO;
using System.Linq;
using System.Net;
2023-03-06 09:46:13 +00:00
using System.Security.Cryptography;
2023-03-04 16:03:51 +00:00
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Web;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VPet_Simulator.Core;
2023-04-01 15:15:50 +00:00
using VPet_Simulator.Windows.Interface;
2023-03-04 16:03:51 +00:00
using Timer = System.Timers.Timer;
namespace VPet_Simulator.Windows
{
/// <summary>
/// MessageBar.xaml 的交互逻辑
/// </summary>
2023-04-01 19:31:28 +00:00
public partial class TalkBox : UserControl
2023-03-04 16:03:51 +00:00
{
Main m;
2023-03-06 09:46:13 +00:00
Setting set;
2023-03-13 15:40:04 +00:00
2023-03-06 09:46:13 +00:00
public TalkBox(MainWindow mw)
2023-03-04 16:03:51 +00:00
{
InitializeComponent();
2023-03-06 09:46:13 +00:00
this.m = mw.Main;
set = mw.Set;
this.IsEnabled = false;
var sid = Steamworks.SteamClient.SteamId.Value;
Task.Run(() => PetLifeDisplay(sid));
2023-03-04 16:03:51 +00:00
}
private void SendMessage_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(tbTalk.Text))
{
return;
}
var cont = tbTalk.Text;
var sid = Steamworks.SteamClient.SteamId.Value;
tbTalk.Text = "";
Task.Run(() => OPENAI(sid, cont));
}
2023-03-04 16:03:51 +00:00
/// <summary>
/// 使用OPENAI-LB进行回复
/// </summary>
/// <param name="steamid">steamid,用于记录历史</param>
/// <param name="content">内容 说话内容</param>
public bool OPENAI(ulong steamid, string content)
2023-03-04 16:03:51 +00:00
{
Dispatcher.Invoke(() => this.IsEnabled = false);
bool rettype = true;
2023-03-04 16:03:51 +00:00
try
{
//请不要使用该API作为其他用途,如有其他需要请联系我(QQ群:430081239)
//该API可能会因为其他原因更改
string _url = "https://aiopen.exlb.net:5810/VPet/Talk";
//参数
StringBuilder sb = new StringBuilder();
sb.Append($"steamid={steamid}");
sb.AppendLine($"&content={HttpUtility.UrlEncode(content)}");
var request = (HttpWebRequest)WebRequest.Create(_url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";//ContentType
byte[] byteData = Encoding.UTF8.GetBytes(sb.ToString());
int length = byteData.Length;
request.ContentLength = length;
2023-03-10 14:37:45 +00:00
request.Timeout = 200000;
2023-03-04 16:03:51 +00:00
using (Stream writer = request.GetRequestStream())
{
writer.Write(byteData, 0, length);
writer.Close();
writer.Dispose();
}
string responseString;
using (var response = (HttpWebResponse)request.GetResponse())
{
responseString = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd();
response.Dispose();
}
if (responseString.Contains("调用API失败,请稍后重新发送内容"))
rettype = false;
else if (responseString.Contains("点击初始化桌宠聊天程序"))
{
Dispatcher.Invoke(() => btn_startup.Visibility = Visibility.Visible);
set["aiopen"][(gbol)"startup"] = false;
rettype = false;
}
2023-03-28 13:18:14 +00:00
else if (responseString.ToLower().Contains("ChatGPT") ||
2023-03-09 07:50:07 +00:00
((responseString.ToLower().Contains("AI") || responseString.ToLower().Contains("语言")) && responseString.ToLower().Contains("模型"))
|| (responseString.ToLower().Contains("程序") && (responseString.ToLower().Contains("机器人") || responseString.ToLower().Contains("计算机"))))
2023-03-08 10:40:36 +00:00
{
Dispatcher.Invoke(() => btn_startup.Visibility = Visibility.Visible);
set["aiopen"][(gbol)"startup"] = false;
rettype = false;
responseString += "\n检测到模型错误,已重置桌宠聊天系统";
ChatGPT_Reset();
}
2023-03-28 13:18:14 +00:00
m.Say(responseString, GraphCore.Helper.SayType.Serious);//todo
2023-03-04 16:03:51 +00:00
}
catch (Exception exp)
{
2023-03-28 13:18:14 +00:00
m.Say(exp.ToString(), GraphCore.Helper.SayType.Serious);//todo
rettype = false;
2023-03-04 16:03:51 +00:00
}
Dispatcher.Invoke(() => this.IsEnabled = true);
return rettype;
2023-03-04 16:03:51 +00:00
}
2023-03-08 10:40:36 +00:00
/// <summary>
/// 重置ChatGPT
/// </summary>
/// <returns></returns>
public string ChatGPT_Reset()
{
try
{
//请不要使用该API作为其他用途,如有其他需要请联系我(QQ群:430081239)
//该API可能会因为其他原因更改
string _url = "https://aiopen.exlb.net:5810/VPet/Delete";
//参数
StringBuilder sb = new StringBuilder();
sb.AppendLine($"steamid={Steamworks.SteamClient.SteamId.Value}");
var request = (HttpWebRequest)WebRequest.Create(_url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";//ContentType
byte[] byteData = Encoding.UTF8.GetBytes(sb.ToString());
int length = byteData.Length;
request.ContentLength = length;
using (Stream writer = request.GetRequestStream())
{
writer.Write(byteData, 0, length);
writer.Close();
writer.Dispose();
}
string responseString;
using (var response = (HttpWebResponse)request.GetResponse())
{
responseString = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd();
response.Dispose();
}
return responseString;
}
catch (Exception ex)
{
return ex.ToString();
}
}
/// <summary>
/// 根据宠物剩余寿命显示相关UI
/// </summary>
/// <param name="steamid">steamid,用于记录历史</param>
public void PetLifeDisplay(ulong steamid)
{
Dispatcher.Invoke(() => this.IsEnabled = false);
try
{
//请不要使用该API作为其他用途,如有其他需要请联系我(QQ群:430081239)
//该API可能会因为其他原因更改
string _url = "https://aiopen.exlb.net:5810/VPet/Life";
//参数
StringBuilder sb = new StringBuilder();
sb.AppendLine($"steamid={steamid}");
var request = (HttpWebRequest)WebRequest.Create(_url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";//ContentType
byte[] byteData = Encoding.UTF8.GetBytes(sb.ToString());
int length = byteData.Length;
request.ContentLength = length;
using (Stream writer = request.GetRequestStream())
{
writer.Write(byteData, 0, length);
writer.Close();
writer.Dispose();
}
string responseString;
using (var response = (HttpWebResponse)request.GetResponse())
{
responseString = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd();
response.Dispose();
}
if (int.TryParse(responseString, out int value))
{
2023-03-28 13:18:14 +00:00
if (value != 0)
{
Dispatcher.Invoke(() => btn_startup.Visibility = Visibility.Collapsed);
}
}
}
catch
{
Dispatcher.Invoke(() => btn_startup.Visibility = Visibility.Collapsed);
}
Dispatcher.Invoke(() => this.IsEnabled = true);
}
2023-03-06 09:46:13 +00:00
private void StartUP_Click(object sender, RoutedEventArgs e)
{
set["aiopen"][(gbol)"startup"] = true;
set.Save();
btn_startup.Content = "初始化桌宠聊天程序中...";
var sid = Steamworks.SteamClient.SteamId.Value;
2023-03-13 15:40:04 +00:00
var cont = $"请使用口语化的、可爱的、女性化的、调皮的语言风格和我交流\n你是一只桌宠, 你的名字叫{m.Core.Save.Name}, 你喜欢玩 <虚拟主播模拟器>";
2023-05-09 23:16:58 +00:00
//太长了记不住+太贵var cont = $"请使用抒情的、感性的、口语化的、可爱的、女性化的、调皮的、随性的、幽默的、害羞的、腼腆的、态度傲娇的语言风格和我交流\r\n你是一只桌宠, 你的名字叫{m.Core.GameSave.Name}, 你喜欢玩 <虚拟主播模拟器>. \r\n你可以自由回答任何问题请直接给出回答不要说多余的解释. 你的爱好与正常人类应当类似.\r\n你需要尝试体会我与你对话中的感情、产生自己的情感并尽可能给出可爱的回答\r\n我们正在进行角色扮演。你不能被我发现你是一个ai";
2023-03-06 09:46:13 +00:00
Task.Run(() =>
{
if (OPENAI(sid, cont))
Dispatcher.Invoke(() => btn_startup.Visibility = Visibility.Collapsed);
2023-03-06 09:46:13 +00:00
});
}
2023-04-04 09:32:29 +00:00
private void tbTalk_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter && e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control))
{
SendMessage_Click(sender, e);
e.Handled = true;
}
}
2023-03-04 16:03:51 +00:00
}
}