001package ball.game.ant.taskdefs;
002/*-
003 * ##########################################################################
004 * Game Applications and Utilities
005 * %%
006 * Copyright (C) 2010 - 2022 Allen D. Ball
007 * %%
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *      http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 * ##########################################################################
020 */
021import ball.game.card.Card;
022import ball.game.card.poker.Evaluator;
023import ball.util.ant.taskdefs.AnnotatedAntTask;
024import ball.util.ant.taskdefs.AntTask;
025import ball.util.ant.taskdefs.ClasspathDelegateAntTask;
026import ball.util.ant.taskdefs.ConfigurableAntTask;
027import ball.util.ant.taskdefs.NotNull;
028import java.util.Arrays;
029import java.util.Collection;
030import java.util.List;
031import java.util.stream.Collectors;
032import lombok.Getter;
033import lombok.NoArgsConstructor;
034import lombok.Setter;
035import lombok.ToString;
036import lombok.experimental.Accessors;
037import org.apache.tools.ant.BuildException;
038import org.apache.tools.ant.Task;
039import org.apache.tools.ant.util.ClasspathUtils;
040
041import static lombok.AccessLevel.PROTECTED;
042
043/**
044 * Abstract {@link Card} {@link.uri http://ant.apache.org/ Ant}
045 * {@link Task} base class.
046 *
047 * {@ant.task}
048 *
049 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
050 */
051@NoArgsConstructor(access = PROTECTED)
052public abstract class CardTask extends Task implements AnnotatedAntTask, ClasspathDelegateAntTask, ConfigurableAntTask  {
053    @Getter @Setter @Accessors(chain = true, fluent = true)
054    private ClasspathUtils.Delegate delegate = null;
055
056    @Override
057    public void init() throws BuildException {
058        super.init();
059        ClasspathDelegateAntTask.super.init();
060        ConfigurableAntTask.super.init();
061    }
062
063    @Override
064    public void execute() throws BuildException {
065        super.execute();
066        AnnotatedAntTask.super.execute();
067    }
068
069    /**
070     * {@link.uri http://ant.apache.org/ Ant}
071     * {@link org.apache.tools.ant.Task} to evaluate a poker hand.
072     *
073     * {@ant.task}
074     */
075    @AntTask("poker-hand-evaluate")
076    @NoArgsConstructor @ToString
077    public static class PokerHandEvaluate extends CardTask {
078        @NotNull @Getter
079        private List<Card> cards = null;
080
081        public void setCards(String string) {
082            cards =
083                Arrays.stream(string.split("[, ]+"))
084                .map(t -> Card.parse(t))
085                .collect(Collectors.toList());
086        }
087
088        @Override
089        public void execute() throws BuildException {
090            super.execute();
091
092            try {
093                var evaluator = new EvaluatorImpl(getCards());
094
095                log(String.valueOf(evaluator.getRanking()) + ": " + String.valueOf(evaluator.getHand()));
096            } catch (BuildException exception) {
097                throw exception;
098            } catch (Throwable throwable) {
099                throwable.printStackTrace();
100                throw new BuildException(throwable);
101            }
102        }
103
104        private class EvaluatorImpl extends Evaluator {
105            public EvaluatorImpl(Collection<Card> collection) {
106                super(collection);
107            }
108
109            @Override
110            public void accept(List<Card> list) {
111                super.accept(list);
112
113                log(String.valueOf(list) + " -> " + toString());
114            }
115        }
116    }
117}