Skip to content

Commit

Permalink
Prepare code for games
Browse files Browse the repository at this point in the history
  • Loading branch information
Xwilarg committed Aug 31, 2024
1 parent e1651c0 commit 11f1275
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Sanara/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Google.Cloud.Translate.V3;
using Google.Cloud.Vision.V1;
using Sanara.Command.SlashCommand.Converter;
using Sanara.Database;

namespace Sanara;

Expand All @@ -33,6 +34,7 @@ public static IServiceProvider CreateProvider(DiscordSocketClient client, Creden
.AddSingleton<HtmlWeb>() // HTML Parser
.AddSingleton<Vndb>() // VNDB Client
.AddSingleton<Random>()
.AddDbContext<SqliteContext>()
.AddSingleton(new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
Expand Down
2 changes: 1 addition & 1 deletion Sanara/Command/SlashCommand/Impl/Entertainment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Entertainment : ISubmodule
private const string InspireRandomUrl = "https://inspirobot.me/api?generate=true";

public string Name => "Entertainment";
public string Description => "";
public string Description => "Fun others utility commands";

CommandData[] ISubmodule.GetCommands()
{
Expand Down
3 changes: 2 additions & 1 deletion Sanara/Command/SlashCommand/Impl/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
using System.Text.Json;
using Sanara.Command.SlashCommand.Converter;
using System.Text;
using HtmlAgilityPack;

namespace Sanara.Command.SlashCommand.Impl;

public class Language : ISubmodule
{
public string Name => "Language";

public string Description => "";
public string Description => "Learning about Japanese language";

public CommandData[] GetCommands()
{
Expand Down
1 change: 1 addition & 0 deletions Sanara/Command/SlashCommand/Impl/Nsfw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public async Task AdultVideoAsync(IContext ctx)
html = web.Load(finalUrl);
var info = html.DocumentNode.SelectSingleNode("//div[contains(@x-show, \"currentTab === 'video_details'\")]");

// Get fields
var name = html.DocumentNode.SelectSingleNode("//h1[contains(@class, 'lg:text-lg')]").InnerHtml;
var description = HttpUtility.HtmlDecode(info.ChildNodes[1].ChildNodes[1].ChildNodes[1].InnerHtml);
var tags = info.ChildNodes[1].ChildNodes[5].ChildNodes[7].SelectNodes("a").Select(x => x.InnerHtml);
Expand Down
18 changes: 18 additions & 0 deletions Sanara/Database/SqliteContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.EntityFrameworkCore;

Check failure on line 1 in Sanara/Database/SqliteContext.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'EntityFrameworkCore' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

Check failure on line 1 in Sanara/Database/SqliteContext.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'EntityFrameworkCore' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
using System.ComponentModel.DataAnnotations;

namespace Sanara.Database;

public class SqliteContext : DbContext

Check failure on line 6 in Sanara/Database/SqliteContext.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'DbContext' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 6 in Sanara/Database/SqliteContext.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'DbContext' could not be found (are you missing a using directive or an assembly reference?)
{
protected override void OnConfiguring(DbContextOptionsBuilder options)

Check failure on line 8 in Sanara/Database/SqliteContext.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'DbContextOptionsBuilder' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 8 in Sanara/Database/SqliteContext.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'DbContextOptionsBuilder' could not be found (are you missing a using directive or an assembly reference?)
=> options.UseSqlite("Data Source=Sqlite.db");
}

public class ServerContext
{
[Key] public ulong Id { set; get; } = 0;

public bool TranslateUsingFlags { set; get; } = false;
public Dictionary<int, ulong> Subscriptions { set; get; } = new();
}
5 changes: 5 additions & 0 deletions Sanara/Game/AGame.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Sanara.Game;

public abstract class AGame
{
}
11 changes: 11 additions & 0 deletions Sanara/Game/GameSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Sanara.Game;

public struct GameSettings
{
public GameSettings(Lobby lobby)
{
Lobby = lobby;
}

public Lobby Lobby { get; }
}
69 changes: 69 additions & 0 deletions Sanara/Game/Impl/ShiritoriPreload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Discord;
using Microsoft.Extensions.DependencyInjection;
using Sanara.Command.SlashCommand.Converter;
using Sanara.Command.SlashCommand.Impl;
using Sanara.Game.Preload;
using Sanara.Game.Result;
using System.Collections.ObjectModel;

namespace Sanara.Game.Impl;

public class ShiritoriPreload : IPreload
{
public string Name => "Shiritori";

public bool IsSafe => true;

public AGame CreateGame(IMessageChannel msgchan, IUser user, GameSettings settings)
{
throw new NotImplementedException();
}

public string Rules
=> "Shiritori is a Japanese game where you must find a word starting by the last syllable of the previous one.\n" +
"Words must be noun, must not end by a ん(n), must not have been already said and must be more than one syllabe.\n" +
"For example if someone say りゅう (ryuu, dragon) you have to say a word starting by う (u), like うさぎ (usagi, rabbit).\n" +
"First player must start by saying しりとり (shiritori)";

public void Init(IServiceProvider provider)
{
var http = provider.GetRequiredService<HttpClient>();
var converter = provider.GetRequiredService<JapaneseConverter>();
if (!File.Exists("Saves/Game/ShiritoriJapanese.txt"))
File.WriteAllBytes("Saves/Game/ShiritoriJapanese.txt", http.GetByteArrayAsync("https://files.zirk.eu/Sanara/ShiritoriJapanese.txt").GetAwaiter().GetResult());
string[] lines = File.ReadAllLines("Saves/Game/ShiritoriJapanese.txt");
_words = [];
foreach (var l in lines)
{
string[] curr = l.Split('$');
string word = curr[0];
_words.Add(new ShiritoriPreloadResult(word, converter.ToRomaji(word), curr[1]));
}
for (int i = 5; i >= 1; i--)
{
if (!File.Exists($"Saves/Game/Jlpt{i}Vocabulary.txt"))
File.WriteAllBytes($"Saves/Game/Jlpt{i}Vocabulary.txt", http.GetByteArrayAsync("https://files.zirk.eu/Sanara/Jlpt" + i + "Vocabulary.txt").GetAwaiter().GetResult());
string[] jlptLines = File.ReadAllLines($"Saves/Game/Jlpt{i}Vocabulary.txt");
foreach (var l in jlptLines)
{
string[] curr = l.Split('$');
string word = curr[0];
var value = _words.Find(x => x.Word == word);
if (value == null)
{
value = new ShiritoriPreloadResult(word, converter.ToRomaji(word), curr[1]);
_words.Add(value);
}
value.LearningLevels.Add(i);
}
}
_words = _words.Where(x => !x.Word.EndsWith("") && x.Word.Length > 1).ToList();
}

public IEnumerable<IPreloadResult> Load()
{
return _words;
}

private List<ShiritoriPreloadResult> _words;
}
5 changes: 5 additions & 0 deletions Sanara/Game/Lobby.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Sanara.Game;

public class Lobby
{
}
26 changes: 26 additions & 0 deletions Sanara/Game/Preload/IPreload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Discord;
using System.Collections.ObjectModel;

namespace Sanara.Game.Preload;

public interface IPreload
{
public void Init(IServiceProvider provider);
/// <summary>
/// Load the game dictionary
/// </summary>
public IEnumerable<IPreloadResult> Load();
public string Name { get; }
/// <summary>
/// Create a new instance of a game and return it
/// </summary>
public AGame CreateGame(IMessageChannel msgchan, IUser user, GameSettings settings);
/// <summary>
/// Returns the game rules
/// </summary>
public string Rules { get; }
/// <summary>
/// Can the game be played in SFW channels
/// </summary>
public bool IsSafe { get; }
}
4 changes: 4 additions & 0 deletions Sanara/Game/Preload/IPreloadResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Sanara.Game.Preload;

public interface IPreloadResult
{ }
31 changes: 31 additions & 0 deletions Sanara/Game/Result/ShiritoriPreloadResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Sanara.Game.Preload;

namespace Sanara.Game.Result;

public class ShiritoriPreloadResult : IPreloadResult
{
public ShiritoriPreloadResult(string word, string wordEnglish, string meanings)
{
Word = word;
WordEnglish = wordEnglish;
Meanings = meanings;
LearningLevels = new();
}

/// <summary>
/// Word in the local language
/// </summary>
public string Word { get; }
/// <summary>
/// Word in latin alphabet (ex: romaji for japanese)
/// </summary>
public string WordEnglish { get; }
/// <summary>
/// What the word mean
/// </summary>
public string Meanings { get; }
/// <summary>
/// JLPT levels
/// </summary>
public List<int> LearningLevels { get; }
}

0 comments on commit 11f1275

Please sign in to comment.