summaryrefslogtreecommitdiff
path: root/game/04_init.rpy
blob: 7117dcc405d296fbfef9140f3a8e7bf6f9c6e099 (plain) (blame)
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
########################################################################################
#     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: WebSocket 

init python:
    # FIXME: Drop dead if session_id mismatches (new "token")
    # REMINDER: onmsg() runs within the thread, not the main app!
    def onmsg(self, message):
        global tr_load, tr_val, tr_busy
        stdout("Server : " + str(message))

        # Wait wait, it might have been a SERVNOTICE!
        # TODO: Validate the ServNotice with token or something
        try:
         if (message.split(':')[0] == "NOTICE"):
            display_msgbox(str(":".join(message.split(':')[1:])))
            return
        except:
            stdout("Error displaying SERVNOTICE!!")
            return

        # It could also have been an APREFRESH token!
        try:
         if (message.split(':')[0] == "APREFRESH"):
            Player["ap"]=(int(":".join(message.split(':')[1:])))
            # TODO: Reload timer (DynamicDisplayable(countdown, length=360.0))
            Player["aptime"] = now()
            renpy.restart_interaction() # I hope it does not explodes
            return
        except:
            stdout("Error enacting APREFRESH!!")
            return

        # Inform whatever is waiting for us that a reply was obtained
        tr_load=True
        tr_val=str(message)
        # We do not clean tr_busy here
        return

    def onopen(self):
        global tr_busy, tr_load, tr_val, HOST
        print("Opening connection to %s" % HOST)
        tr_load=True
        tr_val=""
        tr_busy=False
        send_packet_now("PING")

    def onerror(self, err):
        global TERMINATE
        if err == "Quit":
            return

        stdout("ERROR RECEIVED")
        stdout("More details: %s" % repr(err))
        stdout("An error happened: %s" % str(err))
        # NOTE: Game might die suddenly... In case it is already exiting...
        try:
            renpy.call_screen("msgbox",
            #                  "An unrecoverable error happened.\nPlease close and re-open the app.")
                              "An unrecoverable error happened.\nPress OK to return to main menu screen.")
        except:
            pass
        TERMINATE=True # Relay a TERM to overlay
        return

    class GameClient(WebSocketClient):
        def opened(self):
            onopen(self)

        # FIXME This is also onerror
        def closed(self, code, reason=None):
            print("Closed down", code, reason)
            if reason is None:
                reason="Connection died"
            if reason != "Quit":
                onerror(self, "%s" % str(reason))

        def received_message(self, m):
            onmsg(self, str(m))

    # Be mindful of where/when using this function
    # Or "onmsg" may accidentally not be cast =/
    def send_packet_now(packet, args=""):
        global ws, tr_load, tr_val, tr_busy, TERMINATE, CLOSING

        stdout("Sending: %s" % packet)
        try:
            ws.send(get_token() + ";" + packet + ";" + args)
        except:
            traceback.print_exc()
            stdout("FATAL ERROR, packet was not sent!")
            try:
                renpy.call_screen("msgbox",
                                  "An unrecoverable error happened.\nPlease close and re-open the app.")
            except:
                pass
            TERMINATE=True
            if not CLOSING:
                renpy.quit(relaunch=True)
        return


    def wait_packet(fg=True):
        global tr_load, tr_val, tr_busy
        timeout=0.0
        print("Now waiting for packet!")

        while not tr_load:
            if fg:
                sdelay() # FIXME: This can cause errors in mobile?
            else:
                time.sleep(0.02)
            timeout+=0.02

            if timeout >= TIMEOUT_INTERVAL:
                # FIXME: What if a screen is already being displayed? BUG
                try:
                    renpy.call_screen("msgbox", "Error Code: %d\n\nApplication timeout, click to try again" % (ERR_TIMEOUT))
                    timeout=0.0
                    # TODO: Maybe we should, like, _resent_ the packet?
                    # Or just allow user to be booted to main menu?
                    # Or even yet, send ERR_INVALID and let the game handle it?
                    # Sooo use confirm instead of msgbox?
                except:
                    stdout("ERROR: Timeout and retry failure")
                    break

        val=tr_val
        tr_busy=False

        stdout("Packet received successfully.")
        #print("value obtained: %s" % str(val))
        if (val is None):
            return ERR_INVALID
        return val


    def schedule_packet():
        global tr_load, tr_val, tr_busy
        # TODO: if tr_busy already true, wait until it is made false
        while tr_busy:
            sdelay()

        # Book processing space for ourselves
        tr_busy=True
        tr_load=False
        tr_val=None
        return


    def send_packet(packet, args="", fg=True):
        global tr_busy, tr_load
        schedule_packet()
        send_packet_now(packet, args)
        return wait_packet(fg)

    # In past, this would keep running, in hopes of catching the program quit
    # and being able to kill the threads... But well, it worked poorly.
    # Still called "supervisor" but all it does now is init.
    # sslopt={"cert_reqs": CERT_NONE})
    def supervisor(use_ssl):
        global ws
        stdout(_("Opening new socket..."))
        done=False
        while not done:
            try:
                if use_ssl:
                    ws = GameClient("wss://"+HOST+":61000")
                else:
                    ws = GameClient("ws://"+HOST+":61000")
                ws.connect()
                # May be problematic.
                # Specially if exception is uncaught
                renpy.invoke_in_thread(ws.run_forever)
                done=True
                stdout("Connection established!")
            except:
                traceback.print_exc()
                stdout("CONNECTION FAILED, PLEASE TRY AGAIN")
                display_msgbox("Connection Error. Please check internet connection.")
                sdelay(10.0) # It will keep trying again, ever and ever.
                # FIXME: sdelay(), with fixed time is a bad idea.
                # We should allow user to retry (no hard pause)
                # And we should scale up the time between attempts
                # Just in case server is flooded, we do not want to DoS it =/
                # (And incur in firewall's wrath while at that!)

        # The supervisor module is now uneeded as thread was cast, so return
        return