------ #### V1.0.2404.1201 - 新增支持手动运行规则。 - 规则播放时间间隔不再针对全局声效,而只针对当前规则声效。 - 修复规则中播放文件夹可能导致无法执行的BUG。 - 修复规则不勾选礼物和点赞,则无法执行的BUG。
271 lines
10 KiB
C#
271 lines
10 KiB
C#
using CommunityToolkit.Mvvm.Messaging;
|
|
using HandyControl.Controls;
|
|
using LiveTools.Data;
|
|
using LiveTools.RuleItem;
|
|
using Microsoft.Win32;
|
|
using Newtonsoft.Json.Linq;
|
|
using ryCommon;
|
|
using ryCommonDb;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
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.Shapes;
|
|
|
|
namespace LiveTools
|
|
{
|
|
/// <summary>
|
|
/// FrmAddRule.xaml 的交互逻辑
|
|
/// </summary>
|
|
public partial class FrmAddRule : HandyControl.Controls.Window
|
|
{
|
|
public FrmAddRule()
|
|
{
|
|
InitializeComponent();
|
|
PnlRules.Width = ScrRules.Width;
|
|
//CbbGifts.Items.Clear();
|
|
//CbbGifts.ItemsSource = list_gift;
|
|
//list_gift.Add(new GiftItem2() { Text = "1111" });
|
|
if (System.IO.Directory.Exists(Config.AllUserDbFolder + "\\GiftImages\\Auto"))
|
|
{
|
|
var files = System.IO.Directory.GetFiles(Config.AllUserDbFolder + "\\GiftImages\\Auto","*.png");
|
|
for (int i = 0; i < files.Length; i++)
|
|
{
|
|
GiftItem item = new GiftItem();
|
|
//CbbGifts.DataContext = list_gift;
|
|
item.Text = "礼物" + (i + 1);
|
|
item.ImageFileName = System.IO.Path.GetFileName(files[i]);
|
|
item.Image = new BitmapImage(new Uri(files[i]));
|
|
CbbGifts.Items.Add(item);
|
|
}
|
|
}
|
|
//list_gift.Add(new GiftItem() { Text = "222" });
|
|
WeakReferenceMessenger.Default.Register<RuleMsg>(this, OnReceive);
|
|
}
|
|
private ObservableCollection<GiftItem2> list_gift { get; set; } = new ObservableCollection<GiftItem2>();
|
|
private void OnReceive(object recipient, RuleMsg message)
|
|
{
|
|
if (message.Message == "Close")
|
|
{
|
|
for (int i = 0; i < PnlRules.Children.Count; i++)
|
|
{
|
|
if (PnlRules.Children[i] is UserControl ctl)
|
|
{
|
|
if (ctl is IRule rule)
|
|
{
|
|
if (rule.ID == message.ID)
|
|
{
|
|
PnlRules.Children.Remove(ctl);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public int SelectedId { get; set; } = 0;
|
|
public string PicPath { get; set; } = "";
|
|
public void GetInfo()
|
|
{
|
|
IDbInterface db = new SQLiteDataProvider();
|
|
db.ConnDb(LiveTools.Config.DbFullPath);
|
|
var ds = db.ReadData("select * from Rules where id=" + SelectedId);
|
|
if (ds.HaveData())
|
|
{
|
|
var row = ds.GetRow(0);
|
|
TxtRuleName.Text = row["RuleName"].ToString();
|
|
NumSortIndex.Value = row["SortIndex"].ToDouble();
|
|
JObject jo = JObject.Parse(row["RuleJson"].ToString() ?? "");
|
|
JArray jarr = jo.GetJsonValue("Content", new JArray());
|
|
TagContent.Items.Clear();
|
|
for (int i = 0; i < jarr.Count; i++)
|
|
{
|
|
TagContent.Items.Add(new Tag() { Content = jarr[i].ToString() });
|
|
}
|
|
var GiftTriggerList = "|"+jo.GetJsonValue("GiftTriggerList","")+"|";//触发的礼物列表
|
|
CbbGifts.SelectedItems.Clear();
|
|
for (int i = 0; i < CbbGifts.Items.Count; i++)
|
|
{
|
|
var gift= (GiftItem)CbbGifts.Items[i];
|
|
if(GiftTriggerList.IndexOfEx("|"+ gift.ImageFileName+"|")>=0)
|
|
{
|
|
CbbGifts.SelectedItems.Add(gift);
|
|
}
|
|
}
|
|
ChkGiftTrigger.IsChecked = jo.GetJsonValue("GiftTrigger", false);
|
|
ChkDianzan.IsChecked = jo.GetJsonValue("DianzanTrigger", false);
|
|
PnlRules.Children.Clear();
|
|
var action = jo.GetJsonValue("ActionList", new JArray());
|
|
if (action != null)
|
|
{
|
|
for (int i = 0; i < action.Count; i++)
|
|
{
|
|
var action_item = (JObject)action[i];
|
|
var lll = "LiveTools." + action_item.GetJsonValue("Name", "");
|
|
Type type = Type.GetType("LiveTools." + action_item.GetJsonValue("Name", "")); // 获取类型对象
|
|
if (type != null)
|
|
{
|
|
object instance = Activator.CreateInstance(type);
|
|
if (instance != null && instance is UserControl ctl)
|
|
{
|
|
if (ctl is IRule rule)
|
|
{
|
|
ctl.Margin = new Thickness(1, 0, 1, 2);
|
|
rule.ID = index;
|
|
rule.LoadSetting(action_item);
|
|
PnlRules.Children.Add(ctl);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ds?.Dispose();
|
|
db.Free();
|
|
}
|
|
private void BtnOK_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if (TxtRuleName.Text.Length == 0)
|
|
{
|
|
HandyControl.Controls.MessageBox.Show("请输入规则名称。", "提示");
|
|
return;
|
|
}
|
|
for (int i = 0; i < PnlRules.Children.Count; i++)
|
|
{
|
|
var item = PnlRules.Children[i];
|
|
if (item != null && item is UserControl ctl)
|
|
{
|
|
if (ctl is IRule rule)
|
|
{
|
|
if (!rule.CheckVerification())
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
IDbInterface db = new SQLiteDataProvider();
|
|
db.ConnDb(Config.DbFullPath);
|
|
ryCommonDb.RyQuickSQL mySQL = new ryCommonDb.RyQuickSQL("Rules");
|
|
mySQL.AddField("RuleName", TxtRuleName.Text);
|
|
JArray jarr = new();
|
|
for (int i = 0; i < TagContent.Items.Count; i++)
|
|
{
|
|
if (TagContent.Items[i] is Tag tag)
|
|
jarr.Add(tag.Content.ToString());
|
|
}
|
|
JArray jarr_actions = new();
|
|
for (int i = 0; i < PnlRules.Children.Count; i++)
|
|
{
|
|
var item = PnlRules.Children[i];
|
|
if (item != null && item is UserControl ctl)
|
|
{
|
|
if (ctl is IRule rule)
|
|
{
|
|
jarr_actions.Add(rule.SettingJson());
|
|
}
|
|
}
|
|
}
|
|
var GiftTriggerList = "";//触发的礼物列表
|
|
for (int i = 0; i < CbbGifts.SelectedItems.Count; i++)
|
|
{
|
|
var gift = (GiftItem)CbbGifts.SelectedItems[i];
|
|
GiftTriggerList += gift.ImageFileName+"|";
|
|
}
|
|
JObject jo = new()
|
|
{
|
|
{ "Content", jarr},
|
|
{ "GiftTrigger", ChkGiftTrigger.IsChecked ?? false },
|
|
{ "GiftTriggerList", GiftTriggerList.Trim('|')},
|
|
{ "DianzanTrigger", ChkDianzan.IsChecked ?? false },
|
|
{ "ActionList", jarr_actions },
|
|
};
|
|
mySQL.AddField("RuleJson", jo.ToString());
|
|
mySQL.AddField("SortIndex", NumSortIndex.Value);
|
|
mySQL.AddField("EditTime", DateTime.Now.ToInt64());
|
|
if (SelectedId == -1 || db.Update(mySQL, "id=" + SelectedId) == 0)
|
|
{
|
|
mySQL.AddField("AddTime", DateTime.Now.ToInt64());
|
|
db.Insert(mySQL);
|
|
}
|
|
db.Free();
|
|
DialogResult = true;
|
|
}
|
|
|
|
private void BtnCancel_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
DialogResult = false;
|
|
}
|
|
|
|
private void BtnAddContent_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
TagContent.Items.Add(new Tag() { Content = TxtContent.Text });
|
|
TxtContent.Text = "";
|
|
}
|
|
int index = 0;
|
|
private void BtnShowPic_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
AddRuleItem(new Rule_ShowPic());
|
|
}
|
|
private void AddRuleItem(UserControl ctl)
|
|
{
|
|
index++;
|
|
var item = ctl;
|
|
item.Margin = new Thickness(1, 0, 1, 2);
|
|
if (ctl is IRule rule1)
|
|
{
|
|
rule1.ID = index;
|
|
}
|
|
PnlRules.Children.Add(item);
|
|
}
|
|
private void BtnSoundPlay_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
AddRuleItem(new Rule_SoundPlay());
|
|
}
|
|
|
|
private void BtnVariable_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
AddRuleItem(new Rule_Variable());
|
|
}
|
|
class GiftItem2
|
|
{
|
|
public string Text { get; set; } = "";
|
|
public string ImagePath { get; set; } = "";
|
|
}
|
|
|
|
private void ChkGiftTrigger_Checked(object sender, RoutedEventArgs e)
|
|
{
|
|
if (CbbGifts == null) { return; }
|
|
CbbGifts.IsEnabled = ChkGiftTrigger.IsChecked == true;
|
|
}
|
|
|
|
private void ChkGiftTrigger_Unchecked(object sender, RoutedEventArgs e)
|
|
{
|
|
if (CbbGifts == null) { return; }
|
|
CbbGifts.IsEnabled = ChkGiftTrigger.IsChecked == true;
|
|
}
|
|
|
|
private void TxtContent_KeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.Key == Key.Enter)
|
|
{
|
|
BtnAddContent_Click(BtnAddContent, new RoutedEventArgs());
|
|
}
|
|
}
|
|
|
|
private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
|
|
{
|
|
Growl.Info("右边不选择,则表示全部礼物都触发,否则表示指定礼物触发。");
|
|
}
|
|
}
|
|
}
|