-- args file id
DROP FUNCTION update_points(INT);
CREATE OR REPLACE FUNCTION update_points(INT) RETURNS REAL
AS $$
  # updating filepoints column
  set_filepoints = plpy.prepare("UPDATE file SET points = (SELECT SIGN(COALESCE(SUM(cast(vote AS REAL)-2.5),1))*LN(ABS(COALESCE(SUM(cast(vote AS REAL)-2.5),0))+1) FROM filevote WHERE filevote.fileid = $1), voteavg = (SELECT avg(vote) FROM filevote WHERE filevote.fileid = $2) WHERE id = $3 RETURNING voteavg", ["int", "int", "int"])
  avgs = plpy.execute(set_filepoints, [args[0], args[0], args[0]])

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