From 0b03c056dfb77c0f4d9bd51cf02ef906fde44045 Mon Sep 17 00:00:00 2001 From: Hamza Lazaar Date: Fri, 5 Jul 2019 12:25:26 +0100 Subject: [PATCH 1/4] FIXED: incorrect info in comment removed --- DefaultCloudScript.js | 1 - DefaultCloudScript.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/DefaultCloudScript.js b/DefaultCloudScript.js index 0c42aac..498cae5 100644 --- a/DefaultCloudScript.js +++ b/DefaultCloudScript.js @@ -241,7 +241,6 @@ var RoomClosed = function (args) { }; handlers["RoomClosed"] = RoomClosed; // Triggered automatically when a Photon room game property is updated. -// Note: currentPlayerId is undefined in this function var RoomPropertyUpdated = function (args) { log.debug("Room Property Updated - Game: " + args.GameId); }; diff --git a/DefaultCloudScript.ts b/DefaultCloudScript.ts index bf3445b..2e5ab08 100644 --- a/DefaultCloudScript.ts +++ b/DefaultCloudScript.ts @@ -298,7 +298,6 @@ var RoomClosed = function (args): void { handlers["RoomClosed"] = RoomClosed; // Triggered automatically when a Photon room game property is updated. -// Note: currentPlayerId is undefined in this function var RoomPropertyUpdated = function (args): void { log.debug("Room Property Updated - Game: " + args.GameId); } From 99b3d0b8411cb0ce322fc4ed7ab98d7f253d93d2 Mon Sep 17 00:00:00 2001 From: Hamza Lazaar Date: Fri, 5 Jul 2019 12:26:09 +0100 Subject: [PATCH 2/4] CHANGED: EvCode was added after initial sample --- DefaultCloudScript.js | 2 +- DefaultCloudScript.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DefaultCloudScript.js b/DefaultCloudScript.js index 498cae5..098e425 100644 --- a/DefaultCloudScript.js +++ b/DefaultCloudScript.js @@ -251,7 +251,7 @@ handlers["RoomPropertyUpdated"] = RoomPropertyUpdated; var RoomEventRaised = function (args) { var eventData = args.Data; log.debug("Event Raised - Game: " + args.GameId + " Event Type: " + eventData.eventType); - switch (eventData.eventType) { + switch (eventData.eventType) { // use args.EvCode instead of embedding eventType in payload case "playerMove": processPlayerMove(eventData); break; diff --git a/DefaultCloudScript.ts b/DefaultCloudScript.ts index 2e5ab08..888f4b0 100644 --- a/DefaultCloudScript.ts +++ b/DefaultCloudScript.ts @@ -310,7 +310,7 @@ var RoomEventRaised = function (args): void { var eventData = args.Data; log.debug("Event Raised - Game: " + args.GameId + " Event Type: " + eventData.eventType); - switch (eventData.eventType) { + switch (eventData.eventType) { // use args.EvCode instead of embedding eventType in payload case "playerMove": processPlayerMove(eventData); break; From cd1e8835071407bc308c9b8e5808a543edd1100c Mon Sep 17 00:00:00 2001 From: Hamza Lazaar Date: Fri, 5 Jul 2019 12:28:53 +0100 Subject: [PATCH 3/4] CHANGED: playstream events replacing useless logs --- DefaultCloudScript.js | 102 ++++++++++++++++++++++++++++++++++++++--- DefaultCloudScript.ts | 103 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 192 insertions(+), 13 deletions(-) diff --git a/DefaultCloudScript.js b/DefaultCloudScript.js index 098e425..72abf28 100644 --- a/DefaultCloudScript.js +++ b/DefaultCloudScript.js @@ -221,36 +221,126 @@ handlers["unlockHighSkillContent"] = UnlockHighSkillContent; // This makes it easier than ever to incorporate multiplayer server logic into your game. // Triggered automatically when a Photon room is first created var RoomCreated = function (args) { - log.debug("Room Created - Game: " + args.GameId + " MaxPlayers: " + args.CreateOptions.MaxPlayers); + server.WritePlayerEvent({ + EventName: "room_created", + PlayFabId: args.UserId, + Body: { + WebHook: { + AppVersion: args.AppVersion, + Region: args.Region, + GameId: args.GameId, + Type: args.Type, + ActorNr: args.ActorNr, + CreateOptions: args.CreateOptions + } + } + }); }; handlers["RoomCreated"] = RoomCreated; // Triggered automatically when a player joins a Photon room var RoomJoined = function (args) { - log.debug("Room Joined - Game: " + args.GameId + " PlayFabId: " + args.UserId); + server.WritePlayerEvent({ + EventName: "room_joined", + PlayFabId: args.UserId, + Body: { + WebHook: { + AppVersion: args.AppVersion, + Region: args.Region, + GameId: args.GameId, + ActorNr: args.ActorNr + } + } + }); }; handlers["RoomJoined"] = RoomJoined; // Triggered automatically when a player leaves a Photon room var RoomLeft = function (args) { - log.debug("Room Left - Game: " + args.GameId + " PlayFabId: " + args.UserId); + server.WritePlayerEvent({ + EventName: "room_left", + PlayFabId: args.UserId, + Body: { + WebHook: { + AppVersion: args.AppVersion, + Region: args.Region, + GameId: args.GameId, + Type: args.Type, + ActorNr: args.ActorNr, + IsInactive: args.IsInactive + } + } + }); }; handlers["RoomLeft"] = RoomLeft; // Triggered automatically when a Photon room closes // Note: currentPlayerId is undefined in this function var RoomClosed = function (args) { - log.debug("Room Closed - Game: " + args.GameId); + server.WriteTitleEvent({ + EventName: "room_closed", + Body: { + WebHook: { + AppVersion: args.AppVersion, + Region: args.Region, + GameId: args.GameId, + Type: args.Type, + ActorCount: args.ActorCount + } + } + }); }; handlers["RoomClosed"] = RoomClosed; // Triggered automatically when a Photon room game property is updated. var RoomPropertyUpdated = function (args) { - log.debug("Room Property Updated - Game: " + args.GameId); + if (args.Type === "Game") { + server.WritePlayerEvent({ + EventName: "room_properties_updated", + PlayFabId: args.UserId, + Body: { + WebHook: { + AppVersion: args.AppVersion, + Region: args.Region, + GameId: args.GameId, + ActorNr: args.ActorNr, + Properties: args.Properties + } + } + }); + } + else { // "Actor" + server.WritePlayerEvent({ + EventName: "player_roperties_updated", + PlayFabId: args.UserId, + Body: { + WebHook: { + AppVersion: args.AppVersion, + Region: args.Region, + GameId: args.GameId, + ActorNr: args.ActorNr, + TargetActor: args.TargetActor, + Properties: args.Properties + } + } + }); + } }; handlers["RoomPropertyUpdated"] = RoomPropertyUpdated; // Triggered by calling "OpRaiseEvent" on the Photon client. The "args.Data" property is // set to the value of the "customEventContent" HashTable parameter, so you can use // it to pass in arbitrary data. var RoomEventRaised = function (args) { + server.WritePlayerEvent({ + EventName: "event_raised", + PlayFabId: args.UserId, + Body: { + WebHook: { + AppVersion: args.AppVersion, + Region: args.Region, + GameId: args.GameId, + ActorNr: args.ActorNr, + EvCode: args.EvCode + } + } + }); var eventData = args.Data; - log.debug("Event Raised - Game: " + args.GameId + " Event Type: " + eventData.eventType); switch (eventData.eventType) { // use args.EvCode instead of embedding eventType in payload case "playerMove": processPlayerMove(eventData); diff --git a/DefaultCloudScript.ts b/DefaultCloudScript.ts index 888f4b0..e8f4178 100644 --- a/DefaultCloudScript.ts +++ b/DefaultCloudScript.ts @@ -274,32 +274,109 @@ handlers["unlockHighSkillContent"] = UnlockHighSkillContent; // Triggered automatically when a Photon room is first created var RoomCreated = function (args): void { - log.debug("Room Created - Game: " + args.GameId + " MaxPlayers: " + args.CreateOptions.MaxPlayers); + server.WritePlayerEvent({ + EventName : "room_created", + PlayFabId: args.UserId, + Body: { + WebHook: { + AppVersion: args.AppVersion, + Region: args.Region, + GameId: args.GameId, + Type: args.Type, + ActorNr: args.ActorNr, + CreateOptions: args.CreateOptions + } + } + }); } handlers["RoomCreated"] = RoomCreated; // Triggered automatically when a player joins a Photon room var RoomJoined = function (args): void { - log.debug("Room Joined - Game: " + args.GameId + " PlayFabId: " + args.UserId); + server.WritePlayerEvent({ + EventName: "room_joined", + PlayFabId: args.UserId, + Body: { + WebHook: { + AppVersion: args.AppVersion, + Region: args.Region, + GameId: args.GameId, + ActorNr: args.ActorNr + } + } + }); } handlers["RoomJoined"] = RoomJoined; // Triggered automatically when a player leaves a Photon room var RoomLeft = function (args): void { - log.debug("Room Left - Game: " + args.GameId + " PlayFabId: " + args.UserId); + server.WritePlayerEvent({ + EventName: "room_left", + PlayFabId: args.UserId, + Body: { + WebHook: { + AppVersion: args.AppVersion, + Region: args.Region, + GameId: args.GameId, + Type: args.Type, + ActorNr: args.ActorNr, + IsInactive: args.IsInactive + } + } + }); } handlers["RoomLeft"] = RoomLeft; // Triggered automatically when a Photon room closes // Note: currentPlayerId is undefined in this function var RoomClosed = function (args): void { - log.debug("Room Closed - Game: " + args.GameId); + server.WriteTitleEvent({ + EventName: "room_closed", + Body: { + WebHook: { + AppVersion: args.AppVersion, + Region: args.Region, + GameId: args.GameId, + Type: args.Type, + ActorCount: args.ActorCount + } + } + }); } handlers["RoomClosed"] = RoomClosed; // Triggered automatically when a Photon room game property is updated. var RoomPropertyUpdated = function (args): void { - log.debug("Room Property Updated - Game: " + args.GameId); + if (args.Type === "Game") { + server.WritePlayerEvent({ + EventName: "room_properties_updated", + PlayFabId: args.UserId, + Body: { + WebHook: { + AppVersion: args.AppVersion, + Region: args.Region, + GameId: args.GameId, + ActorNr: args.ActorNr, + Properties: args.Properties + } + } + }); + } else { // "Actor" + server.WritePlayerEvent({ + EventName: "player_roperties_updated", + PlayFabId: args.UserId, + Body: { + WebHook: { + AppVersion: args.AppVersion, + Region: args.Region, + GameId: args.GameId, + ActorNr: args.ActorNr, + TargetActor: args.TargetActor, + Properties: args.Properties + } + } + }); + } } handlers["RoomPropertyUpdated"] = RoomPropertyUpdated; @@ -307,9 +384,21 @@ handlers["RoomPropertyUpdated"] = RoomPropertyUpdated; // set to the value of the "customEventContent" HashTable parameter, so you can use // it to pass in arbitrary data. var RoomEventRaised = function (args): void { - var eventData = args.Data; - log.debug("Event Raised - Game: " + args.GameId + " Event Type: " + eventData.eventType); + server.WritePlayerEvent({ + EventName: "event_raised", + PlayFabId: args.UserId, + Body: { + WebHook: { + AppVersion: args.AppVersion, + Region: args.Region, + GameId: args.GameId, + ActorNr: args.ActorNr, + EvCode: args.EvCode + } + } + }); + var eventData = args.Data; switch (eventData.eventType) { // use args.EvCode instead of embedding eventType in payload case "playerMove": processPlayerMove(eventData); From 44549e83db8976bb371f83a5ef392841d26b2116 Mon Sep 17 00:00:00 2001 From: Hamza Lazaar Date: Fri, 5 Jul 2019 14:27:08 +0100 Subject: [PATCH 4/4] FIXED: expected Photon WebHooks handlers return --- DefaultCloudScript.js | 14 +++++----- DefaultCloudScript.ts | 60 ++++++++++++++++++++++++++++--------------- 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/DefaultCloudScript.js b/DefaultCloudScript.js index 72abf28..edc5875 100644 --- a/DefaultCloudScript.js +++ b/DefaultCloudScript.js @@ -211,14 +211,6 @@ var UnlockHighSkillContent = function (args, context) { return { profile: context.playerProfile }; }; handlers["unlockHighSkillContent"] = UnlockHighSkillContent; -// Photon Webhooks Integration -// -// The following functions are examples of Photon Cloud Webhook handlers. -// When you enable the Photon Add-on (https://playfab.com/marketplace/photon/) -// in the Game Manager, your Photon applications are automatically configured -// to authenticate players using their PlayFab accounts and to fire events that -// trigger your Cloud Script Webhook handlers, if defined. -// This makes it easier than ever to incorporate multiplayer server logic into your game. // Triggered automatically when a Photon room is first created var RoomCreated = function (args) { server.WritePlayerEvent({ @@ -235,6 +227,7 @@ var RoomCreated = function (args) { } } }); + return { ResultCode: 0 }; }; handlers["RoomCreated"] = RoomCreated; // Triggered automatically when a player joins a Photon room @@ -251,6 +244,7 @@ var RoomJoined = function (args) { } } }); + return { ResultCode: 0 }; }; handlers["RoomJoined"] = RoomJoined; // Triggered automatically when a player leaves a Photon room @@ -269,6 +263,7 @@ var RoomLeft = function (args) { } } }); + return { ResultCode: 0 }; }; handlers["RoomLeft"] = RoomLeft; // Triggered automatically when a Photon room closes @@ -286,6 +281,7 @@ var RoomClosed = function (args) { } } }); + return { ResultCode: 0 }; }; handlers["RoomClosed"] = RoomClosed; // Triggered automatically when a Photon room game property is updated. @@ -321,6 +317,7 @@ var RoomPropertyUpdated = function (args) { } }); } + return { ResultCode: 0 }; }; handlers["RoomPropertyUpdated"] = RoomPropertyUpdated; // Triggered by calling "OpRaiseEvent" on the Photon client. The "args.Data" property is @@ -348,6 +345,7 @@ var RoomEventRaised = function (args) { default: break; } + return { ResultCode: 0 }; }; handlers["RoomEventRaised"] = RoomEventRaised; //# sourceMappingURL=DefaultCloudScript.js.map \ No newline at end of file diff --git a/DefaultCloudScript.ts b/DefaultCloudScript.ts index e8f4178..710d183 100644 --- a/DefaultCloudScript.ts +++ b/DefaultCloudScript.ts @@ -55,7 +55,8 @@ var HelloWorldDefault = function (args: any, context: IPlayFabContext): IHelloWo // generated by the function execution. // (https://api.playfab.com/playstream/docs/PlayStreamEventModels/player/player_executed_cloudscript) return { messageValue: message }; -} +}; + interface IHelloWorldResponse { messageValue: string; } @@ -75,7 +76,7 @@ var MakeApiCall = function (args: any, context: IPlayFabContext): void { // authenticated as your title and handles all communication with // the PlayFab API, so you don't have to write extra code to issue HTTP requests. var playerStatResult = server.UpdatePlayerStatistics(request); -} +}; handlers["makeAPICall"] = MakeApiCall; // This is a simple example of making a web request to an external HTTP API. @@ -98,7 +99,8 @@ var MakeHttpRequest = function (args: any, context: IPlayFabContext): IMakeHttpR // The pre-defined http object makes synchronous HTTP requests var response = http.request(url, httpMethod, content, contentType, headers); return { responseContent: response }; -} +}; + interface IMakeHttpRequestResponse { responseContent: string; } @@ -121,7 +123,8 @@ var HandlePlayStreamEventAndProfile = function (args: any, context: IPlayFabCont var response = http.request('https://httpbin.org/status/200', 'post', content, 'application/json', null); return { externalAPIResponse: response }; -} +}; + interface IHandlePlayStreamEventAndProfileResponse { externalAPIResponse: string; } @@ -159,7 +162,7 @@ var CompletedLevel = function (args: any, context: IPlayFabContext): void { server.UpdatePlayerStatistics(request); log.debug("Updated level_monster_kills stat for player " + currentPlayerId + " to " + monstersKilled); -} +}; handlers["completedLevel"] = CompletedLevel; // In addition to the Cloud Script handlers, you can define your own functions and call them from your handlers. @@ -167,7 +170,8 @@ handlers["completedLevel"] = CompletedLevel; var UpdatePlayerMove = function (args): IUpdatePlayerMoveResponse { var validMove = processPlayerMove(args); return { validMove: validMove }; -} +}; + interface IUpdatePlayerMoveResponse { validMove: boolean; } @@ -203,7 +207,7 @@ function processPlayerMove(playerMove): boolean { log.debug("lastMoveTime: " + lastMoveTime + " now: " + now + " timeSinceLastMoveInSeconds: " + timeSinceLastMoveInSeconds); if (timeSinceLastMoveInSeconds < playerMoveCooldownInSeconds) { - log.error("Invalid move - time since last move: " + timeSinceLastMoveInSeconds + "s less than minimum of " + playerMoveCooldownInSeconds + "s.") + log.error("Invalid move - time since last move: " + timeSinceLastMoveInSeconds + "s less than minimum of " + playerMoveCooldownInSeconds + "s."); return false; } } @@ -256,7 +260,8 @@ var UnlockHighSkillContent = function (args: any, context: IPlayFabContext): IUn log.info('Unlocked HighSkillContent for ' + context.playerProfile.DisplayName); return { profile: context.playerProfile }; -} +}; + interface IUnlockHighSkillContentResponse { profile: IPlayFabPlayerProfile; } @@ -272,8 +277,17 @@ handlers["unlockHighSkillContent"] = UnlockHighSkillContent; // This makes it easier than ever to incorporate multiplayer server logic into your game. +interface IPhotonRealtimeWebHookResponse { + ResultCode: number; + Message?: string; +} + +interface IPhotonRealtimePathCreateWebHookResponse extends IPhotonRealtimeWebHookResponse { + State?: any; +} + // Triggered automatically when a Photon room is first created -var RoomCreated = function (args): void { +var RoomCreated = function(args) : IPhotonRealtimePathCreateWebHookResponse { server.WritePlayerEvent({ EventName : "room_created", PlayFabId: args.UserId, @@ -288,11 +302,12 @@ var RoomCreated = function (args): void { } } }); -} + return { ResultCode: 0 }; +}; handlers["RoomCreated"] = RoomCreated; // Triggered automatically when a player joins a Photon room -var RoomJoined = function (args): void { +var RoomJoined = function (args) : IPhotonRealtimeWebHookResponse { server.WritePlayerEvent({ EventName: "room_joined", PlayFabId: args.UserId, @@ -305,11 +320,12 @@ var RoomJoined = function (args): void { } } }); -} + return { ResultCode: 0 }; +}; handlers["RoomJoined"] = RoomJoined; // Triggered automatically when a player leaves a Photon room -var RoomLeft = function (args): void { +var RoomLeft = function (args) : IPhotonRealtimeWebHookResponse { server.WritePlayerEvent({ EventName: "room_left", PlayFabId: args.UserId, @@ -324,12 +340,13 @@ var RoomLeft = function (args): void { } } }); -} + return { ResultCode: 0 }; +}; handlers["RoomLeft"] = RoomLeft; // Triggered automatically when a Photon room closes // Note: currentPlayerId is undefined in this function -var RoomClosed = function (args): void { +var RoomClosed = function (args) : IPhotonRealtimeWebHookResponse { server.WriteTitleEvent({ EventName: "room_closed", Body: { @@ -342,11 +359,12 @@ var RoomClosed = function (args): void { } } }); -} + return { ResultCode: 0 }; +}; handlers["RoomClosed"] = RoomClosed; // Triggered automatically when a Photon room game property is updated. -var RoomPropertyUpdated = function (args): void { +var RoomPropertyUpdated = function (args) : IPhotonRealtimeWebHookResponse { if (args.Type === "Game") { server.WritePlayerEvent({ EventName: "room_properties_updated", @@ -377,13 +395,14 @@ var RoomPropertyUpdated = function (args): void { } }); } -} + return { ResultCode: 0 }; +}; handlers["RoomPropertyUpdated"] = RoomPropertyUpdated; // Triggered by calling "OpRaiseEvent" on the Photon client. The "args.Data" property is // set to the value of the "customEventContent" HashTable parameter, so you can use // it to pass in arbitrary data. -var RoomEventRaised = function (args): void { +var RoomEventRaised = function (args) : IPhotonRealtimeWebHookResponse { server.WritePlayerEvent({ EventName: "event_raised", PlayFabId: args.UserId, @@ -407,5 +426,6 @@ var RoomEventRaised = function (args): void { default: break; } -} + return { ResultCode: 0 }; +}; handlers["RoomEventRaised"] = RoomEventRaised; \ No newline at end of file