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.Text.Json;
|
||||
using riff;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
public class MysteriousCube : IRCBot
|
||||
{
|
||||
private HttpClient RequestClient = new();
|
||||
private const string AllTriviaQuestionsPath = "data/TriviaQuestions.json";
|
||||
private const string UnaskedTriviaQuestionsPath = "data/UnaskedTriviaQuestions.json";
|
||||
// private HttpClient RequestClient = new();
|
||||
private TriviaQuestion[] AllTriviaQuestions;
|
||||
private List<TriviaQuestion> UnaskedTriviaQuestions;
|
||||
private bool ListeningForAnswer = false;
|
||||
private TriviaQuestion? CurrentQuestion = null;
|
||||
private Random Rand = new();
|
||||
private const string AllTriviaQuestionsPath = "data/TriviaQuestions.json";
|
||||
private Dictionary<string, TriviaQuestion?> CurrentQuestions = [];
|
||||
private readonly 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)
|
||||
{
|
||||
AllTriviaQuestions = LoadAllQuestions();
|
||||
UnaskedTriviaQuestions = LoadUnaskedQuestions();
|
||||
|
||||
(string, Action<PRIVMSG>)[] commands = [
|
||||
("echo", Echo),
|
||||
@ -40,41 +40,65 @@ public class MysteriousCube : IRCBot
|
||||
private void PostQuestion(PRIVMSG privmsg)
|
||||
{
|
||||
var question = GetQuestion();
|
||||
CurrentQuestion = question;
|
||||
ListeningForAnswer = true;
|
||||
CurrentQuestions[privmsg.Sender] = question;
|
||||
SendPrivmsg(privmsg.Sender, $"Answer 'true' or 'false': {question.Question}");
|
||||
}
|
||||
|
||||
private void AnswerListener(PRIVMSG privmsg)
|
||||
{
|
||||
if (!ListeningForAnswer || CurrentQuestion == null)
|
||||
TriviaQuestion? currentQuestion = CurrentQuestions.GetValueOrDefault(privmsg.Sender);
|
||||
if (currentQuestion == null)
|
||||
return;
|
||||
string line = privmsg.Body.ToLower().Trim();
|
||||
bool? answer = null;
|
||||
bool? answer;
|
||||
if (line.StartsWith("true"))
|
||||
answer = true;
|
||||
else if (line.StartsWith("false"))
|
||||
answer = false;
|
||||
else
|
||||
return;
|
||||
ListeningForAnswer = false;
|
||||
string response;
|
||||
if (answer == CurrentQuestion.Answer)
|
||||
response = $"{privmsg.Nick} is correct!";
|
||||
else
|
||||
response = $"{privmsg.Nick} is wrong!";
|
||||
CurrentQuestion = null;
|
||||
using (var db = new MysteriousCubeDatabaseContext())
|
||||
{
|
||||
bool correct = answer == currentQuestion.Answer;
|
||||
var user = db.Users.Find(privmsg.Nick);
|
||||
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);
|
||||
}
|
||||
|
||||
private TriviaQuestion GetQuestion()
|
||||
{
|
||||
if (UnaskedTriviaQuestions.Count == 0)
|
||||
UnaskedTriviaQuestions = AllTriviaQuestions.ToList();
|
||||
TriviaQuestion question = UnaskedTriviaQuestions[Rand.Next(UnaskedTriviaQuestions.Count)];
|
||||
UnaskedTriviaQuestions.Remove(question);
|
||||
File.WriteAllText(UnaskedTriviaQuestionsPath, JsonSerializer.Serialize(UnaskedTriviaQuestions));
|
||||
return question;
|
||||
using (var db = new MysteriousCubeDatabaseContext())
|
||||
{
|
||||
var questions = db.UnaskedTriviaQuestions.ToList();
|
||||
Console.WriteLine($"{questions.Count}");
|
||||
if (db.UnaskedTriviaQuestions.Count() == 0)
|
||||
{
|
||||
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()
|
||||
@ -83,7 +107,7 @@ public class MysteriousCube : IRCBot
|
||||
{
|
||||
using (FileStream stream = File.OpenRead(AllTriviaQuestionsPath))
|
||||
{
|
||||
TriviaQuestion[] questions = JsonSerializer.Deserialize<TriviaQuestion[]>(stream)!;
|
||||
var questions = JsonSerializer.Deserialize<TriviaQuestion[]>(stream)!;
|
||||
if (questions == null)
|
||||
throw new NoNullAllowedException();
|
||||
return questions;
|
||||
@ -97,27 +121,34 @@ public class MysteriousCube : IRCBot
|
||||
}
|
||||
}
|
||||
|
||||
private List<TriviaQuestion> LoadUnaskedQuestions()
|
||||
public class MysteriousCubeDatabaseContext : DbContext
|
||||
{
|
||||
try
|
||||
{
|
||||
using (FileStream stream = File.OpenRead(UnaskedTriviaQuestionsPath))
|
||||
{
|
||||
List<TriviaQuestion> questions = JsonSerializer.Deserialize<List<TriviaQuestion>>(stream)!;
|
||||
if (questions == null)
|
||||
throw new NoNullAllowedException();
|
||||
return questions;
|
||||
}
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
return AllTriviaQuestions.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
public DbSet<TriviaQuestion> UnaskedTriviaQuestions { get; set; }
|
||||
public DbSet<User> Users { get; set; }
|
||||
public string DbPath = "data/mysterious_cube.sqlite";
|
||||
|
||||
public class TriviaQuestion
|
||||
{
|
||||
public required string Question { get; set; }
|
||||
public required bool Answer { get; set; }
|
||||
// public UnaskedTriviaQuestionsContext()
|
||||
// {
|
||||
// //
|
||||
// }
|
||||
|
||||
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>
|
||||
|
||||
<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" />
|
||||
</ItemGroup>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user