Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Model = require('lazyboy');
- UUID = require('../../node_modules/node-uuid/uuid.js'),
- https = require('https'),
- url = require('url'),
- path = require('path'),
- fs = require('fs');
- //Model.logger.setLogLevel(7);
- var Account;
- var Game;
- var Metric;
- var app = {
- init : function() {
- Model.create_connection({db:'metr', port:8877, auth: { username: 'fuzzy', password: 'lolcakes' }});
- require('./models/Account');
- Model.load();
- Account = Model('Account');
- Game = Model('Game');
- Metric = Model('Metric');
- },
- accountGet : function( options, onComplete ) {
- var result = null;
- Account.find( options.id, function(err, _acc){
- if(err) {
- console.log('\t :: Account not found,creating account');
- result = app.accountCreate(options);
- app.accountSave(result);
- onComplete(result);
- } else {
- console.log('\t :: Account found, returning account');
- onComplete(_acc);
- }
- });
- },
- //Create an account, returning the instance.
- accountCreate : function( options ) {
- return Account.create( options );
- },
- accountSave : function( acc ) {
- acc.save(function(err, sa){
- console.log( "Account " + sa.id + ' created at ' + sa.dateUpdated);
- });
- },
- gameSave : function( _g ) {
- _g.save(function(err, sa){
- console.log( "Saving game ");
- });
- },
- gameAdd : function( options ) {
- console.log('\t_ add game. Trying with game name ' + options.name);
- //name, platform, version
- var newgame = Game.create(options);
- app.gameSave(newgame);
- },
- getGame : function( userid, name, oncomplete ) {
- var results = {};
- Game.where('uid', userid, function (err, games) {
- //loop over the games, collecting them by name, then platform, then version
- games.forEach(function(item){
- if(item.name == name) {
- if(results[item.name]) {
- if(results[item.name].platforms[item.platform]){
- results[item.name].platforms[item.platform].versions[item.version] = item.apikey;
- } else {
- results[item.name].platforms[item.platform] = {};
- results[item.name].platforms[item.platform].versions = {};
- results[item.name].platforms[item.platform].versions[item.version] = item.apikey;
- }
- } else {
- //first occurrence of this game.
- results[item.name] = {};
- results[item.name].platforms = {};
- results[item.name].platforms[item.platform] = {};
- results[item.name].platforms[item.platform].versions = {};
- results[item.name].platforms[item.platform].versions[item.version] = item.apikey;
- }
- } //if same name.
- });
- console.log(results);
- if(oncomplete) oncomplete(results);
- });
- },
- gameAddVersion : function( options, oncomplete ) {
- app.gameAdd({name:options.gameid, platform:options.platform,version:options.version, uid:options.userid});
- //return the game.
- app.getGame( options.userid, options.gameid, function(g){
- if(oncomplete) oncomplete(g);
- });
- },
- getGames : function (userid, oncomplete) {
- var results = [];
- Game.where('uid', userid, function (err, games) {
- //loop over the games, collecting them by name, then platform, then version
- games.forEach(function(item){
- var found = false;
- results.forEach(function(resitem) {
- if(resitem.name == item.name) {
- found = true;
- }
- });
- if(!found){
- results.push({name:item.name, id:item.id});
- }
- });
- console.log(results);
- if(oncomplete) oncomplete(results);
- });
- },
- metricAdd : function( options, oncomplete ) {
- },
- };
- app.init();
- var options = {
- key: fs.readFileSync('privatekey.pem'),
- cert: fs.readFileSync('certificate.pem')
- };
- var handleModel = function( data, res ) {
- var u = data[2];
- switch(data[0]) {
- case 'account':
- switch(data[1]) {
- case 'login':
- console.log(u + ' asked for key');
- app.accountGet({ id:u }, function(user){ res.end( UUID() ); });
- break;
- };
- break;
- case 'metrics':
- switch(data[1]) {
- case 'add':
- // /metrics/add/key/data
- //data will be coming in as base64 string
- var gamekey = data[2];
- var dd = new Buffer(data[3], 'base64').toString('ascii');
- console.log(gamekey + ' ' + dd);
- res.end('success');
- break;
- }
- break;
- case 'games':
- switch(data[1]){
- case 'add':
- var gameid = data[3].replace(/\u002B/gi,' ');
- var plats = data[4].replace(/\u005F/gi,' ').split(' ');
- console.log(u + ' is adding a game ' + gameid + ' for ' + plats);
- //Add the game, according to platform.
- for(var i = 0; i < plats.length; i++) {
- app.gameAdd({name:gameid, platform:plats[i],version:'default', uid:u});
- }
- res.end('success');
- break;
- case 'edit':
- console.log('changing a game');
- var gameid = data[3].replace(/\u005F/gi,' ');
- var changing = data[4]; // should be /p/ for platforms /v/ for version
- console.log(changing);
- var addorremove = data[5]; //should be + or -
- console.log(addorremove);
- if(changing == 'p') {
- //editing the platform list.
- if(addorremove == '+') {
- } else if(addorremove == '-') {
- }
- } else if(changing == 'v') {
- console.log(u + ' is editing a game ' + gameid + ' for a new version');
- //editing versions
- if(addorremove == '+') {
- var nvplatform = data[6].replace(/\u002B/gi,' ');
- var newversion = data[7].replace(/\u002B/gi,' ');
- console.log('adding a new version called ' + newversion + ' to ' + nvplatform );
- app.gameAddVersion( {userid:u, gameid:gameid, version:newversion, platform:nvplatform },
- function(gg){
- res.end( JSON.stringify(gg) );
- });
- } else if(addorremove == '-') {
- //todo, mark inactive but keep history.
- }
- }
- var plats = data[6].replace(/\u005F/gi,' ').split(' ');
- break;
- case 'get':
- //if not a specific game,
- if(!data[3]) {
- //call into get game with a callback
- app.getGames(u, function(results){
- res.end( JSON.stringify(results) );
- });
- } else {
- //sent in with _ as ' '
- data[3] = data[3].replace('_',' ');
- console.log(u + ' asked for ' + data[3]);
- app.getGame(u, data[3], function(results){
- res.end( JSON.stringify(results) );
- });
- }
- break;
- }
- break;
- case 'action':
- //Tracking doesn't use the account id, rather a game apikey.
- //The method is metr/action/timed/base64data
- /*
- {
- name : 'tracking name',
- timestamp : 'timestamp',
- apikey :'',
- data : {
- }
- */
- /*> console.log(new Buffer("Hello World").toString('base64'));
- SGVsbG8gV29ybGQ=
- > console.log(new Buffer("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'))
- Hello World*/
- break;
- };
- };
- var valid = {'account':['get', 'login'], 'games':['add'], 'metrics':['add']};
- https.createServer(options, function (req, res) {
- var uri = url.parse(req.url).pathname;
- var output = '';
- //remove ->/account
- uri = uri.substr(1,uri.length-1);
- //account/<- get
- var info = uri.split('/');
- console.log(info);
- //first check paths, if not valid, return false.
- if(!valid.hasOwnProperty(info[0])) {
- console.log('returned false');
- res.writeHead(404);
- res.end();
- } else {
- console.log('returned true');
- res.writeHead(200);
- handleModel( info, res );
- }
- }).listen(9999);
- console.log('starting server on 9999');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement