Skip to content

Commit

Permalink
added commands to start and stop listening
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Jun 23, 2015
1 parent 300e205 commit 2b2c008
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
24 changes: 23 additions & 1 deletion grails-app/services/net/zomis/duga/DugaChatListener.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,33 @@ class DugaChatListener implements InitializingBean {

private ChatCommands commands

private final Map<String, ListenTask> listenRooms = new HashMap<>()

@Override
void afterPropertiesSet() throws Exception {
assert !commands
commands = new ChatCommands(this)
scheduler.scheduleWithFixedDelay(new ListenTask(chatBot, '20298', commands, this), 3000)
listenStart('20298')
}

ListenTask listenStart(String roomId) {
if (listenRooms.containsKey(roomId)) {
throw new IllegalStateException('Already listening in room ' + roomId)
}
ListenTask listenTask = new ListenTask(chatBot, roomId, commands, this)
def future = scheduler.scheduleWithFixedDelay(listenTask, 3000)
listenTask.future = future
listenRooms.put(roomId, listenTask)
return listenTask
}

ListenTask listenStop(long roomId) {
String roomKey = String.valueOf(roomId)
ListenTask task = listenRooms.get(roomKey)
if (task) {
task.future.cancel(true)
listenRooms.remove(roomKey)
}
return task
}
}
2 changes: 0 additions & 2 deletions src/main/groovy/net/zomis/duga/ChatCommands.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class ChatCommands {
ChatCommands(DugaChatListener bean) {
this.tasks = bean.tasks
this.bot = bean.chatBot
consumers << {ChatMessageIncoming event ->
}
consumers << {ChatMessageIncoming event ->
if (event.content.contains('add stats')) {
DailyInfo.withNewSession { status ->
Expand Down
16 changes: 16 additions & 0 deletions src/main/groovy/net/zomis/duga/tasks/ChatCommandDelegate.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,20 @@ abstract class ChatCommandDelegate extends Script {
message.reply(loadedTasks.size() + ' reloaded')
}

void listen(int roomId) {
requireAdmin()
def listenTask = bean.listenStart(String.valueOf(roomId))
message.reply('Listening in room ' + roomId)
}

void stop() {
requireAdmin()
ListenTask task = bean.listenStop(message.room_id)
if (task) {
message.post('TTQW! (Stopped listening)')
} else {
message.reply('Did not find task in map. This is probably a bug.')
}
}

}
6 changes: 6 additions & 0 deletions src/main/groovy/net/zomis/duga/tasks/ListenTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer
import org.codehaus.groovy.control.customizers.SecureASTCustomizer

import java.util.concurrent.ScheduledFuture

import static org.codehaus.groovy.syntax.Types.*

class ListenTask implements Runnable {
Expand All @@ -31,6 +33,7 @@ class ListenTask implements Runnable {
private long lastHandledId
private long lastMessageTime
private MechanizeAgent agent
ScheduledFuture<?> future

public ListenTask(DugaBot bot, String room, ChatCommands commandHandler, DugaChatListener bean) {
this.bean = bean
Expand Down Expand Up @@ -148,6 +151,9 @@ class ListenTask implements Runnable {
}
// handler.botCommand(message)
}
if (previousId <= 0) {
bot.postSingle(params, 'Monking! (Duga is now listening for commands)')
}
/* Root node: {"ms":4,"time":41194973,"sync":1433551091,"events":
[{"room_id":16134,"event_type":1,"time_stamp":1433547911,"user_id":125580,"user_name":"Duga","message_id":22039309,"content":"Loki Astari vs. Simon Andr&#233; Forsberg: 4383 diff. Year: -1368. Quarter: -69. Month: -5. Week: +60. Day: -25."}
,{"room_id":16134,"event_type":1,"time_stamp":1433548817,"user_id":125580,"user_name":"Duga","message_id":22039366,"content":"<b><i>RELOAD!<\/i><\/b>"}
Expand Down

0 comments on commit 2b2c008

Please sign in to comment.