python - 用 Python 创建离线游戏排行榜

标签 python leaderboard

对于一个学校项目,我正在创建一个具有评分系统的游戏,并且我想创建某种排行榜。完成后,老师会将其上传到共享服务器,其他学生可以在其中下载游戏的副本,但不幸的是学生无法保存到该服务器;如果可以的话,排行榜将是小菜一碟。最多可以跟踪几百个分数,并且所有计算机都可以访问互联网。

我对服务器或托管不太了解,也不了解java、html或Web开发中常用的任何其他语言,因此其他相关问题并没有真正的帮助。我的游戏将得分信息打印到文本文件中,但从那里我不知道如何将其获取到每个人都可以访问的在线位置。

有没有办法只用Python来完成这样的任务?

这里有用于在获得分数后更新排行榜文件的代码(假设它只是一个文本文件)。这假设我在同一个地方有一份排行榜和分数文件的副本。

这是我的模拟排行榜 (Leaderboards.txt) 的格式:

Leaderboards

1) JOE  10001
2) ANA  10000
3) JAK  8400
4) AAA  4000
5) ABC  3999

这是日志文件将打印的内容 - 缩写和分数 (log.txt):

ABC
3999

代码(适用于 python 2.7 和 3.3):

def extract_log_info(log_file = "log.txt"):
    with open(log_file, 'r') as log_info:
        new_name, new_score = [i.strip('\n') for i in log_info.readlines()[:2]]

    new_score = int(new_score)
    return new_name, new_score

def update_leaderboards(new_name, new_score, lb_file = "Leaderboards.txt"):
    cur_index = None
    with open(lb_file, 'r') as lb_info:
        lb_lines = lb_info.readlines()
        lb_lines_cp = list(lb_lines) # Make a copy for iterating over
        for line in lb_lines_cp:
            if 'Leaderboards' in line or line == '\n':
                continue

            # Now we're at the numbers
            position, name, score = [ i for i in line.split() ]

            if new_score > int(score):
                cur_index = lb_lines.index(line)
                cur_place = int(position.strip(')'))
                break

        # If you have reached the bottom of the leaderboard, and there
        # are no scores lower than yours
        if cur_index is None:
            # last_place essentially gets the number of entries thus far
            last_place = int(lb_lines[-1].split()[0].strip(')'))
            entry = "{}) {}\t{}\n".format((last_place+1), new_name, new_score)
            lb_lines.append(entry)
        else: # You've found a score you've beaten
            entry = "{}) {}\t{}\n".format(cur_place, new_name, new_score)
            lb_lines.insert(cur_index, entry)

            lb_lines_cp = list(lb_lines) # Make a copy for iterating over
            for line in lb_lines_cp[cur_index+1:]:
                position, entry_info = line.split(')', 1)
                new_entry_info = str(int(position)+1) + ')' + entry_info
                lb_lines[lb_lines.index(line)] = new_entry_info

    with open(lb_file, 'w') as lb_file_o:
        lb_file_o.writelines(lb_lines)


if __name__ == '__main__':
    name, score = extract_log_info()
    update_leaderboards(name, score)

更多信息:

  • 分数将低于 1 000 000
  • 理想情况下,解决方案只是游戏外部的一些代码,这样我就可以制作一个用户完成后运行的可执行文件
  • 我知道这听起来不太安全 - 事实并非如此 - 但没关系,它不需要防黑客

最佳答案

最简单的可能就是使用 MongoDB 或者其他东西(MongoDB 是一个 NoSQL 类型的数据库,可以让你轻松保存字典数据...)

您可以使用免费帐户https://mongolab.com (这应该给你足够的空间)。

您还需要 pymongo pip install pymongo

然后您可以简单地在那里保存记录:

from pymongo import MongoClient, DESCENDING

uri = "mongodb://test1:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="582c3d2b2c69183c2b686d69616168763537363f3734393a763b3735" rel="noreferrer noopener nofollow">[email protected]</a>:51990/joran1"
my_db_cli = MongoClient(uri)
db = my_db_cli.joran1  # select the database ... 

my_scores = db.scores  # this will be created if it doesn't exist!
# add a new score
my_scores.insert({"user_name": "Leeeeroy Jenkins", "score": 124, "time": "11/24/2014 13:43:22"})
my_scores.insert({"user_name": "bob smith", "score": 88, "time": "11/24/2014 13:43:22"})

# get a list of high scores (from best to worst)
print(list(my_scores.find().sort("score", DESCENDING)))

如果您想测试系统,这些凭据实际上会起作用(请记住我添加了几次 leeroy)。

关于python - 用 Python 创建离线游戏排行榜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27113495/

相关文章:

python - 我可以看到 Python 装饰器对我的代码做了什么吗?

python - 当使用 Python PuLP 进行线性程序时,有一种方法可以为同一变量指定两个约束

ios - Game Center 沙盒排行榜不会显示本地分数以外的分数

mysql - 使用 Haxe 将 SDateTime 存储在 SQL 数据库中

ios - 如何更新游戏中心排行榜中的分数,即使它是较低的游戏中心

java - 高分排行榜系统问题

python - 接受后使用访问 token 吗?

python - 强制 Django 使用 32 位 Python

python - 将多个 Parquet 文件加载到数据框中进行分析

android - 来自谷歌排行榜的前 5 名