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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
########################################################################################
# This file is part of Spheres.
# Copyright (C) 2019 Jesusalva
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
########################################################################################
# Definitions (critical appliances, classes, imports, renpy internals, updater)
init -3 python:
renpy.add_python_directory("python-extra")
import requests, zlib, base64, sys, copy, uuid, time, json, traceback
import os.path
from threading import Timer
from ws4py.client.threadedclient import WebSocketClient
# set PYTHON_VERSION variable (should be 2713, 3605 could fail)
PYTHON_VERSION="%d%d%02d" % (sys.version_info.major, sys.version_info.minor, sys.version_info.micro)
PYTHON_VERSION=int(PYTHON_VERSION)
# Ren'Py should come with Python 2.7.10 (2710), but just in case
# After all, I only tested with 2.7.10, 2.7.13 and 2.7.15
if (PYTHON_VERSION < 2700 or PYTHON_VERSION > 3000):
raise Exception("WARNING: Python version is not 2.7\nStrange bugs may happen on your client.\n\nClick on \"Ignore\" to continue.\nClick on \"Quit\" to exit.")
# Why setting this...?
ApTimer=""
# Configuration
config.autoreload = False
config.save_on_mobile_background = False
persistent.release_name = "Christmas"
if persistent.host is None:
persistent.host="spheres.tmw2.org"
persistent.serverlist=[["TMW2", "spheres.tmw2.org", 61000]]
# FIXME: Set good defaults (=bad) for Android
if renpy.android:
persistent.nothreading=False
persistent.ssl_enabled=False
else:
persistent.serverlist.append(["Localhost", "localhost", 61000])
if (persistent.allfiles is None):
persistent.allfiles=[]
allfiles=[]
HOST=str(persistent.host)
PORT=str(persistent.port)
FAILUREMSG="Request failed, Please try again"
OFFLINEMSG="401 Unauthorized"
OKMSG="200 OK"
TIMEOUT_INTERVAL=3.0
MAX_IRC_BUFFER=50
SSL_IS_BROKEN=False
CERT_NONE=0
INT_MAX=2147483647
debug=copy.copy(config.developer)
# Error Codes
ERR_JSONDECODER=101
ERR_LOGIN_DEFAULT=102
ERR_INVALID=103
ERR_TIMEOUT=104
ERR_NOGEMS=105
ERR_INVFULL=106
ERR_OK=200
# All error code library
ERRNO=[FAILUREMSG, OFFLINEMSG, OKMSG, ERR_LOGIN_DEFAULT, ERR_INVALID, ERR_TIMEOUT, ERR_NOGEMS, ERR_INVFULL, ERR_OK]
# Core musics/sfx
MUSIC_OPENING="sfx/bgm01.mp3"
MUSIC_TOWN="sfx/bgm02.mp3"
# Spheres actions
AP_NONE =False
AP_SPHERE =1
AP_SKILL =2
# Status
ST_TOWN =0
ST_QUEST =1
# Unit flags
UF_NOLVL =1
UF_NOPART =2
UF_EXPUP =4
UF_EVOMAT =8
UF_SUPEREVO =64
# Jobs
Job_Swordsman =1
Job_Assassin =2
Job_Mage =3
Job_Archer =4
Job_Gunner =5
# IRC flags
IRC_AUTH_NONE =0
IRC_AUTH_USER =1
IRC_AUTH_NICK =2
IRC_AUTH_CHAN =3
# Smart Print command
def stdout(message):
if debug:
if renpy.android:
if not renpy.is_init_phase():
renpy.notify(message)
else:
print(message)
renpy.write_log("[DEBUG] %s" % message)
else:
renpy.write_log("[GAME] %s" % message)
return
# Smart wait
def sdelay(delta=0.02):
try:
renpy.pause(delta, hard=True)
except:
time.sleep(delta)
return
# IF Then Else (IFTE)
def ifte(ifs, then, elses):
if (ifs):
return then
else:
return elses
# Global classes
# We need to override standard list method. Original by Triptych (stackoverflow)
class dlist(list):
def __setitem__(self, index, value):
size = len(self)
if index >= size:
self.extend(None for _ in range(size, index + 1))
list.__setitem__(self, index, value)
class ExecuteOnCall():
def __init__(self, callable, *args, **kwargs):
self.callable = callable
self.args = args
self.kwargs = kwargs
def __call__(self):
rv = self.callable(*self.args, **self.kwargs)
return rv
def id(self):
return self.__call__()
class RetString():
def __init__(self, string):
self.string = string
def __call__(self):
return self.string
def id(self):
return self.__call__()
# Screen Functions/class
# Override
class SpheresMainMenu(MainMenu):
def __call__(self):
if not self.get_sensitive():
return
if self.confirm:
if config.autosave_on_quit:
renpy.force_autosave()
layout.yesno_screen(layout.MAIN_MENU, SpheresMainMenu(False))
else:
# Flush labels/sockets/timers as needed
renpy.call_in_new_context("quit")
# Restart
renpy.full_restart(config.game_main_transition)
class ExtraImage(renpy.display.im.Image):
"""
Custom image manipulator, based on bink's code, topic 11732
"""
def __init__(self, loc, **properties):
"""
@param loc: Where the image really is (get_path already applied)
"""
super(ExtraImage, self).__init__(loc, **properties)
self.loc = loc
def load(self, unscaled=False): # W0221
try:
#page = open(self.loc, "rb")
#pd = page.read()
#picdata = os.tmpfile()
#picdata.write(pd)
#picdata.seek(0) # reset seek position
stdout("Requested to open: %s" % repr(self.loc))
picdata = open(self.loc, "rb")
stdout("Picdata is open (%s)" % self.loc)
if unscaled:
surf = renpy.display.pgrender.load_image_unscaled(picdata, self.loc)
else:
surf = renpy.display.pgrender.load_image(picdata, self.loc)
stdout("Picdata was closed")
picdata.close()
#page.close()
return surf
except Exception, e:
if renpy.config.missing_image_callback:
im = renpy.config.missing_image_callback(self.loc)
if im is None:
raise e
return im.load()
raise
def virtpos(posix):
if isinstance(posix, float):
return posix*1024
else:
return posix/1024.0
# File Managment Functions
def get_path(path):
if True or renpy.android:
path=path.replace("/", "_")
#return renpy.loader.get_path(path)
return renpy.config.savedir + "/" + path
else:
return renpy.loader.get_path(path)
def get_path_if_exists(path):
if True or renpy.android:
path=path.replace("/", "_")
#return renpy.loader.get_path(path)
return renpy.config.savedir + "/" + path
else:
return renpy.loader.transfn(path)
# Override music audio loader
def SpheresLoadAudioFile(fn):
"""
Returns a file-like object for the given filename.
"""
try:
fna = get_path_if_exists(fn)
if not os.path.isfile(fna):
raise Exception("Not a file")
rv = renpy.loader.open_file(fna, "rb")
except:
rv = renpy.loader.load(fn)
return rv
renpy.audio.audio.load=SpheresLoadAudioFile
#############################################################################
# URL3 Function
def GAME_UPDATER():
global tr_uptodate
tr_uptodate=False
# If no version is provided, we are using default files
# Default files version is "1" (Should never happen)
if (persistent.version is None):
persistent.version=1
# Download upstream version
x=requests.get("http://"+HOST+'/version.txt')
try:
ver=int(x.text)
except:
stdout("IMPOSSIBLE TO DETERMINE VERSION")
raise Exception("Could not estabilish a connection to update server:\n%s is not valid." % x.text) # TODO: Show this beautifully?
# TODO: Should we set a "ver"?
if (int(persistent.version) < ver):
# TODO: Check if the server have SSL support
stdout("Downloading updated game data...")
# Download quests.json
f=open(get_path("quests.json"), "w")
stdout("Downloading quests.json")
x=requests.get("http://"+HOST+'/quests.json')
f.write(x.text)
f.close()
# Download units.json
f=open(get_path("units.json"), "w")
stdout("Downloading units.json")
x=requests.get("http://"+HOST+'/units.json')
f.write(x.text)
f.close()
# Download story.json
f=open(get_path("story.json"), "w")
stdout("Downloading story.json")
x=requests.get("http://"+HOST+'/story.json')
f.write(x.text)
f.close()
# Download world.json
f=open(get_path("world.json"), "w")
stdout("Downloading world.json")
x=requests.get("http://"+HOST+'/world.json')
f.write(x.text)
f.close()
# Download bar.json
f=open(get_path("bar.json"), "w")
stdout("Downloading bar.json")
x=requests.get("http://"+HOST+'/bar.json')
f.write(x.text)
f.close()
# Download summons.json
f=open(get_path("summons.json"), "w")
stdout("Downloading summons.json")
x=requests.get("http://"+HOST+'/summons.json')
f.write(x.text)
f.close()
persistent.version=ver
stdout("Update complete")
# Download server news
# Handled by GAME_LOADER
tr_uptodate=True
return tr_load
|