Skip to content

Commit

Permalink
initial work
Browse files Browse the repository at this point in the history
  • Loading branch information
th0mk committed Apr 2, 2024
1 parent 5bf303a commit 7967203
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 9 deletions.
61 changes: 60 additions & 1 deletion src/FMBot.Bot/Builders/GameBuilders.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,65 @@
using Discord;
using FMBot.Bot.Models;
using FMBot.Bot.Resources;
using System.Text;
using FMBot.Domain.Models;
using System.Threading.Tasks;
using FMBot.Bot.Services;

namespace FMBot.Bot.Builders;

public class GameBuilders
{

private readonly UserService _userService;
private readonly GameService _gameService;
private readonly ArtistsService _artistsService;

public GameBuilders(UserService userService, GameService gameService, ArtistsService artistsService)
{
this._userService = userService;
this._gameService = gameService;
this._artistsService = artistsService;
}

public static ResponseModel GameModePick(ContextModel context)
{
var response = new ResponseModel
{
ResponseType = ResponseType.Embed,
Components = new ComponentBuilder()
.WithButton("First correct answer wins", InteractionConstants.Game.StartJumbleFirstWins)
.WithButton("Play as group", InteractionConstants.Game.StartJumbleGroup)
};

var description = new StringBuilder();

description.AppendLine("Start jumble game");

response.Embed.WithDescription(description.ToString());
response.Embed.WithColor(DiscordConstants.InformationColorBlue);

return response;
}

public async Task<ResponseModel> StartJumbleFirstWins(ContextModel context, int userId)
{
var response = new ResponseModel
{
ResponseType = ResponseType.Embed,
};

var topArtists = await this._artistsService.GetUserAllTimeTopArtists(userId, true);
var artist = await this._gameService.PickArtistForJumble(topArtists);
var game = await this._gameService.StartJumbleGame(userId, context.DiscordGuild?.Id, GameType.JumbleFirstWins, artist);

response.Embed.WithAuthor("Guess the artist - Jumble");

var description = new StringBuilder();
response.Embed.WithDescription($"`{game.jumbled}`");
response.Embed.AddField("Hint", "aasddasd");
response.Embed.AddField("Add an answer", "Type your answer in this channel to make a guess");
response.Embed.WithColor(DiscordConstants.InformationColorBlue);

return response;
}
}
45 changes: 45 additions & 0 deletions src/FMBot.Bot/Models/GameModels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;

namespace FMBot.Bot.Models;

public class GameModel
{
public int GameId { get; set; }
public int StarterUserId { get; set; }
public ulong? DiscordGuildId { get; set; }

public int HintCount { get; set; }

public GameType GameType { get; set; }

public DateTime DateStarted { get; set; }
public DateTime? DateEnded { get; set; }

public List<GameAnswerModel> Answers { get; set; }

public string CorrectAnswer { get; set; }
}

public class GameAnswerModel
{
public int GameAnswerId { get; set; }
public int GameId { get; set; }
public int UserId { get; set; }

public bool Correct { get; set; }

public string Answer { get; set; }
}

public enum GameType
{
JumbleFirstWins = 1,
JumbleGroup = 2
}

public enum JumbleHintType
{
Reshuffle = 1,
Description = 2
}
6 changes: 6 additions & 0 deletions src/FMBot.Bot/Resources/InteractionConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,10 @@ public static class ToggleCommand

public const string GuildMembers = "guild-members";
public const string FeaturedLog = "featured-log";

public static class Game
{
public const string StartJumbleFirstWins = "jumble-firstwins";
public const string StartJumbleGroup = "jumble-group";
}
}
68 changes: 68 additions & 0 deletions src/FMBot.Bot/Services/GameService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
using FMBot.Bot.Models;
using FMBot.Domain.Models;

namespace FMBot.Bot.Services;

