Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Usage:
- say /staffchat once added to player
- messages arent saved through server restarts
- */
- //findplayerbycommunityname("Inari").addweapon(this.name);
- function onCreated()
- {
- enum cmd
- {
- SEND_MESSAGE,
- RECEIVE_MESSAGE,
- LOAD_CHAT,
- LOGIN,
- LOGOUT
- }
- this.saveMessages = 30;
- this.users = {};
- this.chatMsgs = new[this.saveMessages];
- }
- function onPlayerLogout(obj,int)
- {
- if(obj != null)
- {
- logoutPlayer(obj);
- }
- }
- function strreplace(input,search,replace)
- {
- temp.pos = input.positions(search);
- for(temp.i=temp.pos.size()-1;temp.i >= 0; temp.i--)
- {
- temp.posi = temp.pos[temp.i];
- input = input.substring(0,temp.posi) @ replace @ input.substring(temp.posi + search.length());
- }
- return input;
- }
- function sanitizeMsg(input)
- {
- input = strreplace(input,"\\n"," ");
- input = strreplace(input,"\\r"," ");
- input = strreplace(input,"<","<");
- return input;
- }
- function addMessage(nick,msg)
- {
- this.chatMsgs.insert(0,{nick,msg});
- this.chatMsgs.delete(30);
- sendToUsers(cmd.RECEIVE_MESSAGE,{player.communityname,temp.msg});
- }
- function onActionServerSide(cmd,data)
- {
- if(!(findeapon(this.name) in player.weapons)) return;
- if( cmd == cmd.SEND_MESSAGE )
- {
- temp.msg = sanitizeMsg(data[0]);
- addMessage(player.communityname,temp.msg);
- }
- elseif( cmd == cmd.LOAD_CHAT )
- {
- loginPlayer();
- player.triggerClient("weapon", this.name, cmd.LOAD_CHAT,{this.chatMsgs,this.users});
- }
- elseif( cmd == cmd.LOGIN )
- {
- sendToUsers(cmd,data);
- }
- elseif( cmd == cmd.LOGOUT )
- {
- sendToUsers(cmd,data);
- }
- }
- function sendToUsers(cmd,data)
- {
- for(temp.user: this.users)
- {
- findPlayer(temp.user[0]).triggerClient("weapon",this.name,cmd,data);
- }
- }
- function loginPlayer(playerVar)
- {
- if( playerVar == null ) playerVar = player;
- sendToUsers(cmd.LOGIN,player.communityname);
- temp.inIt = false;
- for(temp.user: this.users)
- {
- if(temp.user[0] == playerVar.account)
- {
- temp.inIt = true; break;
- }
- }
- addMessage("System","<i>" @ playerVar.communityName @ " logged in</i>");
- if(!inIt) this.users.add({playerVar.account,playerVar.communityname});
- }
- function logoutPlayer(playerVar)
- {
- if(playerVar == null ) playerVar = player;
- if(!(findeapon(this.name) in playerVar.weapons)) return;
- temp.f = false;
- for(temp.i=0; temp.i < this.users.size(); temp.i++)
- {
- if(playerVar.account == this.users[temp.i][0])
- {
- temp.f = true;
- this.users.delete(temp.i); temp.i--;
- }
- }
- if( temp.f )
- {
- addMessage("System","<i>" @ playerVar.communityName @ " logged out</i>");
- sendToUsers(cmd.LOGOUT,playerVar.communityName);
- }
- }
- //#CLIENTSIDE
- function onCreated()
- {
- enum cmd
- {
- SEND_MESSAGE,
- RECEIVE_MESSAGE,
- LOAD_CHAT,
- LOGIN,
- LOGOUT
- }
- this.saveMessages = 200;
- this.loaded = false;
- createGUI();
- }
- function onPlayerChats()
- {
- if(player.chat == "/staffchat")
- {
- if(this.loaded) openGUI();
- else triggerServer("weapon",this.name,cmd.LOAD_CHAT);
- }
- }
- function onActionClientSide(cmd,data)
- {
- if( cmd == cmd.LOAD_CHAT )
- {
- GUI_ChatUsers.clearRows();
- for(temp.userData: data[1])
- {
- GUI_ChatUsers.addRow(0,temp.userData[1]);
- }
- GUI_Chat.text = "Welcome to the Staff Chat!";
- for(temp.chatMsg: data[0])
- {
- if( temp.chatMsg != null )
- GUI_Chat.text @= "\n<b>" @ temp.chatMsg[0] @ "</b>: " @ temp.chatMsg[1];
- }
- openGUI();
- }
- elseif( cmd == cmd.RECEIVE_MESSAGE )
- {
- GUI_Chat.text @= "\n<b>" @ data[0] @ "</b>: " @ data[1];
- }
- elseif( cmd == cmd.LOGIN )
- {
- if(GUI_ChatUsers.findtext(data[0]) == -1 )
- GUI_ChatUsers.addRow(0,data[0]);
- }
- elseif( cmd == cmd.LOGOUT )
- {
- GUI_ChatUsers.removerow(GUI_ChatUsers.findtext(data[0]));
- }
- }
- function openGUI()
- {
- if(GUI_ChatWindow == null ) createGUI();
- GUI_ChatWindow.visible = true;
- }
- function createGUI()
- {
- GUI_ChatWindow.destroy();
- new GuiWindowCtrl("GUI_ChatWindow") {
- profile = GuiBlueWindowProfile;
- style = $pref::Video::defaultguistyle;
- clientrelative = true;
- clientextent = "645,307";
- canmove = true;
- canresize = true;
- closequery = false;
- destroyonhide = false;
- visible = false;
- text = "Staff Chat";
- x = 346;
- y = 526;
- new GuiScrollCtrl("GUI_Chat_Scroll") {
- profile = GuiBlueScrollProfile;
- childmargin = "8,8";
- height = 270;
- hscrollbar = "alwaysOff";
- vscrollbar = "dynamic";
- width = 500;
- x = 8;
- y = 6;
- horizsizing = "width";
- vertsizing = "height";
- new GuiMLTextCtrl("GUI_Chat") {
- profile = GuiBlueMLTextProfile;
- height = 68;
- horizsizing = "width";
- plaintext = "";
- text = "";
- width = 480;
- }
- }
- new GuiTextEditCtrl("GUI_ChatInput") {
- profile = GuiBlueTextEditProfile;
- height = 20;
- width = 450;
- x = 8;
- y = 281;
- horizsizing = "width";
- vertsizing = "top";
- }
- new GuiButtonCtrl("GUI_ChatButton_Send") {
- profile = GuiBlueButtonProfile;
- height = 20;
- text = "Send";
- width = 49;
- x = 460;
- y = 279;
- horizsizing = "left";
- vertsizing = "top";
- }
- new GuiScrollCtrl("GUI_ChatUsers_Scroll") {
- profile = GuiBlueScrollProfile;
- height = 295;
- hscrollbar = "alwaysOff";
- vscrollbar = "dynamic";
- width = 130;
- x = 510;
- y = 6;
- childmargin = "8,8";
- horizsizing = "left";
- vertsizing = "height";
- new GuiTextListCtrl("GUI_ChatUsers") {
- profile = GuiBlueTextListProfile;
- height = 34;
- horizsizing = "width";
- sortcolumn = 82557184;
- width = 126;
- }
- }
- }
- }
- function GUI_ChatButton_Send.onAction()
- {
- triggerServer("weapon",this.name,cmd.SEND_MESSAGE,GUI_ChatInput.text);
- GUI_ChatInput.text = "";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement