mirror of
https://github.com/barelyprofessional/KfChatDotNet.git
synced 2026-05-02 04:22:04 -04:00
Added Twitch view count logging
This commit is contained in:
@@ -15,4 +15,5 @@ public class ApplicationDbContext : DbContext
|
|||||||
public DbSet<SettingDbModel> Settings { get; set; }
|
public DbSet<SettingDbModel> Settings { get; set; }
|
||||||
public DbSet<HowlggBetsDbModel> HowlggBets { get; set; }
|
public DbSet<HowlggBetsDbModel> HowlggBets { get; set; }
|
||||||
public DbSet<RainbetBetsDbModel> RainbetBets { get; set; }
|
public DbSet<RainbetBetsDbModel> RainbetBets { get; set; }
|
||||||
|
public DbSet<TwitchViewCountDbModel> TwitchViewCounts { get; set; }
|
||||||
}
|
}
|
||||||
211
KfChatDotNetBot/Migrations/20240815041430_TwitchViews.Designer.cs
generated
Normal file
211
KfChatDotNetBot/Migrations/20240815041430_TwitchViews.Designer.cs
generated
Normal file
@@ -0,0 +1,211 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using KfChatDotNetBot;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace KfChatDotNetBot.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(ApplicationDbContext))]
|
||||||
|
[Migration("20240815041430_TwitchViews")]
|
||||||
|
partial class TwitchViews
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder.HasAnnotation("ProductVersion", "8.0.7");
|
||||||
|
|
||||||
|
modelBuilder.Entity("KfChatDotNetBot.Models.DbModels.HowlggBetsDbModel", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<long>("Bet")
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<int>("BetId")
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("Date")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("Game")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<long>("GameId")
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<long>("Profit")
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<int>("UserId")
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("HowlggBets");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("KfChatDotNetBot.Models.DbModels.JuicerDbModel", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<float>("Amount")
|
||||||
|
.HasColumnType("REAL");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("JuicedAt")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<int>("UserId")
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("UserId");
|
||||||
|
|
||||||
|
b.ToTable("Juicers");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("KfChatDotNetBot.Models.DbModels.RainbetBetsDbModel", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<long>("BetId")
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("BetSeenAt")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("GameName")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<float>("Multiplier")
|
||||||
|
.HasColumnType("REAL");
|
||||||
|
|
||||||
|
b.Property<float>("Payout")
|
||||||
|
.HasColumnType("REAL");
|
||||||
|
|
||||||
|
b.Property<string>("PublicId")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<int>("RainbetUserId")
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("UpdatedAt")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<float>("Value")
|
||||||
|
.HasColumnType("REAL");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("RainbetBets");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("KfChatDotNetBot.Models.DbModels.SettingDbModel", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<string>("Default")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("Description")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<bool>("IsSecret")
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<string>("Key")
|
||||||
|
.IsRequired()
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("Regex")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("Value")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Settings");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("KfChatDotNetBot.Models.DbModels.TwitchViewCountDbModel", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<double>("ServerTime")
|
||||||
|
.HasColumnType("REAL");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("Time")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("Topic")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<int>("Viewers")
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("TwitchViewCounts");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("KfChatDotNetBot.Models.DbModels.UserDbModel", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<bool>("Ignored")
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<int>("KfId")
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<string>("KfUsername")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<int>("UserRight")
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Users");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("KfChatDotNetBot.Models.DbModels.JuicerDbModel", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("KfChatDotNetBot.Models.DbModels.UserDbModel", "User")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("UserId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("User");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
38
KfChatDotNetBot/Migrations/20240815041430_TwitchViews.cs
Normal file
38
KfChatDotNetBot/Migrations/20240815041430_TwitchViews.cs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace KfChatDotNetBot.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class TwitchViews : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "TwitchViewCounts",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<int>(type: "INTEGER", nullable: false)
|
||||||
|
.Annotation("Sqlite:Autoincrement", true),
|
||||||
|
Topic = table.Column<string>(type: "TEXT", nullable: false),
|
||||||
|
ServerTime = table.Column<double>(type: "REAL", nullable: false),
|
||||||
|
Viewers = table.Column<int>(type: "INTEGER", nullable: false),
|
||||||
|
Time = table.Column<DateTimeOffset>(type: "TEXT", nullable: false)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_TwitchViewCounts", x => x.Id);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "TwitchViewCounts");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -144,6 +144,30 @@ namespace KfChatDotNetBot.Migrations
|
|||||||
b.ToTable("Settings");
|
b.ToTable("Settings");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("KfChatDotNetBot.Models.DbModels.TwitchViewCountDbModel", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<double>("ServerTime")
|
||||||
|
.HasColumnType("REAL");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("Time")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("Topic")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<int>("Viewers")
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("TwitchViewCounts");
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("KfChatDotNetBot.Models.DbModels.UserDbModel", b =>
|
modelBuilder.Entity("KfChatDotNetBot.Models.DbModels.UserDbModel", b =>
|
||||||
{
|
{
|
||||||
b.Property<int>("Id")
|
b.Property<int>("Id")
|
||||||
|
|||||||
10
KfChatDotNetBot/Models/DbModels/TwitchViewCountDbModel.cs
Normal file
10
KfChatDotNetBot/Models/DbModels/TwitchViewCountDbModel.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
namespace KfChatDotNetBot.Models.DbModels;
|
||||||
|
|
||||||
|
public class TwitchViewCountDbModel
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public required string Topic { get; set; }
|
||||||
|
public required double ServerTime { get; set; }
|
||||||
|
public required int Viewers { get; set; }
|
||||||
|
public required DateTimeOffset Time { get; set; }
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
using System.Net.Http.Json;
|
using System.Net.Http.Json;
|
||||||
using System.Net.WebSockets;
|
using System.Net.WebSockets;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using KfChatDotNetBot.Models.DbModels;
|
||||||
using NLog;
|
using NLog;
|
||||||
using Websocket.Client;
|
using Websocket.Client;
|
||||||
|
|
||||||
@@ -132,19 +133,34 @@ public class Twitch : IDisposable
|
|||||||
return;
|
return;
|
||||||
var topicParts = topicString.Split('.');
|
var topicParts = topicString.Split('.');
|
||||||
var channelId = int.Parse(topicParts[^1]);
|
var channelId = int.Parse(topicParts[^1]);
|
||||||
var twitchMessage = data.GetProperty("message")!.GetString()!;
|
var twitchMessage = JsonSerializer.Deserialize<JsonElement>(data.GetProperty("message").GetString());
|
||||||
|
|
||||||
if (twitchMessage.Contains("\"type\":\"stream-up\""))
|
if (twitchMessage.GetProperty("type").GetString() == "stream-up")
|
||||||
{
|
{
|
||||||
OnStreamStateUpdated?.Invoke(this, channelId, true);
|
OnStreamStateUpdated?.Invoke(this, channelId, true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (twitchMessage.Contains("\"type\":\"stream-down\""))
|
if (twitchMessage.GetProperty("type").GetString() == "stream-down")
|
||||||
{
|
{
|
||||||
OnStreamStateUpdated?.Invoke(this, channelId, false);
|
OnStreamStateUpdated?.Invoke(this, channelId, false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (twitchMessage.GetProperty("type").GetString() == "viewcount")
|
||||||
|
{
|
||||||
|
_logger.Info("Updating DB with fresh view count");
|
||||||
|
using var db = new ApplicationDbContext();
|
||||||
|
db.TwitchViewCounts.Add(new TwitchViewCountDbModel
|
||||||
|
{
|
||||||
|
Topic = topicString,
|
||||||
|
ServerTime = twitchMessage.GetProperty("server_time").GetDouble(),
|
||||||
|
Viewers = twitchMessage.GetProperty("viewers").GetInt32(),
|
||||||
|
Time = DateTimeOffset.UtcNow
|
||||||
|
});
|
||||||
|
db.SaveChanges();
|
||||||
|
return;
|
||||||
|
}
|
||||||
_logger.Info("Message from Twitch was unhandled");
|
_logger.Info("Message from Twitch was unhandled");
|
||||||
_logger.Info(message.Text);
|
_logger.Info(message.Text);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user