Show
Ignore:
Timestamp:
12/03/08 16:04:23 (21 months ago)
Author:
dugwyler
Message:

NPE fix.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/twcore/src/twcore/bots/multibot/dodgeball/dodgeball.java

    r2678 r3071  
    1919 
    2020public class dodgeball extends MultiModule { 
    21          
     21 
    2222        // Dodgeball game parameters 
    2323        private int dodgetime = 3; //secs 
     
    2525        private int orgBallCount = -1; 
    2626        protected boolean running = false; 
    27          
     27 
    2828        private HashMap<Integer,Ball> balls = new HashMap<Integer,Ball>(); 
    2929        //             <ball id,Ball> 
    30          
     30 
    3131        private int ballMode = 0; 
    3232        //                              0  =>  ball not carried 
    3333        //                              1  =>  ball carried 
    34          
     34 
    3535        private HashMap<Short,Integer> players = new HashMap<Short,Integer>(); 
    3636        // List with PlayerIDs of players in the game 
    3737        // <PlayerID,number of eliminations> 
    38          
     38 
    3939        private TimerTask checkWinner; 
    40      
     40 
    4141    private List<String> publicHelp = Arrays.asList(new String[]{ 
    4242            "+-----------------------------------------------------------------+", 
     
    5151            "+-----------------------------------------------------------------+" 
    5252            }); 
    53      
     53 
    5454    private List<String> staffHelp = Arrays.asList(new String[]{ 
    5555            "|   !start                        - Starts a new game             |", 
     
    7070            "+-----------------------------------------------------------------+" 
    7171        }); 
    72      
     72 
    7373    @Override 
    7474    public void requestEvents(ModuleEventRequester eventRequester) { 
     
    8080    @Override 
    8181    public void cancel() { 
    82          
     82 
    8383        // When unloading module, set ball count back to what it was when loading module 
    8484        if(orgBallCount != -1 && orgBallCount != ballCount) { 
     
    9191        List<String> help = new ArrayList<String>(publicHelp); 
    9292        help.addAll(staffHelp); 
    93          
     93 
    9494        // Add status line at bottom of staff's !help menu 
    9595        help.addAll( Arrays.asList( new String[] { 
     
    9797                        "+-----------------------------------------------------------------+" 
    9898        })); 
    99          
     99 
    100100        return help.toArray(new String[]{}); 
    101101    } 
     
    111111        return !running; 
    112112    } 
    113      
     113 
    114114    /* (non-Javadoc) 
    115115     * @see twcore.bots.MultiModule#handleEvent(twcore.core.events.Message) 
     
    117117    @Override 
    118118    public void handleEvent(Message event) { 
    119          
    120         if(ballCount == 0)  
     119 
     120        if(ballCount == 0) 
    121121                return; 
    122          
     122 
    123123        if(event.getMessageType() == Message.PRIVATE_MESSAGE) { 
    124124                String message = event.getMessage(); 
     
    129129                } 
    130130                OperatorList opList = m_botAction.getOperatorList(); 
    131                  
     131 
    132132                // Public commands 
    133133                if(!opList.isER(name)) { 
     
    144144                                        running = true; 
    145145                                        m_botAction.shipResetAll(); 
    146                                          
     146 
    147147                                        // Fill list of players 
    148148                                        for(Player p:m_botAction.getPlayingPlayers()) { 
    149149                                                players.put(p.getPlayerID(), 0); 
    150150                                        } 
    151                                          
     151 
    152152                                } else { 
    153153                                        m_botAction.sendPrivateMessage(playerID, "Dodgeball is already started. PM !stop to stop dodgeball."); 
     
    163163                                } 
    164164                        } 
    165                          
     165 
    166166                        if(message.startsWith("!usage")) { 
    167167                                List<String> usage = Arrays.asList(new String[]{ 
     
    188188                                m_botAction.privateMessageSpam(playerID, usage); 
    189189                        } 
    190                          
     190 
    191191                        if(message.startsWith("!add")) { 
    192192                                String arg1 = message.substring(4).trim(); 
    193                                  
     193 
    194194                                if(!running) { 
    195195                                        m_botAction.sendPrivateMessage(playerID, "The command !add can only be used when the game is started. Command aborted."); 
    196196                                        return; 
    197197                                } 
    198                                  
     198 
    199199                                if(arg1 == null || arg1.length() == 0) { 
    200200                                        m_botAction.sendPrivateMessage(playerID, "Syntax error. Please specify part of the playername to add to the game. Type ::!help for more information."); 
    201201                                        return; 
    202202                                } 
    203                                  
     203 
    204204                                Player selectedPlayer = m_botAction.getFuzzyPlayer(arg1); 
    205                                  
     205 
    206206                                if(selectedPlayer == null) { 
    207207                                        m_botAction.sendPrivateMessage(playerID, "The specified player can't be found in this arena. Please specify part of the playername to add to the game. Type ::!help for more information."); 
     
    212212                                        return; 
    213213                                } 
    214                                  
     214 
    215215                                players.put(selectedPlayer.getPlayerID(), 0); 
    216216                                m_botAction.sendPrivateMessage(playerID, "Player '"+selectedPlayer.getPlayerName()+"' added to the game."); 
    217217                                checkWinner(); 
    218218                        } 
    219                          
     219 
    220220                        if(message.startsWith("!remove")) { 
    221221                                String arg1 = message.substring(7).trim(); 
    222                                  
     222 
    223223                                if(!running) { 
    224224                                        m_botAction.sendPrivateMessage(playerID, "The command !remove can only be used when the game is started. Command aborted."); 
    225225                                        return; 
    226226                                } 
    227                                  
     227 
    228228                                if(arg1 == null || arg1.length() == 0) { 
    229229                                        m_botAction.sendPrivateMessage(playerID, "Syntax error. Please specify part of the playername to remove from the game. Type ::!help for more information."); 
    230230                                        return; 
    231231                                } 
    232                                  
     232 
    233233                                Player selectedPlayer = m_botAction.getFuzzyPlayer(arg1); 
    234                                  
     234 
    235235                                if(selectedPlayer == null) { 
    236236                                        m_botAction.sendPrivateMessage(playerID, "The specified player can't be found in this arena. Use the !remove-id and the !list command to remove a specific player using a player ID."); 
     
    245245                                checkWinner(); 
    246246                        } 
    247                          
     247 
    248248                        if(message.startsWith("!list")) { 
    249249                                if(!running) { 
     
    251251                                        return; 
    252252                                } 
    253                                  
     253 
    254254                                List<String> list = Arrays.asList(new String[]{ 
    255255                                                "ID   Name                       Ship      ", 
    256256                                                "---- -------------------------- ----------" 
    257257                                }); 
    258                                  
     258 
    259259                                for(short id:players.keySet()) { 
    260260                                        Player p = m_botAction.getPlayer(id); 
     
    265265                                                ); 
    266266                                } 
    267                                  
     267 
    268268                                m_botAction.privateMessageSpam(playerID, list); 
    269                                  
    270                         } 
    271                          
     269 
     270                        } 
     271 
    272272                        if(message.startsWith("!dodgetime")) { 
    273273                                String arg1 = message.substring(7).trim(); 
    274274                                int argument = 3; 
    275                                  
     275 
    276276                                if(arg1 == null || arg1.length() == 0) { 
    277277                                        m_botAction.sendPrivateMessage(playerID, "Current dodgetime: "+dodgetime+" seconds."); 
     
    283283                                        return; 
    284284                                } 
    285                                  
     285 
    286286                                argument = Integer.parseInt(arg1); 
    287                                  
     287 
    288288                                if(argument > 60) { 
    289289                                        m_botAction.sendPrivateMessage(playerID, "You can't set a dodge time larger then 60 seconds."); 
    290290                                        return; 
    291291                                } 
    292                                  
     292 
    293293                                dodgetime = argument; 
    294294                                m_botAction.sendPrivateMessage(playerID, "Dodge time set to: "+dodgetime+" seconds."); 
    295295                                m_botAction.sendPrivateMessage(playerID, "If game is already started, will take effect immediatly."); 
    296296                        } 
    297                          
     297 
    298298                        if(message.startsWith("!fixball")) { 
    299299                                // !fixball <x>,<y>              - Fixes all balls on <x>,<y> pos 
    300300                    // !fixball <id>,<x>,<y>         - Fixes ball <id> on <x>,<y> pos 
    301301                                String arg1 = message.substring(8).trim(); 
    302                                  
     302 
    303303                                if(arg1 == null || arg1.length() == 0 || arg1.indexOf(',') == -1) { 
    304304                                        m_botAction.sendPrivateMessage(playerID, "Syntax error. Please specify the <x> and <y> coordinates where to fix all the balls. Type ::!help for more information."); 
    305305                                        return; 
    306306                                } 
    307                                  
     307 
    308308                                String[] args = arg1.split(","); 
    309309                                int id = -1; 
    310310                                int x  = -1; 
    311311                                int y  = -1; 
    312                                  
     312 
    313313                                if(args.length < 2 || args.length > 3 || Tools.isAllDigits(arg1.replace(",", "")) == false) { 
    314314                                        m_botAction.sendPrivateMessage(playerID, "Syntax error. Please specify the <x> and <y> coordinates where to fix all the balls. Type ::!help for more information."); 
    315315                                        return; 
    316316                                } 
    317                                  
     317 
    318318                                if(args.length == 2) { 
    319319                                        x = Integer.parseInt(args[0]); 
    320320                                        y = Integer.parseInt(args[1]); 
    321                                          
     321 
    322322                                        for(int i = 0 ; i < ballCount; i++) { 
    323323                                                m_botAction.sendPrivateMessage(playerID, "Moving ball #"+(i+1)+" to "+x+","+y); 
    324324                                                this.moveBall(i, x, y); 
    325325                                        } 
    326                                          
     326 
    327327                                } else if(args.length == 3) { 
    328328                                        id = Integer.parseInt(args[0]); 
    329329                                        x = Integer.parseInt(args[1]); 
    330330                                        y = Integer.parseInt(args[2]); 
    331                                          
     331 
    332332                                        if(id > 0) id--; 
    333333                                        m_botAction.sendPrivateMessage(playerID, "Moving ball #"+(id+1)+" to "+x+","+y); 
     
    335335                                } 
    336336                        } 
    337                          
     337 
    338338                        if(message.startsWith("!setballcount ")) { 
    339339                                String arg1 = message.substring(13).trim(); 
    340340                                int argument = 1; 
    341                                  
     341 
    342342                                if(arg1 == null || arg1.length() == 0) { 
    343343                                        m_botAction.sendPrivateMessage(playerID, "Syntax error. Please specify number of balls to set. Type ::!help for more information"); 
     
    348348                                        return; 
    349349                                } 
    350                                  
     350 
    351351                                argument = Integer.parseInt(arg1); 
    352                                  
     352 
    353353                                if(argument < 1 || argument > 8) { 
    354354                                        m_botAction.sendPrivateMessage(playerID, "You can't set the ball count higher then 8 or lower then 1."); 
    355355                                        return; 
    356356                                } 
    357                                  
     357 
    358358                                m_botAction.sendUnfilteredPublicMessage("?set Soccer:BallCount:"+argument); 
    359359                                m_botAction.sendPrivateMessage(playerID, "Ball count set to "+argument+" balls."); 
    360360                                m_botAction.sendPrivateMessage(playerID, "The arena settings are already changed and the balls will (dis)appear in a few seconds."); 
    361                                  
     361 
    362362                        } 
    363363                } 
    364364        } 
    365          
     365 
    366366        // Gets the result from ?get Soccer:BallCount or ?set Soccer:BallCount: 
    367367        // Soccer:BallCount=1 
     
    371371                        if(Tools.isAllDigits(count)) { 
    372372                                ballCount = Integer.parseInt(count); 
    373                                  
    374                                 if(orgBallCount == -1)  // Store original ball count so it can be set back when unloading  
     373 
     374                                if(orgBallCount == -1)  // Store original ball count so it can be set back when unloading 
    375375                                        orgBallCount = ballCount; 
    376                                  
     376 
    377377                                if(ballCount == 0) { 
    378378                                        m_botAction.sendPublicMessage("No balls are set in this arena. Module disabled."); 
     
    393393        if(ballCount == 0) 
    394394            return; 
    395          
     395 
    396396        Ball ball = null; 
    397397        if(balls.get(event.getBallID()) == null) { 
     
    402402            ball.setCarrier(event.getCarrier()); 
    403403        } 
    404          
     404 
    405405        if(running) { 
    406              
     406 
    407407            if(ball.getCarrier() != -1) { // A player has picked up the ball or is carrying it 
    408408                ballMode = 1; 
    409                  
     409 
    410410                if((System.currentTimeMillis() - ball.getPreviousCarrierTime()) > dodgetime) { 
    411411                    // the player is out 
    412412                    ballMode = 0; 
    413                      
     413 
    414414                    Player p = m_botAction.getPlayer(event.getCarrier()); 
    415415                    Player prev = m_botAction.getPlayer(ball.getPreviousCarrier()); 
     416                    if( p == null || prev == null ) 
     417                        return; 
    416418                    if(p.getFrequency() != prev.getFrequency() && p.getPlayerID() != m_botAction.getPlayerID(m_botAction.getBotName())) { 
    417419                        m_botAction.specWithoutLock(ball.getCarrier()); 
     
    420422                        players.put(ball.getPreviousCarrier(), (players.get(ball.getPreviousCarrier()) + 1)); 
    421423                    } 
    422                      
     424 
    423425                    checkWinner(); 
    424426                } else { 
     
    430432                    ball.setPreviousCarrierTime( System.currentTimeMillis() ); 
    431433                } 
    432                  
     434 
    433435            } 
    434436        } 
    435437    } 
    436      
     438 
    437439    public void handleEvent(PlayerLeft event) { 
    438440        if(running) 
    439441                players.remove(event.getPlayerID()); 
    440442    } 
    441      
     443 
    442444    public void handleEvent(FrequencyShipChange event) { 
    443445        // if game has started and the new ship type is 0 (Spectator) 
     
    447449        } 
    448450    } 
    449      
     451 
    450452    private void checkWinner() { 
    451453        m_botAction.scheduleTask(checkWinner, 1000); 
    452454    } 
    453      
     455 
    454456    private void clear() { 
    455457        players.clear(); 
     
    457459        ballMode = 0; 
    458460    } 
    459      
     461 
    460462    private void moveBall(int ballID, int x, int y) { 
    461463        m_botAction.stopReliablePositionUpdating();                     // makes the bot stop following people 
     
    469471    } 
    470472 
    471      
     473 
    472474    /** 
    473475     * Essentially a TimerTask that stores info about each player. 
     
    485487                                m_botAction.sendArenaMessage(winner.getPlayerName()+" WINS!  ("+players.get(winner.getPlayerID())+" kills)", Tools.Sound.HALLELUJAH); 
    486488                                clear(); 
    487                                  
     489 
    488490                        } else if(players.size() == 0) { 
    489491                                running = false; 
     
    493495                } 
    494496    } 
    495      
     497 
    496498    private class Ball { 
    497499        private short id; 
     
    499501        private short previousCarrier; 
    500502        private long previousCarrierTime; 
    501          
     503 
    502504        public Ball(short id, short carrier, short previousCarrier, long previousCarrierTime) { 
    503505            this.id = id; 
     
    511513        public short getPreviousCarrier() {     return previousCarrier;     } 
    512514        public long  getPreviousCarrierTime() { return previousCarrierTime; } 
    513          
     515 
    514516        public void setCarrier(short carrier) {                         this.carrier = carrier;     } 
    515517        public void setPreviousCarrier(short previousCarrier) {         this.previousCarrier = previousCarrier; } 
    516518        public void setPreviousCarrierTime(long previousCarrierTime) {  this.previousCarrierTime = previousCarrierTime;  } 
    517          
    518     } 
    519      
     519 
     520    } 
     521 
    520522 
    521523}