question

Ayman avatar image
Ayman asked

I have problem when create room in my game

I want to add value (CurrentGameMoney) to a player statistics when I want to challenge, my code is working well good but not add a value to statistics when create room

Cloud Script

function getGameMoney (roomCategory) {
	
	switch (roomCategory) {
        case "1":
            return 100;
            break;
		case "2":
            return 500;
            break;
        case "3":
            return 2000;
            break;
        case "4":
            return 10000;
            break;
        case "5":
            return 20000;
            break;
        case "6":
            return 50000;
            break;
        case "7":
            return 100000;
            break;
        case "8":
            return 500000;
            break;
        case "9":
            return 1000000;
            break;
        default:
        	return 0;
            break;
    }
    return 0;
}


function getGameCategory (roomMoney) {
	
	switch (roomMoney) {
        case 100:
            return 1;
            break;
		case 500:
            return 2;
            break;
        case 2000:
            return 3;
            break;
        case 10000:
            return 4;
            break;
        case 20000:
            return 5;
            break;
        case 50000:
            return 6;
            break;
        case 100000:
            return 7;
            break;
        case 500000:
            return 8;
            break;
        case 1000000:
            return 9;
            break;
        default:
        	return 0;
            break;
    }
    return 0;
}


// Photon Webhooks Integration
//
// The following functions are examples of Photon Cloud Webhook handlers. 
// When you enable Photon integration in the Game Manager, your Photon applications 
// are automatically configured to authenticate players using their PlayFab accounts 
// and to fire events that trigger your CloudScript Webhook handlers, if defined. 
// This makes it easier than ever to incorporate server logic into your game.
//
//  For more information, see https://playfab.com/using-photon-playfab


// Triggered automatically when a Photon room is first created
handlers.RoomCreated = function (args) {


    //log.debug("Room Created - Game: " + args.GameId + " MaxPlayers: " + args.CreateOptions.MaxPlayers);
    
    var userStatsResult = server.GetUserStatistics ({ PlayFabId: currentPlayerId });
	var roomCategory = args.GameId.split(":")[1];
    var money = getGameMoney (roomCategory);
 
    server.UpdateUserStatistics ({ PlayFabId: currentPlayerId, UserStatistics: { "CurrentGameMoney" : money }});
}


// Triggered automatically when a player joins a Photon room
handlers.RoomJoined = function (args) {


    //log.debug("Room Joined - Game: " + args.GameId + " PlayFabId: " + args.UserId);
    
    var userStatsResult = server.GetUserStatistics ({ PlayFabId: args.UserId });
	var roomCategory = args.GameId.split(":")[1];
    var money = getGameMoney (roomCategory);
    
    server.UpdateUserStatistics ({ PlayFabId: args.UserId, UserStatistics: { "CurrentGameMoney" : money }});
}


// Triggered automatically when a player leaves a Photon room
handlers.RoomLeft = function (args) {


    log.debug("Room Left - Game: " + args.GameId + " PlayFabId: " + args.UserId);
    var userStatsResult = server.GetUserStatistics ({ PlayFabId: currentPlayerId });
    
    if (parseInt(userStatsResult.UserStatistics.GameStarted !== undefined ? userStatsResult.UserStatistics.GameStarted : 0) == 1 && parseInt(userStatsResult.UserStatistics.InGame !== undefined ? userStatsResult.UserStatistics.InGame : 0) == 1) {
	    
	    server.SubtractUserVirtualCurrency ({ PlayFabId: currentPlayerId, VirtualCurrency: "GD", Amount: parseInt(userStatsResult.UserStatistics.CurrentGameMoney/2) });
	    var newScore = ((parseInt(userStatsResult.UserStatistics.PlayerWon) * 2) - (parseInt(userStatsResult.UserStatistics.PlayerLose) + 1));
	    if (newScore < 0) {
	    
		    newScore = 0;
	    }
	    server.UpdateUserStatistics ({ PlayFabId: currentPlayerId, UserStatistics: { "GameStarted" : 0, "InGame" : 0, "CurrentGameMoney" : 0, "PlayerLose": (parseInt(userStatsResult.UserStatistics.PlayerLose) + 1) }});
    }
}


