diff --git a/Windows Phone/VineCache/VineCache/App.xaml.cs b/Windows Phone/VineCache/VineCache/App.xaml.cs index 78e9817..77358c5 100644 --- a/Windows Phone/VineCache/VineCache/App.xaml.cs +++ b/Windows Phone/VineCache/VineCache/App.xaml.cs @@ -1,14 +1,19 @@ -using Facebook.Client; +using Facebook; +using Facebook.Client; +using Parse; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; +using System.Threading.Tasks; using Windows.ApplicationModel; using Windows.ApplicationModel.Activation; using Windows.Devices.Geolocation; using Windows.Foundation; using Windows.Foundation.Collections; +using Windows.Storage; +using Windows.Storage.Streams; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; @@ -29,16 +34,24 @@ public sealed partial class App : Application { private TransitionCollection transitions; private static Geolocator s_GPS; - - /// - /// Initializes the singleton application object. This is the first line of authored code - /// executed, and as such is the logical equivalent of main() or WinMain(). - /// - public App() + public static ParseDB parseDB; + public static string facebookToken; + public static string facebookName; + public static string facebookID; + public static string facebookEmail; + public static int currentNodeNumba = 0; + + /// + /// Initializes the singleton application object. This is the first line of authored code + /// executed, and as such is the logical equivalent of main() or WinMain(). + /// + public App() { this.InitializeComponent(); this.Suspending += this.OnSuspending; - } + ParseClient.Initialize("ODbBwcIu8uZ4zuJ8PGsinEtXeyUswCXL9pUnddov", "0xnE8Q36PWvW517XvQlCEyBmKS0etT5VEdP5gyaP"); + parseDB = new ParseDB(); + } /// /// Invoked when the application is launched normally by the end user. Other entry points @@ -105,6 +118,8 @@ protected override void OnLaunched(LaunchActivatedEventArgs e) // Ensure the current window is active Window.Current.Activate(); + + //ParseAnalytics.TrackAppOpenedAsync(); } /// @@ -156,9 +171,50 @@ private void OnFacebookAuthenticationFinished(AccessTokenData session) { Frame rootFrame = Window.Current.Content as Frame; rootFrame.Navigate(typeof(DisplayVideoPage)); - } - - internal static Geolocator GPS + facebookToken = session.AccessToken; + facebookID = session.FacebookId; + + parseDB.parseDelegate = new VineCacheParseDelegate(); + parseDB.GetNextAvailableEvent(); + } + + internal class VineCacheParseDelegate : ParseDelegate + { + public void EventResult(PLEvent eventItem) + { + FacebookClient client = new FacebookClient(facebookToken); + client.GetTaskAsync("me", new { fields = "name, id, email" }).ContinueWith((Task task, object item) => { + dynamic facebookInfo = task.Result; + App.facebookName = facebookInfo.name; + App.facebookID = facebookInfo.id; + //App.facebookEmail = facebookInfo.email; + parseDB.CreatePlayer(App.facebookName, App.facebookEmail, App.facebookID, item as PLEvent); + parseDB.GetMap(item as PLEvent); + }, eventItem); + + } + + public void MapResult(PLMap mapItem) + { + parseDB.GetNodes(mapItem); + } + + public void NodeResult(List nodeList) + { + parseDB.RetrieveVideo(nodeList[currentNodeNumba]); + } + + public async void VideoRetrieved(PLNode node) + { + InMemoryRandomAccessStream s = new InMemoryRandomAccessStream(); + StorageFolder folder = KnownFolders.VideosLibrary; + StorageFile file = await folder.CreateFileAsync("vinecachetargetvideo.mp4", CreationCollisionOption.ReplaceExisting); + await FileIO.WriteBytesAsync(file, node.Video as byte[]); + } + } + + + internal static Geolocator GPS { get { diff --git a/Windows Phone/VineCache/VineCache/DisplayVideoPage.xaml.cs b/Windows Phone/VineCache/VineCache/DisplayVideoPage.xaml.cs index 9f1d622..24d6c36 100644 --- a/Windows Phone/VineCache/VineCache/DisplayVideoPage.xaml.cs +++ b/Windows Phone/VineCache/VineCache/DisplayVideoPage.xaml.cs @@ -13,6 +13,7 @@ using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; using Windows.Devices.Geolocation; +using Windows.Storage; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556 @@ -73,8 +74,9 @@ protected override void OnNavigatedTo(NavigationEventArgs e) private async void videoMediaElement_Tapped(object sender, TappedRoutedEventArgs e) { - this.NavigateToUploadVideoPage(); - videoMediaElement.Play(); + SetVideoTarget(); + //this.NavigateToUploadVideoPage(); + videoMediaElement.Play(); distanceToObjectProgressBar.Value = 50; if (this.startingPoint == null) { @@ -87,6 +89,11 @@ private async void videoMediaElement_Tapped(object sender, TappedRoutedEventArgs } } + public void SetVideoTarget() + { + this.videoMediaElement.Source = new Uri(KnownFolders.VideosLibrary.Path + "/vinecachetargetvideo.mp4"); + } + private void NavigateToUploadVideoPage() { Frame rootFrame = Window.Current.Content as Frame; diff --git a/Windows Phone/VineCache/VineCache/MainPage.xaml b/Windows Phone/VineCache/VineCache/MainPage.xaml index bd9a53f..f23ae57 100644 --- a/Windows Phone/VineCache/VineCache/MainPage.xaml +++ b/Windows Phone/VineCache/VineCache/MainPage.xaml @@ -13,7 +13,7 @@ - + diff --git a/Windows Phone/VineCache/VineCache/Parse.NetFx45.dll b/Windows Phone/VineCache/VineCache/Parse.NetFx45.dll new file mode 100644 index 0000000..8e9f665 Binary files /dev/null and b/Windows Phone/VineCache/VineCache/Parse.NetFx45.dll differ diff --git a/Windows Phone/VineCache/VineCache/Parse.Phone.dll b/Windows Phone/VineCache/VineCache/Parse.Phone.dll new file mode 100644 index 0000000..b41804a Binary files /dev/null and b/Windows Phone/VineCache/VineCache/Parse.Phone.dll differ diff --git a/Windows Phone/VineCache/VineCache/Parse.WinRT.dll b/Windows Phone/VineCache/VineCache/Parse.WinRT.dll new file mode 100644 index 0000000..f980011 Binary files /dev/null and b/Windows Phone/VineCache/VineCache/Parse.WinRT.dll differ diff --git a/Windows Phone/VineCache/VineCache/Parse.dll b/Windows Phone/VineCache/VineCache/Parse.dll new file mode 100644 index 0000000..d758e58 Binary files /dev/null and b/Windows Phone/VineCache/VineCache/Parse.dll differ diff --git a/Windows Phone/VineCache/VineCache/ParseLibrary.cs b/Windows Phone/VineCache/VineCache/ParseLibrary.cs new file mode 100644 index 0000000..9710ea4 --- /dev/null +++ b/Windows Phone/VineCache/VineCache/ParseLibrary.cs @@ -0,0 +1,326 @@ +using Parse; +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Threading.Tasks; + +namespace VineCache +{ + public class PLEvent + { + internal ParseObject pfEvent; + + public PLEvent(ParseObject parseObject) + { + pfEvent = parseObject; + } + + public string Name + { + get + { + return pfEvent["Name"] as string; + } + } + + public int? DurationInMinutes + { + get + { + return pfEvent["Duration"] as int?; + } + } + + public DateTime? StartTime + { + get + { + return pfEvent["StartTime"] as DateTime?; + } + } + } + + public class PLMap + { + internal ParseObject pfMAp; + + public PLMap(ParseObject parseObject) + { + pfMAp = parseObject; + } + + public string Name + { + get + { + return pfMAp["Name"] as string; + } + } + } + + public class PLNode + { + internal ParseObject pfNode; + + public object Video = null; + public bool Completed = false; + + public PLNode(ParseObject parseObject) + { + pfNode = parseObject; + } + + public string Name + { + get + { + return pfNode["Name"] as string; + } + } + + public string Hint + { + get + { + return pfNode["Hint"] as string; + } + } + + public ParseGeoPoint? Coordinates + { + get + { + return pfNode["Coordinates"] as ParseGeoPoint?; + } + } + } + + public class PLPlayer + { + public static PLEvent tempEvent; + internal ParseObject pfPerson; + internal ParseObject pfPlayer; + private PLNode currentNode = null; + private ParseGeoPoint? coordinates = null; + + public PLPlayer(ParseObject personParseObject, ParseObject playerParseObject) + { + pfPerson = personParseObject; + pfPlayer = playerParseObject; + } + + public string Name + { + get + { + return pfPerson["Name"] as string; + } + } + + public string Email + { + get + { + return pfPerson["Email"] as string; + } + } + public string FacebookId + { + get + { + return pfPerson["FacebookId"] as string; + } + } + + public int? Score + { + get + { + return pfPerson["Score"] as int?; + } + set + { + pfPlayer["Score"] = value; + pfPlayer.SaveAsync(); + } + } + public PLNode CurrentNode + { + get + { + return currentNode; + } + set + { + currentNode = value; + pfPlayer["CurrentNode"] = value.pfNode; + pfPlayer.SaveAsync(); + } + } + + public ParseGeoPoint? Coordinates + { + get + { + return coordinates; + } + set + { + coordinates = value; + pfPlayer["Coordinates"] = value; + pfPlayer.SaveAsync(); + } + } + } + + public interface ParseDelegate + { + void EventResult(PLEvent eventItem); + void MapResult(PLMap mapItem); + void NodeResult(List nodeList); + void VideoRetrieved(PLNode node); + } + + public class ParseDB + { + public ParseDelegate parseDelegate = null; + + public static PLEvent _PLEvent; + public static PLMap _PLMap; + public static List _PLNodes; + public static PLPlayer _PLPlayer; + + private void RetrieveRelation(ParseObject parent, string relationKey, Action>> resultFunction) + { + ParseRelation relation = parent.GetRelation(relationKey); + var query = relation.Query; + query.FindAsync().ContinueWith(resultFunction); + } + + public void GetNodes(PLMap map) + { + RetrieveRelation(map.pfMAp, "Nodes", (Task> task) => { + if (task.IsFaulted) + { + return; + } + _PLNodes = new List(); + + foreach (ParseObject node in task.Result) + { + _PLNodes?.Add(new PLNode(node)); + } + this.parseDelegate?.NodeResult(_PLNodes); + }); + } + + public void GetMap(PLEvent plEvent) + { + RetrieveRelation(plEvent.pfEvent, "Map", (Task> task) => + { + if (task.IsFaulted) + { + return; + } + + try + { + PLMap map = new PLMap((task.Result as ParseObject[])[0]); + + ParseDB._PLMap = map; + + this.parseDelegate?.MapResult(_PLMap); + } + catch (Exception) + { } + }); + } + + public void GetNextAvailableEvent() + { + ParseQuery query = new ParseQuery("Event").OrderBy("StartTime").WhereGreaterThan("StartTime", DateTime.Now); + query.FirstOrDefaultAsync().ContinueWith((Task task) => { + if (task.IsFaulted) + { + return; + } + _PLEvent = new PLEvent(task.Result); + this.parseDelegate?.EventResult(_PLEvent); + this.GetMap(_PLEvent); + }); + } + + public void RetrieveVideo(PLNode node) + { + var video = node.pfNode["Video"] as ParseFile; + Uri url = video.Url; + HttpClient httpClient = new HttpClient(); + httpClient.GetByteArrayAsync(video.Url).ContinueWith((Task task, object state) => { + if (task.IsFaulted) + { + return; + } + ((PLNode)state).Video = task.Result; + this.parseDelegate?.VideoRetrieved(node); + }, node); + } + + public void CreatePlayer(string name, string email, string facebookID, PLEvent plEvent) + { + PLPlayer.tempEvent = plEvent; + var person = new ParseObject("Person"); + person["Name"] = name; + person["Email"] = email; + person["FacebookId"] = facebookID; + + var player = new ParseObject("Player"); + //player["Person"] = person; + player["Score"] = 0; + _PLPlayer = new PLPlayer(person, player); + + //Is player already registered in event? + ParseQuery query = new ParseQuery("Players"); + query.FindAsync().ContinueWith((Task> task, object state) => + { + ParseObject currentPerson = (state as PLPlayer).pfPerson; + bool playerExists = false; + foreach (ParseObject existingPlayer in task.Result) + { + ParseObject existingPerson = existingPlayer["Person"] as ParseObject; + if (existingPerson["FacebookID"] == currentPerson["FacebookID"]) + { + playerExists = true; + break; + } + } + + if (!playerExists) + { + currentPerson.SaveAsync().ContinueWith((Task t, object player1) => { + ((player1 as PLPlayer).pfPlayer as ParseObject).SaveAsync().ContinueWith((Task t1, object myplayer) => { + + var playerpersonrelation = (myplayer as PLPlayer).pfPlayer.GetRelation("Person"); + playerpersonrelation.Add((player1 as PLPlayer).pfPerson as ParseObject); + ((player1 as PLPlayer).pfPlayer as ParseObject).SaveAsync(); + + var relation = PLPlayer.tempEvent.pfEvent.GetRelation("Players"); + relation.Add((player1 as PLPlayer).pfPlayer as ParseObject); + PLPlayer.tempEvent.pfEvent.SaveAsync(); + }, player1); + }, state); + } + }, _PLPlayer); + } + + public void UploadVideo(byte[] videoData, PLNode node) + { + var file = new ParseFile(node.Name + ".mp4", videoData); + var video = new ParseObject("Videos"); + video["Video"] = file; + video["Node"] = node.pfNode; + var relation = node.pfNode.GetRelation("Videos"); + relation.Add(video); + node.pfNode.SaveAsync(); + } + } +} \ No newline at end of file diff --git a/Windows Phone/VineCache/VineCache/VineCache.csproj b/Windows Phone/VineCache/VineCache/VineCache.csproj index 6d8e5cf..e7a8951 100644 --- a/Windows Phone/VineCache/VineCache/VineCache.csproj +++ b/Windows Phone/VineCache/VineCache/VineCache.csproj @@ -89,6 +89,7 @@ MainPage.xaml + UploadVideoPage.xaml @@ -138,6 +139,15 @@ ..\packages\Facebook.Client.1.0.2\lib\wpa81\Facebook.Client.dll + + .\Parse.dll + + + .\Parse.NetFx45.dll + + + .\Parse.WinRT.dll +