Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 245 lines (168 sloc) 6.361 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
from __future__ import division, print_function

import constants
import struct

clientevents = {}
serverevents = {}

# decorators to register classes as events
def clientevent(cls):
    clientevents[cls.eventid] = cls
    return cls

def serverevent(cls):
    serverevents[cls.eventid] = cls
    return cls

@serverevent
class ServerEventPlayerJoin(object):
    eventid = constants.EVENT_PLAYER_JOIN

    def __init__(self, id, name):
        self.id = id
        self.name = name

    def pack(self):
        return struct.pack(">H32p", self.id, self.name)

    def unpack(self, packetstr):
        self.id, self.name = struct.unpack_from(">H32p", packetstr)

        return struct.calcsize(">H32p")

@clientevent
class ClientEventHello(object):
    eventid = constants.EVENT_HELLO

    def __init__(self, name, password):
        self.name = name
        self.password = password

    def pack(self):
        return struct.pack(">32p32p", self.name, self.password)

    def unpack(self, packetstr):
        self.name, self.password = struct.unpack_from(">32p32p", packetstr)
        return struct.calcsize(">32p32p")

@serverevent
class ServerEventHello(object):
    eventid = constants.EVENT_HELLO

    def __init__(self, servername, playerid, maxplayers, mapname, version):
        self.servername = servername
        self.playerid = playerid
        self.maxplayers = maxplayers
        self.mapname = mapname
        self.version = version

    def pack(self):
        packetstr = struct.pack(">32pBB64pH", self.servername, self.playerid, self.maxplayers, self.mapname, self.version)

        return packetstr

    def unpack(self, packetstr):
        self.servername, self.playerid, self.maxplayers, self.mapname, self.version = struct.unpack_from(">32pBB64pH", packetstr)

        return struct.calcsize(">32pBB64pH")

@clientevent
class ClientEventJump(object):
    eventid = constants.EVENT_JUMP

    def __init__(self, time):
        self.time = time

    def pack(self):
        packetstr = struct.pack(">I", time)

        return packetstr

    def unpack(self, packetstr):
        self.time = struct.unpack(">I", packetstr)[0]

        return struct.calcsize(">I")

@serverevent
class ServerEventChangeclass(object):
    eventid = constants.EVENT_PLAYER_CHANGECLASS

    def __init__(self, playerid, newclass):
        self.playerid = playerid
        self.newclass = newclass

    def pack(self):
        packetstr = struct.pack(">HB", self.playerid, self.newclass)

        return packetstr

    def unpack(self, packetstr):
        self.playerid, self.newclass = struct.unpack_from(">HB", packetstr)

        return struct.calcsize(">HB")

@clientevent
class ClientEventChangeclass(object):
    eventid = constants.EVENT_PLAYER_CHANGECLASS

    def __init__(self, newclass):
        self.newclass = newclass

    def pack(self):
        packetstr = struct.pack(">B", self.newclass)

        return packetstr

    def unpack(self, packetstr):
        self.newclass = struct.unpack_from(">B", packetstr)[0]

        return struct.calcsize(">B")

@serverevent
class ServerEventSpawn(object):
    eventid = constants.EVENT_PLAYER_SPAWN

    def __init__(self, playerid, x, y):
        self.playerid = playerid
        self.x = x
        self.y = y

    def pack(self):
        packetstr = struct.pack(">BII", self.playerid, self.x, self.y)

        return packetstr

    def unpack(self, packetstr):
        self.playerid, self.x, self.y = struct.unpack_from(">BII", packetstr)

        return struct.calcsize(">BII")

@serverevent
class ServerEventDie(object):
    eventid = constants.EVENT_PLAYER_DIE

    def __init__(self, playerid):
        self.playerid = playerid

    def pack(self):
        packetstr = struct.pack(">B", self.playerid)

        return packetstr

    def unpack(self, packetstr):
        self.playerid = struct.unpack_from(">B", packetstr)[0]

        return struct.calcsize(">B")

@clientevent
class ClientEventInputstate(object):
    eventid = constants.INPUTSTATE

    def __init__(self, bytestr):
        self.bytestr = bytestr

    def pack(self):
        packetstr = struct.pack(">H", len(self.bytestr)) # TODO: Implement a better system that doesn't require this length, because it shouldn't be needed.
        packetstr += self.bytestr

        return packetstr

    def unpack(self, packetstr):
        length = struct.unpack_from(">H", packetstr)[0]
        packetstr = packetstr[2:]
        self.bytestr = packetstr[:length]

        return struct.calcsize(">H")+length

@serverevent
class ServerEventSnapshotUpdate(object):
    eventid = constants.SNAPSHOT_UPDATE

    def __init__(self, bytestr):
        self.bytestr = bytestr

    def pack(self):
        packetstr = struct.pack(">H", len(self.bytestr)) # TODO: Implement a better system that doesn't require this length, because it shouldn't be needed.
        packetstr += self.bytestr

        return packetstr

    def unpack(self, packetstr):
        length = struct.unpack_from(">H", packetstr)[0]
        packetstr = packetstr[2:]
        self.bytestr = packetstr[:length]

        return struct.calcsize(">H")+length

@serverevent
class ServerEventFullUpdate(object):
    eventid = constants.FULL_UPDATE

    def __init__(self, bytestr):
        self.bytestr = bytestr

    def pack(self):
        packetstr = struct.pack(">H", len(self.bytestr)) # TODO: Implement a better system that doesn't require this length, because it shouldn't be needed.
        packetstr += self.bytestr

        return packetstr

    def unpack(self, packetstr):
        length = struct.unpack_from(">H", packetstr)[0]
        packetstr = packetstr[2:]
        self.bytestr = packetstr[:length]
        packetstr = packetstr[length:]

        return struct.calcsize(">H")+length

@clientevent
class ClientEventDisconnect(object):
    eventid = constants.EVENT_PLAYER_DISCONNECT

    def pack(self):
        return ""

    def unpack(self, packetstr):
        return 0

@serverevent
class ServerEventDisconnect(object):
    eventid = constants.EVENT_PLAYER_DISCONNECT

    def __init__(self, playerid):
        self.playerid = playerid

    def pack(self):
        packetstr = struct.pack(">B", self.playerid)

        return packetstr

    def unpack(self, packetstr):
        self.playerid = struct.unpack_from(">B", packetstr)[0]

        return struct.calcsize(">B")
Something went wrong with that request. Please try again.