/
game.py
73 lines (68 loc) · 2.78 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from player import Player
from battle import Battle
from world import World
import pymysql
import config
class Game():
def __init__(self, userID):
self.player = None
dbinfo = config.getInfoToConnectDB()
self.connection = pymysql.connect(host = dbinfo['host'],
user = dbinfo['user_name'],
password = dbinfo['password'],
db = dbinfo['db_name'],
charset = 'utf8',
cursorclass = pymysql.cursors.DictCursor)
if self._exist(userID):
self.player = Player(userID)
self.Registered = True
else:
self.Registered = False
def _exist(self, id):
with self.connection.cursor() as cursor:
cursor.execute("SELECT count(*) FROM USER WHERE id = %s", (id))
recordNum = cursor.fetchone()['count(*)']
if recordNum == 0:
return False
else:
return True
def registUser(self, userID, userName):
with self.connection.cursor() as cursor:
name = userName
money = 1000
position_x = 200
position_y = 200
hp = 30
state = "WORLD"
battleID = None
sql = "INSERT INTO EQUIPMENT(equip_weapon, equip_armor) VALUES(1, 1)"
cursor.execute(sql)
equipID = cursor.lastrowid
sql = "INSERT INTO USER VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
values = (userID, name, money, position_x, position_y, hp, state, battleID, equipID)
print(values)
cursor.execute(sql, values)
self.connection.commit()
world = World(userID)
imageURI = world.getInitMap()
return {"worldImg": [imageURI], "text": ["あなたのセーブデータを作成しました!"]}
def step(self, text):
if self.player is not None:
if self.player.state == "BATTLE":
battle = Battle(self.player.userId)
reply = battle.battle()
if battle.isFinish == True:
world = World(self.player.userId)
imageURI = world.getInitMap()
reply['battlefinished'] = imageURI
return reply
elif self.player.state == "WORLD":
world = World(self.player.userId)
imageURI = world.move(text)
encount, enemyInfo = world.randomEncount()
if encount:
self.player.state = "BATTLE"
return {"img": [imageURI, enemyInfo["img"]], "text": [enemyInfo["text"]]}
return {"worldImg": [imageURI]}
else:
return {"text": ["still not implemented"]}