赞 | 65 |
VIP | 231 |
好人卡 | 2 |
积分 | 19 |
经验 | 35171 |
最后登录 | 2024-9-15 |
在线时间 | 1554 小时 |
Lv3.寻梦者
- 梦石
- 0
- 星屑
- 1912
- 在线时间
- 1554 小时
- 注册时间
- 2013-4-13
- 帖子
- 917
|
4楼
楼主 |
发表于 2019-9-8 01:14:59
|
只看该作者
当前代码, 持续更新中
- //=============================================================================
- // StoneSlots.js
- //=============================================================================
- /*:
- * @plugindesc 基础镶嵌系统
- * @author Heartcase
- *
- * @help
- *
- * 所谓镶嵌系统就是将一些组件自行组合并计算最终收益的系统
- * 所能容纳组件的对象我们称之为插槽
- * 插槽所组成的系统我们称之为面板
- *
- * 本系统对只提供计算收益的接口
- * 对收益的具体逻辑和结构并没有意见
- *
- * 本系统只提供最基础和核心的功能
- * 其他功能可以通过拓展本插件获得
- */
- /**
- * 全局命名空间
- */
- const StoneSlots = {
- Constant: {},
- Model: {},
- View: {},
- Test: {}
- };
- /**
- * 常量
- */
- {
- const { Constant } = StoneSlots;
- /**
- * 操作返回值
- */
- Constant.OP_CODE = {
- SUCCESS: 0,
- NOT_EMPTY: 1,
- EMPTY: 2
- };
- }
- /**
- * 数据类型
- */
- {
- const { Model, Constant } = StoneSlots;
- const { OP_CODE } = Constant;
- /**
- * 组件类
- */
- Model.Stone = class {
- static getStoneById(id) {
- return Model.Stone._stones[id];
- }
- static register(id, stone) {
- if (!Model.Stone._stones) {
- Model.Stone._stones = {};
- }
- Model.Stone._stones[id] = stone;
- }
- static getAllStone() {
- return Model.Stone._stones;
- }
- static getStoneItems(items) {
- const obj = {};
- Object.entries(items)
- .filter(([index, _]) => {
- return Object.keys(Model.Stone._stones).includes(index);
- })
- .forEach(([index, nums]) => {
- obj[index] = nums;
- });
- return obj;
- }
- constructor({ id, effects }) {
- this._effects = effects;
- Model.Stone.register(id, this);
- }
- get effects() {
- return this._effects;
- }
- };
- /**
- * 插槽类
- */
- Model.Slot = class {
- constructor() {
- this._stone = null;
- }
- get stone() {
- return this._stone;
- }
- isEmpty() {
- return !this._stone;
- }
- equip(stone, safe = false) {
- if (safe && !this.isEmpty()) {
- return OP_CODE.NOT_EMPTY;
- }
- this._stone = stone;
- return OP_CODE.SUCCESS;
- }
- unequip(safe = false) {
- if (safe && this.isEmpty()) {
- return OP_CODE.EMPTY;
- }
- this._stone = null;
- return OP_CODE.SUCCESS;
- }
- };
- /**
- * 面板类
- */
- Model.SlotCollection = class {
- constructor({ slotList }) {
- this._slotList = slotList;
- }
- getSlot(index) {
- return this._slotList[index];
- }
- getTotalEffects() {
- return this._slotList
- .map(slot => slot.effects)
- .reduce((x, y) => x.concat(y), []);
- }
- };
- /**
- * 高阶函数
- */
- Model.useLockedSlot = Slot => {
- return class extends Slot {
- constructor({ locked, ...restArgs }) {
- super(restArgs);
- this._locked = locked;
- }
- isLocked() {
- return this._locked;
- }
- lock() {
- this._locked = true;
- }
- unlock() {
- this._locked = false;
- }
- };
- };
- }
- /**
- * 界面
- */
- {
- const { View } = StoneSlots;
- View.Command_Slot = class {};
- View.Window_SlotCollection = class {};
- View.Window_StoneList = class {};
- }
- /**
- * RM 整合
- */
- {
- const _temp = DataManager.onLoad.bind(DataManager);
- // $1: 标签名
- // $2: 内容
- const re = /<(.*?)>(.*?)<\/\1>/g;
- const { Constant, Model } = StoneSlots;
- Constant.META_STONE = "StoneSlots-Stone";
- Constant.META_EFFECT = "StoneSlots-Effect";
- DataManager.onLoad = function(object) {
- _temp(object); //
- if (object === $dataItems) {
- object
- // 去除0号null值
- .slice(1)
- .filter(each => each.meta && each.meta[Constant.META_STONE])
- .forEach(each => {
- const id = each.id;
- const effects = [];
- // 移除换行
- const note = each.note.replace(/\n/g, "");
- let matches;
- while ((matches = re.exec(note))) {
- switch (matches[1]) {
- case Constant.META_EFFECT:
- effects.push(JSON.parse(matches[2]));
- }
- }
- new Model.Stone({ id, effects });
- });
- }
- };
- }
- /**
- * 测试
- */
- {
- const { Constant, Model, Test } = StoneSlots;
- const { OP_CODE } = Constant;
- let assertions;
- let testName;
- // 断言方法
- const expectEqual = (tested, expected) => {
- assertions.push({
- tested: JSON.stringify(tested),
- expected: JSON.stringify(expected),
- name: "isEqual",
- value: tested === expected
- });
- };
- const expectTrue = tested => {
- return expectEqual(tested, true);
- };
- const expectNotTrue = tested => {
- return expectEqual(tested, false);
- };
- const isEquivalent = (objA, objB) => {
- if (typeof objA != "object" || typeof objB != "object") {
- return objA === objB;
- }
- if (Object.keys(objA).length !== Object.keys(objB).length) {
- return false;
- }
- return Object.keys(objA).every(key => {
- if (typeof objA[key] === "object") {
- if (typeof objB[key] === "object") {
- return isEquivalent(objA[key], objB[key]);
- } else {
- return false;
- }
- } else {
- return objA[key] === objB[key];
- }
- });
- };
- const expectEquivalent = (tested, expected) => {
- assertions.push({
- tested: JSON.stringify(tested),
- expected: JSON.stringify(expected),
- value: isEquivalent(tested, expected),
- name: "isEquivalent"
- });
- };
- // 测试周期
- const setup = name => {
- assertions = [];
- testName = name;
- };
- const log = () => {
- console.log(`Start Test: ${testName}`);
- const length = assertions.length;
- assertions.forEach((assertion, index) => {
- const { tested, expected, value, name } = assertion;
- console.log(
- `Testing (${index + 1}/${length}): ${value ? "success" : "fail"}`
- );
- if (!value) {
- console.log(`Expected ${name}: ${expected}, but Recived : ${tested} `);
- }
- });
- console.log(`End of Test: ${testName}`);
- };
- const runTestCase = () => {
- Object.entries(Test).forEach(([name, test]) => {
- setup(name);
- test();
- log();
- });
- };
- // 测试
- Test.testModelStone = () => {
- const id = 101;
- const effects = [{ code: "a", data: "b", value: "c" }];
- const stone = new Model.Stone({ id, effects });
- const items = { 101: 123, 999: 456 };
- const stones = { 101: 123 };
- expectEqual(Model.Stone.getStoneById(id), stone);
- expectEquivalent(stone.effects, effects);
- expectEquivalent(Model.Stone.getAllStone(), { 101: stone });
- expectEquivalent(Model.Stone.getStoneItems(items), stones);
- };
- Test.testModelSlot = () => {
- const slot = new Model.Slot();
- const id = 101;
- const effects = [{ code: "a", data: "b", value: "c" }];
- const stone = new Model.Stone({ id, effects });
- // 没有装备组件时
- expectTrue(slot.isEmpty());
- expectEqual(slot.stone, null);
- expectEqual(slot.unequip(true), OP_CODE.EMPTY);
- expectEqual(slot.unequip(), OP_CODE.SUCCESS);
- // 装备组件时
- expectEqual(slot.equip(stone, true), OP_CODE.SUCCESS);
- expectEqual(slot.equip(stone, true), OP_CODE.NOT_EMPTY);
- expectEqual(slot.equip(stone), OP_CODE.SUCCESS);
- expectNotTrue(slot.isEmpty());
- expectEqual(slot.stone, stone);
- // 卸下组件时
- expectEqual(slot.unequip(true), OP_CODE.SUCCESS);
- expectTrue(slot.isEmpty());
- };
- Test.testUseLockedSlot = () => {
- const testClass = class {};
- const featuredClass = Model.useLockedSlot(testClass);
- const instance = new featuredClass({ locked: false });
- expectNotTrue(instance.isLocked());
- instance.lock();
- expectTrue(instance.isLocked());
- instance.unlock();
- expectNotTrue(instance.isLocked());
- };
- runTestCase();
- }
复制代码
|
|