Skip to content

Commit 7d7d930

Browse files
committed
Handle database errors during duckhunt.befriend and duckhunt.bang
1 parent 9bfdbca commit 7d7d930

1 file changed

Lines changed: 33 additions & 22 deletions

File tree

plugins/duckhunt.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from time import time
55

66
from sqlalchemy import Table, Column, String, Integer, PrimaryKeyConstraint, desc, Boolean
7+
from sqlalchemy.exc import DatabaseError
78
from sqlalchemy.sql import select
89

910
from cloudbot import hook
@@ -318,17 +319,22 @@ def bang(nick, chan, message, db, conn, notice):
318319
else:
319320
message(out)
320321
game_status[network][chan]['duck_status'] = 2
321-
score = db.execute(select([table.c.shot]) \
322-
.where(table.c.network == conn.name) \
323-
.where(table.c.chan == chan.lower()) \
324-
.where(table.c.name == nick.lower())).fetchone()
325-
if score:
326-
score = score[0]
327-
score += 1
328-
dbupdate(nick, chan, db, conn, score, 0)
329-
else:
330-
score = 1
331-
dbadd_entry(nick, chan, db, conn, score, 0)
322+
try:
323+
score = db.execute(select([table.c.shot])
324+
.where(table.c.network == conn.name)
325+
.where(table.c.chan == chan.lower())
326+
.where(table.c.name == nick.lower())).fetchone()
327+
if score:
328+
score = score[0]
329+
score += 1
330+
dbupdate(nick, chan, db, conn, score, 0)
331+
else:
332+
score = 1
333+
dbadd_entry(nick, chan, db, conn, score, 0)
334+
except DatabaseError:
335+
game_status[network][chan]['duck_status'] = 1
336+
raise
337+
332338
timer = "{:.3f}".format(shoot - deploy)
333339
duck = "duck" if score == 1 else "ducks"
334340
message("{} you shot a duck in {} seconds! You have killed {} {} in {}.".format(nick, timer, score, duck, chan))
@@ -380,17 +386,22 @@ def befriend(nick, chan, message, db, conn, notice):
380386
message(out)
381387

382388
game_status[network][chan]['duck_status'] = 2
383-
score = db.execute(select([table.c.befriend]) \
384-
.where(table.c.network == conn.name) \
385-
.where(table.c.chan == chan.lower()) \
386-
.where(table.c.name == nick.lower())).fetchone()
387-
if score:
388-
score = score[0]
389-
score += 1
390-
dbupdate(nick, chan, db, conn, 0, score)
391-
else:
392-
score = 1
393-
dbadd_entry(nick, chan, db, conn, 0, score)
389+
try:
390+
score = db.execute(select([table.c.befriend])
391+
.where(table.c.network == conn.name)
392+
.where(table.c.chan == chan.lower())
393+
.where(table.c.name == nick.lower())).fetchone()
394+
if score:
395+
score = score[0]
396+
score += 1
397+
dbupdate(nick, chan, db, conn, 0, score)
398+
else:
399+
score = 1
400+
dbadd_entry(nick, chan, db, conn, 0, score)
401+
except DatabaseError:
402+
game_status[network][chan]['duck_status'] = 1
403+
raise
404+
394405
duck = "duck" if score == 1 else "ducks"
395406
timer = "{:.3f}".format(shoot - deploy)
396407
message(

0 commit comments

Comments
 (0)