Skip to content

Commit

Permalink
Merge pull request #249 from smoogipoo/better-room-status
Browse files Browse the repository at this point in the history
Update databased room status depending on gameplay state
  • Loading branch information
peppy authored Nov 27, 2024
2 parents 3f5291e + dce812d commit 8719ea4
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
12 changes: 12 additions & 0 deletions osu.Server.Spectator/Database/DatabaseAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ public async Task UpdateRoomSettingsAsync(MultiplayerRoom room)
});
}

public async Task UpdateRoomStatusAsync(MultiplayerRoom room)
{
var connection = await getConnectionAsync();

await connection.ExecuteAsync("UPDATE multiplayer_rooms SET status = @Status WHERE id = @RoomID", new
{
RoomID = room.RoomID,
// needs ToString() to store as enums correctly, see https://github.com/DapperLib/Dapper/issues/813.
Status = room.State.ToDatabaseRoomStatus().ToString(),
});
}

public async Task UpdateRoomHostAsync(MultiplayerRoom room)
{
var connection = await getConnectionAsync();
Expand Down
5 changes: 5 additions & 0 deletions osu.Server.Spectator/Database/IDatabaseAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public interface IDatabaseAccess : IDisposable
/// </summary>
Task UpdateRoomSettingsAsync(MultiplayerRoom room);

/// <summary>
/// Updates the current status of <paramref name="room"/> in the database.
/// </summary>
Task UpdateRoomStatusAsync(MultiplayerRoom room);

/// <summary>
/// Updates the current host of <paramref name="room"/> in the database.
/// </summary>
Expand Down
36 changes: 36 additions & 0 deletions osu.Server.Spectator/Database/Models/database_room_status.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using osu.Game.Online.Multiplayer;

namespace osu.Server.Spectator.Database.Models
{
// ReSharper disable once InconsistentNaming
[Serializable]
public enum database_room_status
{
idle,
playing
}

public static class DatabaseRoomStatusExtensions
{
public static database_room_status ToDatabaseRoomStatus(this MultiplayerRoomState state)
{
switch (state)
{
case MultiplayerRoomState.Open:
case MultiplayerRoomState.Closed:
return database_room_status.idle;

case MultiplayerRoomState.WaitingForLoad:
case MultiplayerRoomState.Playing:
return database_room_status.playing;

default:
throw new ArgumentOutOfRangeException(nameof(state), state, null);
}
}
}
}
1 change: 1 addition & 0 deletions osu.Server.Spectator/Database/Models/multiplayer_room.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class multiplayer_room
public DateTimeOffset? updated_at { get; set; }
public DateTimeOffset? deleted_at { get; set; }
public room_category category { get; set; }
public database_room_status status { get; set; }
public database_match_type type { get; set; }
public database_queue_mode queue_mode { get; set; }
public ushort auto_start_duration { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion osu.Server.Spectator/Hubs/Multiplayer/MultiplayerHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public MultiplayerHub(
Rooms = rooms;
this.databaseFactory = databaseFactory;
this.chatFilters = chatFilters;
HubContext = new MultiplayerHubContext(hubContext, rooms, users, loggerFactory);
HubContext = new MultiplayerHubContext(hubContext, rooms, users, databaseFactory, loggerFactory);
}

public Task<MultiplayerRoom> JoinRoom(long roomId) => JoinRoomWithPassword(roomId, string.Empty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using osu.Game.Online.API;
using osu.Game.Online.Multiplayer;
using osu.Game.Online.Rooms;
using osu.Server.Spectator.Database;
using osu.Server.Spectator.Entities;
using osu.Server.Spectator.Extensions;

Expand All @@ -30,17 +31,20 @@ public class MultiplayerHubContext : IMultiplayerHubContext
private readonly IHubContext<MultiplayerHub> context;
private readonly EntityStore<ServerMultiplayerRoom> rooms;
private readonly EntityStore<MultiplayerClientState> users;
private readonly IDatabaseFactory databaseFactory;
private readonly ILogger logger;

public MultiplayerHubContext(
IHubContext<MultiplayerHub> context,
EntityStore<ServerMultiplayerRoom> rooms,
EntityStore<MultiplayerClientState> users,
IDatabaseFactory databaseFactory,
ILoggerFactory loggerFactory)
{
this.context = context;
this.rooms = rooms;
this.users = users;
this.databaseFactory = databaseFactory;

logger = loggerFactory.CreateLogger(nameof(MultiplayerHub).Replace("Hub", string.Empty));
}
Expand Down Expand Up @@ -161,7 +165,11 @@ public async Task ChangeAndBroadcastUserBeatmapAvailability(ServerMultiplayerRoo
public async Task ChangeRoomState(ServerMultiplayerRoom room, MultiplayerRoomState newState)
{
log(room, null, $"Room state changing from {room.State} to {newState}");

room.State = newState;
using (var db = databaseFactory.GetInstance())
await db.UpdateRoomStatusAsync(room);

await context.Clients.Group(MultiplayerHub.GetGroupId(room.RoomID)).SendAsync(nameof(IMultiplayerClient.RoomStateChanged), newState);
}

Expand Down

0 comments on commit 8719ea4

Please sign in to comment.