1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-11-01 12:24:27 +00:00
Funkin/source/NGio.hx

304 lines
8 KiB
Haxe
Raw Normal View History

2020-11-01 03:42:14 +00:00
package;
2021-02-20 02:11:33 +00:00
#if newgrounds
2020-11-01 03:42:14 +00:00
import flixel.FlxG;
import flixel.util.FlxSignal;
2020-12-25 05:17:21 +00:00
import flixel.util.FlxTimer;
2020-11-01 03:42:14 +00:00
import io.newgrounds.NG;
2021-02-15 17:43:51 +00:00
import io.newgrounds.NGLite;
2020-11-01 03:42:14 +00:00
import io.newgrounds.components.ScoreBoardComponent.Period;
2021-02-18 19:58:16 +00:00
import io.newgrounds.objects.Error;
2020-11-01 03:42:14 +00:00
import io.newgrounds.objects.Medal;
import io.newgrounds.objects.Score;
import io.newgrounds.objects.ScoreBoard;
2020-12-23 00:57:55 +00:00
import io.newgrounds.objects.events.Response;
import io.newgrounds.objects.events.Result.GetCurrentVersionResult;
import io.newgrounds.objects.events.Result.GetVersionResult;
import lime.app.Application;
2020-11-01 03:42:14 +00:00
import openfl.display.Stage;
2021-02-15 00:45:18 +00:00
using StringTools;
2021-02-20 02:11:33 +00:00
#end
2020-11-01 03:42:14 +00:00
/**
* MADE BY GEOKURELI THE LEGENED GOD HERO MVP
*/
class NGio
{
2021-02-20 02:11:33 +00:00
#if newgrounds
2021-02-18 19:58:16 +00:00
/**
* True, if the saved sessionId was used in the initial login, and failed to connect.
* Used in MainMenuState to show a popup to establish a new connection
*/
public static var savedSessionFailed(default, null):Bool = false;
2020-11-01 03:42:14 +00:00
public static var scoreboardsLoaded:Bool = false;
2021-02-20 02:11:33 +00:00
public static var isLoggedIn(get, never):Bool;
inline static function get_isLoggedIn()
{
return NG.core != null && NG.core.loggedIn;
}
2020-11-01 03:42:14 +00:00
public static var scoreboardArray:Array<Score> = [];
public static var ngDataLoaded(default, null):FlxSignal = new FlxSignal();
public static var ngScoresLoaded(default, null):FlxSignal = new FlxSignal();
2020-12-23 00:57:55 +00:00
public static var GAME_VER:String = "";
2021-02-18 19:58:16 +00:00
static public function checkVersion(callback:String->Void)
2020-12-16 03:00:22 +00:00
{
2021-02-18 19:58:16 +00:00
trace('checking NG.io version');
2020-12-23 00:57:55 +00:00
GAME_VER = "v" + Application.current.meta.get('version');
2021-02-18 19:58:16 +00:00
NG.core.calls.app.getCurrentVersion(GAME_VER)
.addDataHandler(function(response)
2020-12-23 00:57:55 +00:00
{
2021-02-18 19:58:16 +00:00
GAME_VER = response.result.data.currentVersion;
trace('CURRENT NG VERSION: ' + GAME_VER);
callback(GAME_VER);
})
.send();
2020-12-16 03:00:22 +00:00
}
2021-02-20 02:11:33 +00:00
static public function init()
2020-11-01 03:42:14 +00:00
{
2021-02-18 19:58:16 +00:00
var api = APIStuff.API;
if (api == null || api.length == 0)
{
trace("Missing Newgrounds API key, aborting connection");
return;
}
2020-11-01 03:42:14 +00:00
trace("connecting to newgrounds");
2021-02-15 17:43:51 +00:00
2021-02-18 19:58:16 +00:00
#if NG_FORCE_EXPIRED_SESSION
var sessionId:String = "fake_session_id";
function onSessionFail(error:Error)
{
trace("Forcing an expired saved session. "
+ "To disable, comment out NG_FORCE_EXPIRED_SESSION in Project.xml");
savedSessionFailed = true;
}
#else
var sessionId:String = NGLite.getSessionId();
if (sessionId != null)
trace("found web session id");
#if (debug)
if (sessionId == null && APIStuff.SESSION != null)
{
trace("using debug session id");
sessionId = APIStuff.SESSION;
}
#end
2021-02-15 17:43:51 +00:00
2021-02-18 19:58:16 +00:00
var onSessionFail:Error->Void = null;
if (sessionId == null && FlxG.save.data.sessionId != null)
{
trace("using stored session id");
sessionId = FlxG.save.data.sessionId;
onSessionFail = function (error) savedSessionFailed = true;
}
2021-02-15 17:43:51 +00:00
#end
2021-02-18 19:58:16 +00:00
NG.create(api, sessionId, #if NG_DEBUG true #else false #end, onSessionFail);
2021-02-15 17:43:51 +00:00
2021-02-18 19:58:16 +00:00
#if NG_VERBOSE NG.core.verbose = true; #end
2020-11-01 03:42:14 +00:00
// Set the encryption cipher/format to RC4/Base64. AES128 and Hex are not implemented yet
2021-02-18 19:58:16 +00:00
NG.core.initEncryption(APIStuff.EncKey); // Found in you NG project view
2020-11-01 03:42:14 +00:00
if (NG.core.attemptingLogin)
{
/* a session_id was found in the loadervars, this means the user is playing on newgrounds.com
* and we should login shortly. lets wait for that to happen
*/
trace("attempting login");
NG.core.onLogin.add(onNGLogin);
}
2021-02-15 17:43:51 +00:00
//GK: taking out auto login, adding a login button to the main menu
2021-02-18 19:58:16 +00:00
// else
// {
// /* They are NOT playing on newgrounds.com, no session id was found. We must start one manually, if we want to.
// * Note: This will cause a new browser window to pop up where they can log in to newgrounds
// */
// NG.core.requestLogin(onNGLogin);
// }
}
/**
* Attempts to log in to newgrounds by requesting a new session ID, only call if no session ID was found automatically
* @param popupLauncher The function to call to open the login url, must be inside
* a user input event or the popup blocker will block it.
* @param onComplete A callback with the result of the connection.
*/
static public function login(?popupLauncher:(Void->Void)->Void, onComplete:ConnectionResult->Void)
{
trace("Logging in manually");
var onPending:Void->Void = null;
if (popupLauncher != null)
2020-11-01 03:42:14 +00:00
{
2021-02-18 19:58:16 +00:00
onPending = function () popupLauncher(NG.core.openPassportUrl);
}
var onSuccess:Void->Void = onNGLogin;
var onFail:Error->Void = null;
var onCancel:Void->Void = null;
if (onComplete != null)
{
onSuccess = function ()
{
onNGLogin();
onComplete(Success);
}
onFail = function (e) onComplete(Fail(e.message));
onCancel = function() onComplete(Cancelled);
2020-11-01 03:42:14 +00:00
}
2021-02-18 19:58:16 +00:00
NG.core.requestLogin(onSuccess, onPending, onFail, onCancel);
2020-11-01 03:42:14 +00:00
}
2021-02-20 02:11:33 +00:00
inline static public function cancelLogin():Void
{
NG.core.cancelLoginRequest();
}
2020-11-01 03:42:14 +00:00
2021-02-18 19:58:16 +00:00
static function onNGLogin():Void
2020-11-01 03:42:14 +00:00
{
trace('logged in! user:${NG.core.user.name}');
FlxG.save.data.sessionId = NG.core.sessionId;
2021-02-18 19:58:16 +00:00
FlxG.save.flush();
2020-11-01 03:42:14 +00:00
// Load medals then call onNGMedalFetch()
NG.core.requestMedals(onNGMedalFetch);
// Load Scoreboards hten call onNGBoardsFetch()
NG.core.requestScoreBoards(onNGBoardsFetch);
ngDataLoaded.dispatch();
}
2021-02-18 19:58:16 +00:00
static public function logout()
{
NG.core.logOut();
FlxG.save.data.sessionId = null;
FlxG.save.flush();
}
2020-11-01 03:42:14 +00:00
// --- MEDALS
2021-02-18 19:58:16 +00:00
static function onNGMedalFetch():Void
2020-11-01 03:42:14 +00:00
{
/*
// Reading medal info
for (id in NG.core.medals.keys())
{
var medal = NG.core.medals.get(id);
trace('loaded medal id:$id, name:${medal.name}, description:${medal.description}');
}
// Unlocking medals
var unlockingMedal = NG.core.medals.get(54352);// medal ids are listed in your NG project viewer
if (!unlockingMedal.unlocked)
unlockingMedal.sendUnlock();
*/
}
// --- SCOREBOARDS
2021-02-18 19:58:16 +00:00
static function onNGBoardsFetch():Void
2020-11-01 03:42:14 +00:00
{
/*
// Reading medal info
for (id in NG.core.scoreBoards.keys())
{
var board = NG.core.scoreBoards.get(id);
trace('loaded scoreboard id:$id, name:${board.name}');
}
*/
// var board = NG.core.scoreBoards.get(8004);// ID found in NG project view
// Posting a score thats OVER 9000!
// board.postScore(FlxG.random.int(0, 1000));
// --- To view the scores you first need to select the range of scores you want to see ---
// add an update listener so we know when we get the new scores
// board.onUpdate.add(onNGScoresFetch);
trace("shoulda got score by NOW!");
// board.requestScores(20);// get the best 10 scores ever logged
// more info on scores --- http://www.newgrounds.io/help/components/#scoreboard-getscores
}
2021-02-18 19:58:16 +00:00
static function onNGScoresFetch():Void
2020-11-01 03:42:14 +00:00
{
scoreboardsLoaded = true;
ngScoresLoaded.dispatch();
/*
for (score in NG.core.scoreBoards.get(8737).scores)
{
trace('score loaded user:${score.user.name}, score:${score.formatted_value}');
}
*/
// var board = NG.core.scoreBoards.get(8004);// ID found in NG project view
// board.postScore(HighScore.score);
// NGio.scoreboardArray = NG.core.scoreBoards.get(8004).scores;
}
2021-02-20 02:11:33 +00:00
#end
2020-11-01 03:42:14 +00:00
2021-02-20 02:11:33 +00:00
static public function logEvent(event:String)
{
2021-02-20 02:11:33 +00:00
#if newgrounds
2020-12-23 00:57:55 +00:00
NG.core.calls.event.logEvent(event).send();
trace('should have logged: ' + event);
2021-02-20 02:11:33 +00:00
#else
#if debug trace('event:$event - not logged, missing NG.io lib'); #end
#end
}
2021-02-20 02:11:33 +00:00
static public function unlockMedal(id:Int)
2020-11-01 03:42:14 +00:00
{
2021-02-20 02:11:33 +00:00
#if newgrounds
2020-11-01 03:42:14 +00:00
if (isLoggedIn)
{
var medal = NG.core.medals.get(id);
if (!medal.unlocked)
medal.sendUnlock();
}
2021-02-20 02:11:33 +00:00
#else
#if debug trace('medal:$id - not unlocked, missing NG.io lib'); #end
#end
}
static public function postScore(score:Int = 0, song:String)
{
#if newgrounds
if (isLoggedIn)
{
for (id in NG.core.scoreBoards.keys())
{
var board = NG.core.scoreBoards.get(id);
if (song == board.name)
{
board.postScore(score, "Uhh meow?");
}
// trace('loaded scoreboard id:$id, name:${board.name}');
}
}
#else
#if debug trace('Song:$song, Score:$score - not posted, missing NG.io lib'); #end
#end
2020-11-01 03:42:14 +00:00
}
}
2021-02-18 19:58:16 +00:00
enum ConnectionResult
{
/** Log in successful */
Success;
/** Could not login */
Fail(msg:String);
/** User cancelled the login */
Cancelled;
}