-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVote_For_Points_StreamlabsSystem.py
More file actions
238 lines (203 loc) · 8.35 KB
/
Vote_For_Points_StreamlabsSystem.py
File metadata and controls
238 lines (203 loc) · 8.35 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# pylint: disable=invalid-name
"""Voting game with lot of variation and customization to fit most streamers"""
#---------------------------------------
# Libraries and references
#---------------------------------------
import codecs
import json
import os
import winsound
import ctypes
import random
import time
#---------------------------------------
# [Required] Script information
#---------------------------------------
ScriptName = "Vote for Points"
Website = "https://www.twitch.tv/generalrommel"
Creator = "GeneralRommel"
Version = "1.0"
Description = "Voting minigame"
#---------------------------------------
# Variables
#---------------------------------------
settingsfile = os.path.join(os.path.dirname(__file__), "settings.json")
MessageBox = ctypes.windll.user32.MessageBoxW
MB_YES = 6
#---------------------------------------
# Classes
#---------------------------------------
class Settings:
"""" Loads settings from file if file is found if not uses default values"""
# The 'default' variable names need to match UI_Config
def __init__(self, settingsfile=None):
if settingsfile and os.path.isfile(settingsfile):
with codecs.open(settingsfile, encoding='utf-8-sig', mode='r') as f:
self.__dict__ = json.load(f, encoding='utf-8-sig')
else: #set variables if no custom settings file is found
self.OnlyLive = True
self.StartCommand = "!startvote"
self.EndCommand = "!endvote"
self.VoteCommand = "!vote"
self.WinCommand = "!win"
self.Permission = "Caster"
self.PermissionInfo = ""
self.Usage = "Stream Chat"
self.EndResponse = "Voting is now closed! Wait for the announcement to see who wins, good luck!"
self.WinResponse = "Team {0} has won! Everyone who voted for them gets 1 {1}"
self.StartResponse = "A round of voting for which team will win has started! Type !vote 1-5 to vote for teams 1-5."
self.VoteMessage = "$user your vote has been registered."
self.PermissionResp = "$user -> only $permission ($permissioninfo) and higher can use this command"
self.VoteTime = -1.0
# Reload settings on save through UI
def Reload(self, data):
"""Reload settings on save through UI"""
self.__dict__ = json.loads(data, encoding='utf-8-sig')
def Save(self, settingsfile):
""" Save settings contained within to .json and .js settings files. """
try:
with codecs.open(settingsfile, encoding="utf-8-sig", mode="w+") as f:
json.dump(self.__dict__, f, encoding="utf-8", ensure_ascii=False)
with codecs.open(settingsfile.replace("json", "js"), encoding="utf-8-sig", mode="w+") as f:
f.write("var settings = {0};".format(json.dumps(self.__dict__, encoding='utf-8', ensure_ascii=False)))
except ValueError:
Parent.Log(ScriptName, "Failed to save settings to file.")
#---------------------------------------
# Settings functions
#---------------------------------------
def SetDefaults():
"""Set default settings function"""
winsound.MessageBeep()
returnValue = MessageBox(0, u"You are about to reset the settings, "
"are you sure you want to contine?"
, u"Reset settings file?", 4)
if returnValue == MB_YES:
returnValue = MessageBox(0, u"Settings successfully restored to default values"
, u"Reset complete!", 0)
global MySet
Settings.Save(MySet, settingsfile)
def ReloadSettings(jsonData):
"""Reload settings on pressing the save button"""
global MySet
MySet.Reload(jsonData)
def SaveSettings():
"""Save settings on pressing the save button"""
Settings.Save(MySet, settingsfile)
#---------------------------------------
# Optional functions
#---------------------------------------
def OpenReadMe():
"""Open the readme.txt in the scripts folder"""
location = os.path.join(os.path.dirname(__file__), "README.txt")
os.startfile(location)
def SendResp(data, Usage, Message):
"""Sends message to Stream or discord chat depending on settings"""
Message = Message.replace("$user", data.UserName)
Message = Message.replace("$currencyname", Parent.GetCurrencyName())
Message = Message.replace("$target", data.GetParam(1))
Message = Message.replace("$permissioninfo", MySet.PermissionInfo)
Message = Message.replace("$permission", MySet.Permission)
l = ["Stream Chat", "Chat Both", "All", "Stream Both"]
if not data.IsFromDiscord() and (Usage in l) and not data.IsWhisper():
Parent.SendStreamMessage(Message)
l = ["Stream Whisper", "Whisper Both", "All", "Stream Both"]
if not data.IsFromDiscord() and data.IsWhisper() and (Usage in l):
Parent.SendStreamWhisper(data.User, Message)
l = ["Discord Chat", "Chat Both", "All", "Discord Both"]
if data.IsFromDiscord() and not data.IsWhisper() and (Usage in l):
Parent.SendDiscordMessage(Message)
l = ["Discord Whisper", "Whisper Both", "All", "Discord Both"]
if data.IsFromDiscord() and data.IsWhisper() and (Usage in l):
Parent.SendDiscordDM(data.User, Message)
#---------------------------------------
# [Required] functions
#---------------------------------------
def Init():
"""data on Load, required function"""
global MySet
MySet = Settings(settingsfile)
if MySet.Usage == "Twitch Chat":
MySet.Usage = "Stream Chat"
Settings.Save(MySet, settingsfile)
elif MySet.Usage == "Twitch Whisper":
MySet.Usage = "Stream Whisper"
Settings.Save(MySet, settingsfile)
elif MySet.Usage == "Twitch Both":
MySet.Usage = "Stream Both"
Settings.Save(MySet, settingsfile)
global State
State = 0
global JoinedPlayers
JoinedPlayers = []
global StartTime
StartTime = None
global StartData
StartData = None
def Execute(data):
"""Required Execute data function"""
global State
global JoinedPlayers
global StartTime
global StartData
if State == 0 and data.IsChatMessage() and data.GetParam(0).lower() == MySet.StartCommand.lower():
if not HasPermission(data):
return
if not MySet.OnlyLive or Parent.IsLive():
State = 1
message = MySet.StartResponse
SendResp(data, MySet.Usage, message)
StartTime = time.time()
StartData = data
return
if State == 1 and data.IsChatMessage() and data.GetParam(0).lower() == MySet.VoteCommand.lower():
JoinedPlayers.append(data)
SendResp(data, MySet.Usage, MySet.VoteMessage)
return
if State == 1 and data.IsChatMessage() and data.GetParam(0).lower() == MySet.EndCommand.lower():
if not HasPermission(data):
return
State = 2
SendResp(data, MySet.Usage, MySet.EndResponse)
return
if (State == 1 or State == 2) and data.IsChatMessage() and data.GetParam(0).lower() == MySet.WinCommand.lower():
if not HasPermission(data):
return
HandleWinner(data)
return
return
def HandleWinner(data):
global State
global JoinedPlayers
global StartTime
State = 0
StartTime = None
if not JoinedPlayers:
SendResp(data, MySet.Usage, MySet.NoJoinResponse)
return
winningTeam = data.GetParam(1).lower();
for player in JoinedPlayers:
if player.GetParam(1).lower() == winningTeam:
Parent.AddPoints(player.User, player.UserName, 1)
currency = Parent.GetCurrencyName()
winMessage = MySet.WinResponse.format(winningTeam, currency)
SendResp(data, MySet.Usage, winMessage)
JoinedPlayers = []
return
def Tick():
"""Required tick function"""
global StartTime
global StartData
if StartTime is not None:
elapsedTime = time.time() - StartTime
if elapsedTime > MySet.VoteTime != -1.0:
HandleWinner(StartData)
return
def HasPermission(data):
"""Returns true if user has permission and false if user doesn't"""
if not Parent.HasPermission(data.User, MySet.Permission, MySet.PermissionInfo):
message = MySet.PermissionResp.format(data.UserName, MySet.Permission, MySet.PermissionInfo)
SendResp(data, MySet.Usage, message)
return False
return True