wsmud_Trigger

武神传说 MUD

目前为 2020-06-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name wsmud_Trigger
  3. // @namespace cqv3
  4. // @version 0.0.38
  5. // @date 03/03/2019
  6. // @modified 05/06/2020
  7. // @homepage https://greasyfork.org/zh-CN/scripts/378984
  8. // @description 武神传说 MUD
  9. // @author Bob.cn, 初心, 白三三
  10. // @match http://*.wsmud.com/*
  11. // @run-at document-end
  12. // @require https://cdn.staticfile.org/vue/2.2.2/vue.min.js
  13. // @grant unsafeWindow
  14. // @grant GM_getValue
  15. // @grant GM_setValue
  16. // @grant GM_deleteValue
  17. // @grant GM_listValues
  18. // @grant GM_setClipboard
  19. // ==/UserScript==
  20.  
  21. (function () {
  22. 'use strict';
  23.  
  24. function CopyObject(obj) {
  25. return JSON.parse(JSON.stringify(obj));
  26. }
  27.  
  28. /***********************************************************************************\
  29. Notification Center
  30. \***********************************************************************************/
  31.  
  32. class Notification {
  33. constructor(name, params) {
  34. this.name = name;
  35. this.params = params;
  36. }
  37. }
  38.  
  39. class NotificationObserver {
  40. constructor(targetName, action) {
  41. this.targetName = targetName;
  42. this.action = action;
  43. }
  44. }
  45.  
  46. const NotificationCenter = {
  47. observe: function(notificationName, action) {
  48. const index = this._getOberverIndex();
  49. const observer = new NotificationObserver(notificationName, action);
  50. this._observers[index] = observer;
  51. return index;
  52. },
  53. removeOberver: function(index) {
  54. delete this._observers[index];
  55. },
  56. /**
  57. * @param {Notification} notification
  58. */
  59. post: function(notification) {
  60. for (const key in this._observers) {
  61. if (!this._observers.hasOwnProperty(key)) continue;
  62. const observer = this._observers[key];
  63. if (observer.targetName != notification.name) continue;
  64. observer.action(notification.params);
  65. }
  66. },
  67.  
  68. _observerCounter: 0,
  69. _observers: {},
  70. _getOberverIndex: function() {
  71. const index = this._observerCounter;
  72. this._observerCounter += 1;
  73. return index;
  74. }
  75. };
  76.  
  77. /***********************************************************************************\
  78. Monitor Center
  79. \***********************************************************************************/
  80.  
  81. class Monitor {
  82. constructor(run) {
  83. this.run = run;
  84. }
  85. }
  86.  
  87. const MonitorCenter = {
  88. addMonitor: function(monitor) {
  89. this._monitors.push(monitor);
  90. },
  91. run: function() {
  92. for (const monitor of this._monitors) {
  93. monitor.run();
  94. }
  95. },
  96.  
  97. _monitors: []
  98. };
  99.  
  100. /***********************************************************************************\
  101. Trigger Template And Trigger
  102. \***********************************************************************************/
  103.  
  104. //---------------------------------------------------------------------------
  105. // Trigger Template
  106. //---------------------------------------------------------------------------
  107.  
  108. const EqualAssert = function(lh, rh) {
  109. return lh == rh;
  110. };
  111.  
  112. const ContainAssert = function(lh, rh) {
  113. if (/^\s*\*?\s*$/.test(lh)) return true;
  114. const list = lh.split("|");
  115. return list.indexOf(rh) != -1;
  116. };
  117.  
  118. const KeyAssert = function(lh, rh) {
  119. if (/^\s*\*?\s*$/.test(lh)) return true;
  120. const list = lh.split("|");
  121. for (const key of list) {
  122. if (rh.indexOf(key) != -1) return true;
  123. }
  124. return false;
  125. };
  126.  
  127. class Filter {
  128. constructor(name, type, defaultValue, assert) {
  129. this.name = name;
  130. this.type = type;
  131. this.defaultValue = defaultValue;
  132. this.assert = assert == null ? EqualAssert : assert;
  133. }
  134. description(value) {
  135. if (value != null) {
  136. this._desc = value;
  137. return;
  138. }
  139. return this._desc == null ? this.name : this._desc;
  140. }
  141. }
  142.  
  143. class SelectFilter extends Filter {
  144. constructor(name, options, defaultNumber, assert) {
  145. const defaultValue = options[defaultNumber];
  146. super(name, "select", defaultValue, assert);
  147. this.options = options;
  148. }
  149. }
  150.  
  151. const InputFilterFormat = {
  152. number: "数字",
  153. text: "文本"
  154. };
  155.  
  156. class InputFilter extends Filter {
  157. /**
  158. * @param {String} name
  159. * @param {InputFilterFormat} format
  160. * @param {*} defaultValue
  161. */
  162. constructor(name, format, defaultValue, assert) {
  163. super(name, "input", defaultValue, assert);
  164. this.format = format;
  165. }
  166. }
  167.  
  168. class TriggerTemplate {
  169. constructor(event, filters, introdution) {
  170. this.event = event;
  171. this.filters = filters;
  172. this.introdution = `${introdution}\n// 如需更多信息,可以到论坛触发器版块发帖。`;
  173. }
  174. getFilter(name) {
  175. for (const filter of this.filters) {
  176. if (filter.name == name) return filter;
  177. }
  178. return null;
  179. }
  180. }
  181.  
  182. const TriggerTemplateCenter = {
  183. add: function(template) {
  184. this._templates[template.event] = template;
  185. },
  186. getAll: function() {
  187. return Object.values(this._templates);
  188. },
  189. get: function(event) {
  190. return this._templates[event];
  191. },
  192.  
  193. _templates: {},
  194. };
  195.  
  196. //---------------------------------------------------------------------------
  197. // Trigger
  198. //---------------------------------------------------------------------------
  199.  
  200. class Trigger {
  201. constructor(name, template, conditions, source) {
  202. this.name = name;
  203. this.template = template;
  204. this.conditions = conditions;
  205. this.source = source;
  206. this._action = function(params) {
  207. let realParams = CopyObject(params);
  208. for (const key in conditions) {
  209. if (!conditions.hasOwnProperty(key)) continue;
  210. const filter = template.getFilter(key);
  211. const fromUser = conditions[key];
  212. const fromGame = params[key];
  213. if (!filter.assert(fromUser, fromGame)) return;
  214. delete realParams[key];
  215. }
  216. let realSource = source;
  217. for (const key in realParams) {
  218. realSource = `($${key}) = ${realParams[key]}\n${realSource}`;
  219. }
  220. if (/\/\/\s*~silent\s*\n/.test(source) == false) {
  221. realSource = `@print 💡<hio>触发=>${name}</hio>\n${realSource}`;
  222. }
  223. ToRaid.perform(realSource, name, false);
  224. };
  225. this._observerIndex = null;
  226. }
  227.  
  228. event() { return this.template.event; }
  229. active() { return this._observerIndex != null; }
  230.  
  231. _activate() {
  232. if (this._observerIndex != null) return;
  233. this._observerIndex = NotificationCenter.observe(this.template.event, this._action);
  234. }
  235. _deactivate() {
  236. if (this._observerIndex == null) return;
  237. NotificationCenter.removeOberver(this._observerIndex);
  238. this._observerIndex = null;
  239. }
  240. }
  241.  
  242. class TriggerData {
  243. constructor(name, event, conditions, source, active) {
  244. this.name = name;
  245. this.event = event;
  246. this.conditions = conditions;
  247. this.source = source;
  248. this.active = active;
  249. }
  250. }
  251.  
  252. const TriggerCenter = {
  253. run: function() {
  254. const allData = GM_getValue(this._saveKey(), {});
  255. for (const name in allData) {
  256. this._loadTrigger(name);
  257. }
  258. },
  259. reload: function() {
  260. for (const name in this._triggers) {
  261. if (!this._triggers.hasOwnProperty(name)) continue;
  262. const trigger = this._triggers[name];
  263. trigger._deactivate();
  264. delete this._triggers[name];
  265. }
  266. this.run();
  267. },
  268.  
  269. // for upload and download
  270. getAllData: function() {
  271. return GM_getValue(this._saveKey(), {});
  272. },
  273. corver: function(triggerDatas) {
  274. for (const old of this.getAll()) {
  275. this.remove(old.name);
  276. }
  277. for (const name in triggerDatas) {
  278. const trigger = triggerDatas[name];
  279. this.create(trigger.name, trigger.event, trigger.conditions, trigger.source, trigger.active);
  280. }
  281. },
  282.  
  283. getAll: function() {
  284. return Object.values(this._triggers);
  285. },
  286. create: function(name, event, conditions, source, active) {
  287. const checkResult = this._checkName(name);
  288. if (checkResult != true) return checkResult;
  289.  
  290. const theActive = active == null ? false : active;
  291. const data = new TriggerData(name, event, conditions, source, theActive);
  292. this._updateData(data);
  293.  
  294. this._loadTrigger(name);
  295. return true;
  296. },
  297. modify: function(originalName, name, conditions, source) {
  298. const trigger = this._triggers[originalName];
  299. if (trigger == null) return "修改不存在的触发器?";
  300.  
  301. const event = trigger.event();
  302. if (originalName == name) {
  303. const data = new TriggerData(name, event, conditions, source, trigger.active());
  304. this._updateData(data);
  305. this._reloadTrigger(name);
  306. return true;
  307. }
  308.  
  309. const result = this.create(name, event, conditions, source);
  310. if (result == true) {
  311. this.remove(originalName);
  312. this._loadTrigger(name);
  313. }
  314. return result;
  315. },
  316. remove: function(name) {
  317. const trigger = this._triggers[name];
  318. if (trigger == null) return;
  319.  
  320. trigger._deactivate();
  321. delete this._triggers[name];
  322. let allData = GM_getValue(this._saveKey(), {});
  323. delete allData[name];
  324. GM_setValue(this._saveKey(), allData);
  325. },
  326.  
  327. activate: function(name) {
  328. const trigger = this._triggers[name];
  329. if (trigger == null) return;
  330. if (trigger.active()) return;
  331. trigger._activate();
  332. let data = this._getData(name);
  333. data.active = true;
  334. this._updateData(data);
  335. },
  336. deactivate: function(name) {
  337. const trigger = this._triggers[name];
  338. if (trigger == null) return;
  339. if (!trigger.active()) return;
  340. trigger._deactivate();
  341. let data = this._getData(name);
  342. data.active = false;
  343. this._updateData(data);
  344. },
  345.  
  346. _triggers: {},
  347.  
  348. _saveKey: function() {
  349. return `${Role.id}@triggers`;
  350. },
  351. _reloadTrigger: function(name) {
  352. const oldTrigger = this._triggers[name];
  353. if (oldTrigger != null) {
  354. oldTrigger._deactivate();
  355. }
  356. this._loadTrigger(name);
  357. },
  358. _loadTrigger: function(name) {
  359. const data = this._getData(name);
  360. if (data == null) return;
  361. const trigger = this._toTrigger(data);
  362. this._triggers[name] = trigger;
  363. if (data.active) {
  364. trigger._activate();
  365. }
  366. },
  367. _getData: function(name) {
  368. let allData = GM_getValue(this._saveKey(), {});
  369. const data = allData[name];
  370. return data;
  371. },
  372. _updateData: function(data) {
  373. let allData = GM_getValue(this._saveKey(), {});
  374. allData[data.name] = data;
  375. GM_setValue(this._saveKey(), allData);
  376. },
  377. _toTrigger: function(data) {
  378. const template = TriggerTemplateCenter.get(data.event);
  379. const trigger = new Trigger(data.name, template, data.conditions, data.source);
  380. return trigger;
  381. },
  382. _checkName: function(name) {
  383. if (this._triggers[name] != null) return "无法修改名称,已经存在同名触发器!";
  384. if (!/\S+/.test(name)) return "触发器的名称不能为空。";
  385. if (!/^[_a-zA-Z0-9\u4e00-\u9fa5]+$/.test(name)) return "触发器的名称只能使用中文、英文和数字字符。";
  386. return true;
  387. }
  388. };
  389.  
  390. /***********************************************************************************\
  391. WSMUD
  392. \***********************************************************************************/
  393.  
  394. var WG = null;
  395. var messageAppend = null;
  396. var messageClear = null;
  397. var ToRaid = null;
  398. var Role = null;
  399.  
  400. //---------------------------------------------------------------------------
  401. // status
  402. //---------------------------------------------------------------------------
  403.  
  404. (function() {
  405. const type = new SelectFilter("改变类型", ["新增", "移除", "层数刷新"], 0);
  406. const value = new InputFilter("BuffId", InputFilterFormat.text, "weapon", ContainAssert);
  407. const target = new SelectFilter("触发对象", ["自己", "他人"], 0);
  408. let filters = [type, value, target];
  409. const intro = `// Buff状态改变触发器
  410. // 触发对象id:(id)
  411. // buff的sid:(sid)
  412. // buff层数:(count)`;
  413. const t = new TriggerTemplate("Buff状态改变", filters, intro);
  414. TriggerTemplateCenter.add(t);
  415.  
  416. const run = function() {
  417. const post = function(data, sid, type) {
  418. let params = {
  419. "改变类型": type,
  420. "BuffId": sid,
  421. "触发对象": data.id == Role.id ? "自己" : "他人"
  422. };
  423. params["id"] = data.id;
  424. params["sid"] = sid;
  425. params["count"] = 0;
  426. if (data.count != null) params["count"] = data.count;
  427. const n = new Notification("Buff状态改变", params);
  428. NotificationCenter.post(n);
  429. };
  430. WG.add_hook("status", data => {
  431. if (data.action == null || data.id == null || data.sid == null) return;
  432. const types = {
  433. "add": "新增",
  434. "remove": "移除",
  435. "refresh": "层数刷新"
  436. };
  437. const type = types[data.action];
  438. if (type == null) return;
  439. if (data.sid instanceof Array) {
  440. for (const s of data.sid) {
  441. post(data, s, type);
  442. }
  443. } else {
  444. post(data, data.sid, type);
  445. }
  446. });
  447. };
  448. const monitor = new Monitor(run);
  449. MonitorCenter.addMonitor(monitor);
  450. })();
  451.  
  452. //---------------------------------------------------------------------------
  453. // msg
  454. //---------------------------------------------------------------------------
  455.  
  456. (function() {
  457. const channel = new SelectFilter(
  458. "频道",
  459. ["全部", "世界", "队伍", "门派", "全区", "帮派", "谣言", "系统"],
  460. 0,
  461. function(fromUser, fromGame) {
  462. if (fromUser == "全部") return true;
  463. return fromUser == fromGame;
  464. }
  465. );
  466. const talker = new InputFilter("发言人", InputFilterFormat.text, "", ContainAssert);
  467. const key = new InputFilter("关键字", InputFilterFormat.text, "", KeyAssert);
  468. let filters = [channel, talker, key];
  469. const intro = `// 新聊天信息触发器
  470. // 聊天信息内容:(content)
  471. // 发言人:(name)
  472. // 频道:(channel)`;
  473. const t = new TriggerTemplate("新聊天信息", filters, intro);
  474. TriggerTemplateCenter.add(t);
  475.  
  476. const run = function() {
  477. WG.add_hook("msg", data => {
  478. if (data.ch == null || data.content == null) return;
  479. const types = {
  480. "chat": "世界",
  481. "tm": "队伍",
  482. "fam": "门派",
  483. "es": "全区",
  484. "pty": "帮派",
  485. "rumor": "谣言",
  486. "sys": "系统"
  487. };
  488. const channel = types[data.ch];
  489. if (channel == null) return;
  490. const name = data.name == null ? "无" : data.name;
  491. let params = {
  492. "频道": channel,
  493. "发言人": name,
  494. "关键字": data.content
  495. };
  496. params["content"] = data.content;
  497. params["name"] = name;
  498. params["channel"] = channel;
  499. const n = new Notification("新聊天信息", params);
  500. NotificationCenter.post(n);
  501. });
  502. };
  503. const monitor = new Monitor(run);
  504. MonitorCenter.addMonitor(monitor);
  505. })();
  506.  
  507. //---------------------------------------------------------------------------
  508. // item add
  509. //---------------------------------------------------------------------------
  510.  
  511. (function() {
  512. const name = new InputFilter("人物名称", InputFilterFormat.text, "", KeyAssert);
  513. name.description("人名关键字");
  514. let filters = [name];
  515. const intro = `// 人物刷新触发器
  516. // 刷新人物id:(id)
  517. // 刷新人物名称:(name)`;
  518. const t = new TriggerTemplate("人物刷新", filters, intro);
  519. TriggerTemplateCenter.add(t);
  520.  
  521. const run = function() {
  522. WG.add_hook("itemadd", data => {
  523. if (data.name == null || data.id == null) return;
  524. let params = {
  525. "人物名称": data.name,
  526. };
  527. params["id"] = data.id;
  528. params["name"] = data.name;
  529. const n = new Notification("人物刷新", params);
  530. NotificationCenter.post(n);
  531. });
  532. };
  533. const monitor = new Monitor(run);
  534. MonitorCenter.addMonitor(monitor);
  535. })();
  536.  
  537. //---------------------------------------------------------------------------
  538. // dialog pack
  539. //---------------------------------------------------------------------------
  540.  
  541. (function() {
  542. const name = new InputFilter("名称关键字", InputFilterFormat.text, "", KeyAssert);
  543. let filters = [name];
  544. const intro = `// 物品拾取触发器
  545. // 拾取物品id:(id)
  546. // 拾取物品名称:(name)
  547. // 拾取物品数量:(count)
  548. // 物品品质:(quality) 值:白、绿、蓝、黄、紫、橙、红、未知`;
  549. const t = new TriggerTemplate("物品拾取", filters, intro);
  550. TriggerTemplateCenter.add(t);
  551.  
  552. const run = function() {
  553. WG.add_hook("dialog", function(data) {
  554. if (data.dialog != "pack" || data.id == null || data.name == null || data.count == null || data.remove != null) return;
  555. let params = {
  556. "名称关键字": data.name,
  557. };
  558. params["id"] = data.id;
  559. params["name"] = data.name;
  560. params["count"] = data.count;
  561. let quality = "未知";
  562. const tag = /<\w{3}>/.exec(data.name)[0];
  563. const tagMap = {
  564. "<wht>": "白",
  565. "<hig>": "绿",
  566. "<hic>": "蓝",
  567. "<hiy>": "黄",
  568. "<HIZ>": "紫",
  569. "<hio>": "橙",
  570. "<ord>": "红"
  571. }
  572. quality = tagMap[tag];
  573. params["quality"] = quality;
  574. const n = new Notification("物品拾取", params);
  575. NotificationCenter.post(n);
  576. });
  577. };
  578. const monitor = new Monitor(run);
  579. MonitorCenter.addMonitor(monitor);
  580. })();
  581.  
  582. //---------------------------------------------------------------------------
  583. // text
  584. //---------------------------------------------------------------------------
  585.  
  586. (function() {
  587. const name = new InputFilter("关键字", InputFilterFormat.text, "", KeyAssert);
  588. let filters = [name];
  589. const intro = `// 新提示信息触发器
  590. // 提示信息:(text)`;
  591. const t = new TriggerTemplate("新提示信息", filters, intro);
  592. TriggerTemplateCenter.add(t);
  593.  
  594. const run = function() {
  595. WG.add_hook("text", data => {
  596. if (data.msg == null) return;
  597. let params = {
  598. "关键字": data.msg,
  599. };
  600. params["text"] = data.msg;
  601. const n = new Notification("新提示信息", params);
  602. NotificationCenter.post(n);
  603. });
  604. };
  605. const monitor = new Monitor(run);
  606. MonitorCenter.addMonitor(monitor);
  607. })();
  608.  
  609. //---------------------------------------------------------------------------
  610. // combat
  611. //---------------------------------------------------------------------------
  612.  
  613. (function() {
  614. const type = new SelectFilter("类型", ["进入战斗", "脱离战斗"], 0);
  615. let filters = [type];
  616. const intro = "// 战斗状态切换触发器";
  617. const t = new TriggerTemplate("战斗状态切换", filters, intro);
  618. TriggerTemplateCenter.add(t);
  619.  
  620. const run = function() {
  621. WG.add_hook("combat", data => {
  622. let params = null;
  623. if (data.start != null && data.start == 1) {
  624. params = { "类型": "进入战斗" };
  625. } else if (data.end != null && data.end == 1) {
  626. params = { "类型": "脱离战斗" };
  627. }
  628. const n = new Notification("战斗状态切换", params);
  629. NotificationCenter.post(n);
  630. });
  631. WG.add_hook("text", function(data) {
  632. if (data.msg == null) return;
  633. if (data.msg.indexOf('只能在战斗中使用') != -1 || data.msg.indexOf('这里不允许战斗') != -1 || data.msg.indexOf('没时间这么做') != -1) {
  634. const params = { "类型": "脱离战斗" };
  635. const n = new Notification("战斗状态切换", params);
  636. NotificationCenter.post(n);
  637. }
  638. });
  639. };
  640. const monitor = new Monitor(run);
  641. MonitorCenter.addMonitor(monitor);
  642. })();
  643.  
  644. //---------------------------------------------------------------------------
  645. // combat
  646. //---------------------------------------------------------------------------
  647.  
  648. (function() {
  649. const type = new SelectFilter("类型", ["已经死亡", "已经复活"], 0);
  650. let filters = [type];
  651. const intro = "// 死亡状态改变触发器";
  652. const t = new TriggerTemplate("死亡状态改变", filters, intro);
  653. TriggerTemplateCenter.add(t);
  654.  
  655. const run = function() {
  656. WG.add_hook("die", data => {
  657. const value = data.relive == null ? "已经死亡" : "已经复活";
  658. let params = {
  659. "类型": value
  660. };
  661. const n = new Notification("死亡状态改变", params);
  662. NotificationCenter.post(n);
  663. });
  664. };
  665. const monitor = new Monitor(run);
  666. MonitorCenter.addMonitor(monitor);
  667. })();
  668.  
  669. //---------------------------------------------------------------------------
  670. // time
  671. //---------------------------------------------------------------------------
  672.  
  673. (function() {
  674. const hours = [
  675. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  676. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  677. 20, 21, 22, 23
  678. ];
  679. const minutes = [
  680. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  681. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  682. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  683. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  684. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  685. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59
  686. ];
  687. const hour = new SelectFilter("时", hours, 0, EqualAssert);
  688. const minute = new SelectFilter("分", minutes, 0, EqualAssert);
  689. const second = new SelectFilter("秒", minutes, 0, EqualAssert);
  690. let filters = [hour, minute, second];
  691. const intro = "// 时辰已到触发器";
  692. const t = new TriggerTemplate("时辰已到", filters, intro);
  693. TriggerTemplateCenter.add(t);
  694.  
  695. const run = function() {
  696. setInterval(_ => {
  697. const date = new Date();
  698. const params = {
  699. "时": date.getHours(),
  700. "分": date.getMinutes(),
  701. "秒": date.getSeconds()
  702. };
  703. const n = new Notification("时辰已到", params);
  704. NotificationCenter.post(n);
  705. }, 1000);
  706. };
  707. const monitor = new Monitor(run);
  708. MonitorCenter.addMonitor(monitor);
  709. })();
  710.  
  711. //---------------------------------------------------------------------------
  712. // dispfm
  713. //---------------------------------------------------------------------------
  714.  
  715. (function() {
  716. const sid = new InputFilter("技能id", InputFilterFormat.text, "", ContainAssert);
  717. let filters = [sid];
  718. const intro = `// 技能释放触发器
  719. // 技能id:(id)
  720. // 出招时间:(rtime)
  721. // 冷却时间:(distime)`;
  722. const t = new TriggerTemplate("技能释放", filters, intro);
  723. TriggerTemplateCenter.add(t);
  724.  
  725. const sid1 = new InputFilter("技能id", InputFilterFormat.text, "", ContainAssert);
  726. let filters1 = [sid1];
  727. const intro1 = `// 技能冷却结束触发器
  728. // 技能id:(id)`;
  729. const t1 = new TriggerTemplate("技能冷却结束", filters1, intro1);
  730. TriggerTemplateCenter.add(t1);
  731.  
  732. const run = function() {
  733. WG.add_hook("dispfm", data => {
  734. if (data.id == null || data.distime == null || data.rtime == null) return;
  735. let params = {
  736. "技能id": data.id
  737. };
  738. params["id"] = data.id;
  739. params["rtime"] = data.rtime;
  740. params["distime"] = data.distime;
  741. const n = new Notification("技能释放", params);
  742. NotificationCenter.post(n);
  743.  
  744. setTimeout(_ => {
  745. let params = {
  746. "技能id": data.id
  747. };
  748. params["id"] = data.id;
  749. const n = new Notification("技能冷却结束", params);
  750. NotificationCenter.post(n);
  751. }, data.distime);
  752. });
  753. };
  754. const monitor = new Monitor(run);
  755. MonitorCenter.addMonitor(monitor);
  756. })();
  757.  
  758. //---------------------------------------------------------------------------
  759. // hp mp
  760. //---------------------------------------------------------------------------
  761.  
  762. var RoomItems = {};
  763.  
  764. (function() {
  765. const name = new InputFilter("人名关键字", InputFilterFormat.text, "", KeyAssert);
  766. const type = new SelectFilter("类型", ["气血", "内力"], 0, EqualAssert);
  767. const compare = new SelectFilter("当", ["低于", "高于"], 0, EqualAssert);
  768. const valueType = new SelectFilter("值类型", ["百分比", "数值"], 0, EqualAssert);
  769. const value = new InputFilter("值", InputFilterFormat.number, 0, function(fromUser, fromGame) {
  770. const parts = fromGame.split(";");
  771. const oldvalue = parseFloat(parts[0]);
  772. const newvalue = parseFloat(parts[1]);
  773. if (oldvalue >= fromUser && newvalue < fromUser) return true;
  774. if (oldvalue <= fromUser && newvalue > fromUser) return true;
  775. return false;
  776. });
  777. let filters = [name, type, compare, valueType, value];
  778. const intro = `// 气血内力改变触发器
  779. // 人物id:(id)
  780. // 人物当前气血:(hp)
  781. // 人物最大气血:(maxHp)
  782. // 人物当前内力:(mp)
  783. // 人物最大内力:(maxMp)`;
  784. const t = new TriggerTemplate("气血内力改变", filters, intro);
  785. TriggerTemplateCenter.add(t);
  786.  
  787. const run = function() {
  788. WG.add_hook("items", data => {
  789. if (data.items == null) return;
  790. RoomItems = {};
  791. for (const item of data.items) {
  792. RoomItems[item.id] = CopyObject(item);
  793. }
  794. });
  795. WG.add_hook("itemadd", data => {
  796. RoomItems[data.id] = CopyObject(data);
  797. });
  798. const decorate = function(params, item) {
  799. params["id"] = item.id;
  800. params["hp"] = item.hp;
  801. params["maxHp"] = item.max_hp;
  802. params["mp"] = item.mp;
  803. params["maxMp"] = item.max_mp;
  804. };
  805. WG.add_hook("sc", data => {
  806. if (data.id == null) return;
  807. let item = RoomItems[data.id];
  808. if (item == null) return;
  809. if (data.hp != null) {
  810. let compare = "低于";
  811. if (data.hp > item.hp) compare = "高于";
  812. const oldValue = item.hp;
  813. const oldPer = (item.hp/item.max_hp*100).toFixed(2);
  814. item.hp = data.hp;
  815. if (item.max_hp < item.hp) item.max_hp = item.hp;
  816. if (data.max_hp != null) item.max_hp = data.max_hp;
  817. const newValue = item.hp;
  818. const newPer = (item.hp/item.max_hp*100).toFixed(2);
  819. let params1 = {
  820. "人名关键字": item.name,
  821. "类型": "气血",
  822. "当": compare,
  823. "值类型": "百分比",
  824. "值": `${oldPer};${newPer}`
  825. };
  826. decorate(params1, item);
  827. const n1 = new Notification("气血内力改变", params1);
  828. NotificationCenter.post(n1);
  829. let params2 = {
  830. "人名关键字": item.name,
  831. "类型": "气血",
  832. "当": compare,
  833. "值类型": "数值",
  834. "值": `${oldValue};${newValue}`
  835. };
  836. decorate(params2, item);
  837. const n2 = new Notification("气血内力改变", params2);
  838. NotificationCenter.post(n2);
  839. }
  840. if (data.mp != null) {
  841. let compare = "低于";
  842. if (data.mp > item.mp) compare = "高于";
  843. const oldValue = item.mp;
  844. const oldPer = (item.mp/item.max_mp*100).toFixed(2);
  845. item.mp = data.mp;
  846. if (item.max_mp < item.mp) item.max_mp = item.mp;
  847. if (data.max_mp != null) item.max_mp = data.max_mp;
  848. const newValue = item.mp;
  849. const newPer = (item.mp/item.max_mp*100).toFixed(2);
  850. let params1 = {
  851. "人名关键字": item.name,
  852. "类型": "内力",
  853. "当": compare,
  854. "值类型": "百分比",
  855. "值": `${oldPer};${newPer}`
  856. };
  857. decorate(params1, item);
  858. const n1 = new Notification("气血内力改变", params1);
  859. NotificationCenter.post(n1);
  860. let params2 = {
  861. "人名关键字": item.name,
  862. "类型": "内力",
  863. "当": compare,
  864. "值类型": "数值",
  865. "值": `${oldValue};${newValue}`
  866. };
  867. decorate(params2, item);
  868. const n2 = new Notification("气血内力改变", params2);
  869. NotificationCenter.post(n2);
  870. }
  871. });
  872. };
  873. const monitor = new Monitor(run);
  874. MonitorCenter.addMonitor(monitor);
  875. })();
  876.  
  877. //---------------------------------------------------------------------------
  878. // damage
  879. //---------------------------------------------------------------------------
  880.  
  881. (function() {
  882. const name = new InputFilter("人名关键字", InputFilterFormat.text, "", KeyAssert);
  883. const valueType = new SelectFilter("值类型", ["百分比", "数值"], 0, EqualAssert);
  884. const value = new InputFilter("值", InputFilterFormat.number, 0, (fromUser, fromGame) => {
  885. const parts = fromGame.split(";");
  886. const oldvalue = parseFloat(parts[0]);
  887. const newvalue = parseFloat(parts[1]);
  888. if (oldvalue <= fromUser && newvalue > fromUser) return true;
  889. return false;
  890. });
  891. let filters = [name, valueType, value];
  892. const intro = `// 伤害已满触发器
  893. // 备注:限制条件-值 不支持多条件
  894. // 人物id:(id)
  895. // 人物名称:(name)
  896. // 伤害数值:(value)
  897. // 伤害百分比:(percent)`;
  898. const t = new TriggerTemplate("伤害已满", filters, intro);
  899. TriggerTemplateCenter.add(t);
  900.  
  901. const run = function() {
  902. const decorate = function(params, item, value, percent) {
  903. params["id"] = item.id;
  904. params["name"] = item.name;
  905. params["value"] = value;
  906. params["percent"] = percent;
  907. };
  908. WG.add_hook("sc", data => {
  909. if (data.id == null || data.damage == null) return;
  910. let item = RoomItems[data.id];
  911. if (item == null || item.id == null || item.name == null || item.max_hp == null) return;
  912. // 获取之前保存的伤害和伤害百分比
  913. const oldValue = item._damage == null ? 0 : item._damage;
  914. const oldPer = item._damagePer == null ? 0 : item._damagePer;
  915. const value = data.damage;
  916. const percent = (data.damage/item.max_hp*100).toFixed(2);
  917. // 保存伤害和伤害百分比
  918. item._damage = value;
  919. item._damagePer = percent;
  920. let params1 = {
  921. "人名关键字": item.name,
  922. "值类型": "百分比",
  923. "值": `${oldPer};${percent}`
  924. };
  925. decorate(params1, item, value, percent);
  926. const n1 = new Notification("伤害已满", params1);
  927. NotificationCenter.post(n1);
  928. let params2 = {
  929. "人名关键字": item.name,
  930. "值类型": "数值",
  931. "值": `${oldValue};${value}`
  932. };
  933. decorate(params2, item, value, percent);
  934. const n2 = new Notification("伤害已满", params2);
  935. NotificationCenter.post(n2);
  936. });
  937. };
  938. const monitor = new Monitor(run);
  939. MonitorCenter.addMonitor(monitor);
  940. })();
  941.  
  942. /***********************************************************************************\
  943. UI
  944. \***********************************************************************************/
  945.  
  946. const Message = {
  947. append: function(msg) {
  948. messageAppend(msg);
  949. },
  950. clean: function() {
  951. messageClear();
  952. },
  953. };
  954.  
  955. const UI = {
  956. triggerHome: function() {
  957. const content = `
  958. <style>.breakText {word-break:keep-all;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}</style>
  959. <span class="zdy-item" style="width:120px" v-for="t in triggers" :style="activeStyle(t)">
  960. <div style="width: 30px; float: left; background-color: rgba(255, 255, 255, 0.31); border-radius: 4px;" v-on:click="editTrigger(t)">⚙</div>
  961. <div class="breakText" style="width: 85px; float: right;" v-on:click="switchStatus(t)">{{ t.name }}</div>
  962. </span>
  963. `;
  964. const rightText = "<span v-on:click='createTrigger()'><wht>新建</wht></span>";
  965. UI._appendHtml("🍟 <hio>触发器</hio>", content, rightText);
  966. new Vue({
  967. el: '#app',
  968. data: {
  969. triggers: TriggerCenter.getAll()
  970. },
  971. methods: {
  972. switchStatus: function(t) {
  973. if (t.active()) {
  974. TriggerCenter.deactivate(t.name);
  975. } else {
  976. TriggerCenter.activate(t.name);
  977. }
  978. UI.triggerHome();
  979. },
  980. editTrigger: UI.editTrigger,
  981. activeStyle: function(t) {
  982. if (t.active()) {
  983. return {
  984. "background-color": "#a0e6e0",
  985. "border": "1px solid #7284ff",
  986. "color": "#001bff"
  987. };
  988. } else {
  989. return { "background-color": "none" };
  990. }
  991. },
  992. createTrigger: UI.selectTriggerTemplate
  993. }
  994. });
  995. },
  996. selectTriggerTemplate: function() {
  997. const content = `
  998. <span class="zdy-item" style="width:120px" v-for="t in templates" v-on:click="select(t)">{{ t.event }}</span>
  999. `;
  1000. const leftText = "<span v-on:click='back()'>< 返回</span>";
  1001. UI._appendHtml("<wht>选择触发事件</wht>", content, null, leftText);
  1002. new Vue({
  1003. el: '#app',
  1004. data: {
  1005. templates: TriggerTemplateCenter.getAll()
  1006. },
  1007. methods: {
  1008. select: UI.createTrigger,
  1009. back: UI.triggerHome
  1010. }
  1011. });
  1012. },
  1013. createTrigger: function(template) {
  1014. UI._updateTrigger(template);
  1015. },
  1016. editTrigger: function(trigger) {
  1017. UI._updateTrigger(trigger.template, trigger);
  1018. },
  1019. _updateTrigger: function(template, trigger) {
  1020. const content = `
  1021. <div style="margin:0 2em 0 2em">
  1022. <div style="float:left;width:120px">
  1023. <span class="zdy-item" style="width:90px" v-for="f in filters">
  1024. <p style="margin:0"><wht>{{ f.description() }}</wht></p>
  1025. <input v-if="f.type=='input'" style="width:80%" v-model="conditions[f.name]">
  1026. <select v-if="f.type=='select'" v-model="conditions[f.name]">
  1027. <option v-for="opt in f.options" :value="opt">{{ opt }}</option>
  1028. </select>
  1029. </span>
  1030. </div>
  1031. <div style="float:right;width:calc(100% - 125px)">
  1032. <textarea class = "settingbox hide" style = "height:10rem;display:inline-block;font-size:0.8em;width:100%" v-model="source"></textarea>
  1033. <span class="raid-item shareTrigger" v-if="canShared" v-on:click="share()">分享此触发器</span>
  1034. </div>
  1035. </div>
  1036. `;
  1037. const title = `<input style='width:110px' type="text" placeholder="输入触发器名称" v-model="name">`;
  1038. let rightText = "<span v-on:click='save'><wht>保存</wht></span>";
  1039. if (trigger) {
  1040. rightText = "<span v-on:click='remove'>删除</span>"
  1041. }
  1042. let leftText = "<span v-on:click='back'>< 返回</span>";
  1043. if (trigger) {
  1044. leftText = "<span v-on:click='saveback'>< 保存&返回</span>"
  1045. }
  1046. UI._appendHtml(title, content, rightText, leftText);
  1047. let conditions = {};
  1048. if (trigger != null) {
  1049. conditions = trigger.conditions;
  1050. } else {
  1051. for (const f of template.filters) {
  1052. conditions[f.name] = f.defaultValue;
  1053. }
  1054. }
  1055. let source = template.introdution;
  1056. if (trigger != null) source = trigger.source;
  1057. new Vue({
  1058. el: '#app',
  1059. data: {
  1060. filters: template.filters,
  1061. name: trigger ? trigger.name : "",
  1062. conditions: conditions,
  1063. source: source,
  1064. canShared: trigger != null
  1065. },
  1066. methods: {
  1067. save: function() {
  1068. const result = TriggerCenter.create(this.name, template.event, this.conditions, this.source);
  1069. if (result == true) {
  1070. UI.triggerHome();
  1071. } else {
  1072. alert(result);
  1073. }
  1074. },
  1075. remove: function() {
  1076. const verify = confirm("确认删除此触发器吗?");
  1077. if (verify) {
  1078. TriggerCenter.remove(trigger.name);
  1079. UI.triggerHome();
  1080. }
  1081. },
  1082. back: function() {
  1083. UI.selectTriggerTemplate();
  1084. },
  1085. saveback: function() {
  1086. const result = TriggerCenter.modify(trigger.name, this.name, this.conditions, this.source);
  1087. if (result == true) {
  1088. UI.triggerHome();
  1089. } else {
  1090. alert(result);
  1091. }
  1092. },
  1093.  
  1094. share: function() {
  1095. ToRaid.shareTrigger(TriggerCenter._getData(trigger.name));
  1096. }
  1097. }
  1098. })
  1099. },
  1100.  
  1101. _appendHtml: function(title, content, rightText, leftText) {
  1102. var realLeftText = leftText == null ? "" : leftText;
  1103. var realRightText = rightText == null ? "" : rightText;
  1104. var html = `
  1105. <div class = "item-commands" style="text-align:center" id="app">
  1106. <div style="margin-top:0.5em">
  1107. <div style="width:8em;float:left;text-align:left;padding:0px 0px 0px 2em;height:1.23em" id="wsmud_raid_left">${realLeftText}</div>
  1108. <div style="width:calc(100% - 16em);float:left;height:1.23em">${title}</div>
  1109. <div style="width:8em;float:left;text-align:right;padding:0px 2em 0px 0px;height:1.23em" id="wsmud_raid_right">${realRightText}</div>
  1110. </div>
  1111. <br><br>
  1112. ${content}
  1113. </div>`;
  1114. Message.clean();
  1115. Message.append(html);
  1116. },
  1117. };
  1118.  
  1119. /***********************************************************************************\
  1120. Trigger Config
  1121. \***********************************************************************************/
  1122.  
  1123. const TriggerConfig = {
  1124. get: function() {
  1125. let all = {};
  1126. let keys = GM_listValues();
  1127. keys.forEach(key => {
  1128. all[key] = GM_getValue(key);
  1129. });
  1130. return all;
  1131. },
  1132. set: function(config) {
  1133. for (const key in config) {
  1134. GM_setValue(key, config[key]);
  1135. }
  1136. TriggerCenter.reload();
  1137. }
  1138. };
  1139.  
  1140. /***********************************************************************************\
  1141. Ready
  1142. \***********************************************************************************/
  1143.  
  1144. let Running = false;
  1145.  
  1146. $(document).ready(function () {
  1147. __init__();
  1148. if (WG == undefined || WG == null || ToRaid == undefined || ToRaid == null) {
  1149. setTimeout(__init__, 300);
  1150. }
  1151. });
  1152.  
  1153. function __init__(){
  1154. WG = unsafeWindow.WG;
  1155.  
  1156. messageAppend = unsafeWindow.messageAppend;
  1157. messageClear = unsafeWindow.messageClear;
  1158. ToRaid = unsafeWindow.ToRaid;
  1159.  
  1160. if (WG == undefined || WG == null || ToRaid == undefined || ToRaid == null) {
  1161. setTimeout(()=>{__init__()}, 300);
  1162. return;
  1163. }
  1164. Role = unsafeWindow.Role;
  1165.  
  1166. unsafeWindow.TriggerUI = UI;
  1167. unsafeWindow.TriggerConfig = TriggerConfig;
  1168. unsafeWindow.TriggerCenter = TriggerCenter;
  1169.  
  1170. WG.add_hook("login", function (data) {
  1171. if (Running) return;
  1172. Running = true;
  1173.  
  1174. TriggerCenter.run();
  1175. MonitorCenter.run();
  1176. });
  1177. }
  1178. })();