*who's wanted register agents
*THINGS THE WWR Checker Does:
* Tracks when users go on and offline
* Actually hosts the WWR
* Adds and remove users from an agent's WWR
* Sends messages to agents that request online/offline notifications whwn those notifications occur
* Counts and updates the number of users and friends that are online

*check before continuing... we really don't want more than one of you:
doif totl 1 2 35700 gt 0
	stop
endi

new: simp 1 2 35700 "blnk" 1 0 0
tick 100
seta game "aw_wwr_agent" targ
setv ov00 0

scrp 1 2 35700 9
*There are took many tasks for this agent to handle quickly on its own--
*so it's going to alternate tasks each time
	inst
	doif game "aw_status" = "offline"
		stop
	endi

*	dbg: outs "The WWR is doing its best"

	doif ov00 eq 1
*	refresh the special contacts
		gsub update_special_contacts
*	dbg: outs "updating special contacts"
		setv ov00 0
	else
*	check the WWRs

*	subtract from the newly-online indicator
		doif ov02 gt 0
			subv ov02 1
		endi
*	dbg: outs "newly online status is"
*	dbg: outv mv02

*	dbg: outs "checking the WWR"
		gsub check_wwr
		setv ov00 1
	endi

*This is the script that checks all the "_status" game variables and sees if any of them have 
*changed since the last script-tick.
	subr check_wwr
		setv va02 stoi caos 0 0 "wwr" targ game "f_LengthofVList" 0 0 va99
*	dbg: outs "wwrs found"
*	dbg: outv va02
		doif va02 gt 0
			setv va01 0
			reps va02
				addv va01 1
*			get the user namevar:
				sets va03 caos 0 1 "wwr" va01 game "f_GetVarInVList" 0 0 va99
*			get the username itself 
				sets va04 name va03
*			see if your online status has changed!
				sets va05 va03
				adds va05 "_status"
				setv va06 name va05
*			va06 is now the old status!
*			see if this has changed! Set the new status
				setv name va05 stoi caos 0 0 va04 0 game "f_aw_ulin" 0 1 va99
*				this incorporates the newly online indicator (mv02)
*				so it doesn't notify you everyone is online when you
*				just logged in!
				doif name va05 ne va06 and mv02 le 0
*				something is different, gasp
*				you have to tell your agents!
*				first make sure you have agents...
					setv va07 stoi caos 0 0 va03 ownr game "f_LengthofVList" 0 0 va99
					doif va07 gt 0
						setv va08 0
						reps va07
							addv va08 1
*						get the agent name:
							sets va09 caos 0 1 va03 va08 game "f_GetVarInVList" 0 0 va99
*						sets the agent name
							doif type name va09 gt 3
								doif name va09 ne null
									seta va10 name va09
*							now you gotta tell the agent what's up!
*						if status is 0, set the "went offline" message
									doif name va05 eq 0
										dbg: outs "sending an offline message about"
										dbg: outs va04
										mesg wrt+ va10 138 va04 0 0
									elif name va05 eq 1
*								if status is 1, set the "went online" message!
										dbg: outs "sending an online message about"
										dbg: outs va04
										mesg wrt+ va10 137 va04 0 0
									endi
*							That agent wasn't found!! It must not exist anymore.
								else

								endi
*							that agent was cleared. Apparently badly or improperly.
							else
							endi
						repe
					endi
				endi
			repe
		endi
	retn


	subr update_special_contacts
		mesg writ ownr 1006
		mesg writ ownr 1007
	retn
endm
*WWR test entries:
scrp 1 2 35700 137
	inst
	sets va00 "According to the WWR, "
	adds va00 _p1_
	adds va00 " has just come online."
	dbg: outs va00
endm

scrp 1 2 35700 138
	inst
	sets va00 "According to the WWR, "
	adds va00 _p1_
	adds va00 " has just gone offline."
	dbg: outs va00
endm

scrp 1 2 35700 1001
*sets number of real, actual contacts into game "contact_count"
	inst
*update the number of contacts you currently have
	sets va40 ""
	sets va41 "_contact"
	setv va45 0
	loop
		sets va42 gamn va40
*			see if string contains search
		setv va43 sins va42 1 va41
*			if it does, and is not an automation:
		doif va43 ne -1
			doif subs va42 1 1 ne "!" and subs va42 1 1 ne "_"
				addv va45 1
			endi
		endi
