Author Topic: My Unified Score Server  (Read 3533 times)

0 Members and 1 Guest are viewing this topic.

Offline Craz1000

  • Forum Regular
  • Posts: 111
  • I'm OK
    • View Profile
    • Craz1000.net
My Unified Score Server
« on: May 09, 2020, 12:57:38 pm »
This is the source for the score server i will be using from now on in all of my games.
Of course the "KEY" I use has been removed. It is very easy to add to existing games you have.
If you would also like for my server to service your game i can add it as well.

LeaderServer.bas
Code: QB64: [Select]
  1. '********************************************************************************
  2. '* Leaderboard Server v1.0 5/9/20
  3. '*
  4. '* This will keep track, back up, and service up to 100 clients at the same time
  5. '* Can also write to a website if you wish
  6. '* Requires ipcommsg.bi from ipcomtools
  7. '*
  8. '********************************************************************************
  9.  
  10.  
  11. _TITLE "Score Server v1.0 - Online since: " + DATE$ + " - " + TIME$
  12.  
  13. TYPE LeaderBoardObject
  14.     Score AS INTEGER
  15.     User AS STRING * 15
  16.  
  17. TYPE ClientObject
  18.     IP AS STRING * 15
  19.     Active AS INTEGER
  20.     ColR AS INTEGER
  21.     ColG AS INTEGER
  22.     ColB AS INTEGER
  23.  
  24. DIM SHARED GamesServiced AS INTEGER
  25. GamesServiced = 1 'Changes how many games the server currently handles
  26.  
  27. DIM SHARED Leaderboard(GamesServiced, 20) AS LeaderBoardObject
  28. DIM SHARED Client(100)
  29. DIM SHARED ClientInfo(100) AS ClientObject
  30. DIM SHARED CONNECTION
  31.  
  32. DIM SHARED UpdateTimer
  33.  
  34. UpdateTimer = _FREETIMER
  35. ON TIMER(UpdateTimer, 300) UpdateServer 'backs up every 5 minutes
  36. TIMER(UpdateTimer) ON
  37.  
  38. LoadDatabase
  39.  
  40. Display& = _NEWIMAGE(1024, 768, 32)
  41. SCREEN Display&
  42.  
  43.  
  44. HOST = _OPENHOST("TCP/IP:3571")
  45.  
  46. IF HOST <> 0 THEN
  47.     DO
  48.         _LIMIT 5
  49.         NEWCLIENT = _OPENCONNECTION(HOST)
  50.  
  51.         IF NEWCLIENT THEN 'when client connects. if it has an open spot will accept connection
  52.             FOR x = 1 TO 100
  53.                 IF ClientInfo(x).Active = 0 THEN
  54.                     Client(x) = NEWCLIENT
  55.                     ClientInfo(x).IP = RIGHT$(_CONNECTIONADDRESS(NEWCLIENT), 15)
  56.                     ClientInfo(x).ColR = INT(RND * 155) + 100
  57.                     ClientInfo(x).ColG = INT(RND * 155) + 100
  58.                     ClientInfo(x).ColB = INT(RND * 155) + 100
  59.                     PRINT RTRIM$(ClientInfo(x).IP); " connected."
  60.                     ClientInfo(x).Active = 1
  61.                     EXIT FOR
  62.                 END IF
  63.             NEXT x
  64.         ELSE
  65.         END IF
  66.  
  67.  
  68.  
  69.         FOR Scan = 1 TO 100 'find if any new messages are waiting from each of the clients
  70.             IF Client(Scan) THEN
  71.  
  72.                 COLOR _RGB(ClientInfo(Scan).ColR, ClientInfo(Scan).ColG, ClientInfo(Scan).ColB)
  73.                 IF ClientInfo(Scan).Active = 1 THEN MSG$ = WAITFORREPLY$(Client(Scan))
  74.  
  75.                 '********************************************************************************
  76.                 ' Code for recieving a new high score
  77.                 '********************************************************************************
  78.                 IF MSG$ = "SENDSCORE" THEN
  79.                     SENDMSG Client(Scan), "GAMENO"
  80.                     GameID = VAL(WAITFORREPLY$(Client(Scan)))
  81.                     SENDMSG Client(Scan), "OK"
  82.                     User$ = WAITFORREPLY$(Client(Scan))
  83.                     'CheckIfUserBanned
  84.                     SENDMSG Client(Scan), "SCORE"
  85.                     Score = VAL(WAITFORREPLY$(Client(Scan)))
  86.  
  87.                     Position = 0
  88.                     FOR x = 1 TO 20
  89.                         IF Score > Leaderboard(GameID, x).Score THEN Position = x: EXIT FOR
  90.                     NEXT x
  91.  
  92.                     IF Position < 20 THEN
  93.                         FOR x = 20 TO Position STEP -1
  94.                             Leaderboard(GameID, x).Score = Leaderboard(GameID, x - 1).Score
  95.                             Leaderboard(GameID, x).User = Leaderboard(GameID, x - 1).User
  96.                         NEXT x
  97.                     END IF
  98.  
  99.                     Leaderboard(GameID, Position).Score = Score
  100.                     Leaderboard(GameID, Position).User = User$
  101.  
  102.                     IF GameID = 1 THEN PRINT "Stereospace2 Board Updated by:"; ClientInfo(Scan).IP
  103.                     MSG$ = ""
  104.  
  105.                     '********************************************************************************
  106.                     ' Code for when client requests the scoreboard
  107.                     '********************************************************************************
  108.                 ELSEIF MSG$ = "GETBOARD" THEN
  109.                     SENDMSG Client(Scan), "GAMENO"
  110.                     GameID = VAL(WAITFORREPLY$(Client(Scan)))
  111.                     SENDMSG Client(Scan), "OK"
  112.                     FOR x = 1 TO 20
  113.                         IF WAITFORREPLY$(Client(Scan)) = "NAME" THEN SENDMSG Client(Scan), RTRIM$(Leaderboard(GameID, x).User)
  114.                         IF WAITFORREPLY$(Client(Scan)) = "SCORE" THEN SENDMSG Client(Scan), STR$(Leaderboard(GameID, x).Score)
  115.                         IF WAITFORREPLY$(Client(Scan)) = "OK" THEN SENDMSG Client(Scan), "NEXT"
  116.                     NEXT x
  117.                     SENDMSG Client(Scan), "DONE"
  118.                     IF GameID = 1 THEN PRINT "Stereospace2 Board Sent to:"; ClientInfo(Scan).IP
  119.                     MSG$ = ""
  120.  
  121.                     '********************************************************************************
  122.                     ' Code for when client requests to close connection
  123.                     '********************************************************************************
  124.                 ELSEIF MSG$ = "CLOSE" THEN
  125.                     SENDMSG Client(Scan), "GOODBYE"
  126.                     ClientInfo(Scan).Active = 0
  127.                     CLOSE #Client(Scan)
  128.                     PRINT "Connection to: "; RTRIM$(ClientInfo(Scan).IP); " closed."
  129.                     MSG$ = ""
  130.  
  131.  
  132.                     '********************************************************************************
  133.                     ' Code for when client requests to be verified
  134.                     '********************************************************************************
  135.                 ELSEIF MSG$ = "HELLO" THEN
  136.                     'CheckIfIPIsBanned
  137.                     SENDMSG Client(Scan), "OK"
  138.                     IF WAITFORREPLY$(Client(Scan)) = "READY" THEN
  139.                         Tmp = INT(RND * 256) + 1 'generate random number for verification
  140.                         SENDMSG Client(Scan), STR$(Tmp) 'send number to client
  141.                         'PW = 'insert math to manipulate random number. Make sure client has same formula
  142.                         Test = VAL(WAITFORREPLY$(Client(Scan))) 'wait for client to send verification code
  143.                         IF PW = Test THEN ' if client got same result as server then verified
  144.                             SENDMSG Client(Scan), "LOCKED"
  145.                             PRINT "Connection to: "; RTRIM$(ClientInfo(Scan).IP); " Locked."
  146.                         ELSE
  147.                             PRINT "Got:"; Test; " instead of"; PW; "disconnecting"
  148.                             ClientInfo(Scan).Active = 0
  149.                             CLOSE #Client(Scan)
  150.                             PRINT "Connection to: "; RTRIM$(ClientInfo(Scan).IP); " closed."
  151.                         END IF
  152.                         MSG$ = ""
  153.                     END IF
  154.  
  155.  
  156.                 END IF
  157.             END IF
  158.         NEXT Scan
  159.  
  160.     LOOP
  161.  
  162.  
  163. '********************************************************************************
  164. ' Backs up server, checks if connections are still active
  165. '********************************************************************************
  166. SUB UpdateServer
  167.     COLOR _RGB(100, 100, 100)
  168.     PRINT "Checking all active connections"
  169.     FOR x = 1 TO 100
  170.         IF ClientInfo(x).Active = 1 THEN
  171.             SENDMSG Client(x), "PING"
  172.             IF WAITFORREPLY$(Client(x)) <> "PONG" THEN
  173.                 ClientInfo(x).Active = 0
  174.                 CLOSE #CONNECTION
  175.                 PRINT "Connection to: "; RTRIM$(ClientInfo(x).IP); " closed."
  176.  
  177.             END IF
  178.         END IF
  179.     NEXT x
  180.  
  181.     PRINT "Backing up database"
  182.  
  183.     FOR x = 1 TO GamesServiced
  184.         IF x = 1 THEN File$ = "Stereospace2.dat"
  185.         OPEN File$ FOR OUTPUT AS #1
  186.         FOR y = 1 TO 20
  187.             PRINT #1, RTRIM$(Leaderboard(x, y).User)
  188.             PRINT #1, Leaderboard(x, y).Score
  189.         NEXT y
  190.         PRINT #1, "EOF";
  191.         PRINT #1, 0
  192.         CLOSE #1
  193.     NEXT x
  194.     PRINT "Writing to web server"
  195.     WriteWebsite
  196.     PRINT "Done"
  197.  
  198.  
  199. '********************************************************************************
  200. ' Loads backed up leaderboards from all games
  201. '********************************************************************************
  202. SUB LoadDatabase
  203.     FOR x = 1 TO GamesServiced
  204.         IF x = 1 THEN File$ = "Stereospace2.dat"
  205.         OPEN File$ FOR INPUT AS #1
  206.         FOR y = 1 TO 20
  207.             INPUT #1, Leaderboard(x, y).User
  208.             INPUT #1, Leaderboard(x, y).Score
  209.         NEXT y
  210.         CLOSE #1
  211.         IF x = gameserviced THEN EXIT FOR
  212.     NEXT x
  213.  
  214.  
  215. '********************************************************************************
  216. ' Website code for server. must edit
  217. '********************************************************************************
  218. SUB WriteWebsite
  219.     'OPEN "C:\inetpub\wwwroot\Scores.html" FOR OUTPUT AS #1
  220.     'PRINT #1, "<html>"
  221.     'PRINT #1, "<head>"
  222.     'PRINT #1, "<link rel="; CHR$(34); "stylesheet"; CHR$(34); " type="; CHR$(34); "text/css"; CHR$(34); " href="; CHR$(34); "css/style.css"; CHR$(34); " />"
  223.     'PRINT #1, "</head>"
  224.     'PRINT #1, "<body>"
  225.     'PRINT #1, "<br><br>"
  226.     'PRINT #1, "<center>"
  227.     'PRINT #1, "<table width=80% border=0>"
  228.     'PRINT #1, "<tr><td>Stereospace 2</tr></td>"
  229.     'FOR y = 1 TO 20
  230.     '    PRINT #1, "<tr><td>"
  231.     'PRINT #1, RTRIM$(Leaderboard(1, y).User); "</td><td>"; Leaderboard(1, y).Score; "</tr></td>"
  232.     'NEXT y
  233.     'PRINT #1, "</table>"
  234.     'PRINT #1, "</body>"
  235.     'PRINT #1, "<body>"
  236.     'PRINT #1, "</html>"
  237.     'CLOSE #1
  238.  
  239.  
  240. '$include:'ipcommsg.bi'
  241.  


