[GAME MODE] Tournament

Incomplete code that isn't ready for use.
6 posts Page 1 of 1 First unread post
thepolm3
Scripter
Scripter
Posts: 424
Joined: Sat Feb 16, 2013 10:49 pm


yeah yeah, blah di blah. Here's the code:
Code: Select all

"""
Tournament mode by thepolm3
Idea by TB_
""" 
from commands import add, admin, get_player, join_arguments, name, alias, get_team
from pyspades.constants import *
from random import randint
from twisted.internet.reactor import callLater

HIDE_COORD = (0, 0, 63)
HIDE_COORD2 = (511, 511, 63)
BUILDING_ENABLED = False
MAX_SPAWN_DISTANCE=5
def apply_script(protocol,connection,config):
    class TournamentConnection(connection):
        tournament_id=None
        myroundended=False
        opponant=None
        
        def on_spawn_location(self, pos):
            if self.protocol.tournament_enabled:
                prot=self.protocol
                if self.tournament_id%2==1:
                    callLater(1,self.match)
                else:
                    self.send_chat("Please wait until an opponant arrives")
                if self.myroundended and prot.round_active:
                    self.opponant=prot.tournament_players[self.tournament_id-1]
                    self.opponant.opponant=self
                    self.send_chat("You have been matched against %s Fight to the death!" %(self.opponant.name))
                    self.opponant.send_chat("You have been matched against %s Fight to the death!" %(self.name))
                    return HIDE_COORD2
                else:
                    if not prot.round_active:
                        self.myroundended=False
                    x,y,z = prot.spawns[self.tournament_id]
                    return(x,y,z)
            return connection.on_spawnlocation(self,pos)
                
                    
        def on_kill(self, killer, type, grenade):
            if self.protocol.tournament_enabled:
                if killer!=None and killer!=self:
                    if self.opponant==killer and self.protocol.round_active and not self.myroundended:
                        killer.send_chat("You won this round!")
                        self.myroundended=True
                        killer.myroundended=True
                        self.send_chat("Sorry, you died. wait until the end of the round to play again")
                        killer.kill()
                        self.protocol.check_end()
                        self.switch_team()
                        a=callLater(0.01,self.switch_team)
                        return connection.on_kill(self,killer,type,grenade)
                    else:
                        if killer.opponant==None:
                            killer.send_chat("Wait for your opponant to arrive!")
                        elif not self.protocol.round_active:
                            killer.send_chat("not yet!")
                        else:
                            killer.send_chat("Shoot %s, not %s!" %(killer.opponant.name,self.name))
                        return False
            return connection.on_kill(self,killer,type,grenade)

        def switch_team(self):
            self.set_team(self.team.other)
            self.spawn()

        def on_position_update(self):
            prot=self.protocol
            if prot.tournament_enabled:
                if self.opponant==None or not prot.round_active:
                    pos = self.world_object.position
                    x,y,z=prot.spawns[self.tournament_id]
                    xd = x - pos.x
                    yd = y - pos.y
                    zd = z - pos.z
                    if (xd ** 2 + yd ** 2 + zd ** 2)**0.5 > MAX_SPAWN_DISTANCE:
                        self.set_location(prot.spawns[self.tournament_id])
                    if self.opponant==None:
                        self.send_chat("Wait for your opponant to arrive!")
            return connection.on_position_update(self)

        def on_connect(self):
            prot=self.protocol
            if prot.tournament_enabled:
                get_team(self, "green").locked = True
                self.tournament_id = len(prot.tournament_players)
                prot.tournament_players.append(self)
            return connection.on_connect(self)

        def on_disconnect(self):
            prot=self.protocol
            if prot.tournament_enabled:
                self.myroundended=False
                del(prot.tournament_players[self.tournament_id])
                self.tournament_id=None
                if self.opponant:
                    self.opponant.send_chat("%s has disconnected and forfeight. Prove yourself next game!"%(self.name))
                self.opponant.opponant=None
                self.opponant=None
            return connection.on_disconnect(self)

    class TournamentProtocol(protocol):
        game_mode=CTF_MODE
        tournament_enabled = False
        spawns=[]
        olds=[]
        tournament_players=[]
        round_active=False
        gamesfinished=0

        def on_map_change(self, map):
            extensions = self.map_info.extensions
            if config.get("game_mode","ctf")=="tournament" or extensions.has_key('tournament'):
                self.tournament_enabled = True
                self.olds = [self.respawn_time,self.building,self.friendly_fire,self.max_players]
                self.respawn_time = 0
                self.building = BUILDING_ENABLED
                self.friendly_fire = True
                if extensions.has_key('tournament'):
                    try:
                        for i in extensions["tournament"]: #setting spawns
                            self.spawns.append(i)
                        self.max_players=len(self.spawns)
                    except Exception:
                        raise "Tournament defined incorrectly in map meta data, use format tournament:[(spawn1),(spawn2),(spawn3)] etc"
                else:
                    raise Exception("No extentions")
                self.send_chat("The round will begin in 10 seconds")
                a=callLater(10,self.begin_round)
            else:
                # Cleanup after a map change
                self.tournament_enabled=False
                if self.olds:
                    self.respawn_time,self.building,self.friendly_fire,self.max_players=self.olds
                self.olds=[]
            return protocol.on_map_change(self,map)

        def check_end(self):
            self.gamesfinished+=1
            if self.gamesfinished>=int(len(self.tournament_players)/2):
                self.end_round()
                self.send_chat("Round finished")
                
        def end_round(self):
            self.gamesfinished=0
            tournament_players=self.tournament_players
            length = len(tournament_players) - 1
            sorted = False
            while not sorted:
                sorted = True
                for i in range(length):
                    if tournament_players[i].kills > tournament_players[i+1].kills:
                        sorted = False
                        tournament_players[i], tournament_players[i+1] = tournament_players[i+1], tournament_players[i]
            for player in tournament_players:
                player.tournament_id=tournament_players.index(player)
                player.kill()
                player.myroundended=False
                player.opponant=None
            self.send_chat("The round will begin in 5 seconds")
            self.round_active=False
            a=callLater(5,self.begin_round)

        def begin_round(self):
            if len(self.tournament_players)>1:
                self.round_active=True
                self.send_chat("Begin killing!")
            else:
                self.send_chat("Not enough players. The round will begin in 10 seconds")
                a=callLater(10,self.begin_round)

        def on_flag_spawn(self, x, y, z, flag, entity_id):
            if not self.tournament_enabled:
                return protocol.on_base_spawn(self, x, y, z, flag, entity_id)
            return HIDE_COORD

        def on_base_spawn(self, x, y, z, base, entity_id):
            if not self.tournament_enabled:
                return protocol.on_base_spawn(self, x, y, z, base, entity_id)
            return HIDE_COORD
        
    return TournamentProtocol, TournamentConnection

