OSDN Git Service

024e49be5beb125dd0e302109cd410437fa22137
[webchat/WebChat.git] / public / scripts / commandparser.js
1 //\r
2 // CommandParser\83N\83\89\83X\r
3 //\r
4 function CommandParser()\r
5 {\r
6         //\83R\83}\83\93\83h\83\8a\83X\83g\82ð\95\\82·\r
7         //\83R\83}\83\93\83h\83\8a\83X\83g\82Ì\8ae\97v\91f\82É\82Ífunction(msg,word){}\82ð\92Ç\89Á\82µ\82È\82¯\82ê\82Î\82È\82ç\82È\82¢\r
8         //\97á\81j/test\83R\83}\83\93\83h\82ð\92Ç\89Á\r
9         //this.CommandList["test"] = function(msg,word){}\r
10         this.CommandList;\r
11         //\82Ç\82Ì\83R\83}\83\93\83h\82É\82à\8aY\93\96\82µ\82È\82¢\8e\9e\82É\8cÄ\82Ñ\82³\82ê\82é\8aÖ\90\94\r
12         this.DefaultCommand = function(msg)\r
13         {\r
14                 return msg.message;\r
15         }\r
16 \r
17         this.parse = function(msg)\r
18         {\r
19                 var word = msg.message.split(" ");\r
20                 if(this.CommandList[word[0]])\r
21                         return this.CommandList[word[0]](msg,word);\r
22                 else\r
23                         return this.DefaultCommand(msg);\r
24         }\r
25 }\r
26 \r
27 //\r
28 // CommandParserWhenGet\83N\83\89\83X\r
29 //\r
30 //      function(msg,word)\r
31 //              msg\r
32 //                      msg\83I\83u\83W\83F\83N\83g\r
33 //              word\r
34 //                      \83g\81[\83N\83\93\83\8a\83X\83g\r
35 //              \95Ô\82è\92l\r
36 //                      \83^\83O\82à\82µ\82­\82Í\95\8e\9a\97ñ\82ð\95Ô\82·\r
37 //                      null\82ð\95Ô\82µ\82½\8fê\8d\87\81A\83\81\83b\83Z\81[\83W\83\8a\83X\83g\82É\82Í\89½\82à\95\\8e¦\82³\82ê\82È\82­\82È\82é\r
38 function CommandParserWhenGet()\r
39 {\r
40         this.DefaultCommand = function(msg)\r
41         {\r
42                 return parseBBCode(msg.message);\r
43         }\r
44         this.CommandList = new Array;\r
45         this.CommandList["/tell"] = function(msg,word)\r
46         {\r
47                 if((word[2] == "all")||\r
48                         (word[2] == document.enter_form.name.value)||\r
49                         (word[1] == document.enter_form.name.value)\r
50                 ){\r
51                         return $("<span/>").attr("id","whisper").append(parseBBCode(word[3]));\r
52                 }else{\r
53                         return null;\r
54                 }\r
55         }\r
56         this.CommandList["/enteredby"] = function(msg,word)\r
57         {\r
58                 var name = word[1];\r
59                 var color = word[2];\r
60                 var mailto = word[3];\r
61                 var text = sprintf($entered_message,name);\r
62                 var date = new Date(Date.parse(msg.date));\r
63                 $names[name] = CreateNameInfo(date.getTime(),color,mailto);\r
64                 createNameList();\r
65                 return text;\r
66         }\r
67         this.CommandList["/quitedby"] = function(msg,word)\r
68         {\r
69                 var name = word[1];\r
70                 var text = sprintf($quited_message,name);\r
71                 delete $names[name];\r
72                 createNameList();\r
73                 return text;\r
74         }\r
75 }\r
76 CommandParserWhenGet.prototype = new CommandParser;\r
77 \r
78 //\r
79 // CommandParserWhenPost\83N\83\89\83X\r
80 //\r
81 //msg\r
82 //      msg\83I\83u\83W\83F\83N\83g\r
83 //word\r
84 //      word[0]\81@\83R\83}\83\93\83h\96¼\r
85 //              word[1]\81@\92P\8cê1\r
86 //                      :\r
87 //      word[n]\81@\92P\8cên\r
88 //      \95Ô\8bp\92l\r
89 //              \95\8e\9a\97ñ\82ð\95Ô\82·\r
90 //              null\82ð\95Ô\82µ\82½\8fê\8d\87\81A\91\97\90M\91\80\8dì\82ª\8ds\82í\82ê\82È\82­\82È\82é\r
91 function CommandParserWhenPost()\r
92 {\r
93         this.CommandList = new Array;\r
94         this.CommandList["/tell"] = function(msg,word)\r
95         {\r
96                 word[3] = word[2];\r
97                 word[2] = word[1];\r
98                 word[1] = msg.name;\r
99                 return word.join(" ");\r
100         }\r
101         this.CommandList["/dice"] = function(msg,word)\r
102         {\r
103                 if(word.length == 1)\r
104                         text = CastDice("6d1");\r
105                 else\r
106                         text = CastDice(word[1]);\r
107                 return text;\r
108         }\r
109         this.CommandList["/send"] = function(msg,word)\r
110         {\r
111                 if(word.length != 3)\r
112                         return msg.message;\r
113 \r
114                 var message = sprintf($send_message,document.enter_form.name.value,word[2]);\r
115 \r
116                 if(word[1] == document.chat_form.rno.value)\r
117                         $socket.json.emit("send msg",{name:$system_name,message:message});\r
118 \r
119                 var socket = io.connect(location.hostname + "/" + word[1] + "?token="+ encodeURIComponent(document.chat_form.token.value));\r
120                 socket.on("error",getErrorMessage);\r
121                 socket.on("connect",function(){\r
122                         socket.json.emit("send msg",{name:$system_name,message:message});\r
123                 });\r
124 \r
125                 return null;\r
126         }\r
127         this.CommandList["/setpassword"] = function(msg,word)\r
128         {\r
129                 if(word.length != 2)\r
130                         return msg.message;\r
131 \r
132                 $socket.json.emit("set password",{owner:document.enter_form.name.value,password:word[1]});\r
133 \r
134                 return null;\r
135         }\r
136 }\r
137 CommandParserWhenPost.prototype = new CommandParser;\r
138 \r
139 $PostCommandParser = new CommandParserWhenPost();\r
140 $GetCommandParser = new CommandParserWhenGet();\r
141 \r
142 function ParseDiceParam(number,option)\r
143 {\r
144         if(typeof(number) != "undefined")\r
145         {\r
146                 var result = new Object();\r
147                 result.option = typeof(option) == "undefined" ? null : option;\r
148                 result.number = parseInt(number);\r
149                 return result;\r
150         }\r
151         return null;\r
152 }\r
153 \r
154 function CastDice(text){\r
155         var p = text.match(/(\d+)D(\d+)C*(\d+)?(\-|\+)?F*(\d+)?(\-|\+)?/i);\r
156         var max = parseInt(p[1]);\r
157         var dice_num = parseInt(p[2]);\r
158         var critical = ParseDiceParam(p[3],p[4]);\r
159         var fanble = ParseDiceParam(p[5],p[6]);\r
160         var total = 0;\r
161         var hasCritical = true;\r
162         var hasFanble = true;\r
163         var util = new Util();\r
164 \r
165         text = "[" + text + "] -> ";\r
166         for(var i = 0; i < dice_num; i++)\r
167         {\r
168                 var t = util.get_random_number(1,max);\r
169                 total += t;\r
170                 text += t + " + ";\r
171 \r
172                 if(hasCritical && critical != null)\r
173                 {\r
174                         if(critical.option == "-" && t <= critical.number)\r
175                                 hasCritical = true;\r
176                         else if(critical.option == "+" && t >= critical.number)\r
177                                 hasCritical = true;\r
178                         else if(critical.option == null && t == critical.number)\r
179                                 hasCritical = true;\r
180                         else\r
181                                 hasCritical = false;\r
182                 }\r
183 \r
184                 if(hasFanble && fanble != null)\r
185                 {\r
186                         if(fanble.option == "-" && t <= fanble.number)\r
187                                 hasFanble = true;\r
188                         else if(fanble.option == "+" && t >= fanble.number)\r
189                                 hasFanble = true;\r
190                         else if(fanble.option == null && t == fanble.number)\r
191                                 hasFanble = true;\r
192                         else\r
193                                 hasFanble = false;\r
194                 }\r
195         }\r
196 \r
197         text = text.slice(0,text.length - 3);   //\8dÅ\8cã\82É\95t\82­" + "\82ð\8eæ\82è\8f\9c\82­\r
198         text += " = " + total;\r
199 \r
200         if(critical == null)\r
201                 hasCritical = false;\r
202         if(fanble == null)\r
203                 hasFanble = false;\r
204 \r
205         if(hasCritical)\r
206                 text = sprintf($critical_message,text);\r
207         else if(hasFanble)\r
208                 text = sprintf($fanble_message,text);\r
209 \r
210         return text;\r
211 }\r