|
|
@ -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<Player> getFriendsFromGraph(String id) |
|
|
|
{ |
|
|
|
System.out.println("fetching friends from graph"); |
|
|
|
List<Player> 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<Player>() |
|
|
|
{{ |
|
|
|
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<String> friendsIds = this.api.getFriends(id); |
|
|
|
|
|
|
|
//find ones not in database |
|
|
|
List<String> notInDatabase = new ArrayList<>(); |
|
|
|
for(String fid : friendsIds) |
|
|
|
{ |
|
|
|
if(!this.alreadyInGraph(fid)) |
|
|
|
{ |
|
|
|
notInDatabase.add(fid); |
|
|
|
} |
|
|
|
} |
|
|
|
Map<String, String> names = this.api.getNames(notInDatabase); |
|
|
|
List<String> 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(); |
|
|
|
// |
|
|
|