> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用 Azure Functions 的 PlayFab CloudScript 上下文模型

> PlayFab CloudScript 通过 ExecuteFunction、计划任务、玩家或实体运行时暴露给 Azure Functions 的上下文模型参考。

# 教程:使用 CloudScript 上下文模型

PlayFab 通过多种机制执行脚本,包括通过 API 执行、通过计划任务执行、通过 PlayStream 事件执行,以及在玩家进入和退出分段时执行。在许多情况下,脚本执行时所处的上下文对于它的运行方式非常重要。一个例子是知道正在为其运行脚本的玩家的 Player ID。运行脚本的上下文决定了可用的数据模型,并提供了脚本中使用的特定于上下文的数据。

在本教程中,你将学习如何:

<Note>
  \[!div class="checklist"]
</Note>

>

<Note>
  * 使用共享的上下文模型 \* 通过 ExecuteFunction API 执行时使用上下文模型。\* 通过计划任务执行时使用上下文模型。\* 在玩家上下文中执行时使用上下文模型。\* 在实体上下文中执行时使用上下文模型。
</Note>

## 使用共享的 Title Authentication 上下文模型

无论使用何种方法执行脚本,始终会提供 Title Authentication 上下文。这包括用于执行脚本的 Title ID 和 Entity Token(有关更多详细信息,请参阅 [GetEntityToken](https://learn.microsoft.com/en-us/rest/api/playfab/authentication/authentication/get-entity-token))。了解此上下文可让你在脚本中使用 Server API 对 PlayFab 进行额外的 API 调用。

```C# theme={null}
// Shared models
public class TitleAuthenticationContext
{
    public string Id { get; set; }
    public string EntityToken { get; set; }
}
```

## 通过 ExecuteFunction API 执行时使用上下文模型

当你使用 [ExecuteFunction API](https://learn.microsoft.com/en-us/rest/api/playfab/cloudscript/server-side-cloud-script/execute-function) 执行脚本时,提供的上下文包括以下信息:

* 调用方的实体配置文件
* Title Authentication 上下文
* 一个布尔值,指示是否在执行的函数一部分中发送 PlayStream 事件
* 调用脚本时使用的函数参数

```C# theme={null}
// Models via ExecuteFunction API
public class FunctionExecutionContext<T>
{
    public PlayFab.ProfilesModels.EntityProfileBody CallerEntityProfile { get; set; }
    public TitleAuthenticationContext TitleAuthenticationContext { get; set; }
    public bool? GeneratePlayStreamEvent { get; set; }
    public T FunctionArgument { get; set; }
}

public class FunctionExecutionContext : FunctionExecutionContext<object>
{
}
```

## 通过计划任务执行时使用上下文模型

当你使用[计划任务](/services/playfab/data-analytics/acting-data/scheduled-tasks)执行脚本时,提供的上下文包括以下信息:

* 计划任务的名称 ID
* 包含 PlayStream 事件堆栈的事件历史
* Title Authentication 上下文
* 一个布尔值,指示是否在执行的函数一部分中发送 PlayStream 事件
* 调用脚本时使用的函数参数

```C# theme={null}
// Models via Scheduled task
public class PlayStreamEventHistory
{
    public string ParentTriggerId { get; set; }
    public string ParentEventId { get; set; }
    public bool TriggeredEvents { get; set; }
}

public class ScheduledTaskFunctionExecutionContext<T>
{
    public PlayFab.CloudScriptModels.NameIdentifier ScheduledTaskNameId { get; set; }
    public Stack<PlayStreamEventHistory> EventHistory { get; set; }
    public TitleAuthenticationContext TitleAuthenticationContext { get; set; }
    public bool? GeneratePlayStreamEvent { get; set; }
    public T FunctionArgument { get; set; }
}

public class ScheduledTaskFunctionExecutionContext : ScheduledTaskFunctionExecutionContext<object>
{
}
```

## 在玩家上下文中执行时使用上下文模型

在通过玩家 PlayStream 事件、进入或离开分段,或作为基于分段的计划任务的一部分执行脚本时,提供的上下文包括以下信息:

* 玩家配置文件
* 指示玩家配置文件是否被截断的布尔值。
  * 如果玩家配置文件超过 2048 字节,它将被截断。发生这种情况时,你需要使用配置文件 API(服务器、客户端或实体 API)来检索完整的配置文件。
* 触发脚本的 PlayStream 事件。
* 一个布尔值,指示是否在执行的函数一部分中发送 PlayStream 事件
* 调用脚本时使用的函数参数

```C# theme={null}
// Models via Player PlayStream event, entering or leaving a 
// player segment or as part of a player segment based scheduled task.
public class PlayerPlayStreamFunctionExecutionContext<T>
{
    public PlayFab.CloudScriptModels.PlayerProfileModel PlayerProfile { get; set; }
    public bool PlayerProfileTruncated { get; set; }
    public PlayFab.CloudScriptModels.PlayStreamEventEnvelopeModel PlayStreamEventEnvelope { get; set; }
    public TitleAuthenticationContext TitleAuthenticationContext { get; set; }
    public bool? GeneratePlayStreamEvent { get; set; }
    public T FunctionArgument { get; set; }
}

public class PlayerPlayStreamFunctionExecutionContext : PlayerPlayStreamFunctionExecutionContext<object>
{
}
```

## 通过实体 PlayStream 事件、进入或离开实体分段,或作为基于实体分段的计划任务的一部分执行时使用上下文模型

在通过实体 PlayStream 事件、进入或离开实体分段,或作为基于实体分段的计划任务的一部分执行脚本时,提供的上下文包括以下信息:

* 实体配置文件
* 触发脚本的 PlayStream 事件。
* 一个布尔值,指示是否在执行的函数一部分中发送 PlayStream 事件

```C# theme={null}
// Models via Entity PlayStream event, entering or leaving an 
// entity segment or as part of an entity segment based scheduled task.
public class EventFullName
{
    public string Name { get; set; }
    public string Namespace { get; set; }
}

public class OriginInfo
{
    public string Id { get; set; }
    public DateTime? Timestamp { get; set; }
}

public class EntityPlayStreamEvent<T>
{
    public string SchemaVersion { get; set; }
    public EventFullName FullName { get; set; }
    public string Id { get; set; }
    public DateTime Timestamp { get; set; }
    public PlayFab.CloudScriptModels.EntityKey Entity { get; set; }
    public PlayFab.CloudScriptModels.EntityKey Originator { get; set; }
    public OriginInfo OriginInfo { get; set; }
    public T Payload { get; set; }
    public PlayFab.ProfilesModels.EntityLineage EntityLineage { get; set; }
}

public class EntityPlayStreamEvent : EntityPlayStreamEvent<object>
{
}

public class EntityPlayStreamFunctionExecutionContext<TPayload, TArg>
{
    public PlayFab.ProfilesModels.EntityProfileBody CallerEntityProfile { get; set; }
    public EntityPlayStreamEvent<TPayload> PlayStreamEvent { get; set; }
    public TitleAuthenticationContext TitleAuthenticationContext { get; set; }
    public bool? GeneratePlayStreamEvent { get; set; }
    public TArg FunctionArgument { get; set; }
}

public class EntityPlayStreamFunctionExecutionContext : EntityPlayStreamFunctionExecutionContext<object, object>
{
}
```

<Note>
  你可以下载完整的[使用 Azure Functions 的 CloudScript 帮助程序类](https://github.com/PlayFab/PlayFab-Samples/blob/master/Samples/CSharp/AzureFunctions/CS2AFHelperClasses.cs)。
</Note>


## Related topics

- [使用 Azure Functions 的 PlayFab CloudScript 快速入门指南](/zh-CN/services/playfab/live-service-management/service-gateway/automation/cloudscript-af/quickstart.md)
- [使用 Azure Functions 的 PlayFab CloudScript](/zh-CN/services/playfab/live-service-management/service-gateway/automation/cloudscript-af/index.md)
- [PlayFab 在线运营管理文档](/zh-CN/services/playfab/live-service-management/index.md)
- [PFCloudScriptExecuteFunctionAsync](/zh-CN/services/playfab/api-references/c/pfcloudscript/functions/pfcloudscriptexecutefunctionasync.md)
- [编写自定义 CloudScript](/zh-CN/services/playfab/live-service-management/service-gateway/automation/cloudscript/writing-custom-cloudscript.md)
