<commit_before>import com.cardshifter.modapi.base.Entity
import com.cardshifter.modapi.cards.DrawStartCards
import com.cardshifter.modapi.cards.ZoneComponent
import com.cardshifter.modapi.players.Players
import com.cardshifter.modapi.resources.ECSResource

class EffectDelegate {

    StringBuilder description = new StringBuilder()
    List<Closure> closures = new ArrayList<>()

    def perform(Entity source, Entity target) {
        closures.each {
            it.call(source, target)
        }
    }

    static def entityLookup(Entity entity, String who) {
        if (who == 'owner') {
            return Players.findOwnerFor(entity)
        }
        if (who == 'you') {
            return Players.findOwnerFor(entity)
        }
        if (who == 'opponent') {
            return Players.getNextPlayer(Players.findOwnerFor(entity))
        }
        assert false
    }

    static def zoneLookup(Entity entity, String zoneName) {
        def zone = entity.getSuperComponents(ZoneComponent).stream()
            .filter({it.getName().equals(zoneName)})
            .findAny().get()
        assert zone
        zone
    }

    def drawCard(String who, int count) {
        def s = count == 1 ? '' : 's'
        description.append("$who draw $count card$s\n")
        closures.add({Entity source, Entity target ->
            Entity drawer = entityLookup(source, who)
            DrawStartCards.drawCard(drawer)
        })
    }

    def heal(int value) {
        [to: {String who ->
            String desc = "Heal $who by $value"
            Closure closure = {Entity source, Entity target ->
                Entity entity = entityLookup(source, who)
                assert entity : 'Invalid entity'
                assert value >= 0 : 'Value cannot be negative'
                ECSResource resource = entity.game.resource('health')
                assert resource : 'health resource not found'
                resource.retriever().resFor(entity).change(value)
            }
            description.append(desc)
            description.append('\n')<commit_msg>added support for `withProbability`, fixes #233<commit_after>import com.cardshifter.modapi.base.Entity
import com.cardshifter.modapi.cards.DrawStartCards
import com.cardshifter.modapi.cards.ZoneComponent
import com.cardshifter.modapi.players.Players
import com.cardshifter.modapi.resources.ECSResource

class EffectDelegate {

    StringBuilder description = new StringBuilder()
    List<Closure> closures = new ArrayList<>()

    def perform(Entity source, Entity target) {
        closures.each {
            it.call(source, target)
        }
    }

    static def entityLookup(Entity entity, String who) {
        if (who == 'owner') {
            return Players.findOwnerFor(entity)
        }
        if (who == 'you') {
            return Players.findOwnerFor(entity)
        }
        if (who == 'opponent') {
            return Players.getNextPlayer(Players.findOwnerFor(entity))
        }
        assert false
    }

    static def zoneLookup(Entity entity, String zoneName) {
        def zone = entity.getSuperComponents(ZoneComponent).stream()
            .filter({it.getName().equals(zoneName)})
            .findAny().get()
        assert zone
        zone
    }

    def withProbability(double probability, @DelegatesTo(EffectDelegate) Closure action) {
        EffectDelegate deleg = new EffectDelegate()
        action.setDelegate(deleg)
        action.setResolveStrategy(Closure.DELEGATE_ONLY)
        action.call()
        assert deleg.closures.size() > 0 : 'probability condition needs to have some actions'
        description.append("$probability chance to $deleg.description")
        closures.add({Entity source, Entity target ->
            double random = source.game.random.nextDouble()
            println "random $random probability $probability perform ${random < probability}"
            if (random < probability) {
                println "Calling closures: $deleg.closures"
                for (Closure act : deleg.closures) {
                    act.call(source, target)
                }
            }
        })
    }

    def drawCard(String who, int count) {
        def s = count == 1 ? '' : 's'
        description.append("$who draw $count card$s\n")
        closures.add({Entity source, Entity target ->
            Entity drawer = entityLookup(source, who)
            DrawStartCards.drawCard(drawer)
        })
    }

    def heal(int value) {
        [to: {String who ->
            String desc = "Heal $who by $value"
            Closure closure = {Entity source, Entity target ->
                Entity entity = entityLookup(source, who)
                assert entity : 'Invalid entity'
                assert value >= 0 : 'Value cannot be negative'
                ECSResource resource = entity.game.resource('health')
                assert resource : 'health resource not found'
                resource.retriever().resFor(entity).change(value)
            }
            description.append(desc)
            description.append('\n')
{'fraction_non_alphanumeric': 0.10083201267828844, 'fraction_numerical': 0.001584786053882726, 'mean_word_length': 2.1457943925233645, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '2275771', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1303, 'n_words': 327}