upload broken code so i can work on it later
This commit is contained in:
parent
12668dff43
commit
2d6e4dffa8
@ -1,23 +1,23 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using riff;
|
using riff;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
|
||||||
public class MysteriousCube : IRCBot
|
public class MysteriousCube : IRCBot
|
||||||
{
|
{
|
||||||
private HttpClient RequestClient = new();
|
// private HttpClient RequestClient = new();
|
||||||
private const string AllTriviaQuestionsPath = "data/TriviaQuestions.json";
|
|
||||||
private const string UnaskedTriviaQuestionsPath = "data/UnaskedTriviaQuestions.json";
|
|
||||||
private TriviaQuestion[] AllTriviaQuestions;
|
private TriviaQuestion[] AllTriviaQuestions;
|
||||||
private List<TriviaQuestion> UnaskedTriviaQuestions;
|
private const string AllTriviaQuestionsPath = "data/TriviaQuestions.json";
|
||||||
private bool ListeningForAnswer = false;
|
private Dictionary<string, TriviaQuestion?> CurrentQuestions = [];
|
||||||
private TriviaQuestion? CurrentQuestion = null;
|
private readonly Random Rand = new();
|
||||||
private Random Rand = new();
|
|
||||||
|
|
||||||
public MysteriousCube(string host, int port, string nick, string realname, List<string> permanentChannels)
|
public MysteriousCube(string host, int port, string nick, string realname, List<string> permanentChannels)
|
||||||
: base(host, port, nick, realname, permanentChannels)
|
: base(host, port, nick, realname, permanentChannels)
|
||||||
{
|
{
|
||||||
AllTriviaQuestions = LoadAllQuestions();
|
AllTriviaQuestions = LoadAllQuestions();
|
||||||
UnaskedTriviaQuestions = LoadUnaskedQuestions();
|
|
||||||
|
|
||||||
(string, Action<PRIVMSG>)[] commands = [
|
(string, Action<PRIVMSG>)[] commands = [
|
||||||
("echo", Echo),
|
("echo", Echo),
|
||||||
@ -40,41 +40,65 @@ public class MysteriousCube : IRCBot
|
|||||||
private void PostQuestion(PRIVMSG privmsg)
|
private void PostQuestion(PRIVMSG privmsg)
|
||||||
{
|
{
|
||||||
var question = GetQuestion();
|
var question = GetQuestion();
|
||||||
CurrentQuestion = question;
|
CurrentQuestions[privmsg.Sender] = question;
|
||||||
ListeningForAnswer = true;
|
|
||||||
SendPrivmsg(privmsg.Sender, $"Answer 'true' or 'false': {question.Question}");
|
SendPrivmsg(privmsg.Sender, $"Answer 'true' or 'false': {question.Question}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AnswerListener(PRIVMSG privmsg)
|
private void AnswerListener(PRIVMSG privmsg)
|
||||||
{
|
{
|
||||||
if (!ListeningForAnswer || CurrentQuestion == null)
|
TriviaQuestion? currentQuestion = CurrentQuestions.GetValueOrDefault(privmsg.Sender);
|
||||||
|
if (currentQuestion == null)
|
||||||
return;
|
return;
|
||||||
string line = privmsg.Body.ToLower().Trim();
|
string line = privmsg.Body.ToLower().Trim();
|
||||||
bool? answer = null;
|
bool? answer;
|
||||||
if (line.StartsWith("true"))
|
if (line.StartsWith("true"))
|
||||||
answer = true;
|
answer = true;
|
||||||
else if (line.StartsWith("false"))
|
else if (line.StartsWith("false"))
|
||||||
answer = false;
|
answer = false;
|
||||||
else
|
else
|
||||||
return;
|
return;
|
||||||
ListeningForAnswer = false;
|
|
||||||
string response;
|
string response;
|
||||||
if (answer == CurrentQuestion.Answer)
|
using (var db = new MysteriousCubeDatabaseContext())
|
||||||
response = $"{privmsg.Nick} is correct!";
|
{
|
||||||
else
|
bool correct = answer == currentQuestion.Answer;
|
||||||
response = $"{privmsg.Nick} is wrong!";
|
var user = db.Users.Find(privmsg.Nick);
|
||||||
CurrentQuestion = null;
|
if (user == null)
|
||||||
|
{
|
||||||
|
user = new User { Nick = privmsg.Nick, TriviaScore = 0 };
|
||||||
|
db.Users.Add(user);
|
||||||
|
db.SaveChanges();
|
||||||
|
user = db.Users.Find(privmsg.Nick)!;
|
||||||
|
}
|
||||||
|
if (correct)
|
||||||
|
user.TriviaScore += 1;
|
||||||
|
else
|
||||||
|
user.TriviaScore =- 1;
|
||||||
|
db.Users.Update(user);
|
||||||
|
db.SaveChanges();
|
||||||
|
string phrasing = correct ? "correct" : "wrong";
|
||||||
|
response = $"{privmsg.Nick} is {phrasing}! {privmsg.Nick} has a score of {user.TriviaScore}.";
|
||||||
|
CurrentQuestions[privmsg.Sender] = null;
|
||||||
|
}
|
||||||
SendPrivmsg(privmsg.Sender, response);
|
SendPrivmsg(privmsg.Sender, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
private TriviaQuestion GetQuestion()
|
private TriviaQuestion GetQuestion()
|
||||||
{
|
{
|
||||||
if (UnaskedTriviaQuestions.Count == 0)
|
using (var db = new MysteriousCubeDatabaseContext())
|
||||||
UnaskedTriviaQuestions = AllTriviaQuestions.ToList();
|
{
|
||||||
TriviaQuestion question = UnaskedTriviaQuestions[Rand.Next(UnaskedTriviaQuestions.Count)];
|
var questions = db.UnaskedTriviaQuestions.ToList();
|
||||||
UnaskedTriviaQuestions.Remove(question);
|
Console.WriteLine($"{questions.Count}");
|
||||||
File.WriteAllText(UnaskedTriviaQuestionsPath, JsonSerializer.Serialize(UnaskedTriviaQuestions));
|
if (db.UnaskedTriviaQuestions.Count() == 0)
|
||||||
return question;
|
{
|
||||||
|
foreach (var q in AllTriviaQuestions)
|
||||||
|
db.UnaskedTriviaQuestions.Add(q);
|
||||||
|
questions = db.UnaskedTriviaQuestions.ToList();
|
||||||
|
}
|
||||||
|
TriviaQuestion question = questions[Rand.Next(questions.Count)];
|
||||||
|
db.UnaskedTriviaQuestions.Remove(question);
|
||||||
|
db.SaveChanges();
|
||||||
|
return question;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private TriviaQuestion[] LoadAllQuestions()
|
private TriviaQuestion[] LoadAllQuestions()
|
||||||
@ -83,7 +107,7 @@ public class MysteriousCube : IRCBot
|
|||||||
{
|
{
|
||||||
using (FileStream stream = File.OpenRead(AllTriviaQuestionsPath))
|
using (FileStream stream = File.OpenRead(AllTriviaQuestionsPath))
|
||||||
{
|
{
|
||||||
TriviaQuestion[] questions = JsonSerializer.Deserialize<TriviaQuestion[]>(stream)!;
|
var questions = JsonSerializer.Deserialize<TriviaQuestion[]>(stream)!;
|
||||||
if (questions == null)
|
if (questions == null)
|
||||||
throw new NoNullAllowedException();
|
throw new NoNullAllowedException();
|
||||||
return questions;
|
return questions;
|
||||||
@ -97,27 +121,34 @@ public class MysteriousCube : IRCBot
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<TriviaQuestion> LoadUnaskedQuestions()
|
public class MysteriousCubeDatabaseContext : DbContext
|
||||||
{
|
{
|
||||||
try
|
public DbSet<TriviaQuestion> UnaskedTriviaQuestions { get; set; }
|
||||||
{
|
public DbSet<User> Users { get; set; }
|
||||||
using (FileStream stream = File.OpenRead(UnaskedTriviaQuestionsPath))
|
public string DbPath = "data/mysterious_cube.sqlite";
|
||||||
{
|
|
||||||
List<TriviaQuestion> questions = JsonSerializer.Deserialize<List<TriviaQuestion>>(stream)!;
|
|
||||||
if (questions == null)
|
|
||||||
throw new NoNullAllowedException();
|
|
||||||
return questions;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (FileNotFoundException)
|
|
||||||
{
|
|
||||||
return AllTriviaQuestions.ToList();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class TriviaQuestion
|
// public UnaskedTriviaQuestionsContext()
|
||||||
{
|
// {
|
||||||
public required string Question { get; set; }
|
// //
|
||||||
public required bool Answer { get; set; }
|
// }
|
||||||
|
|
||||||
|
protected override void OnConfiguring(DbContextOptionsBuilder options)
|
||||||
|
=> options.UseSqlite($"Data Source=data/mysterious_cube.sqlite");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Table("UnaskedTriviaQuestions")]
|
||||||
|
public class TriviaQuestion
|
||||||
|
{
|
||||||
|
[Key]
|
||||||
|
public required string Question { get; set; }
|
||||||
|
public required bool Answer { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Table("Users")]
|
||||||
|
public class User
|
||||||
|
{
|
||||||
|
[Key]
|
||||||
|
public required string Nick { get; set; }
|
||||||
|
public required int TriviaScore { get; set; }
|
||||||
|
}
|
||||||
}
|
}
|
@ -8,6 +8,11 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.6">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.6" />
|
||||||
<PackageReference Include="System.Net.Http.Json" Version="9.0.6" />
|
<PackageReference Include="System.Net.Http.Json" Version="9.0.6" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user