Skip to content
Permalink
Browse files

Now we use MongoDB!

  • Loading branch information...
YogurtTheHorse committed Sep 18, 2016
1 parent 252e5ea commit fcd9e6eb7e9647e4b5c1c6a3e77ad731fcbf22f0

This file was deleted.

@@ -0,0 +1,42 @@
from pymongo import MongoClient
from databasemanager.leaderboard_model import get_model as get_leaderboard_model
from databasemanager.variable_model import get_model as get_variable_model
from databasemanager.lists_model import get_model as get_lists_model

client = MongoClient()
db = client['rogbot']

Leaderboards = get_leaderboard_model(db)
Variables = get_variable_model(db)
Lists = get_lists_model(db)

ROOMS_TABLE = 'rooms'
KILLS_TABLE = 'kills'
GNOME_TABLE = 'gnome'
ROULETTE_TABLE = 'roulette'
RATE_TABLE = 'rate'
DOCTOR_TABLE = 'doctor'

def get_variable(name, def_val=None):
return Variables.get_value(name, def_val)

def set_variable(name, value):
Variables.set_value(name, value)

def clear_list(name):
Lists.clear_list(name)

def remove_from_list(name, val):
Lists.remove_from_list(name, val)

def add_to_list(name, value, force=False):
Lists.add_to_list(name, val, force)

def get_list(name):
return Lists.get_list(name)

def add_to_leaderboard(user, score, leaderboard_name='rooms'):
Leaderboards.add_to_leaderboard(user, score, leaderboard_name)

def get_leaderboard(leaderboard_name='rooms', count=10):
return Leaderboards.get_leaderboard(leaderboard_name, count)
@@ -0,0 +1,47 @@
from mongothon import Schema
from mongothon import create_model
from mongothon.validators import one_of

ROOMS_TABLE = 'rooms'
KILLS_TABLE = 'kills'
GNOME_TABLE = 'gnome'
ROULETTE_TABLE = 'roulette'
RATE_TABLE = 'rate'
DOCTOR_TABLE = 'doctor'

TABLES = [ ROOMS_TABLE, KILLS_TABLE, GNOME_TABLE, ROULETTE_TABLE, RATE_TABLE, DOCTOR_TABLE ]

score_schema = Schema({
'uid': {'type': str, 'required': True},
'name': {'type': str, 'required': True},
'score': {'type': int, 'required': True},
'leaderboard': {'type': str, 'validates': one_of(*TABLES)},
'death_reason': {'type': str }
})

def get_model(db):
Scores = create_model(score_schema, db['scores'])

@Scores.class_method
def get_leaderboard(cls, leaderboard, count=10):
return list(cls.find({"leaderboard": leaderboard}))[:count]

@Scores.class_method
def add_to_leaderboard(cls, user, score, leaderboard_name='rooms'):
name = user.name
if user.pet:
pet = user.get_pet()
name += ' и {0} {1}'.format(pet.name, pet.real_name)

doc = {
'uid': user.uid,
'name': name,
'score': score,
'leaderboard': leaderboard_name
}
if hasattr(user, 'death_reason'):
doc['death_reason'] = user.death_reason

cls.insert(doc)

return Scores
@@ -0,0 +1,49 @@
from mongothon.model import NotFoundException

from mongothon import Schema
from mongothon import create_model

list_schema = Schema({
'name': {"type": str, "required": True},
'value': {"type": list, "required": True }
})

def get_model(db):
Lists = create_model(list_schema, db['vars'])

@Lists.class_method
def get_list(cls, name):
try:
return cls.find_one({'name': name})['value']
except:
return [ ]

@Lists.class_method
def set_list(cls, name, value):
try:
var = cls.find_one({'name': name})
var.update({'value': value})
var.save()
except:
var = Lists({'name': name, 'value': value})
var.save()

@Lists.class_method
def clear_list(cls, name):
cls.set_list(name, [ ])

@Lists.class_method
def remove_from_list(cls, name, val):
lst = cls.get_list(cls, name)
lst.remove(val)
cls.set_list(name, lst)

@Lists.class_method
def add_to_list(cls, name, val, force=True):
lst = cls.get_list(cls, name)

if force or val not in lst:
lst.append(val)
cls.set_list(name, lst)

return Lists
@@ -0,0 +1,31 @@
from mongothon.model import NotFoundException

from mongothon import Schema
from mongothon import create_model

var_schema = Schema({
'name': {"type": str, "required": True},
'value': {"type": object, "required": True }
})

def get_model(db):
Variables = create_model(var_schema, db['vars'])

@Variables.class_method
def get_value(cls, name, def_val=None):
try:
return cls.find_one({'name': name})['value']
except:
return def_val

@Variables.class_method
def set_value(cls, name, value):
try:
var = cls.find_one({'name': name})
var.update({'value': value})
var.save()
except:
var = Variables({'name': name, 'value': value})
var.save()

return Variables
@@ -4,4 +4,6 @@ vk_api
tinydb
tinyrecord
codecov
tweepy
tweepy
mongothon3
pymongo
@@ -17,7 +17,7 @@ def add_to_list(tornament_name, uid):

def get_tornament(tid):
def_val = get_tornament_dict(tid, [ ])
return databasemanager.get_variable(tid, def_val=def_val, table=databasemanager.TORNAMENTS_TABLE)
return databasemanager.get_variable(tid, def_val=def_val)

def get_tornament_dict(tid, uids):
names = [ ]
@@ -34,7 +34,7 @@ def get_tornament_dict(tid, uids):

def save_tornament(tid, uids):
val = get_tornament_dict(tid, uids)
databasemanager.set_variable(tid, val, table=databasemanager.TORNAMENTS_TABLE)
databasemanager.set_variable(tid, val)

def start_tornament(tornament_name, reply):
uids = databasemanager.get_list(tornament_name)

0 comments on commit fcd9e6e

Please sign in to comment.
You can’t perform that action at this time.