MMOwned - World of Warcraft Exploits, Hacks, Bots and Guides  
Homepage Register FAQ Members Mark Forums Read Advertise Marketplace FPSowned


Go Back   MMOwned - World of Warcraft Exploits, Hacks, Bots and Guides > Programming > Programming section > Misc
Reload this Page [DarkBASIC] Making an Instant Messenger
Misc For other languages that don't fit elsewhere like KuRIoSaN or Lavishscript - PROGRAMMING WISE

Reply
 
LinkBack Thread Tools
[DarkBASIC] Making an Instant Messenger
(#1)
Old
ReidE96's Avatar
ReidE96 is Offline
Contributor
Rep Power: 4
Reputation: 272
ReidE96 is a jewel in the roughReidE96 is a jewel in the roughReidE96 is a jewel in the rough
 
Posts: 805
Join Date: Dec 2006
Location: Scotland
Essay [DarkBASIC] Making an Instant Messenger - 03-23-2008

Spinny cubes are fun and all, but of limited practical value. So here's an Instant Messenger, coded in DarkBASIC. The comments (REM or ` for each comment line, REMSTART for the beginning of a block of comments (no REM needed in the block) and REMEND for the end of a block), should help you understand how it all works.

Code:
REMSTART
Project: Simplechat
Rem Created: 05/05/2006 17:09:03

***** Main Source File *****
REMEND

` Set the resolution to 800 by 600, 32 bit
SET DISPLAY MODE 800, 600, 32

` Set the screen refresh rate to 30 Hz
SYNC ON
SYNC RATE 30

` Keep track of what was said and who said it (declaring a few arrays)
DIM ChatText$(32)
DIM PlayersName$(1)
DIM NumberOfPlayers(1)
DIM LastNumberOfPlayers(1)

` Find the TCP/IP connection number with a function we'll write later
TcpIpNumber = FindTCPIPConnectionNumber()

` Print the text in the "" to the screen, then a blank line
PRINT "Welcome to SimpleChat! Press the Escape key to exit."
PRINT

` Make the screen reload
SYNC

` Create a GOTO point in case they don't enter a name
:name

` Get the user's name and store it in the MyName variable
INPUT "Please enter your name. ", MyName$

` Refresh the screen a couple of times
SYNC
SYNC

` Check they entered a name
If MyName$ = ""
   ` If they didn't, tell them they need to
   PRINT "You need to enter a name!"

   ` Wait for the user to press a key
   WAIT KEY

   ` Get them to enter a name again
   GOTO name

` Close our IF
ENDIF

` Store the name in the array
PlayersName$(1) = MyName$

` Find out whether they're the host or client

` Throw in a blank line
PRINT

` Ask ze question
PRINT "Are you the host or client? (Press 1 or 2 to choose)"

` Display the possible answers
PRINT "(1) I'm the Host"
PRINT "(2) I'm the Client"

` Refresh the screen a couple of times
SYNC
SYNC

` Set a variable for what key they press
A$ = ""

` Set a variable to get their answer
Answer = 0

` Get their answer

` Loop till they choose something
WHILE Answer = 0
   ` Make the key variable whatever key they press
   A$ = INKEY$()

   ` If they press 1, set the answer to 1   
   IF A$ = "1" THEN Answer = 1

   ` If they press 2, set the answer to 2
   IF A$ = "2" THEN Answer = 2

` Close the loop
ENDWHILE

` Show a new line
PRINT

` Tell them how to exit
PRINT "Press the Escape key to quit."

` If they're the host, do the hosty stuff
IF Answer = 1

   ` Tell them we're setting up the session
   PRINT "Creating chat session. Please wait"

   ` Refresh the screen
   SYNC

   ` Wait 0.2 seconds
   SLEEP 200

   ` Create a new connection on the port we picked up earlier
   SET NET CONNECTION TcpIpNumber, " "

   ` Create a net "game", as DarkBASIC calls it (t is made for games, after all). We're calling the game "Chat" and their player name will be the name they chose earlier. We're allowing 16 people to be in the room at once, and the flag is 1, making this peer-to-peer (P2P)
   CREATE NET GAME "Chat", MyName$, 16, 1

` Close our If statement
ENDIF

` If they're the client, do the clienty stuff
IF Answer = 2
   ` Create a GOTO point, just in case they screw up
   :client

   ` Ask for an IP to connect to and store it in the AddressData variable
   INPUT "Please enter the host's IP address. ", AddressData$

   ` Tell them we're connecting
   PRINT "Connecting to chat session. Please wait"

   ` Update the screen
   SYNC

   ` Create a net connection using the number we picked up earlier to the IP they just provided
   SET NET CONNECTION TcpIpNumber, AddressData$

   ` Ask the host how many "games" it has open
   PERFORM CHECKLIST FOR NET SESSIONS

   ` Store the number of games in a variable
   NumberOfGames = CHECKLIST QUANTITY()

   ` If there's no "games"
   IF NumberOfGames = 0

      ` Tell the user there's no games
      PRINT "No chat session found at that address"

      ` Update the screen
      SYNC

      ` Wait for the user to press a key
      WAIT KEY

      ` Go to the :client GOTO point
      GOTO client

   ` Close our IF
   ENDIF

   ` If we got past the IF, we've found a game, so connect to it. There's only ever going to be 1 instance of this on the host, so it's game number will be 1. Our player name will be the one they chose earlier
   JOIN NET GAME 1, MyName$

   ` Tell them we've connected
   PRINT "Connected to session!"

   ` Update the screen
   SYNC

` Close our IF
ENDIF

` The prep is complete, so we'll run our main chat client, which is a function. Yay!
ChatClient()

` End the program (because the chat client function will keep going until they leave)
END

` This function will determine which NET CONNECTION number is TCP/IP
FUNCTION FindTCPIPConnectionNumber()
   ` Create a variable to hold the number
   Flag = 0

   ` Clear the screen
   CLS

   ` Get a list of current net connections
   PERFORM CHECKLIST FOR NET CONNECTIONS

   ` Start a FOR loop for every item in the checklist
   FOR x = 1 TO CHECKLIST QUANTITY()

      ` Get the name of the current item in the checklist
      Service$ = CHECKLIST STRING$(x)

      ` See if the first 15 letters of the name are "Internet TCP/IP"
      IF LEFT$(Service$,15) = "Internet TCP/IP"
         ` If they are, set the flag to the current number
         Flag = x

      ` End the IF
      ENDIF

   ` Move on to the next item in the checklist
   NEXT x

` This ends the function, and returns the value of flag
ENDFUNCTION Flag

`This function has all the chat functionality
FUNCTION ChatClient()

   ` Clear the chat text from the array (another function!)
   ClearChatText()

   ` Display the initial chatters in the room
   
   ` Get a list of people in the "game"
   PERFORM CHECKLIST FOR NET PLAYERS

   ` Store the number of players in a variable
   NumberOfPlayers(1) = CHECKLIST QUANTITY()

   ` Start a FOR loop for every entry on the checklist
   FOR x = 1 TO NumberOfPlayers(1)
      ` Use ANOTHER function to show a list of who is here, for each entry on the list
      AddUserMessage(CHECKLIST STRING$(x))

   ` Move onto the next person in the list
   NEXT x

   `Send an entry message

   ` Set the variable C to their name and "has joined"
   C$ = PlayersName$(1)+" has joined."

   ` Send the message to the entire room (0 means everyone, a number would mean an individual in the room)
   SEND NET MESSAGE STRING 0,C$

   ` Displays the chat text with yet ANOTHER function
   DisplayChatText()

   ` Set the buffers for text entry
   A$ = ""
   B$ = ""
   C$ = ""

   ` Clear whatever key was last pressed from the buffer so it's ready
   CLEAR ENTRY BUFFER

   ` Capture Text Input and process it accordingly

   ` Start a WHILE loop that runs until Esc is pressed
   WHILE ESCAPEKEY() = 0
      ` Do the CheckIncomingMessages function
      CheckIncomingMessages()

      ` Set variable A to the key pressed
      A$ = INKEY$()

      ` If the ASCII value of the key pressed is 8 (the backspace key)
      IF ASC(A$) = 8
         ` Set the C variable to whatever it currently is and whatever is in the ENTRY buffer
         C$ = C$ + ENTRY$()

         ` Take the left x letter of C, x being one less than the current length of C
         C$ = LEFT$(C$,LEN(C$)-1)

         ` Clear the entry buffer
         CLEAR ENTRY BUFFER

         ` Clear the screen (to remove any traces of the backspaced letter)
         CLS

         ` Show the current chat text (yup, it's a function :D)
         DisplayChatText()

      ` Close our IF statement
      ENDIF

      ` Set the B variable to the C variable, plus whatever's currently in the ENTRY buffer
      B$ = C$ + ENTRY$()

      ` Show whatever's in B at x coordinate 10, y coordinate 460
      TEXT 10,460,B$

      ` If enter is pressed and the B variable isn't empty
      IF RETURNKEY()=1 AND B$ <> ""

         ` Wait for 0.25 seconds
         SLEEP 250

         ` Send the message

         ` Set variable D to the current user's name, a :, a space, and whatever's in B
         D$ = PlayersName$(1) + ": " + B$

         ` Send D to everyone in the room
         SEND NET MESSAGE STRING 0,D$

         ` Add the message to our own screen with ANOTHER FUNCTION!
         AddStringToChat(D$)

         ` Clear the D, B and C variables, along with the ENTRY buffer
         D$ = ""
         B$ = ""
         C$ = ""
         CLEAR ENTRY BUFFER

         ` Show the current chat text
         DisplayChatText()

      ` Close our IF
      ENDIF

      ` Update the screen
      SYNC

   ` Close the while
   ENDWHILE

`End the function, not returning a variable
ENDFUNCTION

` This function scans the incoming packets for strings of text and displays them
FUNCTION CheckIncomingMessages()
   ` This command gets the oldest message from the incoming message queue (any messages DarkBASIC sends are stored in a queue) and makes it the current message
   GET NET MESSAGE

   ` If there's no incoming packets, end the function
   IF NET MESSAGE EXISTS() = 0 THEN EXITFUNCTION

   ` While there IS a message
   WHILE NET MESSAGE EXISTS() <> 0
      ` Store the type of message (integer, float, string, memblock, image, bitmap, sound, or mesh) in the MsgType variable
      MsgType = NET MESSAGE TYPE()

      ` If the message is of type 3 (a string)
      IF MsgType = 3
         ` Set the Msg variable to the text from the message
         Msg$ = NET MESSAGE STRING$()

         ` Use the AddStringToChat function to add Msg to the chat
         AddStringToChat(Msg$)

         ` Run the DisplayChatText function
         DisplayChatText()

      ` End the IF
      ENDIF

      ` Check for a new net message (once processed, net messages are automagically deleted)
      GET NET MESSAGE

   ` End the WHILE loop
   ENDWHILE

` End the function, returning no variable
ENDFUNCTION

` This function displays messages saying who's in the room when you join, taking the Name variable from when it's called
FUNCTION AddUserMessage(Name$)
   ` Set the NewString variable to the Name variable and the phrase "is here"
   NewString$ = Name$ + " is here."

   ` Use the AddStringToChat function to add NewString to the chat
   AddStringToChat(NewString$)

` End the function
ENDFUNCTION

` This function adds a string to the chat, taking the variable a when it is called
FUNCTION AddStringToChat(a$)
   ` Start a FOR loop going from 1 to 32
   FOR x = 1 TO 32
      ` If the ChatText array at value x is blank
      IF ChatText$(x) = ""
         ` Set the text of the ChatText array at value x to a
         ChatText$(x) = a$
         ` Exit the function
         EXITFUNCTION

      ` End the IF statement
      ENDIF

   ` End the FOR loop
   NEXT x

   ` Start a FOR loop running from 32 to 2
   FOR x = 32 TO 2
      ` Set the variable y to 1 less then the current value of x
      y = x-1

      ` Set the value of the ChatText array at y to the value of the ChatText array at x (has the effect of moving everything up a line and scrolling the top line off the screen)
      ChatText$(y) = ChatText$(x)

   ` End the FOR loop
   NEXT x

   ` Set the 32nd value of the ChatText array to a (if we got to here, the ChatText array was full as the first function would have noticed a blank line and exited the function)
   ChatText$(32) = a$

` This marks the end of the function
ENDFUNCTION

` This function clears the ChatText$ array
FUNCTION ClearChatText()
   ` Starts a FOR loop from 1 to 32
   FOR x = 1 TO 32
      ` Clear the value of the ChatText array at the value of x
      ChatText$(x) = ""

   ` End the FOR loop
   NEXT x

` This marks the end of the funciton
ENDFUNCTION

` This function displays the chat text on the screen
FUNCTION DisplayChatText()
   ` Clear the screen
   CLS

   ` Sets the bitmap we're sending the text to to bitmap 0 (the screen)
   SET CURRENT BITMAP 0

   ` Centres the text "SimpleChat - Copyright(c) Euan Reid, 2006" at x coordinate 400 and y coordinate 10. This is from when I released it in 2006, you can change it if you want. This entire thing is just the source code from an app I made, with added comments to make things REALLY clear
   CENTER TEXT 400, 10, "SimpleChat - Copyright(c) Euan Reid, 2006"

   ` Starts a FOR loop from 1 to 32
   FOR x = 1 TO 32
      ` Show the value of ChatText at the value of x at x coordinate 10 and y coordinate 10 plus the current value of x times 15
      TEXT 10, 10+(x*15), ChatText$(x)

   ` Ends the FOR loop
   NEXT x

` This marks the end of the function
ENDFUNCTION
With any luck, this will help you learn some DarkBASIC. I coded this using DarkBASIC Professional v1.0, and it worked then. It also works in DarkBASIC Professional v1.03, as I just tested it. I don't know if it will work in DarkBASIC v1.x (the not-professional version) as it is far more limited as regards networking commands.

Happy coding!


Reply With Quote

Donate to remove ads.
(#2)
Old
warsheep is Offline
Contributor
Rep Power: 4
Reputation: 184
warsheep has a spectacular aura aboutwarsheep has a spectacular aura about
 
Posts: 1,178
Join Date: Sep 2006
03-27-2008

So.. This is like a client for a instant messenger?
Gotta download DarkBasic to test it out.

Edit:
Read through the well commented code... It looks great! Installing DarkBasic now, this sounds like something I'll have to check out!


<ToXiCa> warsheep + denmark + VIP lounge + cheese = love <3
FOR A MOMENT, NOTHING HAPPENED. THEN, AFTER A SECOND OR SO, NOTHING CONTINUED TO HAPPEN.

Last edited by warsheep; 03-27-2008 at 08:32 PM..
Reply With Quote
(#3)
Old
кιѕѕу's Avatar
кιѕѕу is Offline
Site Donator
Rep Power: 3
Reputation: 45
кιѕѕу is on a distinguished road
 
Posts: 214
Join Date: Jun 2006
Location: In The Upcoming Patc
06-24-2008

you might wanna check line 32
Code:
:name


Now doing Youtube view increasing, PM me with amount of views you want you'll offer in return
(No stupid amounts, you must have 50rep+. Video must be of good quality. I hold the right to refuse my service)

Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vBulletin Skin developed by: vBStyles.com


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382