@ -1,11 +1,12 @@
package net.jrtechs.www.graphDB ;
import net.jrtechs.www.Player ;
import net.jrtechs.www.server. Player ;
import net.jrtechs.www.SteamAPI.APIConnection ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
/ * *
* Does graph based operations with { @link Player }
@ -42,9 +43,36 @@ public class SteamGraph
private boolean alreadyInGraph ( String id )
{
String query = "g.V().hasLabel('player')" +
".has('id', '" + id + "')" ;
System . out . println ( query ) ;
return ( 1 = = con . queryGraph ( query ) . stream ( ) . count ( ) ) ;
".has('id', '" + id + "')" ;
/ / System . out . println ( query ) ;
return ( 1 < = con . queryGraph ( query ) . stream ( ) . count ( ) ) ;
}
/ * *
* Inserts a player vertex into the graph
*
* @param
* /
private void insertPlayerIntoGraph ( String id , String name )
{
try
{
if ( ! this . alreadyInGraph ( id ) )
{
String queryInsertPlayer = "g.addV('player')" +
".property('name', '" + name + "')" +
".property('crawled', '0')" +
".property('id', '" + id + "')" ;
System . out . println ( "inserting " + name + " into graph" ) ;
this . con . queryGraph ( queryInsertPlayer ) ;
}
}
catch ( Exception e )
{
e . printStackTrace ( ) ;
}
}
@ -56,16 +84,16 @@ public class SteamGraph
* @param p2
* @return
* /
private boolean edgeAlreadyInGraph ( Player p1 , Player p2 )
private boolean edgeAlreadyInGraph ( String p1 , String p2 )
{
try
{
String query = "g.V().hasLabel('player')" +
".has('id', '" + p1 . getId ( ) + "')" +
".has('id', '" + p1 + "')" +
".both()" +
".has('id', '" + p2 . getId ( ) + "')" ;
System . out . println ( query ) ;
return ( 1 = = con . queryGraph ( query ) . stream ( ) . count ( ) ) ;
".has('id', '" + p2 + "')" ;
/ / System . out . println ( query ) ;
return ( 1 < = con . queryGraph ( query ) . stream ( ) . count ( ) ) ;
}
catch ( Exception e )
{
@ -74,20 +102,6 @@ public class SteamGraph
}
/ * *
* Inserts a player vertex into the graph
*
* @param player
* /
private void insertSinglePlayer ( Player player )
{
String queryInsertPlayer = "g.addV('player')" +
".property('name', '" + player . getName ( ) + "')" +
".property('id', '" + player . getId ( ) + "')" ;
System . out . println ( queryInsertPlayer ) ;
this . con . queryGraph ( queryInsertPlayer ) ;
}
/ * *
* Inserts a edge between two players into the graph
@ -95,64 +109,69 @@ public class SteamGraph
* @param p1
* @param p2
* /
private void insertEdge ( Player p1 , Player p2 )
private void insertEdgeIntoGraph ( String p1 , String p2 )
{
String query = "g.V().hasLabel('player')" +
".has('id', '" + p1 . getId ( ) + "')" +
".as('p1')" +
"V().hasLabel('player')" +
".has('id', '" + p2 . getId ( ) + "')" +
".as('p2')" +
".addE('friends')" +
".from('p1').to('p2')" ;
System . out . println ( query ) ;
this . con . queryGraph ( query ) ;
try
{
if ( ! this . edgeAlreadyInGraph ( p1 , p2 ) )
{
String query = "g.V().hasLabel('player')" +
".has('id', '" + p1 + "')" +
".as('p1')" +
"V().hasLabel('player')" +
".has('id', '" + p2 + "')" +
".as('p2')" +
".addE('friends')" +
".from('p1').to('p2')" ;
/ / System . out . println ( query ) ;
this . con . queryGraph ( query ) ;
}
}
catch ( Exception e )
{
e . printStackTrace ( ) ;
}
}
/ * *
* Inserts a player and all of it ' s friends into
* the graph .
* determines if a player has been indexed for friends yet
*
* @param player
* @param id
* @return
* /
public void insertIntoGraph ( Player player )
private boolean playerAlreadyIndexed ( String id )
{
System . out . println ( player ) ;
if ( ! this . alreadyInGraph ( player . getId ( ) ) )
try
{
this . insertSinglePlayer ( player ) ;
}
String query = "g.V().hasLabel('player')" +
".has('id', '" + id + "')" +
".has('crawled', '0')" ;
for ( Player friend : player . fetchFriends ( api ) )
return ( 1 ! = con . queryGraph ( query ) . stream ( ) . count ( ) ) ;
}
catch ( Exception e )
{
if ( ! alreadyInGraph ( friend . getId ( ) ) )
{
insertSinglePlayer ( friend ) ;
}
if ( ! edgeAlreadyInGraph ( player , friend ) )
{
insertEdge ( player , friend ) ;
}
e . printStackTrace ( ) ;
}
return true ;
}
/ * *
* Recursive function for scraping the steam api
*
* @param player
* @param debth
* /
public void insertIntoGraph ( Player player , int debth )
private void updateCrawledStatus ( String id )
{
insertIntoGraph ( player ) ;
try
{
String query = "g.V().hasLabel('player')" +
".has('id', '" + id + "')" +
".property('crawled', '1')" ;
if ( debth > 0 )
this . con . queryGraph ( query ) ;
}
catch ( Exception e )
{
player . fetchFriends ( this . api )
. forEach ( f - > insertIntoGraph ( f , debth - 1 ) ) ;
e . printStackTrace ( ) ;
}
}
@ -188,39 +207,76 @@ public class SteamGraph
".both().valueMap()" ;
this . con . queryGraph ( query ) . stream ( ) . forEach ( r - >
friends . add ( new Player (
( ( ArrayList ) ( ( ( HashMap < String , Object > ) ( r . getObject ( ) ) )
. get ( "name" ) ) ) . get ( 0 ) . toString ( ) ,
( ( ArrayList ) ( ( ( HashMap < String , Object > ) ( r . getObject ( ) ) )
. get ( "id" ) ) ) . get ( 0 ) . toString ( ) ) )
friends . add ( new Player (
( ( ArrayList ) ( ( ( HashMap < String , Object > ) ( r . getObject ( ) ) )
. get ( "name" ) ) ) . get ( 0 ) . toString ( ) ,
( ( ArrayList ) ( ( ( HashMap < String , Object > ) ( r . getObject ( ) ) )
. get ( "id" ) ) ) . get ( 0 ) . toString ( ) ) )
) ;
return friends ;
}
/ * *
* tells api to get this dude ' s friends list
*
* @param id
* /
private void indexPersonFriends ( String id )
{
System . out . println ( "indexing " + this . getNameFromGraph ( 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 ) ;
for ( String key : names . keySet ( ) )
{
this . insertPlayerIntoGraph ( key , names . get ( key ) ) ;
}
friendsIds . forEach ( s - > this . insertEdgeIntoGraph ( id , s ) ) ;
this . updateCrawledStatus ( id ) ;
}
/ * *
* Fetches a player from the graph
* Fetches a player from the graph with all of its friends
*
* @param id
* @return
* /
public Player getPlayerInformation ( String id )
public Player getPlayer ( String id )
{
Player p ;
if ( ! this . alreadyInGraph ( id ) )
if ( this . alreadyInGraph ( id ) ) / / yay
{
p = new Player ( id ) ;
this . insertIntoGraph ( p ) ;
p = new Player ( this . getNameFromGraph ( id ) , id ) ;
if ( ! this . playerAlreadyIndexed ( id ) ) / / must index the person
{
this . indexPersonFriends ( id ) ;
}
p . setFriends ( this . getFriendsFromGraph ( id ) ) ;
}
else
else / / smh , shouldn ' t happen frequently
{
p = new Player ( this . getNameFromGraph ( id ) , id ) ;
System . out . println ( "brand spanking new request " + id ) ;
String name = this . api . getPlayerName ( id ) ;
this . insertPlayerIntoGraph ( id , name ) ;
return this . getPlayer ( id ) ;
}
p . setFriends ( this . getFriendsFromGraph ( id ) ) ;
return p ;
}
@ -231,12 +287,12 @@ public class SteamGraph
* /
public static void main ( String [ ] args )
{
SteamGraph graph = new SteamGraph ( ) ;
Player base = new Player ( args [ 0 ] ) ;
int debth = Integer . valueOf ( args [ 1 ] ) ;
graph . insertIntoGraph ( base , debth ) ;
/ / SteamGraph graph = new SteamGraph ( ) ;
/ /
/ / Player base = graph . get Player( args [ 0 ] ) ;
/ /
/ / int debth = Integer . valueOf ( args [ 1 ] ) ;
/ /
/ / graph . insertIntoGraph ( base , debth ) ;
}
}
}