wsmud_Trigger

武神传说 MUD

目前为 2019-03-04 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name wsmud_Trigger
  3. // @namespace cqv3
  4. // @version 0.0.4
  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. } else {
  279. return result;
  280. }
  281. },
  282. remove: function(name) {
  283. const trigger = this._triggers[name];
  284. if (trigger == null) return;
  285.  
  286. trigger._deactivate();
  287. delete this._triggers[name];
  288. let allData = GM_getValue(this._saveKey(), {});
  289. delete allData[name];
  290. GM_setValue(this._saveKey(), allData);
  291. },
  292.  
  293. activate: function(name) {
  294. const trigger = this._triggers[name];
  295. if (trigger == null) return;
  296. if (trigger.active()) return;
  297. trigger._activate();
  298. let data = this._getData(name);
  299. data.active = true;
  300. this._updateData(data);
  301. },
  302. deactivate: function(name) {
  303. const trigger = this._triggers[name];
  304. if (trigger == null) return;
  305. if (!trigger.active()) return;
  306. trigger._deactivate();
  307. let data = this._getData(name);
  308. data.active = false;
  309. this._updateData(data);
  310. },
  311.  
  312. _triggers: {},
  313.  
  314. _saveKey: function() {
  315. return `${Role.id}@triggers`;
  316. },
  317. _reloadTrigger: function(name) {
  318. const oldTrigger = this._triggers[name];
  319. if (oldTrigger != null) {
  320. oldTrigger._deactivate();
  321. }
  322. this._loadTrigger(name);
  323. },
  324. _loadTrigger: function(name) {
  325. const data = this._getData(name);
  326. if (data == null) return;
  327. const trigger = this._toTrigger(data);
  328. this._triggers[name] = trigger;
  329. if (data.active) {
  330. trigger._activate();
  331. }
  332. },
  333. _getData: function(name) {
  334. let allData = GM_getValue(this._saveKey(), {});
  335. const data = allData[name];
  336. return data;
  337. },
  338. _updateData: function(data) {
  339. let allData = GM_getValue(this._saveKey(), {});
  340. allData[data.name] = data;
  341. GM_setValue(this._saveKey(), allData);
  342. },
  343. _toTrigger: function(data) {
  344. const template = TriggerTemplateCenter.get(data.event);
  345. const trigger = new Trigger(data.name, template, data.conditions, data.source);
  346. return trigger;
  347. },
  348. _checkName: function(name) {
  349. if (this._triggers[name] != null) return "无法修改名称,已经存在同名触发器!";
  350. if (!/\S+/.test(name)) return "触发器的名称不能为空。";
  351. if (!/^[_a-zA-Z0-9\u4e00-\u9fa5]+$/.test(name)) return "触发器的名称只能使用中文、英文和数字字符。";
  352. return true;
  353. }
  354. };
  355.  
  356. /***********************************************************************************\
  357. WSMUD
  358. \***********************************************************************************/
  359.  
  360. var WG = null;
  361. var messageAppend = null;
  362. var messageClear = null;
  363.  
  364. //---------------------------------------------------------------------------
  365. // status
  366. //---------------------------------------------------------------------------
  367.  
  368. (function() {
  369. const type = new SelectFilter("改变类型", ["新增", "移除", "层数刷新"], 0);
  370. const value = new InputFilter("BuffId", InputFilterFormat.text, "weapon", ContainAssert);
  371. const target = new SelectFilter("触发对象", ["自己", "他人"], 0);
  372. let filters = [type, value, target];
  373. const intro = `
  374. // 触发对象id:(id)
  375. // buff的sid:(sid)
  376. // buff层数:(count)`;
  377. const t = new TriggerTemplate("Buff状态改变", filters, intro);
  378. TriggerTemplateCenter.add(t);
  379.  
  380. const run = function() {
  381. WG.add_hook("status", data => {
  382. if (data.action == null || data.id == null || data.sid == null) return;
  383. const types = {
  384. "add": "新增",
  385. "remove": "移除",
  386. "refresh": "层数刷新"
  387. };
  388. const type = types[data.action];
  389. if (type == null) return;
  390. let params = {
  391. "改变类型": type,
  392. "BuffId": data.sid,
  393. "触发对象": data.id == Role.id ? "自己" : "他人"
  394. };
  395. params["id"] = data.id;
  396. params["sid"] = data.sid;
  397. params["count"] = 0;
  398. if (data.count != null) params["count"] = data.count;
  399. const n = new Notification("Buff状态改变", params);
  400. NotificationCenter.post(n);
  401. });
  402. };
  403. const monitor = new Monitor(run);
  404. MonitorCenter.addMonitor(monitor);
  405. })();
  406.  
  407. //---------------------------------------------------------------------------
  408. // msg
  409. //---------------------------------------------------------------------------
  410.  
  411. (function() {
  412. const chanel = new SelectFilter(
  413. "频道",
  414. ["全部", "世界", "队伍", "门派", "全区", "帮派", "谣言", "系统"],
  415. 0,
  416. function(fromUser, fromGame) {
  417. if (fromUser == "全部") return true;
  418. return fromUser == fromGame;
  419. }
  420. );
  421. const talker = new InputFilter("发言人", InputFilterFormat.text, "", ContainAssert);
  422. const key = new InputFilter("关键字", InputFilterFormat.text, "", KeyAssert);
  423. let filters = [chanel, talker, key];
  424. const intro = `
  425. // 聊天信息内容:(content)
  426. // 发言人:(name)`;
  427. const t = new TriggerTemplate("新聊天信息", filters, intro);
  428. TriggerTemplateCenter.add(t);
  429.  
  430. const run = function() {
  431. WG.add_hook("msg", data => {
  432. if (data.ch == null || data.name == null || data.content == null) return;
  433. const types = {
  434. "chat": "世界",
  435. "tm": "队伍",
  436. "fam": "门派",
  437. "es": "全区",
  438. "pty": "帮派",
  439. "rumor": "谣言",
  440. "sys": "系统"
  441. };
  442. const chanel = types[data.ch];
  443. if (chanel == null) return;
  444. let params = {
  445. "频道": chanel,
  446. "发言人": data.name,
  447. "关键字": data.content
  448. };
  449. params["content"] = data.content;
  450. params["name"] = data.name;
  451. const n = new Notification("新聊天信息", params);
  452. NotificationCenter.post(n);
  453. });
  454. };
  455. const monitor = new Monitor(run);
  456. MonitorCenter.addMonitor(monitor);
  457. })();
  458.  
  459. //---------------------------------------------------------------------------
  460. // item add
  461. //---------------------------------------------------------------------------
  462.  
  463. (function() {
  464. const name = new InputFilter("人物名称", InputFilterFormat.text, "", ContainAssert);
  465. let filters = [name];
  466. const intro = `
  467. // 刷新人物id:(id)
  468. // 刷新人物名称:(name)`;
  469. const t = new TriggerTemplate("人物刷新", filters, intro);
  470. TriggerTemplateCenter.add(t);
  471.  
  472. const run = function() {
  473. WG.add_hook("itemadd", data => {
  474. if (data.name == null || data.id == null) return;
  475. let params = {
  476. "人物名称": data.name,
  477. };
  478. params["id"] = data.id;
  479. params["name"] = data.name;
  480. const n = new Notification("人物刷新", params);
  481. NotificationCenter.post(n);
  482. });
  483. };
  484. const monitor = new Monitor(run);
  485. MonitorCenter.addMonitor(monitor);
  486. })();
  487.  
  488. //---------------------------------------------------------------------------
  489. // dialog pack
  490. //---------------------------------------------------------------------------
  491.  
  492. (function() {
  493. const name = new InputFilter("名称关键字", InputFilterFormat.text, "", KeyAssert);
  494. let filters = [name];
  495. const intro = `
  496. // 拾取物品id:(id)
  497. // 拾取物品名称:(name)
  498. // 拾取物品数量:(count)
  499. // 物品品质:(quality) 值:白、绿、蓝、黄、紫、橙、红、未知`;
  500. const t = new TriggerTemplate("物品拾取", filters, intro);
  501. TriggerTemplateCenter.add(t);
  502.  
  503. const run = function() {
  504. WG.add_hook("dialog", function(data) {
  505. if (data.dialog != "pack" || data.id == null || data.name == null || data.count == null || data.remove != null) return;
  506. let params = {
  507. "名称关键字": data.name,
  508. };
  509. params["id"] = data.id;
  510. params["name"] = data.name;
  511. params["name"] = data.count;
  512. let quality = "未知";
  513. const tag = /<\w{3}>/.exec(data.name)[0];
  514. const tagMap = {
  515. "<wht>": "白",
  516. "<hig>": "绿",
  517. "<hic>": "蓝",
  518. "<hiy>": "黄",
  519. "<hiz>": "紫",
  520. "<hio>": "橙",
  521. "<ord>": "红"
  522. }
  523. quality = tagMap[tag];
  524. params["quality"] = quality;
  525. const n = new Notification("物品拾取", params);
  526. NotificationCenter.post(n);
  527. });
  528. };
  529. const monitor = new Monitor(run);
  530. MonitorCenter.addMonitor(monitor);
  531. })();
  532.  
  533. //---------------------------------------------------------------------------
  534. // text
  535. //---------------------------------------------------------------------------
  536.  
  537. (function() {
  538. const name = new InputFilter("关键字", InputFilterFormat.text, "", KeyAssert);
  539. let filters = [name];
  540. const intro = `
  541. // 提示信息:(text)`;
  542. const t = new TriggerTemplate("新提示信息", filters, intro);
  543. TriggerTemplateCenter.add(t);
  544.  
  545. const run = function() {
  546. WG.add_hook("text", data => {
  547. if (data.msg == null) return;
  548. let params = {
  549. "关键字": data.msg,
  550. };
  551. params["text"] = data.msg;
  552. const n = new Notification("新提示信息", params);
  553. NotificationCenter.post(n);
  554. });
  555. };
  556. const monitor = new Monitor(run);
  557. MonitorCenter.addMonitor(monitor);
  558. })();
  559.  
  560. //---------------------------------------------------------------------------
  561. // combat
  562. //---------------------------------------------------------------------------
  563.  
  564. (function() {
  565. const type = new SelectFilter("类型", ["进入战斗", "脱离战斗"], 0);
  566. let filters = [type];
  567. const intro = "";
  568. const t = new TriggerTemplate("战斗状态切换", filters, intro);
  569. TriggerTemplateCenter.add(t);
  570.  
  571. const run = function() {
  572. WG.add_hook("combat", data => {
  573. let params = null;
  574. if (data.start != null && data.start == 1) {
  575. params = { "类型": "进入战斗" };
  576. } else if (data.end != null && data.end == 1) {
  577. params = { "类型": "脱离战斗" };
  578. }
  579. const n = new Notification("战斗状态切换", params);
  580. NotificationCenter.post(n);
  581. });
  582. };
  583. const monitor = new Monitor(run);
  584. MonitorCenter.addMonitor(monitor);
  585. })();
  586.  
  587. //---------------------------------------------------------------------------
  588. // combat
  589. //---------------------------------------------------------------------------
  590.  
  591. (function() {
  592. const type = new SelectFilter("类型", ["已经死亡", "已经复活"], 0);
  593. let filters = [type];
  594. const intro = "";
  595. const t = new TriggerTemplate("死亡状态改变", filters, intro);
  596. TriggerTemplateCenter.add(t);
  597.  
  598. const run = function() {
  599. WG.add_hook("die", data => {
  600. const value = data.relive == null ? "已经复活" : "已经死亡";
  601. let params = {
  602. "类型": value
  603. };
  604. const n = new Notification("死亡状态改变", params);
  605. NotificationCenter.post(n);
  606. });
  607. };
  608. const monitor = new Monitor(run);
  609. MonitorCenter.addMonitor(monitor);
  610. })();
  611.  
  612. //---------------------------------------------------------------------------
  613. // time
  614. //---------------------------------------------------------------------------
  615.  
  616. (function() {
  617. const hours = [
  618. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  619. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  620. 20, 21, 22, 23
  621. ];
  622. const minutes = [
  623. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  624. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  625. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  626. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  627. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  628. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59
  629. ];
  630. const hour = new SelectFilter("时", hours, 0, EqualAssert);
  631. const minute = new SelectFilter("分", minutes, 0, EqualAssert);
  632. const second = new SelectFilter("秒", minutes, 0, EqualAssert);
  633. let filters = [hour, minute, second];
  634. const intro = "";
  635. const t = new TriggerTemplate("时辰已到", filters, intro);
  636. TriggerTemplateCenter.add(t);
  637.  
  638. const run = function() {
  639. setInterval(_ => {
  640. const date = new Date();
  641. const params = {
  642. "时": date.getHours(),
  643. "分": date.getMinutes(),
  644. "秒": date.getSeconds()
  645. };
  646. const n = new Notification("时辰已到", params);
  647. NotificationCenter.post(n);
  648. }, 1000);
  649. };
  650. const monitor = new Monitor(run);
  651. MonitorCenter.addMonitor(monitor);
  652. })();
  653.  
  654. /***********************************************************************************\
  655. UI
  656. \***********************************************************************************/
  657.  
  658. const Message = {
  659. append: function(msg) {
  660. messageAppend(msg);
  661. },
  662. clean: function() {
  663. messageClear();
  664. },
  665. };
  666.  
  667. const UI = {
  668. triggerHome: function() {
  669. const content = `
  670. <style>.breakText {word-break:keep-all;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}</style>
  671. <span class="zdy-item" style="width:120px" v-for="t in triggers" :style="activeStyle(t)">
  672. <div style="width: 30px; float: left; background-color: rgba(255, 255, 255, 0.31); border-radius: 4px;" v-on:click="editTrigger(t)">⚙</div>
  673. <div class="breakText" style="width: 85px; float: right;" v-on:click="switchStatus(t)">{{ t.name }}</div>
  674. </span>
  675. `;
  676. const rightText = "<span v-on:click='createTrigger()'><wht>新建</wht></span>";
  677. UI._appendHtml("<hio>触发器</hio>", content, rightText);
  678. new Vue({
  679. el: '#app',
  680. data: {
  681. triggers: TriggerCenter.getAll()
  682. },
  683. methods: {
  684. switchStatus: function(t) {
  685. if (t.active()) {
  686. TriggerCenter.deactivate(t.name);
  687. } else {
  688. TriggerCenter.activate(t.name);
  689. }
  690. UI.triggerHome();
  691. },
  692. editTrigger: UI.editTrigger,
  693. activeStyle: function(t) {
  694. if (t.active()) {
  695. return {
  696. "background-color": "#a0e6e0",
  697. "border": "1px solid #7284ff",
  698. "color": "#001bff"
  699. };
  700. } else {
  701. return { "background-color": "none" };
  702. }
  703. },
  704. createTrigger: UI.selectTriggerTemplate
  705. }
  706. });
  707. },
  708. selectTriggerTemplate: function() {
  709. const content = `
  710. <span class="zdy-item" style="width:120px" v-for="t in templates" v-on:click="select(t)">{{ t.event }}</span>
  711. `;
  712. const leftText = "<span v-on:click='back()'>< 返回</span>";
  713. UI._appendHtml("<wht>选择触发器类型</wht>", content, null, leftText);
  714. new Vue({
  715. el: '#app',
  716. data: {
  717. templates: TriggerTemplateCenter.getAll()
  718. },
  719. methods: {
  720. select: UI.createTrigger,
  721. back: UI.triggerHome
  722. }
  723. });
  724. },
  725. createTrigger: function(template) {
  726. UI._updateTrigger(template);
  727. },
  728. editTrigger: function(trigger) {
  729. UI._updateTrigger(trigger.template, trigger);
  730. },
  731. _updateTrigger: function(template, trigger) {
  732. const content = `
  733. <div style="margin:0 2em 0 2em">
  734. <div style="float:left;width:120px">
  735. <span class="zdy-item" style="width:90px" v-for="f in filters">
  736. <p style="margin:0"><wht>{{ f.name }}</wht></p>
  737. <input v-if="f.type=='input'" style="width:80%" v-model="conditions[f.name]">
  738. <select v-if="f.type=='select'" v-model="conditions[f.name]">
  739. <option v-for="opt in f.options" :value="opt">{{ opt }}</option>
  740. </select>
  741. </span>
  742. </div>
  743. <div style="float:right;width:calc(100% - 125px)">
  744. <textarea class = "settingbox hide" style = "height:10rem;display:inline-block;font-size:0.8em;width:100%" v-model="source"></textarea>
  745. </div>
  746. </div>
  747. `;
  748. const title = `<input style='width:110px' type="text" placeholder="输入触发器名称" v-model="name">`;
  749. let rightText = "<span v-on:click='save'><wht>保存</wht></span>";
  750. if (trigger) {
  751. rightText = "<span v-on:click='remove'>删除</span>"
  752. }
  753. let leftText = "<span v-on:click='back'>< 返回</span>";
  754. if (trigger) {
  755. leftText = "<span v-on:click='saveback'>< 保存&返回</span>"
  756. }
  757. UI._appendHtml(title, content, rightText, leftText);
  758. let conditions = {};
  759. if (trigger != null) {
  760. conditions = trigger.conditions;
  761. } else {
  762. for (const f of template.filters) {
  763. conditions[f.name] = f.defaultValue;
  764. }
  765. }
  766. let source = template.introdution;
  767. if (trigger != null) source = trigger.source;
  768. new Vue({
  769. el: '#app',
  770. data: {
  771. filters: template.filters,
  772. name: trigger ? trigger.name : "",
  773. conditions: conditions,
  774. source: source
  775. },
  776. methods: {
  777. save: function() {
  778. const result = TriggerCenter.create(this.name, template.event, this.conditions, this.source);
  779. if (result == true) {
  780. UI.triggerHome();
  781. } else {
  782. alert(result);
  783. }
  784. },
  785. remove: function() {
  786. const verify = confirm("确认删除此触发器吗?");
  787. if (verify) {
  788. TriggerCenter.remove(trigger.name);
  789. UI.triggerHome();
  790. }
  791. },
  792. back: function() {
  793. UI.selectTriggerTemplate();
  794. },
  795. saveback: function() {
  796. const result = TriggerCenter.modify(trigger.name, this.name, this.conditions, this.source);
  797. if (result == true) {
  798. UI.triggerHome();
  799. } else {
  800. alert(result);
  801. }
  802. }
  803. }
  804. })
  805. },
  806.  
  807. _appendHtml: function(title, content, rightText, leftText) {
  808. var realLeftText = leftText == null ? "" : leftText;
  809. var realRightText = rightText == null ? "" : rightText;
  810. var html = `
  811. <div class = "item-commands" style="text-align:center" id="app">
  812. <div style="margin-top:0.5em">
  813. <div style="width:8em;float:left;text-align:left;padding:0px 0px 0px 2em;height:1.23em" id="wsmud_raid_left">${realLeftText}</div>
  814. <div style="width:calc(100% - 16em);float:left;height:1.23em">${title}</div>
  815. <div style="width:8em;float:left;text-align:right;padding:0px 2em 0px 0px;height:1.23em" id="wsmud_raid_right">${realRightText}</div>
  816. </div>
  817. <br><br>
  818. ${content}
  819. </div>`;
  820. Message.clean();
  821. Message.append(html);
  822. },
  823. };
  824.  
  825. /***********************************************************************************\
  826. Ready
  827. \***********************************************************************************/
  828.  
  829. $(document).ready(function () {
  830. WG = unsafeWindow.WG;
  831. messageAppend = unsafeWindow.messageAppend;
  832. messageClear = unsafeWindow.messageClear;
  833. ToRaid = unsafeWindow.ToRaid;
  834.  
  835. unsafeWindow.TriggerUI = UI;
  836.  
  837. WG.add_hook("login", function(data) {
  838. TriggerCenter.run();
  839. MonitorCenter.run();
  840. });
  841. });
  842. })();