Esempio n. 1
0
        private void Execute()
        {
            switch (_state)
            {
            case States.TitleData:
                #region Update Title Data Keys
                if (!_titleData.FromProcessed)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Getting Title Data from: " + _commandArgs.FromTitleId);
                    PlayFabService.GetTitleData(_commandArgs.FromTitleId, (success, result) =>
                    {
                        if (!success || result.Data.Count == 0)
                        {
                            Console.WriteLine("No Title Data found, skipping");
                            SetNextState();
                        }
                        else
                        {
                            Console.WriteLine("Title Data Keys Found: " + result.Data.Count.ToString());
                            _titleData.Data          = result.Data;
                            _titleData.FromProcessed = true;
                        }
                    });
                }

                if (!_titleData.ToProcessed && _titleData.FromProcessed)
                {
                    if (_titleData.Data.Count == 0)
                    {
                        _titleData.ToProcessed = true;
                        SetNextState();
                        break;
                    }
                    var kvp = _titleData.Pop();
                    Console.WriteLine("Saving Title Data from: " + _commandArgs.FromTitleId + " To: " + _commandArgs.ToTitleId);
                    PlayFabService.UpdateTitleData(_commandArgs.ToTitleId, kvp, (success) =>
                    {
                        if (!success)
                        {
                            Console.WriteLine("Save Title Data Failed, skipping");
                            SetNextState();
                        }
                    });
                }
                #endregion
                break;

            case States.TitleInternalData:
                #region Update Title Internal Data Keys
                if (!_titleInternalData.FromProcessed)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Getting Title Interal Data from: " + _commandArgs.FromTitleId);
                    PlayFabService.GetTitleInternalData(_commandArgs.FromTitleId, (success, result) =>
                    {
                        if (!success || result.Data.Count == 0)
                        {
                            Console.WriteLine("No Title Internal Data found, skipping");
                            SetNextState();
                        }
                        else
                        {
                            Console.WriteLine("Title Internal Data Keys Found: " + result.Data.Count.ToString());
                            _titleInternalData.Data          = result.Data;
                            _titleInternalData.FromProcessed = true;
                        }
                    });
                }

                if (!_titleInternalData.ToProcessed && _titleInternalData.FromProcessed)
                {
                    if (_titleInternalData.Data.Count == 0)
                    {
                        _titleInternalData.ToProcessed = true;
                        SetNextState();
                        break;
                    }
                    var kvp = _titleInternalData.Pop();
                    Console.WriteLine("Saving Title Interal Data from: " + _commandArgs.FromTitleId + " To: " + _commandArgs.ToTitleId);
                    PlayFabService.UpdateTitleInternalData(_commandArgs.ToTitleId, kvp, (success) =>
                    {
                        if (!success)
                        {
                            Console.WriteLine("Save Title Interal Data Failed, skipping");
                            SetNextState();
                        }
                    });
                }
                #endregion
                break;

            case States.Currency:
                #region Update Currency Types
                if (!_currencyData.FromProcessed)
                {
                    Console.WriteLine("Getting Currency Types Data from: " + _commandArgs.FromTitleId);
                    PlayFabService.GetCurrencyData(_commandArgs.FromTitleId, (success, vcData) =>
                    {
                        if (!success || vcData.Count == 0)
                        {
                            Console.WriteLine("Error Fetching Currency Type Data, skipping");
                            SetNextState();
                            return;
                        }
                        _currencyData.Data          = vcData;
                        _currencyData.FromProcessed = true;
                    });
                }

                if (!_currencyData.ToProcessed && _currencyData.FromProcessed)
                {
                    if (_currencyData.Data == null)
                    {
                        _currencyData.ToProcessed = true;
                        SetNextState();
                        break;
                    }

                    PlayFabService.UpdateCurrencyData(_commandArgs.ToTitleId, _currencyData.Data,
                                                      (success) =>
                    {
                        if (!success)
                        {
                            Console.WriteLine("Save Title Data Failed.");
                            _cts.Cancel();
                        }
                        _currencyData.Data = null;
                    });
                }
                #endregion
                break;

            case States.CloudScript:
                #region Update CloudScript File
                if (!_cloudScriptData.FromProcessed)
                {
                    Console.WriteLine("Getting CloudScript Data from: " + _commandArgs.FromTitleId);
                    PlayFabService.GetCloudScript(_commandArgs.FromTitleId, (success, data) =>
                    {
                        if (!success || data.Count == 0)
                        {
                            Console.WriteLine("Error Fetching CloudScript Data, skipping.");
                            SetNextState();
                            return;
                        }
                        _cloudScriptData.Data          = data;
                        _cloudScriptData.FromProcessed = true;
                    });
                }

                if (!_cloudScriptData.ToProcessed && _cloudScriptData.FromProcessed)
                {
                    if (_cloudScriptData.Data == null)
                    {
                        _cloudScriptData.ToProcessed = true;
                        SetNextState();
                        break;
                    }

                    Console.WriteLine("Updating CloudScript on Title: " + _commandArgs.ToTitleId);

                    PlayFabService.UpdateCloudScript(_commandArgs.ToTitleId, _cloudScriptData.Data,
                                                     (success) =>
                    {
                        //if (!success)
                        //{
                        //    Console.WriteLine("Save CloudScript Failed.");
                        //    _cts.Cancel();
                        //}
                        _cloudScriptData.Data = null;
                    });
                }
                #endregion
                break;

            case States.Files:
                #region Update Content Files
                //Start by creating a temp directory
                var path = AppDomain.CurrentDomain.BaseDirectory + "temp";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                if (!_cdnData.FromProcessed)
                {
                    PlayFabService.GetContentList(_commandArgs.FromTitleId, (success, data) =>
                    {
                        if (!success)
                        {
                            Console.WriteLine("Error Fetching CloudScript Data, skipping");
                            SetNextState();
                            return;
                        }
                        _cdnData.Data          = data;
                        _cdnData.FromProcessed = true;
                    });
                }

                if (!_cdnData.ToProcessed && _cdnData.FromProcessed)
                {
                    if (_cdnData.Data.Count == 0 && _cdnData.FileList.Count == 0)
                    {
                        _cdnData.ToProcessed = true;
                        _cdnData.Data        = null;
                        _cdnData.FileList    = null;
                        Directory.Delete(path, true);
                        SetNextState();
                        break;
                    }

                    if (_cdnData.Data.Count > 0)
                    {
                        var isDone = false;

                        PlayFabService.DownloadFile(_commandArgs.FromTitleId, path, _cdnData.popData(), (success, filePath, data) =>
                        {
                            if (success)
                            {
                                _cdnData.FileList.Add(new CdnFileDataMigration.UploadFile()
                                {
                                    Data     = data,
                                    FilePath = filePath
                                });
                            }
                            isDone = true;
                        });
                        do
                        {
                            //block until done.
                        } while (!isDone);
                        break;
                    }

                    if (_cdnData.Data.Count == 0 && _cdnData.FileList.Count > 0)
                    {
                        var isUploadDone = false;
                        PlayFabService.UploadFile(_commandArgs.ToTitleId, _cdnData.popFileList(), (success) =>
                        {
                            isUploadDone = true;
                        });
                        do
                        {
                            //Block until this file upload is done.
                        } while (!isUploadDone);
                    }
                }
                #endregion
                break;

            case States.Catalogs:
                #region Update Catalog
                if (_catalogData.FromProcessed && _catalogData.ToProcessed)
                {
                    SetNextState();
                    break;
                }

                if (!_catalogData.FromProcessed)
                {
                    //TODO: Make this support multiple catalogs
                    Console.WriteLine("Getting Catalog Data for Main Catalog only");
                    PlayFabService.GetCatalogData(_commandArgs.FromTitleId,
                                                  (success, catalogVersion, catalog) =>
                    {
                        if (!success)
                        {
                            Console.WriteLine("Error Fetching CloudScript Data, skipping");
                            SetNextState();
                        }
                        _catalogData.Data           = catalog;
                        _catalogData.CatalogVersion = catalogVersion;
                        _catalogData.FromProcessed  = true;
                    });
                }

                if (!_catalogData.ToProcessed && _catalogData.FromProcessed)
                {
                    Console.WriteLine("Updating Catalog on Title: " + _commandArgs.ToTitleId);
                    PlayFabService.UpdateCatalogData(_commandArgs.ToTitleId, _catalogData.CatalogVersion, true, _catalogData.Data,
                                                     (success) =>
                    {
                        if (!success)
                        {
                            Console.WriteLine("Save Catalog Failed, skipping.");
                            SetNextState();
                        }
                        _catalogData.Data           = null;
                        _catalogData.CatalogVersion = null;
                        _catalogData.ToProcessed    = true;
                    });
                }
                #endregion
                break;

            case States.Stores:
                #region Update Stores
                if (_storeData.IsComplete)
                {
                    _storeData.Data           = null;
                    _storeData.MarketingModel = null;
                    _storeData.StoreId        = null;
                    _storeData.CatalogVersion = null;
                    SetNextState();
                    break;
                }

                if (!_storeData.FromProcessed)
                {
                    if (_storeData.StoreList.Count == 0)
                    {
                        SetNextState();
                        break;
                    }
                    var currentStoreId = _storeData.popStoreId();
                    Console.WriteLine("Getting Store Data for StoreID: " + currentStoreId);
                    PlayFabService.GetStoreData(_commandArgs.FromTitleId, currentStoreId,
                                                (success, catalogVersion, storeId, marketingModel, store) =>
                    {
                        if (!success)
                        {
                            Console.WriteLine("Error Fetching Store Data, Skipping.");
                            SetNextState();
                        }
                        _storeData.FromProcessed  = true;
                        _storeData.Data           = store;
                        _storeData.CatalogVersion = catalogVersion;
                        _storeData.StoreId        = storeId;
                        _storeData.MarketingModel = marketingModel;
                    });
                }

                if (!_storeData.ToProcessed && _storeData.FromProcessed)
                {
                    var currentStoreId = _storeData.StoreId;
                    PlayFabService.UpdateStoreData(_commandArgs.ToTitleId, _storeData.StoreId, _storeData.CatalogVersion, _storeData.MarketingModel, _storeData.Data,
                                                   (success) =>
                    {
                        if (!success)
                        {
                            Console.WriteLine("Save Store Failed,  Skipping.");
                            SetNextState();
                        }
                        _storeData.Data           = null;
                        _storeData.CatalogVersion = null;

                        if (_storeData.StoreList.Count == 0)
                        {
                            _storeData.ToProcessed = true;
                            _storeData.IsComplete  = true;
                        }
                        else
                        {
                            _storeData.ToProcessed   = false;
                            _storeData.FromProcessed = false;
                        }
                    });
                }
                #endregion
                break;

            case States.DropTables:
                #region Update Drop Tables
                if (_droptableData.FromProcessed && _droptableData.ToProcessed)
                {
                    SetNextState();
                    break;
                }

                if (!_droptableData.FromProcessed)
                {
                    //TODO: Make this support multiple catalogs
                    Console.WriteLine("Getting Drop Table Data for Main Catalog only");
                    PlayFabService.GetDropTableData(_commandArgs.FromTitleId,
                                                    (success, catalog) =>
                    {
                        if (!success)
                        {
                            Console.WriteLine("Error Fetching CloudScript Data, skipping");
                            SetNextState();
                        }
                        _droptableData.Data = new Queue <List <RandomResultTable> >();

                        Dictionary <string, RandomResultTableListing> listing = new Dictionary <string, RandomResultTableListing>(catalog);

                        List <RandomResultTable> thisList = new List <RandomResultTable>();

                        foreach (var v in listing)
                        {
                            bool referencesDropTable = false;

                            foreach (var item in v.Value.Nodes)
                            {
                                if (item.ResultItemType == ResultTableNodeType.TableId)
                                {
                                    referencesDropTable = true;
                                    break;
                                }
                            }

                            if (!referencesDropTable)
                            {
                                RandomResultTable table = new RandomResultTable();
                                table.TableId           = v.Value.TableId;
                                table.Nodes             = v.Value.Nodes;

                                thisList.Add(table);
                            }
                        }

                        if (thisList.Count != 0)
                        {
                            while (true)
                            {
                                _droptableData.Data.Enqueue(thisList);

                                foreach (var item in thisList)         //remove from pending list
                                {
                                    listing.Remove(item.TableId);
                                }

                                //Add any references
                                List <RandomResultTable> nextList = new List <RandomResultTable>();

                                foreach (var item in listing)
                                {
                                    bool referencesCurrent = false;

                                    foreach (var node in item.Value.Nodes)
                                    {
                                        if (node.ResultItemType == ResultTableNodeType.TableId && thisList.Any(x => x.TableId == node.ResultItem))
                                        {
                                            referencesCurrent = true;
                                            break;
                                        }
                                    }

                                    if (referencesCurrent)
                                    {
                                        RandomResultTable table = new RandomResultTable();
                                        table.TableId           = item.Value.TableId;
                                        table.Nodes             = item.Value.Nodes;

                                        nextList.Add(table);
                                    }
                                }

                                if (nextList.Count == 0)
                                {
                                    break;
                                }

                                thisList = nextList;
                            }
                        }

                        _droptableData.FromProcessed = true;
                    });
                }

                if (!_droptableData.ToProcessed && _droptableData.FromProcessed)
                {
                    Console.WriteLine("Updating Drop Tables on Title: " + _commandArgs.ToTitleId);
                    PlayFabService.UpdateDropTableData(_commandArgs.ToTitleId, _droptableData.Data.Dequeue(),
                                                       (success) =>
                    {
                        if (!success)
                        {
                            Console.WriteLine("Save Catalog Failed, skipping.");
                            SetNextState();
                        }
                        //_catalogData.Data = null;
                        //_catalogData.CatalogVersion = null;

                        if (_droptableData.Data.Count == 0)
                        {
                            _droptableData.Data        = null;
                            _droptableData.ToProcessed = true;
                        }
                    });
                }
                #endregion
                break;

            case States.Complete:
                Console.WriteLine("Migration Complete.");
                Console.ForegroundColor = ConsoleColor.White;
                PackageManagerService.SetState(MainPackageStates.Idle);
                _cts.Cancel();
                break;
            }
        }
        async public Task MigrateCurrencyAsync(string sourceTitleID, string targetTitleID, bool forceOverWrite = true)
        {
            var console = new ConsoleTaskWriter("# Migrating currency data");

            // - FETCH

            // Get data from both titles for comparison
            ListVirtualCurrencyTypesResult[] results = await Task.WhenAll(
                PlayFabService.GetCurrencyData(sourceTitleID),
                PlayFabService.GetCurrencyData(targetTitleID)
                );

            List <VirtualCurrencyData> sourceData = results[0].VirtualCurrencies ?? new List <VirtualCurrencyData>();
            List <VirtualCurrencyData> targetData = results[1].VirtualCurrencies ?? new List <VirtualCurrencyData>();

            // - DELETE

            // Find all items in the target that don't exist in the source
            List <VirtualCurrencyData> dataToBeDeleted = targetData.FindAll((PlayFab.AdminModels.VirtualCurrencyData targetCurrency) => {
                var delete = true;
                foreach (VirtualCurrencyData sourceCurrency in sourceData)
                {
                    if (sourceCurrency.CurrencyCode == targetCurrency.CurrencyCode)
                    {
                        delete = false;
                    }
                }
                return(delete);
            });

            // Delete data
            if (dataToBeDeleted.Count > 0)
            {
                console.LogProcess("Deleting " + dataToBeDeleted.Count + " items");

                var deletedResult = await PlayFabService.DeleteCurrencyData(targetTitleID, dataToBeDeleted);

                if (deletedResult == null)
                {
                    console.LogError("Deleting currency data failed.");
                    return;
                }
            }

            // - UPDATE

            // Find all items in the source data that don't match target or doesn't exist
            List <VirtualCurrencyData> dataThatNeedsUpdate = sourceData.FindAll((PlayFab.AdminModels.VirtualCurrencyData sourceCurrency) => {
                var needsUpdate = true;
                foreach (VirtualCurrencyData targetCurrency in targetData)
                {
                    if (targetCurrency.CurrencyCode == sourceCurrency.CurrencyCode && targetCurrency.Equals(sourceCurrency))
                    {
                        needsUpdate = false;
                    }
                }
                return(needsUpdate);
            });

            if (dataThatNeedsUpdate.Count == 0)
            {
                console.LogSuccess("Found no data to be updated");
                return;
            }

            // Update data
            if (dataThatNeedsUpdate.Count > 0 || forceOverWrite)
            {
                console.LogProcess("Updating " + dataThatNeedsUpdate.Count + " items");

                var updatedResult = await PlayFabService.UpdateCurrencyData(targetTitleID, dataThatNeedsUpdate);

                if (updatedResult == null)
                {
                    console.LogError("Updating currency data failed.");
                    return;
                }
            }

            console.LogSuccess("Completed Migration of currency data");
        }