Summit Gate BIG PROBLEM! help pleas

ENGLISH:
I need your help. I’ve been trying to pass this level for two days, but it doesn’t work out.
I tried copying the tactics from here: https://github.com/vadim-job-hg/CodeCombat/tree/master/Mountain/SummitsGate
But that doesn’t work either. The hero dies after 1 room where the tower.

RUSSIAN:
Нужна ваша помощь. Два дня пытаюсь пройти этот уровень, никак не выходит.
Попробовал скопировать тактику отсюда: https://github.com/vadim-job-hg/CodeCombat/tree/master/Mountain/SummitsGate
Но и это не работает. Герой умирает после 1 комнаты где башни.

// Прорвись с боем в святилище вождя огров и убей его.
var summonTypes = ["griffin-rider"];
var tactick = "hold";
var stage = 1;

// Призываем солдат
function summonTroops() {
    var type = summonTypes[hero.built.length % summonTypes.length];
    if (hero.gold > hero.costOf(type)) {
        hero.summon(type);
    }
}

// Поиск цели для лечения
function lowestHealthPaladin() {
    var lowestHealth = 99999;
    var lowestFriend = null;
    var friends = hero.findFriends();
    for (var f = 0; f < friends.length; f++) {
        var friend = friends[f];
/*    if (friend.type != "paladin") {
            continue;
        } 
*/
        if (friend.health < lowestHealth && friend.health < friend.maxHealth) {
            lowestHealth = friend.health;
            lowestFriend = friend;
        }
    }
    return lowestFriend;
}

 //Управляем паладинами
function commandPaladin(paladin) {
    var castable = paladin.canCast("heal");
    var target = null;
    if (castable) {
        if (hero.health < hero.maxHealth * 0.8) {
            target = hero;
        } else {
            target = lowestHealthPaladin();
        }
        if (target) {
            hero.command(paladin, "cast", "heal", target);
        }
    } else if (paladin.health < 100) {
        hero.command(paladin, "shield");
    } else if (stage < 4) {
        hero.command(paladin, "move", {'x': 100, 'y': 35});
    } else if (stage == 5) {
        hero.command(paladin, "move", {'x': 284, 'y': 33});
    } else {
        target = hero.findNearestEnemy();
        if (warlock) {
            target = warlock;
        }
        if (target) {
            hero.command(paladin, "attack", target);
        }
    }
}



// командуем солдатами
function commandSoldier(soldier) {
    var target = hero.findNearestEnemy();
    if (warlock) {
        target = warlock;
    } 
    if (stage == 3) {
        hero.command(soldier, "move", {'x': 84, 'y': 34});
    } else if (target) {
        hero.command(soldier, "attack", target);
    }
}

// выбор тактики
function commandFriends() {
    var friends = hero.findFriends();
    for(var i = 0; i < friends.length; i++) {
        var friend = friends[i];
        if (tactick == "hold") {
            hero.command(friend, "defend", {'x': 1, 'y': 40});
        } else if (friend.type == "paladin") {
            commandPaladin(friend);
        } else {
            commandSoldier(friend);
        }
    }
}

function pickUpNearestItem() {
    nearestItem = hero.findNearestItem();
    if (nearestItem)
        move(nearestItem.pos);
}

function moveTo(position) {
    if (hero.isReady("jump")) {
        hero.jumpTo(position);
    } else {
        hero.move(position);
    }
}

function attack(target) {
     if (target) {
        if (hero.distanceTo(target) > 10) {
            moveTo(target.pos);
        } else {
            hero.attack(target);
        }
    }
}

////////////////////////////////////////
commandFriends();
hero.moveXY(31, 56);
while(true) {
    var catapult = hero.findNearest(hero.findByType('catapult'));
    var warlock = hero.findNearest(hero.findByType('warlock'));
    var target = hero.findNearestEnemy();
    var nearestItem = hero.findNearestItem();
    var now = hero.now();
    if (catapult) {
        stage = 1;
        attack(catapult);
    } else if(now < 30) {
        tactick = "defend";
        stage = 2;
        moveTo({'x': 50, 'y': 33});
       
    }  else if (stage < 4) {
        if (target) {
            stage = 3;
            attack(target);
        } else {
            hero.say("m2222");
            moveTo({'x': 172, 'y': 46});
        }
        if (hero.pos.x > 170) {
            stage = 4;
        }
    } else if (stage < 5) {
        if (hero.pos.x < 240) {
            moveTo({'x': 274, 'y': 35});
            tactick = "defend";
        } 
        if(nearestItem && hero.distanceTo(nearestItem) < 10) {
            pickUpNearestItem();
            tactick = 'attack';
        } 
        if(warlock) {
            target = warlock;
            summonTroops();
            hero.attack(target);
        } 
        if (target && target.type != "gates") {
            attack(target);
        }
        if (nearestItem && hero.distanceTo(nearestItem) < 45) {
            pickUpNearestItem();
            tactick = "defend";
            summonTroops();
        } 
        else {
            attack(target);
        }
        if (hero.pos.x > 290) {
            stage = 5;
            tactick = "attack";
        }
    }
    else {
        summonTroops();
        attack(target);
        commandFriends();
    }
commandFriends();
    
}

















Hi welcome to the forum @Bell_Allien I do not understand Russian but I have a suggestion, my suggestion is you do not define “target” more than once unless in if statement.