Guest User

Untitled

a guest
Jul 2nd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 32.17 KB | None | 0 0
  1. /*
  2. (c)2009 xprog.ch technologies. All rights reserved.
  3. Created by Nicolas Hafner(Root)
  4.  
  5.  
  6. boolean name(String a){
  7.         try{
  8.        
  9.        
  10.         }catch(Exception e){debWr(e.toString()+" @ "+debpoint);return(false);}
  11.         return(true);
  12.     }
  13.  
  14. */
  15. import java.io.*;
  16. import java.util.*;
  17. import java.util.zip.*;
  18. import javax.swing.*;
  19. import java.awt.*;
  20. import java.awt.event.*;
  21. import java.sql.*;
  22. import org.apache.commons.net.ftp.*;
  23.  
  24.  
  25. public class Voc extends JFrame{
  26.     //window constants
  27.     static int h = 600;static int w = 400;
  28.     static int dh = 400;static int dw = 400;
  29.     static int ih = 100;static int iw = 400;
  30.     static int eh = 600;static int ew = 500;
  31.     //pw's and greeting variables
  32.     int stage = 0; //stages: 0-program start 1-questioning 2-result 3-stats 4-read file 5-debug 6-sql read 7-sql write 8-voc write 9-sql get 10-admin mode active 11-drop table 12-register 13-login 14-write file 15-update
  33.     boolean admin = false;
  34.     boolean loggedIn = false;
  35.     boolean debug;
  36.     int userid = -1;
  37.     String username = "";
  38.     String userpass = "";
  39.     String usermail = "";
  40.     String SQLuser = "amaterasu";
  41.     String SQLpw = "access";
  42.     String FTPuser = "web40f7";
  43.     String FTPpw = "access";
  44.     String url ="jdbc:mysql://db4free.net:3306/dict";
  45.     String host = "login-48.hoststar.ch";
  46.     int port = 21;
  47.     static String version = "3.10";
  48.     static String subversion = "900";
  49.     String welcome = "Welcome to Voc v."+version+" build "+subversion+" at "+deb.d.toString()+
  50.     "\nStandard commands:"+
  51.     "\n------------------------------------------------------------------"+
  52.     "\nedit         create/edit your own dictionarys"+
  53.     "\n"+
  54.     "\nread         reads in a file."+
  55.     "\nwrVoc        writes to a voc and erg file."+
  56.     "\n"+
  57.     "\nsql          reads from the xprog SQL database."+
  58.     "\ngetSql       gets the available dictionarys."+
  59.     "\nwrSql        writes to the xprog SQL database."+
  60.     "\ndropSql      deletes a dictionary. You can only delete your own dictionarys."+
  61.     "\n"+
  62.     "\nstart        starts questioning."+
  63.     "\nstop         stops questioning."+
  64.     "\n"+
  65.     "\nupdate       checks for updates on the xprog server."+
  66.     "\nusers        shows all registred users."+
  67.     "\nregister     register an account to upload your own dictionarys."+
  68.     "\nlogin        log in to upload your own dictionarys."+
  69.     "\n"+
  70.     "\nadmin        Enables admin privilegies. You have to enter a PW."+
  71.     "\ninfo         shows the stats."+
  72.     "\ndebug        shows the debug console and activates debug mode."+
  73.     "\ndict.info        shows info to the dictionary files in the debug console. Debug mode has to be active."+
  74.     "\ndict.print       shows the whole dictionary in the debug console. Debug mode hs to be active."+
  75.     "\n------------------------------------------------------------------"+
  76.     "\n";
  77.    
  78.     //GUI objects
  79.     TimeChk chk = new TimeChk();
  80.     editVoc edit;
  81.     static Info inf;
  82.     static Debug deb;
  83.     JButton set;
  84.     JTextArea out;
  85.     JTextField in;
  86.     //update stuff
  87.     boolean updatebeta = false;
  88.     boolean updateable = false;
  89.     double updatevers = Double.parseDouble(version);
  90.     int updatesubv = Integer.parseInt(subversion);
  91.     int updateprio = 0;
  92.     String updatedesc = "current version";
  93.     //Dictionary stuff
  94.     int trys = 0;
  95.     int debpoint = -1;
  96.     String bk = "";
  97.     String lect = "";
  98.     File voc;
  99.     File erg;
  100.     HashMap<Integer,String> dict = new HashMap<Integer,String>(5000,5000);
  101.     HashMap<Integer,String> sol = new HashMap<Integer,String>(5000,5000);
  102.     Vector<Integer> randoms = new Vector<Integer>(5000);
  103.     int counter = 0;
  104.    
  105.     //Start action listeners
  106.     class lst implements ActionListener{
  107.         @SuppressWarnings({"deprecation", "deprecation"})
  108.         public void actionPerformed(ActionEvent e){
  109.             if(e.getActionCommand().equalsIgnoreCase("Go")){
  110.                 try{
  111.                 //updates
  112.                 if(stage==15){
  113.                     if(updateSql()){
  114.                         if(updateable){
  115.                             String nachricht="Update available!\ncurrent version:"+version+"."+subversion+"\n\nnew version: \n"+"v.  "+updatevers+"."+updatesubv+"\nbeta:  "+updatebeta+"\npriority:  "+updateprio+"\n\ndescription/changelog:\n"+updatedesc+"\n\n\n Do you want to update?";
  116.                             if(JOptionPane.showConfirmDialog(null,nachricht, "Updatemanager", JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE)==JOptionPane.YES_OPTION){
  117.                                 if(updateFTP())
  118.                                     outWr("Successfully updated. Please restart application.");
  119.                                 else
  120.                                     outWr("Whoops! Something went wrong! Check the debug console!\n");
  121.                             }
  122.                         }else
  123.                         JOptionPane.showMessageDialog(null,"No update available.","Updatemanager",JOptionPane.INFORMATION_MESSAGE);
  124.                     }else
  125.                         outWr("Whoops! Something went wrong! Check the debug console!\n");
  126.                     stage=0;
  127.                 }
  128.                 //stats
  129.                 if(stage==3){
  130.                     inf.setVisible(false);
  131.                     inf.counter.suspend();
  132.                     inf.timer.suspend();
  133.                     outWr("\n\n\nFinished!\nTime needed: "+inf.counter.timem+":"+inf.counter.times+":"+inf.counter.timets);
  134.                     if(inf.score.getValue()>0){
  135.                     double temp = inf.score.getValue()*100/inf.score.getMaximum();
  136.                     outWr("Percentage of correct words: "+temp+"%\n");
  137.                     }else
  138.                     outWr("You didn't do anything right! Shame on you!\n");
  139.                     stage=0;
  140.                 }
  141.                 //debug
  142.                 if(stage==5||debug){
  143.                     debpoint=44;
  144.                     if(in.getText().equalsIgnoreCase("dict.info")){debWr("voc :"+voc.getAbsolutePath()+"@"+voc.length()+"bytes"+"\nerg :"+erg.getAbsolutePath()+"@"+erg.length()+"bytes\nsize: "+dict.size());in.setText("");}
  145.                     if(in.getText().equalsIgnoreCase("dict.print")){for(int i =0;i<dict.size();i++){debWr(dict.get(i)+"\t\t"+sol.get(i));}}
  146.                     debpoint=46;
  147.                 }
  148.                 //readFile
  149.                 if(stage==4){
  150.                     debpoint=50;
  151.                     if(readIn(in.getText()))
  152.                         outWr("Succeeded!\n");
  153.                     else
  154.                         outWr("Whoops! Something went wrong! Check the debug console!\n");
  155.                     in.setText("");
  156.                     stage=0;
  157.                     debpoint=57;
  158.                 }
  159.                 //writeFile
  160.                 if(stage==14){
  161.                     debpoint=122;
  162.                     if(saveOut(in.getText()))
  163.                         outWr("Succeeded!\n");
  164.                     else
  165.                         outWr("Whoops! Something went wrong! Check the debug console!\n");
  166.                     in.setText("");
  167.                     stage=0;
  168.                     debpoint=128;
  169.                 }
  170.                 //read SQL database
  171.                 if(stage==6){
  172.                     debpoint=96;
  173.                     if(readSql(in.getText()))
  174.                         outWr("Succeeded!\n");
  175.                     else
  176.                         outWr("Whoops! Something went wrong! Check the debug console!\n");
  177.                     stage=0;   
  178.                    
  179.                     in.setText("");
  180.                     debpoint=103;
  181.                 }
  182.                 //write SQL database
  183.                 if(stage==7){
  184.                     debpoint=109;
  185.                     if(!bk.equals("")&&!lect.equals("")){
  186.                         if(createSql(in.getText(),bk,lect))
  187.                             outWr("Succeeded!\n");
  188.                         else
  189.                             outWr("Whoops! Something went wrong! Check the debug console!\n");
  190.                         stage=0;   
  191.                         }
  192.                    
  193.                     if(!bk.equals("")&&lect.equals("")){
  194.                         lect=in.getText();
  195.                         outWr("Type in the name of the table.\nThis has nothing to do with the Dictionary.\nPlease use an understandable name.");
  196.                     }
  197.                     if(bk.equals("")&&lect.equals("")){
  198.                         bk=in.getText();
  199.                         outWr("Type in the lection numbers, wich this dict includes.");
  200.                     }
  201.                    
  202.                     in.setText("");
  203.                     debpoint=116;
  204.                 }
  205.                 //get Sql table names
  206.                 if(stage==9){
  207.                     debpoint=121;
  208.                     if(getSql())
  209.                         outWr("Succeeded!\n");
  210.                     else
  211.                         outWr("Whoops! Something went wrong! Check the debug console!\n");
  212.                     in.setText("");
  213.                     stage=0;
  214.                 }
  215.                 //delete Sql table
  216.                 if(stage==11){
  217.                     debpoint=132;
  218.                     if(dropSql(in.getText()))
  219.                         outWr("Succeeded!\n");
  220.                     else
  221.                         outWr("Whoops! Something went wrong! Check the debug console!\n");
  222.                     in.setText("");
  223.                     stage=0;
  224.                 }
  225.                 //register
  226.                 if(stage==12){
  227.                     if(!username.equals("")&&!userpass.equals("")&&usermail.equals("")){
  228.                         usermail=in.getText();in.setText("");outWr("Thank you!\nConnecting to server...");
  229.                         if(registerSql())
  230.                             outWr("Account created!\nYou may now log in!\n");
  231.                         else
  232.                             outWr("Whoops! Something went wrong! Check the debug console!\n");
  233.                     stage=0;}
  234.                    
  235.                     if(!username.equals("")&&userpass.equals("")){
  236.                         userpass=in.getText();in.setText("");outWr("Please enter e-mail adress (optional)");}
  237.                    
  238.                     if(username.equals("")){
  239.                         username=in.getText();in.setText("");outWr("Please enter desired password");}
  240.                 }
  241.                 //log in
  242.                 if(stage==13){
  243.                     debpoint=246;
  244.                     if(!username.equals("")&&!userpass.equals("")){
  245.                         in.setText("");
  246.                         if(verifySql())
  247.                             outWr("Logged in!\nYou may now upload dictionarys!\n");
  248.                         else
  249.                             outWr("Whoops! Something went wrong! Check the debug console!\n");
  250.                         stage=0;}
  251.                     debpoint=254;
  252.                     if(!username.equals("")&&userpass.equals("")){
  253.                         userpass=in.getText();set.doClick();}
  254.                     debpoint=257;
  255.                     if(username.equals("")){
  256.                         username=in.getText();in.setText("");outWr("Please enter password");}
  257.                 }
  258.                 //admin privilegies
  259.                 if(stage==10){
  260.                     if(in.getText().equalsIgnoreCase("XprogOdbc1")){outWr("Admin mode active!\n");in.setText("");admin=true;}else{outWr("Wrong PW. Sorry.\n");in.setText("");stage=0;}
  261.                 }
  262.                 //correct
  263.                 if(stage==2){
  264.                     debpoint=69;
  265.                     if(in.getText().equalsIgnoreCase(sol.get(randoms.get(counter)))){
  266.                         outWr("That's right!");
  267.                         inf.score.setValue(inf.score.getValue()+1);
  268.                         trys=0;
  269.                         if(counter<dict.size()){
  270.                             counter++;stage=1;
  271.                         }else{
  272.                             stage=3;set.doClick();}
  273.                     }else{
  274.                         if(trys<3){
  275.                             outWr("No. try again.");
  276.                             trys++;
  277.                         }else{
  278.                             trys=0;
  279.                             outWr("No. It's "+sol.get(randoms.get(counter)));
  280.                             if(counter<dict.size()){
  281.                                 counter++;stage=1;
  282.                             }else{
  283.                                 stage=3;set.doClick();}
  284.                         }
  285.                     }
  286.                     inf.score.setMaximum(counter);
  287.                     inf.scrl.setText(inf.score.getValue()+"/"+counter);
  288.                     inf.overall.setValue(counter);
  289.                     inf.ovrl.setText(counter+"/"+dict.size());
  290.                    
  291.                 }
  292.                 //ask
  293.                 if(stage==1){
  294.                     inf.counter.resume();
  295.                     inf.timer.resume();
  296.                     debpoint=117;
  297.                     inf.timer.timets=inf.timer.times*10+inf.timer.timem*600;
  298.                     debpoint=119;
  299.                     debpoint=61;
  300.                     outWr(dict.get(randoms.get(counter))+" ?");
  301.                     in.setText("");
  302.                     stage=2;
  303.                 }
  304.                 //commandline
  305.                 if(stage==0||stage==3||stage==4||stage==5){
  306.                     //edit the vocabulary
  307.                     if(in.getText().equalsIgnoreCase("edit")){outWr("Change/Create dictionary.");in.setText("");
  308.                         counter=0;String s="";String d="";String[] data;
  309.                         //send data
  310.                         while(counter<dict.size()){d=d+"\n"+dict.get(counter);s=s+"\n"+sol.get(counter);counter++;}
  311.                         edit.setData(d,s);
  312.                         edit.setVisible(true);
  313.                         data=edit.getData();
  314.                         counter=0;
  315.                         dict.clear();sol.clear();
  316.                         //save data
  317.                         StringTokenizer st = new StringTokenizer(data[0]);
  318.                         StringTokenizer s2 = new StringTokenizer(data[1]);
  319.                         while(st.hasMoreTokens()){
  320.                             dict.put(counter,st.nextToken("\n"));
  321.                             sol.put(counter,s2.nextToken("\n"));
  322.                             counter++;
  323.                         }
  324.                         randomSet();
  325.                     }
  326.                     //jokes ;)
  327.                     if(in.getText().equalsIgnoreCase("dini mami")){outWr("Figg dich!");in.setText("");}
  328.                     if(in.getText().equalsIgnoreCase("fuck you")){outWr("fuck yourself in your ass!");in.setText("");}
  329.                     if(in.getText().equalsIgnoreCase("word")){outWr("woooooord!\nword!\nwooord!!!!111!");in.setText("");}
  330.                     if(in.getText().equalsIgnoreCase("your mother")){outWr("your mother is so fat, you turn around and she's still there!");in.setText("");}
  331.                     if(in.getText().equalsIgnoreCase("fuck")){outWr("STFU!");in.setText("");}
  332.                     if(in.getText().equalsIgnoreCase("scheisse")){outWr("Ja genau, du bisch scheisse!");in.setText("");}
  333.                     if(in.getText().equalsIgnoreCase("pensi")){outWr("GNIHIHIHIHI!");in.setText("");}
  334.                     if(in.getText().equalsIgnoreCase("penis")){outWr("gnihihihihi!");in.setText("");}
  335.                     if(in.getText().equalsIgnoreCase("lol")){outWr("lawl");in.setText("");}
  336.                     if(in.getText().equalsIgnoreCase("lawl")){outWr("rofl");in.setText("");}
  337.                     if(in.getText().equalsIgnoreCase("rofl")){outWr("lmao");in.setText("");}
  338.                     if(in.getText().equalsIgnoreCase("lmao")){outWr("roflmao");in.setText("");}
  339.                     if(in.getText().equalsIgnoreCase("roflmao")){outWr("lolroflyfao");in.setText("");}
  340.                     if(in.getText().equalsIgnoreCase("lolroflyfao")){outWr("Aha?");in.setText("");}
  341.                     if(in.getText().equalsIgnoreCase("gtg")){outWr("Ok... cu");in.setText("");}
  342.                     if(in.getText().equalsIgnoreCase("cu")){outWr("bye");in.setText("");}
  343.                     if(in.getText().equalsIgnoreCase("bye")){outWr("hehe bye!");in.setText("");}
  344.                     if(in.getText().equalsIgnoreCase("stfu")){outWr("I can't even talk, so how should i shut up?\nBesides, you're the one who should shut the fuck up!");in.setText("");}
  345.                     if(in.getText().equalsIgnoreCase("...")){outWr("...");in.setText("");}
  346.                     if(in.getText().equalsIgnoreCase("wtf")){outWr("Yes, what the fuck are you doing?");in.setText("");}
  347.                     if(in.getText().equalsIgnoreCase("omg")){outWr("O my god, you're such a loser!");in.setText("");}
  348.                     if(in.getText().equalsIgnoreCase("omfg")){outWr("O my fucking god, you're the biggest loser i've ever seen!");in.setText("");}
  349.                     if(in.getText().equalsIgnoreCase("xprog.ch")){outWr("The best website in the whole 42!");in.setText("");}
  350.                     if(in.getText().equalsIgnoreCase("the answer to live, the universe and everything")){outWr("42");in.setText("");}
  351.                     if(in.getText().equalsIgnoreCase("42")){outWr("is the answer to live, the universe and everything.");in.setText("");}
  352.                     if(in.getText().equalsIgnoreCase("google")){outWr("A good searchbot.");in.setText("");}
  353.                     if(in.getText().equalsIgnoreCase("onipepper")){outWr("One of the best videogame blogs.");in.setText("");}
  354.                     if(in.getText().equalsIgnoreCase("game one")){outWr("One of the best videogame tv shows.");in.setText("");}
  355.                     if(in.getText().equalsIgnoreCase("i can has cheezburger?")){outWr("No u canz has cheezburgerz!");in.setText("");}
  356.                     if(in.getText().equalsIgnoreCase("!!!!")){outWr("einselfeins");in.setText("");}
  357.                     if(in.getText().equalsIgnoreCase("1111")){outWr("!!!!");in.setText("");}
  358.                     if(in.getText().equalsIgnoreCase("i hate mondays!")){outWr("hi garfield!");in.setText("");}
  359.                     //real commands
  360.                     if(in.getText().equalsIgnoreCase("read")){outWr("Reading a dictionary.\nType in the filename.");in.setText("");stage=4;}
  361.                     if(in.getText().equalsIgnoreCase("wrVoc")){outWr("Saving a dictionary.\nType in the filename.");in.setText("");stage=14;}
  362.                     //sql commands
  363.                     if(in.getText().equalsIgnoreCase("sql")){outWr("Reading SQL database.\nType in the table name.");in.setText("");stage=6;}
  364.                     if(in.getText().equalsIgnoreCase("wrSql")&&loggedIn){outWr("Saving SQL database.\nType in the name of the book containing the words.");in.setText("");stage=7;String bk = "";String lect = "";}
  365.                     if(in.getText().equalsIgnoreCase("getSql")){outWr("Getting available dictionarys...");in.setText("");stage=9;set.doClick();}
  366.                     if(in.getText().equalsIgnoreCase("dropSql")&&(admin||loggedIn)){outWr("Dropping a table.\nType in the table name.");in.setText("");stage=11;}
  367.                     //user commands
  368.                     if(in.getText().equalsIgnoreCase("update")){outWr("Checking for updates.");in.setText("");stage=15;set.doClick();}
  369.                     if(in.getText().equalsIgnoreCase("register")&&!loggedIn){outWr("Registring.\nType in your desired username.");in.setText("");stage=12;}
  370.                     if(in.getText().equalsIgnoreCase("login")&&!loggedIn){outWr("Logging in.\nType in your username.");in.setText("");stage=13;username="";userpass="";usermail="";}
  371.                     if(in.getText().equalsIgnoreCase("users")){outWr("Registred users:\n\n");in.setText("");usersSql();outWr("\n");}
  372.                     //special commands
  373.                     if(in.getText().equalsIgnoreCase("admin")){outWr("Entering admin mode.\nType in the admin PW.");in.setText("");stage=10;}
  374.                     if(in.getText().equalsIgnoreCase("debug")){deb.setVisible(true);stage=5;debug=true;outWr("Entering debug mode");in.setText("");}
  375.                     if(in.getText().equalsIgnoreCase("info")){inf.setVisible(true);in.setText("");}
  376.                     if(in.getText().equalsIgnoreCase("start")){trys=0;counter=0;inf.counter.timem=0;inf.counter.times=0;inf.counter.timets=0;inf.setVisible(true);randomSet();stage=1;outWr("When you're ready, press GO");in.setText("");}
  377.                 }
  378.                 //abort questioning
  379.                 if(in.getText().equalsIgnoreCase("stop")){stage=0;in.setText("");outWr("Aborted by user action");inf.setVisible(false);inf.counter.suspend();inf.timer.suspend();}
  380.                 }catch(Exception f){debWr(f.toString()+" @ "+debpoint);}
  381.             }
  382.         }
  383.     }
  384.     class keys implements KeyListener{
  385.         public void keyTyped(KeyEvent e){}
  386.         public void keyPressed(KeyEvent e){
  387.             if(e.getKeyCode()==KeyEvent.VK_ENTER)
  388.                 set.doClick();}
  389.         public void keyReleased(KeyEvent e){}
  390.     }
  391.     //End action listeners
  392.    
  393.     //Start local functions
  394.     boolean readIn(String name){
  395.         debpoint=105;
  396.         //try to read files
  397.         try{
  398.         voc = new File(name+".voc");
  399.         erg = new File(name+".erg");
  400.         //do these files exist?
  401.         if(!voc.isFile()&&!erg.isFile()){
  402.             debWr("Sorry, the file doesn't exists. @"+debpoint);return(false);}
  403.         //read in
  404.         debpoint=114;
  405.         String puffer;counter=0;
  406.         FileReader leser = new FileReader(voc);
  407.         BufferedReader in = new BufferedReader(leser);
  408.         FileReader leser2 = new FileReader(erg);
  409.         BufferedReader in2 = new BufferedReader(leser2);
  410.         while((puffer=in.readLine())!=null){
  411.             debpoint=121;
  412.             dict.put(counter,puffer);
  413.             sol.put(counter,in2.readLine());
  414.             counter++;
  415.         }
  416.         randomSet();
  417.         debWr("Files read in successful. "+counter+" lines per file.");
  418.         leser.close();leser2.close();
  419.         debpoint=127;
  420.         //set Maximum of progress bar
  421.         inf.overall.setMaximum(counter);
  422.         debpoint=143;
  423.         }catch(Exception e){debWr(e.toString()+" @"+debpoint);return(false);}
  424.         return(true);
  425.     }
  426.     boolean saveOut(String name){
  427.         try{
  428.         debpoint=324;
  429.         FileWriter fw = new FileWriter(name+".voc");
  430.         PrintWriter pw = new PrintWriter(fw);
  431.         FileWriter f2 = new FileWriter(name+".erg");
  432.         PrintWriter p2 = new PrintWriter(f2);
  433.         debpoint=329;
  434.         counter=0;
  435.         while(counter<dict.size()){
  436.             pw.println(dict.get(counter));
  437.             p2.println(sol.get(counter));
  438.             counter++;
  439.         }
  440.         debpoint=335;
  441.         pw.close();
  442.         p2.close();
  443.         }catch(Exception e){debWr(e.toString()+" @ "+debpoint);return(false);}
  444.         return(true);
  445.     }
  446.     //End local functions
  447.    
  448.     //Start writing functions
  449.     void outWr(String in){
  450.         out.setText(out.getText()+"\n"+in);
  451.     }
  452.     void debWr(String in){
  453.         deb.output.setText(deb.output.getText()+"\n"+in);
  454.     }
  455.     //End writing functions
  456.    
  457.     //Start MySQL functions
  458.     boolean getSql(){
  459.         try{
  460.         debpoint=253;
  461.        
  462.         ResultSet rs;
  463.         Statement stmt;
  464.        
  465.         Connection con = DriverManager.getConnection(url,SQLuser, SQLpw);
  466.         stmt = con.createStatement();
  467.        
  468.         rs = stmt.executeQuery("SELECT * FROM dicts");
  469.  
  470.         debpoint=254;
  471.         outWr("\n--------------------------------------"+
  472.               "\n|name\tbook\tlections\t|" +
  473.               "\n--------------------------------------");
  474.         while(rs.next()){
  475.             outWr("|"+rs.getString("name")+"\t"+rs.getString("book")+"\t"+rs.getString("lections")+"\t|");
  476.             debpoint=256;
  477.         }
  478.         outWr("--------------------------------------");
  479.  
  480.         con.close();
  481.         }catch(Exception e){debWr(e.toString()+" @ "+debpoint);return(false);}
  482.         return(true);
  483.     }
  484.     boolean readSql(String table){
  485.         try{
  486.         debpoint=221;
  487.        
  488.         Statement stmt;
  489.         ResultSet rs;
  490.        
  491.         Connection con = DriverManager.getConnection(url,SQLuser, SQLpw);
  492.        
  493.         debpoint=232;
  494.         stmt = con.createStatement();
  495.        
  496.         //get the data
  497.         rs = stmt.executeQuery("SELECT * " +"from "+table);
  498.        
  499.         counter=0;
  500.         while(rs.next()){
  501.             debpoint=241;
  502.             dict.put(counter,rs.getString("q"));
  503.             sol.put(counter,rs.getString("a"));
  504.             counter++;
  505.         }
  506.         outWr("Dictionary created by: "+rs.getString("name"));
  507.        
  508.         randomSet();
  509.        
  510.         debpoint=242;
  511.         con.close();
  512.        
  513.         }catch(Exception e){debWr(e.toString()+" @ "+debpoint);return(false);}
  514.         return(true);
  515.     }
  516.     boolean createSql(String table,String book,String lection){
  517.         try{
  518.         debpoint=243;
  519.        
  520.         Statement stmt;
  521.         ResultSet rs;
  522.        
  523.         Connection con = DriverManager.getConnection(url,SQLuser, SQLpw);
  524.        
  525.         debpoint=232;
  526.        
  527.         stmt = con.createStatement();
  528.        
  529.         //create table
  530.         stmt.executeUpdate("CREATE TABLE "+table+"(version TEXT,q TEXT,a TEXT,name TEXT)");
  531.  
  532.         //update table list.
  533.         PreparedStatement pstmt = con.prepareStatement("INSERT INTO dicts VALUES(?,?,?)");
  534.         pstmt.setString(1,table);
  535.         pstmt.setString(2,book);
  536.         pstmt.setString(3,lection);
  537.         pstmt.executeUpdate();
  538.  
  539.         //save voc data
  540.         counter=0;
  541.         while(counter<dict.size()){
  542.             pstmt = con.prepareStatement("INSERT INTO "+table+" VALUES(?,?,?,?)");
  543.             pstmt.setString(1,version);
  544.             pstmt.setString(2,dict.get(counter));
  545.             pstmt.setString(3,sol.get(counter));
  546.             pstmt.setString(4,username);
  547.             pstmt.executeUpdate();
  548.             counter++;
  549.         }
  550.         //update user data
  551.         rs = stmt.executeQuery("SELECT * FROM users WHERE users.name = '"+username+"'");
  552.         while(rs.next()){
  553.             pstmt = con.prepareStatement("UPDATE users SET dicts = ?");
  554.             pstmt.setInt(1,rs.getInt("dicts")+1);
  555.         }
  556.  
  557.         bk = "";
  558.         lect="";
  559.  
  560.         con.close();
  561.         }catch(Exception e){debWr(e.toString()+" @ "+debpoint);return(false);}
  562.         return(true);
  563.     }
  564.     boolean dropSql(String table){
  565.         try{
  566.         debpoint=388;
  567.        
  568.         Statement stmt;
  569.         ResultSet rs;
  570.        
  571.         Connection con = DriverManager.getConnection(url,SQLuser, SQLpw);
  572.        
  573.         debpoint=232;
  574.        
  575.         stmt = con.createStatement();
  576.        
  577.         rs = stmt.executeQuery("SELECT * FROM "+table);
  578.         while(rs.next()){
  579.             if(rs.getString("name").equals(username)||admin){
  580.                 //drop the table and delete from dicts list.
  581.                 stmt.executeUpdate("DROP TABLE "+table);
  582.                 PreparedStatement pstmt = con.prepareStatement("DELETE FROM 'dicts' WHERE 'dicts'.'name' = ?");
  583.                 pstmt.setString(1,table);
  584.                 pstmt.executeUpdate();
  585.                 //update user data
  586.                 ResultSet rs2 = stmt.executeQuery("SELECT * FROM users WHERE users.name = '"+username+"'");
  587.                 debpoint=402;
  588.                 while(rs2.next()){
  589.                     pstmt = con.prepareStatement("UPDATE users SET dicts = ?");
  590.                     pstmt.setInt(1,rs.getInt("dicts")+1);
  591.                     pstmt.executeUpdate();
  592.                 }
  593.             }else{
  594.                 outWr("You don't have the rights to delete this table.");return(false);
  595.             }
  596.         }
  597.         con.close();
  598.         }catch(Exception e){debWr(e.toString()+" @ "+debpoint);return(false);}
  599.         return(true);
  600.     }
  601.     boolean registerSql(){
  602.         try{
  603.         debpoint=389;
  604.        
  605.         Statement stmt;
  606.         ResultSet rs;
  607.        
  608.         Connection con = DriverManager.getConnection(url,SQLuser, SQLpw);
  609.        
  610.         debpoint=390;
  611.        
  612.         stmt = con.createStatement();
  613.         //                                                              id,name,pass,mail,date,dicts
  614.         PreparedStatement PStmt = con.prepareStatement("INSERT INTO users VALUES(?,?,?,?,NOW(),?)");
  615.         PStmt.setInt(1,0);
  616.         PStmt.setString(2,username);
  617.         PStmt.setString(3,userpass);
  618.         PStmt.setString(4,usermail);
  619.         PStmt.setInt(5,0);
  620.         PStmt.executeUpdate();
  621.        
  622.         debpoint=401;
  623.        
  624.         con.close();
  625.         }catch(Exception e){debWr(e.toString()+" @ "+debpoint);return(false);}
  626.         return(true);
  627.     }
  628.     boolean verifySql(){
  629.         try{
  630.         debpoint=402;
  631.        
  632.         Statement stmt;
  633.         ResultSet rs;
  634.        
  635.         Connection con = DriverManager.getConnection(url,SQLuser, SQLpw);
  636.        
  637.         debpoint=408;
  638.        
  639.         stmt = con.createStatement();
  640.        
  641.         rs = stmt.executeQuery("SELECT * FROM users");
  642.        
  643.         counter=0;
  644.         while(rs.next()){
  645.             if(rs.getString("name").equals(username)&&rs.getString("pass").equals(userpass)){userid=rs.getInt("id");loggedIn=true;break;}
  646.         }
  647.        
  648.         con.close();
  649.         }catch(Exception e){debWr(e.toString()+" @ "+debpoint);return(false);}
  650.         if(loggedIn)
  651.         return(true);
  652.         else
  653.         return(false);
  654.     }
  655.     boolean usersSql(){
  656.         try{
  657.         debpoint=472;
  658.        
  659.         Statement stmt;
  660.         ResultSet rs;
  661.        
  662.         Connection con = DriverManager.getConnection(url,SQLuser, SQLpw);
  663.        
  664.         debpoint=479;
  665.        
  666.         stmt = con.createStatement();
  667.        
  668.         rs = stmt.executeQuery("SELECT * FROM users");
  669.        
  670.         while(rs.next()){
  671.             outWr(rs.getInt("id")+"-"+rs.getString("name")+" member since: "+rs.getString("reg_date")+" dictionarys uploaded: "+rs.getString("dicts"));
  672.         }
  673.         debpoint=488;
  674.         }catch(Exception e){debWr(e.toString()+" @ "+debpoint);return(false);}
  675.         return(true);
  676.     }
  677.     boolean updateSql(){
  678.         try{
  679.         debpoint=603;
  680.         Statement stmt;
  681.         ResultSet rs;
  682.        
  683.         Connection con = DriverManager.getConnection(url,SQLuser, SQLpw);
  684.        
  685.         stmt = con.createStatement();
  686.        
  687.         rs = stmt.executeQuery("SELECT * FROM version");
  688.         debpoint=612;
  689.         while(rs.next()){
  690.             if(rs.getString("program").equals("Voc")){
  691.                 if(rs.getDouble("version")>updatevers){
  692.                     updateable = true;
  693.                     updatevers = rs.getDouble("version");
  694.                     updateprio = rs.getInt("priority");
  695.                     updatesubv = rs.getInt("subversion");
  696.                     updatedesc = rs.getString("description");
  697.                     if(rs.getInt("beta")>0)
  698.                         updatebeta=true;
  699.                     else
  700.                         updatebeta=false;
  701.                 }
  702.             }
  703.         }
  704.        
  705.         }catch(Exception e){debWr(e.toString()+" @ "+debpoint);return(false);}
  706.         return(true);
  707.     }
  708.     //End MySQL functions
  709.    
  710.     //Start FTP functions
  711.     boolean updateFTP(){
  712.         try{
  713.         debpoint=658;
  714.         FTPClient client = new FTPClient();
  715.        
  716.         client.connect(host, port);
  717.         debpoint=662;
  718.        
  719.         if(client.login(FTPuser, FTPpw)) {
  720.             //download
  721.             File file = new File("update.zip");
  722.             FileOutputStream dfile = new FileOutputStream(file);
  723.             client.retrieveFile("/upd/"+updatevers+"-"+updatesubv+"/voc.zip",dfile);
  724.             debpoint=669;
  725.             dfile.close();
  726.             client.disconnect();
  727.            
  728.             //unzip
  729.             final int BUFFER = 2048;
  730.             BufferedOutputStream dest = null;
  731.             FileInputStream fis = new FileInputStream("update.zip");
  732.             ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
  733.             ZipEntry entry;
  734.             while((entry = zis.getNextEntry()) != null) {
  735.                 debpoint=677;
  736.                 System.out.println("Extracting: " +entry);
  737.                 int count;
  738.                 byte data[] = new byte[BUFFER];
  739.                 // write the files to the disk
  740.                 FileOutputStream fos = new FileOutputStream(entry.getName());
  741.                 dest = new BufferedOutputStream(fos, BUFFER);
  742.                 while ((count = zis.read(data, 0, BUFFER)) != -1) {
  743.                     debpoint=685;
  744.                     dest.write(data, 0, count);
  745.                 }
  746.                 dest.flush();
  747.                 dest.close();
  748.             }
  749.             zis.close();
  750.         }
  751.         debpoint=691;
  752.        
  753.         }catch(Exception e){debWr(e.toString()+" @ "+debpoint);return(false);}
  754.         return(true);
  755.     }
  756.     //End FTP functions
  757.    
  758.     //other functions and classes
  759.     void randomSet(){
  760.         Random gen = new Random();
  761.         randoms = new Vector<Integer>(5000);
  762.         if(dict.size()>0){
  763.         randoms.add(gen.nextInt(dict.size()));
  764.         debpoint=131;
  765.         for(int i=0;i<dict.size();i++){
  766.             int ch = gen.nextInt(dict.size()+1)-1;
  767.             debpoint=134;
  768.             while(randoms.contains(ch))
  769.                 ch=gen.nextInt(dict.size()+1)-1;
  770.             debpoint=137;
  771.             randoms.add(ch);
  772.         }}
  773.         randoms.add(10);
  774.     }
  775.     class TimeChk extends Thread{
  776.         public void run(){
  777.             while(!isInterrupted()){
  778.             try{
  779.             if(inf.timer.timets<=0){
  780.                 trys++;set.doClick();}
  781.             deb.data.setText("stage: "+stage+"\ttrys: "+trys+"\nWord: "+randoms.get(counter)+"\tcounter: "+counter+"\nLogged in: "+loggedIn+"\nusername: "+username+"\tuserpass: "+userpass+"\tusermail: "+usermail+"\ndebpoint: "+debpoint);
  782.             Thread.sleep(200);}catch(Exception e){debWr(e.toString()+" @ "+debpoint);}
  783.             }
  784.         }
  785.     }
  786.  
  787.     //Main frame settings
  788.     @SuppressWarnings("deprecation")
  789.     Voc(String title){
  790.         super(title);
  791.        
  792.         stage=0;
  793.        
  794.         edit = new editVoc(this,"Voc-editor",true,ew,eh);
  795.         edit.setLocation(w+10+iw+10,0);
  796.        
  797.         chk.start();
  798.         inf.counter.start();
  799.         inf.counter.suspend();
  800.         inf.timer.start();
  801.         inf.timer.suspend();
  802.        
  803.         randomSet();
  804.        
  805.         setLayout(null);
  806.        
  807.         in=new JTextField();
  808.         set=new JButton("Go");
  809.         out=new JTextArea(welcome);
  810.         JScrollPane scr = new JScrollPane(out);
  811.        
  812.         out.setEditable(false);
  813.        
  814.         scr.setBounds(0,0,w,h-50);
  815.         set.setBounds(w-50,h-50,50,50);
  816.         in.setBounds(0,h-50,w-50,50);
  817.        
  818.         add(set);
  819.         add(scr);
  820.         add(in);
  821.        
  822.         set.addKeyListener(new keys());
  823.         scr.addKeyListener(new keys());
  824.         in.addKeyListener(new keys());
  825.         set.addActionListener(new lst());
  826.        
  827.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  828.     }
  829.     public static void main(String[] args){
  830.         inf = new Info(iw,ih);
  831.         deb = new Debug(dw,dh);
  832.         Voc hpt = new Voc(version);
  833.         inf.setSize(iw+10,ih+35);
  834.         hpt.setSize(w+10,h+25);
  835.         deb.setSize(dw+10,dh+25);
  836.         deb.setLocation(w+10,0);
  837.         inf.setLocation(w+10,dh+25);
  838.         hpt.setVisible(true);
  839.         deb.setVisible(false);
  840.         inf.setVisible(false);
  841.         try{DriverManager.registerDriver(new com.mysql.jdbc.Driver());}catch(Exception e){e.printStackTrace();}
  842.     }
  843.    
  844. }
  845.  
  846. //debug console
  847. class Debug extends JFrame{
  848.     JTextArea output;
  849.     JTextArea data;
  850.     java.util.Date d = new java.util.Date();
  851.     Debug(int dw,int dh){
  852.         super("Debug console");
  853.         setLayout(null);
  854.         data=new JTextArea("");
  855.         output = new JTextArea("Debug console started on "+d.toString());
  856.        
  857.         data.setEditable(false);
  858.         output.setEditable(false);
  859.        
  860.         JScrollPane scroll = new JScrollPane(output);
  861.         JScrollPane scrol2 = new JScrollPane(data);
  862.         scroll.setBounds(0,0,dw,dh);
  863.         scrol2.setBounds(0,dh,dw,(dh)/2);
  864.         add(scroll);
  865.         add(scrol2);
  866.         setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
  867.     }
  868. }
  869.  
  870. //edit voc console
  871. class editVoc extends JDialog implements ActionListener{
  872.     JTextArea dict = null;
  873.     JTextArea sol = null;
  874.     JButton set = null;
  875.    
  876.     private boolean okStatus = false;
  877.    
  878.     public editVoc(Frame besitzer, String titel,boolean modal,int ew,int eh){
  879.         super(besitzer, titel, modal);
  880.        
  881.         setSize(ew+10,eh+25);
  882.         Container cp = getContentPane();
  883.         cp.setLayout(null);
  884.        
  885.         dict = new JTextArea();
  886.         sol = new JTextArea();
  887.         set = new JButton("Done!");
  888.         JScrollPane scr = new JScrollPane(dict);
  889.         JScrollPane sc2 = new JScrollPane(sol);
  890.        
  891.         scr.setBounds(0,0,Math.round(ew/2)-3,eh-50);
  892.         sc2.setBounds(Math.round(ew/2)+3,0,Math.round(ew/2)-3,eh-50);
  893.         set.setBounds(0,eh-50,ew,50);
  894.        
  895.         add(scr);
  896.         add(sc2);
  897.         add(set);
  898.        
  899.         set.addActionListener(this);
  900.     }
  901.     public String[] getData(){
  902.         if(okStatus==true){
  903.             String[] result = new String[2];
  904.             int i=0;
  905.             result[0]=dict.getText();
  906.             result[1]=sol.getText();
  907.             return(result);
  908.         }else{
  909.             return null;
  910.         }
  911.     }
  912.     public void setData(String d,String s){
  913.         dict.setText(d);
  914.         sol.setText(s);
  915.     }
  916.     public static String[] showDialog(Frame f, String titel,int ew, int eh){
  917.         editVoc edit = new editVoc(f,titel,true,ew,eh);
  918.         edit.setVisible(true);
  919.         return(edit.getData());
  920.     }
  921.     public void actionPerformed(ActionEvent e){
  922.         okStatus=true;
  923.         setVisible(false);
  924.     }
  925. }
  926.  
  927. //info frame
  928. class Info extends JFrame{
  929.     JProgressBar time;
  930.     JProgressBar score;
  931.     JProgressBar overall;
  932.     JLabel timl;
  933.     JLabel scrl;
  934.     JLabel ovrl;
  935.     Timer counter;
  936.     Timer timer;
  937.     Info(int iw,int ih){
  938.         super("Info");
  939.         setLayout(null);
  940.         timl=new JLabel("-:-");
  941.         scrl=new JLabel("-/-");
  942.         ovrl=new JLabel("-/-");
  943.         time=new JProgressBar();
  944.         score=new JProgressBar();
  945.         overall=new JProgressBar();
  946.         time.setBounds(10,ih/6,iw-20,ih/6);
  947.         score.setBounds(10,ih/2,iw-20,ih/6);
  948.         overall.setBounds(10,ih/6*5,iw-20,ih/6);
  949.         timl.setBounds(10,0,iw-20,ih/6);
  950.         scrl.setBounds(10,ih/6*2,iw-20,ih/6);
  951.         ovrl.setBounds(10,ih/6*4,iw-20,ih/6);
  952.         add(time);
  953.         add(score);
  954.         add(overall);
  955.         add(timl);
  956.         add(scrl);
  957.         add(ovrl);
  958.         time.setMinimum(0);
  959.         score.setMinimum(0);
  960.         overall.setMinimum(0);
  961.         overall.setString("Overall");
  962.         score.setString("Score");
  963.         time.setString("Time");
  964.        
  965.         timer = new Timer(false,0,20,0);
  966.         counter = new Timer(true,0,0,0);
  967.         setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
  968.     }
  969.  
  970.     //timer
  971.     class Timer extends Thread{
  972.         int timets,times,timem;boolean count = false;
  973.         Timer(boolean mode,int tts,int ts, int tm){
  974.             count=mode;
  975.             timets=tts;times=ts;timem=tm;
  976.             if(!count){
  977.                 timets+=times*10+timem*600;
  978.                 time.setMaximum(timets);}
  979.         }
  980.         public void run(){
  981.             while(isInterrupted()==false){
  982.                 try{
  983.                 if(count){
  984.                     timets++;
  985.                     if(timets>=10){times++;timets=0;}
  986.                     if(times>=60){timem++;times=0;}
  987.                     timl.setText(timem+":"+times+":"+timets);
  988.                 }else{
  989.                     timets--;
  990.                     time.setValue(timets);
  991.                 }
  992.                 Thread.sleep(100);}catch(Exception e){}
  993.             }
  994.         }
  995.     }
  996. }
Add Comment
Please, Sign In to add comment