]> Pileus Git - ~andy/spades/blob - src/Spades.java
Fix .look bugs
[~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         public  boolean look;
10
11         /* Static methods */
12         private static String[] getCards(String msg, String regex)
13         {
14                 String cards = msg
15                         .replaceAll(regex, "$1")
16                         .replaceAll(",", " ")
17                         .replaceAll("♠", "s")
18                         .replaceAll("♥", "h")
19                         .replaceAll("♦", "d")
20                         .replaceAll("♣", "c");
21                 Os.debug("Cards: getCards - [" + cards + "]:" + Os.base64(cards));
22                 return cards.split("\\s+");
23         }
24
25         /* Widget callback functions */
26         public Spades(String admin)
27         {
28                 this.admin = admin;
29         }
30
31         /* IRC Callbacks */
32         public void onMessage(Message msg)
33         {
34                 Os.debug("Spades: onMessage - " + msg.msg);
35                 if (msg.type != Message.Type.PRIVMSG)
36                         return;
37                 if (!msg.from.equals(this.admin))
38                         return;
39
40                 String txt = msg.txt;
41                 if (txt.matches(".*turn!.*") && this.look) {
42                         this.send(this.admin, ".look");
43                         this.look = false;
44                 }
45                 if (txt.startsWith("You have: ")) {
46                         this.cards.hand = Spades.getCards(txt, "You have: (.*)");
47                         this.cards.requestRender();
48                         this.look = false;
49                 }
50                 if (txt.matches(".*turn!.*")) {
51                         this.cards.pile = Spades.getCards(txt, ".*turn! \\((.*)\\)");
52                         this.cards.requestRender();
53                 }
54                 if (txt.startsWith("It is")) {
55                         this.cards.turn  = txt.replaceAll("It is (\\w+)'s (\\w+)!.*", "$1");
56                         this.cards.state = txt.replaceAll("It is (\\w+)'s (\\w+)!.*", "$2");
57                         this.cards.requestRender();
58                 }
59                 if (txt.startsWith("it is your") && msg.to != "") {
60                         this.cards.turn  = msg.to;
61                         this.cards.state = txt.replaceAll("it is your (\\w+)!", "$1");
62                         this.cards.requestRender();
63                 }
64         }
65
66         /* UI Callbacks */
67         public boolean onConnect()
68         {
69                 Os.debug("Spades: onConnect");
70                 this.send(this.admin, ".status");
71                 this.send(this.admin, ".turn");
72                 this.look = true;
73                 return true;
74         }
75
76         public boolean onBid(int bid)
77         {
78                 Os.debug("Spades: onBid - " + bid);
79                 return this.send(".bid " + bid);
80         }
81
82         public boolean onPass(String card)
83         {
84                 Os.debug("Spades: onPass - " + card);
85                 return this.send(this.admin, ".pass " + card);
86         }
87
88         public boolean onLook()
89         {
90                 Os.debug("Spades: onLook");
91                 return this.send(".look");
92         }
93
94         public boolean onPlay(String card)
95         {
96                 Os.debug("Spades: onPlay - " + card);
97                 return this.send(".play " + card);
98         }
99
100         public boolean onTurn()
101         {
102                 Os.debug("Spades: onTurn");
103                 return this.send(".turn");
104         }
105
106         /* Helper functions */
107         private boolean send(String dst, String msg)
108         {
109                 if (this.task == null)
110                         return false;
111                 this.task.send(dst, msg);
112                 return true;
113         }
114         private boolean send(String msg)
115         {
116                 if (this.task == null)
117                         return false;
118                 this.task.send(msg);
119                 return true;
120         }
121 }