16
16
import com .apporelbotna .gameserver .stubs .User ;
17
17
import com .apporelbotna .gameserver .stubs .UserWrapper ;
18
18
19
- public class GameDAO
20
- {
21
- public static final String SERVER_URL = "http://172.16.2.94 :8082/" ;
19
+ public class GameDAO {
20
+ public static final String SERVER_URL = "http://172.16.2.94:8082/" ;
21
+ // public static final String SERVER_URL = "http://localhost :8082/";
22
22
RestTemplate restTemplate = new RestTemplate ();
23
23
24
- public GameDAO ()
25
- {
24
+ public GameDAO () {
26
25
27
26
}
28
27
29
- public boolean finishMatch (Match ... matches )
30
- {
28
+ public boolean finishMatch (Match ... matches ) {
31
29
32
- for (Match match : matches )
33
- {
34
- ResponseEntity <?> response = restTemplate .postForEntity (SERVER_URL + "/match" , match ,
35
- null );
36
- if (!response .getStatusCode ().equals (HttpStatus .CREATED ))
37
- {
30
+ for (Match match : matches ) {
31
+ ResponseEntity <?> response = restTemplate .postForEntity (SERVER_URL + "/match" , match , null );
32
+ if (!response .getStatusCode ().equals (HttpStatus .CREATED )) {
38
33
return false ;
39
34
}
40
35
}
41
36
42
37
return true ;
43
38
}
44
39
45
- public boolean isUserLoggeable (String email , String tokenString )
46
- {
40
+ public boolean isUserLoggeable (String email , String tokenString ) {
47
41
48
42
UserWrapper wrapper = new UserWrapper (new User (email ), new Token (tokenString ));
49
43
50
- ResponseEntity <?> response = restTemplate .postForEntity (SERVER_URL + "/auth" , wrapper ,
51
- null );
44
+ ResponseEntity <?> response = restTemplate .postForEntity (SERVER_URL + "/auth" , wrapper , null );
52
45
53
46
return (response .getStatusCode ().equals (HttpStatus .OK ));
54
47
}
55
48
56
- public User validateUser (String email , String tokenString )
57
- {
49
+ public User validateUser (String email , String tokenString ) {
58
50
return isUserLoggeable (email , tokenString ) ? getUserInformation (email ) : null ;
59
51
}
60
52
61
- public Token login (String email , String password )
62
- {
53
+ public Token login (String email , String password ) {
63
54
64
55
RestTemplate restTemplate = new RestTemplate ();
65
- return restTemplate .getForObject (SERVER_URL + "/login/" + email + "/" + password ,
66
- Token .class );
56
+ return restTemplate .getForObject (SERVER_URL + "/login/" + email + "/" + password , Token .class );
67
57
}
68
58
69
59
// saca todos los juegos de un usuario
70
- public List <Game > findAllGamesByUser (User user )
71
- {
60
+ public List <Game > findAllGamesByUser (User user ) {
72
61
String userEmail = user .getId ();
73
62
74
63
RestTemplate restTemplate = new RestTemplate ();
75
64
76
- ResponseEntity <List <Game >> responseWS = restTemplate .exchange (
77
- SERVER_URL + "/user/game/" + userEmail , HttpMethod .GET , null ,
78
- new ParameterizedTypeReference <List <Game >>()
79
- {
65
+ ResponseEntity <List <Game >> responseWS = restTemplate .exchange (SERVER_URL + "/user/game/" + userEmail ,
66
+ HttpMethod .GET , null , new ParameterizedTypeReference <List <Game >>() {
80
67
});
81
68
82
69
return responseWS .getBody ();
83
70
}
84
71
85
- public boolean createUser (User user , String password )
86
- {
72
+ public boolean createUser (User user , String password ) {
87
73
RegisterUser userToRegister = new RegisterUser (user , password );
88
74
89
- ResponseEntity <?> response = restTemplate .postForEntity (SERVER_URL + "/user/" ,
90
- userToRegister , null );
75
+ ResponseEntity <?> response = restTemplate .postForEntity (SERVER_URL + "/user/" , userToRegister , null );
76
+
77
+ if (response .getStatusCode ().equals (HttpStatus .CREATED )) {
78
+ return true ;
79
+ }
80
+ return false ;
91
81
92
- return (response .getStatusCode ().equals (HttpStatus .CREATED ));
93
82
}
94
83
95
- public User getUserInformation (String email )
96
- {
84
+ public User getUserInformation (String email ) {
97
85
return restTemplate .getForObject (SERVER_URL + "/user/" + email , User .class );
98
86
}
99
87
@@ -103,22 +91,67 @@ public User getUserInformation(String email)
103
91
* @param gameID
104
92
* @return time in miliseconds
105
93
*/
106
- public float gameTimePlayedByGame (String email , int gameID )
107
- {
108
- return restTemplate .getForObject (
109
- SERVER_URL + "/user/" + email + "/game/" + gameID + "/time/" , Float .class );
94
+ public float gameTimePlayedByGame (String email , int gameID ) {
95
+ return restTemplate .getForObject (SERVER_URL + "/user/" + email + "/game/" + gameID + "/time/" , Float .class );
110
96
}
111
97
112
- public List <RankingPointsTO > getRankingPointsByGameAndUser (int gameId )
113
- {
98
+ public List <RankingPointsTO > getRankingPointsByGameAndUser (int gameId ) {
114
99
115
- ResponseEntity <List <RankingPointsTO >> responseWS = restTemplate .exchange (
116
- SERVER_URL + "/ranking/" + gameId , HttpMethod .GET , null ,
117
- new ParameterizedTypeReference <List <RankingPointsTO >>()
118
- {
100
+ ResponseEntity <List <RankingPointsTO >> responseWS = restTemplate .exchange (SERVER_URL + "/ranking/" + gameId ,
101
+ HttpMethod .GET , null , new ParameterizedTypeReference <List <RankingPointsTO >>() {
119
102
});
120
103
121
104
return responseWS .getBody ();
122
105
}
123
106
107
+ public static void main (String [] args ) {
108
+ GameDAO dao = new GameDAO ();
109
+
110
+ // Creating User
111
+ // User user = new User("[email protected] ", "rai");
112
+ // String password = "1234";
113
+ //
114
+ // System.out.println(dao.createUser(user, password));
115
+
116
+ // Testing log in
117
+
118
+ String email =
"[email protected] " ;
119
+ String password = "1234" ;
120
+ Token token = dao .login (email , password );
121
+ System .out .println (token );
122
+
123
+ // Find all games by username
124
+ // List<Game> games = dao.findAllGamesByUser(new User("[email protected] ", "pp"));
125
+ // for (Game game : games)
126
+ // {
127
+ // System.out.println(game);
128
+ // }
129
+
130
+ // TEST USER INFORMATION
131
+
132
+ // User userInformatiton = dao.getUserInformation("[email protected] ");
133
+ // System.out.println(userInformatiton);
134
+
135
+ // GET TIME PLAYED IN GAME
136
+ // String email = "[email protected] ";
137
+ // int game = 1;
138
+ // System.out.println(dao.gameTimePlayedByGame(email, game));
139
+
140
+ // Test ranking
141
+ // List<RankingPointsTO> ranking = dao.getRankingPointsByGameAndUser(1);
142
+ // for (RankingPointsTO rankingPointsTO : ranking) {
143
+ // System.out.println(rankingPointsTO);
144
+ // }
145
+
146
+ // Auth
147
+ // System.out.println(dao.validateUser("[email protected] ",
148
+ // "114e39264fd343e98e138837172a9e36"));
149
+
150
+ // Finish match
151
+ // Match match1 = new Match("[email protected] ", 1, 30000, 25);
152
+ // Match match2 = new Match("[email protected] ", 1, 30000, -25);
153
+ // System.out.println(dao.finishMatch(match1, match2));
154
+
155
+ }
156
+
124
157
}
0 commit comments