[Resolved] Commas and other punctuation in numbers to facebook.

When post to facebook with a number in my string generated via a lua conversion from an integer with commas or other punctuation in it, the post fails without an error message.

Simple example Code:

 local gameScore = comma\_value(theScore)  
 local theMessage = "I just scored " .. gameScore .. " points testing my game."  
 facebook.request( "me/feed", "POST", {message = theMessage})  

if I change the above code to:

 local theMessage = "I just scored 12,437 points testing my game."  
 facebook.request( "me/feed", "POST", {message = theMessage})  

it works.

Also if I change it to:

 local gameScore = tostring(theScore)  
 local theMessage = "I just scored " .. gameScore .. " points testing my game."  
 facebook.request( "me/feed", "POST", {message = theMessage})  

it works.

Here is the comma code:

comma\_value = function(amount)  
 local formatted = tostring(amount)  
 while true do   
 formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')  
 if (k==0) then  
 break  
 end  
 end  
 return formatted  
end  

but it doesn’t matter. I have tried three other different comma functions including one I wrote myself specifically not to use pattern matching.

It’s not just the “message” field, it also fails in the “caption” field in an attachment. (I imagine other fields as well although I have not tested.

I am using build #777 for universal ios. Since I am developing a game with scores that can get very high it looks very unprofessional in posts from the game not to have comma separated numbers.
[import]uid: 98849 topic_id: 24550 reply_id: 324550[/import]

Its probably something weird to do with escaping the comma. Maybe the corona api does’t know to escape it in some situations. This doesnt work?

local gameScore = tostring(comma_value(theScore))

[import]uid: 8872 topic_id: 24550 reply_id: 99421[/import]

I tried fixing it that way. I also tried trimming both ends. [import]uid: 98849 topic_id: 24550 reply_id: 99433[/import]

I just did a quick test using the sample Facebook code in 773;

Added at start;
[lua]local score = 11000

comma_value = function(amount)
local formatted = tostring(amount)
while true do
formatted, k = string.gsub(formatted, “^(-?%d+)(%d%d%d)”, ‘%1,%2’)
if (k==0) then
break
end
end
return formatted
end

fScore = comma_value(score)[/lua]

Default post_photo code which I whacked the number in, very messily;
[lua]if fbCommand == POST_PHOTO then
local attachment = {
name = “Developing a Facebook”…fScore…" Connect app using the Corona SDK!",
link = “http://developer.anscamobile.com/forum”,
caption = “Link caption”,
description = “Corona SDK for developing iOS and Android apps with the same code base.”,
picture = “http://developer.anscamobile.com/demo/Corona90x90.png”,
actions = json.encode( { { name = “Learn More”, link = “http://anscamobile.com” } } )
}

facebook.request( “me/feed”, “POST”, attachment ) – posting the photo
end[/lua]

That actually posted just fine, main line was;
“Developing a Facebook11,000 Connect app using the Corona SDK!”

So, ugly and makes no sense - but it worked. [import]uid: 52491 topic_id: 24550 reply_id: 99456[/import]

Hmmm… It didn’t work. I thought it could be some thing in my facebook settings.
But they are pretty normal. Here are my facebook settings.
App Type: Native/Desktop
iOS Bundle ID: ca.sixaxis.xxxxxxx
iPhone and iPad App Store ID: 513087926 which is for doodle tilt ( in the app store - a rip off of Jonathan Beebe’s Tilt Monster)
Configured for iOS SSO: Enabled
iOS Native Deep Linking: Disabled [import]uid: 98849 topic_id: 24550 reply_id: 99499[/import]

@fredrick.kohler, I wonder if you need to match the iOS Bundle ID with the App Store ID. But then, you mentioned that it works perfectly fine when you don’t format the number… hmmm… makes very little sense.

By the way, do you have the comma_value function above the code for Facebook post – meaning, is the function within scope?

Let us know when you find the answer to this strange puzzle.

Naomi [import]uid: 67217 topic_id: 24550 reply_id: 99533[/import]

Yes, in scope as a global, but I have tried it, in my desperation to get it working, as a local. [import]uid: 98849 topic_id: 24550 reply_id: 99534[/import]

@fredrick.kohler, did you try Peach’s code? I mean, using the attachment method. I use the same method (i.e., formatting a number to a string and post the score on Facebook wall using the facebook.request( “me/feed”, “POST”, attachment ). It posts totally fine. Maybe the request syntax that you are using doesn’t quite work the way we’d expect it to work (and maybe it’s a bug you found.)

Naomi [import]uid: 67217 topic_id: 24550 reply_id: 99540[/import]

I just tried it with the fully quaified bundle ID, same behavior.
iOS Bundle ID: NNNNNNNNNN.ca.sixaxis.xxxxxxx

Here is the exact code I’m currently using:

local touchFBBtn = function( event )  
-- app id for facebook  
local fbAppID = "NNNNNNNNNNNNNNN"  
if event.phase == "release" then  
 --native.showAlert("Sorry.", "Facebook is not implemented for testing.",{ "OK"} )  
 -- Play Sound  
 if soundsOn == true then  
 local freeChan = audio.findFreeChannel()  
 audio.play( tapSound, { channel=freeChan } )  
 end  
 if musicOn == true then  
 audio.stop( gameMusic3 )  
 audio.dispose( gameMusic3 )  
 gameMusic3 = nil  
 end  
 local gameScore = "umptillion"  
 local expertise = gameSettings["expertise"]  
 if theScore \> 0 then  
 gameScore = comma\_value(theScore)  
 --gameScore = tostring(theScore)  
 else  
 gameScore = "nothing"  
 end  
 local attachment = {  
 name = "Download Tiltoid to compete with me!",  
 link = "http://www.sixaxis.ca",  
 caption = "Can you beat my score of "..gameScore.." in the "..expertise.." game?",  
 description = "Tiltoid - a fun new game from Sixaxis Studio",  
 picture = "http://www.sixaxis.ca/TiltoidIcon90x90.png",  
 actions = json.encode( { { name = "Learn More", link = "http://www.sixaxis.ca" } } )  
 }  
 local function onLoginSuccess()  
 facebook.request( "me/feed", "POST", attachment )  
 end  
 -- facebook listener  
 local function fbListener( event )  
 if event.isError then  
 --native.showAlert( "Error!", event.response, { "OK" } )  
 popupNotification( "Error! "..event.response, 255, 0, 0 )  
 else  
 if event.type == "session" and event.phase == "login" then  
 -- login was a success; call function  
 onLoginSuccess()  
 elseif event.type == "request" then  
 -- this block is executed upon successful facebook.request() call  
 --native.showAlert( "Success!", "The message has been posted.", { "OK" } )  
 popupNotification( "Success! The message has been posted.", 255, 255, 255 )  
 end  
 end  
 end  
 -- attachment uploading requires the "publish\_stream" permission  
 facebook.login( fbAppID, fbListener, { "publish\_stream" } )  
end  
end  

with my facebook app ID where “NNNNNNNNNNNNNNN” is. It works with 0 score and the “nothing” text. [import]uid: 98849 topic_id: 24550 reply_id: 99543[/import]

Maybe you want to change this:

local gameScore = “umptillion”

to

local gameScore

It might just do the trick…

Naomi

P.S., I mean, gameScore would hold a number, instead of a string. The mix-up might just be causing you grief.

P.P.S, If I were you, I wouldn’t even introduce gameScore to the picture. I’d just use comma_value(theScore) instead of gameScore in the attachment where you want to include the score.

P.P.P.S, I just thought, maybe, the reason you want gameScore is because there’s a change theScore could be nil? If it’s nil, why not define theScore to 0 (which would solve the problem, I think). [import]uid: 67217 topic_id: 24550 reply_id: 99553[/import]

I tried it this way:

caption = "Can you beat my score of "..comma\_value(theScore).." in the "..expertise.." game?",  

but no joy. Of course now that the zero value of theScore is passed to comma_value, whatever corruption was happening to an non zero value is happening to a zero value also.

To streamline the code I set gameScore to “nothing” and dropped the else statement. [import]uid: 98849 topic_id: 24550 reply_id: 99557[/import]

That is so strange. My score message works like this, and it posts without any problem whatsoever.

[lua]message = "Try beating my high score of " … comma_value(FBScore) … " on Level " … comma_value(FBLevel) … “!”,[/lua]

FBScore is a number (not a string).
FBLevel is also a number (not a string).

Do you ever initialize theScore to 0 (so there’s no way theScore would ever be nil) – this way, you don’t have to worry about nil, and you can always use the number value in the message (with comma formatting done on the fly.)

Naomi

P.S. You could also do this:

[lua]if theScore == nil then
gameScore = 0;
else
gameScore = theScore;
end[/lua]
[lua]caption = “Can you beat my score of “…comma_value(gameScore)…” in the “…expertise…” game?”,[/lua] [import]uid: 67217 topic_id: 24550 reply_id: 99558[/import]

I do initialize “theScore” to 0 so even if the player goes to the score screen before starting the game that it’s not nil. The intent is that if people are sharing a device they can look at each other’s high scores (reloaded from disk) before playing. [import]uid: 98849 topic_id: 24550 reply_id: 99561[/import]

Have you tried “message” instead of “caption” in the attachment? That’s the only thing I can think of at this point. Very strange.

Naomi

P.S. My attachement table has the following elements (which isn’t exactly the same as yours). I wonder if you change your elements inside the attachment, it would work?

[lua]local attachment = {
name = “My Game”,
link = “http://www.myGameSite.com”,
message = "Try beating my high score of " … comma_value(FBScore) … " on Level " … comma_value(FBLevel) … “!”,
picture = “http://www.myGameSite/myImage.png”,
actions = json.encode( { name = “Learn More”, link = “http://www.myGameSite.com” } )
}[/lua] [import]uid: 67217 topic_id: 24550 reply_id: 99562[/import]

No change. Same attachment elements as you, “nothing” works, comma separated number doesn’t. [import]uid: 98849 topic_id: 24550 reply_id: 99565[/import]

I’m sorry to hear it. I have no clue why yours doesn’t work while mine does… What Daily Build are you using (not that it could possibly make any difference…)

Also, maybe, try commenting out the whole thing you’ve got and use the very simple Facebook request with pre-canned numbers and attachment like so:

[lua]local FBScore = 100000;
local FBLevel = 10;
local attachment = {
name = “My Game”,
link = “http://www.myGameSite.com”,
message = "Try beating my high score of " … comma_value(FBScore) … " on Level " … comma_value(FBLevel) … “!”,
picture = “http://www.myGameSite/myImage.png”,
actions = json.encode( { name = “Learn More”, link = “http://www.myGameSite.com” } )
}
facebook.request( “me/feed”, “POST”, attachment )[/lua]

Does this work at all?

Also, could there be any possibility of some sort of error with your comma_value function?

I got mine from http://lua-users.org/wiki/FormattingNumbers and it looks like this (I have it in my main.lua as a global function):
[lua]function comma_value(amount)
local formatted = amount
while true do
formatted, k = string.gsub(formatted, “^(-?%d+)(%d%d%d)”, ‘%1,%2’)
if (k==0) then
break
end
end
return formatted
end[/lua]

Naomi [import]uid: 67217 topic_id: 24550 reply_id: 99569[/import]

That’s essentially the one I was using. See the first post. [import]uid: 98849 topic_id: 24550 reply_id: 99583[/import]

Yeah, it’s really frustrating, eh? I posted the comma_value function just to make sure you’ve got no typo or whatever else that might’ve corrupted your function somehow. (Sorry, I’m too lazy to compare the two to ensure they are exactly the same.) Have you displayed any of your formatted value elsewhere in your game without any problem? You might even try the print statement on your terminal to make sure your comma_value function works as expected. And if there’s nothing wrong with the function, I am absolutely out of any suggestions that you might try. I hope, somehow, you’ll get this sorted – and let us know what might’ve been the culprit.

Naomi [import]uid: 67217 topic_id: 24550 reply_id: 99584[/import]

The only difference with mine was the unnecessary “tostring(amount)” at the entry to the function. I’ve since replaced it with your version. I’m beginning to suspect something at the Facebook end. The frustrating part is that it’s “under the hood” somewhere in the Corona compilation or Facebook handling and based on what Peach said above some difference in my setup is affecting it although I can’t imagine what it might be. [import]uid: 98849 topic_id: 24550 reply_id: 99585[/import]

I was going to try this helper module if I couldn’t get the Facebook part working on Android device (I’m porting iOS version to Android right now). I wonder if you could use it to debug your issue. It’s very well commented and nicely done:

https://developer.anscamobile.com/code/facebook-helpersupport-module

Naomi [import]uid: 67217 topic_id: 24550 reply_id: 99597[/import]