blob: be870bb82362c23f7b430d3780b7bbbf81a9b62f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
;;;; A bot for playing rock paper scissors.
(ql:quickload :cl-ppcre)
(defpackage #:roshambot
(:use :cl :granolin))
;; (:import-from :granolin
;; :handle-event
;; :find-contact
;; :text-message-event
;; :send-text-message
;; :let-cond))
(in-package :roshambot)
;;; The logic of roshambot is something like:
;;; 1. A user indicates to the bot that they will to challenge another user to a
;;; game of roshambo "hey bot, roshambo with bob"
;;; 2. If bob is known to the bot then the bot will open a private with both the
;;; initiator, say alice, and with bob and present them with a chance to make a
;;; move or to cancel.
;;; 3. The bot then waits for both users to respond with a move or for either
;;; one of them to cancel.
;;; 4. The bot then reports the outcome of the match (winner bob, winner alice,
;;; or canceled by x) in the room in which the match was initiated.
(defstruct roshambo-match
room
challenger
challenger-room
challenger-move
challenged
challenged-room
challenged-move)
(defclass roshambot ()
((live-matches
:accessor live-matches
:initform nil)))
(defparameter +challenge-regex+
(ppcre:create-scanner " ?i challenge ([a-zA-Z0-9_.-]+) to roshambo"
:case-insensitive-mode t))
(defun you-wanna-piece-of-this!? (str)
"If the string communicates one user's intention to challenge another to
roshambo, returns the name of the user that has been challenged. Returns NIL otherwise."
(let-when (groups (nth-value 1 (ppcre:scan-to-strings +challenge-regex+ str)))
(aref groups 0)))
(defparameter +roshambo-move-regex+
(ppcre:create-scanner "(rock|paper|scissors|cancel)" :case-insensitive-mode t))
(defun roshambo-move!? (str)
(nth-value 0 (ppcre:scan-to-strings +roshambo-move-regex+ str)))
(defmethod handle-event :after ((bot roshambot) (event text-message-event) &optional room-id)
(let ((text (granolin:msg-body event)))
(let-cond
(challenged (you-wanna-piece-of-this!? text)
(format t "should be challenging: ~a~%" text)
(handle-new-challenge bot room-id (granolin:sender event) challenged))
(roshambo-match (challenger-made-move!? bot room-id (granolin::sender event) text)
(handle-match-state-change bot roshambo-match))
(roshambo-match (challenged-made-move!? bot room-id (granolin::sender event) text)
(handle-match-state-change bot roshambo-match)))))
(defun challenger-made-move!? (bot room-id sender text)
(let-when (roshambo-match (find room-id (live-matches bot)
:key #'roshambo-match-challenger-room
:test #'equal))
(unless (roshambo-match-challenger-move roshambo-match)
(let-when (move (and (equal sender (roshambo-match-challenger roshambo-match))
(roshambo-move!? text)))
(setf (roshambo-match-challenger-move roshambo-match) move)
roshambo-match))))
(defun challenged-made-move!? (bot room-id sender text)
(let-when (roshambo-match (find room-id (live-matches bot)
:key #'roshambo-match-challenged-room
:test #'equal))
(unless (roshambo-match-challenged-move roshambo-match)
(let-when (move (and (equal sender (roshambo-match-challenged roshambo-match))
(roshambo-move!? text)))
(setf (roshambo-match-challenged-move roshambo-match) move)
roshambo-match))))
(defun handle-match-state-change (bot roshambo-match)
(let-cond
(cancelled-by (roshambo-cancelled!? roshambo-match)
(send-text-message bot (roshambo-match-room roshambo-match)
"The match between ~a and ~a was canceled by ~a."
(readable-username
(roshambo-match-challenger roshambo-match))
(readable-username
(roshambo-match-challenged roshambo-match))
cancelled-by)
(kill-roshambo-match bot roshambo-match))
(win-list (roshambo-has-winner!? roshambo-match)
(destructuring-bind (winner win-move loser lose-move) win-list
(if (eql winner :draw) ; this is a draw move
(send-text-message bot (roshambo-match-room roshambo-match)
"It's a draw! Both ~a and ~a picked ~a."
(readable-username win-move)
(readable-username loser)
lose-move)
(send-text-message bot (roshambo-match-room roshambo-match)
"~a's ~a beats ~a's ~a! ~a is the winner!~%"
(readable-username winner)
win-move
(readable-username loser)
lose-move
(readable-username winner))))
(kill-roshambo-match bot roshambo-match))))
(defun roshambo-cancelled!? (rmatch)
(cond ((and (roshambo-match-challenger-move rmatch)
(string-equal "cancel" (roshambo-match-challenger-move rmatch)))
(roshambo-match-challenger rmatch))
((and (roshambo-match-challenged-move rmatch)
(string-equal "cancel" (roshambo-match-challenged-move rmatch)))
(roshambo-match-challenged rmatch))))
(defun roshambo-has-winner!? (rsb)
(with-slots (challenger challenger-move challenged challenged-move) rsb
(when (and challenger-move challenged-move
(not (string-equal "cancel" challenger-move))
(not (string-equal "cancel" challenged-move)))
(cond ((string-equal challenger-move challenged-move)
(list :draw challenger challenged challenger-move)) ; both had same move
((and (string-equal "rock" challenger-move)
(string-equal "scissors" challenged-move))
(list challenger challenger-move challenged challenged-move))
((and (string-equal "paper" challenger-move)
(string-equal "rock" challenged-move))
(list challenger challenger-move challenged challenged-move))
((and (string-equal "scissors" challenger-move)
(string-equal "paper" challenged-move))
(list challenger challenger-move challenged challenged-move))
((and (string-equal "rock" challenged-move)
(string-equal "scissors" challenger-move))
(list challenged challenged-move challenger challenger-move))
((and (string-equal "paper" challenged-move)
(string-equal "rock" challenger-move))
(list challenged challenged-move challenger challenger-move))
((and (string-equal "scissors" challenged-move)
(string-equal "paper" challenger-move))
(list challenged challenged-move challenger challenger-move))))))
(defun kill-roshambo-match (bot roshambo-match)
(setf (live-matches bot)
(delete roshambo-match (live-matches bot))))
(defun handle-new-challenge (bot room-id challenger challenged)
(let ((challenger-room (ensure-direct-room bot challenger))
(challenged-room (ensure-direct-room bot challenged :like t)))
(if (and (send-text-message
bot
challenger-room
"You have challenged ~a to roshambo. Reply with Rock, Paper, Scissors or Cancel."
challenged)
(send-text-message
bot
challenged-room
"~a has challenged you to roshambo. Reply with Rock, Paper, Scissors, or Cancel."
challenger))
(push (make-roshambo-match :room room-id
:challenger challenger
:challenger-room challenger-room
:challenger-move nil
:challenged (find-contact bot challenged :like t)
:challenged-room challenged-room
:challenged-move nil)
(live-matches bot))
(send-text-message bot room-id "Some kind of problem starting a roshambo match :("))))
(defclass roshambo-bot (granolin:client granolin:server-directory roshambot) ())
;; (defmethod handle-event :after ((bot roshambot-bot) (ev timeline-event) &optional room-id)
;; (format t "~a - ~a:~% ~a~%" room-id (granolin::sender ev) (granolin:msg-body ev)))
(defmethod handle-event :after ((bot roshambo-bot) (ev text-message-event) &optional room-id)
(format t "~a - ~a:~% ~a~%" room-id (granolin::sender ev) (granolin:msg-body ev)))
(defmethod handle-event :after ((bot roshambo-bot)
(ev granolin::account-data-event)
&optional room-id)
(format t "~a ~a" (event-type ev) (event-content ev)))
(defvar *roshambot* (make-instance 'roshambo-bot
:homeserver "https://matrix.hrlo.world"))
|