Skip to content

Commit

Permalink
add score updates to multiplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
flustix committed Sep 5, 2024
1 parent 6b47d35 commit 0c6a70e
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 2 deletions.
3 changes: 3 additions & 0 deletions fluXis.Game/Online/API/Models/Multi/MultiplayerRoom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using fluXis.Shared.Components.Maps;
using fluXis.Shared.Components.Multi;
using fluXis.Shared.Components.Users;
using fluXis.Shared.Scoring;

namespace fluXis.Game.Online.API.Models.Multi;

Expand All @@ -12,4 +13,6 @@ public class MultiplayerRoom : IMultiplayerRoom
public APIUser Host { get; set; } = null!;
public List<IMultiplayerParticipant> Participants { get; init; } = new();
public APIMap Map { get; set; } = null!;

public List<ScoreInfo> Scores { get; set; } = new();
}
3 changes: 3 additions & 0 deletions fluXis.Game/Online/Fluxel/FluxelClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ void handleListener<T>(string msg)
// EventType.MultiplayerRoomUpdate => handleListener<MultiplayerRoomUpdate>,
EventType.MultiplayerReady => handleListener<MultiReadyPacket>,
EventType.MultiplayerStartGame => handleListener<MultiStartPacket>,
EventType.MultiplayerScore => handleListener<MultiScorePacket>,
EventType.MultiplayerFinish => handleListener<MultiFinishPacket>,
_ => _ => { }
};
Expand Down Expand Up @@ -538,6 +539,7 @@ private static EventType getType(string id)
PacketIDs.MULTIPLAYER_UPDATE => EventType.MultiplayerRoomUpdate,
PacketIDs.MULTIPLAYER_READY => EventType.MultiplayerReady,
PacketIDs.MULTIPLAYER_START => EventType.MultiplayerStartGame,
PacketIDs.MULTIPLAYER_SCORE => EventType.MultiplayerScore,
PacketIDs.MULTIPLAYER_FINISH => EventType.MultiplayerFinish,

_ => throw new ArgumentOutOfRangeException(nameof(id), id, "Unknown packet ID!")
Expand Down Expand Up @@ -581,5 +583,6 @@ public enum EventType
MultiplayerRoomUpdate,
MultiplayerReady,
MultiplayerStartGame,
MultiplayerScore,
MultiplayerFinish
}
24 changes: 23 additions & 1 deletion fluXis.Game/Online/Multiplayer/MultiplayerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public abstract partial class MultiplayerClient : Component
public event Action<string> MapChangeFailed;

public event Action OnStart;
public event Action<long, int> OnScore;
public event Action<List<ScoreInfo>> OnResultsReady;

public event Action OnDisconnect;
Expand Down Expand Up @@ -109,7 +110,27 @@ protected Task MapChanged(bool success, APIMap map, string error)

protected Task Starting()
{
Schedule(() => OnStart?.Invoke());
Schedule(() =>
{
Room.Scores = new List<ScoreInfo>();
Room.Scores.AddRange(Room.Participants.Where(p => p.ID != Player.ID).Select(p => new ScoreInfo { PlayerID = p.ID }));

OnStart?.Invoke();
});

return Task.CompletedTask;
}

protected Task ScoreUpdated(long id, int score)
{
Schedule(() =>
{
if (id == Player.ID)
return;

OnScore?.Invoke(id, score);
});

return Task.CompletedTask;
}

Expand Down Expand Up @@ -147,6 +168,7 @@ protected void Disconnect()
protected abstract Task<MultiplayerRoom> CreateRoom(string name, long mapid, string hash);
public abstract Task LeaveRoom();
public abstract Task ChangeMap(long map, string hash);
public abstract Task UpdateScore(int score);
public abstract Task Finish(ScoreInfo score);

#endregion
Expand Down
9 changes: 9 additions & 0 deletions fluXis.Game/Online/Multiplayer/OnlineMultiplayerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ private void load()
api.RegisterListener<MultiStatePacket>(EventType.MultiplayerState, onUserStateChange);
api.RegisterListener<MultiMapPacket>(EventType.MultiplayerMap, onMapChange);
api.RegisterListener<MultiStartPacket>(EventType.MultiplayerStartGame, onStartGame);
api.RegisterListener<MultiScorePacket>(EventType.MultiplayerScore, onScoreUpdate);
api.RegisterListener<MultiFinishPacket>(EventType.MultiplayerFinish, onGameFinished);
}

