Skip to content

Commit

Permalink
discord game invites and story mode weeks in freeplay
Browse files Browse the repository at this point in the history
  • Loading branch information
Snirozu committed Mar 14, 2024
1 parent 98f5fb1 commit 9832260
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 45 deletions.
6 changes: 5 additions & 1 deletion source/backend/ClientPrefs.hx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class SaveVariables {
public var comboOffsetOP1:Array<Int> = [0, 0, 0, 0];
public var comboOffsetOP2:Array<Int> = [0, 0, 0, 0];
public var disableStrumMovement:Bool = false;
public var unlockFramerate:Bool = false;

public function new()
{
Expand Down Expand Up @@ -197,7 +198,10 @@ class ClientPrefs {
FlxG.autoPause = ClientPrefs.data.autoPause;
#end

if(data.framerate > FlxG.drawFramerate) {
if (ClientPrefs.data.unlockFramerate) {
FlxG.updateFramerate = 999;
FlxG.drawFramerate = 999;
} else if(data.framerate > FlxG.drawFramerate) {
FlxG.updateFramerate = data.framerate;
FlxG.drawFramerate = data.framerate;
} else {
Expand Down
56 changes: 53 additions & 3 deletions source/backend/Discord.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package backend;

import online.states.Room;
import online.Waiter;
import haxe.crypto.Md5;
import online.GameClient;
import Sys.sleep;
import discord_rpc.DiscordRpc;
import lime.app.Application;
Expand All @@ -10,7 +14,7 @@ class DiscordClient
private static var _defaultID:String = "1185697129717583982";
public static var clientID(default, set):String = _defaultID;

private static var _options:Dynamic = {
private static var _options:DiscordPresenceOptions = {
details: "In the Menus",
state: null,
largeImageKey: 'icon',
Expand All @@ -27,7 +31,9 @@ class DiscordClient
clientID: clientID,
onReady: onReady,
onError: onError,
onDisconnected: onDisconnected
onDisconnected: onDisconnected,
onRequest: onRequest,
onJoin: onJoin
});
trace("Discord Client started.");

Expand All @@ -42,6 +48,24 @@ class DiscordClient
//DiscordRpc.shutdown();
}

static function onRequest(req:Dynamic) {
DiscordRpc.respond(req.userId, !GameClient.room.state.isPrivate ? Reply.Yes : Reply.No);
}

static function onJoin(secret:String) {
Waiter.put(() -> {
GameClient.joinRoom(secret, (err) -> {
if (err != null) {
return;
}

Waiter.put(() -> {
MusicBeatState.switchState(new Room());
});
});
});
}

public static function check()
{
if(!ClientPrefs.data.discordRPC)
Expand Down Expand Up @@ -121,10 +145,36 @@ class DiscordClient
// Obtained times are in milliseconds so they are divided so Discord can use it
_options.startTimestamp = Std.int(startTimestamp / 1000);
_options.endTimestamp = Std.int(endTimestamp / 1000);
DiscordRpc.presence(_options);
updateOnlinePresence();
//DiscordRpc.presence(_options);

//trace('Discord RPC Updated. Arguments: $details, $state, $smallImageKey, $hasStartTimestamp, $endTimestamp');
}

public static function updateOnlinePresence() {
if (GameClient.isConnected()) {
if (!GameClient.room.state.isPrivate) {
_options.partyID = GameClient.rpcClientRoomID;
_options.joinSecret = GameClient.getRoomSecret(true);
_options.state = "In a Public Room";
}
else {
_options.partyID = null;
_options.joinSecret = null;
_options.state = "In a Private Room";
}
_options.partySize = GameClient.getPlayerCount();
_options.partyMax = 2;
}
else {
_options.partyID = null;
_options.joinSecret = null;
_options.partySize = 0;
_options.partyMax = 0;
_options.state = null;
}
DiscordRpc.presence(_options);
}

public static function resetClientID()
clientID = _defaultID;
Expand Down
22 changes: 16 additions & 6 deletions source/backend/WeekData.hx
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ class WeekData {
this.fileName = fileName;
}

public static function reloadWeekFiles(isStoryMode:Null<Bool> = false)
public static function reloadWeekFiles(?isStoryMode:Null<Bool> /*= false*/)
{
if (isStoryMode == null) // dead code btw
isStoryMode = PlayState.isStoryMode;

weeksList = [];
weeksLoaded.clear();
#if MODS_ALLOWED
Expand Down Expand Up @@ -112,7 +115,12 @@ class WeekData {
}
#end

if(weekFile != null && (isStoryMode == null || (isStoryMode && !weekFile.hideStoryMode) || (!isStoryMode && !weekFile.hideFreeplay))) {
if (
weekFile != null && (
(online.GameClient.isConnected() && !isStoryMode) || //freeplay unlocked patched ad free
((isStoryMode && !weekFile.hideStoryMode) || (!isStoryMode && !weekFile.hideFreeplay))
)
) {
weeksLoaded.set(sexList[i], weekFile);
weeksList.push(sexList[i]);
}
Expand All @@ -131,7 +139,7 @@ class WeekData {
var path:String = directory + daWeek + '.json';
if(sys.FileSystem.exists(path))
{
addWeek(daWeek, path, directories[i], i, originalLength);
addWeek(isStoryMode, daWeek, path, directories[i], i, originalLength);
}
}

Expand All @@ -140,15 +148,15 @@ class WeekData {
var path = haxe.io.Path.join([directory, file]);
if (!sys.FileSystem.isDirectory(path) && file.endsWith('.json'))
{
addWeek(file.substr(0, file.length - 5), path, directories[i], i, originalLength);
addWeek(isStoryMode, file.substr(0, file.length - 5), path, directories[i], i, originalLength);
}
}
}
}
#end
}

private static function addWeek(weekToCheck:String, path:String, directory:String, i:Int, originalLength:Int)
private static function addWeek(isStoryMode:Bool, weekToCheck:String, path:String, directory:String, i:Int, originalLength:Int)
{
if(!weeksLoaded.exists(weekToCheck))
{
Expand All @@ -162,7 +170,9 @@ class WeekData {
weekFile.folder = directory.substring(Paths.mods().length, directory.length-1);
#end
}
if((PlayState.isStoryMode && !weekFile.hideStoryMode) || (!PlayState.isStoryMode && !weekFile.hideFreeplay))
if(
(online.GameClient.isConnected() && !isStoryMode) || // freeplay unlocked patched ad free
((isStoryMode && !weekFile.hideStoryMode) || (!isStoryMode && !weekFile.hideFreeplay)))
{
weeksLoaded.set(weekToCheck, weekFile);
weeksList.push(weekToCheck);
Expand Down
2 changes: 1 addition & 1 deletion source/online/FunkinPoints.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import online.states.ResultsScreen;
@:build(online.Macros.getSetForwarder())
class FunkinPoints {
@:forwardField(FlxG.save.data.funkinPointsv1, 0)
public static var funkinPoints(get, null):Float;
public static var funkinPoints(get, set):Float;

public static function calcFP(accuracy:Float, misses:Float, noteDensity:Float, notesHit:Float, combo:Float, songSpeed:Float):Float {
if (accuracy <= 0)
Expand Down
27 changes: 26 additions & 1 deletion source/online/GameClient.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package online;

import haxe.crypto.Md5;
import backend.Song;
import backend.Rating;
import online.schema.Player;
Expand All @@ -22,6 +23,7 @@ class GameClient {
public static var isOwner:Bool;
public static var address:String;
public static var reconnectTries:Int = 0;
public static var rpcClientRoomID:String;

/**
* the server address that the player set, if player set nothing then it returns `serverAddresses[0]`
Expand All @@ -41,6 +43,9 @@ class GameClient {
client = new Client(address);

client.create("room", getOptions(true), RoomState, (err, room) -> _onJoin(err, room, true, address, onJoin));
}, (exc) -> {
LoadingScreen.toggle(false);
Alert.alert("Failed to connect!", exc.toString());
});
}

Expand All @@ -61,6 +66,9 @@ class GameClient {
client = new Client(roomAddress);

client.joinById(roomID, getOptions(false), RoomState, (err, room) -> _onJoin(err, room, false, roomAddress, onJoin));
}, (exc) -> {
LoadingScreen.toggle(false);
Alert.alert("Failed to connect!", exc.toString());
});
}

Expand All @@ -77,6 +85,7 @@ class GameClient {
GameClient.room = room;
GameClient.isOwner = isHost;
GameClient.address = address;
GameClient.rpcClientRoomID = Md5.encode(FlxG.random.int(0, 1000000).hex());
clearOnMessage();

GameClient.room.onError += (id:Int, e:String) -> {
Expand Down Expand Up @@ -178,6 +187,7 @@ class GameClient {
GameClient.room = null;
GameClient.isOwner = false;
GameClient.address = null;
GameClient.rpcClientRoomID = null;

//Downloader.cancelAll();
});
Expand Down Expand Up @@ -219,6 +229,12 @@ class GameClient {
#end
});
});

#if desktop
GameClient.room.state.listen("isPrivate", (value, prev) -> {
DiscordClient.updateOnlinePresence();
});
#end
}

public static function send(type:Dynamic, ?message:Null<Dynamic>) {
Expand Down Expand Up @@ -268,7 +284,7 @@ class GameClient {
});
}

public static function getPlayerCount(callback:(v:Null<Int>)->Void) {
public static function getServerPlayerCount(callback:(v:Null<Int>)->Void) {
Thread.run(() -> {
var http = new Http(addressToUrl(serverAddress) + "/api/online");

Expand Down Expand Up @@ -328,4 +344,13 @@ class GameClient {
GameClient.send("setGameplaySetting", [key, value]);
}
}

public static function getPlayerCount():Int {
if (!GameClient.isConnected())
return 0;

if (GameClient.room.state.player2 != null && GameClient.room.state.player2.name != "")
return 2;
return 1;
}
}
7 changes: 6 additions & 1 deletion source/online/Macros.hx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ class Macros {
access: fieldAccess,
kind: FieldType.FFun({
args: [],
expr: macro return ${meta.params[0]} ?? ${meta.params[1]}
expr: macro {
if (${meta.params[0]} == null)
${meta.params[0]} = ${meta.params[1]};

return ${meta.params[0]}
}
}),
pos: pos,
});
Expand Down
9 changes: 7 additions & 2 deletions source/online/Thread.hx
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package online;

import haxe.Exception;

class Thread {
public static function run(func:Void->Void) {
public static function run(func:Void->Void, ?onException:Exception->Void) {
sys.thread.Thread.create(() -> {
try {
func();
}
catch (exc) {
Waiter.put(() -> { // waiter more errors please!
throw exc;
if (onException != null)
onException(exc);
else
throw exc;
});
}
});
Expand Down
4 changes: 3 additions & 1 deletion source/online/states/BananaDownload.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class BananaDownload extends MusicBeatState {
DiscordClient.changePresence("Browsing mods on GameBanana.", null, null, false);
#end

GameClient.send("status", "Browsing mods on GameBanana");

var bg:FlxSprite = new FlxSprite().loadGraphic(Paths.image('menuDesat'));
bg.color = 0xff46463b;
bg.updateHitbox();
Expand Down Expand Up @@ -143,7 +145,7 @@ class BananaDownload extends MusicBeatState {
if (!searchInput.hasFocus) {
if (controls.BACK) {
FlxG.sound.music.volume = 1;
MusicBeatState.switchState(new OnlineState());
MusicBeatState.switchState(GameClient.isConnected() ? new Room() : new OnlineState());
FlxG.sound.play(Paths.sound('cancelMenu'));
}

Expand Down
4 changes: 2 additions & 2 deletions source/online/states/OnlineState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class OnlineState extends MusicBeatState {
OnlineMods.checkMods();

#if desktop
DiscordClient.changePresence("In online lobby.", null, null, false);
DiscordClient.changePresence("In the Online Menu.", null, null, false);
#end

var bg:FlxSprite = new FlxSprite().loadGraphic(Paths.image('menuDesat'));
Expand Down Expand Up @@ -136,7 +136,7 @@ class OnlineState extends MusicBeatState {

changeSelection(0);

GameClient.getPlayerCount((v) -> {
GameClient.getServerPlayerCount((v) -> {
if (v == null) {
playersOnline.text = "OFFLINE";
//thought this would look cool
Expand Down
Loading

0 comments on commit 9832260

Please sign in to comment.