ng release and blank controls

This commit is contained in:
Brandon
2020-11-01 14:16:22 -05:00
parent 968a001a36
commit 5fa4bc458e
65 changed files with 3468 additions and 126 deletions

View File

@@ -0,0 +1,20 @@
package io.newgrounds.objects;
class Error {
public var code(default, null):Int;
public var message(default, null):String;
public function new (message:String, code:Int = 0) {
this.message = message;
this.code = code;
}
public function toString():String {
if (code > 0)
return '#$code - $message';
return message;
}
}

View File

@@ -0,0 +1,118 @@
package io.newgrounds.objects;
import io.newgrounds.objects.events.Response;
import io.newgrounds.objects.events.Result.MedalUnlockResult;
import io.newgrounds.utils.Dispatcher;
import io.newgrounds.NGLite;
class Medal extends Object {
inline static public var EASY :Int = 1;
inline static public var MODERATE :Int = 2;
inline static public var CHALLENGING:Int = 3;
inline static public var DIFFICULT :Int = 4;
inline static public var BRUTAL :Int = 5;
static var difficultyNames:Array<String> =
[ "Easy"
, "Moderate"
, "Challenging"
, "Difficult"
, "Brutal"
];
// --- FROM SERVER
public var id (default, null):Int;
public var name (default, null):String;
public var description(default, null):String;
public var icon (default, null):String;
public var value (default, null):Int;
public var difficulty (default, null):Int;
public var secret (default, null):Bool;
public var unlocked (default, null):Bool;
// --- HELPERS
public var difficultyName(get, never):String;
public var onUnlock:Dispatcher;
public function new(core:NGLite, data:Dynamic = null):Void {
onUnlock = new Dispatcher();
super(core, data);
}
@:allow(io.newgrounds.NG)
override function parse(data:Dynamic):Void {
var wasLocked = !unlocked;
id = data.id;
name = data.name;
description = data.description;
icon = data.icon;
value = data.value;
difficulty = data.difficulty;
secret = data.secret == 1;
unlocked = data.unlocked;
super.parse(data);
if (wasLocked && unlocked)
onUnlock.dispatch();
}
public function sendUnlock():Void {
if (_core.sessionId == null) {
// --- Unlock regardless, show medal popup to encourage NG signup
unlocked = true;
onUnlock.dispatch();
//TODO: save unlock in local save
}
_core.calls.medal.unlock(id)
.addDataHandler(onUnlockResponse)
.send();
}
function onUnlockResponse(response:Response<MedalUnlockResult>):Void {
if (response.success && response.result.success) {
parse(response.result.data.medal);
// --- Unlock response doesn't include unlock=true, so parse won't change it.
if (!unlocked) {
unlocked = true;
onUnlock.dispatch();
}
}
}
/** Locks the medal on the client and sends an unlock request, Server responds the same either way. */
public function sendDebugUnlock():Void {
if (NG.core.sessionId == null) {
onUnlock.dispatch();
} else {
unlocked = false;
sendUnlock();
}
}
public function get_difficultyName():String {
return difficultyNames[difficulty - 1];
}
public function toString():String {
return 'Medal: $id@$name (${unlocked ? "unlocked" : "locked"}, $value pts, $difficultyName).';
}
}

View File

@@ -0,0 +1,33 @@
package io.newgrounds.objects;
import io.newgrounds.utils.Dispatcher;
import io.newgrounds.NGLite;
class Object {
var _core:NGLite;
public var onUpdate(default, null):Dispatcher;
public function new(core:NGLite, data:Dynamic = null) {
this._core = core;
onUpdate = new Dispatcher();
if (data != null)
parse(data);
}
@:allow(io.newgrounds.NGLite)
function parse(data:Dynamic):Void {
onUpdate.dispatch();
}
public function destroy():Void {
_core = null;
}
}

View File

@@ -0,0 +1,17 @@
package io.newgrounds.objects;
/** We don't want to serialize scores since there's a bajillion of them. */
typedef Score = {
/** The value value in the format selected in your scoreboard settings. */
var formatted_value:String;
/** The tag attached to this value (if any). */
var tag:String;
/** The user who earned value. If this property is absent, the value belongs to the active user. */
var user:User;
/** The integer value of the value. */
var value:Int;
}

View File

@@ -0,0 +1,76 @@
package io.newgrounds.objects;
import io.newgrounds.components.ScoreBoardComponent.Period;
import io.newgrounds.objects.events.Response;
import io.newgrounds.objects.events.Result;
import io.newgrounds.objects.events.Result.ScoreResult;
import io.newgrounds.NGLite;
class ScoreBoard extends Object {
public var scores(default, null):Array<Score>;
/** The numeric ID of the scoreboard.*/
public var id(default, null):Int;
/** The name of the scoreboard. */
public var name(default, null):String;
public function new(core:NGLite, data:Dynamic):Void {super(core, data); }
override function parse(data:Dynamic):Void {
id = data.id;
name = data.name;
super.parse(data);
}
/**
* Fetches score data from the server, this removes all of the existing scores cached
*
* We don't unify the old and new scores because a user's rank or score may change between requests
*/
public function requestScores
( limit :Int = 10
, skip :Int = 0
, period:Period = Period.ALL
, social:Bool = false
, tag :String = null
, user :Dynamic = null
):Void {
_core.calls.scoreBoard.getScores(id, limit, skip, period, social, tag, user)
.addDataHandler(onScoresReceived)
.send();
}
function onScoresReceived(response:Response<ScoreResult>):Void {
if (!response.success || !response.result.success)
return;
scores = response.result.data.scores;
_core.logVerbose('received ${scores.length} scores');
onUpdate.dispatch();
}
public function postScore(value :Int, tag:String = null):Void {
_core.calls.scoreBoard.postScore(id, value, tag)
.addDataHandler(onScorePosted)
.send();
}
function onScorePosted(response:Response<PostScoreResult>):Void {
}
public function toString():String {
return 'ScoreBoard: $id@$name';
}
}

