Greasy Fork 还支持 简体中文。

wsmud_Trigger

武神传说 MUD

目前為 2019-03-28 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name wsmud_Trigger
  3. // @namespace cqv3
  4. // @version 0.0.32
  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. if (/^\s*\*?\s*$/.test(lh)) return true;
  115. const list = lh.split("|");
  116. return list.indexOf(rh) != -1;
  117. };
  118.  
  119. const KeyAssert = function(lh, rh) {
  120. if (/^\s*\*?\s*$/.test(lh)) return true;
  121. const list = lh.split("|");
  122. for (const key of list) {
  123. if (rh.indexOf(key) != -1) return true;
  124. }
  125. return false;
  126. };
  127.  
  128. class Filter {
  129. constructor(name, type, defaultValue, assert) {
  130. this.name = name;
  131. this.type = type;
  132. this.defaultValue = defaultValue;
  133. this.assert = assert == null ? EqualAssert : assert;
  134. }
  135. description(value) {
  136. if (value != null) {
  137. this._desc = value;
  138. return;
  139. }
  140. return this._desc == null ? this.name : this._desc;
  141. }
  142. }
  143.  
  144. class SelectFilter extends Filter {
  145. constructor(name, options, defaultNumber, assert) {
  146. const defaultValue = options[defaultNumber];
  147. super(name, "select", defaultValue, assert);
  148. this.options = options;
  149. }
  150. }
  151.  
  152. const InputFilterFormat = {
  153. number: "数字",
  154. text: "文本"
  155. };
  156.  
  157. class InputFilter extends Filter {
  158. /**
  159. * @param {String} name
  160. * @param {InputFilterFormat} format
  161. * @param {*} defaultValue
  162. */
  163. constructor(name, format, defaultValue, assert) {
  164. super(name, "input", defaultValue, assert);
  165. this.format = format;
  166. }
  167. }
  168.  
  169. class TriggerTemplate {
  170. constructor(event, filters, introdution) {
  171. this.event = event;
  172. this.filters = filters;
  173. this.introdution = `${introdution}\n// 如需更多信息,可以到论坛触发器版块发帖。`;
  174. }
  175. getFilter(name) {
  176. for (const filter of this.filters) {
  177. if (filter.name == name) return filter;
  178. }
  179. return null;
  180. }
  181. }
  182.  
  183. const TriggerTemplateCenter = {
  184. add: function(template) {
  185. this._templates[template.event] = template;
  186. },
  187. getAll: function() {
  188. return Object.values(this._templates);
  189. },
  190. get: function(event) {
  191. return this._templates[event];
  192. },
  193.  
  194. _templates: {},
  195. };
  196.  
  197. //---------------------------------------------------------------------------
  198. // Trigger
  199. //---------------------------------------------------------------------------
  200.  
  201. class Trigger {
  202. constructor(name, template, conditions, source) {
  203. this.name = name;
  204. this.template = template;
  205. this.conditions = conditions;
  206. this.source = source;
  207. this._action = function(params) {
  208. let realParams = CopyObject(params);
  209. for (const key in conditions) {
  210. if (!conditions.hasOwnProperty(key)) continue;
  211. const filter = template.getFilter(key);
  212. const fromUser = conditions[key];
  213. const fromGame = params[key];
  214. if (!filter.assert(fromUser, fromGame)) return;
  215. delete realParams[key];
  216. }
  217. let realSource = source;
  218. for (const key in realParams) {
  219. realSource = `($${key}) = ${realParams[key]}\n${realSource}`;
  220. }
  221. if (/\/\/\s*~silent\s*\n/.test(source) == false) {
  222. realSource = `@print 💡<hio>触发=>${name}</hio>\n${realSource}`;
  223. }
  224. ToRaid.perform(realSource, name, false);
  225. };
  226. this._observerIndex = null;
  227. }
  228.  
  229. event() { return this.template.event; }
  230. active() { return this._observerIndex != null; }
  231.  
  232. _activate() {
  233. if (this._observerIndex != null) return;
  234. this._observerIndex = NotificationCenter.observe(this.template.event, this._action);
  235. }
  236. _deactivate() {
  237. if (this._observerIndex == null) return;
  238. NotificationCenter.removeOberver(this._observerIndex);
  239. this._observerIndex = null;
  240. }
  241. }
  242.  
  243. class TriggerData {
  244. constructor(name, event, conditions, source, active) {
  245. this.name = name;
  246. this.event = event;
  247. this.conditions = conditions;
  248. this.source = source;
  249. this.active = active;
  250. }
  251. }
  252.  
  253. const TriggerCenter = {
  254. run: function() {
  255. const allData = GM_getValue(this._saveKey(), {});
  256. for (const name in allData) {
  257. this._loadTrigger(name);
  258. }
  259. },
  260. reload: function() {
  261. for (const name in this._triggers) {
  262. if (!this._triggers.hasOwnProperty(name)) continue;
  263. const trigger = this._triggers[name];
  264. trigger._deactivate();
  265. delete this._triggers[name];
  266. }
  267. this.run();
  268. },
  269.  
  270. // for upload and download
  271. getAllData: function() {
  272. return GM_getValue(this._saveKey(), {});
  273. },
  274. corver: function(triggerDatas) {
  275. for (const old of this.getAll()) {
  276. this.remove(old.name);
  277. }
  278. for (const name in triggerDatas) {
  279. const trigger = triggerDatas[name];
  280. this.create(trigger.name, trigger.event, trigger.conditions, trigger.source, trigger.active);
  281. }
  282. },
  283.  
  284. getAll: function() {
  285. return Object.values(this._triggers);
  286. },
  287. create: function(name, event, conditions, source, active) {
  288. const checkResult = this._checkName(name);
  289. if (checkResult != true) return checkResult;
  290.  
  291. const theActive = active == null ? false : active;
  292. const data = new TriggerData(name, event, conditions, source, theActive);
  293. this._updateData(data);
  294.  
  295. this._loadTrigger(name);
  296. return true;
  297. },
  298. modify: function(originalName, name, conditions, source) {
  299. const trigger = this._triggers[originalName];
  300. if (trigger == null) return "修改不存在的触发器?";
  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 chanel = 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 = [chanel, talker, key];
  469. const intro = `// 新聊天信息触发器
  470. // 聊天信息内容:(content)
  471. // 发言人:(name)`;
  472. const t = new TriggerTemplate("新聊天信息", filters, intro);
  473. TriggerTemplateCenter.add(t);
  474.  
  475. const run = function() {
  476. WG.add_hook("msg", data => {
  477. if (data.ch == null || data.name == null || data.content == null) return;
  478. const types = {
  479. "chat": "世界",
  480. "tm": "队伍",
  481. "fam": "门派",
  482. "es": "全区",
  483. "pty": "帮派",
  484. "rumor": "谣言",
  485. "sys": "系统"
  486. };
  487. const chanel = types[data.ch];
  488. if (chanel == null) return;
  489. let params = {
  490. "频道": chanel,
  491. "发言人": data.name,
  492. "关键字": data.content
  493. };
  494. params["content"] = data.content;
  495. params["name"] = data.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. </div>
  1031. </div>
  1032. `;
  1033. const title = `<input style='width:110px' type="text" placeholder="输入触发器名称" v-model="name">`;
  1034. let rightText = "<span v-on:click='save'><wht>保存</wht></span>";
  1035. if (trigger) {
  1036. rightText = "<span v-on:click='remove'>删除</span>"
  1037. }
  1038. let leftText = "<span v-on:click='back'>< 返回</span>";
  1039. if (trigger) {
  1040. leftText = "<span v-on:click='saveback'>< 保存&返回</span>"
  1041. }
  1042. UI._appendHtml(title, content, rightText, leftText);
  1043. let conditions = {};
  1044. if (trigger != null) {
  1045. conditions = trigger.conditions;
  1046. } else {
  1047. for (const f of template.filters) {
  1048. conditions[f.name] = f.defaultValue;
  1049. }
  1050. }
  1051. let source = template.introdution;
  1052. if (trigger != null) source = trigger.source;
  1053. new Vue({
  1054. el: '#app',
  1055. data: {
  1056. filters: template.filters,
  1057. name: trigger ? trigger.name : "",
  1058. conditions: conditions,
  1059. source: source
  1060. },
  1061. methods: {
  1062. save: function() {
  1063. const result = TriggerCenter.create(this.name, template.event, this.conditions, this.source);
  1064. if (result == true) {
  1065. UI.triggerHome();
  1066. } else {
  1067. alert(result);
  1068. }
  1069. },
  1070. remove: function() {
  1071. const verify = confirm("确认删除此触发器吗?");
  1072. if (verify) {
  1073. TriggerCenter.remove(trigger.name);
  1074. UI.triggerHome();
  1075. }
  1076. },
  1077. back: function() {
  1078. UI.selectTriggerTemplate();
  1079. },
  1080. saveback: function() {
  1081. const result = TriggerCenter.modify(trigger.name, this.name, this.conditions, this.source);
  1082. if (result == true) {
  1083. UI.triggerHome();
  1084. } else {
  1085. alert(result);
  1086. }
  1087. }
  1088. }
  1089. })
  1090. },
  1091.  
  1092. _appendHtml: function(title, content, rightText, leftText) {
  1093. var realLeftText = leftText == null ? "" : leftText;
  1094. var realRightText = rightText == null ? "" : rightText;
  1095. var html = `
  1096. <div class = "item-commands" style="text-align:center" id="app">
  1097. <div style="margin-top:0.5em">
  1098. <div style="width:8em;float:left;text-align:left;padding:0px 0px 0px 2em;height:1.23em" id="wsmud_raid_left">${realLeftText}</div>
  1099. <div style="width:calc(100% - 16em);float:left;height:1.23em">${title}</div>
  1100. <div style="width:8em;float:left;text-align:right;padding:0px 2em 0px 0px;height:1.23em" id="wsmud_raid_right">${realRightText}</div>
  1101. </div>
  1102. <br><br>
  1103. ${content}
  1104. </div>`;
  1105. Message.clean();
  1106. Message.append(html);
  1107. },
  1108. };
  1109.  
  1110. /***********************************************************************************\
  1111. Trigger Config
  1112. \***********************************************************************************/
  1113.  
  1114. const TriggerConfig = {
  1115. get: function() {
  1116. let all = {};
  1117. let keys = GM_listValues();
  1118. keys.forEach(key => {
  1119. all[key] = GM_getValue(key);
  1120. });
  1121. return all;
  1122. },
  1123. set: function(config) {
  1124. for (const key in config) {
  1125. GM_setValue(key, config[key]);
  1126. }
  1127. TriggerCenter.reload();
  1128. }
  1129. };
  1130.  
  1131. /***********************************************************************************\
  1132. Ready
  1133. \***********************************************************************************/
  1134.  
  1135. let Running = false;
  1136.  
  1137. $(document).ready(function () {
  1138. WG = unsafeWindow.WG;
  1139. messageAppend = unsafeWindow.messageAppend;
  1140. messageClear = unsafeWindow.messageClear;
  1141. ToRaid = unsafeWindow.ToRaid;
  1142. Role = unsafeWindow.Role;
  1143.  
  1144. unsafeWindow.TriggerUI = UI;
  1145. unsafeWindow.TriggerConfig = TriggerConfig;
  1146. unsafeWindow.TriggerCenter = TriggerCenter;
  1147.  
  1148. WG.add_hook("login", function(data) {
  1149. if (Running) return;
  1150. Running = true;
  1151.  
  1152. TriggerCenter.run();
  1153. MonitorCenter.run();
  1154. });
  1155. });
  1156. })();