Jump to content

I made a hangman game using python ( I am considering to convert it into javascript to post it on a website like blogger) It has local multiplayer on the same device but I want to add an online feature. The problem is I have no idea how I can do it. Can you give me some guidance? 
 

Also I want to run this using online tools like repl.it or onlinegdb.com

Link to comment
https://linustechtips.com/topic/1377232-making-an-online-game/
Share on other sites

Link to post
Share on other sites

There's various ways you could do it. 

 

Probably the easiest would be to use a simple web server and use simple messages that are saved by the web server for the duration of the online game session. 

 

Your game can simply connect to the web server, request updates for the online game session and then close connection. Wait 1s or more, and request an update, close connection, repeat until game is over. 

 

You can have a unique key that automatically increments  so that each computer can simply request something like update.php?game=1234&key=12345&playerid=john&lastmessage=100  where

* 100 was the last record received from the game server a few seconds ago... the web server will reply only with messages from 101 and onwards, no need to resend messages 0..100

* game 1234 is the ID of the game lobby that was created

* key  public or private key of the game lobby - so that random players can't just spectate or send commands, you only get this key if you joined a game lobby.

* playerid = your playerid for that game lobby

 

When a game is started, the game can control who's turn is it by simply rejecting commands from the player that's not supposed to play. 

You'd need to have a mechanism to detect if a player is not responding in a reasonable amount of time (like let's say 30s) and kick him out. 

 

Think of it like playing a text mode game or a game where you type  go left , pick knife, open door ... but it's not you typing, it's your game sending commands to the web server and web server keeps track of game session, and replies to you and other players accordingly. 

 

 

 

 

Link to comment
https://linustechtips.com/topic/1377232-making-an-online-game/#findComment-15019183
Share on other sites

Link to post
Share on other sites

35 minutes ago, mariushm said:

There's various ways you could do it. 

 

Probably the easiest would be to use a simple web server and use simple messages that are saved by the web server for the duration of the online game session. 

 

Your game can simply connect to the web server, request updates for the online game session and then close connection. Wait 1s or more, and request an update, close connection, repeat until game is over. 

 

You can have a unique key that automatically increments  so that each computer can simply request something like update.php?game=1234&key=12345&playerid=john&lastmessage=100  where

* 100 was the last record received from the game server a few seconds ago... the web server will reply only with messages from 101 and onwards, no need to resend messages 0..100

* game 1234 is the ID of the game lobby that was created

* key  public or private key of the game lobby - so that random players can't just spectate or send commands, you only get this key if you joined a game lobby.

* playerid = your playerid for that game lobby

 

When a game is started, the game can control who's turn is it by simply rejecting commands from the player that's not supposed to play. 

You'd need to have a mechanism to detect if a player is not responding in a reasonable amount of time (like let's say 30s) and kick him out. 

 

Think of it like playing a text mode game or a game where you type  go left , pick knife, open door ... but it's not you typing, it's your game sending commands to the web server and web server keeps track of game session, and replies to you and other players accordingly. 

 

 

 

 

What do you mean by web server? Things like AWS? Is there a free solution?

Link to comment
https://linustechtips.com/topic/1377232-making-an-online-game/#findComment-15019218
Share on other sites

Link to post
Share on other sites

No. No free solution. 

 

Maybe there's some free services for game lobbies but that's it ... as in a game that wants to create an online multiplayer session tells the server it's hosting and then that server will tell others games that you're willing to host a game and give those players your ip and port on which your game is listening for players. 

 

See for example how Open TTD does it : https://servers.openttd.org/listing

