Sample - Using HTTPS to connect to server to transfer game data
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
-
- Newbie
- Posts: 15
- Joined: Thu Jan 11, 2018 10:02 am
- Contact:
Sample - Using HTTPS to connect to server to transfer game data
Hi~
I don't know about you guys but I'm SUPER excited about the newest release!!
In the release note for 6.99.14, it says TLS has been added so now we can send game data to the server.
Does anyone have an example to show me how it's done?
Can't wait to try this out. Thanks a lot in advance!!
I don't know about you guys but I'm SUPER excited about the newest release!!
In the release note for 6.99.14, it says TLS has been added so now we can send game data to the server.
Does anyone have an example to show me how it's done?
Can't wait to try this out. Thanks a lot in advance!!
- Imperf3kt
- Lemma-Class Veteran
- Posts: 3794
- Joined: Mon Dec 14, 2015 5:05 am
- itch: Imperf3kt
- Location: Your monitor
- Contact:
Re: Sample - Using HTTPS to connect to server to transfer game data
Not sure if this is an actual answer, but here's an example by Xavimat from way before TLS was implemented.
viewtopic.php?f=11&t=41697
viewtopic.php?f=11&t=41697
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.
Current project: GGD Mentor
Twitter
pro·gram·mer (noun) An organism capable of converting caffeine into code.
Current project: GGD Mentor
- PyTom
- Ren'Py Creator
- Posts: 16096
- Joined: Mon Feb 02, 2004 10:58 am
- Completed: Moonlight Walks
- Projects: Ren'Py
- IRC Nick: renpytom
- Github: renpytom
- itch: renpytom
- Location: Kings Park, NY
- Contact:
Re: Sample - Using HTTPS to connect to server to transfer game data
Right now, the most complete example is on my patreon, here:
https://www.patreon.com/posts/news-from-your-15636026
https://www.patreon.com/posts/news-from-your-15636026
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom(When was the last time you backed up your game?)
Software > Drama • https://www.patreon.com/renpytom
-
- Newbie
- Posts: 15
- Joined: Thu Jan 11, 2018 10:02 am
- Contact:
Re: Sample - Using HTTPS to connect to server to transfer game data
Thanks PyTom for your response!
I looked at the link that you've posted and it seems like the post talks about fetching information from JSON file on the server to display the latest news on the client.
From the release note, I was under the impression that the new version allows data to be transferred FROM the client TO the server?
I'm hoping to save player progress on the server so that I can see how far the players have progressed.
Btw, thanks PyTom - your work and help is always appreciated!!!
I looked at the link that you've posted and it seems like the post talks about fetching information from JSON file on the server to display the latest news on the client.
From the release note, I was under the impression that the new version allows data to be transferred FROM the client TO the server?
I'm hoping to save player progress on the server so that I can see how far the players have progressed.
Btw, thanks PyTom - your work and help is always appreciated!!!
- PyTom
- Ren'Py Creator
- Posts: 16096
- Joined: Mon Feb 02, 2004 10:58 am
- Completed: Moonlight Walks
- Projects: Ren'Py
- IRC Nick: renpytom
- Github: renpytom
- itch: renpytom
- Location: Kings Park, NY
- Contact:
Re: Sample - Using HTTPS to connect to server to transfer game data
That's fair. The example I gave shows how to get requests going with Ren'Py. I'd suggest taking a look at the requests quickstart here:
http://docs.python-requests.org/en/mast ... uickstart/
to see how to use it to post to a server. As some advice, make sure you wrap everything in a try-except block, to make sure if it fails the game doesn't crash. (And any web request can fail.)
http://docs.python-requests.org/en/mast ... uickstart/
to see how to use it to post to a server. As some advice, make sure you wrap everything in a try-except block, to make sure if it fails the game doesn't crash. (And any web request can fail.)
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom(When was the last time you backed up your game?)
Software > Drama • https://www.patreon.com/renpytom
- xavimat
- Eileen-Class Veteran
- Posts: 1461
- Joined: Sat Feb 25, 2012 8:45 pm
- Completed: Yeshua, Jesus Life, Cops&Robbers
- Projects: Fear&Love
- Organization: Pilgrim Creations
- Github: xavi-mat
- itch: pilgrimcreations
- Location: Spain
- Discord: xavimat
- Contact:
Re: Sample - Using HTTPS to connect to server to transfer game data
(Probably I'm going to embarrass myself with the code below; I'm not professional and simply copy-paste code from tutorials, with lots of potential errors...)
I'm interested too in learning how the TLS works. This code here DOES NOT USE the new TLS thing (I need to study it, I don't have access to Renpytom's Patreon), but it uses old python2 libraries (urllib and urllib2) to connect to a HTTP (not HTTPS) url. I show this here only as a starting point, and ask for advice. What's wrong with my approach? How it should be done?
This is (simplified) the code I'm using in an app that informs a web server about the progress of the user. The user can also search for other users and follow them (set them as "friends"), and can see the progress other users have made (actually, there is a "champions chart", ordered by Xp points, not showed here). The user can also see what users are following him/her.
I'm showing here only some of the code:
1- connect.rpy, in Ren'Py, allows renpy to connect to the server.
2- Then you need mygame.php, a PHP file in the server that dialogues with the database. With special attention to avoid SQL injection attacks.
- Takes the variables that the python code has passed (action, pwh, webid, version, xp, mascot).
- Makes changes in the database with the new info.
- Creates an output (I use json_encode), that will be read by Ren'Py.
I'm interested too in learning how the TLS works. This code here DOES NOT USE the new TLS thing (I need to study it, I don't have access to Renpytom's Patreon), but it uses old python2 libraries (urllib and urllib2) to connect to a HTTP (not HTTPS) url. I show this here only as a starting point, and ask for advice. What's wrong with my approach? How it should be done?
This is (simplified) the code I'm using in an app that informs a web server about the progress of the user. The user can also search for other users and follow them (set them as "friends"), and can see the progress other users have made (actually, there is a "champions chart", ordered by Xp points, not showed here). The user can also see what users are following him/her.
I'm showing here only some of the code:
1- connect.rpy, in Ren'Py, allows renpy to connect to the server.
Code: Select all
### CONNECT.rpy ###
default network = 1 ## this variable counts the times the urlopen has not been successful. 0==working
default webid = ''
default friends = []
default fans = []
default url = "http://mywebpage.com/mygame.php"
init python:
from urllib import urlencode
from urllib2 import urlopen
from json import load as json_load
def connect(action='inform'):
global network, webid, friends, fans
if webid == '':
action = 'create'
## Preparation of data
data_dict = {}
data_dict['action'] = action
if action == 'create':
data_dict['username'] = username
data_dict['pwh'] = pwh ## password-hashed
data_dict['webid'] = webid ## user's id given by the server
data_dict['version'] = config.version
## user's game progress:
data_dict['xp'] = xp
data_dict['mascot'] = mascot
data = urlencode(data_dict)
try:
response = urlopen(url, data=data, timeout=10)
except:
network += 1
return False
raw_result = json_load(response)
response.close()
friends = []
fans = []
if "CREATED" in raw_result:
webid = str(raw_result["CREATED"])
if "FRIENDS" in raw_result:
for element in raw_result["FRIENDS"]:
friends.append(element)
if "FANS" in raw_result:
for element in raw_result["FANS"]:
fans.append(element)
network = 0
return True
2- Then you need mygame.php, a PHP file in the server that dialogues with the database. With special attention to avoid SQL injection attacks.
- Takes the variables that the python code has passed (action, pwh, webid, version, xp, mascot).
- Makes changes in the database with the new info.
- Creates an output (I use json_encode), that will be read by Ren'Py.
Last edited by xavimat on Mon Jan 15, 2018 5:36 pm, edited 1 time in total.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)
- PyTom
- Ren'Py Creator
- Posts: 16096
- Joined: Mon Feb 02, 2004 10:58 am
- Completed: Moonlight Walks
- Projects: Ren'Py
- IRC Nick: renpytom
- Github: renpytom
- itch: renpytom
- Location: Kings Park, NY
- Contact:
Re: Sample - Using HTTPS to connect to server to transfer game data
I've commented out the second half of xavimat's post, since it looks like it's vulnerable to an SQL injection attack. (At least, I'd need a good argument that it's secure, and I don't see that here - the sanitization for HTML doesn't look like it would protect from SQL injection.)
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom(When was the last time you backed up your game?)
Software > Drama • https://www.patreon.com/renpytom
- xavimat
- Eileen-Class Veteran
- Posts: 1461
- Joined: Sat Feb 25, 2012 8:45 pm
- Completed: Yeshua, Jesus Life, Cops&Robbers
- Projects: Fear&Love
- Organization: Pilgrim Creations
- Github: xavi-mat
- itch: pilgrimcreations
- Location: Spain
- Discord: xavimat
- Contact:
Re: Sample - Using HTTPS to connect to server to transfer game data
Thanks, PyTom. I'm sorry, I thought it was enough. I'll learn more about it and correct the file.PyTom wrote: ↑Mon Jan 15, 2018 3:39 pm I've commented out the second half of xavimat's post, since it looks like it's vulnerable to an SQL injection attack. (At least, I'd need a good argument that it's secure, and I don't see that here - the sanitization for HTML doesn't look like it would protect from SQL injection.)
I've edited my post to at least explain, without code, what the php file is intended to do.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)
Who is online
Users browsing this forum: No registered users