wsmud_Trigger

武神传说 MUD

当前为 2019-03-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name wsmud_Trigger
  3. // @namespace cqv3
  4. // @version 0.0.12
  5. // @date 03/03/2019
  6. // @modified 04/03/2019
  7. // @homepage https://greasyfork.org/zh-CN/scripts/378984
  8. // @description 武神传说 MUD
  9. // @author Bob.cn
  10. // @match http://game.wsmud.com/*
  11. // @match http://www.wsmud.com/*
  12. // @run-at document-end
  13. // @require https://cdn.staticfile.org/vue/2.2.2/vue.min.js
  14. // @grant unsafeWindow
  15. // @grant GM_getValue
  16. // @grant GM_setValue
  17. // @grant GM_deleteValue
  18. // @grant GM_listValues
  19. // @grant GM_setClipboard
  20. // ==/UserScript==
  21.  
  22. (function () {
  23. 'use strict';
  24.  
  25. function CopyObject(obj) {
  26. return JSON.parse(JSON.stringify(obj));
  27. }
  28.  
  29. /***********************************************************************************\
  30. Notification Center
  31. \***********************************************************************************/
  32.  
  33. class Notification {
  34. constructor(name, params) {
  35. this.name = name;
  36. this.params = params;
  37. }
  38. }
  39.  
  40. class NotificationObserver {
  41. constructor(targetName, action) {
  42. this.targetName = targetName;
  43. this.action = action;
  44. }
  45. }
  46.  
  47. const NotificationCenter = {
  48. observe: function(notificationName, action) {
  49. const index = this._getOberverIndex();
  50. const observer = new NotificationObserver(notificationName, action);
  51. this._observers[index] = observer;
  52. return index;
  53. },
  54. removeOberver: function(index) {
  55. delete this._observers[index];
  56. },
  57. /**
  58. * @param {Notification} notification
  59. */
  60. post: function(notification) {
  61. for (const key in this._observers) {
  62. if (!this._observers.hasOwnProperty(key)) continue;
  63. const observer = this._observers[key];
  64. if (observer.targetName != notification.name) continue;
  65. observer.action(notification.params);
  66. }
  67. },
  68.  
  69. _observerCounter: 0,
  70. _observers: {},
  71. _getOberverIndex: function() {
  72. const index = this._observerCounter;
  73. this._observerCounter += 1;
  74. return index;
  75. }
  76. };
  77.  
  78. /***********************************************************************************\
  79. Monitor Center
  80. \***********************************************************************************/
  81.  
  82. class Monitor {
  83. constructor(run) {
  84. this.run = run;
  85. }
  86. }
  87.  
  88. const MonitorCenter = {
  89. addMonitor: function(monitor) {
  90. this._monitors.push(monitor);
  91. },
  92. run: function() {
  93. for (const monitor of this._monitors) {
  94. monitor.run();
  95. }
  96. },
  97.  
  98. _monitors: []
  99. };
  100.  
  101. /***********************************************************************************\
  102. Trigger Template And Trigger
  103. \***********************************************************************************/
  104.  
  105. //---------------------------------------------------------------------------
  106. // Trigger Template
  107. //---------------------------------------------------------------------------
  108.  
  109. const EqualAssert = function(lh, rh) {
  110. return lh == rh;
  111. };
  112.  
  113. const ContainAssert = function(lh, rh) {
  114. const list = lh.split("|");
  115. return list.indexOf(rh) != -1;
  116. };
  117.  
  118. const KeyAssert = function(lh, rh) {
  119. const list = lh.split("|");
  120. for (const key of list) {
  121. if (rh.indexOf(key) != -1) return true;
  122. }
  123. return false;
  124. };
  125.  
  126. class Filter {
  127. constructor(name, type, defaultValue, assert) {
  128. this.name = name;
  129. this.type = type;
  130. this.defaultValue = defaultValue;
  131. this.assert = assert == null ? EqualAssert : assert;
  132. }
  133. }
  134.  
  135. class SelectFilter extends Filter {
  136. constructor(name, options, defaultNumber, assert) {
  137. const defaultValue = options[defaultNumber];
  138. super(name, "select", defaultValue, assert);
  139. this.options = options;
  140. }
  141. }
  142.  
  143. const InputFilterFormat = {
  144. number: "数字",
  145. text: "文本"
  146. };
  147.  
  148. class InputFilter extends Filter {
  149. /**
  150. * @param {String} name
  151. * @param {InputFilterFormat} format
  152. * @param {*} defaultValue
  153. */
  154. constructor(name, format, defaultValue, assert) {
  155. super(name, "input", defaultValue, assert);
  156. this.format = format;
  157. }
  158. }
  159.  
  160. class TriggerTemplate {
  161. constructor(event, filters, introdution) {
  162. this.event = event;
  163. this.filters = filters;
  164. this.introdution = `${introdution}\n// 如需更多信息,可以到论坛触发器版块发帖。`;
  165. }
  166. getFilter(name) {
  167. for (const filter of this.filters) {
  168. if (filter.name == name) return filter;
  169. }
  170. return null;
  171. }
  172. }
  173.  
  174. const TriggerTemplateCenter = {
  175. add: function(template) {
  176. this._templates[template.event] = template;
  177. },
  178. getAll: function() {
  179. return Object.values(this._templates);
  180. },
  181. get: function(event) {
  182. return this._templates[event];
  183. },
  184.  
  185. _templates: {},
  186. };
  187.  
  188. //---------------------------------------------------------------------------
  189. // Trigger
  190. //---------------------------------------------------------------------------
  191.  
  192. class Trigger {
  193. constructor(name, template, conditions, source) {
  194. this.name = name;
  195. this.template = template;
  196. this.conditions = conditions;
  197. this.source = source;
  198. this._action = function(params) {
  199. let realParams = CopyObject(params);
  200. for (const key in conditions) {
  201. if (!conditions.hasOwnProperty(key)) continue;
  202. const filter = template.getFilter(key);
  203. const fromUser = conditions[key];
  204. const fromGame = params[key];
  205. if (!filter.assert(fromUser, fromGame)) return;
  206. delete realParams[key];
  207. }
  208. let realSource = source;
  209. for (const key in realParams) {
  210. realSource = `($${key}) = ${realParams[key]}\n${realSource}`;
  211. }
  212. realSource = `@print 💡<hio>触发=>${name}</hio>\n${realSource}`;
  213. ToRaid.perform(realSource, name, false);
  214. };
  215. this._observerIndex = null;
  216. }
  217.  
  218. event() { return this.template.event; }
  219. active() { return this._observerIndex != null; }
  220.  
  221. _activate() {
  222. if (this._observerIndex != null) return;
  223. this._observerIndex = NotificationCenter.observe(this.template.event, this._action);
  224. }
  225. _deactivate() {
  226. if (this._observerIndex == null) return;
  227. NotificationCenter.removeOberver(this._observerIndex);
  228. this._observerIndex = null;
  229. }
  230. }
  231.  
  232. class TriggerData {
  233. constructor(name, event, conditions, source, active) {
  234. this.name = name;
  235. this.event = event;
  236. this.conditions = conditions;
  237. this.source = source;
  238. this.active = active;
  239. }
  240. }
  241.  
  242. const TriggerCenter = {
  243. run: function() {
  244. const allData = GM_getValue(this._saveKey(), {});
  245. for (const name in allData) {
  246. this._loadTrigger(name);
  247. }
  248. },
  249.  
  250. getAll: function() {
  251. return Object.values(this._triggers);
  252. },
  253. create: function(name, event, conditions, source) {
  254. const checkResult = this._checkName(name);
  255. if (checkResult != true) return checkResult;
  256.  
  257. const data = new TriggerData(name, event, conditions, source, false);
  258. this._updateData(data);
  259.  
  260. this._loadTrigger(name);
  261. return true;
  262. },
  263. modify: function(originalName, name, conditions, source) {
  264. const trigger = this._triggers[originalName];
  265. if (trigger == null) return "修改不存在的触发器?";
  266. const event = trigger.event();
  267. if (originalName == name) {
  268. const data = new TriggerData(name, event, conditions, source, trigger.active());
  269. this._updateData(data);
  270. this._reloadTrigger(name);
  271. return true;
  272. }
  273.  
  274. const result = this.create(name, event, conditions, source);
  275. if (result == true) {
  276. this.remove(originalName);
  277. this._loadTrigger(name);
  278. }
  279. return result;
  280. },
  281. remove: function(name) {
  282. const trigger = this._triggers[name];
  283. if (trigger == null) return;
  284.  
  285. trigger._deactivate();
  286. delete this._triggers[name];
  287. let allData = GM_getValue(this._saveKey(), {});
  288. delete allData[name];
  289. GM_setValue(this._saveKey(), allData);
  290. },
  291.  
  292. activate: function(name) {
  293. const trigger = this._triggers[name];
  294. if (trigger == null) return;
  295. if (trigger.active()) return;
  296. trigger._activate();
  297. let data = this._getData(name);
  298. data.active = true;
  299. this._updateData(data);
  300. },
  301. deactivate: function(name) {
  302. const trigger = this._triggers[name];
  303. if (trigger == null) return;
  304. if (!trigger.active()) return;
  305. trigger._deactivate();
  306. let data = this._getData(name);
  307. data.active = false;
  308. this._updateData(data);
  309. },
  310.  
  311. _triggers: {},
  312.  
  313. _saveKey: function() {
  314. return `${Role.id}@triggers`;
  315. },
  316. _reloadTrigger: function(name) {
  317. const oldTrigger = this._triggers[name];
  318. if (oldTrigger != null) {
  319. oldTrigger._deactivate();
  320. }
  321. this._loadTrigger(name);
  322. },
  323. _loadTrigger: function(name) {
  324. const data = this._getData(name);
  325. if (data == null) return;
  326. const trigger = this._toTrigger(data);
  327. this._triggers[name] = trigger;
  328. if (data.active) {
  329. trigger._activate();
  330. }
  331. },
  332. _getData: function(name) {
  333. let allData = GM_getValue(this._saveKey(), {});
  334. const data = allData[name];
  335. return data;
  336. },
  337. _updateData: function(data) {
  338. let allData = GM_getValue(this._saveKey(), {});
  339. allData[data.name] = data;
  340. GM_setValue(this._saveKey(), allData);
  341. },
  342. _toTrigger: function(data) {
  343. const template = TriggerTemplateCenter.get(data.event);
  344. const trigger = new Trigger(data.name, template, data.conditions, data.source);
  345. return trigger;
  346. },
  347. _checkName: function(name) {
  348. if (this._triggers[name] != null) return "无法修改名称,已经存在同名触发器!";
  349. if (!/\S+/.test(name)) return "触发器的名称不能为空。";
  350. if (!/^[_a-zA-Z0-9\u4e00-\u9fa5]+$/.test(name)) return "触发器的名称只能使用中文、英文和数字字符。";
  351. return true;
  352. }
  353. };
  354.  
  355. /***********************************************************************************\
  356. WSMUD
  357. \***********************************************************************************/
  358.  
  359. var WG = null;
  360. var messageAppend = null;
  361. var messageClear = null;
  362.  
  363. //---------------------------------------------------------------------------
  364. // status
  365. //---------------------------------------------------------------------------
  366.  
  367. (function() {
  368. const type = new SelectFilter("改变类型", ["新增", "移除", "层数刷新"], 0);
  369. const value = new InputFilter("BuffId", InputFilterFormat.text, "weapon", ContainAssert);
  370. const target = new SelectFilter("触发对象", ["自己", "他人"], 0);
  371. let filters = [type, value, target];
  372. const intro = `// Buff状态改变触发器
  373. // 触发对象id:(id)
  374. // buff的sid:(sid)
  375. // buff层数:(count)`;
  376. const t = new TriggerTemplate("Buff状态改变", filters, intro);
  377. TriggerTemplateCenter.add(t);
  378.  
  379. const run = function() {
  380. const post = function(data, sid, type) {
  381. let params = {
  382. "改变类型": type,
  383. "BuffId": sid,
  384. "触发对象": data.id == Role.id ? "自己" : "他人"
  385. };
  386. params["id"] = data.id;
  387. params["sid"] = sid;
  388. params["count"] = 0;
  389. if (data.count != null) params["count"] = data.count;
  390. const n = new Notification("Buff状态改变", params);
  391. NotificationCenter.post(n);
  392. };
  393. WG.add_hook("status", data => {
  394. if (data.action == null || data.id == null || data.sid == null) return;
  395. const types = {
  396. "add": "新增",
  397. "remove": "移除",
  398. "refresh": "层数刷新"
  399. };
  400. const type = types[data.action];
  401. if (type == null) return;
  402. if (data.sid instanceof Array) {
  403. for (const s of data.sid) {
  404. post(data, s, type);
  405. }
  406. } else {
  407. post(data, data.sid, type);
  408. }
  409. });
  410. };
  411. const monitor = new Monitor(run);
  412. MonitorCenter.addMonitor(monitor);
  413. })();
  414.  
  415. //---------------------------------------------------------------------------
  416. // msg
  417. //---------------------------------------------------------------------------
  418.  
  419. (function() {
  420. const chanel = new SelectFilter(
  421. "频道",
  422. ["全部", "世界", "队伍", "门派", "全区", "帮派", "谣言", "系统"],
  423. 0,
  424. function(fromUser, fromGame) {
  425. if (fromUser == "全部") return true;
  426. return fromUser == fromGame;
  427. }
  428. );
  429. const talker = new InputFilter("发言人", InputFilterFormat.text, "", ContainAssert);
  430. const key = new InputFilter("关键字", InputFilterFormat.text, "", KeyAssert);
  431. let filters = [chanel, talker, key];
  432. const intro = `// 新聊天信息触发器
  433. // 聊天信息内容:(content)
  434. // 发言人:(name)`;
  435. const t = new TriggerTemplate("新聊天信息", filters, intro);
  436. TriggerTemplateCenter.add(t);
  437.  
  438. const run = function() {
  439. WG.add_hook("msg", data => {
  440. if (data.ch == null || data.name == null || data.content == null) return;
  441. const types = {
  442. "chat": "世界",
  443. "tm": "队伍",
  444. "fam": "门派",
  445. "es": "全区",
  446. "pty": "帮派",
  447. "rumor": "谣言",
  448. "sys": "系统"
  449. };
  450. const chanel = types[data.ch];
  451. if (chanel == null) return;
  452. let params = {
  453. "频道": chanel,
  454. "发言人": data.name,
  455. "关键字": data.content
  456. };
  457. params["content"] = data.content;
  458. params["name"] = data.name;
  459. const n = new Notification("新聊天信息", params);
  460. NotificationCenter.post(n);
  461. });
  462. };
  463. const monitor = new Monitor(run);
  464. MonitorCenter.addMonitor(monitor);
  465. })();
  466.  
  467. //---------------------------------------------------------------------------
  468. // item add
  469. //---------------------------------------------------------------------------
  470.  
  471. (function() {
  472. const name = new InputFilter("人物名称", InputFilterFormat.text, "", KeyAssert);
  473. let filters = [name];
  474. const intro = `// 人物刷新触发器
  475. // 刷新人物id:(id)
  476. // 刷新人物名称:(name)`;
  477. const t = new TriggerTemplate("人物刷新", filters, intro);
  478. TriggerTemplateCenter.add(t);
  479.  
  480. const run = function() {
  481. WG.add_hook("itemadd", data => {
  482. if (data.name == null || data.id == null) return;
  483. let params = {
  484. "人物名称": data.name,
  485. };
  486. params["id"] = data.id;
  487. params["name"] = data.name;
  488. const n = new Notification("人物刷新", params);
  489. NotificationCenter.post(n);
  490. });
  491. };
  492. const monitor = new Monitor(run);
  493. MonitorCenter.addMonitor(monitor);
  494. })();
  495.  
  496. //---------------------------------------------------------------------------
  497. // dialog pack
  498. //---------------------------------------------------------------------------
  499.  
  500. (function() {
  501. const name = new InputFilter("名称关键字", InputFilterFormat.text, "", KeyAssert);
  502. let filters = [name];
  503. const intro = `// 物品拾取触发器
  504. // 拾取物品id:(id)
  505. // 拾取物品名称:(name)
  506. // 拾取物品数量:(count)
  507. // 物品品质:(quality) 值:白、绿、蓝、黄、紫、橙、红、未知`;
  508. const t = new TriggerTemplate("物品拾取", filters, intro);
  509. TriggerTemplateCenter.add(t);
  510.  
  511. const run = function() {
  512. WG.add_hook("dialog", function(data) {
  513. if (data.dialog != "pack" || data.id == null || data.name == null || data.count == null || data.remove != null) return;
  514. let params = {
  515. "名称关键字": data.name,
  516. };
  517. params["id"] = data.id;
  518. params["name"] = data.name;
  519. params["name"] = data.count;
  520. let quality = "未知";
  521. const tag = /<\w{3}>/.exec(data.name)[0];
  522. const tagMap = {
  523. "<wht>": "白",
  524. "<hig>": "绿",
  525. "<hic>": "蓝",
  526. "<hiy>": "黄",
  527. "<hiz>": "紫",
  528. "<hio>": "橙",
  529. "<ord>": "红"
  530. }
  531. quality = tagMap[tag];
  532. params["quality"] = quality;
  533. const n = new Notification("物品拾取", params);
  534. NotificationCenter.post(n);
  535. });
  536. };
  537. const monitor = new Monitor(run);
  538. MonitorCenter.addMonitor(monitor);
  539. })();
  540.  
  541. //---------------------------------------------------------------------------
  542. // text
  543. //---------------------------------------------------------------------------
  544.  
  545. (function() {
  546. const name = new InputFilter("关键字", InputFilterFormat.text, "", KeyAssert);
  547. let filters = [name];
  548. const intro = `// 新提示信息触发器
  549. // 提示信息:(text)`;
  550. const t = new TriggerTemplate("新提示信息", filters, intro);
  551. TriggerTemplateCenter.add(t);
  552.  
  553. const run = function() {
  554. WG.add_hook("text", data => {
  555. if (data.msg == null) return;
  556. let params = {
  557. "关键字": data.msg,
  558. };
  559. params["text"] = data.msg;
  560. const n = new Notification("新提示信息", params);
  561. NotificationCenter.post(n);
  562. });
  563. };
  564. const monitor = new Monitor(run);
  565. MonitorCenter.addMonitor(monitor);
  566. })();
  567.  
  568. //---------------------------------------------------------------------------
  569. // combat
  570. //---------------------------------------------------------------------------
  571.  
  572. (function() {
  573. const type = new SelectFilter("类型", ["进入战斗", "脱离战斗"], 0);
  574. let filters = [type];
  575. const intro = "// 战斗状态切换触发器";
  576. const t = new TriggerTemplate("战斗状态切换", filters, intro);
  577. TriggerTemplateCenter.add(t);
  578.  
  579. const run = function() {
  580. WG.add_hook("combat", data => {
  581. let params = null;
  582. if (data.start != null && data.start == 1) {
  583. params = { "类型": "进入战斗" };
  584. } else if (data.end != null && data.end == 1) {
  585. params = { "类型": "脱离战斗" };
  586. }
  587. const n = new Notification("战斗状态切换", params);
  588. NotificationCenter.post(n);
  589. });
  590. };
  591. const monitor = new Monitor(run);
  592. MonitorCenter.addMonitor(monitor);
  593. })();
  594.  
  595. //---------------------------------------------------------------------------
  596. // combat
  597. //---------------------------------------------------------------------------
  598.  
  599. (function() {
  600. const type = new SelectFilter("类型", ["已经死亡", "已经复活"], 0);
  601. let filters = [type];
  602. const intro = "// 死亡状态改变触发器";
  603. const t = new TriggerTemplate("死亡状态改变", filters, intro);
  604. TriggerTemplateCenter.add(t);
  605.  
  606. const run = function() {
  607. WG.add_hook("die", data => {
  608. const value = data.relive == null ? "已经复活" : "已经死亡";
  609. let params = {
  610. "类型": value
  611. };
  612. const n = new Notification("死亡状态改变", params);
  613. NotificationCenter.post(n);
  614. });
  615. };
  616. const monitor = new Monitor(run);
  617. MonitorCenter.addMonitor(monitor);
  618. })();
  619.  
  620. //---------------------------------------------------------------------------
  621. // time
  622. //---------------------------------------------------------------------------
  623.  
  624. (function() {
  625. const hours = [
  626. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  627. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  628. 20, 21, 22, 23
  629. ];
  630. const minutes = [
  631. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  632. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  633. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  634. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  635. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  636. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59
  637. ];
  638. const hour = new SelectFilter("时", hours, 0, EqualAssert);
  639. const minute = new SelectFilter("分", minutes, 0, EqualAssert);
  640. const second = new SelectFilter("秒", minutes, 0, EqualAssert);
  641. let filters = [hour, minute, second];
  642. const intro = "// 时辰已到触发器";
  643. const t = new TriggerTemplate("时辰已到", filters, intro);
  644. TriggerTemplateCenter.add(t);
  645.  
  646. const run = function() {
  647. setInterval(_ => {
  648. const date = new Date();
  649. const params = {
  650. "时": date.getHours(),
  651. "分": date.getMinutes(),
  652. "秒": date.getSeconds()
  653. };
  654. const n = new Notification("时辰已到", params);
  655. NotificationCenter.post(n);
  656. }, 1000);
  657. };
  658. const monitor = new Monitor(run);
  659. MonitorCenter.addMonitor(monitor);
  660. })();
  661.  
  662. //---------------------------------------------------------------------------
  663. // dispfm
  664. //---------------------------------------------------------------------------
  665.  
  666. (function() {
  667. const sid = new InputFilter("技能id", InputFilterFormat.text, "", ContainAssert);
  668. let filters = [sid];
  669. const intro = `// 技能释放触发器
  670. // 技能id:(id)
  671. // 出招时间:(rtime)
  672. // 冷却时间:(distime)`;
  673. const t = new TriggerTemplate("技能释放", filters, intro);
  674. TriggerTemplateCenter.add(t);
  675.  
  676. const sid1 = new InputFilter("技能id", InputFilterFormat.text, "", ContainAssert);
  677. let filters1 = [sid1];
  678. const intro1 = `// 技能冷却结束触发器
  679. // 技能id:(id)`;
  680. const t1 = new TriggerTemplate("技能冷却结束", filters1, intro1);
  681. TriggerTemplateCenter.add(t1);
  682.  
  683. const run = function() {
  684. WG.add_hook("dispfm", data => {
  685. if (data.id == null || data.distime == null || data.rtime == null) return;
  686. let params = {
  687. "技能id": data.id
  688. };
  689. params["id"] = data.id;
  690. params["rtime"] = data.rtime;
  691. params["distime"] = data.distime;
  692. const n = new Notification("技能释放", params);
  693. NotificationCenter.post(n);
  694.  
  695. setTimeout(_ => {
  696. let params = {
  697. "技能id": data.id
  698. };
  699. params["id"] = data.id;
  700. const n = new Notification("技能冷却结束", params);
  701. NotificationCenter.post(n);
  702. }, data.distime);
  703. });
  704. };
  705. const monitor = new Monitor(run);
  706. MonitorCenter.addMonitor(monitor);
  707. })();
  708.  
  709. /***********************************************************************************\
  710. UI
  711. \***********************************************************************************/
  712.  
  713. const Message = {
  714. append: function(msg) {
  715. messageAppend(msg);
  716. },
  717. clean: function() {
  718. messageClear();
  719. },
  720. };
  721.  
  722. const UI = {
  723. triggerHome: function() {
  724. const content = `
  725. <style>.breakText {word-break:keep-all;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}</style>
  726. <span class="zdy-item" style="width:120px" v-for="t in triggers" :style="activeStyle(t)">
  727. <div style="width: 30px; float: left; background-color: rgba(255, 255, 255, 0.31); border-radius: 4px;" v-on:click="editTrigger(t)">⚙</div>
  728. <div class="breakText" style="width: 85px; float: right;" v-on:click="switchStatus(t)">{{ t.name }}</div>
  729. </span>
  730. `;
  731. const rightText = "<span v-on:click='createTrigger()'><wht>新建</wht></span>";
  732. UI._appendHtml("🍟 <hio>触发器</hio>", content, rightText);
  733. new Vue({
  734. el: '#app',
  735. data: {
  736. triggers: TriggerCenter.getAll()
  737. },
  738. methods: {
  739. switchStatus: function(t) {
  740. if (t.active()) {
  741. TriggerCenter.deactivate(t.name);
  742. } else {
  743. TriggerCenter.activate(t.name);
  744. }
  745. UI.triggerHome();
  746. },
  747. editTrigger: UI.editTrigger,
  748. activeStyle: function(t) {
  749. if (t.active()) {
  750. return {
  751. "background-color": "#a0e6e0",
  752. "border": "1px solid #7284ff",
  753. "color": "#001bff"
  754. };
  755. } else {
  756. return { "background-color": "none" };
  757. }
  758. },
  759. createTrigger: UI.selectTriggerTemplate
  760. }
  761. });
  762. },
  763. selectTriggerTemplate: function() {
  764. const content = `
  765. <span class="zdy-item" style="width:120px" v-for="t in templates" v-on:click="select(t)">{{ t.event }}</span>
  766. `;
  767. const leftText = "<span v-on:click='back()'>< 返回</span>";
  768. UI._appendHtml("<wht>选择触发事件</wht>", content, null, leftText);
  769. new Vue({
  770. el: '#app',
  771. data: {
  772. templates: TriggerTemplateCenter.getAll()
  773. },
  774. methods: {
  775. select: UI.createTrigger,
  776. back: UI.triggerHome
  777. }
  778. });
  779. },
  780. createTrigger: function(template) {
  781. UI._updateTrigger(template);
  782. },
  783. editTrigger: function(trigger) {
  784. UI._updateTrigger(trigger.template, trigger);
  785. },
  786. _updateTrigger: function(template, trigger) {
  787. const content = `
  788. <div style="margin:0 2em 0 2em">
  789. <div style="float:left;width:120px">
  790. <span class="zdy-item" style="width:90px" v-for="f in filters">
  791. <p style="margin:0"><wht>{{ f.name }}</wht></p>
  792. <input v-if="f.type=='input'" style="width:80%" v-model="conditions[f.name]">
  793. <select v-if="f.type=='select'" v-model="conditions[f.name]">
  794. <option v-for="opt in f.options" :value="opt">{{ opt }}</option>
  795. </select>
  796. </span>
  797. </div>
  798. <div style="float:right;width:calc(100% - 125px)">
  799. <textarea class = "settingbox hide" style = "height:10rem;display:inline-block;font-size:0.8em;width:100%" v-model="source"></textarea>
  800. </div>
  801. </div>
  802. `;
  803. const title = `<input style='width:110px' type="text" placeholder="输入触发器名称" v-model="name">`;
  804. let rightText = "<span v-on:click='save'><wht>保存</wht></span>";
  805. if (trigger) {
  806. rightText = "<span v-on:click='remove'>删除</span>"
  807. }
  808. let leftText = "<span v-on:click='back'>< 返回</span>";
  809. if (trigger) {
  810. leftText = "<span v-on:click='saveback'>< 保存&返回</span>"
  811. }
  812. UI._appendHtml(title, content, rightText, leftText);
  813. let conditions = {};
  814. if (trigger != null) {
  815. conditions = trigger.conditions;
  816. } else {
  817. for (const f of template.filters) {
  818. conditions[f.name] = f.defaultValue;
  819. }
  820. }
  821. let source = template.introdution;
  822. if (trigger != null) source = trigger.source;
  823. new Vue({
  824. el: '#app',
  825. data: {
  826. filters: template.filters,
  827. name: trigger ? trigger.name : "",
  828. conditions: conditions,
  829. source: source
  830. },
  831. methods: {
  832. save: function() {
  833. const result = TriggerCenter.create(this.name, template.event, this.conditions, this.source);
  834. if (result == true) {
  835. UI.triggerHome();
  836. } else {
  837. alert(result);
  838. }
  839. },
  840. remove: function() {
  841. const verify = confirm("确认删除此触发器吗?");
  842. if (verify) {
  843. TriggerCenter.remove(trigger.name);
  844. UI.triggerHome();
  845. }
  846. },
  847. back: function() {
  848. UI.selectTriggerTemplate();
  849. },
  850. saveback: function() {
  851. const result = TriggerCenter.modify(trigger.name, this.name, this.conditions, this.source);
  852. if (result == true) {
  853. UI.triggerHome();
  854. } else {
  855. alert(result);
  856. }
  857. }
  858. }
  859. })
  860. },
  861.  
  862. _appendHtml: function(title, content, rightText, leftText) {
  863. var realLeftText = leftText == null ? "" : leftText;
  864. var realRightText = rightText == null ? "" : rightText;
  865. var html = `
  866. <div class = "item-commands" style="text-align:center" id="app">
  867. <div style="margin-top:0.5em">
  868. <div style="width:8em;float:left;text-align:left;padding:0px 0px 0px 2em;height:1.23em" id="wsmud_raid_left">${realLeftText}</div>
  869. <div style="width:calc(100% - 16em);float:left;height:1.23em">${title}</div>
  870. <div style="width:8em;float:left;text-align:right;padding:0px 2em 0px 0px;height:1.23em" id="wsmud_raid_right">${realRightText}</div>
  871. </div>
  872. <br><br>
  873. ${content}
  874. </div>`;
  875. Message.clean();
  876. Message.append(html);
  877. },
  878. };
  879.  
  880. /***********************************************************************************\
  881. Ready
  882. \***********************************************************************************/
  883.  
  884. let Running = false;
  885.  
  886. $(document).ready(function () {
  887. WG = unsafeWindow.WG;
  888. messageAppend = unsafeWindow.messageAppend;
  889. messageClear = unsafeWindow.messageClear;
  890. ToRaid = unsafeWindow.ToRaid;
  891.  
  892. unsafeWindow.TriggerUI = UI;
  893.  
  894. WG.add_hook("login", function(data) {
  895. if (Running) return;
  896. Running = true;
  897.  
  898. TriggerCenter.run();
  899. MonitorCenter.run();
  900. });
  901. });
  902. })();