mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-03-14 07:00:01 -04:00
Storing certain poll results in User object. Showing a poll once per day.
This commit is contained in:
parent
e21360127d
commit
495845a633
8 changed files with 24 additions and 10 deletions
|
@ -12,8 +12,9 @@ _.extend PollSchema.properties,
|
|||
i18n: {type: 'object', title: 'i18n', format: 'i18n', props: ['text']}
|
||||
votes: {title: 'Votes', type: 'integer', minimum: 0}
|
||||
i18n: {type: 'object', title: 'i18n', format: 'i18n', props: ['name', 'description']}
|
||||
created: c.isodate {title: 'Created', readOnly: true}
|
||||
created: c.date {title: 'Created', readOnly: true}
|
||||
priority: {title: 'Priority', description: 'Lower numbers will show earlier.', type: 'integer'}
|
||||
userProperty: c.shortString {pattern: c.identifierPattern, description: 'Optional: store the answer inside the User object itself, also, with this property name.'}
|
||||
|
||||
c.extendBasicProperties PollSchema, 'poll'
|
||||
c.extendSearchableProperties PollSchema
|
||||
|
|
|
@ -13,7 +13,7 @@ _.extend UserPollsRecordSchema.properties,
|
|||
random: {type: 'number', minimum: 0, maximum: 1}
|
||||
level: {type: 'integer', minimum: 1}
|
||||
level: {type: 'integer', minimum: 1, description: 'The player level when last saved.'}
|
||||
changed: c.isodate title: 'Changed', readOnly: true # Controls when next poll is available
|
||||
changed: c.date title: 'Changed', readOnly: true # Controls when next poll is available
|
||||
|
||||
c.extendBasicProperties UserPollsRecordSchema, 'user-polls-record'
|
||||
|
||||
|
|
|
@ -54,6 +54,7 @@ _.extend UserSchema.properties,
|
|||
firstName: c.shortString({title: 'First Name'})
|
||||
lastName: c.shortString({title: 'Last Name'})
|
||||
gender: {type: 'string', 'enum': ['male', 'female']}
|
||||
ageRange: {type: 'string'} # 'enum': ['0-13', '14-17', '18-24', '25-34', '35-44', '45-100']
|
||||
password: {type: 'string', maxLength: 256, minLength: 2, title: 'Password'}
|
||||
passwordReset: {type: 'string'}
|
||||
photoURL: {type: 'string', format: 'image-file', title: 'Profile Picture', description: 'Upload a 256x256px or larger image to serve as your profile picture.'}
|
||||
|
|
|
@ -17,8 +17,8 @@ me.shortString = (ext) -> combine({type: 'string', maxLength: 100}, ext)
|
|||
me.pct = (ext) -> combine({type: 'number', maximum: 1.0, minimum: 0.0}, ext)
|
||||
|
||||
# Dates should usually be strings, ObjectIds should be strings: https://github.com/codecombat/codecombat/issues/1384
|
||||
me.date = (ext) -> combine({type: ['object', 'string'], format: 'date-time'}, ext)
|
||||
me.isodate = (ext) -> combine({type: ['object'], format: 'date-time'}, ext) # use for server-side-only dates?
|
||||
me.date = (ext) -> combine({type: ['object', 'string'], format: 'date-time'}, ext) # old
|
||||
me.stringDate = (ext) -> combine({type: ['string'], format: 'date-time'}, ext) # new
|
||||
me.objectId = (ext) -> schema = combine({type: ['object', 'string']}, ext) # old
|
||||
me.stringID = (ext) -> schema = combine({type: 'string', minLength: 24, maxLength: 24}, ext) # use for anything new
|
||||
|
||||
|
|
|
@ -573,8 +573,12 @@ module.exports = class CampaignView extends RootView
|
|||
onRecordSync = ->
|
||||
return if @destroyed
|
||||
@userPollsRecord.url = -> '/db/user.polls.record/' + @id
|
||||
# TODO: only load poll if it's been a day
|
||||
@loadPoll()
|
||||
lastVoted = new Date @userPollsRecord.get('changed')
|
||||
interval = new Date() - lastVoted
|
||||
if interval > 22 * 60 * 60 * 1000 # Wait almost a day before showing the next poll
|
||||
@loadPoll()
|
||||
else
|
||||
console.log 'Poll will be ready in', (22 * 60 * 60 * 1000 - interval) / (60 * 60 * 1000), 'hours.'
|
||||
@listenToOnce @userPollsRecord, 'sync', onRecordSync
|
||||
@userPollsRecord = @supermodel.loadModel(@userPollsRecord, 'user_polls_record', null, 0).model
|
||||
onRecordSync.call @ if @userPollsRecord.loaded
|
||||
|
|
|
@ -28,6 +28,7 @@ PollSchema.statics.editableProperties = [
|
|||
'i18n'
|
||||
'i18nCoverage'
|
||||
'priority'
|
||||
'userProperty'
|
||||
]
|
||||
PollSchema.statics.jsonSchema = jsonSchema
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ UserPollsRecordSchema.pre 'save', (next) ->
|
|||
gemDelta = 0
|
||||
for pollID, answer of @get('polls') ? {}
|
||||
previousAnswer = @previousPolls[pollID]
|
||||
updatePollVotes pollID, answer, previousAnswer unless answer is previousAnswer
|
||||
updatePollVotes @get('user'), pollID, answer, previousAnswer unless answer is previousAnswer
|
||||
unless rewards[pollID]
|
||||
rewards[pollID] = reward = random: Math.random(), level: level
|
||||
gemDelta += Math.ceil 2 * reward.random * reward.level
|
||||
|
@ -29,7 +29,7 @@ UserPollsRecordSchema.pre 'save', (next) ->
|
|||
updateUserGems @get('user'), gemDelta if gemDelta
|
||||
next()
|
||||
|
||||
updatePollVotes = (pollID, answer, previousAnswer) ->
|
||||
updatePollVotes = (userID, pollID, answer, previousAnswer) ->
|
||||
Poll.findById mongoose.Types.ObjectId(pollID), {}, (err, poll) ->
|
||||
return log.error err if err
|
||||
answers = poll.get 'answers'
|
||||
|
@ -39,9 +39,16 @@ updatePollVotes = (pollID, answer, previousAnswer) ->
|
|||
poll.markModified 'answers'
|
||||
poll.save (err, newPoll, numberAffected) ->
|
||||
return log.error err if err
|
||||
updateUserProperty userID, userProperty, answer if userProperty = poll.get 'userProperty'
|
||||
|
||||
updateUserProperty = (userID, userProperty, answer) ->
|
||||
update = $set: {"#{userProperty}": answer}
|
||||
User.update {_id: mongoose.Types.ObjectId(userID)}, update, (err, numberAffected) ->
|
||||
return log.error err if err
|
||||
|
||||
updateUserGems = (userID, gemDelta) ->
|
||||
User.update {_id: mongoose.Types.ObjectId(userID)}, {$inc: {'earned.gems': gemDelta}}, (err, numberAffected) ->
|
||||
update = $inc: {'earned.gems': gemDelta}
|
||||
User.update {_id: mongoose.Types.ObjectId(userID)}, update, (err, numberAffected) ->
|
||||
return log.error err if err
|
||||
|
||||
UserPollsRecordSchema.statics.privateProperties = []
|
||||
|
|
|
@ -294,7 +294,7 @@ UserSchema.statics.privateProperties = [
|
|||
UserSchema.statics.jsonSchema = jsonschema
|
||||
UserSchema.statics.editableProperties = [
|
||||
'name', 'photoURL', 'password', 'anonymous', 'wizardColor1', 'volume',
|
||||
'firstName', 'lastName', 'gender', 'facebookID', 'gplusID', 'emails',
|
||||
'firstName', 'lastName', 'gender', 'ageRange', 'facebookID', 'gplusID', 'emails',
|
||||
'testGroupNumber', 'music', 'hourOfCode', 'hourOfCodeComplete', 'preferredLanguage',
|
||||
'wizard', 'aceConfig', 'autocastDelay', 'lastLevel', 'jobProfile', 'savedEmployerFilterAlerts',
|
||||
'heroConfig', 'iosIdentifierForVendor', 'siteref', 'referrer'
|
||||
|
|
Loading…
Reference in a new issue