教程:使用 CloudScript 上下文模型
PlayFab 通过多种机制执行脚本,包括通过 API 执行、通过计划任务、通过 PlayStream 事件执行脚本,以及当玩家进入和退出段时执行脚本。 在许多情况下,脚本执行的上下文对于脚本的运行方式非常重要。 此示例是了解代表其运行脚本的玩家的玩家 ID。 运行脚本的上下文确定可用的数据模型,并提供脚本中使用的上下文特定数据。
本教程介绍如何:
- 使用共享上下文模型
- 通过 ExecuteFunction API 执行时使用上下文模型。
- 通过计划任务执行时使用上下文模型。
- 在播放器的上下文中执行时使用上下文模型。
- 在实体的上下文中执行时使用上下文模型。
使用共享游戏身份验证上下文模型
无论执行脚本的方法如何,始终都会提供游戏身份验证上下文。 这包括用于执行脚本的游戏 ID 和实体令牌(有关更多详细信息,请参阅 GetEntityToken )。 了解此上下文后,可以使用服务器 API 在脚本中对 PlayFab 进行其他 API 调用。
// Shared models
public class TitleAuthenticationContext
{
public string Id { get; set; }
public string EntityToken { get; set; }
}
通过 ExecuteFunction API 执行时使用上下文模型
使用 ExecuteFunction API 执行脚本时,提供的上下文包括以下信息:
- 调用方的实体配置文件
- 游戏身份验证上下文
- 一个布尔值,指示是否将 PlayStream 事件作为正在执行的函数的一部分发送
- 调用脚本时使用的函数参数
// 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>
{
}
通过计划任务执行时使用上下文模型
计划任务执行脚本时,提供的上下文包括以下信息:
- 计划任务名称 ID
- 包含 PlayStream 事件堆栈的事件历史记录
- 标题身份验证上下文
- 一个布尔值,指示是否将 PlayStream 事件作为正在执行的函数的一部分发送
- 调用脚本时使用的函数参数
// 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>
{
}
在播放器的上下文中执行时使用上下文模型
通过 Player PlayStream 事件执行脚本时,输入或离开段或作为基于细分的计划任务的一部分,所提供的上下文包括以下信息:
- 玩家个人资料
- 指示播放器配置文件是否被截断的布尔值。
- 如果玩家档案超过 2048 字节,则将被截断。 如果发生这种情况,则需要使用配置文件 API(服务器、客户端或实体 API)来检索完整配置文件。
- 触发脚本的 PlayStream 事件。
- 一个布尔值,指示是否将 PlayStream 事件作为正在执行的函数的一部分发送
- 调用脚本时使用的函数参数
// 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>
{
}
在通过 Entity PlayStream 事件执行、输入或离开实体段或作为基于实体段的计划任务的一部分时使用上下文模型。
通过 Entity PlayStream 事件执行脚本、输入或离开实体段或作为基于实体段的计划任务的一部分时,提供的上下文包括以下信息:
- 实体配置文件
- 触发脚本的 PlayStream 事件。
- 一个布尔值,指示是否将 PlayStream 事件作为正在执行的函数的一部分发送
// 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>
{
}
注意
可以下载完整的使用 Azure Functions 的 CloudScript。