from db import conn
import json


def first_edit(wpusername):
    __display__ = "Your First Edit"
    query = "SELECT score FROM edits WHERE username=%s"
    cur = conn.cursor()
    cur.execute(query, [wpusername])
    meetsreqs = (len(cur.fetchall()) == 1)
    cur.close()
    return meetsreqs


def always(wpusername):
    __display__ = "Free Achievement"
    return True


ACH_FUNCS = {
    first_edit: 'Your First Edit'
}

def check_all_achievements(wpusername):
    for i in ACH_FUNCS:
        check(i, wpusername)


def check(f, wpusername):
    result = f(wpusername)
    if result:
        name = ACH_FUNCS[f]
        query = "INSERT INTO achievements (uid, name) VALUES ((SELECT uid FROM users WHERE wp_username=%s), %s)"
        cur = conn.cursor()
        cur.execute(query, (wpusername, name))
        conn.commit()

@@ -289,16 +289,413 @@
 tsreqs%0A%0A
+def onehundred_points(wpusername):%0A    __display__ = %22One Hundred Points%22%0A    cur.execute(%22SELECT sum(score) FROM edits WHERE username=%25s%22, (wpusername,))%0A    row = cur.fetchone()%0A    cur.execute(%22WITH SELECT uid AS uid FROM users WHERE wp_username=%25s%5C%0A            SELECT 1 FROM achievements WHERE uid=uid AND name=%25s%22, (wpusername, __display__))%0A    return row%5B0%5D %3E 100 && cur.fetchone() is None%0A
 %0Adef alw
@@ -813,16 +813,61 @@
 st Edit'
+,%0A    onehundred_points: 'One Hundred Points'
 %0A%7D%0A%0Adef 

implement 100 point achievement