LeaderClient.bh
Code: QB64: [Select]
  1. '*************************************************************
  2. '* LeaderClient.bh
  3. '*
  4. '* Header for LeaderClient.bi
  5. '**************************************************************
  6.  
  7. TYPE LeaderBoardObject
  8.     Score AS INTEGER
  9.     User AS STRING * 15
  10.  
  11.  
  12. DIM SHARED LeaderBoard(20) AS LeaderBoardObject
  13. DIM SHARED Server
  14. DIM SHARED ServerAddress AS STRING
  15.  
  16. 'GameID must be unique to every game on your server
  17. GameID =  1
  18. ServerAddress = 'Insert server's address
  19.  



LeaderClient.bi
Code: QB64: [Select]
  1. '****************************************************************************
  2. '* Leaderboard Client v1.0
  3. '*
  4. '* Handles leaderboard communication to server.
  5. '* Requires ipcommsg.bi from ipcomtools
  6. '*
  7. '****************************************************************************
  8.  
  9.  
  10.  
  11.  
  12. '**************************************************************************
  13. '* Gets leader board info from server
  14. '**************************************************************************
  15. FUNCTION GetLeaderBoard
  16.     GetLeaderBoard = 0
  17.     SENDMSG Server, "GETBOARD"
  18.     IF WAITFORREPLY$(Server) = "GAMENO" THEN SENDMSG Server, STR$(GameID)
  19.  
  20.     IF WAITFORREPLY$(Server) = "OK" THEN
  21.         FOR x = 1 TO 20
  22.             SENDMSG Server, "NAME"
  23.             LeaderBoard(x).User = WAITFORREPLY$(Server)
  24.             SENDMSG Server, "SCORE"
  25.             LeaderBoard(x).Score = VAL(WAITFORREPLY$(Server))
  26.             SENDMSG Server, "OK"
  27.             IF WAITFORREPLY$(Server) <> "NEXT" THEN EXIT SUB
  28.         NEXT x
  29.         IF WAITFORREPLY$(Server) = "DONE" THEN GetLeaderBoard = 1
  30.     END IF
  31.  
  32. '**************************************************************************
  33. '* Ends session
  34. '**************************************************************************
  35. SUB CloseConnection
  36.     SENDMSG Server, "CLOSE"
  37.     IF WAITFORREPLY$(Server) = "GOODBYE" THEN CLOSE #Server
  38.  
  39.  
  40. '**************************************************************************
  41. '* Opens connection to server, and handles authentication
  42. '**************************************************************************
  43. FUNCTION OpenConnection
  44.     Server = _OPENCLIENT("TCP/IP:3571:" + ServerAddress)
  45.     IF Server THEN
  46.         SENDMSG Server, "HELLO"
  47.         Step2$ = WAITFORREPLY$(Server)
  48.         IF Step2$ = "OK" THEN
  49.             SENDMSG Server, "READY"
  50.             Tmp = VAL(WAITFORREPLY$(Server)) 'Get random number from server
  51.             PW = 'insert custom math formula to verify with server and manipulate "Tmp".
  52.             SENDMSG Server, STR$(PW) 'Send Key
  53.             IF WAITFORREPLY$(Server) = "LOCKED" THEN
  54.                 OpenConnection = 1 'Verified
  55.             ELSE
  56.                 OpenConnection = 0
  57.             END IF
  58.         ELSEIF Step2$ = "BANNED" THEN
  59.             OpenConnection = 2
  60.         END IF
  61.     ELSE
  62.         OpenConnection = 0
  63.     END IF
  64.  
  65.  
  66. '*************************************************************************
  67. '* Sends score to server
  68. '*************************************************************************
  69. FUNCTION SendScore (User$, Score)
  70.     SendScore = 0
  71.     SENDMSG Server, "SENDSCORE"
  72.     IF WAITFORREPLY$(Server) = "GAMENO" THEN SENDMSG Server, STR$(GameID)
  73.     IF WAITFORREPLY$(Server) = "OK" THEN
  74.         SENDMSG Server, User$
  75.         Step2$ = WAITFORREPLY$(Server)
  76.         IF Step2$ = "SCORE" THEN
  77.             SENDMSG Server, STR$(Score)
  78.             SendScore = 1
  79.         ELSEIF Step2$ = "BANNED" THEN
  80.             SendScore = 2
  81.         END IF
  82.     END IF
  83.  
  84.  
  85.  
  86.  
  87. '$include:'ipcommsg.bi'
  88.  


