/
view_characters.py
94 lines (74 loc) · 3.49 KB
/
view_characters.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
from flask import request
from utils import ProtocolTypes, ResultCodes, checkSessionId, checkContainKeys, commitData
import json
from database import db_session
from sqlalchemy import exc
from models import Character
def createCharacter():
result = dict(
type=ProtocolTypes.CreateCharacter,
result=ResultCodes.Success)
if request.form['data']:
got_data = json.loads(request.form['data'])
from_keys = ['session_id', 'character']
if checkContainKeys(from_keys, got_data):
result['result'], got_user = checkSessionId(got_data['session_id'])
if got_user:
got_character = Character.query.filter_by(user_id=got_user.id).first()
if got_character:
result['result'] = ResultCodes.DataExist
else:
got_character = got_data['character']
got_user.name = got_character['name']
db_session.add(got_user)
user_character = Character(got_user.id, got_character['name'])
user_character.body_type = got_character['body_type']
user_character.cloak_type = got_character['cloak_type']
user_character.color_r = got_character['color_r']
user_character.color_g = got_character['color_g']
user_character.color_b = got_character['color_b']
user_character.face_type = got_character['face_type']
user_character.gender = got_character['gender']
user_character.hair_type = got_character['hair_type']
user_character.weapon_type = got_character['weapon_type']
db_session.add(user_character)
result['result'] = commitData()
else:
result['result'] = ResultCodes.InputParamError
else:
result['result'] = ResultCodes.AccessError
return str(json.dumps(result))
createCharacter.methods = ['POST']
def getCharacter():
result = dict(
type=ProtocolTypes.GetCharacter,
result=ResultCodes.Success)
if request.form['data']:
got_data = json.loads(request.form['data'])
from_keys = ['session_id']
if checkContainKeys(from_keys, got_data):
result['result'], got_user = checkSessionId(got_data['session_id'])
if got_user:
find_character = Character.query.filter_by(user_id=got_user.id).first()
if find_character:
got_character = dict(
name=find_character.name,
body_type=find_character.body_type,
cloak_type=find_character.cloak_type,
color_r=find_character.color_r,
color_g=find_character.color_g,
color_b=find_character.color_b,
face_type=find_character.face_type,
gender=find_character.gender,
hair_type=find_character.hair_type,
weapon_type=find_character.weapon_type)
result['character'] = json.dumps(got_character)
else:
result['result'] = ResultCodes.NoData
else:
result['result'] = ResultCodes.InputParamError
else:
result['result'] = ResultCodes.AccessError
return str(json.dumps(result))
getCharacter.methods = ['POST']