/**
 * Created by King Lee on 2015/1/8.
 */
var handlerMgr = require("./../handlerMgr");
var consts = require("../../../util/consts");
var pomelo = require('pomelo');
var util = require('../../../util/util');
var async = require('async');
var rival_vs_title_json = require('../../../../config/rival_vs_title');

handlerMgr.handler(consts.TYPE_MSG.TYPE_GET_RANK_PARTIAL_FOR_PVP, function (msg, session, next) {
    var device_guid = msg.deviceid;
    var championship_id = util.getWeek(new Date());
    var rank_pvp_wrapper = pomelo.app.get("rank_pvp_wrapper");
    async.parallel([
            function (callback) {
                rank_pvp_wrapper.get_score_rank_partial_weekly(championship_id, function (reply) {
                    //  reply is rank as a json array
                    if(0 != reply.length){
                        rank_pvp_wrapper.get_rank_info_batch(reply, function (reply) {
                            callback(null, reply);
                        });
                    }else{
                        callback(null, []);
                    }
                });
            },
            function (callback) {
                rank_pvp_wrapper.get_score_rank_partial(function (reply) {
                    //  reply is rank as a json array
                    if(0 != reply.length){
                        rank_pvp_wrapper.get_rank_info_batch(reply, function (reply) {
                            callback(null, reply);
                        });
                    }else{
                        callback(null, []);
                    }
                });
            },
            function (callback) {
                rank_pvp_wrapper.get_score_rank_weekly(device_guid,championship_id,function (reply) {
                    callback(null,reply);
                });
            },
            function (callback) {
                rank_pvp_wrapper.get_score_rank(device_guid,function (reply) {
                    callback(null,reply);
                });
            }
        ],
        // optional callback
        function (err,result) {
            if (err) {
                console.error(err);
            }
            var rank_info_array = result[0];
            var rank_info_array_weekly = result[1];
            var mine_score_rank = result[2];
            var mine_score_rank_weekly = result[3];
            var score_rank_array = [];
            var score_rank_array_weekly = [];
            var degree;
            var degree_title;
            for(var i = 0; i < rank_info_array.length; ++i){
                if(rank_info_array[i])
                {
                    var rank_info = JSON.parse(rank_info_array[i]);
                    if(rank_info){
                        for (var v in rival_vs_title_json) {
                            if (rival_vs_title_json[v].score <= rank_info.score) {
                                degree_title = rival_vs_title_json[v].title;
                                degree = rival_vs_title_json[v].grade;
                            }
                        }
                    }
                    score_rank_array.push({driver_id:rank_info.racer,
                        nickname:rank_info.nickname,
                        degree_title:degree_title,
                        area:rank_info.area,
                        rank:i + 1,
                        score:rank_info.score})
                }
            }
            for(var i = 0; i < rank_info_array_weekly.length; ++i){
                if(rank_info_array_weekly[i])
                {
                    var rank_info = JSON.parse(rank_info_array_weekly[i]);
                    if(rank_info){
                        for (var v in rival_vs_title_json) {
                            if (rival_vs_title_json[v].score <= rank_info.score) {
                                degree_title = rival_vs_title_json[v].title;
                                degree = rival_vs_title_json[v].grade;
                            }
                        }
                    }
                    score_rank_array_weekly.push({driver_id:rank_info.racer,
                        nickname:rank_info.nickname,
                        degree_title:degree_title,
                        area:rank_info.area,
                        rank:i + 1,
                        score:rank_info.score})
                }
            }
            next(null, {
                code: 0,
                msg_id : msg.msg_id,
                flowid : msg.flowid,
                time:Math.floor(Date.now()/1000),
                score_rank_array:score_rank_array,
                score_rank_array_weekly:score_rank_array_weekly,
                mine_score_rank:mine_score_rank != null ? parseInt(mine_score_rank) + 1: mine_score_rank,
                mine_score_rank_weekly:mine_score_rank_weekly != null ? parseInt(mine_score_rank_weekly) + 1: mine_score_rank_weekly
            });
        }
    );
});
@@ -2193,32 +2193,113 @@
 %0A            %7D%0D%0A
+            if(0)%7B%0D%0A                console.log(%22%25j%22,result%5B1%5D);%0D%0A            %7D%0D%0A
             var 
@@ -2313,16 +2313,23 @@
 fo_array
+_weekly
  = resul
@@ -2358,39 +2358,32 @@
  rank_info_array
-_weekly
  = result%5B1%5D;%0D%0A 
@@ -2412,16 +2412,23 @@
 ore_rank
+_weekly
  = resul
@@ -2457,39 +2457,32 @@
  mine_score_rank
-_weekly
  = result%5B3%5D;%0D%0A 

fix bug rank for pvp