-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetMultiplayerServer.cs
118 lines (100 loc) · 4.51 KB
/
GetMultiplayerServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Amazon;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DocumentModel;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using PlayFab;
using PlayFab.AuthenticationModels;
using PlayFab.ClientModels;
using PlayFab.MultiplayerModels;
using PlayFab.CloudScriptModels;
namespace Company.Function
{
public static class GetMultiplayerServer
{
[FunctionName("GetMultiplayerServer")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
PlayFabSettings.staticSettings.TitleId = Environment.GetEnvironmentVariable("PLAYFAB_TITLE_ID", EnvironmentVariableTarget.Process);
PlayFabSettings.staticSettings.DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEVELOPER_SECRET_KEY", EnvironmentVariableTarget.Process);
// Getting arguments from the caller (on the client-side)
var context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
dynamic args = context.FunctionArgument;
if (args != null && args["buildId"] != null && args["region"] != null)
{
string buildId = args["buildId"];
string region = args["region"];
var entityTokenRequest = new GetEntityTokenRequest();
await PlayFabAuthenticationAPI.GetEntityTokenAsync(entityTokenRequest);
// When I requested for a new server, I stored the session data in my database. "document" here is a data entry from in my database.
// This can vary according to your games needs.
if (document.ContainsKey("sessionId"))
{
string sessionId = document["sessionId"];
var requestData = new GetMultiplayerServerDetailsRequest
{
BuildId = buildId,
SessionId = sessionId,
Region = region
};
try
{
var response = await PlayFabMultiplayerAPI.GetMultiplayerServerDetailsAsync(requestData);
return new OkObjectResult(response.Result);
}
catch (Exception e)
{
log.LogError(e.Message);
}
}
else
{
var requestData = new RequestMultiplayerServerRequest
{
BuildId = buildId,
SessionId = Guid.NewGuid().ToString(),
PreferredRegions = new List<string> { "USWest" } // Enter your preferred regions here
};
log.LogInformation(requestData.ToString());
try
{
var response = await PlayFabMultiplayerAPI.RequestMultiplayerServerAsync(requestData);
// Updating my database when a new session is created. This is dependent on your strategy.
if (!document.ContainsKey("sessionId")) document.Add("sessionId", response.Result.SessionId);
await table.UpdateItemAsync(document);
return new OkObjectResult(response.Result);
}
catch (Exception e)
{
log.LogError(e.Message);
}
}
}
return new OkObjectResult(null);
}
}
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 TitleAuthenticationContext
{
public string Id { get; set; }
public string EntityToken { get; set; }
}
}