Expand All @@ -42,6 +43,7 @@ protected override void Dispose(bool isDisposing)
api.UnregisterListener<MultiStatePacket>(EventType.MultiplayerState, onUserStateChange);
api.UnregisterListener<MultiMapPacket>(EventType.MultiplayerMap, onMapChange);
api.UnregisterListener<MultiStartPacket>(EventType.MultiplayerStartGame, onStartGame);
api.UnregisterListener<MultiScorePacket>(EventType.MultiplayerScore, onScoreUpdate);
api.UnregisterListener<MultiFinishPacket>(EventType.MultiplayerFinish, onGameFinished);
}

Expand All @@ -65,6 +67,7 @@ private void statusChanged(ValueChangedEvent<ConnectionStatus> e)
private void onUserStateChange(FluxelReply<MultiStatePacket> reply) => UserStateChanged(reply.Data!.UserID, reply.Data.State);
private void onMapChange(FluxelReply<MultiMapPacket> reply) => MapChanged(reply.Success, reply.Data.Map, reply.Message);
private void onStartGame(FluxelReply<MultiStartPacket> reply) => Starting();
private void onScoreUpdate(FluxelReply<MultiScorePacket> reply) => ScoreUpdated(reply.Data!.UserID, reply.Data.Score);
private void onGameFinished(FluxelReply<MultiFinishPacket> reply) => ResultsReady(reply.Data.Scores);

protected override async Task<MultiplayerRoom> CreateRoom(string name, long mapid, string hash)
Expand Down Expand Up @@ -96,6 +99,12 @@ public override Task ChangeMap(long map, string hash)
return Task.CompletedTask;
}

public override Task UpdateScore(int score)
{
api.SendPacketAsync(MultiScorePacket.CreateC2S(score));
return Task.CompletedTask;
}

public override Task Finish(ScoreInfo score)
{
api.SendPacketAsync(MultiCompletePacket.CreateC2S(score));
Expand Down
21 changes: 21 additions & 0 deletions fluXis.Game/Screens/Multiplayer/Gameplay/MultiGameplayScreen.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Collections.Generic;
using System.Linq;
using fluXis.Game.Database.Maps;
using fluXis.Game.Graphics.UserInterface.Panel;
using fluXis.Game.Mods;
using fluXis.Game.Online.Activity;
using fluXis.Game.Online.Multiplayer;
using fluXis.Game.Screens.Gameplay;
using fluXis.Shared.Scoring;
using fluXis.Shared.Scoring.Structs;
using osu.Framework.Allocation;
using osu.Framework.Screens;

Expand Down Expand Up @@ -43,13 +45,20 @@ protected override void LoadComplete()
base.LoadComplete();
HealthProcessor.CanFail = false;

JudgementProcessor.ResultAdded += sendScore;

client.OnScore += onScoreUpdate;
client.OnResultsReady += onOnResultsReady;
client.OnDisconnect += onDisconnect;
}

protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);

JudgementProcessor.ResultAdded -= sendScore;

client.OnScore -= onScoreUpdate;
client.OnResultsReady -= onOnResultsReady;
client.OnDisconnect -= onDisconnect;
}
Expand All @@ -59,6 +68,18 @@ protected override void End()
client.Finish(ScoreProcessor.ToScoreInfo());
}

private void sendScore(HitResult _) => client.UpdateScore(ScoreProcessor.Score);

private void onScoreUpdate(long user, int score)
{
var si = client.Room.Scores.FirstOrDefault(s => s.PlayerID == user);

if (si is null)
return;

si.Score = score;
}

private void onOnResultsReady(List<ScoreInfo> scores)
{
if (this.IsCurrentScreen())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ private void startLoading()

var mods = new List<IMod>();

MultiScreen.Push(new GameplayLoader(map, mods, () => new MultiGameplayScreen(client, map, mods)));
MultiScreen.Push(new GameplayLoader(map, mods, () => new MultiGameplayScreen(client, map, mods) { Scores = client.Room.Scores }));
}

public override bool OnExiting(ScreenExitEvent e)
Expand Down
22 changes: 22 additions & 0 deletions fluXis.Shared/API/Packets/Multiplayer/MultiScorePacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace fluXis.Shared.API.Packets.Multiplayer;

public class MultiScorePacket : IPacket
{
public string ID => PacketIDs.MULTIPLAYER_SCORE;

// eventually needs to be fleshed out to do more than this
public int Score { get; set; }

#region Server2Client

public long UserID { get; set; }

#endregion

public static MultiScorePacket CreateC2S(int score)
=> new() { Score = score };

public static MultiScorePacket CreateS2C(long userId, int score)
=> new() { UserID = userId, Score = score };
}

0 comments on commit 0c6a70e

Please sign in to comment.