PlayCompute.psm1

function Get-PFQosServer
{
    [CmdletBinding( )]
    Param()
    
    Begin
    {}

    Process
    {

        $task = [PlayFab.PLayFabEntityAPI]::ListQosServersAsync($null)
        $task.Wait()
        $result = $task.Result.Result.QoSServers
        $result
     }

     <#
.SYNOPSIS
Gets a list of QoS servers.
 
.DESCRIPTION
Returns list of QoS servers to use for network performance measurements and region selection during allocation.
#>

}

function Get-PFTitleEntityToken
{
    [CmdletBinding( )]
    Param(
    [Parameter(Mandatory = $true)][string]$TitleID, 
    [Parameter(Mandatory = $true)][string]$SecretKey 
    )
    
    Begin
    {}

    Process
    {
 
        #Sets the public properties of the API's static class that configure which title target via entity tokens
        [PlayFab.PlayFabSettings]::TitleID = $TitleID
        [PlayFab.PlayFabSettings]::DeveloperSecretKey = $SecretKey

        $key = new-object PlayFab.EntityModels.EntityKey
        $key.ID = $TitleID
        
        $tokenRequest = new-object PLayFab.EntityModels.GetEntityTokenRequest
        $tokenRequest.Entity = $key

        $tokenTask = [PLayFab.PlayFabEntityAPI]::GetEntityTokenAsync($tokenRequest);
        $tokenTask.Wait()
        $result = $tokenTask.Result.Result.EntityToken
        $result
        
     }
     <#
.SYNOPSIS
Gets an entity token using the provided title and secret key. Required for other Entity API interactions.
 
.DESCRIPTION
Using a secret key generated in Game Manager, this cmdlet generates and entity token for the specified title id. The entity token will be used for authenticating other PlayCompute cmdlets for the length of the PowerShell session.
#>

}

function Get-PFGameAsset
{
    [CmdletBinding( )]
    Param()
    
    Begin
    {}

    Process
    {
        
            $task = [PlayFab.PLayFabEntityAPI]::ListAssetsAsync($null)
            $task.Wait()
            $result = $task.Result.Result.AssetSummaries
            $result
     }

     <#
.SYNOPSIS
 
Gets the game server assets that have been uploaded
 
.DESCRIPTION
 
Gets the game server assets that have been added through Add-PFGameAssets or GetAssetUploadURL API. Command is run in the context of the title specified using Get-PFTitleEntityToken
#>

}

function Add-PFGameAsset
{
    [CmdletBinding( )]
    Param(
    [Parameter(Mandatory = $true)][string]$Name, 
    [Parameter(Mandatory = $true)][string]$FilePath 
    )
    
    Begin
    {}

    Process
    {

        $assetReq = NEW-OBJECT PlayFab.EntityModels.GetAssetUploadUrlRequest
        $assetReq.Name =  $Name


        $metadata = New-Object 'System.Collections.Generic.Dictionary[String,String]'
        $metadata.Add("OriginFilePath",$FilePath)
        $assetReq.MetaData = $MetaData

        $assetTask = [PlayFab.PlayFabEntityAPI]::GetAssetUploadUrlAsync($assetReq);
        $assetTask.Wait()
        
        $sastoken = $assetTask.Result.Result.AssetUploadUrl
        
        $sastoken = $sastoken.Remove($sastoken.LastIndexOf("&api"))
        $storageaccountname = $sastoken.SubString(8,$sastoken.IndexOf("blob")-9)
        $sastoken = $sastoken.substring($sastoken.IndexOf("sv"))

        $accountContext = New-AzureStorageContext -SasToken $sasToken -StorageAccountName   $storageaccountname 
        ## $blob = Get-AzureStorageBlob -Container "gameassets" -Blob $ID -Context $accountContext
        Set-AzureStorageBlobContent -File $FilePath -Container "gameassets" -Context $accountContext -Blob $ID
                
     }

          <#
.SYNOPSIS
Uploads an asset to PlayFab.
 
.DESCRIPTION
Upload an asset (commonly a zip file) by providing a friendly name and file path. This cmdlet uses the GetAssetUploadURl API to get an Azure blob URL, and then uses Azure storage cmdlets to upload the asset.
#>


}

function Add-PFGameCertificate
{
    [CmdletBinding( )]
    Param(
    [Parameter(Mandatory = $true)][string]$Name, 
    [Parameter(Mandatory = $true)][string]$FilePath 
    )
    
    Begin
    {}

    Process
    {
        $certificateBytes = [System.IO.File]::ReadAllBytes($FilePath)
        $base64 = [System.Convert]::ToBase64String($certificateBytes)

        $cert = NEW-OBJECT PlayFab.EntityModels.Certificate
        $cert.Base64EncodedValue = $base64 
        $cert.Name = $Name

        $certReq = NEW-OBJECT PlayFab.EntityModels.UploadCertificateRequest
        $certReq.GameCertificate = $cert

        $certTask = [PlayFab.PlayFabEntityAPI]::UploadCertificateAsync($certReq);
        $certTask.Wait();
        $certTask.Result;
     }

          <#
.SYNOPSIS
Uploads a certificate to PlayFab.
 
.DESCRIPTION
Uploads a certificate to PlayFab for game server usage. Cmdlet does not support certificates with passwords but coming soon.
#>


}

function Get-PFGameCertificate
{
    [CmdletBinding( )]
    Param()
    
    Begin
    {}

    Process
    {
        
            $task = [PlayFab.PLayFabEntityAPI]::ListCertificatesAsync($null)
            $task.Wait()
            $result = $task.Result.Result.CertificateSummaries
            $result
     }

     <#
.SYNOPSIS
 
Gets the game server certificates that have been uploaded
 
.DESCRIPTION
 
Gets the game server Certificate that have been added through Add-PFGameCertificate.
#>

}


##Export cmdlet and alias to module

New-Alias GPFQ Get-PFQosServer
Export-ModuleMember -Alias GPFQ -Function Get-PFQosServer

New-Alias GPFTET Get-PFTitleEntityToken
Export-ModuleMember -Alias GPFTET -Function Get-PFTitleEntityToken

New-Alias GPFGA Get-PFGameAsset
Export-ModuleMember -Alias GPFGA -Function Get-PFGameAsset

New-Alias APFGA Add-PFGameAsset
Export-ModuleMember -Alias APFGA -Function Add-PFGameAsset

New-Alias APFGC Add-PFGameCertificate
Export-ModuleMember -Alias APFGC -Function Add-PFGameCertificate

New-Alias GPFGC Get-PFGameCertificate
Export-ModuleMember -Alias GPFGC -Function Get-PFGameCertificate