TGC Codebase Backup



GMan's chatroom by GMan

16th Sep 2006 13:22
Summary

This code will create a basic chat window which allows up to 255 users. It is simple and easy to understand, so it should be easy to edit. fully commented.



Description

=======================================================================================
IMPORTANT NOTE:
The command 'sync off' was used. Sync was turned off because that way the chat window will take up 0% of the cpu instead of 100% which is the case if it is on. So, if you use this code make sure to turn it on (or delete the 'sync off' line) if you need it.
=======================================================================================
=======================================================================================


If there are any questions or if anyone would like to try this out by connecting to me in the program accross the internet, contact me at 'laptop-44@hotmail.com'
Feel free to use this code in anyway you like. I won't ask for it, but any credit would, however, be appreciated.



I have been informed that some users have trouble with blocked ports, and possibly firewalls. However, I use Norton Interned Security, and I have never encountered a problem, even when connecting to computers across the internet that are not on my network. This is why I will be happy to help anyone test out if their ports, firewall, ect. are properly configured, by connecting to me(above).



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `SET WINDOW
set display mode 640, 480, 32, 0
set window on
set window size 640, 480
set window title "GMan's messenger"
`WILL MAKE APPLICATION RUN AT 0% CPU INSTEAD OF 90-100%, MAKE SURE TO TURN SYNC ON IF IT IS NEEDED.
sync off

`ALL CHAT MESSAGES SENT/RECEIVED AFTER MAXIMUM WILL CAUSE SCROLLING
maxMessages=10
`maxMessages IS REDUCED BECAUSE THE VALUE IS USED IN AN ARRAY.  AN ARRAY OF
`10 MESSAGES IS 0-9 SO 9 BECOMES THE ACTUAL MAXIMUM VALUE.  TO EDIT MAX MESSAGES
`LEAVE THE FOLLOWING LINE ALONE, BUT EDIT ABOVE.
maxMessages=maxMessages-1
`ASK FOR USER NAME
input "name: ", name$



`SET THE NET CONNECTION TO "INTERNET TCP/IP"
perform checklist for net connections
for a=1 to checklist quantity()
   if checklist string$(a)="Internet TCP/IP Connection For DirectPlay"
      connection=a
   endif
next a


`USE 127.0.0.1 IF CONNECTING TO YOURSELF, IT IS THE LOOPBACK ADDRESS.
input "IP: ", ip$
set net connection connection, ip$


`CHECK FOR SERVERS RUNNING ON CONNECTION.  IF NONE EXIST CREATE ONE.
`IF YOU WANT MORE THAN ONE CHAT SESSION ON A SINGLE CONNECTION EDIT BELOW.
perform checklist for net sessions
if checklist quantity()=1
   join net game 1, name$
else
   create net game "chat", name$, 255
endif
cls


`CREATES AN ARRAY TO STORE ALL MESSAGES SENT AND RECEIVED
dim messages$(maxMessages+1)
temp$=""
message$="[ "+name$+" ] HAS JOINED"
messages$(0)=message$
send net message string 0, message$
new=1
`NEW STORES THE CURRENT VALUE IN THE ARRAY.  IT IS SET TO ONE BECAUSE WE
`ALREADY USED messages$(0) TO SEND A GREETING MESSAGE.
clear entry buffer

do
cls

`SCANCODE 14 IS THE BACKSPACE KEY.  IF IT IS PRESSED WE NEED TO DELETE A CHARACTER.
   if scancode()=14
      temp$=temp$+entry$()
      if len(temp$)>1 then temp$=left$(temp$, len(temp$)-1)
      repeat : until scancode()=0
      clear entry buffer
   endif

`THE ENTRY BUFFER STORES ALL KEYSTROKES.
   typing$=temp$+entry$()

`PRINT THE CURRENT MESSAGE BEING TYPED BY THE USER.
   text 0, 400, typing$

`IF THE ENTER KEY IS PRESSED WE WILL SEND THE MESSAGE.
   if returnkey()=1
      message$=name$+": "+typing$
      temp$=""
      clear entry buffer
      send net message string 0, message$
`THE FOLLOWING IF STATEMENT WILL SIMPLY CHECK IF THE ARRAY IS NOT FULL AND STORE THE NEW MESSAGE IN IT.
`IF IT IS FULL WE SCROLL ALL MESSAGES UP ONE SPACE IN THE ARRAY AND STORE
`THE NEW MESSAGE IN THE LAST SPACE.
      if new>maxMessages
         for a=0 to maxMessages-1
            messages$(a)=messages$(a+1)
         next a
         messages$(maxMessages)=message$
      else
         messages$(new)=message$
         new=new+1
      endif
      repeat
      until returnkey()=0
   endif
`NOW WE GET ANY CHAT MESSAGES SENT TO US
   get net message
   received$=net message string$()
   if received$>""
`SCROLL MESSAGES
      if new>maxMessages
         for a=0 to maxMessages-1
            messages$(a)=messages$(a+1)
         next a
         messages$(maxMessages)=received$
      else
         messages$(new)=received$
         new=new+1
      endif
   endif

`DISPLAY THE ARRAY CONTAINING ALL SENT AND RECEIVED MESSAGES.
   for a=0 to maxMessages
      text 0, (20*a), messages$(a)
   next a
loop