Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Properties connInfo = new Properties();
- connInfo.put("characterEncoding","UTF8");
- connInfo.put("user", "root");
- connInfo.put("password", "root");
- try {
- conn = DriverManager.getConnection("jdbc:mysql://127.0.0.5/?", connInfo);
- }
- catch (SQLException ex) {
- System.out.println(ex.toString());
- //ex.printStackTrace();
- }
- while (true){
- System.gc();
- Thread.sleep(500);
- PreparedStatement pstmt = null;
- ResultSet rs = null;
- User[] usersArr = null;
- Map<Integer,Calendar> times=new HashMap<Integer, Calendar>();
- try {
- pstmt = conn.prepareStatement("use db;");
- if(pstmt.execute()) {
- rs = pstmt.getResultSet();
- }
- pstmt = conn.prepareStatement("SELECT * FROM data;");
- if(pstmt.execute()) {
- rs = pstmt.getResultSet();
- while (rs.next()) {
- Calendar a=Calendar.getInstance();
- String date=new String(rs.getString("start_time"));
- Integer type=rs.getInt("type");
- 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)));
- times.put(type,a);
- a=null;
- date=null;
- type=null;
- }
- }
- }
- catch (SQLException ex) {
- System.out.println(ex.toString());
- //ex.printStackTrace();
- }
- float absval;
- Calendar now=Calendar.getInstance();
- absval=(int) (((now.getTime().getTime()-times.get(1).getTime().getTime()))/1000);
- if (absval>60){
- int prize=0;
- try {
- int k = 0;
- 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)
- +":"+now.get(Calendar.MINUTE)+":"+now.get(Calendar.SECOND)+"\" WHERE type=1");
- pstmt.execute();
- pstmt = conn.prepareStatement("SELECT COUNT(*) FROM sessions,users WHERE sessions.type_of_game=1 AND users.id=sessions.user_id");
- if(pstmt.execute()) {
- rs = pstmt.getResultSet();
- rs.next();
- k=rs.getInt(1);
- }
- usersArr=new User[k];
- pstmt = conn.prepareStatement("SELECT * FROM sessions,users WHERE sessions.type_of_game=1 AND users.id=sessions.user_id");
- if(pstmt.execute()) {
- rs = pstmt.getResultSet();
- int counter=0;
- while (rs.next()) {
- User temp=new User();
- temp.balance=rs.getFloat("balance");
- temp.first_name=rs.getString("first_name");
- temp.last_name=rs.getString("last_name");
- temp.fun_credits=rs.getFloat("fun_credits");
- temp.type_of_game=rs.getInt("type_of_game");
- temp.user_id=rs.getInt("user_id");
- temp.username=rs.getString("login");
- temp.points=rs.getInt("points");
- usersArr[counter]=temp;
- counter++;
- temp=null;
- }
- }
- pstmt = conn.prepareStatement("SELECT prize FROM games WHERE type=1");
- if(pstmt.execute()) {
- rs = pstmt.getResultSet();
- rs.next();
- prize=rs.getInt("prize");
- }
- pstmt = conn.prepareStatement("UPDATE games SET prize=0 WHERE type=1");
- if(pstmt.execute()) {
- rs = pstmt.getResultSet();
- rs.next();
- prize=rs.getInt("prize");
- }
- }
- catch (SQLException ex) {
- System.out.println(ex.toString());
- //ex.printStackTrace();
- }
- int max=0;
- for (User user : usersArr) {
- if (user.points>max){
- max=user.points;
- }
- }
- int countmax=0;
- for (User user : usersArr) {
- if (user.points==max){
- countmax++;
- }
- }
- float prize_for_one=prize;
- if (countmax!=0)
- prize_for_one=prize/countmax;
- try{
- 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");
- pstmt.execute();
- pstmt = conn.prepareStatement("DELETE FROM sessions WHERE type_of_game=1");
- pstmt.execute();
- rs = pstmt.getResultSet();
- Calendar writedate=Calendar.getInstance();
- SimpleDateFormat simpleDateformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- System.out.println("["+simpleDateformat.format(writedate.getTime())+"] Minute Game Stopped");
- System.out.println("Count of winners="+countmax+" Game prize="+prize+" Prize for one winner="+prize_for_one );
- System.out.println("["+simpleDateformat.format(writedate.getTime())+"] Minute Game Started");
- writedate=null;
- simpleDateformat=null;
- }
- catch (SQLException ex) {
- System.out.println(ex.toString());
- //ex.printStackTrace();
- }
- now=null;
- usersArr=null;
- pstmt=null;
- rs=null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement