@@ -39,10 +39,10 @@ def get_mysqlclient_connection(autocommit: bool = True) -> MySQLdb.Connection:
39
39
def mysqlclient_recreate_table () -> None :
40
40
with get_mysqlclient_connection () as connection :
41
41
with connection .cursor () as cur :
42
- cur .execute ("DROP TABLE IF EXISTS player ;" )
42
+ cur .execute ("DROP TABLE IF EXISTS players ;" )
43
43
cur .execute (
44
44
"""
45
- CREATE TABLE player (
45
+ CREATE TABLE players (
46
46
`id` VARCHAR(36),
47
47
`coins` INTEGER,
48
48
`goods` INTEGER, PRIMARY KEY (`id`)
@@ -52,25 +52,25 @@ def mysqlclient_recreate_table() -> None:
52
52
53
53
54
54
def create_player (cursor : Cursor , player : tuple ) -> None :
55
- cursor .execute ("INSERT INTO player (id, coins, goods) VALUES (%s, %s, %s)" , player )
55
+ cursor .execute ("INSERT INTO players (id, coins, goods) VALUES (%s, %s, %s)" , player )
56
56
57
57
58
58
def get_player (cursor : Cursor , player_id : str ) -> tuple :
59
- cursor .execute ("SELECT id, coins, goods FROM player WHERE id = %s" , (player_id ,))
59
+ cursor .execute ("SELECT id, coins, goods FROM players WHERE id = %s" , (player_id ,))
60
60
return cursor .fetchone ()
61
61
62
62
63
63
def get_players_with_limit (cursor : Cursor , limit : int ) -> list [tuple ]:
64
- cursor .execute ("SELECT id, coins, goods FROM player LIMIT %s" , (limit ,))
64
+ cursor .execute ("SELECT id, coins, goods FROM players LIMIT %s" , (limit ,))
65
65
return cursor .fetchall ()
66
66
67
67
68
68
def bulk_create_player (cursor : Cursor , players : list [tuple ]) -> None :
69
- cursor .executemany ("INSERT INTO player (id, coins, goods) VALUES (%s, %s, %s)" , players )
69
+ cursor .executemany ("INSERT INTO players (id, coins, goods) VALUES (%s, %s, %s)" , players )
70
70
71
71
72
72
def get_count (cursor : Cursor ) -> None :
73
- cursor .execute ("SELECT count(*) FROM player " )
73
+ cursor .execute ("SELECT count(*) FROM players " )
74
74
return cursor .fetchone ()[0 ]
75
75
76
76
@@ -111,22 +111,22 @@ def simple_example() -> None:
111
111
def trade (connection : MySQLdb .Connection , sell_id : str , buy_id : str , amount : int , price : int ) -> None :
112
112
# This function should be called in a transaction.
113
113
with connection .cursor () as cursor :
114
- cursor .execute ("SELECT coins, goods FROM player WHERE id = %s FOR UPDATE" , (sell_id ,))
114
+ cursor .execute ("SELECT coins, goods FROM players WHERE id = %s FOR UPDATE" , (sell_id ,))
115
115
_ , sell_goods = cursor .fetchone ()
116
116
if sell_goods < amount :
117
117
print (f"sell player { sell_id } goods not enough" )
118
118
connection .rollback ()
119
119
return
120
120
121
- cursor .execute ("SELECT coins, goods FROM player WHERE id = %s FOR UPDATE" , (buy_id ,))
121
+ cursor .execute ("SELECT coins, goods FROM players WHERE id = %s FOR UPDATE" , (buy_id ,))
122
122
buy_coins , _ = cursor .fetchone ()
123
123
if buy_coins < price :
124
124
print (f"buy player { buy_id } coins not enough" )
125
125
connection .rollback ()
126
126
return
127
127
128
128
try :
129
- update_player_sql = "UPDATE player set goods = goods + %s, coins = coins + %s WHERE id = %s"
129
+ update_player_sql = "UPDATE players set goods = goods + %s, coins = coins + %s WHERE id = %s"
130
130
# deduct the goods of seller, and raise his/her the coins
131
131
cursor .execute (update_player_sql , (- amount , price , sell_id ))
132
132
# deduct the coins of buyer, and raise his/her the goods
0 commit comments