<commit_before>package systems;

import SystemsDelegate
import com.cardshifter.modapi.actions.UseCostSystem
import com.cardshifter.modapi.actions.attack.AttackDamageAccumulating
import com.cardshifter.modapi.actions.attack.AttackDamageHealAtEndOfTurn
import com.cardshifter.modapi.actions.attack.AttackOnBattlefield
import com.cardshifter.modapi.actions.attack.AttackSickness
import com.cardshifter.modapi.actions.attack.AttackTargetMinionsFirstThenPlayer
import com.cardshifter.modapi.actions.attack.TrampleSystem
import com.cardshifter.modapi.base.ECSGame
import com.cardshifter.modapi.base.ECSSystem
import com.cardshifter.modapi.base.Entity
import com.cardshifter.modapi.cards.DamageConstantWhenOutOfCardsSystem
import com.cardshifter.modapi.cards.DrawCardAtBeginningOfTurnSystem
import com.cardshifter.modapi.cards.DrawCardEvent
import com.cardshifter.modapi.cards.DrawStartCards
import com.cardshifter.modapi.cards.LimitedHandSizeSystem
import com.cardshifter.modapi.cards.PlayEntersBattlefieldSystem
import com.cardshifter.modapi.cards.PlayFromHandSystem
import com.cardshifter.modapi.phase.GainResourceSystem
import com.cardshifter.modapi.phase.PerformerMustBeCurrentPlayer
import com.cardshifter.modapi.phase.RestoreResourcesSystem
import com.cardshifter.modapi.players.Players
import com.cardshifter.modapi.resources.ECSResource
import com.cardshifter.modapi.resources.RestoreResourcesToSystem
import net.zomis.cardshifter.ecs.effects.EntityInt
import net.zomis.cardshifter.ecs.usage.ApplyAfterAttack
import net.zomis.cardshifter.ecs.usage.DestroyAfterUseSystem

import java.util.function.BiPredicate
import java.util.function.Consumer
import java.util.function.Predicate
import java.util.function.ToIntFunction
import java.util.function.UnaryOperator;

class AttackSystemDelegate {
    ECSGame game

    def zone(String name) {
        assert name == 'Battlefield' // Only supported right now
        addSystem(new AttackOnBattlefield())
    }

    def cardsFirst(ECSResource resource) {
        addSystem new AttackTargetMinionsFirstThenPlayer(resource)
    }

    def sickness(ECSResource resource) {
        addSystem new AttackSickness(resource)
    }

    def accumulating(ECSResource attack, ECSResource health, BiPredicate<Entity, Entity> allowCounterAttack) {
        addSystem new AttackDamageAccumulating(attack, health, allowCounterAttack)
    }

    def healAtEndOfTurn(ECSResource health, ECSResource maxHealth) {
        addSystem new AttackDamageHealAtEndOfTurn(health, maxHealth)
    }

    def afterAttack(Predicate<Entity> condition, Consumer<Entity> apply) {
        addSystem new ApplyAfterAttack(condition, apply)
    }

    def trample(ECSResource resource) {
        addSystem new TrampleSystem(resource)
    }

    def methodMissing(String name, args) {
        println 'AttackSystems missing method ' + name
    }

    def addSystem(ECSSystem system) {
        game.addSystem(system)
    }
}

public class GeneralSystems {
    static UnaryOperator<Entity> whoPays(String str) {
        if (str.equals('owner') || str.equals('player')) {
            println 'return owner pays'
            return {Entity e -> Players.findOwnerFor(e)}
        }
        if (str.equals('self')) {
            println 'return self pays'
            return {Entity e -> e}
        }
        println 'neither matches. throw exception'
        throw new UnsupportedOperationException('whoPays? ' + str)
    }

