Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
New Twiddle
import Ember from 'ember';
import { task, timeout } from 'ember-concurrency';
const { Controller, computed, computed: { or } } = Ember;
export default Controller.extend({
jackpot: 1000,
jackpotText: computed('jackpot', function() {
return `$$${this.jackpot.toFixed()}`;
}),
odds: 0.001,
oddsText: computed('odds', function() {
return `1 in ${(1/this.odds).toFixed()}`;
}),
didWin: false,
buttonDisabled: or('didWin', 'draw.isRunning'),
buttonLabel: 'Try Your Luck',
draw: task(function *() {
let { jackpot, odds } = this;
let didWin = Math.random() < odds;
if (didWin) {
this.setProperties({ didWin });
return;
} else {
odds /= 2;
jackpot *= 2;
this.setProperties({ odds, jackpot });
let seconds = 30;
while (seconds > 0) {
this.set('buttonLabel', `Sorry, you didn't win :( Try again in ${seconds}s...`);
yield timeout(1000);
seconds--;
}
this.set('buttonLabel', 'Try Your Luck');
}
}).drop(),
});
{{#if this.didWin}}
<h1>Congratulations!</h1>
<h2>You may collect {{this.jackpotText}} from your wallet.</h2>
{{else}}
<h2>Jackpot: {{this.jackpotText}}</h2>
<h2>Odds of winning: {{this.oddsText}}</h2>
<button disabled={{this.buttonDisabled}} {{action (perform this.draw)}}>
{{this.buttonLabel}}
</button>
{{/if}}
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3"
},
"addons": {
"ember-concurrency": "0.8.19"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.