#!bin/bash
MONGO_SRV="mongodb://10.6.36.60" #线上mongo集群连接地址
SRV_RANGE=(1 1)                  #区服范围 闭区间 [m,n]
DB=()
function fixSrv {
mongo $MONGO_SRV 2>&1 >/dev/null <<-EOF

const getLevelAndExp = (attrs) => {
let level = 0,
exp = 0,
expIdx = 0
attrs.forEach((attr, idx) => {
var { typ, value } = attr
if (typ == 2) {
level = value
}
if (typ == 3) {
exp = value
expIdx = idx
}
})
return { level, exp, expIdx }
}

let dbs = [
'$1',
]

dbs.forEach((e) => {
print('修复数据库:'+e)
var ins = db.getSiblingDB(e)
ins.role.find().forEach((role) => {
let { level, exp, expIdx } = getLevelAndExp(role.attrs)
level = Number(level)
exp = Number(exp)
if (newCnf[level] == 0) {
// 达到满级 不管了
return
}
let { exp: oldLevelExp } = calcLevel(oldCnf, exp)
// 新的等级经验 = 旧的等级经验/配置 * 新的配置
let newLevelExp = (oldLevelExp / oldCnf[level]) * newCnf[level]
// 向下取整
newLevelExp = Math.floor(newLevelExp)
let newExp = totalExp(newCnf, level) + newLevelExp
let $set = {}
$set['attrs.' + expIdx + '.value'] = NumberLong(newExp)
ins.role.update({ _id: role._id }, { $set })
})
})
EOF
echo 修复完成:$1
}

for srvId in $(seq ${SRV_RANGE[0]} ${SRV_RANGE[1]}); do
DB[${#DB[*]}]="db_vikingar_master_$srvId"
done

for db in ${DB[*]}; do
fixSrv $db &
done
wait
echo "done..."