Created
August 31, 2014 21:25
-
-
Save TrevorBasinger/c37382bb268256a2d814 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
#!/usr/bin/env node | |
require('pointfree-fantasy').expose(global); | |
var Future = require('data.future'); | |
var Either = require('data.either'); | |
var State = require('fantasy-states'); | |
var Tuple2 = require('fantasy-tuples').Tuple2; | |
var Tuple3 = require('fantasy-tuples').Tuple3; | |
var IO = require('fantasy-io'); | |
var Maybe = require('pointfree-fantasy/instances/maybe'); | |
var _ = require('ramda'); | |
var http = require('http'); | |
var rl = require('readline'); | |
var daggy = require('daggy'); | |
var OLens = require('fantasy-lenses').Lens.objectLens; | |
var io = require('socket.io-client'); | |
///////////////////////////////////////////////////////////////// | |
//// Application | |
//// This application runs in a loop. As notifications are posted | |
//// to the Autobeacon db, this bot will manage dispatching | |
//// notifications. | |
///////////////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////////////// | |
//// Pure Code | |
///////////////////////////////////////////////////////////////// | |
/* Lens Helpers */ | |
var get = function (lens) { | |
return lens.get(); | |
}; | |
var set = curry (function (obj, lens) { | |
return lens.set(obj); | |
}); | |
///////////////////////////////////////////////////////////////// | |
// Bot Pointed Functor | |
///////////////////////////////////////////////////////////////// | |
Bot = daggy.tagged('config'); | |
Bot.of = function (c) { | |
return Bot (c); | |
} | |
Bot.prototype.map = function (f) { | |
return f(this.config); | |
}; | |
///////////////////////////////////////////////////////////////// | |
// Bot Lenses | |
///////////////////////////////////////////////////////////////// | |
// idLens :: BotConfig -> Lens | |
var idLens = OLens('id').run; | |
var socketLens = OLens('socket').run; | |
var roleLens = OLens('role').run; | |
///////////////////////////////////////////////////////////////// | |
// Bot Lens Accessors | |
///////////////////////////////////////////////////////////////// | |
// setId :: id -> Bot -> Bot | |
var setId = curry (function (id) { | |
return compose (Bot.of, set (id), map (idLens)); | |
}); | |
// getId :: Bot -> Int | |
var getId = compose (get, map(idLens)); | |
// setRole :: role -> Bot -> Bot | |
var setRole = function (role) { | |
return compose (Bot.of, set(role), map (roleLens)); | |
}; | |
// getRole :: Bot -> Int | |
var getRole = compose (get, map(roleLens)); | |
// setSocket :: socket -> Bot -> Bot | |
var setSocket = function (sock) { | |
return compose( Bot.of, set (sock), map (socketLens)); | |
}; | |
// getSocket :: Bot -> Socket | |
var getSocket = compose (get, map(socketLens)); | |
///////////////////////////////////////////////////////////////// | |
// reassignRole = String -> String | |
var reassignRole = function ( conflict ) { | |
var role = 'client'; | |
if (conflict !== 'manager') { role = 'manager' } | |
else if (conflict !== 'queue') { role = 'queue' } | |
return role; | |
} | |
// resolveConflicts :: Bot -> Config -> () | |
var resolveConflicts = curry (function (bot, config) { | |
var id = getId(bot); | |
var role = getRole(bot); | |
if (role === undefined ) { bot = setRole (reassignRole (config.role))(bot); } | |
if (role === config.role) { bot = setRole (reassignRole (config.role))(bot); } | |
}); | |
// listen :: Bot -> IO Bot | |
var listen = function(bot) { | |
var socket = getSocket(bot); | |
socket.on('status', resolveConflicts(bot)); | |
return IO.of ( setSocket(socket)(bot) ); | |
} | |
// pollBots :: Bot -> IO Bot | |
var pollBots = function (bot) { | |
var socket = getSocket(bot); | |
socket.emit('nbot-all', { action: 'statusCheck' }); | |
return IO.of ( bot ); | |
} | |
var main = function(config) { | |
var bot = Bot.of(config); | |
return IO.of(bot) | |
.chain(listen) | |
.chain(pollBots) | |
} | |
///////////////////////////////////////////////////////////////// | |
//// Impure Code | |
///////////////////////////////////////////////////////////////// | |
function randomInt (min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
var config = { | |
id: randomInt(0, 999999), | |
socket: io.connect('http://localhost:1234'), // 1234 is test server | |
role: undefined, | |
status: undefined, | |
}; | |
main(config).unsafePerform(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment