# Author: Yisc[NL] # # Lapper version needed: 7.0.4.10 or above # # # # Cross-server chat V1.00 14-02-2018 -Initial version # # Cross-server chat V1.01 18-02-2018 -Fixed a bug that caused displaying empty messages when starting from scratch # # Cross-server chat V1.02 22-02-2018 -Fixed a bug that caused Lapper to throw messages about database being locked # # -Changed GetStoredValue to GetStoredNum where possible to get numbers instead of values # ################################################################################################################################################# CatchEvent OnMSO( $userName, $text ) # Player event $idxOfFirtsSpace = indexOf( $text, " "); IF( $idxOfFirtsSpace == -1 ) THEN $command = $text; $argv = ""; ELSE $command = subStr( $text,0,$idxOfFirtsSpace ); $argv = trim( subStr( $text,$idxOfFirtsSpace ) ); ENDIF SWITCH( $command ) CASE "!chat": IF ( $argv != "" ) THEN send_chat_msg_direct ( $argv ); ELSE Chat ( $KeyFlags,$id ); ENDIF BREAK; ENDSWITCH EndCatchEvent CatchEvent OnLapperStart() OnLapperStart_Initialise_chat(); EndCatchEvent Sub OnLapperStart_Initialise_chat() ### Declare global variables ### GlobalVar $cross_srv_chat_version; GlobalVar $chat_msg_old; GlobalVar $chat_msg_new; GlobalVar $chat_msg_max; GlobalVar $chat_msg_nbr; ### End ### ### Set version number ### $cross_srv_chat_version="V1.02"; ### End ### ### Load stored values and give them a default setting if no stored value has been found ### $chat_msg_old = GetStoredNum( "CHAT_MSG_OLD" ); IF ($chat_msg_old == "") THEN $chat_msg_old = 0; SetStoredValue( "CHAT_MSG_OLD" , $chat_msg_old ); ENDIF $chat_msg_new = GetStoredNum( "CHAT_MSG_NEW" ); IF ($chat_msg_new == "") THEN $chat_msg_new = 0; SetStoredValue( "CHAT_MSG_NEW" , $chat_msg_new ); ENDIF $chat_msg_max = GetStoredNum( "CHAT_MSG_MAX" ); IF ($chat_msg_max == "") THEN $chat_msg_max = 7; SetStoredValue( "CHAT_MSG_MAX" , $chat_msg_max ); ENDIF $chat_msg_nbr = GetStoredNum( "CHAT_MSG_NBR" ); IF ($chat_msg_nbr == "") THEN $chat_msg_nbr = 0; SetStoredValue( "CHAT_MSG_NBR" , $chat_msg_nbr ); ENDIF ### End ### EndSub Sub Chat( $KeyFlags,$id ) ### Set left/right and top/bottom coordinates ### $left_right_cross_srv_chat=15; $top_bottom_cross_srv_chat=40; ### End ### ### Draw buttons for background, title bar, refresh counter and close the screen ### openPrivButton( "cross_srv_chat_bg",$left_right_cross_srv_chat,$top_bottom_cross_srv_chat,120,97,5,-1,16,"" ); openPrivButton( "cross_srv_chat_title",$left_right_cross_srv_chat+1,$top_bottom_cross_srv_chat+1,118,5,5,-1,32,"^7Cross-server chat - " . $cross_srv_chat_version ); openPrivButton( "refresh_cross_srv_chat",$left_right_cross_srv_chat+1,$top_bottom_cross_srv_chat+1,5,5,5,15,0,"^3%cpt%",refresh_chat ); openPrivButton( "cross_srv_chat_close",$left_right_cross_srv_chat+114,$top_bottom_cross_srv_chat+1,5,5,5,-1,0,"^3X",close_chat ); ### End ### ### Retrieve current values for chat_msg_old, chat_msg_new and chat_msg_nbr ### $chat_msg_old = GetStoredNum( "CHAT_MSG_OLD" ); $chat_msg_new = GetStoredNum( "CHAT_MSG_NEW" ); $chat_msg_nbr = GetStoredNum( "CHAT_MSG_NBR" ); ### End ### ### Set value for $display_message, set new value for $top_bottom_cross_srv_chat ### $display_message = $chat_msg_old; $top_bottom_cross_srv_chat=51; ### End ### ### Give CurrentPlayerVar "chat_message_old" a value, so it can be used when closing chat, while latest message(s) hasn't/haven't appeared yet ### SetCurrentPlayerVar( "chat_message_old",$chat_msg_old ); ### End ### ### Start WHILE-loop used to display the actual chat messages ### WHILE( $display_message <= $chat_msg_new ) ### Retrieve chat message from database and split the message into an array, using the | as splitting value ### $message = SplitToArray( GetStoredValue( "CHAT_MSG_" . $display_message ),"|" ); ### End ### IF ( $message[0] != "" ) THEN ### Draw buttons to display date, time and nickname of the chat message ### openPrivButton( "cross_srv_chat_msg_date_".$display_message,$left_right_cross_srv_chat+1,$top_bottom_cross_srv_chat,13,5,5,-1,32,"^7".$message[0] ); openPrivButton( "cross_srv_chat_msg_time_".$display_message,$left_right_cross_srv_chat+14,$top_bottom_cross_srv_chat,15,5,5,-1,32,"^7".$message[1] ); openPrivButton( "cross_srv_chat_msg_nick_".$display_message,$left_right_cross_srv_chat+29,$top_bottom_cross_srv_chat,23,5,5,-1,32,"^7".$message[2] ); ### End ### IF ( strlen ( $message[3] ) < 58 ) THEN ### If the length of the message is below 58 characters, display the message and raise $top_bottom_cross_srv_chat by 5 ### openPrivButton( "cross_srv_chat_msg_".$display_message,$left_right_cross_srv_chat+52,$top_bottom_cross_srv_chat,67,5,5,-1,32,"^7".$message[3] ); $top_bottom_cross_srv_chat = $top_bottom_cross_srv_chat + 5; ### End ### ELSE ### If the length of the message is 58 or above, start at position 55 to look for a space,comma or dot, to spread the message over 2 lines ### ### Space, comma or dot should be found between position 55 and 35, when found BREAK the WHILE-loop, if not found then lower position by 1 ### $position = 55; WHILE( $position >= 35 ) IF ( subStr( $message[3], $position, 1 ) == " " || subStr( $message[3], $position, 1 ) == "," || subStr( $message[3], $position, 1 ) == "." ) THEN BREAK; ELSE $position = $position - 1; ENDIF ENDWHILE ### End ### ### determin the exact length of the message, to know where the last character is ### $length = strlen ( $message[3] ); ### End ### ### Draw a background for the message and 2 button to display the splitted messages in ### openPrivButton( "cross_srv_chat_msg_bg_".$display_message,$left_right_cross_srv_chat+52,$top_bottom_cross_srv_chat,67,10,5,-1,32,"" ); openPrivButton( "cross_srv_chat_msg_a_".$display_message,$left_right_cross_srv_chat+52,$top_bottom_cross_srv_chat,67,5,5,-1,0,"^7".subStr( $message[3], 0, $position ) ); openPrivButton( "cross_srv_chat_msg_b_".$display_message,$left_right_cross_srv_chat+52,$top_bottom_cross_srv_chat+5,67,5,5,-1,0,"^7".subStr( $message[3], $position, $length ) ); ### End ### ### Raise $top_bottom_cross_srv_chat by 10, so next message won't overlap previous one ### $top_bottom_cross_srv_chat = $top_bottom_cross_srv_chat + 10; ### End ### ENDIF ENDIF ### Raise $display_message by 1 ### $display_message = $display_message + 1; ### End ### ENDWHILE ### End ### ### Reset value for $top_bottom_cross_srv_chat ### $top_bottom_cross_srv_chat=40; ### End ### IF ( GetCurrentPlayerVar( "chat_message" ) == "" ) THEN openPrivTextButton( "cross_srv_chat_msg",$left_right_cross_srv_chat+1,$top_bottom_cross_srv_chat+86,118,5,5,32,"^3Type message,press ENTER / click OK","^3Click to type a short message (max. 72 char)",72,proces_chat_msg ); ELSE openPrivTextButton( "cross_srv_chat_msg",$left_right_cross_srv_chat+1,$top_bottom_cross_srv_chat+86,118,5,5,32,"^3Type message,press ENTER / click OK","^3" . GetCurrentPlayerVar( "chat_message" ),72,proces_chat_msg ); ENDIF ### Only display SEND button when CurrentPlayerVar "chat_message" isn't empty ### IF ( GetCurrentPlayerVar( "chat_message" ) != "" ) THEN openPrivButton( "cross_srv_chat_send",$left_right_cross_srv_chat+1,$top_bottom_cross_srv_chat+91,118,5,5,-1,32,"^2Send message",send_chat_msg ); ENDIF ### End ### EndSub Sub proces_chat_msg ( $chat_message_value,$text ) ### Collect message tgat has been entered by a user ### $chat_message=$text; SetCurrentPlayerVar( "chat_message",$chat_message ); ### End ### ### Close the send button (in case no message has been entered, there would be nothing to send) and go back to Sub Chat ### closePrivButton( "cross_srv_chat_send" ); Chat( $KeyFlags,$id ); ### End ### EndSub Sub send_chat_msg ( $KeyFlags,$id ) ### Close buttons of the previously displayed messages ### $display_message = $chat_msg_old; WHILE( $display_message <= $chat_msg_new ) closePrivButton( "cross_srv_chat_msg_date_".$display_message ); closePrivButton( "cross_srv_chat_msg_time_".$display_message ); closePrivButton( "cross_srv_chat_msg_nick_".$display_message ); closePrivButton( "cross_srv_chat_msg_".$display_message ); closePrivButton( "cross_srv_chat_msg_bg_".$display_message ); closePrivButton( "cross_srv_chat_msg_a_".$display_message ); closePrivButton( "cross_srv_chat_msg_b_".$display_message ); $display_message = $display_message + 1; ENDWHILE ### End ### ### Get current last chat number and raise that by 1 ### $chat_msg_nbr = GetStoredNum( "CHAT_MSG_NBR" ); $chat_msg_nbr = $chat_msg_nbr + 1; ### End ### ### Get current time, date and the chat message and piece them together for storage using | as seperator ### $time = GetLapperVar( "longtime" ); $date = GetLapperVar( "shortdate" ); $nick = GetCurrentPlayerVar( "NickName" ); $message = $date . "|" . $time . "|" . $nick . "|" . GetCurrentPlayerVar( "chat_message" ); ### End ### ### Store chat message and new chat number ### SetStoredValue( "CHAT_MSG_" . $chat_msg_nbr , $message ); SetStoredValue( "CHAT_MSG_NBR" , $chat_msg_nbr ); $chat_msg_new = $chat_msg_nbr; SetStoredValue( "CHAT_MSG_NEW" , $chat_msg_new ); ### End ### ### Calculate number of chat messages in database and raise that number by 1 ### $nbr_of_messages = ($chat_msg_new - $chat_msg_old) + 1; ### End ### ### Check if number of messages exceeds maximum number of messages and if so, go through WHILE-loop to delete old message ### IF ( $nbr_of_messages >= $chat_msg_max ) THEN WHILE( $nbr_of_messages > $chat_msg_max ) DeleteStoredValue( "CHAT_MSG_" . $chat_msg_old ); $chat_msg_old = $chat_msg_old + 1; SetStoredValue( "CHAT_MSG_OLD" , $chat_msg_old ); $nbr_of_messages = ($chat_msg_new - $chat_msg_old) + 1; ENDWHILE ENDIF ### End ### ### Empty PlayerVar containing chat message, close Send button, go back to Sub Chat ### SetCurrentPlayerVar( "chat_message","" ); closePrivButton( "cross_srv_chat_send" ); Chat( $KeyFlags,$id ); ### End ### EndSub Sub send_chat_msg_direct ( $argv ) ### Close buttons of the previously displayed messages ### $display_message = GetCurrentPlayerVar( "chat_message_old" ); WHILE( $display_message <= $chat_msg_new ) closePrivButton( "cross_srv_chat_msg_date_".$display_message ); closePrivButton( "cross_srv_chat_msg_time_".$display_message ); closePrivButton( "cross_srv_chat_msg_nick_".$display_message ); closePrivButton( "cross_srv_chat_msg_".$display_message ); closePrivButton( "cross_srv_chat_msg_bg_".$display_message ); closePrivButton( "cross_srv_chat_msg_a_".$display_message ); closePrivButton( "cross_srv_chat_msg_b_".$display_message ); $display_message = $display_message + 1; ENDWHILE ### End ### ### Get current last chat number and raise that by 1 ### $chat_msg_nbr = GetStoredNum( "CHAT_MSG_NBR" ); $chat_msg_nbr = $chat_msg_nbr + 1; ### End ### ### Get current time, date and the chat message and piece them together for storage using | as seperator ### $time = GetLapperVar( "longtime" ); $date = GetLapperVar( "shortdate" ); $nick = GetCurrentPlayerVar( "NickName" ); $message = $date . "|" . $time . "|" . $nick . "|" . $argv; ### End ### ### Store chat message and new chat number ### SetStoredValue( "CHAT_MSG_" . $chat_msg_nbr , $message ); SetStoredValue( "CHAT_MSG_NBR" , $chat_msg_nbr ); $chat_msg_new = $chat_msg_nbr; SetStoredValue( "CHAT_MSG_NEW" , $chat_msg_new ); ### End ### ### Calculate number of chat messages in database and raise that number by 1 ### $nbr_of_messages = ($chat_msg_new - $chat_msg_old) + 1; ### End ### ### Check if number of messages exceeds maximum number of messages and if so, go through WHILE-loop to delete old message ### IF ( $nbr_of_messages >= $chat_msg_max ) THEN WHILE( $nbr_of_messages > $chat_msg_max ) DeleteStoredValue( "CHAT_MSG_" . $chat_msg_old ); $chat_msg_old = $chat_msg_old + 1; SetStoredValue( "CHAT_MSG_OLD" , $chat_msg_old ); $nbr_of_messages = ($chat_msg_new - $chat_msg_old) + 1; ENDWHILE ENDIF ### End ### ### Go back to Sub Chat ### Chat( $KeyFlags,$id ); ### End EndSub Sub refresh_chat ( $KeyFlags,$id ) ### Close buttons of the previously displayed messages ### WHILE( $display_message <= $chat_msg_new ) closePrivButton( "cross_srv_chat_msg_date_".$display_message ); closePrivButton( "cross_srv_chat_msg_time_".$display_message ); closePrivButton( "cross_srv_chat_msg_nick_".$display_message ); closePrivButton( "cross_srv_chat_msg_".$display_message ); closePrivButton( "cross_srv_chat_msg_bg_".$display_message ); closePrivButton( "cross_srv_chat_msg_a_".$display_message ); closePrivButton( "cross_srv_chat_msg_b_".$display_message ); $display_message = $display_message + 1; ENDWHILE ### End ### ### Retrieve current values for chat_msg_old, chat_msg_new and chat_msg_nbr ### $chat_msg_old = GetStoredNum( "CHAT_MSG_OLD" ); $chat_msg_new = GetStoredNum( "CHAT_MSG_NEW" ); $chat_msg_nbr = GetStoredNum( "CHAT_MSG_NBR" ); ### End ### ### Set value for $display_message, set new value for $top_bottom_cross_srv_chat ### $display_message = $chat_msg_old; $left_right_cross_srv_chat=15; $top_bottom_cross_srv_chat=40; ### End ### ### Give CurrentPlayerVar "chat_message_old" a value, so it can be used when closing chat, while latest message(s) hasn't/haven't appeared yet ### SetCurrentPlayerVar( "chat_message_old",$chat_msg_old ); ### End ### openPrivButton( "refresh_cross_srv_chat",$left_right_cross_srv_chat+1,$top_bottom_cross_srv_chat+1,5,5,5,15,0,"^3%cpt%",refresh_chat ); $top_bottom_cross_srv_chat=51; ### Start WHILE-loop used to display the actual chat messages ### WHILE( $display_message <= $chat_msg_new ) ### Retrieve chat message from database and split the message into an array, using the | as splitting value ### $message = SplitToArray( GetStoredValue( "CHAT_MSG_" . $display_message ),"|" ); ### End ### IF ( $message[0] != "" ) THEN ### Draw buttons to display date, time and nickname of the chat message ### openPrivButton( "cross_srv_chat_msg_date_".$display_message,$left_right_cross_srv_chat+1,$top_bottom_cross_srv_chat,13,5,5,-1,32,"^7".$message[0] ); openPrivButton( "cross_srv_chat_msg_time_".$display_message,$left_right_cross_srv_chat+14,$top_bottom_cross_srv_chat,15,5,5,-1,32,"^7".$message[1] ); openPrivButton( "cross_srv_chat_msg_nick_".$display_message,$left_right_cross_srv_chat+29,$top_bottom_cross_srv_chat,23,5,5,-1,32,"^7".$message[2] ); ### End ### IF ( strlen ( $message[3] ) < 58 ) THEN ### If the length of the message is below 58 characters, display the message and raise $top_bottom_cross_srv_chat by 5 ### openPrivButton( "cross_srv_chat_msg_".$display_message,$left_right_cross_srv_chat+52,$top_bottom_cross_srv_chat,67,5,5,-1,32,"^7".$message[3] ); $top_bottom_cross_srv_chat = $top_bottom_cross_srv_chat + 5; ### End ### ELSE ### If the length of the message is 58 or above, start at position 55 to look for a space,comma or dot, to spread the message over 2 lines ### ### Space, comma or dot should be found between position 55 and 35, when found BREAK the WHILE-loop, if not found then lower position by 1 ### $position = 55; WHILE( $position >= 35 ) IF ( subStr( $message[3], $position, 1 ) == " " || subStr( $message[3], $position, 1 ) == "," || subStr( $message[3], $position, 1 ) == "." ) THEN BREAK; ELSE $position = $position - 1; ENDIF ENDWHILE ### End ### ### determin the exact length of the message, to know where the last character is ### $length = strlen ( $message[3] ); ### End ### ### Draw a background for the message and 2 button to display the splitted messages in ### openPrivButton( "cross_srv_chat_msg_bg_".$display_message,$left_right_cross_srv_chat+52,$top_bottom_cross_srv_chat,67,10,5,-1,32,"" ); openPrivButton( "cross_srv_chat_msg_a_".$display_message,$left_right_cross_srv_chat+52,$top_bottom_cross_srv_chat,67,5,5,-1,0,"^7".subStr( $message[3], 0, $position ) ); openPrivButton( "cross_srv_chat_msg_b_".$display_message,$left_right_cross_srv_chat+52,$top_bottom_cross_srv_chat+5,67,5,5,-1,0,"^7".subStr( $message[3], $position, $length ) ); ### End ### ### Raise $top_bottom_cross_srv_chat by 10, so next message won't overlap previous one ### $top_bottom_cross_srv_chat = $top_bottom_cross_srv_chat + 10; ### End ### ENDIF ENDIF ### Raise $display_message by 1 ### $display_message = $display_message + 1; ### End ### ENDWHILE ### End ### EndSub Sub close_chat ( $KeyFlags,$id ) closePrivButton( "cross_srv_chat_bg" ); closePrivButton( "cross_srv_chat_title" ); closePrivButton( "refresh_cross_srv_chat" ); closePrivButton( "cross_srv_chat_close" ); closePrivButton( "cross_srv_chat_msg" ); closePrivButton( "cross_srv_chat_send" ); $display_message = GetCurrentPlayerVar( "chat_message_old" ); WHILE( $display_message <= $chat_msg_new ) closePrivButton( "cross_srv_chat_msg_date_".$display_message ); closePrivButton( "cross_srv_chat_msg_time_".$display_message ); closePrivButton( "cross_srv_chat_msg_nick_".$display_message ); closePrivButton( "cross_srv_chat_msg_".$display_message ); closePrivButton( "cross_srv_chat_msg_bg_".$display_message ); closePrivButton( "cross_srv_chat_msg_a_".$display_message ); closePrivButton( "cross_srv_chat_msg_b_".$display_message ); $display_message = $display_message + 1; ENDWHILE EndSub