View File

@@ -0,0 +1,65 @@
package io.newgrounds.objects;
class Session extends Object {
/** If true, the session_id is expired. Use App.startSession to get a new one.*/
public var expired(default, null):Bool;
/** A unique session identifier */
public var id(default, null):String;
/** If the session has no associated user but is not expired, this property will provide a URL that can be used to sign the user in. */
public var passportUrl(default, null):String;
/** If true, the user would like you to remember their session id. */
public var remember(default, null):Bool;
/** If the user has not signed in, or granted access to your app, this will be null */
public var user(default, null):User;
//TODO:desciption
public var status(get, never):SessionStatus;
public function new(core:NGLite, data:Dynamic = null) { super(core, data); }
override public function parse(data:Dynamic):Void {
id = data.id;
expired = data.expired;
passportUrl = data.passport_url;
remember = data.remember;
// --- KEEP THE SAME INSTANCE
if (user == null)
user = data.user;
// TODO?: update original user instance with new data. (probly not)
super.parse(data);
}
public function get_status():SessionStatus {
if (expired || id == null || id == "")
return SessionStatus.SESSION_EXPIRED;
if (user != null && user.name != null && user.name != "")
return SessionStatus.USER_LOADED;
return SessionStatus.REQUEST_LOGIN;
}
public function expire():Void {
expired = true;
id = null;
user = null;
}
}
@:enum
abstract SessionStatus(String) {
var SESSION_EXPIRED = "session-expired";
var REQUEST_LOGIN = "request-login";
var USER_LOADED = "user-loaded";
}

View File

@@ -0,0 +1,19 @@
package io.newgrounds.objects;
typedef User = {
/** The user's icon images. */
var icons:UserIcons;
/** The user's numeric ID. */
var id:Int;
/** The user's textual name. */
var name:String;
/** Returns true if the user has a Newgrounds Supporter upgrade. */
var supporter:Bool;
/** The user's NG profile url. */
var url:String;
}

View File

@@ -0,0 +1,14 @@
package io.newgrounds.objects;
typedef UserIcons = {
/**The URL of the user's large icon. */
var large:String;
/** The URL of the user's medium icon. */
var medium:String;
/** The URL of the user's small icon. */
var small:String;
}

View File

@@ -0,0 +1,43 @@
package io.newgrounds.objects.events;
import io.newgrounds.objects.events.Result.ResultBase;
import haxe.Json;
import io.newgrounds.objects.Error;
typedef DebugResponse = {
var exec_time:Int;
var input:Dynamic;
}
class Response<T:ResultBase> {
public var success(default, null):Bool;
public var error(default, null):Error;
public var debug(default, null):DebugResponse;
public var result(default, null):Result<T>;
public function new (core:NGLite, reply:String) {
var data:Dynamic;
try {
data = Json.parse(reply);
} catch (e:Dynamic) {
data = Json.parse('{"success":false,"error":{"message":"${Std.string(reply)}","code":0}}');
}
success = data.success;
debug = data.debug;
if (!success) {
error = new Error(data.error.message, data.error.code);
core.logError('Call unseccessful: $error');
return;
}
result = new Result<T>(core, data.result);
}
}

View File

@@ -0,0 +1,109 @@
package io.newgrounds.objects.events;
class Result<T:ResultBase> {
public var echo(default, null):String;
public var component(default, null):String;
public var data(default, null):T;
public var success(default, null):Bool;
public var debug(default, null):Bool;
public var error(default, null):Error;
public function new(core:NGLite, data:Dynamic) {
echo = data.echo;
component = data.component;
data = data.data;
success = data.success;
debug = data.debug;
if(!data.success) {
error = new Error(data.error.message, data.error.code);
core.logError('$component fail: $error');
} else
this.data = data;
}
}
typedef ResultBase = { };
typedef SessionResult = {
> ResultBase,
var session:Dynamic;
}
typedef GetHostResult = {
> ResultBase,
var host_approved:Bool;
}
typedef GetCurrentVersionResult = {
> ResultBase,
var current_version:String;
var client_deprecated:Bool;
}
typedef LogEventResult = {
> ResultBase,
var event_name:String;
}
typedef GetDateTimeResult = {
> ResultBase,
var datetime:String;
}
typedef GetVersionResult = {
> ResultBase,
var version:String;
}
typedef PingResult = {
> ResultBase,
var pong:String;
}
typedef MedalListResult = {
> ResultBase,
var medals:Array<Dynamic>;
}
typedef MedalUnlockResult = {
> ResultBase,
var medal_score:String;
var medal:Dynamic;
}
typedef ScoreBoardResult = {
> ResultBase,
var scoreboards:Array<Dynamic>;
}
typedef ScoreResult = {
> ResultBase,
var scores:Array<Score>;
var scoreboard:Dynamic;
}
typedef PostScoreResult = {
> ResultBase,
var tag:String;
var scoreboard:Dynamic;
var score:Score;
}