-- args file id
DROP FUNCTION update_opoints(INT);
CREATE OR REPLACE FUNCTION update_opoints(INT) RETURNS REAL
AS $$
  # updating objectpoints column
  #set_points = plpy.prepare("UPDATE object SET voteavg = (SELECT avg(vote) FROM objectvote WHERE objectvote.id = $1) WHERE objectid = $2 RETURNING voteavg", ["int", "int"])
  #avgs = plpy.execute(set_points, [args[0], args[0]])

  set_opoints = plpy.prepare("UPDATE object SET votepoints = (SELECT SIGN(COALESCE(SUM(cast(vote AS REAL)-2.5),1))*LN(ABS(COALESCE(SUM(cast(vote AS REAL)-2.5),0))+1) FROM objectvote WHERE objectvote.id = $1), voteavg = (SELECT avg(vote) FROM objectvote WHERE objectvote.id = $2) WHERE objectid = $3 RETURNING voteavg", ["int", "int", "int"])
  avgs = plpy.execute(set_opoints, [args[0], args[0], args[0]])

  return avgs[0]["voteavg"]
$$ LANGUAGE plpythonu;
