Open Games Server

User avatar
Kyran
Posts: 499
Joined: Sat Dec 31, 2011 5:19 pm
Location: Australia
Contact:

Open Games Server

Postby Kyran » Tue Nov 26, 2013 8:01 am

This is feb's server for Stratagus.

Code: Select all

# Open Games Server for stratagus
# Copyrighted by Francois Beerten 2003-2004
# Licenced under the GPL version 2. (http://www.gnu.org/copyleft/gpl.html)
# This software is provided without any waranty, use at your own risks.

import socket
import SocketServer
import string

# TODO
# - Multi thread protection of shared data OR asynccore system
# - create a list of compatible games when enumerating games to join
# - real parsing to detect message boundaries (socket.recv is not always one whole message)
# - security



HOST = ''                 # Symbolic name meaning the local host
PORT = 7775               # Arbitrary non-privileged port

class Game:
    def __init__(self):
        self.params = ['IP', '6660', 'FakeName', 'No Map', 'Players', 'Free']
        self.headers = ["<stratagus>","player", "game", "0.0"]
    def SetParams(self, params):
        self.params=params
    def ChangeParams(self, params):
        i=2
        for param in params:
            self.params[i]=param
            i=i+1
    def GetParams(self):
        return self.params

games = [Game()]

def MakeParamsString(params):
    str=""
    for p in params:
        str = str + p + "\n"
    return str


class MetaGameHandler(SocketServer.BaseRequestHandler):
    def setup(self):
        self.createdGame=None
    def AddGame(self, params,headers):
        global games
        self.createdGame=Game()
        # ip address is the source of the connection
        params[0] = self.client_address[0]
        self.createdGame.SetParams(params[:-1])
        self.createdGame.headers = headers
        games.append(self.createdGame)
        # self.request.send("OK\r\n") Not read in actual version of strat, dont send or it fills tcp queue and strat cant handle it

    def StartGame(self, params, headers):
        global games
        self.request.send("OK\r\n")

    def AbandonGame(self, params, headers):
        global games
        games.remove(self.createdGame)
        self.createdGame=None
        self.request.send("OK\r\n\r\n")

    def CreateStringAction(self, str):
        def StringAction(params, headers):
             self.request.send(str)
        return StringAction

    def NumberOfGames(self, params, headers):
        global games
        self.compatibleGames=[]
        for g in games:
            if(g.headers[2:3] == headers[2:3]):
                self.compatibleGames.append(g)
        ngames=len(self.compatibleGames)
        print "games:" , ngames
        reply = "%d\r\n%d\r\n%d\r\n1\r\n"%(ngames+1,ngames+1,ngames+1)
        print reply
        self.request.send(reply)

    def GameNumber(self, params, headers):
        gamenbr=int(params[0])-1
        game=self.compatibleGames[gamenbr]
        reply = ("OK\r\n" +
                 MakeParamsString(game.headers[1:]) + "toto\r\n" +
                 MakeParamsString(game.params))
        print reply
        self.request.send(reply)

    def ChangeGame(self, params, headers):
        self.createdGame.ChangeParams(params[0:3])
        # self.request.send("OK\r\n") Again not good for strat
    def handle(self):
        actions = {"Login": self.CreateStringAction("OK\r\n"),
	   "NumberOfGames": self.NumberOfGames,
	   "Logout":self.CreateStringAction("OK\n"),
	   "AddGame":self.AddGame,
	   "ChangeGame": self.ChangeGame,
	   "AbandonGame":self.AbandonGame,
	   "GameNumber":self.GameNumber,
	   "StartGame":self.StartGame}
        while 1:
            dataReceived = self.request.recv(1024)
            if not dataReceived:
                break
            dataReceived = dataReceived.split('\n')
            dataReceived = map(string.strip,dataReceived)
            print dataReceived
            command = dataReceived[4]
            headers = dataReceived[:3]
            params = dataReceived[5:]
            actions[command](params, headers)
            print "-\n"
    def finish(self):
        print "Connection closed"
        
metaserver = SocketServer.ThreadingTCPServer((HOST,PORT), MetaGameHandler)
metaserver.serve_forever()
04:27 jim4 why haven't you added wc1 support? this project sucks. i'm only going to use freecraft
05:06 jim4 finished wc1 support yet? i've been waiting for 6 years
05:10 jim4 new things scare me

Who is online

Users browsing this forum: No registered users and 3 guests