]> Pileus Git - ~andy/spades/blob - src/Spades.java
Show turn in title bar
[~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 looked;
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.looked) {
42                         this.send(this.admin, ".look");
43                         this.looked = true;
44                 }
45                 if (txt.startsWith("You have: ")) {
46                         this.cards.hand = Spades.getCards(txt, "You have: (.*)");
47                         this.cards.requestRender();
48                 }
49                 if (txt.matches(".*turn!.*")) {
50                         this.cards.pile = Spades.getCards(txt, ".*turn! \\((.*)\\)");
51                         this.cards.requestRender();
52                 }
53                 if (txt.startsWith("It is")) {
54                         this.cards.turn  = txt.replaceAll("It is (\\w+)'s (\\w+)!.*", "$1");
55                         this.cards.state = txt.replaceAll("It is (\\w+)'s (\\w+)!.*", "$2");
56                         this.cards.requestRender();
57                 }
58                 if (txt.startsWith("^it is your")) {
59                         this.cards.turn = msg.to;
60                         this.cards.requestRender();
61                 }
62         }
63
64         /* UI Callbacks */
65         public boolean onConnect()
66         {
67                 Os.debug("Spades: onConnect");
68                 this.looked = false;
69                 this.send(this.admin, ".status");
70                 this.send(this.admin, ".turn");
71                 return true;
72         }
73
74         public boolean onBid(int bid)
75         {
76                 Os.debug("Spades: onBid - " + bid);
77                 return this.send(".bid " + bid);
78         }
79
80         public boolean onPass(String card)
81         {
82                 Os.debug("Spades: onPass - " + card);
83                 return this.send(this.admin, ".pass " + card);
84         }
85
86         public boolean onLook()
87         {
88                 Os.debug("Spades: onLook");
89                 return this.send(".look");
90         }
91
92         public boolean onPlay(String card)
93         {
94                 Os.debug("Spades: onPlay - " + card);
95                 return this.send(".play " + card);
96         }
97
98         public boolean onTurn()
99         {
100                 Os.debug("Spades: onTurn");
101                 return this.send(".turn");
102         }
103
104         /* Helper functions */
105         private boolean send(String dst, String msg)
106         {
107                 if (this.task == null)
108                         return false;
109                 this.task.send(dst, msg);
110                 return true;
111         }
112         private boolean send(String msg)
113         {
114                 if (this.task == null)
115                         return false;
116                 this.task.send(msg);
117                 return true;
118         }
119 }