Created
August 18, 2018 11:05
-
-
Save Bigomby/31f1efabf0772161d0b04dad69b01cb9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Bot = require('./lib/Bot'); | |
const SOFA = require('sofa-js'); | |
const Fiat = require('./lib/Fiat'); | |
// ----------------------------------------------------------------------------- | |
// Globals | |
// ----------------------------------------------------------------------------- | |
const bot = new Bot(); | |
// ----------------------------------------------------------------------------- | |
// Routing | |
// ----------------------------------------------------------------------------- | |
bot.onEvent = function(session, message) { | |
switch (message.type) { | |
case 'Init': | |
onInit(session); | |
break; | |
case 'Command': | |
onCommand(session, message); | |
break; | |
case 'Payment': | |
onPayment(session, message); | |
break; | |
} | |
}; | |
// ----------------------------------------------------------------------------- | |
// Listeners | |
// ----------------------------------------------------------------------------- | |
function onInit(session) { | |
sendMessage(session, 'Hello Token!'); | |
} | |
function onCommand(session, command) { | |
switch (command.content.value) { | |
case 'game': | |
handleGameStart(session); | |
break; | |
case 'game:0': | |
handleGameResponse(session, 0); | |
break; | |
case 'game:1': | |
handleGameResponse(session, 1); | |
break; | |
} | |
} | |
async function onPayment(session, message) { | |
// Ignore payments sent BY the bot | |
if (message.fromAddress === session.config.paymentAddress) { | |
return; | |
} | |
// We are only interested on "unconfirmed" messages | |
if (message.status !== 'unconfirmed') { | |
return; | |
} | |
handleGamePayment(session); | |
} | |
// ----------------------------------------------------------------------------- | |
// Handlers | |
// ----------------------------------------------------------------------------- | |
async function handleGameStart(session) { | |
const res = session.get('res'); | |
if (res) { | |
sendMessage(session, 'Game already in progress!'); | |
return; | |
} | |
const toEth = await Fiat.fetch(); | |
session.requestEth(toEth.EUR(0.1)); | |
session.set('res', Math.floor(Math.random() * 2)); | |
} | |
function handleGamePayment(session) { | |
const controls = [ | |
{ type: 'button', label: '0', value: '0' }, | |
{ type: 'button', label: '1', value: '1' }, | |
]; | |
const res = session.get('res'); | |
if (!res) { | |
sendMessage(session, 'No game in progress, but thanks for the money'); | |
return; | |
} | |
sendMessageKbd(session, 'Choose a binary number ;)', controls); | |
} | |
function handleGameResponse(session, response) { | |
const res = session.get('res'); | |
if (!res) { | |
sendMessage(session, 'There is no game in progress!'); | |
return; | |
} | |
if (response === res) { | |
sendMessage(session, 'Congrats! you win ;)'); | |
} else { | |
sendMessage(session, 'Oops, try again!'); | |
} | |
// Clear the result so the user can start again. Note there is no need for a | |
// separate variable like "newGame" since "res" can be used for that purpose. | |
// Maybe session.delete('res') could work too. | |
session.set('res', undefined); | |
} | |
// ----------------------------------------------------------------------------- | |
// Helpers | |
// ----------------------------------------------------------------------------- | |
function sendMessage(session, body) { | |
const controls = [{ type: 'button', label: 'Game', value: 'game' }]; | |
session.reply(SOFA.Message({ body, controls, showKeyboard: false })); | |
} | |
function sendMessageKbd(session, body, controls) { | |
session.reply(SOFA.Message({ body, controls, showKeyboard: false })); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment