|
楼主 |
发表于 2021-10-20 11:10:43
|
显示全部楼层
继续
接下去我们引入 提案proposal的概念
proposal就是一个需要判断是否要去处理的事情,比如检查钱少,是否要去取钱,检查食物是否够,检查饮水是否够,不够去买。
proposal有两个方法,一个是submit,即提交判断是否可以实行,一个是Execute,即去进行相应操作
- (function (app) {
- let Proposal=function(id){
- if (!id) {
- throw "Proposal的id不能为空"
- }
- this.ID=id
- this.OnFinish=""
- this.OnExecute=function(){
- }
- this.Execute=function(onFinish){
- this.OnFinish=onFinish
- this.OnExecute()
- }
- this.Submit=function(){
- return false
- }
- }
- return Proposal
- })(App)
复制代码 https://github.com/hellclient-scripts/pkuxkx.noob/blob/bbae4ab351568ac9a9df70d2bb249ebc91251c5b/script/core/proposal/proposal.js
proposal的管理代码如下
- (function (app) {
- app.Proposals = {}
- app.ProposalGroups = {}
- app.RegisterProposal = function (proposal) {
- if (proposal.ID == "") {
- throw "Proposal id不可为空"
- }
- app.Proposals[proposal.ID] = proposal
- }
- app.RegisterProposalGroup = function (groupid, proposals) {
- app.ProposalGroups[groupid] = proposals
- }
- let try_proposals = function (proposals, onfinish) {
- for (var i in proposals) {
- let proposal = app.Proposals[proposals[i]]
- if (!proposal) {
- throw "提案 [" + proposals[i] + "] 没找到"
- }
- if (proposal.Submit()) {
- proposal.Execute(onfinish)
- return true
- }
- }
- return false
- }
- let try_proposalgroup = function (groupid, onfinish) {
- let group = app.ProposalGroups[groupid]
- if (!group) {
- throw "提案组 [" + groupid + "] 没找到"
- }
- return try_proposals(group, onfinish)
- }
- let try_proposalgroups = function (groups, onfinish) {
- for (var i in groups) {
- let group=groups[i]
- if (try_proposalgroup(group,onfinish)){
- return true
- }
- }
- return false
- }
- app.TryProposalGroups = function (groups, onfinish, onrejected) {
- if (try_proposalgroups(groups, onfinish)) {
- return true
- }
- app.ExecuteCallback(onrejected)
- return false
- }
- app.TryProposalGroup = function (groupid, onfinish, onrejected) {
- if (try_proposalgroup(groupid, onfinish)) {
- return true
- }
- app.ExecuteCallback(onrejected)
- return false
- }
- app.TryProposals = function (proposals, onfinish, onrejected) {
- if (try_proposals(proposals, onfinish)) {
- return true
- }
- app.ExecuteCallback(onrejected)
- return false
- }
- Include("core/proposal/cash.js")
- Include("core/proposal/food.js")
- Include("core/proposal/drink.js")
- Include("core/proposal/prepare.js")
- })(App)
复制代码 https://github.com/hellclient-scripts/pkuxkx.noob/blob/bbae4ab351568ac9a9df70d2bb249ebc91251c5b/script/core/proposal.js
引入三个全局函数
- TryProposals,尝试执行执行多个提案
- TryProposalGroup 尝试执行预先定义的提案组
- app.TryProposalGroups 尝试执行预先定义的多个提案组
|
|