65.9K
CodeProject 正在变化。 阅读更多。
Home

面向游戏开发者的 Azure 第 3 部分:使用 Microsoft Game Stack 进行 LiveOps 和分析

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2021 年 10 月 12 日

CPOL

5分钟阅读

viewsIcon

4188

在本文中,我们将探讨如何使用 PlayFab 添加事件并从我们的游戏中收集数据,以用于 LiveOps 和分析。

Microsoft Game Stack 是一个工具和服务集合,包括 Microsoft Azure、PlayFab、Xbox Services 和 Visual Studio 等工具。各种规模的游戏开发者,从独立工作室到 AAA 团队,都可以使用这些工具来构建和发布出色的游戏。游戏部署后,除非能够衡量,否则很难知道应该改进游戏的哪些功能或方面。建立这种理解是管理游戏的 LiveOps 方法的核心。

在本文中,我们将使用 PlayFab AnalyticsPlayFab Experiments 为上一部分系列的贪吃蛇游戏添加自定义事件和仪器。我们将了解这些数据如何成为游戏 LiveOps 之旅的一部分。

要求

要跟随本指南,您需要一个 GitHub 帐户、一个 PlayFab 帐户以及在您的计算机上安装的以下软件

您还需要克隆 贪吃蛇游戏。请按照本系列 上一部分中的步骤将其克隆到您的计算机并在本地运行。

使用 PlayFab 配置游戏

要开始使用 PlayFab,我们需要做的第一件事是 登录并创建一个工作室。我们将通过添加一个名称并选择 PlayFab 作为身份验证提供商来完成此操作。

创建工作室将为您创建一个名为 My Game 的占位符游戏标题。请记下游戏标题中的 ID 并将其复制到某处,因为我们将在 PlayFab SDK 中使用该 ID。

添加自定义匿名游戏会话

接下来,让我们开始使用 PlayFab 的 JavaScript SDK 跟踪唯一的游戏会话。

使用 Visual Studio Code 打开项目,并将 PlayFab SDK 导入到游戏页面,方法是在“app/views/game.html”的 <head> 标记内添加以下代码段。

<script src="https://download.playfab.com/PlayFabClientApi.js"></script>

<head> 标记应如下所示

<html>
    <head>
        <title>Snake</title>
        <link rel="stylesheet" type="text/css" href="css/global.css">
        <link rel="stylesheet" type="text/css" href="css/game.css">
        <link rel="stylesheet" type="text/css" href="css/settings.css">
        <script src="https://download.playfab.com/PlayFabClientApi.js"></script>
    </head>
    <body>
        <a id="top"></a>
...

然后打开“public/js/main.js”文件,并将此代码添加到页面底部,位于游戏控制器下方,基于 PlayFab 的 JavaScript SDK 示例 HTML 文件。将 id 变量设置为您的 PlayFab 游戏标题 ID。

PlayFabLogin();

function CreateGUID()
{
    // http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);});
}

function PlayFabLogin()
{
    var id = "YOUR-TITLE-ID";
    var GUID = localStorage.userId ? localStorage.userId : CreateGUID();

    if( !id || id == "" )
    {
        console.log( "TitleId cannot be null" );
        return;
    }
    else if( typeof PlayFab == 'undefined' ) // make sure we have the SDK prior to calling / setting
    {
        console.log( "The PlayFab SDK could not be found. Double-check your script sources" );
        return;
    }

    // save these values locally to ease use
    localStorage.userId = GUID;
    localStorage.titleId = id;

    // save title id
    PlayFab.settings.titleId = id;

    // build http request object for LoginWithCustomId
    var LoginWithCustomIdRequest = {};
    LoginWithCustomIdRequest.TitleId = id;
    LoginWithCustomIdRequest.CustomId = GUID;
    LoginWithCustomIdRequest.CreateAccount = true;

    console.log( "Logging into PlayFab..." );
    PlayFabClientSDK.LoginWithCustomID( LoginWithCustomIdRequest, ( response, error ) => {
        if( error )
        {
            console.log( error );
        }
        else
        {
            // display account details
            var result = response.data;
            var status = "Player: " + result.PlayFabId + " Session ticket: " + result.SessionTicket;
            console.log( status );
        }
    });
};

现在,如果您在本地运行贪吃蛇游戏并在 https://:3000 上打开它,则登录信息应显示在 PlayFab 仪表板的 **PlayStream Monitor** 页面上。

报告自定义游戏事件

接下来,让我们对代码进行各种事件的仪器化,以便我们可以在 PlayFab 仪表板中查看这些信息。我们将看到这些信息与 PlayFab 自动跟踪的默认事件一起显示。

