def requestHandlerLottery(request, sessid): global _lottery_seed user = storage.get_user_by_sessid(sessid) if not user: raise RuntimeError("invalid session key") seed = (int(time.time()) - 1521849600) // (60 * 60 * 3) seed *= 8 seed += 11 _lottery_seed = seed def rnd(): global _lottery_seed _lottery_seed += 1 x = math.sin(_lottery_seed) * 10000 return x - math.floor(x) alpha = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ') random.shuffle(alpha, rnd) alpha = alpha[0:5] matches = [1 if x in user['username'].upper() else 0 for x in alpha] letter_str = bytes.ljust(util.to_enhanced_charset(', '.join(alpha)), 20) match_count = sum(matches) type_str = bytes.ljust( util.to_enhanced_charset( ['bepis', 'single', 'double', 'triple', 'quadruple', 'quintuple'][match_count]), 10) for i in range(1, match_count + 1): storage.push_event(user['id'], storage.EVT_LOTTERY, i) return b''.join((bytes([match_count]), letter_str, type_str, bytes([random.randrange(0, 255)])))
def requestHandlerSyncAndPrepareMap(request, sessid): user = storage.get_user_by_sessid(sessid) if not user: raise RuntimeError("invalid session key") map_id = request[41] + request[42] * 256 map_name = "MYSTERY ZONE" if os.path.isfile(util.get_map_filename(map_id)): if map_id in config.MAP_NAMES: map_name = config.MAP_NAMES[map_id] else: map_name = "bepis" else: map_id = 0xFFFF map_name = bytes.ljust(util.to_standard_charset(map_name) + b'\x50', 20) storage.check_map_load_allowed(user['id'], map_id) inventory = request[0:41] if storage.is_inventory_valid(inventory): storage.save_player_inventory(user['id'], inventory) else: inventory = storage.load_player_inventory(user['id']) storage.push_event(user['id'], storage.EVT_MAPVISITED, map_id) bitfields = storage.generate_event_bitfields(user['id']) return list(map_name) + bitfields + inventory + [user['fun']]
def requestCrackerCavernChallenge(request, sessid): user = storage.get_user_by_sessid(sessid) if not user: raise RuntimeError("invalid session key") out = b"This is not the message I'm looking for..." if request[0] == 0x13 and request[1] == 0x37 and request[2] == 0xcc: storage.push_event(user['id'], storage.EVT_FLAGTRIGGERED, 101) out = b"Congratulations, you completed the challenge!" return list(out)
def requestHandlerBalls(request, sessid): user = storage.get_user_by_sessid(sessid) if not user: raise RuntimeError("invalid session key") series = len(storage.get_events(user['id'], storage.EVT_GAVEBALLS)) + 1 if series > 9: raise RuntimeError("this is quite a lot of balls") storage.push_event(user['id'], storage.EVT_GAVEBALLS, series) return [series, random.randrange(0, 255), random.randrange(0, 255)]
def requestHandlerCrackme(request, sessid): user = storage.get_user_by_sessid(sessid) if not user: raise RuntimeError("invalid session key") password = util.from_standard_charset(bytes(request)) success_byte = 0 if password == b'6830672995': storage.push_event(user['id'], storage.EVT_FLAGTRIGGERED, 100) success_byte = 1 return [success_byte, random.randrange(0, 255), random.randrange(0, 255)]
def appApiRegister(environ, start_response): data_len = int(environ.get('CONTENT_LENGTH', 0)) data = urllib.parse.parse_qs(environ['wsgi.input'].read(data_len).decode('utf-8'), keep_blank_values=True) user = storage.get_user_data(data['username'][0]) if user: start_response('200 OK', HEADERS_JSON + HEADERS_CORS) return [util.json_bytes({'error': 2, 'message': 'Username already exists.'})] if not (data['username'][0].strip() and data['password'][0].strip()): start_response('200 OK', HEADERS_JSON + HEADERS_CORS) return [util.json_bytes({'error': 3, 'message': 'This field must not be empty.'})] if not re.match('^[A-Za-z0-9]{1,15}$', data['username'][0]): start_response('200 OK', HEADERS_JSON + HEADERS_CORS) return [util.json_bytes({'error': 4, 'message': 'Invalid username.'})] if len(data['message'][0]) > 120: start_response('200 OK', HEADERS_JSON + HEADERS_CORS) return [util.json_bytes({'error': 5, 'message': 'Message too long.'})] storage.sql(''' INSERT INTO users (username, password, message, fun) VALUES (?, ?, ?, ?) ''', ( data['username'][0], util.password_hash(data['password'][0]), data['message'][0], random.randrange(0,255) )) user = storage.get_user_data(data['username'][0]) storage.push_event(user.id, storage.EVT_REGISTERED, 0) storage.sql(''' INSERT INTO leaderboard (user_id, points, achievements) VALUES (?, 0, '{}') ''', (user['id'],)) key = util.new_session_key() storage.sql(''' UPDATE users SET session=? WHERE id=? ''', (key, user['id'])) start_response('200 OK', HEADERS_JSON + HEADERS_CORS) return [util.json_bytes({'error': 0, 'message': 'OK'})]
def requestHandlerTriggerEvent(request, sessid): user = storage.get_user_by_sessid(sessid) if not user: raise RuntimeError("invalid session key") evt = request[0] evt += request[1] * 256 evt += request[2] * 256 * 256 evt += request[3] * 256 * 256 * 256 result = 0 if evt in config.EVENT_FLAGS: result = config.EVENT_FLAGS[evt] storage.push_event(user['id'], storage.EVT_FLAGTRIGGERED, result) inventory = request[4:] if storage.is_inventory_valid(inventory): storage.save_player_inventory(user['id'], inventory) return [result, random.randrange(0, 255), random.randrange(0, 255)]