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

193 lines
5 KiB
Haxe
Raw Normal View History

2020-11-01 03:42:14 +00:00
package;
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;
import io.newgrounds.components.ScoreBoardComponent.Period;
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;
/**
* MADE BY GEOKURELI THE LEGENED GOD HERO MVP
*/
class NGio
{
public static var isLoggedIn:Bool = false;
public static var scoreboardsLoaded:Bool = false;
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 = "";
2020-12-25 05:17:21 +00:00
public static var gotOnlineVer:Bool = false;
2020-12-23 00:57:55 +00:00
2020-12-16 03:00:22 +00:00
public static function noLogin(api:String)
{
2020-12-23 00:57:55 +00:00
trace('INIT NOLOGIN');
GAME_VER = "v" + Application.current.meta.get('version');
2020-12-16 03:00:22 +00:00
NG.create(api);
2020-12-23 00:57:55 +00:00
2020-12-25 05:17:21 +00:00
new FlxTimer().start(2, function(tmr:FlxTimer)
2020-12-23 00:57:55 +00:00
{
2020-12-25 05:17:21 +00:00
var call = NG.core.calls.app.getCurrentVersion(GAME_VER).addDataHandler(function(response:Response<GetCurrentVersionResult>)
2020-12-23 00:57:55 +00:00
{
2020-12-25 05:17:21 +00:00
GAME_VER = response.result.data.current_version;
2020-12-23 00:57:55 +00:00
trace('CURRENT NG VERSION: ' + GAME_VER);
2020-12-25 05:17:21 +00:00
gotOnlineVer = true;
2020-12-23 00:57:55 +00:00
});
call.send();
});
2020-12-16 03:00:22 +00:00
}
2020-11-01 03:42:14 +00:00
public function new(api:String, encKey:String, ?sessionId:String)
{
trace("connecting to newgrounds");
NG.createAndCheckSession(api, sessionId);
NG.core.verbose = true;
// Set the encryption cipher/format to RC4/Base64. AES128 and Hex are not implemented yet
NG.core.initEncryption(encKey); // Found in you NG project view
trace(NG.core.attemptingLogin);
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);
}
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);
}
}
function onNGLogin():Void
{
trace('logged in! user:${NG.core.user.name}');
isLoggedIn = true;
FlxG.save.data.sessionId = NG.core.sessionId;
// FlxG.save.flush();
// Load medals then call onNGMedalFetch()
NG.core.requestMedals(onNGMedalFetch);
// Load Scoreboards hten call onNGBoardsFetch()
NG.core.requestScoreBoards(onNGBoardsFetch);
ngDataLoaded.dispatch();
}
// --- MEDALS
function onNGMedalFetch():Void
{
/*
// 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
function onNGBoardsFetch():Void
{
/*
// 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
}
2020-11-01 09:55:02 +00:00
inline static public function postScore(score:Int = 0, song:String)
{
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}');
}
}
}
2020-11-01 03:42:14 +00:00
function onNGScoresFetch():Void
{
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;
}
inline static public function logEvent(event:String)
{
2020-12-23 00:57:55 +00:00
NG.core.calls.event.logEvent(event).send();
trace('should have logged: ' + event);
}
2020-11-01 03:42:14 +00:00
inline static public function unlockMedal(id:Int)
{
if (isLoggedIn)
{
var medal = NG.core.medals.get(id);
if (!medal.unlocked)
medal.sendUnlock();
}
}
}