Public feed #531226

Stuff

public text v1 · immutable
Raw Edit
·published 2007-06-02 12:24 UTC
rendered paste body

/*---------------------------------------------------------
   Name: Make the table if it doesn't exist
---------------------------------------------------------*/
if ( !sql.TableExists( "sui_ratings" ) ) then

	sql.Query( "CREATE TABLE IF NOT EXISTS sui_ratings ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, target INTEGER, rater INTEGER, rating INTEGER );" )
	sql.Query( "CREATE INDEX IDX_RATINGS_TARGET ON sui_ratings ( target DESC )" )
	Msg("SQL: Created sui_ratings table!\n")
	
end


/*---------------------------------------------------------
   Name: ValidRatings - only these ratings are valid!
---------------------------------------------------------*/
local ValidRatings = { "naughty", "smile", "love", "artistic", "gold_star", "builder", "gay", "informative", "friendly", "lol", "curvey", "best_landvehicle", "best_airvehicle", "stunter", "god" }
local GetRatings = { "thinks you suck", "gave you a smile", "loves you, alot", "rated you artistic", "gave you a gold star", "rated you a builder", "thinks your a flaming faggot", "finds you very informative", "rated you a friendly guy", "lol's at you", "likes your curves, oh yes", "rated you to have the coolest car", "loves your airplane", "is impressed by your stunter", "worships you, your his god" }
local GiveRatings = { "a very bad rating", "a smile", "some love", "an artistic rating", "a gold star", "a builder rating", "a gay rating", "a informative rating", "a friendly rating", "a lol", "a curvey rating", "a best landvehicle rating", "a best airvehicle rating", "a stunter rating", "a faint idea of how much you worship him" }

local function GetRatingID( name )

	for k, v in pairs( ValidRatings ) do
		if ( name == v ) then return k end
	end
	
	return false

end

local function GetRatingName( id )

	for k, v in pairs( ValidRatings ) do
		if ( id == k ) then return GetRatings[v] end
	end
	
	return false

end

local function GiveRatingName( id )

	for k, v in pairs( ValidRatings ) do
		if ( id == k ) then return GiveRatings[v] end
	end
	
	return false

end
/*---------------------------------------------------------
   Name: Update the player's networkvars based on the DB
---------------------------------------------------------*/
local function UpdatePlayerRatings( ply )

	local result = sql.Query( "SELECT rating, count(*) as cnt FROM sui_ratings WHERE target = "..ply:UniqueID().." GROUP BY rating " )
	
	if ( !result ) then return end
	
	for id, row in pairs( result ) do
	
		ply:SetNetworkedInt( "SuiRating."..ValidRatings[ tonumber( row['rating'] ) ], row['cnt'] )
	
	end

end
	
/*---------------------------------------------------------
   Name: CCRateUser
---------------------------------------------------------*/
local function CCRateUser( player, command, arguments )

	local Rater 	= player
	local Target 	= Entity( tonumber( arguments[1] ) )
	local Rating	= arguments[2]
	
	// Don't rate non players
	if ( !Target:IsPlayer() ) then return end
	
	// Don't rate self
	if ( Rater == Target ) then return end
	
	local RatingID = GetRatingID( Rating )
	local RaterID = Rater:UniqueID()
	local TargetID = Target:UniqueID()
	
	// Rating isn't valid
	if (!RatingID) then return end
	
	// When was the last time this player rated this player
	// Only let them rate each other every 60 seconds
	Target.RatingTimers = Target.RatingTimers or {}
	if ( Target.RatingTimers[ RaterID ] && Target.RatingTimers[ RaterID ] > CurTime() - 60 ) then
	
		Rater:ChatPrint( "Please wait before rating ".. Target:Nick() .." again.\n" );
		return
		
	end
	
	Target.RatingTimers[ RaterID ] = CurTime()
	
	// Tell the target that they have been rated
	Target:ChatPrint( Rater:Nick() .. " " .. GetRatingName(RatingID) .. ".\n" );
		
	// Let the rater know that their vote was succesfull
	Rater:ChatPrint( "Gave ".. Target:Nick() .. " " .. GiveRatingName(RatingID) .. "'.\n" );
	
	sql.Query( "INSERT INTO sui_ratings ( target, rater, rating ) VALUES ( "..TargetID..", "..RaterID..", "..RatingID.." )" )

	// We changed something so update the networked vars
	UpdatePlayerRatings( Target )
	
end

concommand.Add( "sui_rateuser", CCRateUser )


/*---------------------------------------------------------
   When the player joins the server we 
   need to restore the NetworkedInt's
---------------------------------------------------------*/
local function PlayerRatingsRestore( ply )

	UpdatePlayerRatings( ply )

end
hook.Add( "PlayerInitialSpawn", "PlayerRatingsRestore", PlayerRatingsRestore )