├── .gitignore ├── babel.config.js ├── src ├── utils │ ├── event.js │ └── api.js ├── assets │ ├── bg.png │ ├── refresh.png │ ├── bet-token.png │ ├── cocosbcx.png │ ├── waiting-male.jpg │ ├── waiting-female.jpg │ └── dice.svg ├── css │ └── style.css ├── App.vue ├── main.js ├── contracts │ └── dice.lua ├── store │ └── index.js └── components │ ├── header.vue │ ├── orders.vue │ ├── slider.vue │ └── game.vue ├── run_serve.png ├── public ├── favicon.ico └── index.html ├── vue.config.js ├── README_cn.md ├── package.json ├── README.md └── yarn-error.log /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/* 2 | dist/* 3 | package-lock.json 4 | yarn.lock 5 | 6 | -------------------------------------------------------------------------------- /babel.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | presets: [ 3 | '@vue/app' 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /src/utils/event.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue'; 2 | 3 | export default new Vue(); 4 | 5 | -------------------------------------------------------------------------------- /run_serve.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cocos-BCX/cocos-dice-sample/HEAD/run_serve.png -------------------------------------------------------------------------------- /src/assets/bg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cocos-BCX/cocos-dice-sample/HEAD/src/assets/bg.png -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cocos-BCX/cocos-dice-sample/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /src/assets/refresh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cocos-BCX/cocos-dice-sample/HEAD/src/assets/refresh.png -------------------------------------------------------------------------------- /vue.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | publicPath: process.env.NODE_ENV === 'production' ? '' : '/' 3 | }; -------------------------------------------------------------------------------- /src/assets/bet-token.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cocos-BCX/cocos-dice-sample/HEAD/src/assets/bet-token.png -------------------------------------------------------------------------------- /src/assets/cocosbcx.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cocos-BCX/cocos-dice-sample/HEAD/src/assets/cocosbcx.png -------------------------------------------------------------------------------- /src/assets/waiting-male.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cocos-BCX/cocos-dice-sample/HEAD/src/assets/waiting-male.jpg -------------------------------------------------------------------------------- /src/assets/waiting-female.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cocos-BCX/cocos-dice-sample/HEAD/src/assets/waiting-female.jpg -------------------------------------------------------------------------------- /src/utils/api.js: -------------------------------------------------------------------------------- 1 | export default (...args) => { 2 | return fetch(...args).then(res => { 3 | if (res.ok) return Promise.inject(res.json()); 4 | return Promise.reject(res); 5 | }); 6 | }; 7 | 8 | -------------------------------------------------------------------------------- /src/css/style.css: -------------------------------------------------------------------------------- 1 | body * { 2 | box-sizing: border-box; 3 | } 4 | 5 | p { 6 | margin: 0; 7 | } 8 | 9 | ul, 10 | ol { 11 | margin: 0; 12 | padding: 0; 13 | list-style: none; 14 | } 15 | 16 | .el-slider__runway { 17 | background-color: #de3162 18 | } 19 | 20 | .el-slider__bar { 21 | background-color: #6eed9b; 22 | -webkit-box-shadow: 0 0 20px #02f292; 23 | box-shadow: 0 0 20px #02f292; 24 | } 25 | 26 | .el-loading-mask { 27 | background: rgba(78, 71, 71, 0.9) 28 | } 29 | 30 | .el-slider__button { 31 | border: 1px solid #c5c5c5; 32 | background: #f6f6f6; 33 | cursor: -webkit-grabbing; 34 | cursor: grabbing; 35 | } 36 | 37 | 38 | 39 | @media screen (max-width: 1024px) { 40 | body { 41 | width: 2000px; 42 | } 43 | } -------------------------------------------------------------------------------- /src/App.vue: -------------------------------------------------------------------------------- 1 | 7 | 8 | 20 | 21 | 31 | -------------------------------------------------------------------------------- /README_cn.md: -------------------------------------------------------------------------------- 1 | ## cocos-dice: 2 | A Dice Game For Cocos-BCX 3 | 4 | ## 准备工作: 5 | ###### 1.下载钱包 6 | 7 | 下载安装Google插件钱包 [CocosPay](https://github.com/Cocos-BCX/CocosPay) 8 | 或 9 | 根据系统选择安装桌面版钱包 10 | [CocosDesktop for Mac](https://cocosbcx.oss-cn-beijing.aliyuncs.com/CocosDesktop.dmg) 11 | [CocosDesktop for Windows](https://cocosbcx.oss-cn-beijing.aliyuncs.com/CocosDesktop.exe) 12 | 13 | ###### 2.登录钱包 14 | 打开钱包创建/登录账户 15 | 16 | 17 | 18 | ## 编译运行: 19 | 20 | ###### 1.克隆项目代码到本地后,切换到项目的 front 目录; 21 | 22 | ``` 23 | cd front 24 | ``` 25 | 26 | ###### 2.在front目录下,按顺序执行以下命令(注意:运行以下命令前请确认您安装了8.9以上版本的node.js) 27 | 28 | 29 | 30 | ``` 31 | yarn install 32 | ``` 33 | 34 | 35 | ``` 36 | yarn run build 37 | ``` 38 | 39 | 40 | 41 | ``` 42 | yarn run serve 43 | ``` 44 | 45 | ###### 3.运行结果如图; 46 | ``` test 47 | 注意: 48 | 1.如果您安装的是Goole插件钱包,请使用Goole浏览器打开链接; 49 | 2.确保钱包正常安装,并有登录账号 50 | ``` 51 | 52 | ![avatar](https://github.com/Cocos-BCX/cocos-dice-sample/blob/master/run_serve.png) 53 | -------------------------------------------------------------------------------- /src/main.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import App from './App.vue' 3 | import ElementUI from 'element-ui'; 4 | import store from './store'; 5 | import { 6 | library 7 | } from '@fortawesome/fontawesome-svg-core' 8 | import { 9 | faSignOutAlt, 10 | } from '@fortawesome/free-solid-svg-icons' 11 | import { 12 | FontAwesomeIcon 13 | } from '@fortawesome/vue-fontawesome' 14 | import brands from '@fortawesome/fontawesome-free-brands'; 15 | 16 | import 'normalize.css'; 17 | import 'element-ui/lib/theme-chalk/base.css'; 18 | import 'element-ui/lib/theme-chalk/dialog.css'; 19 | import 'element-ui/lib/theme-chalk/alert.css'; 20 | import 'element-ui/lib/theme-chalk/message.css'; 21 | import 'element-ui/lib/theme-chalk/notification.css'; 22 | import './css/style.css'; 23 | 24 | library.add(faSignOutAlt, brands); 25 | 26 | Vue.use(ElementUI); 27 | Vue.config.productionTip = false 28 | 29 | Vue.component('font-awesome-icon', FontAwesomeIcon) 30 | 31 | new Vue({ 32 | store, 33 | render: h => h(App) 34 | }).$mount('#app') -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dice", 3 | "version": "0.1.0", 4 | "private": true, 5 | "scripts": { 6 | "serve": "vue-cli-service serve", 7 | "build": "vue-cli-service build" 8 | }, 9 | "dependencies": { 10 | "@fortawesome/fontawesome-free-brands": "^5.0.13", 11 | "@fortawesome/fontawesome-svg-core": "^1.2.4", 12 | "@fortawesome/free-solid-svg-icons": "^5.3.1", 13 | "@fortawesome/vue-fontawesome": "^0.1.1", 14 | "bcxjs-api": "^1.4.33", 15 | "element-ui": "^2.4.6", 16 | "get-random-values": "^1.2.0", 17 | "isomorphic-ws": "^4.0.1", 18 | "node-gyp": "^4.0.0", 19 | "normalize.css": "^8.0.0", 20 | "vue": "^2.5.17", 21 | "vuex": "^3.0.1" 22 | }, 23 | "devDependencies": { 24 | "@vue/cli-plugin-babel": "^3.0.1", 25 | "@vue/cli-service": "^3.0.1", 26 | "vue-template-compiler": "^2.5.17" 27 | }, 28 | "postcss": { 29 | "plugins": { 30 | "autoprefixer": {} 31 | } 32 | }, 33 | "browserslist": [ 34 | "> 1%", 35 | "last 2 versions", 36 | "not ie <= 8" 37 | ] 38 | } 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [中文](https://github.com/Cocos-BCX/cocos-dice-sample/blob/master/README_cn.md) 2 | 3 | ## cocos-dice 4 | A Fully-Functional Dice DApp Game for the Cocos-BCX Platform 5 | 6 | ## Prerequisites 7 | 8 | * Node.js 8.9+ 9 | * yarn 10 | 11 | ### Wallet 12 | 13 | * [CocosPay Chrome Extension](https://chrome.google.com/webstore/detail/cocospay/ffbhaeoepdfapfjhcihbbhlaigejfack) 14 | * Alternatively [CocosPay from Source](https://github.com/Cocos-BCX/CocosPay) 15 | 16 | Or, alternatively (for Chrome & non-Chrome), install CocosDesktop Wallet 17 | 18 | [CocosDesktop for Mac](https://cocosbcx.oss-cn-beijing.aliyuncs.com/CocosDesktop.dmg) 19 | [CocosDesktop for Windows](https://cocosbcx.oss-cn-beijing.aliyuncs.com/CocosDesktop.exe) 20 | 21 | ### Login 22 | Open wallet to sign in or sign up 23 | 24 | 25 | ## Build and Run 26 | 27 | ```sh 28 | yarn install 29 | yarn build 30 | yarn serve 31 | ``` 32 | 33 | ## Open App in Browser 34 | 35 | App should be available at: http://localhost:8080/ 36 | 37 | ``` test 38 | Notice: 39 | 1. If you are using Google Chrome extension wallet, do open the link with Chrome; 40 | 2. Make sure you've installed wallet correctly and logged in 41 | ``` 42 | 43 | Output should be something like: 44 | 45 | ![avatar](https://github.com/Cocos-BCX/cocos-dice-sample/blob/master/run_serve.png) 46 | -------------------------------------------------------------------------------- /src/contracts/dice.lua: -------------------------------------------------------------------------------- 1 | -- very import 2 | COCOS_ACCURACY = 100000 3 | 4 | function init() 5 | assert(chainhelper:is_owner(),'no auth') 6 | read_list = {public_data={rate=true,max_bet=true}} 7 | chainhelper:read_chain() 8 | public_data.rate = 98 9 | public_data.max_bet = 1000000 10 | write_list = {public_data={rate=true,max_bet=true}} 11 | chainhelper:write_chain() 12 | end 13 | 14 | 15 | function bet(num, amount) 16 | read_list = {public_data={rate=true,max_bet=true}} 17 | chainhelper:read_chain() 18 | 19 | num = tonumber(num) 20 | amount = tonumber(amount) * COCOS_ACCURACY 21 | 22 | assert( 1 < num and num < 97, "num must in [2,96] " ) 23 | assert( 0 < amount and amount < public_data.max_bet * COCOS_ACCURACY, "amount must in [1, max]") 24 | 25 | chainhelper:transfer_from_caller(contract_base_info.owner, amount, 'COCOS', true) 26 | 27 | result_num = chainhelper:random() % 100 28 | 29 | if result_num < num then 30 | win_chance = num - 1 31 | reward_ratio = public_data.rate*1.0/win_chance 32 | reward = amount*reward_ratio 33 | chainhelper:transfer_from_owner(contract_base_info.caller, reward, 'COCOS', true) 34 | end 35 | end 36 | 37 | function hello_world() 38 | chainhelper:log('hello world') 39 | chainhelper:log(date('%Y-%m-%dT%H:%M:%S', chainhelper:time())) 40 | end -------------------------------------------------------------------------------- /src/store/index.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import Vuex from 'vuex' 3 | import eventHub from "@/utils/event"; 4 | import { 5 | Message 6 | } from 'element-ui' 7 | Vue.use(Vuex) 8 | 9 | 10 | export default new Vuex.Store({ 11 | state: { 12 | account: {}, 13 | cocos: 0, 14 | loading: false, 15 | bcx: {} 16 | }, 17 | mutations: { 18 | UPDATE_ACCOUNT(state, account) { 19 | state.account = account 20 | }, 21 | SET_BCX(state, bcx) { 22 | state.bcx = bcx 23 | }, 24 | SET_COCOS(state, cocos) { 25 | state.cocos = cocos 26 | }, 27 | LOADING(state, loading) { 28 | state.loading = loading 29 | } 30 | }, 31 | actions: { 32 | async CONNECT_COCOS({ 33 | state, 34 | commit 35 | }) { 36 | try { 37 | commit('LOADING', true) 38 | let timer = null 39 | clearInterval(timer) 40 | timer = setInterval(() => { 41 | if (window.BcxWeb) { 42 | commit('SET_BCX', window.BcxWeb) 43 | window.BcxWeb.getAccountInfo().then(res => { 44 | if (res.locked) { 45 | Message({ 46 | duration: 1200, 47 | message: 'Account Locked', 48 | type: 'error', 49 | }) 50 | return 51 | } 52 | commit('UPDATE_ACCOUNT', { 53 | name: res.account_name, 54 | }) 55 | commit('LOADING', false) 56 | }) 57 | clearInterval(timer) 58 | } 59 | }, 1000) 60 | } catch (error) { 61 | commit('LOADING', false) 62 | clearInterval(timer) 63 | Message({ 64 | duration: 1200, 65 | message: 'connect failed', 66 | type: 'error', 67 | }) 68 | } 69 | }, 70 | }, 71 | }) -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Fair Dice 9 | 10 | 11 | 12 | 13 | 43 | 52 | 53 | 54 | 55 |
56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /yarn-error.log: -------------------------------------------------------------------------------- 1 | Arguments: 2 | D:\node\node.exe D:\Program Files (x86)\bin\yarn.js install 3 | 4 | PATH: 5 | C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;D:\Java\git\Git\cmd;D:\GoRoot\bin;D:\gcc\bin;D:\node\;D:\Program Files (x86)\bin\;C:\Users\admin\AppData\Local\Microsoft\WindowsApps;D:\vscode\Microsoft VS Code\bin;D:\Program Files (x86)\Bandizip\;D:\Docker Toolbox;C:\Users\admin\AppData\Roaming\npm;C:\Users\admin\AppData\Local\Yarn\bin 6 | 7 | Yarn version: 8 | 1.15.2 9 | 10 | Node version: 11 | 10.15.3 12 | 13 | Platform: 14 | win32 x64 15 | 16 | Trace: 17 | Error: connect ETIMEDOUT 104.16.24.35:443 18 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14) 19 | 20 | npm manifest: 21 | { 22 | "name": "dice", 23 | "version": "0.1.0", 24 | "private": true, 25 | "scripts": { 26 | "serve": "vue-cli-service serve", 27 | "build": "vue-cli-service build", 28 | "lint": "vue-cli-service lint" 29 | }, 30 | "dependencies": { 31 | "@fortawesome/fontawesome-free-brands": "^5.0.13", 32 | "@fortawesome/fontawesome-svg-core": "^1.2.4", 33 | "@fortawesome/free-solid-svg-icons": "^5.3.1", 34 | "@fortawesome/vue-fontawesome": "^0.1.1", 35 | "element-ui": "^2.4.6", 36 | "normalize.css": "^8.0.0", 37 | "vue": "^2.5.17", 38 | "vuex": "^3.0.1" 39 | }, 40 | "devDependencies": { 41 | "@vue/cli-plugin-babel": "^3.0.1", 42 | "@vue/cli-plugin-eslint": "^3.0.1", 43 | "@vue/cli-service": "^3.0.1", 44 | "vue-template-compiler": "^2.5.17" 45 | }, 46 | "eslintConfig": { 47 | "root": true, 48 | "env": { 49 | "node": true 50 | }, 51 | "extends": [ 52 | "plugin:vue/essential", 53 | "eslint:recommended" 54 | ], 55 | "rules": {}, 56 | "parserOptions": { 57 | "parser": "babel-eslint" 58 | } 59 | }, 60 | "postcss": { 61 | "plugins": { 62 | "autoprefixer": {} 63 | } 64 | }, 65 | "browserslist": [ 66 | "> 1%", 67 | "last 2 versions", 68 | "not ie <= 8" 69 | ] 70 | } 71 | 72 | yarn manifest: 73 | No manifest 74 | 75 | Lockfile: 76 | No lockfile 77 | -------------------------------------------------------------------------------- /src/components/header.vue: -------------------------------------------------------------------------------- 1 | 18 | 19 | 20 | 46 | 47 | 159 | 160 | -------------------------------------------------------------------------------- /src/components/orders.vue: -------------------------------------------------------------------------------- 1 | 34 | 35 | 94 | 95 | 155 | 156 | -------------------------------------------------------------------------------- /src/components/slider.vue: -------------------------------------------------------------------------------- 1 | 23 | 24 | 72 | 73 | 245 | 246 | -------------------------------------------------------------------------------- /src/assets/dice.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/components/game.vue: -------------------------------------------------------------------------------- 1 | 118 | 355 | 356 | 965 | 966 | --------------------------------------------------------------------------------