Skip to content

galfar-coder/ScoreSpaceAPI

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 

ScoreSpaceAPI

A public leaderboard for ScoreSpace Jams

How to use

python

First you need to have installed requests library. if you dont have installed requests use command in command prompt: python3 -m pip install requests

if you have everything installed you can copy the commented code below!

Note that this is code for python 3.x.x !

import json, requests

def createGame(game: str) -> None:
	"""
	Will create a game if a game by that name has not already been created
	game is the name of your game
	syntax: `createGame(GameName)`
	returns: `None`
	"""
	try:
		r = requests.post(
			"http://galfar.dyndns.org:25577/reggame/?game={game}".format(game=game)
		)
	except Exception as e:
		# Something went wrong!
		print("Something went wrong! error: " + str(e))

def createUser(game: str,user: str, args: dict) -> str:
	"""
	Will create a user if a user by that name has not already been created on that game
	info:
	game is the name of your game
	user is the user that you're trying to set
	args is the default values for ex. Highscore so {"Highscore": 999}
	will return the secret for the created user if it succeded
	will return "ERR" if it cannot connect to the api
	if the user has already been created it will not return the secret key and instead it will return "already in the system! forgot secret? contact: galfar#1954"
	syntax: `createUser(GameName, Username, Args)`
	returns: `str`
	"""

	try:
		r = requests.post(
			"http://galfar.dyndns.org:25577/register/?game={game}&username={username}&args={args}".format(game=game,username=user,args=json.dumps(args))
		)
		return r.text
	except Exception as e:
		# Something went wrong!
		print("Something went wrong! error: " + str(e))
		return "ERR"

def setHighScore(game: str, user: str, secret: str, data: dict) -> None:
	"""
	Sets a highscore of a game and user and secret
	info:
	
	game is the name of your game
	user is the user that you're trying to set
	secret is the secret that is generated when you create a user the secret key ONLY works with the user it was created with
	data is the updated highscores you want to upload to the api
	syntax: `setHighScore(GameName, Username, Secret, Data)`
	returns: `None`
	"""
	try:
		# formating the dict to json
		data = json.dumps(data)
		r = requests.post(
			"http://galfar.dyndns.org:25577/?game={game}&user={user}&secret={secret}&highscore={data}".format(game=game, user=user, secret=secret, data=data)
		)
		
	except Exception as e:
		# Something went wrong!
		print("Something went wrong! error: " + str(e))

def getHighScore(game: str) -> dict:
	"""
	gets the highscore of a game
	syntax: `getHighScore(game)`
	returns: `dict`
	"""
	
	# gets every data, we want to pick only users data
	# user can be only a string
	# getting all data here
	try:
		r = requests.get(
			"http://galfar.dyndns.org:25577/"
			)

		# formating it into dict from json
		data = json.loads(r.content)

		# getting users data
		gamedata = data[game]

		# returning the dictionary with high scores, yay!
		return gamedata

	except Exception as e:
		# Something went wrong!
		print("Something went wrong! error: " + str(e))

#Create's the Game
createGame("GameName")

#Save the secret to a file and load it when the game starts so you can access that user
secret = createUser("GameName", "User",{"Highscore": 0})

#will set the highscore to 10
setHighScore("GameName", "Username", secret, {"Highscore": 10})

#Will get all the users highscores for that game
getHighScore("GameName")

About

A public leaderboard for ScoreSpace Jams

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published