Giter Site home page Giter Site logo

Comments (4)

marwanhilmi avatar marwanhilmi commented on August 16, 2024 1

A lot of the code here is indeed duplicated but there may be subtle differences. I believe the initial implementation of the single player server was an experiment and we had just copy-pasted the code but would make sense to convert into a shared library.

from duelyst.

willroberts avatar willroberts commented on August 16, 2024 1

I wrote a Python script to diff these automatically:

from typing import Dict, List

# Identifies CoffeeScript functions through signature and indentation.
def find_functions(filename: str) -> Dict[str, List[str]]:
    start_index = -1
    functions: Dict[str, List[str]] = dict()

    with open(filename, 'r') as f:
        lines = f.readlines()

    for i in range(0, len(lines)):
        line = lines[i]

        # Ignore comments and lines starting with whitespace.
        if line[0] in ('#', '\t', ' '): continue

        # Save the starting line for function declarations.
        if '->' in line and ' = (' in line:
            start_index = i
            continue

        # Don't try to find the end of a function before one begins.
        if start_index == -1: continue

        # Something new has started at the top level; save and reset.
        functions[lines[start_index].rstrip('\n')] = lines[start_index:i]
        start_index = -1

    return functions

# Returns True if the provided lists have identical contents.
def diff_function(a: List[str], b: List[str]) -> bool:
    if len(a) != len(b):
        return False
    for i in range(len(a)):
        if a[i] != b[i]:
            return False
    return True

if __name__ == '__main__':
    game = find_functions('game.coffee')
    sp = find_functions('single_player.coffee')
    for f in game.keys() & sp.keys():
        if diff_function(game[f], sp[f]):
            print('Function with declaration {} is duplicated!'.format(f))

The results are:

Function with declaration afterGameOver = (gameId, gameSession, mouseAndUIEvents) -> is duplicated!
Function with declaration destroyGameSessionIfNoConnectionsLeft = (gameId,persist=false)-> is duplicated!
Function with declaration dnsHealthCheck = () -> is duplicated!
Function with declaration flushSpectatorNetworkEventBuffer = (gameId) -> is duplicated!
Function with declaration getConnectedSpectatorsDataForGamePlayer = (gameId,playerId)-> is duplicated!
Function with declaration initGameSession = (gameId,onComplete) -> is duplicated!
Function with declaration initSpectatorGameSession = (gameId)-> is duplicated!
Function with declaration onGameEvent = (eventData) -> is duplicated!
Function with declaration onGamePlayerJoin = (requestData) -> is duplicated!
Function with declaration onGameSpectatorJoin = (requestData) -> is duplicated!
Function with declaration onGameTimeTick = (gameId) -> is duplicated!
Function with declaration onInvalidAction = (event) -> is duplicated!
Function with declaration onSpectatorDelayedGameTick = (gameId) -> is duplicated!
Function with declaration onStep = (event) -> is duplicated!
Function with declaration playerLeaveGameIfNeeded = (socket, silent=false) -> is duplicated!
Function with declaration restartSpectatorDelayedGameInterval = (gameId) -> is duplicated!
Function with declaration restartTurnTimer = (gameId) -> is duplicated!
Function with declaration saveGameCount = (gameCount) -> is duplicated!
Function with declaration savePlayerCount = (playerCount) -> is duplicated!
Function with declaration shutdown = () -> is duplicated!
Function with declaration spectatorLeaveGameIfNeeded = (socket) -> is duplicated!
Function with declaration stopSpectatorDelayedGameInterval = (gameId) -> is duplicated!
Function with declaration stopTurnTimer = (gameId) -> is duplicated!
Function with declaration tearDownSpectateSystemsIfNoSpectatorsLeft = (gameId)-> is duplicated!

from duelyst.

willroberts avatar willroberts commented on August 16, 2024

Definitely! I'll diff each of these one at a time and see what we can do.

from duelyst.

willroberts avatar willroberts commented on August 16, 2024

Another challenge is the reliance on global vars, specifically these two:

Functions accessing these (nearly all of the above) would need to be refactored or remain duplicated.

I made some progress on this work here: willroberts/duelyst@main...willroberts:duelyst:server-dedupe

Given the io/games access challenges I'll hold off on this for now.

from duelyst.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.