-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
72 lines (52 loc) · 3.65 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Server script that compares the received number with the correct one.
Usage: server.py
Author: Andres J. Sanchez-Fernandez
Email: [email protected]
Date: 2024-03-21
Version: v1
"""
import sys, Ice # Import the sys and Ice libraries (Ice runtime).
import NumberGuessingGame # Import the NumberGuessingGame module (proxies and skeletons).
import argparse # Import the argparse library for cmd arguments.
import random # Import the random library for random number generation.
class GameI(NumberGuessingGame.Game):
"""
Class that implements the 'Game' interface.
This class inherits from the 'Game' class in the 'NumberGuessingGame' module.
Methods:
checkGuess: Method that compares the received number with the correct one.
"""
def __init__(self):
self.target_number = random.randint(1, 100)
# print(self.target_number)
def checkGuess(self, guess, current=None):
"""Method that compares the received number with the correct one."""
if guess > self.target_number: # Check if the guess is higher than the target number.
return 'Your guess is HIGHER than the target number!'
elif guess < self.target_number: # Check if the guess is lower than the target number.
return 'Your guess is LOWER than the target number!'
else: # The guess is correct.
print(f'You won! The correct number was {self.target_number} indeed.')
return 'Correct'
def main() -> bool:
"""
Main function.
Returns:
A boolean indicating the success of the process.
"""
port = 10000 # Set the default port number.
print(f'Listening port: {port}') # Print the port number.
with Ice.initialize(sys.argv) as communicator: # Initialize the Ice run time and create a communicator.
adapter = communicator.createObjectAdapterWithEndpoints( # Create an object adapter with the name 'NumberGuessingGame' and an
'NumberGuessingGameAdapter', f'default -p {port}' # endpoint with the default protocol and the specified port number.
)
servant = GameI() # Create an instance of the 'GameI' class.
proxy = adapter.add(servant, communicator.stringToIdentity('NumberGuessingGame')) # Add the 'servant' instance to the adapter with the identity 'NumberGuessingGame'.
adapter.activate() # Activate the adapter to make the servant available for incoming requests.
communicator.waitForShutdown() # Wait for the communicator to be destroyed.
return 0
if __name__ == '__main__':
sys.exit(main()) # Call the main function and exit with the returned status code.