PDA

View Full Version : Chat Console


LalVlpShade
05-14-2008, 06:47 AM
I'm posting this for a friend, since he doesn't have a gold account (I hope that's not against the rules x_x). Here's what he says:

So, I've written this code, based off of Inverness' staff debug console (if you've worked on a server with him, you might recognize the GUI). I'm no experienced scripter, as I've only been scripting in GS2 for 4-5 months now. Basically, what I've written is a custom chat system. It's a basic GUI, with an MLText control. A player simply enters the text in the chat bar and pressing enter.

However, before even thinking about implementing this, I would like to know what other, more experienced scripters think. If you see anything that I can change to increase efficiency and speed, or if you just see something I've done wrong, please feel free to post it. I'm not posting this just to show off the script :p.

If anybody needs me to comment the script, or explain what certain functions do, just ask.

NOTE: This script utilizes Inverness' Clientside <-> Serverside Function Caller (http://forums.graalonline.com/forums/showthread.php?t=73564), and a custom string function I made, which can be found at the bottom of this post.
function sendtext(text, special, acct) {
switch(special) {
case "pm":
with(findPlayer(acct)) {
clientfunction2(null,"sendtext", text);
}
break;
case "public":
for(temp.i: players) {
with(i) {
clientfunction2(null, "sendtext", text);
}
}
break;
case "announce":
for(temp.i: allplayers) {
with(i) {
clientfunction2(null, "sendtext", text);
}
}
break;
}
}
function onCreated() {
this.join("util_triggercontrol");
}
//#CLIENTSIDE
function onCreated() {
this.join("util_triggercontrol");
this.join("util_math");
config();
checkdefaults();
createcontrols();
ChatBar.destroy();
}
function onKeyPressed() {
if(params[0] == 9) {
if(ChatConsole_Window.visible == false) {
ChatConsole_Window.fadein(0.25,1);
ChatConsole_Chat.makefirstresponder(true);
}
else {
ChatConsole_Window.fadeout(0.25,0);
}
}
}
/* Event Handling */
function ChatConsole_Window.onResize() {
client.chatconsoleextent = {ChatConsole_Window.width,ChatConsole_Window.heigh t};
}
function ChatConsole_Window.onMove() {
client.chatconsoleposition = {ChatConsole_Window.x,ChatConsole_Window.y};
}
function ChatConsole_Chat.onAction(text) {
temp.text = ChatConsole_Chat.text.substring(0,-1);
temp.textstarts = text.tokenize();
if(text.starts("!")) {
temp.i = text.substring(1).tokenize();
cmd(i[0],i[1],i[2],i[3],i[4],i[5],i[6],i[7],i[8],i[9]);
}
else if(text.starts("/") || text.starts("warpto")
|| text.starts("setnick") || text == "unstickme"
|| text.starts("(")) {
player.chat = text;
}
else if(text == null) {
player.chat = "";
setTimer(0);
}
else if(!text.starts("warpto") && !text.starts("setnick") && text != null
&& !text.starts("!") && !text.starts("/") && text != "unstickme"
&& !text.starts("(") && !client.ooc) {
this.currentchat = text;
player.chat = text;
setTimer(1);
addtext(text);
}
ChatConsole_Chat.text = "";
}
function ChatConsole_Chat.onTabComplete() {
ChatConsole_Window.fadeout(0.25,0);
}
/* Text Functions */
function sendtext(text) {
ChatConsole_Display.addtext("\n" @ text,1);
ChatConsole_Display.scrolltobottom();
}
function addtext(text) {
temp.nicktext = 0;
temp.chattext = text;
temp.total = 0;
temp.i = stringtoarray(player.nick);
if(i.size() > 10) {
temp.nicktext = player.nick.substring(0,7) @ "<font size=2>...</font>";
}
else {
temp.nicktext = player.nick;
}
if(player.guild == "RP Admin") {
temp.total = "<font color=orange>" @ player.nick @ ":" SPC chattext @ "</font>";
}
else {
temp.total = "<font color=limegreen>" @ nicktext @ ":</font>" SPC chattext;
}
serverfunction2(null,"sendtext",total,"public");
}
/* Command Execution */
function cmd(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9) {
switch(p0) {
case "help":
ChatConsole_Display.addtext(this.commandtext,1);
break;
case "clear":
ChatConsole_Display.text = this.welcometext;
break;
case "pm":
player.chat = "";
temp.sender = "<font color=salmon>" @ player.account;
temp.to = p1;
temp.text = sender @ " [PM]: " @ p2 @ "</font>";
serverfunction2(null,"sendtext",text,"pm",to);
break;
case "announce":
if(clientr.staff != 1) {
return;
}
else {
temp.text = "<font color=yellow>[ANNOUNCEMENT]: " @ p1 @ "</font>";
serverfunction2(null,"sendtext",text,"announce");
}
break;
case "setfont":
client.chatconsolefont = p1;
player.chat = "/reconnect";
break;
case "setfontsize":
client.chatconsolefontsize = p1;
player.chat = "/reconnect";
break;
}
}
/* Client. Variable Setting */
function checkdefaults() {
if(client.chatconsoleversion != null) {
return;
}
else {
client.chatconsolefillcolor = this.defaultfillcolor;
client.chatconsolefontcolor = this.defaultfontcolor;
client.chatconsoleposition = this.defaultposition;
client.chatconsoleextent = this.defaultextent;
client.chatconsolefont = this.defaultfont;
client.chatconsolefontsize = this.defaultfontsize;
client.chatconsoleversion = 1;
}
}
/* Configuration */
function config() {
this.defaultfillcolor = {0, 0, 0, 192};
this.defaultfontcolor = {192, 192, 192, 255};
this.defaultposition = {16, 39};
this.defaultextent = {397, 471};
this.defaultfont = "Lucida Console";
this.defaultfontsize = 11;
this.welcometext = "<shadow:1:1><shadowcolor:000000>Welcome to the chat console<br>Type \"!help\" for a list of commands.";
this.commandtext = "\n<font color=limegreen>COMMANDS:</font>"
@ "\n <font color=limegreen>!help:</font> displays command information."
@ "\n <font color=limegreen>!clear:</font> clears the chat display."
@ "\n <font color=limegreen>!pm acct \"message\":</font> sends PM to account."
@ "\n <font color=limegreen>!setfont:</font> sets a custom font."
@ "\n <font color=limegreen>!setfontsize:</font> sets a custom font size.";
if(clientr.staff == 1) {
this.commandtext @= "\n <font color=limegreen>!announce \"message\":</font> staff only command.";
}
}
/* GUI Controls */
function createcontrols() {
with (new GuiWindowCtrl("ChatConsole_Window")) {
GUIContainer.addcontrol(this);
this.join("util_guifade");
this.useownprofile = true;
this.profile.copyfrom(GuiBlueWindowProfile);
this.profile.transparency = 0;
this.position = client.chatconsoleposition;
this.extent = client.chatconsoleextent;
this.minextent = {200, 80};
this.text = "Valikorlia Chat Console";
this.canmove = true;
this.canresize = true;
this.canclose = true;
this.canminimize = true;
this.canmaximize = true;
this.destroyonhide = false;
this.tile = true;
this.alpha = 0;
this.visible = false;

new GuiScrollCtrl("ChatConsole_Scroll") {
this.useownprofile = true;
this.profile.copyfrom(GuiBlueScrollProfile);
this.profile.bitmap = "guiblue_scroll_noback.png";
this.profile.transparency = 0;
this.profile.fillcolor = {0,0,0,192};
this.profile.border = 3;
this.position = {6, 24};
this.extent = {
client.chatconsoleextent[0]-12,
client.chatconsoleextent[1]-46
};
this.horizsizing = "width";
this.vertsizing = "height";
this.hscrollbar = "alwaysOff";
this.vscrollbar = "dynamic";

new GuiMLTextCtrl("ChatConsole_Display") {
this.useownprofile = true;
this.profile.copyfrom(GuiBlueMLTextProfile);
this.profile.fonttype = client.chatconsolefont;
this.profile.fontcolor = client.chatconsolefontcolor;
this.profile.fontsize = client.chatconsolefontsize;
this.horizsizing = "width";
this.position = {2, 2};
this.wordwrap = true;
this.extent = {
client.chatconsoleextent[0]-33,
client.chatconsoleextent[1]-58
};
if (text == "") {
text = thiso.welcometext;
}
}
}
new GuiTextEditCtrl("ChatConsole_Chat") {
this.profile = GuiBlueTextEditProfile;
this.horizsizing = "width";
this.vertsizing = "top";
this.position = {6, client.chatconsoleextent[1] - 25};
this.extent = {client.chatconsoleextent[0]-12, 20};
this.historysize = 100;
this.tabcomplete = true;
}
this.bringtofront();
}
}
function onTimeout() {
if(this.second < 16) {
player.chat = this.currentchat;
setTimer(1);
this.second++;
}
else {
player.chat = "";
this.currentchat = "";
this.second = 0;
setTimer(0);
}
}


function stringtoarray(str) {
temp.a = 0;
temp.b = 0;
for(a = 0; a < str.length(); a++) {
if(a.substring(b,1) != " ") {
b.add(str.substring(a,1));
}
}
return b;
}