def userCheck(): for grid in Grid.all(): for uid in grid.getUsers(): if uid not in UpdateManager.clients: user = User(uid) UpdateManager.delClient(user) grid.delUser(user)
def sendMessage(handler, **args): UpdateManager.sendGrid(Grid(handler.user['grid']), "newMessage", handler.user, pid = handler.user['pid'], text = args['text'] ) return { "status": 200 }
def post(self): name = self.get_argument("name", None) size = self.get_argument("size", None) mapfile = "default" if name is None or size is None: return jsonify(self, status=406, error = "name") try: int(size) except ValueError: return jsonify(self, status=406, error = "size") if size not in ['16', '32', '64']: return jsonify(self, status=404) # Make sure the name doesn't already exist if Grid.fromName(name).exists(): return jsonify(self, status=406, error = "Name taken") status, g = Grid.create(name, size, "default") if status == False: return jsonify(self, status=406, error=g) # Send to nogrids UpdateManager.sendNogrids("newGrid", gid = g['id'], size = size, name = g['name'], players = 1, ) return jsonify(self, status=200, gid = g['id'])
def add_house(grid, coord, player): new_tlim = int(player['tlim']) + 4 if new_tlim > int(grid['tlim']): new_tlim = int(grid['tlim']) player['tlim'] = new_tlim UpdateManager.sendClient(player.getUser(), "setTerritory", tlim = player['tlim'], tused = player['tused']) return True
def dest_miner(grid, coord, player): mines = grid.inRangeOf(coord, 99, 1) if mines == 0: return income = mines * 5 total = player.addIncome(-income) UpdateManager.sendClient(player.getUser(), "setInc", inc = total)
def add_defender(grid, coord, player): # Rename the defending coord db.rename(coord.dbid, "def:" + coord.dbid) coord['type'] = 8 coord['health'] = 25 coord['player'] = player['pid'] db.zadd(grid.dbid + ":def", str(coord), int(time())) UpdateManager.sendCoord(grid, coord) return True
def joinGrid(handler, **args): try: #TODO: Sanity checks gid = args['gid'] except KeyError: return {"status": 406, "error": "no gid"} pid = None if "pid" in args: pid = args['pid'] g = Grid(gid) if g.exists() is False: return {"status":404, "error":"Grid not found."} # Add the user to the grid/UpdateManager pid = g.addUser(handler.user, pid) if pid is False: return { "status":406, "error": "Grid is full" } handler.user['pid'] = pid handler.user['grid'] = gid handler.user['active'] = True player = handler.user.getPlayer() # Looks like it's a new player, go ahead and init them if g.playerExists(pid) is False: player['cash'] = g['init_cash'] # Starting cash value player['inc'] = 0 player['lastInc'] = int(time()) player['tused'] = g['init_tused'] player['tlim'] = g['init_tlim'] updated = g.loadEvent("join_%s" % pid) # Add their new coords for coord in updated: UpdateManager.sendCoord(g, coord, handler.user) # Announce our color to all other clients UpdateManager.sendGrid(g, "addPlayer", handler.user) return { "status":200, "uid": handler.user['id'], "pid": pid, "cash": player['cash'], "inc": player['inc'], "tused": player['tused'], "tlim": player['tlim'], "colors": g.getColors(), "color": player['color'], "coords": g.dump() }
def damager(): for grid in Grid.all(): length = db.llen(grid.dbid + ":dam") # List of tiles that can be damaged takes_damage = [2, 3, 4, 5, 6, 7] for i in range(0, length): c = grid.get(db.lindex(grid.dbid + ":dam", i)) around = grid.around(c, 0, 1) for coord in around: # Make sure we can damage it if (int(coord['type']) not in takes_damage) or (c['player'] == coord['player']): continue before = coord['type'] coord.damage(10) if coord['type'] == before: UpdateManager.sendGrid(grid, "setHealth", coord=str(coord), health=coord['health']) else: UpdateManager.sendGrid(grid, "set", coord=str(coord), tile = 1, player = coord['player'], health=25) # Damage/delete the damager c.damage(10) if int(c['type']) == 6: UpdateManager.sendGrid(grid, "setHealth", coord=str(c), health=c['health']) else: db.lrem(grid.dbid + ":dam", str(c), 0) UpdateManager.sendCoord(grid, c)
def payDay(): for grid in Grid.all(): for u in grid.getPlayers(): inc = int(u['inc']) last = time() - float(u['lastInc']) if not u.getUser()['active']: continue # Check their interval if inc == 0: continue if inc * 100 < int(u['cash']): continue if last < math.log(inc) / 2: continue u.addCash(inc) u['lastInc'] = time() UpdateManager.sendClient(u.getUser(), "setCash", cash = u['cash'])
def infector(): for grid in Grid.all(): dbid = "g:%s:inf" % grid['id'] # Get the most recently placed infector try: latest = db.zrange(dbid, 0, 1)[0] except IndexError: continue l_time = db.zrank(dbid, latest) if (int(time()) - int(l_time)) < 3: # The most recent has been sittig for under 3 seconds, # so we're done here continue # Looks like we can't take that shortcut. Let's loop. coords = db.zrange(dbid, 0, -1) for infector in coords: c = grid.get(infector) if int(time()) - int(db.zscore(dbid, infector)) < 3: break around = grid.around(c, [1,4], 1, True) for coord in around: if coord['player'] == c['player']: continue if coord['type'] == "4": TileDest[4](grid, coord, grid.getPlayer(coord['player'])) coords.remove(str(coord)) else: TileDest[1](grid, coord, grid.getPlayer(coord['player'])) coord['type'] = 1 coord['player'] = c['player'] UpdateManager.sendCoord(grid, coord) # Delete the infector db.zrem(dbid, str(c)) c['type'] = 1 c['health'] = 25 UpdateManager.sendCoord(grid, c)
# If it's not placeable if placeable is False: return { "status": 412, "coord": coord, "error": "invalid placement" } # Make sure they have enough cash for it if int(player['cash']) < props['price']: return { "status": 412, "coord": coord, "error": "not enough cash" } # Make sure they have enough territory if tile == 1 and int(player['tused']) >= int(player['tlim']): return { "status": 412, "coord": coord, "error": "territory limit" } elif tile == 1: player['tused'] = int(player['tused']) + 1 UpdateManager.sendClient(handler.user, "setTerritory", tused = player['tused'], tlim = player['tlim'] ) # Subtract the cash player.addCash(-props['price']) UpdateManager.sendClient(handler.user, "setCash", cash = player['cash']) c['type'] = tile c['player'] = handler.user['pid'] c['health'] = props['health'] UpdateManager.sendCoord(g, c, handler.user) return { "status": 200 } def sendMessage(handler, **args):
def dest_defender(grid, coord, player): db.zrem(grid.dbid + ":def", str(coord)) db.delete(coord.dbid) db.rename("def:" + coord.dbid, coord.dbid) UpdateManager.sendCoord(grid, coord)
def dest_territory(grid, coord, player): new_tused = int(player['tused']) - 1 player['tused'] = new_tused UpdateManager.sendClient(player.getUser(), "setTerritory", tlim = player['tlim'], tused = new_tused)
def dest_house(grid, coord, player): new_tlim = int(player['tlim']) - 4 player['tlim'] = new_tlim UpdateManager.sendClient(player.getUser(), "setTerritory", tlim = player['tlim'], tused = player['tused'])