mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-08-05 03:52:14 +00:00
encapsulate items.buyItem better in habitrpg-shared/items.coffee so we can call from API, derby, & mobile app
This commit is contained in:
parent
aeee52a0e7
commit
ca14c39c5f
4 changed files with 37 additions and 12 deletions
|
|
@ -1,6 +1,10 @@
|
|||
items = require 'habitrpg-shared/script/items'
|
||||
_ = require 'lodash'
|
||||
|
||||
updateStore = (model) ->
|
||||
nextItems = items.updateStore(model.get('_user'))
|
||||
_.each nextItems, (v,k) -> model.set("_items.next.#{k}",v); true
|
||||
|
||||
###
|
||||
server exports
|
||||
###
|
||||
|
|
@ -12,13 +16,13 @@ module.exports.server = (model) ->
|
|||
app exports
|
||||
###
|
||||
module.exports.app = (appExports, model) ->
|
||||
user = model.at '_user'
|
||||
misc = require './misc'
|
||||
|
||||
model.on "set", "_user.items.*", -> updateStore(model)
|
||||
|
||||
appExports.buyItem = (e, el) ->
|
||||
[type, value, index] = [ $(el).attr('data-type'), $(el).attr('data-value'), $(el).attr('data-index') ]
|
||||
if changes = items.buyItem(user.get(), type, value, index)
|
||||
_.each changes, (v,k) -> user.set k,v; true
|
||||
updateStore(model)
|
||||
misc.batchTxn model, (uObj, paths) ->
|
||||
items.buyItem uObj, $(el).attr('data-type'), {paths}
|
||||
|
||||
appExports.activateRewardsTab = ->
|
||||
model.set '_activeTabRewards', true
|
||||
|
|
@ -27,11 +31,6 @@ module.exports.app = (appExports, model) ->
|
|||
model.set '_activeTabPets', true
|
||||
model.set '_activeTabRewards', false
|
||||
|
||||
module.exports.updateStore = updateStore = (model) ->
|
||||
nextItems = items.updateStore(model.get('_user'))
|
||||
_.each nextItems, (v,k) -> model.set("_items.next.#{k}",v); true
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ module.exports.app = (appExports, model) ->
|
|||
[uObj, paths] = [user.get(), {}]
|
||||
algos.revive(uObj, {paths})
|
||||
_.each paths, ((v,k) -> user.set k, helpers.dotGet(k, uObj))
|
||||
items.updateStore(model)
|
||||
|
||||
appExports.reset = (e, el) ->
|
||||
misc.batchTxn model, (uObj, paths, batch) ->
|
||||
|
|
@ -20,7 +19,6 @@ module.exports.app = (appExports, model) ->
|
|||
['habit', 'daily', 'todo', 'reward'].forEach (type) -> batch.set("#{type}Ids", [])
|
||||
_.each {hp:50, lvl:1, gp:0, exp:0}, (v,k) -> batch.set("stats.#{k}",v)
|
||||
_.each {armor:0, weapon:0, head:0, shield:0}, (v,k) -> batch.set("items.#{k}",v)
|
||||
items.updateStore(model)
|
||||
browser.resetDom(model)
|
||||
|
||||
appExports.closeNewStuff = (e, el) ->
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ _ = require 'lodash'
|
|||
async = require 'async'
|
||||
algos = require 'habitrpg-shared/script/algos'
|
||||
helpers = require 'habitrpg-shared/script/helpers'
|
||||
items = require 'habitrpg-shared/script/items'
|
||||
validator = require 'derby-auth/node_modules/validator'
|
||||
check = validator.check
|
||||
sanitize = validator.sanitize
|
||||
|
|
@ -222,6 +223,24 @@ api.sortTask = (req, res, next) ->
|
|||
a.splice(to, 0, a.splice(from, 1)[0])
|
||||
user.set path, a, next
|
||||
|
||||
###
|
||||
------------------------------------------------------------------------
|
||||
Items
|
||||
------------------------------------------------------------------------
|
||||
###
|
||||
api.buy = (req, res, next) ->
|
||||
type = req.params.type
|
||||
unless type in ['weapon', 'armor', 'head', 'shield']
|
||||
return res.json 400, err: ":type must be in one of: 'weapon', 'armor', 'head', 'shield'"
|
||||
hasEnough = true
|
||||
done = ->
|
||||
return res.json 400, err: "Not enough GP" unless hasEnough
|
||||
req.habit.result = data: req.habit.user.get("items")
|
||||
next()
|
||||
misc.batchTxn req.getModel(), (uObj, paths) ->
|
||||
hasEnough = items.buyItem(uObj, type, {paths})
|
||||
,{user:req.habit.user, done}
|
||||
|
||||
###
|
||||
------------------------------------------------------------------------
|
||||
User
|
||||
|
|
@ -346,13 +365,19 @@ api.batchUpdate = (req, res, next) ->
|
|||
{user} = req.habit
|
||||
|
||||
performAction = (action, cb) ->
|
||||
# TODO come up with a more consistent approach here. like:
|
||||
# req.body=action.data; delete action.data; _.defaults(req.params, action)
|
||||
# Would require changing action.dir on mobile app
|
||||
req.params.id = action.data?.id
|
||||
req.params.direction = action.dir
|
||||
req.params.type = action.type
|
||||
req.body = action.data
|
||||
|
||||
switch action.op
|
||||
when "score"
|
||||
api.scoreTask(req, res, cb)
|
||||
when "buy"
|
||||
api.buy(req, res, cb)
|
||||
when "sortTask"
|
||||
api.sortTask(req, res, cb)
|
||||
when "addTask"
|
||||
|
|
|
|||
|
|
@ -41,6 +41,9 @@ router.delete '/user/task/:id', auth, cron, validateTask, api.deleteTa
|
|||
router.post '/user/task', auth, cron, validateTask, api.createTask, v1Send
|
||||
router.put '/user/task/:id/sort', auth, cron, validateTask, api.sortTask, v1Send
|
||||
|
||||
# Items
|
||||
router.post '/user/buy/:type', auth, cron, api.buy, v1Send
|
||||
|
||||
# User
|
||||
router.get '/user', auth, cron, api.getUser, v1Send
|
||||
router.post '/user/auth/local', api.loginLocal, v1Send
|
||||
|
|
|
|||
Loading…
Reference in a new issue