 
 
 fetchFightMode = ((request, response) ->
+	# TODO: merge these two queries
+	# TODO: merge everything from uniusers into one SELECT
 	request.uonline.fight_mode = lib.game.isInFight.sync null, dbConnection, request.uonline.userid
+	request.uonline.autoinvolved_fm = lib.game.isAutoinvolved.sync null, dbConnection, request.uonline.userid
 ).asyncMiddleware()
 
 
 fetchArmor = ((request, response) ->
 	request.uonline.armor = lib.game.getUserArmor.sync null, dbConnection, request.uonline.userid
+).asyncMiddleware()
+
+
+fetchLocation = ((request, response) ->
+	try
+		location = lib.game.getUserLocation.sync null, dbConnection, request.uonline.userid
+		request.uonline.location_id = location.id
+		request.uonline.location_name = location.title
+		#request.uonline.pic = request.uonline.picture  if request.uonline.picture?  # TODO: LOLWHAT
+		request.uonline.description = location.description
+		request.uonline.ways = location.ways
+	catch e
+		console.error e.stack
+		location = lib.game.getInitialLocation.sync null, dbConnection
+		lib.game.changeLocation.sync null, dbConnection, request.uonline.userid, location.id
+).asyncMiddleware()
+
+
+fetchArea = ((request, response) ->
+	area = lib.game.getUserArea.sync null, dbConnection, request.uonline.userid
+	request.uonline.area_name = area.title
+).asyncMiddleware()
+
+
+fetchUsersNearby = ((request, response) ->
+	tmpUsers = lib.game.getNearbyUsers.sync null,
+		dbConnection, request.uonline.userid, request.uonline.location_id
+	request.uonline.players_list = tmpUsers
+).asyncMiddleware()
+
+
+fetchMonstersNearby = ((request, response) ->
+	tmpMonsters = lib.game.getNearbyMonsters.sync null, dbConnection, request.uonline.location_id
+	request.uonline.monsters_list = tmpMonsters
+).asyncMiddleware()
+
+
+fetchStats = ((request, response) ->
+	chars = lib.game.getUserCharacters.sync null, dbConnection, request.uonline.userid
+	for i of chars
+		request.uonline[i] = chars[i]
+).asyncMiddleware()
+
+
+fetchBattleGroups = ((request, response) ->
+	if request.uonline.fight_mode
+		request.uonline.participants = lib.game.getBattleParticipants.sync null,
+			dbConnection, request.uonline.userid
+		request.uonline.our_side = request.uonline.participants.find(
+			(p) -> p.kind=='user' && p.id==request.uonline.userid).side
 ).asyncMiddleware()
 
 

	if status >= 500 and status <= 600 then color = chalk.red
	return color(status)
morgan.token 'uu', (req, res) ->
	name = req.uonline?.username or req.uonline?.legacyOpts?.username or '-'
	return chalk.gray(name)
app.use morgan ":remote-addr :uu  :coloredStatus :method :url  #{chalk.gray '":user-agent"'}  :response-time ms"



app.get '/game/',
	mustBeAuthed,
	fetchLocation, fetchArea, fetchUsersNearby, fetchMonstersNearby,
	fetchFightMode, fetchStats, fetchBattleGroups,
	setInstance('game'), render('game')


app.get '/inventory/',
