Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.33 KB | None | 0 0
  1. var Model = require('lazyboy');
  2. UUID = require('../../node_modules/node-uuid/uuid.js'),
  3. https = require('https'),
  4. url = require('url'),
  5. path = require('path'),
  6. fs = require('fs');
  7.  
  8. //Model.logger.setLogLevel(7);
  9.  
  10. var Account;
  11. var Game;
  12. var Metric;
  13.  
  14. var app = {
  15.  
  16. init : function() {
  17. Model.create_connection({db:'metr', port:8877, auth: { username: 'fuzzy', password: 'lolcakes' }});
  18. require('./models/Account');
  19. Model.load();
  20.  
  21. Account = Model('Account');
  22. Game = Model('Game');
  23. Metric = Model('Metric');
  24. },
  25.  
  26. accountGet : function( options, onComplete ) {
  27.  
  28. var result = null;
  29.  
  30. Account.find( options.id, function(err, _acc){
  31.  
  32. if(err) {
  33.  
  34. console.log('\t :: Account not found,creating account');
  35. result = app.accountCreate(options);
  36. app.accountSave(result);
  37. onComplete(result);
  38.  
  39. } else {
  40.  
  41. console.log('\t :: Account found, returning account');
  42. onComplete(_acc);
  43.  
  44. }
  45.  
  46. });
  47. },
  48.  
  49. //Create an account, returning the instance.
  50. accountCreate : function( options ) {
  51.  
  52. return Account.create( options );
  53.  
  54. },
  55.  
  56. accountSave : function( acc ) {
  57. acc.save(function(err, sa){
  58. console.log( "Account " + sa.id + ' created at ' + sa.dateUpdated);
  59. });
  60. },
  61.  
  62. gameSave : function( _g ) {
  63. _g.save(function(err, sa){
  64. console.log( "Saving game ");
  65. });
  66. },
  67.  
  68. gameAdd : function( options ) {
  69. console.log('\t_ add game. Trying with game name ' + options.name);
  70. //name, platform, version
  71. var newgame = Game.create(options);
  72. app.gameSave(newgame);
  73. },
  74.  
  75. getGame : function( userid, name, oncomplete ) {
  76. var results = {};
  77. Game.where('uid', userid, function (err, games) {
  78.  
  79. //loop over the games, collecting them by name, then platform, then version
  80. games.forEach(function(item){
  81. if(item.name == name) {
  82. if(results[item.name]) {
  83. if(results[item.name].platforms[item.platform]){
  84. results[item.name].platforms[item.platform].versions[item.version] = item.apikey;
  85. } else {
  86. results[item.name].platforms[item.platform] = {};
  87. results[item.name].platforms[item.platform].versions = {};
  88. results[item.name].platforms[item.platform].versions[item.version] = item.apikey;
  89. }
  90. } else {
  91. //first occurrence of this game.
  92. results[item.name] = {};
  93. results[item.name].platforms = {};
  94. results[item.name].platforms[item.platform] = {};
  95. results[item.name].platforms[item.platform].versions = {};
  96. results[item.name].platforms[item.platform].versions[item.version] = item.apikey;
  97. }
  98. } //if same name.
  99.  
  100. });
  101.  
  102. console.log(results);
  103. if(oncomplete) oncomplete(results);
  104. });
  105. },
  106.  
  107. gameAddVersion : function( options, oncomplete ) {
  108.  
  109. app.gameAdd({name:options.gameid, platform:options.platform,version:options.version, uid:options.userid});
  110. //return the game.
  111. app.getGame( options.userid, options.gameid, function(g){
  112. if(oncomplete) oncomplete(g);
  113. });
  114. },
  115.  
  116. getGames : function (userid, oncomplete) {
  117. var results = [];
  118. Game.where('uid', userid, function (err, games) {
  119.  
  120. //loop over the games, collecting them by name, then platform, then version
  121. games.forEach(function(item){
  122.  
  123. var found = false;
  124. results.forEach(function(resitem) {
  125. if(resitem.name == item.name) {
  126. found = true;
  127. }
  128. });
  129.  
  130. if(!found){
  131. results.push({name:item.name, id:item.id});
  132. }
  133.  
  134. });
  135.  
  136. console.log(results);
  137. if(oncomplete) oncomplete(results);
  138. });
  139. },
  140.  
  141. metricAdd : function( options, oncomplete ) {
  142.  
  143. },
  144. };
  145.  
  146.  
  147. app.init();
  148.  
  149. var options = {
  150. key: fs.readFileSync('privatekey.pem'),
  151. cert: fs.readFileSync('certificate.pem')
  152. };
  153.  
  154. var handleModel = function( data, res ) {
  155. var u = data[2];
  156. switch(data[0]) {
  157. case 'account':
  158. switch(data[1]) {
  159. case 'login':
  160. console.log(u + ' asked for key');
  161. app.accountGet({ id:u }, function(user){ res.end( UUID() ); });
  162. break;
  163. };
  164. break;
  165.  
  166. case 'metrics':
  167. switch(data[1]) {
  168. case 'add':
  169. // /metrics/add/key/data
  170. //data will be coming in as base64 string
  171. var gamekey = data[2];
  172. var dd = new Buffer(data[3], 'base64').toString('ascii');
  173. console.log(gamekey + ' ' + dd);
  174. res.end('success');
  175. break;
  176. }
  177. break;
  178.  
  179. case 'games':
  180.  
  181. switch(data[1]){
  182. case 'add':
  183. var gameid = data[3].replace(/\u002B/gi,' ');
  184. var plats = data[4].replace(/\u005F/gi,' ').split(' ');
  185. console.log(u + ' is adding a game ' + gameid + ' for ' + plats);
  186. //Add the game, according to platform.
  187. for(var i = 0; i < plats.length; i++) {
  188. app.gameAdd({name:gameid, platform:plats[i],version:'default', uid:u});
  189. }
  190. res.end('success');
  191. break;
  192.  
  193. case 'edit':
  194. console.log('changing a game');
  195. var gameid = data[3].replace(/\u005F/gi,' ');
  196.  
  197. var changing = data[4]; // should be /p/ for platforms /v/ for version
  198. console.log(changing);
  199. var addorremove = data[5]; //should be + or -
  200. console.log(addorremove);
  201. if(changing == 'p') {
  202. //editing the platform list.
  203. if(addorremove == '+') {
  204.  
  205. } else if(addorremove == '-') {
  206.  
  207. }
  208. } else if(changing == 'v') {
  209. console.log(u + ' is editing a game ' + gameid + ' for a new version');
  210. //editing versions
  211. if(addorremove == '+') {
  212. var nvplatform = data[6].replace(/\u002B/gi,' ');
  213. var newversion = data[7].replace(/\u002B/gi,' ');
  214. console.log('adding a new version called ' + newversion + ' to ' + nvplatform );
  215. app.gameAddVersion( {userid:u, gameid:gameid, version:newversion, platform:nvplatform },
  216. function(gg){
  217. res.end( JSON.stringify(gg) );
  218. });
  219. } else if(addorremove == '-') {
  220. //todo, mark inactive but keep history.
  221. }
  222. }
  223. var plats = data[6].replace(/\u005F/gi,' ').split(' ');
  224.  
  225.  
  226. break;
  227.  
  228. case 'get':
  229. //if not a specific game,
  230. if(!data[3]) {
  231. //call into get game with a callback
  232. app.getGames(u, function(results){
  233. res.end( JSON.stringify(results) );
  234. });
  235.  
  236. } else {
  237. //sent in with _ as ' '
  238. data[3] = data[3].replace('_',' ');
  239. console.log(u + ' asked for ' + data[3]);
  240. app.getGame(u, data[3], function(results){
  241. res.end( JSON.stringify(results) );
  242. });
  243. }
  244. break;
  245. }
  246. break;
  247.  
  248.  
  249. case 'action':
  250. //Tracking doesn't use the account id, rather a game apikey.
  251. //The method is metr/action/timed/base64data
  252. /*
  253. {
  254. name : 'tracking name',
  255. timestamp : 'timestamp',
  256. apikey :'',
  257. data : {
  258.  
  259. }
  260. */
  261.  
  262. /*> console.log(new Buffer("Hello World").toString('base64'));
  263. SGVsbG8gV29ybGQ=
  264. > console.log(new Buffer("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'))
  265. Hello World*/
  266.  
  267. break;
  268. };
  269. };
  270.  
  271. var valid = {'account':['get', 'login'], 'games':['add'], 'metrics':['add']};
  272.  
  273. https.createServer(options, function (req, res) {
  274. var uri = url.parse(req.url).pathname;
  275. var output = '';
  276.  
  277. //remove ->/account
  278. uri = uri.substr(1,uri.length-1);
  279. //account/<- get
  280. var info = uri.split('/');
  281. console.log(info);
  282. //first check paths, if not valid, return false.
  283. if(!valid.hasOwnProperty(info[0])) {
  284. console.log('returned false');
  285. res.writeHead(404);
  286. res.end();
  287. } else {
  288. console.log('returned true');
  289. res.writeHead(200);
  290. handleModel( info, res );
  291.  
  292. }
  293.  
  294. }).listen(9999);
  295.  
  296. console.log('starting server on 9999');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement