Skip to content

Commit

Permalink
Updated to wirk with new Cormas
Browse files Browse the repository at this point in the history
  • Loading branch information
olekscode committed Sep 12, 2024
1 parent 4919fca commit 4d5686e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 55 deletions.
44 changes: 31 additions & 13 deletions src/Cormas-Telegram/CormasBot.class.st
Original file line number Diff line number Diff line change
@@ -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 |
Expand All @@ -35,7 +39,7 @@ CormasBot >> defaultText: aMessage [
session interpret: aMessage.
]

{ #category : #initialization }
{ #category : 'initialization' }
CormasBot >> initialize [

super initialize.
Expand All @@ -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"

Expand All @@ -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
Expand All @@ -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
]
15 changes: 8 additions & 7 deletions src/Cormas-Telegram/CormasBotCommand.class.st
Original file line number Diff line number Diff line change
@@ -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
Expand Down
80 changes: 46 additions & 34 deletions src/Cormas-Telegram/CormasBotSession.class.st
Original file line number Diff line number Diff line change
@@ -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 |
Expand All @@ -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 |
Expand All @@ -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 |
Expand All @@ -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.
Expand All @@ -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
]
2 changes: 1 addition & 1 deletion src/Cormas-Telegram/package.st
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Package { #name : #'Cormas-Telegram' }
Package { #name : 'Cormas-Telegram' }

0 comments on commit 4d5686e

Please sign in to comment.