213 lines
7.4 KiB
C#
213 lines
7.4 KiB
C#
using Activity.Entitys;
|
||
using Activity.Properties;
|
||
using Api.Framework;
|
||
using Api.Framework.SDK;
|
||
using Api.Framework.Tools;
|
||
using Chat.Framework.WXSdk;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Activity
|
||
{
|
||
public class Class1 : Plugin
|
||
{
|
||
public Class1()
|
||
{
|
||
this.Logo = Resources.活动;
|
||
this.Name = Resources.PluginName;
|
||
this.Note = Resources.PluginNote;
|
||
}
|
||
|
||
#region 定义的变量
|
||
|
||
public static Config Config; // 插件配置
|
||
private MainForm mainForm = null; //主窗体
|
||
|
||
#endregion
|
||
|
||
|
||
|
||
public override void ShowForm()
|
||
{
|
||
if (mainForm == null || mainForm.IsDisposed)
|
||
{
|
||
mainForm = new MainForm();
|
||
mainForm.Show();
|
||
}
|
||
mainForm.Activate();
|
||
}
|
||
|
||
public override void Start()
|
||
{
|
||
var session = ApiClient.GetSession();
|
||
|
||
#region 判断表是否存在,不存在创建表
|
||
if (!session.TableExist<fl_plugin_activity_info>()) session.CreateTable<fl_plugin_activity_info>();
|
||
#endregion
|
||
|
||
Config = this.ReadConfig<Config>();
|
||
SDK.ReciveIMEvent += SDK_ReciveIMEvent;
|
||
}
|
||
|
||
public override void Stop()
|
||
{
|
||
try
|
||
{
|
||
if (mainForm != null)
|
||
{
|
||
mainForm.CloseForm();
|
||
mainForm = null;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
this.OnLog(ex.Message);
|
||
}
|
||
}
|
||
public enum GameType : int
|
||
{
|
||
幸运楼层 = 1
|
||
}
|
||
public class Game
|
||
{
|
||
public string Groupid { get; set; }
|
||
public GameType GameType { get; set; }
|
||
public List<string> Members{ get; set; }
|
||
/// <summary>
|
||
/// 打乱顺序
|
||
/// </summary>
|
||
public List<string> MembersOver { get; set; }
|
||
public bool Gameover { get; set; }
|
||
public Game()
|
||
{
|
||
this.Groupid = string.Empty;
|
||
this.Members = new List<string>();
|
||
}
|
||
|
||
public int GetIndex(string username)
|
||
{
|
||
for (int i = 0; i < Members.Count; i++)
|
||
{
|
||
if (Members[i].ToString() == username) return i;
|
||
}
|
||
return -1;
|
||
}
|
||
}
|
||
private Dictionary<string, Game> Games = new Dictionary<string, Game>();
|
||
|
||
|
||
private void SDK_ReciveIMEvent(object sender, ReciveIMEvent e)
|
||
{
|
||
var client = e.Sender as Chat.Framework.WXSdk.Implement.WXClientImpl_IPAD;
|
||
try
|
||
{
|
||
if (string.IsNullOrEmpty(e.Groupid)) return;
|
||
|
||
|
||
if (e.Message == "开始游戏")
|
||
{
|
||
|
||
var group = client.GetContact(e.Groupid);
|
||
if (group.ChatRoomOwner == e.Username)
|
||
{
|
||
if (Games.ContainsKey(e.Groupid))
|
||
{
|
||
if (!Games[e.Groupid].Gameover) throw new Exception("当前游戏尚未结束,请耐心等待!");
|
||
}
|
||
var game = new Game() { Groupid = e.Groupid, GameType = GameType.幸运楼层, Members = new List<string>() };
|
||
Games[e.Groupid] = game;
|
||
game.Members.Clear();
|
||
e.SendMessage(@"😎游戏开始:
|
||
①邀请【1】人进群可参与
|
||
②回复任意数字加入游戏
|
||
③耐心等待几分钟,系统开奖
|
||
|
||
PS小提示:
|
||
商城地址:http://t.cn/EiuwmKd
|
||
每轮抽取3位幸运小伙伴,商城内任选9.9元以内的宝贝!(包邮免费送)");
|
||
}
|
||
}
|
||
else if (e.Message == "公布结果")
|
||
{
|
||
var group = client.GetContact(e.Groupid);
|
||
if (group.ChatRoomOwner == e.Username)
|
||
{
|
||
if (Games.ContainsKey(e.Groupid))
|
||
{
|
||
var game = Games[e.Groupid];
|
||
if (game.MembersOver == null)
|
||
{
|
||
game.MembersOver = RandomSortList(game.Members);
|
||
}
|
||
game.Gameover = true;
|
||
StringBuilder sb = new StringBuilder("游戏结果如下:\r\n");
|
||
int number = 0;
|
||
foreach (string item in game.MembersOver)
|
||
{
|
||
number++;
|
||
var member = client.GetMember(e.Groupid,item);
|
||
if (member == null) continue;
|
||
sb.Append($"{member.GetName()}(在{game.GetIndex(item) + 1}楼)\r\n");
|
||
if (number >= 3) break;
|
||
}
|
||
|
||
sb.Append("-----------------\r\nPS小提示:\r\n商城地址:http://t.cn/EiuwmKd\r\n请再商城内认选一个9.9元的宝贝,联系萌萌代付!");
|
||
e.SendMessage(sb.ToString());
|
||
|
||
|
||
}
|
||
else throw new Exception("游戏已结束!");
|
||
|
||
}
|
||
}
|
||
else
|
||
{
|
||
var reg = Regex.Match(e.Message, @"^\d+$");
|
||
if (reg.Success)
|
||
{
|
||
if (Games.ContainsKey(e.Groupid) && !Games[e.Groupid].Gameover)
|
||
{
|
||
if (!Games[e.Groupid].Members.Contains(e.Username))
|
||
{
|
||
var count = client.GetMembers(e.Groupid).Where(f => f.InviterUserName == e.Username).ToList().Count;
|
||
if (count >= 1)
|
||
{
|
||
Games[e.Groupid].Members.Add(e.Username);
|
||
client.SendMessage(e.Groupid, $"@{e.NickName}\r\n加入成功!您是第{Games[e.Groupid].Members.Count}位玩家!", e.Username);
|
||
}
|
||
else
|
||
{
|
||
throw new Exception($"需邀请{1}人进群才可以加入!\r\n查看数据请输入:邀请统计");
|
||
}
|
||
|
||
return;
|
||
}
|
||
else throw new Exception("您已加入,无需重复加入!");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
client.SendMessage(e.Groupid,"@"+e.NickName+":\r\n"+ex.Message,e.Username);
|
||
}
|
||
}
|
||
|
||
public List<T> RandomSortList<T>(List<T> ListT)
|
||
{
|
||
Random random = new Random();
|
||
List<T> newList = new List<T>();
|
||
foreach (T item in ListT)
|
||
{
|
||
newList.Insert(random.Next(newList.Count + 1), item);
|
||
}
|
||
return newList;
|
||
}
|
||
|
||
}
|
||
}
|