Skip to content

Commit

Permalink
Change notify data structure, use memory instead of console to clear …
Browse files Browse the repository at this point in the history
…messages
  • Loading branch information
tedivm committed Apr 8, 2018
1 parent 9b6b590 commit 909b865
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 24 deletions.
60 changes: 49 additions & 11 deletions js/Notify.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
const MAX_QUEUE_SIZE = 30

var Notify = function (message, limit=false, groups=false) {

// Remove messages from older versions
if(Memory.__notify) {
delete Memory.__notify
}

if(!groups) {
groups = ['default']
}
Expand Down Expand Up @@ -35,16 +41,39 @@ var Notify = function (message, limit=false, groups=false) {
return 0
}

let reset = Game.time
let num = 0
let shardid = ''
if(Game.shard && Game.shard.name) {
const matches = Game.shard.name.match(/\d+$/);
if (matches) {
shardid = parseInt(matches[0]).toString(36);
}
}

Notify.getUUID = function () {
if (reset !== Game.time) {
reset = Game.time
num = 0
}
num++
return shardid + Game.time.toString(36) + num.toString(36).leftPad(3, '0')
}


Notify.queueMessage = function (message, groups) {
if(!Memory.__notify) {
Memory.__notify = []
if(!Memory.__notify_v2) {
Memory.__notify_v2 = {}
}
if (Memory.__notify_v2.length >= MAX_QUEUE_SIZE) {
return false
}
Memory.__notify.push({
const id = Notify.getUUID()
Memory.__notify_v2[id] = {
'message': message,
'groups': groups,
'tick': Game.time
})
}
}

// Clean up history instead of leaving old messages around
Expand All @@ -53,15 +82,24 @@ Notify.cleanHistory = function (limit) {
limit = 20000
}

if(!Memory.__notify_history) {
return
// Clear any already sent messages
if(Memory.__notify_v2) {
var ids = Object.keys(Memory.__notify_v2)
for(var id of ids) {
if(typeof Memory.__notify_v2[id] !== 'Object') {
delete Memory.__notify_v2[id]
}
}
}

var messages = Object.keys(Memory.__notify_history)
for(var i in messages) {
var message = messages[i]
if(Memory.__notify_history[message] < Game.time - limit) {
delete Memory.__notify_history[message]
// Clear expired historical messages.
if(Memory.__notify_history) {
var messages = Object.keys(Memory.__notify_history)
for(var i in messages) {
var message = messages[i]
if(Memory.__notify_history[message] < Game.time - limit) {
delete Memory.__notify_history[message]
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion screeps_notify/clear_notifications.js

This file was deleted.

26 changes: 14 additions & 12 deletions screeps_notify/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,23 @@ def getScreepsConnection():

def getNotifications(shard):
sconn = getScreepsConnection()
notifications = sconn.memory(path='__notify', shard=shard)
notifications = sconn.memory(path='__notify_v2', shard=shard)
if 'data' not in notifications:
return False
return notifications['data']
notificationMap = notifications['data']
messages = []
for messageId, message in notificationMap.items():
if isinstance(message, dict):
message['messageId'] = messageId
message['shard'] = shard
messages.append(message)
return messages


def clearNotifications(tick=0, shard='shard0'):
def clearNotification(messageId, shard='shard0'):
print 'clearing sent messages'
sconn = getScreepsConnection()
javascript_clear = 'var limit=' + str(tick) + ';'
javascript_clear += "if(typeof limit == 'undefined') var limit = 0; Memory.__notify = _.filter(Memory.__notify, function(notification){ return notification.tick > this.limit }.bind({'limit':limit}))"
sconn.console(javascript_clear, shard=shard)
sconn.set_memory('__notify_v2.%s' % messageId, None, shard)


class App():
Expand All @@ -63,26 +68,23 @@ def run(self):
if not notifications or len(notifications) <= 0:
print 'No notifications to send.'
continue
limit = 0

print 'Sending notifications.'
for notification in notifications:
if notification['tick'] > limit:
limit = notification['tick']

msgid = notification['messageId']
if 'groups' in notification:
groups = notification['groups']
else:
groups = ['default']

services = config.getServicesFromGroups(groups)
for service in services:
try:
driver = messenger.getMessengerDriver(service)
driver.sendMessage(notification['message'], shard)
except:
traceback.print_exc()
clearNotification(msgid, shard)

clearNotifications(limit, shard)

def lambda_handler(event,context):
app = App()
Expand Down

0 comments on commit 909b865

Please sign in to comment.