aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/client/server_fixture.py (2300B)


      1 """Create the isolated character used by native and browser smoke checks."""
      2 import getpass
      3 import hashlib
      4 import http.cookiejar
      5 import pathlib
      6 import secrets
      7 import sqlite3
      8 import time
      9 import urllib.parse
     10 import urllib.request
     11 
     12 
     13 def create_player(directory, name, rotation):
     14     root = pathlib.Path(__file__).resolve().parents[2]
     15     (directory / 'data').mkdir(parents=True, exist_ok=True)
     16     with sqlite3.connect(directory / 'data/world.sqlite') as database:
     17         database.executescript((root / 'server/world.sql').read_text())
     18         database.execute(
     19             "INSERT INTO characters (id, name, appearance_race, appearance_fatness, appearance_breed, "
     20             "appearance_sex, appearance_side, rotation_x, rotation_y, rotation_z, rotation_w) "
     21             "VALUES (73, ?, 1, 1, 1, 2, 1, ?, ?, ?, ?) "
     22             "ON CONFLICT(id) DO UPDATE SET name=excluded.name, playfield=705, "
     23             "position_x=960, position_y=16.5, position_z=510, rotation_x=excluded.rotation_x, "
     24             "rotation_y=excluded.rotation_y, rotation_z=excluded.rotation_z, rotation_w=excluded.rotation_w",
     25             (name, *rotation))
     26         database.execute("INSERT OR IGNORE INTO players (id) VALUES (73)")
     27 
     28 
     29 def admin_opener(directory):
     30     """Authorize tools against their own disposable server database."""
     31     token = secrets.token_urlsafe(32)
     32     with sqlite3.connect(directory / 'data/world.sqlite') as database:
     33         database.execute("INSERT OR IGNORE INTO admin_users (id, username, password_salt, password_hash) VALUES (1, 'fixture', X'00', X'00')")
     34         database.execute("INSERT INTO admin_sessions (token_hash, admin_id, expires_at) VALUES (?, 1, ?)",
     35                          (hashlib.sha256(token.encode()).digest(), int(time.time()) + 43200))
     36     opener = urllib.request.build_opener()
     37     opener.addheaders = [('Cookie', 'ao_admin=' + token)]
     38     return opener
     39 
     40 
     41 def admin_login(url):
     42     """Log in interactively when a verification tool uses an existing server."""
     43     opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()))
     44     fields = {'username': input('Admin username: '), 'password': getpass.getpass('Admin password: ')}
     45     opener.open(url + '/admin/login', urllib.parse.urlencode(fields).encode()).read()
     46     return opener