public class GameService
{
public async Task<string> PickArtistForJumble(List<TopArtist> topArtists)
{
return "The Beatles";
}

public async Task<(GameModel game, string jumbled)> StartJumbleGame(int userId, ulong? discordGuildId, GameType gameType, string artist)
{
var game = new GameModel
{
DateStarted = DateTime.UtcNow,
StarterUserId = userId,
DiscordGuildId = discordGuildId,
GameType = gameType,
CorrectAnswer = artist,
HintCount = 0
};

var jumbled = JumbleWords(artist);



return (game, jumbled);
}

//public async Task<string> GetJumbleHint(int userId, string artist)
//{

// var jumbled = JumbleWords(artist);



// return (game, jumbled);
//}

public static string JumbleWords(string input)
{
var words = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

var jumbledWords = words.Select(JumbleWord);

return string.Join(" ", jumbledWords);
}

private static string JumbleWord(string word)
{
var letters = word.ToCharArray();

for (var i = letters.Length - 1; i > 0; i--)
{
var swapIndex = RandomNumberGenerator.GetInt32(0, i + 1);
(letters[i], letters[swapIndex]) = (letters[swapIndex], letters[i]);
}

return new string(letters);
}
}
32 changes: 32 additions & 0 deletions src/FMBot.Bot/SlashCommands/GameSlashCommands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Threading.Tasks;
using Discord.Interactions;
using FMBot.Bot.Attributes;
using FMBot.Bot.Builders;
using FMBot.Bot.Extensions;
using FMBot.Bot.Models;
using FMBot.Bot.Resources;
using FMBot.Bot.Services;

namespace FMBot.Bot.SlashCommands;

public class GameSlashCommands : InteractionModuleBase
{
private readonly GameBuilders _gameBuilders;
private readonly UserService _userService;

public GameSlashCommands(GameBuilders gameBuilders, UserService userService)
{
this._gameBuilders = gameBuilders;
this._userService = userService;
}

[ComponentInteraction(InteractionConstants.Game.StartJumbleFirstWins)]
public async Task StartJumbleFirstAnswerWins()
{
var contextUser = await this._userService.GetUserSettingsAsync(this.Context.User);

var response = await this._gameBuilders.StartJumbleFirstWins(new ContextModel(this.Context), contextUser.UserId);

await this.Context.UpdateInteractionEmbed(response);
}
}
1 change: 1 addition & 0 deletions src/FMBot.Bot/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ private void ConfigureServices(IServiceCollection services)
.AddSingleton<GenreBuilders>()
.AddSingleton<GenreService>()
.AddSingleton<GameBuilders>()
.AddSingleton<GameService>()
.AddSingleton<GuildBuilders>()
.AddSingleton<GuildService>()
.AddSingleton<GuildSettingBuilder>()
Expand Down
12 changes: 6 additions & 6 deletions src/FMBot.Bot/TextCommands/GameCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public GameCommands(IOptions<BotSettings> botSettings, UserService userService,
this.Interactivity = interactivity;
}

//[Command("jumble", RunMode = RunMode.Async)]
//[Summary("Play the Jumble game.")]
[Command("jumble", RunMode = RunMode.Async)]
[Summary("Play the Jumble game.")]
[UsernameSetRequired]
//[CommandCategories(CommandCategory.Friends)]
[CommandCategories(CommandCategory.Friends)]
public async Task JumbleAsync()
{
_ = this.Context.Channel.TriggerTypingAsync();
Expand All @@ -43,10 +43,10 @@ public async Task JumbleAsync()

try
{
//var response = await this._gameBuilders.FriendsAsync(new ContextModel(this.Context, prfx, contextUser));
var response = GameBuilders.GameModePick(new ContextModel(this.Context, prfx, contextUser));

//await this.Context.SendResponse(this.Interactivity, response);
//this.Context.LogCommandUsed(response.CommandResponse);
await this.Context.SendResponse(this.Interactivity, response);
this.Context.LogCommandUsed(response.CommandResponse);
}
catch (Exception e)
{
Expand Down
2 changes: 1 addition & 1 deletion src/FMBot.Discord.slnLaunch
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Projects": [
{
"Name": "..\\..\\fmbot-web-backend\\src\\Web.InternalApi\\Web.InternalApi.csproj",
"Action": "Start",
"Action": "StartWithoutDebugging",
"DebugTarget": "http"
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/FMBot.Domain/Models/TopTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class TopTrack

public string TrackName { get; set; }
public string TrackUrl { get; set; }

public string ArtistName { get; set; }
public string ArtistUrl { get; set; }

Expand Down

0 comments on commit 7967203

Please sign in to comment.