// Triggered automatically when a Photon room closes
// Note: currentPlayerId is undefined in this function
handlers.RoomClosed = function (args) {
    log.debug("Room Closed - Game: " + args.GameId);
}


// Triggered automatically when a Photon room game property is updated.
// Note: currentPlayerId is undefined in this function
handlers.RoomPropertyUpdated = function (args) {
    log.debug("Room Property Updated - Game: " + args.GameId);
}


// 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.
handlers.RoomEventRaised = function (args) {


    var eventData = args.Data; 
    var userStatsResult = server.GetUserStatistics ({ PlayFabId: currentPlayerId });
    
    if (eventData[0] == "GameStarted") {
	    
	    server.UpdateUserStatistics ({ PlayFabId: currentPlayerId, UserStatistics: { "GameStarted" : 1, "InGame" : 1 } });
    } else if (eventData[0] == "GameOver") {
    
	    var userInventory = server.GetUserInventory ({ PlayFabId: currentPlayerId });
	   
	    if (eventData[1] == "Win") {
		    
		    server.AddUserVirtualCurrency ({ PlayFabId: currentPlayerId, VirtualCurrency: "GD", Amount: parseInt(userStatsResult.UserStatistics.CurrentGameMoney/2) });
		    var newScore = userStatsResult.UserStatistics.PlayerScore + (getGameCategory(userStatsResult.UserStatistics.CurrentGameMoney) * 2);
		    if (newScore < 0) {
		    
			    newScore = 0;
		    }
		    server.UpdateUserStatistics ({ PlayFabId: currentPlayerId, UserStatistics: { "GameStarted" : 0, "InGame" : 0, "PlayerWon": (parseInt(userStatsResult.UserStatistics.PlayerWon) + 1) , "PlayerScore": newScore }});
	    } else {
		    
		    server.SubtractUserVirtualCurrency ({ PlayFabId: currentPlayerId, VirtualCurrency: "GD", Amount: parseInt(userStatsResult.UserStatistics.CurrentGameMoney/2) });
		    var newScore = userStatsResult.UserStatistics.PlayerScore - (getGameCategory(userStatsResult.UserStatistics.CurrentGameMoney) * 2);
		    if (newScore < 0) {
		    
			    newScore = 0;
		    }
		    server.UpdateUserStatistics ({ PlayFabId: currentPlayerId, UserStatistics: { "GameStarted" : 0, "InGame" : 0, "PlayerLose": (parseInt(userStatsResult.UserStatistics.PlayerLose) + 1) , "PlayerScore": newScore  }});
	    }
	    updatePlayerLevel(eventData);
    }
}
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Seth Du avatar image Seth Du ♦ commented ·

may I ask do you mean the RoomCreated function is not triggered properly?

0 Likes 0 ·
Ayman avatar image
Ayman answered

Hello SethDu

Yes , Not triggered properly

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Seth Du avatar image Seth Du ♦ commented ·

Sorry for the late response.

Would you check the Photon Dashboard, and make sure the webhook has been assigned with proper value? refering Photon quickstart - PlayFab | Microsoft Docs

1 Like 1 ·
Ayman avatar image
Ayman answered

Thank you for your interest


2021-2-16.jpg (140.8 KiB)
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Seth Du avatar image Seth Du ♦ commented ·

We got several reports about Photon webhook issues. For now, there are few things you need to confirm

  • Webhook paths and Cloud Script Live revision functions. I have checked your title, and it should be fine.
  • Authentication field on Photon Dashboard, remember to use server provider.
  • The BaseUrl key is generated by PlayFab, which is located at add-on page.

If all the configuration is correct, you may change the 'Realtime App ID' and replace it with a dummy ID, save the settings. Then replace it with a real ID and save again. This should work in most cases. If you are using Photon Chat, you may do the same actions to refresh the settings.

If you generate a new Photon secret key, remember to modify the corresponding configuration on Photon Dashboard.

0 Likes 0 ·
Ayman avatar image
Ayman answered

It was confirmed and the problem was not solved yet

.

There is an alarm in the console when creating a room


12345.jpg (154.1 KiB)
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Seth Du avatar image Seth Du ♦ commented ·

What's the title ID? I may look into it.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.