@ -32,7 +32,7 @@ namespace DiscoBot.gsmeet
private static readonly string ApplicationName = "DiscoBot" ;
private static readonly string ApplicationName = "DiscoBot" ;
private SheetsService service ;
private SheetsService service ;
private Dictionary < string , GSMeetingTimers > meetingTimers = new Dictionary < string , GSMeetingTimers > ( ) ;
private List < GSMeeting > lastMeetings = null ;
@ -73,7 +73,13 @@ namespace DiscoBot.gsmeet
timer . AutoReset = true ;
timer . AutoReset = true ;
timer . Elapsed + = async ( sender , e ) = >
timer . Elapsed + = async ( sender , e ) = >
{
{
await Task . Run ( ( ) = > HandleSheetCheck ( sheet ) ) ;
try
{
await Task . Run ( ( ) = > HandleSheetCheck ( sheet ) ) ;
} catch ( Exception ex )
{
Console . WriteLine ( "gsmeet: EXCEPTION IN SHEET CHECK" + ex ) ;
}
} ;
} ;
timer . Start ( ) ;
timer . Start ( ) ;
timers . Add ( sheet . Db . Name , timer ) ;
timers . Add ( sheet . Db . Name , timer ) ;
@ -133,6 +139,7 @@ namespace DiscoBot.gsmeet
var binHash = sha1 . ComputeHash ( Encoding . ASCII . GetBytes ( idString ) ) ;
var binHash = sha1 . ComputeHash ( Encoding . ASCII . GetBytes ( idString ) ) ;
var hash = BitConverter . ToString ( binHash ) . Replace ( "-" , string . Empty ) ;
var hash = BitConverter . ToString ( binHash ) . Replace ( "-" , string . Empty ) ;
meeting . Id = hash ;
meeting . Id = hash ;
meeting . FetchTime = DateTimeOffset . Now ;
return meeting ;
return meeting ;
}
}
@ -167,6 +174,40 @@ namespace DiscoBot.gsmeet
return meetings ;
return meetings ;
}
}
private string MissingStringForEvent ( GSMeeting m )
{
var now = DateTimeOffset . Now ;
string s = "" ;
for ( var i = 0 ; i < m . Dates . Count ; + + i )
{
var date = m . Dates [ i ] ;
if ( date < now | | date > now . AddDays ( 7 ) )
{
continue ;
}
var us = "" ;
foreach ( var user in m . Users )
{
if ( user . Signups [ i ] ! = "1" & & user . Signups [ i ] ! = "0" & & user . Signups [ i ] ! = "0.5" )
{
if ( user . User ! = null )
{
us = user . User . Mention + " " ;
}
else
{
us + = user . Name + " " ;
}
}
}
if ( us ! = "" )
{
s + = date + ": " + us + "\n" ;
}
}
return s ;
}
private async Task HandleSheetCheck ( GSSheet sheet )
private async Task HandleSheetCheck ( GSSheet sheet )
{
{
SocketTextChannel c = guild . Channels . Where ( g = > g . Id = = sheet . Db . Channel ) . Single ( ) as SocketTextChannel ;
SocketTextChannel c = guild . Channels . Where ( g = > g . Id = = sheet . Db . Channel ) . Single ( ) as SocketTextChannel ;
@ -176,7 +217,33 @@ namespace DiscoBot.gsmeet
{
{
Console . WriteLine ( "No values found." ) ;
Console . WriteLine ( "No values found." ) ;
}
}
var meetings = await ParseGSMeetSheet ( sheet , values ) ;
lastMeetings = await ParseGSMeetSheet ( sheet , values ) ;
var ln = sheet . Db . LastNotified ;
var now = DateTimeOffset . Now ;
if ( ln . AddDays ( 1 ) > now )
{
Console . WriteLine ( "It is: " + now + " Next notify is: " + ln . AddDays ( 1 ) ) ;
return ;
}
Console . WriteLine ( "Notifying..." ) ;
string missStr = "" ;
foreach ( var meeting in lastMeetings )
{
missStr + = MissingStringForEvent ( meeting ) ;
}
if ( ! string . IsNullOrEmpty ( missStr ) )
{
string msg = "Heyo! I found the following missing raid signups:\n" ;
msg + = missStr ;
msg + = "Please sign up so we can raid! :)" ;
await c . SendMessageAsync ( msg ) ;
}
//TODO FIXME: Remove the hardcode 18 and read meeting timespan from sheet and use this value
var nextDate = now . AddHours ( - now . Hour + 1 8 ) . AddMinutes ( - now . Minute ) . AddSeconds ( - now . Second ) . AddMilliseconds ( - now . Millisecond ) ;
Console . WriteLine ( "Setting next notify to: " + nextDate ) ;
sheet . Db . LastNotified = nextDate ;
gsmeetContext . SaveChanges ( ) ;
/ *
foreach ( var meeting in meetings )
foreach ( var meeting in meetings )
{
{
if ( ! sheet . Timers . ContainsKey ( meeting . Id ) )
if ( ! sheet . Timers . ContainsKey ( meeting . Id ) )
@ -187,17 +254,6 @@ namespace DiscoBot.gsmeet
sheet . Timers [ meeting . Id ] . UpdateTimers ( c , meeting ) ;
sheet . Timers [ meeting . Id ] . UpdateTimers ( c , meeting ) ;
}
}
Console . WriteLine ( "Parsed sheet " + sheet . Db . Id ) ;
Console . WriteLine ( "Parsed sheet " + sheet . Db . Id ) ;
/ *
var str = "" ;
foreach ( var row in values )
{
foreach ( var col in row )
{
str + = col + "| " ;
}
str + = "\n" ;
}
c . SendMessageAsync ( "Result: " + str ) ;
* /
* /
}
}
@ -216,6 +272,8 @@ namespace DiscoBot.gsmeet
sheet . Id = sheetId ;
sheet . Id = sheetId ;
sheet . SheetName = sheetName ;
sheet . SheetName = sheetName ;
sheet . LastChecked = DateTimeOffset . Now ;
sheet . LastChecked = DateTimeOffset . Now ;
var now = DateTimeOffset . Now ;
sheet . LastNotified = now . AddDays ( - 1 ) . AddHours ( - now . Hour ) . AddMinutes ( - now . Minute ) . AddSeconds ( - now . Second ) . AddMilliseconds ( - now . Millisecond ) ;
sheet . CheckInterval = new TimeSpan ( 0 , 0 , timeSec ) ;
sheet . CheckInterval = new TimeSpan ( 0 , 0 , timeSec ) ;
gsmeetContext . GSheets . Add ( sheet ) ;
gsmeetContext . GSheets . Add ( sheet ) ;
try
try