Official Everybody Edits Forums

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.

#1 2018-07-28 15:32:03, last edited by LukeM (2018-07-28 16:00:36)

LukeM
Member
From: England
Joined: 2016-06-03
Posts: 3,009
Website

[Library] ConnectHandler.js 1.0

ConnectHandler.js 1.0

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 //forums.everybodyedits.com/img/smilies/tongue), 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.

Source:
// 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 };
}
Changelog:
Version 1.0
How to use this library:

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

Wooted by:

#2 2018-07-28 18:44:28, last edited by SirJosh3917 (2018-07-28 18:44:45)

SirJosh3917
Formerly ninjasupeatsninja
From: USA
Joined: 2015-04-05
Posts: 2,095

Re: [Library] ConnectHandler.js 1.0

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

#3 2018-07-28 20:04:34

LukeM
Member
From: England
Joined: 2016-06-03
Posts: 3,009
Website

Re: [Library] ConnectHandler.js 1.0

ninjasupeatsninja wrote:

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 //forums.everybodyedits.com/img/smilies/big_smile

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

#4 2018-07-28 20:21:22

SirJosh3917
Formerly ninjasupeatsninja
From: USA
Joined: 2015-04-05
Posts: 2,095

Re: [Library] ConnectHandler.js 1.0

LukeM wrote:

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

SirJosh39171532805682716691

Board footer

Powered by FluxBB

[ Started around 1732354785.7445 - Generated in 0.072 seconds, 11 queries executed - Memory usage: 1.45 MiB (Peak: 1.59 MiB) ]