Ipcommsg.bi
Code: QB64: [Select]
  1. '**********************************************************************
  2. '* Ipcommsg.bi v1.1
  3. '*
  4. '* IP COMMUNICATION TOOLS. MESSAGING ONLY
  5. '*
  6. '* ABILITIES:
  7. '* MESSAGE SENDING / RECIEVING
  8. '**********************************************************************
  9.  
  10.  
  11. '**********************************************************************
  12. '* SENDMSG SENDS RAW STRING
  13. '**********************************************************************
  14. SUB SENDMSG (CONNECTION, MSG$)
  15.     MSG$ = UCASE$(MSG$)
  16.     PUT #CONNECTION, , MSG$
  17. '**********************************************************************
  18.  
  19.  
  20. '**********************************************************************
  21. '* WAITFORREPLY$ HANGS AND WAITS FOR DATA TO BE TRANSMITTED BEFORE TIMEOUT
  22. '**********************************************************************
  23. FUNCTION WAITFORREPLY$ (CONNECTION)
  24.     T! = TIMER
  25.     DO
  26.         GET #CONNECTION, , RPL$
  27.     LOOP UNTIL RPL$ <> "" OR TIMER > T! + 3
  28.  
  29.     IF TIMER > T! + 3 THEN
  30.         WAITFORREPLY$ = "TIMEOUT"
  31.     ELSE
  32.         WAITFORREPLY$ = RTRIM$(RPL$)
  33.     END IF
  34. '**********************************************************************
  35.  
« Last Edit: May 09, 2020, 01:14:20 pm by Craz1000 »

FellippeHeitor

  • Guest
Re: My Unified Score Server
« Reply #1 on: May 09, 2020, 01:12:51 pm »
Cool! I'll definitely be studying this one. Thanks for sharing.

Offline Craz1000

  • Forum Regular
  • Posts: 111
  • I'm OK
    • View Profile
    • Craz1000.net
Re: My Unified Score Server
« Reply #2 on: May 10, 2020, 07:59:51 pm »
Ignore this post.

Created a separate post for this issue.
« Last Edit: May 11, 2020, 11:25:59 pm by Craz1000 »