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.

287 lines
6.5 KiB

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