forked from nko2/website
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Visnu Pitiyanuvath
committed
Aug 31, 2012
1 parent
923d587
commit c7544ff
Showing
9 changed files
with
62 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
mongoose = require 'mongoose' | ||
|
||
TeamLimitSchema = module.exports = new mongoose.Schema | ||
limit: | ||
type: Number | ||
required: true | ||
effectiveAt: | ||
type: Date | ||
required: true | ||
TeamLimitSchema.plugin require('../lib/use-timestamps') | ||
TeamLimitSchema.index effectiveAt: -1 | ||
|
||
# class methods | ||
|
||
lastLimit = null | ||
TeamLimitSchema.static 'current', (next) -> | ||
now = new Date | ||
|
||
# only check every hour, unless we're near 0:00/12:00 UTC | ||
if lastLimit?.updatedAt > now - (1000*60*60) and not @aroundMeridian(now) | ||
next null, lastLimit.limit | ||
else | ||
TeamLimit.findOne { effectiveAt: { $lte: now } }, {}, { sort: [[ 'effectiveAt', -1 ]] }, (err, limit) -> | ||
return next err if err | ||
|
||
limit ||= new TeamLimit limit: 0, effectiveAt: now # default to 0 | ||
limit.updatedAt = now | ||
limit.save() | ||
|
||
lastLimit = limit | ||
|
||
next null, limit.limit | ||
|
||
TeamLimitSchema.static 'aroundMeridian', (now) -> | ||
[h, m] = [now.getUTCHours(), now.getUTCMinutes()] | ||
(h == 11 && m > 54) || (h == 12 && m < 5) || # 11:55 - 12:05 | ||
(h == 23 && m > 54) || (h == 0 && m < 5) # 23:55 - 00:05 | ||
|
||
TeamLimit = mongoose.model 'TeamLimit', TeamLimitSchema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters