diff --git a/WebApi/Controllers/PlayerController.cs b/WebApi/Controllers/PlayerController.cs index 96b1624..54c13ee 100644 --- a/WebApi/Controllers/PlayerController.cs +++ b/WebApi/Controllers/PlayerController.cs @@ -56,7 +56,7 @@ public PlayerController(IDatabase db, INearByFinder nearByFinder, IDirectionCalc [HttpPost("{id}/location")] public ActionResult UpdateLocation(string id, [FromBody] LocationUpdateDTO pos) { - var newPosition = new Location { lat = pos.Lat, lon = pos.Long, tracked = pos.Tracked, at_home = pos.at_home }; + Location newPosition = CreateLocationFromDTO(pos); if (db.Contains(id)) { db.Update(id, newPosition); @@ -73,6 +73,18 @@ public ActionResult UpdateLocation(string id, [FromBo }; } + private static Location CreateLocationFromDTO(LocationUpdateDTO pos) + { + return new Location + { + lat = pos.Lat, + lon = pos.Long, + tracked = pos.Tracked, + at_home = pos.at_home, + timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + }; + } + private LocationUpdateResponseNearbyPlayerDTO CreateNearbyPlayerDTO(Location me, Location other) { var latDistance = me.lat - other.lat; diff --git a/WebApi/Dependencies/MongoDatabase.cs b/WebApi/Dependencies/MongoDatabase.cs index 568bc4f..47efe83 100644 --- a/WebApi/Dependencies/MongoDatabase.cs +++ b/WebApi/Dependencies/MongoDatabase.cs @@ -21,6 +21,8 @@ public class PlayerModel public bool currentTracked; public bool previousTracked; public bool atHome; + public long currentTimestamp; + public long previousTimestamp; } public class MongoDatabase : IDatabase, INearByFinder @@ -66,6 +68,8 @@ public void Create(string id, Location location) atHome = location.at_home, currentTracked = location.tracked, previousTracked = location.tracked, + currentTimestamp = location.timestamp, + previousTimestamp = location.timestamp, }); } @@ -85,10 +89,18 @@ public void Update(string id, Location location) "$set", new BsonDocument(new BsonElement("previousTracked", "$currentTracked")) )), + new BsonDocument(new BsonElement( + "$set", + new BsonDocument(new BsonElement("previousTimestamp", "$currentTimestamp")) + )), new BsonDocument(new BsonElement( "$set", new BsonDocument(new BsonElement("currentTracked", location.tracked)) )), + new BsonDocument(new BsonElement( + "$set", + new BsonDocument(new BsonElement("currentTimestamp", location.timestamp)) + )), new BsonDocument(new BsonElement( "$set", new BsonDocument(new BsonElement("currentLocation", GeoJson.Point(GeoJson.Geographic(location.lon, location.lat)).ToBsonDocument())) diff --git a/WebApi/Entities/Location.cs b/WebApi/Entities/Location.cs index 209e88a..957a2db 100644 --- a/WebApi/Entities/Location.cs +++ b/WebApi/Entities/Location.cs @@ -11,5 +11,10 @@ public class Location public double lon; public bool tracked; public bool at_home; + + /// + /// represents the number of milliseconds that have elapsed since 1970-01-01T00:00:00.000Z + /// + public long timestamp; } }