2024-04-05 22:15:59 -04:00
namespace vassago ;
2023-06-01 00:03:23 -04:00
#pragma warning disable 4014 //the "not awaited" error
2024-04-05 22:15:59 -04:00
using vassago.Behavior ;
2023-06-01 00:03:23 -04:00
using vassago.Models ;
using System ;
using System.Linq ;
using System.Text ;
using System.Text.RegularExpressions ;
using System.Threading.Tasks ;
using System.Collections.Generic ;
2023-06-15 23:29:07 -04:00
public class Behaver
2023-06-01 00:03:23 -04:00
{
2024-01-05 21:39:31 -05:00
private List < Account > SelfAccounts { get ; set ; } = new List < Account > ( ) ;
private User SelfUser { get ; set ; }
2024-04-05 22:15:59 -04:00
public static List < vassago . Behavior . Behavior > Behaviors { get ; private set ; } = new List < vassago . Behavior . Behavior > ( ) ;
2023-06-18 22:10:51 -04:00
internal Behaver ( )
{
var subtypes = AppDomain . CurrentDomain . GetAssemblies ( )
. SelectMany ( domainAssembly = > domainAssembly . GetTypes ( ) )
2024-04-05 22:15:59 -04:00
. Where ( type = > type . IsSubclassOf ( typeof ( vassago . Behavior . Behavior ) ) & & ! type . IsAbstract & &
2023-06-20 21:26:44 -04:00
type . GetCustomAttributes ( typeof ( StaticPlzAttribute ) , false ) ? . Any ( ) = = true )
2023-06-18 22:10:51 -04:00
. ToList ( ) ;
foreach ( var subtype in subtypes )
{
2024-04-05 22:15:59 -04:00
Behaviors . Add ( ( vassago . Behavior . Behavior ) Activator . CreateInstance ( subtype ) ) ;
2023-06-18 22:10:51 -04:00
}
}
2023-06-15 23:29:07 -04:00
static Behaver ( ) { }
2023-06-01 00:03:23 -04:00
2023-06-15 23:29:07 -04:00
private static readonly Behaver _instance = new Behaver ( ) ;
2023-06-01 00:03:23 -04:00
2023-06-15 23:29:07 -04:00
public static Behaver Instance
2023-06-01 00:03:23 -04:00
{
get { return _instance ; }
}
public async Task < bool > ActOn ( Message message )
{
2023-06-20 21:26:44 -04:00
foreach ( var behavior in Behaviors )
2023-06-18 22:10:51 -04:00
{
2023-06-19 11:03:06 -04:00
if ( behavior . ShouldAct ( message ) )
2023-06-18 22:10:51 -04:00
{
2023-06-19 11:03:06 -04:00
behavior . ActOn ( message ) ;
2023-06-18 22:10:51 -04:00
message . ActedOn = true ;
2023-08-22 14:58:44 -04:00
Console . WriteLine ( "acted on, moving forward" ) ;
2023-06-18 22:10:51 -04:00
}
}
2024-01-05 21:39:31 -05:00
if ( message . ActedOn = = false & & message . MentionsMe & & message . Content . Contains ( '?' ) & & ! Behaver . Instance . SelfAccounts . Any ( acc = > acc . Id = = message . Author . Id ) )
2023-06-18 22:10:51 -04:00
{
Console . WriteLine ( "providing bullshit nonanswer / admitting uselessness" ) ;
var responses = new List < string > ( ) {
@"Well, that's a great question, and there are certainly many different possible answers. Ultimately, the decision will depend on a variety of factors, including your personal interests and goals, as well as any practical considerations (like the economy). I encourage you to do your research, speak with experts and educators, and explore your options before making a decision that's right for you." ,
2023-08-22 14:58:44 -04:00
@"┐(゚ ~゚ )┌" , @"¯\_(ツ)_/¯" , @"╮ (. ❛ ᴗ ❛.) ╭" , @"╮(╯ _╰ )╭"
2023-06-18 22:10:51 -04:00
} ;
await message . Channel . SendMessage ( responses [ Shared . r . Next ( responses . Count ) ] ) ;
message . ActedOn = true ;
}
return message . ActedOn ;
2023-06-01 00:03:23 -04:00
}
2024-01-05 21:39:31 -05:00
internal bool IsSelf ( Guid AccountId )
{
2024-01-10 21:21:31 -05:00
var db = new ChattingContext ( ) ;
var acc = db . Accounts . Find ( AccountId ) ;
2024-01-05 21:39:31 -05:00
return SelfAccounts . Any ( acc = > acc . Id = = AccountId ) ;
}
public void MarkSelf ( Account selfAccount )
{
2024-01-10 21:21:31 -05:00
var db = new ChattingContext ( ) ;
2024-01-05 21:39:31 -05:00
if ( SelfUser = = null )
{
SelfUser = selfAccount . IsUser ;
}
else if ( SelfUser ! = selfAccount . IsUser )
{
2024-01-10 21:21:31 -05:00
CollapseUsers ( SelfUser , selfAccount . IsUser , db ) ;
2024-01-05 21:39:31 -05:00
}
2024-01-10 21:21:31 -05:00
SelfAccounts = db . Accounts . Where ( a = > a . IsUser = = SelfUser ) . ToList ( ) ;
2024-01-05 21:39:31 -05:00
}
2024-01-05 22:12:57 -05:00
2024-01-10 21:21:31 -05:00
public bool CollapseUsers ( User primary , User secondary , ChattingContext db )
2024-01-05 22:12:57 -05:00
{
Console . WriteLine ( $"{secondary.Id} is being consumed into {primary.Id}" ) ;
primary . Accounts . AddRange ( secondary . Accounts ) ;
foreach ( var a in secondary . Accounts )
{
a . IsUser = primary ;
}
secondary . Accounts . Clear ( ) ;
Console . WriteLine ( "accounts transferred" ) ;
try
{
2024-01-10 21:21:31 -05:00
db . SaveChangesAsync ( ) . Wait ( ) ;
2024-01-05 22:12:57 -05:00
}
catch ( Exception e )
{
Console . WriteLine ( "First save exception." ) ;
Console . Error . WriteLine ( e ) ;
return false ;
}
Console . WriteLine ( "saved" ) ;
2024-01-10 21:21:31 -05:00
db . Users . Remove ( secondary ) ;
2024-01-05 22:12:57 -05:00
Console . WriteLine ( "old account cleaned up" ) ;
try
{
2024-01-10 21:21:31 -05:00
db . SaveChangesAsync ( ) . Wait ( ) ;
2024-01-05 22:12:57 -05:00
}
catch ( Exception e )
{
Console . WriteLine ( "Second save exception." ) ;
Console . Error . WriteLine ( e ) ;
return false ;
}
Console . WriteLine ( "saved, again, separately" ) ;
return true ;
}
2023-06-01 00:03:23 -04:00
}
2024-01-05 21:39:31 -05:00
#pragma warning restore 4014 //the "async not awaited" error