mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-23 23:58:02 -05:00
A bit of error handling.
This commit is contained in:
parent
0ce04b147d
commit
a9725f2dc6
6 changed files with 52 additions and 45 deletions
|
@ -47,14 +47,14 @@ module.exports = GPlusHandler = class GPlusHandler extends CocoClass
|
|||
'client_id' : clientID
|
||||
'scope' : scope
|
||||
gapi.auth.authorize params, @onGPlusLogin
|
||||
|
||||
|
||||
onGPlusLogin: (e) =>
|
||||
@loggedIn = true
|
||||
storage.save(GPLUS_TOKEN_KEY, e)
|
||||
@accessToken = e
|
||||
@trigger 'logged-in'
|
||||
return if (not me) or me.get 'gplusID' # so only get more data
|
||||
|
||||
return if (not me) or me.get 'gplusID' # so only get more data
|
||||
|
||||
# email and profile data loaded separately
|
||||
@responsesComplete = 0
|
||||
gapi.client.request(path:plusURL, callback:@onPersonEntityReceived)
|
||||
|
@ -104,12 +104,13 @@ module.exports = GPlusHandler = class GPlusHandler extends CocoClass
|
|||
success: (model) ->
|
||||
window.location.reload() if wasAnonymous and not model.get('anonymous')
|
||||
})
|
||||
|
||||
|
||||
loadFriends: (friendsCallback) ->
|
||||
return friendsCallback() unless @loggedIn
|
||||
expires_in = if @accessToken then parseInt(@accessToken.expires_at) - new Date().getTime()/1000 else -1
|
||||
expiresIn = if @accessToken then parseInt(@accessToken.expires_at) - new Date().getTime()/1000 else -1
|
||||
onReauthorized = => gapi.client.request({path:'/plus/v1/people/me/people/visible', callback: friendsCallback})
|
||||
if expires_in < 0
|
||||
if expiresIn < 0
|
||||
# TODO: this tries to open a popup window, which might not ever finish or work, so the callback may never be called.
|
||||
@reauthorize()
|
||||
@listenToOnce(@, 'logged-in', onReauthorized)
|
||||
else
|
||||
|
|
|
@ -21,7 +21,7 @@ class CocoModel extends Backbone.Model
|
|||
|
||||
type: ->
|
||||
@constructor.className
|
||||
|
||||
|
||||
clone: (withChanges=true) ->
|
||||
# Backbone does not support nested documents
|
||||
clone = super()
|
||||
|
@ -207,20 +207,20 @@ class CocoModel extends Backbone.Model
|
|||
return true if permission.access in ['owner', 'write']
|
||||
|
||||
return false
|
||||
|
||||
|
||||
getDelta: ->
|
||||
differ = deltasLib.makeJSONDiffer()
|
||||
differ.diff @_revertAttributes, @attributes
|
||||
|
||||
|
||||
applyDelta: (delta) ->
|
||||
newAttributes = $.extend(true, {}, @attributes)
|
||||
jsondiffpatch.patch newAttributes, delta
|
||||
@set newAttributes
|
||||
|
||||
|
||||
getExpandedDelta: ->
|
||||
delta = @getDelta()
|
||||
deltasLib.expandDelta(delta, @_revertAttributes, @schema())
|
||||
|
||||
|
||||
addPatchToAcceptOnSave: (patch) ->
|
||||
@acceptedPatches ?= []
|
||||
@acceptedPatches.push patch
|
||||
|
|
|
@ -28,7 +28,7 @@ module.exports = class LevelSaveView extends SaveVersionModal
|
|||
context.hasChanges = (context.levelNeedsSave or context.modifiedComponents.length or context.modifiedSystems.length)
|
||||
@lastContext = context
|
||||
context
|
||||
|
||||
|
||||
afterRender: ->
|
||||
super()
|
||||
changeEls = @$el.find('.changes-stub')
|
||||
|
@ -37,8 +37,11 @@ module.exports = class LevelSaveView extends SaveVersionModal
|
|||
models = models.concat @lastContext.modifiedSystems
|
||||
for changeEl, i in changeEls
|
||||
model = models[i]
|
||||
deltaView = new DeltaView({model:model})
|
||||
@insertSubView(deltaView, $(changeEl))
|
||||
try
|
||||
deltaView = new DeltaView({model:model})
|
||||
@insertSubView(deltaView, $(changeEl))
|
||||
catch e
|
||||
console.error "Couldn't create delta view:", e
|
||||
|
||||
shouldSaveEntity: (m) ->
|
||||
return true if m.hasLocalChanges()
|
||||
|
|
|
@ -31,8 +31,11 @@ module.exports = class SaveVersionModal extends ModalView
|
|||
super()
|
||||
@$el.find(if me.get('signedCLA') then '#accept-cla-wrapper' else '#save-version-button').hide()
|
||||
changeEl = @$el.find('.changes-stub')
|
||||
deltaView = new DeltaView({model:@model})
|
||||
@insertSubView(deltaView, changeEl)
|
||||
try
|
||||
deltaView = new DeltaView({model:@model})
|
||||
@insertSubView(deltaView, changeEl)
|
||||
catch e
|
||||
console.error "Couldn't create delta view:", e
|
||||
@$el.find('.commit-message input').attr('placeholder', $.i18n.t('general.commit_msg'))
|
||||
|
||||
onClickSaveButton: ->
|
||||
|
@ -61,7 +64,7 @@ module.exports = class SaveVersionModal extends ModalView
|
|||
|
||||
res.success =>
|
||||
@hide()
|
||||
|
||||
|
||||
onClickCLALink: ->
|
||||
window.open('/cla', 'cla', 'height=800,width=900')
|
||||
|
||||
|
@ -79,4 +82,4 @@ module.exports = class SaveVersionModal extends ModalView
|
|||
@$el.find('#save-version-button').show()
|
||||
|
||||
onAgreeFailed: =>
|
||||
@$el.find('#agreement-button').text('Failed').prop('disabled', false)
|
||||
@$el.find('#agreement-button').text('Failed').prop('disabled', false)
|
||||
|
|
|
@ -41,7 +41,7 @@ module.exports = class LadderTabView extends CocoView
|
|||
checkFriends: ->
|
||||
return if @checked or (not window.FB) or (not window.gapi)
|
||||
@checked = true
|
||||
|
||||
|
||||
@addSomethingToLoad("facebook_status")
|
||||
FB.getLoginStatus (response) =>
|
||||
@facebookStatus = response.status
|
||||
|
@ -65,7 +65,7 @@ module.exports = class LadderTabView extends CocoView
|
|||
loadFacebookFriends: ->
|
||||
@addSomethingToLoad("facebook_friends")
|
||||
FB.api '/me/friends', @onFacebookFriendsLoaded
|
||||
|
||||
|
||||
onFacebookFriendsLoaded: (response) =>
|
||||
@facebookData = response.data
|
||||
@loadFacebookFriendSessions()
|
||||
|
@ -89,7 +89,7 @@ module.exports = class LadderTabView extends CocoView
|
|||
friend.otherTeam = if friend.team is 'humans' then 'ogres' else 'humans'
|
||||
friend.imageSource = "http://graph.facebook.com/#{friend.facebookID}/picture"
|
||||
@facebookFriendSessions = result
|
||||
|
||||
|
||||
# GOOGLE PLUS
|
||||
|
||||
onConnectGPlus: ->
|
||||
|
@ -98,10 +98,10 @@ module.exports = class LadderTabView extends CocoView
|
|||
application.gplusHandler.reauthorize()
|
||||
|
||||
onConnectedWithGPlus: -> location.reload() if @connecting
|
||||
|
||||
|
||||
gplusSessionStateLoaded: ->
|
||||
if application.gplusHandler.loggedIn
|
||||
@addSomethingToLoad("gplus_friends")
|
||||
@addSomethingToLoad("gplus_friends", 0) # this might not load ever, so we can't wait for it
|
||||
application.gplusHandler.loadFriends @gplusFriendsLoaded
|
||||
|
||||
gplusFriendsLoaded: (friends) =>
|
||||
|
@ -127,7 +127,7 @@ module.exports = class LadderTabView extends CocoView
|
|||
friend.otherTeam = if friend.team is 'humans' then 'ogres' else 'humans'
|
||||
friend.imageSource = friendsMap[friend.gplusID].image.url
|
||||
@gplusFriendSessions = result
|
||||
|
||||
|
||||
# LADDER LOADING
|
||||
|
||||
refreshLadder: ->
|
||||
|
@ -140,7 +140,7 @@ module.exports = class LadderTabView extends CocoView
|
|||
|
||||
render: ->
|
||||
super()
|
||||
|
||||
|
||||
@$el.find('.histogram-display').each (i, el) =>
|
||||
histogramWrapper = $(el)
|
||||
team = _.find @teams, name: histogramWrapper.data('team-name')
|
||||
|
@ -149,7 +149,7 @@ module.exports = class LadderTabView extends CocoView
|
|||
$.get("/db/level/#{@level.get('slug')}/histogram_data?team=#{team.name.toLowerCase()}", (data) -> histogramData = data)
|
||||
).then =>
|
||||
@generateHistogram(histogramWrapper, histogramData, team.name.toLowerCase())
|
||||
|
||||
|
||||
getRenderData: ->
|
||||
ctx = super()
|
||||
ctx.level = @level
|
||||
|
@ -166,7 +166,7 @@ module.exports = class LadderTabView extends CocoView
|
|||
#renders twice, hack fix
|
||||
if $("#"+histogramElement.attr("id")).has("svg").length then return
|
||||
histogramData = histogramData.map (d) -> d*100
|
||||
|
||||
|
||||
margin =
|
||||
top: 20
|
||||
right: 20
|
||||
|
@ -175,17 +175,17 @@ module.exports = class LadderTabView extends CocoView
|
|||
|
||||
width = 300 - margin.left - margin.right
|
||||
height = 125 - margin.top - margin.bottom
|
||||
|
||||
|
||||
formatCount = d3.format(",.0")
|
||||
|
||||
|
||||
x = d3.scale.linear().domain([-3000,6000]).range([0,width])
|
||||
|
||||
data = d3.layout.histogram().bins(x.ticks(20))(histogramData)
|
||||
y = d3.scale.linear().domain([0,d3.max(data, (d) -> d.y)]).range([height,0])
|
||||
|
||||
|
||||
#create the x axis
|
||||
xAxis = d3.svg.axis().scale(x).orient("bottom").ticks(5).outerTickSize(0)
|
||||
|
||||
|
||||
svg = d3.select("#"+histogramElement.attr("id")).append("svg")
|
||||
.attr("width", width + margin.left + margin.right)
|
||||
.attr("height", height + margin.top + margin.bottom)
|
||||
|
@ -194,25 +194,25 @@ module.exports = class LadderTabView extends CocoView
|
|||
barClass = "bar"
|
||||
if teamName.toLowerCase() is "ogres" then barClass = "ogres-bar"
|
||||
if teamName.toLowerCase() is "humans" then barClass = "humans-bar"
|
||||
|
||||
|
||||
bar = svg.selectAll(".bar")
|
||||
.data(data)
|
||||
.enter().append("g")
|
||||
.attr("class",barClass)
|
||||
.attr("transform", (d) -> "translate(#{x(d.x)},#{y(d.y)})")
|
||||
|
||||
.attr("transform", (d) -> "translate(#{x(d.x)},#{y(d.y)})")
|
||||
|
||||
bar.append("rect")
|
||||
.attr("x",1)
|
||||
.attr("width",width/20)
|
||||
.attr("height", (d) -> height - y(d.y))
|
||||
if @leaderboards[teamName].session?
|
||||
playerScore = @leaderboards[teamName].session.get('totalScore') * 100
|
||||
if playerScore = @leaderboards[teamName].session?.get('totalScore')
|
||||
playerScore *= 100
|
||||
scorebar = svg.selectAll(".specialbar")
|
||||
.data([playerScore])
|
||||
.enter().append("g")
|
||||
.attr("class","specialbar")
|
||||
.attr("transform", "translate(#{x(playerScore)},#{y(9001)})")
|
||||
|
||||
|
||||
scorebar.append("rect")
|
||||
.attr("x",1)
|
||||
.attr("width",3)
|
||||
|
@ -220,7 +220,7 @@ module.exports = class LadderTabView extends CocoView
|
|||
rankClass = "rank-text"
|
||||
if teamName.toLowerCase() is "ogres" then rankClass = "rank-text ogres-rank-text"
|
||||
if teamName.toLowerCase() is "humans" then rankClass = "rank-text humans-rank-text"
|
||||
|
||||
|
||||
message = "#{histogramData.length} players"
|
||||
if @leaderboards[teamName].session? then message="#{@leaderboards[teamName].myRank}/#{histogramData.length}"
|
||||
svg.append("g")
|
||||
|
@ -230,14 +230,14 @@ module.exports = class LadderTabView extends CocoView
|
|||
.attr("text-anchor","end")
|
||||
.attr("x",width)
|
||||
.text(message)
|
||||
|
||||
|
||||
#Translate the x-axis up
|
||||
svg.append("g")
|
||||
.attr("class", "x axis")
|
||||
.attr("transform","translate(0," + height + ")")
|
||||
.call(xAxis)
|
||||
|
||||
|
||||
|
||||
|
||||
consolidateFriends: ->
|
||||
allFriendSessions = (@facebookFriendSessions or []).concat(@gplusFriendSessions or [])
|
||||
sessions = _.uniq allFriendSessions, false, (session) -> session._id
|
||||
|
@ -249,11 +249,11 @@ class LeaderboardData extends CocoClass
|
|||
###
|
||||
Consolidates what you need to load for a leaderboard into a single Backbone Model-like object.
|
||||
###
|
||||
|
||||
|
||||
constructor: (@level, @team, @session) ->
|
||||
super()
|
||||
@fetch()
|
||||
|
||||
|
||||
fetch: ->
|
||||
@topPlayers = new LeaderboardCollection(@level, {order:-1, scoreOffset: HIGHEST_SCORE, team: @team, limit: 20})
|
||||
promises = []
|
||||
|
@ -279,7 +279,7 @@ class LeaderboardData extends CocoClass
|
|||
@trigger 'sync', @
|
||||
# TODO: cache user ids -> names mapping, and load them here as needed,
|
||||
# and apply them to sessions. Fetching each and every time is too costly.
|
||||
|
||||
|
||||
onFail: (resource, jqxhr) =>
|
||||
return if @destroyed
|
||||
@trigger 'error', @, jqxhr
|
||||
|
|
|
@ -130,7 +130,7 @@ module.exports = class TomeView extends View
|
|||
@thangSpells[thang.id].push spellKey
|
||||
unless method.cloneOf
|
||||
skipProtectAPI = @getQueryVariable "skip_protect_api", not @options.ladderGame
|
||||
skipFlow = @getQueryVariable "skip_flow", @options.levelID is 'brawlwood'
|
||||
skipFlow = @getQueryVariable "skip_flow", @options.levelID is 'brawlwood' or @options.levelID is 'resource-gathering-multiplayer'
|
||||
spell = @spells[spellKey] = new Spell programmableMethod: method, spellKey: spellKey, pathComponents: pathPrefixComponents.concat(pathComponents), session: @options.session, supermodel: @supermodel, skipFlow: skipFlow, skipProtectAPI: skipProtectAPI, worker: @worker
|
||||
for thangID, spellKeys of @thangSpells
|
||||
thang = world.getThangByID thangID
|
||||
|
|
Loading…
Reference in a new issue