Commit c32be040 authored by June's avatar June
Browse files

Player locations are now persisted in Redis

parent 87ee02bb
......@@ -5,6 +5,7 @@ using System.Text;
namespace MomentaryMeeting.Game
{
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Hosting;
using MomentaryMeeting.Game.Interactions;
using System;
......@@ -26,9 +27,10 @@ namespace MomentaryMeeting.Game
private ArrangementGenerator _arrangementGenerator;
private InteractionManager _interactionManager;
private readonly IHostEnvironment _hostEnvironment;
private readonly IDistributedCache _distributedCache;
private readonly Database _database;
public CommandParser(ILogger<CommandParser> logger, IDatabaseApi databaseApi, ArrangementGenerator arrangementGenerator, InteractionManager interactionManager, IHostEnvironment hostEnvironment)
public CommandParser(ILogger<CommandParser> logger, IDatabaseApi databaseApi, ArrangementGenerator arrangementGenerator, InteractionManager interactionManager, IHostEnvironment hostEnvironment, IDistributedCache distributedCache)
{
_databaseApi = databaseApi;
_database = _databaseApi.Current;
......@@ -57,6 +59,7 @@ namespace MomentaryMeeting.Game
_interactionManager = interactionManager;
_hostEnvironment = hostEnvironment;
_distributedCache = distributedCache;
}
private static bool IsCardinal(string s)
......@@ -119,7 +122,7 @@ namespace MomentaryMeeting.Game
response = _database.GlobalText.SelectFromList("can_do").Replace("__VERB__", verb).Replace("__SUBJECT__", direction)
+ "{.5}\n\n";
_logger.LogInformation($"Moving player to new location {newLocation}");
player.LocationName = newLocation;
await player.SetLocationNameAsync(_distributedCache, newLocation);
response += GetRoomDescription(player);
response += "\n\n" + await _interactionManager.CheckForInteraction(gameInstance, player);
}
......@@ -536,7 +539,7 @@ namespace MomentaryMeeting.Game
$" run - Moves in a direction (forward, left, right, back)\n" +
$" AKA: walk, go, move\n\n";
if(_hostEnvironment.IsDevelopment())
if (_hostEnvironment.IsDevelopment())
{
response += $"DEVELOPER ENVIRONMENT DETECTED!!!\n" +
$"Here are some additional developer commands:\n" +
......@@ -571,7 +574,7 @@ namespace MomentaryMeeting.Game
string newLoc = words[1];
response = $"Through powers unknown, you instantaneously teleport from {player.LocationName} to {newLoc}!\n\n";
player.LocationName = newLoc;
await player.SetLocationNameAsync(_distributedCache, newLoc);
// then return new location
response += GetRoomDescription(player);
......
......@@ -7,6 +7,7 @@ using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
namespace MomentaryMeeting.Game
{
......@@ -28,15 +29,17 @@ namespace MomentaryMeeting.Game
private readonly ILogger<GameInstance> _logger;
private readonly CommandParser _commandParser;
private ArrangementGenerator _arrangementGenerator;
private readonly IDistributedCache _distributedCache;
private Task _arrangementGeneratorTask;
public GameInstance(IDatabaseApi databaseApi, ILogger<GameInstance> logger, CommandParser commandParser, ArrangementGenerator arrangementGenerator)
public GameInstance(IDatabaseApi databaseApi, ILogger<GameInstance> logger, CommandParser commandParser, ArrangementGenerator arrangementGenerator, IDistributedCache distributedCache)
{
_players = new Dictionary<ulong, ConnectionInfo>();
_databaseApi = databaseApi;
_logger = logger;
_commandParser = commandParser;
_arrangementGenerator = arrangementGenerator;
_distributedCache = distributedCache;
// TODO: put this in a better place
// Also schedule it to be called at some interval
_arrangementGenerator.Generate();
......@@ -73,11 +76,19 @@ namespace MomentaryMeeting.Game
CloseWebSocket = socketFinishedTsc,
};
// set up user
incomingUser.LocationName = GameHelper.GetRandomLocation(_databaseApi.Current);
while (_databaseApi.Current.Locations[incomingUser.LocationName].NoSpawn) // tries to set a spawnable location
var existingLocation = await _distributedCache.GetStringAsync("loc-" + incomingUser.DiscordId);
if (existingLocation != null)
{
incomingUser.LocationName = GameHelper.GetRandomLocation(_databaseApi.Current);
await incomingUser.SetLocationNameAsync(_distributedCache, existingLocation);
}
else
{
var desiredLocation = GameHelper.GetRandomLocation(_databaseApi.Current);
while (_databaseApi.Current.Locations[desiredLocation].NoSpawn) // tries to set a spawnable location
{
desiredLocation = GameHelper.GetRandomLocation(_databaseApi.Current);
}
await incomingUser.SetLocationNameAsync(_distributedCache, desiredLocation);
}
await SyncPlayerMusic(_players[incomingUser.DiscordId]);
......@@ -140,7 +151,7 @@ namespace MomentaryMeeting.Game
{
return _players[id];
}
public async Task SyncPlayerMusic(ConnectionInfo connection)
{
var location = _databaseApi.Current.Locations[connection.User.LocationName];
......
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using MomentaryMeeting.Game.Interactions;
using MomentaryMeeting.Models;
using MomentaryMeeting.Services;
......@@ -20,16 +21,18 @@ namespace MomentaryMeeting.Game
private readonly IDatabaseApi _databaseApi;
private readonly Database _database;
private readonly ILogger<InteractionManager> _logger;
private readonly ArrangementGenerator _arrangementGenerator;
private readonly ArrangementGenerator _arrangementGenerator;
private readonly IDistributedCache _distributedCache;
private List<Meeting> _meetings;
private GameInstance _gameInstance;
public InteractionManager(IDatabaseApi databaseApi, ILogger<InteractionManager> logger, ArrangementGenerator arrangementGenerator)
public InteractionManager(IDatabaseApi databaseApi, ILogger<InteractionManager> logger, ArrangementGenerator arrangementGenerator, IDistributedCache distributedCache)
{
_databaseApi = databaseApi;
_database = _databaseApi.Current;
_logger = logger;
_arrangementGenerator = arrangementGenerator;
_arrangementGenerator = arrangementGenerator;
_distributedCache = distributedCache;
_meetings = new List<Meeting>();
}
......@@ -37,7 +40,7 @@ namespace MomentaryMeeting.Game
{
if (_gameInstance == null)
_gameInstance = gameInstance;
foreach(UserModel otherUser in gameInstance.GetPlayers())
foreach (UserModel otherUser in gameInstance.GetPlayers())
{
_logger.LogInformation($"ID: {player.DiscordId} {otherUser.DiscordId} {player.DiscordId != otherUser.DiscordId} object: {player != otherUser}");
// if someone else is in the room, and they are not me and they are not in meeting
......@@ -89,8 +92,8 @@ namespace MomentaryMeeting.Game
if (westNeighbour != "")
rooms.Add(westNeighbour);
newRoom = rooms[new Random().Next(rooms.Count)];
}
player.LocationName = newRoom;
}
await player.SetLocationNameAsync(_distributedCache, newRoom);
await _gameInstance.NotifyPlayerOfMeetingEnd(player.DiscordId);
}
_gameInstance.GenerateAndNotify();
......@@ -98,7 +101,7 @@ namespace MomentaryMeeting.Game
public Meeting FindMeeting(UserModel aUser)
{
foreach(Meeting meeting in _meetings)
foreach (Meeting meeting in _meetings)
{
if (meeting._users.Contains(aUser))
return meeting;
......
using Microsoft.Extensions.Caching.Distributed;
using MomentaryMeeting.Services;
using System.Threading.Tasks;
namespace MomentaryMeeting.Models
{
......@@ -8,7 +10,13 @@ namespace MomentaryMeeting.Models
public string Username { get; set; }
public string LocationName { get; set; }
public string LocationName { get; private set; }
public async Task SetLocationNameAsync(IDistributedCache distributedCache, string newLocation)
{
this.LocationName = newLocation;
await distributedCache.SetStringAsync("loc-" + DiscordId, newLocation);
}
public enum CardinalDirection
{
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment