2014-11-24 47 views
1

對於一個學校項目,我創建了一個有分數系統的遊戲,並且我想創建某種排行榜。一旦完成,教師將把它上傳到共享服務器,其他學生可以下載該遊戲的副本,但不幸的是,學生無法保存到該服務器;如果可以的話,排行榜將是小菜一碟。最多隻有幾百個分數來跟蹤,所有電腦都可以訪問互聯網。在Python中爲離線遊戲創建排行榜

我對服務器或託管知之甚少,並且我不知道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
  • 理想的情況下,該解決方案將只是一些代碼外部的比賽,讓我只想讓他們已經完成
  • 後,用戶將運行可執行文件
  • 我知道這聽起來不是很安全 - 這是不是 - 但沒關係,它並不需要是hackproof
+2

粘貼一些代碼! – 2014-11-24 20:24:35

+2

Python可以做到這一點,但請讓你的問題更具體,以便它可以回答。 – Garrett 2014-11-24 20:29:10

+0

我已經更新了它,所以你可以看到遊戲會吐出什麼樣的信息。此外,還有一個模擬排行榜 – JCK 2014-11-24 20:31:49

回答

1

最簡單的可能是隻使用MongoDB的或某事(蒙戈DB是一個nosql類型的數據庫,允許您輕鬆地保存字典數據...)

您可以使用免費的帳戶https://mongolab.com(taht應該給你足夠的空間)

(需要pymongo以及easy_install pymongo

,那麼你可以簡單地保存記錄有

from pymongo import MongoClient 
uri = "mongodb://test1:[email protected]: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 doesnt 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"}) 
from pymongo import DESCENDING 
#get a list of high scores (from best to worst) 
print list(my_scores.find().sort("score",DESCENDING)) 

如果你想測試系統,那些證書將會真正起作用(請記住我已經加了幾次leeroy)

+0

這看起來像我可能會用到的東西,謝謝!對不起,如果問題太大,我還是新手。 – JCK 2014-11-24 20:57:21

+0

作品完美,謝謝! – JCK 2014-11-26 04:00:17