Do you think I could just leave this part blank and it'd be okay? We're just going to replace the whole thing with a header image anyway, right?
You are not logged in.
Pages: 1
Helping AnatolyEE with getting linked accounts working prompted me to make a neater version of the code and release it here, just in case it helps other people too.
This automatically detects whether an account is linked or not and if it is it does the linking for you, as well as handling the enabling of https if it is required.
This uses modern async functions and fancy new modules, which are now supported by all major desktop browsers apart from IE (but who cares about IE ), which means it will be supported on around 80% of computers, which will be increasing as more people update to the newer versions. Using async means that the code can be written without the callback pyramids of doom caused by having so many steps in the authentication process (although PlayerIO doesn't support this directly, so I use an asPromise helper function to do this), and using modules helps with the mess caused by trying to limit the scope to a single file to avoid naming clashes.
// ConnectHandler.js Version 1.0 (by LukeM)
const isHttps = location.protocol == 'https:';
PlayerIO.useSecureApiRequests = isHttps;
function asPromise(func, ...args) {
return new Promise((resolve, reject) => func(...args, resolve, reject));
}
export async function connect(email, password, worldID, log) {
let cli = await asPromise(PlayerIO.authenticate, "everybody-edits-su9rn58o40itdbnw69plyw", "simpleUsers", { email: email, password: password }, { });
cli.multiplayer.useSecureConnections = isHttps;
log("Logged in");
const objPromise = asPromise(cli.bigDB.loadMyPlayerObject);
const cfgPromise = asPromise(cli.bigDB.load, "config", "config");
let obj = await objPromise;
log("Loaded player object");
if ("linkedTo" in obj) {
log("Detected linked account");
let auth = await asPromise(cli.multiplayer.createJoinRoom, "auth" + cli.ConnectedUserId, "AuthRoom", true, null, { type: "Link" });
log("Joined authentication room");
let msg = await asPromise(auth.addMessageCallback, "auth");
log("Received authentication key");
cli = await asPromise(PlayerIO.authenticate, "everybody-edits-su9rn58o40itdbnw69plyw", "linked", { userId: msg.getString(0), auth: msg.getString(1) }, { });
cli.multiplayer.useSecureConnections = isHttps;
log("Logged in to linked account");
}
let cfg = await cfgPromise;
log("Loaded config");
let con = await asPromise(cli.multiplayer.createJoinRoom, worldID, "Everybodyedits" + cfg.version, true, null, null);
log("Connected");
return { cli: cli, con: con };
}
This uses the new import and export functions added recently, so instead of adding a script tag you just need to save it in your bot directory then reference it in your main javascript file (named Example.js in this example):
import { connect } from "./ConnectHandler.js";
let con;
async function Setup() {
let email = prompt("Email: ");
let password = prompt("Password: ");
let worldID = prompt("World ID: ");
con = await connect(email, password, worldID, Log).con;
con.addMessageCallback('*', OnMessage);
con.send("init");
}
function Log(text) { console.log(text) }
function OnMessage(m) {
switch (m.type) {
case "init":
Log("Received init message");
// Init handling stuff here
con.send("init2");
break;
case "init2":
Log("Received init2 message");
// Init2 handling stuff here
break;
// ...
}
}
Setup();
// The function 'connect' returns an object containing the client 'cli' and the connection 'con', but in most cases you will only need to use con.
To get modules to work, you just need to state that your script uses modules by using the type="module" attribute:
<html>
<body>
<script src="PlayerIOClient.development.js"></script>
<script src="Example.js" type="module"></script>
</body>
</html>
If your browser doesn't support modules yet, or if you want to be extra safe, you can just remove the export keyword and use this as normal, just make sure you don't use the same names as any of the variables / functions this uses.
Offline
I've been wanting something like this for a long time!
What's up with the log functions though in the source?
export async function connect(email, password, worldID, log) {
let cli = await asPromise(PlayerIO.authenticate, "everybody-edits-su9rn58o40itdbnw69plyw", "simpleUsers", { email: email, password: password }, { });
cli.multiplayer.useSecureConnections = isHttps;
log("Logged in");
const objPromise = asPromise(cli.bigDB.loadMyPlayerObject);
const cfgPromise = asPromise(cli.bigDB.load, "config", "config");
let obj = await objPromise;
log("Loaded player object");
if ("linkedTo" in obj) {
Offline
I've been wanting something like this for a long time!
What's up with the log functions though in the source?
▼Code
Glad you'd find it useful
Not sure what you mean about the log functions? They are passed in as an argument so you can do other things with the logs other than just logging to the console, e.g. logging to some HTML thing on the page instead.
Offline
Not sure what you mean about the log functions? They are passed in as an argument so you can do other things with the logs other than just logging to the console, e.g. logging to some HTML thing on the page instead.
My bad, I didn't see the log as a parameter - oops
Offline
Pages: 1
[ Started around 1732213052.1037 - Generated in 0.146 seconds, 12 queries executed - Memory usage: 1.45 MiB (Peak: 1.59 MiB) ]