Commits

Greg Taylor committed ff82cc2

Hooking up events.

Comments (0)

Files changed (5)

lib/src/unit_store/bs_client.dart

 class BattleSnakeClient extends WampClientProtocol {
   UnitStore unitStore;
 
+  static final Map EVENTS = {
+    "http://hexmap.com/unit/events/detected": {
+      'handler': BSClientPubSubHandler.handleUnitDetected,
+    },
+    "http://hexmap.com/unit/events/destroyedd": {
+      'handler': BSClientPubSubHandler.handleUnitDestroyed,
+    },
+    "http://hexmap.com/unit/events/stale-removal": {
+      'handler': BSClientPubSubHandler.handleUnitStaleRemoval,
+    },
+    "http://hexmap.com/unit/events/hit-landed": {
+      'handler': BSClientPubSubHandler.handleShotLanded,
+    },
+    "http://hexmap.com/unit/events/shot-missed": {
+      'handler': BSClientPubSubHandler.handleShotMissed,
+    },
+    "http://hexmap.com/unit/events/state-changed": {
+      'handler': BSClientPubSubHandler.handleUnitStateChanged,
+    }
+  };
+
   BattleSnakeClient(socket, UnitStore this.unitStore) : super(socket) {
 
   }
   }
 
   onEvent(topicUri, event) {
-    //print("Event received");
+    //print("Event received ${topicUri}");
+    var handlerFunc = EVENTS[topicUri]['handler'];
+    handlerFunc(event, unitStore);
+  }
+}
+
+
+class BSClientPubSubHandler {
+
+  static void handleUnitDetected(event, UnitStore unitStore) {
+    print("Handling unit detected ${event}");
+    Map uDat = JSON.decode(event['unit']);
+    Unit unit = BSUnitParser.parseUnitJson(uDat);
+    unitStore.addNewUnit(unit);
+  }
+
+  static void handleUnitDestroyed(event, UnitStore unitStore) {
+    print("Handling unit destroyed ${event}");
+    unitStore.markUnitAsDestroyedById(event['victim_id']);
+  }
+
+  static void handleUnitStaleRemoval(event, UnitStore unitStore) {
+    print("Handling unit stale removal ${event}");
+    unitStore.removeUnitById(event['unit_id']);
+  }
+
+  static void handleShotLanded(event, UnitStore unitStore) {
+    print("Handling shot landed ${event}");
+    unitStore.recordHit(
+        event['victim_id'], event['aggressor_id'], event['weapon_name']);
+  }
+
+  static void handleShotMissed(event, UnitStore unitStore) {
+    print("Handling shot missed ${event}");
+    unitStore.recordMiss(
+        event['victim_id'], event['aggressor_id'], event['weapon_name']);
   }
+
+  static void handleUnitStateChanged(event, UnitStore unitStore) {
+    print("Handling state change ${event}");
+    Map uDat = JSON.decode(event['unit']);
+    Unit unit = BSUnitParser.parseUnitJson(uDat);
+    unitStore.updateUnit(unit);
+  }
+
 }
 
 
   static void handleUnitStoreGet(response, UnitStore unitStore) {
     Map unitStoreJson = JSON.decode(response);
     unitStoreJson.values.forEach((uDat) {
-      print(uDat);
-      DateTime lastSeen = DateTime.parse(uDat['last_seen']);
-      Unit unit = new Unit(
-          uDat['contact_id'], uDat['unit_type'], uDat['mech_name'],
-          uDat['x_coord'], uDat['y_coord'], uDat['z_coord'],
-          uDat['speed'], uDat['heading'], uDat['jump_heading'],
-          uDat['range_to_hex_center'], uDat['bearing_to_hex_center'],
-          uDat['tonnage'], uDat['heat'], uDat['status'], lastSeen
-      );
-      unitStore.updateOrAddUnit(unit);
+      Unit unit = BSUnitParser.parseUnitJson(uDat);
+      unitStore.addNewUnit(unit);
     });
   }
+
+}
+
+
+class BSUnitParser {
+  static Unit parseUnitJson(Map uDat) {
+    DateTime lastSeen = DateTime.parse(uDat['last_seen']);
+    Unit unit = new Unit(
+        uDat['contact_id'], uDat['unit_type'], uDat['mech_name'],
+        uDat['x_coord'], uDat['y_coord'], uDat['z_coord'],
+        uDat['speed'], uDat['heading'], uDat['jump_heading'],
+        uDat['range_to_hex_center'], uDat['bearing_to_hex_center'],
+        uDat['tonnage'], uDat['heat'], uDat['status'], lastSeen
+    );
+    return unit;
+  }
 }

lib/src/unit_store/store.dart

 part of unit_store;
 
+final EventType<Unit> unitAddedEvent = new EventType<Unit>();
+final EventType<Unit> unitUpdatedEvent = new EventType<Unit>();
+final EventType<String> unitRemovedEvent = new EventType<String>();
+final EventType<String> unitDestroyedEvent = new EventType<String>();
+final EventType<Map> shotLanded = new EventType<Map>();
+final EventType<Map> shotMissed = new EventType<Map>();
+
 class UnitStore {
   Map<String,Unit> _units;
+  EventBus eventBus;
 
-  UnitStore() {
+  UnitStore(this.eventBus) {
     _units = new Map<String,Unit>();
+
+    //eventBus.on(newUnitAddedEvent).listen((Unit unit) {
+    //  print(unit);
+    //});
+  }
+
+  void addNewUnit(Unit unit) {
+    print("Adding ${unit}");
+    _units[unit.contactId] = unit;
+    eventBus.fire(unitAddedEvent, unit);
   }
 
-  void updateOrAddUnit(Unit unit) {
+  void updateUnit(Unit unit) {
+    print("Updating ${unit}");
     _units[unit.contactId] = unit;
+    eventBus.fire(unitUpdatedEvent, unit);
+  }
+
+  void markUnitAsDestroyedById(String unitId) {
+    eventBus.fire(unitDestroyedEvent, unitId);
+    removeUnitById(unitId);
+  }
+
+  void removeUnitById(String unitId) {
+    print("Removing ${unitId}");
+    _units.remove(unitId);
+    eventBus.fire(unitRemovedEvent, unitId);
+  }
+
+  void recordHit(String victimId, String aggressorId, String weaponName) {
+    Map shot = {
+      'victimId': victimId,
+      'aggressorId': aggressorId,
+      'weaponName': weaponName,
+    };
+    eventBus.fire(shotLanded, shot);
+  }
+
+  void recordMiss(String victimId, String aggressorId, String weaponName) {
+    Map shot = {
+      'victimId': victimId,
+      'aggressorId': aggressorId,
+      'weaponName': weaponName,
+    };
+    eventBus.fire(shotMissed, shot);
   }
 }
 
     this.y_coord, this.z_coord, this.speed, this.heading, this.jump_heading,
     this.rangeToHexCenter, this.bearingToHexCenter, this.tonnage,
     this.heat, this.status, this.lastSeen);
-}
 
+  String toString() {
+    return "[${contactId}] ${mechName}";
+  }
+}

lib/src/unit_store/unit_store.dart

 library unit_store;
 
 import 'dart:convert';
+import 'package:event_bus/event_bus.dart';
 import "package:hexmap/wamp_client/wamp_client.dart";
 
 part 'bs_client.dart';
 dependencies:
   browser: any
   crypto: any
-  json_object: any
+  event_bus: any
   stagexl: any
 
 import 'dart:html' as html;
 import 'dart:async' as async;
+import 'package:event_bus/event_bus.dart';
 import 'package:stagexl/stagexl.dart';
 import "package:hexmap/src/unit_store/unit_store.dart";
 import "package:hexmap/src/map_widget/map_widget.dart";
 html.Element stageElement = html.querySelector('#stage');
 Stage stage = new Stage(stageElement, webGL: true);
 ResourceManager resourceManager = new ResourceManager();
+EventBus eventBus = new EventBus();
 
 void main() {
   RenderLoop renderLoop = new RenderLoop()
     ..addStage(stage);
 
-  UnitStore unitStore = new UnitStore();
+  UnitStore unitStore = new UnitStore(eventBus);
   var socket = new html.WebSocket('ws://127.0.0.1:9000', ["wamp"]);
   var client = new BattleSnakeClient(socket, unitStore);