*	set 'previous entry' to current entry
		sets va40 va42
	untl va42 eq ""
	setv game "contact_count" va45
endm

*add someone to the WWR
*_p1_ (string) is the name, _p2_ (agent) is the agent
scrp 1 2 35700 1002
	inst
*	dbg: outs "HEY WE ARE ADDING SOMEONE HERE"
*	exclusive add the username to the list:
	sets va02 caos 0 1 "wwr" _p1_ game "f_ExAddtoVList" 0 0 va99
*	and then set its status so we know if it changes
	sets va03 va02
	adds va03 "_status"
	setv name va03 stoi caos 0 0 _p1_ 0 game "f_aw_ulin" 0 1 va99
*	exclusive add the agent to the usernames's sublist
	sets va04 caos 0 1 va02 _p2_ game "f_ExAddtoVList" 0 0 va99

*	dbg: outs va03
	dbg: outs _p1_
	dbg: outs "was added to the wwr for agent:"
	targ _p2_
	dbg: outv spcs
*	dbg: outs "new length:"
*	dbg: outs caos 0 0 "wwr" ownr game "f_LengthofVList" 1 1 va99
endm

*remove someone from the WWR
*_p1_ (string) is the name, _p2_ (agent) is the agent
scrp 1 2 35700 1003
	inst
*	find the username in the list:
	sets va02 caos 0 1 "wwr" _p1_ game "f_SearchVList" 0 0 va99
*	remove agent from the sublist:
	sets va04 caos 0 1 va02 _p2_ game "f_SearchVList" 0 0 va99
	doif strl va04 gt strl va02
		sets va03 caos 0 1 va02 va04 game "f_RemoveFromVList" 0 0 va99
	endi
*	see if your list is empty!
	doif stoi caos 0 0 va02 targ game "f_LengthofVList" 0 0 va99 eq 0
*	remove your own entry:
		sets va05 caos 0 1 "wwr" va02 game "f_RemoveFromVList" 0 0 va99
	endi
endm

*net: whoz
scrp 1 2 35700 1004
	inst
*	p1 is the agent
*	for each in "wwr"
	setv va00 stoi caos 0 0 "wwr" ownr game "f_LengthofVList" 0 0 va99
	doif va00 eq 0
		stop
	endi
	setv va02 0
	reps va00
		addv va02 1
		sets va06 "wwr_"
		adds va06 vtos va02
*		see if agent is in sublist
		sets va04 caos 0 1 va06 _p1_ game "f_SearchVList" 1 0 va99
*		remove it
		doif strl va04 gt strl va06
			sets va03 caos 0 1 va06 va04 game "f_RemoveFromVList" 1 0 va99
		endi
*		see if your list is empty!
		doif stoi caos 0 0 va06 targ game "f_LengthofVList" 1 0 va99 eq 0
*		remove your own entry:
			sets va05 caos 0 1 "wwr" va02 game "f_RemoveFromVList" 1 0 va99
		endi
	repe

endm

*	net: whod
scrp 1 2 35700 1005
	inst

	sets va00 "Who's Wanted Registers:\n"
*	for each in "wwr"
	setv va10 stoi caos 0 0 "wwr" ownr game "f_LengthofVList" 1 0 va99
	doif va10 eq 0
		outs "WWR is empty!"
		stop
	endi
	setv va01 0

	reps va10
		addv va01 1
		sets va03 caos 0 1 "wwr" va01 game "f_GetVarInVList" 1 0 va99
		adds va00 name va03
		adds va00 ":"
*		dbg: outs "hello"
		setv va06 stoi caos 0 0 va03 ownr game "f_LengthofVList" 1 0 va99
*		dbg: outs "hellooo"
		setv va05 0
		doif va06 gt 0
			reps va06
				addv va05 1
				sets va07 caos 0 1 va03 va05 game "f_GetVarInVList" 1 0 va99
				doif type name va07 gt 2
					targ name va07
					adds va00 vtos fmly
					adds va00 " "
					adds va00 vtos gnus
					adds va00 " "
					adds va00 vtos spcs
					adds va00 " ("
					adds va00 vtos unid
					adds va00 ") "
					targ ownr
				else
					adds va00 "this isn't an agent for some reason"
				endi
			repe
		endi
		adds va00 "\n"
	repe
	dbg: outs va00
	outs va00
