Funkin/source/NGio.hx

200 lines
5.2 KiB
Haxe
Raw Normal View History

2020-10-31 23:42:14 -04:00
package;
import flixel.FlxG;
import flixel.util.FlxSignal;
2020-12-25 00:17:21 -05:00
import flixel.util.FlxTimer;
2020-10-31 23:42:14 -04: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-22 19:57:55 -05: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-10-31 23:42:14 -04:00
import openfl.display.Stage;
2021-02-14 19:45:18 -05:00
using StringTools;
2020-10-31 23:42:14 -04:00
/**
* 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-22 19:57:55 -05:00
public static var GAME_VER:String = "";
2021-02-14 19:45:18 -05:00
public static var GAME_VER_NUMS:String = '';
2020-12-25 00:17:21 -05:00
public static var gotOnlineVer:Bool = false;
2020-12-22 19:57:55 -05:00
2020-12-15 22:00:22 -05:00
public static function noLogin(api:String)
{
2020-12-22 19:57:55 -05:00
trace('INIT NOLOGIN');
GAME_VER = "v" + Application.current.meta.get('version');
if (api.length != 0)
2020-12-22 19:57:55 -05:00
{
NG.create(api);
new FlxTimer().start(2, function(tmr:FlxTimer)
2020-12-22 19:57:55 -05:00
{
var call = NG.core.calls.app.getCurrentVersion(GAME_VER).addDataHandler(function(response:Response<GetCurrentVersionResult>)
{
2021-02-14 19:45:18 -05:00
GAME_VER = response.result.data.currentVersion;
GAME_VER_NUMS = GAME_VER.split(" ")[0].trim();
trace('CURRENT NG VERSION: ' + GAME_VER);
gotOnlineVer = true;
});
2020-12-22 19:57:55 -05:00
call.send();
});
}
2020-12-15 22:00:22 -05:00
}
2020-10-31 23:42:14 -04: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 04:55:02 -05: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-10-31 23:42:14 -04: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-22 19:57:55 -05:00
NG.core.calls.event.logEvent(event).send();
trace('should have logged: ' + event);
}
2020-10-31 23:42:14 -04:00
inline static public function unlockMedal(id:Int)
{
if (isLoggedIn)
{
var medal = NG.core.medals.get(id);
if (!medal.unlocked)
medal.sendUnlock();
}
}
}