From 909b8655c8a7e30984c98dec6d5a4edf25581527 Mon Sep 17 00:00:00 2001 From: Robert Hafner Date: Sat, 7 Apr 2018 17:09:47 -0700 Subject: [PATCH] Change notify data structure, use memory instead of console to clear messages --- js/Notify.js | 60 ++++++++++++++++++++++----- screeps_notify/clear_notifications.js | 1 - screeps_notify/notify.py | 26 ++++++------ 3 files changed, 63 insertions(+), 24 deletions(-) delete mode 100644 screeps_notify/clear_notifications.js diff --git a/js/Notify.js b/js/Notify.js index b4d026f..93ffecf 100644 --- a/js/Notify.js +++ b/js/Notify.js @@ -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'] } @@ -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 @@ -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] + } } } } diff --git a/screeps_notify/clear_notifications.js b/screeps_notify/clear_notifications.js deleted file mode 100644 index a4ccd23..0000000 --- a/screeps_notify/clear_notifications.js +++ /dev/null @@ -1 +0,0 @@ -if(typeof limit == 'undefined') var limit = 0; Memory.__notify = _.filter(Memory.__notify, function(notification){ return notification.tick > this.limit }.bind({'limit':limit})) \ No newline at end of file diff --git a/screeps_notify/notify.py b/screeps_notify/notify.py index 533641b..fe7a2e8 100755 --- a/screeps_notify/notify.py +++ b/screeps_notify/notify.py @@ -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(): @@ -63,17 +68,14 @@ 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: @@ -81,8 +83,8 @@ def run(self): driver.sendMessage(notification['message'], shard) except: traceback.print_exc() + clearNotification(msgid, shard) - clearNotifications(limit, shard) def lambda_handler(event,context): app = App()