コード例 #1
0
        /// <summary>
        /// sends the games to the server that are live.
        /// </summary>
        public static void sendLiveGameToServer()
        {
            try
            {
                if (GameViewModel.Instance.IsCurrentlySendingOnlineGame)
                    return;
                //controls the fact we are actually sending the game, so we don't try and send it two times at the same time
                GameViewModel.Instance.IsCurrentlySendingOnlineGame = true;

                Task<bool>.Factory.StartNew(
                     () =>
                     {
                         try
                         {
                             System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(Instance.GetType());
                             System.IO.StreamWriter file = new System.IO.StreamWriter(ScoreboardConfig.SAVE_TEMP_GAMES_FILE);
                             Logger.Instance.logMessage("opening Stream Writer" + ScoreboardConfig.SAVE_TEMP_GAMES_FILE, LoggerEnum.message);
                             writer.Serialize(file, Instance);
                             file.Close();
                             file.Dispose();

                             if (GameViewModel.Instance.IsBeingTested)
                             {
                                 GameViewModel.Instance.IsCurrentlySendingOnlineGame = false;
                                 return true;
                             }

                             if (!GameViewModel.Instance.PublishGameOnline && !GameViewModel.Instance.SaveGameOnline)
                             {
                                 GameViewModel.Instance.IsGameOnline = GameViewModelIsOnlineEnum.IsOffline;
                                 GameViewModel.Instance.IsCurrentlySendingOnlineGame = false;
                                 return true;
                             }
                             //controls the UI for the user to tell them we are sending online.
                             GameViewModel.Instance.IsGameOnline = GameViewModelIsOnlineEnum.IsSending;

                             if (!Network.Internet.CheckConnection())
                             {
                                 GameViewModel.Instance.IsCurrentlySendingOnlineGame = false;
                                 GameViewModel.Instance.IsGameOnline = GameViewModelIsOnlineEnum.InternetProblem;
                                 return true;
                             }
                             Logger.Instance.logMessage("encrypting file" + ScoreboardConfig.SAVE_TEMP_GAMES_FILE, LoggerEnum.message);
                             Encryption.EncryptFiletoFile(ScoreboardConfig.SAVE_TEMP_GAMES_FILE, ScoreboardConfig.SAVE_GAMES_TEMP_ENCRYPTED_FILE);
                             Logger.Instance.logMessage("compressing file" + ScoreboardConfig.SAVE_GAMES_TEMP_ENCRYPTED_FILE, LoggerEnum.message);
                             string compressedFile = Compression.Compress(new FileInfo(ScoreboardConfig.SAVE_GAMES_TEMP_ENCRYPTED_FILE));


                             string gameUrl = ScoreboardConfig.UPLOAD_LIVE_GAMES_URL;
                             if (String.IsNullOrEmpty(compressedFile))
                             {
                                 GameViewModel.Instance.IsCurrentlySendingOnlineGame = false;
                                 Logger.Instance.logMessage("file doesn't exist22 " + compressedFile, LoggerEnum.message);
                                 return true;
                             }
                             FileInfo fileInfo = new FileInfo(compressedFile);
                             //we check for existance because there might have been an error compressing and encrypting
                             // the file.  So we want to make sure it exists before we try 
                             //and send it.
                             if (!fileInfo.Exists)
                             {
                                 GameViewModel.Instance.IsCurrentlySendingOnlineGame = false;
                                 Logger.Instance.logMessage("file doesn't exist " + compressedFile, LoggerEnum.message);
                                 return true;
                             }
                             Logger.Instance.logMessage("uploading file" + compressedFile, LoggerEnum.message);
                             WebClientRDN client = new WebClientRDN();

                             byte[] rawResponse = client.UploadFile(new Uri(gameUrl), compressedFile);
                             string re = System.Text.Encoding.ASCII.GetString(rawResponse);
                             JavaScriptSerializer serializer = new JavaScriptSerializer();
                             GameOnlineModel onlineGame = serializer.Deserialize<GameOnlineModel>(re);
                             GameViewModel.Instance.UrlForAdministeringGameOnline = onlineGame.url;
                             GameViewModel.Instance.GameIsConfirmedOnline = onlineGame.IsGameOnline;

                             Thread.Sleep(2000);
                             //delete the file after sending.
                             if (fileInfo.Exists)
                                 fileInfo.Delete();

                             FileInfo file1 = new FileInfo(ScoreboardConfig.SAVE_GAMES_TEMP_ENCRYPTED_FILE);
                             if (file1.Exists)
                                 file1.Delete();

                             if (GameViewModel.Instance.GameIsConfirmedOnline)
                                 GameViewModel.Instance.IsGameOnline = GameViewModelIsOnlineEnum.IsOnline;

                             GameViewModel.Instance.IsCurrentlySendingOnlineGame = false;
                         }
                         catch (Exception e)
                         {
                             ErrorViewModel.Save(e, e.GetType(), null, null, null, Logger.Instance.getLoggedMessages());
                             GameViewModel.Instance.IsCurrentlySendingOnlineGame = false;
                         }

                         GameViewModel.Instance.IsCurrentlySendingOnlineGame = false;
                         return true;
                     });
            }
            catch (Exception exception)
            {
                ErrorViewModel.Save(exception, exception.GetType(), ErrorGroupEnum.UI);
                GameViewModel.Instance.IsCurrentlySendingOnlineGame = false;
            }
        }
