 
     request: (taskId) ->
 
+        chatType = Chat.tasks[taskId].type
+
         request.push
 
             action: 'getPaginatedPlexts'
             data:   Chat.tasks[taskId].data
             onSuccess: (response, callback) ->
 
-                parseChatResponse taskId, response.result, callback
+                parseChatResponse chatType, taskId, response.result, callback
 
             onError: (err, callback) ->
 
                 # TODO: Recover tasks
                 
-                logger.error "[Broadcasts] #{err.message}"
+                logger.error "[Broadcasts.#{chatType}] #{err.message}"
                 callback()
 
             afterResponse: (callback) ->
 
-                logger.info "[Broadcasts] " +
+                logger.info "[Broadcasts.#{chatType}] " +
                     Math.round(request.done / request.max * 100).toString() +
                     "%\t[#{request.done}/#{request.max}]" +
                     "\t#{messageCount} messages (#{dbQueue.length()} in buffer)"
 
                 callback()
 
-parseChatResponse = (taskId, response, callback) ->
+parseChatResponse = (type, taskId, response, callback) ->
 
     async.each response, (rec, callback) ->
 
-        insertMessage rec[0], rec[1], rec[2], callback
+        insertMessage type, rec[0], rec[1], rec[2], callback
 
     , ->
 

bootstrap = (callback) ->

    if argv.new or argv.n
        Chat.prepareNew ->
            Chat.start callback
    else
        Chat.prepareFromDatabase ->
            Chat.start callback

Chat = 
    
    tasks: {}
    length: 0

    createTasks: (timestampMin, callback) ->

        timestampMax = new Date().getTime()

        preparedTasks = []

        for TSmin in [timestampMin..timestampMax] by Config.Chat.SplitTimespanMS
            
            TSmax = Math.min(timestampMax, TSmin + Config.Chat.SplitTimespanMS - 1)
            continue if TSmax is TSmin

            preparedTasks.push
                data:
                    desiredNumItems: Config.Chat.FetchItemCount
                    minLatE6:        Math.round(Config.Region.SouthWest.Lat * 1e6)


, Config.Database.MaxParallel

insertMessage = (type, id, timestamp, data, callback) ->

    async.series [

        (callback) ->

            if insertCount % 100 is 0
                
                Database.db.collection('Chat.' + type).count {}, (err, count) ->
                    messageCount = count
                    callback()

            else

                callback()
        
        , (callback) ->

            insertCount++

            data2 = data.plext

            # parse markup
            markup = {}
            count = {}

            for m in data.plext.markup
                count[m[0]] = 0 if not count[m[0]]?
                count[m[0]]++
                markup[m[0]+count[m[0]].toString()] = m[1]

            data2.markup = markup

            dbQueue.push (done) ->

                doc = data2
                doc._id = id
                doc.time = timestamp

                async.series [

                    (callback) ->
                    
                        Database.db.collection('Chat.' + type).insert doc, callback

                    , (callback) ->

                        # resove player names
                        if doc.markup.PLAYER1?

                            level = 0

                            if doc.markup.TEXT1.plain is ' deployed an '
                                level = parseInt doc.markup.TEXT2.plain.substr(1)

                            Agent.resolved doc.markup.PLAYER1.plain,
                                team:  Agent.strToTeam(doc.markup.PLAYER1.team)
                                level: level
                            , callback

                        else

                            callback()
                        
                ], ->

                    done()
                    callback()
    
    ], callback