0
\$\begingroup\$

I am creating a simple multiplayer puzzle game in which multiple players can join a lobby and compete with each other. the player that submits the answer first gets the highest point, the player that submits the answers second gets the second highest point and so on..

How do I maintain the order of request on the server side?

I assume i cannot use an event-based server as it would become impossible to process requests in order.

a simple multithreaded server would be an ideal choice in this case as the requests will be short-lived.

initially, I was thinking to create the server in python but since it has not so good support for "real threads" it would mean compromising on performance.

My two main questions are:

  1. how is this scenario generally handled?
  2. how is this scenario generally handle if the language of choice is python?
\$\endgroup\$
0
\$\begingroup\$

When you have one or more threads which access the game state, then you want to avoid that more than one thread does so at the same time. Otherwise you can run into very hard to reproduce bugs which occur when one thread tries to access a data structure while another thread is in the middle of changing it.

In order to avoid this, you can use a Lock.

When your networking thread wants to check on the state of the game to see if it is the first to answer the question, use lock.acquire(). When no other thread has the lock acquired at the moment, the execution will continue immediately. When there is another thread which acquired the lock, then execution will pause until that thread does a lock.release(). That means a lock allows you to define a section of code where only one thread is allowed at a time. All other threads need to wait until it's their turn.

Pseudocode:

def checkAnswer( answer )

    lock.acquire()

    if questionAlreadyAnswered:
         print "you were too slow."
    else
         if answer == correctAnswer:
              print "correct!"
              questionAlreadyAnswered = true
         else
              print "wrong!"

    lock.release()
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.