-
Notifications
You must be signed in to change notification settings - Fork 0
/
Driver.py
230 lines (193 loc) · 6.37 KB
/
Driver.py
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
import tweepy
import Passwords as passwords
from Player import Player
from Environment import Environment
from Scenario import Scenario
from Items import Items
import json
import time
import random
sleepTime = 2
savedPlayerJSON = './json/player.json'
savedScenJSON = './json/scenario.json'
"""
API initialization for the bot
"""
auth = tweepy.OAuthHandler(passwords.apiKey, passwords.apiSecret)
auth.set_access_token(passwords.access_token, passwords.access_token_secret)
api = tweepy.API(auth)
def getReplies(lastID):
countOne = 0
countTwo = 0
countThree = 0
for user in tweepy.Cursor(api.followers).items():
print user.screen_name
for tweet in tweepy.Cursor(api.user_timeline, screen_name= user.screen_name, count = 20, since_id=lastID).items():
print tweet.text
if tweet.text.find('#1')>=0:
countOne+=1
break
elif tweet.text.find('#2')>=0:
countTwo+=1
break
elif tweet.text.find('#3')>=0:
countThree+=1
break
if countOne>=countTwo and countOne>=countThree:
return 1
elif countTwo>=countThree:
return 2
else:
return 3
def loadScenFromFile(jsonFile):
s = Scenario()
with open(jsonFile, "r") as data_file:
data = json.load(data_file)
initial = data["Scenario"][0]["initial"]
s.initial = str(initial)
flavor = data["Scenario"][0]["flavorText"]
s.flavorText = str(flavor)
option1 = data["Scenario"][0]["option1"]
s.option1 = str(option1)
option2 = data["Scenario"][0]["option2"]
s.option2 = str(option2)
option3 = data["Scenario"][0]["option3"]
s.option3 = str(option3)
finish1 = data["Scenario"][0]["finish1"]
s.finish1 = finish1
finish2 = data["Scenario"][0]["finish2"]
s.finish2 = finish2
finish3 = data["Scenario"][0]["finish3"]
s.finish3 = finish3
finished = data["Scenario"][0]["finished"]
s.finished = bool(finished)
#s.loadCreature()
return s
"""
"This class represents a driver for the game
"Trend Dungeon.
"""
class Driver:
p = None #the player
trend = "" #the Trending topic
scen = None #the scenerio to do next
"""
Constructor
"""
def __init__(self):
self.p = Player(savedPlayerJSON)
#self.updatePlayer(self.p,True,0,0)
if self.p.isDead():
self.p = Player("./json/newPlayerTemplate.json")
self.trend = getTrend()
#print self.trend
self.scen = Scenario()
self.scen.loadFromFile(savedScenJSON)
if self.scen.finished:
option = getReplies(self.p.lastID)
self.followUpTweet(option)
if option == 1:
item = self.scen.finish1[1]
exp = self.scen.finish1[2]
health = self.scen.finish1[3]
elif option ==2:
item = self.scen.finish2[1]
exp = self.scen.finish2[2]
health = self.scen.finish2[3]
else:
item = self.scen.finish3[1]
exp = self.scen.finish3[2]
health = self.scen.finish3[3]
self.updatePlayer(self.p,item,exp,health)
tempEnviro = Environment(self.trend)
tempEnviro.generateScenario(self.trend).saveScenario()
self.scen = loadScenFromFile(savedScenJSON)
def updatePlayer(self,player,item,exp,health):
#print item , exp, health
if item:
player.item = Items(None)
player.giveExperiencePoints(int(exp)/int(player.level))
if health>0:
player.giveHealth(int(health))
else:
player.removeHealth(int(health))
player.savePlayer()
"""
Method to post the Announce Tweet using API
"""
def announceTweet(self):
msg = "The trend for this encounter is {}! Prepare for adventure!"
msg = msg.format(self.trend)
print msg #api.update_status(status = msg)
time.sleep(sleepTime)
"""
Method to post the Scenario Tweet using API
"""
def scenarioTweet(self):
msg = self.scen.getInitial()
print msg #api.update_status(status = msg)
time.sleep(sleepTime)
"""
Method to post the Status Tweet using API
"""
def statusTweet(self):
msg = "You have {0.currentHealth} Health, {0.experiencePoints} XP, and are level {0.level}."
if self.p.item.name != "":
msg+=" You currently have the {0.item.name} Item."
msg = msg.format(self.p)
print msg #api.update_status(status = msg)
time.sleep(sleepTime)
"""
Method to post the Option Tweet using API
"""
def optionsTweet(self):
temp = self.scen.getEncounterText()
msg = ""
if self.scen.option1 == "Fight":
msg+= "A {} attacks! ".format(self.scen.creature.getCreatureType())
msg += "Follow and reply with #1 to {0.option1}"
if self.scen.option2 != "":
msg+=", #2 to {0.option2}"
if self.scen.option3 != "":
msg+=" , or #3 to {0.option3}"
msg+= "! {}".format(self.trend)
msg = msg.format(self.scen)
self.scen.finished = True
print temp
print msg
api.update_status(status = temp)
time.sleep(sleepTime)
return api.update_status(status = msg)
def followUpTweet(self, option):
msg = "{}"
temp = self.scen.getFinished(option)[0]
msg = msg.format(temp)
msg2 = " You have {0.currentHealth} Health, {0.experiencePoints} XP, and are level {0.level}."
if self.p.item.name != "":
msg2+=" You currently have the {0.item.name} Item."
msg2 = msg2.format(self.p)
msg += msg2
print msg
api.update_status(status = msg)
time.sleep(sleepTime)
"""
Method to get the random trend to use on this run
"""
def getTrend():
#return 'trend'
trendsJSON = api.trends_place(23424977)
trends = trendsJSON[0]
x = random.randint(0,9)
trend = trends['trends'][x]['name'].encode('utf-8','ignore')
trend = Environment(trend).getNoun(trend)
print trend
return trend
def main():
driver = Driver()
#driver.announceTweet()
#driver.scenarioTweet()
#driver.statusTweet()
driver.p.lastID = driver.optionsTweet().id
driver.p.savePlayer()
driver.scen.saveToFile()
main()