endm

scrp 1 2 35700 1006
*sets number of real, actual online contacts into game "online_user_count"
*also updates game "!net: ruso_status"
	inst
*update the number of contacts you currently have
	sets va40 ""
	sets va41 "_contact"
	setv va45 0
	loop
		sets va42 gamn va40
*			see if string contains search
		setv va43 sins va42 1 va41
*			if it does, and is not an automation:
		doif va43 ne -1
			doif subs va42 1 1 ne "!" and subs va42 1 1 ne "_"
*get the actual username from the string
				setv va46 strl va42
				subv va46 strl va41
				sets va47 subs va42 1 va46
				doif stoi caos 0 0 va47 0 game "f_aw_ulin" 0 1 va99 eq 1
					addv va45 1
*dbg: outs va47
				endi
			endi
		endi
*	set 'previous entry' to current entry
		sets va40 va42
	untl va42 eq ""
	setv game "online_user_count" va45
	doif va45 gt 0
		sets game "!net: ruso_status" "online"
	else
		sets game "!net: ruso_status" "offline"
*		dbg: outs "no one is online"
	endi
endm

scrp 1 2 35700 1007
*sets number of online friends into game "online_friend_count"
*also updates game "!friend_status"
	inst
*update the number of contacts you currently have
	sets va40 ""
	sets va41 "_contact"
	setv va45 0
	loop
		sets va42 gamn va40
*			see if string contains search
		setv va43 sins va42 1 va41
*			if it does, and is not an automation:
		doif va43 ne -1
			doif subs va42 1 1 ne "!" and subs va42 1 1 ne "_"
*get the actual username from the string
				setv va46 strl va42
				subv va46 strl va41
				sets va47 subs va42 1 va46
				doif stoi caos 0 0 va47 0 game "f_aw_ulin" 0 1 va99 eq 1
					adds va47 "_group"
					doif game va47 eq 2
						addv va45 1
					endi
*dbg: outs va47
				endi
			endi
		endi
*	set 'previous entry' to current entry
		sets va40 va42
	untl va42 eq ""
	setv game "online_friend_count" va45
	doif va45 gt 0
		sets game "!friend_status" "online"
	else
		sets game "!friend_status" "offline"
	endi
endm

*A user has changed status (only to be called by the AW Client)
* _p1_ is the username, _p2_ is either "online" or "offline" 
*Example: mesg wrt+ game "aw_wwr_agent" 1008 "Verm" "online" 0
scrp 1 2 35700 1008
	inst
	dbg: outs "Manually Triggered WWR Event for"
	dbg: outs _p1_
*	set the gamevar status
	sets va11 _p1_
	adds va11 "_status"
	sets game va11 _p2_
	dbg: outs va11
	dbg: outs game va11

*find that username in the WWR:
	sets va02 caos 0 1 "wwr" _p1_ game "f_SearchVList" 0 0 va99
*if the username is real:
	doif va02 ne "#N#"
*		check your agents!
		setv va07 stoi caos 0 0 va02 ownr game "f_LengthofVList" 0 0 va99
		doif va07 gt 0
*dbg: outs "User is accountable to this many agents:"
*dbg: outv va07
			setv va08 0
			reps va07
				addv va08 1
*					get the agent name:
				sets va09 caos 0 1 va02 va08 game "f_GetVarInVList" 0 0 va99
*					sets the agent name
				doif name va09 ne null
					seta va10 name va09
*					now you gotta tell the agent what's up!
*					if status is 0, set the "went offline" message
					doif lowa _p2_ eq "offline"
						dbg: outs "sending an offline message about"
						dbg: outs _p1_
						sets va05 va02
						adds va05 "_status"
						setv name va05 0
						mesg wrt+ va10 138 _p1_ 0 0
					elif lowa _p2_ eq "online"
*							if status is 1, set the "went online" message!
						dbg: outs "sending an online message about"
						dbg: outs _p1_
						sets va05 va02
						adds va05 "_status"
						setv name va05 1
						mesg wrt+ va10 137 _p1_ 0 0
					endi
*						That agent wasn't found!! It must not exist anymore.
				else

				endi
			repe
		else
			dbg: outs "This user is not in the WWR for any agents"
		endi

	else
		dbg: outs "This user is not in the WWR"
	endi
endm