├── .eslintrc.json ├── .gitignore ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── agent.zip ├── agent ├── agent.json ├── entities │ ├── letter.json │ └── letter_entries_en.json ├── intents │ ├── Fallback.json │ ├── Guess Letter or Word.json │ ├── Guess Letter or Word_usersays_en.json │ ├── Instructions.json │ ├── Instructions_usersays_en.json │ ├── Play Again.json │ ├── Play Again_usersays_en.json │ ├── Quit.json │ ├── Quit_usersays_en.json │ ├── Start Game.json │ ├── Start Game_usersays_en.json │ └── Welcome.json └── package.json ├── firebase.json ├── functions ├── index.js ├── package.json └── util │ └── dictionary.js └── public ├── 404.html ├── assets ├── background.png ├── fonts │ └── bitmap │ │ ├── google-sans-font.png │ │ └── google-sans-font.xml ├── snow-pal.json ├── snow-pal.png ├── snow-pal.sketch ├── win-lose.json ├── win-lose.png ├── win-lose.sketch ├── you-lose.png └── you-win.png ├── css └── main.css ├── index.html └── js ├── assistant.js ├── game.js └── log.js /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "parserOptions": { 3 | "ecmaVersion": 2018, 4 | "sourceType": "script" 5 | }, 6 | "extends": ["google"], 7 | "env": { 8 | "node": true 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | functions/node_modules 2 | .firebaserc 3 | package-lock.json 4 | yarn.lock 5 | .firebase 6 | *.log 7 | .DS_Store 8 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to become a contributor and submit your own code 2 | 3 | ## Contributor License Agreements 4 | 5 | We'd love to accept your sample apps and patches! Before we can take them, we 6 | have to jump a couple of legal hurdles. 7 | 8 | Please fill out either the individual or corporate Contributor License Agreement 9 | (CLA). 10 | 11 | * If you are an individual writing original source code and you're sure you 12 | own the intellectual property, then you'll need to sign an [individual CLA](https://developers.google.com/open-source/cla/individual). 13 | * If you work for a company that wants to allow you to contribute your work, 14 | then you'll need to sign a [corporate CLA](https://developers.google.com/open-source/cla/corporate). 15 | 16 | Follow either of the two links above to access the appropriate CLA and 17 | instructions for how to sign and return it. Once we receive it, we'll be able to 18 | accept your pull requests. 19 | 20 | ## Contributing A Patch 21 | 22 | 1. Submit an issue describing your proposed change to the repo in question. 23 | 1. The repo owner will respond to your issue promptly. 24 | 1. If your proposed change is accepted, and you haven't already done so, sign a 25 | Contributor License Agreement (see details above). 26 | 1. Fork the desired repo, develop and test your code changes. 27 | 1. Ensure that your code adheres to the existing style in the sample to which 28 | you are contributing. Refer to the 29 | [Google Cloud Platform Samples Style Guide](https://github.com/GoogleCloudPlatform/Template/wiki/style.html) for the 30 | recommended coding standards for this organization. 31 | 1. Ensure that your code has an appropriate set of unit tests which all pass. 32 | 1. Submit a pull request. 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Actions on Google: Snow Pal Interactive Canvas Sample 2 | 3 | *:warning: Warning: Conversational Actions will be deprecated on June 13, 2023. For more information, 4 | see [Conversational Actions Sunset](https://goo.gle/ca-sunset).* 5 | 6 | 7 | This sample demonstrates how to create an [Interactive Canvas](https://developers.google.com/assistant/interactivecanvas/) experience using Actions on Google for the Google Assistant. It uses the [Node.js client library](https://github.com/actions-on-google/actions-on-google-nodejs) and is deployed on [Cloud Functions for Firebase](https://firebase.google.com/docs/functions/) and [Firebase Hosting](https://firebase.google.com/docs/hosting). 8 | 9 | :warning: This code sample was built using Dialogflow. We now recommend using [Actions Builder or the Actions SDK](https://developers.google.com/assistant/conversational/overview) to develop, test, and deploy Conversational Actions. 10 | 11 | ## Setup Instructions 12 | ### Prerequisites 13 | 1. Node.js and NPM 14 | + We recommend installing using [nvm for Linux/Mac](https://github.com/creationix/nvm) and [nvm-windows for Windows](https://github.com/coreybutler/nvm-windows) 15 | 1. Install the [Firebase CLI](https://developers.google.com/assistant/actions/dialogflow/deploy-fulfillment) 16 | + We recommend using MAJOR version `6` with `6.5.0` or above, `npm install -g firebase-tools@^6.5.0` 17 | + Run `firebase login` with your Google account 18 | 19 | ### Configuration 20 | #### Actions Console 21 | 1. From the [Actions on Google Console](https://console.actions.google.com/), add a new project > **Create Project** > under **More options** > **Conversational** 22 | 1. Click **Deploy** in the top menu. Then, click **Additional information**. 23 | 1. Under **Category**, select **Games & fun** 24 | 1. Under **Interactive Canvas** *Do your Actions use Interactive Canvas?*, check **Yes** 25 | 1. Click **Develop** in the top menu. Then, click **Actions** > **Add Your First Action** > **Custom intent** > **BUILD** (this will bring you to the Dialogflow console) > Select language and time zone > **CREATE**. 26 | 1. In the Dialogflow console, go to **Settings** ⚙ > **Export and Import** > **Restore from zip** using the `agent.zip` in this sample's directory. 27 | 28 | 29 | #### Firebase Deployment 30 | 1. On your local machine, in the `functions` directory, run `npm install` 31 | 1. Run `firebase deploy --project {PROJECT_ID}` to deploy the function and hosting 32 | + To find your **Project ID**: In [Dialogflow console](https://console.dialogflow.com/) under **Settings** ⚙ > **General** tab > **Project ID**. 33 | 34 | #### Dialogflow Console 35 | 1. Return to the [Dialogflow Console](https://console.dialogflow.com) > select **Fulfillment** > **Enable** Webhook > Set **URL** to the **Function URL** that was returned after the deploy command > **SAVE**. 36 | ``` 37 | Function URL (dialogflowFirebaseFulfillment): https://${REGION}-${PROJECT_ID}.cloudfunctions.net/dialogflowFirebaseFulfillment 38 | ``` 39 | 1. From the left navigation menu, click **Integrations** > **Integration Settings** under Google Assistant > Enable **Auto-preview changes** > **Test** to open the Actions on Google simulator then say or type `Talk to my test app`. 40 | 41 | ### Running this Sample 42 | + You can test your Action on any Google Assistant-enabled device on which the Assistant is signed into the same account used to create this project. Just say or type, “OK Google, talk to my test app”. 43 | + You can also use the Actions on Google Console simulator to test most features and preview on-device behavior. 44 | 45 | ## References & Issues 46 | + Questions? Go to [StackOverflow](https://stackoverflow.com/questions/tagged/actions-on-google), [Assistant Developer Community on Reddit](https://www.reddit.com/r/GoogleAssistantDev/) or [Support](https://developers.google.com/assistant/support). 47 | + For bugs, please report an issue on Github. 48 | + Actions on Google [Interactive Canvas Documentation](https://developers.google.com/assistant/interactivecanvas/) 49 | + Actions on Google [Documentation](https://developers.google.com/assistant) 50 | + Actions on Google [Codelabs](https://codelabs.developers.google.com/?cat=Assistant) 51 | + [Webhook Boilerplate Template](https://github.com/actions-on-google/dialogflow-webhook-boilerplate-nodejs) for Actions on Google 52 | 53 | ## Make Contributions 54 | Please read and follow the steps in the [CONTRIBUTING.md](CONTRIBUTING.md). 55 | 56 | ## License 57 | See [LICENSE](LICENSE). 58 | 59 | ## Terms 60 | Your use of this sample is subject to, and by using or downloading the sample files you agree to comply with, the [Google APIs Terms of Service](https://developers.google.com/terms/). 61 | -------------------------------------------------------------------------------- /agent.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/actions-on-google/dialogflow-snowman-nodejs/a102fee2810b60745481e2b8d7da467cf525706e/agent.zip -------------------------------------------------------------------------------- /agent/agent.json: -------------------------------------------------------------------------------- 1 | { 2 | "description": "", 3 | "language": "en", 4 | "shortDescription": "", 5 | "examples": "", 6 | "linkToDocs": "", 7 | "disableInteractionLogs": false, 8 | "disableStackdriverLogs": true, 9 | "googleAssistant": { 10 | "googleAssistantCompatible": true, 11 | "project": "", 12 | "welcomeIntentSignInRequired": false, 13 | "startIntents": [], 14 | "systemIntents": [], 15 | "endIntentIds": [ 16 | "0a44ec19-583e-4a55-9507-a7a3141280a2" 17 | ], 18 | "oAuthLinking": { 19 | "required": false, 20 | "providerId": "", 21 | "authorizationUrl": "", 22 | "tokenUrl": "", 23 | "scopes": "", 24 | "privacyPolicyUrl": "", 25 | "grantType": "AUTH_CODE_GRANT" 26 | }, 27 | "voiceType": "MALE_1", 28 | "capabilities": [], 29 | "env": "", 30 | "protocolVersion": "V2", 31 | "autoPreviewEnabled": true, 32 | "isDeviceAgent": false 33 | }, 34 | "defaultTimezone": "America/Los_Angeles", 35 | "webhook": { 36 | "url": "https://us-central1-PROJECT_ID.cloudfunctions.net/dialogflowFirebaseFulfillment", 37 | "username": "", 38 | "headers": { 39 | "": "" 40 | }, 41 | "available": true, 42 | "useForDomains": false, 43 | "cloudFunctionsEnabled": false, 44 | "cloudFunctionsInitialized": false 45 | }, 46 | "isPrivate": true, 47 | "customClassifierMode": "use.after", 48 | "mlMinConfidence": 0.3, 49 | "supportedLanguages": [], 50 | "onePlatformApiVersion": "v2", 51 | "analyzeQueryTextSentiment": false, 52 | "enabledKnowledgeBaseNames": [], 53 | "knowledgeServiceConfidenceAdjustment": -0.4, 54 | "dialogBuilderMode": false, 55 | "baseActionPackagesUrl": "" 56 | } -------------------------------------------------------------------------------- /agent/entities/letter.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "c6b738cb-4cbc-47e9-8a3c-6b8fc72fabc3", 3 | "name": "letter", 4 | "isOverridable": true, 5 | "isEnum": false, 6 | "isRegexp": false, 7 | "automatedExpansion": false, 8 | "allowFuzzyExtraction": false 9 | } -------------------------------------------------------------------------------- /agent/entities/letter_entries_en.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "value": "a", 4 | "synonyms": [ 5 | "a" 6 | ] 7 | }, 8 | { 9 | "value": "b", 10 | "synonyms": [ 11 | "b" 12 | ] 13 | }, 14 | { 15 | "value": "c", 16 | "synonyms": [ 17 | "c" 18 | ] 19 | }, 20 | { 21 | "value": "d", 22 | "synonyms": [ 23 | "d" 24 | ] 25 | }, 26 | { 27 | "value": "e", 28 | "synonyms": [ 29 | "e" 30 | ] 31 | }, 32 | { 33 | "value": "f", 34 | "synonyms": [ 35 | "f" 36 | ] 37 | }, 38 | { 39 | "value": "g", 40 | "synonyms": [ 41 | "g" 42 | ] 43 | }, 44 | { 45 | "value": "h", 46 | "synonyms": [ 47 | "h" 48 | ] 49 | }, 50 | { 51 | "value": "i", 52 | "synonyms": [ 53 | "i" 54 | ] 55 | }, 56 | { 57 | "value": "j", 58 | "synonyms": [ 59 | "j" 60 | ] 61 | }, 62 | { 63 | "value": "k", 64 | "synonyms": [ 65 | "k" 66 | ] 67 | }, 68 | { 69 | "value": "l", 70 | "synonyms": [ 71 | "l" 72 | ] 73 | }, 74 | { 75 | "value": "m", 76 | "synonyms": [ 77 | "m" 78 | ] 79 | }, 80 | { 81 | "value": "n", 82 | "synonyms": [ 83 | "n" 84 | ] 85 | }, 86 | { 87 | "value": "o", 88 | "synonyms": [ 89 | "o" 90 | ] 91 | }, 92 | { 93 | "value": "p", 94 | "synonyms": [ 95 | "p" 96 | ] 97 | }, 98 | { 99 | "value": "q", 100 | "synonyms": [ 101 | "q" 102 | ] 103 | }, 104 | { 105 | "value": "r", 106 | "synonyms": [ 107 | "r" 108 | ] 109 | }, 110 | { 111 | "value": "s", 112 | "synonyms": [ 113 | "s" 114 | ] 115 | }, 116 | { 117 | "value": "t", 118 | "synonyms": [ 119 | "t" 120 | ] 121 | }, 122 | { 123 | "value": "u", 124 | "synonyms": [ 125 | "u" 126 | ] 127 | }, 128 | { 129 | "value": "v", 130 | "synonyms": [ 131 | "v" 132 | ] 133 | }, 134 | { 135 | "value": "w", 136 | "synonyms": [ 137 | "w" 138 | ] 139 | }, 140 | { 141 | "value": "x", 142 | "synonyms": [ 143 | "x" 144 | ] 145 | }, 146 | { 147 | "value": "y", 148 | "synonyms": [ 149 | "y" 150 | ] 151 | }, 152 | { 153 | "value": "z", 154 | "synonyms": [ 155 | "z" 156 | ] 157 | } 158 | ] -------------------------------------------------------------------------------- /agent/intents/Fallback.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "ef09eaa7-72b3-4fe0-8551-84096acca188", 3 | "name": "Fallback", 4 | "auto": true, 5 | "contexts": [], 6 | "responses": [ 7 | { 8 | "resetContexts": false, 9 | "affectedContexts": [], 10 | "parameters": [], 11 | "messages": [], 12 | "defaultResponsePlatforms": {}, 13 | "speech": [] 14 | } 15 | ], 16 | "priority": 500000, 17 | "webhookUsed": true, 18 | "webhookForSlotFilling": false, 19 | "fallbackIntent": true, 20 | "events": [], 21 | "conditionalResponses": [], 22 | "condition": "", 23 | "conditionalFollowupEvents": [] 24 | } -------------------------------------------------------------------------------- /agent/intents/Guess Letter or Word.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "bd490bf1-d6b8-4e8e-90d5-8a059ca12474", 3 | "name": "Guess Letter or Word", 4 | "auto": true, 5 | "contexts": [], 6 | "responses": [ 7 | { 8 | "resetContexts": false, 9 | "affectedContexts": [], 10 | "parameters": [ 11 | { 12 | "id": "3268c5a2-d5be-417b-be63-0f3faf551414", 13 | "required": false, 14 | "dataType": "@sys.any", 15 | "name": "letterOrWord", 16 | "value": "$letterOrWord", 17 | "promptMessages": [], 18 | "noMatchPromptMessages": [], 19 | "noInputPromptMessages": [], 20 | "outputDialogContexts": [], 21 | "isList": false 22 | } 23 | ], 24 | "messages": [ 25 | { 26 | "type": 0, 27 | "lang": "en", 28 | "condition": "", 29 | "speech": [] 30 | } 31 | ], 32 | "defaultResponsePlatforms": {}, 33 | "speech": [] 34 | } 35 | ], 36 | "priority": 500000, 37 | "webhookUsed": true, 38 | "webhookForSlotFilling": false, 39 | "fallbackIntent": false, 40 | "events": [], 41 | "conditionalResponses": [], 42 | "condition": "", 43 | "conditionalFollowupEvents": [] 44 | } -------------------------------------------------------------------------------- /agent/intents/Guess Letter or Word_usersays_en.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": "43c07458-1ed8-4327-a42d-7901022afd0f", 4 | "data": [ 5 | { 6 | "text": "France", 7 | "alias": "letterOrWord", 8 | "meta": "@sys.any", 9 | "userDefined": true 10 | } 11 | ], 12 | "isTemplate": false, 13 | "count": 0, 14 | "updated": 0 15 | }, 16 | { 17 | "id": "aff6f0ec-ad01-4e4d-9fd8-4e573f6c4bb4", 18 | "data": [ 19 | { 20 | "text": "it\u0027s ", 21 | "userDefined": false 22 | }, 23 | { 24 | "text": "google", 25 | "alias": "letterOrWord", 26 | "meta": "@sys.any", 27 | "userDefined": true 28 | } 29 | ], 30 | "isTemplate": false, 31 | "count": 0, 32 | "updated": 0 33 | }, 34 | { 35 | "id": "170afa3c-97f6-47f5-8b7a-043be9cd5ad4", 36 | "data": [ 37 | { 38 | "text": "I guess it\u0027s ", 39 | "userDefined": false 40 | }, 41 | { 42 | "text": "house", 43 | "alias": "letterOrWord", 44 | "meta": "@sys.any", 45 | "userDefined": true 46 | } 47 | ], 48 | "isTemplate": false, 49 | "count": 0, 50 | "updated": 0 51 | }, 52 | { 53 | "id": "e835b7e5-4469-4764-884c-17b4989f9d6c", 54 | "data": [ 55 | { 56 | "text": "it\u0027s ", 57 | "userDefined": false 58 | }, 59 | { 60 | "text": "sombrero", 61 | "alias": "letterOrWord", 62 | "meta": "@sys.any", 63 | "userDefined": true 64 | } 65 | ], 66 | "isTemplate": false, 67 | "count": 0, 68 | "updated": 0 69 | }, 70 | { 71 | "id": "dcd5523a-2bc6-4072-8819-362e2e5b0120", 72 | "data": [ 73 | { 74 | "text": "it\u0027s the word ", 75 | "userDefined": false 76 | }, 77 | { 78 | "text": "apartment", 79 | "alias": "letterOrWord", 80 | "meta": "@sys.any", 81 | "userDefined": true 82 | } 83 | ], 84 | "isTemplate": false, 85 | "count": 0, 86 | "updated": 0 87 | }, 88 | { 89 | "id": "07b6a3ed-d0a3-41a3-b7ef-6836e50b042b", 90 | "data": [ 91 | { 92 | "text": "what about ", 93 | "userDefined": false 94 | }, 95 | { 96 | "text": "panfleto", 97 | "alias": "letterOrWord", 98 | "meta": "@sys.any", 99 | "userDefined": true 100 | } 101 | ], 102 | "isTemplate": false, 103 | "count": 0, 104 | "updated": 0 105 | }, 106 | { 107 | "id": "a0ecb8e7-8dd5-46d0-8e41-a72cc5e96b53", 108 | "data": [ 109 | { 110 | "text": "try ", 111 | "userDefined": false 112 | }, 113 | { 114 | "text": "Colombia", 115 | "alias": "letterOrWord", 116 | "meta": "@sys.any", 117 | "userDefined": true 118 | } 119 | ], 120 | "isTemplate": false, 121 | "count": 0, 122 | "updated": 0 123 | }, 124 | { 125 | "id": "ba2e53de-e5f5-4ed4-b3fa-80da0dc0d62d", 126 | "data": [ 127 | { 128 | "text": "the word is ", 129 | "userDefined": false 130 | }, 131 | { 132 | "text": "home", 133 | "alias": "letterOrWord", 134 | "meta": "@sys.any", 135 | "userDefined": true 136 | } 137 | ], 138 | "isTemplate": false, 139 | "count": 0, 140 | "updated": 0 141 | }, 142 | { 143 | "id": "29928c04-dd88-4f6c-af19-1803fd6e7ffe", 144 | "data": [ 145 | { 146 | "text": "i guess ", 147 | "userDefined": false 148 | }, 149 | { 150 | "text": "z", 151 | "alias": "letterOrWord", 152 | "meta": "@sys.any", 153 | "userDefined": true 154 | } 155 | ], 156 | "isTemplate": false, 157 | "count": 0, 158 | "updated": 0 159 | }, 160 | { 161 | "id": "87c2f068-2fad-419b-a1a4-fd70fdfca53e", 162 | "data": [ 163 | { 164 | "text": "letter ", 165 | "userDefined": false 166 | }, 167 | { 168 | "text": "a", 169 | "alias": "letterOrWord", 170 | "meta": "@sys.any", 171 | "userDefined": true 172 | } 173 | ], 174 | "isTemplate": false, 175 | "count": 0, 176 | "updated": 0 177 | } 178 | ] -------------------------------------------------------------------------------- /agent/intents/Instructions.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "42c877a7-4b2c-4dc4-a039-86cc175f8b03", 3 | "name": "Instructions", 4 | "auto": true, 5 | "contexts": [], 6 | "responses": [ 7 | { 8 | "resetContexts": false, 9 | "affectedContexts": [], 10 | "parameters": [], 11 | "messages": [], 12 | "defaultResponsePlatforms": {}, 13 | "speech": [] 14 | } 15 | ], 16 | "priority": 500000, 17 | "webhookUsed": true, 18 | "webhookForSlotFilling": false, 19 | "fallbackIntent": false, 20 | "events": [], 21 | "conditionalResponses": [], 22 | "condition": "", 23 | "conditionalFollowupEvents": [] 24 | } -------------------------------------------------------------------------------- /agent/intents/Instructions_usersays_en.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": "2c8f8fb0-8c9b-49ac-bbc0-35cc002c713e", 4 | "data": [ 5 | { 6 | "text": "help", 7 | "userDefined": false 8 | } 9 | ], 10 | "isTemplate": false, 11 | "count": 0, 12 | "updated": 0 13 | }, 14 | { 15 | "id": "47e61ef2-01a2-4f4e-b9c8-f8023140bafb", 16 | "data": [ 17 | { 18 | "text": "ask for help", 19 | "userDefined": false 20 | } 21 | ], 22 | "isTemplate": false, 23 | "count": 0, 24 | "updated": 0 25 | }, 26 | { 27 | "id": "2de0503b-18d9-4a27-9ba2-ef38544dbf25", 28 | "data": [ 29 | { 30 | "text": "what do you want me to do?", 31 | "userDefined": false 32 | } 33 | ], 34 | "isTemplate": false, 35 | "count": 0, 36 | "updated": 0 37 | }, 38 | { 39 | "id": "7178eef2-323b-4973-aa85-206528b2d082", 40 | "data": [ 41 | { 42 | "text": "what are the instructions?", 43 | "userDefined": false 44 | } 45 | ], 46 | "isTemplate": false, 47 | "count": 0, 48 | "updated": 0 49 | }, 50 | { 51 | "id": "1db72cf0-7c89-4e73-bb74-9ab7abf3132b", 52 | "data": [ 53 | { 54 | "text": "instructions", 55 | "userDefined": false 56 | } 57 | ], 58 | "isTemplate": false, 59 | "count": 0, 60 | "updated": 0 61 | } 62 | ] -------------------------------------------------------------------------------- /agent/intents/Play Again.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "f49138fb-ea3d-4d16-803d-89122fad5525", 3 | "name": "Play Again", 4 | "auto": true, 5 | "contexts": [], 6 | "responses": [ 7 | { 8 | "resetContexts": false, 9 | "affectedContexts": [], 10 | "parameters": [], 11 | "messages": [ 12 | { 13 | "type": 0, 14 | "lang": "en", 15 | "condition": "", 16 | "speech": [] 17 | } 18 | ], 19 | "defaultResponsePlatforms": {}, 20 | "speech": [] 21 | } 22 | ], 23 | "priority": 500000, 24 | "webhookUsed": true, 25 | "webhookForSlotFilling": false, 26 | "fallbackIntent": false, 27 | "events": [], 28 | "conditionalResponses": [], 29 | "condition": "", 30 | "conditionalFollowupEvents": [] 31 | } -------------------------------------------------------------------------------- /agent/intents/Play Again_usersays_en.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": "37fd528e-8b53-4117-b98c-c9259e45d46d", 4 | "data": [ 5 | { 6 | "text": "play another game", 7 | "userDefined": false 8 | } 9 | ], 10 | "isTemplate": false, 11 | "count": 0, 12 | "updated": 0 13 | }, 14 | { 15 | "id": "b875b4eb-7067-4abf-b767-9c6565b3a000", 16 | "data": [ 17 | { 18 | "text": "another word", 19 | "userDefined": false 20 | } 21 | ], 22 | "isTemplate": false, 23 | "count": 0, 24 | "updated": 0 25 | }, 26 | { 27 | "id": "64b5c96a-2f53-448c-9841-be0974b975e4", 28 | "data": [ 29 | { 30 | "text": "another one", 31 | "userDefined": false 32 | } 33 | ], 34 | "isTemplate": false, 35 | "count": 0, 36 | "updated": 0 37 | }, 38 | { 39 | "id": "818643e1-4b6a-47ad-ab4e-13c9c25ccae7", 40 | "data": [ 41 | { 42 | "text": "restart", 43 | "userDefined": false 44 | } 45 | ], 46 | "isTemplate": false, 47 | "count": 0, 48 | "updated": 0 49 | }, 50 | { 51 | "id": "b21845c8-3012-45e3-ae17-d50f5b6f6922", 52 | "data": [ 53 | { 54 | "text": "play again", 55 | "userDefined": false 56 | } 57 | ], 58 | "isTemplate": false, 59 | "count": 0, 60 | "updated": 0 61 | } 62 | ] -------------------------------------------------------------------------------- /agent/intents/Quit.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "0a44ec19-583e-4a55-9507-a7a3141280a2", 3 | "name": "Quit", 4 | "auto": true, 5 | "contexts": [], 6 | "responses": [ 7 | { 8 | "resetContexts": false, 9 | "affectedContexts": [], 10 | "parameters": [], 11 | "messages": [ 12 | { 13 | "type": 0, 14 | "lang": "en", 15 | "condition": "", 16 | "speech": [] 17 | } 18 | ], 19 | "defaultResponsePlatforms": {}, 20 | "speech": [] 21 | } 22 | ], 23 | "priority": 500000, 24 | "webhookUsed": false, 25 | "webhookForSlotFilling": false, 26 | "fallbackIntent": false, 27 | "events": [], 28 | "conditionalResponses": [], 29 | "condition": "", 30 | "conditionalFollowupEvents": [] 31 | } -------------------------------------------------------------------------------- /agent/intents/Quit_usersays_en.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": "0ec1ea7a-2c96-4451-9f4b-b05c4a04db60", 4 | "data": [ 5 | { 6 | "text": "no", 7 | "userDefined": false 8 | } 9 | ], 10 | "isTemplate": false, 11 | "count": 0, 12 | "updated": 0 13 | }, 14 | { 15 | "id": "424f4e7a-2d49-4e98-9a42-e3add9af9f2c", 16 | "data": [ 17 | { 18 | "text": "quit game", 19 | "userDefined": false 20 | } 21 | ], 22 | "isTemplate": false, 23 | "count": 0, 24 | "updated": 0 25 | }, 26 | { 27 | "id": "e39906ba-fb02-4c53-953f-9a7594a15950", 28 | "data": [ 29 | { 30 | "text": "goodbye", 31 | "userDefined": false 32 | } 33 | ], 34 | "isTemplate": false, 35 | "count": 0, 36 | "updated": 0 37 | }, 38 | { 39 | "id": "b63a7f10-1bfe-44f1-ae24-a6d005535e5b", 40 | "data": [ 41 | { 42 | "text": "I\u0027m done", 43 | "userDefined": false 44 | } 45 | ], 46 | "isTemplate": false, 47 | "count": 0, 48 | "updated": 0 49 | }, 50 | { 51 | "id": "ce021760-9e84-4fec-a9af-c9d3711cb6ec", 52 | "data": [ 53 | { 54 | "text": "quit", 55 | "userDefined": false 56 | } 57 | ], 58 | "isTemplate": false, 59 | "count": 0, 60 | "updated": 0 61 | } 62 | ] -------------------------------------------------------------------------------- /agent/intents/Start Game.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "f2eb67aa-2d88-4cab-98b4-778363a72f8a", 3 | "name": "Start Game", 4 | "auto": true, 5 | "contexts": [], 6 | "responses": [ 7 | { 8 | "resetContexts": false, 9 | "affectedContexts": [], 10 | "parameters": [], 11 | "messages": [ 12 | { 13 | "type": 0, 14 | "lang": "en", 15 | "condition": "", 16 | "speech": [] 17 | } 18 | ], 19 | "defaultResponsePlatforms": {}, 20 | "speech": [] 21 | } 22 | ], 23 | "priority": 500000, 24 | "webhookUsed": true, 25 | "webhookForSlotFilling": false, 26 | "fallbackIntent": false, 27 | "events": [], 28 | "conditionalResponses": [], 29 | "condition": "", 30 | "conditionalFollowupEvents": [] 31 | } -------------------------------------------------------------------------------- /agent/intents/Start Game_usersays_en.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": "f75d4426-0be3-45ee-8f5f-6e3ce35680d8", 4 | "data": [ 5 | { 6 | "text": "yes", 7 | "userDefined": false 8 | } 9 | ], 10 | "isTemplate": false, 11 | "count": 0, 12 | "updated": 0 13 | } 14 | ] -------------------------------------------------------------------------------- /agent/intents/Welcome.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "df333e1d-586b-4bc2-b3a2-b77296a4a6e5", 3 | "name": "Welcome", 4 | "auto": true, 5 | "contexts": [], 6 | "responses": [ 7 | { 8 | "resetContexts": false, 9 | "affectedContexts": [], 10 | "parameters": [], 11 | "messages": [], 12 | "defaultResponsePlatforms": {}, 13 | "speech": [] 14 | } 15 | ], 16 | "priority": 500000, 17 | "webhookUsed": true, 18 | "webhookForSlotFilling": false, 19 | "fallbackIntent": false, 20 | "events": [ 21 | { 22 | "name": "WELCOME" 23 | }, 24 | { 25 | "name": "actions_intent_PLAY_GAME" 26 | } 27 | ], 28 | "conditionalResponses": [], 29 | "condition": "", 30 | "conditionalFollowupEvents": [] 31 | } -------------------------------------------------------------------------------- /agent/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "1.0.0" 3 | } -------------------------------------------------------------------------------- /firebase.json: -------------------------------------------------------------------------------- 1 | { 2 | "hosting": { 3 | "public": "public", 4 | "headers": [ 5 | { 6 | "source": "**", 7 | "headers": [ 8 | { 9 | "key": "Cache-Control", 10 | "value": "no-cache,no-store,must-revalidate" 11 | }, 12 | { 13 | "key": "Access-Control-Allow-Origin", 14 | "value": "*" 15 | }, 16 | { 17 | "key": "Access-Control-Expose-Headers", 18 | "value": "ETag" 19 | } 20 | ] 21 | } 22 | ] 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /functions/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2019 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 'use strict'; 18 | 19 | const functions = require('firebase-functions'); 20 | const {dialogflow, HtmlResponse} = require('actions-on-google'); 21 | const Dictionary = require('./util/dictionary.js'); 22 | 23 | const firebaseConfig = JSON.parse(process.env.FIREBASE_CONFIG); 24 | const app = dialogflow({debug: true}); 25 | 26 | const MAX_INCORRECT_GUESSES = 5; 27 | 28 | const INSTRUCTIONS = 29 | `Try to figure out the word by guessing letters that you think are in the word. ` + 30 | `Figure out the word before Snow Pal melts to win the game! After 5 ` + 31 | `incorrect guesses, Snow Pal melts and the game is over. ` + 32 | `If you know the word, you can say, for instance, “The word is penguin.” ` + 33 | `You can try another word, or ask for help.`; 34 | 35 | const PLAY_AGAIN_INSTRUCTIONS = `Would you like to play again or quit?`; 36 | 37 | const WELCOME_BACK_GREETINGS = [ 38 | `Hey, you're back to Snow Pal!`, 39 | `Welcome back to Snow Pal!`, 40 | `I'm glad you're back to play!`, 41 | `Hey there, you made it! Let's play Snow Pal.` 42 | ]; 43 | 44 | const START_GAME_RESPONSES = [ 45 | `Try guessing a letter in the word, or guess the entire word if you think you know what it is.`, 46 | `Try guessing a letter in the word, or guess the entire word if you're feeling confident!`, 47 | `Try guessing a letter in the word or guessing the word.`, 48 | `Try guessing a letter in the word or guessing the word.` 49 | ]; 50 | 51 | const RIGHT_RESPONSES = ['Right on! Good guess.', 'Splendid!', 52 | 'Wonderful! Keep going!', 'Easy peasy lemon squeezy!', 'Easy as pie!']; 53 | 54 | const WRONG_RESPONSES = [`Whoops, that letter isn’t in the word. Try again!`, 55 | 'Try again!', 'You can do this!', 'Incorrect. Keep on trying!']; 56 | 57 | const WIN_RESPONSES = ['Congratulations and BRAVO!', 58 | 'You did it! So proud of you!', 59 | 'Well done!', 'I’m happy for you!', 60 | 'This is awesome! You’re awesome! Way to go!']; 61 | 62 | const DICTIONARY = new Dictionary(); 63 | 64 | /** 65 | * Pick a random item from an array. This is to make 66 | * responses more conversational. 67 | * 68 | * @param {array} array representing a list of elements. 69 | * @return {string} item from an array. 70 | */ 71 | const randomArrayItem = (array) => { 72 | return array[Math.floor(Math.random() * array.length)]; 73 | }; 74 | 75 | /** 76 | * Updates the string to display to the user with the guessed 77 | * letter or word. 78 | * 79 | * @param {string} letterOrWord the letter or word used to update the display string. 80 | */ 81 | const updateWordToDisplay = (conv, letterOrWord) => { 82 | if(letterOrWord === conv.data.correctWord) { 83 | conv.data.wordToDisplay = conv.data.correctWord; 84 | } else { 85 | conv.data.correctWord.split('').forEach((character, index) => { 86 | if (letterOrWord === character) { 87 | conv.data.wordToDisplay = conv.data.wordToDisplay.substr(0, index) + 88 | letterOrWord + conv.data.wordToDisplay.substr(index + letterOrWord.length) 89 | } 90 | }); 91 | } 92 | } 93 | 94 | app.intent('Welcome', (conv) => { 95 | if (conv.user.last.seen) { 96 | conv.ask(`${randomArrayItem(WELCOME_BACK_GREETINGS)} Would you like to start playing the game?`); 97 | } else { 98 | conv.ask(`Welcome to Snow Pal! Would you like to start playing the game?`); 99 | } 100 | conv.ask(new HtmlResponse({ 101 | url: `https://${firebaseConfig.projectId}.firebaseapp.com`, 102 | })); 103 | }); 104 | 105 | app.intent('Start Game', (conv) => { 106 | if (conv.user.last.seen) { 107 | conv.ask(randomArrayItem(START_GAME_RESPONSES)); 108 | } else { 109 | conv.ask(`${INSTRUCTIONS}`); 110 | } 111 | conv.data.incorrectGuesses = 0; 112 | // Generate new word to guess 113 | conv.data.correctWord = DICTIONARY.getWord().toLocaleUpperCase(); 114 | conv.data.wordToDisplay = '_'.repeat(conv.data.correctWord.length); 115 | conv.ask(new HtmlResponse({ 116 | data: { 117 | state: 'NEW_GAME', 118 | wordToDisplay: conv.data.wordToDisplay 119 | }, 120 | })); 121 | }); 122 | 123 | app.intent('Fallback', (conv) => { 124 | conv.ask(`I don’t understand. Try guessing a letter!`); 125 | conv.ask(new HtmlResponse()); 126 | }); 127 | 128 | /** 129 | * Guess a letter or word from Snow Pal. 130 | * 131 | * @param {conv} standard Actions on Google conversation object. 132 | * @param {string} letterOrWord from A-Z. 133 | */ 134 | app.intent('Guess Letter or Word', (conv, {letterOrWord}) => { 135 | conv.ask(`Let's see if ${letterOrWord} is there...`); 136 | letterOrWord = letterOrWord.toLocaleUpperCase(); 137 | // Check if the letter guessed is part of the correct word. 138 | let correctGuess = conv.data.correctWord.indexOf(letterOrWord) > -1; 139 | if (correctGuess) { 140 | // Update the word to be displayed to the user with the newly guessed letter. 141 | updateWordToDisplay(conv, letterOrWord); 142 | // Check if the correct guess will result in the user winning the game. 143 | const userHasWon = conv.data.wordToDisplay === conv.data.correctWord; 144 | if (userHasWon) { 145 | conv.ask(`${letterOrWord} is right. That spells ${conv.data.correctWord}! ${randomArrayItem(WIN_RESPONSES)} ` + 146 | `${PLAY_AGAIN_INSTRUCTIONS}`); 147 | // User has won the game. Update game to WIN state, and pass the 148 | // updated word to display to the front-end. 149 | conv.ask(new HtmlResponse({ 150 | data: { 151 | state: 'WIN', 152 | wordToDisplay: conv.data.wordToDisplay 153 | }, 154 | })); 155 | } else { 156 | conv.ask(`${letterOrWord} is right. ${randomArrayItem(RIGHT_RESPONSES)}`); 157 | // User made a correct guess. Update game to CORRECT state, and pass the 158 | // updated word to display to the front-end. 159 | conv.ask(new HtmlResponse({ 160 | data: { 161 | state: 'CORRECT', 162 | wordToDisplay: conv.data.wordToDisplay 163 | }, 164 | })); 165 | } 166 | } 167 | else { 168 | conv.data.incorrectGuesses++; 169 | // Check if the user has exceeded the maximum amount of max guesses allowed. 170 | const userHasLost = conv.data.incorrectGuesses >= MAX_INCORRECT_GUESSES 171 | if (userHasLost) { 172 | conv.ask(`Sorry, you lost. The word is ${conv.data.correctWord}.` + 173 | `${PLAY_AGAIN_INSTRUCTIONS}`); 174 | // User has lost the game. Update game to LOSE state. 175 | conv.ask(new HtmlResponse({ 176 | data: { 177 | state: 'LOSE', 178 | }, 179 | })); 180 | } else { 181 | conv.ask(`${letterOrWord} is wrong. ${randomArrayItem(WRONG_RESPONSES)}`); 182 | // User made an incorrect guess. Update game to INCORRECT state. 183 | conv.ask(new HtmlResponse({ 184 | data: { 185 | state: 'INCORRECT', 186 | }, 187 | })); 188 | } 189 | } 190 | }); 191 | 192 | /** 193 | * Provide standard instructions about the game. 194 | * 195 | * @param {conv} standard Actions on Google conversation object. 196 | */ 197 | app.intent('Instructions', (conv) => { 198 | conv.ask(`${INSTRUCTIONS}`); 199 | conv.ask(new HtmlResponse()); 200 | }); 201 | 202 | /** 203 | * Trigger to re-play the game again at anytime. 204 | * 205 | * @param {conv} standard Actions on Google conversation object. 206 | */ 207 | app.intent('Play Again', (conv) => { 208 | conv.ask(`Okay, here’s another game!`); 209 | conv.data.incorrectGuesses = 0; 210 | conv.data.correctWord = DICTIONARY.getWord().toLocaleUpperCase(); 211 | conv.data.wordToDisplay = '_'.repeat(conv.data.correctWord.length); 212 | conv.ask(new HtmlResponse({ 213 | data: { 214 | state: 'NEW_GAME', 215 | wordToDisplay: conv.data.wordToDisplay 216 | }, 217 | })); 218 | }); 219 | 220 | exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app); 221 | -------------------------------------------------------------------------------- /functions/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "snow-pal-canvas-sample-functions", 3 | "description": "Snow Pal Actions on Google Interactive Canvas Sample Cloud Function", 4 | "license": "Apache-2.0", 5 | "private": true, 6 | "engines": { 7 | "node": "8" 8 | }, 9 | "dependencies": { 10 | "actions-on-google": "^2.10.0", 11 | "firebase-admin": "~8.0.0", 12 | "firebase-functions": "^3.1.0" 13 | }, 14 | "scripts": { 15 | "lint": "./node_modules/.bin/eslint --fix \"**/*.js\"", 16 | "start": "firebase serve --only functions", 17 | "deploy": "firebase deploy --only functions", 18 | "test": "npm run lint" 19 | }, 20 | "devDependencies": { 21 | "eslint": "^6.0.1", 22 | "eslint-config-google": "^0.13.0" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /functions/util/dictionary.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2019 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 'use strict'; 18 | 19 | const words = ['Sombrero', 'Google', 'Mountain', 'California', 20 | 'Kubernetes', 'Tensorflow', 'Rhythmic', 'Numbskull', 'Haphazard', 21 | 'Memento', 'Ostracize', 'Mystify', 'Squawk', 'Wildebeest', 'Zombie', 22 | 'Zealous', 'Colombia']; 23 | 24 | const clone = (obj) => JSON.parse(JSON.stringify(obj)); 25 | /** 26 | * Dictionary of words to be used by Snow Pal. 27 | */ 28 | module.exports = class Dictionary { 29 | /** 30 | * Build all entries in the dictionary. 31 | * This dictionary can then be replaced by a payload from an API 32 | * on a less trivial implementation. 33 | */ 34 | constructor() { 35 | this.entries = this.shuffle(clone(words)); 36 | } 37 | 38 | /** 39 | * Retrieve words from the dictionary. 40 | * 41 | * @return {string} an unused word to be used by Snow Pal placeholder. 42 | */ 43 | getWord() { 44 | if (this.entries.length === 0) { 45 | this.entries = this.shuffle(clone(words)); 46 | } 47 | return this.entries.pop(); 48 | } 49 | 50 | /** 51 | * Shuffles array in place. 52 | * @param {Array} a items An array containing the items. 53 | * @return {Array} shuffled 54 | */ 55 | shuffle(a) { 56 | let j; let x; let i; 57 | for (i = a.length - 1; i > 0; i--) { 58 | j = Math.floor(Math.random() * (i + 1)); 59 | x = a[i]; 60 | a[i] = a[j]; 61 | a[j] = x; 62 | } 63 | return a; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /public/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Page Not Found 7 | 8 | 23 | 24 | 25 |
26 |

404

27 |

Page Not Found

28 |

The specified file was not found on this website. Please check the URL for mistakes and try again.

29 |

Why am I seeing this?

30 |

This page was generated by the Firebase Command-Line Interface. To modify it, edit the 404.html file in your project's configured public directory.

31 |
32 | 33 | 34 | -------------------------------------------------------------------------------- /public/assets/background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/actions-on-google/dialogflow-snowman-nodejs/a102fee2810b60745481e2b8d7da467cf525706e/public/assets/background.png -------------------------------------------------------------------------------- /public/assets/fonts/bitmap/google-sans-font.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/actions-on-google/dialogflow-snowman-nodejs/a102fee2810b60745481e2b8d7da467cf525706e/public/assets/fonts/bitmap/google-sans-font.png -------------------------------------------------------------------------------- /public/assets/fonts/bitmap/google-sans-font.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /public/assets/snow-pal.json: -------------------------------------------------------------------------------- 1 | { 2 | "textures": [ 3 | { 4 | "image": "snow-pal.png", 5 | "format": "RGBA8888", 6 | "size": { 7 | "w": 695, 8 | "h": 188 9 | }, 10 | "scale": 1, 11 | "frames": [ 12 | { 13 | "filename": "Bottom.png", 14 | "rotated": false, 15 | "trimmed": true, 16 | "sourceSize": { 17 | "w": 204, 18 | "h": 184 19 | }, 20 | "spriteSourceSize": { 21 | "x": 10, 22 | "y": 9, 23 | "w": 184, 24 | "h": 166 25 | }, 26 | "frame": { 27 | "x": 1, 28 | "y": 1, 29 | "w": 184, 30 | "h": 166 31 | } 32 | }, 33 | { 34 | "filename": "Left_Arm.png", 35 | "rotated": false, 36 | "trimmed": true, 37 | "sourceSize": { 38 | "w": 334, 39 | "h": 133 40 | }, 41 | "spriteSourceSize": { 42 | "x": 42, 43 | "y": 27, 44 | "w": 250, 45 | "h": 80 46 | }, 47 | "frame": { 48 | "x": 187, 49 | "y": 1, 50 | "w": 250, 51 | "h": 80 52 | } 53 | }, 54 | { 55 | "filename": "Right_Arm.png", 56 | "rotated": false, 57 | "trimmed": true, 58 | "sourceSize": { 59 | "w": 334, 60 | "h": 133 61 | }, 62 | "spriteSourceSize": { 63 | "x": 42, 64 | "y": 27, 65 | "w": 250, 66 | "h": 80 67 | }, 68 | "frame": { 69 | "x": 187, 70 | "y": 83, 71 | "w": 250, 72 | "h": 80 73 | } 74 | }, 75 | { 76 | "filename": "Torso.png", 77 | "rotated": false, 78 | "trimmed": true, 79 | "sourceSize": { 80 | "w": 184, 81 | "h": 133 82 | }, 83 | "spriteSourceSize": { 84 | "x": 18, 85 | "y": 6, 86 | "w": 148, 87 | "h": 122 88 | }, 89 | "frame": { 90 | "x": 439, 91 | "y": 1, 92 | "w": 148, 93 | "h": 122 94 | } 95 | }, 96 | { 97 | "filename": "Head.png", 98 | "rotated": false, 99 | "trimmed": true, 100 | "sourceSize": { 101 | "w": 139, 102 | "h": 206 103 | }, 104 | "spriteSourceSize": { 105 | "x": 17, 106 | "y": 16, 107 | "w": 105, 108 | "h": 186 109 | }, 110 | "frame": { 111 | "x": 589, 112 | "y": 1, 113 | "w": 105, 114 | "h": 186 115 | } 116 | } 117 | ] 118 | } 119 | ] 120 | } 121 | -------------------------------------------------------------------------------- /public/assets/snow-pal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/actions-on-google/dialogflow-snowman-nodejs/a102fee2810b60745481e2b8d7da467cf525706e/public/assets/snow-pal.png -------------------------------------------------------------------------------- /public/assets/snow-pal.sketch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/actions-on-google/dialogflow-snowman-nodejs/a102fee2810b60745481e2b8d7da467cf525706e/public/assets/snow-pal.sketch -------------------------------------------------------------------------------- /public/assets/win-lose.json: -------------------------------------------------------------------------------- 1 | { 2 | "textures": [ 3 | { 4 | "image": "win-lose.png", 5 | "format": "RGBA8888", 6 | "size": { 7 | "w": 1429, 8 | "h": 340 9 | }, 10 | "scale": 1, 11 | "frames": [ 12 | { 13 | "filename": "you-lose.png", 14 | "rotated": false, 15 | "trimmed": true, 16 | "sourceSize": { 17 | "w": 1024, 18 | "h": 600 19 | }, 20 | "spriteSourceSize": { 21 | "x": 99, 22 | "y": 91, 23 | "w": 823, 24 | "h": 338 25 | }, 26 | "frame": { 27 | "x": 1, 28 | "y": 1, 29 | "w": 823, 30 | "h": 338 31 | } 32 | }, 33 | { 34 | "filename": "you-win.png", 35 | "rotated": false, 36 | "trimmed": true, 37 | "sourceSize": { 38 | "w": 1024, 39 | "h": 600 40 | }, 41 | "spriteSourceSize": { 42 | "x": 207, 43 | "y": 90, 44 | "w": 602, 45 | "h": 329 46 | }, 47 | "frame": { 48 | "x": 826, 49 | "y": 1, 50 | "w": 602, 51 | "h": 329 52 | } 53 | } 54 | ] 55 | } 56 | ] 57 | } 58 | -------------------------------------------------------------------------------- /public/assets/win-lose.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/actions-on-google/dialogflow-snowman-nodejs/a102fee2810b60745481e2b8d7da467cf525706e/public/assets/win-lose.png -------------------------------------------------------------------------------- /public/assets/win-lose.sketch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/actions-on-google/dialogflow-snowman-nodejs/a102fee2810b60745481e2b8d7da467cf525706e/public/assets/win-lose.sketch -------------------------------------------------------------------------------- /public/assets/you-lose.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/actions-on-google/dialogflow-snowman-nodejs/a102fee2810b60745481e2b8d7da467cf525706e/public/assets/you-lose.png -------------------------------------------------------------------------------- /public/assets/you-win.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/actions-on-google/dialogflow-snowman-nodejs/a102fee2810b60745481e2b8d7da467cf525706e/public/assets/you-win.png -------------------------------------------------------------------------------- /public/css/main.css: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2020 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | html { 18 | height: 100%; 19 | } 20 | 21 | body { 22 | margin: 0; 23 | background-color: #F5F5F5; 24 | } 25 | 26 | ::-webkit-scrollbar { 27 | width: 0px; 28 | background: transparent; 29 | } 30 | 31 | ::-webkit-scrollbar-thumb { 32 | background: transparent; 33 | } 34 | 35 | .view { 36 | position: absolute; 37 | right: 0; 38 | bottom: 0; 39 | z-index: 2; 40 | } 41 | 42 | .debug { 43 | display: flex; 44 | flex-direction: column; 45 | 46 | /* Uncomment below to disable the debug overlay */ 47 | /* display: none !important; */ 48 | 49 | width: 500px; 50 | height: 150px; 51 | right: 0; 52 | bottom: 0; 53 | position: absolute; 54 | } 55 | 56 | .logs { 57 | display: block; 58 | opacity: 0.8; 59 | overflow-y: scroll; 60 | text-align: right; 61 | margin-top: auto; 62 | float: bottom; 63 | font-size: 12px; 64 | } 65 | 66 | .logs > p { 67 | display: inline-block; 68 | padding-top: 1px; 69 | padding-bottom: 1px; 70 | margin: 0px; 71 | margin-top: 1px; 72 | background: black; 73 | max-width: 100%; 74 | 75 | font-family: Arial, sans-serif; 76 | color: white; 77 | text-align: right; 78 | overflow-wrap: break-word; 79 | } 80 | 81 | .logs > p.error { 82 | color: red; 83 | } 84 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 21 | Snow Pal with Interactive Canvas 22 | 23 | 24 | 25 | 26 | 27 | 28 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 |
42 |
43 |
44 |
45 |
46 |
47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /public/js/assistant.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2020 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * This class is used as a wrapper for Interactive Canvas Assistant Action class along 19 | * with its callbacks. 20 | */ 21 | class Assistant { 22 | /** 23 | * @param {Phaser.Scene} scene which serves as a container of all visual 24 | * and audio elements. 25 | */ 26 | constructor(scene) { 27 | this.canvas = window.interactiveCanvas; 28 | this.gameScene = scene; 29 | const that = this; 30 | this.commands = { 31 | CORRECT: function(data) { 32 | that.gameScene.correctGuess(data.wordToDisplay); 33 | }, 34 | INCORRECT: function(data) { 35 | that.gameScene.incorrectGuess(); 36 | }, 37 | WIN: function(data) { 38 | that.gameScene.win(data.wordToDisplay); 39 | }, 40 | LOSE: function(data) { 41 | that.gameScene.lose(); 42 | }, 43 | NEW_GAME: function(data) { 44 | that.gameScene.start(data.wordToDisplay); 45 | }, 46 | DEFAULT: function() { 47 | // do nothing, when no command is found 48 | }, 49 | }; 50 | } 51 | 52 | /** 53 | * Register all callbacks used by the Interactive Canvas Action 54 | * executed during game creation time. 55 | */ 56 | setCallbacks() { 57 | const that = this; 58 | // Declare the Interactive Canvas action callbacks. 59 | const callbacks = { 60 | onUpdate(data) { 61 | that.commands[data.state ? data.state.toUpperCase() : 'DEFAULT'](data); 62 | }, 63 | }; 64 | // Called by the Interactive Canvas web app once web app has loaded to 65 | // register callbacks. 66 | this.canvas.ready(callbacks); 67 | } 68 | } -------------------------------------------------------------------------------- /public/js/game.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2020 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | window.onload = () => { 18 | const config = { 19 | type: Phaser.AUTO, 20 | scale: { 21 | mode: Phaser.Scale.FIT, 22 | autoCenter: Phaser.Scale.CENTER_HORIZONTALLY, 23 | width: window.innerWidth * window.devicePixelRatio, 24 | height: window.innerHeight * window.devicePixelRatio, 25 | }, 26 | parent: 'phaser-example', 27 | scene: [Main], 28 | backgroundColor: 0xffffff, 29 | audio: { 30 | disableWebAudio: false, 31 | }, 32 | physics: { 33 | default: 'arcade', 34 | arcade: { 35 | debug: false 36 | } 37 | }, 38 | }; 39 | const game = new Phaser.Game(config); 40 | window.focus(); 41 | }; 42 | 43 | /** 44 | * Represent Snow Pal game scene. 45 | */ 46 | export default class Main extends Phaser.Scene { 47 | /** 48 | * Constructor to initialize game including the Interactive Canvas object. 49 | * Assistant Canvas Action object registers all callbacks triggered by voice. 50 | * 51 | * "PlayGame" scene. 52 | */ 53 | constructor() { 54 | super('Main'); 55 | } 56 | /** 57 | * Preload assets (sprites, sounds, or text) to be used by the game. 58 | */ 59 | preload() { 60 | // Images 61 | this.load.bitmapFont('googleSans', 62 | '/assets/fonts/bitmap/google-sans-font.png', 63 | '/assets/fonts/bitmap/google-sans-font.xml'); 64 | this.load.image('background', '/assets/background.png'); 65 | this.load.path = '/assets/'; 66 | this.load.multiatlas('snow-pal', ['./snow-pal.json']); 67 | this.load.multiatlas('win_lose', ['./win-lose.json']); 68 | 69 | // Sounds 70 | this.wrongSound = new Howl({ 71 | src: ['https://actions.google.com/sounds/v1/cartoon/cartoon_boing.ogg'], 72 | autoplay: false, 73 | loop: false, 74 | volume: 1, 75 | }); 76 | this.correctSound = new Howl({ 77 | src: ['https://actions.google.com/sounds/v1/cartoon/siren_whistle.ogg'], 78 | autoplay: false, 79 | loop: false, 80 | volume: 1, 81 | sprite: { 82 | up: [0, 3300], 83 | } 84 | }); 85 | this.winSound = new Howl({ 86 | src: ['https://actions.google.com/sounds/v1/cartoon/crazy_dinner_bell.ogg'], 87 | autoplay: false, 88 | loop: false, 89 | volume: 1, 90 | }); 91 | this.loseSound = new Howl({ 92 | src: ['https://actions.google.com/sounds/v1/cartoon/concussive_hit_guitar_boing.ogg'], 93 | autoplay: false, 94 | loop: false, 95 | volume: 1, 96 | }); 97 | } 98 | 99 | /** 100 | * Called once to create the game along with other objects required 101 | * to produce the initial state of the game. 102 | */ 103 | create() { 104 | this.visibleObjects = []; 105 | this.youWinImage = this.add.image(this.scale.width / 2, 106 | (this.scale.height / 2) + 20, 'win_lose', 'you-win.png', this) 107 | .setVisible(false); 108 | this.youLoseImage = this.add.image(this.scale.width / 2, 109 | (this.scale.height / 2) + 20, 'win_lose', 'you-lose.png', this) 110 | .setVisible(false); 111 | 112 | // Background 113 | this.background = this.add.image(0, 0, 'background') 114 | .setOrigin(0, 0) 115 | .setDisplaySize(this.scale.width, this.scale.height); 116 | const that = this; 117 | window.interactiveCanvas.getHeaderHeightPx() 118 | .then((headerHeight) => { 119 | that.background 120 | .setY(headerHeight) 121 | .setDisplaySize(that.scale.width, that.scale.height - headerHeight); 122 | }); 123 | 124 | // Start Button 125 | this.startButton = new Phaser.GameObjects.Text(this, 0, this.scale.height / 2, 'Start Game', {fontSize: 75, fill: '#000'}); 126 | this.startButton.x = (this.scale.width / 2) - (this.startButton.width / 2); 127 | this.startButton 128 | .setInteractive({ useHandCursor: true }) 129 | .on('pointerover', () => this.startButton.setStyle({fill: '#ff0'})) 130 | .on('pointerout', () => this.startButton.setStyle({fill: '#000'})) 131 | .on('pointerdown', () => this.startButton.setStyle({fill: '#0ff'})) 132 | .on('pointerup', () => { 133 | this.startButton.setStyle({fill: '#ff0'}); 134 | window.interactiveCanvas.sendTextQuery('Yes'); 135 | }); 136 | 137 | // Play Again Button 138 | this.playAgainButton = new Phaser.GameObjects.Text(this, 0, this.scale.height / 1.5, 'Play Again', {fontSize: 75, fill: '#000'}); 139 | this.playAgainButton.x = (this.scale.width / 2) - (this.playAgainButton.width / 2); 140 | this.playAgainButton 141 | .setVisible(false) 142 | .setInteractive({ useHandCursor: true }) 143 | .on('pointerover', () => this.playAgainButton.setStyle({fill: '#ff0'})) 144 | .on('pointerout', () => this.playAgainButton.setStyle({fill: '#000'})) 145 | .on('pointerdown', () => this.playAgainButton.setStyle({fill: '#0ff'})) 146 | .on('pointerup', () => { 147 | this.playAgainButton.setStyle({fill: '#ff0'}); 148 | window.interactiveCanvas.sendTextQuery('Play again'); 149 | }); 150 | 151 | this.visibleObjects.push(this.background); 152 | this.add.existing(this.startButton); 153 | this.add.existing(this.playAgainButton); 154 | 155 | this.word = new Word(this); 156 | this.snowPal = new SnowPal(this); 157 | 158 | // Set assistant at game level. 159 | this.assistant = new Assistant(this); 160 | // Call setCallbacks to register assistant action callbacks. 161 | this.assistant.setCallbacks(); 162 | } 163 | 164 | /** 165 | * Call to start Snow Pal and reset images from initial state. 166 | */ 167 | start(wordToDisplay) { 168 | this.setVisible(true); 169 | this.youWinImage.setVisible(false); 170 | this.youLoseImage.setVisible(false); 171 | this.playAgainButton.setVisible(false); 172 | this.startButton.setVisible(false); 173 | this.snowPal.reset(); 174 | this.word.updateText(wordToDisplay); 175 | } 176 | 177 | correctGuess(wordToDisplay) { 178 | this.correctSound.play('up'); 179 | this.word.updateText(wordToDisplay); 180 | } 181 | 182 | incorrectGuess() { 183 | this.wrongSound.play(); 184 | this.snowPal.meltNextPart(); 185 | } 186 | 187 | win(wordToDisplay) { 188 | this.winSound.play(); 189 | this.word.updateText(wordToDisplay); 190 | this.displayGameOverScreen(true); 191 | } 192 | 193 | lose() { 194 | this.loseSound.play(); 195 | this.snowPal.meltNextPart(); 196 | this.displayGameOverScreen(false); 197 | } 198 | 199 | /** 200 | * Finish game by setting invisible main stage and displaying images of win or lose. 201 | * @param {boolean} userWins true to display win image or false to display 202 | * false one. 203 | */ 204 | displayGameOverScreen(userWins) { 205 | const that = this; 206 | // fade duration and RGB to fade out and fade in main camera 207 | that.cameras.main.fade(3000, 255, 255, 255, true, (cam, complete) => { 208 | if (complete === 1) { 209 | this.setVisible(0); 210 | if (userWins) { 211 | that.youWinImage.setVisible(true); 212 | } else { 213 | that.youLoseImage.setVisible(true); 214 | } 215 | that.playAgainButton.setVisible(true); 216 | that.cameras.main.fadeIn(3000, 255, 255, 255); 217 | } 218 | }); 219 | } 220 | 221 | /** 222 | * Set visibility to images. Since images use Alpha, then 0 is false and 223 | * 1 is true. 224 | * @param {boolean} visible to set visibility to true or false on a object. 225 | */ 226 | setVisible(visible) { 227 | this.visibleObjects.forEach((vo) => vo.setAlpha(visible ? 1 : 0)); 228 | } 229 | } 230 | 231 | /** 232 | * A Snow Pal class represented on the screen. 233 | */ 234 | class SnowPal { 235 | /** 236 | * SnowPal constructor. 237 | * @constructor 238 | * @param {Phaser.Scene} scene instance with access to all objects. 239 | */ 240 | constructor(scene) { 241 | const xPosition = scene.scale.width / 2; 242 | const yPosition = (scene.scale.height / 2) + 20; 243 | this.leftArm = scene.physics.add.sprite(-150, 100, 'snow-pal', 'Left_Arm.png').setVisible(false); 244 | this.rightArm = scene.physics.add.sprite(150, 100, 'snow-pal', 'Right_Arm.png').setVisible(false); 245 | this.bottom = scene.physics.add.sprite(0, 225, 'snow-pal', 'Bottom.png').setVisible(false); 246 | this.torso = scene.physics.add.sprite(0, 125, 'snow-pal', 'Torso.png').setVisible(false); 247 | this.head = scene.physics.add.sprite(0, 0, 'snow-pal', 'Head.png').setVisible(false); 248 | this.snowPalParts = [this.leftArm, this.rightArm, this.bottom, this.torso, this.head]; 249 | this.snowPal = scene.add.container(xPosition, yPosition, this.snowPalParts); 250 | this.snowPal.scale = .75; 251 | 252 | this.visibleIndex = 0; 253 | scene.visibleObjects = scene.visibleObjects.concat(this.snowPalParts); 254 | } 255 | 256 | /** 257 | * Melt the next part of Snow Pal. 258 | */ 259 | meltNextPart() { 260 | if (this.visibleIndex === (this.snowPalParts.length)) { 261 | return; 262 | } 263 | this.snowPalParts[this.visibleIndex++].setVisible(false); 264 | } 265 | 266 | reset() { 267 | this.visibleIndex = 0; 268 | this.snowPalParts.forEach((part) => part.setVisible(true)); 269 | } 270 | } 271 | 272 | /** 273 | * Represent the word to be guessed and helper functions to verify guesses. 274 | */ 275 | class Word { 276 | /** 277 | * Sets placeholders to be displayed. They must be equal to the word. 278 | * @param {Phaser.Scene} scene instance representing the game scene. 279 | */ 280 | constructor(scene) { 281 | this.scene = scene; 282 | this.word = scene.add.bitmapText(0, 0, 'googleSans', '', 100, 1); 283 | this.word.size = 12; 284 | this.word.setLetterSpacing(20); 285 | this.word.x = scene.scale.width / 2 - (this.word.width / 2); 286 | this.word.y = scene.scale.height - 150; 287 | this.scene.visibleObjects.push(this.word); 288 | } 289 | 290 | updateText(wordToDisplay) { 291 | this.word.text = wordToDisplay; 292 | this.word.size = (this.scene.scale.width - 80) / wordToDisplay.length; 293 | this.word.x = this.scene.scale.width / 2 - (this.word.width / 2); 294 | } 295 | } -------------------------------------------------------------------------------- /public/js/log.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2020 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // code to overlay console.log and console.error onto page 18 | 19 | const log = document.getElementsByClassName('logs')[0]; 20 | 21 | const prepend = (level, line) => { 22 | const p = document.createElement('p'); 23 | p.textContent = line; 24 | p.className = level; 25 | log.prepend(document.createElement('br')); 26 | log.prepend(p); 27 | }; 28 | 29 | const info = console.log.bind(console); 30 | console.log = (...messages) => { 31 | const logMessages = messages.join(' ') 32 | if (logMessages.indexOf('https://phaser.io') <= -1) { 33 | prepend('log', logMessages); 34 | info(...messages); 35 | } 36 | }; 37 | 38 | const error = console.error.bind(console); 39 | console.error = (...messages) => { 40 | prepend('error', messages.join(' ')); 41 | error(...messages); 42 | }; --------------------------------------------------------------------------------