summaryrefslogtreecommitdiff
path: root/NET/worlds/console/WhisperManager.java
blob: 180fa1036f2c36f6425ee0de70236294e26570bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package NET.worlds.console;

import NET.worlds.network.NetUpdate;
import NET.worlds.scape.InventoryManager;
import java.util.Enumeration;
import java.util.Hashtable;

public class WhisperManager {
   private static WhisperManager manager_;
   private Hashtable<String, WhisperDialog> dialogs_;
   private Hashtable<String, TradeDialog> tradeDialogs_;
   private java.awt.Window parent;
   private String inventory = "";
   final String tradeServerName = "TRADE";
   static GiftDialog outstandingGift;

   private WhisperManager() {
      this.dialogs_ = new Hashtable<String, WhisperDialog>();
      this.tradeDialogs_ = new Hashtable<String, TradeDialog>();
   }

   public static WhisperManager whisperManager() {
      if (manager_ == null) {
         manager_ = new WhisperManager();
      }

      return manager_;
   }

   void setParent(java.awt.Window p) {
      this.parent = p;
   }

   public Hashtable<String, WhisperDialog> dialogs() {
      return this.dialogs_;
   }

   public Hashtable<String, TradeDialog> tradeDialogs() {
      return this.tradeDialogs_;
   }

   private WhisperDialog findWhisperDialog(String to) {
      return !this.dialogs_.containsKey(to) ? null : this.dialogs_.get(to);
   }

   private TradeDialog findTradeDialog(String to) {
      return !this.tradeDialogs_.containsKey(to) ? null : this.tradeDialogs_.get(to);
   }

   private WhisperDialog start(String to, boolean takeFocus) {
      WhisperDialog wd = this.findWhisperDialog(to);
      if (wd == null) {
         this.dialogs_.put(to, wd = new WhisperDialog(this.parent, to));
      }

      if (takeFocus) {
         wd.takeFocus();
      }

      wd.ready();
      return wd;
   }

   public void remove(String name) {
      this.dialogs_.remove(name);
   }

   public void startTo(String to) {
      WhisperDialog wd = this.start(to, true);
   }

   public TradeDialog startToTrade(String to) {
      TradeDialog wd = this.findTradeDialog(to);
      if (wd == null) {
         this.tradeDialogs_.put(to, wd = new TradeDialog(this.parent, to));
      }

      wd.takeFocus();
      wd.ready();
      wd.setTrading(true);
      wd.whisperPart.println(Console.message("trade-start"));
      return wd;
   }

   public void printFrom(String from, String msg) {
      if (!msg.startsWith("&|+")) {
         WhisperDialog it = this.findWhisperDialog(from);
         if (msg.equals("&|+trade>cancel") && (it == null || !it.isActive() || !it.isTrading)) {
            return;
         }

         it = this.start(from, false);
         it.print(msg);
      } else if (msg.startsWith("&|+gift>") && from.equalsIgnoreCase("TRADE")) {
         maybeQueryGift(msg.substring(8));
      } else if (msg.startsWith("&|+inv>") && from.equalsIgnoreCase("TRADE")) {
         this.tradeMsg(msg.substring(7));
      } else if (msg.startsWith("&|+trade>")) {
         TradeDialog it = this.findTradeDialog(from);
         if (msg.equals("&|+trade>cancel") && (it == null || !it.isActive() || !it.isTrading)) {
            return;
         }

         it = this.startToTrade(from);
         it.print(msg);
      }
   }

   public void tradeMsg(String newInv) {
      if (!newInv.equals(this.inventory)) {
         Enumeration<TradeDialog> e = this.tradeDialogs_.elements();

         while (e.hasMoreElements()) {
            TradeDialog wd = e.nextElement();
            wd.doneDeal();
         }

         InventoryManager.getInventoryManager().setInventory(newInv);
      }
   }

   public void printTo(String to, String msg) {
      if (!to.equals("world") && !to.equals("TRADE")) {
         if (!msg.startsWith("&|+") || msg.startsWith("&|+trade>")) {
            WhisperDialog it = this.start(to, false);
            it.send(msg);
         }
      }
   }

   public void giftDialogDone() {
      outstandingGift = null;
   }

   public static void maybeQueryGift(String inv) {
      if (NetUpdate.isInternalVersion()) {
         Console c = Console.getActive();
         if (c != null && outstandingGift == null && !c.isSleeping()) {
            outstandingGift = new GiftDialog(inv, 3000000);
         }
      }
   }
}