    static def setup(ECSGame game) {
        SystemsDelegate.metaClass.PerformerMustBeCurrentPlayer << {
            addSystem(new PerformerMustBeCurrentPlayer())
        }
        SystemsDelegate.metaClass.startCards << {int count ->
            addSystem(new DrawStartCards(count))
        }
        SystemsDelegate.metaClass.attackSystem << {Closure clos ->
            clos.delegate = new AttackSystemDelegate(game: game)
            clos.call()
        }
    }

    static def resourceSystems() {
        SystemsDelegate.metaClass.gainResource << {Map map ->
            ECSResource resource = (ECSResource) map.get('res')
            int value = (int) map.get('value')
            int untilMax = (int) map.get('untilMax')
            EntityInt restore = {e -> Math.min(1, Math.max((int) 0, (int) untilMax - resource.getFor(entity)))}

            addSystem(new GainResourceSystem(resource, {e -> 1}))
        }
        SystemsDelegate.metaClass.restoreResources << {Map map ->
            ECSResource resource = (ECSResource) map.get('resource')
            Object value = map.get('value')
            EntityInt entityInt;
            if (value instanceof Closure) {
                entityInt = value
            } else if (value instanceof ECSResource) {
                entityInt = {Entity e -> (value as ECSResource).getFor(e)}
            } else {
                assert value instanceof Number
                entityInt = {e -> value}<commit_msg>added more Groovy system support<commit_after>package systems;

import SystemsDelegate
import com.cardshifter.modapi.actions.UseCostSystem
import com.cardshifter.modapi.actions.attack.AttackDamageAccumulating
import com.cardshifter.modapi.actions.attack.AttackDamageHealAtEndOfTurn
import com.cardshifter.modapi.actions.attack.AttackOnBattlefield
import com.cardshifter.modapi.actions.attack.AttackSickness
import com.cardshifter.modapi.actions.attack.AttackTargetMinionsFirstThenPlayer
import com.cardshifter.modapi.actions.attack.TrampleSystem
import com.cardshifter.modapi.base.ECSGame
import com.cardshifter.modapi.base.ECSSystem
import com.cardshifter.modapi.base.Entity
import com.cardshifter.modapi.cards.DamageConstantWhenOutOfCardsSystem
import com.cardshifter.modapi.cards.DrawCardAtBeginningOfTurnSystem
import com.cardshifter.modapi.cards.DrawCardEvent
import com.cardshifter.modapi.cards.DrawStartCards
import com.cardshifter.modapi.cards.LimitedHandSizeSystem
import com.cardshifter.modapi.cards.PlayEntersBattlefieldSystem
import com.cardshifter.modapi.cards.PlayFromHandSystem
import com.cardshifter.modapi.cards.RemoveDeadEntityFromZoneSystem
import com.cardshifter.modapi.phase.GainResourceSystem
import com.cardshifter.modapi.phase.PerformerMustBeCurrentPlayer
import com.cardshifter.modapi.phase.RestoreResourcesSystem
import com.cardshifter.modapi.players.Players
import com.cardshifter.modapi.resources.ECSResource
import com.cardshifter.modapi.resources.GameOverIfNoHealth
import com.cardshifter.modapi.resources.ResourceRecountSystem
import com.cardshifter.modapi.resources.RestoreResourcesToSystem
import net.zomis.cardshifter.ecs.effects.EffectActionSystem
import net.zomis.cardshifter.ecs.effects.EffectTargetFilterSystem
import net.zomis.cardshifter.ecs.effects.EntityInt
import net.zomis.cardshifter.ecs.usage.ApplyAfterAttack
import net.zomis.cardshifter.ecs.usage.DestroyAfterUseSystem
import net.zomis.cardshifter.ecs.usage.LastPlayersStandingEndsGame

import java.util.function.BiPredicate
import java.util.function.Consumer
import java.util.function.Predicate
import java.util.function.ToIntFunction
import java.util.function.UnaryOperator;

class AttackSystemDelegate {
    ECSGame game

    def zone(String name) {
        assert name == 'Battlefield' // Only supported right now
        addSystem(new AttackOnBattlefield())
    }

    def cardsFirst(ECSResource resource) {
        addSystem new AttackTargetMinionsFirstThenPlayer(resource)
    }

    def sickness(ECSResource resource) {
        addSystem new AttackSickness(resource)
    }

    def accumulating(ECSResource attack, ECSResource health, BiPredicate<Entity, Entity> allowCounterAttack) {
        addSystem new AttackDamageAccumulating(attack, health, allowCounterAttack)
    }

    def healAtEndOfTurn(ECSResource health, ECSResource maxHealth) {
        addSystem new AttackDamageHealAtEndOfTurn(health, maxHealth)
    }

    def afterAttack(Predicate<Entity> condition, Consumer<Entity> apply) {
        addSystem new ApplyAfterAttack(condition, apply)
    }

    def trample(ECSResource resource) {
        addSystem new TrampleSystem(resource)
    }

    def methodMissing(String name, args) {
        println 'AttackSystems missing method ' + name
    }

    def addSystem(ECSSystem system) {
        game.addSystem(system)
    }
}

public class GeneralSystems {
    static UnaryOperator<Entity> whoPays(String str) {
        if (str.equals('owner') || str.equals('player')) {
            println 'return owner pays'
            return {Entity e -> Players.findOwnerFor(e)}
        }
        if (str.equals('self')) {
            println 'return self pays'
            return {Entity e -> e}
        }
        println 'neither matches. throw exception'
        throw new UnsupportedOperationException('whoPays? ' + str)
    }

    static def setup(ECSGame game) {
        SystemsDelegate.metaClass.PerformerMustBeCurrentPlayer << {
            addSystem(new PerformerMustBeCurrentPlayer())
        }
        SystemsDelegate.metaClass.startCards << {int count ->
            addSystem(new DrawStartCards(count))
        }
        SystemsDelegate.metaClass.attackSystem << {Closure clos ->
            clos.delegate = new AttackSystemDelegate(game: game)
            clos.call()
        }
        SystemsDelegate.metaClass.EffectActionSystem << {String name ->
            addSystem new EffectActionSystem(name)
        }
        SystemsDelegate.metaClass.targetFilterSystem << {String name ->
            addSystem new EffectTargetFilterSystem(name)
        }

        SystemsDelegate.metaClass.GameOverIfNoHealth << {ECSResource resource ->
            addSystem new GameOverIfNoHealth(resource)
        }
        SystemsDelegate.metaClass.LastPlayersStandingEndsGame << {
            addSystem new LastPlayersStandingEndsGame()
        }
        SystemsDelegate.metaClass.RemoveDeadEntityFromZoneSystem << {
            addSystem new RemoveDeadEntityFromZoneSystem()
        }
        SystemsDelegate.metaClass.ResourceRecountSystem << {
            addSystem new ResourceRecountSystem()
        }
    }

    static def resourceSystems() {
        SystemsDelegate.metaClass.gainResource << {Map map ->
            ECSResource resource = (ECSResource) map.get('res')
            int value = (int) map.get('value')
            int untilMax = (int) map.get('untilMax')
            EntityInt restore = {e -> Math.min(1, Math.max((int) 0, (int) untilMax - resource.getFor(entity)))}

            addSystem(new GainResourceSystem(resource, {e -> 1}))
        }
        SystemsDelegate.metaClass.restoreResources << {Map map ->
            ECSResource resource = (ECSResource) map.get('resource')
            Object value = map.get('value')
            EntityInt entityInt;
            if (value instanceof Closure) {
                entityInt = value
            } else if (value instanceof ECSResource) {
                entityInt = {Entity e -> (value as ECSResource).getFor(e)}
            } else {
                assert value instanceof Number
                entityInt = {e -> value}
{'fraction_non_alphanumeric': 0.08485235144002916, 'fraction_numerical': 0.0005468465184104995, 'mean_word_length': 3.385691446842526, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3022058', 'n_tokens_mistral': 3218, 'n_tokens_neox': 3083, 'n_words': 583}