PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/AtecConnectMain/server/CommandHandlers.js

https://gitlab.com/AtecStudios/AtecConnect
JavaScript | 345 lines | 248 code | 67 blank | 30 comment | 14 complexity | 38bc4e16b9ca71a509d5b71bdec6cd7e MD5 | raw file
  1. RegisterConnection = function (data, connection)
  2. {
  3. Fiber(function(){
  4. //Finds any API_KEYS that match what the TCP Packet sent
  5. var Akey = APIKeys.findOne({key: GetParameter(data,'API_KEY')});
  6. // Checks if it found a key by checking if Akey is null
  7. if(Akey){
  8. // Updates DB
  9. connection.APIKey = Akey;
  10. var mProjectID = GetParameter(data,'PROJECT_ID');
  11. if(mProjectID != "SEARCH_FAILED")
  12. {
  13. connection.Auth = true;
  14. connection.Project = mProjectID;
  15. connection.socket.write("RESPONSE_REGISTER_CONNECTION|DETAIL=API_KEY_INVALID;");
  16. LogEvent(EventType.EVENT, EventDetail.COMMAND_HANDLER,
  17. "Registered Connection Success", {PID: mProjectID, RAW: GetParameter(data,'PROJECT_ID'), DATA: data.toString()});
  18. }
  19. else{
  20. connection.socket.write("FAILURE|DETAIL=NO_PROJECT_ID");
  21. console.log("NO PROJECT ID!");
  22. LogEvent(EventType.ERROR, EventDetail.COMMAND_HANDLER,
  23. "Connection Registration Rejected (PROJECT_ID)", "");
  24. }
  25. }
  26. else{
  27. // Prints and Sends Invalid Key to console and client
  28. console.log("FAILURE|DETAIL=API_KEY_INVALID;");
  29. connection.socket.write("FAILURE|DETAIL=API_KEY_INVALID;");
  30. LogEvent(EventType.ERROR, EventDetail.COMMAND_HANDLER,
  31. "Connection Registration Rejected (API KEY)", "");
  32. }
  33. }).run();
  34. }
  35. ControllerConnected = function (data, connection){
  36. Fiber(function(){
  37. //Finds any API_KEYS that match what the TCP Packet sent
  38. // Checks if it found a key by checking if Akey is null
  39. if(connection.Auth){
  40. // Updates DB
  41. Projects.update(GetParameter(data,'PROJECT_ID'), {
  42. $inc: {controllers: 1}
  43. });
  44. var newController = {
  45. ip: GetParameter(data,"IP"),
  46. port: GetParameter(data,"PORT"),
  47. maxservers: GetParameter(data,"MAXSERVERS"),
  48. project: GetParameter(data, 'PROJECT_ID'),
  49. socket: connection.id,
  50. createdAt: new Date()
  51. };
  52. var id = Controllers.insert(newController);
  53. connection.Type = ConnectionType.CONTROLLER;
  54. connection.DatabaseID = id;
  55. console.log(id);
  56. connection.socket.write("SUCESS|ID=" + id + ";");
  57. LogEvent(EventType.EVENT, EventDetail.COMMAND_HANDLER,
  58. "Controller Connected", connection);
  59. }
  60. else{
  61. // Prints and Sends Invalid Key to console and client
  62. console.log("FAILURE|DETAIL=NOT_AUTH;");
  63. connection.socket.write("FAILURE|DETAIL=NOT_AUTH;");
  64. }
  65. }).run();
  66. }
  67. ControllerDisconnected = function(data, connection, timeout)
  68. {
  69. Fiber(function()
  70. {
  71. var GameServers = Servers.find({controller: connection.id});
  72. var GameServerAmount = GameServers.count()
  73. if(timeout)
  74. {
  75. Projects.update(connection.pid, {
  76. $inc: {controllers: -1}
  77. });
  78. Projects.update(connection.pid, {
  79. $inc: {servers: -GameServerAmount}
  80. });
  81. console.log(GameServerAmount);
  82. Controllers.remove(connection.dbid);
  83. Servers.remove({controller: connection.id});
  84. }
  85. else
  86. {
  87. var Akey = APIKeys.findOne({key: GetParameter(data,'API_KEY')});
  88. // Checks if it found a key by checking if Akey is null
  89. if(Akey)
  90. {
  91. // Updates DB
  92. Projects.update(GetParameter(data,'PROJECT_ID'), {
  93. $inc: {controllers: -1}
  94. });
  95. Projects.update(GetParameter(data,'PROJECT_ID'), {
  96. $inc: {servers: GameServerAmount}
  97. });
  98. Controllers.remove(connection.dbid);
  99. }
  100. else{
  101. // Prints and Sends Invalid Key to console and client
  102. console.log("INVALD KEY!");
  103. connection.socket.write("INVALID KEY!");
  104. }
  105. }
  106. //Finds any API_KEYS that match what the TCP Packet sent
  107. }).run();
  108. console.log("CONTROLLER DISCONNECTEd TO PROJECT: " + connection.pid);
  109. }
  110. GameConnected = function(data, connection)
  111. {
  112. Fiber(function()
  113. {
  114. //Finds any API_KEYS that match what the TCP Packet sent
  115. // Checks if it found a key by checking if Akey is null
  116. if(connection.Auth)
  117. {
  118. // Updates DB
  119. var newGame = {
  120. createdAt: new Date(),
  121. ping: 0,
  122. ip: connection.socket.remoteAddress
  123. };
  124. console.log(connection.Project);
  125. Projects.update({puid: connection.Project}, {
  126. $inc: {numGames: 1},
  127. $push: {games: newGame}
  128. });
  129. var id = Games.insert(newGame);
  130. connection.type = ConnectionType.GAME;
  131. connection.dbid = id;
  132. //socket.write(newController._id);
  133. console.log("TEST");
  134. connection.socket.write("RESPONSE_GAME_CONNECTED|ID=" + id + ";");
  135. }
  136. else{
  137. // Prints and Sends Invalid Key to console and client
  138. console.log("FAILURE|DETAIL=API_KEY_INVALID;");
  139. connection.socket.write("FAILURE|DETAIL=API_KEY_INVALID;");
  140. }
  141. }).run();
  142. console.log("CONTROLLER CONNECT TO PROJECT: " +
  143. GetParameter(data,'PROJECT_ID'));
  144. }
  145. GameDisconnected = function (data, connection, timeout)
  146. {
  147. Fiber(function()
  148. {
  149. //Finds any API_KEYS that match what the TCP Packet sent
  150. if(timeout)
  151. {
  152. Projects.update(connection.pid, {
  153. $inc: {numGames: -1}
  154. });
  155. }
  156. else
  157. {
  158. var Akey = APIKeys.findOne({key: GetParameter(data,'API_KEY')});
  159. // Checks if it found a key by checking if Akey is null
  160. if(Akey)
  161. {
  162. // Updates DB
  163. Projects.update(GetParameter(data,'PROJECT_ID'), {
  164. $inc: {games: -1}
  165. });
  166. Games.remove(connection.dbid);
  167. }
  168. else{
  169. // Prints and Sends Invalid Key to console and client
  170. console.log("INVALD KEY!");
  171. connection.socket.write("INVALID KEY!");
  172. }
  173. }
  174. //Finds any API_KEYS that match what the TCP Packet sent
  175. console.log("GAME DISCONNECTED TO PROJECT: " + connection.pid);
  176. }).run();
  177. }
  178. GetServers =function (data, connection)
  179. {
  180. Fiber(function()
  181. {
  182. //Finds any API_KEYS that match what the TCP Packet sent
  183. // Checks if it found a key by checking if Akey is null
  184. if(connection.Auth)
  185. {
  186. var SendString = "GET_SEVERS|servers=[";
  187. var servers = Projects.findOne({puid: connection.Project}).servers;
  188. if(servers)
  189. {
  190. for(var i=0;i<servers.length;i++)
  191. {
  192. var row = servers[i];
  193. console.log(row.ip)
  194. var ServerObject = "SERVER={";
  195. ServerObject += "PORT="+row.port+";";
  196. ServerObject += "IP="+row.ip+";";
  197. ServerObject +="};";
  198. SendString += ServerObject;
  199. }
  200. }
  201. SendString +="];";
  202. console.log("RETURNING: " + SendString);
  203. connection.socket.write(SendString);
  204. }
  205. else{
  206. // Prints and Sends Invalid Key to console and client
  207. console.log("FAILURE|DETAIL=API_KEY_INVALID;");
  208. socket.write("FAILURE|DETAIL=API_KEY_INVALID;");
  209. }
  210. }).run();
  211. console.log("CONTROLLER CONNECT TO PROJECT: " +
  212. GetParameter(data,'PROJECT_ID'));
  213. }
  214. RegisterServer = function (data, connection)
  215. {
  216. WebConsole(EventType.EVENT, EventDetail.COMMAND_HANDLER,
  217. "Server Attempting Registration", "Server Attempting Registration: SOCKET ID=" + connection.id );
  218. Fiber(function()
  219. {
  220. //Finds any API_KEYS that match what the TCP Packet sent
  221. var Key = APIKeys.findOne({key: GetParameter(data,'API_KEY')});
  222. // Checks if it found a key by checking if Akey is null
  223. if(Key)
  224. {
  225. Projects.update(GetParameter(data,'PROJECT_ID'), {
  226. $inc: {servers: 1}
  227. });
  228. var newServer = {
  229. ip: GetParameter(data, "IP"),
  230. port : GetParameter(data, "PORT"),
  231. controller: connection.id,
  232. createdAt: new Date()
  233. };
  234. var id = Servers.insert(newServer);
  235. WebConsole(EventType.EVENT, EventDetail.COMMAND_HANDLER,
  236. "Server Registered", "Server Registered: SOCKET ID=" + id );
  237. connection.socket.write("SUCESS|ID=" + id);
  238. }
  239. else{
  240. // Prints and Sends Invalid Key to console and client
  241. console.log("FAILURE|DETAIL=API_KEY_INVALID;");
  242. connection.socket.write("FAILURE|DETAIL=API_KEY_INVALID;");
  243. }
  244. }).run();
  245. }
  246. RegisterLocalHost = function(data,connection)
  247. {
  248. LogEvent(EventType.EVENT, EventDetail.COMMAND_HANDLER,
  249. "Registering LocalHost Server...", {PID: connection.Project, IP: connection.socket.remoteAddress, PORT: 7777});
  250. console.log("LOCAL HOST!!!!");
  251. Fiber(function()
  252. {
  253. // Checks if it found a key by checking if Akey is null
  254. if(connection.Auth)
  255. {
  256. var newServer = {
  257. ip: connection.socket.remoteAddress,
  258. port : GetParameter(data, "PORT"),
  259. controller: "LOCAL_HOST",
  260. createdAt: new Date()
  261. };
  262. Projects.update({puid: connection.Project}, {
  263. $inc: {numServers: 1},
  264. $push: {servers: newServer}
  265. });
  266. connection.socket.write("SUCESS|;");
  267. LogEvent(EventType.EVENT, EventDetail.COMMAND_HANDLER,
  268. "Registering LocalHost Server SUCCESS!", {PID: connection.Project, IP: connection.socket.remoteAddress, PORT: 7777});
  269. }
  270. else{
  271. // Prints and Sends Invalid Key to console and client
  272. LogEvent(EventType.ERROR, EventDetail.COMMAND_HANDLER,
  273. "Registered Local Host", {PID: connection.Project, IP: connection.socket.remoteAddress, PORT: 7777});
  274. connection.socket.write("FAILURE|DETAIL=API_KEY_INVALID;");
  275. console.log("No Auth");
  276. }
  277. }).run();
  278. }