From c01d5d79d6edc113a9c354c8dca0aefd91a3d437 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Mon, 6 Jan 2014 14:43:01 -0700 Subject: [PATCH] event-items: try a different approach than "unbreakable" (1335750). If they've owned it before (item==false), then they canOwn() again. In those cases, set item to 'event' --- dist/habitrpg-shared.js | 24 ++++++++++++++++-------- script/content.coffee | 10 +++++++++- script/index.coffee | 6 ++---- tests/algos.mocha.coffee | 17 +++++++++++++---- 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/dist/habitrpg-shared.js b/dist/habitrpg-shared.js index 454c85758b..7918648a80 100644 --- a/dist/habitrpg-shared.js +++ b/dist/habitrpg-shared.js @@ -9787,10 +9787,10 @@ var global=self;/** text: "Absurd Party Hat", notes: "You've received an Absurd Party Hat! Wear it with pride while ringing in the New Year!", value: 0, - canOwn: (function() { - return false; - }), - unbreakable: true + event: { + start: '2013-12-31', + end: '2014-02-01' + } } } }, @@ -9956,7 +9956,7 @@ var global=self;/** _.each(['weapon', 'armor', 'head', 'shield'], function(type) { return _.each(['base', 'warrior', 'rogue', 'healer', 'wizard', 'special'], function(klass) { return _.each(gear[type][klass], function(item, i) { - var key; + var key, _canOwn; key = "" + type + "_" + klass + "_" + i; _.defaults(item, { type: type, @@ -9968,6 +9968,14 @@ var global=self;/** per: 0, con: 0 }); + if (item.event) { + _canOwn = item.canOwn || (function() { + return true; + }); + item.canOwn = function(u) { + return _canOwn(u) && ((u.items.gear.owned[key] != null) || (moment().isAfter(item.event.start) && moment().isBefore(item.event.end))); + }; + } return api.gear.flat[key] = item; }); }); @@ -11229,12 +11237,12 @@ var process=require("__browserify_process");(function() { if (lostStat) { user.stats[lostStat]--; } - lostItem = user.fns.randomVal(_.reduce(user.items.gear.owned, function(m, v, k) { - if (v && !content.gear.flat['' + k].unbreakable) { + lostItem = user.fns.randomVal(_.reduce(user.items.gear.owned, (function(m, v, k) { + if (v) { m['' + k] = '' + k; } return m; - }, {})); + }), {})); if (item = content.gear.flat[lostItem]) { user.items.gear.owned[lostItem] = false; if (user.items.gear.equipped[item.type] === lostItem) { diff --git a/script/content.coffee b/script/content.coffee index 1afd55d3f9..11feb8b588 100644 --- a/script/content.coffee +++ b/script/content.coffee @@ -131,7 +131,7 @@ gear = 0: text: "Shade Helm", notes:'Blood and ash, lava and obsidian give this helm its imagery and power. Increases INT by 20.', int: 20, value:150, canOwn: ((u)-> +u.backer?.tier >= 45) 1: text: "Crystal Helm", notes:'The favored crown of those who lead by example. Increases all attributes by 6.', con: 6, str: 6, per: 6, int: 6, value:170, canOwn: ((u)-> +u.contributor?.level >= 3) 2: text: "Nameless Helm", notes:'A testament to those who gave of themselves while asking nothing in return. Increases INT and STR by 25 each.', int: 25, str: 25, value:200, canOwn: ((u)-> +u.backer?.tier >= 300) - nye: text: "Absurd Party Hat", notes:"You've received an Absurd Party Hat! Wear it with pride while ringing in the New Year!", value: 0, canOwn: (->false), unbreakable: true + nye: text: "Absurd Party Hat", notes:"You've received an Absurd Party Hat! Wear it with pride while ringing in the New Year!", value: 0, event: {start:'2013-12-31',end:'2014-02-01'} #candycane: text: "Candy Cane Hat", notes: 'A hat adorned in candy, a wintery treat!', value:10, canOwn: ((u)-> moment(u.auth.timestamps?.created).isBefore(new Date '01/10/2014')) shield: @@ -179,6 +179,14 @@ _.each ['weapon', 'armor', 'head', 'shield'], (type) -> _.each gear[type][klass], (item, i) -> key = "#{type}_#{klass}_#{i}" _.defaults item, {type, key, klass, index: i, str:0, int:0, per:0, con:0} + + if item.event + #? indicates null/undefined. true means they own currently, false means they once owned - and false is what we're + # after (they can buy back if they bought it during the event's timeframe) + _canOwn = item.canOwn or (->true) + item.canOwn = (u)-> + _canOwn(u) and (u.items.gear.owned[key]? or (moment().isAfter(item.event.start) and moment().isBefore(item.event.end))) + api.gear.flat[key] = item ### diff --git a/script/index.coffee b/script/index.coffee index 70de6f58aa..b3006308ec 100644 --- a/script/index.coffee +++ b/script/index.coffee @@ -369,10 +369,8 @@ api.wrap = (user) -> # Lose a gear piece # Note, they can actually lose item weapon_*_0 - it's 0 to buy back, no big deal - lostItem = user.fns.randomVal _.reduce user.items.gear.owned, (m,v,k)-> - # Note ""+k string-casting. Without this, when run on the server Mongoose returns funny objects - (m[''+k]=''+k if v and !content.gear.flat[''+k].unbreakable);m - , {} + # Note ""+k string-casting. Without this, when run on the server Mongoose returns funny objects + lostItem = user.fns.randomVal _.reduce(user.items.gear.owned, ((m,v,k)->m[''+k]=''+k if v;m), {}) if item = content.gear.flat[lostItem] user.items.gear.owned[lostItem] = false diff --git a/tests/algos.mocha.coffee b/tests/algos.mocha.coffee index 603486b8f0..a86a5f6301 100644 --- a/tests/algos.mocha.coffee +++ b/tests/algos.mocha.coffee @@ -138,6 +138,7 @@ describe 'User', -> expect(user._statsComputed.maxMP).to.eql 63 describe 'Death', -> + user = undefined it 'revives correctly', -> user = newUser() user.stats = { gp: 10, exp: 100, lvl: 2, hp: 1 } @@ -155,14 +156,22 @@ describe 'User', -> user.items.gear.owned['head_special_nye'] = true expect(ce user.items.gear.owned).to.be 3 user.ops.revive() - console.log(user.items.gear.owned) expect(ce(user.items.gear.owned)).to.be 2 user.ops.revive() - console.log(user.items.gear.owned) expect(ce(user.items.gear.owned)).to.be 1 user.ops.revive() - console.log(user.items.gear.owned) - expect(ce(user.items.gear.owned)).to.be 1 + expect(ce(user.items.gear.owned)).to.be 0 + + it "handles event items", -> + shared.content.gear.flat.head_special_nye.event.start = '2012-01-01' + shared.content.gear.flat.head_special_nye.event.end = '2012-02-01' + expect(shared.content.gear.flat.head_special_nye.canOwn(user)).to.be true + delete user.items.gear.owned['head_special_nye'] + expect(shared.content.gear.flat.head_special_nye.canOwn(user)).to.be false + + shared.content.gear.flat.head_special_nye.event.start = moment().subtract('days',5) + shared.content.gear.flat.head_special_nye.event.end = moment().add('days',5) + expect(shared.content.gear.flat.head_special_nye.canOwn(user)).to.be true describe 'store', ->