wsmud_Trigger

武神传说 MUD

目前为 2019-04-22 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name wsmud_Trigger
  3. // @namespace cqv3
  4. // @version 0.0.35
  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://*.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. const event = trigger.event();
  301. if (originalName == name) {
  302. const data = new TriggerData(name, event, conditions, source, trigger.active());
  303. this._updateData(data);
  304. this._reloadTrigger(name);
  305. return true;
  306. }
  307.  
  308. const result = this.create(name, event, conditions, source);
  309. if (result == true) {
  310. this.remove(originalName);
  311. this._loadTrigger(name);
  312. }
  313. return result;
  314. },
  315. remove: function(name) {
  316. const trigger = this._triggers[name];
  317. if (trigger == null) return;
  318.  
  319. trigger._deactivate();
  320. delete this._triggers[name];
  321. let allData = GM_getValue(this._saveKey(), {});
  322. delete allData[name];
  323. GM_setValue(this._saveKey(), allData);
  324. },
  325.  
  326. activate: function(name) {
  327. const trigger = this._triggers[name];
  328. if (trigger == null) return;
  329. if (trigger.active()) return;
  330. trigger._activate();
  331. let data = this._getData(name);
  332. data.active = true;
  333. this._updateData(data);
  334. },
  335. deactivate: function(name) {
  336. const trigger = this._triggers[name];
  337. if (trigger == null) return;
  338. if (!trigger.active()) return;
  339. trigger._deactivate();
  340. let data = this._getData(name);
  341. data.active = false;
  342. this._updateData(data);
  343. },
  344.  
  345. _triggers: {},
  346.  
  347. _saveKey: function() {
  348. return `${Role.id}@triggers`;
  349. },
  350. _reloadTrigger: function(name) {
  351. const oldTrigger = this._triggers[name];
  352. if (oldTrigger != null) {
  353. oldTrigger._deactivate();
  354. }
  355. this._loadTrigger(name);
  356. },
  357. _loadTrigger: function(name) {
  358. const data = this._getData(name);
  359. if (data == null) return;
  360. const trigger = this._toTrigger(data);
  361. this._triggers[name] = trigger;
  362. if (data.active) {
  363. trigger._activate();
  364. }
  365. },
  366. _getData: function(name) {
  367. let allData = GM_getValue(this._saveKey(), {});
  368. const data = allData[name];
  369. return data;
  370. },
  371. _updateData: function(data) {
  372. let allData = GM_getValue(this._saveKey(), {});
  373. allData[data.name] = data;
  374. GM_setValue(this._saveKey(), allData);
  375. },
  376. _toTrigger: function(data) {
  377. const template = TriggerTemplateCenter.get(data.event);
  378. const trigger = new Trigger(data.name, template, data.conditions, data.source);
  379. return trigger;
  380. },
  381. _checkName: function(name) {
  382. if (this._triggers[name] != null) return "无法修改名称,已经存在同名触发器!";
  383. if (!/\S+/.test(name)) return "触发器的名称不能为空。";
  384. if (!/^[_a-zA-Z0-9\u4e00-\u9fa5]+$/.test(name)) return "触发器的名称只能使用中文、英文和数字字符。";
  385. return true;
  386. }
  387. };
  388.  
  389. /***********************************************************************************\
  390. WSMUD
  391. \***********************************************************************************/
  392.  
  393. var WG = null;
  394. var messageAppend = null;
  395. var messageClear = null;
  396. var ToRaid = null;
  397. var Role = null;
  398.  
  399. //---------------------------------------------------------------------------
  400. // status
  401. //---------------------------------------------------------------------------
  402.  
  403. (function() {
  404. const type = new SelectFilter("改变类型", ["新增", "移除", "层数刷新"], 0);
  405. const value = new InputFilter("BuffId", InputFilterFormat.text, "weapon", ContainAssert);
  406. const target = new SelectFilter("触发对象", ["自己", "他人"], 0);
  407. let filters = [type, value, target];
  408. const intro = `// Buff状态改变触发器
  409. // 触发对象id:(id)
  410. // buff的sid:(sid)
  411. // buff层数:(count)`;
  412. const t = new TriggerTemplate("Buff状态改变", filters, intro);
  413. TriggerTemplateCenter.add(t);
  414.  
  415. const run = function() {
  416. const post = function(data, sid, type) {
  417. let params = {
  418. "改变类型": type,
  419. "BuffId": sid,
  420. "触发对象": data.id == Role.id ? "自己" : "他人"
  421. };
  422. params["id"] = data.id;
  423. params["sid"] = sid;
  424. params["count"] = 0;
  425. if (data.count != null) params["count"] = data.count;
  426. const n = new Notification("Buff状态改变", params);
  427. NotificationCenter.post(n);
  428. };
  429. WG.add_hook("status", data => {
  430. if (data.action == null || data.id == null || data.sid == null) return;
  431. const types = {
  432. "add": "新增",
  433. "remove": "移除",
  434. "refresh": "层数刷新"
  435. };
  436. const type = types[data.action];
  437. if (type == null) return;
  438. if (data.sid instanceof Array) {
  439. for (const s of data.sid) {
  440. post(data, s, type);
  441. }
  442. } else {
  443. post(data, data.sid, type);
  444. }
  445. });
  446. };
  447. const monitor = new Monitor(run);
  448. MonitorCenter.addMonitor(monitor);
  449. })();
  450.  
  451. //---------------------------------------------------------------------------
  452. // msg
  453. //---------------------------------------------------------------------------
  454.  
  455. (function() {
  456. const chanel = new SelectFilter(
  457. "频道",
  458. ["全部", "世界", "队伍", "门派", "全区", "帮派", "谣言", "系统"],
  459. 0,
  460. function(fromUser, fromGame) {
  461. if (fromUser == "全部") return true;
  462. return fromUser == fromGame;
  463. }
  464. );
  465. const talker = new InputFilter("发言人", InputFilterFormat.text, "", ContainAssert);
  466. const key = new InputFilter("关键字", InputFilterFormat.text, "", KeyAssert);
  467. let filters = [chanel, talker, key];
  468. const intro = `// 新聊天信息触发器
  469. // 聊天信息内容:(content)
  470. // 发言人:(name)`;
  471. const t = new TriggerTemplate("新聊天信息", filters, intro);
  472. TriggerTemplateCenter.add(t);
  473.  
  474. const run = function() {
  475. WG.add_hook("msg", data => {
  476. if (data.ch == null || data.content == null) return;
  477. const types = {
  478. "chat": "世界",
  479. "tm": "队伍",
  480. "fam": "门派",
  481. "es": "全区",
  482. "pty": "帮派",
  483. "rumor": "谣言",
  484. "sys": "系统"
  485. };
  486. const chanel = types[data.ch];
  487. if (chanel == null) return;
  488. const name = data.name == null ? "无" : data.name;
  489. let params = {
  490. "频道": chanel,
  491. "发言人": name,
  492. "关键字": data.content
  493. };
  494. params["content"] = data.content;
  495. params["name"] = name;
  496. const n = new Notification("新聊天信息", params);
  497. NotificationCenter.post(n);
  498. });
  499. };
  500. const monitor = new Monitor(run);
  501. MonitorCenter.addMonitor(monitor);
  502. })();
  503.  
  504. //---------------------------------------------------------------------------
  505. // item add
  506. //---------------------------------------------------------------------------
  507.  
  508. (function() {
  509. const name = new InputFilter("人物名称", InputFilterFormat.text, "", KeyAssert);
  510. name.description("人名关键字");
  511. let filters = [name];
  512. const intro = `// 人物刷新触发器
  513. // 刷新人物id:(id)
  514. // 刷新人物名称:(name)`;
  515. const t = new TriggerTemplate("人物刷新", filters, intro);
  516. TriggerTemplateCenter.add(t);
  517.  
  518. const run = function() {
  519. WG.add_hook("itemadd", data => {
  520. if (data.name == null || data.id == null) return;
  521. let params = {
  522. "人物名称": data.name,
  523. };
  524. params["id"] = data.id;
  525. params["name"] = data.name;
  526. const n = new Notification("人物刷新", params);
  527. NotificationCenter.post(n);
  528. });
  529. };
  530. const monitor = new Monitor(run);
  531. MonitorCenter.addMonitor(monitor);
  532. })();
  533.  
  534. //---------------------------------------------------------------------------
  535. // dialog pack
  536. //---------------------------------------------------------------------------
  537.  
  538. (function() {
  539. const name = new InputFilter("名称关键字", InputFilterFormat.text, "", KeyAssert);
  540. let filters = [name];
  541. const intro = `// 物品拾取触发器
  542. // 拾取物品id:(id)
  543. // 拾取物品名称:(name)
  544. // 拾取物品数量:(count)
  545. // 物品品质:(quality) 值:白、绿、蓝、黄、紫、橙、红、未知`;
  546. const t = new TriggerTemplate("物品拾取", filters, intro);
  547. TriggerTemplateCenter.add(t);
  548.  
  549. const run = function() {
  550. WG.add_hook("dialog", function(data) {
  551. if (data.dialog != "pack" || data.id == null || data.name == null || data.count == null || data.remove != null) return;
  552. let params = {
  553. "名称关键字": data.name,
  554. };
  555. params["id"] = data.id;
  556. params["name"] = data.name;
  557. params["count"] = data.count;
  558. let quality = "未知";
  559. const tag = /<\w{3}>/.exec(data.name)[0];
  560. const tagMap = {
  561. "<wht>": "白",
  562. "<hig>": "绿",
  563. "<hic>": "蓝",
  564. "<hiy>": "黄",
  565. "<HIZ>": "紫",
  566. "<hio>": "橙",
  567. "<ord>": "红"
  568. }
  569. quality = tagMap[tag];
  570. params["quality"] = quality;
  571. const n = new Notification("物品拾取", params);
  572. NotificationCenter.post(n);
  573. });
  574. };
  575. const monitor = new Monitor(run);
  576. MonitorCenter.addMonitor(monitor);
  577. })();
  578.  
  579. //---------------------------------------------------------------------------
  580. // text
  581. //---------------------------------------------------------------------------
  582.  
  583. (function() {
  584. const name = new InputFilter("关键字", InputFilterFormat.text, "", KeyAssert);
  585. let filters = [name];
  586. const intro = `// 新提示信息触发器
  587. // 提示信息:(text)`;
  588. const t = new TriggerTemplate("新提示信息", filters, intro);
  589. TriggerTemplateCenter.add(t);
  590.  
  591. const run = function() {
  592. WG.add_hook("text", data => {
  593. if (data.msg == null) return;
  594. let params = {
  595. "关键字": data.msg,
  596. };
  597. params["text"] = data.msg;
  598. const n = new Notification("新提示信息", params);
  599. NotificationCenter.post(n);
  600. });
  601. };
  602. const monitor = new Monitor(run);
  603. MonitorCenter.addMonitor(monitor);
  604. })();
  605.  
  606. //---------------------------------------------------------------------------
  607. // combat
  608. //---------------------------------------------------------------------------
  609.  
  610. (function() {
  611. const type = new SelectFilter("类型", ["进入战斗", "脱离战斗"], 0);
  612. let filters = [type];
  613. const intro = "// 战斗状态切换触发器";
  614. const t = new TriggerTemplate("战斗状态切换", filters, intro);
  615. TriggerTemplateCenter.add(t);
  616.  
  617. const run = function() {
  618. WG.add_hook("combat", data => {
  619. let params = null;
  620. if (data.start != null && data.start == 1) {
  621. params = { "类型": "进入战斗" };
  622. } else if (data.end != null && data.end == 1) {
  623. params = { "类型": "脱离战斗" };
  624. }
  625. const n = new Notification("战斗状态切换", params);
  626. NotificationCenter.post(n);
  627. });
  628. WG.add_hook("text", function(data) {
  629. if (data.msg == null) return;
  630. if (data.msg.indexOf('只能在战斗中使用') != -1 || data.msg.indexOf('这里不允许战斗') != -1 || data.msg.indexOf('没时间这么做') != -1) {
  631. const params = { "类型": "脱离战斗" };
  632. const n = new Notification("战斗状态切换", params);
  633. NotificationCenter.post(n);
  634. }
  635. });
  636. };
  637. const monitor = new Monitor(run);
  638. MonitorCenter.addMonitor(monitor);
  639. })();
  640.  
  641. //---------------------------------------------------------------------------
  642. // combat
  643. //---------------------------------------------------------------------------
  644.  
  645. (function() {
  646. const type = new SelectFilter("类型", ["已经死亡", "已经复活"], 0);
  647. let filters = [type];
  648. const intro = "// 死亡状态改变触发器";
  649. const t = new TriggerTemplate("死亡状态改变", filters, intro);
  650. TriggerTemplateCenter.add(t);
  651.  
  652. const run = function() {
  653. WG.add_hook("die", data => {
  654. const value = data.relive == null ? "已经死亡" : "已经复活";
  655. let params = {
  656. "类型": value
  657. };
  658. const n = new Notification("死亡状态改变", params);
  659. NotificationCenter.post(n);
  660. });
  661. };
  662. const monitor = new Monitor(run);
  663. MonitorCenter.addMonitor(monitor);
  664. })();
  665.  
  666. //---------------------------------------------------------------------------
  667. // time
  668. //---------------------------------------------------------------------------
  669.  
  670. (function() {
  671. const hours = [
  672. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  673. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  674. 20, 21, 22, 23
  675. ];
  676. const minutes = [
  677. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  678. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  679. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  680. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  681. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  682. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59
  683. ];
  684. const hour = new SelectFilter("时", hours, 0, EqualAssert);
  685. const minute = new SelectFilter("分", minutes, 0, EqualAssert);
  686. const second = new SelectFilter("秒", minutes, 0, EqualAssert);
  687. let filters = [hour, minute, second];
  688. const intro = "// 时辰已到触发器";
  689. const t = new TriggerTemplate("时辰已到", filters, intro);
  690. TriggerTemplateCenter.add(t);
  691.  
  692. const run = function() {
  693. setInterval(_ => {
  694. const date = new Date();
  695. const params = {
  696. "时": date.getHours(),
  697. "分": date.getMinutes(),
  698. "秒": date.getSeconds()
  699. };
  700. const n = new Notification("时辰已到", params);
  701. NotificationCenter.post(n);
  702. }, 1000);
  703. };
  704. const monitor = new Monitor(run);
  705. MonitorCenter.addMonitor(monitor);
  706. })();
  707.  
  708. //---------------------------------------------------------------------------
  709. // dispfm
  710. //---------------------------------------------------------------------------
  711.  
  712. (function() {
  713. const sid = new InputFilter("技能id", InputFilterFormat.text, "", ContainAssert);
  714. let filters = [sid];
  715. const intro = `// 技能释放触发器
  716. // 技能id:(id)
  717. // 出招时间:(rtime)
  718. // 冷却时间:(distime)`;
  719. const t = new TriggerTemplate("技能释放", filters, intro);
  720. TriggerTemplateCenter.add(t);
  721.  
  722. const sid1 = new InputFilter("技能id", InputFilterFormat.text, "", ContainAssert);
  723. let filters1 = [sid1];
  724. const intro1 = `// 技能冷却结束触发器
  725. // 技能id:(id)`;
  726. const t1 = new TriggerTemplate("技能冷却结束", filters1, intro1);
  727. TriggerTemplateCenter.add(t1);
  728.  
  729. const run = function() {
  730. WG.add_hook("dispfm", data => {
  731. if (data.id == null || data.distime == null || data.rtime == null) return;
  732. let params = {
  733. "技能id": data.id
  734. };
  735. params["id"] = data.id;
  736. params["rtime"] = data.rtime;
  737. params["distime"] = data.distime;
  738. const n = new Notification("技能释放", params);
  739. NotificationCenter.post(n);
  740.  
  741. setTimeout(_ => {
  742. let params = {
  743. "技能id": data.id
  744. };
  745. params["id"] = data.id;
  746. const n = new Notification("技能冷却结束", params);
  747. NotificationCenter.post(n);
  748. }, data.distime);
  749. });
  750. };
  751. const monitor = new Monitor(run);
  752. MonitorCenter.addMonitor(monitor);
  753. })();
  754.  
  755. //---------------------------------------------------------------------------
  756. // hp mp
  757. //---------------------------------------------------------------------------
  758.  
  759. var RoomItems = {};
  760.  
  761. (function() {
  762. const name = new InputFilter("人名关键字", InputFilterFormat.text, "", KeyAssert);
  763. const type = new SelectFilter("类型", ["气血", "内力"], 0, EqualAssert);
  764. const compare = new SelectFilter("当", ["低于", "高于"], 0, EqualAssert);
  765. const valueType = new SelectFilter("值类型", ["百分比", "数值"], 0, EqualAssert);
  766. const value = new InputFilter("值", InputFilterFormat.number, 0, function(fromUser, fromGame) {
  767. const parts = fromGame.split(";");
  768. const oldvalue = parseFloat(parts[0]);
  769. const newvalue = parseFloat(parts[1]);
  770. if (oldvalue >= fromUser && newvalue < fromUser) return true;
  771. if (oldvalue <= fromUser && newvalue > fromUser) return true;
  772. return false;
  773. });
  774. let filters = [name, type, compare, valueType, value];
  775. const intro = `// 气血内力改变触发器
  776. // 人物id:(id)
  777. // 人物当前气血:(hp)
  778. // 人物最大气血:(maxHp)
  779. // 人物当前内力:(mp)
  780. // 人物最大内力:(maxMp)`;
  781. const t = new TriggerTemplate("气血内力改变", filters, intro);
  782. TriggerTemplateCenter.add(t);
  783.  
  784. const run = function() {
  785. WG.add_hook("items", data => {
  786. if (data.items == null) return;
  787. RoomItems = {};
  788. for (const item of data.items) {
  789. RoomItems[item.id] = CopyObject(item);
  790. }
  791. });
  792. WG.add_hook("itemadd", data => {
  793. RoomItems[data.id] = CopyObject(data);
  794. });
  795. const decorate = function(params, item) {
  796. params["id"] = item.id;
  797. params["hp"] = item.hp;
  798. params["maxHp"] = item.max_hp;
  799. params["mp"] = item.mp;
  800. params["maxMp"] = item.max_mp;
  801. };
  802. WG.add_hook("sc", data => {
  803. if (data.id == null) return;
  804. let item = RoomItems[data.id];
  805. if (item == null) return;
  806. if (data.hp != null) {
  807. let compare = "低于";
  808. if (data.hp > item.hp) compare = "高于";
  809. const oldValue = item.hp;
  810. const oldPer = (item.hp/item.max_hp*100).toFixed(2);
  811. item.hp = data.hp;
  812. if (item.max_hp < item.hp) item.max_hp = item.hp;
  813. if (data.max_hp != null) item.max_hp = data.max_hp;
  814. const newValue = item.hp;
  815. const newPer = (item.hp/item.max_hp*100).toFixed(2);
  816. let params1 = {
  817. "人名关键字": item.name,
  818. "类型": "气血",
  819. "当": compare,
  820. "值类型": "百分比",
  821. "值": `${oldPer};${newPer}`
  822. };
  823. decorate(params1, item);
  824. const n1 = new Notification("气血内力改变", params1);
  825. NotificationCenter.post(n1);
  826. let params2 = {
  827. "人名关键字": item.name,
  828. "类型": "气血",
  829. "当": compare,
  830. "值类型": "数值",
  831. "值": `${oldValue};${newValue}`
  832. };
  833. decorate(params2, item);
  834. const n2 = new Notification("气血内力改变", params2);
  835. NotificationCenter.post(n2);
  836. }
  837. if (data.mp != null) {
  838. let compare = "低于";
  839. if (data.mp > item.mp) compare = "高于";
  840. const oldValue = item.mp;
  841. const oldPer = (item.mp/item.max_mp*100).toFixed(2);
  842. item.mp = data.mp;
  843. if (item.max_mp < item.mp) item.max_mp = item.mp;
  844. if (data.max_mp != null) item.max_mp = data.max_mp;
  845. const newValue = item.mp;
  846. const newPer = (item.mp/item.max_mp*100).toFixed(2);
  847. let params1 = {
  848. "人名关键字": item.name,
  849. "类型": "内力",
  850. "当": compare,
  851. "值类型": "百分比",
  852. "值": `${oldPer};${newPer}`
  853. };
  854. decorate(params1, item);
  855. const n1 = new Notification("气血内力改变", params1);
  856. NotificationCenter.post(n1);
  857. let params2 = {
  858. "人名关键字": item.name,
  859. "类型": "内力",
  860. "当": compare,
  861. "值类型": "数值",
  862. "值": `${oldValue};${newValue}`
  863. };
  864. decorate(params2, item);
  865. const n2 = new Notification("气血内力改变", params2);
  866. NotificationCenter.post(n2);
  867. }
  868. });
  869. };
  870. const monitor = new Monitor(run);
  871. MonitorCenter.addMonitor(monitor);
  872. })();
  873.  
  874. //---------------------------------------------------------------------------
  875. // damage
  876. //---------------------------------------------------------------------------
  877.  
  878. (function() {
  879. const name = new InputFilter("人名关键字", InputFilterFormat.text, "", KeyAssert);
  880. const valueType = new SelectFilter("值类型", ["百分比", "数值"], 0, EqualAssert);
  881. const value = new InputFilter("值", InputFilterFormat.number, 0, (fromUser, fromGame) => {
  882. const parts = fromGame.split(";");
  883. const oldvalue = parseFloat(parts[0]);
  884. const newvalue = parseFloat(parts[1]);
  885. if (oldvalue <= fromUser && newvalue > fromUser) return true;
  886. return false;
  887. });
  888. let filters = [name, valueType, value];
  889. const intro = `// 伤害已满触发器
  890. // 备注:限制条件-值 不支持多条件
  891. // 人物id:(id)
  892. // 人物名称:(name)
  893. // 伤害数值:(value)
  894. // 伤害百分比:(percent)`;
  895. const t = new TriggerTemplate("伤害已满", filters, intro);
  896. TriggerTemplateCenter.add(t);
  897.  
  898. const run = function() {
  899. const decorate = function(params, item, value, percent) {
  900. params["id"] = item.id;
  901. params["name"] = item.name;
  902. params["value"] = value;
  903. params["percent"] = percent;
  904. };
  905. WG.add_hook("sc", data => {
  906. if (data.id == null || data.damage == null) return;
  907. let item = RoomItems[data.id];
  908. if (item == null || item.id == null || item.name == null || item.max_hp == null) return;
  909. // 获取之前保存的伤害和伤害百分比
  910. const oldValue = item._damage == null ? 0 : item._damage;
  911. const oldPer = item._damagePer == null ? 0 : item._damagePer;
  912. const value = data.damage;
  913. const percent = (data.damage/item.max_hp*100).toFixed(2);
  914. // 保存伤害和伤害百分比
  915. item._damage = value;
  916. item._damagePer = percent;
  917. let params1 = {
  918. "人名关键字": item.name,
  919. "值类型": "百分比",
  920. "值": `${oldPer};${percent}`
  921. };
  922. decorate(params1, item, value, percent);
  923. const n1 = new Notification("伤害已满", params1);
  924. NotificationCenter.post(n1);
  925. let params2 = {
  926. "人名关键字": item.name,
  927. "值类型": "数值",
  928. "值": `${oldValue};${value}`
  929. };
  930. decorate(params2, item, value, percent);
  931. const n2 = new Notification("伤害已满", params2);
  932. NotificationCenter.post(n2);
  933. });
  934. };
  935. const monitor = new Monitor(run);
  936. MonitorCenter.addMonitor(monitor);
  937. })();
  938.  
  939. /***********************************************************************************\
  940. UI
  941. \***********************************************************************************/
  942.  
  943. const Message = {
  944. append: function(msg) {
  945. messageAppend(msg);
  946. },
  947. clean: function() {
  948. messageClear();
  949. },
  950. };
  951.  
  952. const UI = {
  953. triggerHome: function() {
  954. const content = `
  955. <style>.breakText {word-break:keep-all;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}</style>
  956. <span class="zdy-item" style="width:120px" v-for="t in triggers" :style="activeStyle(t)">
  957. <div style="width: 30px; float: left; background-color: rgba(255, 255, 255, 0.31); border-radius: 4px;" v-on:click="editTrigger(t)">⚙</div>
  958. <div class="breakText" style="width: 85px; float: right;" v-on:click="switchStatus(t)">{{ t.name }}</div>
  959. </span>
  960. `;
  961. const rightText = "<span v-on:click='createTrigger()'><wht>新建</wht></span>";
  962. UI._appendHtml("🍟 <hio>触发器</hio>", content, rightText);
  963. new Vue({
  964. el: '#app',
  965. data: {
  966. triggers: TriggerCenter.getAll()
  967. },
  968. methods: {
  969. switchStatus: function(t) {
  970. if (t.active()) {
  971. TriggerCenter.deactivate(t.name);
  972. } else {
  973. TriggerCenter.activate(t.name);
  974. }
  975. UI.triggerHome();
  976. },
  977. editTrigger: UI.editTrigger,
  978. activeStyle: function(t) {
  979. if (t.active()) {
  980. return {
  981. "background-color": "#a0e6e0",
  982. "border": "1px solid #7284ff",
  983. "color": "#001bff"
  984. };
  985. } else {
  986. return { "background-color": "none" };
  987. }
  988. },
  989. createTrigger: UI.selectTriggerTemplate
  990. }
  991. });
  992. },
  993. selectTriggerTemplate: function() {
  994. const content = `
  995. <span class="zdy-item" style="width:120px" v-for="t in templates" v-on:click="select(t)">{{ t.event }}</span>
  996. `;
  997. const leftText = "<span v-on:click='back()'>< 返回</span>";
  998. UI._appendHtml("<wht>选择触发事件</wht>", content, null, leftText);
  999. new Vue({
  1000. el: '#app',
  1001. data: {
  1002. templates: TriggerTemplateCenter.getAll()
  1003. },
  1004. methods: {
  1005. select: UI.createTrigger,
  1006. back: UI.triggerHome
  1007. }
  1008. });
  1009. },
  1010. createTrigger: function(template) {
  1011. UI._updateTrigger(template);
  1012. },
  1013. editTrigger: function(trigger) {
  1014. UI._updateTrigger(trigger.template, trigger);
  1015. },
  1016. _updateTrigger: function(template, trigger) {
  1017. const content = `
  1018. <div style="margin:0 2em 0 2em">
  1019. <div style="float:left;width:120px">
  1020. <span class="zdy-item" style="width:90px" v-for="f in filters">
  1021. <p style="margin:0"><wht>{{ f.description() }}</wht></p>
  1022. <input v-if="f.type=='input'" style="width:80%" v-model="conditions[f.name]">
  1023. <select v-if="f.type=='select'" v-model="conditions[f.name]">
  1024. <option v-for="opt in f.options" :value="opt">{{ opt }}</option>
  1025. </select>
  1026. </span>
  1027. </div>
  1028. <div style="float:right;width:calc(100% - 125px)">
  1029. <textarea class = "settingbox hide" style = "height:10rem;display:inline-block;font-size:0.8em;width:100%" v-model="source"></textarea>
  1030. <span class="raid-item shareTrigger" v-if="canShared" v-on:click="share()">分享此触发器</span>
  1031. </div>
  1032. </div>
  1033. `;
  1034. const title = `<input style='width:110px' type="text" placeholder="输入触发器名称" v-model="name">`;
  1035. let rightText = "<span v-on:click='save'><wht>保存</wht></span>";
  1036. if (trigger) {
  1037. rightText = "<span v-on:click='remove'>删除</span>"
  1038. }
  1039. let leftText = "<span v-on:click='back'>< 返回</span>";
  1040. if (trigger) {
  1041. leftText = "<span v-on:click='saveback'>< 保存&返回</span>"
  1042. }
  1043. UI._appendHtml(title, content, rightText, leftText);
  1044. let conditions = {};
  1045. if (trigger != null) {
  1046. conditions = trigger.conditions;
  1047. } else {
  1048. for (const f of template.filters) {
  1049. conditions[f.name] = f.defaultValue;
  1050. }
  1051. }
  1052. let source = template.introdution;
  1053. if (trigger != null) source = trigger.source;
  1054. new Vue({
  1055. el: '#app',
  1056. data: {
  1057. filters: template.filters,
  1058. name: trigger ? trigger.name : "",
  1059. conditions: conditions,
  1060. source: source,
  1061. canShared: trigger != null
  1062. },
  1063. methods: {
  1064. save: function() {
  1065. const result = TriggerCenter.create(this.name, template.event, this.conditions, this.source);
  1066. if (result == true) {
  1067. UI.triggerHome();
  1068. } else {
  1069. alert(result);
  1070. }
  1071. },
  1072. remove: function() {
  1073. const verify = confirm("确认删除此触发器吗?");
  1074. if (verify) {
  1075. TriggerCenter.remove(trigger.name);
  1076. UI.triggerHome();
  1077. }
  1078. },
  1079. back: function() {
  1080. UI.selectTriggerTemplate();
  1081. },
  1082. saveback: function() {
  1083. const result = TriggerCenter.modify(trigger.name, this.name, this.conditions, this.source);
  1084. if (result == true) {
  1085. UI.triggerHome();
  1086. } else {
  1087. alert(result);
  1088. }
  1089. },
  1090.  
  1091. share: function() {
  1092. ToRaid.shareTrigger(TriggerCenter._getData(trigger.name));
  1093. }
  1094. }
  1095. })
  1096. },
  1097.  
  1098. _appendHtml: function(title, content, rightText, leftText) {
  1099. var realLeftText = leftText == null ? "" : leftText;
  1100. var realRightText = rightText == null ? "" : rightText;
  1101. var html = `
  1102. <div class = "item-commands" style="text-align:center" id="app">
  1103. <div style="margin-top:0.5em">
  1104. <div style="width:8em;float:left;text-align:left;padding:0px 0px 0px 2em;height:1.23em" id="wsmud_raid_left">${realLeftText}</div>
  1105. <div style="width:calc(100% - 16em);float:left;height:1.23em">${title}</div>
  1106. <div style="width:8em;float:left;text-align:right;padding:0px 2em 0px 0px;height:1.23em" id="wsmud_raid_right">${realRightText}</div>
  1107. </div>
  1108. <br><br>
  1109. ${content}
  1110. </div>`;
  1111. Message.clean();
  1112. Message.append(html);
  1113. },
  1114. };
  1115.  
  1116. /***********************************************************************************\
  1117. Trigger Config
  1118. \***********************************************************************************/
  1119.  
  1120. const TriggerConfig = {
  1121. get: function() {
  1122. let all = {};
  1123. let keys = GM_listValues();
  1124. keys.forEach(key => {
  1125. all[key] = GM_getValue(key);
  1126. });
  1127. return all;
  1128. },
  1129. set: function(config) {
  1130. for (const key in config) {
  1131. GM_setValue(key, config[key]);
  1132. }
  1133. TriggerCenter.reload();
  1134. }
  1135. };
  1136.  
  1137. /***********************************************************************************\
  1138. Ready
  1139. \***********************************************************************************/
  1140.  
  1141. let Running = false;
  1142.  
  1143. $(document).ready(function () {
  1144. WG = unsafeWindow.WG;
  1145. messageAppend = unsafeWindow.messageAppend;
  1146. messageClear = unsafeWindow.messageClear;
  1147. ToRaid = unsafeWindow.ToRaid;
  1148. Role = unsafeWindow.Role;
  1149.  
  1150. unsafeWindow.TriggerUI = UI;
  1151. unsafeWindow.TriggerConfig = TriggerConfig;
  1152. unsafeWindow.TriggerCenter = TriggerCenter;
  1153.  
  1154. WG.add_hook("login", function(data) {
  1155. if (Running) return;
  1156. Running = true;
  1157.  
  1158. TriggerCenter.run();
  1159. MonitorCenter.run();
  1160. });
  1161. });
  1162. })();