Browse Source

Updated the server to be able to make friends with friends graphs.

pull/8/head
jrtechs 6 years ago
parent
commit
5d722183f7
2 changed files with 72 additions and 16 deletions
  1. +14
    -9
      src/main/java/net/jrtechs/www/graphDB/SteamGraph.java
  2. +58
    -7
      src/main/java/net/jrtechs/www/server/Client.java

+ 14
- 9
src/main/java/net/jrtechs/www/graphDB/SteamGraph.java View File

@ -205,15 +205,20 @@ public class SteamGraph
String query = "g.V().hasLabel('player')" + String query = "g.V().hasLabel('player')" +
".has('id', '" + id + "')" + ".has('id', '" + id + "')" +
".both().valueMap()"; ".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()))
);
try
{
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()))
);
}
catch(Exception e)
{
e.printStackTrace();
}
return friends; return friends;
} }

+ 58
- 7
src/main/java/net/jrtechs/www/server/Client.java View File

@ -98,6 +98,11 @@ public class Client extends Thread
} }
/**
* sends a json object to the client
*
* @param request
*/
private void sendJSON(JSONObject request) private void sendJSON(JSONObject request)
{ {
this.client.send(request.toString()); this.client.send(request.toString());
@ -117,7 +122,7 @@ public class Client extends Thread
* *
* @param p * @param p
*/ */
private void sendPlayerToClient(Player p, int x, int y, int gen)
private void sendPlayerToClient(Player p, int x, int y, int gen, int multiplier)
{ {
if(gen == 1) if(gen == 1)
{ {
@ -132,7 +137,8 @@ public class Client extends Thread
for(Player friend: friends) for(Player friend: friends)
{ {
this.sendNodeAdd(friend, (int)(x + Math.cos(currentStep) * (300/gen)), (int)(y + Math.sin(currentStep) * (300/gen)));
this.sendNodeAdd(friend, (int)(x + Math.cos(currentStep) *
(multiplier/gen)), (int)(y + Math.sin(currentStep) * (multiplier/gen)));
this.sendEdgeAdd(p, friend); this.sendEdgeAdd(p, friend);
@ -142,15 +148,14 @@ public class Client extends Thread
/** /**
* Where the magic happens
* Generates a friends of friends graph for the client
*/ */
@Override
public void run()
private void friendsOfFriends()
{ {
Player b = this.graph.getPlayer(this.baseId); Player b = this.graph.getPlayer(this.baseId);
List<Player> friends = b.fetchFriends(); List<Player> friends = b.fetchFriends();
this.sendPlayerToClient(b, 300, 243, 1);
this.sendPlayerToClient(b, 300, 243, 1, 300);
double radianStep = Math.PI * 2 / friends.size(); double radianStep = Math.PI * 2 / friends.size();
@ -160,11 +165,57 @@ public class Client extends Thread
for(Player f : b.fetchFriends()) for(Player f : b.fetchFriends())
{ {
f = this.graph.getPlayer(f.getId()); f = this.graph.getPlayer(f.getId());
this.sendPlayerToClient(f, (int)(300 + Math.cos(currentStep) * 300), (int)(243 + Math.sin(currentStep) * 300) ,2);
this.sendPlayerToClient(f, (int)(300 + Math.cos(currentStep) * 300), (int)(243 + Math.sin(currentStep) * 300) ,2, 300);
currentStep += radianStep; currentStep += radianStep;
} }
this.client.close();
}
/**
* Generates the friends with friends graph for the client
*
* Displays all of the requested ids friends, then it only adds edges
* between players if they are both friends of yours.
*/
private void friendsWithFriends()
{
Player b = this.graph.getPlayer(this.baseId);
this.sendPlayerToClient(b, 600, 440, 1, 600);
for(Player f : b.fetchFriends()) //all my friends
{
f = this.graph.getPlayer(f.getId());
for(Player ff : f.fetchFriends()) // all my friends friends
{
for(Player f2 : b.fetchFriends()) // all my friends
{
if(f2.getId().equals(ff.getId()))
{
this.sendEdgeAdd(f, ff);
}
}
}
}
this.client.close(); this.client.close();
} }
/**
* Where the magic happens
*/
@Override
public void run()
{
if(this.type == 1)
{
friendsOfFriends();
}
else
{
friendsWithFriends();
}
}
} }

Loading…
Cancel
Save