]> Pileus Git - ~andy/spades/blob - src/Spades.java
Prevent crash with zero-length strings
[~andy/spades] / src / Spades.java
1 package org.pileus.spades;
2
3 public class Spades
4 {
5         /* Properties */
6         public  Task    task;
7         public  Cards   cards;
8         public  String  admin;
9
10         /* Static methods */
11         private static String[] getCards(String msg, String regex)
12         {
13                 String cards = msg
14                         .replaceAll(regex, "$1")
15                         .replaceAll(",", " ")
16                         .replaceAll("♠", "s")
17                         .replaceAll("♥", "h")
18                         .replaceAll("♦", "d")
19                         .replaceAll("♣", "c");
20                 Os.debug("Cards: getCards - [" + cards + "]:" + Os.base64(cards));
21                 return cards.split("\\s+");
22         }
23
24         /* Widget callback functions */
25         public Spades(String admin)
26         {
27                 this.admin = admin;
28         }
29
30         /* IRC Callbacks */
31         public void onMessage(Message msg)
32         {
33                 Os.debug("Spades: onMessage - " + msg.msg);
34                 if (msg.type != Message.Type.PRIVMSG)
35                         return;
36                 if (!msg.from.equals(this.admin))
37                         return;
38
39                 String txt = msg.txt;
40                 if (txt.startsWith("You have: ")) {
41                         this.cards.hand = Spades.getCards(txt, "You have: (.*)");
42                         this.cards.requestRender();
43                 }
44                 if (txt.matches(".*turn!.*")) {
45                         this.cards.pile = Spades.getCards(txt, ".*turn! \\((.*)\\)");
46                         this.cards.requestRender();
47                 }
48         }
49
50         /* UI Callbacks */
51         public boolean onBid(int bid)
52         {
53                 Os.debug("Spades: onBid - " + bid);
54                 return this.send(".bid " + bid);
55         }
56
57         public boolean onPass(String card)
58         {
59                 Os.debug("Spades: onPass - " + card);
60                 return this.send(".pass " + card);
61         }
62
63         public boolean onLook()
64         {
65                 Os.debug("Spades: onLook");
66                 return this.send(".look");
67         }
68
69         public boolean onPlay(String card)
70         {
71                 Os.debug("Spades: onPlay - " + card);
72                 return this.send(".play " + card);
73         }
74
75         public boolean onTurn()
76         {
77                 Os.debug("Spades: onTurn");
78                 return this.send(".turn");
79         }
80
81         /* Helper functions */
82         private boolean send(String msg)
83         {
84                 if (this.task == null)
85                         return false;
86                 this.task.send(msg);
87                 return true;
88         }
89 }