codecombat/app/lib/world/world_script_note.coffee

43 lines
1.4 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
{clone} = require './world_utils'
{scriptMatchesEventPrereqs} = require './script_event_prereqs'
module.exports = class WorldScriptNote
2014-06-30 22:16:26 -04:00
@className: 'WorldScriptNote'
2014-01-03 13:32:13 -05:00
constructor: (script, @event, world) ->
return unless script?
@invalid = true
return unless scriptMatchesEventPrereqs(script, @event)
2014-01-03 13:32:13 -05:00
# Could add the scriptPrereqsSatisfied or seen/repeats stuff if needed
@invalid = false
@channel = script.channel
@event ?= {}
@event.replacedNoteChain = script.noteChain
serialize: ->
o = {channel: @channel, event: {}}
for key, value of @event
if value?.isThang
2014-01-03 13:32:13 -05:00
value = {isThang: true, id: value.id}
else if _.isArray value
for subval, i in value
if subval?.isThang
2014-01-03 13:32:13 -05:00
value[i] = {isThang: true, id: subval.id}
o.event[key] = value
o
@deserialize: (o, world, classMap) ->
scriptNote = new WorldScriptNote
scriptNote.channel = o.channel
scriptNote.event = {}
for key, value of o.event
if value? and typeof value is 'object' and value.isThang
value = world.getThangByID value.id
else if _.isArray value
for subval, i in value
if subval? and typeof subval is 'object' and subval.isThang
value[i] = world.getThangByID subval.id
else if value? and typeof value is 'object' and value.CN
value = classMap[value.CN].deserialize value, world, classMap
scriptNote.event[key] = value
scriptNote