Skip to content

Instantly share code, notes, and snippets.

@CaptainZidgel
Created March 2, 2023 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CaptainZidgel/5cba66054057e1036d8cf2f3a49b9a4a to your computer and use it in GitHub Desktop.
Save CaptainZidgel/5cba66054057e1036d8cf2f3a49b9a4a to your computer and use it in GitHub Desktop.
Example of faking a server for mgeme testing (200x easier than sourcemod!)
# Example of faking a server for mgeme testing
# (Mgeme is a project of mine, but this code example may be useful to somebody else anyway)
import asyncio
import websockets
import sys
import json
host = sys.argv[1]
port = sys.argv[2]
secret = sys.argv[3]
hello = {
'type': "ServerHello",
"payload": {
"apiKey": secret,
"serverNum": "1",
"serverHost": "xx.xx.xx.xx",
"serverPort": "27015",
"stvPort": "27020"
}
}
def matchBegan(match):
return {
"type": "MatchBegan",
"payload": {
"p1": match["p1Id"],
"p2": match["p2Id"],
}
}
def matchResults(winner, loser, finished):
return {
"type": "MatchResults",
"payload": {
"winner": winner,
"loser": loser,
"finished": finished,
}
}
async def until(websocket, type):
while True:
msg = await websocket.recv()
obj = json.loads(msg)
try:
if obj['type'] == type:
return obj
except KeyError:
pass
async def serv():
async with websockets.connect(f"ws://{host}:{port}/tf2serverep") as websocket:
print("Sending ServerHello")
await websocket.send(
json.dumps(hello)
)
await until(websocket, "ServerAck")
print("Got ServerAck, waiting for MatchDetails")
matchdets = await until(websocket, "MatchDetails")
print("Got match details, waiting 5 seconds then beginning")
await asyncio.sleep(5)
await websocket.send(
json.dumps(matchBegan(matchdets))
)
print("Began match, waiting 5 seconds then sending finish")
await asyncio.sleep(5)
await websocket.send(
json.dumps(matchResults(matchdets["p1Id"], matchdets["p2Id"], True))
)
print("Sleeping 25 seconds")
await asyncio.sleep(25)
asyncio.run(serv())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment