Page 1 of 1

[SCRIPT] census

Posted: Mon Oct 21, 2013 11:19 am
by Kuma
Well, This script allows to get the total number of players, server, numbers of players playing on your server, etc. It also gives you the percentage of how many people are playing on your server out of total people playing ace of spades. You MAY or MAY NOT find this script useful. I made this because of me talking with moose on the irc(Yes this is inspired of his so called royal spades)
Code: Select all
#Census by Kuma
#Inspired by server lists developed by many people (Moose)

from commands import add, admin, alias
from json import loads
from urllib2 import urlopen

NUMBER_OF_PLAYERS = "The current number of players on the server: {} ({})"
WORLD_POPULATION_IS = "server list population: {} players out of {} available slots (Servers: {})"
NOT_CONNECTED_TO_MASTER = "The server is isn't connected to the master server"
REFRESH_DONE = "Refreshed!"

version = '0.75' #change this to '0.76 for 0.76 server list'
url = 'http://services.buildandshoot.com/serverlist.php?version=075' if version == '0.75' else 'http://services.buildandshoot.com/serverlist.php?version=076'
server_list = loads(urlopen(url).read())

@alias('sp')
@admin
def serverpopulation(connection):
    protocol = connection.protocol
    current_players = len(protocol.players)
    if protocol.master:
        if connection in protocol.players:
            connection.send_chat(NUMBER_OF_PLAYERS.format(current_players, percent(current_players, number_of_players_playing(connection, server_list))))
        else:
            protocol.irc_say(NUMBER_OF_PLAYERS.format(current_players, percent(current_players, number_of_players_playing(connection, server_list))))
    else:
        connection.send_chat(NOT_CONNECTED_TO_MASTER) if connection in protocol.players else protocol.irc_say(NOT_CONNECTED_TO_MASTER)

@alias('wp')
def worldpopulation(connection):
    protocol = connection.protocol
    if connection in protocol.players:
        connection.send_chat(WORLD_POPULATION_IS.format(number_of_players_playing(connection, server_list, c = False), slots(connection, server_list), number_of_servers(connection, url)))
    else:
        protocol.irc_say(WORLD_POPULATION_IS.format(number_of_players_playing(connection, server_list, c = False),slots(connection, server_list),number_of_servers(connection, url)))
   

@admin
def refresh(connection): #It causes lag, don't use it often
    server_list = p_url(url)
    protocol = connection.protocol
    if connection in protocol.players:
        connection.send_chat(REFRESH_DONE)
    else:
        protocol.irc_say(REFRESH_DONE)

add(serverpopulation)
add(worldpopulation)
add(refresh)


#JSON functions-------------------------------
def number_of_players_playing(connection, url, c = True):
    numbers, total_players, protocol = [], 0, connection.protocol
    get_value(url, numbers, "players_current")
    for number in numbers:
        total_players += int(number)
    return total_players - len(protocol.players) if c else total_players

def slots(connection, url):
    numbers, total_slots, protocol = [], 0, connection.protocol
    get_value(url, numbers, "players_max")
    for number in numbers:
        total_slots += int(number)
    return total_slots

def number_of_servers(connection, url):
    return len(server_list)
    

def get_value(list, var, value = "name"):
    for line in list:
        dict_obj = line
        var.append(dict_obj.get(value))

#Returns a percentage
def percent(num1, num2):
    return str(100 * float(num1)/float(num2)) + "%"

#Prepares a string url to json
def p_url(url):
    return loads(urlopen(url).read())

def apply_script(protocol, connection, config):
    return protocol, connection


Commands
Spoiler:
/wp or /worldpopulation - Gives you the current number of people playing and number of slots available, it also gives the number of servers.
/sp or /serverpopulation - Gives the current number of players playing on your server, also compares it with the current number of people playing ace of spades and gives a percentage. (Needs master on)
/refresh - Refreshes the json/dictionary/database of server. (Laggy, don't use it often)
Irc Demo
Spoiler:
It can be used ingame and in the irc
Image

Re: [SCRIPT] cencus

Posted: Mon Oct 21, 2013 11:52 am
by learn_more
would be better if it also supports 076 serverlist, by using the active server version on the query instead of hardcoded 075

Re: [SCRIPT] cencus

Posted: Mon Oct 21, 2013 12:34 pm
by Kuma
learn_more wrote:
would be better if it also supports 076 serverlist, by using the active server version on the query instead of hardcoded 075
Ok next version will have that.

Re: [SCRIPT] cencus

Posted: Thu Oct 24, 2013 3:16 pm
by thepolm3
also a history would be nice. You know, 30% of players online now have visited your server

*census

Re: [SCRIPT] census

Posted: Fri Oct 25, 2013 4:41 pm
by Kuma
lol I spelled census right in the script but wrong in the title. I will probably add census to the logs.(not now)

Just a little update, if you change the version to "0.76" it will load the 0.76 serverlist