Graph database Analysis of the Steam Network
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
6.5 KiB

  1. package net.jrtechs.www.server;
  2. import net.jrtechs.www.graphDB.SteamGraph;
  3. import org.java_websocket.WebSocket;
  4. import org.json.JSONObject;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. /**
  8. * Client thread which gets graph information from
  9. * the graphDB and sends it to the client so that
  10. * they can render it in their web browser.
  11. *
  12. * @author Jeffery Russell 5-27-18
  13. */
  14. public class Client extends Thread
  15. {
  16. /** Web connection to the client */
  17. private WebSocket client;
  18. /** Graph interface to fetch data */
  19. private SteamGraph graph;
  20. /** base id to look at */
  21. private String baseId;
  22. /** JSONObjects to send the client */
  23. private List<JSONObject> queue;
  24. private int type;
  25. /**
  26. * Initializes the client with a steam graph and
  27. * web socket information.
  28. * @param client
  29. */
  30. public Client(WebSocket client, String id, int type)
  31. {
  32. this.client = client;
  33. this.graph = new SteamGraph();
  34. this.type = type;
  35. this.baseId = id;
  36. this.queue = new ArrayList<>();
  37. }
  38. /**
  39. * returns the web socket object
  40. *
  41. * @return
  42. */
  43. public WebSocket getSocket()
  44. {
  45. return this.client;
  46. }
  47. /**
  48. * Sends the client the request to add a new node to their
  49. * graph.
  50. *
  51. * @param p
  52. */
  53. private void sendNodeAdd(Player p, int x, int y, int size)
  54. {
  55. JSONObject request = new JSONObject();
  56. request.put("action", 1);
  57. request.put("id", p.getId());
  58. request.put("name", p.getName());
  59. request.put("size", size);
  60. request.put("x", x);
  61. request.put("y", y);
  62. this.sendJSON(request);
  63. }
  64. /**
  65. * Sends the client to request to connect two nodes
  66. * via an edge.
  67. *
  68. * @param p1
  69. * @param p2
  70. */
  71. private void sendEdgeAdd(Player p1, Player p2)
  72. {
  73. JSONObject request = new JSONObject();
  74. request.put("action", 2);
  75. request.put("id", p1.getId() + p2.getId());
  76. request.put("p1", p1.getId());
  77. request.put("p2", p2.getId());
  78. this.sendJSON(request);
  79. }
  80. /**
  81. * Tells the tells the js on the client side to start
  82. * the force applied to the graph
  83. */
  84. private void sendFinished()
  85. {
  86. JSONObject request = new JSONObject();
  87. request.put("action", 3);
  88. this.sendJSON(request);
  89. }
  90. /**
  91. * sends a json object to the client
  92. *
  93. * @param request
  94. */
  95. private void sendJSON(JSONObject request)
  96. {
  97. this.queue.add(request);
  98. }
  99. /**
  100. * Sends the next object to the client
  101. */
  102. public void sendNextRequest()
  103. {
  104. while(this.queue.isEmpty())
  105. {
  106. try
  107. {
  108. Thread.sleep(500);
  109. }
  110. catch (Exception e)
  111. {
  112. e.printStackTrace();
  113. }
  114. }
  115. JSONObject send =queue.remove(0);
  116. this.client.send(send.toString());
  117. if(send.getInt("action") == 3)
  118. {
  119. this.client.close();
  120. }
  121. }
  122. /**
  123. * Sends an entire player and all of their friends to the client
  124. *
  125. * @param p
  126. */
  127. private void sendPlayerToClient(Player p, int x, int y,
  128. int gen, int multiplier)
  129. {
  130. if(gen == 1)
  131. {
  132. sendNodeAdd(p, x, y, 150);
  133. }
  134. List<Player> friends = p.fetchFriends();
  135. double radianStep = Math.PI * 2 / friends.size();
  136. double currentStep = 0;
  137. for(Player friend: friends)
  138. {
  139. if(gen == 1)
  140. {
  141. this.sendNodeAdd(friend, (int)(x + Math.cos(currentStep) *
  142. (multiplier/gen)), (int)(y + Math.sin(currentStep) *
  143. (multiplier/gen)), 150);
  144. }
  145. else
  146. {
  147. this.sendNodeAdd(friend, (int)(x + Math.cos(currentStep) *
  148. (multiplier/gen)), (int)(y + Math.sin(currentStep) *
  149. (multiplier/gen)), 30);
  150. }
  151. this.sendEdgeAdd(p, friend);
  152. currentStep += radianStep;
  153. }
  154. }
  155. /**
  156. * Generates a friends of friends graph for the client
  157. */
  158. private void friendsOfFriends()
  159. {
  160. Player b = this.graph.getPlayer(this.baseId);
  161. if(b == null)
  162. {
  163. this.sendPlayerNotFound();
  164. }
  165. else
  166. {
  167. List<Player> friends = b.fetchFriends();
  168. this.sendPlayerToClient(b, 300, 243, 1, 300);
  169. double radianStep = Math.PI * 2 / friends.size();
  170. double currentStep = 0;
  171. for(Player f : b.fetchFriends())
  172. {
  173. f = this.graph.getPlayer(f.getId());
  174. this.sendPlayerToClient(f, (int)(300 + Math.cos(currentStep) * 300),
  175. (int)(243 + Math.sin(currentStep) * 300) ,2, 300);
  176. currentStep += radianStep;
  177. }
  178. this.sendFinished();
  179. }
  180. }
  181. /**
  182. * Generates the friends with friends graph for the client
  183. *
  184. * Displays all of the requested ids friends, then it only adds edges
  185. * between players if they are both friends of yours.
  186. */
  187. private void friendsWithFriends()
  188. {
  189. Player b = this.graph.getPlayer(this.baseId);
  190. if(b == null)
  191. {
  192. this.sendPlayerNotFound();
  193. }
  194. else
  195. {
  196. this.sendPlayerToClient(b, 600, 440, 1, 600);
  197. for(Player f : b.fetchFriends()) //all my friends
  198. {
  199. f = this.graph.getPlayer(f.getId());
  200. for(Player ff : f.fetchFriends()) // all my friends friends
  201. {
  202. for(Player f2 : b.fetchFriends()) // all my friends
  203. {
  204. if(f2.getId().equals(ff.getId()))
  205. {
  206. this.sendEdgeAdd(f, ff);
  207. }
  208. }
  209. }
  210. }
  211. this.sendFinished();
  212. }
  213. }
  214. /**
  215. * Tells the client that the steam id provided was invalid
  216. */
  217. private void sendPlayerNotFound()
  218. {
  219. JSONObject request = new JSONObject();
  220. request.put("action", -1);
  221. this.sendJSON(request);
  222. }
  223. /**
  224. * Where the magic happens
  225. */
  226. @Override
  227. public void run()
  228. {
  229. if(this.type == 1)
  230. {
  231. friendsOfFriends();
  232. }
  233. else
  234. {
  235. friendsWithFriends();
  236. }
  237. }
  238. }