Commit 6fad2dcb authored by Chris Chua's avatar Chris Chua
Browse files

Setting meeting length to 90 s, players can lose each other if apart for more than 30 s

parent 9371acc6
......@@ -161,12 +161,21 @@ namespace MomentaryMeeting.Game
}
}
public async Task NotifyPlayerOfMeetingEnd(ulong discordId)
public async Task NotifyPlayerOfMeetingEnd(ulong discordId, bool isTornApart)
{
ConnectionInfo conn = GetConnection(discordId);
string term = _databaseApi.Current.GlobalText.SelectFromList("end_meeting");
term += "\n\n" + _commandParser.GetRoomDescription(conn.User);
await SendPromptToPlayer(conn, term);
string term;
if (isTornApart)
{
term = _databaseApi.Current.GlobalText.SelectFromList("end_meeting_torn");
term += "\n\n" + _commandParser.GetRoomDescription(conn.User);
await SendPromptToPlayer(conn, term);
}
else
{
term = ">" + _databaseApi.Current.GlobalText.SelectFromList("end_meeting_apart");
await SendPromptToPlayer(conn, term);
}
await SyncPlayerMusic(conn);
}
}
......
......@@ -10,7 +10,7 @@ namespace MomentaryMeeting.Game
{
public interface IEndMeetings
{
Task EndMeeting(Meeting meeting);
Task EndMeeting(Meeting meeting, bool isTornApart);
}
public class InteractionManager : IEndMeetings
......@@ -61,37 +61,40 @@ namespace MomentaryMeeting.Game
return null;
}
public async Task EndMeeting(Meeting meeting)
public async Task EndMeeting(Meeting meeting, bool isTornApart)
{
_logger.LogInformation("Ending meeting between " + meeting._users[0] + " and " + meeting._users[1]);
foreach (UserModel player in meeting._users)
{
player.InMeeting = false;
_meetings.Remove(meeting);
// send them two rooms away
ArrangementGenerator.Neighbours neighbours;
string newRoom = player.LocationName;
int roomDistance = 3;
for (int i = 0; i < roomDistance; i++)
if (isTornApart)
{
neighbours = _arrangementGenerator.GetNeighbours(newRoom);
List<string> rooms = new List<string>();
string northNeighbour = neighbours.GetNeighbour(UserModel.CardinalDirection.North);
if (northNeighbour != "")
rooms.Add(northNeighbour);
string southNeighbour = neighbours.GetNeighbour(UserModel.CardinalDirection.South);
if (southNeighbour != "")
rooms.Add(southNeighbour);
string eastNeighbour = neighbours.GetNeighbour(UserModel.CardinalDirection.East);
if (eastNeighbour != "")
rooms.Add(eastNeighbour);
string westNeighbour = neighbours.GetNeighbour(UserModel.CardinalDirection.West);
if (westNeighbour != "")
rooms.Add(westNeighbour);
newRoom = rooms[new Random().Next(rooms.Count)];
// send them several rooms away
ArrangementGenerator.Neighbours neighbours;
string newRoom = player.LocationName;
int roomDistance = 3;
for (int i = 0; i < roomDistance; i++)
{
neighbours = _arrangementGenerator.GetNeighbours(newRoom);
List<string> rooms = new List<string>();
string northNeighbour = neighbours.GetNeighbour(UserModel.CardinalDirection.North);
if (northNeighbour != "")
rooms.Add(northNeighbour);
string southNeighbour = neighbours.GetNeighbour(UserModel.CardinalDirection.South);
if (southNeighbour != "")
rooms.Add(southNeighbour);
string eastNeighbour = neighbours.GetNeighbour(UserModel.CardinalDirection.East);
if (eastNeighbour != "")
rooms.Add(eastNeighbour);
string westNeighbour = neighbours.GetNeighbour(UserModel.CardinalDirection.West);
if (westNeighbour != "")
rooms.Add(westNeighbour);
newRoom = rooms[new Random().Next(rooms.Count)];
}
player.LocationName = newRoom;
}
player.LocationName = newRoom;
await _gameInstance.NotifyPlayerOfMeetingEnd(player.DiscordId);
await _gameInstance.NotifyPlayerOfMeetingEnd(player.DiscordId, isTornApart);
}
_gameInstance.GenerateAndNotify();
}
......
using MomentaryMeeting.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace MomentaryMeeting.Game.Interactions
{
public class Meeting
{
private const int _meetingLengthSeconds = 30;
public long _timestamp;
public List<UserModel> _users;
private IEndMeetings _endPoint;
private Task _endMeetingTask;
public Meeting(IEndMeetings endPoint)
{
_users = new List<UserModel>();
_timestamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
_endPoint = endPoint;
_endMeetingTask = Task.Run(async () =>
{
await Task.Delay(1000 * _meetingLengthSeconds);
await _endPoint.EndMeeting(this);
});
}
public bool IsOverYet()
{
return new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds() - _timestamp > _meetingLengthSeconds;
}
}
}
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace MomentaryMeeting.Game.Interactions
{
public class Meeting
{
// Meetings can never be any longer than this; players will be torn
// apart after this
private const int _maxMeetingLengthSeconds = 90;
// If players spend more than this amount of time not in the same
// location, their meeting will end
private const int _driftApartTimeout = 30;
public long _startTimestamp;
public long _lastSeenTogetherTimestamp;
public List<UserModel> _users;
private IEndMeetings _endPoint;
private Task _endMeetingTask;
public static long Now()
{
return new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
}
public Meeting(IEndMeetings endPoint)
{
_users = new List<UserModel>();
_startTimestamp = Now();
_lastSeenTogetherTimestamp = _startTimestamp;
_endPoint = endPoint;
_endMeetingTask = Task.Run(async () =>
{
bool movePlayers = true;
bool allTogether = true;
while ((Now() - _startTimestamp) < _maxMeetingLengthSeconds)
{
// Check whether players are still together
allTogether = true;
if (_users.Count > 0)
{
foreach (var player in _users)
{
if (player.LocationName != _users[0].LocationName)
{
allTogether = false;
break;
}
}
}
// As long as players are still together, keep updating _lastSeenTogetherTimestamp.
// If they aren't together, _lastSeenTogetherTimestamp will stop being updated and
// if they aren't together for too long the check below will fail.
if (allTogether)
{
_lastSeenTogetherTimestamp = Now();
}
if ((Now() - _lastSeenTogetherTimestamp) >= _driftApartTimeout)
{
movePlayers = false;
break;
}
await Task.Delay(500);
}
// players spend too long together => tear them apart
// players go separate ways => don't tear them apart
await _endPoint.EndMeeting(this, movePlayers);
});
}
public bool IsOverYet()
{
return (Now() - _startTimestamp) > _maxMeetingLengthSeconds;
}
}
}
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