diff --git a/src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java b/src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java index eb177b5..fe2e760 100644 --- a/src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java +++ b/src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java @@ -156,29 +156,28 @@ public class APIConnection * @param ids * @return */ - public Map getNames(List ids) + public List getPlayers(List ids) { System.out.println(ids); - Map map = new HashMap<>(); - + List players = new ArrayList<>(); while(!ids.isEmpty()) { - String queryUrl = baseURL + playerInfoURL + apiKey + "&steamids="; + StringBuilder queryUrl = new StringBuilder(baseURL + playerInfoURL + apiKey + "&steamids="); - int remove = (ids.size() > 100) ? 100 : ids.size(); + int remove = Math.min(ids.size(), 100); for(int i = 0; i < remove; i++) { - queryUrl = queryUrl + "," + ids.remove(0); + queryUrl.append(",").append(ids.remove(0)); } System.out.println(queryUrl); JSONArray names; - String apiResult = this.querySteamAPI(queryUrl); + String apiResult = this.querySteamAPI(queryUrl.toString()); if(apiResult.equals("")) - return map; + return players; JSONObject object = new JSONObject(apiResult); @@ -189,43 +188,37 @@ public class APIConnection else { //eh - return map; + return players; } for(int i = 0; i < names.length(); i++) { JSONObject player = names.getJSONObject(i); - if(player.has("steamid") && player.has("personaname")) + if(player.has(Player.KEY_STEAM_ID) && player.has(Player.KEY_USERNAME)) { - map.put(player.getString("steamid"), - player.getString("personaname")); + players.add(transformToPlayer(player)); } } } - return map; + return players; } - /** - * Wrapper for getNames which returns a list of players instead - * of a map from id's to names - * - * @param ids - * @return - */ - public List getFullPlayers(List ids) + private Player transformToPlayer(JSONObject player) { - Map map = this.getNames(ids); - - List players = new ArrayList<>(); - - for(String id: map.keySet()) - { - players.add(new Player(map.get(id),id)); - } - - return players; + String avatar = player.has(Player.KEY_AVATAR) ? + player.getString(Player.KEY_AVATAR) : + ""; + String realName = player.has(Player.KEY_REAL_NAME) ? + player.getString(Player.KEY_REAL_NAME) : + ""; + String id = player.getString(Player.KEY_STEAM_ID); + Integer timeCreated = player.has(Player.KEY_TIME_CREATED) ? + player.getInt(Player.KEY_TIME_CREATED) : + 0; + String username = player.getString(Player.KEY_USERNAME); + return new Player(username, id, realName, timeCreated, avatar); } @@ -236,20 +229,11 @@ public class APIConnection * @param steamid the steam id of player * @return */ - public String getPlayerName(String steamid) + public Player getSingle(String steamid) throws SteamConnectionException { - JSONObject response; - try - { - response = new JSONObject(WebScraper - .getWebsite(this.baseURL + this.playerInfoURL + - this.apiKey + "&steamids=" + steamid)); - } - catch (SteamConnectionException ex) - { - return ""; - } - + JSONObject response = new JSONObject(WebScraper + .getWebsite(this.baseURL + this.playerInfoURL + + this.apiKey + "&steamids=" + steamid)); if(response.has("response")) { @@ -257,9 +241,10 @@ public class APIConnection if(response.has("players")) { JSONArray arr = response.getJSONArray("players"); + if(arr.length() > 0) { - return arr.getJSONObject(0).getString("personaname"); + return transformToPlayer(arr.getJSONObject(0)); } } } @@ -267,13 +252,13 @@ public class APIConnection } - public static void main(String[] args) + public static void main(String[] args) throws SteamConnectionException { APIConnection con = new APIConnection(); //steam id of jrtechs con.getFriends("76561198188400721").forEach(System.out::println); - System.out.println(con.getPlayerName("76561198188400721")); + System.out.println(con.getSingle("76561198188400721")); } } \ No newline at end of file diff --git a/src/main/java/net/jrtechs/www/graphDB/SteamGraph.java b/src/main/java/net/jrtechs/www/graphDB/SteamGraph.java index e63a546..8b0481d 100644 --- a/src/main/java/net/jrtechs/www/graphDB/SteamGraph.java +++ b/src/main/java/net/jrtechs/www/graphDB/SteamGraph.java @@ -1,5 +1,6 @@ package net.jrtechs.www.graphDB; +import net.jrtechs.www.SteamAPI.SteamConnectionException; import net.jrtechs.www.server.Player; import net.jrtechs.www.SteamAPI.APIConnection; @@ -7,6 +8,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; /** * Does graph based operations with {@link Player} @@ -16,6 +18,10 @@ import java.util.Map; */ public class SteamGraph { + public static String KEY_PLAYER = "player"; + public static String KEY_CRAWLED_STATUS = "crawled"; + + /** Connection to the graph server */ private GraphConnection con; @@ -44,8 +50,8 @@ public class SteamGraph { System.out.println("Checking id:" + id); return !con.getTraversal() - .V().hasLabel("player") - .has("id", id) + .V().hasLabel(SteamGraph.KEY_PLAYER) + .has(Player.KEY_STEAM_ID, id) .toList().isEmpty(); } @@ -56,18 +62,22 @@ public class SteamGraph * * @param */ - private void insertPlayerIntoGraph(String id, String name, boolean check) + private void insertPlayerIntoGraph(Player p, boolean check) { try { - if(!check || !this.alreadyInGraph(id)) + if(!check || !this.alreadyInGraph(p.getId())) { - System.out.println("inserting " + name + " into graph"); + System.out.println("inserting " + p.getName() + " into graph"); this.con.getTraversal() - .addV("player") - .property("name", name) - .property("crawled", 0) - .property("id", id).id().next(); + .addV(SteamGraph.KEY_PLAYER) + .property(Player.KEY_USERNAME, p.getName()) + .property(SteamGraph.KEY_CRAWLED_STATUS, 0) + .property(Player.KEY_STEAM_ID, p.getId()) + .property(Player.KEY_AVATAR, p.getAvatar()) + .property(Player.KEY_REAL_NAME, p.getRealName()) + .property(Player.KEY_TIME_CREATED, p.getTimeCreated()) + .id().next(); } } catch (Exception e) @@ -90,10 +100,10 @@ public class SteamGraph try { return !this.con.getTraversal() - .V().hasLabel("player") - .has("id", p1) + .V().hasLabel(SteamGraph.KEY_PLAYER) + .has(Player.KEY_STEAM_ID, p1) .both() - .has("id", p2) + .has(Player.KEY_STEAM_ID, p2) .toList().isEmpty(); } catch(Exception e) @@ -120,13 +130,13 @@ public class SteamGraph System.out.println("Inserting edge: " + p1 + ":" + p2); this.con.getTraversal() .V() - .hasLabel("player") - .has("id", p1) + .hasLabel(SteamGraph.KEY_PLAYER) + .has(Player.KEY_STEAM_ID, p1) .as("p1") - .V().hasLabel("player") - .has("id", p2) + .V().hasLabel(SteamGraph.KEY_PLAYER) + .has(Player.KEY_STEAM_ID, p2) .as("p2") - .addE("friends") + .addE(Player.KEY_FRIENDS) .from("p1").to("p2").id().next(); } } @@ -149,9 +159,9 @@ public class SteamGraph try { return this.con.getTraversal() - .V().hasLabel("player") - .has("id", id) - .has("crawled", 0) + .V().hasLabel(SteamGraph.KEY_PLAYER) + .has(Player.KEY_STEAM_ID, id) + .has(SteamGraph.KEY_CRAWLED_STATUS, 0) .toList().isEmpty(); } catch(Exception e) @@ -167,9 +177,9 @@ public class SteamGraph try { this.con.getTraversal().V() - .hasLabel("player") - .has("id", id) - .property("crawled", 1).id().next(); + .hasLabel(SteamGraph.KEY_PLAYER) + .has(Player.KEY_STEAM_ID, id) + .property(SteamGraph.KEY_CRAWLED_STATUS, 1).id().next(); } catch (Exception e) { @@ -184,13 +194,13 @@ public class SteamGraph * @param id * @return */ - private String getNameFromGraph(String id) + private Player getPlayerFromGraph(String id) { - return this.con.getTraversal().V() - .hasLabel("player") - .has("id", id) - .values("name") - .toStream().findFirst().get().toString(); + return new Player(this.con.getTraversal().V() + .hasLabel(SteamGraph.KEY_PLAYER) + .has(Player.KEY_STEAM_ID, id) + .valueMap() + .toStream().findFirst().get()); } @@ -203,25 +213,14 @@ public class SteamGraph private List getFriendsFromGraph(String id) { System.out.println("fetching friends from graph"); - List friends = new ArrayList<>(); - try - { - this.con.getTraversal().V() - .hasLabel("player") - .has("id", id) - .both().valueMap().toStream().forEach(r -> - friends.add( - new Player(r.get("name").toString(), - r.get("id").toString() - ) - ) - ); - } - catch(Exception e) - { - e.printStackTrace(); - } - return friends; + return new ArrayList() + {{ + con.getTraversal().V() + .hasLabel(SteamGraph.KEY_PLAYER) + .has(Player.KEY_STEAM_ID, id) + .both().valueMap().toStream().forEach(r -> + add(new Player(r))); + }}; } @@ -232,27 +231,22 @@ public class SteamGraph */ private void indexPersonFriends(String id) { - System.out.println("indexing " + this.getNameFromGraph(id)); + System.out.println("indexing " + id); List friendsIds = this.api.getFriends(id); //find ones not in database - List notInDatabase = new ArrayList<>(); - for(String fid : friendsIds) - { - if(!this.alreadyInGraph(fid)) - { - notInDatabase.add(fid); - } - } - Map names = this.api.getNames(notInDatabase); + List notInDatabase = friendsIds + .stream() + .filter(p -> !alreadyInGraph(p)) + .collect(Collectors.toList()); - for(String key: names.keySet()) - { - this.insertPlayerIntoGraph(key, names.get(key), false); - } + this.api.getPlayers(notInDatabase) + .forEach(p -> + insertPlayerIntoGraph(p, false)); - friendsIds.forEach(s-> this.insertEdgeIntoGraph(id, s)); + friendsIds.forEach(s-> + this.insertEdgeIntoGraph(id, s)); this.updateCrawledStatus(id); this.con.commit(); @@ -270,7 +264,7 @@ public class SteamGraph Player p; if(this.alreadyInGraph(id)) // yay { - p = new Player(this.getNameFromGraph(id), id); + p = this.getPlayerFromGraph(id); if(!this.playerAlreadyIndexed(id)) //must index the person { this.indexPersonFriends(id); @@ -281,15 +275,15 @@ public class SteamGraph else //smh, shouldn't happen frequently { System.out.println("brand spanking new request " + id); - String name = this.api.getPlayerName(id); - if(name == null) + try { - return null; + p = this.api.getSingle(id); + this.insertPlayerIntoGraph(p, false); } - else + catch (SteamConnectionException e) { - this.insertPlayerIntoGraph(id, name, false); - return this.getPlayer(id); + e.printStackTrace(); + return null; } } return p; @@ -315,7 +309,7 @@ public class SteamGraph public static void main(String[] args) { SteamGraph graph = new SteamGraph(); - graph.getPlayer("76561198013779806").getFriends().stream().forEach(System.out::println); + graph.getPlayer("76561198068098265").getFriends().stream().forEach(System.out::println); // graph.indexPersonFriends("76561198188400721"); graph.close(); // diff --git a/src/main/java/net/jrtechs/www/server/Client.java b/src/main/java/net/jrtechs/www/server/Client.java index 4fea5cc..4f8ee00 100644 --- a/src/main/java/net/jrtechs/www/server/Client.java +++ b/src/main/java/net/jrtechs/www/server/Client.java @@ -159,7 +159,7 @@ public class Client extends Thread sendNodeAdd(p, x, y, 150); } - List friends = p.fetchFriends(); + List friends = p.getFriends(); double radianStep = Math.PI * 2 / friends.size(); @@ -200,7 +200,7 @@ public class Client extends Thread } else { - List friends = b.fetchFriends(); + List friends = b.getFriends(); this.sendPlayerToClient(b, 300, 243, 1, 300); double radianStep = Math.PI * 2 / friends.size(); @@ -208,7 +208,7 @@ public class Client extends Thread double currentStep = 0; - for(Player f : b.fetchFriends()) + for(Player f : b.getFriends()) { f = this.graph.getPlayer(f.getId()); this.sendPlayerToClient(f, (int)(300 + Math.cos(currentStep) * 300), @@ -239,12 +239,12 @@ public class Client extends Thread { this.sendPlayerToClient(b, 600, 440, 1, 600); - for(Player f : b.fetchFriends()) //all my friends + for(Player f : b.getFriends()) //all my friends { f = this.graph.getPlayer(f.getId()); - for(Player ff : f.fetchFriends()) // all my friends friends + for(Player ff : f.getFriends()) // all my friends friends { - for(Player f2 : b.fetchFriends()) // all my friends + for(Player f2 : b.getFriends()) // all my friends { if(f2.getId().equals(ff.getId())) { diff --git a/src/main/java/net/jrtechs/www/server/Player.java b/src/main/java/net/jrtechs/www/server/Player.java index 49e1e42..8645ad8 100644 --- a/src/main/java/net/jrtechs/www/server/Player.java +++ b/src/main/java/net/jrtechs/www/server/Player.java @@ -1,9 +1,8 @@ package net.jrtechs.www.server; -import java.time.LocalDate; import java.util.ArrayList; -import java.util.Date; import java.util.List; +import java.util.Map; /** @@ -13,6 +12,15 @@ import java.util.List; */ public class Player { + public static String KEY_STEAM_ID = "steamid"; + public static String KEY_REAL_NAME = "realname"; + public static String KEY_TIME_CREATED = "timecreated"; + public static String KEY_AVATAR = "avatarfull"; + public static String KEY_USERNAME = "personaname"; + + public static String KEY_FRIENDS = "friends"; + + /** Name of the player **/ private String name; @@ -22,8 +30,11 @@ public class Player /** List of friends the player has */ private List friends; - /** Time which the player was crawled */ - private Date date; + private String realName; + + private String avatar; + + private Integer timeCreated; /** @@ -32,35 +43,34 @@ public class Player * @param name * @param id */ - public Player(String name, String id) + public Player(String name, String id, + String realName, Integer timeCreated, + String avatar) { this.name = name; this.id = id; - this.friends = null; - this.date = new Date(); + this.realName = realName; + this.timeCreated = timeCreated; + this.avatar = avatar; this.friends = new ArrayList<>(); } - public List getFriends() + public Player(Map apiInfo) { - return friends; + this.id = ((List) apiInfo.get(Player.KEY_STEAM_ID)).get(0).toString(); + this.name = ((List) apiInfo.get(Player.KEY_USERNAME)).get(0).toString(); + this.realName = ((List) apiInfo.getOrDefault(Player.KEY_REAL_NAME, "")).get(0).toString(); + this.avatar = ((List) apiInfo.getOrDefault(Player.KEY_AVATAR, "")).get(0).toString(); + this.timeCreated = (Integer)((List)apiInfo.get(KEY_TIME_CREATED)).get(0); + this.friends = new ArrayList<>(); } - public Date getDate() + public List getFriends() { - return date; + return friends; } - /** - * Returns a list of all the friends of a specific player - * - * @return - */ - public List fetchFriends() - { - return this.friends; - } /** * Getter for display name of player @@ -88,10 +98,32 @@ public class Player this.friends = friends; } + public String getRealName() + { + return this.realName; + } + + + public String getAvatar() + { + return this.avatar; + } + + + public Integer getTimeCreated() + { + return this.timeCreated; + } + @Override public String toString() { - return "Name: " + this.name + " id: " + this.id + " " + friends.size(); + return "Name: " + this.name + + " id: " + this.id + + " friend count " + friends.size() + + " avatar:" + this.avatar + + " real name " + this.realName + + " created " + this.timeCreated; } } \ No newline at end of file diff --git a/src/main/java/net/jrtechs/www/server/Server.java b/src/main/java/net/jrtechs/www/server/Server.java index cf2057c..e7193be 100644 --- a/src/main/java/net/jrtechs/www/server/Server.java +++ b/src/main/java/net/jrtechs/www/server/Server.java @@ -99,6 +99,11 @@ public class Server extends WebSocketServer .getAddress().getHostAddress()); } + @Override + public void onStart() { + + } + /** * Removes a client from the main list of clients diff --git a/src/main/java/net/jrtechs/www/webCrawler/SteamWebCrawler.java b/src/main/java/net/jrtechs/www/webCrawler/SteamWebCrawler.java index b708aec..67bda53 100644 --- a/src/main/java/net/jrtechs/www/webCrawler/SteamWebCrawler.java +++ b/src/main/java/net/jrtechs/www/webCrawler/SteamWebCrawler.java @@ -12,6 +12,7 @@ import java.util.*; * * @author Jeffery Russell 11-18-18 */ +@Deprecated public class SteamWebCrawler { /** Object used to limit the speed at which I access the steam @@ -73,7 +74,7 @@ public class SteamWebCrawler { winners.add(this.namelessQueue.remove()); } - List namedPlayers = connection.getFullPlayers(winners); + List namedPlayers = connection.getPlayers(winners); this.throttler.wait(1); downlaodQueue.addAll(namedPlayers); } @@ -98,7 +99,7 @@ public class SteamWebCrawler { if(fileIO.playerExists(s)) { - downlaodQueue.add(new Player("dummy", s)); + //downlaodQueue.add(new Player("dummy", s)); } else { @@ -150,7 +151,7 @@ public class SteamWebCrawler */ public void runSteamCrawlerBase(String baseID) { - downlaodQueue.add(new Player("jrtechs", baseID)); + //downlaodQueue.add(new Player("jrtechs", baseID)); runCrawler(); }