PageRenderTime 61ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/rex_bahamut/runtime.js

https://github.com/rexrainbow/C2_plugins
JavaScript | 457 lines | 389 code | 50 blank | 18 comment | 56 complexity | 3eb4f49f50d51793406cd053d3c416b0 MD5 | raw file
  1. // ECMAScript 5 strict mode
  2. "use strict";
  3. assert2(cr, "cr namespace not created");
  4. assert2(cr.plugins_, "cr.plugins_ not created");
  5. /////////////////////////////////////
  6. // Plugin class
  7. cr.plugins_.Rex_Bahamut = function(runtime)
  8. {
  9. this.runtime = runtime;
  10. };
  11. (function ()
  12. {
  13. var pluginProto = cr.plugins_.Rex_Bahamut.prototype;
  14. /////////////////////////////////////
  15. // Object type class
  16. pluginProto.Type = function(plugin)
  17. {
  18. this.plugin = plugin;
  19. this.runtime = plugin.runtime;
  20. };
  21. var typeProto = pluginProto.Type.prototype;
  22. typeProto.onCreate = function()
  23. {
  24. jsfile_load("jquery.xdomainajax.js");
  25. };
  26. var jsfile_load = function(file_name)
  27. {
  28. var scripts=document.getElementsByTagName("script");
  29. var exist=false;
  30. for(var i=0;i<scripts.length;i++)
  31. {
  32. if(scripts[i].src.indexOf(file_name) != -1)
  33. {
  34. exist=true;
  35. break;
  36. }
  37. }
  38. if(!exist)
  39. {
  40. var newScriptTag=document.createElement("script");
  41. newScriptTag.setAttribute("type","text/javascript");
  42. newScriptTag.setAttribute("src", file_name);
  43. document.getElementsByTagName("head")[0].appendChild(newScriptTag);
  44. }
  45. };
  46. /////////////////////////////////////
  47. // Instance class
  48. pluginProto.Instance = function(type)
  49. {
  50. this.type = type;
  51. this.runtime = type.runtime;
  52. };
  53. var instanceProto = pluginProto.Instance.prototype;
  54. instanceProto.onCreate = function()
  55. {
  56. this._user_data = {};
  57. this._tag = "";
  58. this._current_user_name = "";
  59. this.exp_CurFriendName = "";
  60. this.exp_CurFriendNickname = "";
  61. };
  62. var _is_vaild_html = function(content)
  63. {
  64. return (content != "") && content.indexOf("查詢失敗") == (-1);
  65. };
  66. var _index_get = function (content, start_index, k)
  67. {
  68. return content.indexOf(k, start_index) + k.length;
  69. }
  70. var _user2nickname = function(content, start_index)
  71. {
  72. var start_index = _index_get(content, start_index, "暱稱:");
  73. var start_index = content.indexOf(">", start_index) +1;
  74. var end_index = content.indexOf("<", start_index);
  75. var value = content.substring(start_index, end_index);
  76. return value;
  77. };
  78. var _user2level = function(content, start_index)
  79. {
  80. // level
  81. var start_index = _index_get(content, start_index, "LV");
  82. var end_index = content.indexOf(" ", start_index);
  83. var lv = parseFloat( content.substring(start_index, end_index) );
  84. // race
  85. var start_index = content.indexOf(" ", end_index+1) + 1;
  86. var end_index = content.indexOf(" ", start_index);
  87. var race = content.substring(start_index, end_index);
  88. // occupation
  89. var start_index = content.indexOf(" ", end_index+1) + 1;
  90. var end_index = content.indexOf("<", start_index);
  91. var occupation = content.substring(start_index, end_index);
  92. return [lv, race, occupation];
  93. };
  94. var _user2property = function(content, start_index, property_name)
  95. {
  96. property_name += ":";
  97. var start_index = _index_get(content, start_index, property_name);
  98. var end_index = content.indexOf("<", start_index);
  99. var value = content.substring(start_index, end_index);
  100. return parseFloat(value);
  101. };
  102. var _get_friendlist = function(content)
  103. {
  104. var friend_list = [];
  105. var bound_index = content.indexOf("<!--內容左側區塊結束-->");
  106. var key = '<a href="http://home.gamer.com.tw/';
  107. var key_length = key.length;
  108. var start_index = content.indexOf(key);
  109. var name, end_index, nickname;
  110. while (start_index < bound_index)
  111. {
  112. start_index += key_length;
  113. end_index = content.indexOf('"', start_index);
  114. name = content.substring(start_index, end_index);
  115. nickname = _friendlist_name2nickname(content, name, end_index);
  116. start_index = content.indexOf(key, end_index+1);
  117. friend_list.push([name,nickname]);
  118. }
  119. return friend_list;
  120. };
  121. var _friendlist_name2nickname = function (content, name, start_index)
  122. {
  123. var nickname, nickname_start_index, nickname_end_index, nickname_key;
  124. nickname_key = name + "<br/>\n";
  125. nickname_start_index = content.indexOf(nickname_key, start_index);
  126. nickname_start_index += nickname_key.length;
  127. nickname_end_index = content.indexOf("</a></td>", nickname_start_index);
  128. nickname = content.substring(nickname_start_index, nickname_end_index);
  129. return nickname;
  130. };
  131. instanceProto.filled_user_data = function (content, usr_name)
  132. {
  133. if (!_is_vaild_html(content))
  134. {
  135. return false;
  136. }
  137. if (!this._user_data.hasOwnProperty(usr_name))
  138. {
  139. this._user_data[usr_name] = {};
  140. }
  141. var user_data = this._user_data[usr_name];
  142. var start_index = _index_get(content, 'MSG-mydata1">', 0);
  143. user_data.Nickname = _user2nickname(content, start_index);
  144. var lv = _user2level(content, start_index);
  145. user_data.LV = lv[0];
  146. user_data.RACE = lv[1];
  147. user_data.OCCUPATION = lv[2];
  148. var start_index = _index_get(content, 'MSG-mydata3">', start_index);
  149. user_data.STR = _user2property(content, start_index, "STR");
  150. user_data.DEX = _user2property(content, start_index, "DEX");
  151. user_data.INT = _user2property(content, start_index, "INT");
  152. user_data.LUK = _user2property(content, start_index, "LUK");
  153. user_data.VIT = _user2property(content, start_index, "VIT");
  154. user_data.AGI = _user2property(content, start_index, "AGI");
  155. user_data.MND = _user2property(content, start_index, "MND");
  156. return true;
  157. }
  158. instanceProto.filled_friend_list = function (content, usr_name)
  159. {
  160. if (!_is_vaild_html(content))
  161. {
  162. return false;
  163. }
  164. if (!this._user_data.hasOwnProperty(usr_name))
  165. {
  166. this._user_data[usr_name] = {};
  167. }
  168. this._user_data[usr_name].friend_list = _get_friendlist(content);
  169. return true;
  170. };
  171. //////////////////////////////////////
  172. // Conditions
  173. function Cnds() {};
  174. pluginProto.cnds = new Cnds();
  175. Cnds.prototype.OnGetUserData = function(tag)
  176. {
  177. return (this._tag == tag);
  178. };
  179. Cnds.prototype.OnGetUserDataFailed = function(tag)
  180. {
  181. return (this._tag == tag);
  182. };
  183. Cnds.prototype.OnGetFriendList = function(tag)
  184. {
  185. return (this._tag == tag);
  186. };
  187. Cnds.prototype.OnGetFriendListFailed = function(tag)
  188. {
  189. return (this._tag == tag);
  190. };
  191. Cnds.prototype.OnGetGameCard = function(tag)
  192. {
  193. // return (this._tag == tag);
  194. };
  195. Cnds.prototype.OnGetGameCardFailed = function(tag)
  196. {
  197. // return (this._tag == tag);
  198. };
  199. Cnds.prototype.ForEachFriend = function (user_name)
  200. {
  201. var current_event = this.runtime.getCurrentEventStack().current_event;
  202. var usr_data = this._user_data[user_name];
  203. if (usr_data == null)
  204. return false;
  205. var friend_list = usr_data.friend_list;
  206. if (friend_list == null)
  207. return false;
  208. var friend_cnt=friend_list.length;
  209. var i, friend_item;
  210. for (i=0; i<friend_cnt; i++)
  211. {
  212. friend_item = friend_list[i];
  213. this.exp_CurFriendName = friend_item[0];
  214. this.exp_CurFriendNickname = friend_item[1];
  215. this.runtime.pushCopySol(current_event.solModifiers);
  216. current_event.retrigger();
  217. this.runtime.popSol(current_event.solModifiers);
  218. }
  219. this.exp_CurFriendName = "";
  220. this.exp_CurFriendNickname = "";
  221. return false;
  222. };
  223. //////////////////////////////////////
  224. // Actions
  225. function Acts() {};
  226. pluginProto.acts = new Acts();
  227. Acts.prototype.CleanUserData = function()
  228. {
  229. var name;
  230. for (name in this._user_data)
  231. delete this._user_data[name];
  232. };
  233. Acts.prototype.GetUserData = function(user_name, tag)
  234. {
  235. this._current_user_name = user_name;
  236. var self = this;
  237. var is_success = false;
  238. window["xdmAjax"]({
  239. "url": "http://home.gamer.com.tw/homeindex.php?owner="+user_name,
  240. "type": 'GET',
  241. "success": function(res) {
  242. var content = res.responseText;
  243. content = content.replace(/\r\n/g, "\n");
  244. is_success = self.filled_user_data(content, user_name);
  245. },
  246. "error": function()
  247. {
  248. is_success = false;
  249. },
  250. "complete": function()
  251. {
  252. self._current_user_name = user_name;
  253. var trg_method = (is_success)?
  254. cr.plugins_.Rex_Bahamut.prototype.cnds.OnGetUserData:
  255. cr.plugins_.Rex_Bahamut.prototype.cnds.OnGetUserDataFailed;
  256. self._tag = tag;
  257. self.runtime.trigger(trg_method, self);
  258. }
  259. });
  260. };
  261. Acts.prototype.GetFriendList = function(user_name, tag)
  262. {
  263. this._current_user_name = user_name;
  264. var self = this;
  265. var is_success = false;
  266. window["xdmAjax"]({
  267. "url": "http://home.gamer.com.tw/friend.php?owner="+user_name,
  268. "type": 'GET',
  269. "success": function(res) {
  270. var content = res.responseText;
  271. content = content.replace(/\r\n/g, "\n");
  272. is_success = self.filled_friend_list(content, user_name);
  273. },
  274. "error": function()
  275. {
  276. is_success = false;
  277. },
  278. "complete": function()
  279. {
  280. self._current_user_name = user_name;
  281. var trg_method = (is_success)?
  282. cr.plugins_.Rex_Bahamut.prototype.cnds.OnGetFriendList:
  283. cr.plugins_.Rex_Bahamut.prototype.cnds.OnGetFriendListFailed;
  284. self._tag = tag;
  285. self.runtime.trigger(trg_method, self);
  286. }
  287. });
  288. };
  289. Acts.prototype.GetGameCard = function(user_name, tag) {};
  290. //////////////////////////////////////
  291. // Expressions
  292. function Exps() {};
  293. pluginProto.exps = new Exps();
  294. Exps.prototype.CurUserName = function(ret)
  295. {
  296. ret.set_string(this._current_user_name);
  297. };
  298. Exps.prototype.STR = function(ret, user_name)
  299. {
  300. if (user_name == null)
  301. user_name = this._current_user_name;
  302. var val = (this._user_data[user_name] == null)?
  303. 0:this._user_data[user_name].STR;
  304. ret.set_float(val);
  305. };
  306. Exps.prototype.DEX = function(ret, user_name)
  307. {
  308. if (user_name == null)
  309. user_name = this._current_user_name;
  310. var val = (this._user_data[user_name] == null)?
  311. 0:this._user_data[user_name].DEX;
  312. ret.set_float(val);
  313. };
  314. Exps.prototype.INT = function(ret, user_name)
  315. {
  316. if (user_name == null)
  317. user_name = this._current_user_name;
  318. var val = (this._user_data[user_name] == null)?
  319. 0:this._user_data[user_name].INT;
  320. ret.set_float(val);
  321. };
  322. Exps.prototype.LUK = function(ret, user_name)
  323. {
  324. if (user_name == null)
  325. user_name = this._current_user_name;
  326. var val = (this._user_data[user_name] == null)?
  327. 0:this._user_data[user_name].LUK;
  328. ret.set_float(val);
  329. };
  330. Exps.prototype.VIT = function(ret, user_name)
  331. {
  332. if (user_name == null)
  333. user_name = this._current_user_name;
  334. var val = (this._user_data[user_name] == null)?
  335. 0:this._user_data[user_name].VIT;
  336. ret.set_float(val);
  337. };
  338. Exps.prototype.AGI = function(ret, user_name)
  339. {
  340. if (user_name == null)
  341. user_name = this._current_user_name;
  342. var val = (this._user_data[user_name] == null)?
  343. 0:this._user_data[user_name].AGI;
  344. ret.set_float(val);
  345. };
  346. Exps.prototype.MND = function(ret, user_name)
  347. {
  348. if (user_name == null)
  349. user_name = this._current_user_name;
  350. var val = (this._user_data[user_name] == null)?
  351. 0:this._user_data[user_name].MND;
  352. ret.set_float(val);
  353. };
  354. Exps.prototype.ImageURL = function(ret, user_name)
  355. {
  356. if (user_name == null)
  357. user_name = this._current_user_name;
  358. var img_url = "http://avatar2.bahamut.com.tw/avataruserpic/"+
  359. user_name.charAt(0) +"/"+ user_name.charAt(1)+ "/"+ user_name +"/"+ user_name +".png";
  360. ret.set_string(img_url);
  361. };
  362. Exps.prototype.CurFriendName = function(ret)
  363. {
  364. ret.set_string(this.exp_CurFriendName);
  365. };
  366. Exps.prototype.CurFriendNickname = function(ret)
  367. {
  368. ret.set_string(this.exp_CurFriendNickname);
  369. };
  370. Exps.prototype.GameCardURL = function(ret, user_name)
  371. {
  372. ret.set_string("");
  373. };
  374. Exps.prototype.Nickname = function(ret, user_name)
  375. {
  376. if (user_name == null)
  377. user_name = this._current_user_name;
  378. var val = (this._user_data[user_name] == null)?
  379. "":this._user_data[user_name].Nickname;
  380. ret.set_string(val);
  381. };
  382. Exps.prototype.LV = function(ret, user_name)
  383. {
  384. if (user_name == null)
  385. user_name = this._current_user_name;
  386. var val = (this._user_data[user_name] == null)?
  387. 0:this._user_data[user_name].LV;
  388. ret.set_float(val);
  389. };
  390. Exps.prototype.RACE = function(ret, user_name)
  391. {
  392. if (user_name == null)
  393. user_name = this._current_user_name;
  394. var val = (this._user_data[user_name] == null)?
  395. "":this._user_data[user_name].RACE;
  396. ret.set_string(val);
  397. };
  398. Exps.prototype.OCCUPATION = function(ret, user_name)
  399. {
  400. if (user_name == null)
  401. user_name = this._current_user_name;
  402. var val = (this._user_data[user_name] == null)?
  403. "":this._user_data[user_name].OCCUPATION;
  404. ret.set_string(val);
  405. };
  406. }());