When you start hosting a multiplayer game (either using a dedicated game server or while you're playing the game) your game just sends a message to a web server they have with some data and your ip and the port it's listening on  ... other players receive the list of games and when they connect to a game,  they connect directly to the game server. 

 

In my example the web server would be more involved, not just a list of games and telling players game server's ip and port. 

It's more complexity making the web server scripts but makes it simpler because there's no communication between players, the players only communicate to the web server and the web server acts as middle man, passing the messages and controlling the game flow (telling a player it's their turn, accepting chat messages and forwarding them to others etc etc) ... and you don't have to deal with port forwarding in routers for your game (because other players wouldn't be able to connect to your game otherwise) 

 

Link to comment
https://linustechtips.com/topic/1377232-making-an-online-game/#findComment-15019239
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

Don't start signing up to various web services without understanding the basics of network programming.

 

You can do everything locally first, the programming language doesn't matter.

 

For example, you can run a Quake or Half-Life server on your machine and run multiple copies of quake/hl to connect to your local server.

 

I would recommend starting with a simple echo server first.

Write and run a server which accepts multiple client socket connections.

It is important to understand state. What is it? Where is it? A server maintains state, and clients request updates to the state while also trying to update the server state.

 

Link to comment
https://linustechtips.com/topic/1377232-making-an-online-game/#findComment-15040921
Share on other sites

Link to post
Share on other sites

1 minute ago, TrickyDicky said:

Don't start signing up to various web services without understanding the basics of network programming.

 

You can do everything locally first, the programming language doesn't matter.

 

For example, you can run a Quake or Half-Life server on your machine and run multiple copies of quake/hl to connect to your local server.

 

I would recommend starting with a simple echo server first.

Write and run a server which accepts multiple client socket connections.

It is important to understand state. What is it? Where is it? A server maintains state, and clients request updates to the state while also trying to update the server state.

 

 

Link to comment
https://linustechtips.com/topic/1377232-making-an-online-game/#findComment-15040924
Share on other sites

Link to post
Share on other sites

On 9/29/2021 at 10:53 PM, Wictorian said:

I made a hangman game using python ( I am considering to convert it into javascript to post it on a website like blogger) It has local multiplayer on the same device but I want to add an online feature. The problem is I have no idea how I can do it. Can you give me some guidance? 

Good question!  I have questions for you:

 

1. How would you play multiplayer hangman?  One person sets the word and the other person guesses?

2. If you want to have multiplayer online, you'll need a way to "store" the data to persist across multiple devices.  That adds some complexity if you want to do everything for free

3. What if the "word" was set using the URL?  Like

http://example.com/?word=theword

Obviously, you wouldn't want it so easily seen by the person playing... so maybe you could scramble it somehow, maybe there's an encryption library so that the url wouldn't give the word away, but the browser would still now the answer? I honestly don't know, just thinking about ways to do this without a database.

 

You can also host static pages on GitHub using GitHub Pages, that's a nice alternative without having to use Blogger or other services like that.  

Link to comment
https://linustechtips.com/topic/1377232-making-an-online-game/#findComment-15054552
Share on other sites

Link to post
Share on other sites

53 minutes ago, Mikey A. said:

Good question!  I have questions for you:

 

1. How would you play multiplayer hangman?  One person sets the word and the other person guesses?

2. If you want to have multiplayer online, you'll need a way to "store" the data to persist across multiple devices.  That adds some complexity if you want to do everything for free

3. What if the "word" was set using the URL?  Like


http://example.com/?word=theword

Obviously, you wouldn't want it so easily seen by the person playing... so maybe you could scramble it somehow, maybe there's an encryption library so that the url wouldn't give the word away, but the browser would still now the answer? I honestly don't know, just thinking about ways to do this without a database.

 

You can also host static pages on GitHub using GitHub Pages, that's a nice alternative without having to use Blogger or other services like that.  

1 Yes.

3 That is actually pretty good! 
 

But how will the devices communicate? If they can, can’t I just store the data in variables?

Link to comment
https://linustechtips.com/topic/1377232-making-an-online-game/#findComment-15054615
Share on other sites

Link to post
Share on other sites

10 hours ago, Wictorian said:

But how will the devices communicate? If they can, can’t I just store the data in variables?

Not quite - in my idea the devices do not communicate, that adds a lot of complexity.  Engineer Man has a good series on how he built a game that has realtime communication between multiple clients: https://www.youtube.com/watch?v=P9S5PfygOvw

 

One way I would suggest approaching it is in versions.  Maybe for version 1, just JavaScript single player and you supply random words they have to guess.  For version 2, maybe add a leaderboard.  For version 3, multiplayer?   etc...

Link to comment
https://linustechtips.com/topic/1377232-making-an-online-game/#findComment-15055360
Share on other sites

Link to post
Share on other sites

19 minutes ago, Mikey A. said:

Not quite - in my idea the devices do not communicate, that adds a lot of complexity.  Engineer Man has a good series on how he built a game that has realtime communication between multiple clients: https://www.youtube.com/watch?v=P9S5PfygOvw

 

One way I would suggest approaching it is in versions.  Maybe for version 1, just JavaScript single player and you supply random words they have to guess.  For version 2, maybe add a leaderboard.  For version 3, multiplayer?   etc...

Yeah I have version 1 , 2 and 3 actually. Now I want to implement online multiplayer. 

Link to comment
https://linustechtips.com/topic/1377232-making-an-online-game/#findComment-15055416
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×