rewrite & api: add reroll api route POST /api/v1/user/reroll. pretty bad

interface on client for now since I can't get the UI to update, have to
refresh. will revisit. routes/api.coffee => routes/api.js
This commit is contained in:
Tyler Renelle 2013-08-29 00:20:38 -04:00
parent ad67cbcc1c
commit afb510fe84
8 changed files with 88 additions and 69 deletions

2
.gitignore vendored
View file

@ -9,9 +9,7 @@ lib
public/bower_components
builtAssets/
src/*/*.js
src/*/*.map
src/*/*/*.js
src/*/*/*.map
test/*.js
test/*.map

View file

@ -2,8 +2,8 @@
// Make user and settings available for everyone through root scope.
habitrpg.controller('SettingsCtrl',
['$scope', 'User', '$location',
function($scope, User, $location) {
['$scope', 'User', '$rootScope', '$http', 'API_URL',
function($scope, User, $rootScope, $http, API_URL) {
// FIXME we have this re-declared everywhere, figure which is the canonical version and delete the rest
// $scope.auth = function (id, token) {
@ -24,6 +24,15 @@ habitrpg.controller('SettingsCtrl',
}
$scope.reroll = function(){
$http.post(API_URL + '/api/v1/user/reroll')
.success(function(){
window.location.href = '/';
// FIXME, I can't get the tasks to update in the browser, even with _.extend(user,data). refreshing for now
})
.error(function(data){
alert(data.err)
})
}
}

View file

@ -21,8 +21,8 @@ api = module.exports
------------------------------------------------------------------------
####
NO_TOKEN_OR_UID = err: "You must include a token and uid (user id) in your request"
NO_USER_FOUND = err: "No user found."
NO_TOKEN_OR_UID = {err: "You must include a token and uid (user id) in your request"}
NO_USER_FOUND = {err: "No user found."}
###
beforeEach auth interceptor
@ -379,6 +379,19 @@ api.revive = (req, res, next) ->
return res.json(500,{err}) if err
res.json 200, saved
api.reroll = (req, res, next) ->
{user} = res.locals
if user.balance < 1
return res.json 401, {err: "Not enough tokens."}
user.balance -= 1
_.each user.tasks, (task) ->
user.tasks[task.id].value = 0 unless task.type is 'reward'
true
user.stats.hp = 50
user.save (err, saved) ->
return res.json(500, {err}) if err
res.json 200, saved
###
------------------------------------------------------------------------
Party
@ -460,6 +473,8 @@ api.batchUpdate = (req, res, next) ->
api.revive(req, res)
when "clear-completed"
api.clearCompleted(req, res)
when "reroll"
api.reroll(req, res)
else cb()
# Setup the array of functions we're going to call in parallel with async

View file

@ -30,17 +30,6 @@ module.exports.app = (appExports, model) ->
panelLabel: "Checkout"
token: token
###
Buy Reroll Button
###
appExports.buyReroll = ->
misc.batchTxn model, (uObj, paths, batch) ->
uObj.balance -= 1; paths['balance'] =1
_.each uObj.tasks, (task) ->
batch.set("tasks.#{task.id}.value", 0) unless task.type is 'reward'
true
$('#reroll-modal').modal('hide')
module.exports.routes = (expressApp) ->
###
Setup Stripe response when posting payment

View file

@ -178,6 +178,7 @@ UserSchema.methods.toJSON = () ->
doc["#{type}s"] = _.transform doc["#{type}Ids"], (result, tid) -> result.push(doc.tasks[tid])
#delete doc["#{type}Ids"]
#delete doc.tasks
doc.filters = {}
doc
# FIXME - since we're using special @post('init') above, we need to flag when the original path was modified.

View file

@ -1,50 +0,0 @@
express = require 'express'
router = new express.Router()
api = require '../controllers/api'
###
---------- /api/v1 API ------------
Every url added to router is prefaced by /api/v1
See ./routes/coffee for routes
v1 API. Requires x-api-user (user id) and x-api-key (api key) headers, Test with:
$ cd node_modules/racer && npm install && cd ../..
$ mocha test/api.mocha.coffee
###
{auth, verifyTaskExists, cron} = api
router.get '/status', (req, res) -> res.json status: 'up'
# Auth
router.post '/register', api.registerUser
# Scoring
router.post '/user/task/:id/:direction', auth, cron, api.scoreTask
router.post '/user/tasks/:id/:direction', auth, cron, api.scoreTask
# Tasks
router.get '/user/tasks', auth, cron, api.getTasks
router.get '/user/task/:id', auth, cron, api.getTask
router.put '/user/task/:id', auth, cron, verifyTaskExists, api.updateTask
router.post '/user/tasks', auth, cron, api.updateTasks
router.delete '/user/task/:id', auth, cron, verifyTaskExists, api.deleteTask
router.post '/user/task', auth, cron, api.createTask
router.put '/user/task/:id/sort', auth, cron, verifyTaskExists, api.sortTask
router.post '/user/clear-completed', auth, cron, api.clearCompleted
# Items
router.post '/user/buy/:type', auth, cron, api.buy
# User
router.get '/user', auth, cron, api.getUser
router.post '/user/auth/local', api.loginLocal
router.post '/user/auth/facebook', api.loginFacebook
router.put '/user', auth, cron, api.updateUser
router.post '/user/revive', auth, cron, api.revive
router.post '/user/batch-update', auth, cron, api.batchUpdate
# Groups
router.get '/groups', auth, api.getGroups
module.exports = router

57
src/routes/api.js Normal file
View file

@ -0,0 +1,57 @@
var express = require('express');
var router = new express.Router();
var api = require('../controllers/api');
/*
---------- /api/v1 API ------------
Every url added to router is prefaced by /api/v1
See ./routes/coffee for routes
v1 API. Requires x-api-user (user id) and x-api-key (api key) headers, Test with:
$ cd node_modules/racer && npm install && cd ../..
$ mocha test/api.mocha.coffee
*/
var auth, cron, verifyTaskExists;
auth = api.auth, verifyTaskExists = api.verifyTaskExists, cron = api.cron;
router.get('/status', function(req, res) {
return res.json({
status: 'up'
});
});
/* Auth*/
router.post('/register', api.registerUser);
/* Scoring*/
router.post('/user/task/:id/:direction', auth, cron, api.scoreTask);
router.post('/user/tasks/:id/:direction', auth, cron, api.scoreTask);
/* Tasks*/
router.get('/user/tasks', auth, cron, api.getTasks);
router.get('/user/task/:id', auth, cron, api.getTask);
router.put('/user/task/:id', auth, cron, verifyTaskExists, api.updateTask);
router.post('/user/tasks', auth, cron, api.updateTasks);
router["delete"]('/user/task/:id', auth, cron, verifyTaskExists, api.deleteTask);
router.post('/user/task', auth, cron, api.createTask);
router.put('/user/task/:id/sort', auth, cron, verifyTaskExists, api.sortTask);
router.post('/user/clear-completed', auth, cron, api.clearCompleted);
/* Items*/
router.post('/user/buy/:type', auth, cron, api.buy);
/* User*/
router.get('/user', auth, cron, api.getUser);
router.post('/user/auth/local', api.loginLocal);
router.post('/user/auth/facebook', api.loginFacebook);
router.put('/user', auth, cron, api.updateUser);
router.post('/user/revive', auth, cron, api.revive);
router.post('/user/batch-update', auth, cron, api.batchUpdate);
router.post('/user/reroll', auth, cron, api.reroll);
/* Groups*/
router.get('/groups', auth, api.getGroups);
module.exports = router;

View file

@ -13,7 +13,7 @@ div(modal='modals.reroll')
span(ng-if='user.balance < 1')
a.btn.btn-success.btn-large(data-dismiss="modal", x-bind="click:showStripe") Buy More Gems
span.gem-cost Not enough Gems
span(ng-if='user.balance >= 1')
a.btn.btn-danger.btn-large(data-dismiss="modal", x-bind='click:buyReroll') Re-Roll
span(ng-if='user.balance >= 1', ng-controller='SettingsCtrl')
a.btn.btn-danger.btn-large(data-dismiss="modal", ng-click='reroll()') Re-Roll
span.gem-cost 4 Gems