コード例 #2
0
        /// <summary>
        /// sends the games to the server that are live.
        /// </summary>
        public static void sendCompletedGameToServer()
        {
            Logger.Instance.logMessage("sending finished game", LoggerEnum.message);
            if (GameViewModel.Instance.IsBeingTested)
                return;

            Task<bool>.Factory.StartNew(
                             () =>
                             {
                                 try
                                 {
                                     CheckIfSendingGameOnline();
                                     GameViewModel.Instance.IsGameOnline = GameViewModelIsOnlineEnum.IsSending;

                                     if (!Network.Internet.CheckConnection())
                                     {
                                         GameViewModel.Instance.IsCurrentlySendingOnlineGame = false;
                                         GameViewModel.Instance.IsGameOnline = GameViewModelIsOnlineEnum.IsOffline;
                                         return true;
                                     }

                                     var f = Instance;


                                     System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(Instance.GetType());
                                     System.IO.StreamWriter file = new System.IO.StreamWriter(ScoreboardConfig.SAVE_TEMP_GAMES_FILE);

                                     writer.Serialize(file, f);
                                     file.Close();
                                     file.Dispose();
                                     Encryption.EncryptFiletoFile(ScoreboardConfig.SAVE_TEMP_GAMES_FILE, ScoreboardConfig.SAVE_GAMES_TEMP_ENCRYPTED_FILE);
                                     string compressedFile = Compression.Compress(new FileInfo(ScoreboardConfig.SAVE_GAMES_TEMP_ENCRYPTED_FILE));

                                     WebClientRDN client = new WebClientRDN();

                                     client.UploadFile(new Uri(ScoreboardConfig.UPLOAD_LIVE_GAMES_URL), compressedFile);

                                     Thread.Sleep(2000);
                                     FileInfo fileInfo = new FileInfo(compressedFile);
                                     if (fileInfo.Exists)
                                         fileInfo.Delete();

                                     FileInfo file1 = new FileInfo(ScoreboardConfig.SAVE_GAMES_TEMP_ENCRYPTED_FILE);
                                     if (file1.Exists)
                                         file1.Delete();

                                     if (GameViewModel.Instance.PublishGameOnline)
                                         GameViewModel.Instance.IsGameOnline = GameViewModelIsOnlineEnum.IsOnline;
                                 }
                                 catch (Exception e)
                                 {
                                     ErrorViewModel.Save(e, e.GetType(), null, null, null, Logger.Instance.getLoggedMessages());

                                 }

                                 GameViewModel.Instance.IsCurrentlySendingOnlineGame = false;
                                 return true;
                             });
        }