• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript machina.BehavioralFsm类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了TypeScript中machina.BehavioralFsm的典型用法代码示例。如果您正苦于以下问题:TypeScript BehavioralFsm类的具体用法?TypeScript BehavioralFsm怎么用?TypeScript BehavioralFsm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了BehavioralFsm类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。

示例1: function

export const ReplaceBeaconTaskFsm = machina.BehavioralFsm.extend({
    namespace: "place_beacons",
    initialState: "supply",
    states: {
        supply: {
            _onEnter: function(task: Task) {
                task.context = {slot: task.instructionParams.slot};
                task.deployment().fetch()
                .then((deployment: Deployment) => {
                    let text = `One of our beacons needs to be replaced because it isn't working. Please go to the Supply Station (${deployment.supplyStation}). Tell me when you are 'there'.`;
                    return bot.sendMessage(
                        task.volunteerFbid,
                        msgUtil.quickReplyMessage(text, ["there"])
                    );
                });

            },
            "msg:there": "pickup",
        },
        pickup: {
            _onEnter: function(task) {
                let text = "Great! To open the lockbox, type the code 020217, then #, then turn the switch. Please take a replacement beacon. Please close and lock the box. Let me know when you are 'ready'.";
                bot.sendMessage(
                    task.volunteerFbid,
                    msgUtil.quickReplyMessage(text, ["ready"])
                );
            },
            "msg:ready": "go",
        },
        go: {
            _onEnter: function(task: Task) {
                const url = `${config.BASE_URL}/map/?advanced&hidden&beacon=${task.instructionParams.slot}`;
                const text = "Please go to the location marked on the map below. Tell me when you are 'there'.";
                const buttons = [{
                    type: "web_url",
                    title: "Open Map",
                    url: url,
                    webview_height_ratio: "tall",
                    messenger_extensions: true,
                }] as Array<FBTypes.MessengerButton>;
                bot.FBPlatform.sendButtonMessage(task.volunteerFbid.toString(), text, buttons);
            },
            "msg:there": "old_beacon",
        },
        old_beacon: {
            _onEnter: function(task) {
                let text = "Is there an existing beacon at this location?";
                bot.sendMessage(
                    task.volunteerFbid,
                    msgUtil.quickReplyMessage(text, ["yes", "no"])
                );
            },
            "msg:yes": function(task) {
                task.context.return = true;
                this.transition(this, "which");
            },
            "msg:no": function(task) {
                task.context.return = false;
                this.transition(this, "which");
            }
        },
        which: {
            _onEnter: function(task) {
                bot.sendMessage(
                    task.volunteerFbid,
                    {text: `What is the number on the back of the replacement beacon?`}
                );
            },
            number: function(task, id) {
                // TODO: double check if it seems like that beacon doesn't exist or is already placed.
                task.context.currentBeacon = id;
                task.context.return ? this.transition(task, "replace_return") : this.transition(task, "replace");
            }
        },
        replace_return: {
            _onEnter: function(task) {
                // record beacon status as broken and in possession of volunteer
                let text = "Please take down the old beacon and put the new one in it's place. Then return the old beacon to the pickup location. Tell me when you are 'done'.";
                bot.sendMessage(
                    task.volunteerFbid,
                    msgUtil.quickReplyMessage(text, ["done"])
                );
            },
            "msg:done": function(task) {
                task.saveScore(40).then(() => this.handle(task, "complete"));
            },
        },
        replace: {
            _onEnter: function(task) {
                let text = "Please put the beacon on the wall (you can double check the location using the map above). Tell me when you are 'done'.";
                bot.sendMessage(
                    task.volunteerFbid,
                    msgUtil.quickReplyMessage(text, ["done"])
                );
                // record beacon's status as MIA, look for it.
            },
            "msg:done": function(task: Task) {
                const updateSlot = new BeaconSlot({id: task.context.slot})
                .save({beacon_id: task.context.currentBeacon}, {patch: true});
                const updateScore = task.saveScore(40);
//.........这里部分代码省略.........
开发者ID:CMUBigLab,项目名称:cmuNodeFbot,代码行数:101,代码来源:replace_beacon_task.ts


示例2: Task

export const FingerprintTaskFsm = machina.BehavioralFsm.extend({
    namespace: "fingerprint",
    initialState: "ios_check",
    states: {
        ios_check: {
            _onEnter: function(task: Task) {
                return task.assignedVolunteer().fetch()
                .then(vol => {
                    if (vol.hasIOS !== null) {
                        return this.transition(task, "download_app");
                    }
                    const text = "Some of our tasks require the use of a helper app to collect Bluetooth data. Do you have an iOS device?";
                    return vol.sendMessage(msgUtil.quickReplyMessage(text, ["yes", "no"]));
                });
            },
            "msg:yes": function(task: Task) {
                return task.assignedVolunteer().fetch()
                .then((vol: Volunteer) => vol.save({"has_ios": true}))
                .then(() => this.transition(task, "download_app"));
            },
            "msg:no": function(task: Task) {
                return task.assignedVolunteer().fetch()
                .then((vol: Volunteer) => vol.save({"has_ios": false}))
                .then((vol: Volunteer) => {
                    const text = "Unforuntately, we don't have the helper app available for other platforms yet. We will contact you when we do!";
                    return vol.sendMessage({text})
                    .then(() => vol.unassignTask());
                }).then(([vol, oldTask]) => getAndAssignVolTask(vol));
            },
        },
        download_app: {
            _onEnter: function(task: Task) {
                return task.assignedVolunteer().fetch()
                .then(vol => {
                    if (vol.appState === "installed") {
                        return this.transition(task, "how_many");
                    }
                    const text = "You will need to download the app 'LuzDeploy Data Sampler'. Press the link below to open the App Store.";
                    const url = "http://appstore.com/luzdeploydatasampler";
                    const buttons = [{
                        "type": "web_url",
                        "title": "Download App",
                        "url": url,
                        "webview_height_ratio": "compact",
                    }] as Array<FBTypes.MessengerButton>;
                    return bot.FBPlatform.sendButtonMessage(
                        vol.fbid.toString(),
                        text,
                        buttons
                    ).then(() => {
                        const text = "Then come back to this conversation and let me know when you are 'done'!";
                        return vol.sendMessage(msgUtil.quickReplyMessage(text, ["done"]));
                    }); 
                });
            },
            "msg:done": function(task: Task) {
                return task.assignedVolunteer().fetch()
                .then((vol: Volunteer) => vol.save({app_state: "installed"}))
                .then(() => this.transition(task, "how_many"));
            }
        },
        how_many: {
            _onEnter: function(task: Task) {
                let text = "We need you to help us sample beacon data in the building. How many samples would you like to collect (one sample takes about 30 seconds on average)?";
                bot.sendMessage(task.volunteerFbid, msgUtil.quickReplyMessage(text, ['5', '10', '15']));
            },
            number: function(task: Task, n) {
                task.context = {numSamples: n};
                this.transition(task, "load_points");
            }
        },
        load_points: {
            _onEnter: function(task: Task) {
                let self = this;
                FingerprintPoint.getPointsForSampling(
                    task.deploymentId,
                    task.context.numSamples
                ).then(function(points) {
                    task.context.points = points.map((p: FingerprintPoint) => ({
                            floor: p.floor,
                            lat: p.latitude,
                            long: p.longitude
                    }));
                }).then(function() {
                    self.transition(task, "goto");
                });
            }
        },
        goto: {
            _onEnter: function(task: Task) {
                const text = "Please open the LuzDeploy app below and follow the instructions. Let me know when you are 'done'!";
                const locations = task.context.points.map(
                    p => `${p.floor},${p.lat},${p.long}`
                ).join(";");
                const url = `${config.BASE_URL}/redirect.html?type=datasampler&major=65535&locations=${locations}&wid=${task.volunteerFbid}&next=${config.THREAD_URI}&base=${config.BASE_URL}`
                const buttons = [
                    {
                        "type": "web_url",
                        "title": "Open LuzDeploy",
                        "url": url,
//.........这里部分代码省略.........
开发者ID:CMUBigLab,项目名称:cmuNodeFbot,代码行数:101,代码来源:fingerprint_task.ts


示例3: function

export const SetUsernameFsm = machina.BehavioralFsm.extend({
    namespace: "set_username",
    initialState: "get_username",
    states: {
        get_username: {
            _onEnter: function(user) {
                let self = this;
                bot.FBPlatform.createButtonMessage(user.fbid)
                .text(`Hi, ${user.firstName}! In the spirit of friendly competition, we have a leaderboard that shows a score based on how many tasks you have done. Your nickname been randomly selected as "${user.username}". Would you like to change it?`)
                .postbackButton("Change Nickname", "")
                .postbackButton("Leave As Is", "")
                .send();
            },
            "change": "get_input",
            "leave": "completed"
        },
        get_input: {
            _onEnter: function(user) {
                const text = "Ok, what would you like it to be?";
                 bot.FBPlatform.sendTextMessage
            },
            "msg:done": function(task) {
                this.handle(task, "complete");
            },
            "webhook:done": function(task) {
                this.handle(task, "complete");
            }
        },
    }
});
开发者ID:CMUBigLab,项目名称:cmuNodeFbot,代码行数:30,代码来源:set_username.ts


示例4: Task

export const SweepTaskFsm = machina.BehavioralFsm.extend({
    namespace: "sweep_edge",
    initialState: "ios_check",
    states: {
        ios_check: {
            _onEnter: function(task: Task) {
                return task.assignedVolunteer().fetch()
                .then(vol => {
                    if (vol.hasIOS !== null) {
                        return this.transition(task, "download_app");
                    }
                    const text = "Some of our tasks require the use of a helper app to collect Bluetooth data. Do you have an iOS device?";
                    return vol.sendMessage(msgUtil.quickReplyMessage(text, ["yes", "no"]));
                });
            },
            "msg:yes": function(task: Task) {
                return task.assignedVolunteer().fetch()
                .then((vol: Volunteer) => vol.save({"has_ios": true}))
                .then(() => this.transition(task, "download_app"));
            },
            "msg:no": function(task: Task) {
                return task.assignedVolunteer().fetch()
                .then((vol: Volunteer) => vol.save({"has_ios": false}))
                .then((vol: Volunteer) => {
                    const text = "Unforuntately, we don't have the helper app available for other platforms yet. We will contact you when we do!";
                    return vol.sendMessage({text})
                    .then(() => vol.unassignTask());
                }).then(([vol, oldTask]) => getAndAssignVolTask(vol));
            },
        },
        download_app: {
            _onEnter: function(task: Task) {
                return task.assignedVolunteer().fetch()
                .then(vol => {
                    if (vol.appState === "installed") {
                        return this.transition(task, "goto");
                    }
                    const text = "You will need to download the app 'LuzDeploy Data Sampler'. Press the link below to open the App Store.";
                    const url = "http://appstore.com/luzdeploydatasampler";
                    const buttons = [{
                        "type": "web_url",
                        "title": "Download App",
                        "url": url,
                        "webview_height_ratio": "compact",
                    }] as Array<FBTypes.MessengerButton>;
                    return bot.FBPlatform.sendButtonMessage(
                        vol.fbid.toString(),
                        text,
                        buttons
                    ).then(() => {
                        const text = "Then come back to this conversation and let me know when you are 'done'!";
                        return vol.sendMessage(msgUtil.quickReplyMessage(text, ["done"]));
                    }); 
                });
            },
            "msg:done": function(task: Task) {
                return task.assignedVolunteer().fetch()
                .then((vol: Volunteer) => vol.save({app_state: "installed"}))
                .then(() => this.transition(task, "goto"));
            }
        },
        goto: {
            _onEnter: function(task: Task) {
                const text = "We need you to help us check which beacons are not working in the building. Please open the LuzDeploy app below and follow the instructions. Let me know when you are 'done'!";
                const params = task.instructionParams;
                const url = `${config.BASE_URL}/redirect.html?type=beaconsweeper&major=65535&edge=${params.edge}&beaconlist=${params.beacons}&wid=${task.volunteerFbid}&start=${params.start}&end=${params.end}&next=${config.THREAD_URI}&base=${config.BASE_URL}`;
                const buttons = [{
                    type: "web_url",
                    title: "Open LuzDeploy",
                    url: url,
                    webview_height_ratio: "compact",
                    messenger_extensions: true,
                }] as Array<FBTypes.MessengerButton>;
                bot.FBPlatform.sendButtonMessage(task.volunteerFbid.toString(), text, buttons);
            },
            "msg:done": function(task: Task) {
                task.saveScore(40, 0.4).then(() => this.handle(task, "complete"));
            },
            "webhook:done": function(task) {
                task.saveScore(40, 0.4).then(() => this.handle(task, "complete"));
            }
        },
    },
    getNewTask: function(vol: Volunteer): Promise<Task> {
        if (vol.hasIOS === false) {
            return null;
        }
        return Beacon.collection<Beacon>()
        .query(qb => {
            qb.where({deployment_id: vol.deploymentId})
            .whereNotNull("slot");
        }).fetch({withRelated: "slot"})
        // Temporary TODO(cgleason): remove this later
        .then(beacons => beacons.filter(
            (b: Beacon) => (b.related<BeaconSlot>("slot") as BeaconSlot).building === "GHC"
        )).then(beacons => _.groupBy(beacons,
            (b: Beacon) => (b.related<BeaconSlot>("slot") as BeaconSlot).edge)
        ).then(edges => {
            const minDate = (bs: Beacon[]) => _.min(bs.map(b => b.lastSwept)) || null;
            const edge = _(edges).keys().sortBy([
//.........这里部分代码省略.........
开发者ID:CMUBigLab,项目名称:cmuNodeFbot,代码行数:101,代码来源:sweep_task.ts


示例5: BeaconSlot

export const PlaceBeaconsTaskFsm = machina.BehavioralFsm.extend({
    namespace: "place_beacons",
    initialState: "supply",
    beaconToReturn: function(task) {
        task.context.toReturn.push(task.context.currentBeaconNumber);
        task.context.currentBeacon = null;
        task.context.numBeacons--;
        task.context.slots.pop(1);
        if (task.context.numBeacons === 0) {
            this.transition(task, "return");
            return;
        } else {
            return bot.sendMessage(
            task.volunteerFbid,
            {text: "Later, please return that beacon to the supply station. For now we will plave another beacon."}
            ).then(() => this.transition(task, "which"));
        }
    },
    states: {
        supply: {
            _onEnter: function(task: Task) {
                return task.deployment().fetch()
                .then((deployment: Deployment) => {
                    let text = `In this task you will place beacons in the environment that will be used by people with visual impairments to navigate. Please go to the Supply Station (${deployment.supplyStation}). Tell me when you are 'there'.`;
                    return bot.sendMessage(
                    task.volunteerFbid,
                    msgUtil.quickReplyMessage(text, ["there"])
                    );
                });
            },
            "msg:there": "pickup",
        },
        pickup: {
            _onEnter: function(task) {
                return task.deployment().fetch()
                .then((deployment: Deployment) => {
                    let text = `Great! ${deployment.supplyStationInstructions} Tell me how many you took (you can press a button or type a number).`;
                    return bot.sendMessage(
                    task.volunteerFbid,
                    msgUtil.quickReplyMessage(text, ["1", "3", "5", "10"])
                    );
                });
            },
            number: function(task: Task, n) {
                task.context = {
                    initialBeacons: n,
                    numBeacons: n,
                    currentSlot: null,
                    currentBeacon: null,
                    toReturn: [],
                    score: 30 + n * 10
                };
                let self = this;
                BeaconSlot.getNSlots(n, task.deploymentId)
                .then((slots) => {
                    if (slots.length === 0) {
                        task.context.numBeacons = 0;
                        let text = `I could not find any place that needs beacons. Please return all beacons.`;
                        return bot.sendMessage(task.volunteerFbid, {text: text})
                        .then(() => this.handle(task , "reject"))
                    } else if (slots.length !== n) {
                        // TODO: handle case when slots.length == 0
                        task.context.numBeacons = slots.length;
                        let text = `I could only find ${slots.length} places needing beacons. Please return any excess beacons.`;
                        return bot.sendMessage(task.volunteerFbid, {text: text})
                        .then(() => self.transition(task, "go"));
                    }
                    task.context.slots = slots.map(s => s.id);
                    return slots.invokeThen("save", {in_progress: true})
                    .then(() => self.transition(task, "go"));
                });
            },
        },
        go: {
            _onEnter: function(task) {
                task.context.currentSlot = task.context.slots.pop(1);
                return task.deployment().fetch()
                .then((deployment: Deployment) => {
                    const url = `${config.BASE_URL}/map/?advanced&hidden&map=${deployment.mapFilename}&beacon=${task.context.currentSlot}`
                    const text = `You have ${task.context.numBeacons} beacons to place. Please go to the location marked on the map below.`;
                    const buttons = [
                    {
                        "type": "web_url",
                        "title": "Open Map",
                        "url": url,
                        "webview_height_ratio": "tall",
                        "messenger_extensions": true,
                    }
                    ] as Array<FBTypes.MessengerButton>;
                    return bot.FBPlatform.sendButtonMessage(task.volunteerFbid, text, buttons)
                }).then(() => {
                    return bot.sendMessage(
                    task.volunteerFbid,
                    msgUtil.quickReplyMessage("Tell me when you are 'there'!", ["there"])
                    );
                });
            },
            "msg:there": "confirm_empty",
        },
        confirm_empty: {
//.........这里部分代码省略.........
开发者ID:CMUBigLab,项目名称:cmuNodeFbot,代码行数:101,代码来源:place_beacon_task.ts



注:本文中的machina.BehavioralFsm类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript madge.default函数代码示例发布时间:2022-05-25
下一篇:
TypeScript m2e-build-tools.m2ePackages类代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap