game_machine 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (273) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/Gemfile.lock +72 -0
  4. data/Rakefile +38 -0
  5. data/bin/game_machine +79 -0
  6. data/config/cluster.conf +65 -0
  7. data/config/config.example.yml +93 -0
  8. data/config/game_messages.proto +45 -0
  9. data/config/messages.proto +339 -0
  10. data/config/regions.example.yml +9 -0
  11. data/config/standalone.conf +36 -0
  12. data/db/do_not_delete +0 -0
  13. data/game_machine.gemspec +38 -0
  14. data/games/example/boot.rb +6 -0
  15. data/games/example/data/game_data.yml +13 -0
  16. data/games/example/lib/aggressive_npc.rb +176 -0
  17. data/games/example/lib/authentication_handler.rb +69 -0
  18. data/games/example/lib/chatbot.rb +61 -0
  19. data/games/example/lib/combat_controller.rb +145 -0
  20. data/games/example/lib/example_controller.rb +21 -0
  21. data/games/example/lib/game.rb +85 -0
  22. data/games/example/lib/models/attack.rb +9 -0
  23. data/games/example/lib/models/combat_update.rb +11 -0
  24. data/games/example/lib/models/player_command.rb +7 -0
  25. data/games/example/lib/models/user.rb +11 -0
  26. data/games/example/lib/models/vitals.rb +17 -0
  27. data/games/example/lib/npc.rb +111 -0
  28. data/games/example/lib/npc_group.rb +42 -0
  29. data/games/example/lib/npc_movement.rb +116 -0
  30. data/games/example/lib/player_manager.rb +58 -0
  31. data/games/example/lib/player_register.rb +80 -0
  32. data/games/example/lib/tracking_handler.rb +17 -0
  33. data/games/example/lib/zone_manager.rb +57 -0
  34. data/games/preload.rb +8 -0
  35. data/integration_tests/basic_spec.rb +68 -0
  36. data/integration_tests/bot_spec.rb +18 -0
  37. data/integration_tests/chat_spec.rb +45 -0
  38. data/integration_tests/distributed_spec.rb +34 -0
  39. data/integration_tests/entity_tracking_spec.rb +48 -0
  40. data/integration_tests/mono_spec.rb +16 -0
  41. data/integration_tests/objectdb_spec.rb +55 -0
  42. data/integration_tests/tcp_client_spec.rb +71 -0
  43. data/integration_tests/udp_client_spec.rb +61 -0
  44. data/integration_tests/udp_spec.rb +20 -0
  45. data/integration_tests/udt_client_spec.rb +23 -0
  46. data/integration_tests/udt_spec.rb +31 -0
  47. data/java/.gitignore +1 -0
  48. data/java/build.gradle +93 -0
  49. data/java/component.erb +396 -0
  50. data/java/gradle.properties +6 -0
  51. data/java/gradle/wrapper/gradle-wrapper.jar +0 -0
  52. data/java/gradle/wrapper/gradle-wrapper.properties +6 -0
  53. data/java/gradlew +164 -0
  54. data/java/gradlew.bat +90 -0
  55. data/java/local_lib/protostuff-compiler-1.0.7-jarjar.jar +0 -0
  56. data/java/settings.gradle +2 -0
  57. data/java/src/main/java/com/game_machine/core/ActorFactory.java +25 -0
  58. data/java/src/main/java/com/game_machine/core/ActorUtil.java +39 -0
  59. data/java/src/main/java/com/game_machine/core/CommandProxy.java +9 -0
  60. data/java/src/main/java/com/game_machine/core/EntitySerializer.java +66 -0
  61. data/java/src/main/java/com/game_machine/core/EventStreamHandler.java +43 -0
  62. data/java/src/main/java/com/game_machine/core/GameMachineLoader.java +25 -0
  63. data/java/src/main/java/com/game_machine/core/Grid.java +195 -0
  64. data/java/src/main/java/com/game_machine/core/GridValue.java +30 -0
  65. data/java/src/main/java/com/game_machine/core/IActorFactory.java +7 -0
  66. data/java/src/main/java/com/game_machine/core/NetMessage.java +28 -0
  67. data/java/src/main/java/com/game_machine/core/UdpServer.java +97 -0
  68. data/java/src/main/java/com/game_machine/core/UdpServerHandler.java +90 -0
  69. data/java/src/main/resources/game_machine.java.stg +738 -0
  70. data/java/src/main/resources/logback.xml +14 -0
  71. data/java/src/main/resources/logging.properties +3 -0
  72. data/java/src/main/resources/protostuff.properties +7 -0
  73. data/lib/game_machine.rb +85 -0
  74. data/lib/game_machine/actor.rb +7 -0
  75. data/lib/game_machine/actor/base.rb +184 -0
  76. data/lib/game_machine/actor/builder.rb +108 -0
  77. data/lib/game_machine/actor/development.rb +31 -0
  78. data/lib/game_machine/actor/factory.rb +35 -0
  79. data/lib/game_machine/actor/mono_actor.rb +89 -0
  80. data/lib/game_machine/actor/ref.rb +81 -0
  81. data/lib/game_machine/actor/reloadable.rb +98 -0
  82. data/lib/game_machine/actor/system.rb +32 -0
  83. data/lib/game_machine/akka.rb +98 -0
  84. data/lib/game_machine/app_config.rb +49 -0
  85. data/lib/game_machine/application.rb +181 -0
  86. data/lib/game_machine/auth_handlers/base.rb +21 -0
  87. data/lib/game_machine/auth_handlers/public.rb +34 -0
  88. data/lib/game_machine/bot/chat.rb +66 -0
  89. data/lib/game_machine/bot/client.rb +54 -0
  90. data/lib/game_machine/client_manager.rb +204 -0
  91. data/lib/game_machine/clients.rb +4 -0
  92. data/lib/game_machine/clients/client.rb +45 -0
  93. data/lib/game_machine/clients/tcp_client.rb +25 -0
  94. data/lib/game_machine/clients/test_client.rb +151 -0
  95. data/lib/game_machine/clients/udp_client.rb +25 -0
  96. data/lib/game_machine/clients/udt_client.rb +34 -0
  97. data/lib/game_machine/cluster_monitor.rb +115 -0
  98. data/lib/game_machine/commands.rb +23 -0
  99. data/lib/game_machine/commands/base.rb +21 -0
  100. data/lib/game_machine/commands/chat_commands.rb +88 -0
  101. data/lib/game_machine/commands/datastore_commands.rb +60 -0
  102. data/lib/game_machine/commands/grid_commands.rb +35 -0
  103. data/lib/game_machine/commands/message_helper.rb +25 -0
  104. data/lib/game_machine/commands/misc_commands.rb +29 -0
  105. data/lib/game_machine/commands/navigation_commands.rb +24 -0
  106. data/lib/game_machine/commands/player_commands.rb +28 -0
  107. data/lib/game_machine/commands/proxy.rb +16 -0
  108. data/lib/game_machine/console.rb +3 -0
  109. data/lib/game_machine/console/build.rb +74 -0
  110. data/lib/game_machine/console/install.rb +92 -0
  111. data/lib/game_machine/console/server.rb +120 -0
  112. data/lib/game_machine/data_store.rb +52 -0
  113. data/lib/game_machine/data_stores/couchbase.rb +18 -0
  114. data/lib/game_machine/data_stores/mapdb.rb +49 -0
  115. data/lib/game_machine/data_stores/memory.rb +35 -0
  116. data/lib/game_machine/data_stores/redis.rb +46 -0
  117. data/lib/game_machine/endpoints.rb +6 -0
  118. data/lib/game_machine/endpoints/mono_gateway.rb +87 -0
  119. data/lib/game_machine/endpoints/tcp.rb +51 -0
  120. data/lib/game_machine/endpoints/tcp_handler.rb +75 -0
  121. data/lib/game_machine/endpoints/udp.rb +88 -0
  122. data/lib/game_machine/endpoints/udp_incoming.rb +113 -0
  123. data/lib/game_machine/endpoints/udp_outgoing.rb +46 -0
  124. data/lib/game_machine/game_loader.rb +46 -0
  125. data/lib/game_machine/game_systems.rb +14 -0
  126. data/lib/game_machine/game_systems/agents/controller.rb +118 -0
  127. data/lib/game_machine/game_systems/chat.rb +256 -0
  128. data/lib/game_machine/game_systems/chat_manager.rb +108 -0
  129. data/lib/game_machine/game_systems/chat_topic.rb +36 -0
  130. data/lib/game_machine/game_systems/devnull.rb +13 -0
  131. data/lib/game_machine/game_systems/entity_loader.rb +12 -0
  132. data/lib/game_machine/game_systems/entity_tracking.rb +133 -0
  133. data/lib/game_machine/game_systems/local_echo.rb +16 -0
  134. data/lib/game_machine/game_systems/objectdb_proxy.rb +61 -0
  135. data/lib/game_machine/game_systems/private_chat.rb +20 -0
  136. data/lib/game_machine/game_systems/region_manager.rb +91 -0
  137. data/lib/game_machine/game_systems/region_service.rb +94 -0
  138. data/lib/game_machine/game_systems/region_settings.rb +13 -0
  139. data/lib/game_machine/game_systems/remote_echo.rb +14 -0
  140. data/lib/game_machine/game_systems/stress_test.rb +21 -0
  141. data/lib/game_machine/grid.rb +60 -0
  142. data/lib/game_machine/grid_replicator.rb +31 -0
  143. data/lib/game_machine/handlers/authentication.rb +55 -0
  144. data/lib/game_machine/handlers/game.rb +63 -0
  145. data/lib/game_machine/handlers/request.rb +80 -0
  146. data/lib/game_machine/hashring.rb +48 -0
  147. data/lib/game_machine/helpers/game_message.rb +159 -0
  148. data/lib/game_machine/helpers/state_machine.rb +29 -0
  149. data/lib/game_machine/java_lib.rb +51 -0
  150. data/lib/game_machine/logger.rb +39 -0
  151. data/lib/game_machine/message_buffer.rb +58 -0
  152. data/lib/game_machine/message_queue.rb +63 -0
  153. data/lib/game_machine/model.rb +125 -0
  154. data/lib/game_machine/models.rb +3 -0
  155. data/lib/game_machine/models/player_status_update.rb +8 -0
  156. data/lib/game_machine/models/region.rb +9 -0
  157. data/lib/game_machine/mono_server.rb +20 -0
  158. data/lib/game_machine/navigation.rb +4 -0
  159. data/lib/game_machine/navigation/detour.rb +20 -0
  160. data/lib/game_machine/navigation/detour_navmesh.rb +53 -0
  161. data/lib/game_machine/navigation/detour_path.rb +53 -0
  162. data/lib/game_machine/navigation/path.rb +31 -0
  163. data/lib/game_machine/object_db.rb +67 -0
  164. data/lib/game_machine/protobuf.rb +6 -0
  165. data/lib/game_machine/protobuf/game_messages.rb +24 -0
  166. data/lib/game_machine/protobuf/generate.rb +113 -0
  167. data/lib/game_machine/protobuf_extensions/entity_helper.rb +11 -0
  168. data/lib/game_machine/reloadable_monitor.rb +26 -0
  169. data/lib/game_machine/restart_watcher.rb +17 -0
  170. data/lib/game_machine/ruby_extensions/nilclass.rb +10 -0
  171. data/lib/game_machine/ruby_extensions/string.rb +17 -0
  172. data/lib/game_machine/scheduler.rb +23 -0
  173. data/lib/game_machine/securerandom.rb +6 -0
  174. data/lib/game_machine/settings.rb +11 -0
  175. data/lib/game_machine/system_monitor.rb +19 -0
  176. data/lib/game_machine/system_stats.rb +24 -0
  177. data/lib/game_machine/uniqueid.rb +23 -0
  178. data/lib/game_machine/vector.rb +95 -0
  179. data/lib/game_machine/version.rb +3 -0
  180. data/lib/game_machine/write_behind_cache.rb +164 -0
  181. data/mono/bin/csharp/common.xslt +109 -0
  182. data/mono/bin/csharp/csharp.xslt +628 -0
  183. data/mono/bin/csharp/descriptor.proto +533 -0
  184. data/mono/bin/csharp/protobuf-net.dll +0 -0
  185. data/mono/bin/csharp/protobuf-net.pdb +0 -0
  186. data/mono/bin/csharp/protobuf-net.xml +2879 -0
  187. data/mono/bin/csharp/protogen.exe.config +3 -0
  188. data/mono/bin/csharp/protogen.pdb +0 -0
  189. data/mono/bin/csharp/protogen_csharp.exe +0 -0
  190. data/mono/bin/csharp/vb.xslt +745 -0
  191. data/mono/bin/csharp/xml.xslt +26 -0
  192. data/mono/server/Makefile +6 -0
  193. data/mono/server/NLog.config +12 -0
  194. data/mono/server/NLog.dll +0 -0
  195. data/mono/server/actor.cs +37 -0
  196. data/mono/server/build.bat +3 -0
  197. data/mono/server/cscompmgd.dll +0 -0
  198. data/mono/server/iactor.cs +11 -0
  199. data/mono/server/message_router.cs +67 -0
  200. data/mono/server/message_util.cs +29 -0
  201. data/mono/server/messages.cs +1888 -0
  202. data/mono/server/protobuf-net.dll +0 -0
  203. data/mono/server/proxy_client.cs +73 -0
  204. data/mono/server/proxy_server.cs +30 -0
  205. data/mono/server/test_actor.cs +33 -0
  206. data/pathfinding/bin/premake4 +0 -0
  207. data/pathfinding/include/mesh_loader.h +28 -0
  208. data/pathfinding/include/pathfind.h +167 -0
  209. data/pathfinding/main.cpp +39 -0
  210. data/pathfinding/mesh_loader.cpp +108 -0
  211. data/pathfinding/pathfind.cpp +174 -0
  212. data/pathfinding/pathfinder.cs +66 -0
  213. data/pathfinding/premake4.lua +109 -0
  214. data/script/server.sh +109 -0
  215. data/script/watch.sh +11 -0
  216. data/spec/actor/actor_spec.rb +73 -0
  217. data/spec/actor/builder_spec.rb +56 -0
  218. data/spec/actor/ref_spec.rb +83 -0
  219. data/spec/application_spec.rb +7 -0
  220. data/spec/client_manager_spec.rb +171 -0
  221. data/spec/commands/chat_commands_spec.rb +38 -0
  222. data/spec/commands/datastore_commands_spec.rb +91 -0
  223. data/spec/commands/grid_commands_spec.rb +37 -0
  224. data/spec/commands/navigation_commands_spec.rb +51 -0
  225. data/spec/commands/player_commands_spec.rb +48 -0
  226. data/spec/commands_spec.rb +38 -0
  227. data/spec/data_stores/mapdb_spec.rb +46 -0
  228. data/spec/data_stores/redis_spec.rb +44 -0
  229. data/spec/game_systems/agents/controller_spec.rb +84 -0
  230. data/spec/game_systems/agents/test_agent.rb +10 -0
  231. data/spec/game_systems/agents/test_agent_config.rb +29 -0
  232. data/spec/game_systems/chat_manager_spec.rb +66 -0
  233. data/spec/game_systems/chat_spec.rb +187 -0
  234. data/spec/game_systems/entity_tracking_spec.rb +64 -0
  235. data/spec/game_systems/region_manager_spec.rb +138 -0
  236. data/spec/grid_spec.rb +37 -0
  237. data/spec/handlers/authentication_spec.rb +36 -0
  238. data/spec/handlers/game_spec.rb +49 -0
  239. data/spec/handlers/request_spec.rb +65 -0
  240. data/spec/hashring_spec.rb +59 -0
  241. data/spec/integration_helper.rb +120 -0
  242. data/spec/java_grid_spec.rb +89 -0
  243. data/spec/message_buffer_spec.rb +67 -0
  244. data/spec/message_expectations.rb +47 -0
  245. data/spec/message_queue_spec.rb +23 -0
  246. data/spec/misc_spec.rb +71 -0
  247. data/spec/model_spec.rb +103 -0
  248. data/spec/mono_spec.rb +36 -0
  249. data/spec/mono_test.rb +18 -0
  250. data/spec/navigation/detour_navmesh_spec.rb +34 -0
  251. data/spec/navigation/detour_path_spec.rb +25 -0
  252. data/spec/spec_helper.rb +40 -0
  253. data/spec/udp_server_spec.rb +10 -0
  254. data/spec/write_behind_cache_spec.rb +109 -0
  255. data/web/app.rb +131 -0
  256. data/web/config/trinidad.yml +4 -0
  257. data/web/controllers/auth_controller.rb +19 -0
  258. data/web/controllers/base_controller.rb +16 -0
  259. data/web/controllers/index_controller.rb +7 -0
  260. data/web/controllers/log_controller.rb +47 -0
  261. data/web/controllers/messages_controller.rb +59 -0
  262. data/web/controllers/player_register_controller.rb +15 -0
  263. data/web/log/development.log +1339 -0
  264. data/web/tmp/restart.txt +0 -0
  265. data/web/views/game_messages.haml +45 -0
  266. data/web/views/index.haml +6 -0
  267. data/web/views/layout.haml +41 -0
  268. data/web/views/logs.haml +32 -0
  269. data/web/views/player_register.haml +22 -0
  270. data/web/views/player_registered.haml +2 -0
  271. data/web/views/register_layout.haml +22 -0
  272. data/web/views/restart.haml +35 -0
  273. metadata +576 -0
@@ -0,0 +1,25 @@
1
+ package com.game_machine.core;
2
+
3
+ import java.util.logging.Logger;
4
+
5
+ import akka.actor.ActorSystem;
6
+ import akka.actor.Props;
7
+
8
+ public class GameMachineLoader {
9
+
10
+
11
+
12
+ private static final Logger log = Logger.getLogger(GameMachineLoader.class.getName());
13
+ private static ActorSystem actorSystem;
14
+
15
+ public static ActorSystem getActorSystem() {
16
+ return actorSystem;
17
+ }
18
+
19
+ public void run(ActorSystem newActorSystem, String gameHandler) {
20
+ Thread.currentThread().setName("game-machine");
21
+ actorSystem = newActorSystem;
22
+ actorSystem.actorOf(Props.create(EventStreamHandler.class), EventStreamHandler.class.getSimpleName());
23
+ }
24
+
25
+ }
@@ -0,0 +1,195 @@
1
+ package com.game_machine.core;
2
+
3
+ import java.util.ArrayList;
4
+ import java.util.HashSet;
5
+ import java.util.Set;
6
+ import java.util.concurrent.ConcurrentHashMap;
7
+
8
+ public class Grid {
9
+
10
+ private float max;
11
+ private int cellSize = 0;
12
+ private float convFactor;
13
+ private int width;
14
+ private int cellCount;
15
+
16
+ public ConcurrentHashMap<String, GridValue> deltaIndex = new ConcurrentHashMap<String, GridValue>();
17
+ public ConcurrentHashMap<String, GridValue> objectIndex = new ConcurrentHashMap<String, GridValue>();
18
+ public ConcurrentHashMap<Integer, ConcurrentHashMap<String, GridValue>> cells = new ConcurrentHashMap<Integer, ConcurrentHashMap<String, GridValue>>();
19
+ public ConcurrentHashMap<Integer, Set<Integer>> cellsCache = new ConcurrentHashMap<Integer, Set<Integer>>();
20
+
21
+ public Grid(int max, int cellSize) {
22
+ this.max = max;
23
+ this.cellSize = cellSize;
24
+ this.convFactor = 1.0f / this.cellSize;
25
+ this.width = (int) (this.max / this.cellSize);
26
+ this.cellCount = this.width * this.width;
27
+ }
28
+
29
+ public int getWidth() {
30
+ return this.width;
31
+ }
32
+
33
+ public int getCellCount() {
34
+ return this.cellCount;
35
+ }
36
+
37
+ public Set<Integer> cellsWithinRadius(float x, float y) {
38
+ int cellHash = hash(x, y);
39
+ return cellsWithinRadius(cellHash, x, y);
40
+ }
41
+
42
+ public Set<Integer> cellsWithinRadius(int cellHash, float x, float y) {
43
+ int key = cellHash;
44
+ Set<Integer> cells = cellsCache.get(key);
45
+ if (cells != null) {
46
+ return cells;
47
+ }
48
+ cells = new HashSet<Integer>();
49
+
50
+ int offset = this.cellSize;
51
+
52
+ int startX = (int) (x - offset);
53
+ int startY = (int) (y - offset);
54
+ int endX = (int) (x + offset);
55
+ int endY = (int) (y + offset);
56
+
57
+ for (int rowNum = startX; rowNum <= endX; rowNum += this.cellSize) {
58
+ for (int colNum = startY; colNum <= endY; colNum += this.cellSize) {
59
+ if (rowNum >= 0 && colNum >= 0) {
60
+ cells.add(hash(rowNum, colNum));
61
+ }
62
+ }
63
+ }
64
+ cellsCache.put(key, cells);
65
+ return cells;
66
+ }
67
+
68
+ public ArrayList<GridValue> neighbors(float x, float y, String entityType) {
69
+ int myCell = hash(x, y);
70
+ return neighbors(myCell, x, y, entityType);
71
+ }
72
+
73
+ public ArrayList<GridValue> neighbors(int myCell, float x, float y, String entityType) {
74
+ ArrayList<GridValue> result;
75
+
76
+ GridValue[] gridValues;
77
+ result = new ArrayList<GridValue>();
78
+ Set<Integer> cells = cellsWithinRadius(myCell, x, y);
79
+ for (int cell : cells) {
80
+ gridValues = gridValuesInCell(cell);
81
+ if (gridValues != null) {
82
+ for (GridValue gridValue : gridValues) {
83
+ if (gridValue != null) {
84
+ if (entityType == null) {
85
+ result.add(gridValue);
86
+ } else if (gridValue.entityType.equals(entityType)) {
87
+ result.add(gridValue);
88
+ }
89
+ }
90
+ }
91
+ }
92
+ }
93
+ return result;
94
+ }
95
+
96
+ public GridValue[] gridValuesInCell(int cell) {
97
+ ConcurrentHashMap<String, GridValue> cellGridValues = cells.get(cell);
98
+
99
+ if (cellGridValues != null) {
100
+ GridValue[] a = new GridValue[cellGridValues.size()];
101
+ cellGridValues.values().toArray(a);
102
+ return a;
103
+ // return cellGridValues.values();
104
+ } else {
105
+ return null;
106
+ }
107
+ }
108
+
109
+ public void updateFromDelta(GridValue[] gridValues) {
110
+ for (GridValue gridValue : gridValues) {
111
+ if (gridValue != null) {
112
+ objectIndex.put(gridValue.id, gridValue);
113
+ }
114
+ }
115
+ }
116
+
117
+ public GridValue[] currentDelta() {
118
+ GridValue[] a = new GridValue[deltaIndex.size()];
119
+ deltaIndex.values().toArray(a);
120
+ deltaIndex.clear();
121
+ return a;
122
+ }
123
+
124
+ public ArrayList<GridValue> getNeighborsFor(String id, String entityType) {
125
+ GridValue gridValue = get(id);
126
+ if (gridValue == null)
127
+ {
128
+ return null;
129
+ }
130
+ return neighbors(gridValue.x, gridValue.y, entityType);
131
+ }
132
+
133
+ public GridValue get(String id) {
134
+ return objectIndex.get(id);
135
+ }
136
+
137
+ public void remove(String id) {
138
+ GridValue indexValue = objectIndex.get(id);
139
+ if (indexValue != null) {
140
+ int cell = indexValue.cell;
141
+ ConcurrentHashMap<String, GridValue> cellGridValues = cells.get(cell);
142
+ if (cellGridValues != null) {
143
+ cellGridValues.remove(id);
144
+ }
145
+ objectIndex.remove(id);
146
+ }
147
+ }
148
+
149
+ public Boolean set(String id, float x, float y, float z, String entityType) {
150
+ GridValue oldValue = objectIndex.get(id);
151
+
152
+ if (oldValue != null) {
153
+ if (oldValue.x == x && oldValue.y == y) {
154
+ return false;
155
+ }
156
+ }
157
+
158
+ int cell = hash(x, y);
159
+
160
+
161
+ GridValue gridValue;
162
+ gridValue = new GridValue(id, cell, x, y, z, entityType);
163
+
164
+ if (oldValue == null) {
165
+ objectIndex.put(id, gridValue);
166
+ } else {
167
+ if (oldValue.cell != cell) {
168
+ ConcurrentHashMap<String, GridValue> cellGridValues = cells.get(oldValue.cell);
169
+ cellGridValues.remove(id);
170
+ if (cellGridValues.size() == 0) {
171
+ cells.remove(oldValue.cell);
172
+ }
173
+
174
+ }
175
+ objectIndex.replace(id, gridValue);
176
+ }
177
+
178
+ if (!cells.containsKey(cell)) {
179
+ cells.put(cell, new ConcurrentHashMap<String, GridValue>());
180
+ }
181
+ cells.get(cell).put(id, gridValue);
182
+
183
+ //deltaIndex.put(id, gridValue);
184
+
185
+ return true;
186
+ }
187
+
188
+ public int hash2(float x, float y) {
189
+ return (int) (Math.floor(x / this.cellSize) + Math.floor(y / this.cellSize) * width);
190
+ }
191
+
192
+ public int hash(float x, float y) {
193
+ return (int) ((x * this.convFactor)) + (int) ((y * this.convFactor)) * this.width;
194
+ }
195
+ }
@@ -0,0 +1,30 @@
1
+ package com.game_machine.core;
2
+
3
+ import java.io.Serializable;
4
+
5
+
6
+ public class GridValue implements Serializable {
7
+
8
+ /**
9
+ *
10
+ */
11
+ private static final long serialVersionUID = -2653791674585495109L;
12
+ public final String id;
13
+ public final int cell;
14
+ public final float x;
15
+ public final float y;
16
+ public final float z;
17
+
18
+ public final String entityType;
19
+ public long createdAt;
20
+
21
+ public GridValue(String id, int cell, float x, float y, float z, String entityType) {
22
+ this.id = id;
23
+ this.cell = cell;
24
+ this.x = x;
25
+ this.y = y;
26
+ this.z = z;
27
+ this.entityType = entityType;
28
+ //this.createdAt = System.nanoTime();
29
+ }
30
+ }
@@ -0,0 +1,7 @@
1
+ package com.game_machine.core;
2
+
3
+ import akka.actor.UntypedActor;
4
+
5
+ public interface IActorFactory {
6
+ UntypedActor create();
7
+ }
@@ -0,0 +1,28 @@
1
+ package com.game_machine.core;
2
+
3
+ import io.netty.channel.ChannelHandlerContext;
4
+
5
+ public class NetMessage {
6
+
7
+ // Protocols
8
+ public static final int UDP = 0;
9
+ public static final int UDT = 1;
10
+ public static final int TCP = 2;
11
+
12
+ public final byte[] bytes;
13
+ public final String host;
14
+ public final int port;
15
+ public final int protocol;
16
+ public final String clientId;
17
+ public final ChannelHandlerContext ctx;
18
+
19
+ public NetMessage(String clientId, int protocol, byte[] bytes, String host, int port, ChannelHandlerContext ctx) {
20
+ this.clientId = clientId;
21
+ this.bytes = bytes;
22
+ this.host = host;
23
+ this.port = port;
24
+ this.protocol = protocol;
25
+ this.ctx = ctx;
26
+ }
27
+
28
+ }
@@ -0,0 +1,97 @@
1
+ package com.game_machine.core;
2
+
3
+ import org.slf4j.Logger;
4
+ import org.slf4j.LoggerFactory;
5
+
6
+ import io.netty.bootstrap.Bootstrap;
7
+ import io.netty.channel.ChannelFuture;
8
+ import io.netty.channel.ChannelHandlerContext;
9
+ import io.netty.channel.ChannelInitializer;
10
+ import io.netty.channel.ChannelOption;
11
+ import io.netty.channel.ChannelPipeline;
12
+ import io.netty.channel.EventLoopGroup;
13
+ import io.netty.channel.group.ChannelGroup;
14
+ import io.netty.channel.group.ChannelGroupFuture;
15
+ import io.netty.channel.group.DefaultChannelGroup;
16
+ import io.netty.channel.nio.NioEventLoopGroup;
17
+ import io.netty.channel.socket.nio.NioDatagramChannel;
18
+ import io.netty.util.concurrent.DefaultEventExecutorGroup;
19
+
20
+ import com.game_machine.core.NetMessage;
21
+
22
+ public final class UdpServer implements Runnable {
23
+
24
+ private static final Logger log = LoggerFactory.getLogger(UdpServer.class);
25
+ private static Thread serverThread;
26
+
27
+ private static UdpServer udpServer;
28
+
29
+ private final String hostname;
30
+ private final int port;
31
+
32
+ private final UdpServerHandler handler;
33
+
34
+
35
+ public static UdpServer getUdpServer() {
36
+ return udpServer;
37
+ }
38
+
39
+ public static void start(String host, Integer port) {
40
+
41
+ // Don't try to start an already running server
42
+ if (udpServer != null) {
43
+ return;
44
+ }
45
+
46
+ udpServer = new UdpServer(host, port);
47
+ serverThread = new Thread(udpServer);
48
+ serverThread.start();
49
+ }
50
+
51
+ public static void stop() {
52
+ log.info("Stopping UDP server");
53
+ // Don't try to stop a server that's not running
54
+ if (udpServer == null) {
55
+ return;
56
+ }
57
+
58
+ }
59
+
60
+ public UdpServerHandler getHandler() {
61
+ return this.handler;
62
+ }
63
+
64
+ public UdpServer(final String hostname, final int port) {
65
+ this.port = port;
66
+ this.hostname = hostname;
67
+ this.handler = new UdpServerHandler();
68
+ }
69
+
70
+ public void run() {
71
+ log.info("Starting UdpServer port=" + this.port + " hostname=" + this.hostname);
72
+ Thread.currentThread().setName("udp-server");
73
+ EventLoopGroup group = new NioEventLoopGroup();
74
+ try {
75
+ Bootstrap boot = new Bootstrap();
76
+ boot.channel(NioDatagramChannel.class);
77
+ boot.group(group);
78
+ boot.option(ChannelOption.SO_BROADCAST, false);
79
+ boot.handler(new UdpServerHandler());
80
+
81
+ boot.bind(this.port).sync().channel().closeFuture().await();
82
+
83
+ } catch (InterruptedException e) {
84
+ // TODO Auto-generated catch block
85
+ e.printStackTrace();
86
+ } finally {
87
+ group.shutdownGracefully();
88
+ }
89
+ }
90
+
91
+ public void sendToClient(byte[] bytes, String host, int port, ChannelHandlerContext ctx) {
92
+ handler.send(bytes, host, port, ctx);
93
+ }
94
+
95
+
96
+
97
+ }
@@ -0,0 +1,90 @@
1
+ package com.game_machine.core;
2
+
3
+ import io.netty.buffer.ByteBuf;
4
+ import io.netty.buffer.Unpooled;
5
+ import io.netty.channel.ChannelFuture;
6
+ import io.netty.channel.ChannelFutureListener;
7
+ import io.netty.channel.ChannelHandlerContext;
8
+ import io.netty.channel.SimpleChannelInboundHandler;
9
+ import io.netty.channel.socket.DatagramPacket;
10
+
11
+ import java.net.InetSocketAddress;
12
+ import java.util.HashMap;
13
+
14
+ import org.slf4j.Logger;
15
+ import org.slf4j.LoggerFactory;
16
+
17
+ import akka.actor.ActorSelection;
18
+
19
+ import com.game_machine.core.ActorUtil;
20
+ import com.game_machine.core.NetMessage;
21
+
22
+ //@Sharable
23
+ public final class UdpServerHandler extends
24
+ SimpleChannelInboundHandler<DatagramPacket> {
25
+
26
+ private static final Logger log = LoggerFactory
27
+ .getLogger(UdpServerHandler.class);
28
+ public ChannelHandlerContext ctx = null;
29
+ private ActorSelection inbound;
30
+
31
+ private HashMap<String,String> clients = new HashMap<String,String>();
32
+
33
+ public UdpServerHandler() {
34
+ this.inbound = ActorUtil
35
+ .getSelectionByName("GameMachine::Endpoints::UdpIncoming");
36
+ }
37
+
38
+ @Override
39
+ public void exceptionCaught(final ChannelHandlerContext ctx,
40
+ final Throwable cause) {
41
+ log.info("close the connection when an exception is raised", cause);
42
+ ctx.close();
43
+ }
44
+
45
+ @Override
46
+ public void channelReadComplete(ChannelHandlerContext ctx) {
47
+ ctx.flush();
48
+ }
49
+
50
+ @Override
51
+ public void channelActive(final ChannelHandlerContext ctx) {
52
+ log.info("UDP server active");
53
+ this.ctx = ctx;
54
+ }
55
+
56
+ public void send(byte[] bytes, String host, int port,
57
+ ChannelHandlerContext ctx) {
58
+ //log.info("send " + "host: " + host + " port: " + port + " bytes: "
59
+ // + bytes.toString()+" ctx: "+ctx);
60
+
61
+ ByteBuf buf = Unpooled.copiedBuffer(bytes);
62
+ DatagramPacket packet = new DatagramPacket(buf,
63
+ new InetSocketAddress(host, port));
64
+ ctx.writeAndFlush(packet);
65
+ }
66
+
67
+ public void echo(ChannelHandlerContext ctx, DatagramPacket m, byte[] bytes)
68
+ {
69
+ ByteBuf buf = Unpooled.copiedBuffer(bytes);
70
+ DatagramPacket packet = new DatagramPacket(buf,
71
+ new InetSocketAddress(m.sender().getHostString(), m.sender().getPort()));
72
+ ctx.writeAndFlush(packet);
73
+ }
74
+
75
+ @Override
76
+ protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket m)
77
+ throws Exception {
78
+ byte[] bytes = new byte[m.content().readableBytes()];
79
+ m.content().readBytes(bytes);
80
+
81
+
82
+ NetMessage gameMessage = new NetMessage(null, NetMessage.UDP, bytes, m
83
+ .sender().getHostString(), m.sender().getPort(), ctx);
84
+ log.debug("MessageReceived length" + bytes.length + " "
85
+ + new String(bytes));
86
+ this.inbound.tell(gameMessage, null);
87
+
88
+ }
89
+
90
+ }