例如,如果您想在客户端网页中添加一个简单的事件,可以在成功的 PlayFabLogin 中添加这段代码来调用 WriteTitleEvent。它接受一个 JSON 对象作为参数,并且只需要一个 EventName 字段,我们将将其命名为“test_event”。

PlayFabClientSDK.WriteTitleEvent( {
    EventName: "test_event",
}, ( response, error ) => {
    if( error ) {
        console.log( error );
    }
    else {
        console.log( response );
    }
});

将此事件添加到登录函数中,它将与 PlayStream 中的登录事件一起显示。

我们还在后端服务中添加一些事件。我们需要一个标题的服务器端密钥,因此请从 PlayFab 仪表板打开 **Title settings** 页面,然后导航到 **Secret Keys**,找到页面上的 **Secret key**,或者在没有的情况下创建一个。

从终端窗口将 PlayFab Node.js SDK 添加到贪吃蛇游戏中

npm install playfab-sdk

打开“app/services/player-service.js”,并将以下 PlayFab 配置插入到文件顶部附近,紧随其他 require 调用之后。将其设置为您的标题 ID 和密钥。

const { PlayFab, PlayFabServer } = require( "playfab-sdk" );
PlayFab.settings.titleId = "YOUR-TITLE-ID";
PlayFab.settings.developerSecretKey = "YOUR-SECRET-KEY";

然后,让我们在要跟踪的事件中添加一些 WriteTitleEvent 调用,例如 changeColor 函数中的“change_color”,或者 changePlayerName 函数。

changeColor(socket) {
    const player = this.playerContainer.getPlayer(socket.id);
    const newColor = this.colorService.getColor();
    this.colorService.returnColor(player.color);
    player.color = newColor;
    this.playerStatBoard.changePlayerColor(player.id, newColor);
    socket.emit(ServerConfig.IO.OUTGOING.NEW_PLAYER_INFO, player.name, newColor);
    this.notificationService.broadcastNotification(`${player.name} has changed colors.`, newColor);

    PlayFabServer.WriteTitleEvent( {
        EventName: "change_color",
        Custom: {
            "Name": player.name
        }
    }, ( response, error ) => {
        if( error ) {
            console.log( error );
        }
        else {
            console.log( response );
        }
    });
}

现在,当您运行游戏服务器并尝试更改颜色或玩家姓名时,您将在 PlayStream 界面上实时看到事件的显示。

使用 PlayFab Experiments

既然我们可以在实时查看分析数据,那么让我们快速了解一下如何使用 PlayFab Experiments 来运行各种 A/B 测试,而无需每次都部署新代码。

PlayFab Experiments 使运行这些测试变得非常容易,因为它为我们处理了所有用户分段和分配。我们只需要在登录时使用 PlayFab 的配置数据,并根据数据调整游戏。

在 PlayFab 仪表板中,转到 **Experiments** 并创建一个名为“Color Test”的新实验。对于 **Control Variant**,添加一个名为“Color”的 **Control Variable**,并将其值设置为“red”。向 **Treatment Variant** 添加一个类似的 **Treatment Variable**,也命名为“Color”,并将其值设置为“blue”。

之后,滚动到底部并单击 **Run Now** 立即开始实验。几分钟后,您将在登录返回的数据中看到分配的变体和变量。

这些实验变量位于 **TreatmentAssignment** 字段中,您可以在“public/js/main.js”中像这样访问它们:

PlayFabClientSDK.LoginWithCustomID( LoginWithCustomIdRequest, ( response, error ) => {
    if( error )
    {
        console.log( error );
    }
    else
    {
        // display account details
        var result = response.data;
        console.log( result.TreatmentAssignment );
        console.log( result.TreatmentAssignment.Variables );
        if( result.TreatmentAssignment.Variables.length > 0 ) {
            document.body.style.background = result.TreatmentAssignment.Variables[ 0 ].Value;
        }
    }
});

就是这样!根据变体,您的游戏页面的背景现在应该是红色或蓝色,而不是黑色。很容易想象这也可以用于其他游戏属性的实验,例如难度级别和食物分数。

实验期结束后,生成的实验报告可以帮助我们利用真实玩家数据来决定哪个选项效果最好。

下一步是什么?

太棒了!这结束了关于使用 Microsoft Game Stack 构建多人游戏的系列。现在您已经拥有了配置和查看游戏实时分析数据以及运行实验以根据数据做出未来决策的工具和知识。

我们希望您喜欢跟随本系列,学习使用 Microsoft Game Stack 来构建和部署全球规模的游戏服务器,并了解通过如此大规模的玩家和游戏分析可能实现的潜力。

祝您游戏开发愉快!

© . All rights reserved.