Advertisement
Guest User

java

a guest
Mar 14th, 2012
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.66 KB | None | 0 0
  1.  Properties connInfo = new Properties();
  2.                
  3.                 connInfo.put("characterEncoding","UTF8");
  4.                 connInfo.put("user", "root");
  5.                 connInfo.put("password", "root");
  6.                 try {
  7.                     conn  = DriverManager.getConnection("jdbc:mysql://127.0.0.5/?", connInfo);
  8.                 }
  9.                 catch (SQLException ex) {            
  10.                     System.out.println(ex.toString());
  11.                     //ex.printStackTrace();
  12.                 }
  13. while (true){
  14.     System.gc();
  15.     Thread.sleep(500);
  16.          PreparedStatement   pstmt = null;
  17.          ResultSet            rs    = null;
  18.          User[] usersArr = null;
  19.          Map<Integer,Calendar>          times=new HashMap<Integer, Calendar>();
  20.          try {
  21.         pstmt = conn.prepareStatement("use db;");
  22.        
  23.         if(pstmt.execute()) {
  24.             rs = pstmt.getResultSet();
  25.         }
  26.         pstmt = conn.prepareStatement("SELECT * FROM data;");
  27.         if(pstmt.execute()) {
  28.             rs = pstmt.getResultSet();                                
  29.            
  30.             while (rs.next()) {
  31.                 Calendar a=Calendar.getInstance();
  32.                 String date=new String(rs.getString("start_time"));
  33.                 Integer type=rs.getInt("type");
  34.                 a.set(Integer.parseInt(date.substring(0, 4)), Integer.parseInt(date.substring(5,7))-1, Integer.parseInt(date.substring(8, 10)), Integer.parseInt(date.substring(11,13)), Integer.parseInt(date.substring(14,16)), Integer.parseInt(date.substring(17,19)));
  35.                 times.put(type,a);
  36.                 a=null;
  37.                 date=null;
  38.                 type=null;
  39.                
  40.             }
  41.         }
  42.          }
  43.          catch (SQLException ex) {            
  44.                 System.out.println(ex.toString());
  45.                 //ex.printStackTrace();
  46.             }
  47.        
  48.         float absval;
  49.        
  50.         Calendar now=Calendar.getInstance();
  51.         absval=(int) (((now.getTime().getTime()-times.get(1).getTime().getTime()))/1000);
  52.         if (absval>60){
  53.             int prize=0;
  54.               try {
  55.                   int k = 0;
  56.                   pstmt = conn.prepareStatement("UPDATE data SET start_time=\""+now.get(Calendar.YEAR)+"-"+(now.get(Calendar.MONTH)+1)+"-"+now.get(Calendar.DATE)+" "+now.get(Calendar.HOUR_OF_DAY)
  57.                           +":"+now.get(Calendar.MINUTE)+":"+now.get(Calendar.SECOND)+"\" WHERE type=1");
  58.                   pstmt.execute();
  59.                   pstmt = conn.prepareStatement("SELECT COUNT(*) FROM sessions,users WHERE sessions.type_of_game=1 AND users.id=sessions.user_id");
  60.                   if(pstmt.execute()) {
  61.                         rs = pstmt.getResultSet();
  62.                         rs.next();
  63.                         k=rs.getInt(1);
  64.                     }
  65.                  
  66.                   usersArr=new User[k];
  67.                     pstmt = conn.prepareStatement("SELECT * FROM sessions,users WHERE sessions.type_of_game=1 AND users.id=sessions.user_id");
  68.                    
  69.                     if(pstmt.execute()) {
  70.                         rs = pstmt.getResultSet();                                
  71.                         int counter=0;
  72.                         while (rs.next()) {
  73.                             User temp=new User();
  74.                             temp.balance=rs.getFloat("balance");
  75.                             temp.first_name=rs.getString("first_name");
  76.                             temp.last_name=rs.getString("last_name");
  77.                             temp.fun_credits=rs.getFloat("fun_credits");
  78.                             temp.type_of_game=rs.getInt("type_of_game");
  79.                             temp.user_id=rs.getInt("user_id");
  80.                             temp.username=rs.getString("login");
  81.                             temp.points=rs.getInt("points");
  82.                             usersArr[counter]=temp;
  83.                             counter++;
  84.                             temp=null;
  85.                         }
  86.                     }
  87.                     pstmt = conn.prepareStatement("SELECT prize FROM games WHERE type=1");
  88.                       if(pstmt.execute()) {
  89.                             rs = pstmt.getResultSet();
  90.                             rs.next();
  91.                             prize=rs.getInt("prize");
  92.                         }
  93.                       pstmt = conn.prepareStatement("UPDATE games SET prize=0 WHERE type=1");
  94.                       if(pstmt.execute()) {
  95.                             rs = pstmt.getResultSet();
  96.                             rs.next();
  97.                             prize=rs.getInt("prize");
  98.                         }
  99.                      }
  100.              
  101.                      catch (SQLException ex) {            
  102.                             System.out.println(ex.toString());
  103.                             //ex.printStackTrace();
  104.                         }
  105.               int max=0;
  106.               for (User user : usersArr) {
  107.                 if (user.points>max){
  108.                     max=user.points;
  109.                 }
  110.         }
  111.               int countmax=0;
  112.                 for (User user : usersArr) {
  113.                     if (user.points==max){
  114.                         countmax++;
  115.                     }
  116.             }
  117.                 float prize_for_one=prize;
  118.                 if (countmax!=0)
  119.                     prize_for_one=prize/countmax;
  120.                         try{
  121.                         pstmt = conn.prepareStatement("UPDATE users,sessions SET users.balance=users.balance+"+prize_for_one+" WHERE sessions.points="+max+" AND sessions.user_id=users.id AND sessions.type_of_game=2");
  122.                           pstmt.execute();
  123.  
  124.                           pstmt = conn.prepareStatement("DELETE FROM sessions WHERE type_of_game=1");
  125.                           pstmt.execute();
  126.                                 rs = pstmt.getResultSet();
  127.                                 Calendar writedate=Calendar.getInstance();
  128.                                 SimpleDateFormat simpleDateformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  129.                                 System.out.println("["+simpleDateformat.format(writedate.getTime())+"] Minute Game Stopped");
  130.                                 System.out.println("Count of winners="+countmax+" Game prize="+prize+" Prize for one winner="+prize_for_one );
  131.                                 System.out.println("["+simpleDateformat.format(writedate.getTime())+"] Minute Game Started");
  132.                                 writedate=null;
  133.                                 simpleDateformat=null;
  134.                         }
  135.                         catch (SQLException ex) {            
  136.                             System.out.println(ex.toString());
  137.                             //ex.printStackTrace();
  138.                         }
  139.                         now=null;
  140.                         usersArr=null;
  141.                         pstmt=null;
  142.                         rs=null;
  143.         }
  144.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement