diff --git a/src/Cormas-Telegram/CormasBot.class.st b/src/Cormas-Telegram/CormasBot.class.st index 0e9b3a7..3e14572 100644 --- a/src/Cormas-Telegram/CormasBot.class.st +++ b/src/Cormas-Telegram/CormasBot.class.st @@ -1,28 +1,32 @@ Class { - #name : #CormasBot, - #superclass : #Bottlegram, + #name : 'CormasBot', + #superclass : 'Bottlegram', #instVars : [ 'sessions', - 'connectedChats' + 'connectedChats', + 'updateBlock', + 'simulation' ], - #category : #'Cormas-Telegram' + #category : 'Cormas-Telegram', + #package : 'Cormas-Telegram' } -{ #category : #'as yet unclassified' } -CormasBot >> createSessionFor: aModel titled: aString [ +{ #category : 'as yet unclassified' } +CormasBot >> createSessionFor: aSimulation titled: aString [ | session | session := CormasBotSession new bot: self; - cormasModel: aModel; + simulation: aSimulation; title: aString; + whenActionPerformedDo: updateBlock; yourself. sessions add: session. ] -{ #category : #action } +{ #category : 'action' } CormasBot >> defaultText: aMessage [ | session | @@ -35,7 +39,7 @@ CormasBot >> defaultText: aMessage [ session interpret: aMessage. ] -{ #category : #initialization } +{ #category : 'initialization' } CormasBot >> initialize [ super initialize. @@ -44,9 +48,17 @@ CormasBot >> initialize [ self registerCommand: #slashMySession: to: '/mysession'. self registerCommand: #slashConnect: to: '/connect'. + + updateBlock := [ "do nothing" ] +] + +{ #category : 'accessing' } +CormasBot >> simulation: aSimulation [ + + simulation := aSimulation ] -{ #category : #action } +{ #category : 'action' } CormasBot >> slashConnect: aMessage [ "TODO: Implement a way to choose session" @@ -57,13 +69,13 @@ CormasBot >> slashConnect: aMessage [ aMessage send: 'You have joined the session ''', session title, ''''. ] -{ #category : #action } +{ #category : 'action' } CormasBot >> slashHelp: aMessage [ aMessage send: 'Hello, I''m a Telegram bot for Cormas written in Pharo. Use me to select an agent that you wish to control and then make it move or eat' ] -{ #category : #action } +{ #category : 'action' } CormasBot >> slashMySession: aMessage [ connectedChats at: aMessage chat id @@ -74,8 +86,14 @@ CormasBot >> slashMySession: aMessage [ ifAbsent: [ aMessage send: 'You are not connected to any session yet' ]. ] -{ #category : #action } +{ #category : 'action' } CormasBot >> slashStart: aMessage [ aMessage send: 'Hello, I''m a Telegram bot written in Pharo. Send /help for more information' ] + +{ #category : 'enumerating' } +CormasBot >> whenActionPerformedDo: aBlock [ + + updateBlock := aBlock +] diff --git a/src/Cormas-Telegram/CormasBotCommand.class.st b/src/Cormas-Telegram/CormasBotCommand.class.st index 5f15beb..6859dd5 100644 --- a/src/Cormas-Telegram/CormasBotCommand.class.st +++ b/src/Cormas-Telegram/CormasBotCommand.class.st @@ -1,32 +1,33 @@ Class { - #name : #CormasBotCommand, - #superclass : #Object, + #name : 'CormasBotCommand', + #superclass : 'Object', #instVars : [ 'name', 'action' ], - #category : #'Cormas-Telegram' + #category : 'Cormas-Telegram', + #package : 'Cormas-Telegram' } -{ #category : #accessing } +{ #category : 'accessing' } CormasBotCommand >> action: aBlock [ "An action to be executed" action := aBlock ] -{ #category : #'as yet unclassified' } +{ #category : 'as yet unclassified' } CormasBotCommand >> exectuteFor: anEntity [ action value: anEntity ] -{ #category : #accessing } +{ #category : 'accessing' } CormasBotCommand >> name [ ^ name ] -{ #category : #accessing } +{ #category : 'accessing' } CormasBotCommand >> name: aString [ "Name must be lowercase. We will match it with what user types ('move', 'Move' and 'MOVE' should all match the command named 'move')" name := aString diff --git a/src/Cormas-Telegram/CormasBotSession.class.st b/src/Cormas-Telegram/CormasBotSession.class.st index 96fd7ce..a1c83a9 100644 --- a/src/Cormas-Telegram/CormasBotSession.class.st +++ b/src/Cormas-Telegram/CormasBotSession.class.st @@ -1,55 +1,49 @@ Class { - #name : #CormasBotSession, - #superclass : #Object, + #name : 'CormasBotSession', + #superclass : 'Object', #instVars : [ 'id', 'title', 'bot', - 'cormasModel', + 'simulation', 'entities', - 'commands' + 'commands', + 'updateBlock' ], - #category : #'Cormas-Telegram' + #category : 'Cormas-Telegram', + #package : 'Cormas-Telegram' } -{ #category : #accessing } +{ #category : 'accessing' } CormasBotSession >> bot: aBot [ bot := aBot ] -{ #category : #'as yet unclassified' } +{ #category : 'as yet unclassified' } CormasBotSession >> controlEntity: aMessage [ "Assign control over certain entity (agent, patch, etc.) to a chat that requested control over it" - | words type id entityCollection entity | + | words type entityId entityClass getter entityCollection entity | words := aMessage text substrings. type := words second. - id := words third asInteger. + entityId := words third asInteger. - entityCollection := cormasModel perform: ('the', type, 's') asSymbol. + entityClass := simulation cormasModel class allEntityClasses detect: [ :aClass | aClass name includesSubstring: type ]. - entity := entityCollection detect: [ :each | each id = id ]. + getter := simulation cormasModel getterForEntityClass: entityClass. + + entityCollection := simulation cormasModel perform: getter. + + entity := entityCollection detect: [ :each | each id = entityId ]. entities at: aMessage chat id put: entity. - aMessage send: 'Controlling ', type, ' entity with id ', id asString -] - -{ #category : #accessing } -CormasBotSession >> cormasModel [ - - ^ cormasModel + aMessage send: 'Controlling ', type, ' entity with id ', entityId asString ] -{ #category : #accessing } -CormasBotSession >> cormasModel: aModel [ - - cormasModel := aModel -] - -{ #category : #execution } +{ #category : 'execution' } CormasBotSession >> execute: aMessage [ | words entity informalSelector arguments selector | @@ -69,12 +63,12 @@ CormasBotSession >> execute: aMessage [ numberOfArguments: arguments size. entity perform: selector withArguments: arguments. - cormasModel timeChanged. + updateBlock value. aMessage send: 'Performing action #', selector. ] -{ #category : #'as yet unclassified' } +{ #category : 'as yet unclassified' } CormasBotSession >> findSelectorIn: anObject matching: aString numberOfArguments: aNumber [ | selectors | @@ -94,22 +88,22 @@ CormasBotSession >> findSelectorIn: anObject matching: aString numberOfArguments ' with ', aNumber asString, ' arguments' ]. ] -{ #category : #accessing } +{ #category : 'accessing' } CormasBotSession >> id [ ^ id ] -{ #category : #initialization } +{ #category : 'initialization' } CormasBotSession >> initialize [ super initialize. id := self hash. - entities := Dictionary new. + entities := Dictionary new ] -{ #category : #private } +{ #category : 'private' } CormasBotSession >> interpret: aMessage [ | text | @@ -127,7 +121,7 @@ CormasBotSession >> interpret: aMessage [ aMessage send: exception description ]. ] -{ #category : #'as yet unclassified' } +{ #category : 'as yet unclassified' } CormasBotSession >> simplifySelector: aString [ "Simplify the selector to match it to a text received from chat. For example, we want users to write 'Move' or 'move' to match selector #move. They can also write 'Move 4' to match selector #move: with one argument 4. @@ -136,14 +130,32 @@ CormasBotSession >> simplifySelector: aString [ ^ (aString copyWithout: $:) asLowercase ] -{ #category : #accessing } +{ #category : 'accessing' } +CormasBotSession >> simulation [ + + ^ simulation +] + +{ #category : 'accessing' } +CormasBotSession >> simulation: aSimulation [ + + simulation := aSimulation +] + +{ #category : 'accessing' } CormasBotSession >> title [ ^ title ] -{ #category : #accessing } +{ #category : 'accessing' } CormasBotSession >> title: aString [ title := aString ] + +{ #category : 'enumerating' } +CormasBotSession >> whenActionPerformedDo: aBlock [ + + updateBlock := aBlock +] diff --git a/src/Cormas-Telegram/package.st b/src/Cormas-Telegram/package.st index 0804698..211e5ed 100644 --- a/src/Cormas-Telegram/package.st +++ b/src/Cormas-Telegram/package.st @@ -1 +1 @@ -Package { #name : #'Cormas-Telegram' } +Package { #name : 'Cormas-Telegram' }