From 3951791854b1f31d6c3084620f565b097356639b Mon Sep 17 00:00:00 2001 From: Rintim Date: Mon, 18 Sep 2023 10:37:59 +0800 Subject: [PATCH 1/5] =?UTF-8?q?[2023/09/18:=20=E2=91=A0]=20Extend=20`Event?= =?UTF-8?q?#setContent`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/game.js | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/game/game.js b/game/game.js index b2b32c011..78cc7facf 100644 --- a/game/game.js +++ b/game/game.js @@ -28510,16 +28510,32 @@ } return this; }, - setContent:function(name){ - if(typeof name=='function'){ - this.content=lib.init.parsex(name); - } - else{ - if(!lib.element.content[name]._parsed){ - lib.element.content[name]=lib.init.parsex(lib.element.content[name]); - lib.element.content[name]._parsed=true; - } - this.content=lib.element.content[name]; + setContent:function(item){ + switch(typeof item){ + case "object": + if(Array.isArray(item)){ + // TODO: Parse Array + throw new Error("NYI: Parse Common Object"); + } + else{ + // TODO: Parse Common Object + throw new Error("NYI: Parse Common Object"); + } + break; + case "function": + if (gnc.is.generatorFunc(item)) { + // TODO: Parse Generator Function + throw new Error("NYI: Pase Generator Function"); + } + else this.content=lib.init.parsex(item); + break; + default: + if(!lib.element.content[item]._parsed){ + lib.element.content[item]=lib.init.parsex(lib.element.content[item]); + lib.element.content[item]._parsed=true; + } + this.content=lib.element.content[item]; + break; } return this; }, From 09479829fd765f4315282ce908db377534a12fdf Mon Sep 17 00:00:00 2001 From: Rintim Date: Mon, 18 Sep 2023 11:07:55 +0800 Subject: [PATCH 2/5] =?UTF-8?q?[2023/09/18:=20=E2=91=A0]=20Handle=20the=20?= =?UTF-8?q?array=20content.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/game.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/game/game.js b/game/game.js index 78cc7facf..8d1252d4a 100644 --- a/game/game.js +++ b/game/game.js @@ -28514,8 +28514,26 @@ switch(typeof item){ case "object": if(Array.isArray(item)){ - // TODO: Parse Array - throw new Error("NYI: Parse Common Object"); + let lastEvent=null; + this.content=(event,step,source,player,target,targets,card,cards,skill,forced,num,trigger,result,_status,lib,game,ui,get,ai)=>{ + if(step>=item.length) return event.finish(); + var current=item[step]; + lastEvent=current(event,{ + event:event, + step:step, + source:source, + player:player, + target:target, + targets:targets, + card:card, + cards:cards, + skill:skill, + forced:forced, + num:num, + trigger:trigger, + result:result + },(lastEvent&&("result" in lastEvent))?lastEvent.result:null); + } } else{ // TODO: Parse Common Object From 897f993409591195c35b779ed27f56e842e17645 Mon Sep 17 00:00:00 2001 From: Rintim Date: Mon, 18 Sep 2023 12:43:49 +0800 Subject: [PATCH 3/5] =?UTF-8?q?[2023/09/18:=20=E2=91=A0]=20Move=20content?= =?UTF-8?q?=20handle=20to=20`lib.init.parsex`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/game.js | 33 +-------------------------------- 1 file changed, 1 insertion(+), 32 deletions(-) diff --git a/game/game.js b/game/game.js index 8d1252d4a..26121efdc 100644 --- a/game/game.js +++ b/game/game.js @@ -28513,39 +28513,8 @@ setContent:function(item){ switch(typeof item){ case "object": - if(Array.isArray(item)){ - let lastEvent=null; - this.content=(event,step,source,player,target,targets,card,cards,skill,forced,num,trigger,result,_status,lib,game,ui,get,ai)=>{ - if(step>=item.length) return event.finish(); - var current=item[step]; - lastEvent=current(event,{ - event:event, - step:step, - source:source, - player:player, - target:target, - targets:targets, - card:card, - cards:cards, - skill:skill, - forced:forced, - num:num, - trigger:trigger, - result:result - },(lastEvent&&("result" in lastEvent))?lastEvent.result:null); - } - } - else{ - // TODO: Parse Common Object - throw new Error("NYI: Parse Common Object"); - } - break; case "function": - if (gnc.is.generatorFunc(item)) { - // TODO: Parse Generator Function - throw new Error("NYI: Pase Generator Function"); - } - else this.content=lib.init.parsex(item); + this.content=lib.init.parsex(item); break; default: if(!lib.element.content[item]._parsed){ From c7bf5c03f23be7a59a5f7c2b4fd2c08337724b01 Mon Sep 17 00:00:00 2001 From: Rintim Date: Mon, 18 Sep 2023 12:55:40 +0800 Subject: [PATCH 4/5] =?UTF-8?q?[2023/09/18:=20=E2=91=A0]=20Move=20content?= =?UTF-8?q?=20handle=20to=20`lib.init.parsex`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/game.js | 113 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 38 deletions(-) diff --git a/game/game.js b/game/game.js index 26121efdc..eb7e7c0e6 100644 --- a/game/game.js +++ b/game/game.js @@ -10234,47 +10234,84 @@ localStorage.removeItem(lib.configprefix+'background'); } }, - //by 诗笺、Tipx-L - parsex:function(func){ - //Remove all comments - //移除所有注释 - var str=func.toString().replace(/((?:(?:^[ \t]*)?(?:\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/(?:[ \t]*\r?\n(?=[ \t]*(?:\r?\n|\/\*|\/\/)))?|\/\/(?:[^\\]|\\(?:\r?\n)?)*?(?:\r?\n(?=[ \t]*(?:\r?\n|\/\*|\/\/))|(?=\r?\n))))+)|("(?:\\[\s\S]|[^"\\])*"|'(?:\\[\s\S]|[^'\\])*'|(?:\r?\n|[\s\S])[^\/"'\\\s]*)/mg,'$2').trim(); - //获取第一个 { 后的所有字符 - str=str.slice(str.indexOf('{')+1); - //func中要写步骤的话,必须要写step 0 - if(str.indexOf('step 0')==-1){ - str='{if(event.step==1) {event.finish();return;}\n'+str; - }else{ - var skip=0; - //每层最多找99个step - for (var k=0;k<99;k++) { - //正则表达式 - var reg=new RegExp(`['"]step ${k}['"]`); - var result=str.slice(skip).match(reg); - if(result==null) break; - var insertStr; - if(k==0){ - insertStr=`switch(step){case 0:`; - }else{ - insertStr=`break;case ${k}:`; - } - var copy=str; - copy=copy.slice(0,skip+result.index)+insertStr+copy.slice(skip+result.index+result[0].length); - //测试是否有错误 - try{ - new Function(copy); - str=copy; - skip+=result.index+insertStr.length; - }catch(error){ - k--; - skip+=result.index+result[0].length; + parsex:function(item){ + //by 诗笺、Tipx-L + function Legacy(func){ + //Remove all comments + //移除所有注释 + var str=func.toString().replace(/((?:(?:^[ \t]*)?(?:\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/(?:[ \t]*\r?\n(?=[ \t]*(?:\r?\n|\/\*|\/\/)))?|\/\/(?:[^\\]|\\(?:\r?\n)?)*?(?:\r?\n(?=[ \t]*(?:\r?\n|\/\*|\/\/))|(?=\r?\n))))+)|("(?:\\[\s\S]|[^"\\])*"|'(?:\\[\s\S]|[^'\\])*'|(?:\r?\n|[\s\S])[^\/"'\\\s]*)/mg,'$2').trim(); + //获取第一个 { 后的所有字符 + str=str.slice(str.indexOf('{')+1); + //func中要写步骤的话,必须要写step 0 + if(str.indexOf('step 0')==-1){ + str='{if(event.step==1) {event.finish();return;}\n'+str; + }else{ + var skip=0; + //每层最多找99个step + for (var k=0;k<99;k++) { + //正则表达式 + var reg=new RegExp(`['"]step ${k}['"]`); + var result=str.slice(skip).match(reg); + if(result==null) break; + var insertStr; + if(k==0){ + insertStr=`switch(step){case 0:`; + }else{ + insertStr=`break;case ${k}:`; + } + var copy=str; + copy=copy.slice(0,skip+result.index)+insertStr+copy.slice(skip+result.index+result[0].length); + //测试是否有错误 + try{ + new Function(copy); + str=copy; + skip+=result.index+insertStr.length; + }catch(error){ + k--; + skip+=result.index+result[0].length; + } } + str=`if(event.step==${k}){event.finish();return;}`+str; } - str=`if(event.step==${k}){event.finish();return;}`+str; + return (new Function('event','step','source','player','target','targets', + 'card','cards','skill','forced','num','trigger','result', + '_status','lib','game','ui','get','ai',str)); + } + switch(typeof item){ + case "object": + if(Array.isArray(item)){ + let lastEvent=null; + return (event,step,source,player,target,targets,card,cards,skill,forced,num,trigger,result,_status,lib,game,ui,get,ai)=>{ + if(step>=item.length) return event.finish(); + var current=item[step]; + lastEvent=current(event,{ + event:event, + step:step, + source:source, + player:player, + target:target, + targets:targets, + card:card, + cards:cards, + skill:skill, + forced:forced, + num:num, + trigger:trigger, + result:result + },(lastEvent&&("result" in lastEvent))?lastEvent.result:null); + } + } + else{ + // TODO: Parse Common Object + throw new Error("NYI: Parse Common Object"); + } + case "function": + if (gnc.is.generatorFunc(item)) { + // TODO: Parse Generator Function + throw new Error("NYI: Pase Generator Function"); + } + else return Legacy(item); } - return (new Function('event','step','source','player','target','targets', - 'card','cards','skill','forced','num','trigger','result', - '_status','lib','game','ui','get','ai',str)); }, eval:function(func){ if(typeof func=='function'){ From ee0bfc60a8db0b76bf7a2ab1410a260a3d07d15e Mon Sep 17 00:00:00 2001 From: Rintim Date: Mon, 18 Sep 2023 16:06:58 +0800 Subject: [PATCH 5/5] =?UTF-8?q?[2023/09/18:=20=E2=91=A1]=20Handle=20Genera?= =?UTF-8?q?torFunction.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/game.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/game/game.js b/game/game.js index eb7e7c0e6..e33a81a0f 100644 --- a/game/game.js +++ b/game/game.js @@ -10307,8 +10307,27 @@ } case "function": if (gnc.is.generatorFunc(item)) { - // TODO: Parse Generator Function - throw new Error("NYI: Pase Generator Function"); + let gen,lastEvent; + return (event,step,source,player,target,targets,card,cards,skill,forced,num,trigger,result,_status,lib,game,ui,get,ai)=>{ + if(!gen)gen=item(event,{ + event:event, + step:step, + source:source, + player:player, + target:target, + targets:targets, + card:card, + cards:cards, + skill:skill, + forced:forced, + num:num, + trigger:trigger, + result:result + }); + var res=gen.next((lastEvent&&("result" in lastEvent))?lastEvent.result:null); + if(res.done) event.finish(); + else lastEvent=res.value; + } } else return Legacy(item); }