// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 05.09.2006 15:41:33
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   QuizModul.java

package Modules;

import Network.Network;
import Network.NetworkListener;
import Password.Password;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
import java.util.StringTokenizer;
import javax.swing.*;

// Referenced classes of package Modules:
//            Module, ModuleHandler

public class QuizModul
    implements Module, NetworkListener
{
    class DelayedMessage
        implements Runnable
    {

        public void run()
        {
            try
            {
                Thread.sleep(delay);
                network.sendString(message, 0);
            }
            catch(Throwable throwable) { }
        }

        long delay;
        Network network;
        String message;
        final QuizModul this$0;

        public DelayedMessage(Network network1, String s, float f)
        {
            this$0 = QuizModul.this;
            super();
            delay = 0L;
            message = "";
            delay = (long)f;
            message = s;
            network = network1;
            (new Thread(this)).start();
        }
    }


    public QuizModul()
    {
        mainPanel = new JPanel();
        loggedIn = false;
        nickname = "";
        passwort = "";
        channel = "";
        answerSillyboy = false;
        currentRound = 0;
        questionCount = 0;
        rightAnswers = 0;
        currentQuestion = "";
    }

    public String getName()
    {
        return "Quiz";
    }

    public void addModuleHandler(ModuleHandler modulehandler)
    {
        modulehandler.message("KZ-Quiz Ist geladen... ok =)");
        handler = modulehandler;
    }

    public JComponent getGuiElement()
    {
        return mainPanel;
    }

    public void init()
    {
        learnQuestions = false;
        answerQuestions = false;
        answerMath = false;
        usePercentRule = false;
        answerDelay = 2500;
        answerDelayPerChar = 300F;
        mainPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
        JPanel jpanel = new JPanel();
        JButton jbutton = new JButton("LOGIN");
        JButton jbutton1 = new JButton("EXIT");
        final JTextField nickField = new JTextField();
        final JPasswordField passField = new JPasswordField();
        final JTextField channelField = new JTextField();
        jpanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "KZ-Einstellungen:"));
        jpanel.setLayout(new GridLayout(4, 2));
        jpanel.add(new JLabel("Nickname:"));
        jpanel.add(nickField);
        jpanel.add(new JLabel("Passwort:"));
        jpanel.add(passField);
        jpanel.add(new JLabel("Channel:"));
        jpanel.add(channelField);
        jpanel.add(jbutton);
        jpanel.add(jbutton1);
        final QuizModul networkListener = this;
        jbutton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent actionevent)
            {
                if(network == null && !loggedIn)
                {
                    nickname = nickField.getText();
                    passwort = new String(passField.getPassword());
                    channel = channelField.getText();
                    network = new Network(networkListener);
                    handler.message(">>> Erfolgreich mit dem KZ-Chat verbunden!");
                } else
                {
                    handler.message(">>> Evtl. schon Verbunden? =)");
                }
            }

            final JTextField val$nickField;
            final JPasswordField val$passField;
            final JTextField val$channelField;
            final QuizModul val$networkListener;
            final QuizModul this$0;

            
            {
                this$0 = QuizModul.this;
                nickField = jtextfield;
                passField = jpasswordfield;
                channelField = jtextfield1;
                networkListener = quizmodul1;
                super();
            }
        });
        jbutton1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent actionevent)
            {
                if(network != null)
                    network.disconnect();
                else
                    handler.message(">>> Evtl. schon Verbunden? =)");
            }

            final QuizModul this$0;

            
            {
                this$0 = QuizModul.this;
                super();
            }
        });
        JPanel jpanel1 = new JPanel();
        jpanel1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "KZ-Einstellungen:"));
        jpanel1.setLayout(new GridLayout(1, 2));
        jpanel1.add(new JLabel("KZ-URL:"));
        jpanel1.add(scriptField);
        JPanel jpanel2 = new JPanel();
        final JComboBox delayBox = new JComboBox();
        final JComboBox delayPerCharBox = new JComboBox();
        delayBox.addItem("1");
        delayBox.addItem("50");
        delayBox.addItem("100");
        delayBox.addItem("200");
        delayBox.addItem("400");
        delayBox.addItem("800");
        delayBox.addItem("1000");
        delayBox.addItem("2000");
        delayBox.addItem("3000");
        delayBox.addItem("4000");
        delayBox.addItem("4500");
        delayBox.addItem("4750");
        delayBox.addItem("5000");
        delayPerCharBox.addItem("30");
        delayPerCharBox.addItem("50");
        delayPerCharBox.addItem("100");
        delayPerCharBox.addItem("200");
        delayPerCharBox.addItem("300");
        delayPerCharBox.addItem("325");
        delayPerCharBox.addItem("350");
        delayPerCharBox.addItem("375");
        delayPerCharBox.addItem("400");
        delayPerCharBox.addItem("425");
        delayPerCharBox.addItem("450");
        delayPerCharBox.addItem("475");
        delayPerCharBox.addItem("500");
        delayBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent actionevent)
            {
                answerDelay = Integer.parseInt(delayBox.getSelectedItem().toString());
            }

            final JComboBox val$delayBox;
            final QuizModul this$0;

            
            {
                this$0 = QuizModul.this;
                delayBox = jcombobox;
                super();
            }
        });
        delayPerCharBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent actionevent)
            {
                answerDelayPerChar = (float)Integer.parseInt(delayPerCharBox.getSelectedItem().toString());
            }

            final JComboBox val$delayPerCharBox;
            final QuizModul this$0;

            
            {
                this$0 = QuizModul.this;
                delayPerCharBox = jcombobox;
                super();
            }
        });
        jpanel2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Antwortverz\366gerung:"));
        jpanel2.setLayout(new GridLayout(2, 2));
        jpanel2.add(new JLabel("Mindestverz\366gerung: "));
        jpanel2.add(delayBox);
        jpanel2.add(new JLabel("Zeitverz\366gerung pro Zeichen: "));
        jpanel2.add(delayPerCharBox);
        JPanel jpanel3 = new JPanel();
        final JCheckBox optionLearn = new JCheckBox("KZ-Textfragen Lernen");
        final JCheckBox optionAnswer = new JCheckBox("KZ-Textfragen beantworten");
        final JCheckBox optionMath = new JCheckBox("KZ-Mathefragen beantworten");
        JCheckBox jcheckbox = new JCheckBox("10% Regel einhalten");
        final JCheckBox optionSillyboy = new JCheckBox("KZ-S.M.P");
        optionLearn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent actionevent)
            {
                learnQuestions = optionLearn.isSelected();
            }

            final JCheckBox val$optionLearn;
            final QuizModul this$0;

            
            {
                this$0 = QuizModul.this;
                optionLearn = jcheckbox;
                super();
            }
        });
        optionAnswer.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent actionevent)
            {
                answerQuestions = optionAnswer.isSelected();
            }

            final JCheckBox val$optionAnswer;
            final QuizModul this$0;

            
            {
                this$0 = QuizModul.this;
                optionAnswer = jcheckbox;
                super();
            }
        });
        optionMath.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent actionevent)
            {
                answerMath = optionMath.isSelected();
            }

            final JCheckBox val$optionMath;
            final QuizModul this$0;

            
            {
                this$0 = QuizModul.this;
                optionMath = jcheckbox;
                super();
            }
        });
        jcheckbox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent actionevent)
            {
                usePercentRule = optionAnswer.isSelected();
            }

            final JCheckBox val$optionAnswer;
            final QuizModul this$0;

            
            {
                this$0 = QuizModul.this;
                optionAnswer = jcheckbox;
                super();
            }
        });
        optionSillyboy.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent actionevent)
            {
                answerSillyboy = optionSillyboy.isSelected();
            }

            final JCheckBox val$optionSillyboy;
            final QuizModul this$0;

            
            {
                this$0 = QuizModul.this;
                optionSillyboy = jcheckbox;
                super();
            }
        });
        jpanel3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "KZ-Optionen:"));
        jpanel3.setLayout(new GridLayout(3, 2));
        jpanel3.add(optionLearn);
        jpanel3.add(optionAnswer);
        jpanel3.add(optionMath);
        jpanel3.add(optionSillyboy);
        JPanel jpanel4 = new JPanel();
        JButton jbutton2 = new JButton("DB-Update");
        final JLabel questionCountLabel = new JLabel("DB Fragen: Keine");
        jbutton2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent actionevent)
            {
                questionCountLabel.setText((new StringBuilder()).append("DB-ONline: ").append(getDatacount(scriptField.getText())).toString());
            }

            final JLabel val$questionCountLabel;
            final QuizModul this$0;

            
            {
                this$0 = QuizModul.this;
                questionCountLabel = jlabel;
                super();
            }
        });
        jpanel4.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "DB-statistik:"));
        jpanel4.setLayout(new BorderLayout());
        jpanel4.add(questionCountLabel, "Center");
        jpanel4.add(jbutton2, "South");
        mainPanel.setLayout(new BorderLayout());
        JPanel jpanel5 = new JPanel();
        jpanel5.setLayout(new BorderLayout());
        jpanel5.add(jpanel1, "North");
        jpanel5.add(jpanel4, "Center");
        JPanel jpanel6 = new JPanel();
        jpanel6.setLayout(new BorderLayout());
        jpanel6.add(jpanel2, "South");
        jpanel6.add(jpanel3, "North");
        JPanel jpanel7 = new JPanel();
        jpanel7.setLayout(new GridLayout(2, 1, 5, 10));
        jpanel7.add(jpanel6);
        jpanel7.add(jpanel5);
        mainPanel.add(jpanel, "North");
        mainPanel.add(jpanel7, "Center");
    }

    public void kill()
    {
    }

    public void stringRecieved(Network network1, String s)
    {
        StringTokenizer stringtokenizer = new StringTokenizer(s, "\0");
        String s1 = stringtokenizer.nextToken();
        if(s1.equals("(") && !loggedIn)
        {
            String s2 = stringtokenizer.nextToken();
            handler.message(">>> Sende Logindaten...");
            network1.sendString((new StringBuilder()).append("n\0").append(channel).append("\0").append(nickname).append("\0").append(Password.getPasswd(passwort, s2)).toString(), 0);
        }
        if(s1.equals("a") && !loggedIn)
        {
            handler.message(">>> Login OK! =)");
            loggedIn = true;
            network1.sendString((new StringBuilder()).append("a\0").append(channel).toString(), 0);
        }
        if(s1.equals("k") && !loggedIn)
        {
            handler.message(">>> Fehler beim Login aufgetreten!");
            network1.disconnect();
        }
        if(s1.equals("4"))
        {
            String s3 = stringtokenizer.nextToken();
            StringTokenizer stringtokenizer1 = new StringTokenizer(s3, "|");
            String s6 = stringtokenizer1.nextToken();
            handler.message((new StringBuilder()).append(">>> ").append(s6).append(" pingt dich! =(").toString());
            network1.sendString((new StringBuilder()).append("h\0").append(s3).toString(), 0);
        }
        if(s1.equals("e"))
        {
            String s4 = stringtokenizer.nextToken();
            String s5 = stringtokenizer.nextToken();
            String s7 = stringtokenizer.nextToken();
            if(s4.equalsIgnoreCase("James"))
            {
                if(s7.indexOf("Es ist soweit,") != -1)
                {
                    handler.message(">>> Anfang einer Quizrunde.");
                    network1.sendString((new StringBuilder()).append("e\0").append(channel).append("\000/ok").toString(), 0);
                    handler.message(">>> Nickname angemeldet!");
                }
                if((s7.startsWith("Genau _") || s7.startsWith("Zeit vorbei") || s7.startsWith("Die Zeit")) && (learnQuestions || answerSillyboy))
                {
                    handler.message(">>> Antwort erkannt, versuche in die KZ-DB zu schreiben.");
                    String s8 = s7.substring(s7.indexOf("\260R18\260_") + 6, s7.indexOf("_\260r\260"));
                    if(answerSillyboy)
                        network1.sendString((new StringBuilder()).append("e\0").append(channel).append("\0").append(s8).toString(), 0);
                    if(learnQuestions)
                        addAnswer(currentQuestion, s8, scriptField.getText());
                }
                if(s7.startsWith("\260B18\260"))
                {
                    s7 = s7.replaceFirst("\260B18\260_", "");
                    currentQuestion = s7.substring(0, s7.indexOf("_\260r\260"));
                    if(answerQuestions)
                    {
                        String s9 = getAnswer(currentQuestion, scriptField.getText());
                        handler.message((new StringBuilder()).append("Quiz> Suche  in der KZ-DB => ").append(s9).toString());
                        if(!s9.equals("null"))
                        {
                            float f = 0.0F;
                            f = (float)s9.length() * answerDelayPerChar;
                            if(f < (float)answerDelay)
                                f = answerDelay;
                            new DelayedMessage(network1, (new StringBuilder()).append("e\0").append(channel).append("\0").append(s9.toLowerCase()).toString(), f);
                        }
                    }
                    if(currentQuestion.startsWith("Nennen Sie die gesuchte Zahl: ") && answerMath)
                    {
                        handler.message(">>> Rechne Mathefrage...");
                        currentQuestion = currentQuestion.replaceFirst("Nennen Sie die gesuchte Zahl: ", "");
                        int i = 0;
                        StringTokenizer stringtokenizer2 = new StringTokenizer(currentQuestion);
                        String s10 = "";
                        int j;
                        try
                        {
                            j = Integer.parseInt(stringtokenizer2.nextToken());
                        }
                        catch(Exception exception)
                        {
                            j = 0;
                        }
                        s10 = stringtokenizer2.nextToken();
                        int k;
                        try
                        {
                            k = Integer.parseInt(stringtokenizer2.nextToken());
                        }
                        catch(Exception exception1)
                        {
                            k = 0;
                        }
                        stringtokenizer2.nextToken();
                        int l;
                        try
                        {
                            l = Integer.parseInt(stringtokenizer2.nextToken());
                        }
                        catch(Exception exception2)
                        {
                            l = 0;
                        }
                        if(s10.equals("plus"))
                        {
                            if(j == 0)
                                i = l - k;
                            if(k == 0)
                                i = l - j;
                            if(l == 0)
                                i = j + k;
                        }
                        if(s10.equals("minus"))
                        {
                            if(j == 0)
                                i = l + k;
                            if(k == 0)
                                i = j - l;
                            if(l == 0)
                                i = j - k;
                        }
                        if(s10.equals("mal"))
                        {
                            if(j == 0)
                                i = l / k;
                            if(k == 0)
                                i = l / j;
                            if(l == 0)
                                i = j * k;
                        }
                        if(s10.equals("durch"))
                        {
                            if(j == 0)
                                i = l * k;
                            if(k == 0)
                                i = j / l;
                            if(l == 0)
                                i = j / k;
                        }
                        handler.message((new StringBuilder()).append(">>> Mathefrage ausgerechnet! (").append(i).append(")").toString());
                        new DelayedMessage(network1, (new StringBuilder()).append("e\0").append(channel).append("\0").append(i).toString(), answerDelay);
                    }
                }
            }
        }
    }

    public void connectionLost(Network network1)
    {
        if(network != null)
        {
            handler.message(">>> Verbindung zu KZ-Chat verloren! =(");
            network = null;
            loggedIn = false;
        }
    }

    public void stringSend(Network network1, String s)
    {
    }

    public String getDatacount(String s)
    {
        String s1;
        URL url = new URL((new StringBuilder()).append(s).append("?action=count").toString());
        BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream()));
        s1 = bufferedreader.readLine();
        if(s1 == null || !s1.equals("<!--Gesperrt-->"))
            handler.message(">>> Fehler beim  der KZ-DB (Falsche Quelle?)");
        s1 = bufferedreader.readLine();
        if(s1 != null)
            return s1;
        break MISSING_BLOCK_LABEL_110;
        Throwable throwable;
        throwable;
        handler.message(">>> Fehler beim  der KZ-DB (Falsche Quelle?)");
        return "-1";
    }

    public String getAnswer(String s, String s1)
    {
        String s2;
        URL url = new URL((new StringBuilder()).append(s1).append("?action=search&q=").append(URLEncoder.encode(s)).toString());
        BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream()));
        s2 = bufferedreader.readLine();
        if(s2 == null || !s2.equals("<!--Gesperrt-->"))
            handler.message(">>> Fehler beim  der KZ-DB (Falsche Quelle?)");
        s2 = bufferedreader.readLine();
        if(s2 != null && !s2.equals(""))
            return s2;
        break MISSING_BLOCK_LABEL_130;
        Throwable throwable;
        throwable;
        handler.message(">>> Fehler beim  der KZ-DB (Falsche Quelle?)");
        return "null";
    }

    public void addAnswer(String s, String s1, String s2)
    {
        try
        {
            URL url = new URL((new StringBuilder()).append(s2).append("?action=add&q=").append(URLEncoder.encode(s)).append("&a=").append(URLEncoder.encode(s1)).toString());
            BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream()));
            String s3 = bufferedreader.readLine();
            if(s3 == null || !s3.equals("<!--Gesperrt-->"))
                handler.message(">>> Fehler beim  der KZ-DB (Falsche Quelle?)");
        }
        catch(Throwable throwable)
        {
            handler.message(">>> Fehler beim  der KZ-DB (Falsche Quelle?)");
        }
    }

    private ModuleHandler handler;
    private JPanel mainPanel;
    private boolean loggedIn;
    private String nickname;
    private String passwort;
    private String channel;
    private final JTextField scriptField = new JTextField("http://profiurl.pr.ohost.de/index.php");
    private boolean learnQuestions;
    private boolean answerQuestions;
    private boolean answerMath;
    private boolean usePercentRule;
    private boolean answerSillyboy;
    private int answerDelay;
    private float answerDelayPerChar;
    private Network network;
    private int currentRound;
    private int questionCount;
    private int rightAnswers;
    private String currentQuestion;















}