0

I just recently learned Python and now I thought about making a very simple online game for fun. I don't know what the game should be but I thought I should be able to somehow communicate with others playing the game.

I figured I could save actions I'm doing in the game in a file on a Webserver. The game should then run some function for the other players if the file gets changed. Now my question: Is there any way to watch that file for changes? I tried watchdog but as far as I found it only works on local files. I could probably use a loop with urllib2 checking the content but I'm afraid that's not very elegant or fast.

Im using Python 2.7.13 on a Windows 10 computer.

Thanks in advance for your answer :)

  • 1
    ".. but that's not very elegant or fast" but if you have multiple players in your game... do you have a single file being read and written for all of those individual players' actions? If you monitor a file just for changes, then you would have to read the file to find those changes and propagate the messages to other players. _Very_ quickly this becomes unscaleable. – roganjosh May 03 '17 at 17:36
  • Yeah, I didn't think about that. Well, I don't really know how "real" online games do stuff like that. I'm not that advanced in programming. Maybe I'll figure it out, or i'lll do something easier. – Patrick Schmitter May 03 '17 at 17:54
  • Actually, I don't know exactly how they do it either, but something that would allow concurrent read/write access I would think. For chat, you might want a "pub/sub" setup. Your actions and chat would probably be handled separately. Hopefully someone else can clarify the correct approach; I just wanted to highlight the limitations in your current approach (your question is valid but perhaps not fit for your purpose) before you keep building. – roganjosh May 03 '17 at 17:58

1 Answers1

0

(disclaimer, I agree with @roganjosh in the comments, I don't think this is the right way to approach your problem, but just to answer your question)

In order to monitor changes in a webserver, unless they provide some other API, usually you do implement a loop but instead of getting the whole file all the time, you only send a "HEAD" request. If you see updates in the file then you fetch it.

See this answer for more information: How do you send a HEAD HTTP request in Python 2?

Community
  • 1
  • 1
Josep Valls
  • 5,483
  • 2
  • 33
  • 67
  • The issue with your disclaimer is that now this question has an answer when people scan the front page so they're less likely to open it? I was quite looking forward to understanding how this should work. :) – roganjosh May 03 '17 at 18:59
  • This is an answer to the title of the posted question but as you mentioned, I don't think this is the right way to implement communication in an online game. What's your question/suggestion? – Josep Valls May 03 '17 at 19:07