From my coding experience, I think this will not work fully. This is STILL IN DEVELOPMENT please report any bugs, have fun and say Hi to TB_ for me!
Bux Xray
3 Years of Ace of Spades
3 Years of Ace of Spades
Posts: 218
Joined: Sat Aug 24, 2013 9:07 am


umm..gimme some more infos pls. :)
thepolm3
Scripter
Scripter
Posts: 424
Joined: Sat Feb 16, 2013 10:49 pm


Pitches players together in a fight to the death. Winners then fight each other etc until 1 remains
Bux Xray
3 Years of Ace of Spades
3 Years of Ace of Spades
Posts: 218
Joined: Sat Aug 24, 2013 9:07 am


oh thx, I might use this :)
Bux Xray
3 Years of Ace of Spades
3 Years of Ace of Spades
Posts: 218
Joined: Sat Aug 24, 2013 9:07 am


tb of supercool? i knew him
Teeth
League Participant
League Participant
Posts: 914
Joined: Tue Nov 26, 2013 8:31 am


Bux Xray wrote:
tb of supercool? i knew him
Fair enough then. Try to stay on topic please.

Good job on this, Thepolm. I hope to see some server hosters trying to use this.
6 posts Page 1 of 1 First unread post
Return to “Work In Progress”

Who is online

Users browsing this forum: No registered users and 11 guests