wsmud_Trigger

武神传说 MUD

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

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