-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRPS-LS_StreamlabsSystem.py
More file actions
245 lines (183 loc) · 5.97 KB
/
Copy pathRPS-LS_StreamlabsSystem.py
File metadata and controls
245 lines (183 loc) · 5.97 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/python
# -*- coding: utf-8 -*-
""" RPS-LS
Classic Rock Paper Scissors mini game and LS extension
1.0.0
Initial release
Upcoming features
latin to ascii comparisons (Lézard == Lezard) should be True
Add multiple way to write a choice (ex. French: Pierre, Roche)
"""
# --------------------------------------
# Script Import Libraries
# --------------------------------------
import clr
import os
import json
import codecs
import re
clr.AddReference("IronPython.SQLite.dll")
clr.AddReference("IronPython.Modules.dll")
# --------------------------------------
# Script Information
# --------------------------------------
ScriptName = "RPS-LS"
Website = "https://github.com/CVex2150J"
Description = "Rock Paper Scissors LS"
Creator = "CVex2150J"
Version = "1.0.0"
# --------------------------------------
# Script Variables
# --------------------------------------
SettingsFile = os.path.join(os.path.dirname(__file__), "settings.json")
cooldown_command = "!rps"
local = {}
winningTable = [
# 0: rock, 1: paper, 2: scissors, 3: lizard, 4: Spock
[2, 1, 5], # cuts
[1, 0, 6], # covers
[0, 2, 7], # crushes
[0, 3, 8], # crushes
[3, 4, 9], # poisons
[4, 2, 10], # smashes
[2, 3, 11], # decapitates
[3, 1, 12], # eats
[1, 4, 13], # disproves
[4, 0, 14] # vaporizes
]
# --------------------------------------
# Script Classes
# --------------------------------------
class Settings(object):
""" Load in saved settings file if available else set default values. """
classic_command = "!rps"
lizardspock_command = "!rpsls"
localisation_file = "local_en.txt"
reward = 100
user_cooldown = 60
def __init__(self, settingsfile=None):
try:
with codecs.open(settingsfile, encoding="utf-8-sig", mode="r") as f:
self.__dict__ = json.load(f, encoding="utf-8")
except:
return
def Reload(self, jsondata):
""" Reload settings from interface by given json data. """
self.__dict__ = json.loads(jsondata, encoding="utf-8")
# --------------------------------------
# Script Functions
# --------------------------------------
# Utilities
def Log(message):
Parent.Log(ScriptName, str(message))
def Message(message):
Parent.SendStreamMessage(str(message))
# def Whisper(target, message):
# Parent.SendStreamWhisper(str(target), str(message))
# Functions
def LoadLocalisation():
global local
try:
# Parse localisation file
file_name = os.path.join(os.path.dirname(__file__), ScriptSettings.localisation_file)
_file = codecs.open(file_name, encoding="utf-8-sig", mode="r")
# get all lines, strip \n and remove any comments commencing with #
lines = [re.sub('#.*', '', line.rstrip('\r\n')) for line in _file]
# discard all empty and comment line
local = list(filter(lambda x: x, lines))
except Exception as e:
Log("ERROR : Unable to parse localisation file." + str(e))
def add_user_cooldown(data):
if ScriptSettings.user_cooldown > 0:
Parent.AddUserCooldown(ScriptName, cooldown_command, data.User, ScriptSettings.user_cooldown)
def giveReward(data):
if ScriptSettings.reward > 0:
Parent.AddPoints(data.User, data.UserName, ScriptSettings.reward)
def show_result(data, u, c, win):
if u != c:
if u == win[0]:
# win
giveReward(data)
result = local[15]
else:
# loose
result = local[16]
result = result.replace('{phrase}', local[win[0]] + ' ' + local[win[2]] + ' ' + local[win[1]])
else:
# tie
result = local[17]
result = result.replace('{user}', data.UserName)
# result = result.replace('{bot}', ... Bot name ? )
result = result.replace('{user_pick}', local[u])
result = result.replace('{bot_pick}', local[c])
Message(result)
# limit 3 : classic rock, paper, scissors
# limit 5 : rock, paper, scissors, lizard, Spock
def play(data, limit=3):
if ScriptSettings.user_cooldown > 0:
duration = Parent.GetUserCooldownDuration(ScriptName, cooldown_command, data.User)
if duration > 0:
# Message(data.UserName + ' can\'t use this command for another ' + str(duration) + ' seconds.')
return
# parse parameter 1 and try to find its index 1-3 in classic or 1-5 in LS mode
user_choice_str = data.GetParam(1).lower()
user_choice = -1
for c in local:
user_choice += 1
if user_choice > limit:
user_choice = -1
break
elif c.lower() == user_choice_str:
break
# user_choice -1 : the user gives and invalid option
if user_choice != -1:
add_user_cooldown(data)
# random computer choice
computer_choice = Parent.GetRandom(0, limit) # Limit is excluded
if user_choice == computer_choice:
# Equality
show_result(data, user_choice, computer_choice, None)
else:
# Find the choice combination
for win in winningTable:
if user_choice in win and computer_choice in win:
show_result(data, user_choice, computer_choice, win)
break
# --------------------------------------
# Chatbot Initialize Function
# --------------------------------------
def Init():
global ScriptSettings
# Load settings from settings file
ScriptSettings = Settings(SettingsFile)
LoadLocalisation()
# --------------------------------------
# Chatbot Save Settings Function
# --------------------------------------
def ReloadSettings(jsondata):
# Reload newly saved settings and verify
ScriptSettings.Reload(jsondata)
LoadLocalisation()
# --------------------------------------
# Chatbot Execute Function
# --------------------------------------
def Execute(data):
# Twitch chat message only for now
if not data.IsFromTwitch() or not data.IsChatMessage() or data.IsWhisper():
return
command = data.GetParam(0).lower()
if len(ScriptSettings.lizardspock_command) > 0 and command == ScriptSettings.lizardspock_command.lower():
play(data, 5)
elif len(ScriptSettings.classic_command) > 0 and command == ScriptSettings.classic_command.lower():
play(data, 3)
return
# --------------------------------------
# Chatbot Script Unload Function
# --------------------------------------
def Unload():
return
# --------------------------------------
# Chatbot Tick Function
# --------------------------------------
def Tick():
return