Greasy Fork 还支持 简体中文。

wsmud_Trigger

武神传说 MUD

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

  1. // ==UserScript==
  2. // @name wsmud_Trigger
  3. // @namespace cqv3
  4. // @version 0.0.33
  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.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. const name = data.name == null ? "无" : data.name;
  490. let params = {
  491. "频道": chanel,
  492. "发言人": name,
  493. "关键字": data.content
  494. };
  495. params["content"] = data.content;
  496. params["name"] = name;
  497. const n = new Notification("新聊天信息", params);
  498. NotificationCenter.post(n);
  499. });
  500. };
  501. const monitor = new Monitor(run);
  502. MonitorCenter.addMonitor(monitor);
  503. })();
  504.  
  505. //---------------------------------------------------------------------------
  506. // item add
  507. //---------------------------------------------------------------------------
  508.  
  509. (function() {
  510. const name = new InputFilter("人物名称", InputFilterFormat.text, "", KeyAssert);
  511. name.description("人名关键字");
  512. let filters = [name];
  513. const intro = `// 人物刷新触发器
  514. // 刷新人物id:(id)
  515. // 刷新人物名称:(name)`;
  516. const t = new TriggerTemplate("人物刷新", filters, intro);
  517. TriggerTemplateCenter.add(t);
  518.  
  519. const run = function() {
  520. WG.add_hook("itemadd", data => {
  521. if (data.name == null || data.id == null) return;
  522. let params = {
  523. "人物名称": data.name,
  524. };
  525. params["id"] = data.id;
  526. params["name"] = data.name;
  527. const n = new Notification("人物刷新", params);
  528. NotificationCenter.post(n);
  529. });
  530. };
  531. const monitor = new Monitor(run);
  532. MonitorCenter.addMonitor(monitor);
  533. })();
  534.  
  535. //---------------------------------------------------------------------------
  536. // dialog pack
  537. //---------------------------------------------------------------------------
  538.  
  539. (function() {
  540. const name = new InputFilter("名称关键字", InputFilterFormat.text, "", KeyAssert);
  541. let filters = [name];
  542. const intro = `// 物品拾取触发器
  543. // 拾取物品id:(id)
  544. // 拾取物品名称:(name)
  545. // 拾取物品数量:(count)
  546. // 物品品质:(quality) 值:白、绿、蓝、黄、紫、橙、红、未知`;
  547. const t = new TriggerTemplate("物品拾取", filters, intro);
  548. TriggerTemplateCenter.add(t);
  549.  
  550. const run = function() {
  551. WG.add_hook("dialog", function(data) {
  552. if (data.dialog != "pack" || data.id == null || data.name == null || data.count == null || data.remove != null) return;
  553. let params = {
  554. "名称关键字": data.name,
  555. };
  556. params["id"] = data.id;
  557. params["name"] = data.name;
  558. params["count"] = data.count;
  559. let quality = "未知";
  560. const tag = /<\w{3}>/.exec(data.name)[0];
  561. const tagMap = {
  562. "<wht>": "白",
  563. "<hig>": "绿",
  564. "<hic>": "蓝",
  565. "<hiy>": "黄",
  566. "<hiz>": "紫",
  567. "<hio>": "橙",
  568. "<ord>": "红"
  569. }
  570. quality = tagMap[tag];
  571. params["quality"] = quality;
  572. const n = new Notification("物品拾取", params);
  573. NotificationCenter.post(n);
  574. });
  575. };
  576. const monitor = new Monitor(run);
  577. MonitorCenter.addMonitor(monitor);
  578. })();
  579.  
  580. //---------------------------------------------------------------------------
  581. // text
  582. //---------------------------------------------------------------------------
  583.  
  584. (function() {
  585. const name = new InputFilter("关键字", InputFilterFormat.text, "", KeyAssert);
  586. let filters = [name];
  587. const intro = `// 新提示信息触发器
  588. // 提示信息:(text)`;
  589. const t = new TriggerTemplate("新提示信息", filters, intro);
  590. TriggerTemplateCenter.add(t);
  591.  
  592. const run = function() {
  593. WG.add_hook("text", data => {
  594. if (data.msg == null) return;
  595. let params = {
  596. "关键字": data.msg,
  597. };
  598. params["text"] = data.msg;
  599. const n = new Notification("新提示信息", params);
  600. NotificationCenter.post(n);
  601. });
  602. };
  603. const monitor = new Monitor(run);
  604. MonitorCenter.addMonitor(monitor);
  605. })();
  606.  
  607. //---------------------------------------------------------------------------
  608. // combat
  609. //---------------------------------------------------------------------------
  610.  
  611. (function() {
  612. const type = new SelectFilter("类型", ["进入战斗", "脱离战斗"], 0);
  613. let filters = [type];
  614. const intro = "// 战斗状态切换触发器";
  615. const t = new TriggerTemplate("战斗状态切换", filters, intro);
  616. TriggerTemplateCenter.add(t);
  617.  
  618. const run = function() {
  619. WG.add_hook("combat", data => {
  620. let params = null;
  621. if (data.start != null && data.start == 1) {
  622. params = { "类型": "进入战斗" };
  623. } else if (data.end != null && data.end == 1) {
  624. params = { "类型": "脱离战斗" };
  625. }
  626. const n = new Notification("战斗状态切换", params);
  627. NotificationCenter.post(n);
  628. });
  629. WG.add_hook("text", function(data) {
  630. if (data.msg == null) return;
  631. if (data.msg.indexOf('只能在战斗中使用') != -1 || data.msg.indexOf('这里不允许战斗') != -1 || data.msg.indexOf('没时间这么做') != -1) {
  632. const params = { "类型": "脱离战斗" };
  633. const n = new Notification("战斗状态切换", params);
  634. NotificationCenter.post(n);
  635. }
  636. });
  637. };
  638. const monitor = new Monitor(run);
  639. MonitorCenter.addMonitor(monitor);
  640. })();
  641.  
  642. //---------------------------------------------------------------------------
  643. // combat
  644. //---------------------------------------------------------------------------
  645.  
  646. (function() {
  647. const type = new SelectFilter("类型", ["已经死亡", "已经复活"], 0);
  648. let filters = [type];
  649. const intro = "// 死亡状态改变触发器";
  650. const t = new TriggerTemplate("死亡状态改变", filters, intro);
  651. TriggerTemplateCenter.add(t);
  652.  
  653. const run = function() {
  654. WG.add_hook("die", data => {
  655. const value = data.relive == null ? "已经死亡" : "已经复活";
  656. let params = {
  657. "类型": value
  658. };
  659. const n = new Notification("死亡状态改变", params);
  660. NotificationCenter.post(n);
  661. });
  662. };
  663. const monitor = new Monitor(run);
  664. MonitorCenter.addMonitor(monitor);
  665. })();
  666.  
  667. //---------------------------------------------------------------------------
  668. // time
  669. //---------------------------------------------------------------------------
  670.  
  671. (function() {
  672. const hours = [
  673. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  674. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  675. 20, 21, 22, 23
  676. ];
  677. const minutes = [
  678. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  679. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  680. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  681. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  682. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  683. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59
  684. ];
  685. const hour = new SelectFilter("时", hours, 0, EqualAssert);
  686. const minute = new SelectFilter("分", minutes, 0, EqualAssert);
  687. const second = new SelectFilter("秒", minutes, 0, EqualAssert);
  688. let filters = [hour, minute, second];
  689. const intro = "// 时辰已到触发器";
  690. const t = new TriggerTemplate("时辰已到", filters, intro);
  691. TriggerTemplateCenter.add(t);
  692.  
  693. const run = function() {
  694. setInterval(_ => {
  695. const date = new Date();
  696. const params = {
  697. "时": date.getHours(),
  698. "分": date.getMinutes(),
  699. "秒": date.getSeconds()
  700. };
  701. const n = new Notification("时辰已到", params);
  702. NotificationCenter.post(n);
  703. }, 1000);
  704. };
  705. const monitor = new Monitor(run);
  706. MonitorCenter.addMonitor(monitor);
  707. })();
  708.  
  709. //---------------------------------------------------------------------------
  710. // dispfm
  711. //---------------------------------------------------------------------------
  712.  
  713. (function() {
  714. const sid = new InputFilter("技能id", InputFilterFormat.text, "", ContainAssert);
  715. let filters = [sid];
  716. const intro = `// 技能释放触发器
  717. // 技能id:(id)
  718. // 出招时间:(rtime)
  719. // 冷却时间:(distime)`;
  720. const t = new TriggerTemplate("技能释放", filters, intro);
  721. TriggerTemplateCenter.add(t);
  722.  
  723. const sid1 = new InputFilter("技能id", InputFilterFormat.text, "", ContainAssert);
  724. let filters1 = [sid1];
  725. const intro1 = `// 技能冷却结束触发器
  726. // 技能id:(id)`;
  727. const t1 = new TriggerTemplate("技能冷却结束", filters1, intro1);
  728. TriggerTemplateCenter.add(t1);
  729.  
  730. const run = function() {
  731. WG.add_hook("dispfm", data => {
  732. if (data.id == null || data.distime == null || data.rtime == null) return;
  733. let params = {
  734. "技能id": data.id
  735. };
  736. params["id"] = data.id;
  737. params["rtime"] = data.rtime;
  738. params["distime"] = data.distime;
  739. const n = new Notification("技能释放", params);
  740. NotificationCenter.post(n);
  741.  
  742. setTimeout(_ => {
  743. let params = {
  744. "技能id": data.id
  745. };
  746. params["id"] = data.id;
  747. const n = new Notification("技能冷却结束", params);
  748. NotificationCenter.post(n);
  749. }, data.distime);
  750. });
  751. };
  752. const monitor = new Monitor(run);
  753. MonitorCenter.addMonitor(monitor);
  754. })();
  755.  
  756. //---------------------------------------------------------------------------
  757. // hp mp
  758. //---------------------------------------------------------------------------
  759.  
  760. var RoomItems = {};
  761.  
  762. (function() {
  763. const name = new InputFilter("人名关键字", InputFilterFormat.text, "", KeyAssert);
  764. const type = new SelectFilter("类型", ["气血", "内力"], 0, EqualAssert);
  765. const compare = new SelectFilter("当", ["低于", "高于"], 0, EqualAssert);
  766. const valueType = new SelectFilter("值类型", ["百分比", "数值"], 0, EqualAssert);
  767. const value = new InputFilter("值", InputFilterFormat.number, 0, function(fromUser, fromGame) {
  768. const parts = fromGame.split(";");
  769. const oldvalue = parseFloat(parts[0]);
  770. const newvalue = parseFloat(parts[1]);
  771. if (oldvalue >= fromUser && newvalue < fromUser) return true;
  772. if (oldvalue <= fromUser && newvalue > fromUser) return true;
  773. return false;
  774. });
  775. let filters = [name, type, compare, valueType, value];
  776. const intro = `// 气血内力改变触发器
  777. // 人物id:(id)
  778. // 人物当前气血:(hp)
  779. // 人物最大气血:(maxHp)
  780. // 人物当前内力:(mp)
  781. // 人物最大内力:(maxMp)`;
  782. const t = new TriggerTemplate("气血内力改变", filters, intro);
  783. TriggerTemplateCenter.add(t);
  784.  
  785. const run = function() {
  786. WG.add_hook("items", data => {
  787. if (data.items == null) return;
  788. RoomItems = {};
  789. for (const item of data.items) {
  790. RoomItems[item.id] = CopyObject(item);
  791. }
  792. });
  793. WG.add_hook("itemadd", data => {
  794. RoomItems[data.id] = CopyObject(data);
  795. });
  796. const decorate = function(params, item) {
  797. params["id"] = item.id;
  798. params["hp"] = item.hp;
  799. params["maxHp"] = item.max_hp;
  800. params["mp"] = item.mp;
  801. params["maxMp"] = item.max_mp;
  802. };
  803. WG.add_hook("sc", data => {
  804. if (data.id == null) return;
  805. let item = RoomItems[data.id];
  806. if (item == null) return;
  807. if (data.hp != null) {
  808. let compare = "低于";
  809. if (data.hp > item.hp) compare = "高于";
  810. const oldValue = item.hp;
  811. const oldPer = (item.hp/item.max_hp*100).toFixed(2);
  812. item.hp = data.hp;
  813. if (item.max_hp < item.hp) item.max_hp = item.hp;
  814. if (data.max_hp != null) item.max_hp = data.max_hp;
  815. const newValue = item.hp;
  816. const newPer = (item.hp/item.max_hp*100).toFixed(2);
  817. let params1 = {
  818. "人名关键字": item.name,
  819. "类型": "气血",
  820. "当": compare,
  821. "值类型": "百分比",
  822. "值": `${oldPer};${newPer}`
  823. };
  824. decorate(params1, item);
  825. const n1 = new Notification("气血内力改变", params1);
  826. NotificationCenter.post(n1);
  827. let params2 = {
  828. "人名关键字": item.name,
  829. "类型": "气血",
  830. "当": compare,
  831. "值类型": "数值",
  832. "值": `${oldValue};${newValue}`
  833. };
  834. decorate(params2, item);
  835. const n2 = new Notification("气血内力改变", params2);
  836. NotificationCenter.post(n2);
  837. }
  838. if (data.mp != null) {
  839. let compare = "低于";
  840. if (data.mp > item.mp) compare = "高于";
  841. const oldValue = item.mp;
  842. const oldPer = (item.mp/item.max_mp*100).toFixed(2);
  843. item.mp = data.mp;
  844. if (item.max_mp < item.mp) item.max_mp = item.mp;
  845. if (data.max_mp != null) item.max_mp = data.max_mp;
  846. const newValue = item.mp;
  847. const newPer = (item.mp/item.max_mp*100).toFixed(2);
  848. let params1 = {
  849. "人名关键字": item.name,
  850. "类型": "内力",
  851. "当": compare,
  852. "值类型": "百分比",
  853. "值": `${oldPer};${newPer}`
  854. };
  855. decorate(params1, item);
  856. const n1 = new Notification("气血内力改变", params1);
  857. NotificationCenter.post(n1);
  858. let params2 = {
  859. "人名关键字": item.name,
  860. "类型": "内力",
  861. "当": compare,
  862. "值类型": "数值",
  863. "值": `${oldValue};${newValue}`
  864. };
  865. decorate(params2, item);
  866. const n2 = new Notification("气血内力改变", params2);
  867. NotificationCenter.post(n2);
  868. }
  869. });
  870. };
  871. const monitor = new Monitor(run);
  872. MonitorCenter.addMonitor(monitor);
  873. })();
  874.  
  875. //---------------------------------------------------------------------------
  876. // damage
  877. //---------------------------------------------------------------------------
  878.  
  879. (function() {
  880. const name = new InputFilter("人名关键字", InputFilterFormat.text, "", KeyAssert);
  881. const valueType = new SelectFilter("值类型", ["百分比", "数值"], 0, EqualAssert);
  882. const value = new InputFilter("值", InputFilterFormat.number, 0, (fromUser, fromGame) => {
  883. const parts = fromGame.split(";");
  884. const oldvalue = parseFloat(parts[0]);
  885. const newvalue = parseFloat(parts[1]);
  886. if (oldvalue <= fromUser && newvalue > fromUser) return true;
  887. return false;
  888. });
  889. let filters = [name, valueType, value];
  890. const intro = `// 伤害已满触发器
  891. // 备注:限制条件-值 不支持多条件
  892. // 人物id:(id)
  893. // 人物名称:(name)
  894. // 伤害数值:(value)
  895. // 伤害百分比:(percent)`;
  896. const t = new TriggerTemplate("伤害已满", filters, intro);
  897. TriggerTemplateCenter.add(t);
  898.  
  899. const run = function() {
  900. const decorate = function(params, item, value, percent) {
  901. params["id"] = item.id;
  902. params["name"] = item.name;
  903. params["value"] = value;
  904. params["percent"] = percent;
  905. };
  906. WG.add_hook("sc", data => {
  907. if (data.id == null || data.damage == null) return;
  908. let item = RoomItems[data.id];
  909. if (item == null || item.id == null || item.name == null || item.max_hp == null) return;
  910. // 获取之前保存的伤害和伤害百分比
  911. const oldValue = item._damage == null ? 0 : item._damage;
  912. const oldPer = item._damagePer == null ? 0 : item._damagePer;
  913. const value = data.damage;
  914. const percent = (data.damage/item.max_hp*100).toFixed(2);
  915. // 保存伤害和伤害百分比
  916. item._damage = value;
  917. item._damagePer = percent;
  918. let params1 = {
  919. "人名关键字": item.name,
  920. "值类型": "百分比",
  921. "值": `${oldPer};${percent}`
  922. };
  923. decorate(params1, item, value, percent);
  924. const n1 = new Notification("伤害已满", params1);
  925. NotificationCenter.post(n1);
  926. let params2 = {
  927. "人名关键字": item.name,
  928. "值类型": "数值",
  929. "值": `${oldValue};${value}`
  930. };
  931. decorate(params2, item, value, percent);
  932. const n2 = new Notification("伤害已满", params2);
  933. NotificationCenter.post(n2);
  934. });
  935. };
  936. const monitor = new Monitor(run);
  937. MonitorCenter.addMonitor(monitor);
  938. })();
  939.  
  940. /***********************************************************************************\
  941. UI
  942. \***********************************************************************************/
  943.  
  944. const Message = {
  945. append: function(msg) {
  946. messageAppend(msg);
  947. },
  948. clean: function() {
  949. messageClear();
  950. },
  951. };
  952.  
  953. const UI = {
  954. triggerHome: function() {
  955. const content = `
  956. <style>.breakText {word-break:keep-all;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}</style>
  957. <span class="zdy-item" style="width:120px" v-for="t in triggers" :style="activeStyle(t)">
  958. <div style="width: 30px; float: left; background-color: rgba(255, 255, 255, 0.31); border-radius: 4px;" v-on:click="editTrigger(t)">⚙</div>
  959. <div class="breakText" style="width: 85px; float: right;" v-on:click="switchStatus(t)">{{ t.name }}</div>
  960. </span>
  961. `;
  962. const rightText = "<span v-on:click='createTrigger()'><wht>新建</wht></span>";
  963. UI._appendHtml("🍟 <hio>触发器</hio>", content, rightText);
  964. new Vue({
  965. el: '#app',
  966. data: {
  967. triggers: TriggerCenter.getAll()
  968. },
  969. methods: {
  970. switchStatus: function(t) {
  971. if (t.active()) {
  972. TriggerCenter.deactivate(t.name);
  973. } else {
  974. TriggerCenter.activate(t.name);
  975. }
  976. UI.triggerHome();
  977. },
  978. editTrigger: UI.editTrigger,
  979. activeStyle: function(t) {
  980. if (t.active()) {
  981. return {
  982. "background-color": "#a0e6e0",
  983. "border": "1px solid #7284ff",
  984. "color": "#001bff"
  985. };
  986. } else {
  987. return { "background-color": "none" };
  988. }
  989. },
  990. createTrigger: UI.selectTriggerTemplate
  991. }
  992. });
  993. },
  994. selectTriggerTemplate: function() {
  995. const content = `
  996. <span class="zdy-item" style="width:120px" v-for="t in templates" v-on:click="select(t)">{{ t.event }}</span>
  997. `;
  998. const leftText = "<span v-on:click='back()'>< 返回</span>";
  999. UI._appendHtml("<wht>选择触发事件</wht>", content, null, leftText);
  1000. new Vue({
  1001. el: '#app',
  1002. data: {
  1003. templates: TriggerTemplateCenter.getAll()
  1004. },
  1005. methods: {
  1006. select: UI.createTrigger,
  1007. back: UI.triggerHome
  1008. }
  1009. });
  1010. },
  1011. createTrigger: function(template) {
  1012. UI._updateTrigger(template);
  1013. },
  1014. editTrigger: function(trigger) {
  1015. UI._updateTrigger(trigger.template, trigger);
  1016. },
  1017. _updateTrigger: function(template, trigger) {
  1018. const content = `
  1019. <div style="margin:0 2em 0 2em">
  1020. <div style="float:left;width:120px">
  1021. <span class="zdy-item" style="width:90px" v-for="f in filters">
  1022. <p style="margin:0"><wht>{{ f.description() }}</wht></p>
  1023. <input v-if="f.type=='input'" style="width:80%" v-model="conditions[f.name]">
  1024. <select v-if="f.type=='select'" v-model="conditions[f.name]">
  1025. <option v-for="opt in f.options" :value="opt">{{ opt }}</option>
  1026. </select>
  1027. </span>
  1028. </div>
  1029. <div style="float:right;width:calc(100% - 125px)">
  1030. <textarea class = "settingbox hide" style = "height:10rem;display:inline-block;font-size:0.8em;width:100%" v-model="source"></textarea>
  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. },
  1062. methods: {
  1063. save: function() {
  1064. const result = TriggerCenter.create(this.name, template.event, this.conditions, this.source);
  1065. if (result == true) {
  1066. UI.triggerHome();
  1067. } else {
  1068. alert(result);
  1069. }
  1070. },
  1071. remove: function() {
  1072. const verify = confirm("确认删除此触发器吗?");
  1073. if (verify) {
  1074. TriggerCenter.remove(trigger.name);
  1075. UI.triggerHome();
  1076. }
  1077. },
  1078. back: function() {
  1079. UI.selectTriggerTemplate();
  1080. },
  1081. saveback: function() {
  1082. const result = TriggerCenter.modify(trigger.name, this.name, this.conditions, this.source);
  1083. if (result == true) {
  1084. UI.triggerHome();
  1085. } else {
  1086. alert(result);
  1087. }
  1088. }
  1089. }
  1090. })
  1091. },
  1092.  
  1093. _appendHtml: function(title, content, rightText, leftText) {
  1094. var realLeftText = leftText == null ? "" : leftText;
  1095. var realRightText = rightText == null ? "" : rightText;
  1096. var html = `
  1097. <div class = "item-commands" style="text-align:center" id="app">
  1098. <div style="margin-top:0.5em">
  1099. <div style="width:8em;float:left;text-align:left;padding:0px 0px 0px 2em;height:1.23em" id="wsmud_raid_left">${realLeftText}</div>
  1100. <div style="width:calc(100% - 16em);float:left;height:1.23em">${title}</div>
  1101. <div style="width:8em;float:left;text-align:right;padding:0px 2em 0px 0px;height:1.23em" id="wsmud_raid_right">${realRightText}</div>
  1102. </div>
  1103. <br><br>
  1104. ${content}
  1105. </div>`;
  1106. Message.clean();
  1107. Message.append(html);
  1108. },
  1109. };
  1110.  
  1111. /***********************************************************************************\
  1112. Trigger Config
  1113. \***********************************************************************************/
  1114.  
  1115. const TriggerConfig = {
  1116. get: function() {
  1117. let all = {};
  1118. let keys = GM_listValues();
  1119. keys.forEach(key => {
  1120. all[key] = GM_getValue(key);
  1121. });
  1122. return all;
  1123. },
  1124. set: function(config) {
  1125. for (const key in config) {
  1126. GM_setValue(key, config[key]);
  1127. }
  1128. TriggerCenter.reload();
  1129. }
  1130. };
  1131.  
  1132. /***********************************************************************************\
  1133. Ready
  1134. \***********************************************************************************/
  1135.  
  1136. let Running = false;
  1137.  
  1138. $(document).ready(function () {
  1139. WG = unsafeWindow.WG;
  1140. messageAppend = unsafeWindow.messageAppend;
  1141. messageClear = unsafeWindow.messageClear;
  1142. ToRaid = unsafeWindow.ToRaid;
  1143. Role = unsafeWindow.Role;
  1144.  
  1145. unsafeWindow.TriggerUI = UI;
  1146. unsafeWindow.TriggerConfig = TriggerConfig;
  1147. unsafeWindow.TriggerCenter = TriggerCenter;
  1148.  
  1149. WG.add_hook("login", function(data) {
  1150. if (Running) return;
  1151. Running = true;
  1152.  
  1153. TriggerCenter.run();
  1154. MonitorCenter.run();
  1155. });
  1156. });
  1157. })();